Search in sources :

Example 1 with PanMouseAdaptor

use of org.twak.utils.PanMouseAdaptor in project chordatlas by twak.

the class SolverState method miniPainter.

public ICanPaint miniPainter() {
    return new ICanPaint() {

        @Override
        public void paint(Graphics2D g, PanMouseAdaptor ma) {
            // for ( MegaFeatures f : SS.minis.keySet() ) {
            // 
            // for ( MFPoint mfp : SS.minis.get( f ) ) {
            // Line l = mfp.image.mega.megafacade;
            // 
            // spreadImages( g, ma, l, mfp,  Listz.from( mfp.left, mfp.right ) );
            // 
            // }
            // }
            // if (SS.minis == null)
            int brake = 100;
            for (HalfFace f : mesh) {
                for (HalfEdge e : f) {
                    if (e.over != null)
                        continue;
                    if (brake-- < 0)
                        break;
                    SuperEdge se = (SuperEdge) e;
                    if (se.mini == null)
                        continue;
                    List<MiniFacade> mfs = new ArrayList(se.mini);
                    // while (mfs .size() < 2)
                    // mfs.add(null);
                    spreadImages(g, ma, se.line(), se.line().fromPPram(0.5), mfs);
                }
            }
            int i = 0;
            if (minis != null)
                for (MegaFeatures f : minis.keySet()) {
                    // PaintThing.paint (f.megafacade, g, ma);
                    DumbCluster1D<MFPoint> res = GurobiSkelSolver.clusterMinis(f, minis);
                    Vector2d dir = f.megafacade.dir();
                    Vector2d out = new Vector2d(dir);
                    out.set(-out.y, out.x);
                    out.scale(ma.toZoom(2) / out.length());
                    for (Cluster<MFPoint> c : res) {
                        g.setColor(Rainbow.getColour(i++));
                        for (MFPoint mfp : c.things) {
                            Point2d pt = new Point2d(mfp);
                            pt.add(out);
                            g.setStroke(new BasicStroke(0.2f));
                            PaintThing.paint(pt, g, ma);
                            g.setStroke(new BasicStroke(0.2f));
                            for (HalfEdge e : GurobiSkelSolver.findNear(f.megafacade, mfp, mesh)) g.drawLine(ma.toX(pt.x), ma.toY(pt.y), ma.toX(e.end.x), ma.toY(e.end.y));
                            if (mfp.selectedEdge != null) {
                                g.setStroke(new BasicStroke(2f));
                                g.drawLine(ma.toX(pt.x), ma.toY(pt.y), ma.toX(mfp.selectedEdge.end.x), ma.toY(mfp.selectedEdge.end.y));
                            }
                        }
                    }
                }
            g.setStroke(new BasicStroke(1));
        }

        private void spreadImages(Graphics2D g, PanMouseAdaptor ma, Line sel, Point2d cen, List<MiniFacade> mfs) {
            Vector2d perp = sel.dir();
            perp.set(-perp.y, perp.x);
            perp.normalize();
            for (int i = 0; i < mfs.size(); i++) {
                MiniFacade mf = mfs.get(i);
                Vector2d p2 = new Vector2d(perp);
                p2.scale((i + 1) * 10);
                p2.add(cen);
                if (mf == null) {
                    g.setColor(Color.black);
                    g.fillRect(ma.toX(p2.x - 1), ma.toY(p2.y - 3), ma.toZoom(2), ma.toZoom(6));
                    continue;
                }
                double w = mf.width * 0.1;
                double h = mf.height * 0.1;
                mf.paintImage(g, ma, p2.x - w, p2.y - h, p2.x + w, p2.y + h);
            }
        }
    };
}
Also used : BasicStroke(java.awt.BasicStroke) MiniFacade(org.twak.viewTrace.facades.MiniFacade) MegaFeatures(org.twak.tweed.gen.FeatureCache.MegaFeatures) ArrayList(java.util.ArrayList) Cluster(org.twak.utils.DumbCluster1D.Cluster) HalfEdge(org.twak.utils.geom.HalfMesh2.HalfEdge) HalfFace(org.twak.utils.geom.HalfMesh2.HalfFace) DumbCluster1D(org.twak.utils.DumbCluster1D) ICanPaint(org.twak.utils.PaintThing.ICanPaint) MFPoint(org.twak.tweed.gen.FeatureCache.MFPoint) ICanPaint(org.twak.utils.PaintThing.ICanPaint) Graphics2D(java.awt.Graphics2D) Line(org.twak.utils.Line) SuperLine(org.twak.viewTrace.SuperLine) Vector2d(javax.vecmath.Vector2d) Point2d(javax.vecmath.Point2d) PanMouseAdaptor(org.twak.utils.PanMouseAdaptor) MFPoint(org.twak.tweed.gen.FeatureCache.MFPoint) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with PanMouseAdaptor

use of org.twak.utils.PanMouseAdaptor in project chordatlas by twak.

the class SolverState method debugSolverResult.

public void debugSolverResult() {
    new Plot(mesh).add(miniPainter()).add(new ICanPaint() {

        @Override
        public void paint(Graphics2D g, PanMouseAdaptor ma) {
            Set<HalfEdge> seen = new HashSet<>();
            Color tWhite = new Color(255, 255, 255, 150);
            if (!seen.isEmpty())
                for (HalfFace f : mesh) {
                    for (HalfEdge e : f) {
                        int i = ((SuperEdge) e).profI;
                        // if (i == -1)
                        // continue;
                        Point2d loc = e.line().fromPPram(0.5);
                        String s = "" + i;
                        int offset = e.start.x < e.end.x ? -10 : 10;
                        seen.add(e);
                        Rectangle2D b = g.getFontMetrics().getStringBounds(s, g);
                        b = new Rectangle2D.Double(0, 0, b.getWidth() + 4, b.getHeight());
                        g.setColor(tWhite);
                        g.fillRect(ma.toX(loc.x) - (int) (b.getWidth() / 2), offset + ma.toY(loc.y) - (int) (b.getHeight() / 2), (int) (b.getWidth()), (int) (b.getHeight()));
                        g.setColor(Color.gray);
                        g.drawString(s, ma.toX(loc.x) - (int) (b.getWidth() / 2) + 2, offset + ma.toY(loc.y) + (int) (b.getHeight() / 2) - 3);
                    }
                }
        }
    });
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Plot(org.twak.utils.ui.Plot) Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) HalfEdge(org.twak.utils.geom.HalfMesh2.HalfEdge) HalfFace(org.twak.utils.geom.HalfMesh2.HalfFace) ICanPaint(org.twak.utils.PaintThing.ICanPaint) MFPoint(org.twak.tweed.gen.FeatureCache.MFPoint) ICanPaint(org.twak.utils.PaintThing.ICanPaint) Graphics2D(java.awt.Graphics2D) Point2d(javax.vecmath.Point2d) PanMouseAdaptor(org.twak.utils.PanMouseAdaptor)

Example 3 with PanMouseAdaptor

use of org.twak.utils.PanMouseAdaptor in project chordatlas by twak.

the class Slice method buildUI.

private void buildUI() {
    ma = new PanMouseAdaptor(this);
    ma.center(new Point2d((max[flatAxis[0]] - min[flatAxis[0]]) / 2 + min[flatAxis[0]], (max[flatAxis[1]] - min[flatAxis[1]]) / 2 + min[flatAxis[1]]));
    ma.setZoom(16);
    JFrame jf = new JFrame("slice");
    WindowManager.register(jf);
    jf.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
        // System.exit(0);
        }
    });
    jf.setLayout(new BorderLayout());
    this.setPreferredSize(new Dimension(600, 600));
    jf.add(this, BorderLayout.CENTER);
    final JSlider heightSlider = new JSlider(SwingConstants.VERTICAL, (int) (min[majorAxis] * sliderScale), (int) (max[majorAxis] * sliderScale), (int) ((max[majorAxis] - min[majorAxis]) * 0.5 + min[majorAxis]) * sliderScale);
    final JSlider lineSlider = new JSlider(SwingConstants.VERTICAL, -1, 50, -1);
    heightSlider.setPaintLabels(false);
    final ChangeListener cl;
    heightSlider.addChangeListener(cl = new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent arg0) {
            sliceHeight = heightSlider.getValue() / (double) sliderScale;
            LineSoup rawSoup = sliceMesh(rawMesh, sliceHeight);
            slice = sliceMesh(filteredMesh, sliceHeight);
            foundLines = new FindLines(slice, gisBias, lineSlider.getValue(), rawSoup.clique(P.CL_HIGH, P.CL_LOW), P);
            Concarnie cc = new Concarnie(foundLines.result, foundLines.cliques, P);
            // Concarnie cc = new Concarnie(slice, rawSoup.clique() );
            carnieSoup = cc.graph;
            carnie = cc.out;
            Slice.this.repaint();
        }
    });
    cl.stateChanged(null);
    lineSlider.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            cl.stateChanged(null);
        }
    });
    final JSlider parameterScaleSlider = new JSlider(SwingConstants.VERTICAL, 0, 2000, (int) (P.getScale() * 100));
    parameterScaleSlider.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            P.setScale(parameterScaleSlider.getValue() / 100.);
        }
    });
    JButton go = new JButton("support");
    go.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            setupObj(new ObjRead(toSlice));
        // supportPoints.clear();
        // Slice.this.repaint();
        // SwingUtilities.invokeLater(new Runnable() {
        // @Override
        // public void run() {
        // PerpSupport ps = new PerpSupport(slice, gis);
        // Slice.this.supportMax = ps.supportMax;
        // Slice.this.supportPoints = ps.supportPoints;
        // Slice.this.repaint();
        // }
        // });
        }
    });
    JButton fs = new JButton("full support");
    fs.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            Set<Point2d> points = new HashSet();
            for (double v = min[majorAxis]; v < max[majorAxis]; v += 0.01) {
                System.out.println("processing slice " + v + " / " + max[majorAxis]);
                slice = sliceMesh(filteredMesh, sliceHeight);
                for (SupportPoint sp : new PerpSupport(slice, gis).supportPoints) points.add(new Point2d(sp.support, max[majorAxis] - v));
            }
            System.out.println("found " + points.size() + " pts");
            new Plot(points);
        }
    });
    JButton ge = new JButton("lines");
    ge.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            foundLines = new FindLines(slice, gisBias, lineSlider.getValue(), slice.clique(P.CL_HIGH, P.CL_LOW), P);
            Slice.this.repaint();
        }
    });
    JButton cc = new JButton("carnie");
    cc.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            LineSoup rawSoup = sliceMesh(rawMesh, sliceHeight);
            foundLines = new FindLines(slice, gisBias, lineSlider.getValue(), rawSoup.clique(P.CL_HIGH, P.CL_LOW), P);
            Concarnie cc = new Concarnie(foundLines.result, foundLines.cliques, P);
            carnieSoup = cc.graph;
            carnie = cc.out;
            Slice.this.repaint();
        }
    });
    JButton ob = new JButton("dump");
    ob.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            ObjDump out = new ObjDump();
            double delta = 2.5;
            for (double d = min[majorAxis]; d <= max[majorAxis]; d += delta) {
                System.out.println(d + " / " + max[majorAxis]);
                boolean error;
                int count = 0;
                do {
                    error = false;
                    try {
                        LineSoup rawSoup = sliceMesh(rawMesh, d);
                        slice = sliceMesh(filteredMesh, d);
                        foundLines = new FindLines(slice, null, lineSlider.getValue(), rawSoup.clique(P.CL_HIGH, P.CL_LOW), P);
                        // foundLines = new FindLines(slice, gisBias, lineSlider.getValue(), rawSoup.clique(P.CL_HIGH, P.CL_LOW), P);
                        Concarnie c = new Concarnie(foundLines.result, foundLines.cliques, P);
                        // 
                        extrude(out, c.out, d, d + delta);
                        // capAtHeight (out, c.out, false, d );
                        capAtHeight(out, c.out, true, d + delta);
                    } catch (Throwable th) {
                        error = true;
                    }
                } while (error && count++ < 10);
            }
            out.dump(new File(Tweed.SCRATCH + "test2.obj"));
        }
    });
    JButton an = new JButton("anim");
    an.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            BufferedImage bi = new BufferedImage(Slice.this.getWidth(), Slice.this.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
            for (File f : new File(Tweed.SCRATCH + "vid").listFiles()) f.delete();
            int c = 0;
            for (int i = heightSlider.getMinimum(); i < heightSlider.getMaximum(); i += 100) {
                heightSlider.setValue(i);
                Slice.this.paintComponent(bi.getGraphics());
                try {
                    ImageIO.write(bi, "png", new File(String.format(Tweed.SCRATCH + "vid/%05d.png", c++)));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    JButton sl = new JButton("global");
    sl.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            new SliceSolver(new File(Tweed.SCRATCH + "test2.obj"), Slice.this, P);
        }
    });
    JButton pr = new JButton("profiles");
    pr.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            double delta = 0.5;
            BufferedImage out = new BufferedImage(Slice.this.getWidth(), Slice.this.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D g2 = (Graphics2D) out.getGraphics();
            g2.setColor(Color.white);
            g2.fillRect(0, 0, getWidth(), getHeight());
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(new Color(0, 0, 0, 50));
            for (double d = min[majorAxis]; d <= max[majorAxis]; d += delta) {
                System.out.println(d + " / " + max[majorAxis]);
                boolean error;
                int count = 0;
                do {
                    error = false;
                    try {
                        LineSoup rawSoup = sliceMesh(rawMesh, d);
                        slice = sliceMesh(filteredMesh, d);
                        FindLines fl = new FindLines(slice, gisBias, lineSlider.getValue(), rawSoup.clique(P.CL_HIGH, P.CL_LOW), P);
                        PaintThing.paint(fl.result, g2, ma);
                    // Concarnie c = new Concarnie(foundLines.result, foundLines.cliques, P);
                    } catch (Throwable th) {
                        error = true;
                    }
                } while (error && count++ < 10);
            }
            g2.dispose();
            try {
                ImageIO.write(out, "png", new File(Tweed.SCRATCH + "lines"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    final JCheckBox sg = new JCheckBox("gis", true);
    sg.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            showGis = sg.isSelected();
            Slice.this.repaint();
        }
    });
    final JCheckBox sf = new JCheckBox("fit", true);
    sf.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            showFit = sf.isSelected();
            Slice.this.repaint();
        }
    });
    final JCheckBox sc = new JCheckBox("poly-out", true);
    sc.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            showCarnie = sc.isSelected();
            Slice.this.repaint();
        }
    });
    final JCheckBox ss = new JCheckBox("slice", true);
    ss.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            showSlice = ss.isSelected();
            Slice.this.repaint();
        }
    });
    JPanel buttons = new JPanel(new ListDownLayout());
    buttons.add(go);
    buttons.add(fs);
    buttons.add(ge);
    buttons.add(cc);
    buttons.add(an);
    buttons.add(ob);
    buttons.add(sl);
    buttons.add(pr);
    buttons.add(sg);
    buttons.add(sf);
    buttons.add(sc);
    buttons.add(ss);
    buttons.add(lineSlider);
    JPanel controls = new JPanel(new BorderLayout());
    controls.add(heightSlider, BorderLayout.CENTER);
    controls.add(parameterScaleSlider, BorderLayout.WEST);
    controls.add(buttons, BorderLayout.EAST);
    jf.add(controls, BorderLayout.EAST);
    jf.pack();
    jf.setVisible(true);
}
Also used : JPanel(javax.swing.JPanel) ListDownLayout(org.twak.utils.ui.ListDownLayout) Set(java.util.Set) HashSet(java.util.HashSet) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) WindowAdapter(java.awt.event.WindowAdapter) ObjRead(org.twak.utils.geom.ObjRead) BufferedImage(java.awt.image.BufferedImage) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) ObjDump(org.twak.utils.geom.ObjDump) JSlider(javax.swing.JSlider) ChangeListener(javax.swing.event.ChangeListener) HashSet(java.util.HashSet) Plot(org.twak.utils.ui.Plot) Color(java.awt.Color) Dimension(java.awt.Dimension) IOException(java.io.IOException) Graphics2D(java.awt.Graphics2D) JCheckBox(javax.swing.JCheckBox) ChangeEvent(javax.swing.event.ChangeEvent) ActionListener(java.awt.event.ActionListener) Point2d(javax.vecmath.Point2d) PanMouseAdaptor(org.twak.utils.PanMouseAdaptor) WindowEvent(java.awt.event.WindowEvent) File(java.io.File)

Aggregations

Graphics2D (java.awt.Graphics2D)3 Point2d (javax.vecmath.Point2d)3 PanMouseAdaptor (org.twak.utils.PanMouseAdaptor)3 Color (java.awt.Color)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 MFPoint (org.twak.tweed.gen.FeatureCache.MFPoint)2 ICanPaint (org.twak.utils.PaintThing.ICanPaint)2 HalfEdge (org.twak.utils.geom.HalfMesh2.HalfEdge)2 HalfFace (org.twak.utils.geom.HalfMesh2.HalfFace)2 Plot (org.twak.utils.ui.Plot)2 BasicStroke (java.awt.BasicStroke)1 BorderLayout (java.awt.BorderLayout)1 Dimension (java.awt.Dimension)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 WindowAdapter (java.awt.event.WindowAdapter)1 WindowEvent (java.awt.event.WindowEvent)1 Rectangle2D (java.awt.geom.Rectangle2D)1 BufferedImage (java.awt.image.BufferedImage)1