Skip to content

Commit

Permalink
Merge 067f495 into 0aa04e8
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Mackey committed May 28, 2019
2 parents 0aa04e8 + 067f495 commit c24ad8a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
14 changes: 13 additions & 1 deletion ladybug_geometry/geometry2d/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Polygon2D(Base2DIn2D):
is_clockwise
is_convex
is_self_intersecting
is_valid
"""
__slots__ = ('_vertices', '_segments', '_triangulated_mesh',
'_min', '_max', '_center', '_perimeter', '_area',
Expand Down Expand Up @@ -291,8 +292,19 @@ def is_self_intersecting(self):
break
return self._is_self_intersecting

@property
def is_valid(self):
"""Boolean noting whether the polygon is valid (having a non-zero area).
Note that polygons are still considered valid if they have self-intersecting
edges, or duplicate/colinear vertices. The s_self_intersecting property
identifies self-intersecting edges, and the remove_colinear_vertices method
will remove duplicate/colinear vertices.
"""
return not self.area == 0

def remove_colinear_vertices(self, tolerance):
"""Get a version of this polygon with colinear vertices removed.
"""Get a version of this polygon without colinear or duplicate vertices.
Args:
tolerance: The minimum distance that a vertex can be from a line
Expand Down
14 changes: 13 additions & 1 deletion ladybug_geometry/geometry3d/face.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Face3D(Base2DIn3D):
is_clockwise
is_convex
is_self_intersecting
is_valid
has_holes
upper_left_counter_clockwise_vertices
"""
Expand Down Expand Up @@ -457,6 +458,17 @@ def is_self_intersecting(self):
self._is_self_intersecting = self.polygon2d.is_self_intersecting
return self._is_self_intersecting

@property
def is_valid(self):
"""Boolean noting whether the face is valid (having a non-zero area).
Note that faces are still considered valid if they have out-of-plane vertices,
self-intersecting edges, or duplicate/colinear vertices. The validate_planarity
method can be used to detect if there are out-of-plane vertices. The
is_self_intersecting property identifies self-intersecting edges, and the
remove_colinear_vertices method will remove duplicate/colinear vertices."""
return not self.area == 0

@property
def has_holes(self):
"""Boolean noting whether the face has holes within it."""
Expand Down Expand Up @@ -615,7 +627,7 @@ def validate_planarity(self, tolerance, raise_exception=True):
return True

def remove_colinear_vertices(self, tolerance):
"""Get a version of this face with colinear vertices removed.
"""Get a version of this face without colinear or duplicate vertices.
Args:
tolerance: The minimum distance between a vertex and the boundary segments
Expand Down
11 changes: 11 additions & 0 deletions tests/face3d_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,17 @@ def test_is_self_intersecting(self):
assert face_3.is_self_intersecting is False
assert face_4.is_self_intersecting is True

def test_is_valid(self):
"""Test the is_valid property."""
plane_1 = Plane(Vector3D(0, 0, 1))
pts_1 = (Point3D(0, 0), Point3D(2, 0), Point3D(2, 2))
pts_2 = (Point3D(0, 0), Point3D(2, 0), Point3D(2, 0))
face_1 = Face3D(pts_1, plane_1)
face_2 = Face3D(pts_2, plane_1)

assert face_1.is_valid is True
assert face_2.is_valid is False

def test_min_max_center(self):
"""Test the Face3D min, max and center."""
pts_1 = (Point3D(0, 0, 2), Point3D(2, 0, 2), Point3D(2, 2, 2), Point3D(0, 2, 2))
Expand Down
10 changes: 10 additions & 0 deletions tests/polygon2d_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ def test_is_self_intersecting(self):
assert polygon_1.is_self_intersecting is False
assert polygon_2.is_self_intersecting is True

def test_is_valid(self):
"""Test the is_valid property."""
pts_1 = (Point2D(0, 0), Point2D(2, 0), Point2D(2, 2))
pts_2 = (Point2D(0, 0), Point2D(2, 0), Point2D(2, 0))
polygon_1 = Polygon2D(pts_1)
polygon_2 = Polygon2D(pts_2)

assert polygon_1.is_valid is True
assert polygon_2.is_valid is False

def test_min_max_center(self):
"""Test the Polygon2D min, max and center."""
pts = (Point2D(0, 0), Point2D(2, 0), Point2D(2, 2), Point2D(0, 2))
Expand Down

0 comments on commit c24ad8a

Please sign in to comment.