Skip to content

Commit dcf5a44

Browse files
committed
Adding the normal information into the polygon on STL loading
1 parent 12975f3 commit dcf5a44

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/main/java/eu/mihosoft/vrl/v3d/STL.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,14 @@ public static CSG file(Path path) throws IOException, InvalidNormalException, To
9292
STLLoader loader = new STLLoader();
9393

9494
List<Polygon> polygons = new ArrayList<>();
95-
List<Vector3d> vertices = new ArrayList<>();
96-
for (Vector3d p : loader.parse(path.toFile())) {
95+
List<Vertex> vertices = new ArrayList<>();
96+
for (Vertex p : loader.parse(path.toFile())) {
9797
vertices.add(p);
9898
if (vertices.size() == 3) {
9999
try {
100-
polygons.add(Polygon.fromPointsAllowDegenerate(vertices));
100+
Plane createFromPoints = Plane.createFromPoints(vertices);
101+
createFromPoints.setNormal(vertices.get(0).normal);
102+
polygons.add(new Polygon(vertices, new PropertyStorage(), createFromPoints));
101103
} catch (RuntimeException ex) {
102104
//ex.printStackTrace();
103105
System.err.println("Pruning polygon loading STL::file");

src/main/java/eu/mihosoft/vrl/v3d/ext/imagej/STLLoader.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.ArrayList;
2020

2121
import eu.mihosoft.vrl.v3d.Vector3d;
22+
import eu.mihosoft.vrl.v3d.Vertex;
2223

2324
;
2425

@@ -60,10 +61,10 @@ public STLLoader() {
6061

6162
/** The vertices. */
6263
// attributes of the currently read mesh
63-
private ArrayList<Vector3d> vertices = new ArrayList<>();
64+
private ArrayList<Vertex> vertices = new ArrayList<>();
6465

6566
/** The normal. */
66-
private Vector3d normal = new Vector3d(0.0f, 0.0f, 0.0f); // to be used for file checking
67+
//private Vector3d normal = new Vector3d(0.0f, 0.0f, 0.0f); // to be used for file checking
6768

6869
/** The fis. */
6970
private FileInputStream fis;
@@ -79,7 +80,7 @@ public STLLoader() {
7980
* @return the array list
8081
* @throws IOException Signals that an I/O exception has occurred.
8182
*/
82-
public ArrayList<Vector3d> parse(File f) throws IOException {
83+
public ArrayList<Vertex> parse(File f) throws IOException {
8384
vertices.clear();
8485

8586
// determine if this is a binary or ASCII STL
@@ -132,14 +133,15 @@ private void parseAscii(File f) {
132133
}
133134
vertices = new ArrayList<>();
134135
try {
136+
Vector3d normal = new Vector3d(0, 0,0);
135137
while ((line = in.readLine()) != null) {
136138
String[] numbers = line.trim().split("\\s+");
137139
if (numbers[0].equals("vertex")) {
138140
double x = parseDouble(numbers[1]);
139141
double y = parseDouble(numbers[2]);
140142
double z = parseDouble(numbers[3]);
141143
Vector3d vertex = new Vector3d(x, y, z);
142-
vertices.add(vertex);
144+
vertices.add(new Vertex(vertex, normal));
143145
} else if (numbers[0].equals("facet") && numbers[1].equals("normal")) {
144146
normal.x = parseDouble(numbers[2]);
145147
normal.y = parseDouble(numbers[3]);
@@ -160,7 +162,7 @@ private void parseAscii(File f) {
160162
* @param f the f
161163
*/
162164
private void parseBinary(File f) {
163-
vertices = new ArrayList<Vector3d>();
165+
vertices = new ArrayList<Vertex>();
164166
try {
165167
fis = new FileInputStream(f);
166168
for (int h = 0; h < 84; h++) {
@@ -171,6 +173,7 @@ private void parseBinary(File f) {
171173
for (int tb = 0; tb < 50; tb++) {
172174
tri[tb] = (byte) fis.read();
173175
}
176+
Vector3d normal = new Vector3d(0, 0,0);
174177
normal.x = leBytesToFloat(tri[0], tri[1], tri[2], tri[3]);
175178
normal.y = leBytesToFloat(tri[4], tri[5], tri[6], tri[7]);
176179
normal.z = leBytesToFloat(tri[8], tri[9], tri[10], tri[11]);
@@ -180,7 +183,7 @@ private void parseBinary(File f) {
180183
double py = leBytesToFloat(tri[j + 4], tri[j + 5], tri[j + 6], tri[j + 7]);
181184
double pz = leBytesToFloat(tri[j + 8], tri[j + 9], tri[j + 10], tri[j + 11]);
182185
Vector3d p = new Vector3d(px, py, pz);
183-
vertices.add(p);
186+
vertices.add(new Vertex(p, normal));
184187
}
185188
}
186189
fis.close();

0 commit comments

Comments
 (0)