Normand Briere
2019-09-04 c8bb659043bfa0ccf9436d7cbbc49255b4c82402
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//import org.j3d.renderer.aviatrix3d.loader.*;
import org.j3d.aviatrix3d.*;
import javax.vecmath.*;
import java.awt.Color;
import java.awt.color.ColorSpace;
 
class cAviatrix
{
   static Matrix4d tempmat = new Matrix4d();
   
   static Object3D Read(Node s)
   {
       if (s == null)
           return new Object3D("NULL");
       
       if (s instanceof Group)
           return Read((Group)s);
       if (s instanceof SharedGroup)
           return Read(((SharedGroup)s).getChild(0));
       if (s instanceof Shape3D)
           return Read((Shape3D)s);
       if (s instanceof SharedNode)
           return Read(((SharedNode)s).getChild());
       
       System.out.println("Unknown node class : " + s.getClass().getName());
       //new Exception().printStackTrace();
       
       return new Object3D(s.getClass().getName());
   }
   
   static Object3D Read(Group g)
   {
       //if (g instanceof SharedGroup)
           //return Read(((SharedGroup)g).getChild(0));
       
       Composite c = new Composite(g.getClass().getName());
       
       if (g instanceof TransformGroup)
       {
           ((TransformGroup)g).getTransform(tempmat);
                        if (c.toParent == null)
                        {
                            c.toParent = LA.newMatrix();
                            c.fromParent = LA.newMatrix();
                        }
           FillMatrix(tempmat, c.toParent);
           LA.matInvert(c.toParent, c.fromParent);
       }
       
       for (int i=0; i<g.numChildren(); i++)
       {
           c.addChild(Read(g.getChild(i)));
       }
       
       return c;
   }
   
   static float[] buffer = new float[4];
   
   static cMaterial Read(Material mat)
   {
       cMaterial cmat = new cMaterial();
 
       mat.getDiffuseColor(buffer);
       //Color col = new Color(buffer[0], buffer[1], buffer[2]);
       //col.getColorComponents(ColorSpace.getInstance(ColorSpace.TYPE_HSV), buffer);
       cColor.RGBtoHSB(buffer[0], buffer[1], buffer[2], buffer);
       
       cmat.color = buffer[0]+0.001f;
       cmat.modulation = buffer[1]+0.001f;
       cmat.diffuse = buffer[2]+0.001f;
       
       mat.getSpecularColor(buffer);
       
       cmat.specular = buffer[0]+0.001f;
       // No way to get the metalness
       
       mat.getAmbientColor(buffer);
       
       cmat.ambient = buffer[0]+0.001f;
       
       cmat.lightarea = 1/(mat.getShininess()+0.001f);
       
       return cmat;
   }
   
   static BoundaryRep Read(org.j3d.aviatrix3d.Geometry g)
   {
       BoundaryRep result = null;
       
       Vertex.normalmode = true;
       
       if (g instanceof TriangleStripArray)
           result = Read((TriangleStripArray)g);
       //else if (g instanceof TriangleFanArray)
           //return Read((TriangleFanArray)g);
       else if (g instanceof TriangleArray)
           result = Read((TriangleArray)g);
       else if (g != null)
           System.out.println("geometry class = " + g.getClass().getName());
       //assert false;
       
       Vertex.normalmode = false;
       
       return result;
   }
   
   static BoundaryRep Read(TriangleStripArray tsa)
   {
   //System.out.println("TriangleStripArray");
       BoundaryRep bRep = new BoundaryRep(tsa.getValidVertexCount(),
                                          tsa.getValidVertexCount() -
                                          tsa.getValidStripCount()*2);
       
       boolean cache = true;
       
       if (!cache)
           bRep.Trim();
       
       float[] vertices = bRep.getRawVertices(); // new float[tsa.getValidVertexCount()*3];
       tsa.getVertices(vertices);
       float[] normals = bRep.getRawNormals(); // new float[tsa.getValidVertexCount()*3];
       tsa.getNormals(normals);
       float[] uvmap = bRep.getRawUVMap(); // new float[tsa.getValidVertexCount()*2];
 
       float[][] textures = new float[1][];
       textures[0] = uvmap;
 
       try
       {
           tsa.getTextureCoordinates(textures);
       }
       catch (Exception e)
       {
           System.out.println("MULTI-TEXTURE ???");
       }
           
       if (cache)
       {
           // Vertices    
           int i3 = 0, i2 = 0;
           for (int i=0; i<tsa.getValidVertexCount(); i++, i3+=3, i2+=2)
           {
               //Vertex v = new Vertex(true);
               v./*pos.*/set(vertices[i3], vertices[i3+1], vertices[i3+2]);
               v.norm.set(normals[i3], normals[i3+1], normals[i3+2]);
               //v.s = textures[0][i2];
               //v.t = textures[0][i2+1];
               v.s = uvmap[i2];
               v.t = uvmap[i2+1];
               Vertex in = bRep.GetCache(v);
               if (true) // in == null)
               {
                   v.index = -1;
                   Vertex out = new Vertex(v);
                   //bRep.AddVertex(out);
                   bRep.Remember(out);
               }
               else
               {
                   bRep.AddVertex(in);
               }
           }
       }
       
       // Faces
       int[] counts = new int[tsa.getValidStripCount()];
       tsa.getStripCount(counts);
       
       int indexF = 0;
       int indexV = 0;
       
       for (int i=0; i<tsa.getValidStripCount(); i++)
       {
           int count = counts[i];
           
           assert count >= 3;
           
           bRep.AddFace(indexV, indexV+1, indexV+2);
           indexF++;
           //bRep.faces.addElement(null);
           //bRep.setFace(indexF++, indexV, indexV+1, indexV+2);
           indexV += 3;
           
           for (int j=3; j<count; j++, indexV++)
           {
               //bRep.faces.addElement(null);
               if ((indexF % 2) == 0)
                   bRep.AddFace(indexV-2, indexV-1, indexV);
               else
                   bRep.AddFace(indexV-2, indexV, indexV-1);
 
               indexF++;
           }
       }
 
       assert indexF == tsa.getValidVertexCount() - tsa.getValidStripCount()*2;
       assert indexV == tsa.getValidVertexCount();
       
       if (cache)
           bRep.Trim(cJME.trim, cJME.gennormals, false, cJME.stripify, cJME.genUV);
       
       return bRep;
   }
 
   static BoundaryRep Read(TriangleFanArray tsa)
   {
       BoundaryRep bRep = new BoundaryRep();
 
       return bRep;
   }
 
   static Vertex v = new Vertex(true);
   
   static BoundaryRep Read(TriangleArray tsa)
   {
   //System.out.println("TriangleArray");
       int faces = tsa.getValidVertexCount()/3;
       
       BoundaryRep bRep = new BoundaryRep(tsa.getValidVertexCount(),
                                          faces);
       
       boolean cache = true;
       
       if (!cache)
           bRep.Trim();
       
       float[] vertices = bRep.getRawVertices(); // new float[tsa.getValidVertexCount()*3];
       float[] normals = bRep.getRawNormals(); // new float[tsa.getValidVertexCount()*3];
       float[] uvmap = bRep.getRawUVMap(); // new float[tsa.getValidVertexCount()*2];
 
       assert (vertices != null);
       assert (normals != null);
       try
       {
           tsa.getVertices(vertices);
           tsa.getNormals(normals);
 
           float[][] textures = new float[1][];
           textures[0] = uvmap;
 
           tsa.getTextureCoordinates(textures);
       }
       catch (Exception e)
       {
           System.out.println("NO VERTEX!");
       }
 
       if (cache)
       {
           // Vertices        
           int i3 = 0;
           int i2 = 0;
           for (int i=0; i<tsa.getValidVertexCount(); i++, i3+=3, i2+=2)
           {
               //Vertex v = new Vertex();
               v./*pos.*/set(vertices[i3], vertices[i3+1], vertices[i3+2]);
               v.norm.set(normals[i3], normals[i3+1], normals[i3+2]);
               v.s = uvmap[i2];
               v.t = uvmap[i2+1];
               Vertex in = bRep.GetCache(v);
               if (in == null)
               {
                   v.index = -1;
                   bRep.Remember(new Vertex(v));
               }
               else
               {
                   bRep.AddVertex(in);
               }
           }
       }
       
       // Faces
       int indexF = 0;
       int indexV = 0;
       
       for (int i=0; i<faces; i++)
       {
           //bRep.faces.addElement(null);
           bRep.AddFace(indexV, indexV+1, indexV+2);
           indexF++;
           
           indexV += 3;
       }
 
       assert indexF == faces;
       assert indexV == tsa.getValidVertexCount();
       
       if (cache)
           bRep.Trim(cJME.trim, cJME.gennormals, false, cJME.stripify, cJME.genUV);
 
       return bRep;
   }
 
   static Object3D Read(Shape3D s)
   {
       Object3D o = new Object3D(s.getClass().getName());
       
       Material mat = s.getAppearance().getMaterial();
       
       if (mat != null)
           o.material = Read(mat);
       
       //System.out.println("Geometry = " + s.getGeometry());
       
       o.bRep = Read(s.getGeometry());
       
       return o;
   }
       
   static void FillMatrix(Matrix4d in, double[][] out)
   {
       for (int j=0; j<4; j++)
           for (int i=0; i<4; i++)
               out[i][j] = in.getElement(j,i);
   }
}