Debian Codegraph @

libSoXt/src/viewers/SoXtConstVwr.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 * Copyright (C) 1990,91 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision: 1.1.1.1 $
44 |
45 | Classes : SoXtConstrainedViewer
46 |
47 | Author(s) : Alain Dumesny
48 |
49 ______________ S I L I C O N G R A P H I C S I N C . ____________
50 _______________________________________________________________________
51 */
52
53
54 #include <Inventor/SoPickedPoint.h>
55 #include <Inventor/actions/SoGetBoundingBoxAction.h>
56 #include <Inventor/actions/SoRayPickAction.h>
57 #include <Inventor/nodes/SoCamera.h>
58 #include <Inventor/nodes/SoSeparator.h>
59 #include <Inventor/Xt/viewers/SoXtConstrainedViewer.h>
60
61
62 /*
63 * Defines
64 */
65
66 #define MIN_ANGLE (5*M_PI/180.) // minimum angle between look at
67 // direction and up direction (in rad)
68
69
70 ////////////////////////////////////////////////////////////////////////
71 //
72 // Constructor
73 //
74 // Use: protected
75
76 SoXtConstrainedViewer::SoXtConstrainedViewer(
77 Widget parent,
78 const char *name,
79 SbBool buildInsideParent,
80 SoXtFullViewer::BuildFlag b,
81 SoXtViewer::Type t,
82 SbBool buildNow)
83 : SoXtFullViewer(
84 parent,
85 name,
86 buildInsideParent,
87 b,
88 t,
89 FALSE) // tell GLWidget not to build just yet
90 //
91 ////////////////////////////////////////////////////////////////////////
92 {
93 // init local vars
94 upDirection.setValue(0, 1, 0);
95 sceneHeight = 0.0;
96
97 // assign decoration titles
98 setBottomWheelString("Rotate");
99 setLeftWheelString("Tilt");
100 setRightWheelString("Dolly");
101
102 // Build the widget tree, and let SoXtComponent know about our base widget.
103 if (buildNow) {
104 Widget w = buildWidget(getParentWidget());
105 setBaseWidget(w);
106 }
107 }
108
109 ////////////////////////////////////////////////////////////////////////
110 //
111 // Destructor
112 //
113 // Use: protected
114
115 SoXtConstrainedViewer::~SoXtConstrainedViewer()
116 //
117 ////////////////////////////////////////////////////////////////////////
118 {
119 }
120
121 ////////////////////////////////////////////////////////////////////////
122 //
123 // Description:
124 // redefines this set the scene size on max(width,depth) and update
125 // the scene height (based on up direction)
126 //
127 // use: virtual public
128
129 void
130 SoXtConstrainedViewer::recomputeSceneSize()
131 //
132 ////////////////////////////////////////////////////////////////////////
133 {
134 if (! sceneGraph || ! sceneRoot) {
135 sceneSize = sceneHeight = 0.0;
136 return;
137 }
138
139 // Use assignment notation to disambiguate from expression (edison)
140 SoGetBoundingBoxAction bboxAct = SoGetBoundingBoxAction(SbViewportRegion(getGlxSize()));
141 bboxAct.apply(sceneRoot);
142 SbBox3f bbox = bboxAct.getBoundingBox();
143
144 if (bbox.isEmpty()) {
145 sceneSize = sceneHeight = 0.0;
146 return;
147 }
148
149 // ??? this assumes Y is up right now (for sceneHeight)
150 float x, z;
151 bbox.getSize(x, sceneHeight, z);
152 sceneSize = (x > z) ? x : z;
153
154 if (sceneSize <= 0.0)
155 sceneSize = 0.0;
156 if (sceneHeight <= 0.0)
157 sceneHeight = 0.0;
158 }
159
160 ////////////////////////////////////////////////////////////////////////
161 //
162 // Description:
163 // Sets the viewer new up direction, and rotates the camera to align
164 // it correctly
165 //
166 // Use: public
167
168 void
169 SoXtConstrainedViewer::setUpDirection(const SbVec3f &newUpDirection)
170 //
171 ////////////////////////////////////////////////////////////////////////
172 {
173 SbRotation rot(upDirection, newUpDirection);
174 upDirection = newUpDirection;
175
176 // rotate the camera and check for constrain
177 if (camera != NULL) {
178 camera->orientation = rot * camera->orientation.getValue();
179 checkForCameraUpConstrain();
180 }
181 }
182
183 ////////////////////////////////////////////////////////////////////////
184 //
185 // Description:
186 // Sets the camera to use (done in base class) and makes sure that
187 // camera constrains are satisfied.
188 //
189 // Use: virtual public
190 void
191 SoXtConstrainedViewer::setCamera(SoCamera *newCamera)
192 //
193 ////////////////////////////////////////////////////////////////////////
194 {
195 // call base class routine
196 SoXtFullViewer::setCamera(newCamera);
197
198 // now check for constrains
199 if (camera != NULL)
200 checkForCameraUpConstrain();
201 }
202
203 ////////////////////////////////////////////////////////////////////////
204 //
205 // Description:
206 // Calls the parent class and also save the up direction
207 //
208 // Use: virtual public
209
210 void
211 SoXtConstrainedViewer::saveHomePosition()
212 //
213 ////////////////////////////////////////////////////////////////////////
214 {
215 // call the parent class
216 SoXtFullViewer::saveHomePosition();
217
218 origUpDirection = upDirection;
219 }
220
221 ////////////////////////////////////////////////////////////////////////
222 //
223 // Description:
224 // Calls the parent class and also restores the up direction
225 //
226 // Use: virtual public
227
228 void
229 SoXtConstrainedViewer::resetToHomePosition()
230 //
231 ////////////////////////////////////////////////////////////////////////
232 {
233 // call the parent class
234 SoXtFullViewer::resetToHomePosition();
235
236 upDirection = origUpDirection;
237 }
238
239 ////////////////////////////////////////////////////////////////////////
240 //
241 // Description:
242 // Rotates the camera around the up direction (called by thumb wheel).
243 //
244 // Use: virtual protected
245
246 void
247 SoXtConstrainedViewer::bottomWheelMotion(float newVal)
248 //
249 ////////////////////////////////////////////////////////////////////////
250 {
251 if (camera == NULL)
252 return;
253
254 // get rotation and apply to camera
255 SbRotation rot(upDirection, bottomWheelVal - newVal);
256 camera->orientation = camera->orientation.getValue() * rot;
257
258 bottomWheelVal = newVal;
259 }
260
261 ////////////////////////////////////////////////////////////////////////
262 //
263 // Description:
264 // Tilts the camera up/down (called by thumb wheel).
265 //
266 // Use: virtual protected
267
268 void
269 SoXtConstrainedViewer::leftWheelMotion(float newVal)
270 //
271 ////////////////////////////////////////////////////////////////////////
272 {
273 tiltCamera(leftWheelVal - newVal);
274
275 leftWheelVal = newVal;
276 }
277
278 ////////////////////////////////////////////////////////////////////////
279 //
280 // Description:
281 // Tilts the camera, restraining it to 180 degree rotation from the
282 // up direction. A positive angle tilts the camera up.
283 //
284 // Use: protected
285
286 void
287 SoXtConstrainedViewer::tiltCamera(float deltaAngle)
288 //
289 ////////////////////////////////////////////////////////////////////////
290 {
291 if (camera == NULL)
292 return;
293
294 // get camera forward direction
295 SbMatrix mx;
296 mx = camera->orientation.getValue();
297 SbVec3f forward(-mx[2][0], -mx[2][1], -mx[2][2]);
298
299 // get the angle between the foward and up direction
300 // as well as the axis of rotation
301 SbRotation rot(forward, upDirection);
302 SbVec3f axis;
303 float angle;
304 rot.getValue(axis, angle);
305 // make angle in [-PI,PI] range
306 if (angle > M_PI)
307 angle -= 2*M_PI;
308 else if (angle < -M_PI)
309 angle += 2*M_PI;
310 // make rotation toward up direction positive angle
311 if (angle < 0.0) {
312 angle = -angle;
313 axis = -axis;
314 }
315
316 // check if we are already looking almost along the up direction
317 if ( (angle <= MIN_ANGLE && deltaAngle > 0) ||
318 (angle >= (M_PI - MIN_ANGLE) && deltaAngle < 0) )
319 return;
320
321 // clamp the angle change as to not get too close along the up direction
322 if (deltaAngle > 0 && deltaAngle > (angle - MIN_ANGLE))
323 deltaAngle = angle - MIN_ANGLE;
324 else if (deltaAngle < 0 && deltaAngle < (angle + MIN_ANGLE - M_PI))
325 deltaAngle = angle + MIN_ANGLE - M_PI;
326
327 // finally rotate the camera by the given angle
328 rot.setValue(axis, deltaAngle);
329 camera->orientation = camera->orientation.getValue() * rot;
330 }
331
332 ////////////////////////////////////////////////////////////////////////
333 //
334 // Description:
335 // computes what the final camera seek orientation should be.
336 //
337 // Use: virtual protected
338
339 void
340 SoXtConstrainedViewer::computeSeekFinalOrientation()
341 //
342 ////////////////////////////////////////////////////////////////////////
343 {
344 SbMatrix mx;
345 SbVec3f viewVector;
346
347 // find the camera final orientation
348 if ( isDetailSeek() ) {
349
350 // get the camera unconstrained new orientation
351 mx = camera->orientation.getValue();
352 viewVector.setValue(-mx[2][0], -mx[2][1], -mx[2][2]);
353 SbRotation changeOrient;
354 changeOrient.setValue(viewVector, seekPoint - camera->position.getValue());
355 newCamOrientation = camera->orientation.getValue() * changeOrient;
356
357 // check for constrains
358 camera->orientation = newCamOrientation;
359 checkForCameraUpConstrain();
360 newCamOrientation = camera->orientation.getValue();
361 }
362 else
363 newCamOrientation = camera->orientation.getValue();
364 }
365
366
367 ////////////////////////////////////////////////////////////////////////
368 //
369 // Description:
370 // This routine checks the camera orientation and makes sure that the
371 // current right vector and the ideal right vector (cross between the
372 // view vector and world up direction) are the same (i.e. no unwanted
373 // roll), else it fixes it. This keeps the up direction valid.
374 //
375 // Use: protected
376 //
377 void
378 SoXtConstrainedViewer::checkForCameraUpConstrain()
379 //
380 ////////////////////////////////////////////////////////////////////////
381 {
382 if (camera == NULL)
383 return;
384
385 // adjust the camera if necessary so that the new right vector
386 // lies in a plane parallel to our old right vector
387 SbMatrix mx;
388 mx = camera->orientation.getValue();
389 SbVec3f newForward(-mx[2][0], -mx[2][1], -mx[2][2]);
390 SbVec3f newRight(mx[0][0], mx[0][1], mx[0][2]);
391
392 // idealRight is the right vector computed from the pasted forward
393 // and the existing world up vector
394 SbVec3f idealRight = newForward.cross(upDirection);
395 idealRight.normalize();
396
397 // if idealRight is 0, then the newForward is looking in the
398 // same direction as the world up. In this case, we have to choose
399 // a right vector, so we choose the newRight. Otherwise, we have
400 // to rotate our orientation from the newRight to the idealRight.
401 if (idealRight != SbVec3f(0.0, 0.0, 0.0)) {
402 // rotate to idealRight!
403 SbRotation rot(newRight, idealRight);
404 camera->orientation.setValue(
405 camera->orientation.getValue() * rot);
406 }
407 }
408
409 ////////////////////////////////////////////////////////////////////////
410 //
411 // Description:
412 // Change the values of our camera to newCamera,
413 // keeping the right vector in a parallel plane.
414 // ??? animate from old values to new?
415 //
416 // Use: virtual, protected
417 //
418 void
419 SoXtConstrainedViewer::changeCameraValues(SoCamera *newCamera)
420 //
421 ////////////////////////////////////////////////////////////////////////
422 {
423 if (camera == NULL)
424 return;
425
426 // only paste cameras of the same type
427 if (camera->getTypeId() != newCamera->getTypeId())
428 return;
429
430 // let the base class copy camera values
431 SoXtFullViewer::changeCameraValues(newCamera);
432
433 // now check for constrains
434 checkForCameraUpConstrain();
435 }
436
437 ////////////////////////////////////////////////////////////////////////
438 //
439 // Description:
440 // Does a picking at the mouse location and sets the Up direction to
441 // the normal of the picked point.
442 // ??? should we animate
443 //
444 // Use: protected
445
446 void
447 SoXtConstrainedViewer::findUpDirection(const SbVec2s &mouseLocation)
448 //
449 ////////////////////////////////////////////////////////////////////////
450 {
451 if (camera == NULL)
452 return;
453
454 // do the picking
455 SbVec2s size = getGlxSize();
456 SoRayPickAction pick( size );
457 pick.setPoint(mouseLocation);
458 pick.setRadius(1.0);
459 pick.setPickAll(FALSE);
460 pick.apply(sceneRoot);
461
462 // makes sure something got picked, then
463 // get the picked point.
464 SoPickedPoint *pp = pick.getPickedPoint();
465 if (pp == NULL)
466 return;
467 SbVec3f normal = pp->getNormal();
468
469 // check whether the normal is pointing toward the camera, else
470 // flip the normal around.
471 SbVec3f point = pp->getPoint();;
472 if ( normal.dot(camera->position.getValue() - point) < 0 )
473 normal.negate();
474
475 //printf("%f %f %f\n", normal[0], normal[1], normal[2]);
476
477 setUpDirection(normal);
478 }