constraint.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 
3  Copyright (C) 2002-2014 Gilles Debunne. All rights reserved.
4 
5  This file is part of the QGLViewer library version 2.6.3.
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 #include "constraint.h"
24 #include "frame.h"
25 #include "camera.h"
26 #include "manipulatedCameraFrame.h"
27 
28 using namespace qglviewer;
29 using namespace std;
30 
32 // Constraint //
34 
40  : translationConstraintType_(FREE), rotationConstraintType_(FREE)
41 {
42  // Do not use set since setRotationConstraintType needs a read.
43 }
44 
47 {
50 }
51 
54 {
56  {
57  const qreal norm = direction.norm();
58  if (norm < 1E-8)
59  {
60  qWarning("AxisPlaneConstraint::setTranslationConstraintDir: null vector for translation constraint");
62  }
63  else
64  translationConstraintDir_ = direction/norm;
65  }
66 }
67 
70 {
73 }
74 
77 {
79  {
80  const qreal norm = direction.norm();
81  if (norm < 1E-8)
82  {
83  qWarning("AxisPlaneConstraint::setRotationConstraintDir: null vector for rotation constraint");
85  }
86  else
87  rotationConstraintDir_ = direction/norm;
88  }
89 }
90 
103 {
105  {
106  qWarning("AxisPlaneConstraint::setRotationConstraintType: the PLANE type cannot be used for a rotation constraints");
107  return;
108  }
109 
111 }
112 
113 
115 // LocalConstraint //
117 
121 void LocalConstraint::constrainTranslation(Vec& translation, Frame* const frame)
122 {
123  Vec proj;
124  switch (translationConstraintType())
125  {
127  break;
130  translation.projectOnPlane(proj);
131  break;
134  translation.projectOnAxis(proj);
135  break;
137  translation = Vec(0.0, 0.0, 0.0);
138  break;
139  }
140 }
141 
146 {
147  switch (rotationConstraintType())
148  {
150  break;
152  break;
154  {
156  Vec quat = Vec(rotation[0], rotation[1], rotation[2]);
157  quat.projectOnAxis(axis);
158  rotation = Quaternion(quat, 2.0*acos(rotation[3]));
159  }
160  break;
162  rotation = Quaternion(); // identity
163  break;
164  }
165 }
166 
168 // WorldConstraint //
170 
174 void WorldConstraint::constrainTranslation(Vec& translation, Frame* const frame)
175 {
176  Vec proj;
177  switch (translationConstraintType())
178  {
180  break;
182  if (frame->referenceFrame())
183  {
185  translation.projectOnPlane(proj);
186  }
187  else
189  break;
191  if (frame->referenceFrame())
192  {
194  translation.projectOnAxis(proj);
195  }
196  else
198  break;
200  translation = Vec(0.0, 0.0, 0.0);
201  break;
202  }
203 }
204 
209 {
210  switch (rotationConstraintType())
211  {
213  break;
215  break;
217  {
218  Vec quat(rotation[0], rotation[1], rotation[2]);
220  quat.projectOnAxis(axis);
221  rotation = Quaternion(quat, 2.0*acos(rotation[3]));
222  break;
223  }
225  rotation = Quaternion(); // identity
226  break;
227  }
228 }
229 
231 // CameraConstraint //
233 
237  : AxisPlaneConstraint(), camera_(camera)
238 {}
239 
243 void CameraConstraint::constrainTranslation(Vec& translation, Frame* const frame)
244 {
245  Vec proj;
246  switch (translationConstraintType())
247  {
249  break;
252  if (frame->referenceFrame())
253  proj = frame->referenceFrame()->transformOf(proj);
254  translation.projectOnPlane(proj);
255  break;
258  if (frame->referenceFrame())
259  proj = frame->referenceFrame()->transformOf(proj);
260  translation.projectOnAxis(proj);
261  break;
263  translation = Vec(0.0, 0.0, 0.0);
264  break;
265  }
266 }
267 
272 {
273  switch (rotationConstraintType())
274  {
276  break;
278  break;
280  {
281  Vec axis = frame->transformOf(camera()->frame()->inverseTransformOf(rotationConstraintDirection()));
282  Vec quat = Vec(rotation[0], rotation[1], rotation[2]);
283  quat.projectOnAxis(axis);
284  rotation = Quaternion(quat, 2.0*acos(rotation[3]));
285  }
286  break;
288  rotation = Quaternion(); // identity
289  break;
290  }
291 }
void setRotationConstraintDirection(const Vec &direction)
Definition: constraint.cpp:76
#define FREE(p)
Definition: gpc.cpp:155
Vec translationConstraintDirection() const
Definition: constraint.h:238
Vec inverseTransformOf(const Vec &src) const
Definition: frame.cpp:856
void setTranslationConstraint(Type type, const Vec &direction)
Definition: constraint.cpp:46
const Camera * camera() const
Definition: constraint.h:330
void setRotationConstraint(Type type, const Vec &direction)
Definition: constraint.cpp:69
qreal norm() const
Definition: vec.h:335
An abstract class for Frame Constraints defined by an axis or a plane.
Definition: constraint.h:168
Vec transformOf(const Vec &src) const
Definition: frame.cpp:843
Vec rotate(const Vec &v) const
Definition: quaternion.cpp:76
void projectOnPlane(const Vec &normal)
Definition: vec.cpp:47
Type translationConstraintType() const
Definition: constraint.h:228
Vec rotationConstraintDirection() const
Definition: constraint.h:259
Type rotationConstraintType() const
Definition: constraint.h:251
virtual void constrainTranslation(Vec &translation, Frame *const frame)
Definition: constraint.cpp:121
CameraConstraint(const Camera *const camera)
Definition: constraint.cpp:236
virtual void constrainTranslation(Vec &translation, Frame *const frame)
Definition: constraint.cpp:174
ManipulatedCameraFrame * frame() const
Definition: camera.h:334
void setTranslationConstraintDirection(const Vec &direction)
Definition: constraint.cpp:53
The Vec class represents 3D positions and 3D vectors.
Definition: vec.h:65
Quaternion rotation() const
Definition: frame.h:237
virtual void constrainRotation(Quaternion &rotation, Frame *const frame)
Definition: constraint.cpp:145
A perspective or orthographic camera.
Definition: camera.h:84
The Quaternion class represents 3D rotations and orientations.
Definition: quaternion.h:66
void projectOnAxis(const Vec &direction)
Definition: vec.cpp:34
void setTranslationConstraintType(Type type)
Definition: constraint.h:216
The Frame class represents a coordinate system, defined by a position and an orientation.
Definition: frame.h:121
void setRotationConstraintType(Type type)
Definition: constraint.cpp:102
const Frame * referenceFrame() const
Definition: frame.h:262
virtual void constrainTranslation(Vec &translation, Frame *const frame)
Definition: constraint.cpp:243
virtual void constrainRotation(Quaternion &rotation, Frame *const frame)
Definition: constraint.cpp:271
virtual void constrainRotation(Quaternion &rotation, Frame *const frame)
Definition: constraint.cpp:208


octovis
Author(s): Kai M. Wurm , Armin Hornung
autogenerated on Wed Jun 5 2019 19:26:39