summer_course_2024/city_model_structure/polyhedron.py

119 lines
2.8 KiB
Python
Raw Normal View History

2020-06-09 14:07:47 -04:00
"""
Polyhedron module
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2020 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
"""
import numpy as np
2020-06-11 17:30:50 -04:00
from trimesh import Trimesh
from helpers.geometry_helper import GeometryHelper
class Polyhedron:
2020-06-10 13:08:51 -04:00
"""
Polyhedron class
"""
def __init__(self, surfaces):
2020-06-10 13:08:51 -04:00
self._surfaces = list(surfaces)
self._polygons = [s.polygon for s in surfaces]
self._polyhedron = None
self._volume = None
self._faces = None
self._vertices = None
self._mesh = None
2020-06-26 10:06:43 -04:00
self._centroid = None
self._max_z = None
2020-06-11 16:22:58 -04:00
self._geometry = GeometryHelper()
def _position_of(self, point):
vertices = self.vertices
2020-06-11 17:30:50 -04:00
for i in range(len(vertices)):
if self._geometry.almost_equal(vertices[i], point):
return i
return -1
@property
2020-06-10 13:08:51 -04:00
def vertices(self) -> np.ndarray:
"""
Polyhedron vertices
:return: np.ndarray(int)
"""
if self._vertices is None:
vertices, self._vertices = [], []
_ = [vertices.extend(s.points) for s in self._surfaces]
for vertex_1 in vertices:
found = False
for vertex_2 in self._vertices:
found = False
if self._geometry.almost_equal(vertex_1, vertex_2):
found = True
break
if not found:
self._vertices.append(vertex_1)
self._vertices = np.asarray(self._vertices)
return self._vertices
@property
2020-06-10 13:08:51 -04:00
def faces(self) -> np.ndarray:
"""
Polyhedron faces
:return: np.ndarray([int])
"""
if self._faces is None:
self._faces = []
for surface in self._surfaces:
face = []
points = surface.points
for point in points:
face.append(self._position_of(point))
self._faces.append(face)
2020-06-26 10:06:43 -04:00
#self._faces = np.asarray(self._faces)
return self._faces
@property
def _polyhedron_mesh(self):
2020-06-26 10:06:43 -04:00
if self._mesh is None:
2020-06-26 10:06:43 -04:00
self._mesh = Trimesh(vertices=self.vertices, faces=self.faces)
return self._mesh
@property
def volume(self):
2020-06-10 13:08:51 -04:00
"""
Polyhedron volume in cubic meters
:return: float
"""
if self._volume is None:
2020-06-26 10:06:43 -04:00
if not self._polyhedron_mesh.is_volume:
print('The geometry is not a closed volume')
self._volume = np.inf
else:
self._volume = self._polyhedron_mesh.volume
return self._volume
@property
def max_z(self):
2020-06-10 13:08:51 -04:00
"""
Polyhedron maximal z value
:return: float
"""
2020-06-11 17:30:50 -04:00
bounds = self._polyhedron_mesh.bounds
z_max = max(bounds[:, 2])
return z_max
@property
def centroid(self):
"""
Polyhedron centroid
:return: [x,y,z]
"""
return self._polyhedron_mesh.centroid
2020-06-15 11:03:04 -04:00
def export(self, full_path):
2020-06-10 13:08:51 -04:00
"""
Export the polyhedron to stl given file
:param full_path: str
:return: None
"""
2020-06-15 11:03:04 -04:00
self._polyhedron_mesh.export(full_path)