2021-10-25 13:28:43 -04:00
|
|
|
"""
|
|
|
|
Rhino module parses rhino files and import the geometry into the city model structure
|
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
|
|
Copyright © 2020 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
|
|
|
|
"""
|
2022-02-16 15:08:05 -05:00
|
|
|
from numpy import inf
|
2021-10-25 13:28:43 -04:00
|
|
|
from rhino3dm import *
|
|
|
|
from rhino3dm._rhino3dm import MeshType
|
|
|
|
|
|
|
|
from city_model_structure.attributes.polygon import Polygon
|
|
|
|
from city_model_structure.building import Building
|
|
|
|
from city_model_structure.building_demand.surface import Surface as LibsSurface
|
2022-03-02 12:13:11 -05:00
|
|
|
from city_model_structure.city import City
|
|
|
|
from helpers.configuration_helper import ConfigurationHelper
|
2021-10-25 13:28:43 -04:00
|
|
|
from imports.geometry.helpers.geometry_helper import GeometryHelper
|
|
|
|
|
|
|
|
|
|
|
|
class Rhino:
|
|
|
|
def __init__(self, path):
|
|
|
|
self._model = File3dm.Read(str(path))
|
2022-02-16 15:08:05 -05:00
|
|
|
max_float = float(ConfigurationHelper().max_coordinate)
|
|
|
|
min_float = float(ConfigurationHelper().min_coordinate)
|
2021-10-25 13:28:43 -04:00
|
|
|
self._min_x = self._min_y = self._min_z = max_float
|
|
|
|
self._max_x = self._max_y = self._max_z = min_float
|
2022-02-16 15:08:05 -05:00
|
|
|
|
2021-10-25 13:28:43 -04:00
|
|
|
@staticmethod
|
2022-03-02 12:56:20 -05:00
|
|
|
def _solid_points(coordinates):
|
2021-10-25 13:28:43 -04:00
|
|
|
solid_points = np.fromstring(coordinates, dtype=float, sep=' ')
|
|
|
|
solid_points = GeometryHelper.to_points_matrix(solid_points)
|
|
|
|
return solid_points
|
|
|
|
|
|
|
|
def _lower_corner(self, x, y, z):
|
|
|
|
if x < self._min_x:
|
|
|
|
self._min_x = x
|
|
|
|
if y < self._min_y:
|
|
|
|
self._min_y = y
|
|
|
|
if z < self._min_z:
|
|
|
|
self._min_z = z
|
|
|
|
if x > self._max_x:
|
|
|
|
self._max_x = x
|
|
|
|
if y > self._max_y:
|
|
|
|
self._max_y = y
|
|
|
|
if z > self._max_z:
|
|
|
|
self._max_z = z
|
|
|
|
|
|
|
|
@property
|
|
|
|
def city(self) -> City:
|
2022-02-16 15:08:05 -05:00
|
|
|
rhino_objects = []
|
2021-10-25 13:28:43 -04:00
|
|
|
buildings = []
|
2022-02-16 15:08:05 -05:00
|
|
|
windows = []
|
|
|
|
holes = []
|
2021-10-25 13:28:43 -04:00
|
|
|
for obj in self._model.Objects:
|
|
|
|
name = obj.Attributes.Id
|
|
|
|
surfaces = []
|
2022-02-17 11:35:34 -05:00
|
|
|
try:
|
|
|
|
for face in obj.Geometry.Faces:
|
|
|
|
if face is None:
|
|
|
|
break
|
|
|
|
_mesh = face.GetMesh(MeshType.Default)
|
|
|
|
polygon_points = None
|
|
|
|
for i in range(0, len(_mesh.Faces)):
|
|
|
|
faces = _mesh.Faces[i]
|
|
|
|
_points = ''
|
|
|
|
for index in faces:
|
|
|
|
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} '
|
|
|
|
polygon_points = Rhino._solid_points(_points.strip())
|
|
|
|
surfaces.append(LibsSurface(Polygon(polygon_points), Polygon(polygon_points)))
|
|
|
|
except AttributeError:
|
|
|
|
continue
|
2022-02-16 15:08:05 -05:00
|
|
|
rhino_objects.append(Building(name, 3, surfaces, 'unknown', 'unknown', (self._min_x, self._min_y, self._min_z), []))
|
2021-10-25 13:28:43 -04:00
|
|
|
lower_corner = (self._min_x, self._min_y, self._min_z)
|
|
|
|
upper_corner = (self._max_x, self._max_y, self._max_z)
|
2022-02-16 15:08:05 -05:00
|
|
|
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
|
2022-02-17 11:35:34 -05:00
|
|
|
|
2022-02-16 15:08:05 -05:00
|
|
|
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
|
2022-02-17 11:35:34 -05:00
|
|
|
# todo: this is a hack for dompark project it should not be done this way windows should be correctly modeled
|
|
|
|
# if the distance between the wall plane and the window is less than 2m
|
|
|
|
# and the window Z coordinate it's between the wall Z, it's a window of that wall
|
|
|
|
if plane.distance(corner) <= 2:
|
2022-02-16 15:08:05 -05:00
|
|
|
# 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)
|
|
|
|
|
2021-10-25 13:28:43 -04:00
|
|
|
for building in buildings:
|
|
|
|
city.add_city_object(building)
|
|
|
|
return city
|
|
|
|
|
|
|
|
|