Search in sources :

Example 71 with Point2d

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");
}
Also used : FactoryException(org.opengis.referencing.FactoryException) Arrays(java.util.Arrays) CRS(org.geotools.referencing.CRS) NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) Matrix4d(javax.vecmath.Matrix4d) Closer(org.twak.viewTrace.Closer) ConvexHull2D(org.apache.commons.math3.geometry.euclidean.twod.hull.ConvexHull2D) Loop(org.twak.utils.collections.Loop) ObjRead(org.twak.utils.geom.ObjRead) Complete(org.twak.utils.Parallel.Complete) Map(java.util.Map) Point3d(javax.vecmath.Point3d) LoopL(org.twak.utils.collections.LoopL) SuperLoop(org.twak.utils.collections.SuperLoop) Parallel(org.twak.utils.Parallel) ListDownLayout(org.twak.utils.ui.ListDownLayout) Line(org.twak.utils.Line) Set(java.util.Set) ConvergenceException(org.apache.commons.math3.exception.ConvergenceException) DefaultGeocentricCRS(org.geotools.referencing.crs.DefaultGeocentricCRS) Collectors(java.util.stream.Collectors) SatUtils(org.twak.footprints.SatUtils) FacadeFinder(org.twak.viewTrace.FacadeFinder) List(java.util.List) Optional(java.util.Optional) GMLReader(org.twak.viewTrace.GMLReader) Line3d(org.twak.utils.geom.Line3d) JPanel(javax.swing.JPanel) GenHandlesSelect(org.twak.tweed.GenHandlesSelect) FacadeTool(org.twak.tweed.tools.FacadeTool) HashMap(java.util.HashMap) Graph2D(org.twak.utils.geom.Graph2D) Pair(org.twak.utils.Pair) Vector3d(javax.vecmath.Vector3d) Vector2D(org.apache.commons.math3.geometry.euclidean.twod.Vector2D) Tweed(org.twak.tweed.Tweed) ArrayList(java.util.ArrayList) TweedSettings(org.twak.tweed.TweedSettings) HashSet(java.util.HashSet) Files(com.google.common.io.Files) SelectTool(org.twak.tweed.tools.SelectTool) NoSuchElementException(java.util.NoSuchElementException) JComponent(javax.swing.JComponent) UnionWalker(org.twak.utils.geom.UnionWalker) Work(org.twak.utils.Parallel.Work) MonotoneChain(org.apache.commons.math3.geometry.euclidean.twod.hull.MonotoneChain) IOException(java.io.IOException) JOptionPane(javax.swing.JOptionPane) File(java.io.File) Loopz(org.twak.utils.collections.Loopz) Point2d(javax.vecmath.Point2d) ConvexHull2D(org.apache.commons.math3.geometry.euclidean.twod.hull.ConvexHull2D) Line3d(org.twak.utils.geom.Line3d) MonotoneChain(org.apache.commons.math3.geometry.euclidean.twod.hull.MonotoneChain) Point3d(javax.vecmath.Point3d) ConvergenceException(org.apache.commons.math3.exception.ConvergenceException) UnionWalker(org.twak.utils.geom.UnionWalker) Loop(org.twak.utils.collections.Loop) SuperLoop(org.twak.utils.collections.SuperLoop) IOException(java.io.IOException) Graph2D(org.twak.utils.geom.Graph2D) Line(org.twak.utils.Line) Vector2D(org.apache.commons.math3.geometry.euclidean.twod.Vector2D) Point2d(javax.vecmath.Point2d) File(java.io.File)

Example 72 with Point2d

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();
    }
}
Also used : MegaFeatures(org.twak.tweed.gen.FeatureCache.MegaFeatures) HalfEdge(org.twak.utils.geom.HalfMesh2.HalfEdge) HalfFace(org.twak.utils.geom.HalfMesh2.HalfFace) GRBException(gurobi.GRBException) Line(org.twak.utils.Line) Point2d(javax.vecmath.Point2d) MFPoint(org.twak.tweed.gen.FeatureCache.MFPoint) GRBVar(gurobi.GRBVar) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) MultiMap(org.twak.utils.collections.MultiMap)

Example 73 with Point2d

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;
        }
    }
}
Also used : Line(org.twak.utils.Line) GRBLinExpr(gurobi.GRBLinExpr) Point2d(javax.vecmath.Point2d) Color(java.awt.Color) GRBVar(gurobi.GRBVar)

Example 74 with Point2d

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;
}
Also used : Color(java.awt.Color) XStream(com.thoughtworks.xstream.XStream) Arrays(java.util.Arrays) Rectangle2D(java.awt.geom.Rectangle2D) FacadeTool(org.twak.tweed.tools.FacadeTool) MiniFacade(org.twak.viewTrace.facades.MiniFacade) Tweed(org.twak.tweed.Tweed) ArrayList(java.util.ArrayList) TweedSettings(org.twak.tweed.TweedSettings) LinkedHashMap(java.util.LinkedHashMap) AlphaComposite(java.awt.AlphaComposite) PanMouseAdaptor(org.twak.utils.PanMouseAdaptor) Spatial(com.jme3.scene.Spatial) Graphics2D(java.awt.Graphics2D) Map(java.util.Map) ICanPaintU(org.twak.utils.PaintThing.ICanPaintU) ImageIO(javax.imageio.ImageIO) PaintThing(org.twak.utils.PaintThing) JComponent(javax.swing.JComponent) ICanPaint(org.twak.utils.PaintThing.ICanPaint) Composite(java.awt.Composite) BufferedImage(java.awt.image.BufferedImage) Files(java.nio.file.Files) MultiMap(org.twak.utils.collections.MultiMap) Line(org.twak.utils.Line) HalfEdge(org.twak.utils.geom.HalfMesh2.HalfEdge) Set(java.util.Set) IOException(java.io.IOException) JOptionPane(javax.swing.JOptionPane) Collectors(java.util.stream.Collectors) File(java.io.File) Point2d(javax.vecmath.Point2d) List(java.util.List) Paths(java.nio.file.Paths) JLabel(javax.swing.JLabel) YamlReader(com.esotericsoftware.yamlbeans.YamlReader) Optional(java.util.Optional) FileReader(java.io.FileReader) MultiMap(org.twak.utils.collections.MultiMap) MiniFacade(org.twak.viewTrace.facades.MiniFacade) Point2d(javax.vecmath.Point2d) ICanPaint(org.twak.utils.PaintThing.ICanPaint)

Example 75 with Point2d

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;
}
Also used : ConsecutiveItPairs(org.twak.utils.collections.ConsecutiveItPairs) ArrayList(java.util.ArrayList) Mesh(com.jme3.scene.Mesh) Vector2d(javax.vecmath.Vector2d) Vector3d(javax.vecmath.Vector3d) Point2d(javax.vecmath.Point2d) Point3d(javax.vecmath.Point3d)

Aggregations

Point2d (javax.vecmath.Point2d)84 Line (org.twak.utils.Line)37 ArrayList (java.util.ArrayList)29 Point3d (javax.vecmath.Point3d)27 List (java.util.List)18 Vector2d (javax.vecmath.Vector2d)17 SuperLine (org.twak.viewTrace.SuperLine)17 File (java.io.File)15 Vector3d (javax.vecmath.Vector3d)15 Map (java.util.Map)14 HalfEdge (org.twak.utils.geom.HalfMesh2.HalfEdge)14 HashMap (java.util.HashMap)13 Loop (org.twak.utils.collections.Loop)13 Collectors (java.util.stream.Collectors)12 IOException (java.io.IOException)11 HashSet (java.util.HashSet)11 Set (java.util.Set)11 Tweed (org.twak.tweed.Tweed)11 Matrix4d (javax.vecmath.Matrix4d)10 LinearForm (org.twak.utils.geom.LinearForm)10