1 /*****************************************************************************
2 * Virtual Mockup for Machine Vision
3 * Copyright (C) 2001-2003 Fabio R. de Miranda, João E. Kogler Jr.,
4 * Carlos S. Santos.
5 * Virtual Mockup for Machine Vision Project funded by SENAC-SP
6 *
7 * Permission is granted to redistribute and/or modify this
8 * software under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License (http://www.gnu.org/copyleft/lesser.html)
16 * for more details.
17 *
18 *****************************************************************************/
19
20 package camera3d;
21
22 import com.sun.j3d.utils.geometry.*;
23 import javax.media.j3d.*;
24 import javax.vecmath.*;
25 import java.awt.Font;
26 import camera3d.test.NodeTester;
27
28 /***
29 * An axis system (XYZ) representation.
30 * @author Fábio Roberto de Miranda
31 * @version 1.0
32 */
33 public class Axis {
34
35 /*** root BranchGroup for the whole axis*/
36 private BranchGroup axisBG;
37
38 /*** BranchGroup that directly holds axis geometry. It is made dettachable so axis can be hidden */
39 BranchGroup secondaryBG;
40
41 /*** isAttached will be true when secondaryBG is attached to axisBG*/
42 boolean isAttached = true;
43
44 /*** root TransformGroup for the axis*/
45 TransformGroup axisTG;
46
47 /***
48 * The axis themselves
49 */
50 Shape3D axisShape;
51
52 /* The Labels "X", "Y" and "Z" that will be at the extremities of the axes
53 */
54 /* Uncomment this some day in hope that more than one view can see the same
55 * OrientedShape3D - Fabio 24-04-2002 */
56 /*
57 OrientedShape3D xShape;
58 OrientedShape3D yShape;
59 OrientedShape3D zShape;
60 */
61 //
62
63 Shape3D xShape;
64 Shape3D yShape;
65 Shape3D zShape;
66
67 /* Displacements for x, y and z shapes of characters*/
68 TransformGroup xTG;
69 TransformGroup yTG;
70 TransformGroup zTG;
71
72 /* LineArrays to hold line segments
73 */
74 LineArray xGeometry;
75 LineArray yGeometry;
76 LineArray zGeometry;
77
78 /* LineAttributes common to all LineArrays */
79 LineAttributes lineAttributes;
80
81 /* PolygonAttributes valid for axis labels */
82 PolygonAttributes polygonAttributes;
83
84 /* ColoringAttributes valid for axis labels */
85 ColoringAttributes coloringAttributes;
86
87
88 /* Appearance node common to all shape3Ds*/
89 Appearance appearance = new Appearance();
90 Material material = new Material();
91
92 /* Font3d used to write axis labels*/
93 //Font3D font3d = new Font3D(new Font("Arial", Font.BOLD, 1), new FontExtrusion());
94 Font3D font3d = new Font3D(Font.decode("Arial-BOLD-1"), new FontExtrusion());
95
96
97 Color3f defaultColor = new Color3f(0.0f, 0.1f, 0.5f);
98 Color3f selectedColor = new Color3f(1.0f, 0.0f, 0.0f);
99
100 Color3f red = new Color3f(1.f, 0.f, 0.f);
101 Color3f green = new Color3f(0.f, 1.f, 0.f);
102 Color3f blue = new Color3f(0.f, 0.f, 1.f);
103
104 /* The point around which, in their local coordinate systems, X, Y and Z labels will rotate*/
105 Point3f centerP3f = new Point3f(0.0f, 0.0f, 0.0f);
106
107 Transform3D tempT3D = new Transform3D();
108
109 public Axis() {
110 axisShape = new Shape3D();
111 appearance = new Appearance();
112 axisBG = new BranchGroup();
113 secondaryBG = new BranchGroup();
114 axisTG = new TransformGroup();
115 secondaryBG.addChild(axisTG);
116 axisBG.addChild(secondaryBG);
117 //appearance.setMaterial(material);
118 material.setDiffuseColor(defaultColor);
119 material.setAmbientColor(defaultColor);
120 material.setSpecularColor(defaultColor);
121
122
123 polygonAttributes = new PolygonAttributes(PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_BACK, 0.5f);
124 appearance.setPolygonAttributes(polygonAttributes);
125 coloringAttributes = new ColoringAttributes(defaultColor, ColoringAttributes.SHADE_FLAT);
126 appearance.setColoringAttributes(coloringAttributes);
127
128
129 xTG = new TransformGroup();
130 yTG = new TransformGroup();
131 zTG = new TransformGroup();
132
133 axisBG.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
134 axisBG.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
135 axisBG.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
136 axisTG.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
137 axisTG.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
138 axisTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
139 axisTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
140
141 secondaryBG.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
142 secondaryBG.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
143 secondaryBG.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
144 secondaryBG.setCapability(BranchGroup.ALLOW_DETACH);
145
146 xTG.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
147 xTG.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
148 xTG.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ);
149
150 yTG.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
151 yTG.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
152 yTG.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ);
153
154 zTG.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
155 zTG.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
156 zTG.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ);
157
158 Vector3f tempVec3f = new Vector3f();
159 Transform3D tempT3D = new Transform3D();
160
161 /* If this scale factor is not set labels tend to get too big */
162 tempT3D.setScale(0.22);
163
164 /* Sets up line attributes and adds it to appearance */
165 lineAttributes = new LineAttributes(3, LineAttributes.PATTERN_SOLID, false);
166 appearance.setLineAttributes(lineAttributes);
167
168 axisShape.setAppearance(appearance);
169 axisShape.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
170 axisShape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
171 axisShape.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ);
172
173
174 /* X axis */
175 xGeometry = new LineArray(2, LineArray.COORDINATES|LineArray.COLOR_3);
176 xGeometry.setCapability(LineArray.ALLOW_COLOR_READ);
177 xGeometry.setCapability(LineArray.ALLOW_COLOR_WRITE);
178 double[][] xCoordinates = new double[][]{{0.0, 0.0, 0.0},{1.0, 0.0, 0.0}};
179 for (int i=0; i<xCoordinates.length; i++){
180 xGeometry.setCoordinate(i, xCoordinates[i]);
181 //xGeometry.setColor(i, defaultColor);
182 xGeometry.setColor(i, red);
183 }
184 axisShape.addGeometry(xGeometry);
185 axisShape.setAppearance(appearance);
186 Text3D xText = new Text3D(font3d, "X");
187
188 // Construtor para quando nenhum orientedShape3D funcionar
189 xShape = new Shape3D(xText, appearance);
190 //xShape = new OrientedShape3D(xText, appearance, OrientedShape3D.ROTATE_ABOUT_POINT, centerP3f);
191 //xShape.setConstantScaleEnable(true);
192 xShape.setAppearance(appearance);
193 tempVec3f.x = 1.f;
194 tempVec3f.y = 0.f;
195 tempVec3f.z = 0.f;
196 tempT3D.setTranslation(tempVec3f);
197 xTG.setTransform(tempT3D);
198 xTG.addChild(xShape);
199 xShape.setCapability(xShape.ALLOW_GEOMETRY_READ);
200 xShape.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ);
201 xShape.setCapability(OrientedShape3D.ALLOW_SCALE_READ);
202 //
203
204 /* Y axis */
205 yGeometry = new LineArray(2, LineArray.COORDINATES|LineArray.COLOR_3);
206 yGeometry.setCapability(LineArray.ALLOW_COLOR_READ);
207 yGeometry.setCapability(LineArray.ALLOW_COLOR_WRITE);
208 double[][] yCoordinates = new double[][]{{0.0, 0.0, 0.0},{0.0, 1.0, 0.0}};
209 for (int i=0; i<yCoordinates.length; i++){
210 yGeometry.setCoordinate(i, yCoordinates[i]);
211 //yGeometry.setColor(i, defaultColor);
212 yGeometry.setColor(i, green);
213 }
214 axisShape.addGeometry(yGeometry);
215
216 Text3D yText = new Text3D(font3d, "Y");
217 yShape = new Shape3D(yText, appearance);
218 //yShape = new OrientedShape3D(yText, appearance, OrientedShape3D.ROTATE_ABOUT_POINT, centerP3f);
219 //yShape.setConstantScaleEnable(true);
220 tempVec3f.x = 0.f;
221 tempVec3f.y = 1.f;
222 tempVec3f.z = 0.f;
223 tempT3D.setTranslation(tempVec3f);
224 yTG.setTransform(tempT3D);
225 yTG.addChild(yShape);
226 yShape.setCapability(xShape.ALLOW_GEOMETRY_READ);
227 yShape.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ);
228 //
229
230 /* Z axis */
231 zGeometry = new LineArray(2, LineArray.COORDINATES|LineArray.COLOR_3);
232 zGeometry.setCapability(LineArray.ALLOW_COLOR_READ);
233 zGeometry.setCapability(LineArray.ALLOW_COLOR_WRITE);
234 double[][] zCoordinates = new double[][]{{0.0, 0.0, 0.0},{0.0, 0.0, 1.0}};
235 for (int i=0; i<zCoordinates.length; i++){
236 zGeometry.setCoordinate(i, zCoordinates[i]);
237 //zGeometry.setColor(i, defaultColor);
238 zGeometry.setColor(i, blue);
239 }
240 axisShape.addGeometry(zGeometry);
241
242 Text3D zText = new Text3D(font3d, "Z");
243 zShape = new Shape3D(zText, appearance);
244 //zShape = new OrientedShape3D(zText, appearance, OrientedShape3D.ROTATE_ABOUT_POINT, centerP3f);
245 //zShape.setConstantScaleEnable(true);
246 tempVec3f.x = 0.f;
247 tempVec3f.y = 0.f;
248 tempVec3f.z = 1.f;
249 tempT3D.setTranslation(tempVec3f);
250 zTG.setTransform(tempT3D);
251 zTG.addChild(zShape);
252 zShape.setCapability(xShape.ALLOW_GEOMETRY_READ);
253 zShape.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ);
254
255 //
256
257 xShape.setAppearance(appearance);
258 yShape.setAppearance(appearance);
259 zShape.setAppearance(appearance);
260
261 axisTG.addChild(xTG);
262 axisTG.addChild(yTG);
263 axisTG.addChild(zTG);
264 axisTG.addChild(axisShape);
265
266 axisBG.setPickable(false);
267 axisBG.compile();
268
269 }
270
271 /***
272 * Returns root branch group under which is attached axis geometry.
273 */
274 public BranchGroup getRootGroup(){
275 return this.axisBG;
276 }
277
278 /***
279 * Controls visibility of the axis representation.
280 */
281 public void setVisible(boolean visible){
282 ///Commented on 21-03-2002 while tracking a SingularMatrixException
283 ///double size = DimensionsManager.getDimensionsManager().getAxisSize();/-
284 ///this.setSize(size);
285 if (visible == true){
286 if (isAttached){
287 // do nothing
288 } else {
289 // attach
290 attachBG();
291 }
292 } else {
293 if (isAttached){
294 //detach
295 detachBG();
296 } else {
297 // do nothing
298 }
299 }
300 }
301
302 void detachBG(){
303 secondaryBG.detach();
304 isAttached = false;
305 }
306
307 void attachBG(){
308 axisBG.addChild(secondaryBG);
309 isAttached = true;
310 }
311
312 /***
313 * Tests the Axis class by creating a simple virtual universe and adding an Axis
314 * to it.
315 *
316 */
317 public static void main(String[] args) {
318 String[] fontNames = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
319 System.out.println("Available font names:");
320 for (int i=0; i<fontNames.length; i++){
321 System.out.println(fontNames[i]);
322 }
323 Axis axis = new Axis();
324 NodeTester tester = new NodeTester();
325 tester.add(axis.getRootGroup());
326 }
327
328 /***
329 * Sets the length of the axis representation (the three axis have the same length).
330 */
331 public void setSize(double size){
332 tempT3D.setScale(size);
333 this.axisTG.setTransform(tempT3D);
334 }
335
336 /***
337 * Returns the length of the axis representation (the three axis have the same length).
338 */
339 public double getSize(){
340 this.axisTG.getTransform(tempT3D);
341 return tempT3D.getScale();
342 }
343
344
345 }
This page was automatically generated by Maven