View Javadoc

1   /* -------------------------------------------------------------------
2    * Java source file for the class BasicTransformGroup
3    * 
4    * Copyright (c), 2003, Masahiro Takatsuka.
5    * All Rights Researved.
6    * 
7    * Original Author: Masahiro Takatsuka (masa@jbeans.net)
8    * $Author: takatsukam $
9    * 
10   * $Date: 2004/03/03 11:53:06 $
11   * 
12   * $Id: BasicTransformGroup.java,v 1.3 2004/03/03 11:53:06 takatsukam Exp $
13   * 
14   * Reference:		Document no:
15   * ___				___
16   * 
17   * To Do:
18   * ___
19   * 
20  ------------------------------------------------------------------- */
21  
22  /* --------------------------- Package ---------------------------- */
23  package net.jbeans.j3d.transform;
24  
25  /* ------------------ Import classes (packages) ------------------- *//package-summary/html">class="comment"> ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment"> ------------------ Import classes (packages) ------------------- */
26  import javax.media.j3d.Bounds;
27  import javax.media.j3d.Transform3D;
28  import javax.vecmath.*;
29  import com.sun.j3d.utils.behaviors.mouse.*;
30  
31  import net.jbeans.util.debug.*;
32  
33  /*====================================================================
34               Implementation of class BasicTransformGroup              
35  ====================================================================*/
36  /***
37   * generally describe BasicTransformGroup in here
38   * 
39   * @version $Revision: 1.3 $
40   * @author Masahiro Takatsuka (masa@jbeans.net)
41   * @see javax.media.j3d.TransformGroup
42   */
43  
44  public class BasicTransformGroup extends javax.media.j3d.TransformGroup {
45  	/* ------------------------ not serialized ------------------------ */
46      transient private Transform3D temp= new Transform3D();
47      transient private Transform3D transform3D = new Transform3D();
48  	transient private Vector3d zero = new Vector3d();
49      transient private Vector3d vector = new Vector3d();	
50  	
51  	
52  	/* -------------------------- serialized -------------------------- */
53      private Transform3D initialTransform3D;
54  	
55      public BasicTransformGroup() {
56  		this(new Transform3D());
57      }
58  
59  	public BasicTransformGroup(Transform3D transform3d) {
60    		super();
61  		this.initialTransform3D = transform3d;
62  
63  		common();
64  		set(transform3d);
65  	}
66  	
67      protected void common() {
68  		setCapability(ALLOW_TRANSFORM_READ);
69  		setCapability(ALLOW_TRANSFORM_WRITE);
70  		setCapability(ALLOW_COLLISION_BOUNDS_READ);
71  		setCapability(ALLOW_COLLISION_BOUNDS_WRITE);		
72  		setCapability(ALLOW_CHILDREN_EXTEND);
73  		setCapability(ALLOW_CHILDREN_READ);
74  		setCapability(ALLOW_CHILDREN_WRITE);
75  		setCapability(ALLOW_AUTO_COMPUTE_BOUNDS_READ);
76  		setCapability(ALLOW_AUTO_COMPUTE_BOUNDS_WRITE);
77  		setCapability(ALLOW_BOUNDS_READ);
78  		setCapability(ALLOW_BOUNDS_WRITE);
79  		setCapability(ALLOW_COLLIDABLE_READ);
80  		setCapability(ALLOW_COLLIDABLE_WRITE);		
81  		setCapability(ALLOW_PICKABLE_READ);
82  		setCapability(ALLOW_PICKABLE_WRITE);		
83  		setCapability(ALLOW_LOCAL_TO_VWORLD_READ);
84  		setCapability(ENABLE_PICK_REPORTING);
85  
86  		setBoundsAutoCompute(true);
87  
88          reset();
89      }
90  
91      public void reset() {
92          this.transform3D.set(this.initialTransform3D);
93          updateTransform(this.transform3D);
94      }
95  
96      protected void updateTransform(Transform3D transform3d) {
97          setTransform(transform3d);
98      }
99  
100 	public void setInitialTransform3D(Transform3D t3d) {
101 		this.initialTransform3D = t3d;
102 	}
103 
104 	public Transform3D getInitialTransform3d() {
105 		return this.initialTransform3D;
106 	}
107 	
108     public Vector3d get() {
109         Vector3d vector3d = new Vector3d();
110         get(this.transform3D);
111         this.transform3D.get(vector3d);
112         return vector3d;
113     }
114 
115     public void get(Transform3D transform3d) {
116         getTransform(transform3d);
117     }
118 
119     public void get(double ad[]) {
120         get(this.transform3D);
121         this.transform3D.get(ad);
122     }
123 
124     public void move(Vector3d vector3d) {
125         get(this.transform3D);
126         this.transform3D.get(this.vector);
127         this.vector.add(vector3d);
128         this.transform3D.setTranslation(this.vector);
129         updateTransform(this.transform3D);
130     }
131 
132     public void move(Vector3f vector3f) {
133         move(new Vector3d(vector3f));
134     }
135 
136     public void rotX(double d) {
137         get(this.transform3D);
138         this.temp.setIdentity();
139         this.temp.rotX(d);
140         this.transform3D.get(this.vector);
141         this.transform3D.setTranslation(this.zero);
142         this.transform3D.mul(this.temp, this.transform3D);
143         this.transform3D.setTranslation(this.vector);
144         updateTransform(this.transform3D);
145     }
146 
147     public void rotY(double d) {
148         get(this.transform3D);
149         this.temp.setIdentity();
150         this.temp.rotY(d);
151         this.transform3D.get(this.vector);
152         this.transform3D.setTranslation(this.zero);
153         this.transform3D.mul(this.temp, this.transform3D);
154         this.transform3D.setTranslation(this.vector);
155         updateTransform(this.transform3D);
156     }
157 
158     public void rotZ(double d) {
159         get(this.transform3D);
160         this.temp.setIdentity();
161         this.temp.rotZ(d);
162         this.transform3D.get(this.vector);
163         this.transform3D.setTranslation(this.zero);
164         this.transform3D.mul(this.temp, this.transform3D);
165         this.transform3D.setTranslation(this.vector);
166         updateTransform(this.transform3D);
167     }
168 
169     public void scale(double d) {
170         get(this.transform3D);
171         this.temp.setIdentity();
172         this.temp.set(d);
173         this.transform3D.get(this.vector);
174         this.transform3D.setTranslation(this.zero);
175         this.transform3D.mul(this.temp, this.transform3D);
176         this.transform3D.setTranslation(this.vector);
177         updateTransform(this.transform3D);
178     }
179 
180     public void scale(Vector3d vector3d) {
181         getTransform(this.transform3D);
182         this.temp.setIdentity();
183         this.temp.setScale(vector3d);
184         this.transform3D.get(this.vector);
185         this.transform3D.setTranslation(this.zero);
186         this.transform3D.mul(this.temp, this.transform3D);
187         this.transform3D.setTranslation(this.vector);
188         updateTransform(this.transform3D);
189     }
190 
191     public void set(Transform3D transform3d) {
192         updateTransform(transform3d);
193     }
194 
195     public void set(Vector3d vector3d) {
196         get(this.transform3D);
197         this.transform3D.setTranslation(vector3d);
198         updateTransform(this.transform3D);
199     }
200 
201     public void set(Vector3f vector3f) {
202         get(this.transform3D);
203         this.transform3D.setTranslation(vector3f);
204         updateTransform(this.transform3D);
205     }
206 	
207     public void set(Vector3d vector3d, double d, double d1, double d2) {
208         this.transform3D.setIdentity();
209         this.temp.setIdentity();
210         this.temp.rotX(d);
211         this.transform3D.mul(this.temp, this.transform3D);
212         this.temp.setIdentity();
213         this.temp.rotY(d1);
214         this.transform3D.mul(this.temp, this.transform3D);
215         this.temp.setIdentity();
216         this.temp.rotZ(d2);
217         this.transform3D.mul(this.temp, this.transform3D);
218         this.transform3D.setTranslation(vector3d);
219         updateTransform(this.transform3D);
220     }
221 
222     public void set(TransformGroup gvtransformgroup) {
223         gvtransformgroup.get(this.transform3D);
224         updateTransform(this.transform3D);
225     }
226 
227     public void set(double ad[]) {
228         if(ad.length != 16) {
229             return;
230         } else {
231             this.transform3D.set(ad);
232             updateTransform(this.transform3D);
233             return;
234         }
235     }
236 
237 	public void enableMouseBehaviors() {
238 		BasicTransformGroup.enableMouseBehaviors(this);
239 	}
240 
241 	public static void enableMouseBehaviors(javax.media.j3d.TransformGroup tg ) {
242 		Bounds bounds = tg.getBounds();
243 		
244 		// Create the rotate behavior node
245 		MouseRotate behavior = new MouseRotate();
246 		behavior.setTransformGroup(tg);
247 		tg.addChild(behavior);
248 		behavior.setSchedulingBounds(bounds);
249 		
250 		// Create the zoom behavior node
251 		MouseZoom behavior2 = new MouseZoom();
252 		behavior2.setTransformGroup(tg);
253 		tg.addChild(behavior2);
254 		behavior2.setSchedulingBounds(bounds);
255 		
256 		// Create the translate behavior node
257 		MouseTranslate behavior3 = new MouseTranslate();
258 		behavior3.setTransformGroup(tg);
259 		tg.addChild(behavior3);
260 		behavior3.setSchedulingBounds(bounds);
261 	}
262 }