system_assignation/city_model_structure/polyhedron.py

80 lines
2.1 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
import stl
from helpers.geometry import Geometry
class Polyhedron:
def __init__(self, surfaces):
self._surfaces = [s for s in surfaces]
self._polygons = [s.polygon for s in surfaces]
self._polyhedron = None
self._volume = None
self._faces = None
self._vertices = None
self._mesh = None
self._geometry = Geometry()
def _position_of(self, point):
vertices = self.vertices
for i in range(len(vertices)):
if self._geometry.almost_equal(vertices[i], point):
return i
return -1
@property
def vertices(self):
if self._vertices is None:
vertices, self._vertices = [], []
[vertices.extend(s.points) for s in self._surfaces]
for v1 in vertices:
found = False
for v2 in self._vertices:
found = False
if self._geometry.almost_equal(v1, v2):
found = True
break
if not found:
self._vertices.append(v1)
self._vertices = np.asarray(self._vertices)
return self._vertices
@property
def faces(self):
if self._faces is None:
self._faces = []
for s in self._surfaces:
face = []
points = s.points
for p in points:
face.append(self._position_of(p))
self._faces.append(face)
self._faces = np.asarray(self._faces)
return self._faces
@property
def _polyhedron_mesh(self):
if self._mesh is None:
self._mesh = stl.mesh.Mesh(np.zeros(self.faces.shape[0], dtype=stl.mesh.Mesh.dtype))
for i, f in enumerate(self.faces):
for j in range(3):
self._mesh.vectors[i][j] = self.vertices[f[j], :]
return self._mesh
@property
def volume(self):
if self._volume is None:
self._volume, cog, inertia = self._polyhedron_mesh.get_mass_properties()
return self._volume
@property
def max_z(self):
return self._polyhedron_mesh.z.max()
def save(self, full_path):
self._polyhedron_mesh.save(full_path)