LogitechTracker.java :  » 6.0-JDK-Modules » java-3d » com » sun » j3d » utils » trackers » Java Open Source

Java Open Source » 6.0 JDK Modules » java 3d 
java 3d » com » sun » j3d » utils » trackers » LogitechTracker.java
/*
 * $RCSfile: LogitechTracker.java,v $
 *
 * Copyright 1996-2007 Sun Microsystems, Inc.  All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any
 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
 * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
 * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
 * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
 * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
 * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that this software is not designed, licensed or
 * intended for use in the design, construction, operation or
 * maintenance of any nuclear facility.
 *
 * $Revision: 1.1 $
 * $Date: 2007/09/25 20:01:19 $
 * $State: Exp $
 */

package com.sun.j3d.utils.trackers;

import java.lang.String;
import javax.vecmath.*;
import javax.media.j3d.*;
// Note the following is necessary only for the sleep calls.
import java.lang.Thread;

/**
 * NOTE: this class is very much out of date.  It is using deprecated
 * and unimplemented 1.1 API constants (POLLED and STREAMING) that are
 * now gone in 1.2.  The offending lines have been commented out
 * and marked with an "OBSOLETE:" comment.
 *<p>
 *
 * The LogitechTracker Class defines the code to make a logitech input
 * device work correclty
 *
 * @version   1.11, 99/09/15 13:47:17
 * @author   Henry Sowizral
 */
public class LogitechTracker extends Tracker {

    // This device's slave filename
    String slaveFilename;

    // Current Sensor Read (last one we generated)
    SensorRead currentRead;

    // Next SensorRead (the one we're now generating)
    SensorRead nextRead;
    
    // The current sensor
    int currentSensor = 0;
    
    //  Holds a sensor's Euler Angles
    Vector3d deviceEulerAngles = new Vector3d();

    //  Holds the sensor's current Position
    Vector3d deviceTranslateValues = new Vector3d();

    // The number of sensors associated with this device.
    static final int SensorCount = 8;

    // The number of buttons associated with this device.
    static final int ButtonCount = 5;

    // The initial position and orientation
    Transform3D initialPosOrient = new Transform3D();
    

    static float PositionOrientation[] = new float[6];
    static int ButtonArray[] = new int[1];

    /**
     * Construct a new LogitechTracker
     * @param view the view object
     * @paran deviceFilename the Logitech's devide name
     * @param mode the mode of access one of POLLED or STREAMING
     * @param sensorCount the number of sensors associated with this logitech
     * @param buttonCount the number of buttons associated with each sensor
     */
    public LogitechTracker(View view, String deviceFilename, int mode,
         int sensorCount, int buttonCount) {
  this.super(view, deviceFilename, mode, sensorCount, buttonCount);
    }

    /**
     * Construct a new LogitechTracker with a hand tracker
     * @param view the view object
     * @param masterFilename the Logitech's master port device name
     * @paran slaveFilename the Logitech's slave port device name
     * @param mode the mode of access one of POLLED or STREAMING
     * @param sensorCount the number of sensors associated with this logitech
     * @param buttonCount the number of buttons associated with each sensor
     */
    public LogitechTracker(View view, String masterFilename,
         String slaveFilename, int mode,
         int sensorCount, int buttonCount) {
  this.super(view, masterFilename, mode, sensorCount, buttonCount);
  this.slaveFilename = slaveFilename;
    }

    native int StartUpPolled(int fd);
    native int StartUpStreaming(int fd);
    native int PollInput(int fd, float PosOrient[], int Buttons[]);
    native int ProcessStream(int fd, float PosOrient[], int Buttons[]);
    native int OpenSerialDeviceRaw(String filename, int baudRate);

    /**
     * Code to initialize the device
     * @param deviceFilename
     */
    public boolean initialize() {
  return this.initialize(19200);
    }

    /**
     * Initializes the Logitech deviceFilename by opening the
     * device and sending the Logitech the initialization information.
     * @param deviceFilename the Logitech's deviceFilename
     * @param baudRate the speed we want the Logitech to run at
     */
    public boolean initialize(int baudRate) {
  fileDescriptor = this.OpenSerialDeviceRaw(deviceFilename, baudRate);
  if (fileDescriptor < 0) {
      System.out.println("Unable to initialize Logitech on " +
             deviceFilename + " error code " +
             fileDescriptor);
      return false;
  }

  try {
      Thread.sleep(2000L);
  } catch (InterruptedException e) {
      System.out.println(e);
  }
  
  /* OBSOLETE:
  if(processingMode == POLLED){
      System.out.println("Initializing Polled");
     return (0 == this.StartUpPolled(fileDescriptor));
  } else if (processingMode == STREAMING) {
      System.out.println("Initializing Streaming");
      return (0 == this.StartUpStreaming(fileDescriptor));
  }
  */

  return false;
    }


    /**
     * Code to set the device's current position and orientation as the devices
     * nominal position and orientation(establish its reference frame relative
     * to the "Tracker base" reference frame).
     */
    public void setNominalPositionAndOrientation() {
  Transform3D BaseTransform = new Transform3D();

  /** The following is not MT safe 
    pollAndProcessInput();
  */
  (sensors[currentSensor].getCurrentSensorRead()).get(BaseTransform);

  initialPosOrient.invert(BaseTransform);
  newTransform.setIdentity();
  ButtonArray[0] = 0;
  sensors[currentSensor].setNextSensorRead(System.currentTimeMillis(),
             newTransform, ButtonArray);
    }



    /**
     * Code to poll and then process the input from a logitech.
     */
    public void pollAndProcessInput() {
  long time;

  System.out.println("pollAndProcessInput: entered");
  this.PollInput(fileDescriptor, PositionOrientation, ButtonArray);
  System.out.println("PositionOrientation: " 
                           + PositionOrientation[0] +
         " " + PositionOrientation[0] +
         " " + PositionOrientation[1] +
         " " + PositionOrientation[2] +
         " " + PositionOrientation[3] +
         " " + PositionOrientation[4] +
         " " + PositionOrientation[5]);
  System.out.println("Buttons " + ButtonArray[0]);

  time = System.currentTimeMillis();

  tmpVector.set(PositionOrientation[0], PositionOrientation[1],
      PositionOrientation[2]);
  newTransform.setTranslation(tmpVector);

  setMatrixFromValues(PositionOrientation, newMatrix);
  newTransform.setRotationScale(newMatrix);

  newTransform.mul(initialPosOrient, newTransform);
  sensors[currentSensor].setNextSensorRead(time, newTransform,
             ButtonArray);
    }


    Matrix3d oldMatrix = new Matrix3d();
    Matrix3d newMatrix = new Matrix3d();
    Matrix3d tmpMatrix = new Matrix3d();

    Transform3D oldTransform = new Transform3D();
    Vector3d oldLocation = new Vector3d();
    
    Transform3D newTransform = new Transform3D();
    Vector3d newLocation = new Vector3d();

    Vector3d tmpVector = new Vector3d();
    /**
      * Code to process the device's streaming input.
      */
    public void processStreamInput() {
  long time;

  System.out.println("processStreamInput: entered");
  this.ProcessStream(fileDescriptor, PositionOrientation, ButtonArray);
  System.out.println("PositionOrientation: " 
                           + PositionOrientation[0] +
         " " + PositionOrientation[0] +
         " " + PositionOrientation[1] +
         " " + PositionOrientation[2] +
         " " + PositionOrientation[3] +
         " " + PositionOrientation[4] +
         " " + PositionOrientation[5]);
  time = System.currentTimeMillis();

  tmpVector.set(PositionOrientation[0], PositionOrientation[1],
      PositionOrientation[2]);
  newTransform.setTranslation(tmpVector);

  setMatrixFromValues(PositionOrientation, newMatrix);
  newTransform.setRotationScale(newMatrix);

  newTransform.mul(initialPosOrient, newTransform);
  sensors[currentSensor].setNextSensorRead(time, newTransform,
             ButtonArray);
    }

    /**
     * Code to construct a delta Matrix from Logitech inputs
     */
    void setMatrixFromValues(float posOrient[], Matrix3d Delta){
  double sina, sinb, sinc, cosa, cosb, cosc;

  sina = Math.sin(posOrient[3]);
  sinb = Math.sin(posOrient[4]);
  sinc = Math.sin(posOrient[5]);
  
  cosa = Math.cos(posOrient[3]);
  cosb = Math.cos(posOrient[4]);
  cosc = Math.cos(posOrient[5]);

  Delta.m00 = cosb * cosc;
  Delta.m01 = cosb * sinc;
  Delta.m02 = -sinb;

  Delta.m10 = -(cosa * sinc) + (sina * sinb * sinc);
  Delta.m11 = (cosa * cosc) + (sina * sinb * sinc);
  Delta.m12 = sina * cosb;

      Delta.m20 = (sina * sinc) + (cosa * sinb * cosc);
  Delta.m21 = -(sina * cosc) + (cosa * sinb * sinc);
  Delta.m22 = cosa * cosb;
    }


}


java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.