use of org.apache.commons.math3.geometry.euclidean.twod.Vector2D in project chordatlas by twak.
the class GISGen method importMesh.
private void importMesh(int index) {
LoopL<Point3d> polies = blocks.get(index);
List<Vector2D> verts = polies.stream().flatMap(ll -> ll.streamAble()).map(x -> {
Line3d l = new Line3d(x.get(), x.getNext().get());
l.move(perp(l.dir(), EXPAND_MESH));
return new Vector2D(l.start.x, l.start.z);
}).collect(Collectors.toList());
double tol = 0.0001;
ConvexHull2D chull = null;
while (tol < 10) {
try {
chull = new MonotoneChain(false, tol).generate(verts);
tol = 1000;
} catch (ConvergenceException e) {
tol *= 10;
}
}
if (chull == null) {
System.out.println("unable to find hull");
return;
}
Loop<Point3d> hull = new Loop<Point3d>((Arrays.stream(chull.getLineSegments()).map(x -> new Point3d(x.getStart().getX(), 0, x.getStart().getY())).collect(Collectors.toList())));
File root = new File(Tweed.SCRATCH + "meshes" + File.separator);
int i = 0;
File l;
while ((l = new File(root, "" + i)).exists()) i++;
l.mkdirs();
File croppedFile = new File(l, CROPPED_OBJ);
boolean found = false;
for (Gen gen : tweed.frame.gens(MiniGen.class)) {
// minigen == optimised obj
((MiniGen) gen).clip(hull, croppedFile);
found = true;
}
if (!found)
for (Gen gen : tweed.frame.gens(MeshGen.class)) {
// obj == just import whole obj
ObjGen objg = (ObjGen) gen;
try {
Files.asByteSource(objg.getFile()).copyTo(Files.asByteSink(croppedFile));
objg.setVisible(false);
found = true;
} catch (IOException e) {
e.printStackTrace();
}
}
if (found) {
Graph2D g2 = new Graph2D();
polies.stream().flatMap(ll -> ll.streamAble()).forEach(x -> g2.add(new Point2d(x.get().x, x.get().z), new Point2d(x.getNext().get().x, x.getNext().get().z)));
g2.removeInnerEdges();
// new Plot (true, g2 );
UnionWalker uw = new UnionWalker();
for (Point2d p : g2.map.keySet()) for (Line line : g2.map.get(p)) uw.addEdge(line.end, line.start);
// new Plot (true, new ArrayList( uw.map.keySet()) );
Loopz.writeXZObj(uw.findAll(), new File(l, "gis.obj"), true);
Loopz.writeXZObj(Loopz.to2dLoop(polies, 1, null), new File(l, "gis_footprints.obj"), false);
BlockGen bg = new BlockGen(l, tweed, polies);
lastMesh.put(index, bg);
tweed.frame.addGen(bg, true);
tweed.frame.setSelected(bg);
} else
JOptionPane.showMessageDialog(tweed.frame(), "Failed to find mesh from minimesh or gml layers");
}
use of org.apache.commons.math3.geometry.euclidean.twod.Vector2D in project chordatlas by twak.
the class Prof method parameterize.
public Prof parameterize() {
// find and subtract vertical lines
Set<Line> lines = new HashSet<>();
for (int i = 1; i < size(); i++) lines.add(new Line(get(i - 1), get(i)));
double avgMinY = get(0).y;
SliceParameters P = new SliceParameters(5);
P.FL_REGRESS = false;
P.FL_BINS = 20;
// simple = 0.4
double A = 0.4;
// simple = 1;
double B = 1;
lines = new FindLines(lines, P) {
protected double nextAngle(Set<Line> remaining, int iteration) {
double delta = Math.PI / P.FL_BINS;
// angle bin
Bin<Line> aBin = new Bin(-Math.PI - delta, Math.PI + delta, P.FL_BINS * 2, true);
for (Line l : remaining) {
double len = l.length();
double angle = l.aTan2();
aBin.add(angle, len, l);
}
// return MUtils.PI2;
if (iteration < 1 && aBin.getWeight(Mathz.PI2) >= 5)
return Mathz.PI2;
int aBinI = aBin.maxI();
return aBin.val(aBinI);
}
protected double getTolNearLine(Point2d p) {
return P.FL_NEAR_LINE * (p.y < avgMinY + 5 ? 5 : B);
}
protected double getTolNearLine2(Point2d p) {
return P.FL_NEAR_LINE_2 * (p.y < avgMinY + 5 ? 10 : B);
}
}.result.all;
clean = new Prof(this);
clean.clear();
if (lines.isEmpty()) {
clean.add(new Point2d(0, 0));
clean.add(new Point2d(0, 1));
return clean;
}
List<Line> llines = new ArrayList(lines);
llines.stream().filter(l -> l.start.y > l.end.y).forEach(l -> l.reverseLocal());
Collections.sort(llines, new Comparator<Line>() {
public int compare(Line o1, Line o2) {
return Double.compare(o1.start.y + o1.end.y, o2.start.y + o2.end.y);
}
});
for (int i = 0; i < llines.size(); i++) {
Line l = llines.get(i);
double angle = l.aTan2();
if (angle < Mathz.PI2 + 0.1 && angle > Mathz.PI2 - 0.4)
llines.set(i, FindLines.rotateToAngle(l, l.fromPPram(0.5), Mathz.PI2));
}
Line bottomLine = llines.get(0);
llines.add(0, new Line(new Point2d(bottomLine.start.x, get(0).y), new Point2d(bottomLine.start.x, get(0).y)));
double lastY = -Double.MAX_VALUE, lastX = Double.MAX_VALUE;
for (Line l : llines) {
boolean startAbove = l.start.y >= lastY && l.start.x <= lastX, endAbove = l.end.y >= lastY && l.end.x <= lastX;
if (startAbove && endAbove) {
clean.add(l.start);
clean.add(l.end);
} else if (!startAbove && endAbove) {
if (l.start.y < lastY) {
Point2d sec = new LinearForm(new Vector2d(1, 0)).findC(new Point2d(0, lastY)).intersect(new LinearForm(l));
if (sec != null)
l.start = sec;
}
if (l.start.x > lastX) {
Point2d sec = new LinearForm(new Vector2d(0, 1)).findC(new Point2d(lastX, 0)).intersect(new LinearForm(l));
if (sec != null)
l.start = sec;
}
if (l.end.distanceSquared(l.start) < 100)
clean.add(l.start);
clean.add(l.end);
} else {
Vector2d dir = l.dir();
if (Math.abs(dir.x) > Math.abs(dir.y)) {
LinearForm x = new LinearForm(new Vector2d(1, 0)).findC(new Point2d(0, lastY));
l.start = x.project(l.start);
l.end = x.project(l.end);
l.start.x = Math.min(l.start.x, lastX);
l.end.x = Math.min(l.end.x, lastX);
} else {
LinearForm y = new LinearForm(new Vector2d(0, 1)).findC(new Point2d(lastX, 0));
l.start = y.project(l.start);
l.end = y.project(l.end);
l.start.y = Math.max(l.start.y, lastY);
l.end.y = Math.max(l.end.y, lastY);
}
clean.add(l.start);
clean.add(l.end);
}
lastY = l.end.y;
lastX = l.end.x;
}
clean.clean(0.2);
return clean;
}
use of org.apache.commons.math3.geometry.euclidean.twod.Vector2D in project tutorials by eugenp.
the class GeometryUnitTest method whenLineIntersection_thenCorrect.
@Test
public void whenLineIntersection_thenCorrect() {
Line l1 = new Line(new Vector2D(0, 0), new Vector2D(1, 1), 0);
Line l2 = new Line(new Vector2D(0, 1), new Vector2D(1, 1.5), 0);
Vector2D intersection = l1.intersection(l2);
Assert.assertEquals(2, intersection.getX(), 1e-7);
Assert.assertEquals(2, intersection.getY(), 1e-7);
}
use of org.apache.commons.math3.geometry.euclidean.twod.Vector2D in project imagej-ops by imagej.
the class DefaultMinimumFeret method calculate.
@Override
public Pair<RealLocalizable, RealLocalizable> calculate(Polygon2D input) {
final List<? extends RealLocalizable> points = GeomUtils.vertices(function.calculate(input));
double distance = Double.POSITIVE_INFINITY;
RealLocalizable p0 = points.get(0);
RealLocalizable p1 = points.get(0);
double tmpDist = 0;
RealLocalizable tmpP0 = p0;
RealLocalizable tmpP1 = p1;
for (int i = 0; i < points.size() - 2; i++) {
final RealLocalizable lineStart = points.get(i);
final RealLocalizable lineEnd = points.get(i + 1);
tmpDist = 0;
final Line l = new Line(new Vector2D(lineStart.getDoublePosition(0), lineStart.getDoublePosition(1)), new Vector2D(lineEnd.getDoublePosition(0), lineEnd.getDoublePosition(1)), 10e-12);
for (int j = 0; j < points.size(); j++) {
if (j != i && j != i + 1) {
final RealLocalizable ttmpP0 = points.get(j);
final double tmp = l.distance(new Vector2D(ttmpP0.getDoublePosition(0), ttmpP0.getDoublePosition(1)));
if (tmp > tmpDist) {
tmpDist = tmp;
final Vector2D vp = (Vector2D) l.project(new Vector2D(ttmpP0.getDoublePosition(0), ttmpP0.getDoublePosition(1)));
tmpP0 = new RealPoint(vp.getX(), vp.getY());
tmpP1 = ttmpP0;
}
}
}
if (tmpDist < distance) {
distance = tmpDist;
p0 = tmpP0;
p1 = tmpP1;
}
}
final RealLocalizable lineStart = points.get(points.size() - 1);
final RealLocalizable lineEnd = points.get(0);
final Line l = new Line(new Vector2D(lineStart.getDoublePosition(0), lineStart.getDoublePosition(1)), new Vector2D(lineEnd.getDoublePosition(0), lineEnd.getDoublePosition(1)), 10e-12);
tmpDist = 0;
for (int j = 0; j < points.size(); j++) {
if (j != points.size() - 1 && j != 0 + 1) {
final RealLocalizable ttmpP0 = points.get(j);
final double tmp = l.distance(new Vector2D(ttmpP0.getDoublePosition(0), ttmpP0.getDoublePosition(1)));
if (tmp > tmpDist) {
tmpDist = tmp;
final Vector2D vp = (Vector2D) l.project(new Vector2D(ttmpP0.getDoublePosition(0), ttmpP0.getDoublePosition(1)));
tmpP0 = new RealPoint(vp.getX(), vp.getY());
tmpP1 = ttmpP0;
}
}
}
if (tmpDist < distance) {
distance = tmpDist;
p0 = tmpP0;
p1 = tmpP1;
}
return new ValuePair<>(p0, p1);
}
Aggregations