From 517dccd2844cdfbd32f44de1c286c2555225b5da Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Dominik=20R=C3=A1=C4=8Dek?= <xracek2@fi.muni.cz>
Date: Wed, 15 Dec 2021 12:37:47 +0100
Subject: [PATCH] Add unit tests for CrossSection

---
 .../analyst/visitors/mesh/CrossSection.java   |   5 +-
 .../visitors/mesh/CrossSectionZigZag.java     |  11 ++
 .../visitors/mesh/CrossSectionTest.java       | 116 ++++++++++++++++++
 .../analyst/symmetry/ProfilesAction.java      |   1 -
 .../analyst/mesh/core/MeshTriangle.java       |   9 +-
 .../analyst/mesh/core/MeshTriangleTest.java   |  97 ++++++++++++++-
 6 files changed, 230 insertions(+), 9 deletions(-)
 create mode 100644 Comparison/src/test/java/cz/fidentis/analyst/visitors/mesh/CrossSectionTest.java

diff --git a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/CrossSection.java b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/CrossSection.java
index aedee2ec..31359cc2 100644
--- a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/CrossSection.java
+++ b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/CrossSection.java
@@ -28,8 +28,9 @@ public class CrossSection extends MeshVisitor {
     private Set<MeshTriangle> visited;
 
     /**
-     *  Constructor
-     * @param plane the cutting plane
+     * Constructor for CrossSection visitor
+     *
+     * @param plane cutting plane
      */
     public CrossSection(Plane plane) {
         this.plane = plane;
diff --git a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/CrossSectionZigZag.java b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/CrossSectionZigZag.java
index c4f96c24..960b1d01 100644
--- a/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/CrossSectionZigZag.java
+++ b/Comparison/src/main/java/cz/fidentis/analyst/visitors/mesh/CrossSectionZigZag.java
@@ -28,6 +28,11 @@ public class CrossSectionZigZag extends MeshVisitor {
 
     private Plane plane;
 
+    /**
+     * Constructor for CrossSectionZigZag visitor
+     *
+     * @param plane cutting plane
+     */
     public CrossSectionZigZag(Plane plane) {
         this.plane = plane;
         this.points = new ArrayList<>();
@@ -118,24 +123,30 @@ public class CrossSectionZigZag extends MeshVisitor {
 
             if (intersection1 != null) {
                 //Intersection 1
+                addPoint(intersection1, true);
                 visitMeshFacetRec(facet, first.index1, first.index2, first, true);
                 if (intersection2 != null) {
                     //Intersection 1 and 2
+                    addPoint(intersection2, false);
                     visitMeshFacetRec(facet, first.index2, first.index3, first, false);
                 } else if (intersection3 != null) {
                     //Intersection 1 and 3
+                    addPoint(intersection3, false);
                     visitMeshFacetRec(facet, first.index3, first.index1, first, false);
                 }
             } else if (intersection2 != null) {
                 //Intersection 2
+                addPoint(intersection2, true);
                 visitMeshFacetRec(facet, first.index2, first.index3, first, true);
                 if (intersection3 != null) {
                     //Intersection 2 and 3
+                    addPoint(intersection3, false);
                     visitMeshFacetRec(facet, first.index3, first.index1, first, false);
                 }
 
             } else if (intersection3 != null) {
                 //Intersection 3 only
+                addPoint(intersection3, true);
                 visitMeshFacetRec(facet, first.index3, first.index1, first, true);
             }
             //No intersection
diff --git a/Comparison/src/test/java/cz/fidentis/analyst/visitors/mesh/CrossSectionTest.java b/Comparison/src/test/java/cz/fidentis/analyst/visitors/mesh/CrossSectionTest.java
new file mode 100644
index 00000000..5e496332
--- /dev/null
+++ b/Comparison/src/test/java/cz/fidentis/analyst/visitors/mesh/CrossSectionTest.java
@@ -0,0 +1,116 @@
+package cz.fidentis.analyst.visitors.mesh;
+import java.util.List;
+import javax.vecmath.Point3d;
+import javax.vecmath.Vector3d;
+
+import cz.fidentis.analyst.mesh.core.CornerTableRow;
+import cz.fidentis.analyst.mesh.core.MeshFacet;
+import cz.fidentis.analyst.mesh.core.MeshFacetImpl;
+import cz.fidentis.analyst.mesh.core.MeshModel;
+import cz.fidentis.analyst.mesh.core.MeshPointImpl;
+import cz.fidentis.analyst.symmetry.Plane;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * @author Dominik Racek
+ */
+public class CrossSectionTest {
+
+    private MeshModel createModel() {
+        MeshModel model = new MeshModel();
+
+        MeshFacet facet = new MeshFacetImpl();
+        facet.addVertex(new MeshPointImpl(new Point3d(0, 0, 0), null, null));
+        facet.addVertex(new MeshPointImpl(new Point3d(1, 0, 0), null, null));
+        facet.addVertex(new MeshPointImpl(new Point3d(1, 1, 0), null, null));
+        facet.addVertex(new MeshPointImpl(new Point3d(0, 1, 0), null, null));
+        facet.addVertex(new MeshPointImpl(new Point3d(1, 2, 0), null, null));
+        facet.addVertex(new MeshPointImpl(new Point3d(0, 2, 0), null, null));
+        facet.addVertex(new MeshPointImpl(new Point3d(1, 3, 0), null, null));
+
+        facet.getCornerTable().addRow(new CornerTableRow(0, -1));
+        facet.getCornerTable().addRow(new CornerTableRow(1, 5));
+        facet.getCornerTable().addRow(new CornerTableRow(2, -1));
+        facet.getCornerTable().addRow(new CornerTableRow(0, 8));
+        facet.getCornerTable().addRow(new CornerTableRow(2, -1));
+        facet.getCornerTable().addRow(new CornerTableRow(3, 1));
+        facet.getCornerTable().addRow(new CornerTableRow(3, -1));
+        facet.getCornerTable().addRow(new CornerTableRow(2, 11));
+        facet.getCornerTable().addRow(new CornerTableRow(4, 3));
+        facet.getCornerTable().addRow(new CornerTableRow(3, 13));
+        facet.getCornerTable().addRow(new CornerTableRow(4, -1));
+        facet.getCornerTable().addRow(new CornerTableRow(5, 7));
+        facet.getCornerTable().addRow(new CornerTableRow(4, -1));
+        facet.getCornerTable().addRow(new CornerTableRow(6, 9));
+        facet.getCornerTable().addRow(new CornerTableRow(5, -1));
+
+        model.addFacet(facet);
+        return model;
+    }
+
+    /**
+     * Unit test for CrossSectionZigZag visitor
+     */
+    @Test
+    public void CrossSectionZigZag() {
+        Plane cuttingPlane = new Plane(new Vector3d(1, 0, 0), -0.5);
+        MeshModel model = createModel();
+
+        CrossSectionZigZag cs = new CrossSectionZigZag(cuttingPlane);
+        model.compute(cs);
+        List<Point3d> points = cs.getPoints();
+
+        //They can be ordered two ways, check both
+        Assertions.assertEquals(points.size(), 6);
+        if (points.get(0).equals(new Point3d(0.5, 0, 0))) {
+            Assertions.assertEquals(points.get(0), new Point3d(0.5, 0, 0));
+            Assertions.assertEquals(points.get(1), new Point3d(0.5, 0.5, 0));
+            Assertions.assertEquals(points.get(2), new Point3d(0.5, 1, 0));
+            Assertions.assertEquals(points.get(3), new Point3d(0.5, 1.5, 0));
+            Assertions.assertEquals(points.get(4), new Point3d(0.5, 2, 0));
+            Assertions.assertEquals(points.get(5), new Point3d(0.5, 2.5, 0));
+        } else if (points.get(0).equals(new Point3d(0.5, 2.5, 0))) {
+            Assertions.assertEquals(points.get(0), new Point3d(0.5, 2.5, 0));
+            Assertions.assertEquals(points.get(1), new Point3d(0.5, 2, 0));
+            Assertions.assertEquals(points.get(2), new Point3d(0.5, 1.5, 0));
+            Assertions.assertEquals(points.get(3), new Point3d(0.5, 1, 0));
+            Assertions.assertEquals(points.get(4), new Point3d(0.5, 0.5, 0));
+            Assertions.assertEquals(points.get(5), new Point3d(0.5, 0, 0));
+        } else {
+            Assertions.fail();
+        }
+    }
+
+    /**
+     * Unit test for CrossSection visitor
+     */
+    @Test
+    public void CrossSection() {
+        Plane cuttingPlane = new Plane(new Vector3d(1, 0, 0), -0.5);
+        MeshModel model = createModel();
+
+        CrossSection cs = new CrossSection(cuttingPlane);
+        model.compute(cs);
+        List<Point3d> points = cs.getPoints();
+
+        //They can be ordered two ways, check both
+        //Flaw of the non-zigzag design: it can't detect first and last intersection,
+        //since it only controls edges between two triangles.
+        Assertions.assertEquals(points.size(), 4);
+        if (points.get(0).equals(new Point3d(0.5, 0.5, 0))) {
+            Assertions.assertEquals(points.get(0), new Point3d(0.5, 0.5, 0));
+            Assertions.assertEquals(points.get(1), new Point3d(0.5, 1, 0));
+            Assertions.assertEquals(points.get(2), new Point3d(0.5, 1.5, 0));
+            Assertions.assertEquals(points.get(3), new Point3d(0.5, 2, 0));
+        } else if (points.get(0).equals(new Point3d(0.5, 2, 0))) {
+            Assertions.assertEquals(points.get(1), new Point3d(0.5, 2, 0));
+            Assertions.assertEquals(points.get(2), new Point3d(0.5, 1.5, 0));
+            Assertions.assertEquals(points.get(3), new Point3d(0.5, 1, 0));
+            Assertions.assertEquals(points.get(4), new Point3d(0.5, 0.5, 0));
+        } else {
+            Assertions.fail();
+        }
+    }
+
+}
diff --git a/GUI/src/main/java/cz/fidentis/analyst/symmetry/ProfilesAction.java b/GUI/src/main/java/cz/fidentis/analyst/symmetry/ProfilesAction.java
index 67463fc3..147f60af 100644
--- a/GUI/src/main/java/cz/fidentis/analyst/symmetry/ProfilesAction.java
+++ b/GUI/src/main/java/cz/fidentis/analyst/symmetry/ProfilesAction.java
@@ -2,7 +2,6 @@ package cz.fidentis.analyst.symmetry;
 
 import cz.fidentis.analyst.canvas.Canvas;
 import cz.fidentis.analyst.core.ControlPanelAction;
-import cz.fidentis.analyst.visitors.mesh.CrossSection;
 import cz.fidentis.analyst.visitors.mesh.CrossSectionZigZag;
 import org.openide.filesystems.FileChooserBuilder;
 
diff --git a/MeshModel/src/main/java/cz/fidentis/analyst/mesh/core/MeshTriangle.java b/MeshModel/src/main/java/cz/fidentis/analyst/mesh/core/MeshTriangle.java
index 86badda1..803940f1 100644
--- a/MeshModel/src/main/java/cz/fidentis/analyst/mesh/core/MeshTriangle.java
+++ b/MeshModel/src/main/java/cz/fidentis/analyst/mesh/core/MeshTriangle.java
@@ -73,13 +73,14 @@ public class MeshTriangle implements Iterable<MeshPoint> {
     }
 
     @Override
-    public boolean equals(Object obj)
-    {
-        if(this == obj)
+    public boolean equals(Object obj) {
+        if(this == obj) {
             return true;
+        }
 
-        if(obj == null || obj.getClass()!= this.getClass())
+        if(obj == null || obj.getClass()!= this.getClass()) {
             return false;
+        }
 
         MeshTriangle mt = (MeshTriangle) obj;
 
diff --git a/MeshModel/src/test/java/cz/fidentis/analyst/mesh/core/MeshTriangleTest.java b/MeshModel/src/test/java/cz/fidentis/analyst/mesh/core/MeshTriangleTest.java
index 1fd03eec..b7e2b12b 100644
--- a/MeshModel/src/test/java/cz/fidentis/analyst/mesh/core/MeshTriangleTest.java
+++ b/MeshModel/src/test/java/cz/fidentis/analyst/mesh/core/MeshTriangleTest.java
@@ -1,7 +1,7 @@
 package cz.fidentis.analyst.mesh.core;
 
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
+import java.util.List;
+import javax.vecmath.Point3d;
 import javax.vecmath.Vector3d;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
@@ -9,6 +9,7 @@ import org.junit.jupiter.api.Test;
 /**
  *
  * @author oslejsek
+ * @author Dominik Racek
  */
 public class MeshTriangleTest {
     
@@ -39,4 +40,96 @@ public class MeshTriangleTest {
     }
     */
 
+    private MeshFacet createFacet() {
+        MeshFacet facet = new MeshFacetImpl();
+        facet.addVertex(new MeshPointImpl(new Point3d(0, 0, 0), null, null));
+        facet.addVertex(new MeshPointImpl(new Point3d(1, 0, 0), null, null));
+        facet.addVertex(new MeshPointImpl(new Point3d(1, 1, 0), null, null));
+        facet.addVertex(new MeshPointImpl(new Point3d(2, 1, 0), null, null));
+        facet.addVertex(new MeshPointImpl(new Point3d(1, 2, 0), null, null));
+        facet.addVertex(new MeshPointImpl(new Point3d(2, 0, 0), null, null));
+        facet.addVertex(new MeshPointImpl(new Point3d(3, 0, 0), null, null));
+        facet.addVertex(new MeshPointImpl(new Point3d(3, 1, 0), null, null));
+        return facet;
+    }
+
+    /**
+     * Unit test for triangle equals
+     */
+    @Test
+    void trianglesEquals() {
+        MeshFacet facet = createFacet();
+
+        MeshTriangle tri1 = new MeshTriangle(facet, 0, 1, 2);
+        MeshTriangle tri2 = new MeshTriangle(facet, 2, 0, 1);
+        MeshTriangle tri3 = new MeshTriangle(facet, 1, 2, 0);
+        MeshTriangle tri4 = new MeshTriangle(facet, 1, 2, 3);
+        MeshTriangle tri5 = new MeshTriangle(facet, 2, 3, 4);
+
+        Assertions.assertEquals(tri1, tri1);
+        Assertions.assertEquals(tri1, tri2);
+        Assertions.assertEquals(tri1, tri3);
+        Assertions.assertNotEquals(tri1, tri4);
+        Assertions.assertNotEquals(tri1, tri5);
+        Assertions.assertNotEquals(tri4, tri5);
+    }
+
+    /**
+     * Unit test for getCommonPoints
+     */
+    @Test
+    void getCommonPoints() {
+        MeshFacet facet = createFacet();
+
+        MeshTriangle tri1 = new MeshTriangle(facet, 0, 1, 2);
+        MeshTriangle tri2 = new MeshTriangle(facet, 1, 2, 3);
+        MeshTriangle tri3 = new MeshTriangle(facet, 2, 3, 4);
+        MeshTriangle tri4 = new MeshTriangle(facet, 5, 6, 7);
+
+        List<Point3d> common1 = tri1.getCommonPoints(tri2);
+        List<Point3d> common2 = tri2.getCommonPoints(tri3);
+        List<Point3d> common3 = tri1.getCommonPoints(tri3);
+        List<Point3d> common4 = tri1.getCommonPoints(tri4);
+
+        Assertions.assertEquals(common1.size(), 2);
+        Assertions.assertTrue(common1.contains(new Point3d(1, 0, 0)));
+        Assertions.assertTrue(common1.contains(new Point3d(1, 1, 0)));
+
+        Assertions.assertEquals(common2.size(), 2);
+        Assertions.assertTrue(common2.contains(new Point3d(1, 1, 0)));
+        Assertions.assertTrue(common2.contains(new Point3d(2, 1, 0)));
+
+
+        Assertions.assertEquals(common3.size(), 1);
+        Assertions.assertTrue(common3.contains(new Point3d(1, 1, 0)));
+
+        Assertions.assertEquals(common4.size(), 0);
+    }
+
+    /**
+     * Unit test for getCommonPoints
+     */
+    @Test
+    void checkIntersectionWithPlane() {
+        MeshFacet facet = createFacet();
+
+        Vector3d normal = new Vector3d(1, 0, 0);
+        double d1 = -1;
+        double d2 = -0.5;
+
+        MeshTriangle tri1 = new MeshTriangle(facet, 0, 1, 2);
+        MeshTriangle tri2 = new MeshTriangle(facet, 1, 2, 3);
+        MeshTriangle tri3 = new MeshTriangle(facet, 2, 3, 4);
+        MeshTriangle tri4 = new MeshTriangle(facet, 5, 6, 7);
+
+        Assertions.assertTrue(tri1.checkIntersectionWithPlane(normal, d1));
+        Assertions.assertTrue(tri2.checkIntersectionWithPlane(normal, d1));
+        Assertions.assertTrue(tri3.checkIntersectionWithPlane(normal, d1));
+        Assertions.assertFalse(tri4.checkIntersectionWithPlane(normal, d1));
+
+        Assertions.assertTrue(tri1.checkIntersectionWithPlane(normal, d2));
+        Assertions.assertFalse(tri2.checkIntersectionWithPlane(normal, d2));
+        Assertions.assertFalse(tri3.checkIntersectionWithPlane(normal, d2));
+        Assertions.assertFalse(tri4.checkIntersectionWithPlane(normal, d2));
+    }
 }
-- 
GitLab