Class PolygonsSet
- java.lang.Object
-
- org.apache.commons.math3.geometry.partitioning.AbstractRegion<Euclidean2D,Euclidean1D>
-
- org.apache.commons.math3.geometry.euclidean.twod.PolygonsSet
-
- All Implemented Interfaces:
Region<Euclidean2D>
public class PolygonsSet extends AbstractRegion<Euclidean2D,Euclidean1D>
This class represents a 2D region: a set of polygons.- Since:
- 3.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classPolygonsSet.ConnectableSegmentPrivate extension of Segment allowing connection.private static classPolygonsSet.EdgeInternal class for holding edges while they are processed to build a BSP tree.private static classPolygonsSet.SegmentsBuilderVisitor building segments.private static classPolygonsSet.VertexInternal class for holding vertices while they are processed to build a BSP tree.-
Nested classes/interfaces inherited from interface org.apache.commons.math3.geometry.partitioning.Region
Region.Location
-
-
Field Summary
Fields Modifier and Type Field Description private static doubleDEFAULT_TOLERANCEDefault value for tolerance.private Vector2D[][]verticesVertices organized as boundary loops.
-
Constructor Summary
Constructors Constructor Description PolygonsSet()Deprecated.as of 3.3, replaced withPolygonsSet(double)PolygonsSet(double tolerance)Build a polygons set representing the whole plane.PolygonsSet(double xMin, double xMax, double yMin, double yMax)Deprecated.as of 3.3, replaced withPolygonsSet(double, double, double, double, double)PolygonsSet(double xMin, double xMax, double yMin, double yMax, double tolerance)Build a parallellepipedic box.PolygonsSet(double hyperplaneThickness, Vector2D... vertices)Build a polygon from a simple list of vertices.PolygonsSet(java.util.Collection<SubHyperplane<Euclidean2D>> boundary)Deprecated.as of 3.3, replaced withPolygonsSet(Collection, double)PolygonsSet(java.util.Collection<SubHyperplane<Euclidean2D>> boundary, double tolerance)Build a polygons set from a Boundary REPresentation (B-rep).PolygonsSet(BSPTree<Euclidean2D> tree)Deprecated.as of 3.3, replaced withPolygonsSet(BSPTree, double)PolygonsSet(BSPTree<Euclidean2D> tree, double tolerance)Build a polygons set from a BSP tree.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static Line[]boxBoundary(double xMin, double xMax, double yMin, double yMax, double tolerance)Create a list of hyperplanes representing the boundary of a box.PolygonsSetbuildNew(BSPTree<Euclidean2D> tree)Build a region using the instance as a prototype.private intcloseVerticesConnections(java.util.List<PolygonsSet.ConnectableSegment> segments)Connect the segments using Euclidean distance.protected voidcomputeGeometricalProperties()Compute some geometrical properties.private voidfilterSpuriousVertices(java.util.List<Segment> loop)Filter out spurious vertices on straight lines (at machine precision).private java.util.List<Segment>followLoop(PolygonsSet.ConnectableSegment defining)Build the loop containing a segment.private PolygonsSet.ConnectableSegmentgetUnprocessed(java.util.List<PolygonsSet.ConnectableSegment> segments)Get first unprocessed segment from a list.Vector2D[][]getVertices()Get the vertices of the polygon.private static voidinsertEdges(double hyperplaneThickness, BSPTree<Euclidean2D> node, java.util.List<PolygonsSet.Edge> edges)Recursively build a tree by inserting cut sub-hyperplanes.private intnaturalFollowerConnections(java.util.List<PolygonsSet.ConnectableSegment> segments)Connect the segments using only natural follower information.private intsplitEdgeConnections(java.util.List<PolygonsSet.ConnectableSegment> segments)Connect the segments resulting from a line splitting a straight edge.private static BSPTree<Euclidean2D>verticesToTree(double hyperplaneThickness, Vector2D... vertices)Build the BSP tree of a polygons set from a simple list of vertices.-
Methods inherited from class org.apache.commons.math3.geometry.partitioning.AbstractRegion
applyTransform, checkPoint, checkPoint, checkPoint, checkPoint, contains, copySelf, getBarycenter, getBoundarySize, getSize, getTolerance, getTree, intersection, isEmpty, isEmpty, isFull, isFull, projectToBoundary, setBarycenter, setBarycenter, setSize, side
-
-
-
-
Field Detail
-
DEFAULT_TOLERANCE
private static final double DEFAULT_TOLERANCE
Default value for tolerance.- See Also:
- Constant Field Values
-
vertices
private Vector2D[][] vertices
Vertices organized as boundary loops.
-
-
Constructor Detail
-
PolygonsSet
public PolygonsSet(double tolerance)
Build a polygons set representing the whole plane.- Parameters:
tolerance- tolerance below which points are considered identical- Since:
- 3.3
-
PolygonsSet
public PolygonsSet(BSPTree<Euclidean2D> tree, double tolerance)
Build a polygons set from a BSP tree.The leaf nodes of the BSP tree must have a
Booleanattribute representing the inside status of the corresponding cell (true for inside cells, false for outside cells). In order to avoid building too many small objects, it is recommended to use the predefined constantsBoolean.TRUEandBoolean.FALSEThis constructor is aimed at expert use, as building the tree may be a difficult task. It is not intended for general use and for performances reasons does not check thoroughly its input, as this would require walking the full tree each time. Failing to provide a tree with the proper attributes, will therefore generate problems like
NullPointerExceptionorClassCastExceptiononly later on. This limitation is known and explains why this constructor is for expert use only. The caller does have the responsibility to provided correct arguments.- Parameters:
tree- inside/outside BSP tree representing the regiontolerance- tolerance below which points are considered identical- Since:
- 3.3
-
PolygonsSet
public PolygonsSet(java.util.Collection<SubHyperplane<Euclidean2D>> boundary, double tolerance)
Build a polygons set from a Boundary REPresentation (B-rep).The boundary is provided as a collection of
sub-hyperplanes. Each sub-hyperplane has the interior part of the region on its minus side and the exterior on its plus side.The boundary elements can be in any order, and can form several non-connected sets (like for example polygons with holes or a set of disjoint polygons considered as a whole). In fact, the elements do not even need to be connected together (their topological connections are not used here). However, if the boundary does not really separate an inside open from an outside open (open having here its topological meaning), then subsequent calls to the
checkPointmethod will not be meaningful anymore.If the boundary is empty, the region will represent the whole space.
- Parameters:
boundary- collection of boundary elements, as a collection ofSubHyperplaneobjectstolerance- tolerance below which points are considered identical- Since:
- 3.3
-
PolygonsSet
public PolygonsSet(double xMin, double xMax, double yMin, double yMax, double tolerance)Build a parallellepipedic box.- Parameters:
xMin- low bound along the x directionxMax- high bound along the x directionyMin- low bound along the y directionyMax- high bound along the y directiontolerance- tolerance below which points are considered identical- Since:
- 3.3
-
PolygonsSet
public PolygonsSet(double hyperplaneThickness, Vector2D... vertices)Build a polygon from a simple list of vertices.The boundary is provided as a list of points considering to represent the vertices of a simple loop. The interior part of the region is on the left side of this path and the exterior is on its right side.
This constructor does not handle polygons with a boundary forming several disconnected paths (such as polygons with holes).
For cases where this simple constructor applies, it is expected to be numerically more robust than the
general constructorusingsubhyperplanes.If the list is empty, the region will represent the whole space.
Polygons with thin pikes or dents are inherently difficult to handle because they involve lines with almost opposite directions at some vertices. Polygons whose vertices come from some physical measurement with noise are also difficult because an edge that should be straight may be broken in lots of different pieces with almost equal directions. In both cases, computing the lines intersections is not numerically robust due to the almost 0 or almost π angle. Such cases need to carefully adjust the
hyperplaneThicknessparameter. A too small value would often lead to completely wrong polygons with large area wrongly identified as inside or outside. Large values are often much safer. As a rule of thumb, a value slightly below the size of the most accurate detail needed is a good value for thehyperplaneThicknessparameter.- Parameters:
hyperplaneThickness- tolerance below which points are considered to belong to the hyperplane (which is therefore more a slab)vertices- vertices of the simple loop boundary
-
PolygonsSet
@Deprecated public PolygonsSet()
Deprecated.as of 3.3, replaced withPolygonsSet(double)Build a polygons set representing the whole real line.
-
PolygonsSet
@Deprecated public PolygonsSet(BSPTree<Euclidean2D> tree)
Deprecated.as of 3.3, replaced withPolygonsSet(BSPTree, double)Build a polygons set from a BSP tree.The leaf nodes of the BSP tree must have a
Booleanattribute representing the inside status of the corresponding cell (true for inside cells, false for outside cells). In order to avoid building too many small objects, it is recommended to use the predefined constantsBoolean.TRUEandBoolean.FALSE- Parameters:
tree- inside/outside BSP tree representing the region
-
PolygonsSet
@Deprecated public PolygonsSet(java.util.Collection<SubHyperplane<Euclidean2D>> boundary)
Deprecated.as of 3.3, replaced withPolygonsSet(Collection, double)Build a polygons set from a Boundary REPresentation (B-rep).The boundary is provided as a collection of
sub-hyperplanes. Each sub-hyperplane has the interior part of the region on its minus side and the exterior on its plus side.The boundary elements can be in any order, and can form several non-connected sets (like for example polygons with holes or a set of disjoint polygons considered as a whole). In fact, the elements do not even need to be connected together (their topological connections are not used here). However, if the boundary does not really separate an inside open from an outside open (open having here its topological meaning), then subsequent calls to the
checkPointmethod will not be meaningful anymore.If the boundary is empty, the region will represent the whole space.
- Parameters:
boundary- collection of boundary elements, as a collection ofSubHyperplaneobjects
-
PolygonsSet
@Deprecated public PolygonsSet(double xMin, double xMax, double yMin, double yMax)Deprecated.as of 3.3, replaced withPolygonsSet(double, double, double, double, double)Build a parallellepipedic box.- Parameters:
xMin- low bound along the x directionxMax- high bound along the x directionyMin- low bound along the y directionyMax- high bound along the y direction
-
-
Method Detail
-
boxBoundary
private static Line[] boxBoundary(double xMin, double xMax, double yMin, double yMax, double tolerance)
Create a list of hyperplanes representing the boundary of a box.- Parameters:
xMin- low bound along the x directionxMax- high bound along the x directionyMin- low bound along the y directionyMax- high bound along the y directiontolerance- tolerance below which points are considered identical- Returns:
- boundary of the box
-
verticesToTree
private static BSPTree<Euclidean2D> verticesToTree(double hyperplaneThickness, Vector2D... vertices)
Build the BSP tree of a polygons set from a simple list of vertices.The boundary is provided as a list of points considering to represent the vertices of a simple loop. The interior part of the region is on the left side of this path and the exterior is on its right side.
This constructor does not handle polygons with a boundary forming several disconnected paths (such as polygons with holes).
For cases where this simple constructor applies, it is expected to be numerically more robust than the
general constructorusingsubhyperplanes.- Parameters:
hyperplaneThickness- tolerance below which points are consider to belong to the hyperplane (which is therefore more a slab)vertices- vertices of the simple loop boundary- Returns:
- the BSP tree of the input vertices
-
insertEdges
private static void insertEdges(double hyperplaneThickness, BSPTree<Euclidean2D> node, java.util.List<PolygonsSet.Edge> edges)Recursively build a tree by inserting cut sub-hyperplanes.- Parameters:
hyperplaneThickness- tolerance below which points are consider to belong to the hyperplane (which is therefore more a slab)node- current tree node (it is a leaf node at the beginning of the call)edges- list of edges to insert in the cell defined by this node (excluding edges not belonging to the cell defined by this node)
-
buildNew
public PolygonsSet buildNew(BSPTree<Euclidean2D> tree)
Build a region using the instance as a prototype.This method allow to create new instances without knowing exactly the type of the region. It is an application of the prototype design pattern.
The leaf nodes of the BSP tree must have a
Booleanattribute representing the inside status of the corresponding cell (true for inside cells, false for outside cells). In order to avoid building too many small objects, it is recommended to use the predefined constantsBoolean.TRUEandBoolean.FALSE. The tree also must have either null internal nodes or internal nodes representing the boundary as specified in thegetTreemethod).- Specified by:
buildNewin interfaceRegion<Euclidean2D>- Specified by:
buildNewin classAbstractRegion<Euclidean2D,Euclidean1D>- Parameters:
tree- inside/outside BSP tree representing the new region- Returns:
- the built region
-
computeGeometricalProperties
protected void computeGeometricalProperties()
Compute some geometrical properties.The properties to compute are the barycenter and the size.
- Specified by:
computeGeometricalPropertiesin classAbstractRegion<Euclidean2D,Euclidean1D>
-
getVertices
public Vector2D[][] getVertices()
Get the vertices of the polygon.The polygon boundary can be represented as an array of loops, each loop being itself an array of vertices.
In order to identify open loops which start and end by infinite edges, the open loops arrays start with a null point. In this case, the first non null point and the last point of the array do not represent real vertices, they are dummy points intended only to get the direction of the first and last edge. An open loop consisting of a single infinite line will therefore be represented by a three elements array with one null point followed by two dummy points. The open loops are always the first ones in the loops array.
If the polygon has no boundary at all, a zero length loop array will be returned.
All line segments in the various loops have the inside of the region on their left side and the outside on their right side when moving in the underlying line direction. This means that closed loops surrounding finite areas obey the direct trigonometric orientation.
- Returns:
- vertices of the polygon, organized as oriented boundary loops with the open loops first (the returned value is guaranteed to be non-null)
-
naturalFollowerConnections
private int naturalFollowerConnections(java.util.List<PolygonsSet.ConnectableSegment> segments)
Connect the segments using only natural follower information.- Parameters:
segments- segments complete segments list- Returns:
- number of connections performed
-
splitEdgeConnections
private int splitEdgeConnections(java.util.List<PolygonsSet.ConnectableSegment> segments)
Connect the segments resulting from a line splitting a straight edge.- Parameters:
segments- segments complete segments list- Returns:
- number of connections performed
-
closeVerticesConnections
private int closeVerticesConnections(java.util.List<PolygonsSet.ConnectableSegment> segments)
Connect the segments using Euclidean distance.This connection heuristic should be used last, as it relies only on a fuzzy distance criterion.
- Parameters:
segments- segments complete segments list- Returns:
- number of connections performed
-
getUnprocessed
private PolygonsSet.ConnectableSegment getUnprocessed(java.util.List<PolygonsSet.ConnectableSegment> segments)
Get first unprocessed segment from a list.- Parameters:
segments- segments list- Returns:
- first segment that has not been processed yet or null if all segments have been processed
-
followLoop
private java.util.List<Segment> followLoop(PolygonsSet.ConnectableSegment defining)
Build the loop containing a segment.The segment put in the loop will be marked as processed.
- Parameters:
defining- segment used to define the loop- Returns:
- loop containing the segment (may be null if the loop is a degenerated infinitely thin 2 points loop
-
filterSpuriousVertices
private void filterSpuriousVertices(java.util.List<Segment> loop)
Filter out spurious vertices on straight lines (at machine precision).- Parameters:
loop- segments loop to filter (will be modified in-place)
-
-