Skip to content
Snippets Groups Projects
Commit 6d587c5d authored by Radek Ošlejšek's avatar Radek Ošlejšek
Browse files

HausdorffDistMeshTriVisitor unit tests

parent fcbf4ce5
No related branches found
No related tags found
No related merge requests found
package cz.fidentis.analyst.mesh.core;
import cz.fidentis.analyst.mesh.visitors.TriangleListVisitor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
......
package cz.fidentis.analyst.mesh.visitors;
import cz.fidentis.analyst.mesh.core.MeshFacet;
import cz.fidentis.analyst.mesh.core.MeshPoint;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Visitor for Hausdorff distance.
* This visitor is instantiated on a single mesh facet or multiple facets.
* When applied to other facets, it computes Huasdorff distance to them.
*
* Distances to triangles are computed, in constrast to the {@code HausdorffDistMeshTriVisitor}
* which calculated distances to mesh points.
*
* @author Matej Lukes
* @author Radek Oslejsek
*/
public class HausdorffDistMeshTriVisitor extends HausdorffDistMeshVisitor {
/**
* @param mainFacets Facets whose distance to other facets is computed. Must not be {@code null}
* @param relativeDistance If true, then the visitor calculates the relative distances with respect
* to the normal vectors of source facets (normal vectors have to present),
* i.e., we can get negative distances.
* @throws IllegalArgumentException if some parametr is wrong
*/
public HausdorffDistMeshTriVisitor(Set<MeshFacet> mainFacets, boolean relativeDistance) {
super(mainFacets, relativeDistance);
}
/**
* @param mainFacet Primary facet of which distance to others is to be computed. Must not be {@code null}
* @param relativeDistance If true, then the visitor calculates the relative distances with respect
* to the normal vectors of source facets (normal vectors have to present),
* i.e., we can get negative distances.
* @throws IllegalArgumentException if some parametr is wrong
*/
public HausdorffDistMeshTriVisitor(MeshFacet mainFacet, boolean relativeDistance) {
super(mainFacet, relativeDistance);
}
@Override
public void visitMeshFacet(MeshFacet comparedFacet) {
for (Map.Entry<MeshFacet, List<Double>> entry: getDistMap().entrySet()) {
List<MeshPoint> vertices = entry.getKey().getVertices();
List<Double> distList = entry.getValue();
boolean firstComparison = distList.isEmpty();
for (int i = 0; i < vertices.size(); i++) {
Point2MeshTriVisitor visitor = new Point2MeshTriVisitor(vertices.get(i), isRelativeDistance());
comparedFacet.accept(visitor);
double dist = visitor.getDistance();
updateDistances(distList, firstComparison, dist, i);
}
}
}
}
......@@ -13,7 +13,7 @@ import java.util.Set;
/**
* Visitor for Hausdorff distance.
* This visitor is instantiated on a single mesh facet or multiple facets.
* When applied to other facets, it computes Huasdorff distance to these facets.
* When applied to other facets, it computes Huasdorff distance to them.
*
* @author Matej Lukes
* @author Radek Oslejsek
......@@ -66,19 +66,7 @@ public class HausdorffDistMeshVisitor implements MeshVisitor {
Point2MeshVisitor visitor = new Point2MeshVisitor(vertices.get(i), relativeDistance);
comparedFacet.accept(visitor);
double dist = visitor.getDistance();
if (firstComparison) {
distList.add(dist);
} else {
if (dist >= 0 && dist < distList.get(i)) {
distList.set(i, dist);
continue;
}
if (dist < 0 && dist > distList.get(i)) { // for relative dist only
distList.set(i, dist);
continue;
}
}
updateDistances(distList, firstComparison, dist, i);
}
}
}
......@@ -95,5 +83,28 @@ public class HausdorffDistMeshVisitor implements MeshVisitor {
public Map<MeshFacet, List<Double>> getDistances() {
return Collections.unmodifiableMap(distances);
}
protected boolean isRelativeDistance() {
return relativeDistance;
}
protected Map<MeshFacet, List<Double>> getDistMap() {
return distances;
}
protected void updateDistances(List<Double> distList, boolean firstComparison, double dist, int index) {
if (firstComparison) {
distList.add(dist);
return;
}
if (dist >= 0 && dist < distList.get(index)) {
distList.set(index, dist);
return;
}
if (dist < 0 && dist > distList.get(index)) { // for relative dist only
distList.set(index, dist);
}
}
}
......@@ -97,38 +97,14 @@ public class Point2MeshVisitor implements MeshVisitor {
return relativeDist;
}
protected void setRelativeDist(boolean relativeDist) {
this.relativeDist = relativeDist;
}
protected MeshPoint getMyPoint() {
return myPoint;
}
protected void setMyPoint(MeshPoint myPoint) {
this.myPoint = myPoint;
}
protected int getSign() {
return sign;
}
protected void setSign(int sign) {
this.sign = sign;
}
protected void setDistance(double dist) {
this.distance = dist;
}
protected List<Integer> getModifIndices() {
return indices;
}
protected List<MeshFacet> getModifClosestFacets() {
return closestFacets;
}
protected void checkAndUpdateDistance(Vector3d other, MeshFacet facet, int index) {
Vector3d aux = new Vector3d(other);
aux.sub(myPoint.getPosition());
......
package cz.fidentis.analyst.mesh.visitors;
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.MeshPointImpl;
import java.util.List;
import java.util.Map;
import javax.vecmath.Vector3d;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class HausdorffDistMeshTriVisitorTest {
private MeshFacet getTrivialFacet(double offset, double size) {
MeshFacet facet = new MeshFacetImpl();
facet.addVertex(new MeshPointImpl(new Vector3d(0, 0, offset), new Vector3d(0, 0, 1), new Vector3d()));
facet.addVertex(new MeshPointImpl(new Vector3d(size, 0, offset), new Vector3d(0, 0, 1), new Vector3d()));
facet.addVertex(new MeshPointImpl(new Vector3d(0, size, offset), new Vector3d(0, 0, 1), new Vector3d()));
facet.getCornerTable().addRow(new CornerTableRow(0, -1));
facet.getCornerTable().addRow(new CornerTableRow(1, -1));
facet.getCornerTable().addRow(new CornerTableRow(2, -1));
return facet;
}
@Test
public void visitToVerticesTest() {
MeshFacet mainFacet = getTrivialFacet(1, 1);
MeshFacet comparedFacet = getTrivialFacet(1.5, 1);
HausdorffDistMeshTriVisitor hausdorffDistance = new HausdorffDistMeshTriVisitor(mainFacet, false);
hausdorffDistance.visitMeshFacet(comparedFacet);
Map<MeshFacet, List<Double>> map = hausdorffDistance.getDistances();
List<Double> results = map.get(mainFacet);
for (int i = 0; i < mainFacet.getNumberOfVertices(); i++) {
assertEquals(0.5, results.get(i));
}
}
@Test
public void visitToVerticesBehindMeshTest() {
MeshFacet mainFacet = getTrivialFacet(1, 1);
MeshFacet comparedFacet = getTrivialFacet(-1.5, 1);
HausdorffDistMeshTriVisitor hausdorffDistance = new HausdorffDistMeshTriVisitor(mainFacet, false);
hausdorffDistance.visitMeshFacet(comparedFacet);
Map<MeshFacet, List<Double>> map = hausdorffDistance.getDistances();
List<Double> results = map.get(mainFacet);
for (int i = 0; i < mainFacet.getNumberOfVertices(); i++) {
assertEquals(2.5, results.get(i));
}
}
@Test
public void visitToVerticesRelativeDistanceTest() {
MeshFacet mainFacet = getTrivialFacet(1, 1);
MeshFacet comparedFacet = getTrivialFacet(1.5, 1);
HausdorffDistMeshTriVisitor hausdorffDistance = new HausdorffDistMeshTriVisitor(mainFacet, true);
hausdorffDistance.visitMeshFacet(comparedFacet);
Map<MeshFacet, List<Double>> map = hausdorffDistance.getDistances();
List<Double> results = map.get(mainFacet);
for (int i = 0; i < mainFacet.getNumberOfVertices(); i++) {
assertEquals(0.5, results.get(i));
}
}
@Test
public void visitToVerticesBehindMeshRelativeDistanceTest() {
MeshFacet mainFacet = getTrivialFacet(1, 1);
MeshFacet comparedFacet = getTrivialFacet(-1.5, 1);
HausdorffDistMeshTriVisitor hausdorffDistance = new HausdorffDistMeshTriVisitor(mainFacet, true);
hausdorffDistance.visitMeshFacet(comparedFacet);
Map<MeshFacet, List<Double>> map = hausdorffDistance.getDistances();
List<Double> results = map.get(mainFacet);
for (int i = 0; i < mainFacet.getNumberOfVertices(); i++) {
assertEquals(-2.5, results.get(i));
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cz.fidentis.analyst.mesh.visitors;
import cz.fidentis.analyst.mesh.core.CornerTableRow;
......@@ -12,13 +7,9 @@ import cz.fidentis.analyst.mesh.core.MeshPointImpl;
import java.util.List;
import java.util.Map;
import javax.vecmath.Vector3d;
import org.junit.jupiter.api.Assertions;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
/**
*
* @author oslejsek
*/
public class HausdorffDistMeshVisitorTest {
private MeshFacet getTrivialFacet(double offset, double size) {
......@@ -45,7 +36,7 @@ public class HausdorffDistMeshVisitorTest {
Map<MeshFacet, List<Double>> map = hausdorffDistance.getDistances();
List<Double> results = map.get(mainFacet);
for (int i = 0; i < mainFacet.getNumberOfVertices(); i++) {
Assertions.assertEquals(0.5, results.get(i));
assertEquals(0.5, results.get(i));
}
}
......@@ -60,7 +51,7 @@ public class HausdorffDistMeshVisitorTest {
Map<MeshFacet, List<Double>> map = hausdorffDistance.getDistances();
List<Double> results = map.get(mainFacet);
for (int i = 0; i < mainFacet.getNumberOfVertices(); i++) {
Assertions.assertEquals(2.5, results.get(i));
assertEquals(2.5, results.get(i));
}
}
......@@ -75,7 +66,7 @@ public class HausdorffDistMeshVisitorTest {
Map<MeshFacet, List<Double>> map = hausdorffDistance.getDistances();
List<Double> results = map.get(mainFacet);
for (int i = 0; i < mainFacet.getNumberOfVertices(); i++) {
Assertions.assertEquals(0.5, results.get(i));
assertEquals(0.5, results.get(i));
}
}
......@@ -90,7 +81,7 @@ public class HausdorffDistMeshVisitorTest {
Map<MeshFacet, List<Double>> map = hausdorffDistance.getDistances();
List<Double> results = map.get(mainFacet);
for (int i = 0; i < mainFacet.getNumberOfVertices(); i++) {
Assertions.assertEquals(-2.5, results.get(i));
assertEquals(-2.5, results.get(i));
}
}
......
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