TUIO.TuioClient Maven / Gradle / Ivy
Show all versions of ProcessingTUIO Show documentation
/*
TUIO Java library
Copyright (c) 2005-2014 Martin Kaltenbrunner
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
package TUIO;
import com.illposed.osc.*;
import java.util.*;
/**
* The TuioClient class is the central TUIO protocol decoder component. It provides a simple callback infrastructure using the {@link TuioListener} interface.
* In order to receive and decode TUIO messages an instance of TuioClient needs to be created. The TuioClient instance then generates TUIO events
* which are broadcasted to all registered classes that implement the {@link TuioListener} interface.
*
* TuioClient client = new TuioClient();
* client.addTuioListener(myTuioListener);
* client.connect();
*
*
* @author Martin Kaltenbrunner
* @version 1.1.0
*/
public class TuioClient implements OSCListener {
private int port = 3333;
private OSCPortIn oscPort;
private boolean connected = false;
private Hashtable objectList = new Hashtable();
private Vector aliveObjectList = new Vector();
private Vector newObjectList = new Vector();
private Hashtable cursorList = new Hashtable();
private Vector aliveCursorList = new Vector();
private Vector newCursorList = new Vector();
private Hashtable blobList = new Hashtable();
private Vector aliveBlobList = new Vector();
private Vector newBlobList = new Vector();
private Vector frameObjects = new Vector();
private Vector frameCursors = new Vector();
private Vector frameBlobs = new Vector();
private Vector freeCursorList = new Vector();
private int maxCursorID = -1;
private Vector freeBlobList = new Vector();
private int maxBlobID = -1;
private long currentFrame = 0;
private TuioTime currentTime;
private Vector listenerList = new Vector();
/**
* The default constructor creates a client that listens to the default TUIO port 3333
*/
public TuioClient() {}
/**
* This constructor creates a client that listens to the provided port
*
* @param port the listening port number
*/
public TuioClient(int port) {
this.port = port;
}
/**
* The TuioClient starts listening to TUIO messages on the configured UDP port
* All reveived TUIO messages are decoded and the resulting TUIO events are broadcasted to all registered TuioListeners
*/
public void connect() {
TuioTime.initSession();
currentTime = new TuioTime();
currentTime.reset();
try {
oscPort = new OSCPortIn(port);
oscPort.addListener("/tuio/2Dobj",this);
oscPort.addListener("/tuio/2Dcur",this);
oscPort.addListener("/tuio/2Dblb",this);
oscPort.startListening();
connected = true;
} catch (Exception e) {
System.out.println("TuioClient: failed to connect to port "+port);
connected = false;
}
}
/**
* The TuioClient stops listening to TUIO messages on the configured UDP port
*/
public void disconnect() {
oscPort.stopListening();
try { Thread.sleep(100); }
catch (Exception e) {};
oscPort.close();
connected = false;
}
/**
* Returns true if this TuioClient is currently connected.
* @return true if this TuioClient is currently connected
*/
public boolean isConnected() { return connected; }
/**
* Adds the provided TuioListener to the list of registered TUIO event listeners
*
* @param listener the TuioListener to add
*/
public void addTuioListener(TuioListener listener) {
listenerList.addElement(listener);
}
/**
* Removes the provided TuioListener from the list of registered TUIO event listeners
*
* @param listener the TuioListener to remove
*/
public void removeTuioListener(TuioListener listener) {
listenerList.removeElement(listener);
}
/**
* Removes all TuioListener from the list of registered TUIO event listeners
*/
public void removeAllTuioListeners() {
listenerList.clear();
}
/**
* Returns a Vector of all currently active TuioObjects
*
* @return a Vector of all currently active TuioObjects
* @deprecated use {@link #getTuioObjectList()} instead.
*/
@Deprecated
public Vector getTuioObjects() {
return new Vector(objectList.values());
}
/**
* Returns an ArrayList of all currently active TuioObjects
*
* @return an ArrayList of all currently active TuioObjects
*/
public ArrayList getTuioObjectList() {
return new ArrayList(objectList.values());
}
/**
* Returns a Vector of all currently active TuioCursors
*
* @return a Vector of all currently active TuioCursors
* @deprecated use {@link #getTuioCursorList()} instead.
*/
@Deprecated
public Vector getTuioCursors() {
return new Vector(cursorList.values());
}
/**
* Returns an ArrayList of all currently active TuioCursors
*
* @return an ArrayList of all currently active TuioCursors
*/
public ArrayList getTuioCursorList() {
return new ArrayList(cursorList.values());
}
/**
* Returns a Vector of all currently active TuioBlobs
*
* @return a Vector of all currently active TuioBlobs
* @deprecated use {@link #getTuioBlobList()} instead.
*/
@Deprecated
public Vector getTuioBlobs() {
return new Vector(blobList.values());
}
/**
* Returns an ArrayList of all currently active TuioBlobs
*
* @return an ArrayList of all currently active TuioBlobs
*/
public ArrayList getTuioBlobList() {
return new ArrayList(blobList.values());
}
/**
* Returns the TuioObject corresponding to the provided Session ID
* or NULL if the Session ID does not refer to an active TuioObject
*
* @param s_id the Session ID of the required TuioObject
* @return an active TuioObject corresponding to the provided Session ID or NULL
*/
public TuioObject getTuioObject(long s_id) {
return objectList.get(s_id);
}
/**
* Returns the TuioCursor corresponding to the provided Session ID
* or NULL if the Session ID does not refer to an active TuioCursor
*
* @param s_id the Session ID of the required TuioCursor
* @return an active TuioCursor corresponding to the provided Session ID or NULL
*/
public TuioCursor getTuioCursor(long s_id) {
return cursorList.get(s_id);
}
/**
* Returns the TuioBlob corresponding to the provided Session ID
* or NULL if the Session ID does not refer to an active TuioBlob
*
* @param s_id the Session ID of the required TuioBlob
* @return an active TuioBlob corresponding to the provided Session ID or NULL
*/
public TuioBlob getTuioBlob(long s_id) {
return blobList.get(s_id);
}
/**
* The OSC callback method where all TUIO messages are received and decoded
* and where the TUIO event callbacks are dispatched
*
* @param date the time stamp of the OSC bundle
* @param message the received OSC message
*/
public void acceptMessage(Date date, OSCMessage message) {
Object[] args = message.getArguments();
String command = (String)args[0];
String address = message.getAddress();
if (address.equals("/tuio/2Dobj")) {
if (command.equals("set")) {
long s_id = ((Integer)args[1]).longValue();
int c_id = ((Integer)args[2]).intValue();
float xpos = ((Float)args[3]).floatValue();
float ypos = ((Float)args[4]).floatValue();
float angle = ((Float)args[5]).floatValue();
float xspeed = ((Float)args[6]).floatValue();
float yspeed = ((Float)args[7]).floatValue();
float rspeed = ((Float)args[8]).floatValue();
float maccel = ((Float)args[9]).floatValue();
float raccel = ((Float)args[10]).floatValue();
if (objectList.get(s_id) == null) {
TuioObject addObject = new TuioObject(s_id,c_id,xpos,ypos,angle);
frameObjects.addElement(addObject);
} else {
TuioObject tobj = objectList.get(s_id);
if (tobj==null) return;
if ((tobj.xpos!=xpos) || (tobj.ypos!=ypos) || (tobj.angle!=angle) || (tobj.x_speed!=xspeed) || (tobj.y_speed!=yspeed) || (tobj.rotation_speed!=rspeed) || (tobj.motion_accel!=maccel) || (tobj.rotation_accel!=raccel)) {
TuioObject updateObject = new TuioObject(s_id,c_id,xpos,ypos,angle);
updateObject.update(xpos,ypos,angle,xspeed,yspeed,rspeed,maccel,raccel);
frameObjects.addElement(updateObject);
}
}
} else if (command.equals("alive")) {
newObjectList.clear();
for (int i=1;i0) {
if (fseq>currentFrame) currentTime = TuioTime.getSessionTime();
if ((fseq>=currentFrame) || ((currentFrame-fseq)>100)) currentFrame=fseq;
else lateFrame = true;
} else if (TuioTime.getSessionTime().subtract(currentTime).getTotalMilliseconds()>100) {
currentTime = TuioTime.getSessionTime();
}
if (!lateFrame) {
Enumeration frameEnum = frameObjects.elements();
while(frameEnum.hasMoreElements()) {
TuioObject tobj = frameEnum.nextElement();
switch (tobj.getTuioState()) {
case TuioObject.TUIO_REMOVED:
TuioObject removeObject = tobj;
removeObject.remove(currentTime);
for (int i=0;i buffer = aliveObjectList;
aliveObjectList = newObjectList;
// recycling the vector
newObjectList = buffer;
}
frameObjects.clear();
}
} else if (address.equals("/tuio/2Dcur")) {
if (command.equals("set")) {
long s_id = ((Integer)args[1]).longValue();
float xpos = ((Float)args[2]).floatValue();
float ypos = ((Float)args[3]).floatValue();
float xspeed = ((Float)args[4]).floatValue();
float yspeed = ((Float)args[5]).floatValue();
float maccel = ((Float)args[6]).floatValue();
if (cursorList.get(s_id) == null) {
TuioCursor addCursor = new TuioCursor(s_id, -1 ,xpos,ypos);
frameCursors.addElement(addCursor);
} else {
TuioCursor tcur = cursorList.get(s_id);
if (tcur==null) return;
if ((tcur.xpos!=xpos) || (tcur.ypos!=ypos) || (tcur.x_speed!=xspeed) || (tcur.y_speed!=yspeed) || (tcur.motion_accel!=maccel)) {
TuioCursor updateCursor = new TuioCursor(s_id,tcur.getCursorID(),xpos,ypos);
updateCursor.update(xpos,ypos,xspeed,yspeed,maccel);
frameCursors.addElement(updateCursor);
}
}
//System.out.println("set cur " + s_id+" "+xpos+" "+ypos+" "+xspeed+" "+yspeed+" "+maccel);
} else if (command.equals("alive")) {
newCursorList.clear();
for (int i=1;i0) {
if (fseq>currentFrame) currentTime = TuioTime.getSessionTime();
if ((fseq>=currentFrame) || ((currentFrame-fseq)>100)) currentFrame = fseq;
else lateFrame = true;
} else if (TuioTime.getSessionTime().subtract(currentTime).getTotalMilliseconds()>100) {
currentTime = TuioTime.getSessionTime();
}
if (!lateFrame) {
Enumeration frameEnum = frameCursors.elements();
while(frameEnum.hasMoreElements()) {
TuioCursor tcur = frameEnum.nextElement();
switch (tcur.getTuioState()) {
case TuioCursor.TUIO_REMOVED:
TuioCursor removeCursor = tcur;
removeCursor.remove(currentTime);
for (int i=0;i0) {
Enumeration clist = cursorList.elements();
while (clist.hasMoreElements()) {
int c_id = clist.nextElement().getCursorID();
if (c_id>maxCursorID) maxCursorID=c_id;
}
Enumeration flist = freeCursorList.elements();
while (flist.hasMoreElements()) {
int c_id = flist.nextElement().getCursorID();
if (c_id>=maxCursorID) freeCursorList.removeElement(c_id);
}
} else freeCursorList.clear();
} else if (removeCursor.getCursorID()0)) {
TuioCursor closestCursor = freeCursorList.firstElement();
Enumeration testList = freeCursorList.elements();
while (testList.hasMoreElements()) {
TuioCursor testCursor = testList.nextElement();
if (testCursor.getDistance(tcur) buffer = aliveCursorList;
aliveCursorList = newCursorList;
// recycling the vector
newCursorList = buffer;
}
frameCursors.clear();
}
} else if (address.equals("/tuio/2Dblb")) {
if (command.equals("set")) {
long s_id = ((Integer)args[1]).longValue();
float xpos = ((Float)args[3]).floatValue();
float ypos = ((Float)args[4]).floatValue();
float angle = ((Float)args[5]).floatValue();
float width = ((Float)args[6]).floatValue();
float height = ((Float)args[7]).floatValue();
float area = ((Float)args[8]).floatValue();
float xspeed = ((Float)args[9]).floatValue();
float yspeed = ((Float)args[10]).floatValue();
float rspeed = ((Float)args[11]).floatValue();
float maccel = ((Float)args[12]).floatValue();
float raccel = ((Float)args[13]).floatValue();
if (blobList.get(s_id) == null) {
TuioBlob addBlob = new TuioBlob(s_id, -1 ,xpos,ypos,angle,width,height,area);
frameBlobs.addElement(addBlob);
} else {
TuioBlob tblb = blobList.get(s_id);
if (tblb==null) return;
if ((tblb.xpos!=xpos) || (tblb.ypos!=ypos) || (tblb.x_speed!=xspeed) || (tblb.y_speed!=yspeed) || (tblb.motion_accel!=maccel)) {
TuioBlob updateBlob = new TuioBlob(s_id,tblb.getBlobID(),xpos,ypos,angle,width,height,area);
updateBlob.update(xpos,ypos,angle,width,height,area,xspeed,yspeed,rspeed,maccel,raccel);
frameBlobs.addElement(updateBlob);
}
}
//System.out.println("set blb " + s_id+" "+xpos+" "+ypos+" "+xspeed+" "+yspeed+" "+maccel);
} else if (command.equals("alive")) {
newBlobList.clear();
for (int i=1;i0) {
if (fseq>currentFrame) currentTime = TuioTime.getSessionTime();
if ((fseq>=currentFrame) || ((currentFrame-fseq)>100)) currentFrame = fseq;
else lateFrame = true;
} else if (TuioTime.getSessionTime().subtract(currentTime).getTotalMilliseconds()>100) {
currentTime = TuioTime.getSessionTime();
}
if (!lateFrame) {
Enumeration frameEnum = frameBlobs.elements();
while(frameEnum.hasMoreElements()) {
TuioBlob tblb = frameEnum.nextElement();
switch (tblb.getTuioState()) {
case TuioBlob.TUIO_REMOVED:
TuioBlob removeBlob = tblb;
removeBlob.remove(currentTime);
for (int i=0;i0) {
Enumeration blist = blobList.elements();
while (blist.hasMoreElements()) {
int b_id = blist.nextElement().getBlobID();
if (b_id>maxBlobID) maxBlobID=b_id;
}
Enumeration flist = freeBlobList.elements();
while (flist.hasMoreElements()) {
int b_id = flist.nextElement().getBlobID();
if (b_id>=maxBlobID) freeBlobList.removeElement(b_id);
}
} else freeBlobList.clear();
} else if (removeBlob.getBlobID()0)) {
TuioBlob closestBlob = freeBlobList.firstElement();
Enumeration testList = freeBlobList.elements();
while (testList.hasMoreElements()) {
TuioBlob testBlob = testList.nextElement();
if (testBlob.getDistance(tblb) buffer = aliveBlobList;
aliveBlobList = newBlobList;
// recycling the vector
newBlobList = buffer;
}
frameBlobs.clear();
}
}
}
}