JavaCAD
CSGtoJavafx.java
Go to the documentation of this file.
1 package eu.mihosoft.vrl.v3d;
2 
3 import java.util.Arrays;
4 import java.util.List;
5 
6 import javafx.scene.shape.TriangleMesh;
7 
8 public class CSGtoJavafx {
9 
10  public static MeshContainer meshFromPolygon(Polygon... poly) {
11  return meshFromPolygon(Arrays.asList(poly));
12  }
13  public static MeshContainer meshFromPolygon(List<Polygon> poly) {
14  TriangleMesh mesh = new TriangleMesh();
15 
16  double minX = Double.POSITIVE_INFINITY;
17  double minY = Double.POSITIVE_INFINITY;
18  double minZ = Double.POSITIVE_INFINITY;
19 
20  double maxX = Double.NEGATIVE_INFINITY;
21  double maxY = Double.NEGATIVE_INFINITY;
22  double maxZ = Double.NEGATIVE_INFINITY;
23 
24  int counter = 0;
25  for (Polygon p : poly) {
26  if (p.vertices.size() >= 3) {
27 
28  // TODO: improve the triangulation?
29  //
30  // JavaOne requires triangular polygons.
31  // If our polygon has more vertices, create
32  // multiple triangles:
33  Vertex firstVertex = p.vertices.get(0);
34  for (int i = 0; i < p.vertices.size() - 2; i++) {
35 
36  if (firstVertex.pos.x < minX) {
37  minX = firstVertex.pos.x;
38  }
39  if (firstVertex.pos.y < minY) {
40  minY = firstVertex.pos.y;
41  }
42  if (firstVertex.pos.z < minZ) {
43  minZ = firstVertex.pos.z;
44  }
45 
46  if (firstVertex.pos.x > maxX) {
47  maxX = firstVertex.pos.x;
48  }
49  if (firstVertex.pos.y > maxY) {
50  maxY = firstVertex.pos.y;
51  }
52  if (firstVertex.pos.z > maxZ) {
53  maxZ = firstVertex.pos.z;
54  }
55 
56  mesh.getPoints().addAll((float) firstVertex.pos.x, (float) firstVertex.pos.y,
57  (float) firstVertex.pos.z);
58 
59  mesh.getTexCoords().addAll(0); // texture (not covered)
60  mesh.getTexCoords().addAll(0);
61 
62  Vertex secondVertex = p.vertices.get(i + 1);
63 
64  if (secondVertex.pos.x < minX) {
65  minX = secondVertex.pos.x;
66  }
67  if (secondVertex.pos.y < minY) {
68  minY = secondVertex.pos.y;
69  }
70  if (secondVertex.pos.z < minZ) {
71  minZ = secondVertex.pos.z;
72  }
73 
74  if (secondVertex.pos.x > maxX) {
75  maxX = firstVertex.pos.x;
76  }
77  if (secondVertex.pos.y > maxY) {
78  maxY = firstVertex.pos.y;
79  }
80  if (secondVertex.pos.z > maxZ) {
81  maxZ = firstVertex.pos.z;
82  }
83 
84  mesh.getPoints().addAll((float) secondVertex.pos.x, (float) secondVertex.pos.y,
85  (float) secondVertex.pos.z);
86 
87  mesh.getTexCoords().addAll(0); // texture (not covered)
88  mesh.getTexCoords().addAll(0);
89 
90  Vertex thirdVertex = p.vertices.get(i + 2);
91 
92  mesh.getPoints().addAll((float) thirdVertex.pos.x, (float) thirdVertex.pos.y,
93  (float) thirdVertex.pos.z);
94 
95  if (thirdVertex.pos.x < minX) {
96  minX = thirdVertex.pos.x;
97  }
98  if (thirdVertex.pos.y < minY) {
99  minY = thirdVertex.pos.y;
100  }
101  if (thirdVertex.pos.z < minZ) {
102  minZ = thirdVertex.pos.z;
103  }
104 
105  if (thirdVertex.pos.x > maxX) {
106  maxX = firstVertex.pos.x;
107  }
108  if (thirdVertex.pos.y > maxY) {
109  maxY = firstVertex.pos.y;
110  }
111  if (thirdVertex.pos.z > maxZ) {
112  maxZ = firstVertex.pos.z;
113  }
114 
115  mesh.getTexCoords().addAll(0); // texture (not covered)
116  mesh.getTexCoords().addAll(0);
117 
118  mesh.getFaces().addAll(counter, // first vertex
119  0, // texture (not covered)
120  counter + 1, // second vertex
121  0, // texture (not covered)
122  counter + 2, // third vertex
123  0 // texture (not covered)
124  );
125  counter += 3;
126  } // end for
127  } // end if #verts >= 3
128 
129  } // end for polygon
130 
131  return new MeshContainer(new Vector3d(minX, minY, minZ), new Vector3d(maxX, maxY, maxZ), mesh);
132  }
133 
134 }
static MeshContainer meshFromPolygon(Polygon... poly)
static MeshContainer meshFromPolygon(List< Polygon > poly)
static Vector3d y(double y)
Definition: Vector3d.java:484
static Vector3d z(double z)
Definition: Vector3d.java:494
static Vector3d x(double x)
Definition: Vector3d.java:474