File: constrainedCamera.html

package info (click to toggle)
libqglviewer 2.8.0%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,076 kB
  • sloc: cpp: 25,884; sh: 14; makefile: 12
file content (339 lines) | stat: -rw-r--r-- 10,208 bytes parent folder | download | duplicates (3)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>libQGLViewer constrainedCamera example</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <link href="../qglviewer.css" rel="stylesheet" type="text/css" />
  <link rel="shortcut icon" href="../images/qglviewer.ico" type="image/x-icon" />
  <link rel="icon" href="../images/qglviewer.icon.png" type="image/png" />
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-23223012-2']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
</head>
<body>

<div class="banner">
 <a class="qindex" href="../index.html">Home</a>
 <a class="qindex" href="../download.html">Download</a>
 <a class="qindex highlight" href="index.html">Gallery</a>
 <a class="qindex" href="../refManual/hierarchy.html">Documentation</a>
 <a class="qindex" href="../developer.html">Developer</a>
</div>

<h1>The constrainedCamera example</h1>

<center>
  <img src="../images/constrainedCamera.jpg" width="330" height="228" alt="constrainedCamera"/>
</center>

<p>
 Constraints applied on the Camera Frame to limit its translation and/or rotation.
</p>
<p>
 Try the different possible constraints using the T (translate) and R (rotate) keys. G and D change
 the constraint directions. Press Space to change the coordinate system (World or Camera) which
 defines the constraint directions.
</p>
<p>
 Note that in order to apply a constraint to a camera, you need to apply it to the camera()-&gt;frame().
</p>
<h2>constrainedCamera.h</h2>
<pre>
#include &lt;QGLViewer/qglviewer.h&gt;

class Viewer : public QGLViewer {
protected:
  virtual void init();
  virtual void draw();
  virtual QString helpString() const;
  virtual void keyPressEvent(QKeyEvent *);

  void displayText();
  void displayType(const qglviewer::AxisPlaneConstraint::Type type, const int x,
                   const int y, const char c);
  void displayDir(const unsigned short dir, const int x, const int y,
                  const char c);

private:
  int transDir;
  int rotDir;

  static qglviewer::AxisPlaneConstraint::Type nextTranslationConstraintType(
      const qglviewer::AxisPlaneConstraint::Type &amp;type);
  static qglviewer::AxisPlaneConstraint::Type
  nextRotationConstraintType(const qglviewer::AxisPlaneConstraint::Type &amp;type);

  void changeConstraint();
  qglviewer::AxisPlaneConstraint *constraints[2];
  unsigned short activeConstraint;
};
</pre>


<h2>constrainedCamera.cpp</h2>
<pre>
#include "constrainedCamera.h"

#include &lt;QGLViewer/manipulatedCameraFrame.h&gt;
#include &lt;QKeyEvent&gt;

using namespace qglviewer;
using namespace std;

AxisPlaneConstraint::Type
Viewer::nextTranslationConstraintType(const AxisPlaneConstraint::Type &amp;type) {
  switch (type) {
  case AxisPlaneConstraint::FREE:
    return AxisPlaneConstraint::PLANE;
    break;
  case AxisPlaneConstraint::PLANE:
    return AxisPlaneConstraint::AXIS;
    break;
  case AxisPlaneConstraint::AXIS:
    return AxisPlaneConstraint::FORBIDDEN;
    break;
  case AxisPlaneConstraint::FORBIDDEN:
    return AxisPlaneConstraint::FREE;
    break;
  default:
    return AxisPlaneConstraint::FREE;
  }
}

AxisPlaneConstraint::Type
Viewer::nextRotationConstraintType(const AxisPlaneConstraint::Type &amp;type) {
  switch (type) {
  case AxisPlaneConstraint::FREE:
    return AxisPlaneConstraint::AXIS;
    break;
  case AxisPlaneConstraint::PLANE:
    return AxisPlaneConstraint::FREE;
    break;
  case AxisPlaneConstraint::AXIS:
    return AxisPlaneConstraint::FORBIDDEN;
    break;
  case AxisPlaneConstraint::FORBIDDEN:
    return AxisPlaneConstraint::FREE;
    break;
  default:
    return AxisPlaneConstraint::FREE;
  }
}

void Viewer::changeConstraint() {
  unsigned short previous = activeConstraint;
  activeConstraint = (activeConstraint + 1) % 2;

  constraints[activeConstraint]-&gt;setTranslationConstraintType(
      constraints[previous]-&gt;translationConstraintType());
  constraints[activeConstraint]-&gt;setTranslationConstraintDirection(
      constraints[previous]-&gt;translationConstraintDirection());
  constraints[activeConstraint]-&gt;setRotationConstraintType(
      constraints[previous]-&gt;rotationConstraintType());
  constraints[activeConstraint]-&gt;setRotationConstraintDirection(
      constraints[previous]-&gt;rotationConstraintDirection());

  camera()-&gt;frame()-&gt;setConstraint(constraints[activeConstraint]);
}

void Viewer::init() {
  restoreStateFromFile();

  constraints[0] = new WorldConstraint();

  // Note that a CameraConstraint(camera) would produce the same results:
  // A CameraConstraint is a LocalConstraint when applied to the camera frame !
  constraints[1] = new LocalConstraint();

  transDir = 0;
  rotDir = 0;
  activeConstraint = 0;

  camera()-&gt;frame()-&gt;setConstraint(constraints[activeConstraint]);

  setAxisIsDrawn();

  setKeyDescription(Qt::Key_G, "Change translation constraint direction");
  setKeyDescription(Qt::Key_D, "Change rotation constraint direction");
  setKeyDescription(Qt::Key_Space, "Change constraint reference");
  setKeyDescription(Qt::Key_T, "Change translation constraint type");
  setKeyDescription(Qt::Key_R, "Change rotation constraint type");

  help();
}

void Viewer::draw() {
  const float nbSteps = 200.0;
  glBegin(GL_QUAD_STRIP);
  for (float i = 0; i &lt; nbSteps; ++i) {
    float ratio = i / nbSteps;
    float angle = 21.0 * ratio;
    float c = cos(angle);
    float s = sin(angle);
    float r1 = 1.0 - 0.8 * ratio;
    float r2 = 0.8 - 0.8 * ratio;
    float alt = ratio - 0.5;
    const float nor = .5;
    const float up = sqrt(1.0 - nor * nor);
    glColor3f(1 - ratio, 0.2f, ratio);
    glNormal3f(nor * c, up, nor * s);
    glVertex3f(r1 * c, alt, r1 * s);
    glVertex3f(r2 * c, alt + 0.05, r2 * s);
  }
  glEnd();

  displayText();
}

void Viewer::keyPressEvent(QKeyEvent *e) {
  switch (e-&gt;key()) {
  case Qt::Key_G:
    transDir = (transDir + 1) % 3;
    break;
  case Qt::Key_D:
    rotDir = (rotDir + 1) % 3;
    break;
  case Qt::Key_Space:
    changeConstraint();
    break;
  case Qt::Key_T:
    constraints[activeConstraint]-&gt;setTranslationConstraintType(
        nextTranslationConstraintType(
            constraints[activeConstraint]-&gt;translationConstraintType()));
    break;
  case Qt::Key_R:
    constraints[activeConstraint]-&gt;setRotationConstraintType(
        nextRotationConstraintType(
            constraints[activeConstraint]-&gt;rotationConstraintType()));
    break;
  default:
    QGLViewer::keyPressEvent(e);
  }

  Vec dir(0.0, 0.0, 0.0);
  dir[transDir] = 1.0;
  constraints[activeConstraint]-&gt;setTranslationConstraintDirection(dir);

  dir = Vec(0.0, 0.0, 0.0);
  dir[rotDir] = 1.0;
  constraints[activeConstraint]-&gt;setRotationConstraintDirection(dir);

  update();
}

void Viewer::displayType(const AxisPlaneConstraint::Type type, const int x,
                         const int y, const char c) {
  QString text;
  switch (type) {
  case AxisPlaneConstraint::FREE:
    text = QString("FREE (%1)").arg(c);
    break;
  case AxisPlaneConstraint::PLANE:
    text = QString("PLANE (%1)").arg(c);
    break;
  case AxisPlaneConstraint::AXIS:
    text = QString("AXIS (%1)").arg(c);
    break;
  case AxisPlaneConstraint::FORBIDDEN:
    text = QString("FORBIDDEN (%1)").arg(c);
    break;
  }
  drawText(x, y, text);
}

void Viewer::displayDir(const unsigned short dir, const int x, const int y,
                        const char c) {
  QString text;
  switch (dir) {
  case 0:
    text = QString("X (%1)").arg(c);
    break;
  case 1:
    text = QString("Y (%1)").arg(c);
    break;
  case 2:
    text = QString("Z (%1)").arg(c);
    break;
  }
  drawText(x, y, text);
}

void Viewer::displayText() {
  glColor4f(foregroundColor().redF(), foregroundColor().greenF(),
            foregroundColor().blueF(), foregroundColor().alphaF());
  glDisable(GL_LIGHTING);
  drawText(10, height() - 30, "TRANSLATION :");
  displayDir(transDir, 190, height() - 30, 'G');
  displayType(constraints[activeConstraint]-&gt;translationConstraintType(), 10,
              height() - 60, 'T');

  drawText(width() - 220, height() - 30, "ROTATION :");
  displayDir(rotDir, width() - 100, height() - 30, 'D');
  displayType(constraints[activeConstraint]-&gt;rotationConstraintType(),
              width() - 220, height() - 60, 'R');

  switch (activeConstraint) {
  case 0:
    drawText(20, 20, "Constraint direction defined w/r to WORLD (SPACE)");
    break;
  case 1:
    drawText(20, 20, "Constraint direction defined w/r to CAMERA (SPACE)");
    break;
  }

  glEnable(GL_LIGHTING);
}

QString Viewer::helpString() const {
  QString text("&lt;h2&gt;C o n s t r a i n e d C a m e r a&lt;/h2&gt;");
  text += "The camera frame can be constrained to limit the camera "
          "displacements.&lt;br&gt;&lt;br&gt;";
  text += "Try the different translation (press &lt;b&gt;G&lt;/b&gt; and &lt;b&gt;T&lt;/b&gt;) and "
          "rotation ";
  text += "(&lt;b&gt;D&lt;/b&gt; and &lt;b&gt;R&lt;/b&gt;) constraints while moving the camera with "
          "the mouse. ";
  text += "The constraints can be defined with respect to various coordinates ";
  text += "systems : press &lt;b&gt;Space&lt;/b&gt; to switch.&lt;br&gt;&lt;br&gt;";
  text += "You can easily define your own constraints to create a specific "
          "camera constraint.";
  return text;
}
</pre>


<h2>main.cpp</h2>
<pre>
#include "constrainedCamera.h"
#include &lt;qapplication.h&gt;

int main(int argc, char **argv) {
  QApplication application(argc, argv);

  Viewer viewer;

  viewer.setWindowTitle("constrainedCamera");

  viewer.show();

  return application.exec();
}
</pre>



<p>
  Back to the <a href="index.html">examples main page</a>.
</p>

</body>
</html>