def is_intersection_line_line(l1, l2, tol=1e-6): """Verifies if two lines intersect. Parameters ---------- l1 : tuple A sequence of XYZ coordinates of two 3D points representing two points on the line. l2 : tuple A sequence of XYZ coordinates of two 3D points representing two points on the line. tol : float, optional A tolerance for intersection verification. Default is ``1e-6``. Returns -------- bool ``True``if the lines intersect in one point. ``False`` if the lines are skew, parallel or lie on top of each other. """ a, b = l1 c, d = l2 e1 = normalize_vector(subtract_vectors(b, a)) e2 = normalize_vector(subtract_vectors(d, c)) # check for parallel lines if abs(dot_vectors(e1, e2)) > 1.0 - tol: return False # check for intersection d_vector = cross_vectors(e1, e2) if dot_vectors(d_vector, subtract_vectors(c, a)) == 0: return True return False
def is_intersection_line_line(l1, l2, tol=1e-6): """Verifies if two lines intersect. Parameters ---------- l1 : [point, point] | :class:`compas.geometry.Line` A line. l2 : [point, point] | :class:`compas.geometry.Line` A line. tol : float, optional A tolerance for intersection verification. Returns -------- bool True if the lines intersect in one point. False if the lines are skew, parallel or lie on top of each other. """ a, b = l1 c, d = l2 e1 = normalize_vector(subtract_vectors(b, a)) e2 = normalize_vector(subtract_vectors(d, c)) # check for parallel lines if abs(dot_vectors(e1, e2)) > 1.0 - tol: return False # check for intersection if abs(dot_vectors(cross_vectors(e1, e2), subtract_vectors(c, a))) < tol: return True return False
def is_intersection_segment_plane(segment, plane, tol=1e-6): """Determine if a line segment intersects with a plane. Parameters ---------- segment : [point, point] | :class:`compas.geometry.Line` A line segment. plane : [point, vector] | :class:`compas.geometry.Plane` A plane. tol : float, optional A tolerance for intersection verification. Returns ------- bool True if the segment intersects with the plane. False otherwise. """ pt1 = segment[0] pt2 = segment[1] p_cent = plane[0] p_norm = plane[1] v1 = subtract_vectors(pt2, pt1) dot = dot_vectors(p_norm, v1) if fabs(dot) > tol: v2 = subtract_vectors(pt1, p_cent) fac = -dot_vectors(p_norm, v2) / dot if fac > 0. and fac < 1.: return True return False else: return False
def is_intersection_segment_plane(segment, plane, tol=1e-6): """Determine if a line segment intersects with a plane. Parameters ---------- segment : tuple Two points defining the segment. plane : tuple The base point and normal defining the plane. tol : float, optional A tolerance for intersection verification. Default is ``1e-6``. Returns ------- bool ``True`` if the segment intersects with the plane, ``False`` otherwise. """ pt1 = segment[0] pt2 = segment[1] p_cent = plane[0] p_norm = plane[1] v1 = subtract_vectors(pt2, pt1) dot = dot_vectors(p_norm, v1) if fabs(dot) > tol: v2 = subtract_vectors(pt1, p_cent) fac = -dot_vectors(p_norm, v2) / dot if fac > 0. and fac < 1.: return True return False else: return False
def circle_from_points(a, b, c): """Construct a circle from three points. Parameters ---------- a : [float, float, float] | :class:`compas.geometry.Point` XYZ coordinates. b : [float, float, float] | :class:`compas.geometry.Point` XYZ coordinates. c : [float, float, float] | :class:`compas.geometry.Point` XYZ coordinates. Returns ------- [float, float, float] Center of the circle. float Radius of the circle. [float, float, float] Normal of the plane containing the circle. Notes ----- For more information, see [1]_. References ---------- .. [1] Wikipedia. *Circumscribed circle*. Available at: https://en.wikipedia.org/wiki/Circumscribed_circle. Examples -------- >>> """ ab = subtract_vectors(b, a) cb = subtract_vectors(b, c) ba = subtract_vectors(a, b) ca = subtract_vectors(a, c) ac = subtract_vectors(c, a) bc = subtract_vectors(c, b) normal = normalize_vector(cross_vectors(ab, ac)) d = 2 * length_vector_sqrd(cross_vectors(ba, cb)) A = length_vector_sqrd(cb) * dot_vectors(ba, ca) / d B = length_vector_sqrd(ca) * dot_vectors(ab, cb) / d C = length_vector_sqrd(ba) * dot_vectors(ac, bc) / d Aa = scale_vector(a, A) Bb = scale_vector(b, B) Cc = scale_vector(c, C) center = sum_vectors([Aa, Bb, Cc]) radius = length_vector(subtract_vectors(a, center)) return center, radius, normal
def is_intersection_line_plane(line, plane, tol=1e-6): """Determine if a line (ray) intersects with a plane. Parameters ---------- line : tuple Two points defining the line. plane : tuple The base point and normal defining the plane. tol : float, optional A tolerance for intersection verification. Default is ``1e-6``. Returns ------- bool ``True`` if the line intersects with the plane. ``False`` otherwise. """ pt1 = line[0] pt2 = line[1] p_norm = plane[1] v1 = subtract_vectors(pt2, pt1) dot = dot_vectors(p_norm, v1) if fabs(dot) > tol: return True return False
def is_parallel_line_line(line1, line2, tol=1e-6): """Determine if two lines are parallel. Parameters ---------- line1 : [point, point] or :class:`compas.geometry.Line` Line 1. line2 : [point, point] or :class:`compas.geometry.Line` Line 2. tol : float, optional A tolerance for colinearity verification. Default is ``1e-6``. Returns ------- bool ``True`` if the lines are colinear. ``False`` otherwise. """ a, b = line1 c, d = line2 e1 = normalize_vector(subtract_vectors(b, a)) e2 = normalize_vector(subtract_vectors(d, c)) return abs(dot_vectors(e1, e2)) > 1.0 - tol
def angle_vectors(u, v, deg=False, tol=0.0): """Compute the smallest angle between two vectors. Parameters ---------- u : [float, float, float] | :class:`compas.geometry.Vector` XYZ components of the first vector. v : [float, float, float] | :class:`compas.geometry.Vector` XYZ components of the second vector. deg : bool, optional If True, returns the angle in degrees. tol : float, optional Tolerance for the length of the vectors. Returns ------- float The smallest angle in radians (in degrees if ``deg == True``). The angle is always positive. Examples -------- >>> angle_vectors([0.0, 1.0, 0.0], [1.0, 0.0, 0.0]) 1.57079 """ L = length_vector(u) * length_vector(v) if tol and L < tol: return 0 a = dot_vectors(u, v) / L a = max(min(a, 1), -1) if deg: return degrees(acos(a)) return acos(a)
def is_intersection_line_plane(line, plane, tol=1e-6): """Determine if a line (ray) intersects with a plane. Parameters ---------- line : [point, point] | :class:`compas.geometry.Line` A line. plane : [point, vector] | :class:`compas.geometry.Plane` A plane. tol : float, optional A tolerance for intersection verification. Returns ------- bool True if the line intersects with the plane. False otherwise. """ pt1 = line[0] pt2 = line[1] p_norm = plane[1] v1 = subtract_vectors(pt2, pt1) dot = dot_vectors(p_norm, v1) if fabs(dot) > tol: return True return False
def angle_vectors(u, v, deg=False, tol=0.0): """Compute the smallest angle between two vectors. Parameters ---------- u : sequence of float XYZ components of the first vector. v : sequence of float XYZ components of the second vector. deg : boolean returns angle in degrees if True Returns ------- float The smallest angle in radians (in degrees if deg == True). The angle is always positive. Examples -------- >>> angle_vectors([0.0, 1.0, 0.0], [1.0, 0.0, 0.0]) """ L = length_vector(u) * length_vector(v) if tol and L < tol: return 0 a = dot_vectors(u, v) / L a = max(min(a, 1), -1) if deg: return degrees(acos(a)) return acos(a)
def intersection_plane_plane(plane1, plane2, tol=1e-6): """Computes the intersection of two planes Parameters ---------- plane1 : tuple The base point and normal (normalized) defining the 1st plane. plane2 : tuple The base point and normal (normalized) defining the 2nd plane. tol : float, optional A tolerance for membership verification. Default is ``1e-6``. Returns ------- line : tuple Two points defining the intersection line. None if planes are parallel. """ o1, n1 = plane1 o2, n2 = plane2 if fabs(dot_vectors(n1, n2)) >= 1 - tol: return None # direction of intersection line d = cross_vectors(n1, n2) # vector in plane 1 perpendicular to the direction of the intersection line v1 = cross_vectors(d, n1) # point on plane 1 p1 = add_vectors(o1, v1) x1 = intersection_line_plane((o1, p1), plane2, tol=tol) x2 = add_vectors(x1, d) return x1, x2
def is_on_same_side(p1, p2, segment): a, b = segment v = subtract_vectors(b, a) c1 = cross_vectors(v, subtract_vectors(p1, a)) c2 = cross_vectors(v, subtract_vectors(p2, a)) if dot_vectors(c1, c2) >= 0: return True return False
def is_coplanar(points, tol=0.01): """Determine if the points are coplanar. Parameters ---------- points : sequence A sequence of locations in three-dimensional space. tol : float, optional A tolerance for planarity validation. Default is ``0.01``. Returns ------- bool ``True`` if the points are coplanar. ``False`` otherwise. Notes ----- Compute the normal vector (cross product) of the vectors formed by the first three points. Include one more vector at a time to compute a new normal and compare with the original normal. If their cross product is not zero, they are not parallel, which means the point are not in the same plane. Four points are coplanar if the volume of the tetrahedron defined by them is 0. Coplanarity is equivalent to the statement that the pair of lines determined by the four points are not skew, and can be equivalently stated in vector form as (x2 - x0).[(x1 - x0) x (x3 - x2)] = 0. """ tol2 = tol**2 if len(points) == 4: v01 = subtract_vectors(points[1], points[0]) v02 = subtract_vectors(points[2], points[0]) v23 = subtract_vectors(points[3], points[0]) res = dot_vectors(v02, cross_vectors(v01, v23)) return res**2 < tol2 # len(points) > 4 # compare length of cross product vector to tolerance a, b, c = sample(points, 3) u = subtract_vectors(b, a) v = subtract_vectors(c, a) w = cross_vectors(u, v) for i in range(0, len(points) - 2): u = v v = subtract_vectors(points[i + 2], points[i + 1]) wuv = cross_vectors(w, cross_vectors(u, v)) if wuv[0]**2 > tol2 or wuv[1]**2 > tol2 or wuv[2]**2 > tol2: return False return True
def intersection_segment_plane(segment, plane, tol=1e-6): """Computes the intersection point of a line segment and a plane Parameters ---------- segment : tuple Two points defining the line segment. plane : tuple The base point and normal defining the plane. tol : float, optional A tolerance for membership verification. Default is ``1e-6``. Returns ------- point : tuple if the line segment intersects with the plane, None otherwise. """ a, b = segment o, n = plane ab = subtract_vectors(b, a) cosa = dot_vectors(n, ab) if fabs(cosa) <= tol: # if the dot product (cosine of the angle between segment and plane) # is close to zero the line and the normal are almost perpendicular # hence there is no intersection return None # based on the ratio = -dot_vectors(n, ab) / dot_vectors(n, oa) # there are three scenarios # 1) 0.0 < ratio < 1.0: the intersection is between a and b # 2) ratio < 0.0: the intersection is on the other side of a # 3) ratio > 1.0: the intersection is on the other side of b oa = subtract_vectors(a, o) ratio = - dot_vectors(n, oa) / cosa if 0.0 <= ratio and ratio <= 1.0: ab = scale_vector(ab, ratio) return add_vectors(a, ab) return None
def distance_point_plane_signed(point, plane): r"""Compute the signed distance from a point to a plane defined by origin point and normal. Parameters ---------- point : list Point coordinates. plane : tuple A point and a vector defining a plane. Returns ------- float Distance between point and plane. Notes ----- The distance from a point to a plane can be computed from the coefficients of the equation of the plane and the coordinates of the point [1]_. The equation of a plane is .. math:: Ax + By + Cz + D = 0 where .. math:: :nowrap: \begin{align} D &= - Ax_0 - Bx_0 - Cz_0 \\ Q &= (x_0, y_0, z_0) \\ N &= (A, B, C) \end{align} with :math:`Q` a point on the plane, and :math:`N` the normal vector at that point. The distance of any point :math:`P` to a plane is the value of the dot product of the vector from :math:`Q` to :math:`P` and the normal at :math:`Q`. References ---------- .. [1] Nykamp, D. *Distance from point to plane*. Available at: http://mathinsight.org/distance_point_plane. Examples -------- >>> """ base, normal = plane vector = subtract_vectors(point, base) return dot_vectors(vector, normal)
def is_coplanar(points, tol=0.01): """Determine if the points are coplanar. Parameters ---------- points : list of points A sequence of point locations. tol : float, optional A tolerance for planarity validation. Default is ``0.01``. Returns ------- bool ``True`` if the points are coplanar. ``False`` otherwise. Notes ----- Compute the normal vector (cross product) of the vectors formed by the first three points. Include one more vector at a time to compute a new normal and compare with the original normal. If their cross product is not zero, they are not parallel, which means the point are not in the same plane. Four points are coplanar if the volume of the tetrahedron defined by them is 0. Coplanarity is equivalent to the statement that the pair of lines determined by the four points are not skew, and can be equivalently stated in vector form as (x2 - x0).[(x1 - x0) x (x3 - x2)] = 0. """ if len(points) < 4: return True tol2 = tol ** 2 if len(points) == 4: v01 = subtract_vectors(points[1], points[0]) v02 = subtract_vectors(points[2], points[0]) v23 = subtract_vectors(points[3], points[2]) res = dot_vectors(v02, cross_vectors(v01, v23)) return res**2 < tol2 a, b, c = points[:3] ab = subtract_vectors(b, a) n0 = cross_vectors(ab, subtract_vectors(c, a)) points = points[3:] for c in points: n1 = cross_vectors(ab, subtract_vectors(c, a)) if length_vector_sqrd(cross_vectors(n0, n1)) > tol: return False return True
def distance_line_line(l1, l2, tol=0.0): r"""Compute the shortest distance between two lines. Parameters ---------- l1 : tuple Two points defining a line. l2 : tuple Two points defining a line. Returns ------- float The distance between the two lines. Notes ----- The distance is the absolute value of the dot product of a unit vector that is perpendicular to the two lines, and the vector between two points on the lines ([1]_, [2]_). If each of the lines is defined by two points (:math:`l_1 = (\mathbf{x_1}, \mathbf{x_2})`, :math:`l_2 = (\mathbf{x_3}, \mathbf{x_4})`), then the unit vector that is perpendicular to both lines is... References ---------- .. [1] Weisstein, E.W. *Line-line Distance*. Available at: http://mathworld.wolfram.com/Line-LineDistance.html. .. [2] Wikipedia. *Skew lines Distance*. Available at: https://en.wikipedia.org/wiki/Skew_lines#Distance. Examples -------- >>> """ a, b = l1 c, d = l2 ab = subtract_vectors(b, a) cd = subtract_vectors(d, c) ac = subtract_vectors(c, a) n = cross_vectors(ab, cd) length = length_vector(n) if length <= tol: return distance_point_point(closest_point_on_line(l1[0], l2), l1[0]) n = scale_vector(n, 1.0 / length) return fabs(dot_vectors(n, ac))
def is_polygon_convex(polygon): """Determine if a polygon is convex. Parameters ---------- polygon : sequence of sequence of floats The XYZ coordinates of the corners of the polygon. Notes ----- Use this function for *spatial* polygons. If the polygon is in a horizontal plane, use :func:`is_polygon_convex_xy` instead. Returns ------- bool ``True`` if the polygon is convex. ``False`` otherwise. See Also -------- is_polygon_convex_xy Examples -------- >>> polygon = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.4, 0.4, 0.0], [0.0, 1.0, 0.0]] >>> is_polygon_convex(polygon) False """ a = polygon[0] o = polygon[1] b = polygon[2] oa = subtract_vectors(a, o) ob = subtract_vectors(b, o) n0 = cross_vectors(oa, ob) for a, o, b in window(polygon + polygon[:2], 3): oa = subtract_vectors(a, o) ob = subtract_vectors(b, o) n = cross_vectors(oa, ob) if dot_vectors(n, n0) >= 0: continue else: return False return True
def is_point_behind_plane(point, plane, tol=1e-6): """Determine if a point lies behind a plane. Parameters ---------- point : [float, float, float] | :class:`compas.geometry.Point` A point. plane : [point, normal] | :class:`compas.geometry.Plane` A plane. tol : float, optional A tolerance for membership verification. Returns ------- bool True if the point is in front of the plane. False otherwise. """ return dot_vectors(subtract_vectors(point, plane[0]), plane[1]) < -tol
def is_polygon_convex(polygon): """Determine if a polygon is convex. Parameters ---------- polygon : sequence[point] | :class:`compas.geometry.Polygon` A polygon. Returns ------- bool True if the polygon is convex. False otherwise. Notes ----- Use this function for *spatial* polygons. If the polygon is in a horizontal plane, use :func:`is_polygon_convex_xy` instead. Examples -------- >>> polygon = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.4, 0.4, 0.0], [0.0, 1.0, 0.0]] >>> is_polygon_convex(polygon) False """ a = polygon[0] o = polygon[1] b = polygon[2] oa = subtract_vectors(a, o) ob = subtract_vectors(b, o) n0 = cross_vectors(oa, ob) for a, o, b in window(polygon + polygon[:2], 3): oa = subtract_vectors(a, o) ob = subtract_vectors(b, o) n = cross_vectors(oa, ob) if dot_vectors(n, n0) >= 0: continue else: return False return True
def is_point_infront_plane(point, plane, tol=1e-6): """Determine if a point lies in front of a plane. Parameters ---------- point : [x, y, z] or :class:`compas.geometry.Point` A point. plane : [point, vector] or :class:`compas.geometry.Plane` A plane. tol : float, optional A tolerance for membership verification. Default is ``1e-6``. Returns ------- bool ``True`` if the point is in front of the plane. ``False`` otherwise. """ return dot_vectors(subtract_vectors(point, plane[0]), plane[1]) > tol
def is_point_infront_plane(point, plane, tol=1e-6): """Determine if a point lies in front of a plane. Parameters ---------- point : sequence of float XYZ coordinates. plane : tuple Base point and normal defining a plane. tol : float, optional A tolerance for membership verification. Default is ``1e-6``. Returns ------- bool ``True`` if the point is in front of the plane. ``False`` otherwise. """ return dot_vectors(subtract_vectors(point, plane[0]), plane[1]) > tol
def is_intersection_plane_plane(plane1, plane2, tol=1e-6): """Verifies if two planes intersect. Parameters ---------- plane1 : [point, vector] | :class:`compas.geometry.Plane` A plane. plane2 : [point, vector] | :class:`compas.geometry.Plane` A plane. tol : float, optional A tolerance for intersection verification. Returns ------- bool True if plane1 intersects with plane2. False otherwise. """ # check for parallelity of planes if abs(dot_vectors(plane1[1], plane2[1])) > 1 - tol: return False return True
def area_polygon(polygon): """Compute the area of a polygon. Parameters ---------- polygon : sequence[point] | :class:`compas.geometry.Polygon` The XYZ coordinates of the vertices/corners of the polygon. The vertices are assumed to be in order. The polygon is assumed to be closed: the first and last vertex in the sequence should not be the same. Returns ------- float The area of the polygon. Examples -------- >>> """ o = centroid_points(polygon) a = polygon[-1] b = polygon[0] oa = subtract_vectors(a, o) ob = subtract_vectors(b, o) n0 = cross_vectors(oa, ob) area = 0.5 * length_vector(n0) for i in range(0, len(polygon) - 1): oa = ob b = polygon[i + 1] ob = subtract_vectors(b, o) n = cross_vectors(oa, ob) if dot_vectors(n, n0) > 0: area += 0.5 * length_vector(n) else: area -= 0.5 * length_vector(n) return area
def is_intersection_plane_plane(plane1, plane2, tol=1e-6): """Verifies if two planes intersect. Parameters ---------- plane1 : tuple The base point and normal (normalized) defining the 1st plane. plane2 : tuple The base point and normal (normalized) defining the 2nd plane. tol : float, optional A tolerance for intersection verification. Default is ``1e-6``. Returns ------- bool ``True`` if plane1 intersects with plane2. ``False`` otherwise. """ # check for parallelity of planes if abs(dot_vectors(plane1[1], plane2[1])) > 1 - tol: return False return True
def is_intersection_line_triangle(line, triangle, tol=1e-6): """Verifies if a line (ray) intersects with a triangle. Parameters ---------- line : [point, point] | :class:`compas.geometry.Line` A line. triangle : [point, point, point] A triangle. tol : float, optional A tolerance for intersection verification. Returns ------- bool True if the line (ray) intersects with the triangle. False otherwise. Notes ----- Based on the Moeller Trumbore intersection algorithm. The line is treated as continues, directed ray and not as line segment with a start and end point Examples -------- >>> """ a, b, c = triangle # direction vector and base point of line v1 = subtract_vectors(line[1], line[0]) p1 = line[0] # Find vectors for two edges sharing triangle vertex 1 e1 = subtract_vectors(b, a) e2 = subtract_vectors(c, a) # Begin calculating determinant - also used to calculate u parameter p = cross_vectors(v1, e2) # if determinant is near zero, ray lies in plane of triangle det = dot_vectors(e1, p) # NOT CULLING if det > -tol and det < tol: return False inv_det = 1.0 / det # calculate distance from V1 to ray origin t = subtract_vectors(p1, a) # Calculate u parameter and make_blocks bound u = dot_vectors(t, p) * inv_det # The intersection lies outside of the triangle if u < 0.0 or u > 1.0: return False # Prepare to make_blocks v parameter q = cross_vectors(t, e1) # Calculate V parameter and make_blocks bound v = dot_vectors(v1, q) * inv_det # The intersection lies outside of the triangle if v < 0.0 or u + v > 1.0: return False t = dot_vectors(e2, q) * inv_det if t > tol: return True # No hit return False
def centroid_polyhedron(polyhedron): """Compute the center of mass of a polyhedron. Parameters ---------- polyhedron : tuple The coordinates of the vertices, and the indices of the vertices forming the faces. Returns ------- list XYZ coordinates of the center of mass. Warning ------- This function assumes that the vertex cycles of the faces are such that the face normals are consistently pointing outwards, resulting in a *positive* polyhedron. Examples -------- >>> from compas.geometry._core import Polyhedron >>> p = Polyhedron.generate(6) >>> centroid_polyhedron(p) [0.0, 0.0, 0.0] """ vertices, faces = polyhedron V = 0 x = 0.0 y = 0.0 z = 0.0 ex = [1.0, 0.0, 0.0] ey = [0.0, 1.0, 0.0] ez = [0.0, 0.0, 1.0] for face in faces: if len(face) == 3: triangles = [face] else: centroid = centroid_points([vertices[index] for index in face]) w = len(vertices) vertices.append(centroid) triangles = [[w, u, v] for u, v in pairwise(face + face[0:1])] for triangle in triangles: a = vertices[triangle[0]] b = vertices[triangle[1]] c = vertices[triangle[2]] ab = subtract_vectors(b, a) ac = subtract_vectors(c, a) n = cross_vectors(ab, ac) V += dot_vectors(a, n) nx = dot_vectors(n, ex) ny = dot_vectors(n, ey) nz = dot_vectors(n, ez) ab = add_vectors(a, b) bc = add_vectors(b, c) ca = add_vectors(c, a) ab_x2 = dot_vectors(ab, ex)**2 bc_x2 = dot_vectors(bc, ex)**2 ca_x2 = dot_vectors(ca, ex)**2 x += nx * (ab_x2 + bc_x2 + ca_x2) ab_y2 = dot_vectors(ab, ey)**2 bc_y2 = dot_vectors(bc, ey)**2 ca_y2 = dot_vectors(ca, ey)**2 y += ny * (ab_y2 + bc_y2 + ca_y2) ab_z2 = dot_vectors(ab, ez)**2 bc_z2 = dot_vectors(bc, ez)**2 ca_z2 = dot_vectors(ca, ez)**2 z += nz * (ab_z2 + bc_z2 + ca_z2) # for j in (-1, 0, 1): # ab = add_vectors(vertices[triangle[j]], vertices[triangle[j + 1]]) # x += nx * dot_vectors(ab, ex) ** 2 # y += ny * dot_vectors(ab, ey) ** 2 # z += nz * dot_vectors(ab, ez) ** 2 V = V / 6.0 if V < 1e-9: d = 1.0 / (2 * 24) else: d = 1.0 / (2 * 24 * V) x *= d y *= d z *= d return [x, y, z]
def centroid_polygon(polygon): r"""Compute the centroid of the surface of a polygon. Parameters ---------- polygon : list of point A sequence of polygon point coordinates. Returns ------- list The XYZ coordinates of the centroid. Notes ----- The centroid is the centre of gravity of the polygon surface if mass would be uniformly distributed over it. It is calculated by triangulating the polygon surface with respect to the centroid of the polygon vertices, and then computing the centroid of the centroids of the individual triangles, weighted by the corresponding triangle area in proportion to the total surface area. .. math:: c_x = \frac{1}{A} \sum_{i=1}^{N} A_i \cdot c_{x,i} c_y = \frac{1}{A} \sum_{i=1}^{N} A_i \cdot c_{y,i} c_z = \frac{1}{A} \sum_{i=1}^{N} A_i \cdot c_{z,i} Warning ------- The polygon need not be convex. The polygon need not be flat. However, it is unclear what the meaning of the centroid is in that case. The polygon may be self-intersecting. However, it is unclear what the meaning of the centroid is in that case. Examples -------- >>> polygon = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 1.0, 0.0]] >>> centroid_polygon(polygon) """ p = len(polygon) assert p > 2, "At least three points required" if p == 3: return centroid_points(polygon) cx, cy, cz = 0.0, 0.0, 0.0 A2 = 0 o = centroid_points(polygon) a = polygon[-1] b = polygon[0] oa = subtract_vectors(a, o) ob = subtract_vectors(b, o) n0 = cross_vectors(oa, ob) x, y, z = centroid_points([o, a, b]) a2 = length_vector(n0) A2 += a2 cx += a2 * x cy += a2 * y cz += a2 * z for i in range(1, len(polygon)): a = b b = polygon[i] oa = ob ob = subtract_vectors(b, o) n = cross_vectors(oa, ob) x, y, z = centroid_points([o, a, b]) if dot_vectors(n, n0) > 0: a2 = length_vector(n) else: a2 = -length_vector(n) A2 += a2 cx += a2 * x cy += a2 * y cz += a2 * z if A2 == 0: return polygon[0] return [cx / A2, cy / A2, cz / A2]
def volume_polyhedron(polyhedron): r"""Compute the volume of a polyhedron represented by a closed mesh. Parameters ---------- polyhedron : tuple[sequence[[float, float, float] | :class:`compas.geometry.Point`], sequence[sequence[int]]] The vertices and faces of the polyhedron. Returns ------- float The volume of the polyhedron. Notes ----- This implementation is based on the divergence theorem, the fact that the *area vector* is constant for each face, and the fact that the area of each face can be computed as half the length of the cross product of two adjacent edge vectors [1]_. .. math:: :nowrap: \begin{align} V = \int_{P} 1 &= \frac{1}{3} \int_{\partial P} \mathbf{x} \cdot \mathbf{n} \\ &= \frac{1}{3} \sum_{i=0}^{N-1} \int{A_{i}} a_{i} \cdot n_{i} \\ &= \frac{1}{6} \sum_{i=0}^{N-1} a_{i} \cdot \hat n_{i} \end{align} Warnings -------- The volume computed by this funtion is only correct if the polyhedron is convex, has planar faces, and is positively oriented (all face normals point outwards). References ---------- .. [1] Nurnberg, R. *Calculating the area and centroid of a polygon in 2d*. Available at: http://wwwf.imperial.ac.uk/~rn/centroid.pdf """ xyz, faces = polyhedron V = 0 for vertices in faces: if len(vertices) == 3: triangles = [vertices] else: centroid = centroid_points([xyz[i] for i in vertices]) i = len(xyz) xyz.append(centroid) triangles = [] for u, v in pairwise(vertices + vertices[0:1]): triangles.append([i, u, v]) for u, v, w in triangles: a = xyz[u] b = xyz[v] c = xyz[w] ab = subtract_vectors(b, a) ac = subtract_vectors(c, a) n = cross_vectors(ab, ac) V += dot_vectors(a, n) return V / 6.