@@ -10,6 +10,8 @@ public class CSGtoJavafx {
1010 public static MeshContainer meshFromPolygon (Polygon ... poly ) {
1111 return meshFromPolygon (Arrays .asList (poly ));
1212 }
13+
14+ // Uses fan triangulation, works for convex polygons only!
1315 public static MeshContainer meshFromPolygon (List <Polygon > poly ) {
1416 TriangleMesh mesh = new TriangleMesh ();
1517
@@ -21,112 +23,51 @@ public static MeshContainer meshFromPolygon(List<Polygon> poly) {
2123 double maxY = Double .NEGATIVE_INFINITY ;
2224 double maxZ = Double .NEGATIVE_INFINITY ;
2325
24- int counter = 0 ;
26+ // One texture pair per mesh (not used currently)
27+ mesh .getTexCoords ().addAll (0 , 0 );
28+
29+ int vertexOffset = 0 ;
30+
31+ // Process a polygon, triangulate if needed
2532 for (int j = 0 ; j < poly .size (); j ++) {
33+
2634 Polygon p = poly .get (j );
2735 if (p .getVertices ().size () >= 3 ) {
2836
29- // TODO: improve the triangulation?
30- //
31- // JavaOne requires triangular polygons.
32- // If our polygon has more vertices, create
33- // multiple triangles:
34- Vertex firstVertex = p .getVertices ().get (0 );
35- for (int i = 0 ; i < p .getVertices ().size () - 2 ; i ++) {
37+ // Add all polygon vertices to the mesh, and get the bounds min and max
38+ for (Vertex v : p .getVertices ()) {
39+ mesh .getPoints ().addAll ((float )v .pos .x , (float )v .pos .y , (float )v .pos .z );
40+
41+ if (v .pos .x < minX )
42+ minX = v .pos .x ;
3643
37- if (firstVertex .pos .x < minX ) {
38- minX = firstVertex .pos .x ;
39- }
40- if (firstVertex .pos .y < minY ) {
41- minY = firstVertex .pos .y ;
42- }
43- if (firstVertex .pos .z < minZ ) {
44- minZ = firstVertex .pos .z ;
45- }
46-
47- if (firstVertex .pos .x > maxX ) {
48- maxX = firstVertex .pos .x ;
49- }
50- if (firstVertex .pos .y > maxY ) {
51- maxY = firstVertex .pos .y ;
52- }
53- if (firstVertex .pos .z > maxZ ) {
54- maxZ = firstVertex .pos .z ;
55- }
56-
57- mesh .getPoints ().addAll ((float ) firstVertex .pos .x , (float ) firstVertex .pos .y ,
58- (float ) firstVertex .pos .z );
59-
60- mesh .getTexCoords ().addAll (0 ); // texture (not covered)
61- mesh .getTexCoords ().addAll (0 );
62-
63- Vertex secondVertex = p .getVertices ().get (i + 1 );
64-
65- if (secondVertex .pos .x < minX ) {
66- minX = secondVertex .pos .x ;
67- }
68- if (secondVertex .pos .y < minY ) {
69- minY = secondVertex .pos .y ;
70- }
71- if (secondVertex .pos .z < minZ ) {
72- minZ = secondVertex .pos .z ;
73- }
74-
75- if (secondVertex .pos .x > maxX ) {
76- maxX = firstVertex .pos .x ;
77- }
78- if (secondVertex .pos .y > maxY ) {
79- maxY = firstVertex .pos .y ;
80- }
81- if (secondVertex .pos .z > maxZ ) {
82- maxZ = firstVertex .pos .z ;
83- }
84-
85- mesh .getPoints ().addAll ((float ) secondVertex .pos .x , (float ) secondVertex .pos .y ,
86- (float ) secondVertex .pos .z );
87-
88- mesh .getTexCoords ().addAll (0 ); // texture (not covered)
89- mesh .getTexCoords ().addAll (0 );
90-
91- Vertex thirdVertex = p .getVertices ().get (i + 2 );
92-
93- mesh .getPoints ().addAll ((float ) thirdVertex .pos .x , (float ) thirdVertex .pos .y ,
94- (float ) thirdVertex .pos .z );
95-
96- if (thirdVertex .pos .x < minX ) {
97- minX = thirdVertex .pos .x ;
98- }
99- if (thirdVertex .pos .y < minY ) {
100- minY = thirdVertex .pos .y ;
101- }
102- if (thirdVertex .pos .z < minZ ) {
103- minZ = thirdVertex .pos .z ;
104- }
105-
106- if (thirdVertex .pos .x > maxX ) {
107- maxX = firstVertex .pos .x ;
108- }
109- if (thirdVertex .pos .y > maxY ) {
110- maxY = firstVertex .pos .y ;
111- }
112- if (thirdVertex .pos .z > maxZ ) {
113- maxZ = firstVertex .pos .z ;
114- }
115-
116- mesh .getTexCoords ().addAll (0 ); // texture (not covered)
117- mesh .getTexCoords ().addAll (0 );
118-
119- mesh .getFaces ().addAll (counter , // first vertex
120- 0 , // texture (not covered)
121- counter + 1 , // second vertex
122- 0 , // texture (not covered)
123- counter + 2 , // third vertex
124- 0 // texture (not covered)
125- );
126- counter += 3 ;
44+ if (v .pos .y < minY )
45+ minY = v .pos .y ;
46+
47+ if (v .pos .z < minZ )
48+ minZ = v .pos .z ;
49+
50+ if (v .pos .x > maxX )
51+ maxX = v .pos .x ;
52+
53+ if (v .pos .y > maxY )
54+ maxY = v .pos .y ;
55+
56+ if (v .pos .z > maxZ )
57+ maxZ = v .pos .z ;
12758 } // end for
59+
60+ // Add the vertex indexes (0, 1, 2) (0, 2, 3) (0, 3, 4) etc.
61+ for (int i = 0 ; i < p .getVertices ().size () - 2 ; i ++) {
62+ mesh .getFaces ().addAll (
63+ vertexOffset + 0 , 0 , // always first vertex of polygon
64+ vertexOffset + i + 1 , 0 , // second vertex
65+ vertexOffset + i + 2 , 0 ); // third vertex
66+ }
67+ vertexOffset += p .getVertices ().size ();
68+
12869 } // end if #verts >= 3
129- }
70+ } // end for
13071
13172 return new MeshContainer (new Vector3d (minX , minY , minZ ), new Vector3d (maxX , maxY , maxZ ), mesh );
13273 }
0 commit comments