Rhino implementation partially completed, need to be fully tested

This commit is contained in:
Guille Gutierrez 2022-02-16 15:08:05 -05:00
parent b54048f7db
commit 49bfc6eec9
9 changed files with 877 additions and 714 deletions

View File

@ -15,12 +15,13 @@ class Plane:
Plane class Plane class
""" """
def __init__(self, origin=None, normal=None): def __init__(self, origin, normal):
# todo: other options to define the plane: # todo: other options to define the plane:
# by two lines # by two lines
# by three points # by three points
self._origin = origin self._origin = origin
self._normal = normal self._normal = normal
self._equation = None
self._opposite_normal = None self._opposite_normal = None
@property @property
@ -29,8 +30,6 @@ class Plane:
Get plane origin point Get plane origin point
:return: Point :return: Point
""" """
if self._origin is None:
raise NotImplementedError
return self._origin return self._origin
@property @property
@ -39,10 +38,36 @@ class Plane:
Get plane normal [x, y, z] Get plane normal [x, y, z]
:return: np.ndarray :return: np.ndarray
""" """
if self._normal is None:
raise NotImplementedError
return self._normal return self._normal
@property
def equation(self) -> (float, float, float, float):
"""
Get the plane equation components Ax + By + Cz + D = 0
:return: (A, B, C, D)
"""
if self._equation is None:
a = self.normal[0]
b = self.normal[1]
c = self.normal[2]
d = ((-1 * self.origin.coordinates[0]) * self.normal[0])
d += ((-1 * self.origin.coordinates[1]) * self.normal[1])
d += ((-1 * self.origin.coordinates[2]) * self.normal[2])
self._equation = (a, b, c, d)
return self._equation
def distance(self, point):
"""
Distance between the given point and the plane
:return: float
"""
p = point
e = self.equation
denominator = np.abs((p[0] * e[0]) + (p[1] * e[1]) + (p[2] * e[2]) + e[3])
numerator = np.sqrt((e[0]**2) + (e[1]**2) + (e[2]**2))
return denominator/numerator
@property @property
def opposite_normal(self): def opposite_normal(self):
""" """

File diff suppressed because it is too large Load Diff

View File

@ -14,240 +14,240 @@ from helpers.configuration_helper import ConfigurationHelper
class Polyhedron: class Polyhedron:
""" """
Polyhedron class Polyhedron class
""" """
def __init__(self, polygons): def __init__(self, polygons):
self._polygons = polygons self._polygons = polygons
self._polyhedron = None self._polyhedron = None
self._triangulated_polyhedron = None self._triangulated_polyhedron = None
self._volume = None self._volume = None
self._faces = None self._faces = None
self._vertices = None self._vertices = None
self._trimesh = None self._trimesh = None
self._centroid = None self._centroid = None
self._max_z = None self._max_z = None
self._max_y = None self._max_y = None
self._max_x = None self._max_x = None
self._min_z = None self._min_z = None
self._min_y = None self._min_y = None
self._min_x = None self._min_x = None
def _position_of(self, point, face): def _position_of(self, point, face):
""" """
position of a specific point in the list of points that define a face position of a specific point in the list of points that define a face
:return: int :return: int
""" """
vertices = self.vertices vertices = self.vertices
for i in range(len(vertices)): for i in range(len(vertices)):
# ensure not duplicated vertex # ensure not duplicated vertex
power = 0 power = 0
vertex2 = vertices[i] vertex2 = vertices[i]
for dimension in range(0, 3): for dimension in range(0, 3):
power += math.pow(vertex2[dimension] - point[dimension], 2) power += math.pow(vertex2[dimension] - point[dimension], 2)
distance = math.sqrt(power) distance = math.sqrt(power)
if i not in face and distance == 0: if i not in face and distance == 0:
return i return i
return -1 return -1
@property @property
def vertices(self) -> np.ndarray: def vertices(self) -> np.ndarray:
""" """
Get polyhedron vertices Get polyhedron vertices
:return: np.ndarray(int) :return: np.ndarray(int)
""" """
if self._vertices is None: if self._vertices is None:
vertices, self._vertices = [], [] vertices, self._vertices = [], []
_ = [vertices.extend(s.coordinates) for s in self._polygons] _ = [vertices.extend(s.coordinates) for s in self._polygons]
for vertex_1 in vertices: for vertex_1 in vertices:
found = False found = False
for vertex_2 in self._vertices: for vertex_2 in self._vertices:
found = False found = False
power = 0 power = 0
for dimension in range(0, 3): for dimension in range(0, 3):
power += math.pow(vertex_2[dimension] - vertex_1[dimension], 2) power += math.pow(vertex_2[dimension] - vertex_1[dimension], 2)
distance = math.sqrt(power) distance = math.sqrt(power)
if distance == 0: if distance == 0:
found = True found = True
break break
if not found: if not found:
self._vertices.append(vertex_1) self._vertices.append(vertex_1)
self._vertices = np.asarray(self._vertices) self._vertices = np.asarray(self._vertices)
return self._vertices return self._vertices
@property @property
def faces(self) -> List[List[int]]: def faces(self) -> List[List[int]]:
""" """
Get polyhedron triangular faces Get polyhedron triangular faces
:return: [face] :return: [face]
""" """
if self._faces is None: if self._faces is None:
self._faces = [] self._faces = []
for polygon in self._polygons: for polygon in self._polygons:
face = [] face = []
points = polygon.coordinates points = polygon.coordinates
if len(points) != 3: if len(points) != 3:
sub_polygons = polygon.triangulate() sub_polygons = polygon.triangulate()
# todo: I modified this! To be checked @Guille # todo: I modified this! To be checked @Guille
if len(sub_polygons) >= 1: if len(sub_polygons) >= 1:
for sub_polygon in sub_polygons: for sub_polygon in sub_polygons:
face = [] face = []
points = sub_polygon.coordinates points = sub_polygon.coordinates
for point in points: for point in points:
face.append(self._position_of(point, face)) face.append(self._position_of(point, face))
self._faces.append(face) self._faces.append(face)
else: else:
for point in points: for point in points:
face.append(self._position_of(point, face)) face.append(self._position_of(point, face))
self._faces.append(face) self._faces.append(face)
return self._faces return self._faces
@property @property
def trimesh(self) -> Union[Trimesh, None]: def trimesh(self) -> Union[Trimesh, None]:
""" """
Get polyhedron trimesh Get polyhedron trimesh
:return: Trimesh :return: Trimesh
""" """
if self._trimesh is None: if self._trimesh is None:
for face in self.faces: for face in self.faces:
if len(face) != 3: if len(face) != 3:
sys.stderr.write('Not able to generate trimesh\n') sys.stderr.write('Not able to generate trimesh\n')
return None return None
self._trimesh = Trimesh(vertices=self.vertices, faces=self.faces) self._trimesh = Trimesh(vertices=self.vertices, faces=self.faces)
return self._trimesh return self._trimesh
@property @property
def volume(self): def volume(self):
""" """
Get polyhedron volume in cubic meters Get polyhedron volume in cubic meters
:return: float :return: float
""" """
if self._volume is None: if self._volume is None:
if self.trimesh is None: if self.trimesh is None:
self._volume = np.inf self._volume = np.inf
elif not self.trimesh.is_volume: elif not self.trimesh.is_volume:
self._volume = np.inf self._volume = np.inf
else: else:
self._volume = self.trimesh.volume self._volume = self.trimesh.volume
return self._volume return self._volume
@property @property
def max_z(self): def max_z(self):
""" """
Get polyhedron maximal z value in meters Get polyhedron maximal z value in meters
:return: float :return: float
""" """
if self._max_z is None: if self._max_z is None:
self._max_z = ConfigurationHelper().min_coordinate self._max_z = ConfigurationHelper().min_coordinate
for polygon in self._polygons: for polygon in self._polygons:
for point in polygon.coordinates: for point in polygon.coordinates:
self._max_z = max(self._max_z, point[2]) self._max_z = max(self._max_z, point[2])
return self._max_z return self._max_z
@property @property
def max_y(self): def max_y(self):
""" """
Get polyhedron maximal y value in meters Get polyhedron maximal y value in meters
:return: float :return: float
""" """
if self._max_y is None: if self._max_y is None:
self._max_y = ConfigurationHelper().min_coordinate self._max_y = ConfigurationHelper().min_coordinate
for polygon in self._polygons: for polygon in self._polygons:
for point in polygon.coordinates: for point in polygon.coordinates:
if self._max_y < point[1]: if self._max_y < point[1]:
self._max_y = point[1] self._max_y = point[1]
return self._max_y return self._max_y
@property @property
def max_x(self): def max_x(self):
""" """
Get polyhedron maximal x value in meters Get polyhedron maximal x value in meters
:return: float :return: float
""" """
if self._max_x is None: if self._max_x is None:
self._max_x = ConfigurationHelper().min_coordinate self._max_x = ConfigurationHelper().min_coordinate
for polygon in self._polygons: for polygon in self._polygons:
for point in polygon.coordinates: for point in polygon.coordinates:
self._max_x = max(self._max_x, point[0]) self._max_x = max(self._max_x, point[0])
return self._max_x return self._max_x
@property @property
def min_z(self): def min_z(self):
""" """
Get polyhedron minimal z value in meters Get polyhedron minimal z value in meters
:return: float :return: float
""" """
if self._min_z is None: if self._min_z is None:
self._min_z = self.max_z self._min_z = self.max_z
for polygon in self._polygons: for polygon in self._polygons:
for point in polygon.coordinates: for point in polygon.coordinates:
if self._min_z > point[2]: if self._min_z > point[2]:
self._min_z = point[2] self._min_z = point[2]
return self._min_z return self._min_z
@property @property
def min_y(self): def min_y(self):
""" """
Get polyhedron minimal y value in meters Get polyhedron minimal y value in meters
:return: float :return: float
""" """
if self._min_y is None: if self._min_y is None:
self._min_y = self.max_y self._min_y = self.max_y
for polygon in self._polygons: for polygon in self._polygons:
for point in polygon.coordinates: for point in polygon.coordinates:
if self._min_y > point[1]: if self._min_y > point[1]:
self._min_y = point[1] self._min_y = point[1]
return self._min_y return self._min_y
@property @property
def min_x(self): def min_x(self):
""" """
Get polyhedron minimal x value in meters Get polyhedron minimal x value in meters
:return: float :return: float
""" """
if self._min_x is None: if self._min_x is None:
self._min_x = self.max_x self._min_x = self.max_x
for polygon in self._polygons: for polygon in self._polygons:
for point in polygon.coordinates: for point in polygon.coordinates:
if self._min_x > point[0]: if self._min_x > point[0]:
self._min_x = point[0] self._min_x = point[0]
return self._min_x return self._min_x
@property @property
def centroid(self) -> Union[None, List[float]]: def centroid(self) -> Union[None, List[float]]:
""" """
Get polyhedron centroid Get polyhedron centroid
:return: [x,y,z] :return: [x,y,z]
""" """
if self._centroid is None: if self._centroid is None:
trimesh = self.trimesh trimesh = self.trimesh
if trimesh is None: if trimesh is None:
return None return None
self._centroid = self.trimesh.centroid self._centroid = self.trimesh.centroid
return self._centroid return self._centroid
def stl_export(self, full_path): def stl_export(self, full_path):
""" """
Export the polyhedron to stl given file Export the polyhedron to stl given file
:param full_path: str :param full_path: str
:return: None :return: None
""" """
self.trimesh.export(full_path, 'stl_ascii') self.trimesh.export(full_path, 'stl_ascii')
def obj_export(self, full_path): def obj_export(self, full_path):
""" """
Export the polyhedron to obj given file Export the polyhedron to obj given file
:param full_path: str :param full_path: str
:return: None :return: None
""" """
self.trimesh.export(full_path, 'obj') self.trimesh.export(full_path, 'obj')
def show(self): def show(self):
""" """
Auxiliary function to render the polyhedron Auxiliary function to render the polyhedron
:return: None :return: None
""" """
self.trimesh.show() self.trimesh.show()

View File

@ -14,14 +14,14 @@ from city_model_structure.building_demand.usage_zone import UsageZone
from city_model_structure.building_demand.storey import Storey from city_model_structure.building_demand.storey import Storey
from city_model_structure.city_object import CityObject from city_model_structure.city_object import CityObject
from city_model_structure.building_demand.household import Household from city_model_structure.building_demand.household import Household
from city_model_structure.attributes.polyhedron import Polyhedron
class Building(CityObject): class Building(CityObject):
""" """
Building(CityObject) class Building(CityObject) class
""" """
def __init__(self, name, lod, surfaces, year_of_construction, function, def __init__(self, name, lod, surfaces, year_of_construction, function, city_lower_corner, terrains=None):
city_lower_corner, terrains=None):
super().__init__(name, lod, surfaces, city_lower_corner) super().__init__(name, lod, surfaces, city_lower_corner)
self._households = None self._households = None
self._basement_heated = None self._basement_heated = None
@ -34,6 +34,7 @@ class Building(CityObject):
self._floor_area = None self._floor_area = None
self._roof_type = None self._roof_type = None
self._storeys = None self._storeys = None
self._geometrical_zones = None
self._thermal_zones = [] self._thermal_zones = []
self._thermal_boundaries = None self._thermal_boundaries = None
self._usage_zones = [] self._usage_zones = []
@ -60,6 +61,15 @@ class Building(CityObject):
else: else:
self._internal_walls.append(surface) self._internal_walls.append(surface)
@property
def geometrical_zones(self) -> List[Polyhedron]:
if self._geometrical_zones is None:
polygons = []
for surface in self.surfaces:
polygons.append(surface.perimeter_polygon)
self._geometrical_zones = [Polyhedron(polygons)]
return self._geometrical_zones
@property @property
def grounds(self) -> List[Surface]: def grounds(self) -> List[Surface]:
""" """
@ -256,10 +266,6 @@ class Building(CityObject):
if value is not None: if value is not None:
self._storeys_above_ground = int(value) self._storeys_above_ground = int(value)
@staticmethod
def _tuple_to_point(xy_tuple):
return [xy_tuple[0], xy_tuple[1], 0.0]
@property @property
def heating(self) -> dict: def heating(self) -> dict:
""" """

View File

@ -8,7 +8,7 @@ contributors Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca
from __future__ import annotations from __future__ import annotations
import uuid import uuid
import numpy as np import numpy as np
from typing import Union from typing import List, Union
from city_model_structure.attributes.polygon import Polygon from city_model_structure.attributes.polygon import Polygon
from city_model_structure.attributes.plane import Plane from city_model_structure.attributes.plane import Plane
from city_model_structure.attributes.point import Point from city_model_structure.attributes.point import Point
@ -233,8 +233,16 @@ class Surface:
""" """
return self._solid_polygon return self._solid_polygon
@solid_polygon.setter
def solid_polygon(self, value):
"""
Set the solid surface
:return: Polygon
"""
self._solid_polygon = value
@property @property
def holes_polygons(self) -> Union[Polygon, None]: def holes_polygons(self) -> Union[List[Polygon], None]:
""" """
Get hole surfaces, a list of hole polygons found in the surface Get hole surfaces, a list of hole polygons found in the surface
:return: None, [] or [Polygon] :return: None, [] or [Polygon]
@ -244,6 +252,14 @@ class Surface:
""" """
return self._holes_polygons return self._holes_polygons
@holes_polygons.setter
def holes_polygons(self, value):
"""
Set the hole surfaces
:param value: [Polygon]
"""
self._holes_polygons = value
@property @property
def pv_system_installed(self) -> PvSystem: def pv_system_installed(self) -> PvSystem:
""" """

View File

@ -30,6 +30,10 @@ class ExportsFactory:
""" """
raise NotImplementedError raise NotImplementedError
@property
def _collada(self):
raise NotImplementedError
@property @property
def _energy_ade(self): def _energy_ade(self):
""" """

View File

@ -87,3 +87,7 @@ HVAC_AVAILABILITY = 'HVAC Avail'
INFILTRATION = 'Infiltration' INFILTRATION = 'Infiltration'
COOLING_SET_POINT = 'ClgSetPt' COOLING_SET_POINT = 'ClgSetPt'
HEATING_SET_POINT = 'HtgSetPt' HEATING_SET_POINT = 'HtgSetPt'
# Geometry
EPSILON = 0.0000001

View File

@ -3,27 +3,40 @@ Rhino module parses rhino files and import the geometry into the city model stru
SPDX - License - Identifier: LGPL - 3.0 - or -later SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2020 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca Copyright © 2020 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
""" """
from numpy import inf
from rhino3dm import * from rhino3dm import *
from rhino3dm._rhino3dm import MeshType from rhino3dm._rhino3dm import MeshType
from city_model_structure.attributes.point import Point
import numpy as np import numpy as np
import helpers.configuration_helper from helpers.configuration_helper import ConfigurationHelper
from city_model_structure.attributes.polygon import Polygon from city_model_structure.attributes.polygon import Polygon
from city_model_structure.building import Building from city_model_structure.building import Building
from city_model_structure.city import City from city_model_structure.city import City
from city_model_structure.building_demand.surface import Surface as LibsSurface from city_model_structure.building_demand.surface import Surface as LibsSurface
from helpers.constants import EPSILON
from imports.geometry.helpers.geometry_helper import GeometryHelper from imports.geometry.helpers.geometry_helper import GeometryHelper
from helpers.configuration_helper import ConfigurationHelper
class Rhino: class Rhino:
def __init__(self, path): def __init__(self, path):
self._model = File3dm.Read(str(path)) self._model = File3dm.Read(str(path))
max_float = 1.7976931348623157e+308 max_float = float(ConfigurationHelper().max_coordinate)
min_float = -1.7976931348623157e+308 min_float = float(ConfigurationHelper().min_coordinate)
self._min_x = self._min_y = self._min_z = max_float self._min_x = self._min_y = self._min_z = max_float
self._max_x = self._max_y = self._max_z = min_float self._max_x = self._max_y = self._max_z = min_float
print('init')
@staticmethod
def _in_perimeter(wall, corner):
res = wall.contains_point(Point(corner))
print(f'belong: {res} wall:({wall.coordinates}) corner: ({corner})')
return res
@staticmethod
def _add_hole(solid_polygon, hole):
first = solid_polygon.points[0]
points = first + hole.points + solid_polygon.points
return Polygon(points)
@staticmethod @staticmethod
def _solid_points(coordinates) -> np.ndarray: def _solid_points(coordinates) -> np.ndarray:
@ -47,8 +60,10 @@ class Rhino:
@property @property
def city(self) -> City: def city(self) -> City:
rhino_objects = []
buildings = [] buildings = []
print('city') windows = []
holes = []
for obj in self._model.Objects: for obj in self._model.Objects:
name = obj.Attributes.Id name = obj.Attributes.Id
surfaces = [] surfaces = []
@ -56,20 +71,45 @@ class Rhino:
if face is None: if face is None:
break break
_mesh = face.GetMesh(MeshType.Default) _mesh = face.GetMesh(MeshType.Default)
polygon_points = None
for i in range(0, len(_mesh.Faces)): for i in range(0, len(_mesh.Faces)):
faces = _mesh.Faces[i] faces = _mesh.Faces[i]
_points = '' _points = ''
for index in faces: for index in faces:
self._lower_corner(_mesh.Vertices[index].X, _mesh.Vertices[index].Y, _mesh.Vertices[index].Z) self._lower_corner(_mesh.Vertices[index].X, _mesh.Vertices[index].Y, _mesh.Vertices[index].Z)
_points = _points + f'{_mesh.Vertices[index].X} {_mesh.Vertices[index].Y} {_mesh.Vertices[index].Z} ' _points = _points + f'{_mesh.Vertices[index].X} {_mesh.Vertices[index].Y} {_mesh.Vertices[index].Z} '
polygon_points = Rhino._solid_points(_points.strip()) polygon_points = Rhino._solid_points(_points.strip())
print(_points)
surfaces.append(LibsSurface(Polygon(polygon_points), Polygon(polygon_points))) surfaces.append(LibsSurface(Polygon(polygon_points), Polygon(polygon_points)))
buildings.append(Building(name, 3, surfaces, 'unknown', 'unknown', (self._min_x, self._min_y, self._min_z), [])) rhino_objects.append(Building(name, 3, surfaces, 'unknown', 'unknown', (self._min_x, self._min_y, self._min_z), []))
lower_corner = (self._min_x, self._min_y, self._min_z) lower_corner = (self._min_x, self._min_y, self._min_z)
upper_corner = (self._max_x, self._max_y, self._max_z) upper_corner = (self._max_x, self._max_y, self._max_z)
city = City(lower_corner, upper_corner, 'Montreal') city = City(lower_corner, upper_corner, 'EPSG:26918')
for rhino_object in rhino_objects:
if rhino_object.volume is inf:
# is not a building but a window!
for surface in rhino_object.surfaces:
# add to windows the "hole" with the normal inverted
print('window')
windows.append(Polygon(surface.perimeter_polygon.inverse))
else:
buildings.append(rhino_object)
# todo: this method will be pretty inefficient
for hole in windows:
corner = hole.coordinates[0]
for building in buildings:
for surface in building.surfaces:
plane = surface.perimeter_polygon.plane
if plane.distance(corner) <= EPSILON:
# todo: this is a hack for dompark project it should not be done this way
# check if the window is in the right high.
if surface.upper_corner[2] >= corner[2] >= surface.lower_corner[2]:
if surface.holes_polygons is None:
surface.holes_polygons = []
surface.holes_polygons.append(hole)
for building in buildings: for building in buildings:
city.add_city_object(building) city.add_city_object(building)
return city return city

View File

@ -8,7 +8,7 @@ from city_model_structure.city import City
from imports.geometry.citygml import CityGml from imports.geometry.citygml import CityGml
from imports.geometry.obj import Obj from imports.geometry.obj import Obj
from imports.geometry.osm_subway import OsmSubway from imports.geometry.osm_subway import OsmSubway
#from imports.geometry.rhino import Rhino from imports.geometry.rhino import Rhino
class GeometryFactory: class GeometryFactory:
@ -43,13 +43,13 @@ class GeometryFactory:
""" """
return OsmSubway(self._path).city return OsmSubway(self._path).city
# @property @property
# def _rhino(self) -> City: def _rhino(self) -> City:
# """ """
# Enrich the city by using OpenStreetMap information as data source Enrich the city by using OpenStreetMap information as data source
# :return: City :return: City
# """ """
# return Rhino(self._path).city return Rhino(self._path).city
@property @property
def city(self) -> City: def city(self) -> City:
@ -59,10 +59,10 @@ class GeometryFactory:
""" """
return getattr(self, self._file_type, lambda: None) return getattr(self, self._file_type, lambda: None)
# @property @property
# def city_debug(self) -> City: def city_debug(self) -> City:
# """ """
# Enrich the city given to the class using the class given handler Enrich the city given to the class using the class given handler
# :return: City :return: City
# """ """
# return Rhino(self._path).city return Rhino(self._path).city