constraint.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 
3  Copyright (C) 2002-2013 Gilles Debunne. All rights reserved.
4 
5  This file is part of the QGLViewer library version 2.4.0.
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 
27 using namespace qglviewer;
28 using namespace std;
29 
31 // Constraint //
33 
39  : translationConstraintType_(FREE), rotationConstraintType_(FREE)
40 {
41  // Do not use set since setRotationConstraintType needs a read.
42 }
43 
46 {
49 }
50 
53 {
55  {
56  const float norm = direction.norm();
57  if (norm < 1E-8)
58  {
59  qWarning("AxisPlaneConstraint::setTranslationConstraintDir: null vector for translation constraint");
61  }
62  else
63  translationConstraintDir_ = direction/norm;
64  }
65 }
66 
69 {
72 }
73 
76 {
78  {
79  float norm = direction.norm();
80  if (norm < 1E-8)
81  {
82  qWarning("AxisPlaneConstraint::setRotationConstraintDir: null vector for rotation constraint");
84  }
85  else
86  rotationConstraintDir_ = direction/norm;
87  }
88 }
89 
102 {
104  {
105  qWarning("AxisPlaneConstraint::setRotationConstraintType: the PLANE type cannot be used for a rotation constraints");
106  return;
107  }
108 
110 }
111 
112 
114 // LocalConstraint //
116 
120 void LocalConstraint::constrainTranslation(Vec& translation, Frame* const frame)
121 {
122  Vec proj;
123  switch (translationConstraintType())
124  {
126  break;
129  translation.projectOnPlane(proj);
130  break;
133  translation.projectOnAxis(proj);
134  break;
136  translation = Vec(0.0, 0.0, 0.0);
137  break;
138  }
139 }
140 
145 {
146  switch (rotationConstraintType())
147  {
149  break;
151  break;
153  {
155  Vec quat = Vec(rotation[0], rotation[1], rotation[2]);
156  quat.projectOnAxis(axis);
157  rotation = Quaternion(quat, 2.0*acos(rotation[3]));
158  }
159  break;
161  rotation = Quaternion(); // identity
162  break;
163  }
164 }
165 
167 // WorldConstraint //
169 
173 void WorldConstraint::constrainTranslation(Vec& translation, Frame* const frame)
174 {
175  Vec proj;
176  switch (translationConstraintType())
177  {
179  break;
181  if (frame->referenceFrame())
182  {
184  translation.projectOnPlane(proj);
185  }
186  else
188  break;
190  if (frame->referenceFrame())
191  {
193  translation.projectOnAxis(proj);
194  }
195  else
197  break;
199  translation = Vec(0.0, 0.0, 0.0);
200  break;
201  }
202 }
203 
208 {
209  switch (rotationConstraintType())
210  {
212  break;
214  break;
216  {
217  Vec quat(rotation[0], rotation[1], rotation[2]);
219  quat.projectOnAxis(axis);
220  rotation = Quaternion(quat, 2.0*acos(rotation[3]));
221  break;
222  }
224  rotation = Quaternion(); // identity
225  break;
226  }
227 }
228 
230 // CameraConstraint //
232 
236  : AxisPlaneConstraint(), camera_(camera)
237 {}
238 
242 void CameraConstraint::constrainTranslation(Vec& translation, Frame* const frame)
243 {
244  Vec proj;
245  switch (translationConstraintType())
246  {
248  break;
251  if (frame->referenceFrame())
252  proj = frame->referenceFrame()->transformOf(proj);
253  translation.projectOnPlane(proj);
254  break;
257  if (frame->referenceFrame())
258  proj = frame->referenceFrame()->transformOf(proj);
259  translation.projectOnAxis(proj);
260  break;
262  translation = Vec(0.0, 0.0, 0.0);
263  break;
264  }
265 }
266 
271 {
272  switch (rotationConstraintType())
273  {
275  break;
277  break;
279  {
280  Vec axis = frame->transformOf(camera()->frame()->inverseTransformOf(rotationConstraintDirection()));
281  Vec quat = Vec(rotation[0], rotation[1], rotation[2]);
282  quat.projectOnAxis(axis);
283  rotation = Quaternion(quat, 2.0*acos(rotation[3]));
284  }
285  break;
287  rotation = Quaternion(); // identity
288  break;
289  }
290 }
void setRotationConstraintDirection(const Vec &direction)
Definition: constraint.cpp:75
#define FREE(p)
Definition: gpc.cpp:155
Vec translationConstraintDirection() const
Definition: constraint.h:238
Vec inverseTransformOf(const Vec &src) const
Definition: frame.cpp:847
void setTranslationConstraint(Type type, const Vec &direction)
Definition: constraint.cpp:45
const Camera * camera() const
Definition: constraint.h:330
void setRotationConstraint(Type type, const Vec &direction)
Definition: constraint.cpp:68
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:834
double norm() const
Definition: vec.h:339
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:120
CameraConstraint(const Camera *const camera)
Definition: constraint.cpp:235
virtual void constrainTranslation(Vec &translation, Frame *const frame)
Definition: constraint.cpp:173
ManipulatedCameraFrame * frame() const
Definition: camera.h:372
void setTranslationConstraintDirection(const Vec &direction)
Definition: constraint.cpp:52
The Vec class represents 3D positions and 3D vectors.
Definition: vec.h:69
Quaternion rotation() const
Definition: frame.h:244
virtual void constrainRotation(Quaternion &rotation, Frame *const frame)
Definition: constraint.cpp:144
A perspective or orthographic camera.
Definition: camera.h:81
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:126
void setRotationConstraintType(Type type)
Definition: constraint.cpp:101
const Frame * referenceFrame() const
Definition: frame.h:269
virtual void constrainTranslation(Vec &translation, Frame *const frame)
Definition: constraint.cpp:242
virtual void constrainRotation(Quaternion &rotation, Frame *const frame)
Definition: constraint.cpp:270
virtual void constrainRotation(Quaternion &rotation, Frame *const frame)
Definition: constraint.cpp:207


octovis
Author(s): Kai M. Wurm , Armin Hornung
autogenerated on Mon Jun 10 2019 14:00:24