Search in sources :

Example 21 with Point2d

use of javax.vecmath.Point2d in project chordatlas by twak.

the class ImagePlaneGen method rotateByAngle.

public ImagePlaneGen rotateByAngle(double deltaAngle, double distance) {
    Line nLine = new Line(new Point2d(a.x, a.z), new Point2d(b.x, b.z));
    Point2d cen = nLine.fromPPram(0.5);
    double angle = nLine.aTan2() + deltaAngle;
    double len = nLine.length() / 2;
    Vector2d dir = new Vector2d(-Math.cos(angle) * len, -Math.sin(angle) * len);
    Point2d start = new Point2d(cen), end = new Point2d(cen);
    start.add(dir);
    end.sub(dir);
    ImagePlaneGen out = new ImagePlaneGen(this);
    out.a.set((float) start.x, a.y, (float) start.y);
    out.b.set((float) end.x, b.y, (float) end.y);
    out.calcUV();
    return out;
}
Also used : Line(org.twak.utils.Line) Vector2d(javax.vecmath.Vector2d) Point2d(javax.vecmath.Point2d)

Example 22 with Point2d

use of javax.vecmath.Point2d in project chordatlas by twak.

the class ImagePlaneGen method render.

public BufferedImage render(File folder, float scale, Pano pano, Line mega, String filename) {
    Point3d worldPos = new Point3d();
    Vector3d worldNormal = new Vector3d(), planeNormal = new Vector3d(Jme3z.from(b));
    planeNormal.sub(Jme3z.from(a));
    planeNormal.normalize();
    planeNormal.set(planeNormal.z, 0, -planeNormal.x);
    BufferedImage out = new BufferedImage((int) (a.distance(b) * scale), (int) ((heightMax - heightMin) * scale), BufferedImage.TYPE_3BYTE_BGR);
    BufferedImage outClip;
    if (DO_MASK)
        outClip = new BufferedImage(out.getWidth(), out.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
    BufferedImage source = pano.getRenderPano();
    for (int x = 0; x < out.getWidth(); x++) {
        for (int y = 0; y < out.getHeight(); y++) {
            float[] planeWorld = uvToWorld(x / (float) out.getWidth(), 1 - (y / (float) out.getHeight()));
            int c = pano.castTo(planeWorld, source, worldPos, worldNormal);
            out.setRGB(x, y, c);
            if (DO_MASK) {
                Color d = Color.white;
                if (Double.isNaN(worldPos.x) || (mega != null && mega.distance(new Point2d(worldPos.x, worldPos.z), true) > 5))
                    d = Color.black;
                outClip.setRGB(x, y, d.getRGB());
            }
        }
        if (x % 100 == 0)
            System.out.println(x + " / " + out.getWidth());
    }
    System.out.println("render complete!");
    if (folder != null)
        try {
            ImageIO.write(out, "png", new File(folder, filename + ".png"));
            if (DO_MASK)
                ImageIO.write(outClip, "png", new File(folder, "mask.png"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    return out;
}
Also used : Vector3d(javax.vecmath.Vector3d) Point2d(javax.vecmath.Point2d) Point3d(javax.vecmath.Point3d) Color(java.awt.Color) IOException(java.io.IOException) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Example 23 with Point2d

use of javax.vecmath.Point2d in project chordatlas by twak.

the class GBias method getAngle.

/**
 * local bias
 */
public Double getAngle(Line line, Point2d cen) {
    final double PI8 = Math.PI / 8;
    DRectangle r = new DRectangle(line);
    r.grow(expand);
    Collection<Longer> res = tree.queryRange(r);
    double bestScore = -Double.MAX_VALUE;
    Double bestAngle = null;
    for (AxisAlignedBoundingBox aabb : res) {
        Line gis = ((Longer) aabb).line;
        double len = gis.length();
        // gis.distance( line );
        double dist = gis.fromPPram(0.5).distance(line.fromPPram(0.5));
        if (dist < len * expand) {
            double angle = line.absAngle(gis);
            if (angle < PI8) {
                double score = (PI8 - angle) / dist;
                if (score > bestScore) {
                    bestAngle = gis.aTan2();
                    bestScore = score;
                }
            } else if (Mathz.inRangeTol(angle, Mathz.PI2, PI8)) {
                double score = 0.2 * (PI8 - angle) / dist;
                if (score > bestScore) {
                    gis = new Line(new Point2d(gis.start.y, -gis.start.x), new Point2d(gis.end.y, -gis.end.x));
                    if (gis.absAngle(line) > PI8)
                        gis = gis.reverse();
                    bestAngle = gis.aTan2();
                    bestScore = score;
                }
            }
        }
    }
    if (bestAngle != null)
        return bestAngle;
    else
        return null;
}
Also used : Line(org.twak.utils.Line) QLine(org.twak.viewTrace.QuadTree.QLine) AxisAlignedBoundingBox(org.twak.viewTrace.QuadTree.AxisAlignedBoundingBox) DRectangle(org.twak.utils.geom.DRectangle) Point2d(javax.vecmath.Point2d)

Example 24 with Point2d

use of javax.vecmath.Point2d in project chordatlas by twak.

the class Concarnie method apply.

private List<Problem> apply(Problem problem, int count) {
    if (depth > 10 || problem.soup.isEmpty()) {
        problem.addPortal();
        return Collections.emptyList();
    }
    Set<Line> portals = new HashSet<>();
    Set<Line> in = new HashSet<>(problem.soup);
    for (Line sl : problem.hull) {
        RangeMerge<Line> rm = new RangeMerge<>(P.CON_TOL, P.CON_TOL);
        LinearForm lf = new LinearForm(sl);
        for (Line l : problem.soup) {
            if (onHull(sl, l)) {
                double pps = lf.findPParam(l.start), ppe = lf.findPParam(l.end);
                rm.add(pps, ppe, l);
                in.remove(l);
            }
        }
        List<Double> rmGet = rm.get();
        if (rmGet.isEmpty()) {
            if (!sl.start.equals(sl.end)) {
                if (Double.isNaN(sl.start.x))
                    System.out.println("help!");
                // whole thing is portal
                portals.add(sl);
            }
        } else {
            List<Point2d> occupied = new ArrayList();
            {
                double lf1 = lf.findPParam(sl.start), lf2 = lf.findPParam(sl.end);
                if (lf1 > lf2) {
                    double tmp = lf1;
                    lf1 = lf2;
                    lf2 = tmp;
                }
                for (double d : rmGet) {
                    d = Mathz.clamp(d, lf1, lf2);
                    occupied.add(lf.fromPParam(d));
                }
            }
            boolean onHull = false;
            {
                Point2d snapE = occupied.get(0), snapS = occupied.get(occupied.size() - 1);
                if (snapS.distance(sl.start) < P.CON_TOL)
                    snapS.set(sl.start);
                else {
                    occupied.add(new Point2d(sl.start));
                }
                if (snapE.distance(sl.end) < P.CON_TOL)
                    snapE.set(sl.end);
                else {
                    occupied.add(0, new Point2d(sl.end));
                    onHull = true;
                }
            }
            for (Pair<Point2d, Point2d> pair : new ConsecutiveItPairs<Point2d>(occupied)) {
                onHull = !onHull;
                if (pair.first().equals(pair.second()))
                    continue;
                Line line = new Line(pair.first(), pair.second());
                if (onHull) {
                    if (depth % 2 == 0)
                        line = line.reverse();
                    graph.add(line);
                } else {
                    portals.add(line.reverse());
                }
            }
        }
    }
    if (in.size() == problem.soup.size()) {
        // we didn't do anything! remove something, anything...
        List<Line> d = new ArrayList(in);
        Collections.sort(d, (a, b) -> Double.compare(a.length(), b.length()));
        for (// remove the shortest 1/3 lines
        int i = 0; // remove the shortest 1/3 lines
        i < Math.max(1, in.size() * 0.33); // remove the shortest 1/3 lines
        i++) in.remove(d.get(i));
    }
    List<Portal> mergedPortals = mergeConsecutive(portals);
    // assign each member of in to a portal
    MultiMapSet<Portal, Line> subproblems = new MultiMapSet();
    if (!mergedPortals.isEmpty()) {
        // O(n^3) closest-clique assignment
        MultiMapSet<Portal, Line> sub2 = new MultiMapSet();
        for (Portal p : mergedPortals) sub2.putAll(p, p.lines);
        while (!in.isEmpty()) {
            double bestDist = Double.MAX_VALUE;
            Portal bestP = null;
            Line bestL = null;
            for (Line l : in) for (Portal p : sub2.keySet()) for (Line sl : sub2.get(p)) {
                double dlsl = l.distance(sl);
                if (// ignore lines a long way away
                dlsl > Math.max(P.CON_TOL * 3, p.length * 0.5))
                    continue;
                double dist = dlsl + 0.1 * l.distance(p.summary);
                if (dist < bestDist) {
                    bestP = p;
                    bestDist = dist;
                    bestL = l;
                }
            }
            if (bestL == null)
                break;
            in.remove(bestL);
            double lenBestL = bestL.length();
            if (lenBestL > P.CON_TOL && lenBestL > 0.5 * bestP.summary.length()) {
                in.add(new Line(bestL.start, bestL.fromPPram(0.5)));
                in.add(new Line(bestL.fromPPram(0.5), bestL.end));
            } else {
                subproblems.put(bestP, bestL);
                sub2.put(bestP, bestL);
            }
        }
    } else {
        mergedPortals.add(null);
        subproblems.putAll(null, in);
    }
    return mergedPortals.stream().map(x -> new Problem(x == null ? Collections.emptyList() : x.lines, subproblems.get(x))).collect(Collectors.toList());
}
Also used : Arrays(java.util.Arrays) Location(org.apache.commons.math3.geometry.partitioning.Region.Location) ConsecutivePairs(org.twak.utils.collections.ConsecutivePairs) Segment(org.apache.commons.math3.geometry.euclidean.twod.Segment) ConvexHull2D(org.apache.commons.math3.geometry.euclidean.twod.hull.ConvexHull2D) MultiMapSet(org.twak.utils.collections.MultiMapSet) HashMap(java.util.HashMap) Graph2D(org.twak.utils.geom.Graph2D) Pair(org.twak.utils.Pair) ConsecutiveItPairs(org.twak.utils.collections.ConsecutiveItPairs) Vector2D(org.apache.commons.math3.geometry.euclidean.twod.Vector2D) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Loop(org.twak.utils.collections.Loop) Map(java.util.Map) Mathz(org.twak.utils.Mathz) PaintThing(org.twak.utils.PaintThing) Euclidean2D(org.apache.commons.math3.geometry.euclidean.twod.Euclidean2D) MathIllegalArgumentException(org.apache.commons.math3.exception.MathIllegalArgumentException) LoopL(org.twak.utils.collections.LoopL) UnionWalker(org.twak.utils.geom.UnionWalker) Iterator(java.util.Iterator) Collection(java.util.Collection) Line(org.twak.utils.Line) Set(java.util.Set) MonotoneChain(org.apache.commons.math3.geometry.euclidean.twod.hull.MonotoneChain) Region(org.apache.commons.math3.geometry.partitioning.Region) Vector2d(javax.vecmath.Vector2d) LinearForm(org.twak.utils.geom.LinearForm) Collectors(java.util.stream.Collectors) Point2d(javax.vecmath.Point2d) List(java.util.List) Paint(java.awt.Paint) ItComb(org.twak.utils.collections.ItComb) Loopable(org.twak.utils.collections.Loopable) InsufficientDataException(org.apache.commons.math3.exception.InsufficientDataException) Anglez(org.twak.utils.geom.Anglez) Collections(java.util.Collections) ConsecutiveItPairs(org.twak.utils.collections.ConsecutiveItPairs) ArrayList(java.util.ArrayList) LinearForm(org.twak.utils.geom.LinearForm) Paint(java.awt.Paint) Line(org.twak.utils.Line) Point2d(javax.vecmath.Point2d) HashSet(java.util.HashSet) MultiMapSet(org.twak.utils.collections.MultiMapSet)

Example 25 with Point2d

use of javax.vecmath.Point2d in project chordatlas by twak.

the class Concarnie method project.

public static Point2d project(Line l, Point2d pt) {
    Vector2d v1 = new Vector2d(l.end);
    v1.sub(l.start);
    Vector2d v2 = new Vector2d(pt);
    v2.sub(l.start);
    double param = v2.dot(v1) / v1.length();
    if (param < 0 || param > v1.length())
        return null;
    v1.normalize();
    v1.scale(param);
    v1.add(l.start);
    return new Point2d(v1);
}
Also used : Vector2d(javax.vecmath.Vector2d) Point2d(javax.vecmath.Point2d)

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