Commit 89977fad authored by Benedikt Zoennchen's avatar Benedikt Zoennchen
Browse files

meshing of the s2ucre bridge, new distance functions, fmm on mesh: small corrections.

parent 3b81a22e
# nVertices dimension nAttributes boundaryMarker
21 2 0 0
# vertexId x y
1 0.500000 0.500000
2 0.326351 11.349890
3 50.295924 42.506680
4 10.068317 15.240938
5 50.129121 5.936819
6 49.500000 49.500000
7 49.500000 0.500000
8 7.711478 10.030659
9 0.463889 45.907735
10 0.500000 49.500000
11 32.129121 5.936819
12 29.943119 0.478289
13 3.888684 20.251412
14 0.463889 42.907735
15 1.463889 42.907735
16 23.463889 45.907735
17 0.326351 -0.650110
18 15.459378 12.808810
19 20.931872 10.544835
20 29.943119 -1.521711
21 31.295924 42.506680
#
# nSegments boundaryMarker
21 0
# lineId vertexId1 vertexId2
1 7 6
2 6 10
3 10 1
4 1 7
5 3 21
6 21 19
7 19 11
8 11 5
9 5 3
10 20 12
11 12 8
12 8 2
13 2 17
14 17 20
15 13 4
16 4 18
17 18 16
18 16 9
19 9 14
20 14 15
21 15 13
#
# nHoles
3
# vertexId x y (of a vertex which lies inside the hole)
1 37.999061 23.296979
2 11.703993 3.490417
3 11.505979 32.412715
\ No newline at end of file
......@@ -55,8 +55,9 @@ public class EikMeshPlots {
discSubtractRect2(0.03);
discSubtractRect2(0.01);
kaiserslautern();*/
ruppertsAndEikMeshKaiserslautern();
kaiserslautern();
ruppertsAndEikMeshKaiserslautern();*/
bridge();
}
public static void randomDelaunay() throws IOException {
......@@ -116,6 +117,42 @@ public class EikMeshPlots {
meshPanel.display("Combined distance functions " + h0);
}
public static void bridge() throws IOException, InterruptedException {
final InputStream inputStream = MeshExamples.class.getResourceAsStream("/poly/bridge.poly");
PSLG pslg = PolyGenerator.toPSLGtoVShapes(inputStream);
Collection<VPolygon> holes = pslg.getHoles();
VPolygon segmentBound = pslg.getSegmentBound();
IPointConstructor<EikMeshPoint> pointConstructor = (x, y) -> new EikMeshPoint(x, y);
IDistanceFunction distanceFunction = IDistanceFunction.create(segmentBound, holes);
// (3) use EikMesh to improve the mesh
double h0 = 1.0;
var meshImprover = new PEikMeshGen<EikMeshPoint, Double, Double>(
distanceFunction,
p -> h0 + 0.5 * Math.abs(distanceFunction.apply(p)),
h0,
new VRectangle(segmentBound.getBounds2D()),
pslg.getHoles(),
pointConstructor
);
var meshPanel = new PMeshPanel<>(meshImprover.getMesh(), 1000, 800);
meshPanel.display("Combined distance functions " + h0);
while (!meshImprover.isFinished()) {
meshImprover.improve();
Thread.sleep(20);
meshPanel.repaint();
}
//meshImprover.generate();
write(toTexDocument(TexGraphGenerator.toTikz(meshImprover.getMesh(), f-> lightBlue, 1.0f)), "eikmesh_kaiserslautern_"+ Double.toString(h0).replace('.', '_'));
// display the mesh
meshPanel.display("Combined distance functions " + h0);
}
public static void eikMeshA(double h0) throws IOException, InterruptedException {
final InputStream inputStream = MeshExamples.class.getResourceAsStream("/poly/a.poly");
PSLG pslg = PolyGenerator.toPSLGtoVShapes(inputStream);
......
......@@ -254,7 +254,7 @@ public class MeshExamples {
var allConstrains = cdt.getConstrains();
Function<PHalfEdge<VPoint, Double, Double>, Color> colorFunction = e -> allConstrains.contains(e) ? red : Color.GRAY;
System.out.println(TexGraphGenerator.toTikz(cdt.getMesh(), f -> Color.WHITE, colorFunction, 1.0f));
System.out.println(TexGraphGenerator.toTikz(cdt.getMesh(), f -> Color.WHITE, colorFunction, 1.0f, false));
//System.out.println(TexGraphGenerator.toTikz(dt.getMesh(), 1.0f));
......
......@@ -2237,7 +2237,7 @@ public interface IMesh<
Map<V, Integer> indexMap = new HashMap<>();
// [x1, x2, ...]
builder.append("[");
builder.append("x =[");
for(int i = 0; i < vertices.size(); i++) {
V v = vertices.get(i);
indexMap.put(v, i);
......@@ -2247,7 +2247,7 @@ public interface IMesh<
builder.append("]\n");
// [y1, y2, ...]
builder.append("[");
builder.append("y = [");
for(V v : vertices) {
builder.append(v.getY() + ",");
}
......@@ -2255,7 +2255,7 @@ public interface IMesh<
builder.append("]\n");
// [z1, z2, ...] z = value
builder.append("[");
builder.append("z = [");
for(V v : vertices) {
builder.append(evalPoint.apply(getPoint(v)) + ",");
}
......@@ -2264,7 +2264,7 @@ public interface IMesh<
// [[vId1, vId2, vId3], ...]
List<F> faces = getFaces();
builder.append("[");
builder.append("tris = [");
for(F face : faces) {
builder.append("[");
for(V v : getVertexIt(face)) {
......
......@@ -146,7 +146,8 @@ public class TexGraphGenerator {
@NotNull final IMesh<P, CE, CF, V, E, F> mesh,
@NotNull final Function<F, Color> coloring,
@Nullable final Function<E, Color> edgeColorFunction,
final float scaling) {
final float scaling,
final boolean drawVertices) {
StringBuilder builder = new StringBuilder();
builder.append("\\begin{tikzpicture}[scale="+scaling+"]\n");
......@@ -174,6 +175,12 @@ public class TexGraphGenerator {
builder.append("\\draw[color="+tikzColor+"]("+toString(line.getX1())+","+toString(line.getY1())+") -- ("+toString(line.getX2())+","+toString(line.getY2())+");\n");
}
}
double pt = 1.5;
if(drawVertices) {
for(P point : mesh.getPoints()) {
builder.append("\\draw[color=black, fill=black]("+toString(point.getX())+","+toString(point.getY())+") circle ("+pt+"pt);\n");
}
}
/*for(F face : mesh.getFaces()) {
String poly = mesh.streamVertices(face).map(v -> "("+v.getX()+","+v.getY()+")").reduce((s1, s2) -> s1 + "--" + s2).get();
......@@ -205,7 +212,7 @@ public class TexGraphGenerator {
@NotNull final IMesh<P, CE, CF, V, E, F> mesh,
@NotNull final Function<F, Color> coloring,
final float scaling) {
return toTikz(mesh, coloring, null, scaling);
return toTikz(mesh, coloring, null, scaling, false);
}
/**
......
......@@ -21,6 +21,7 @@ import org.vadere.util.math.IDistanceFunction;
import org.vadere.util.math.InterpolationUtil;
import org.vadere.util.math.MathUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
......@@ -62,6 +63,9 @@ public class EikonalSolverFMMTriangulation<P extends IPotentialPoint, V extends
*/
private ITimeCostFunction timeCostFunction;
/**
* Gives the distance to the boundary i.e. the targets
*/
private IDistanceFunction distFunc;
private Collection<V> targetVertices;
......@@ -114,10 +118,15 @@ public class EikonalSolverFMMTriangulation<P extends IPotentialPoint, V extends
this.calculationFinished = false;
this.timeCostFunction = timeCostFunction;
this.narrowBand = new PriorityQueue<>(pointComparator);
this.targetVertices = new HashSet<>();
this.distFunc = p -> IDistanceFunction.createToTargetPoints(targetPoints).apply(p);
for(IPoint point : targetPoints) {
F face = triangulation.locateFace(point.getX(), point.getY()).get();
initialFace(face, p -> point.distance(p));
if(!getMesh().isBoundary(face)) {
targetVertices.addAll(getMesh().getVertices(face));
}
//initialFace(face, p -> point.distance(p));
}
}
......@@ -136,12 +145,14 @@ public class EikonalSolverFMMTriangulation<P extends IPotentialPoint, V extends
this.calculationFinished = false;
this.timeCostFunction = timeCostFunction;
this.narrowBand = new PriorityQueue<>(pointComparator);
this.targetVertices = new HashSet<>();
this.distFunc = IDistanceFunction.createToTargets(targetShapes);
for(VShape shape : targetShapes) {
getMesh().streamFaces()
.filter(f -> !getMesh().isBoundary(f))
.filter(f -> shape.intersects(getMesh().toTriangle(f)))
.forEach(f -> initialFace(f, p -> Math.max(shape.distance(p), 0)));
.forEach(f -> targetVertices.addAll(getMesh().getVertices(f)));
}
}
......@@ -164,6 +175,7 @@ public class EikonalSolverFMMTriangulation<P extends IPotentialPoint, V extends
this.narrowBand = new PriorityQueue<>(pointComparator);
this.distFunc = distFunc;
this.targetVertices = targetVertices;
for(F face : triangulation.getMesh().getFaces()) {
if(isNonAcute(face)) {
nonAccuteTris.add(face);
......@@ -216,7 +228,7 @@ public class EikonalSolverFMMTriangulation<P extends IPotentialPoint, V extends
for(V vertex : targetVertices) {
P potentialPoint = getMesh().getPoint(vertex);
double distance = -distFunc.apply(potentialPoint);
double distance = Math.max(distFunc.apply(potentialPoint), 0);
if(potentialPoint.getPathFindingTag() != PathFindingTag.Undefined) {
narrowBand.remove(vertex);
......@@ -230,7 +242,7 @@ public class EikonalSolverFMMTriangulation<P extends IPotentialPoint, V extends
P potentialP = getMesh().getPoint(v);
if(potentialP.getPathFindingTag() == PathFindingTag.Undefined) {
double dist = Math.max(-distFunc.apply(potentialP), 0);
double dist = Math.max(distFunc.apply(potentialP), 0);
logger.debug("T at " + potentialP + " = " + dist);
potentialP.setPotential(Math.min(potentialP.getPotential(), dist / timeCostFunction.costAt(potentialP)));
potentialP.setPathFindingTag(PathFindingTag.Reachable);
......
package org.vadere.simulator.models.potential.solver.calculators.mesh;
import org.vadere.meshing.examples.MeshExamples;
import org.vadere.meshing.mesh.gen.PFace;
import org.vadere.meshing.mesh.gen.PHalfEdge;
import org.vadere.meshing.mesh.gen.PVertex;
import org.vadere.meshing.mesh.impl.PMeshPanel;
import org.vadere.meshing.mesh.impl.PSLG;
import org.vadere.meshing.mesh.inter.IIncrementalTriangulation;
import org.vadere.meshing.mesh.inter.IPointConstructor;
import org.vadere.meshing.mesh.triangulation.improver.eikmesh.gen.PEikMeshGen;
import org.vadere.meshing.utils.io.poly.PolyGenerator;
import org.vadere.meshing.utils.io.tex.TexGraphGenerator;
import org.vadere.simulator.models.potential.solver.calculators.EikonalSolver;
import org.vadere.simulator.models.potential.solver.timecost.UnitTimeCostFunction;
import org.vadere.util.geometry.GeometryUtils;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VPolygon;
import org.vadere.util.geometry.shapes.VRectangle;
import org.vadere.util.logging.Logger;
import org.vadere.util.math.IDistanceFunction;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.function.Function;
public class FMMTriangulationExamples {
private static Logger log = Logger.getLogger(FMMTriangulationExamples.class);
public static void main(String... args) throws IOException, InterruptedException {
var triangulation = bridge();
VPolygon targetShape = GeometryUtils.toPolygon(new VPoint(33.90000000002328, 0.20000000018626451),
new VPoint(29.900000000023283, 0.5),
new VPoint(32.300000000046566, 6.0),
new VPoint(36.40000000002328, 4.900000000372529));
EikonalSolver solver = new EikonalSolverFMMTriangulation(Arrays.asList(targetShape), new UnitTimeCostFunction(), triangulation);
log.info("start FFM");
solver.initialize();
log.info("FFM finished");
System.out.println(triangulation.getMesh().toPythonTriangulation(p -> p.getPotential()));
}
public static IIncrementalTriangulation<PotentialPoint, Double, Double,
PVertex<PotentialPoint, Double, Double>,
PHalfEdge<PotentialPoint, Double, Double>,
PFace<PotentialPoint, Double, Double>> bridge() throws IOException, InterruptedException {
final InputStream inputStream = MeshExamples.class.getResourceAsStream("/poly/bridge.poly");
PSLG pslg = PolyGenerator.toPSLGtoVShapes(inputStream);
Collection<VPolygon> holes = pslg.getHoles();
VPolygon segmentBound = pslg.getSegmentBound();
IPointConstructor<PotentialPoint> pointConstructor = (x, y) -> new PotentialPoint(x, y);
IDistanceFunction distanceFunction = IDistanceFunction.create(segmentBound, holes);
// (3) use EikMesh to improve the mesh
double h0 = 1.0;
var meshImprover = new PEikMeshGen<PotentialPoint, Double, Double>(
distanceFunction,
p -> h0 + 0.5 * Math.abs(distanceFunction.apply(p)),
h0,
new VRectangle(segmentBound.getBounds2D()),
pslg.getHoles(),
pointConstructor
);
var mesh = meshImprover.getMesh();
Color green = new Color(85, 168, 104);
Color red = new Color(196,78,82);
Color blue = new Color(76,114,202);
Function<PFace<PotentialPoint, Double, Double>, Color> colorFunction = f -> {
VPoint midpoint = mesh.toTriangle(f).midPoint();
if(midpoint.getY() < 46 && midpoint.getX() < 10) {
return blue;
}
else if(midpoint.getY() < 20) {
return green;
} else {
return red;
}
};
var meshPanel = new PMeshPanel<>(meshImprover.getMesh(), 1000, 800, colorFunction);
meshPanel.display("Combined distance functions " + h0);
while (!meshImprover.isFinished()) {
meshImprover.improve();
Thread.sleep(20);
meshPanel.repaint();
}
//meshImprover.generate();
// display the mesh
meshPanel.display("Combined distance functions " + h0);
System.out.println(TexGraphGenerator.toTikz(mesh, colorFunction, null, 1.0f, true));
return meshImprover.getTriangulation();
}
}
......@@ -41,6 +41,34 @@ public interface IDistanceFunction extends Function<IPoint, Double> {
};
}
static IDistanceFunction createToTargets(final Collection<? extends VShape> targets) {
assert !targets.isEmpty();
return p -> {
double min = Double.POSITIVE_INFINITY;
for (VShape shape : targets) {
double dist = shape.distance(p);
if(dist < min) {
min = dist;
}
}
return min;
};
}
static IDistanceFunction createToTargetPoints(final Collection<? extends IPoint> targetPoints) {
assert !targetPoints.isEmpty();
return p -> {
double min = Double.POSITIVE_INFINITY;
for (IPoint targetPoint : targetPoints) {
double dist = targetPoint.distance(p);
if(dist < min) {
min = dist;
}
}
return min;
};
}
static IDistanceFunction create(final VRectangle regionBoundingBox, final Collection<? extends VShape> obstacles) {
return new DistanceFunction(regionBoundingBox, obstacles);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment