mt
 All Classes Files Functions Enumerations Groups Pages
transform.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2006 by Adolfo Rodriguez *
3  * adolfo.rodriguez@upc.edu *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
22 
23 // HEADER GUARD
24 #ifndef MT_TRANSFORM_H
25 #define MT_TRANSFORM_H
26 
27 // C++ STANDARD HEADERS
28 #include <iostream>
29 
30 // MT LIBRARY HEADERS
31 #include <mt/matrix3x3.h>
32 #include <mt/rotation.h>
33 #include <mt/scalar.h>
34 #include <mt/unit3.h>
35 #include <mt/point3.h>
36 
38 
39 namespace mt
40 {
41 
42 
44 
47 
79 
80 class Transform
81 {
82 public:
83 
84 // LIFECYCLE
85 
87  Transform();
88 
90  Transform(const Rotation& rotation,
91  const Point3& translation);
92 
93  // Compiler generated copy constructor is being used
94 
95  // Compiler generated destructor is being used
96 
97 
98 // OPERATORS
99 
100  // Compiler generated assignment operator is being used
101 
103  Transform& operator*=(const Transform& t);
104 
106  Point3 operator()(const Point3& p) const;
107 
108  bool operator==(const Transform& t) const;
109 
110  bool operator!=(const Transform& t) const;
111 
112 
113 // OPERATIONS
114 
116  Transform inverse() const;
117 
118 
119 // ACCESS
120 
122  Point3 getTranslation() const;
123  Point3& getTranslationRef();
124  const Point3& getTranslationRef() const;
125 
127  Rotation getRotation() const;
128  Rotation& getRotationRef();
129  const Rotation& getRotationRef() const;
130 
132  void setTranslation(const Point3& translation);
133 
135  void setRotation(const Rotation& rotation);
136 
138  void setIdentity();
139 
140 
141 private:
142 
143 // MEMBERS
144 
145 Rotation m_rotation;
146 Point3 m_translation;
147 
148 };
149 
151 
152 // OPERATORS
153 
155 Point3 operator*(const Transform& t,
156  const Point3& p);
157 
159 Transform operator*(const Transform& t1,
160  const Transform& t2);
161 
163 std::ostream& operator<<(std::ostream& os,
164  const Transform& t);
165 
166 
167 // FUNCTIONS
168 
174 
175 Transform interpolate(const Transform& t1,
176  const Transform& t2,
177  const Scalar& s);
178 
179 
181 Transform inverse(const Transform& t);
182 
184 
185 // LIFECYCLE
186 
188 {
189  setIdentity();
190 }
191 
192 
193 inline Transform::Transform(const Rotation& rotation,
194  const Point3& translation) :
195 
196  m_rotation(rotation),
197  m_translation(translation) {}
198 
199 
200 // OPERATORS
201 
203 {
204  m_translation += (m_rotation(t.m_translation));
205  m_rotation *= t.m_rotation;
206  return *this;
207 }
208 
209 
210 inline Point3 Transform::operator()(const Point3& p) const
211 {
212  return Point3(m_rotation(p) + m_translation);
213 }
214 
215 
216 inline bool Transform::operator==(const Transform& t) const
217 {
218  return (m_rotation == t.getRotation() &&
219  m_translation == t.getTranslation());
220 }
221 
222 
223 inline bool Transform::operator!=(const Transform& t) const
224 {
225  return !(*this == t);
226 }
227 
228 
229 // OPERATIONS
230 
232 {
233  const Rotation inv(m_rotation.inverse());
234 
235  return Transform(inv, inv(-m_translation));
236 }
237 
238 
239 // ACCESS
240 
241 
243 {
244  return m_translation;
245 }
246 
247 
248 inline Point3& Transform::getTranslationRef()
249 {
250  return m_translation;
251 }
252 
253 
254 inline const Point3& Transform::getTranslationRef() const
255 {
256  return m_translation;
257 }
258 
259 
261 {
262  return m_rotation;
263 }
264 
265 
266 inline Rotation& Transform::getRotationRef()
267 {
268  return m_rotation;
269 }
270 
271 
272 inline const Rotation& Transform::getRotationRef() const
273 {
274  return m_rotation;
275 }
276 
277 
278 inline void Transform::setTranslation(const Point3& translation)
279 {
280  m_translation = translation;
281 }
282 
283 
284 inline void Transform::setRotation(const Rotation& rotation)
285 {
286  m_rotation = rotation;
287 }
288 
289 
291 {
292  m_rotation.setValue(Scalar(0.0), Scalar(0.0), Scalar(0.0), Scalar(1.0));
293  m_translation.setValue(Scalar(0.0), Scalar(0.0), Scalar(0.0));
294 }
295 
296 
298 
299 // OPERATORS
300 
301 inline Point3 operator*(const Transform& t,
302  const Point3& p)
303 {
304  return t(p);
305 }
306 
307 
308 inline Transform operator*(const Transform& t1,
309  const Transform& t2)
310 {
311  const Rotation rotation (t1.getRotation() * t2.getRotation());
312  const Point3 translation (t1 * t2.getTranslation());
313 
314  return Transform(rotation, translation);
315 }
316 
317 
318 inline std::ostream& operator<<(std::ostream& os,
319  const Transform& t)
320 {
321  return os << "Rotation: " << t.getRotation() << "\n"
322  << "Translation: " << t.getTranslation();
323 }
324 
325 
326 // FUNCTIONS
327 
328 inline Transform interpolate(const Transform& t1,
329  const Transform& t2,
330  const Scalar& s)
331 {
332  const Point3 p = lerp(t1.getTranslation(),
333  t2.getTranslation(),
334  s);
335 
336  const Rotation r = slerp(t1.getRotation(),
337  t2.getRotation(),
338  s);
339 
340  return Transform(r, p);
341 }
342 
343 
344 inline Transform inverse(const Transform& t)
345 {
346  return t.inverse();
347 }
348 
349 }// mt
350 
351 #endif // MT_TRANSFORM_H