diff --git a/src/ifcopenshell-python/ifcopenshell/util/shape.py b/src/ifcopenshell-python/ifcopenshell/util/shape.py index a9e6c609b5..4c3fd756fa 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/shape.py +++ b/src/ifcopenshell-python/ifcopenshell/util/shape.py @@ -175,18 +175,21 @@ def get_bbox_centroid(geometry: ShapeType) -> tuple[float, float, float]: :return: A tuple representing the XYZ centroid :rtype: tuple[float, float, float] """ - verts_flat = get_vertices(geometry).ravel() - x_values = verts_flat[0::3] - y_values = verts_flat[1::3] - z_values = verts_flat[2::3] - minx = min(x_values) - maxx = max(x_values) - miny = min(y_values) - maxy = max(y_values) - minz = min(z_values) - maxz = max(z_values) - res = np.array((minx + ((maxx - minx) / 2), miny + ((maxy - miny) / 2), minz + ((maxz - minz) / 2))) - return res.tolist() + vertices_array = np.array(geometry.verts).reshape(-1, 3) + return (np.min(vertices_array, axis=0) + np.max(vertices_array, axis=0)) / 2 + + +def get_vert_centroid(geometry: ShapeType) -> tuple[float, float, float]: + """Calculates the average vertex centroid of the geometry + + The centroid is in local coordinates relative to the object's placement. + + :param geometry: Geometry output calculated by IfcOpenShell + :type geometry: geometry + :return: A tuple representing the XYZ centroid + :rtype: tuple[float, float, float] + """ + return np.mean(np.array(geometry.verts).reshape(-1, 3), axis=0) def get_element_bbox_centroid(element: ifcopenshell.entity_instance, geometry) -> npt.NDArray[np.float64]: