2020-06-09 14:07:47 -04:00
|
|
|
"""
|
|
|
|
Geometry helper
|
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
2022-04-08 09:35:33 -04:00
|
|
|
Copyright © 2022 Concordia CERC group
|
|
|
|
Project Coder Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
|
|
|
|
Code contributors: Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca
|
2020-06-09 14:07:47 -04:00
|
|
|
"""
|
2020-05-19 17:00:15 -04:00
|
|
|
import math
|
|
|
|
import numpy as np
|
2020-06-26 14:34:37 -04:00
|
|
|
import requests
|
2020-06-09 11:34:12 -04:00
|
|
|
from trimesh import Trimesh
|
|
|
|
from trimesh import intersections
|
2023-01-24 10:51:50 -05:00
|
|
|
from hub.city_model_structure.attributes.polygon import Polygon
|
|
|
|
from hub.city_model_structure.attributes.polyhedron import Polyhedron
|
|
|
|
from hub.helpers.location import Location
|
2020-05-19 17:00:15 -04:00
|
|
|
|
2023-02-23 06:56:13 -05:00
|
|
|
class MapPoint:
|
|
|
|
def __init__(self, x, y):
|
|
|
|
self.x = int(x)
|
|
|
|
self.y = int(y)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f'({self.x}, {self.y})'
|
2020-05-19 17:00:15 -04:00
|
|
|
|
2020-06-11 16:22:58 -04:00
|
|
|
class GeometryHelper:
|
2020-06-11 15:45:11 -04:00
|
|
|
"""
|
|
|
|
Geometry helper class
|
|
|
|
"""
|
2022-11-24 17:58:45 -05:00
|
|
|
srs_transformations = {
|
|
|
|
'urn:adv:crs:ETRS89_UTM32*DE_DHHN92_NH': 'epsg:25832'
|
|
|
|
}
|
2021-01-07 16:16:48 -05:00
|
|
|
|
2021-01-13 16:41:45 -05:00
|
|
|
def __init__(self, delta=0, area_delta=0):
|
2020-05-19 17:00:15 -04:00
|
|
|
self._delta = delta
|
2020-12-02 11:56:33 -05:00
|
|
|
self._area_delta = area_delta
|
2020-05-19 17:00:15 -04:00
|
|
|
|
2020-06-29 17:24:05 -04:00
|
|
|
@staticmethod
|
2023-02-23 06:56:13 -05:00
|
|
|
def coordinate_to_map_point(coordinate, city):
|
|
|
|
return MapPoint((city.upper_corner[0] - coordinate[0])/2, (city.upper_corner[1] - coordinate[1])/2)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def point_between_point(point_1, point_2, x):
|
|
|
|
m = (point_1.y - point_2.y)/(point_1.x - point_2.x)
|
|
|
|
c = point_2.y - (m*point_2.x)
|
|
|
|
y = (m*x)+c
|
|
|
|
return MapPoint(x,y)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def city_mapping(city, building_names=None):
|
|
|
|
if building_names is None:
|
|
|
|
building_names = [b.name for b in city.buildings]
|
|
|
|
x = int((city.upper_corner[0] - city.lower_corner[0]) / 2)
|
|
|
|
y = int((city.upper_corner[1] - city.lower_corner[1]) / 2)
|
|
|
|
city_map = [['' for _ in range(y+1)] for _ in range(x+1)]
|
|
|
|
city_image = [[0 for _ in range(y+1)] for _ in range(x+1)]
|
|
|
|
for building_name in building_names:
|
|
|
|
building = city.city_object(building_name)
|
|
|
|
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_1 = GeometryHelper.coordinate_to_map_point(coordinate, city)
|
|
|
|
point_2 = GeometryHelper.coordinate_to_map_point(next_coordinate, city)
|
|
|
|
for x in range(point_1.x, point_2.x):
|
|
|
|
y = GeometryHelper.point_between_point(point_1, point_2, x).y
|
|
|
|
if city_map[x][y] == '':
|
|
|
|
city_map[x][y] = building.name
|
|
|
|
city_image[x][y] = 1
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
x = int((city.upper_corner[0] - coordinate[0]) / 2)
|
|
|
|
y = int((city.upper_corner[1] - coordinate[1]) / 2)
|
|
|
|
if city_map[x][y] == '':
|
|
|
|
city_map[x][y] = building.name
|
|
|
|
city_image[x][y] = 1
|
|
|
|
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)
|
|
|
|
"""
|
2020-06-22 14:35:52 -04:00
|
|
|
|
2020-06-09 11:34:12 -04:00
|
|
|
@staticmethod
|
2021-03-31 14:17:53 -04:00
|
|
|
def segment_list_to_trimesh(lines) -> Trimesh:
|
2021-08-26 13:27:43 -04:00
|
|
|
"""
|
|
|
|
Transform a list of segments into a Trimesh
|
|
|
|
"""
|
2023-02-10 05:42:57 -05:00
|
|
|
# todo: trimesh has a method for this
|
2021-03-30 17:57:28 -04:00
|
|
|
line_points = [lines[0][0], lines[0][1]]
|
|
|
|
lines.remove(lines[0])
|
|
|
|
while len(lines) > 1:
|
|
|
|
i = 0
|
|
|
|
for line in lines:
|
|
|
|
i += 1
|
|
|
|
if GeometryHelper.distance_between_points(line[0], line_points[len(line_points) - 1]) < 1e-8:
|
|
|
|
line_points.append(line[1])
|
|
|
|
lines.pop(i - 1)
|
|
|
|
break
|
2021-08-26 13:27:43 -04:00
|
|
|
if GeometryHelper.distance_between_points(line[1], line_points[len(line_points) - 1]) < 1e-8:
|
2021-03-30 17:57:28 -04:00
|
|
|
line_points.append(line[0])
|
|
|
|
lines.pop(i - 1)
|
|
|
|
break
|
2023-02-10 05:42:57 -05:00
|
|
|
polyhedron = Polyhedron(Polygon(line_points).triangles)
|
2021-03-31 14:17:53 -04:00
|
|
|
trimesh = Trimesh(polyhedron.vertices, polyhedron.faces)
|
|
|
|
return trimesh
|
2020-06-09 11:34:12 -04:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _merge_meshes(mesh1, mesh2):
|
|
|
|
v_1 = mesh1.vertices
|
|
|
|
f_1 = mesh1.faces
|
|
|
|
v_2 = mesh2.vertices
|
|
|
|
f_2 = mesh2.faces
|
|
|
|
length = len(v_1)
|
|
|
|
v_merge = np.concatenate((v_1, v_2))
|
|
|
|
f_merge = np.asarray(f_1)
|
|
|
|
|
|
|
|
for item in f_2:
|
|
|
|
point1 = item.item(0) + length
|
|
|
|
point2 = item.item(1) + length
|
|
|
|
point3 = item.item(2) + length
|
|
|
|
surface = np.asarray([point1, point2, point3])
|
|
|
|
f_merge = np.concatenate((f_merge, [surface]))
|
|
|
|
|
|
|
|
mesh_merge = Trimesh(vertices=v_merge, faces=f_merge)
|
2021-03-31 14:17:53 -04:00
|
|
|
mesh_merge.fix_normals()
|
2020-06-09 11:34:12 -04:00
|
|
|
|
|
|
|
return mesh_merge
|
|
|
|
|
|
|
|
@staticmethod
|
2021-03-31 14:17:53 -04:00
|
|
|
def divide_mesh_by_plane(trimesh, normal_plane, point_plane):
|
2020-06-11 15:45:11 -04:00
|
|
|
"""
|
|
|
|
Divide a mesh by a plane
|
2021-03-31 14:17:53 -04:00
|
|
|
:param trimesh: Trimesh
|
2020-06-11 15:45:11 -04:00
|
|
|
:param normal_plane: [x, y, z]
|
|
|
|
:param point_plane: [x, y, z]
|
|
|
|
:return: [Trimesh]
|
|
|
|
"""
|
2020-06-09 11:34:12 -04:00
|
|
|
# The first mesh returns the positive side of the plane and the second the negative side.
|
|
|
|
# If the plane does not divide the mesh (i.e. it does not touch it or it is coplanar with one or more faces),
|
|
|
|
# then it returns only the original mesh.
|
2021-03-31 14:17:53 -04:00
|
|
|
# todo: review split method in https://github.com/mikedh/trimesh/issues/235,
|
|
|
|
# once triangulate_polygon in Polygon class is solved
|
|
|
|
|
2020-06-09 11:34:12 -04:00
|
|
|
normal_plane_opp = [None] * len(normal_plane)
|
|
|
|
for i in range(0, len(normal_plane)):
|
|
|
|
normal_plane_opp[i] = - normal_plane[i]
|
|
|
|
|
2021-03-31 14:17:53 -04:00
|
|
|
section_1 = intersections.slice_mesh_plane(trimesh, normal_plane, point_plane)
|
|
|
|
if section_1 is None:
|
|
|
|
return [trimesh]
|
|
|
|
lines = list(intersections.mesh_plane(trimesh, normal_plane, point_plane))
|
|
|
|
cap = GeometryHelper.segment_list_to_trimesh(lines)
|
|
|
|
trimesh_1 = GeometryHelper._merge_meshes(section_1, cap)
|
|
|
|
|
|
|
|
section_2 = intersections.slice_mesh_plane(trimesh, normal_plane_opp, point_plane)
|
|
|
|
if section_2 is None:
|
|
|
|
return [trimesh_1]
|
|
|
|
trimesh_2 = GeometryHelper._merge_meshes(section_2, cap)
|
|
|
|
|
|
|
|
return [trimesh_1, trimesh_2]
|
2020-06-22 13:26:50 -04:00
|
|
|
|
2020-06-26 14:34:37 -04:00
|
|
|
@staticmethod
|
2021-08-26 13:27:43 -04:00
|
|
|
def get_location(latitude, longitude) -> Location:
|
|
|
|
"""
|
|
|
|
Get Location from latitude and longitude
|
|
|
|
"""
|
2020-06-26 14:34:37 -04:00
|
|
|
url = 'https://nominatim.openstreetmap.org/reverse?lat={latitude}&lon={longitude}&format=json'
|
2021-04-13 14:12:45 -04:00
|
|
|
response = requests.get(url.format(latitude=latitude, longitude=longitude))
|
|
|
|
if response.status_code != 200:
|
2020-06-26 14:34:37 -04:00
|
|
|
# This means something went wrong.
|
2021-04-13 14:12:45 -04:00
|
|
|
raise Exception('GET /tasks/ {}'.format(response.status_code))
|
2021-08-26 13:27:43 -04:00
|
|
|
|
|
|
|
response = response.json()
|
|
|
|
city = 'Unknown'
|
|
|
|
country = 'ca'
|
|
|
|
if 'city' in response['address']:
|
|
|
|
city = response['address']['city']
|
|
|
|
if 'country_code' in response['address']:
|
|
|
|
country = response['address']['country_code']
|
|
|
|
return Location(country, city)
|
2020-11-27 11:31:25 -05:00
|
|
|
|
2021-01-11 10:25:34 -05:00
|
|
|
@staticmethod
|
|
|
|
def distance_between_points(vertex1, vertex2):
|
2021-03-31 14:17:53 -04:00
|
|
|
"""
|
|
|
|
distance between points in an n-D Euclidean space
|
|
|
|
:param vertex1: point or vertex
|
|
|
|
:param vertex2: point or vertex
|
|
|
|
:return: float
|
|
|
|
"""
|
2021-01-11 10:25:34 -05:00
|
|
|
power = 0
|
|
|
|
for dimension in range(0, len(vertex1)):
|
|
|
|
power += math.pow(vertex2[dimension]-vertex1[dimension], 2)
|
|
|
|
distance = math.sqrt(power)
|
|
|
|
return distance
|