/* ========================
* 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: TransformDataJava3d.java,v 1.12 2005/11/02 18:01:26 ogor Exp $
*
* Changes
* -------
* 17-Jan-2004 : Creation date (CC);
*
*/
package syn3d.data.java3d;
import java.io.Serializable;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Matrix3f;
import javax.vecmath.Matrix4f;
import javax.vecmath.Quat4f;
import javax.vecmath.Vector3d;
import syn3d.data.TransformData;
import syn3d.nodes.TransformGroupNode;
/**
* Class description ...
*
* @author Claude CAZENAVE
*
*/
public class TransformDataJava3d
extends TransformData implements Cloneable, Serializable{
static final long serialVersionUID = 5432749054522844975L;
protected transient Transform3D trans=new Transform3D();
protected transient Transform3D composedTrans = new Transform3D();
protected transient TransformGroup group;
Vector3d v3=new Vector3d();
Matrix3f m3=new Matrix3f();
Matrix4f m4=new Matrix4f();
Quat4f q=new Quat4f();
/**
* @param node
*/
public TransformDataJava3d(TransformGroup group, TransformGroupNode node) {
super(node);
this.group = group;
group.getTransform(trans);
init();
}
// Composed transformation
public TransformDataJava3d( TransformGroupNode node, TransformData transformRoot) {
super(node, transformRoot);
this.group = new TransformGroup();
init();
}
public void restoreTransform(TransformGroup group, TransformGroupNode node){
super.restoreTransform(node);
this.group = group;
composedTrans = new Transform3D();
trans = new Transform3D();
group.getTransform(trans);
updateTransform();
group.setTransform(trans);
if (node!=null) node.setDirty();
}
public void restoreTransform(TransformGroupNode node, TransformData transformRoot){
super.restoreTransform(node, transformRoot);
this.group = new TransformGroup();
trans = new Transform3D();
group.getTransform(trans);
updateTransform();
group.setTransform(trans);
if (node!=null) node.setDirty();
}
public Transform3D getTransform(){
return trans;
}
/**
* @param preferredKind
* @param fromNode
*/
protected void init(int preferredKind, boolean fromNode){
if(fromNode){
Transform3D t = new Transform3D();
group.getTransform(t);
trans.set(t);
}
int k;
if(preferredKind<0){
trans.get(m4);
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]=(float)v3.x;
set(k,vx,null);
break;
case TRANSY:
trans.get(v3);
float[] vy=new float[1];
vy[0]=(float)v3.y;
set(k,vy,null);
break;
case TRANSZ:
trans.get(v3);
float[] vz=new float[1];
vz[0]=(float)v3.z;
set(k,vz,null);
break;
case TRANSXYZ:
trans.get(v3);
float[] v=new float[3];
v[0]=(float)v3.x;
v[1]=(float)v3.y;
v[2]=(float)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++){
m3.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{
TransformDataJava3d res=(TransformDataJava3d)super.clone();
res.trans=new Transform3D();
res.v3=new Vector3d();
res.m3=new Matrix3f();
res.m4=new Matrix4f();
res.q=new Quat4f();
return res;
}
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 SCALE:
v3.x=params[0];
v3.y=v3.x;
v3.z=v3.x;
trans.setScale(v3);
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) && isComposed()){
composedTrans.setIdentity();
for(int i=transformChildren.size()-1; i>=0; i--){
((TransformData)transformChildren.get(i)).updateTransform();
composedTrans.mul (((TransformDataJava3d)transformChildren.get(i)).getTransform());
}
composedTrans.mul(trans);
group.setTransform(composedTrans);
// Single transform
}else{
if ((transformRoot==null) || ((transformRoot==this) && (transformChildren.size()==0)))
group.setTransform(trans);
}
}
// Composed transforms
public TransformData addComposedTransformation(){
TransformDataJava3d newTransform = new TransformDataJava3d(node, this);
transformChildren.add(newTransform);
return newTransform;
}
public void removeLastComposedTransformation(){
((TransformData)transformChildren.get(transformChildren.size()-1)).removeSceneGraphData();
transformChildren.remove(transformChildren.size()-1);
}
}