system_assignation/city_model_structure/attributes/polyhedron.py

253 lines
6.4 KiB
Python
Raw Normal View History

2020-10-28 13:42:58 -04:00
"""
Polyhedron module
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2020 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
Contributors Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca
2020-10-28 13:42:58 -04:00
"""
from typing import List, Union
import sys
2021-03-31 14:17:53 -04:00
import math
import numpy as np
2020-10-28 13:42:58 -04:00
from trimesh import Trimesh
from helpers.configuration_helper import ConfigurationHelper
2020-10-28 13:42:58 -04:00
class Polyhedron:
"""
Polyhedron class
"""
def __init__(self, polygons):
self._polygons = polygons
2020-10-28 13:42:58 -04:00
self._polyhedron = None
self._triangulated_polyhedron = None
2020-10-28 13:42:58 -04:00
self._volume = None
self._faces = None
self._vertices = None
self._trimesh = None
2020-10-28 13:42:58 -04:00
self._centroid = None
self._max_z = None
2020-12-01 07:33:23 -05:00
self._max_y = None
self._max_x = None
self._min_z = None
self._min_y = None
self._min_x = None
2020-10-28 13:42:58 -04:00
def _position_of(self, point, face):
"""
position of a specific point in the list of points that define a face
:return: int
"""
2020-10-28 13:42:58 -04:00
vertices = self.vertices
for i in range(len(vertices)):
# ensure not duplicated vertex
2021-03-31 14:17:53 -04:00
power = 0
vertex2 = vertices[i]
for dimension in range(0, 3):
power += math.pow(vertex2[dimension] - point[dimension], 2)
distance = math.sqrt(power)
if i not in face and distance == 0:
2020-10-28 13:42:58 -04:00
return i
return -1
@property
def vertices(self) -> np.ndarray:
"""
Polyhedron vertices
:return: np.ndarray(int)
"""
if self._vertices is None:
vertices, self._vertices = [], []
_ = [vertices.extend(s.coordinates) for s in self._polygons]
2020-10-28 13:42:58 -04:00
for vertex_1 in vertices:
found = False
for vertex_2 in self._vertices:
found = False
2021-03-31 14:17:53 -04:00
power = 0
for dimension in range(0, 3):
power += math.pow(vertex_2[dimension] - vertex_1[dimension], 2)
distance = math.sqrt(power)
if distance == 0:
2020-10-28 13:42:58 -04:00
found = True
break
if not found:
self._vertices.append(vertex_1)
self._vertices = np.asarray(self._vertices)
return self._vertices
@property
def faces(self) -> List[List[int]]:
2020-10-28 13:42:58 -04:00
"""
Polyhedron triangular faces
:return: [face]
2020-10-28 13:42:58 -04:00
"""
if self._faces is None:
self._faces = []
for polygon in self._polygons:
2020-10-28 13:42:58 -04:00
face = []
points = polygon.coordinates
2020-12-01 07:33:23 -05:00
if len(points) != 3:
sub_polygons = polygon.triangulate()
# todo: I modified this! To be checked @Guille
if len(sub_polygons) >= 1:
for sub_polygon in sub_polygons:
face = []
points = sub_polygon.coordinates
for point in points:
face.append(self._position_of(point, face))
self._faces.append(face)
else:
for point in points:
face.append(self._position_of(point, face))
self._faces.append(face)
2020-10-28 13:42:58 -04:00
return self._faces
@property
def trimesh(self) -> Union[Trimesh, None]:
"""
Get trimesh
:return: Trimesh
"""
if self._trimesh is None:
for face in self.faces:
if len(face) != 3:
sys.stderr.write('Not able to generate trimesh\n')
return None
self._trimesh = Trimesh(vertices=self.vertices, faces=self.faces)
return self._trimesh
2020-10-28 13:42:58 -04:00
@property
def volume(self):
"""
Polyhedron volume in cubic meters
:return: float
"""
if self._volume is None:
if self.trimesh is None:
self._volume = np.inf
elif not self.trimesh.is_volume:
2020-10-28 13:42:58 -04:00
self._volume = np.inf
else:
self._volume = self.trimesh.volume
2020-10-28 13:42:58 -04:00
return self._volume
@property
def max_z(self):
"""
Polyhedron maximal z value in meters
2020-10-28 13:42:58 -04:00
:return: float
"""
2020-12-01 07:33:23 -05:00
if self._max_z is None:
self._max_z = ConfigurationHelper().min_coordinate
for polygon in self._polygons:
for point in polygon.coordinates:
self._max_z = max(self._max_z, point[2])
2020-12-01 07:33:23 -05:00
return self._max_z
@property
def max_y(self):
"""
Polyhedron maximal y value in meters
2020-12-01 07:33:23 -05:00
:return: float
"""
if self._max_y is None:
self._max_y = ConfigurationHelper().min_coordinate
for polygon in self._polygons:
for point in polygon.coordinates:
2020-12-01 07:33:23 -05:00
if self._max_y < point[1]:
self._max_y = point[1]
return self._max_y
@property
def max_x(self):
"""
Polyhedron maximal x value in meters
2020-12-01 07:33:23 -05:00
:return: float
"""
if self._max_x is None:
self._max_x = ConfigurationHelper().min_coordinate
for polygon in self._polygons:
for point in polygon.coordinates:
2020-12-01 07:33:23 -05:00
self._max_x = max(self._max_x, point[0])
return self._max_x
@property
def min_z(self):
"""
Polyhedron minimal z value in meters
2020-12-01 07:33:23 -05:00
:return: float
"""
if self._min_z is None:
self._min_z = self.max_z
for polygon in self._polygons:
for point in polygon.coordinates:
2020-12-01 07:33:23 -05:00
if self._min_z > point[2]:
self._min_z = point[2]
return self._min_z
@property
def min_y(self):
"""
Polyhedron minimal y value in meters
2020-12-01 07:33:23 -05:00
:return: float
"""
if self._min_y is None:
self._min_y = self.max_y
for polygon in self._polygons:
for point in polygon.coordinates:
2020-12-01 07:33:23 -05:00
if self._min_y > point[1]:
self._min_y = point[1]
return self._min_y
@property
def min_x(self):
"""
Polyhedron minimal x value in meters
2020-12-01 07:33:23 -05:00
:return: float
"""
if self._min_x is None:
self._min_x = self.max_x
for polygon in self._polygons:
for point in polygon.coordinates:
2020-12-01 07:33:23 -05:00
if self._min_x > point[0]:
self._min_x = point[0]
return self._min_x
2020-10-28 13:42:58 -04:00
@property
def centroid(self):
2020-10-28 13:42:58 -04:00
"""
Polyhedron centroid
:return: [x,y,z]
"""
if self._centroid is None:
trimesh = self.trimesh
if trimesh is None:
return None
self._centroid = self.trimesh.centroid
return self._centroid
2020-12-01 07:33:23 -05:00
2020-11-18 08:58:37 -05:00
def stl_export(self, full_path):
2020-10-28 13:42:58 -04:00
"""
Export the polyhedron to stl given file
:param full_path: str
:return: None
"""
self.trimesh.export(full_path, 'stl_ascii')
2020-11-18 08:58:37 -05:00
def obj_export(self, full_path):
"""
Export the polyhedron to obj given file
:param full_path: str
:return: None
"""
self.trimesh.export(full_path, 'obj')
def show(self):
"""
Auxiliary function to render the polyhedron
"""
self.trimesh.show()