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):
""" """

View File

@ -11,8 +11,10 @@ from typing import List
import numpy as np import numpy as np
from trimesh import Trimesh from trimesh import Trimesh
import trimesh.intersections import trimesh.intersections
from city_model_structure.attributes.point import Point
from city_model_structure.attributes.plane import Plane
from city_model_structure.attributes.point import Point
import helpers.constants as cte
class Polygon: class Polygon:
""" """
@ -20,7 +22,6 @@ class Polygon:
""" """
def __init__(self, coordinates): def __init__(self, coordinates):
self._area = None self._area = None
self._points = None self._points = None
self._points_list = None self._points_list = None
@ -31,6 +32,7 @@ class Polygon:
self._triangles = None self._triangles = None
self._vertices = None self._vertices = None
self._faces = None self._faces = None
self._plane = None
@property @property
def points(self) -> List[Point]: def points(self) -> List[Point]:
@ -44,6 +46,16 @@ class Polygon:
self._points.append(Point(coordinate)) self._points.append(Point(coordinate))
return self._points return self._points
@property
def plane(self) -> Plane:
"""
Get the polygon plane
:return: Plane
"""
if self._plane is None:
self._plane = Plane(normal=self.normal, origin=self.points[0])
return self._plane
@property @property
def coordinates(self) -> List[np.ndarray]: def coordinates(self) -> List[np.ndarray]:
""" """
@ -52,6 +64,62 @@ class Polygon:
""" """
return self._coordinates return self._coordinates
@staticmethod
def _module(vector):
x2 = vector[0] ** 2
y2 = vector[1] ** 2
z2 = vector[2] ** 2
return math.sqrt(x2+y2+z2)
@staticmethod
def _scalar_product(vector_0, vector_1):
x = vector_0[0] * vector_1[0]
y = vector_0[1] * vector_1[1]
z = vector_0[2] * vector_1[2]
return x+y+z
def contains_point(self, point):
"""
Determines if the given point is contained by the current polygon
:return: boolean
"""
# fixme: This method doesn't seems to work.
n = len(self.vertices)
angle_sum = 0
for i in range(0, n):
vector_0 = self.vertices[i]
vector_1 = self.vertices[(i+1) % n]
# set to origin
vector_0[0] = vector_0[0] - point.coordinates[0]
vector_0[1] = vector_0[1] - point.coordinates[1]
vector_0[2] = vector_0[2] - point.coordinates[2]
vector_1[0] = vector_1[0] - point.coordinates[0]
vector_1[1] = vector_1[1] - point.coordinates[1]
vector_1[2] = vector_1[2] - point.coordinates[2]
module = Polygon._module(vector_0) * Polygon._module(vector_1)
scalar_product = Polygon._scalar_product(vector_0, vector_1)
angle = np.pi/2
if module != 0:
angle = abs(np.arcsin(scalar_product / module))
angle_sum += angle
print(angle_sum)
return abs(angle_sum - math.pi*2) < cte.EPSILON
def contains_polygon(self, polygon):
"""
Determines if the given polygon is contained by the current polygon
:return: boolean
"""
print('contains')
for point in polygon.points:
print(point.coordinates, self.contains_point(point))
if not self.contains_point(point):
return False
print('Belong!')
return True
@property @property
def points_list(self) -> np.ndarray: def points_list(self) -> np.ndarray:
""" """

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