001: /*
002: * $RCSfile: OrientedShape3DRetained.java,v $
003: *
004: * Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
006: *
007: * This code is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License version 2 only, as
009: * published by the Free Software Foundation. Sun designates this
010: * particular file as subject to the "Classpath" exception as provided
011: * by Sun in the LICENSE file that accompanied this code.
012: *
013: * This code is distributed in the hope that it will be useful, but WITHOUT
014: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: * version 2 for more details (a copy is included in the LICENSE file that
017: * accompanied this code).
018: *
019: * You should have received a copy of the GNU General Public License version
020: * 2 along with this work; if not, write to the Free Software Foundation,
021: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
022: *
023: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
024: * CA 95054 USA or visit www.sun.com if you need additional information or
025: * have any questions.
026: *
027: * $Revision: 1.6 $
028: * $Date: 2008/02/28 20:17:27 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import javax.vecmath.*;
035: import java.util.ArrayList;
036:
037: class OrientedShape3DRetained extends Shape3DRetained {
038:
039: static final int ALIGNMENT_CHANGED = LAST_DEFINED_BIT << 1;
040: static final int AXIS_CHANGED = LAST_DEFINED_BIT << 2;
041: static final int ROTATION_CHANGED = LAST_DEFINED_BIT << 3;
042: static final int CONSTANT_SCALE_CHANGED = LAST_DEFINED_BIT << 4;
043: static final int SCALE_FACTOR_CHANGED = LAST_DEFINED_BIT << 5;
044:
045: int mode = OrientedShape3D.ROTATE_ABOUT_AXIS;
046:
047: // Axis about which to rotate.
048: Vector3f axis = new Vector3f(0.0f, 1.0f, 0.0f);
049: Point3f rotationPoint = new Point3f(0.0f, 0.0f, 1.0f);
050: private Vector3d nAxis = new Vector3d(0.0, 1.0, 0.0); // normalized axis
051:
052: // reused temporaries
053: private Point3d viewPosition = new Point3d();
054: private Point3d yUpPoint = new Point3d();
055:
056: private Vector3d eyeVec = new Vector3d();
057: private Vector3d yUp = new Vector3d();
058: private Vector3d zAxis = new Vector3d();
059: private Vector3d yAxis = new Vector3d();
060: private Vector3d vector = new Vector3d();
061:
062: private AxisAngle4d aa = new AxisAngle4d();
063:
064: private Transform3D xform = new Transform3D(); // used several times
065: private Transform3D zRotate = new Transform3D();
066:
067: // For scale invariant mode
068: boolean constantScale = false;
069: double scaleFactor = 1.0;
070:
071: // Frequently used variables for scale invariant computation
072: // Left and right Vworld to Clip coordinates transforms
073: private Transform3D left_xform = new Transform3D();
074: private Transform3D right_xform = new Transform3D();
075:
076: // Transform for scaling the OrientedShape3D to correct for
077: // perspective foreshortening
078: Transform3D scaleXform = new Transform3D();
079:
080: // Variables for converting between clip to local world coords
081: private Vector4d im_vec[] = { new Vector4d(), new Vector4d() };
082: private Vector4d lvec = new Vector4d();
083:
084: boolean orientedTransformDirty = true;
085:
086: Transform3D[] orientedTransforms = new Transform3D[1];
087: static final double EPSILON = 1.0e-6;
088:
089: /**
090: * Constructs a OrientedShape3D node with default parameters.
091: * The default values are as follows:
092: * <ul>
093: * alignment mode : ROTATE_ABOUT_AXIS<br>
094: * alignment axis : Y-axis (0,1,0)<br>
095: * rotation point : (0,0,1)<br>
096: *</ul>
097: */
098: public OrientedShape3DRetained() {
099: super ();
100: this .nodeType = NodeRetained.ORIENTEDSHAPE3D;
101: }
102:
103: // initializes alignment mode
104: void initAlignmentMode(int mode) {
105: this .mode = mode;
106: }
107:
108: /**
109: * Sets the alignment mode.
110: * @param mode one of: ROTATE_ABOUT_AXIS or ROTATE_ABOUT_POINT
111: */
112: void setAlignmentMode(int mode) {
113: if (this .mode != mode) {
114: initAlignmentMode(mode);
115: sendChangedMessage(ALIGNMENT_CHANGED, new Integer(mode));
116: }
117: }
118:
119: /**
120: * Retrieves the alignment mode.
121: * @return one of: ROTATE_ABOUT_AXIS or ROTATE_ABOUT_POINT
122: */
123: int getAlignmentMode() {
124: return (mode);
125: }
126:
127: // initializes alignment axis
128: void initAlignmentAxis(Vector3f axis) {
129: initAlignmentAxis(axis.x, axis.y, axis.z);
130: }
131:
132: // initializes alignment axis
133: void initAlignmentAxis(float x, float y, float z) {
134: this .axis.set(x, y, z);
135: double invMag;
136: invMag = 1.0 / Math.sqrt(axis.x * axis.x + axis.y * axis.y
137: + axis.z * axis.z);
138: nAxis.x = (double) axis.x * invMag;
139: nAxis.y = (double) axis.y * invMag;
140: nAxis.z = (double) axis.z * invMag;
141: }
142:
143: /**
144: * Sets the new alignment axis. This is the ray about which this
145: * OrientedShape3D rotates when the mode is ROTATE_ABOUT_AXIS.
146: * @param axis the new alignment axis
147: */
148: void setAlignmentAxis(Vector3f axis) {
149: setAlignmentAxis(axis.x, axis.y, axis.z);
150: }
151:
152: /**
153: * Sets the new alignment axis. This is the ray about which this
154: * OrientedShape3D rotates when the mode is ROTATE_ABOUT_AXIS.
155: * @param x the x component of the alignment axis
156: * @param y the y component of the alignment axis
157: * @param z the z component of the alignment axis
158: */
159: void setAlignmentAxis(float x, float y, float z) {
160: initAlignmentAxis(x, y, z);
161:
162: if (mode == OrientedShape3D.ROTATE_ABOUT_AXIS) {
163: sendChangedMessage(AXIS_CHANGED, new Vector3f(x, y, z));
164: }
165: }
166:
167: /**
168: * Retrieves the alignment axis of this OrientedShape3D node,
169: * and copies it into the specified vector.
170: * @param axis the vector that will contain the alignment axis
171: */
172: void getAlignmentAxis(Vector3f axis) {
173: axis.set(this .axis);
174: }
175:
176: // initializes rotation point
177: void initRotationPoint(Point3f point) {
178: rotationPoint.set(point);
179: }
180:
181: // initializes rotation point
182: void initRotationPoint(float x, float y, float z) {
183: rotationPoint.set(x, y, z);
184: }
185:
186: /**
187: * Sets the new rotation point. This is the point about which the
188: * OrientedShape3D rotates when the mode is ROTATE_ABOUT_POINT.
189: * @param point the new rotation point
190: */
191: void setRotationPoint(Point3f point) {
192: setRotationPoint(point.x, point.y, point.z);
193: }
194:
195: /**
196: * Sets the new rotation point. This is the point about which the
197: * OrientedShape3D rotates when the mode is ROTATE_ABOUT_POINT.
198: * @param x the x component of the rotation point
199: * @param y the y component of the rotation point
200: * @param z the z component of the rotation point
201: */
202: void setRotationPoint(float x, float y, float z) {
203: initRotationPoint(x, y, z);
204:
205: if (mode == OrientedShape3D.ROTATE_ABOUT_POINT) {
206: sendChangedMessage(ROTATION_CHANGED, new Point3f(x, y, z));
207: }
208: }
209:
210: /**
211: * Retrieves the rotation point of this OrientedShape3D node,
212: * and copies it into the specified vector.
213: * @param axis the point that will contain the rotation point
214: */
215: void getRotationPoint(Point3f point) {
216: point.set(rotationPoint);
217: }
218:
219: void setConstantScaleEnable(boolean enable) {
220: if (constantScale != enable) {
221: initConstantScaleEnable(enable);
222: sendChangedMessage(CONSTANT_SCALE_CHANGED, new Boolean(
223: enable));
224: }
225: }
226:
227: boolean getConstantScaleEnable() {
228: return constantScale;
229: }
230:
231: void initConstantScaleEnable(boolean cons_scale) {
232: constantScale = cons_scale;
233: }
234:
235: void setScale(double scale) {
236: initScale(scale);
237: if (constantScale)
238: sendChangedMessage(SCALE_FACTOR_CHANGED, new Double(scale));
239: }
240:
241: void initScale(double scale) {
242: scaleFactor = scale;
243: }
244:
245: double getScale() {
246: return scaleFactor;
247: }
248:
249: void sendChangedMessage(int component, Object attr) {
250: J3dMessage changeMessage = new J3dMessage();
251: changeMessage.type = J3dMessage.ORIENTEDSHAPE3D_CHANGED;
252: changeMessage.threads = targetThreads;
253: changeMessage.universe = universe;
254: changeMessage.args[0] = getGeomAtomsArray(mirrorShape3D);
255: changeMessage.args[1] = new Integer(component);
256: changeMessage.args[2] = attr;
257: OrientedShape3DRetained[] o3dArr = new OrientedShape3DRetained[mirrorShape3D
258: .size()];
259: mirrorShape3D.toArray(o3dArr);
260: changeMessage.args[3] = o3dArr;
261: changeMessage.args[4] = this ;
262: VirtualUniverse.mc.processMessage(changeMessage);
263: }
264:
265: void updateImmediateMirrorObject(Object[] args) {
266: int component = ((Integer) args[1]).intValue();
267: if ((component & (ALIGNMENT_CHANGED | AXIS_CHANGED
268: | ROTATION_CHANGED | CONSTANT_SCALE_CHANGED | SCALE_FACTOR_CHANGED)) != 0) {
269: OrientedShape3DRetained[] msArr = (OrientedShape3DRetained[]) args[3];
270: Object obj = args[2];
271: if ((component & ALIGNMENT_CHANGED) != 0) {
272: int mode = ((Integer) obj).intValue();
273: for (int i = 0; i < msArr.length; i++) {
274: msArr[i].initAlignmentMode(mode);
275: }
276: } else if ((component & AXIS_CHANGED) != 0) {
277: Vector3f axis = (Vector3f) obj;
278: for (int i = 0; i < msArr.length; i++) {
279: msArr[i].initAlignmentAxis(axis);
280: }
281: } else if ((component & ROTATION_CHANGED) != 0) {
282: Point3f point = (Point3f) obj;
283: for (int i = 0; i < msArr.length; i++) {
284: msArr[i].initRotationPoint(point);
285: }
286: } else if ((component & CONSTANT_SCALE_CHANGED) != 0) {
287: boolean bool = ((Boolean) obj).booleanValue();
288: for (int i = 0; i < msArr.length; i++) {
289: msArr[i].initConstantScaleEnable(bool);
290: }
291: } else if ((component & SCALE_FACTOR_CHANGED) != 0) {
292: double scale = ((Double) obj).doubleValue();
293: for (int i = 0; i < msArr.length; i++) {
294: msArr[i].initScale(scale);
295: }
296: }
297: } else {
298: super .updateImmediateMirrorObject(args);
299: }
300: }
301:
302: Transform3D getOrientedTransform(int viewIndex) {
303: synchronized (orientedTransforms) {
304: if (viewIndex >= orientedTransforms.length) {
305: Transform3D xform = new Transform3D();
306: Transform3D[] newList = new Transform3D[viewIndex + 1];
307: for (int i = 0; i < orientedTransforms.length; i++) {
308: newList[i] = orientedTransforms[i];
309: }
310: newList[viewIndex] = xform;
311: orientedTransforms = newList;
312: } else {
313: if (orientedTransforms[viewIndex] == null) {
314: orientedTransforms[viewIndex] = new Transform3D();
315: }
316: }
317: }
318: return orientedTransforms[viewIndex];
319: }
320:
321: // called on the parent object
322: // Should be synchronized so that the user thread does not modify the
323: // OrientedShape3D params while computing the transform
324: synchronized void updateOrientedTransform(Canvas3D canvas,
325: int viewIndex) {
326: double angle = 0.0;
327: double sign;
328: boolean status;
329:
330: Transform3D orientedxform = getOrientedTransform(viewIndex);
331: // get viewplatforms's location in virutal world
332: if (mode == OrientedShape3D.ROTATE_ABOUT_AXIS) { // rotate about axis
333: canvas.getCenterEyeInImagePlate(viewPosition);
334: canvas.getImagePlateToVworld(xform); // xform is imagePlateToLocal
335: xform.transform(viewPosition);
336:
337: // get billboard's transform
338: xform.set(getCurrentLocalToVworld());
339: xform.invert(); // xform is now vWorldToLocal
340:
341: // transform the eye position into the billboard's coordinate system
342: xform.transform(viewPosition);
343:
344: // eyeVec is a vector from the local origin to the eye pt in local
345: eyeVec.set(viewPosition);
346: eyeVec.normalize();
347:
348: // project the eye into the rotation plane
349: status = projectToPlane(eyeVec, nAxis);
350:
351: if (status) {
352: // project the z axis into the rotation plane
353: zAxis.x = 0.0;
354: zAxis.y = 0.0;
355: zAxis.z = 1.0;
356: status = projectToPlane(zAxis, nAxis);
357: }
358: if (status) {
359:
360: // compute the sign of the angle by checking if the cross product
361: // of the two vectors is in the same direction as the normal axis
362: vector.cross(eyeVec, zAxis);
363: if (vector.dot(nAxis) > 0.0) {
364: sign = 1.0;
365: } else {
366: sign = -1.0;
367: }
368:
369: // compute the angle between the projected eye vector and the
370: // projected z
371:
372: double dot = eyeVec.dot(zAxis);
373: if (dot > 1.0f) {
374: dot = 1.0f;
375: } else if (dot < -1.0f) {
376: dot = -1.0f;
377: }
378:
379: angle = sign * Math.acos(dot);
380:
381: // use -angle because xform is to *undo* rotation by angle
382: aa.x = nAxis.x;
383: aa.y = nAxis.y;
384: aa.z = nAxis.z;
385: aa.angle = -angle;
386: orientedxform.set(aa);
387: } else {
388: orientedxform.setIdentity();
389: }
390:
391: } else { // rotate about point
392: // Need to rotate Z axis to point to eye, and Y axis to be
393: // parallel to view platform Y axis, rotating around rotation pt
394:
395: // get the eye point
396: canvas.getCenterEyeInImagePlate(viewPosition);
397:
398: // derive the yUp point
399: yUpPoint.set(viewPosition);
400: yUpPoint.y += 0.01; // one cm in Physical space
401:
402: // transform the points to the Billboard's space
403: canvas.getImagePlateToVworld(xform); // xform is ImagePlateToVworld
404: xform.transform(viewPosition);
405: xform.transform(yUpPoint);
406:
407: // get billboard's transform
408: xform.set(getCurrentLocalToVworld());
409: xform.invert(); // xform is vWorldToLocal
410:
411: // transfom points to local coord sys
412: xform.transform(viewPosition);
413: xform.transform(yUpPoint);
414:
415: // Make a vector from viewPostion to 0,0,0 in the BB coord sys
416: eyeVec.set(viewPosition);
417: eyeVec.normalize();
418:
419: // create a yUp vector
420: yUp.set(yUpPoint);
421: yUp.sub(viewPosition);
422: yUp.normalize();
423:
424: // find the plane to rotate z
425: zAxis.x = 0.0;
426: zAxis.y = 0.0;
427: zAxis.z = 1.0;
428:
429: // rotation axis is cross product of eyeVec and zAxis
430: vector.cross(eyeVec, zAxis); // vector is cross product
431:
432: // if cross product is non-zero, vector is rotation axis and
433: // rotation angle is acos(eyeVec.dot(zAxis)));
434: double length = vector.length();
435: if (length > 0.0001) {
436: double dot = eyeVec.dot(zAxis);
437: if (dot > 1.0f) {
438: dot = 1.0f;
439: } else if (dot < -1.0f) {
440: dot = -1.0f;
441: }
442: angle = Math.acos(dot);
443: aa.x = vector.x;
444: aa.y = vector.y;
445: aa.z = vector.z;
446: aa.angle = -angle;
447: zRotate.set(aa);
448: } else {
449: // no rotation needed, set to identity (scale = 1.0)
450: zRotate.set(1.0);
451: }
452:
453: // Transform the yAxis by zRotate
454: yAxis.x = 0.0;
455: yAxis.y = 1.0;
456: yAxis.z = 0.0;
457: zRotate.transform(yAxis);
458:
459: // project the yAxis onto the plane perp to the eyeVec
460: status = projectToPlane(yAxis, eyeVec);
461:
462: if (status) {
463: // project the yUp onto the plane perp to the eyeVec
464: status = projectToPlane(yUp, eyeVec);
465: }
466:
467: if (status) {
468: // rotation angle is acos(yUp.dot(yAxis));
469: double dot = yUp.dot(yAxis);
470:
471: // Fix numerical error, otherwise acos return NULL
472: if (dot > 1.0f) {
473: dot = 1.0f;
474: } else if (dot < -1.0f) {
475: dot = -1.0f;
476: }
477:
478: angle = Math.acos(dot);
479:
480: // check the sign by looking a the cross product vs the eyeVec
481: vector.cross(yUp, yAxis); // vector is cross product
482: if (eyeVec.dot(vector) < 0) {
483: angle *= -1;
484: }
485: aa.x = eyeVec.x;
486: aa.y = eyeVec.y;
487: aa.z = eyeVec.z;
488: aa.angle = -angle;
489: xform.set(aa); // xform is now yRotate
490:
491: // rotate around the rotation point
492: vector.x = rotationPoint.x;
493: vector.y = rotationPoint.y;
494: vector.z = rotationPoint.z; // vector to translate to RP
495: orientedxform.set(vector); // translate to RP
496: orientedxform.mul(xform); // yRotate
497: orientedxform.mul(zRotate); // zRotate
498: vector.scale(-1.0); // vector to translate back
499: xform.set(vector); // xform to translate back
500: orientedxform.mul(xform); // translate back
501: } else {
502: orientedxform.setIdentity();
503: }
504:
505: }
506: //Scale invariant computation
507: if (constantScale) {
508: // Back Xform a unit vector to local world coords
509: canvas.getInverseVworldProjection(left_xform, right_xform);
510:
511: // the two endpts of the vector have to be transformed
512: // individually because the Xform is not affine
513: im_vec[0].set(0.0, 0.0, 0.0, 1.0);
514: im_vec[1].set(1.0, 0.0, 0.0, 1.0);
515: left_xform.transform(im_vec[0]);
516: left_xform.transform(im_vec[1]);
517:
518: left_xform.set(getCurrentLocalToVworld());
519: left_xform.invert();
520: left_xform.transform(im_vec[0]);
521: left_xform.transform(im_vec[1]);
522: lvec.set(im_vec[1]);
523: lvec.sub(im_vec[0]);
524:
525: // We simply need the direction of this vector
526: lvec.normalize();
527: im_vec[0].set(0.0, 0.0, 0.0, 1.0);
528: im_vec[1].set(lvec);
529: im_vec[1].w = 1.0;
530:
531: // Forward Xfrom to clip coords
532: left_xform.set(getCurrentLocalToVworld());
533: left_xform.transform(im_vec[0]);
534: left_xform.transform(im_vec[1]);
535:
536: canvas.getVworldProjection(left_xform, right_xform);
537: left_xform.transform(im_vec[0]);
538: left_xform.transform(im_vec[1]);
539:
540: // Perspective division
541: im_vec[0].x /= im_vec[0].w;
542: im_vec[0].y /= im_vec[0].w;
543: im_vec[0].z /= im_vec[0].w;
544:
545: im_vec[1].x /= im_vec[1].w;
546: im_vec[1].y /= im_vec[1].w;
547: im_vec[1].z /= im_vec[1].w;
548:
549: lvec.set(im_vec[1]);
550: lvec.sub(im_vec[0]);
551:
552: // Use the length of this vector to determine the scaling
553: // factor
554: double scale = 1 / lvec.length();
555:
556: // Convert to meters
557: scale *= scaleFactor * canvas.getPhysicalWidth() / 2;
558:
559: // Scale object so that it appears the same size
560: scaleXform.setScale(scale);
561: orientedxform.mul(scaleXform);
562: }
563:
564: }
565:
566: private boolean projectToPlane(Vector3d projVec, Vector3d planeVec) {
567: double dis = planeVec.dot(projVec);
568: projVec.x = projVec.x - planeVec.x * dis;
569: projVec.y = projVec.y - planeVec.y * dis;
570: projVec.z = projVec.z - planeVec.z * dis;
571:
572: double length = projVec.length();
573: if (length < EPSILON) { // projVec is parallel to planeVec
574: return false;
575: }
576: projVec.scale(1 / length);
577: return true;
578: }
579:
580: void compile(CompileState compState) {
581:
582: super .compile(compState);
583:
584: mergeFlag = SceneGraphObjectRetained.DONT_MERGE;
585:
586: // don't push the static transform to orientedShape3D
587: // because orientedShape3D is rendered using vertex array;
588: // it's not worth pushing the transform here
589:
590: compState.keepTG = true;
591: }
592:
593: void searchGeometryAtoms(UnorderList list) {
594: list.add(getGeomAtom(getMirrorShape(key)));
595: }
596: }
|