Skip to content

Commit

Permalink
Some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
reportmill committed Nov 20, 2024
1 parent f0be4b0 commit b870b27
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 69 deletions.
47 changes: 28 additions & 19 deletions src/snap/geom/SegmentPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
public class SegmentPath extends Shape {

// The original shape
private Shape _shape;
private Shape _shape;

// The list of segments
private List<Segment> _segs = new ArrayList<>();
private List<Segment> _segs = new ArrayList<>();

/**
* Constructor.
Expand Down Expand Up @@ -84,7 +84,17 @@ public boolean contains(double aX, double aY)
}

/**
* Returns whether this SegmentPath contains given point.
* Returns whether this SegmentPath contains given segment end point.
*/
public boolean containsSegEnd(Segment aSeg)
{
double endX = aSeg.getX1();
double endY = aSeg.getY1();
return contains(endX, endY);
}

/**
* Returns whether this SegmentPath contains given segment mid point.
*/
public boolean containsSegMid(Segment aSeg)
{
Expand All @@ -98,21 +108,15 @@ public boolean containsSegMid(Segment aSeg)
*/
public boolean containsEndPoint(double x, double y)
{
for (Segment seg : _segs)
if (Segment.equals(seg.x1, x) && Segment.equals(seg.y1, y))
return true;
return false;
return ListUtils.hasMatch(_segs, seg -> Segment.equals(seg.x1, x) && Segment.equals(seg.y1, y));
}

/**
* Returns whether segment list contains segment (regardless of direction).
*/
public boolean hasSeg(Segment aSeg)
{
for (Segment seg : _segs)
if (seg.matches(aSeg))
return true;
return false;
return ListUtils.hasMatch(_segs, seg -> seg.matches(aSeg));
}

/**
Expand Down Expand Up @@ -170,8 +174,9 @@ public List <Segment> getSegmentsThatStartOrEndAtSegmentEndPoint(Segment aSeg)
*/
public boolean isSelfIntersecting()
{
// Iterate over all segments and return if intersects other segment
int segCount = getSegCount();

// Iterate over all segments and return if intersects other segment
for (int i = 0; i < segCount; i++) { Segment seg1 = getSeg(i);
for (int j = i + 1; j < segCount; j++) { Segment seg2 = getSeg(j);

Expand Down Expand Up @@ -268,10 +273,12 @@ public boolean splitIntersectingSegmentsAtIntersectionPoints()
*/
public void splitIntersectingSegmentsAtIntersectionPoints(SegmentPath aSegmentPath)
{
int segCount1 = getSegCount();
int setCount2 = aSegmentPath.getSegCount();

// Iterate over all segments and split at all intersections with other segment
int sc = getSegCount(), sc2 = aSegmentPath.getSegCount();
for (int i=0; i<sc; i++) { Segment seg1 = getSeg(i);
for (int j=0; j<sc2; j++) { Segment seg2 = aSegmentPath.getSeg(j);
for (int i = 0; i < segCount1; i++) { Segment seg1 = getSeg(i);
for (int j = 0; j < setCount2; j++) { Segment seg2 = aSegmentPath.getSeg(j);

// If segments intersect
if (seg1.intersectsSeg(seg2)) {
Expand All @@ -280,14 +287,16 @@ public void splitIntersectingSegmentsAtIntersectionPoints(SegmentPath aSegmentPa
double hp1 = seg1.getHitPoint(seg2);
if (hp1 > .001 && hp1 < .999) {
Segment tail = seg1.split(hp1);
addSeg(tail, i+1); sc++;
addSeg(tail, i + 1);
segCount1++;
}

// Find intersection point for seg2 and split/add if inside
double hp2 = seg2.getHitPoint(seg1);
if (hp2 > .001 && hp2 < .999) {
Segment tail = seg2.split(hp2);
aSegmentPath.addSeg(tail, j+1); sc2++;
aSegmentPath.addSeg(tail, j + 1);
setCount2++;
}
}
}
Expand Down Expand Up @@ -387,13 +396,13 @@ public Seg getNext(double[] coords)
Line line = (Line) seg;
if (_moveX == line.x1 && _moveY == line.y1)
return close();
return lineTo(_lineX =line.x1, _lineY =line.y1, coords);
return lineTo(_lineX = line.x1, _lineY = line.y1, coords);
}

// Handle Seg Quad
if (seg instanceof Quad) {
Quad quad = (Quad) seg;
return quadTo(quad.cpx, quad.cpy, _lineX =quad.x1, _lineY =quad.y1, coords);
return quadTo(quad.cpx, quad.cpy, _lineX = quad.x1, _lineY = quad.y1, coords);
}

// Handle Seg Cubic)
Expand Down
108 changes: 58 additions & 50 deletions src/snap/geom/SegmentPathCAG.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Copyright (c) 2010, ReportMill Software. All rights reserved.
*/
package snap.geom;
import snap.util.ListUtils;

import java.util.List;

/**
Expand Down Expand Up @@ -48,12 +50,12 @@ public static Shape addShapes(Shape aShape1, Shape aShape2)
shape3.addSeg(seg);

// Search SegmentPaths for next outside segment
Segment nextSeg = getNextSegOutside(shape1, shape2, shape3, seg);
Segment nextSeg = getNextSegOutside(shape1, seg, shape2, shape3);

// If not found, swap order to search second SegmentPath
if (nextSeg == null) {
SegmentPath swap = shape1; shape1 = shape2; shape2 = swap;
nextSeg = getNextSegOutside(shape1, shape2, shape3, seg);
nextSeg = getNextSegOutside(shape1, seg, shape2, shape3);
}

// Update seg and add to list if non-null
Expand Down Expand Up @@ -145,76 +147,73 @@ public static Shape intersectShapes(Shape aShape1, Shape aShape2)
return aShape1;

// Create SegmentPaths for given shapes and new shape
SegmentPath shape1 = new SegmentPath(aShape1);
SegmentPath shape2 = new SegmentPath(aShape2);
SegmentPath shape3 = new SegmentPath();
SegmentPath segPath1 = new SegmentPath(aShape1);
SegmentPath segPath2 = new SegmentPath(aShape2);
SegmentPath newPath = new SegmentPath();

// Split segments in shape1 & shape2
shape1.splitIntersectingSegmentsAtIntersectionPoints(shape2);
// Split segments in segPaths so all intersections are segment end points
segPath1.splitIntersectingSegmentsAtIntersectionPoints(segPath2);

// Find first segment contained by both shape1 and shape2
Segment seg = shape1.getFirstSegInside(shape2);
if (seg == null) { // Should never happen
System.err.println("SegmentPathCAG.intersectShapes: No points!"); return aShape1; }
// Find first segment contained by both segPaths
Segment loopSeg = segPath1.getFirstSegInside(segPath2);
if (loopSeg == null) { // Should never happen
System.err.println("SegmentPathCAG.intersectShapes: No points!");
return aShape1;
}

// Iterate over segments to find those with endpoints in opposing shape and add to new shape
SegmentPath owner = shape1;
SegmentPath opposingShape = shape2;
while (seg != null) {
// Loop vars
SegmentPath mainPath = segPath1;
SegmentPath otherPath = segPath2;
int maxSegments = segPath1.getSegCount() + segPath2.getSegCount() + 10;

// Add segment to new list
shape3.addSeg(seg);
// Iterate over segments to find those with endpoints in opposing shape and add to new shape
while (loopSeg != null) {

// Get segment at end point for current seg shape
List<Segment> segs = owner.getSegmentsThatStartOrEndAtSegmentEndPoint(seg);
Segment nextSeg = null;
for (Segment sg : segs) {
if (opposingShape.contains(sg.getX1(), sg.getY1()) && !shape3.hasSeg(sg)) {
nextSeg = sg; break; }
// Add segment to new path
newPath.addSeg(loopSeg);
if (newPath.getSegCount() > maxSegments) {
System.err.println("SegmentPathCAG: too many segs");
break;
}

// If not found, look for seg from other shape
// Get the next segment inside other path
Segment nextSeg = getNextSegInside(mainPath, loopSeg, otherPath, newPath);

// If not found, look for seg from other path (if found, swap paths)
if (nextSeg == null) {
segs = opposingShape.getSegmentsThatStartOrEndAtSegmentEndPoint(seg);
for (Segment sg : segs) {
if (owner.contains(sg.getX1(), sg.getY1()) && !shape3.hasSeg(sg)) {
nextSeg = sg;
owner = opposingShape; opposingShape = opposingShape==shape1? shape2 : shape1;
break;
}
}
nextSeg = getNextSegInside(otherPath, loopSeg, mainPath, newPath);
if (nextSeg != null) {
SegmentPath swap = mainPath; mainPath = otherPath; otherPath = swap; }
}

// Update seg and add to list if non-null
seg = nextSeg;

// Check to see if things are out of hand (should probably go)
if (shape3.getSegCount() > 30) {
seg = null; System.err.println("SegmentPathCAG: too many segs"); }
// Update seg
loopSeg = nextSeg;
}

// Return path for segments list
return new Path2D(shape3);
return new Path2D(newPath);
}

/**
* Returns the next segment outside of both SegmentPath.
* Returns the next segment outside other path (but not in new path).
*/
private static Segment getNextSegOutside(SegmentPath aShp1, SegmentPath aShp2, SegmentPath aShp3, Segment aSeg)
private static Segment getNextSegOutside(SegmentPath mainPath, Segment prevSeg, SegmentPath otherPath, SegmentPath newSegPath)
{
List <Segment> segs = aShp1.getSegmentsThatStartOrEndAtSegmentEndPoint(aSeg);
for (Segment seg : segs) {
boolean outside = !aShp2.containsSegMid(seg) || aShp2.hasSeg(seg);
if (outside && !aShp3.hasSeg(seg))
return seg;
}
List<Segment> segs = mainPath.getSegmentsThatStartOrEndAtSegmentEndPoint(prevSeg);
return ListUtils.findMatch(segs, seg -> isSegOutsideSegmentPaths(otherPath, seg, newSegPath));
}

// Return not found
return null;
/**
* Returns the next segment inside other path (but not in new path).
*/
private static Segment getNextSegInside(SegmentPath mainPath, Segment prevSeg, SegmentPath otherPath, SegmentPath newSegPath)
{
List<Segment> segs = mainPath.getSegmentsThatStartOrEndAtSegmentEndPoint(prevSeg);
return ListUtils.findMatch(segs, seg -> otherPath.containsSegEnd(seg) && !newSegPath.hasSeg(seg));
}

/**
* Returns the next segment outside of both SegmentPaths.
* Returns the next segment outside both SegmentPaths.
*/
private static Segment getNextSegSubtract(SegmentPath aRefShp, SegmentPath aShp1, SegmentPath aShp2, SegmentPath aShp3, Segment aSeg)
{
Expand All @@ -228,4 +227,13 @@ private static Segment getNextSegSubtract(SegmentPath aRefShp, SegmentPath aShp1
// Return not found
return null;
}

/**
* Returns whether segment is outside given SegmentPaths.
*/
private static boolean isSegOutsideSegmentPaths(SegmentPath segmentPath, Segment seg, SegmentPath newSegPath)
{
boolean outside = !segmentPath.containsSegMid(seg) || segmentPath.hasSeg(seg);
return outside && !newSegPath.hasSeg(seg);
}
}

0 comments on commit b870b27

Please sign in to comment.