File: constraint.cpp

package info (click to toggle)
libqglviewer 2.6.3%2Bdfsg2-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,460 kB
  • sloc: cpp: 26,447; sh: 14; makefile: 11
file content (291 lines) | stat: -rw-r--r-- 10,385 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/****************************************************************************

 Copyright (C) 2002-2014 Gilles Debunne. All rights reserved.

 This file is part of the QGLViewer library version 2.6.3.

 http://www.libqglviewer.com - contact@libqglviewer.com

 This file may be used under the terms of the GNU General Public License 
 versions 2.0 or 3.0 as published by the Free Software Foundation and
 appearing in the LICENSE file included in the packaging of this file.
 In addition, as a special exception, Gilles Debunne gives you certain 
 additional rights, described in the file GPL_EXCEPTION in this package.

 libQGLViewer uses dual licensing. Commercial/proprietary software must
 purchase a libQGLViewer Commercial License.

 This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

*****************************************************************************/

#include "constraint.h"
#include "frame.h"
#include "camera.h"
#include "manipulatedCameraFrame.h"

using namespace qglviewer;
using namespace std;

////////////////////////////////////////////////////////////////////////////////
//                                  Constraint                                //
////////////////////////////////////////////////////////////////////////////////

/*! Default constructor.

translationConstraintType() and rotationConstraintType() are set to AxisPlaneConstraint::FREE.
translationConstraintDirection() and rotationConstraintDirection() are set to (0,0,0). */
AxisPlaneConstraint::AxisPlaneConstraint()
	: translationConstraintType_(FREE), rotationConstraintType_(FREE)
{
	// Do not use set since setRotationConstraintType needs a read.
}

/*! Simply calls setTranslationConstraintType() and setTranslationConstraintDirection(). */
void AxisPlaneConstraint::setTranslationConstraint(Type type, const Vec& direction)
{
	setTranslationConstraintType(type);
	setTranslationConstraintDirection(direction);
}

/*! Defines the translationConstraintDirection(). The coordinate system where \p direction is expressed depends on your class implementation. */
void AxisPlaneConstraint::setTranslationConstraintDirection(const Vec& direction)
{
	if ((translationConstraintType()!=AxisPlaneConstraint::FREE) && (translationConstraintType()!=AxisPlaneConstraint::FORBIDDEN))
	{
		const qreal norm = direction.norm();
		if (norm < 1E-8)
		{
			qWarning("AxisPlaneConstraint::setTranslationConstraintDir: null vector for translation constraint");
			translationConstraintType_ = AxisPlaneConstraint::FREE;
		}
		else
			translationConstraintDir_ = direction/norm;
	}
}

/*! Simply calls setRotationConstraintType() and setRotationConstraintDirection(). */
void AxisPlaneConstraint::setRotationConstraint(Type type, const Vec& direction)
{
	setRotationConstraintType(type);
	setRotationConstraintDirection(direction);
}

/*! Defines the rotationConstraintDirection(). The coordinate system where \p direction is expressed depends on your class implementation. */
void AxisPlaneConstraint::setRotationConstraintDirection(const Vec& direction)
{
	if ((rotationConstraintType()!=AxisPlaneConstraint::FREE) && (rotationConstraintType()!=AxisPlaneConstraint::FORBIDDEN))
	{
		const qreal norm = direction.norm();
		if (norm < 1E-8)
		{
			qWarning("AxisPlaneConstraint::setRotationConstraintDir: null vector for rotation constraint");
			rotationConstraintType_ = AxisPlaneConstraint::FREE;
		}
		else
			rotationConstraintDir_ = direction/norm;
	}
}

/*! Set the Type() of the rotationConstraintType(). Default is AxisPlaneConstraint::FREE.

 Depending on this value, the Frame will freely rotate (AxisPlaneConstraint::FREE), will only be able
 to rotate around an axis (AxisPlaneConstraint::AXIS), or will not able to rotate at all
 (AxisPlaneConstraint::FORBIDDEN).

 Use Frame::setOrientation() to define the orientation of the constrained Frame before it gets
 constrained.

 \attention An AxisPlaneConstraint::PLANE Type() is not meaningful for rotational constraints and
 will be ignored. */
void AxisPlaneConstraint::setRotationConstraintType(Type type)
{
	if (rotationConstraintType() == AxisPlaneConstraint::PLANE)
	{
		qWarning("AxisPlaneConstraint::setRotationConstraintType: the PLANE type cannot be used for a rotation constraints");
		return;
	}

	rotationConstraintType_ = type;
}


////////////////////////////////////////////////////////////////////////////////
//                               LocalConstraint                              //
////////////////////////////////////////////////////////////////////////////////

/*! Depending on translationConstraintType(), constrain \p translation to be along an axis or
  limited to a plane defined in the Frame local coordinate system by
  translationConstraintDirection(). */
void LocalConstraint::constrainTranslation(Vec& translation, Frame* const frame)
{
	Vec proj;
	switch (translationConstraintType())
	{
	case AxisPlaneConstraint::FREE:
		break;
	case AxisPlaneConstraint::PLANE:
		proj = frame->rotation().rotate(translationConstraintDirection());
		translation.projectOnPlane(proj);
		break;
	case AxisPlaneConstraint::AXIS:
		proj = frame->rotation().rotate(translationConstraintDirection());
		translation.projectOnAxis(proj);
		break;
	case AxisPlaneConstraint::FORBIDDEN:
		translation = Vec(0.0, 0.0, 0.0);
		break;
	}
}

/*! When rotationConstraintType() is AxisPlaneConstraint::AXIS, constrain \p rotation to be a rotation
  around an axis whose direction is defined in the Frame local coordinate system by
  rotationConstraintDirection(). */
void LocalConstraint::constrainRotation(Quaternion& rotation, Frame* const)
{
	switch (rotationConstraintType())
	{
	case AxisPlaneConstraint::FREE:
		break;
	case AxisPlaneConstraint::PLANE:
		break;
	case AxisPlaneConstraint::AXIS:
	{
		Vec axis = rotationConstraintDirection();
		Vec quat = Vec(rotation[0], rotation[1], rotation[2]);
		quat.projectOnAxis(axis);
		rotation = Quaternion(quat, 2.0*acos(rotation[3]));
	}
		break;
	case AxisPlaneConstraint::FORBIDDEN:
		rotation = Quaternion(); // identity
		break;
	}
}

////////////////////////////////////////////////////////////////////////////////
//                               WorldConstraint                              //
////////////////////////////////////////////////////////////////////////////////

/*! Depending on translationConstraintType(), constrain \p translation to be along an axis or
  limited to a plane defined in the world coordinate system by
  translationConstraintDirection(). */
void WorldConstraint::constrainTranslation(Vec& translation, Frame* const frame)
{
	Vec proj;
	switch (translationConstraintType())
	{
	case AxisPlaneConstraint::FREE:
		break;
	case AxisPlaneConstraint::PLANE:
		if (frame->referenceFrame())
		{
			proj = frame->referenceFrame()->transformOf(translationConstraintDirection());
			translation.projectOnPlane(proj);
		}
		else
			translation.projectOnPlane(translationConstraintDirection());
		break;
	case AxisPlaneConstraint::AXIS:
		if (frame->referenceFrame())
		{
			proj = frame->referenceFrame()->transformOf(translationConstraintDirection());
			translation.projectOnAxis(proj);
		}
		else
			translation.projectOnAxis(translationConstraintDirection());
		break;
	case AxisPlaneConstraint::FORBIDDEN:
		translation = Vec(0.0, 0.0, 0.0);
		break;
	}
}

/*! When rotationConstraintType() is AxisPlaneConstraint::AXIS, constrain \p rotation to be a rotation
  around an axis whose direction is defined in the world coordinate system by
  rotationConstraintDirection(). */
void WorldConstraint::constrainRotation(Quaternion& rotation, Frame* const frame)
{
	switch (rotationConstraintType())
	{
	case AxisPlaneConstraint::FREE:
		break;
	case AxisPlaneConstraint::PLANE:
		break;
	case AxisPlaneConstraint::AXIS:
	{
		Vec quat(rotation[0], rotation[1], rotation[2]);
		Vec axis = frame->transformOf(rotationConstraintDirection());
		quat.projectOnAxis(axis);
		rotation = Quaternion(quat, 2.0*acos(rotation[3]));
		break;
	}
	case AxisPlaneConstraint::FORBIDDEN:
		rotation = Quaternion(); // identity
		break;
	}
}

////////////////////////////////////////////////////////////////////////////////
//                               CameraConstraint                              //
////////////////////////////////////////////////////////////////////////////////

/*! Creates a CameraConstraint, whose constrained directions are defined in the \p camera coordinate
  system. */
CameraConstraint::CameraConstraint(const Camera* const camera)
	: AxisPlaneConstraint(), camera_(camera)
{}

/*! Depending on translationConstraintType(), constrain \p translation to be along an axis or
  limited to a plane defined in the camera() coordinate system by
  translationConstraintDirection(). */
void CameraConstraint::constrainTranslation(Vec& translation, Frame* const frame)
{
	Vec proj;
	switch (translationConstraintType())
	{
	case AxisPlaneConstraint::FREE:
		break;
	case AxisPlaneConstraint::PLANE:
		proj = camera()->frame()->inverseTransformOf(translationConstraintDirection());
		if (frame->referenceFrame())
			proj = frame->referenceFrame()->transformOf(proj);
		translation.projectOnPlane(proj);
		break;
	case AxisPlaneConstraint::AXIS:
		proj = camera()->frame()->inverseTransformOf(translationConstraintDirection());
		if (frame->referenceFrame())
			proj = frame->referenceFrame()->transformOf(proj);
		translation.projectOnAxis(proj);
		break;
	case AxisPlaneConstraint::FORBIDDEN:
		translation = Vec(0.0, 0.0, 0.0);
		break;
	}
}

/*! When rotationConstraintType() is AxisPlaneConstraint::AXIS, constrain \p rotation to be a rotation
  around an axis whose direction is defined in the camera() coordinate system by
  rotationConstraintDirection(). */
void CameraConstraint::constrainRotation(Quaternion& rotation, Frame* const frame)
{
	switch (rotationConstraintType())
	{
	case AxisPlaneConstraint::FREE:
		break;
	case AxisPlaneConstraint::PLANE:
		break;
	case AxisPlaneConstraint::AXIS:
	{
		Vec axis = frame->transformOf(camera()->frame()->inverseTransformOf(rotationConstraintDirection()));
		Vec quat = Vec(rotation[0], rotation[1], rotation[2]);
		quat.projectOnAxis(axis);
		rotation = Quaternion(quat, 2.0*acos(rotation[3]));
	}
		break;
	case AxisPlaneConstraint::FORBIDDEN:
		rotation = Quaternion(); // identity
		break;
	}
}