View Javadoc

1   /* -------------------------------------------------------------------
2    * Java source file for the class PolygonModeler
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:05 $
11   * 
12   * $Id: PolygonModeler.java,v 1.3 2004/03/03 11:53:05 takatsukam Exp $
13   * 
14   * Reference:		Document no:
15   * ___				___
16   * 
17   * To Do:
18   * ___
19   * 
20  ------------------------------------------------------------------- */
21  
22  /* --------------------------- Package ---------------------------- */
23  package net.jbeans.j3d.modeler.geometry;
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 java.awt.*;
27  import java.awt.event.*;
28  import java.io.*;
29  import java.lang.reflect.*;
30  import java.util.*;
31  import com.sun.j3d.utils.geometry.*;
32  
33  import net.jbeans.util.debug.*;
34  
35  /*====================================================================
36                  Implementation of class PolygonModeler                
37  ====================================================================*/
38  /***
39   * PolygonModeler generates a suface geometry from the specified data.
40   * 
41   * @version $Revision: 1.3 $
42   * @author Masahiro Takatsuka (masa@jbeans.net)
43   * @see SurfaceModeler
44   */
45  
46  public class PolygonModeler extends SurfaceModeler {
47  	private static final boolean DEBUG = Debug.getDebugFlag(PolygonModeler.class);
48  
49  	public static final int XC = 0; // X coordinates
50  	public static final int YC = 1; // Y coordinates
51  	public static final int ZC = 2; // Z coordinates
52  	public static final int CC = 3; // color
53  	public static final int NC = 4; // normals
54  	public static final int TC = 5; // texture coordinates
55  	public static final int AC = 6; // Attributes such as scalar, tensor, vector
56  	public static final int STRIPC = 7; // strip counts
57  	public static final int CONC = 8; // contour counts
58  
59  	/* ------------------------ not serialized ------------------------ */
60  	
61  	/* -------------------------- serialized -------------------------- */
62  	
63  	public PolygonModeler() {
64  		super();
65  	}
66  
67  	protected void initialize() {
68  		super.initialize();
69  		setPrimitive(GeometryInfo.POLYGON_ARRAY);
70  	}
71  
72  	protected void initGeometry() {
73  		this.totalIndex = 9;// x, y, cx, cy, cz, cc, cn, ct, ca.
74  		//this.essentials = new int[] {XC, YC, ZC, STRIPC, CONC};
75  		this.essentials = new int[] {XC, YC, ZC, STRIPC};
76  		this.entryArray = new Object[this.totalIndex];
77  	}
78  	
79  	public void setXCoordinates(double[] x) {
80  		setValueAt(XC, x);
81  	}
82  
83  	public void setYCoordinates(double[] y) {
84  		setValueAt(YC, y);
85  	}
86  
87  	public void setZCoordinates(double[] z) {
88  		setValueAt(ZC, z);
89  	}
90  
91  	public void setColors(double[] color) {
92  		setValueAt(CC, color);
93  	}
94  
95  	public void setNormals(double[][] normal) {
96  		setValueAt(NC, normal);
97  	}
98  
99  	public void setTextureCoords(double[][] tCoords) {
100 		setValueAt(TC, tCoords);
101 	}
102 
103 	public void setStripCounts(int[] stripcounts) {
104 		setValueAt(STRIPC, stripcounts);
105 	}
106 
107 	public void setContourCounts(int[] contourcounts) {
108 		setValueAt(CONC, contourcounts);
109 	}
110 
111 	/*
112 	  public void setAttributtes(Object[] attri) {
113 	  setValueAt(AC, attri);
114 	  }
115 	*/
116 	
117 	protected boolean checkSize() {
118 		double[] x = (double[]) this.entryArray[XC];
119 		double[] y = (double[]) this.entryArray[YC];
120 		double[] z = (double[]) this.entryArray[ZC];
121 		double[] color = (double[]) this.entryArray[CC];
122 		double[][] normal = (double[][]) this.entryArray[NC];
123 		double[][] tCoord = (double[][]) this.entryArray[TC];
124 
125 		int size = -1;
126 
127 		for (int i = 0; i < this.entryArray.length; i++) {
128 			if (this.entryArray[i] != null) {
129 				size = Array.getLength(this.entryArray[i]);
130 				break;
131 			}
132 		}
133 		if (DEBUG) {
134 			System.out.println("size = " + size);
135 		}
136 
137 		if ((x == null || x.length != size) ||
138 			(y == null || y.length != size) ||
139 			(z == null || z.length != size) ||
140 			(color != null && color.length != size) ||
141 			(normal != null && normal.length != size) ||
142 			(tCoord != null && tCoord.length != size)
143 			) {
144 			if (DEBUG) {
145 				System.out.println("size false");
146 			}
147 			return false;
148 		}
149 		return true;
150 	}
151 
152 	protected boolean needsModel() {
153 		double[] x = (double[]) this.entryArray[XC];
154 		double[] color = (double[]) this.entryArray[CC];
155 		double[] normal = (double[]) this.entryArray[NC];
156 		double[] tcoord = (double[]) this.entryArray[TC];				
157 		int[] stripCounts = (int[]) this.entryArray[STRIPC];
158 		int[] contourCounts = (int[]) this.entryArray[CONC];
159 
160 		if (DEBUG) {
161 			System.out.println("x = " + x);
162 			System.out.println("s = " + stripCounts);
163 			System.out.println("c = " + contourCounts);		
164 			System.out.println("x = " + this.x);
165 			System.out.println("s = " + this.stripCounts);
166 			System.out.println("c = " + this.contourCounts);
167 		}
168 		if ((this.x == null) || (x.length != this.x.length) ||
169 			(this.stripCounts == null) || (this.contourCounts == null) ||
170 			(stripCounts.length != this.stripCounts.length) ||
171 			(contourCounts.length != this.contourCounts.length) ||
172 			(this.color == null && color != null) ||
173 			(this.normal == null && normal != null) ||
174 			(this.tCoords == null && tcoord != null)) {
175 			return true;
176 		}
177 		for (int i = 0; i < this.stripCounts.length; i++) {
178 			if (this.stripCounts[i] != stripCounts[i]) {
179 				return true;
180 			}
181 		}
182 		for (int i = 0; i < this.contourCounts.length; i++) {
183 			if (this.contourCounts[i] != contourCounts[i]) {
184 				return true;
185 			}
186 		}
187 		return false;
188 	}
189 
190 	private int computeTotalNumberOfVertices(int[] stripCounts) {
191 		int num = 0;
192 		for (int i = 0; i < stripCounts.length; i++) {
193 			num += stripCounts[i];
194 		}
195 		return num;
196 	}
197 	
198 	protected synchronized void setData() {
199 		double[] x = (double[]) this.entryArray[XC];
200 		double[] y = (double[]) this.entryArray[YC];
201 		double[] z = (double[]) this.entryArray[ZC];
202 		double[] color = (double[]) this.entryArray[CC];
203 		double[][] normal = (double[][]) this.entryArray[NC];
204 		double[][] tCoord = (double[][]) this.entryArray[TC];				
205 		int[] stripCounts = (int[]) this.entryArray[STRIPC];
206 		int[] contourCounts = (int[]) this.entryArray[CONC];
207 
208 		super.setXCoordinates(x);
209 		super.setYCoordinates(y);
210 		super.setZCoordinates(z);
211 		super.setStripCounts(stripCounts);
212 		super.setContourCounts(contourCounts);
213 		
214 		if (color != null) {
215 			super.setColors(color);
216 		}
217 		
218 		if (normal != null) {
219 			super.setNormals(normal);
220 		}
221 		
222 		if (tCoord != null) {
223 			super.setTextureCoords(tCoord);
224 		}
225 	}
226 
227 	public void test() {
228 		double[] x = {0.0, 3.0, 2.0, 0.0, 1.0, 2.0, 2.0, 4.0, 7.0, 7.0, 4.0, 3.0, 5.0, 6.0, 6.0, 5.0};
229 		double[] y = {0.0, 0.0, 3.0, 2.0, 1.0, 1.0, 2.0, 1.0, 1.0, 5.0, 5.0, 4.0, 2.0, 3.0, 4.0, 4.0};
230 		double[] z = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
231 		double[] color = {0.0, 0.2, 0.4, 0.6, 0.8, 0.8, 0.8, 0.0, 0.2, 0.4, 0.6, 0.8, 0.8, 0.8, 1.0, 1.0};
232 		int[] stripCount = {4, 3, 5, 4};
233 		int[] contourCount = {2, 2};
234 
235 		setXCoordinates(x);
236 		setYCoordinates(y);
237 		setZCoordinates(z);
238 		setColors(color);
239 		setContourCounts(contourCount);
240 		setStripCounts(stripCount);
241 
242 	}
243 }
244