city_retrofit/imports/geometry/rhino.py

78 lines
2.7 KiB
Python

"""
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
"""
from rhino3dm import *
from rhino3dm._rhino3dm import MeshType
import numpy as np
import helpers.configuration_helper
from city_model_structure.attributes.polygon import Polygon
from city_model_structure.building import Building
from city_model_structure.city import City
from city_model_structure.building_demand.surface import Surface as LibsSurface
from imports.geometry.helpers.geometry_helper import GeometryHelper
from helpers.configuration_helper import ConfigurationHelper
class Rhino:
def __init__(self, path):
self._model = File3dm.Read(str(path))
max_float = 1.7976931348623157e+308
min_float = -1.7976931348623157e+308
self._min_x = self._min_y = self._min_z = max_float
self._max_x = self._max_y = self._max_z = min_float
print('init')
@staticmethod
def _solid_points(coordinates) -> np.ndarray:
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:
buildings = []
print('city')
for obj in self._model.Objects:
name = obj.Attributes.Id
surfaces = []
for face in obj.Geometry.Faces:
if face is None:
break
_mesh = face.GetMesh(MeshType.Default)
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())
print(_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), []))
lower_corner = (self._min_x, self._min_y, self._min_z)
upper_corner = (self._max_x, self._max_y, self._max_z)
city = City(lower_corner, upper_corner, 'Montreal')
for building in buildings:
city.add_city_object(building)
return city