Skip to content

Commit

Permalink
Fix the incorrectly computed normal
Browse files Browse the repository at this point in the history
The normal should take into account all of the points, not just the
first 3. the change allows for polygons whs first 3 points may in fact
be in a line producing an invalid normal
  • Loading branch information
madhephaestus committed Oct 14, 2024
1 parent 28a8055 commit a10a30b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/main/java/eu/mihosoft/vrl/v3d/Polygon.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,8 @@ public Polygon transform(Transform transform) {
);

Vector3d a = this.vertices.get(0).pos;
Vector3d b = this.vertices.get(1).pos;
Vector3d c = this.vertices.get(2).pos;

this.plane.normal = b.minus(a).cross(c.minus(a)).normalized();
this.plane.normal = computeNormal();
this.plane.dist = this.plane.normal.dot(a);

if (transform.isMirror()) {
Expand All @@ -361,7 +359,21 @@ public Polygon transform(Transform transform) {
}
return this;
}
public Vector3d computeNormal() {
Vector3d normal = new Vector3d(0, 0, 0);
int n = this.vertices.size();

for (int i = 0; i < n; i++) {
Vector3d current = this.vertices.get(i).pos;
Vector3d next = this.vertices.get((i + 1) % n).pos;

normal.x += (current.y - next.y) * (current.z + next.z);
normal.y += (current.z - next.z) * (current.x + next.x);
normal.z += (current.x - next.x) * (current.y + next.y);
}

return normal.normalized();
}
/**
* Returns a transformed copy of this polygon.
*
Expand Down
12 changes: 11 additions & 1 deletion src/test/java/eu/mihosoft/vrl/v3d/SVGLoadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@
import eu.mihosoft.vrl.v3d.svg.SVGLoad;

public class SVGLoadTest {

@Test
public void adversarial() throws IOException {
File svg = new File("Part-Num-0.svg");
if(!svg.exists())
throw new RuntimeException("Test file missing!"+svg.getAbsolutePath());
SVGLoad s = new SVGLoad(svg.toURI());
run(s);
//fail("Not yet implemented");
}
@Test
public void test() throws IOException {
File svg = new File("Test.SVG");
Expand Down Expand Up @@ -45,5 +53,7 @@ private ArrayList<Object> run(SVGLoad s) {

return polys;
}



}

0 comments on commit a10a30b

Please sign in to comment.