frame.h
1 /****************************************************************************
2 
3  Copyright (C) 2002-2013 Gilles Debunne. All rights reserved.
4 
5  This file is part of the QGLViewer library version 2.5.2.
6 
7  http://www.libqglviewer.com - contact@libqglviewer.com
8 
9  This file may be used under the terms of the GNU General Public License
10  versions 2.0 or 3.0 as published by the Free Software Foundation and
11  appearing in the LICENSE file included in the packaging of this file.
12  In addition, as a special exception, Gilles Debunne gives you certain
13  additional rights, described in the file GPL_EXCEPTION in this package.
14 
15  libQGLViewer uses dual licensing. Commercial/proprietary software must
16  purchase a libQGLViewer Commercial License.
17 
18  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
19  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 
21 *****************************************************************************/
22 
23 #ifndef QGLVIEWER_FRAME_H
24 #define QGLVIEWER_FRAME_H
25 
26 #include <QObject>
27 #include <QString>
28 
29 #include "constraint.h"
30 // #include "GL/gl.h" is now included in config.h for ease of configuration
31 
32 namespace qglviewer {
121 class QGLVIEWER_EXPORT Frame : public QObject
122 {
123  Q_OBJECT
124 
125 public:
126  Frame();
127 
129  virtual ~Frame() {}
130 
131  Frame(const Frame& frame);
132  Frame& operator=(const Frame& frame);
133 
134 Q_SIGNALS:
158  void modified();
159 
167  void interpolated();
168 
169 public:
172  Frame(const Vec& position, const Quaternion& orientation);
173 
174  void setPosition(const Vec& position);
175  void setPosition(float x, float y, float z);
176  void setPositionWithConstraint(Vec& position);
177 
178  void setOrientation(const Quaternion& orientation);
179  void setOrientation(double q0, double q1, double q2, double q3);
180  void setOrientationWithConstraint(Quaternion& orientation);
181 
182  void setPositionAndOrientation(const Vec& position, const Quaternion& orientation);
183  void setPositionAndOrientationWithConstraint(Vec& position, Quaternion& orientation);
184 
185  Vec position() const;
186  Quaternion orientation() const;
187 
188  void getPosition(float& x, float& y, float& z) const;
189  void getOrientation(double& q0, double& q1, double& q2, double& q3) const;
191 
192 
193 public:
201  void setTranslation(const Vec& translation) { t_ = translation; Q_EMIT modified(); }
202  void setTranslation(float x, float y, float z);
203  void setTranslationWithConstraint(Vec& translation);
204 
215  void setRotation(const Quaternion& rotation) { q_ = rotation; Q_EMIT modified(); }
216  void setRotation(double q0, double q1, double q2, double q3);
217  void setRotationWithConstraint(Quaternion& rotation);
218 
219  void setTranslationAndRotation(const Vec& translation, const Quaternion& rotation);
220  void setTranslationAndRotationWithConstraint(Vec& translation, Quaternion& rotation);
221 
228  Vec translation() const { return t_; }
237  Quaternion rotation() const { return q_; }
238 
239  void getTranslation(float& x, float& y, float& z) const;
240  void getRotation(double& q0, double& q1, double& q2, double& q3) const;
242 
243 public:
262  const Frame* referenceFrame() const { return referenceFrame_; }
263  void setReferenceFrame(const Frame* const refFrame);
264  bool settingAsReferenceFrameWillCreateALoop(const Frame* const frame);
266 
267 
270  void translate(Vec& t);
271  void translate(const Vec& t);
272  // Some compilers complain about "overloading cannot distinguish from previous declaration"
273  // Simply comment out the following method and its associated implementation
274  void translate(float x, float y, float z);
275  void translate(float& x, float& y, float& z);
276 
277  void rotate(Quaternion& q);
278  void rotate(const Quaternion& q);
279  // Some compilers complain about "overloading cannot distinguish from previous declaration"
280  // Simply comment out the following method and its associated implementation
281  void rotate(double q0, double q1, double q2, double q3);
282  void rotate(double& q0, double& q1, double& q2, double& q3);
283 
284  void rotateAroundPoint(Quaternion& rotation, const Vec& point);
285  void rotateAroundPoint(const Quaternion& rotation, const Vec& point);
286 
287  void alignWithFrame(const Frame* const frame, bool move=false, float threshold=0.0f);
288  void projectOnLine(const Vec& origin, const Vec& direction);
290 
291 
294  Vec coordinatesOf(const Vec& src) const;
295  Vec inverseCoordinatesOf(const Vec& src) const;
296  Vec localCoordinatesOf(const Vec& src) const;
297  Vec localInverseCoordinatesOf(const Vec& src) const;
298  Vec coordinatesOfIn(const Vec& src, const Frame* const in) const;
299  Vec coordinatesOfFrom(const Vec& src, const Frame* const from) const;
300 
301  void getCoordinatesOf(const float src[3], float res[3]) const;
302  void getInverseCoordinatesOf(const float src[3], float res[3]) const;
303  void getLocalCoordinatesOf(const float src[3], float res[3]) const;
304  void getLocalInverseCoordinatesOf(const float src[3], float res[3]) const;
305  void getCoordinatesOfIn(const float src[3], float res[3], const Frame* const in) const;
306  void getCoordinatesOfFrom(const float src[3], float res[3], const Frame* const from) const;
308 
310  // A frame is as a new coordinate system, defined with respect to a reference frame (the world
311  // coordinate system by default, see the "Composition of frame" section).
312 
313  // The transformOf() (resp. inverseTransformOf()) functions transform a 3D vector from (resp.
314  // to) the world coordinates system. This section defines the 3D vector transformation
315  // functions. See the Coordinate system transformation of 3D points above for the transformation
316  // of 3D points. The difference between the two sets of functions is simple: for vectors, only
317  // the rotational part of the transformations is taken into account, while translation is also
318  // considered for 3D points.
319 
320  // The length of the resulting transformed vector is identical to the one of the source vector
321  // for all the described functions.
322 
323  // When local is prepended to the names of the functions, the functions simply transform from
324  // (and to) the reference frame.
325 
326  // When In (resp. From) is appended to the names, the functions transform from (resp. To) the
327  // frame that is given as an argument. The frame does not need to be in the same branch or the
328  // hierarchical tree, and can be \c NULL (the world coordinates system).
329 
330  // Combining any of these functions with its inverse (in any order) leads to the identity.
332  Vec transformOf(const Vec& src) const;
333  Vec inverseTransformOf(const Vec& src) const;
334  Vec localTransformOf(const Vec& src) const;
335  Vec localInverseTransformOf(const Vec& src) const;
336  Vec transformOfIn(const Vec& src, const Frame* const in) const;
337  Vec transformOfFrom(const Vec& src, const Frame* const from) const;
338 
339  void getTransformOf(const float src[3], float res[3]) const;
340  void getInverseTransformOf(const float src[3], float res[3]) const;
341  void getLocalTransformOf(const float src[3], float res[3]) const;
342  void getLocalInverseTransformOf(const float src[3], float res[3]) const;
343  void getTransformOfIn(const float src[3], float res[3], const Frame* const in) const;
344  void getTransformOfFrom(const float src[3], float res[3], const Frame* const from) const;
346 
347 
356  Constraint* constraint() const { return constraint_; }
361  void setConstraint(Constraint* const constraint) { constraint_ = constraint; }
363 
366 public:
367  const GLdouble* matrix() const;
368  void getMatrix(GLdouble m[4][4]) const;
369  void getMatrix(GLdouble m[16]) const;
370 
371  const GLdouble* worldMatrix() const;
372  void getWorldMatrix(GLdouble m[4][4]) const;
373  void getWorldMatrix(GLdouble m[16]) const;
374 
375  void setFromMatrix(const GLdouble m[4][4]);
376  void setFromMatrix(const GLdouble m[16]);
378 
381  Frame inverse() const;
390  Frame worldInverse() const { return Frame(-(orientation().inverseRotate(position())), orientation().inverse()); }
392 
395 public:
396  virtual QDomElement domElement(const QString& name, QDomDocument& document) const;
397 public Q_SLOTS:
398  virtual void initFromDOMElement(const QDomElement& element);
400 
401 private:
402  // P o s i t i o n a n d o r i e n t a t i o n
403  Vec t_;
404  Quaternion q_;
405 
406  // C o n s t r a i n t s
407  Constraint* constraint_;
408 
409  // F r a m e c o m p o s i t i o n
410  const Frame* referenceFrame_;
411 };
412 
413 } // namespace qglviewer
414 
415 #endif // QGLVIEWER_FRAME_H
Frame worldInverse() const
Returns the inverse() of the Frame world transformation.
Definition: frame.h:390
Vec translation() const
Returns the Frame translation, defined with respect to the referenceFrame().
Definition: frame.h:228
void setRotation(const Quaternion &rotation)
Set the current rotation Quaternion.
Definition: frame.h:215
The Vec class represents 3D positions and 3D vectors.
Definition: vec.h:65
void setTranslation(const Vec &translation)
Sets the translation() of the frame, locally defined with respect to the referenceFrame().
Definition: frame.h:201
virtual ~Frame()
Virtual destructor.
Definition: frame.h:129
The Quaternion class represents 3D rotations and orientations.
Definition: quaternion.h:66
const Frame * referenceFrame() const
Returns the reference Frame, in which coordinates system the Frame is defined.
Definition: frame.h:262
void setConstraint(Constraint *const constraint)
Sets the constraint() attached to the Frame.
Definition: frame.h:361
The Frame class represents a coordinate system, defined by a position and an orientation.
Definition: frame.h:121
Quaternion rotation() const
Returns the Frame rotation, defined with respect to the referenceFrame().
Definition: frame.h:237
An interface class for Frame constraints.
Definition: constraint.h:117
Constraint * constraint() const
Returns the current constraint applied to the Frame.
Definition: frame.h:356