View Javadoc

1   /* -------------------------------------------------------------------
2    * Java source file for the class Manipulator
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:07 $
11   * 
12   * $Id: Manipulator.java,v 1.3 2004/03/03 11:53:07 takatsukam Exp $
13   * 
14   * Reference:		Document no:
15   * ___				___
16   * 
17   * To Do:
18   * ___
19   * 
20  ------------------------------------------------------------------- */
21  
22  /* --------------------------- Package ---------------------------- */
23  package net.jbeans.j3d.util.manipulator;
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.*;
27  import javax.vecmath.*;
28  
29  /*====================================================================
30                   Implementation of class Manipulator                  
31  ====================================================================*/
32  /***
33   * generally describe Manipulator in here
34   * 
35   * @version $Revision: 1.3 $
36   * @author Masahiro Takatsuka (masa@jbeans.net)
37   */
38  
39  public abstract class Manipulator {
40      private boolean buttonPress;
41  	private Vector3d center;
42  	private Vector4d tmpcenter;
43  	private Transform3D centertrans;	
44  	private Matrix3d m3d;
45  	
46  	protected Vector3d localcenter;
47      protected TransformGroup transformGroup;
48      protected Transform3D currXform;
49      protected boolean reset;
50      protected boolean invert;
51  	protected boolean useL2U;
52  	protected Transform3D l2u;	// just rotation
53      protected ManipulatorCallback callback;
54  
55      public Manipulator() {
56          this.buttonPress = false;
57          this.reset = false;
58          this.invert = false;
59          this.currXform = new Transform3D();
60          this.reset = true;
61  		this.useL2U = false;
62  		this.l2u = null;
63          this.callback = null;
64  		this.center = new Vector3d();
65  		this.localcenter = new Vector3d();
66  		this.centertrans = new Transform3D();			
67  		initialize();
68      }
69  
70      public Manipulator(boolean invert) {
71          this.buttonPress = false;
72          this.reset = false;
73          this.invert = invert;
74          this.currXform = new Transform3D();
75          this.reset = true;
76  		this.useL2U = false;
77  		this.l2u = null;
78  		this.callback = null;
79  		this.center = new Vector3d();
80  		this.localcenter = new Vector3d();
81  		this.centertrans = new Transform3D();	
82  		initialize();
83      }
84  	
85      public Manipulator(TransformGroup transformgroup) {
86          this.buttonPress = false;
87          this.reset = false;
88          this.invert = false;
89          this.transformGroup = transformgroup;
90          this.currXform = new Transform3D();
91          this.reset = true;
92  		this.useL2U = false;
93  		this.l2u = null;
94  		this.callback = null;
95  		this.center = new Vector3d();
96  		this.localcenter = new Vector3d();
97  		this.centertrans = new Transform3D();	
98  		initialize();		
99      }
100 
101     public void initialize() {
102     }
103 
104 	public void setCenter(Tuple3d center) {
105 		if (center == null) {
106 			this.center.set(0, 0, 0);
107 		} else {
108 			this.center.set(center);
109 		}
110 	}
111 
112 	public Vector3d getCenter() {
113 		return this.center;
114 	}
115 
116 	public void getCenter(Tuple3d center) {
117 		center.set(this.center);
118 	}
119 
120 	public void setInvert(boolean invert) {
121 		this.invert = invert;
122 	}
123 
124 	public boolean getInvert() {
125 		return this.invert;
126 	}
127 	
128 	public void useLocalToUniverseTransform(boolean useL2U) {
129 		this.useL2U = useL2U;
130 	}
131 
132 	public boolean isLocalToUniverseTransformUsed() {
133 		return this.useL2U;
134 	}
135 
136     public void setTransformGroup(TransformGroup transformgroup) {
137         this.transformGroup = transformgroup;
138         this.currXform = new Transform3D();
139         this.reset = true;
140 		this.useL2U = false;
141 		this.l2u = null;
142     }
143 
144     public void setCallback(ManipulatorCallback callback) {
145         this.callback = callback;
146     }
147 	
148     public void processStimulus(Stimulus stimulus) {
149 		if (this.useL2U) {					
150 			if (this.l2u == null) {
151 				this.l2u = new Transform3D();
152 			}
153 			if (this.m3d == null) {
154 				this.m3d = new Matrix3d();
155 			}
156 			try {
157 				this.transformGroup.getLocalToVworld(this.l2u);
158 				this.l2u.get(this.m3d);		// rotation matrix
159 				this.l2u.set(this.m3d);		// just rotation
160 				this.l2u.invert();
161 			} catch (CapabilityNotSetException e) {
162 				this.useL2U = false;
163 			}
164 		}
165 		if (this.tmpcenter == null) {
166 			this.tmpcenter = new Vector4d();
167 		}
168 		this.tmpcenter.set(this.center.x, this.center.y, this.center.z, 1.0D);
169 		this.transformGroup.getLocalToVworld(this.centertrans);
170 		this.centertrans.invert();
171 		this.centertrans.transform(this.tmpcenter);
172 		this.localcenter.set(this.tmpcenter.x, this.tmpcenter.y, this.tmpcenter.z);
173 	}
174 }