Debian Codegraph @

lib/database/src/sb/SbView.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.2 $
44 |
45 | Classes:
46 | SbViewVolume
47 |
48 | Author(s) : Nick Thompson, Paul S. Strauss
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54 #include <Inventor/SbLinear.h>
55 #include <Inventor/SbBox.h>
56 #include <cmath> // for M_PI_2
57
58 ////////////////////////////////////////////
59 //
60 // And now, the member functions...
61 //
62 ////////////////////////////////////////////
63
64 ////////////////////////////////////////////////////////////////////////
65 //
66 // Description:
67 // Yes, this should be inlineable
68 //
69 // Use: public
70
71 SbViewVolume::SbViewVolume()
72 //
73 ////////////////////////////////////////////////////////////////////////
74 {
75 }
76
77 ////////////////////////////////////////////////////////////////////////
78 //
79 // Description:
80 // Returns the affine*projection matrix corresponding to the view volume
81 //
82 // Use: public
83
84 SbMatrix
85 SbViewVolume::getMatrix() const
86 //
87 ////////////////////////////////////////////////////////////////////////
88 {
89 SbMatrix affine, proj;
90 getMatrices(affine, proj);
91
92 return affine.multRight( proj );
93 }
94
95 ////////////////////////////////////////////////////////////////////////
96 //
97 // Description:
98 // Returns two matrices corresponding to the view volume. The
99 // first is a viewing matrix, which is guaranteed to be an affine
100 // transformation. The second is suitable for use as a projection
101 // matrix in GL. This part about finding the projection matrix
102 // is taken from the old GT Graphics Library User's Guide, page B-3.
103 //
104 // Use: public
105
106 void
107 SbViewVolume::getMatrices(SbMatrix &affine, SbMatrix &proj) const
108 //
109 ////////////////////////////////////////////////////////////////////////
110 {
111 SbMatrix skewMat;
112
113 SbVec3f right = lrfO - llfO;
114 SbVec3f up = ulfO - llfO;
115
116 float width = right.length();
117 float height = up.length();
118
119 //
120 // skewMat is the matrix that would take a nice orthogonal view volume
121 // that is aligned with x,y and neg-z and skew it to this view volume,
122 // based on llfO being at the origin.
123 //
124 skewMat[0][0] = right[0]/width;
125 skewMat[0][1] = right[1]/width;
126 skewMat[0][2] = right[2]/width;
127 skewMat[0][3] = 0;
128
129 skewMat[1][0] = up[0]/height;
130 skewMat[1][1] = up[1]/height;
131 skewMat[1][2] = up[2]/height;
132 skewMat[1][3] = 0;
133
134 skewMat[2][0] = -projDir[0];
135 skewMat[2][1] = -projDir[1];
136 skewMat[2][2] = -projDir[2];
137 skewMat[2][3] = 0;
138
139 skewMat[3][0] = 0;
140 skewMat[3][1] = 0;
141 skewMat[3][2] = 0;
142 skewMat[3][3] = 1;
143
144 // Therefore, its inverse takes our probably rotated and potentially
145 // skewed view volume and makes it orthogonal (unskewed) and aligned
146 // with neg-z axis
147 SbMatrix skewMatInv = skewMat.inverse();
148
149 affine.setTranslate(-(llfO+projPoint));
150 affine.multRight( skewMatInv );
151
152 SbVec3f eye;
153 affine.multVecMatrix(projPoint, eye);
154
155 SbMatrix moveToEye;
156 moveToEye.setTranslate(- eye);
157 affine.multRight( moveToEye );
158
159 SbVec3f llfEye, lrfEye, ulfEye;
160 skewMatInv.multVecMatrix(llfO, llfEye);
161 skewMatInv.multVecMatrix(lrfO, lrfEye);
162 skewMatInv.multVecMatrix(ulfO, ulfEye);
163
164 proj = SbMatrix::identity();
165
166 // Convenient stuff for building the projection matrices
167 float rightMinusLeft = lrfEye[0] - llfEye[0];
168 float rightPlusLeft = lrfEye[0] + llfEye[0];
169
170 float topMinusBottom = ulfEye[1] - llfEye[1];
171 float topPlusBottom = ulfEye[1] + llfEye[1];
172
173 const float & farMinusNear = nearToFar;
174 float far = nearDist + nearToFar;
175 float farPlusNear = far + nearDist;
176
177 if (type == ORTHOGRAPHIC) {
178 proj[0][0] = 2.0 / rightMinusLeft;
179 proj[1][1] = 2.0 / topMinusBottom;
180 proj[2][2] = -2.0 / farMinusNear;
181
182 proj[3][0] = - rightPlusLeft / rightMinusLeft;
183 proj[3][1] = - topPlusBottom / topMinusBottom;
184 proj[3][2] = - farPlusNear / farMinusNear;
185 }
186 else { // type == PERSPECTIVE
187
188 proj[0][0] = 2.0 * nearDist / rightMinusLeft;
189
190 proj[1][1] = 2.0 * nearDist / topMinusBottom;
191
192 proj[2][0] = rightPlusLeft / rightMinusLeft;
193 proj[2][1] = topPlusBottom / topMinusBottom;
194 proj[2][2] = - farPlusNear / farMinusNear;
195 proj[2][3] = - 1.0;
196
197 proj[3][2] = - 2.0 * nearDist * far / farMinusNear;
198 proj[3][3] = 0.0;
199 }
200 }
201
202
203 ////////////////////////////////////////////////////////////////////////
204 //
205 // Description:
206 // Returns a matrix that transforms the view volume into camera
207 // space: it translates the view volume so the view point is at the
208 // origin, and rotates it so the view direction is along the
209 // negative z axis.
210 //
211 // Use: public
212
213 SbMatrix
214 SbViewVolume::getCameraSpaceMatrix() const
215 //
216 ////////////////////////////////////////////////////////////////////////
217 {
218 SbMatrix m, m2;
219
220 // Translate projPoint to (0,0,0)
221 m.setTranslate(- projPoint);
222
223 // Rotate projDir into negative z axis
224 m2.setRotate(SbRotation(projDir, SbVec3f(0.0, 0.0, -1.0)));
225 m *= m2;
226
227 return m;
228 }
229
230 ////////////////////////////////////////////////////////////////////////
231 //
232 // Description:
233 //
234 // Use: public
235
236 void
237 SbViewVolume::projectPointToLine(const SbVec2f &pt,
238 SbVec3f &line0, SbVec3f &line1) const
239 //
240 ////////////////////////////////////////////////////////////////////////
241 {
242 float ptx = 2.0 * pt[0] - 1.0;
243 float pty = 2.0 * pt[1] - 1.0;
244 SbMatrix mat = getMatrix().inverse();
245 float x, y, z, w;
246
247 /* ptz = -1 */
248 x = ptx*mat[0][0] + pty*mat[1][0] - mat[2][0] + mat[3][0];
249 y = ptx*mat[0][1] + pty*mat[1][1] - mat[2][1] + mat[3][1];
250 z = ptx*mat[0][2] + pty*mat[1][2] - mat[2][2] + mat[3][2];
251 w = ptx*mat[0][3] + pty*mat[1][3] - mat[2][3] + mat[3][3];
252 line0[0] = x/w;
253 line0[1] = y/w;
254 line0[2] = z/w;
255
256 /* ptz = +1 */
257 x += 2.0 * mat[2][0];
258 y += 2.0 * mat[2][1];
259 z += 2.0 * mat[2][2];
260 w += 2.0 * mat[2][3];
261 line1[0] = x/w;
262 line1[1] = y/w;
263 line1[2] = z/w;
264 }
265
266 ////////////////////////////////////////////////////////////////////////
267 //
268 // Description:
269 //
270 // Use: public
271
272 void
273 SbViewVolume::projectPointToLine(const SbVec2f &pt, SbLine &line) const
274 //
275 ////////////////////////////////////////////////////////////////////////
276 {
277 SbVec3f p0, p1;
278 projectPointToLine(pt, p0, p1);
279 line.setValue(p0, p1);
280 }
281
282 ////////////////////////////////////////////////////////////////////////
283 //
284 // Description:
285 // Projects a point in world coordinates to normalized screen
286 // coordinates.
287 //
288 // Use: public
289
290 void
291 SbViewVolume::projectToScreen(const SbVec3f &src, SbVec3f &dst) const
292 //
293 ////////////////////////////////////////////////////////////////////////
294 {
295 SbMatrix mat = getMatrix();
296
297 mat.multVecMatrix(src, dst);
298
299 // dst will now range from -1 to +1 in x, y, and z. Normalize this
300 // to range from 0 to 1.
301 dst[0] = (1.0 + dst[0]) / 2.0;
302 dst[1] = (1.0 + dst[1]) / 2.0;
303 dst[2] = (1.0 + dst[2]) / 2.0;
304 }
305
306 ////////////////////////////////////////////////////////////////////////
307 //
308 // Description:
309 // Returns a plane parallel to the near (or far) plane of the view
310 // volume at a given distance from the projection point (eye).
311 //
312 // Use: public
313
314 SbPlane
315 SbViewVolume::getPlane(float distFromEye) const
316 //
317 ////////////////////////////////////////////////////////////////////////
318 {
319 return SbPlane(-projDir, projPoint + distFromEye * projDir);
320 }
321
322 ////////////////////////////////////////////////////////////////////////
323 //
324 // Description:
325 // Returns the point along the line of sight at the given distance
326 // from the projection point (eye).
327 //
328 // Use: public
329
330 SbVec3f
331 SbViewVolume::getSightPoint(float distFromEye) const
332 //
333 ////////////////////////////////////////////////////////////////////////
334 {
335 return projPoint + distFromEye * projDir;
336 }
337
338 ////////////////////////////////////////////////////////////////////////
339 //
340 // Description:
341 // Returns the projection of a given point in normalized screen
342 // coords (see projectToScreen()) onto the plane parallel to the
343 // near plane that is at distFromEye units from the eye.
344 //
345 // Use: public
346
347 SbVec3f
348 SbViewVolume::getPlanePoint(float distFromEye, const SbVec2f &normPoint) const
349 //
350 ////////////////////////////////////////////////////////////////////////
351 {
352 SbLine line;
353 SbVec3f point;
354 SbPlane plane = getPlane(distFromEye);
355
356 // Project screen point to line through view volume
357 projectPointToLine(normPoint, line);
358
359 // This intersection should always be valid, since the plane is
360 // parallel to the near plane
361 plane.intersect(line, point);
362
363 return point;
364 }
365
366 ////////////////////////////////////////////////////////////////////////
367 //
368 // Description:
369 // Returns a rotation that would align a viewed object so that its
370 // positive x-axis (of its object space) is to the right in the
371 // view and it's positive y-axis is up. If rightAngleOnly is TRUE,
372 // it will come as close as it can to this goal by using only 90
373 // degree rotations.
374 //
375 // Use: public
376
377 SbRotation
378 SbViewVolume::getAlignRotation(SbBool rightAngleOnly) const
379 //
380 ////////////////////////////////////////////////////////////////////////
381 {
382 SbVec3f yAxis(0.0, 1.0, 0.0);
383 SbVec3f up = ulfO - llfO;
384 SbVec3f right = lrfO - llfO;
385 SbVec3f newRight;
386 SbMatrix rotMat;
387 SbRotation result;
388
389 up.normalize();
390 right.normalize();
391
392 if (! rightAngleOnly) {
393
394 // First rotate so that y-axis becomes "up".
395 result.setValue(yAxis, up);
396
397 // Then rotate about "up" so the x-axis becomes "right".
398 rotMat.setRotate(result);
399 rotMat.multDirMatrix(SbVec3f(1.0, 0.0, 0.0), newRight);
400
401 // Need to make certain that the rotation is phrased as a rot
402 // about the y axis. If we just use
403 // SbRotation(newRight,right), in the case where 'newRight' is
404 // opposite direction of 'right' the algorithm gives us a 180
405 // degree rot about z, not y, which screws things up.
406 float thetaCos = newRight.dot(right);
407 if (thetaCos < -0.99999) {
408 result *= SbRotation( SbVec3f(0,1,0), 3.14159 );
409 }
410 else {
411 result *= SbRotation(newRight, right);
412 }
413 }
414
415 else {
416 SbRotation rotToUp, rot1, rot2;
417 SbVec3f vec;
418 float d, max;
419 int i;
420
421 // Rotate to get the best possible rotation to put Y close to "up"
422 rotToUp.setValue(yAxis, up.getClosestAxis());
423
424 // Rotate to get the best possible rotation to put X close to "right".
425 rotMat.setRotate(rotToUp);
426 rotMat.multDirMatrix(SbVec3f(1.0, 0.0, 0.0), newRight);
427
428 // Find which of the 4 rotations that are multiples of 90
429 // degrees about the y-axis brings X closest to right
430
431 max = -237.4;
432
433 for (i = 0; i < 4; i++) {
434
435 // Rotate by -90, 0, 90, 180 degrees
436 rot1.setValue(yAxis, (i-1) * M_PI_2);
437
438 rot2 = rot1 * rotToUp;
439 rotMat.setRotate(rot2);
440 rotMat.multDirMatrix(newRight, vec);
441 d = vec.dot(right);
442 if (d > max) {
443 result = rot2;
444 max = d;
445 }
446 }
447 }
448
449 return result;
450 }
451
452 ////////////////////////////////////////////////////////////////////////
453 //
454 // Description:
455 // Returns a scale factor that would scale a unit sphere centered
456 // at worldCenter so that it would appear to have the given radius
457 // in normalized screen coordinates when projected onto the near
458 // plane.
459 //
460 // Use: public
461
462 float
463 SbViewVolume::getWorldToScreenScale(const SbVec3f &worldCenter,
464 float normRadius) const
465 //
466 ////////////////////////////////////////////////////////////////////////
467 {
468 // Project worldCenter into normalized coordinates
469 SbVec3f normCenter3;
470 projectToScreen(worldCenter, normCenter3);
471 SbVec2f normCenter( normCenter3[0], normCenter3[1] );
472
473
474 // This method really behaves best if you keep the normalized
475 // points within the (0,0) (1,1) range. So we shift the
476 // normCenter if necessary
477 SbBool centerShifted = FALSE;
478 if ( normCenter[0] < 0.0 ) {
479 normCenter[0] = 0.0;
480 centerShifted = TRUE;
481 }
482 if ( normCenter[0] > 1.0 ) {
483 normCenter[0] = 1.0;
484 centerShifted = TRUE;
485 }
486 if ( normCenter[1] < 0.0 ) {
487 normCenter[1] = 0.0;
488 centerShifted = TRUE;
489 }
490 if ( normCenter[1] > 1.0 ) {
491 normCenter[1] = 1.0;
492 centerShifted = TRUE;
493 }
494
495 // We'll either take the distance between a point that's offset
496 // vertically or horizontally from the original point. This
497 // depends on the aspect ration of the view.
498 //
499 // If it's wider-than-tall, we'll use the vertical offset since
500 // the area subtended in the world will not change vertically as
501 // we stretch horizontally until we reach a square aspec.
502 //
503 // If it's taller-than-wide, we'll use the hozizontal offset for
504 // similar reasons. Also, we'll offset up or down depending on
505 // which brings us more toward the center of the viewport, where
506 // results are better.
507
508 // Should we use a vertical or horizontal offset?
509 SbBool goVertical = (getWidth() > getHeight());
510
511 // Pick a point that is normRadius away from normCenter in the
512 // desired direction.,
513 SbVec2f offsetPoint = normCenter;
514 if ( goVertical ) {
515 if (offsetPoint[1] < 0.5)
516 offsetPoint[1] += normRadius;
517 else
518 offsetPoint[1] -= normRadius;
519 }
520 else {
521 if (offsetPoint[0] < 0.5)
522 offsetPoint[0] += normRadius;
523 else
524 offsetPoint[0] -= normRadius;
525 }
526
527 // The original method only works for perspective projections. The
528 // problem is in construction of 'plane.' For an ortho view, this
529 // should be a plane at the location worldCenter, but with a
530 // normal in the direction parallel to our line.
531
532 // Find centerLine, the line you get when you project normCenter into
533 // the scene.
534 SbLine centerLine;
535 projectPointToLine(normCenter, centerLine);
536
537 // Find the plane that passes through worldCenter and is
538 // perpendicular to the centerLine
539 SbVec3f norm = centerLine.getDirection();
540 norm.normalize();
541 SbPlane plane(norm, worldCenter);
542
543 // Project offsetPoint onto that plane and return distance from
544 // worldCenter.
545 SbLine offsetLine;
546 projectPointToLine(offsetPoint, offsetLine);
547
548 // Intersect centerLine with the plane to get the location of normCenter
549 // projected onto that plane. If we didn't need to shift the normCenter
550 // then this is just the same as worldCenter
551 SbVec3f worldSeedPoint = worldCenter;
552
553 if ( centerShifted == TRUE ) {
554 SbBool isOk = plane.intersect(centerLine, worldSeedPoint);
555
556 if ( !isOk ) {
557 // intersection did not succeeded, just return a value of 1
558 return 1.0;
559 }
560 }
561
562 // Fix uninitialized memory read. We need to check if the plane
563 // intersection is successful, and if not we must not use results
564 // to calculate an answer. Instead, if the plane intersection
565 // fails, we return 1.0
566 SbVec3f worldOffsetPoint;
567 SbBool isOk = plane.intersect(offsetLine, worldOffsetPoint);
568
569 if ( !isOk ) {
570 // intersection did not succeeded, just return a value of 1
571 return 1.0;
572 }
573
574 // intersection succeeded. return dist from worldCenter
575 float answerDist = (worldSeedPoint - worldOffsetPoint).length();
576
577 return answerDist;
578 }
579
580 ////////////////////////////////////////////////////////////////////////
581 //
582 // Description:
583 // Projects the given 3D bounding box onto the near plane and
584 // returns the size (in normalized screen coords) of the
585 // rectangular region that encloses it.
586 //
587 // Use: public
588
589 SbVec2f
590 SbViewVolume::projectBox(const SbBox3f &box) const
591 //
592 ////////////////////////////////////////////////////////////////////////
593 {
594 SbVec3f min, max, screenPoint[8];
595
596 box.getBounds(min, max);
597
598 // Project points to (0 <= x,y,z <= 1) screen coordinates
599 projectToScreen(SbVec3f(min[0], min[1], min[2]), screenPoint[0]);
600 projectToScreen(SbVec3f(min[0], min[1], max[2]), screenPoint[1]);
601 projectToScreen(SbVec3f(min[0], max[1], min[2]), screenPoint[2]);
602 projectToScreen(SbVec3f(min[0], max[1], max[2]), screenPoint[3]);
603 projectToScreen(SbVec3f(max[0], min[1], min[2]), screenPoint[4]);
604 projectToScreen(SbVec3f(max[0], min[1], max[2]), screenPoint[5]);
605 projectToScreen(SbVec3f(max[0], max[1], min[2]), screenPoint[6]);
606 projectToScreen(SbVec3f(max[0], max[1], max[2]), screenPoint[7]);
607
608 // Find the encompassing 2d box (-1 <= x,y <= 1)
609 SbBox2f fBox;
610 for (int i = 0; i < 8; i++)
611 fBox.extendBy(SbVec2f(screenPoint[i][0], screenPoint[i][1]));
612
613 // Return size of box
614 SbVec2f size;
615 fBox.getSize(size[0], size[1]);
616 return size;
617 }
618
619 ////////////////////////////////////////////////////////////////////////
620 //
621 // Description:
622 // Given a view volume, this narrows the view to the given sub-rectangle
623 // of the near plane. The coordinates of the rectangle are between
624 // 0 and 1, where (0,0) is the lower-left corner of the near plane
625 // and (1,1) is the upper-right corner.
626 //
627 // Use: public
628
629 SbViewVolume
630 SbViewVolume::narrow(float left, float bottom, float right, float top) const
631 //
632 ////////////////////////////////////////////////////////////////////////
633 {
634 SbViewVolume vv;
635
636 vv.type = type;
637 vv.projPoint = projPoint;
638 vv.projDir = projDir;
639 vv.llfO = (lrfO - llfO) * left + (ulfO - llfO) * bottom + llfO;
640 vv.lrfO = (lrfO - llfO) * right + (ulfO - llfO) * bottom + llfO;
641 vv.ulfO = (lrfO - llfO) * left + (ulfO - llfO) * top + llfO;
642 vv.llf = vv.llfO + vv.projPoint; // For compatibility
643 vv.lrf = vv.lrfO + vv.projPoint;
644 vv.ulf = vv.ulfO + vv.projPoint;
645 vv.nearDist = nearDist;
646 vv.nearToFar = nearToFar;
647
648 return vv;
649 }
650
651 ////////////////////////////////////////////////////////////////////////
652 //
653 // Description:
654 // Sets up an orthographic view volume with the given sides.
655 // The parameters are the same as for the GL ortho() routine.
656 //
657 // Use: public
658
659 void
660 SbViewVolume::ortho(float left, float right,
661 float bottom, float top,
662 float near, float far)
663 //
664 ////////////////////////////////////////////////////////////////////////
665 {
666 type = ORTHOGRAPHIC;
667 projPoint = SbVec3f(0.0, 0.0, 0.0);
668 projDir = SbVec3f(0.0, 0.0, -1.0);
669 llfO = SbVec3f(left, bottom, -near);
670 lrfO = SbVec3f(right, bottom, -near);
671 ulfO = SbVec3f(left, top, -near);
672 llf = llfO + projPoint; // For compatibility
673 lrf = lrfO + projPoint;
674 ulf = ulfO + projPoint;
675 nearDist = near;
676 nearToFar = far - near;
677 }
678
679 ////////////////////////////////////////////////////////////////////////
680 //
681 // Description:
682 // Sets up a perspective view volume with the given field of view
683 // and aspect ratio. The parameters are the same as for the GL
684 // perspective() routine, except that the field of view angle is
685 // specified in radians.
686 //
687 // Use: public
688
689 void
690 SbViewVolume::perspective(float fovy, float aspect,
691 float near, float far)
692 //
693 ////////////////////////////////////////////////////////////////////////
694 {
695 float tanfov = tan(fovy / 2.0);
696
697 type = PERSPECTIVE;
698
699 projPoint.setValue(0.0, 0.0, 0.0);
700 projDir.setValue(0.0, 0.0, -1.0);
701 llfO[2] = lrfO[2] = ulfO[2] = - near;
702
703 ulfO[1] = near * tanfov;
704 llfO[1] = lrfO[1] = - ulfO[1];
705 ulfO[0] = llfO[0] = aspect * llfO[1];
706 lrfO[0] = - llfO[0];
707
708 llf = llfO + projPoint; // For compatibility
709 lrf = lrfO + projPoint;
710 ulf = ulfO + projPoint;
711
712 nearDist = near;
713 nearToFar = far - near;
714 }
715
716 ////////////////////////////////////////////////////////////////////////
717 //
718 // Description:
719 // Translate the camera viewpoint. Note that this accomplishes
720 // the reverse of doing a GL translate() command after defining a
721 // camera, which translates the scene viewed by the camera.
722 //
723 // Use: public
724
725 void
726 SbViewVolume::translateCamera(const SbVec3f &v)
727 //
728 ////////////////////////////////////////////////////////////////////////
729 {
730 projPoint += v;
731 llf += v;
732 lrf += v;
733 ulf += v;
734 }
735
736 ////////////////////////////////////////////////////////////////////////
737 //
738 // Description:
739 // Rotate the camera view direction. Note that this accomplishes
740 // the reverse of doing a GL rotate() command after defining a
741 // camera, which rotates the scene viewed by the camera.
742 //
743 // Use: public
744
745 void
746 SbViewVolume::rotateCamera(const SbRotation &r)
747 //
748 ////////////////////////////////////////////////////////////////////////
749 {
750 SbMatrix m;
751 m.setRotate(r);
752
753 m.multDirMatrix(projDir, projDir);
754
755 m.multDirMatrix(llfO, llfO);
756 m.multDirMatrix(lrfO, lrfO);
757 m.multDirMatrix(ulfO, ulfO);
758
759 llf = llfO + projPoint; // For compatibility
760 lrf = lrfO + projPoint;
761 ulf = ulfO + projPoint;
762 }
763
764 ////////////////////////////////////////////////////////////////////////
765 //
766 // Description:
767 // Returns the vector that will map to the positive z axis in eye
768 // space. This vector will be perpendicular to the near and far
769 // clipping planes. In this coordinate system, the z value of the
770 // near plane should be GREATER than the z value of the far plane.
771 //
772 // Use: public
773
774 SbVec3f
775 SbViewVolume::zVector() const
776 //
777 ////////////////////////////////////////////////////////////////////////
778 {
779 SbPlane plane;
780
781 // note dependency on how the plane is calculated: we want the
782 // returned vector to point away from the projDir
783 plane = SbPlane(llfO, ulfO, lrfO);
784
785 // If we wanted a world-space plane, would also have to translate
786 // it to projPoint. But all we're interested in here is the normal:
787 return - plane.getNormal();
788 }
789
790 ////////////////////////////////////////////////////////////////////////
791 //
792 // Description:
793 // Returns a narrowed view volume which contains as tightly as
794 // possible the given interval on the z axis (in eye space). The
795 // returned view volume will never be larger than the current volume,
796 // however.
797 //
798 // Near and far are relative to the current clipping planes, where
799 // 1.0 is the current near plane and 0.0 is the current far plane.
800 //
801 // Use: public
802
803 SbViewVolume
804 SbViewVolume::zNarrow(float near, float far) const
805 //
806 ////////////////////////////////////////////////////////////////////////
807 {
808 SbPlane plane;
809 SbViewVolume narrowed;
810 SbVec3f zVec = zVector();
811
812 // make sure we aren't expanding the volume
813 if (near > 1.0)
814 near = 1.0;
815 if (far < 0.0)
816 far = 0.0;
817
818 narrowed.type = type;
819 narrowed.projPoint = projPoint;
820 narrowed.projDir = projDir;
821
822 // the near-to-far distance can be calculated from the new near
823 // and far values
824 narrowed.nearDist = near;
825 narrowed.nearToFar = (near - far) * nearToFar;
826
827 // the new near plane
828 // find the old near plane
829 plane = SbPlane(zVec, llfO);
830 // offset it
831 plane.offset((near - 1.0) * nearToFar);
832
833 // intersect various lines with the new near plane to find the new
834 // info for the view volume
835 if (type == ORTHOGRAPHIC) {
836 plane.intersect(SbLine(llfO, llfO + projDir), narrowed.llfO);
837 plane.intersect(SbLine(lrfO, lrfO + projDir), narrowed.lrfO );
838 plane.intersect(SbLine(ulfO, ulfO + projDir), narrowed.ulfO );
839 }
840 else { // type == PERSPECTIVE
841 SbVec3f origin(0,0,0);
842 plane.intersect(SbLine(origin, llfO), narrowed.llfO );
843 plane.intersect(SbLine(origin, lrfO), narrowed.lrfO );
844 plane.intersect(SbLine(origin, ulfO), narrowed.ulfO );
845 }
846
847 narrowed.llf = narrowed.llfO+narrowed.projPoint; // For compatibility
848 narrowed.lrf = narrowed.lrfO+narrowed.projPoint;
849 narrowed.ulf = narrowed.ulfO+narrowed.projPoint;
850
851 return narrowed;
852 }
853
854 ////////////////////////////////////////////////////////////////////////
855 //
856 // Description:
857 // Scales width and height of view volume by given factor.
858 //
859 // Use: public
860
861 void
862 SbViewVolume::scale(float factor)
863 //
864 ////////////////////////////////////////////////////////////////////////
865 {
866 // Don't like negative factors:
867 if (factor < 0) factor = -factor;
868
869 // Compute amount to move corners
870 float diff = (1.0 - factor) / 2.0;
871
872 // Find vectors from lower left corner to lower right corner and
873 // to upper left corner and scale them
874 SbVec3f widthVec = diff * (lrfO - llfO);
875 SbVec3f heightVec = diff * (ulfO - llfO);
876
877 // Move all corners in correct direction
878 llfO += ( heightVec + widthVec);
879 lrfO += ( heightVec - widthVec);
880 ulfO += (-heightVec + widthVec);
881
882 llf = llfO + projPoint; // For compatibility
883 lrf = lrfO + projPoint;
884 ulf = ulfO + projPoint;
885 }
886
887 ////////////////////////////////////////////////////////////////////////
888 //
889 // Description:
890 // Scales view volume to be the given ratio of its current width,
891 // leaving the resulting view volume centered about the same point
892 // (in the near plane) as the current one.
893 //
894 // Use: public
895
896 void
897 SbViewVolume::scaleWidth(float ratio)
898 //
899 ////////////////////////////////////////////////////////////////////////
900 {
901 // Don't like negative ratios:
902 if (ratio < 0) ratio = -ratio;
903
904 // Find vector from lower left corner to lower right corner
905 SbVec3f widthVec = lrfO - llfO;
906
907 // Compute amount to move corners left or right and scale vector
908 widthVec *= (1.0 - ratio) / 2.0;
909
910 // Move all corners in correct direction
911 llfO += widthVec;
912 ulfO += widthVec;
913 lrfO -= widthVec;
914
915 llf = llfO + projPoint; // For compatibility
916 lrf = lrfO + projPoint;
917 ulf = ulfO + projPoint;
918 }
919
920 ////////////////////////////////////////////////////////////////////////
921 //
922 // Description:
923 // Scales view volume to be the given ratio of its current height,
924 // leaving the resulting view volume centered about the same point
925 // (in the near plane) as the current one.
926 //
927 // Use: public
928
929 void
930 SbViewVolume::scaleHeight(float ratio)
931 //
932 ////////////////////////////////////////////////////////////////////////
933 {
934 // Don't like negative ratios:
935 if (ratio < 0) ratio = -ratio;
936
937 // Find vector from lower left corner to upper left corner
938 SbVec3f heightVec = ulfO - llfO;
939
940 // Compute amount to move corners up or down and scale vector
941 heightVec *= (1.0 - ratio) / 2.0;
942
943 // Move all corners in correct direction
944 llfO += heightVec;
945 lrfO += heightVec;
946 ulfO -= heightVec;
947
948 llf = llfO + projPoint; // For compatibility
949 lrf = lrfO + projPoint;
950 ulf = ulfO + projPoint;
951 }
952
953 ////////////////////////////////////////////////////////////////////////
954 //
955 // Description:
956 //
957 // Use: public
958
959 SbViewVolume
960 SbViewVolume::narrow(const SbBox3f &box) const
961 //
962 ////////////////////////////////////////////////////////////////////////
963 {
964 SbViewVolume view;
965 const SbVec3f &max = box.getMax(), &min = box.getMin();
966
967 view = narrow(min[0], min[1], max[0], max[1]);
968
969 return view.zNarrow(max[2], min[2]);
970 }
971
972 ////////////////////////////////////////////////////////////////////////
973 //
974 // Description:
975 // Transforms the view volume by the given matrix.
976 // NOTE: If there is a scale and a rotation in the matrix, the angles
977 // between the transformed projection direction and everything else
978 // are not preserved!
979 //
980 // Use: internal
981
982 void
983 SbViewVolume::transform(const SbMatrix &matrix)
984 //
985 ////////////////////////////////////////////////////////////////////////
986 {
987 SbViewVolume xfVol;
988 SbVec3f nearPt, farPt;
989
990 xfVol.type = type;
991 matrix.multVecMatrix(projPoint, xfVol.projPoint);
992 matrix.multDirMatrix(projDir, xfVol.projDir);
993 xfVol.projDir.normalize();
994
995 // Matrices that translate to/from origin-centered space.
996 // We want to find llf', and we know that:
997 // llf'+projPoint' = matrix*(llf+projPoint)
998
999 matrix.multVecMatrix((llfO+projPoint), xfVol.llfO);
1000 xfVol.llfO -= xfVol.projPoint;
1001 matrix.multVecMatrix((lrfO+projPoint), xfVol.lrfO);
1002 xfVol.lrfO -= xfVol.projPoint;
1003 matrix.multVecMatrix((ulfO+projPoint), xfVol.ulfO);
1004 xfVol.ulfO -= xfVol.projPoint;
1005
1006 matrix.multVecMatrix(projPoint + nearDist * projDir, nearPt);
1007 matrix.multVecMatrix(projPoint + (nearDist + nearToFar) * projDir, farPt);
1008 xfVol.nearDist = (nearPt - xfVol.projPoint).length();
1009 if (nearDist < 0)
1010 xfVol.nearDist = -xfVol.nearDist;
1011 xfVol.nearToFar = (farPt - xfVol.projPoint).length() - xfVol.nearDist;
1012
1013 *this = xfVol;
1014
1015 // Check for inside-out view volume:
1016 SbVec3f wVec = lrfO-llfO;
1017 SbVec3f hVec = ulfO-llfO;
1018 if ((hVec.cross(wVec)).dot(projDir) <= 0.0) {
1019 // Swap left and right:
1020 SbVec3f temp = llfO;
1021 llfO = lrfO;
1022 lrfO = temp;
1023 ulfO = llfO+hVec;
1024 }
1025
1026 llf = llfO + projPoint; // For compatibility
1027 lrf = lrfO + projPoint;
1028 ulf = ulfO + projPoint;
1029 }
1030
1031 ////////////////////////////////////////////////////////////////////////
1032 //
1033 // Description:
1034 // Intersects view volume with point. Returns TRUE if intersection.
1035 //
1036 // Use: internal
1037
1038 SbBool
1039 SbViewVolume::intersect(const SbVec3f &point) const
1040 //
1041 ////////////////////////////////////////////////////////////////////////
1042 {
1043 // Transform point to origin:
1044 SbVec3f pt = point - projPoint;
1045
1046 // Compare the point with all 6 planes of the view volume
1047 SbVec3f origin(0,0,0);
1048
1049 if (type == PERSPECTIVE) {
1050 // Left plane is formed from origin, llfO, ulfO
1051 SbPlane leftPlane(origin, llfO, ulfO);
1052 if (! leftPlane.isInHalfSpace(pt))
1053 return FALSE;
1054
1055 // Figure out urf point:
1056 SbVec3f urfO = lrfO + (ulfO - llfO);
1057 // Right is origin, urfO, lrfO
1058 SbPlane rightPlane(origin, urfO, lrfO);
1059 if (! rightPlane.isInHalfSpace(pt))
1060 return FALSE;
1061
1062 // Near is lrfO, llfO, ulfO:
1063 SbPlane nearPlane(lrfO, llfO, ulfO);
1064 if (! nearPlane.isInHalfSpace(pt))
1065 return FALSE;
1066
1067 // Far is near points in opposite order, translated to far plane:
1068 SbVec3f farOffset = nearToFar * projDir;
1069 SbPlane farPlane(ulfO+farOffset, llfO+farOffset, lrfO+farOffset);
1070 if (! farPlane.isInHalfSpace(pt))
1071 return FALSE;
1072
1073 // Bottom is origin, lrfO, llfO
1074 SbPlane bottomPlane(origin, lrfO, llfO);
1075 if (! bottomPlane.isInHalfSpace(pt))
1076 return FALSE;
1077
1078 // Finally, top is origin, ulfO, urfO
1079 SbPlane topPlane(origin, ulfO, urfO);
1080 if (! topPlane.isInHalfSpace(pt))
1081 return FALSE;
1082 }
1083
1084 else { // type == ORTHOGRAPHIC
1085 // Left plane is formed from llfO, lff+projDir, ulfO
1086 SbPlane leftPlane(llfO, llfO+projDir, ulfO);
1087 if (! leftPlane.isInHalfSpace(pt))
1088 return FALSE;
1089
1090 // Figure out urfO point:
1091 SbVec3f urfO = lrfO + (ulfO - llfO);
1092 // Right is urfO+projDir, lrfO, urfO
1093 SbPlane rightPlane(urfO+projDir, lrfO, urfO);
1094 if (! rightPlane.isInHalfSpace(pt))
1095 return FALSE;
1096
1097 // Near is lrfO, llfO, ulfO:
1098 SbPlane nearPlane(lrfO, llfO, ulfO);
1099 if (! nearPlane.isInHalfSpace(pt))
1100 return FALSE;
1101
1102 // Far is near points in opposite order, translated to far plane:
1103 SbVec3f farOffset = nearToFar * projDir;
1104 SbPlane farPlane(ulfO+farOffset, llfO+farOffset, lrfO+farOffset);
1105 if (! farPlane.isInHalfSpace(pt))
1106 return FALSE;
1107
1108 // Bottom is lrfO, lrfO+projDir, lrfO, llfO
1109 SbPlane bottomPlane(lrfO, lrfO+projDir, llfO);
1110 if (! bottomPlane.isInHalfSpace(pt))
1111 return FALSE;
1112
1113 // Finally, top is urfO, ulfO, ulfO+projDir
1114 SbPlane topPlane(urfO, ulfO, ulfO+projDir);
1115 if (! topPlane.isInHalfSpace(pt))
1116 return FALSE;
1117 }
1118
1119 return TRUE;
1120 }
1121
1122 ////////////////////////////////////////////////////////////////////////
1123 //
1124 // Description:
1125 // Intersects view volume with line segment. Returns TRUE if intersection.
1126 //
1127 // Use: internal
1128
1129 SbBool
1130 SbViewVolume::intersect(const SbVec3f &p0, const SbVec3f &p1,
1131 SbVec3f &closestPoint) const
1132 //
1133 ////////////////////////////////////////////////////////////////////////
1134 {
1135 SbLine line(p0, p1);
1136 SbLine projLine;
1137 SbVec3f centerPt, ptOnProjLine;
1138
1139 // Use line from projection point through center of near rectangle
1140 centerPt = llfO+projPoint + 0.5 * ((ulfO - llfO) + (lrfO - llfO));
1141 if (type == ORTHOGRAPHIC)
1142 // projDir is normalized, so it might not be in the same
1143 // ballpark as centerPt. To fix this, use the vector from the
1144 // near plane to the far plane.
1145 projLine.setValue(centerPt - nearToFar * projDir, centerPt);
1146 else
1147 projLine.setValue(projPoint, centerPt);
1148
1149 SbBool validIntersection =
1150
1151 // Find point on segment that's closest to projection line.
1152 // (If they are parallel, no intersection.)
1153 (line.getClosestPoints(projLine, closestPoint, ptOnProjLine) &&
1154
1155 // Make sure this point is within the ends of the segment
1156 ((closestPoint - p0).dot(p1 - p0) >= 0.0 &&
1157 (closestPoint - p1).dot(p0 - p1) >= 0.0) &&
1158
1159 // Also make sure that the intersection is within the view volume
1160 intersect(closestPoint));
1161
1162 return validIntersection;
1163 }
1164
1165 ////////////////////////////////////////////////////////////////////////
1166 //
1167 // Description:
1168 // Intersects view volume with box. Returns TRUE if intersection.
1169 //
1170 // Use: internal
1171
1172 SbBool
1173 SbViewVolume::intersect(const SbBox3f &box) const
1174 //
1175 ////////////////////////////////////////////////////////////////////////
1176 {
1177 // Empty bboxes can cause problems:
1178 if (box.isEmpty()) return FALSE;
1179
1180 //
1181 // The bounding box is the set of all points between its bounds;
1182 // that is, all points (x,y,z) such that:
1183 // x_min <= x <= x_max
1184 // y_min <= y <= y_max
1185 // z_min <= z <= z_max
1186 // (Inventor bounding boxes are axis-aligned)
1187 //
1188 // We will consider there to be no intersection if all of those
1189 // points are on the 'outside' side of any of the view-volume
1190 // planes (this may pass some things that could be culled, but
1191 // will never cull things that are visible). So we want to see if
1192 // any point is on the inside, in which case the intersection
1193 // returns TRUE.
1194 //
1195 // The equation of the view-volume planes is Ax+By+Cz=D, where
1196 // (A,B,C) is the plane normal and D is the distance from the
1197 // origin. The condition for a point (x,y,z) being on the
1198 // 'outside' side is Ax+By+Cz-D < 0. We need to test the largest
1199 // value of Ax+By+Cz-D to see if it is negative. If it is, then we
1200 // know all points are outside.
1201 //
1202 // We can minimize the number of point/plane checks we do by
1203 // noticing that Ax+By+Cx-D will be greatest when each of its
1204 // terms is greatest; for example, if A is positive, Ax will be
1205 // greatest when x is x_max. If A is negative, Ax will be greatest
1206 // when x is x_min.
1207 // So, we can reduce the test to a check of one point against each
1208 // of the 6 plane equations. The outsideTest() method does this.
1209 //
1210
1211 SbVec3f min, max;
1212 box.getBounds(min, max);
1213
1214 // Transform bbox to origin:
1215 min -= projPoint;
1216 max -= projPoint;
1217
1218 // OPPORTUNITY FOR OPTIMIZATION HERE: We could precompute planes
1219 // and save some work.
1220
1221 SbVec3f origin(0,0,0);
1222
1223 if (type == PERSPECTIVE) {
1224 // Left plane is formed from origin, llfO, ulfO
1225 SbPlane leftPlane(origin, llfO, ulfO);
1226 if (outsideTest(leftPlane, min, max))
1227 return FALSE;
1228
1229 // Figure out urf point:
1230 SbVec3f urfO = lrfO + (ulfO - llfO);
1231 // Right is origin, urfO, lrfO
1232 SbPlane rightPlane(origin, urfO, lrfO);
1233 if (outsideTest(rightPlane, min, max))
1234 return FALSE;
1235
1236 // Near is lrfO, llfO, ulfO:
1237 SbPlane nearPlane(lrfO, llfO, ulfO);
1238 if (outsideTest(nearPlane, min, max))
1239 return FALSE;
1240
1241 // Far is near points in opposite order, translated to far plane:
1242 SbVec3f farOffset = nearToFar * projDir;
1243 SbPlane farPlane(ulfO+farOffset, llfO+farOffset, lrfO+farOffset);
1244 if (outsideTest(farPlane, min, max))
1245 return FALSE;
1246
1247 // Bottom is origin, lrfO, llfO
1248 SbPlane bottomPlane(origin, lrfO, llfO);
1249 if (outsideTest(bottomPlane, min, max))
1250 return FALSE;
1251
1252 // Finally, top is origin, ulfO, urfO
1253 SbPlane topPlane(origin, ulfO, urfO);
1254 if (outsideTest(topPlane, min, max))
1255 return FALSE;
1256 }
1257
1258 else { // type == ORTHOGRAPHIC
1259 // Left plane is formed from llfO, lff+projDir, ulfO
1260 SbPlane leftPlane(llfO, llfO+projDir, ulfO);
1261 if (outsideTest(leftPlane, min, max))
1262 return FALSE;
1263
1264 // Figure out urfO point:
1265 SbVec3f urfO = lrfO + (ulfO - llfO);
1266 // Right is urfO+projDir, lrfO, urfO
1267 SbPlane rightPlane(urfO+projDir, lrfO, urfO);
1268 if (outsideTest(rightPlane, min, max))
1269 return FALSE;
1270
1271 // Near is lrfO, llfO, ulfO:
1272 SbPlane nearPlane(lrfO, llfO, ulfO);
1273 if (outsideTest(nearPlane, min, max))
1274 return FALSE;
1275
1276 // Far is near points in opposite order, translated to far plane:
1277 SbVec3f farOffset = nearToFar * projDir;
1278 SbPlane farPlane(ulfO+farOffset, llfO+farOffset, lrfO+farOffset);
1279 if (outsideTest(farPlane, min, max))
1280 return FALSE;
1281
1282 // Bottom is lrfO, lrfO+projDir, lrfO, llfO
1283 SbPlane bottomPlane(lrfO, lrfO+projDir, llfO);
1284 if (outsideTest(bottomPlane, min, max))
1285 return FALSE;
1286
1287 // Finally, top is urfO, ulfO, ulfO+projDir
1288 SbPlane topPlane(urfO, ulfO, ulfO+projDir);
1289 if (outsideTest(topPlane, min, max))
1290 return FALSE;
1291 }
1292
1293 return TRUE;
1294 }
1295
1296 ////////////////////////////////////////////////////////////////////////
1297 //
1298 // Description:
1299 // Returns TRUE if the bounding box defined by the two given points
1300 // is totally outside the given plane. See the comments in
1301 // intersect(bbox) for details.
1302 //
1303 // Use: internal
1304
1305 SbBool
1306 SbViewVolume::outsideTest(const SbPlane &p,
1307 const SbVec3f &min, const SbVec3f &max) const
1308 //
1309 ////////////////////////////////////////////////////////////////////////
1310 {
1311 const SbVec3f &abc = p.getNormal();
1312 float sum;
1313
1314 // Compute the greatest value of Ax+By+Cz-D
1315 sum = -p.getDistanceFromOrigin(); // -D
1316 sum += abc[0] > 0.0 ? max[0] * abc[0] : min[0] * abc[0]; // Ax
1317 sum += abc[1] > 0.0 ? max[1] * abc[1] : min[1] * abc[1]; // By
1318 sum += abc[2] > 0.0 ? max[2] * abc[2] : min[2] * abc[2]; // Cz
1319
1320 // Box is outside only if largest value is negative
1321 return (sum < 0.0 ? TRUE : FALSE);
1322 }