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
23 import org.jdom.*;
24 import javax.media.j3d.Transform3D;
25 import java.util.*;
26
27 /***
28 * Abstract superclass for XML handlers. It provides the basic functionality for
29 * setting the label and the transformation of VcObjects. Subclassess should override
30 * the type field to reflect the kind of Object they operate on.
31 *
32 * @author Carlos da Silva dos Santos
33 * @version 1.0
34 */
35 abstract class VcObjectXMLHandler {
36
37 private final String TransformAttribute = "Transform";
38 private final String LabelAttribute = "Label";
39
40 protected static final String trueString = "True";
41 protected static final String falseString = "False";
42
43 private StringTokenizer tokenizer;
44 // children classes must override this to reflect
45 // their own type
46 String type = "VcObject";
47
48 double mat[] = new double[16];
49 Transform3D t3d = new Transform3D();
50 Element element;
51
52 VcObjectXMLHandler() {
53
54 }
55
56 /***
57 * Adds to the scene graph all VcObjects contained in a list.
58 * @param list contains VcObjects.
59 * @param base J3DBase object to receive new VcObjects.
60 */
61 void processList(List list, J3DBase base){
62 if(list.size()<1) return;
63 Iterator iter =list.iterator();
64 Object obj;
65 VcObject vcObj,aux;
66 while(iter.hasNext()){
67 obj = iter.next();
68 if(obj instanceof Element){
69 element = (Element) obj;
70 vcObj = createObject(element);
71 if(vcObj!=null){
72 addObject(vcObj,base);
73 }
74 }
75 }
76 }
77
78 /***
79 * Adds a given VcObject to the scene graph. Subclasses should implement this
80 * method so to perform the specific operations necessary to add an object to
81 * the scene graph, depending on the type of VcObject being processed.
82 * @param vcObject object to be added to scene graph.
83 * @param base J3DBase object to receive new VcObject.
84 */
85 abstract void addObject(VcObject vcObject, J3DBase base);
86
87 /***
88 * Creates an XML Element for a given VcObject. Transformation and Label information
89 * are transferred to the Element.
90 */
91 Element createElement(VcObject obj){
92 Element element = new Element(type);
93 element.setAttribute(LabelAttribute,obj.getLabel());
94
95 obj.getTransforms(t3d);
96 t3d.get(mat);
97
98 // holds a string representation of the transform matrix
99 StringBuffer matrix = new StringBuffer();
100 for(int i=0;i<mat.length;i++){
101 matrix.append(mat[i]);
102 matrix.append(";");
103 }
104 System.out.println("matrix: "+matrix);
105 element.setAttribute(TransformAttribute,matrix.toString());
106
107 return element;
108 }
109
110 abstract VcObject createObject(Element el);
111
112 /***
113 * Copies basic VcObject parameters from input element to a given VcObject.
114 * Subclasses may rely on this method to set the label and transformation for
115 * an object newly created.
116 * @param element XML Element containing label and transform informations
117 * @param obj VcObject to be initialized
118 */
119 void initializeObject(Element element, VcObject obj){
120 obj.setLabel(element.getAttributeValue(LabelAttribute));
121 String matrix = element.getAttributeValue(TransformAttribute);
122 if(matrix!=null&&matrix.length()!=0){
123 tokenizer = new StringTokenizer(matrix,";",false);
124 // 16 = number of elements in transform matrix
125 if(tokenizer.countTokens()!=16) return;
126 for(int i=0;i<mat.length;i++){
127 mat[i]= Double.parseDouble(tokenizer.nextToken());
128 }
129 t3d.set(mat);
130 obj.setTransform(t3d);
131 }
132 }
133
134 /***
135 * Returns a String identifying the type of VcObject processed by the handler.
136 */
137 String getType(){
138 return this.type;
139 }
140 }
This page was automatically generated by Maven