Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.
44 changes: 22 additions & 22 deletions Clipper/src/de/lighti/clipper/Clipper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,52 @@
import de.lighti.clipper.Point.LongPoint;

public interface Clipper {
public enum ClipType {
enum ClipType {
INTERSECTION, UNION, DIFFERENCE, XOR
}

enum Direction {
RIGHT_TO_LEFT, LEFT_TO_RIGHT
};
}

public enum EndType {
enum EndType {
CLOSED_POLYGON, CLOSED_LINE, OPEN_BUTT, OPEN_SQUARE, OPEN_ROUND
};
}

public enum JoinType {
enum JoinType {
SQUARE, ROUND, MITER
};
}

public enum PolyFillType {
enum PolyFillType {
EVEN_ODD, NON_ZERO, POSITIVE, NEGATIVE
};
}

public enum PolyType {
enum PolyType {
SUBJECT, CLIP
};
}

public interface ZFillCallback {
void zFill( LongPoint bot1, LongPoint top1, LongPoint bot2, LongPoint top2, LongPoint pt );
};
interface ZFillCallback {
void zFill(LongPoint bot1, LongPoint top1, LongPoint bot2, LongPoint top2, LongPoint pt);
}

//InitOptions that can be passed to the constructor ...
public final static int REVERSE_SOLUTION = 1;
int REVERSE_SOLUTION = 1;

public final static int STRICTLY_SIMPLE = 2;
int STRICTLY_SIMPLE = 2;

public final static int PRESERVE_COLINEAR = 4;
int PRESERVE_COLINEAR = 4;

boolean addPath( Path pg, PolyType polyType, boolean Closed );
boolean addPath(Path pg, PolyType polyType, boolean Closed);

boolean addPaths( Paths ppg, PolyType polyType, boolean closed );
boolean addPaths(Paths ppg, PolyType polyType, boolean closed);

void clear();

boolean execute( ClipType clipType, Paths solution );
boolean execute(ClipType clipType, Paths solution);

boolean execute( ClipType clipType, Paths solution, PolyFillType subjFillType, PolyFillType clipFillType );
boolean execute(ClipType clipType, Paths solution, PolyFillType subjFillType, PolyFillType clipFillType);

boolean execute( ClipType clipType, PolyTree polytree );
boolean execute(ClipType clipType, PolyTree polytree);

public boolean execute( ClipType clipType, PolyTree polytree, PolyFillType subjFillType, PolyFillType clipFillType );
boolean execute(ClipType clipType, PolyTree polytree, PolyFillType subjFillType, PolyFillType clipFillType);
}
36 changes: 15 additions & 21 deletions Clipper/src/de/lighti/clipper/ClipperBase.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package de.lighti.clipper;

import de.lighti.clipper.Path.OutRec;
import de.lighti.clipper.Point.LongPoint;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import de.lighti.clipper.Path.OutRec;
import de.lighti.clipper.Point.LongPoint;

public abstract class ClipperBase implements Clipper {
protected class LocalMinima {
long y;
Expand All @@ -26,14 +26,14 @@ protected class Maxima {
Maxima prev;
}

private static void initEdge( Edge e, Edge eNext, Edge ePrev, LongPoint pt ) {
private static void initEdge(Edge e, Edge eNext, Edge ePrev, LongPoint pt ) {
e.next = eNext;
e.prev = ePrev;
e.setCurrent( new LongPoint( pt ) );
e.outIdx = Edge.UNASSIGNED;
}

private static void initEdge2( Edge e, PolyType polyType ) {
private static void initEdge2(Edge e, PolyType polyType ) {
if (e.getCurrent().getY() >= e.next.getCurrent().getY()) {
e.setBot( new LongPoint( e.getCurrent() ) );
e.setTop( new LongPoint( e.next.getCurrent() ) );
Expand All @@ -55,7 +55,7 @@ private static void rangeTest( LongPoint Pt ) {
}
}

private static Edge removeEdge( Edge e ) {
private static Edge removeEdge(Edge e ) {
//removes e from double_linked_list (but without removing from memory)
e.prev.next = e.next;
e.next.prev = e.prev;
Expand All @@ -72,11 +72,9 @@ private static Edge removeEdge( Edge e ) {

protected LocalMinima currentLM;

private final List<List<Edge>> edges = new ArrayList<List<Edge>>();

protected Scanbeam scanbeam;

protected final List<OutRec> polyOuts = new ArrayList<OutRec>();
protected final List<OutRec> polyOuts = new ArrayList<>();

protected Edge activeEdges;

Expand All @@ -95,7 +93,7 @@ protected ClipperBase( boolean preserveCollinear ) //constructor (nb: no externa
}

@Override
public boolean addPath( Path pg, PolyType polyType, boolean Closed ) {
public boolean addPath(Path pg, PolyType polyType, boolean Closed ) {

if (!Closed && polyType == PolyType.CLIP) {
throw new IllegalStateException( "AddPath: Open paths must be subject." );
Expand All @@ -115,7 +113,7 @@ public boolean addPath( Path pg, PolyType polyType, boolean Closed ) {
}

//create a new edge array ...
final List<Edge> edges = new ArrayList<Edge>( highI + 1 );
final List<Edge> edges = new ArrayList<>( highI + 1 );
for (int i = 0; i <= highI; i++) {
edges.add( new Edge() );
}
Expand Down Expand Up @@ -217,11 +215,9 @@ else if (Closed && Point.slopesEqual( e.prev.getCurrent(), e.getCurrent(), e.nex
e = e.next;
}
insertLocalMinima( locMin );
this.edges.add( edges );
return true;
}

this.edges.add( edges );
boolean leftBoundIsForward;
Edge EMin = null;

Expand Down Expand Up @@ -295,10 +291,10 @@ else if (locMin.rightBound.outIdx == Edge.SKIP) {
}

@Override
public boolean addPaths( Paths ppg, PolyType polyType, boolean closed ) {
public boolean addPaths(Paths paths, PolyType polyType, boolean closed ) {
boolean result = false;
for (int i = 0; i < ppg.size(); ++i) {
if (addPath( ppg.get( i ), polyType, closed )) {
for (Path path : paths) {
if (addPath(path, polyType, closed)) {
result = true;
}
}
Expand All @@ -308,7 +304,6 @@ public boolean addPaths( Paths ppg, PolyType polyType, boolean closed ) {
@Override
public void clear() {
disposeLocalMinimaList();
edges.clear();
hasOpenPaths = false;
}

Expand Down Expand Up @@ -338,8 +333,7 @@ else if (newLm.y >= minimaList.y) {
tmpLm.next = newLm;
}
}

public boolean isPreserveCollinear() {
private boolean isPreserveCollinear() {
return preserveCollinear;
}

Expand All @@ -353,7 +347,7 @@ protected boolean popLocalMinima( long y, LocalMinima[] current ) {
return false;
}

private Edge processBound( Edge e, boolean LeftBoundIsForward ) {
private Edge processBound(Edge e, boolean LeftBoundIsForward ) {
Edge EStart, result = e;
Edge Horz;

Expand Down Expand Up @@ -605,7 +599,7 @@ protected void updateEdgeIntoAEL( Edge e ) {
}
}

protected void swapPositionsInAEL( Edge edge1, Edge edge2 ) {
protected void swapPositionsInAEL(Edge edge1, Edge edge2 ) {
LOGGER.entering( ClipperBase.class.getName(), "swapPositionsInAEL" );

//check that one or other edge hasn't already been removed from AEL ...
Expand Down
54 changes: 25 additions & 29 deletions Clipper/src/de/lighti/clipper/ClipperOffset.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package de.lighti.clipper;

import de.lighti.clipper.Clipper.*;
import de.lighti.clipper.Point.DoublePoint;
import de.lighti.clipper.Point.LongPoint;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import de.lighti.clipper.Clipper.ClipType;
import de.lighti.clipper.Clipper.EndType;
import de.lighti.clipper.Clipper.JoinType;
import de.lighti.clipper.Clipper.PolyFillType;
import de.lighti.clipper.Clipper.PolyType;
import de.lighti.clipper.Point.DoublePoint;
import de.lighti.clipper.Point.LongPoint;

public class ClipperOffset {
private static boolean nearZero( double val ) {
return val > -TOLERANCE && val < TOLERANCE;
Expand Down Expand Up @@ -45,12 +41,12 @@ public ClipperOffset( double miterLimit, double arcTolerance ) {
this.miterLimit = miterLimit;
this.arcTolerance = arcTolerance;
lowest = new LongPoint();
lowest.setX( -1l );
lowest.setX( -1L );
polyNodes = new PolyNode();
normals = new ArrayList<DoublePoint>();
normals = new ArrayList<>();
}

public void addPath( Path path, JoinType joinType, EndType endType ) {
public void addPath(Path path, JoinType joinType, EndType endType ) {
int highI = path.size() - 1;
if (highI < 0) {
return;
Expand Down Expand Up @@ -100,20 +96,20 @@ public void addPath( Path path, JoinType joinType, EndType endType ) {
}
}

public void addPaths( Paths paths, JoinType joinType, EndType endType ) {
public void addPaths(Paths paths, JoinType joinType, EndType endType ) {
for (final Path p : paths) {
addPath( p, joinType, endType );
}
}

public void clear() {
polyNodes.getChilds().clear();
lowest.setX( -1l );
lowest.setX( -1L );
}

private void doMiter( int j, int k, double r ) {
final double q = delta / r;
destPoly.add( new LongPoint( (int) Math.round( srcPoly.get( j ).getX() + (normals.get( k ).getX() + normals.get( j ).getX()) * q ), (int) Math
destPoly.add( new LongPoint( Math.round( srcPoly.get( j ).getX() + (normals.get( k ).getX() + normals.get( j ).getX()) * q ), Math
.round( srcPoly.get( j ).getY() + (normals.get( k ).getY() + normals.get( j ).getY()) * q ) ) );
}

Expand Down Expand Up @@ -175,7 +171,7 @@ else if (arcTolerance > Math.abs( delta ) * DEFAULT_ARC_TOLERANCE) {
if (node.getJoinType() == JoinType.ROUND) {
double X = 1.0, Y = 0.0;
for (int j = 1; j <= steps; j++) {
destPoly.add( new LongPoint( (int) Math.round( srcPoly.get( 0 ).getX() + X * delta ), (int) Math.round( srcPoly.get( 0 ).getY() + Y
destPoly.add( new LongPoint( Math.round( srcPoly.get( 0 ).getX() + X * delta ), Math.round( srcPoly.get( 0 ).getY() + Y
* delta ) ) );
final double X2 = X;
X = X * cos - sin * Y;
Expand All @@ -185,7 +181,7 @@ else if (arcTolerance > Math.abs( delta ) * DEFAULT_ARC_TOLERANCE) {
else {
double X = -1.0, Y = -1.0;
for (int j = 0; j < 4; ++j) {
destPoly.add( new LongPoint( (int) Math.round( srcPoly.get( 0 ).getX() + X * delta ), (int) Math.round( srcPoly.get( 0 ).getY() + Y
destPoly.add( new LongPoint( Math.round( srcPoly.get( 0 ).getX() + X * delta ), Math.round( srcPoly.get( 0 ).getY() + Y
* delta ) ) );
if (X < 0) {
X = 1;
Expand Down Expand Up @@ -249,10 +245,10 @@ else if (node.getEndType() == EndType.CLOSED_LINE) {
LongPoint pt1;
if (node.getEndType() == EndType.OPEN_BUTT) {
final int j = len - 1;
pt1 = new LongPoint( (int) Math.round( srcPoly.get( j ).getX() + normals.get( j ).getX() * delta ), (int) Math.round( srcPoly.get( j )
pt1 = new LongPoint( Math.round( srcPoly.get( j ).getX() + normals.get( j ).getX() * delta ), Math.round( srcPoly.get( j )
.getY() + normals.get( j ).getY() * delta ), 0 );
destPoly.add( pt1 );
pt1 = new LongPoint( (int) Math.round( srcPoly.get( j ).getX() - normals.get( j ).getX() * delta ), (int) Math.round( srcPoly.get( j )
pt1 = new LongPoint( Math.round( srcPoly.get( j ).getX() - normals.get( j ).getX() * delta ), Math.round( srcPoly.get( j )
.getY() - normals.get( j ).getY() * delta ), 0 );
destPoly.add( pt1 );
}
Expand Down Expand Up @@ -282,10 +278,10 @@ else if (node.getEndType() == EndType.CLOSED_LINE) {
}

if (node.getEndType() == EndType.OPEN_BUTT) {
pt1 = new LongPoint( (int) Math.round( srcPoly.get( 0 ).getX() - normals.get( 0 ).getX() * delta ), (int) Math.round( srcPoly.get( 0 )
pt1 = new LongPoint( Math.round( srcPoly.get( 0 ).getX() - normals.get( 0 ).getX() * delta ), Math.round( srcPoly.get( 0 )
.getY() - normals.get( 0 ).getY() * delta ) );
destPoly.add( pt1 );
pt1 = new LongPoint( (int) Math.round( srcPoly.get( 0 ).getX() + normals.get( 0 ).getX() * delta ), (int) Math.round( srcPoly.get( 0 )
pt1 = new LongPoint( Math.round( srcPoly.get( 0 ).getX() + normals.get( 0 ).getX() * delta ), Math.round( srcPoly.get( 0 )
.getY() + normals.get( 0 ).getY() * delta ) );
destPoly.add( pt1 );
}
Expand All @@ -310,12 +306,12 @@ private void doRound( int j, int k ) {

double X = normals.get( k ).getX(), Y = normals.get( k ).getY(), X2;
for (int i = 0; i < steps; ++i) {
destPoly.add( new LongPoint( (int) Math.round( srcPoly.get( j ).getX() + X * delta ), (int) Math.round( srcPoly.get( j ).getY() + Y * delta ) ) );
destPoly.add( new LongPoint( Math.round( srcPoly.get( j ).getX() + X * delta ), Math.round( srcPoly.get( j ).getY() + Y * delta ) ) );
X2 = X;
X = X * cos - sin * Y;
Y = X2 * sin + Y * cos;
}
destPoly.add( new LongPoint( (int) Math.round( srcPoly.get( j ).getX() + normals.get( j ).getX() * delta ), (int) Math.round( srcPoly.get( j ).getY()
destPoly.add( new LongPoint( Math.round( srcPoly.get( j ).getX() + normals.get( j ).getX() * delta ), Math.round( srcPoly.get( j ).getY()
+ normals.get( j ).getY() * delta ) ) );
}

Expand All @@ -327,13 +323,13 @@ private void doSquare( int j, int k ) {
final double sjx = srcPoly.get( j ).getX();
final double sjy = srcPoly.get( j ).getY();
final double dx = Math.tan( Math.atan2( inA, nkx * njx + nky * njy ) / 4 );
destPoly.add( new LongPoint( (int) Math.round( sjx + delta * (nkx - nky * dx) ), (int) Math.round( sjy + delta * (nky + nkx * dx) ), 0 ) );
destPoly.add( new LongPoint( (int) Math.round( sjx + delta * (njx + njy * dx) ), (int) Math.round( sjy + delta * (njy - njx * dx) ), 0 ) );
destPoly.add( new LongPoint( Math.round( sjx + delta * (nkx - nky * dx) ), Math.round( sjy + delta * (nky + nkx * dx) ), 0 ) );
destPoly.add( new LongPoint( Math.round( sjx + delta * (njx + njy * dx) ), Math.round( sjy + delta * (njy - njx * dx) ), 0 ) );
}

//------------------------------------------------------------------------------

public void execute( Paths solution, double delta ) {
public void execute(Paths solution, double delta ) {
solution.clear();
fixOrientations();
doOffset( delta );
Expand Down Expand Up @@ -363,7 +359,7 @@ public void execute( Paths solution, double delta ) {

//------------------------------------------------------------------------------

public void execute( PolyTree solution, double delta ) {
public void execute(PolyTree solution, double delta ) {
solution.Clear();
fixOrientations();
doOffset( delta );
Expand Down Expand Up @@ -442,7 +438,7 @@ private void offsetPoint( int j, int[] kV, JoinType jointype ) {
final double cosA = nkx * njx + njy * nky;
if (cosA > 0) // angle ==> 0 degrees
{
destPoly.add( new LongPoint( (int) Math.round( sjx + nkx * delta ), (int) Math.round( sjy + nky * delta ), 0 ) );
destPoly.add( new LongPoint( Math.round( sjx + nkx * delta ), Math.round( sjy + nky * delta ), 0 ) );
return;
}
//else angle ==> 180 degrees
Expand All @@ -455,9 +451,9 @@ else if (inA < -1.0) {
}

if (inA * delta < 0) {
destPoly.add( new LongPoint( (int) Math.round( sjx + nkx * delta ), (int) Math.round( sjy + nky * delta ) ) );
destPoly.add( new LongPoint( Math.round( sjx + nkx * delta ), Math.round( sjy + nky * delta ) ) );
destPoly.add( srcPoly.get( j ) );
destPoly.add( new LongPoint( (int) Math.round( sjx + njx * delta ), (int) Math.round( sjy + njy * delta ) ) );
destPoly.add( new LongPoint( Math.round( sjx + njx * delta ), Math.round( sjy + njy * delta ) ) );
}
else {
switch (jointype) {
Expand Down
Loading