Skip to content

Commit 62b1940

Browse files
committed
Read the Normal from the STL file
1 parent e658c92 commit 62b1940

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ public Plane(Vector3d normal, double dist) {
8888
this.setNormal(normal.normalized());
8989
this.setDist(dist);
9090
}
91+
/**
92+
* Constructor. Creates a new plane defined by its normal vector and the
93+
* distance to the origin.
94+
*
95+
* @param normal plane normal
96+
* @param dist distance from origin
97+
*/
98+
public Plane(Vector3d normal, List<Vertex> vertices) {
99+
this.setNormal(normal.normalized());
100+
this.setDist(normal.dot(vertices.get(0).pos));
101+
}
91102

92103
/**
93104
* Creates a plane defined by the the specified points.

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,14 @@ public static CSG file(Path path) throws IOException {
8484
STLLoader loader = new STLLoader();
8585

8686
List<Polygon> polygons = new ArrayList<>();
87-
List<Vector3d> vertices = new ArrayList<>();
88-
for (Vector3d p : loader.parse(path.toFile())) {
87+
List<Vertex> vertices = new ArrayList<>();
88+
for (Vertex p : loader.parse(path.toFile())) {
8989
vertices.add(p);
9090
if (vertices.size() == 3) {
9191
try {
92-
polygons.add(Polygon.fromPointsAllowDegenerate(vertices));
92+
93+
Plane pl = new Plane(vertices.get(0).normal, vertices);
94+
polygons.add(new Polygon(vertices, null, false, pl));
9395
} catch (RuntimeException ex) {
9496
//ex.printStackTrace();
9597
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)