001: /*
002: * $RCSfile: JavaOnePolhemusTracker.java,v $
003: *
004: * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.1 $
041: * $Date: 2007/09/25 20:01:19 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.utils.trackers;
046:
047: import java.lang.String;
048: import javax.vecmath.*;
049: import javax.media.j3d.*; // Note the following is necessary only for the sleep calls.
050: import java.lang.Thread;
051:
052: /**
053: * NOTE: this class is very much out of date. It is using deprecated
054: * and unimplemented 1.1 API constants (POLLED and STREAMING) that are
055: * now gone in 1.2. The offending lines have been commented out
056: * and marked with an "OBSOLETE:" comment.
057: *<p>
058: *
059: * The SharedMemoryPolhemusTracker Class defines the code to make a
060: * sharedMemoryPolhemus input device work correctly
061: *
062: * @version 1.5, 98/03/17 22:11:14
063: * @author Henry Sowizral
064: */
065: public class JavaOnePolhemusTracker extends Tracker {
066:
067: boolean headTrackingEnabled = false;
068: View view;
069: long count = 0;
070:
071: // Current Sensor Read (last one we generated)
072: SensorRead currentRead;
073:
074: // Next SensorRead (the one we're now generating)
075: SensorRead nextRead;
076:
077: // The current sensor
078: int currentSensor = 0;
079:
080: // Holds a sensor's Euler Angles
081: Vector3d deviceEulerAngles = new Vector3d();
082:
083: // Holds the sensor's current Position
084: Vector3d deviceTranslateValues = new Vector3d();
085:
086: // The number of sensors associated with this device.
087: static final int SensorCount = 8;
088:
089: // The number of buttons associated with this device.
090: static final int ButtonCount = 5;
091:
092: // The initial position and orientation
093: Transform3D initialPosOrient = new Transform3D();
094:
095: Matrix3d yawMat = new Matrix3d();
096: Matrix3d pitchMat = new Matrix3d();
097: Matrix3d rollMat = new Matrix3d();
098:
099: int sensorIndex;
100:
101: static float PositionOrientation[] = new float[6];
102: static int[] ButtonArray = new int[1];
103:
104: native int StartUp(String filename, int sensorCount);
105:
106: native int ProcessStream(int sensorIndex, float PosOrient[],
107: int Buttons[]);
108:
109: /**
110: * Construct a new SharedMemoryPolhemusTracker
111: * @param view the view object
112: * @paran deviceFilename the SharedMemoryPolhemus's devide name
113: * @param mode the mode of access one of POLLED or STREAMING
114: * @param sensorCount the number of sensors associated with this
115: * sharedMemoryPolhemus
116: * @param buttonCount the number of buttons associated with each sensor
117: */
118: public JavaOnePolhemusTracker(View view, String deviceFilename,
119: int mode, int sensorCount, int sensorIndex) {
120: this .super (view, deviceFilename, mode, sensorCount, 0);
121: this .sensorIndex = sensorIndex;
122: this .view = view;
123: }
124:
125: /**
126: * Initializes the SharedMemoryPolhemus deviceFilename by opening the
127: * device and sending the SharedMemoryPolhemus the initialization
128: * information.
129: * @param deviceFilename the SharedMemoryPolhemus's deviceFilename
130: */
131: public boolean initialize() {
132: /* OBSOLETE:
133: if (processingMode == STREAMING) {
134: return (0 == this.StartUp("", sensorCount));
135: }
136: */
137: System.out.println("Polling not supported");
138: initialPosOrient.setIdentity();
139: return false;
140: }
141:
142: /**
143: * Code to set the device's current position and orientation as the devices
144: * nominal position and orientation(establish its reference frame relative
145: * to the "Tracker base" reference frame).
146: */
147: public void setNominalPositionAndOrientation() {
148:
149: Transform3D BaseTransform = new Transform3D();
150: this .ProcessStream(sensorIndex, PositionOrientation,
151: ButtonArray);
152: tmpVector.set(PositionOrientation[0], PositionOrientation[1],
153: PositionOrientation[2]);
154: newTransform.setTranslation(tmpVector);
155: setMatrixFromValues(PositionOrientation, newMatrix);
156: newTransform.setRotationScale(newMatrix);
157: initialPosOrient.invert(newTransform);
158: if (headTrackingEnabled == false) {
159: view.setTrackingEnable(true);
160: headTrackingEnabled = true;
161: }
162: }
163:
164: /**
165: * Code to poll and then process the input from a sharedMemoryPolhemus.
166: */
167: public void pollAndProcessInput() {
168: System.out
169: .println("pollAndProcessInput: entered. not supported");
170: }
171:
172: Matrix3d oldMatrix = new Matrix3d();
173: Matrix3d newMatrix = new Matrix3d();
174: Matrix3d tmpMatrix = new Matrix3d();
175:
176: Transform3D oldTransform = new Transform3D();
177: Vector3d oldLocation = new Vector3d();
178:
179: Transform3D newTransform = new Transform3D();
180: Vector3d newLocation = new Vector3d();
181:
182: Transform3D tmpTransform = new Transform3D();
183:
184: Vector3d tmpVector = new Vector3d();
185:
186: /**
187: * Code to process the device's streaming input.
188: */
189: public void processStreamInput() {
190: long time;
191:
192: this .ProcessStream(sensorIndex, PositionOrientation,
193: ButtonArray);
194:
195: if (count++ % 120 == 0) {
196: System.out.println("PositionOrientation: "
197: + PositionOrientation[0] + " "
198: + PositionOrientation[1] + " "
199: + PositionOrientation[2] + " "
200: + PositionOrientation[3] + " "
201: + PositionOrientation[4] + " "
202: + PositionOrientation[5]);
203: }
204:
205: time = System.currentTimeMillis();
206:
207: tmpVector.set(PositionOrientation[0], PositionOrientation[1],
208: PositionOrientation[2]);
209: newTransform.setTranslation(tmpVector);
210:
211: setMatrixFromValues(PositionOrientation, newMatrix);
212: newTransform.setRotationScale(newMatrix);
213:
214: /*
215: System.out.println("POLHEMUS MATRIX");
216: System.out.println(newTransform);
217: */
218:
219: tmpTransform.mul(initialPosOrient, newTransform);
220: sensors[currentSensor].setNextSensorRead(time, tmpTransform,
221: ButtonArray);
222:
223: }
224:
225: /**
226: * Code to construct a delta Matrix from SharedMemoryPolhemus inputs
227: */
228: void setMatrixFromValues(float posOrient[], Matrix3d Delta) {
229: double sina, sinb, sinc, cosa, cosb, cosc;
230:
231: sina = Math.sin(posOrient[5]);
232: sinb = Math.sin(posOrient[4]);
233: sinc = Math.sin(posOrient[3]);
234:
235: cosa = Math.cos(posOrient[5]);
236: cosb = Math.cos(posOrient[4]);
237: cosc = Math.cos(posOrient[3]);
238:
239: Delta.m00 = cosb * cosc;
240: Delta.m01 = cosb * sinc;
241: Delta.m02 = -sinb;
242:
243: Delta.m10 = -(cosa * sinc) + (sina * sinb * cosc);
244: Delta.m11 = (cosa * cosc) + (sina * sinb * sinc);
245: Delta.m12 = sina * cosb;
246:
247: Delta.m20 = (sina * sinc) + (cosa * sinb * cosc);
248: Delta.m21 = -(sina * cosc) + (cosa * sinb * sinc);
249: Delta.m22 = cosa * cosb;
250:
251: Delta.transpose();
252:
253: }
254:
255: }
|