Can help me?
Thank you
loic
The code :
SpaceMouseBehavior
Code: Select all
package interaction;
import javax.media.j3d.WakeupCondition;
import javax.media.j3d.WakeupOnElapsedFrames;
import javax.media.j3d.Behavior;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.Transform3D;
import javax.media.j3d.Sensor;
import javax.vecmath.*;
/** Class to apply a Sensor's Transform3D to a TransformGroup
*@author Designed by Boran Gögetap, Continental Software Design. Freeware.
*/
public class SpacemouseBehavior extends Behavior
{
static Sensor sens;
/** this holds the Group to Transform
*/
protected TransformGroup transformGroup;
/** this holds the Sensor's latest data
*/
protected Transform3D currXform = new Transform3D();
/** supply TransformGroup and Sensor in the constructor
*/
public SpacemouseBehavior( TransformGroup transformGroup, Sensor sens )
{
//System.out.println( "SpacemouseBehavior.SpacemouseBehavior()" );
this.transformGroup = transformGroup;
this.sens = sens;
}
/** used by j3d framework, not to be called by application
*/
public void initialize()
{
System.out.println( "initialisation..." );
WakeupCondition wu = new WakeupOnElapsedFrames( 1 );
wakeupOn( wu );
}
/** used by j3d framework, not to be called by application
*/
public void processStimulus(java.util.Enumeration criteria)
{
//Matrix3d matrice = new Matrix3d();
//Vector3d vecteur = new Vector3d();
sens.getRead( currXform );
//currXform.get(matrice,vecteur);
//System.out.println(matrice +" "+vecteur);
transformGroup.setTransform( currXform );
WakeupCondition wu = new WakeupOnElapsedFrames( 1 );
wakeupOn( wu );
}
}
Code: Select all
package device;
//package materiel.capteur3d;
//package device;
import javax.media.j3d.InputDevice;
import javax.media.j3d.Sensor;
import javax.media.j3d.Transform3D;
import javax.vecmath.*;
import java.io.*;
//import com._3Dconnexion.*;
/**
* Class to support Space Control's Spacemouse (also known as Space Controller or Magellan)
*
* changed to support the newer InputDevice interface found in Java 3D 1.2 final.
*
* depends on MglStreams by Joerg Vogel (see author info in respective class)
*
*@author Designed by Boran Gögetap, CONTINENTAL SOFTWARE Design. All rights reserved.
*/
public class SpacemouseDevice implements InputDevice
{
/** holds translational data */
protected static Vector3d translation = new Vector3d();
/** holds combined rotational data */
protected static Matrix3d rotation = new Matrix3d();
/** holds rotation about X axis */
protected static Matrix3d rX = new Matrix3d();
/** holds rotation about Y axis */
protected static Matrix3d rY = new Matrix3d();
/** holds rotation about Z axis */
protected static Matrix3d rZ = new Matrix3d();
/** holds translational scale, default = 0.001 */
protected static double Tscale = 0.0001;
/** holds rotational scale, default = 0.001 */
protected static double Rscale = 0.0001;
/** holds Sensor object which is managed by this device
*/
protected static Sensor sens;
/** access to 3DxWare driver
*/
protected static JNIsiapp siapp;
private static boolean firstTime = true;
private static char inbuffer[] = new char[1000];
private static int stelle = 0;
private static boolean indaten = false;
/** taken from CDK's object model */
protected static boolean accumflag = false;
/** taken from CDK's object model */
protected static boolean dirtyflag = true;
//boolean translock, rotlock, dominant;
//int fehlerpakete, iolength;
/** raw data from Spacemouse */
protected static int MagellanData[] = new int[6];
/** handle to inputstream, outputstream + helper funtions to communicate with Spacemouse */
//protected MglStreams streams;
/**
* get raw data.
* @return 6 integer values.
*/
public int[] getMagellanData()
{
return MagellanData;
}
/** taken from Space Control's reference implementation */
private StringBuffer MagellanNibbleList = new StringBuffer( "0AB3D56GH9:K<MN?" );
/**
* nom de la frame
*/
private String nomFrame;
/** taken from Space Control's reference implementation */
private int MagellanNibbleMask( int Value )
{
return Value & 0x000F;
}
/** taken from Space Control's reference implementation */
private boolean isNibble( char CHAR )
{
return MagellanNibbleList.charAt( CHAR&0x0F ) == CHAR;
}
/**
* taken from CDK's object model, controls whether or not to accumulate data samples and effectively make Spacemouse an absolute sensor rather than a relative one.
*/
public void SetAccumFlag( boolean accumflag )
{
this.accumflag = accumflag;
}
/**
* taken from CDK's object model, used to scale rotational data; 0.001 is a reasonable value and also the default
*/
public void SetRscale( double Rscale )
{
this.Rscale = Rscale;
}
/**
* taken from CDK's object model, used to scale translational data; 0.001 is a reasonable value and also the default
*/
public void SetTscale( double Tscale )
{
this.Tscale = Tscale;
}
//
// Konstruktoren:
//
/** construct using default port
*/
public SpacemouseDevice()
{
setNomFrame("VRMiner");
System.out.println("java.home is " + System.getProperty("java.home"));
/*
streams = new MglStreams();
streams.open();
streams.reset();
*/
sens = new Sensor( this, 1 );
rotation.setIdentity();
}
//
// Device Interface Implementierungen:
//
/** used by j3d framework, not to be called by application
*/
public void close()
{
System.out.println( "Closing device.." );
//streams.close();
}
/** used by j3d framework, not to be called by application
*/
public boolean initialize()
{
System.out.println( "initializing device..." );
for( int i=0; i < 0; ++i )
{
sens.setNextSensorRead(
System.currentTimeMillis(),
new Transform3D(
new Matrix3d(),
new Vector3d(10.0,10.0,10.0),
1
),
new int[0]
);
}
System.out.println( "initialized." );
return true;
}
/** used by j3d framework, useless for application
*/
public int getProcessingMode()
{
return InputDevice.BLOCKING;
}
/** used by j3d framework, not to be called by application; always returns 1
*/
public int getSensorCount()
{
//System.out.println( "SpacemouseDevice.getSensorCount()" );
return 1;
}
/** used by j3d framework, not to be called by application; always returns the one and only Sensor
*/
public Sensor getSensor(int sensorIndex)
{
//System.out.println( "SpacemouseDevice.getSensor(" + sensorIndex + ")" );
return sens;
}
/** This method causes the device's sensor readings to be updated by the device driver. For BLOCKING and NON_BLOCKING drivers, this method is called regularly and the Java 3D implementation can cache the sensor values. For DEMAND_DRIVEN drivers this method is called each time one of the Sensor.getRead methods is called, and is not otherwise called.
*/
public void pollAndProcessInput()
{
try {
// System.out.println("SpacemouseDevice: pollandprocessinput");
if (firstTime) {
firstTime = false;
siapp = new JNIsiapp();
siapp.SiInitialize();
siapp.SiOpenWinInit(nomFrame,"eventCallback");
//ouverture du flux
siapp.SiOpen();
System.out.println("Capteur 3D démarré.");
}
// Go into a wait state waiting to be woken up by the JNIsiapp library
siapp.SiWaitForEvent();
// This isn't really necessary. We don't return from SiWaitForEvent
// unless a new event has arrived.
if (siapp.SiIsNewDataAvail()) {
siapp.SiGetEvent();
System.out.println("Event type is "+siapp.event.type);
// Motion event
if (siapp.event.type == JNIsiapp.SiEventType.SI_MOTION_EVENT)
{
/*
System.out.println("Motion Event: "
+siapp.event.mData[0]+", "+
+siapp.event.mData[1]+", "+
+siapp.event.mData[2]+", "+
+siapp.event.mData[3]+", "+
+siapp.event.mData[4]+", "+
+siapp.event.mData[5]+", ");
if(siapp.event.mData[0] < 0) System.out.println("Evenement 0 : Tire");
if(siapp.event.mData[1] < 0) System.out.println("Evenement 1 : Appui");
if(siapp.event.mData[2] < 0) System.out.println("Evenement 2 : droite");
if(siapp.event.mData[3] < 0) System.out.println("Evenement 3 : haut");
if(siapp.event.mData[4] < 0) System.out.println("Evenement 4 : Rotation");
if(siapp.event.mData[5] < 0) System.out.println("Evenement 5 : gauche");
*/
///////////////////////////
// Translation behandeln:
///////////////////////////
if( accumflag )
{
translation.add(
new Vector3d(
siapp.event.mData[JNIsiapp.SI_TX] * Tscale,
siapp.event.mData[JNIsiapp.SI_TY] * Tscale,
-siapp.event.mData[JNIsiapp.SI_TZ] * Tscale
)
);
}
else
{
translation.set(
siapp.event.mData[JNIsiapp.SI_TX] * Tscale,
siapp.event.mData[JNIsiapp.SI_TY] * Tscale,
-siapp.event.mData[JNIsiapp.SI_TZ] * Tscale
);
}
////////////////////////////////
// Rotation behandeln:
////////////////////////////////
rX.rotX( siapp.event.mData[JNIsiapp.SI_RX] * Rscale );
rY.rotY( siapp.event.mData[JNIsiapp.SI_RY] * Rscale );
rZ.rotZ(-siapp.event.mData[JNIsiapp.SI_RZ] * Rscale );
if( !accumflag )
rotation.setIdentity();
/* diese Reihenfolge erzeugt eine Matrix,
welche zu CDK's Make6DRecord äquivalent ist:
*/
rotation.mul( rY, rotation );
rotation.mul( rZ, rotation );
rotation.mul( rX, rotation );
sens.setNextSensorRead(
System.currentTimeMillis(),
new Transform3D(
rotation,
translation,
1
),
new int[0]
);
}
// Button event
else if (siapp.event.type == JNIsiapp.SiEventType.SI_BUTTON_EVENT)
{
System.out.println("Button Event: ");
System.out.println(" last: "+siapp.event.bData.last);
System.out.println(" current: "+siapp.event.bData.current);
System.out.println(" pressed: "+siapp.event.bData.pressed);
System.out.println(" released: "+siapp.event.bData.released);
//reset!
translation.set(0,0,0);
rotation.setIdentity();
rotation.mul(rX);
rotation.mul(rY);
rotation.mul(rZ);
accumflag = false;
}
// Button event
else if (siapp.event.type == JNIsiapp.SiEventType.SI_ZERO_EVENT)
{
System.out.println("Attente d'une action");
}
}
} catch (Exception e) {
System.out.println("Exception in JNIsiappThread: "+e);
siapp.SiClose();
siapp.SiTerminate();
}
}
/** This method will not be called by the Java 3D implementation and should be implemented as an empty method.
*/
public void processStreamInput()
{
}
/** used by j3d framework, not to be called by application - NOT IMPLEMENTED
*/
public void setNominalPositionAndOrientation()
{
System.out.println( "SpacemouseDevice.setNominalPositionAndOrientation()" );
}
/** used by j3d framework, not to be called by application - NOT IMPLEMENTED
*/
public void setProcessingMode(int mode)
{
System.out.println( "SpacemouseDevice.setProcessingMode(" + mode + ")" );
}
/**
* obtient le nom de la frame
* @return le nom de la frame
*/
public String getNomFrame() {
return nomFrame;
}
/**
* definit le nom de la frame
* @param nomFrame le nom de la frame
*/
public void setNomFrame(String nomFrame) {
this.nomFrame = nomFrame;
}
}