forked from s_ranjbar/city_retrofit
Correct rhino read
This commit is contained in:
parent
d430abfefd
commit
0ec80dacc0
|
@ -2,20 +2,22 @@
|
||||||
Rhino module parses rhino files and import the geometry into the city model structure
|
Rhino module parses rhino files and import the geometry into the city model structure
|
||||||
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
||||||
Copyright © 2022 Concordia CERC group
|
Copyright © 2022 Concordia CERC group
|
||||||
Project Coder Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
|
Project Coder Guille Gutierrez guillermo.gutierrezmorote@concordia.capip
|
||||||
"""
|
"""
|
||||||
from numpy import inf
|
from numpy import inf
|
||||||
from rhino3dm import *
|
from rhino3dm import *
|
||||||
|
from rhino3dm._rhino3dm import Extrusion
|
||||||
from rhino3dm._rhino3dm import MeshType
|
from rhino3dm._rhino3dm import MeshType
|
||||||
|
|
||||||
|
|
||||||
from city_model_structure.attributes.point import Point
|
from city_model_structure.attributes.point import Point
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
from city_model_structure.building import Building
|
||||||
from helpers.configuration_helper import ConfigurationHelper
|
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.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 HubSurface
|
||||||
from imports.geometry.helpers.geometry_helper import GeometryHelper
|
from imports.geometry.helpers.geometry_helper import GeometryHelper
|
||||||
|
|
||||||
|
|
||||||
|
@ -42,60 +44,110 @@ class Rhino:
|
||||||
def _solid_points(coordinates) -> np.ndarray:
|
def _solid_points(coordinates) -> np.ndarray:
|
||||||
solid_points = np.fromstring(coordinates, dtype=float, sep=' ')
|
solid_points = np.fromstring(coordinates, dtype=float, sep=' ')
|
||||||
solid_points = GeometryHelper.to_points_matrix(solid_points)
|
solid_points = GeometryHelper.to_points_matrix(solid_points)
|
||||||
|
|
||||||
|
result = []
|
||||||
|
found = False
|
||||||
|
for row in solid_points:
|
||||||
|
for row2 in result:
|
||||||
|
if row[0] == row2[0] and row[1] == row2[1] and row[2] == row2[2]:
|
||||||
|
found = True
|
||||||
|
if not found:
|
||||||
|
result.append(row)
|
||||||
return solid_points
|
return solid_points
|
||||||
|
|
||||||
def _lower_corner(self, x, y, z):
|
def _corners(self, point):
|
||||||
if x < self._min_x:
|
if point.X < self._min_x:
|
||||||
self._min_x = x
|
self._min_x = point.X
|
||||||
if y < self._min_y:
|
if point.Y < self._min_y:
|
||||||
self._min_y = y
|
self._min_y = point.Y
|
||||||
if z < self._min_z:
|
if point.Z < self._min_z:
|
||||||
self._min_z = z
|
self._min_z = point.Z
|
||||||
if x > self._max_x:
|
if point.X > self._max_x:
|
||||||
self._max_x = x
|
self._max_x = point.X
|
||||||
if y > self._max_y:
|
if point.Y > self._max_y:
|
||||||
self._max_y = y
|
self._max_y = point.Y
|
||||||
if z > self._max_z:
|
if point.Z > self._max_z:
|
||||||
self._max_z = z
|
self._max_z = point.Z
|
||||||
|
|
||||||
|
def _add_face(self, face):
|
||||||
|
hub_surfaces = []
|
||||||
|
_mesh = face.GetMesh(MeshType.Default)
|
||||||
|
for i in range(0, len(_mesh.Faces)):
|
||||||
|
mesh_faces = _mesh.Faces[i]
|
||||||
|
_points = ''
|
||||||
|
for index in mesh_faces:
|
||||||
|
self._corners(_mesh.Vertices[index])
|
||||||
|
_points = _points + f'{_mesh.Vertices[index].X} {_mesh.Vertices[index].Y} {_mesh.Vertices[index].Z} '
|
||||||
|
polygon_points = Rhino._solid_points(_points.strip())
|
||||||
|
hub_surfaces.append(HubSurface(Polygon(polygon_points), Polygon(polygon_points)))
|
||||||
|
return hub_surfaces
|
||||||
|
|
||||||
|
def _add_face2(self, face):
|
||||||
|
hub_surfaces = []
|
||||||
|
_mesh = face.GetMesh(MeshType.Default)
|
||||||
|
for i in range(0, len(_mesh.Faces)):
|
||||||
|
mesh_faces = _mesh.Faces[i]
|
||||||
|
_points = ''
|
||||||
|
for index in mesh_faces:
|
||||||
|
self._corners(_mesh.Vertices[index])
|
||||||
|
_points = _points + f'{_mesh.Vertices[index].X} {_mesh.Vertices[index].Y} {_mesh.Vertices[index].Z} '
|
||||||
|
polygon_points = Rhino._solid_points(_points.strip())
|
||||||
|
x = 'x = ['
|
||||||
|
y = 'y = ['
|
||||||
|
z = 'z = ['
|
||||||
|
for point in polygon_points:
|
||||||
|
if x == 'x = [':
|
||||||
|
x = f'{x}{point[0]}'
|
||||||
|
y = f'{y}{point[1]}'
|
||||||
|
z = f'{z}{point[2]}'
|
||||||
|
else:
|
||||||
|
x = f'{x}, {point[0]}'
|
||||||
|
y = f'{y}, {point[1]}'
|
||||||
|
z = f'{z}, {point[2]}'
|
||||||
|
x = f'{x}]'
|
||||||
|
y = f'{y}]'
|
||||||
|
z = f'{z}]'
|
||||||
|
print(x)
|
||||||
|
print(y)
|
||||||
|
print(z)
|
||||||
|
print('_add(ax, x, y, z)')
|
||||||
|
hub_surfaces.append(HubSurface(Polygon(polygon_points), Polygon(polygon_points)))
|
||||||
|
return hub_surfaces
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def city(self) -> City:
|
def city(self) -> City:
|
||||||
rhino_objects = []
|
i = 0
|
||||||
buildings = []
|
buildings = []
|
||||||
|
city_objects = [] # building and "windows"
|
||||||
windows = []
|
windows = []
|
||||||
holes = []
|
_prev_name = ''
|
||||||
|
|
||||||
for obj in self._model.Objects:
|
for obj in self._model.Objects:
|
||||||
name = obj.Attributes.Id
|
name = obj.Attributes.Id
|
||||||
surfaces = []
|
hub_surfaces = []
|
||||||
try:
|
if isinstance(obj.Geometry, Extrusion):
|
||||||
|
surface = obj.Geometry
|
||||||
|
hub_surfaces = hub_surfaces + self._add_face(surface)
|
||||||
|
else:
|
||||||
for face in obj.Geometry.Faces:
|
for face in obj.Geometry.Faces:
|
||||||
if face is None:
|
if face is None:
|
||||||
break
|
break
|
||||||
_mesh = face.GetMesh(MeshType.Default)
|
hub_surfaces = hub_surfaces + self._add_face(face)
|
||||||
polygon_points = None
|
building = Building(name, 3, hub_surfaces, 'unknown', 'unknown', (self._min_x, self._min_y, self._min_z), [])
|
||||||
for i in range(0, len(_mesh.Faces)):
|
city_objects.append(building)
|
||||||
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
|
|
||||||
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, 'EPSG:26918')
|
city = City(lower_corner, upper_corner, 'EPSG:26918')
|
||||||
for rhino_object in rhino_objects:
|
for building in city_objects:
|
||||||
if rhino_object.volume is inf:
|
if len(building.surfaces) <= 2:
|
||||||
# is not a building but a window!
|
# is not a building but a window!
|
||||||
for surface in rhino_object.surfaces:
|
for surface in building.surfaces:
|
||||||
# add to windows the "hole" with the normal inverted
|
# add to windows the "hole" with the normal inverted
|
||||||
|
|
||||||
windows.append(Polygon(surface.perimeter_polygon.inverse))
|
windows.append(Polygon(surface.perimeter_polygon.inverse))
|
||||||
else:
|
else:
|
||||||
buildings.append(rhino_object)
|
buildings.append(building)
|
||||||
|
|
||||||
# todo: this method will be pretty inefficient
|
# todo: this method will be pretty inefficient
|
||||||
for hole in windows:
|
for hole in windows:
|
||||||
corner = hole.coordinates[0]
|
corner = hole.coordinates[0]
|
||||||
|
@ -115,5 +167,3 @@ class Rhino:
|
||||||
for building in buildings:
|
for building in buildings:
|
||||||
city.add_city_object(building)
|
city.add_city_object(building)
|
||||||
return city
|
return city
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ Project Coder Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
from imports.geometry_factory import GeometryFactory
|
from imports.geometry_factory import GeometryFactory
|
||||||
|
from imports.construction_factory import ConstructionFactory
|
||||||
|
|
||||||
|
|
||||||
class TestGeometryFactory(TestCase):
|
class TestGeometryFactory(TestCase):
|
||||||
|
@ -35,6 +36,12 @@ class TestGeometryFactory(TestCase):
|
||||||
self.assertIsNotNone(self._city, 'city is none')
|
self.assertIsNotNone(self._city, 'city is none')
|
||||||
return self._city
|
return self._city
|
||||||
|
|
||||||
|
def _get_rhino(self, file):
|
||||||
|
file_path = (self._example_path / file).resolve()
|
||||||
|
self._city = GeometryFactory('rhino', file_path).city_debug
|
||||||
|
self.assertIsNotNone(self._city, 'city is none')
|
||||||
|
return self._city
|
||||||
|
|
||||||
def _check_buildings(self, city):
|
def _check_buildings(self, city):
|
||||||
for building in city.buildings:
|
for building in city.buildings:
|
||||||
self.assertIsNotNone(building.name, 'building name is none')
|
self.assertIsNotNone(building.name, 'building name is none')
|
||||||
|
@ -105,6 +112,18 @@ class TestGeometryFactory(TestCase):
|
||||||
self._check_buildings(city)
|
self._check_buildings(city)
|
||||||
for building in city.buildings:
|
for building in city.buildings:
|
||||||
self._check_surfaces(building)
|
self._check_surfaces(building)
|
||||||
|
building.year_of_construction = 2006
|
||||||
|
city = ConstructionFactory('nrel', city).enrich()
|
||||||
|
|
||||||
|
# rhino
|
||||||
|
def test_import_rhino(self):
|
||||||
|
"""
|
||||||
|
Test rhino import
|
||||||
|
"""
|
||||||
|
file = 'dompark.3dm'
|
||||||
|
city = self._get_rhino(file)
|
||||||
|
self.assertIsNotNone(city, 'city is none')
|
||||||
|
self.assertTrue(len(city.buildings) == 36)
|
||||||
|
|
||||||
# obj
|
# obj
|
||||||
def test_import_obj(self):
|
def test_import_obj(self):
|
||||||
|
@ -128,7 +147,6 @@ class TestGeometryFactory(TestCase):
|
||||||
file_path = (self._example_path / 'subway.osm').resolve()
|
file_path = (self._example_path / 'subway.osm').resolve()
|
||||||
|
|
||||||
city = GeometryFactory('osm_subway', file_path).city
|
city = GeometryFactory('osm_subway', file_path).city
|
||||||
print(len(city.city_objects))
|
|
||||||
|
|
||||||
self.assertIsNotNone(city, 'subway entrances is none')
|
self.assertIsNotNone(city, 'subway entrances is none')
|
||||||
self.assertEqual(len(city.city_objects), 20, 'Wrong number of subway entrances')
|
self.assertEqual(len(city.city_objects), 20, 'Wrong number of subway entrances')
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user