diff --git a/imports/geometry/rhino.py b/imports/geometry/rhino.py index bdee4a41..3e394866 100644 --- a/imports/geometry/rhino.py +++ b/imports/geometry/rhino.py @@ -2,20 +2,22 @@ Rhino module parses rhino files and import the geometry into the city model structure SPDX - License - Identifier: LGPL - 3.0 - or -later 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 rhino3dm import * +from rhino3dm._rhino3dm import Extrusion from rhino3dm._rhino3dm import MeshType + from city_model_structure.attributes.point import Point import numpy as np +from city_model_structure.building import Building from helpers.configuration_helper import ConfigurationHelper 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 city_model_structure.building_demand.surface import Surface as HubSurface from imports.geometry.helpers.geometry_helper import GeometryHelper @@ -42,60 +44,110 @@ class Rhino: def _solid_points(coordinates) -> np.ndarray: solid_points = np.fromstring(coordinates, dtype=float, sep=' ') 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 - 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 + def _corners(self, point): + if point.X < self._min_x: + self._min_x = point.X + if point.Y < self._min_y: + self._min_y = point.Y + if point.Z < self._min_z: + self._min_z = point.Z + if point.X > self._max_x: + self._max_x = point.X + if point.Y > self._max_y: + self._max_y = point.Y + if point.Z > self._max_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 def city(self) -> City: - rhino_objects = [] + i = 0 buildings = [] + city_objects = [] # building and "windows" windows = [] - holes = [] + _prev_name = '' + for obj in self._model.Objects: name = obj.Attributes.Id - surfaces = [] - try: + hub_surfaces = [] + if isinstance(obj.Geometry, Extrusion): + surface = obj.Geometry + hub_surfaces = hub_surfaces + self._add_face(surface) + else: 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 - rhino_objects.append(Building(name, 3, surfaces, 'unknown', 'unknown', (self._min_x, self._min_y, self._min_z), [])) + hub_surfaces = hub_surfaces + self._add_face(face) + building = Building(name, 3, hub_surfaces, 'unknown', 'unknown', (self._min_x, self._min_y, self._min_z), []) + city_objects.append(building) 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, 'EPSG:26918') - for rhino_object in rhino_objects: - if rhino_object.volume is inf: + for building in city_objects: + if len(building.surfaces) <= 2: # 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 - windows.append(Polygon(surface.perimeter_polygon.inverse)) else: - buildings.append(rhino_object) + buildings.append(building) + # todo: this method will be pretty inefficient for hole in windows: corner = hole.coordinates[0] @@ -114,6 +166,4 @@ class Rhino: for building in buildings: city.add_city_object(building) - return city - - + return city \ No newline at end of file diff --git a/unittests/test_geometry_factory.py b/unittests/test_geometry_factory.py index 7686f185..cce867da 100644 --- a/unittests/test_geometry_factory.py +++ b/unittests/test_geometry_factory.py @@ -7,6 +7,7 @@ Project Coder Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca from pathlib import Path from unittest import TestCase from imports.geometry_factory import GeometryFactory +from imports.construction_factory import ConstructionFactory class TestGeometryFactory(TestCase): @@ -35,6 +36,12 @@ class TestGeometryFactory(TestCase): self.assertIsNotNone(self._city, 'city is none') 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): for building in city.buildings: self.assertIsNotNone(building.name, 'building name is none') @@ -105,6 +112,18 @@ class TestGeometryFactory(TestCase): self._check_buildings(city) for building in city.buildings: 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 def test_import_obj(self): @@ -128,7 +147,6 @@ class TestGeometryFactory(TestCase): file_path = (self._example_path / 'subway.osm').resolve() city = GeometryFactory('osm_subway', file_path).city - print(len(city.city_objects)) self.assertIsNotNone(city, 'subway entrances is none') self.assertEqual(len(city.city_objects), 20, 'Wrong number of subway entrances') diff --git a/unittests/tests_data/dompark.3dm b/unittests/tests_data/dompark.3dm index fc2bca8b..da86349e 100644 Binary files a/unittests/tests_data/dompark.3dm and b/unittests/tests_data/dompark.3dm differ