/*
* 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 bs.bs2d.slicer;
import bs.bs2d.geom.JTSUtils;
import bs.bs2d.gui.BSConstants;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryCollection;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
/**
*
* @author Djen
*/
public class GCodeSnippet {
private final ArrayList<GCodeCommand> commands;
public GCodeSnippet(){
commands = new ArrayList<>();
}
public void append(GCodeCommand c){
commands.add(c);
}
/**
* The bounding box of this code snippet (in 2D) or null, if no moves are
* defined in this snippet.
* @return the bounding box
*/
public Rectangle2D getBounds(){
Rectangle2D bounds = null;
for (GCodeCommand c : commands) {
if(c.type == GCodeCommand.Type.MOVE){
if(bounds == null)
bounds = new Rectangle2D.Double(c.x, c.y, 0, 0);
else
bounds.add(c.x, c.y);
}
}
return bounds;
}
public Geometry getHull(){
ArrayList<Point> pList = new ArrayList<>(commands.size());
for (GCodeCommand c : commands) {
if(c.type == GCodeCommand.Type.MOVE)
pList.add(JTSUtils.createPoint2D(c.x, c.y));
}
GeometryCollection g = new GeometryCollection(pList.toArray(new Point[pList.size()]), JTSUtils.getFactory());
return g.convexHull();
}
/**
*
* @return a deep copy of this snippet
*/
public GCodeSnippet copy(){
GCodeSnippet snip = new GCodeSnippet();
for (GCodeCommand c : commands) {
snip.append(c.copy());
}
return snip;
}
}