/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package matlabjts;

import com.vividsolutions.jts.awt.ShapeWriter;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.CoordinateSequence;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.geom.impl.PackedCoordinateSequenceFactory;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.impl.CoordinateArraySequenceFactory;
import com.vividsolutions.jts.geom.util.PolygonExtracter;
import com.vividsolutions.jts.operation.union.CascadedPolygonUnion;
import java.awt.BasicStroke;
import java.awt.Color;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestUnionError {

    //private final static GeometryFactory gfact = new GeometryFactory();
    private static JPanel p;
    private final static ShapeWriter sw = new ShapeWriter();

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        double[] XY = {0.0100573988512497, 4.78384542541783, 0.0254790503435339, 4.7853680160678, 0.0257229583365779, 4.8203671661845, -0.0432753661791916, 4.82084801337078, -0.110460580710986, 4.81977663231897, -0.125859513580305, 4.81803925397127, -0.125615605587261, 4.78304010385457, 0.0100573988512497, 4.78384542541783};
        PackedCoordinateSequenceFactory pcsf = new PackedCoordinateSequenceFactory();
        CoordinateSequence g0PackedCoordinateSequence = pcsf.create(XY, 2);

        
        // PackedCoordinateSequence FAILS in SnapIfNeededOverlayOp -> 
        GeometryFactory pgfact = new GeometryFactory(pcsf);
        final Geometry g0 = pgfact.createPolygon(g0PackedCoordinateSequence.toCoordinateArray());

        
        // THIS CODE WORKS FINE I.E. NO PackedCoordinateSequence
        //GeometryFactory qgfact = new GeometryFactory();
        //final Geometry g0 = qgfact.createPolygon(g0PackedCoordinateSequence.toCoordinateArray());

        Coordinate[] g1Coord = new Coordinate[13];
        g1Coord[0] = new Coordinate(-0.0852823000943891, 4.820178142278531);
        g1Coord[1] = new Coordinate(-0.0432753661791916, 4.82084801337078);
        g1Coord[2] = new Coordinate(-0.034111552138555, 4.820784151034586);
        g1Coord[3] = new Coordinate(-0.0234947209409801, 4.82090988483611);
        g1Coord[4] = new Coordinate(0.0455036035747894, 4.82042903764982);
        g1Coord[5] = new Coordinate(0.0452596955817454, 4.78542988753313);
        g1Coord[6] = new Coordinate(0.0344169517745771, 4.78443089375864);
        g1Coord[7] = new Coordinate(-0.101266578492074, 4.78396502078076);
        g1Coord[8] = new Coordinate(-0.110216672717942, 4.78477748220227);
        g1Coord[9] = new Coordinate(-0.110460580710986, 4.81977663231897);
        g1Coord[10] = new Coordinate(-0.0909282486242289, 4.820088108149497);
        g1Coord[11] = new Coordinate(-0.090682719052808, 4.82011418579351);
        g1Coord[12] = new Coordinate(-0.0852823000943891, 4.820178142278531);

        CoordinateArraySequenceFactory casf = CoordinateArraySequenceFactory.instance();
        CoordinateSequence g1coordinateSequence = casf.create(g1Coord);
        GeometryFactory cgfact = new GeometryFactory();
        final Geometry g1 = cgfact.createPolygon(g1coordinateSequence);

        final Geometry gu = g0.union(g1);

        System.out.println(g0);
        System.out.println(g1);
        System.out.println(gu);

        p = new JPanel() {
            public void paintComponent(Graphics g) {

                Shape g0Shape = sw.toShape(g0);
                Shape g1Shape = sw.toShape(g1);
                Shape guShape = sw.toShape(gu);

                Rectangle2D bounds = guShape.getBounds2D();
                double ratioW = ((getWidth() - 200) / bounds.getWidth());
                double ratioH = ((getHeight() - 200) / bounds.getHeight());
//
//                double zoom = Math.min(ratioW, ratioH);
//
//                AffineTransform transforms[]
//                        = {
//                            AffineTransform.getTranslateInstance(getWidth(), getHeight()),
//                            AffineTransform.getScaleInstance(ratioW, ratioH)
////                            AffineTransform.getTranslateInstance(-bounds.getX(), -1.007*bounds.getY())
//                        };
//
//                AffineTransform tr = new AffineTransform();
//                for (int i = 0; i < transforms.length; ++i) {
//                    tr.concatenate(transforms[i]);
//                }
//
//                Shape g0ShapeAT = tr.createTransformedShape(g0Shape);
//                Shape g1ShapeAT = tr.createTransformedShape(g1Shape);
//                Shape guShapeAT = tr.createTransformedShape(guShape);

//                Rectangle2D boundsAT = guShapeAT.getBounds2D();
//                System.out.println(boundsAT);
                
                Graphics2D g2d = (Graphics2D) g;
                
                g2d.translate(bounds.getX()+100,bounds.getY()+(getHeight()-100));
                g2d.scale(ratioW,-ratioH);
                g2d.translate(-bounds.getX(),-bounds.getY());
                
                //g2d.translate(100, 100);
                g2d.setStroke(new BasicStroke(0.0005f));
                g2d.setColor(Color.red);
                g2d.fill(g0Shape);                    
                g2d.setColor(Color.CYAN.brighter());
                g2d.fill(g1Shape);                    
                g2d.setColor(Color.black);
                g2d.draw(guShape);

            }
        };

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(p);
        f.show();
        p.setSize(800, 800);
        f.pack();

    }

}
