Debian Codegraph @

lib/database/src/so/nodes/SoTransform.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:
46 | SoTransform
47 |
48 | Author(s) : 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/actions/SoCallbackAction.h>
55 #include <Inventor/actions/SoGLRenderAction.h>
56 #include <Inventor/actions/SoGetBoundingBoxAction.h>
57 #include <Inventor/actions/SoGetMatrixAction.h>
58 #include <Inventor/actions/SoPickAction.h>
59 #include <Inventor/elements/SoBBoxModelMatrixElement.h>
60 #include <Inventor/elements/SoGLModelMatrixElement.h>
61 #include <Inventor/elements/SoLocalBBoxMatrixElement.h>
62 #include <Inventor/elements/SoModelMatrixElement.h>
63 #include <Inventor/errors/SoDebugError.h>
64 #include <Inventor/nodes/SoTransform.h>
65
66 SO_NODE_SOURCE(SoTransform);
67
68 // Shorthand for accumulating a rotation into matrix and inverse using
69 // intermediate matrix m. Destructive to "rot".
70 #define ROTATE(rot, matrix, inverse, m) \
71 rot.getValue(m); \
72 matrix.multLeft(m); \
73 rot.invert(); \
74 rot.getValue(m); \
75 inverse.multRight(m)
76
77 // Shorthand for accumulating a scale vector into matrix and inverse
78 // using intermediate matrix m. Destructive to "vec".
79 #define SCALE(vec, matrix, inverse, m) \
80 m.setScale(vec); \
81 matrix.multLeft(m); \
82 vec[0] = 1.0 / vec[0]; \
83 vec[1] = 1.0 / vec[1]; \
84 vec[2] = 1.0 / vec[2]; \
85 m.setScale(vec); \
86 inverse.multRight(m)
87
88 // Shorthand for accumulating a translation vector into matrix and
89 // inverse using intermediate matrix m. Non-destructive to "vec".
90 #define TRANSLATE(vec, matrix, inverse, m) \
91 m.setTranslate(vec); \
92 matrix.multLeft(m); \
93 m.setTranslate(-vec); \
94 inverse.multRight(m)
95
96 ////////////////////////////////////////////////////////////////////////
97 //
98 // Description:
99 // Constructor
100 //
101 // Use: public
102
103 SoTransform::SoTransform()
104 //
105 ////////////////////////////////////////////////////////////////////////
106 {
107 SO_NODE_CONSTRUCTOR(SoTransform);
108 SO_NODE_ADD_FIELD(translation, (0.0, 0.0, 0.0));
109 SO_NODE_ADD_FIELD(rotation, (0.0, 0.0, 0.0, 1.0));
110 SO_NODE_ADD_FIELD(scaleFactor, (1.0, 1.0, 1.0));
111 SO_NODE_ADD_FIELD(scaleOrientation,(0.0, 0.0, 0.0, 1.0));
112 SO_NODE_ADD_FIELD(center, (0.0, 0.0, 0.0));
113 isBuiltIn = TRUE;
114 }
115
116 ////////////////////////////////////////////////////////////////////////
117 //
118 // Description:
119 // Destructor
120 //
121 // Use: private
122
123 SoTransform::~SoTransform()
124 //
125 ////////////////////////////////////////////////////////////////////////
126 {
127 }
128
129 ////////////////////////////////////////////////////////////////////////
130 //
131 // Description:
132 // Sets the transform to translate the origin to the fromPoint and
133 // rotate the negative z-axis (0,0,-1) to lie on the vector from
134 // fromPoint to toPoint. This always tries to keep the "up"
135 // direction the positive y-axis, unless that is impossible.
136 // All current info in the node is wiped out.
137 //
138 // Use: public
139
140 void
141 SoTransform::pointAt(const SbVec3f &fromPoint, const SbVec3f &toPoint)
142 //
143 ////////////////////////////////////////////////////////////////////////
144 {
145 // Wipe out all transformation info
146 setToDefaults();
147
148 // Translate origin to fromPoint
149 translation = fromPoint;
150
151 // Rotate negative z-axis to correct vector
152 // ??? This does not preserve "up" direction - fix it later!!!! ???
153 rotation.setValue(SbRotation(SbVec3f(0.0, 0.0, -1.0), toPoint - fromPoint));
154 }
155
156 ////////////////////////////////////////////////////////////////////////
157 //
158 // Description:
159 // Returns composite matrix that takes you from object space to
160 // scale space.
161 //
162 // Use: public
163
164 void
165 SoTransform::getScaleSpaceMatrix(SbMatrix &mat, SbMatrix &inv) const
166 //
167 ////////////////////////////////////////////////////////////////////////
168 {
169 SbMatrix m;
170
171 mat.makeIdentity();
172 inv.makeIdentity();
173
174 if (! scaleFactor.isIgnored() && ! scaleFactor.isDefault()) {
175 SbVec3f s = scaleFactor.getValue();
176 SCALE(s, mat, inv, m);
177
178 if (! scaleOrientation.isIgnored() && ! scaleOrientation.isDefault()) {
179 SbRotation r = scaleOrientation.getValue();
180 r.invert();
181 ROTATE(r, mat, inv, m);
182 }
183
184 }
185 if (! center.isIgnored() && ! center.isDefault()) {
186 SbVec3f c = center.getValue();
187 c.negate();
188 TRANSLATE(c, mat, inv, m);
189 }
190 }
191
192 ////////////////////////////////////////////////////////////////////////
193 //
194 // Description:
195 // Returns composite matrix that takes you from object space to
196 // rotation space.
197 //
198 // Use: public
199
200 void
201 SoTransform::getRotationSpaceMatrix(SbMatrix &mat, SbMatrix &inv) const
202 //
203 ////////////////////////////////////////////////////////////////////////
204 {
205 SbMatrix m, minv;
206
207 mat.makeIdentity();
208 inv.makeIdentity();
209
210 if (! rotation.isIgnored() && ! rotation.isDefault()) {
211 SbRotation r = rotation.getValue();
212 ROTATE(r, mat, inv, m);
213 }
214
215 if ((! scaleFactor.isIgnored() && ! scaleFactor.isDefault()) ||
216 (! center.isIgnored() && ! center.isDefault())) {
217
218 if (! scaleOrientation.isIgnored() && ! scaleOrientation.isDefault()) {
219 SbRotation r = scaleOrientation.getValue();
220 ROTATE(r, mat, inv, m);
221 }
222
223 getScaleSpaceMatrix(m, minv);
224 mat.multLeft(m);
225 inv.multRight(minv);
226 }
227 }
228
229 ////////////////////////////////////////////////////////////////////////
230 //
231 // Description:
232 // Returns composite matrix that takes you from object space to
233 // translation space.
234 //
235 // Use: public
236
237 void
238 SoTransform::getTranslationSpaceMatrix(SbMatrix &mat, SbMatrix &inv) const
239 //
240 ////////////////////////////////////////////////////////////////////////
241 {
242 SbMatrix m, minv;
243
244 mat.makeIdentity();
245 inv.makeIdentity();
246
247 if (! translation.isIgnored() && ! translation.isDefault()) {
248 const SbVec3f &v = translation.getValue();
249 TRANSLATE(v, mat, inv, m);
250 }
251
252 if (! center.isIgnored() && ! center.isDefault()) {
253 const SbVec3f &c = center.getValue();
254 TRANSLATE(c, mat, inv, m);
255 }
256
257 getRotationSpaceMatrix(m, minv);
258 mat.multLeft(m);
259 inv.multRight(minv);
260 }
261
262 ////////////////////////////////////////////////////////////////////////
263 //
264 // Description:
265 // Convenience function that premultiplies the effects of a matrix
266 // transformation into the transformation stored in this node.
267 //
268 // Use: public
269
270 void
271 SoTransform::multLeft(const SbMatrix &mat)
272 //
273 ////////////////////////////////////////////////////////////////////////
274 {
275 // Get the matrix corresponding to this transform
276 SoGetMatrixAction ma(SbVec2s(1,1));
277 ref();
278 ma.apply(this);
279 unrefNoDelete();
280
281 // Multiply the result and set this node from it
282 setMatrix(mat * ma.getMatrix());
283 }
284
285 ////////////////////////////////////////////////////////////////////////
286 //
287 // Description:
288 // Convenience function that postmultiplies the effects of a matrix
289 // transformation into the transformation stored in this node.
290 //
291 // Use: public
292
293 void
294 SoTransform::multRight(const SbMatrix &mat)
295 //
296 ////////////////////////////////////////////////////////////////////////
297 {
298 // Get the matrix corresponding to this transform
299 SoGetMatrixAction ma(SbVec2s(1,1));
300 ref();
301 ma.apply(this);
302 unrefNoDelete();
303
304 // Multiply the result and set this node from it
305 setMatrix(ma.getMatrix() * mat);
306 }
307
308 ////////////////////////////////////////////////////////////////////////
309 //
310 // Description:
311 // This is the same as multLeft(), except that the transformation
312 // to multiply into this node comes from another transformation
313 // node.
314 //
315 // Use: public
316
317 void
318 SoTransform::combineLeft(SoTransformation *nodeOnLeft)
319 //
320 ////////////////////////////////////////////////////////////////////////
321 {
322 // Get the matrix corresponding to the given node and multiply
323 SoGetMatrixAction ma(SbVec2s(1,1));
324 nodeOnLeft->ref();
325 ma.apply(nodeOnLeft);
326 nodeOnLeft->unrefNoDelete();
327 multLeft(ma.getMatrix());
328 }
329
330 ////////////////////////////////////////////////////////////////////////
331 //
332 // Description:
333 // This is the same as multRight(), except that the transformation
334 // to multiply into this node comes from another transformation
335 // node.
336 //
337 // Use: public
338
339 void
340 SoTransform::combineRight(SoTransformation *nodeOnRight)
341 //
342 ////////////////////////////////////////////////////////////////////////
343 {
344 // Get the matrix corresponding to the given node and multiply
345 SoGetMatrixAction ma(SbVec2s(1,1));
346 nodeOnRight->ref();
347 ma.apply(nodeOnRight);
348 nodeOnRight->unrefNoDelete();
349 multRight(ma.getMatrix());
350 }
351
352 ////////////////////////////////////////////////////////////////////////
353 //
354 // Description:
355 // Sets the fields in the node to implement the transformation
356 // represented by the given matrix.
357 //
358 // Use: public
359
360 void
361 SoTransform::setMatrix(const SbMatrix &mat)
362 //
363 ////////////////////////////////////////////////////////////////////////
364 {
365 // Factor mat into the necessary fields, while keeping the center
366 // field unchanged. Therefore, we need to factor a new matrix,
367 // [mNoCenter], given by:
368 // [centerInverse][mNoCenter][center] = [mat]
369 //
370 // So, [mNoCenter] = [center][mat][centerInverse]
371
372 SbMatrix mNoCenter, subMat;
373 mNoCenter.setTranslate(-center.getValue());
374 mNoCenter.multLeft(mat);
375 subMat.setTranslate(center.getValue());
376 mNoCenter.multLeft(subMat);
377
378 // Now, factor mNoCenter and plug it back into the fields
379 SbMatrix shearRotMat, rotMat, projMat;
380 SbVec3f sVec, tVec;
381
382 if (mNoCenter.factor(shearRotMat, sVec, rotMat, tVec, projMat)) {
383 SbRotation rot = rotMat;
384
385 if (translation.getValue() != tVec)
386 translation = tVec;
387
388 if (rotation.getValue() != rot)
389 rotation = rot;
390
391 if (scaleFactor.getValue() != sVec)
392 scaleFactor = sVec;
393
394 // Don't change the scale orientation if the scale is unity
395 if (sVec != SbVec3f(1.0, 1.0, 1.0)) {
396 rot = shearRotMat.transpose();
397
398 if (scaleOrientation.getValue() != rot)
399 scaleOrientation = rot;
400 }
401 }
402 else
403 SoDebugError::post("SoTransform::setMatrix",
404 "Can't factor given matrix");
405 }
406
407 ////////////////////////////////////////////////////////////////////////
408 //
409 // Description:
410 // Changes the center of the transformation to the given point
411 // without affecting the overall effect of the transformation.
412 //
413 // Use: public
414
415 void
416 SoTransform::recenter(const SbVec3f &newCenter)
417 //
418 ////////////////////////////////////////////////////////////////////////
419 {
420 // We want to change the center field of the transform node to newCenter,
421 // while keeping the resulting size, orientation, and location the
422 // same.
423 // If we just change the center field, and the scale is not (1,1,1),
424 // the result can be that the object shifts in space.
425 // So, we need to do the following:
426 // [1] find [xfMat], the SbMatrix we get by applying a getMatrix action
427 SoGetMatrixAction ma(SbVec2s(1,1));
428 ref();
429 ma.apply(this);
430 unrefNoDelete();
431 const SbMatrix &xfMat = ma.getMatrix();
432
433 // Factor the resulting matrix into the necessary fields, while
434 // making the center field be equal to newCenter. Therefore, we
435 // need to factor a new matrix, [mNoCenter], given by:
436 // [newCenterInverse][mNoCenter][newCenter] = [xfMat]
437 //
438 // So, [mNoCenter] = [newCenter][xfMat][newCenterInverse]
439
440 SbMatrix mNoCenter, subMat;
441 mNoCenter.setTranslate(-newCenter);
442 mNoCenter.multLeft(xfMat);
443 subMat.setTranslate(newCenter);
444 mNoCenter.multLeft(subMat);
445
446 // Now, factor mNoCenter to get remaining fields of node...
447 SbMatrix shearRotMat, rotMat, projMat;
448 SbVec3f sVec, tVec;
449 if (mNoCenter.factor(shearRotMat, sVec, rotMat, tVec, projMat)) {
450 SbRotation rot;
451
452 if (center.getValue() != newCenter)
453 center = newCenter;
454 if (translation.getValue() != tVec)
455 translation = tVec;
456 rot = rotMat;
457 if (rotation.getValue() != rot)
458 rotation = rot;
459 if (scaleFactor.getValue() != sVec)
460 scaleFactor = sVec;
461 // don't change the scale orientation if the scale is unity
462 if (sVec != SbVec3f(1.0, 1.0, 1.0)) {
463 rot = shearRotMat.transpose();
464 if (scaleOrientation.getValue() != rot)
465 scaleOrientation = rot;
466 }
467 }
468 else
469 SoDebugError::post("SoTransform::recenter",
470 "Can't factor centering matrix");
471 }
472
473 ////////////////////////////////////////////////////////////////////////
474 //
475 // Description:
476 // Does all the work for most actions.
477 //
478 // Use: extender
479
480 void
481 SoTransform::doAction(SoAction *action)
482 //
483 ////////////////////////////////////////////////////////////////////////
484 {
485 SbBool doCenter, doScaleOrient;
486 SoState *state = action->getState();
487
488 doCenter = (! center.isIgnored() && ! center.isDefault());
489 doScaleOrient = (! scaleOrientation.isIgnored() &&
490 ! scaleOrientation.isDefault());
491
492 // Do these in GL (right-to-left) order
493 if (! translation.isIgnored() && ! translation.isDefault())
494 SoModelMatrixElement::translateBy(state, this, translation.getValue());
495
496 if (doCenter)
497 SoModelMatrixElement::translateBy(state, this, center.getValue());
498
499 if (! rotation.isIgnored() && ! rotation.isDefault())
500 SoModelMatrixElement::rotateBy(state, this, rotation.getValue());
501
502 if (! scaleFactor.isIgnored() && ! scaleFactor.isDefault()) {
503
504 if (doScaleOrient)
505 SoModelMatrixElement::rotateBy(state, this,
506 scaleOrientation.getValue());
507
508 SoModelMatrixElement::scaleBy(state, this, scaleFactor.getValue());
509
510 if (doScaleOrient) {
511 SbRotation r = scaleOrientation.getValue();
512 r.invert();
513 SoModelMatrixElement::rotateBy(state, this, r);
514 }
515 }
516
517 if (doCenter)
518 SoModelMatrixElement::translateBy(state, this, -center.getValue());
519 }
520
521 ////////////////////////////////////////////////////////////////////////
522 //
523 // Description:
524 // Handles callback action
525 //
526 // Use: extender
527
528 void
529 SoTransform::callback(SoCallbackAction *action)
530 //
531 ////////////////////////////////////////////////////////////////////////
532 {
533 SoTransform::doAction(action);
534 }
535
536 ////////////////////////////////////////////////////////////////////////
537 //
538 // Description:
539 // Handles GL render action
540 //
541 // Use: extender
542
543 void
544 SoTransform::GLRender(SoGLRenderAction *action)
545 //
546 ////////////////////////////////////////////////////////////////////////
547 {
548 SoTransform::doAction(action);
549 }
550
551 ////////////////////////////////////////////////////////////////////////
552 //
553 // Description:
554 // Handles get bounding box action
555 //
556 // Use: extender
557
558 void
559 SoTransform::getBoundingBox(SoGetBoundingBoxAction *action)
560 //
561 ////////////////////////////////////////////////////////////////////////
562 {
563 SoTransform::doAction(action);
564 }
565
566 ////////////////////////////////////////////////////////////////////////
567 //
568 // Description:
569 // Returns transformation matrix.
570 //
571 // Use: extender
572
573 void
574 SoTransform::getMatrix(SoGetMatrixAction *action)
575 //
576 ////////////////////////////////////////////////////////////////////////
577 {
578 SbMatrix &ctm = action->getMatrix();
579 SbMatrix &inv = action->getInverse();
580 SbMatrix m;
581 SbVec3f v, centerVec;
582 SbBool doCenter, doScaleOrient;
583
584 doCenter = (! center.isIgnored() && ! center.isDefault());
585 doScaleOrient = (! scaleOrientation.isIgnored() &&
586 ! scaleOrientation.isDefault());
587
588 if (! translation.isIgnored() && ! translation.isDefault()) {
589 v = translation.getValue();
590 TRANSLATE(v, ctm, inv, m);
591 }
592
593 if (doCenter) {
594 centerVec = center.getValue();
595 TRANSLATE(centerVec, ctm, inv, m);
596 }
597
598 if (! rotation.isIgnored() && ! rotation.isDefault()) {
599 SbRotation r = rotation.getValue();
600 ROTATE(r, ctm, inv, m);
601 }
602
603 if (! scaleFactor.isIgnored() && ! scaleFactor.isDefault()) {
604
605 if (doScaleOrient) {
606 SbRotation r = scaleOrientation.getValue();
607 ROTATE(r, ctm, inv, m);
608 }
609
610 SbVec3f s = scaleFactor.getValue();
611 SCALE(s, ctm, inv, m);
612
613 if (doScaleOrient) {
614 SbRotation r = scaleOrientation.getValue();
615 r.invert();
616 ROTATE(r, ctm, inv, m);
617 }
618
619 }
620
621 if (doCenter) {
622 centerVec.negate();
623 TRANSLATE(centerVec, ctm, inv, m);
624 }
625 }
626
627 ////////////////////////////////////////////////////////////////////////
628 //
629 // Description:
630 // Handles pick action.
631 //
632 // Use: extender
633
634 void
635 SoTransform::pick(SoPickAction *action)
636 //
637 ////////////////////////////////////////////////////////////////////////
638 {
639 SoTransform::doAction(action);
640 }
641
642 #undef ROTATE
643 #undef SCALE
644 #undef TRANSLATE