From d979c74b8d7efc649630ebcc6133cb17a30dfd97 Mon Sep 17 00:00:00 2001 From: guille Date: Fri, 17 Mar 2023 13:36:43 -0400 Subject: [PATCH] partial; optimization --- hub/exports/formats/obj.py | 2 + hub/helpers/geometry_helper.py | 45 ++++++++++++++++++- hub/imports/geometry/geojson.py | 9 +++- .../geometry/helpers/geometry_helper.py | 7 --- hub/unittests/test_geometry_factory.py | 2 +- 5 files changed, 55 insertions(+), 10 deletions(-) diff --git a/hub/exports/formats/obj.py b/hub/exports/formats/obj.py index 0be1faf7..1bcf3a1c 100644 --- a/hub/exports/formats/obj.py +++ b/hub/exports/formats/obj.py @@ -38,6 +38,8 @@ class Obj: faces = [] for building in self._city.buildings: obj.write(f'# building {building.name}\n') + obj.write(f'g {building.name}\n') + obj.write('s off\n') for surface in building.surfaces: obj.write(f'# surface {surface.name}\n') face = 'f ' diff --git a/hub/helpers/geometry_helper.py b/hub/helpers/geometry_helper.py index f6003ff7..0c08cf59 100644 --- a/hub/helpers/geometry_helper.py +++ b/hub/helpers/geometry_helper.py @@ -6,7 +6,7 @@ Project Coder Guille Gutierrez guillermo.gutierrezmorote@concordia.ca Code contributors: Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca """ import math - +from numba import jit, Float import numpy as np import requests from trimesh import Trimesh @@ -61,6 +61,7 @@ class GeometryHelper: self._area_delta = area_delta @staticmethod + @jit(nopython=True, parallel=True) def coordinate_to_map_point(coordinate, city): return MapPoint(((city.upper_corner[0] - coordinate[0]) * 0.5), ((city.upper_corner[1] - coordinate[1]) * 0.5)) @@ -171,6 +172,48 @@ class GeometryHelper: img.show() return lines_information + @staticmethod + def fast_city_mapping(city, building_names=None): + lines_information = {} + if building_names is None: + building_names = [b.name for b in city.buildings] + x = int((city.upper_corner[0] - city.lower_corner[0]) * 0.5) + 1 + y = int((city.upper_corner[1] - city.lower_corner[1]) * 0.5) + 1 + city_map = [['' for _ in range(y + 1)] for _ in range(x + 1)] + for building_name in building_names: + building = city.city_object(building_name) + line = 0 + for ground in building.grounds: + length = len(ground.perimeter_polygon.coordinates) - 1 + for i, coordinate in enumerate(ground.perimeter_polygon.coordinates): + j = i + 1 + if i == length: + j = 0 + next_coordinate = ground.perimeter_polygon.coordinates[j] + point = GeometryHelper.coordinate_to_map_point(coordinate, city) + distance = int(GeometryHelper.distance_between_points(coordinate, next_coordinate)) + if distance == 0: + continue + delta_x = (coordinate[0] - next_coordinate[0]) / (distance / 0.5) + delta_y = (coordinate[1] - next_coordinate[1]) / (distance / 0.5) + for k in range(0, distance): + x = MapPoint(point.x + (delta_x * k), point.y + (delta_y * k)).x + y = MapPoint(point.x + (delta_x * k), point.y + (delta_y * k)).y + if city_map[x][y] == '': + city_map[x][y] = building.name + elif city_map[x][y] != building.name: + neighbour = city.city_object(city_map[x][y]) + if building.neighbours is None: + building.neighbours = [neighbour] + elif neighbour not in building.neighbours: + building.neighbours.append(neighbour) + if neighbour.neighbours is None: + neighbour.neighbours = [building] + elif building not in neighbour.neighbours: + neighbour.neighbours.append(building) + line += 1 + return lines_information + @staticmethod def segment_list_to_trimesh(lines) -> Trimesh: """ diff --git a/hub/imports/geometry/geojson.py b/hub/imports/geometry/geojson.py index 50c5583d..fcdee3f9 100644 --- a/hub/imports/geometry/geojson.py +++ b/hub/imports/geometry/geojson.py @@ -4,6 +4,7 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later Copyright © 2022 Concordia CERC group Project Coder Guillermo Gutierrez Guillermo.GutierrezMorote@concordia.ca """ +import datetime import json import numpy as np @@ -180,6 +181,7 @@ class Geojson: Get city out of a Geojson file """ if self._city is None: + start = datetime.datetime.now() missing_functions = [] buildings = [] building_id = 0 @@ -228,12 +230,17 @@ class Geojson: [polygon]) self._city = City([self._min_x, self._min_y, 0.0], [self._max_x, self._max_y, self._max_z], 'epsg:26911') + print(f'features: {datetime.datetime.now()-start}') + for building in buildings: self._city.add_city_object(building) self._city.level_of_detail.geometry = lod if lod == 1: + start = datetime.datetime.now() lines_information = GeometryHelper.city_mapping(self._city) - self._store_shared_percentage_to_walls(self._city, lines_information) + print(f'mapping: {datetime.datetime.now() - start}') + + # self._store_shared_percentage_to_walls(self._city, lines_information) if len(missing_functions) > 0: print(f'There are unknown functions {missing_functions}') return self._city diff --git a/hub/imports/geometry/helpers/geometry_helper.py b/hub/imports/geometry/helpers/geometry_helper.py index 54aa0a5e..acd47c24 100644 --- a/hub/imports/geometry/helpers/geometry_helper.py +++ b/hub/imports/geometry/helpers/geometry_helper.py @@ -106,10 +106,3 @@ class GeometryHelper: alpha = math.acos(cosine) return alpha - - @staticmethod - def invert_points(points): - res = [] - for point in points: - res.insert(0,point) - return res diff --git a/hub/unittests/test_geometry_factory.py b/hub/unittests/test_geometry_factory.py index 93a2f832..dd0d8e0f 100644 --- a/hub/unittests/test_geometry_factory.py +++ b/hub/unittests/test_geometry_factory.py @@ -134,7 +134,7 @@ class TestGeometryFactory(TestCase): """ Test geojson import """ - file = '2000_buildings.geojson' + file = '10000_buildings.geojson' start = datetime.datetime.now() city = self._get_city(file, 'geojson', height_field='building_height',