use of javax.vecmath.Point2d 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 javax.vecmath.Point2d in project chordatlas by twak.
the class GurobiSkelSolver method solve.
public void solve() {
try {
for (HalfFace f : mesh.faces) for (HalfEdge e : f.edges()) {
edges.add(e);
totalEdgeLength += e.length();
}
print("total edge length " + totalEdgeLength);
progress.setProgress((int) (Math.random() * 90));
buildProblem();
model.getEnv().set(GRB.DoubleParam.TimeLimit, GRB.INFINITY);
long time = System.currentTimeMillis();
new Thread(() -> {
while (!gurobiDone) {
try {
Thread.sleep(300);
if ((System.currentTimeMillis() - time) / 1000 > maxTimeSecs) {
print("time's up");
model.terminate();
progress.close();
return;
}
} catch (InterruptedException e1) {
}
progress.setProgress((int) (Math.random() * 90));
if (progress.isCanceled()) {
print("user cancelled");
model.terminate();
}
}
progress.setProgress(100);
}).start();
try {
String file = Tweed.SCRATCH + "problem_" + System.currentTimeMillis() + ".mps";
new File(file).getParentFile().mkdirs();
model.write(file);
model.optimize();
} finally {
gurobiDone = true;
progress.setProgress(100);
}
print("time " + (System.currentTimeMillis() - time) / 1000. + " seconds");
if (model.get(GRB.IntAttr.Status) == GRB.Status.INFEASIBLE) {
print("Can't solve; won't solve");
return;
}
for (HalfFace f : mesh.faces) ((SuperFace) f).classification = index(faceInfo.get(f).color);
if (minis != null)
for (MegaFeatures mf : minis.keySet()) {
if (mega2clusters.containsKey(mf))
for (MiniPtCluster pt : mega2clusters.get(mf)) {
for (Map.Entry<MFPoint, MiniSelectEdge> selectPt : pt.entrySet()) {
for (Map.Entry<HalfEdge, GRBVar> selectEdge : selectPt.getValue().edge.entrySet()) {
if (selectEdge.getValue().get(GRB.DoubleAttr.X) > 0.5) {
HalfEdge e = selectEdge.getKey();
selectPt.getKey().selectedEdge = e;
selectPt.getKey().sameCluster = pt.keySet();
if (e.over != null && e.next.over == null) {
for (MFPoint p : pt.keySet()) ((SuperEdge) e.next).addMini(p.right);
for (MFPoint p : pt.keySet()) ((SuperEdge) e.over.findBefore()).addMini(p.left);
}
if (e.over == null && e.next.over == null) {
Line parallel = selectPt.getKey().mega.megafacade;
if (parallel.absAngle(e.line()) < parallel.absAngle(e.next.line()))
for (MFPoint p : pt.keySet()) ((SuperEdge) e).addMini(p.left);
else
for (MFPoint p : pt.keySet()) ((SuperEdge) e.next).addMini(p.right);
}
}
}
}
}
}
if (false)
for (HalfEdge he : edges) {
if (he.over != null && he.next.over == null) {
// face comes to boundary
boolean viz = false;
EdgeVars ei = edgeInfo.get(he);
if (ei.edgeNoMini != null) {
viz = true;
}
if (viz)
PaintThing.debug.put("dd", new Point2d(he.end));
}
}
if (globalProfs != null)
for (HalfEdge e : edges) {
EdgeVars ei = edgeInfo.get(e);
GRBVar plot = ei.debug;
if (plot != null) {
// && plot.get(GRB.DoubleAttr.X) > 0.5 ) {
Point2d o = new Point2d(e.end);
PaintThing.debug.put(1, o);
}
int p = index(ei.profile);
((SuperEdge) e).profI = p;
((SuperEdge) e).prof = p < 0 ? null : globalProfs.get(p);
}
dumpModelStats(edges);
model.getEnv().dispose();
model.dispose();
} catch (GRBException e) {
print("Error code: " + e.getErrorCode() + ". " + e.getMessage());
e.printStackTrace();
}
}
use of javax.vecmath.Point2d in project chordatlas by twak.
the class GurobiSkelSolver method notBoth.
private void notBoth(HalfEdge e1, HalfEdge e2, boolean isContraint, double badThingsAreBad) throws GRBException {
GRBLinExpr no = new GRBLinExpr();
no.addTerm(1, edgeInfo.get(e1).isEdge);
no.addTerm(1, edgeInfo.get(e2).isEdge);
if (isContraint) {
model.addConstr(no, GRB.LESS_EQUAL, 1, "bad geom < 1");
} else {
GRBVar bothSelected = model.addVar(0.0, 1.0, 0, GRB.BINARY, BAD_GEOM);
no.addTerm(-2, bothSelected);
model.addConstr(no, GRB.LESS_EQUAL, 1, "bad geom <");
model.addConstr(no, GRB.GREATER_EQUAL, -0.5, "bad geom >");
target.addTerm(badThingsAreBad, bothSelected);
{
Line l1 = e1.line(), l2 = e2.line();
l1.start = new Point2d(l1.start);
l1.end = new Point2d(l1.end);
l2.start = new Point2d(l2.start);
l2.end = new Point2d(l2.end);
l1.moveLeft(-0.1);
l2.moveLeft(-0.1);
PaintThing.debug(new Color(255, 170, 0), 2, l1);
PaintThing.debug(new Color(170, 0, 255), 2, l2);
edgeInfo.get(e1).debug = bothSelected;
}
}
}
use of javax.vecmath.Point2d in project chordatlas by twak.
the class FeatureCache method createMinis.
public MultiMap<MegaFeatures, MFPoint> createMinis(BlockGen blockGen) {
MultiMap<MegaFeatures, MFPoint> out = new MultiMap();
for (MegaFeatures m : getBlock(blockGen.center).getFeatures()) {
double mLen = m.megafacade.length();
for (ImageFeatures i : m.features) {
for (int mi = 0; mi <= i.miniFacades.size(); mi++) {
MiniFacade n = mi < i.miniFacades.size() ? i.miniFacades.get(mi) : null, p = (mi - 1) >= 0 ? i.miniFacades.get(mi - 1) : null;
if (// skinny mf filter
n != null && (n.width < MFWidthTol || (n.rects.countValue() == 0 && n.width < MFWidthTol * 3)))
n = null;
if (p != null && (p.width < MFWidthTol || (p.rects.countValue() == 0 && p.width < MFWidthTol * 3)))
p = null;
if (n == null && p == null || n == null && p.softRight || p == null && n.softLeft || p != null && n != null && p.softRight && n.softLeft)
continue;
double ptDist = n == null ? (p.left + p.width) : n.left;
Point2d pt = m.megafacade.fromPPram(ptDist / mLen);
double covTol = 2;
Set<ImageFeatures> covering = m.features.stream().filter(ii -> ii.start + covTol < ptDist && ii.end > ptDist + covTol).collect(Collectors.toSet());
// stuff beyond the end of the facade
double pa = m.megafacade.findPPram(pt) * mLen;
if (pa < -5 || pa > mLen + 5)
continue;
out.put(m, new MFPoint(pt, covering, m, i, p, n));
}
}
}
return out;
}
use of javax.vecmath.Point2d in project chordatlas by twak.
the class Prof method renderStrip.
public Mesh renderStrip(double width, Point3d point3d) {
Mesh m = new Mesh();
m.setMode(Mesh.Mode.Triangles);
List<Float> coords = new ArrayList();
List<Integer> inds = new ArrayList();
Vector3d perp = new Vector3d(dir);
perp.scale(width / 2);
Point3d delta = new Point3d();
if (point3d != null) {
delta.set(point3d);
delta.sub(to3d(get(0)));
}
for (Pair<Point3d, Point3d> p : new ConsecutiveItPairs<>(get3D())) {
int o = coords.size() / 3;
inds.add(o + 0);
inds.add(o + 1);
inds.add(o + 2);
inds.add(o + 2);
inds.add(o + 1);
inds.add(o + 0);
inds.add(o + 1);
inds.add(o + 3);
inds.add(o + 2);
inds.add(o + 2);
inds.add(o + 3);
inds.add(o + 1);
Point3d a1 = new Point3d(p.first()), a2 = new Point3d(p.first()), b1 = new Point3d(p.second()), b2 = new Point3d(p.second());
a1.add(delta);
a2.add(delta);
b1.add(delta);
b2.add(delta);
a1.sub(perp);
a2.add(perp);
b1.sub(perp);
b2.add(perp);
for (Point3d pt : new Point3d[] { a1, a2, b1, b2 }) {
coords.add((float) pt.x);
coords.add((float) pt.y);
coords.add((float) pt.z);
}
}
{
Point3d p1 = to3d(get(size() - 1)), p2 = to3d(get(size() - 1));
Point2d arrowT = new Point2d(get(size() - 1)), arrowB = null;
for (int e = size() - 2; e >= 0; e--) if (!get(e).equals(get(size() - 1))) {
arrowB = new Point2d(get(e));
break;
}
if (arrowB != null) {
arrowT.sub(arrowB);
arrowT.scale(width * 1.3 / new Vector2d(arrowT).length());
arrowT.add(get(size() - 1));
Point3d p3 = to3d(arrowT);
p1.add(perp);
p2.sub(perp);
p1.add(delta);
p2.add(delta);
p3.add(delta);
int o = coords.size() / 3;
inds.add(o + 0);
inds.add(o + 1);
inds.add(o + 2);
inds.add(o + 2);
inds.add(o + 1);
inds.add(o + 0);
for (Point3d pt : new Point3d[] { p1, p2, p3 }) {
coords.add((float) pt.x);
coords.add((float) pt.y);
coords.add((float) pt.z);
}
}
}
m.setBuffer(VertexBuffer.Type.Position, 3, Arrayz.toFloatArray(coords));
m.setBuffer(VertexBuffer.Type.Index, 2, Arrayz.toIntArray(inds));
return m;
}
Aggregations