/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info: http://jsynoptic.sourceforge.net/index.html
*
* This program 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 2.1 of the License, or (at your option) any later version.
*
* This program 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
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2004, by :
* Corporate:
* Astrium SAS
* Individual:
* Claude Cazenave
*
* $Id: TransformDataXith3d.java,v 1.7 2005/11/02 18:00:33 ogor Exp $
*
* Changes
* -------
* 17-Jan-2004 : Creation date (CC);
*
*/
package syn3d.data.xith3d;
import java.io.Serializable;
import javax.vecmath.Matrix3f;
import javax.vecmath.Matrix4f;
import javax.vecmath.Quat4f;
import javax.vecmath.Vector3f;
import syn3d.data.TransformData;
import syn3d.nodes.TransformGroupNode;
import com.xith3d.scenegraph.Transform3D;
import com.xith3d.scenegraph.TransformGroup;
/**
* Class description ...
*
* @author Claude CAZENAVE
*
*/
public class TransformDataXith3d
extends TransformData implements Cloneable, Serializable{
protected Transform3D trans;
protected TransformGroup group;
protected Vector3f v3=new Vector3f();
protected Matrix3f m3=new Matrix3f();
protected Matrix4f m4=new Matrix4f();
protected Quat4f q=new Quat4f();
/**
* @param node
*/
public TransformDataXith3d(TransformGroup group, TransformGroupNode node) {
super(node);
this.group = group;
trans = group.getTransform();
init();
}
public Transform3D getTransform(){
return trans;
}
// Composed transformation
public TransformDataXith3d( TransformGroupNode node, TransformData transformRoot) {
super(node, transformRoot);
this.group = new TransformGroup();
init();
}
/* (non-Javadoc)
* @see syn3d.builtin.TransformData#updateTransform()
*/
public void updateTransform() {
switch(kind){
case IDENTITY:
trans.setIdentity();
break;
case TRANSX:
v3.x=params[0];
v3.y=0.f;
v3.z=0.f;
trans.set(v3);
break;
case TRANSY:
v3.x=0.f;
v3.y=params[0];
v3.z=0.f;
trans.set(v3);
break;
case TRANSZ:
v3.x=0.f;
v3.y=0.f;
v3.z=params[0];
trans.set(v3);
break;
case TRANSXYZ:
v3.x=params[0];
v3.y=params[1];
v3.z=params[2];
trans.set(v3);
break;
case ROTX:
trans.rotX(params[0]);
break;
case ROTY:
trans.rotY(params[0]);
break;
case ROTZ:
trans.rotZ(params[0]);
break;
case ROTXYZ:
q.w=params[0];
q.x=params[1];
q.y=params[2];
q.z=params[3];
trans.set(q);
break;
case SCALEX:
v3.x=params[0];
v3.y=1.f;
v3.z=1.f;
trans.setScale(v3);
break;
case SCALEY:
v3.x=1.f;
v3.y=params[0];
v3.z=1.f;
trans.setScale(v3);
break;
case SCALEZ:
v3.x=1.f;
v3.y=1.f;
v3.z=params[0];
trans.setScale(v3);
break;
case SCALEXYZ:
v3.x=params[0];
v3.y=params[1];
v3.z=params[2];
trans.setScale(v3);
break;
case AFFINE:
m3.set(params);
trans.set(m3);
break;
case FULL:
m4.set(params);
trans.set(m4);
break;
}
// Composed transform
if ((transformRoot==this) && (transformChildren.size()!=0)){
Transform3D composedTrans = new Transform3D();
for(int i=transformChildren.size()-1; i>=0; i--){
((TransformData)transformChildren.get(i)).updateTransform();
composedTrans.mul (((TransformDataXith3d)transformChildren.get(i)).getTransform());
}
composedTrans.mul(trans);
group.setTransform(composedTrans);
// Single transform
}else{
if ((transformRoot==null) || ((transformRoot==this) && (transformChildren.size()==0)))
group.setTransform(trans);
}
}
/* (non-Javadoc)
* @see jsynoptic3d.data.TransformData#init(int)
*/
protected void init(int preferredKind, boolean fromNode){
if(fromNode){
Transform3D t = group.getTransform();
if(t!=null){
trans.set(t);
}
else{
trans.setIdentity();
}
}
int k;
trans.get(m4);
if(preferredKind<0){
k=getKind(m4);
}
else{
k=preferredKind;
}
switch(k){
case IDENTITY:
set(k,new float[0],null);
break;
case TRANSX:
trans.get(v3);
float[] vx=new float[1];
vx[0]=v3.x;
set(k,vx,null);
break;
case TRANSY:
trans.get(v3);
float[] vy=new float[1];
vy[0]=v3.y;
set(k,vy,null);
break;
case TRANSZ:
trans.get(v3);
float[] vz=new float[1];
vz[0]=v3.z;
set(k,vz,null);
break;
case TRANSXYZ:
trans.get(v3);
float[] v=new float[3];
v[0]=v3.x;
v[1]=v3.y;
v[2]=v3.z;
set(k,v,null);
break;
case ROTX:
trans.get(q);
float[] qx=new float[1];
qx[0]=2.f*(float)Math.atan2(q.x,q.w);
set(k,qx,null);
break;
case ROTY:
trans.get(q);
float[] qy=new float[1];
qy[0]=2.f*(float)Math.atan2(q.y,q.w);
set(k,qy,null);
break;
case ROTZ:
trans.get(q);
float[] qz=new float[1];
qz[0]=2.f*(float)Math.atan2(q.z,q.w);
set(k,qz,null);
break;
case ROTXYZ:
trans.get(q);
float[] qq=new float[4];
qq[0]=q.w;
qq[1]=q.x;
qq[2]=q.y;
qq[3]=q.z;
set(k,qq,null);
break;
case SCALEX:
float[] sx=new float[1];
sx[0]=m4.m00;
set(k,sx,null);
break;
case SCALEY:
float[] sy=new float[1];
sy[0]=m4.m11;
set(k,sy,null);
break;
case SCALEZ:
float[] sz=new float[1];
sz[0]=m4.m22;
set(k,sz,null);
break;
case SCALEXYZ:
float[] ss=new float[3];
ss[0]=m4.m00;
ss[1]=m4.m11;
ss[2]=m4.m22;
set(k,ss,null);
break;
case SCALE:
float[] s=new float[1];
s[0]=m4.m00;
set(k,s,null);
break;
case AFFINE:
float[] v9=new float[9];
float[] vcc=new float[3];
for(int i=0;i<3;i++){
m4.getRow(i,vcc);
for(int j=0;j<3;j++){
v9[i*3+j]=vcc[j];
}
}
set(k,v9,null);
break;
case FULL:
float[] v16=new float[16];
float[] vc=new float[4];
for(int i=0;i<4;i++){
m4.getRow(i,vc);
for(int j=0;j<4;j++){
v16[i*4+j]=vc[j];
}
}
set(k,v16,null);
break;
}
}
public Object clone() throws CloneNotSupportedException{
TransformDataXith3d res=(TransformDataXith3d)super.clone();
res.trans=new Transform3D();
res.v3=new Vector3f();
res.m3=new Matrix3f();
res.m4=new Matrix4f();
res.q=new Quat4f();
return res;
}
// Composed transforms
public TransformData addComposedTransformation(){
TransformDataXith3d newTransform = new TransformDataXith3d(node, this);
transformChildren.add(newTransform);
return newTransform;
}
public void removeLastComposedTransformation(){
((TransformData)transformChildren.get(transformChildren.size()-1)).removeSceneGraphData();
transformChildren.remove(transformChildren.size()-1);
}
}