Skip to content
Snippets Groups Projects
Commit 517dccd2 authored by Dominik Ráček's avatar Dominik Ráček Committed by Radek Ošlejšek
Browse files

Add unit tests for CrossSection

parent 6412ac23
No related branches found
No related tags found
No related merge requests found
......@@ -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;
......
......@@ -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
......
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();
}
}
}
......@@ -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;
......
......@@ -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;
......
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));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment