2020-06-09 14:07:47 -04:00
|
|
|
"""
|
|
|
|
Geometry helper
|
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
|
|
Copyright © 2020 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
|
|
|
|
"""
|
2020-05-19 17:00:15 -04:00
|
|
|
import math
|
2020-06-22 13:26:50 -04:00
|
|
|
from datetime import datetime
|
2020-06-16 10:34:17 -04:00
|
|
|
|
2020-05-19 17:00:15 -04:00
|
|
|
import numpy as np
|
2020-06-16 10:34:17 -04:00
|
|
|
import open3d as o3d
|
2020-06-09 11:34:12 -04:00
|
|
|
from trimesh import Trimesh
|
|
|
|
from trimesh import intersections
|
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
|
|
|
|
"""
|
2020-05-19 17:00:15 -04:00
|
|
|
def __init__(self, delta=0.5):
|
|
|
|
self._delta = delta
|
|
|
|
|
|
|
|
def almost_equal(self, v1, v2):
|
2020-06-11 15:45:11 -04:00
|
|
|
"""
|
|
|
|
Compare two points and decides if they are almost equal (quadratic error under delta)
|
|
|
|
:param v1: [x,y,z]
|
|
|
|
:param v2: [x,y,z]
|
|
|
|
:return: Boolean
|
|
|
|
"""
|
2020-05-28 15:22:46 -04:00
|
|
|
delta = math.sqrt(pow((v1[0] - v2[0]), 2) + pow((v1[1] - v2[1]), 2) + pow((v1[2] - v2[2]), 2))
|
2020-05-19 17:00:15 -04:00
|
|
|
return delta <= self._delta
|
|
|
|
|
2020-05-28 15:22:46 -04:00
|
|
|
def is_almost_same_surface(self, s1, s2):
|
2020-06-11 15:45:11 -04:00
|
|
|
"""
|
|
|
|
Compare two surfaces and decides if they are almost equal (quadratic error under delta)
|
|
|
|
:param s1: Surface
|
|
|
|
:param s2: Surface
|
|
|
|
:return: Boolean
|
|
|
|
"""
|
2020-06-22 13:26:50 -04:00
|
|
|
start = datetime.utcnow()
|
|
|
|
|
2020-05-28 15:22:46 -04:00
|
|
|
# delta is grads an need to be converted into radians
|
|
|
|
delta = np.rad2deg(self._delta)
|
|
|
|
difference = (s1.inclination - s2.inclination) % math.pi
|
|
|
|
if abs(difference) > delta:
|
|
|
|
return False
|
|
|
|
# s1 and s2 are at least almost parallel surfaces
|
2020-05-29 07:25:33 -04:00
|
|
|
# calculate distance point to plane using all the vertex
|
2020-05-28 15:22:46 -04:00
|
|
|
# select surface1 value for the point (X,Y,Z) where two of the values are 0
|
2020-06-03 16:56:17 -04:00
|
|
|
minimum_distance = self._delta + 1
|
2020-05-29 07:25:33 -04:00
|
|
|
parametric = s2.polygon.get_parametric()
|
2020-05-28 15:22:46 -04:00
|
|
|
n2 = s2.normal
|
2020-05-29 07:25:33 -04:00
|
|
|
for point in s1.points:
|
|
|
|
distance = abs(
|
|
|
|
(point[0] * parametric[0]) + (point[1] * parametric[1]) + (point[2] * parametric[2]) + parametric[3])
|
|
|
|
normal_module = math.sqrt(pow(n2[0], 2) + pow(n2[1], 2) + pow(n2[2], 2))
|
2020-06-03 16:56:17 -04:00
|
|
|
|
2020-05-29 07:25:33 -04:00
|
|
|
if normal_module == 0:
|
|
|
|
continue
|
|
|
|
distance = distance / normal_module
|
|
|
|
if distance < minimum_distance:
|
|
|
|
minimum_distance = distance
|
2020-06-03 16:56:17 -04:00
|
|
|
if minimum_distance <= self._delta:
|
|
|
|
break
|
2020-06-22 13:26:50 -04:00
|
|
|
print('intersect', datetime.utcnow() - start)
|
2020-06-03 16:56:17 -04:00
|
|
|
if minimum_distance > self._delta or s1.intersect(s2) is None:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
2020-05-28 15:22:46 -04:00
|
|
|
|
2020-05-19 17:00:15 -04:00
|
|
|
@staticmethod
|
|
|
|
def to_points_matrix(points, remove_last=False):
|
2020-06-11 15:45:11 -04:00
|
|
|
"""
|
|
|
|
Transform a point vector into a point matrix
|
|
|
|
:param points: [x, y, z, x, y, z ...]
|
|
|
|
:param remove_last: Boolean
|
|
|
|
:return: [[x,y,z],[x,y,z]...]
|
|
|
|
"""
|
2020-05-28 15:22:46 -04:00
|
|
|
rows = points.size // 3
|
2020-05-19 17:00:15 -04:00
|
|
|
points = points.reshape(rows, 3)
|
|
|
|
if remove_last:
|
2020-05-28 15:22:46 -04:00
|
|
|
points = np.delete(points, rows - 1, 0)
|
2020-05-19 17:00:15 -04:00
|
|
|
return points
|
2020-06-09 11:34:12 -04:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _segment_list_to_point_cloud(segment_list):
|
|
|
|
point_list = np.asarray(segment_list[0])
|
|
|
|
for segment in segment_list:
|
|
|
|
for new_point in segment:
|
|
|
|
found = False
|
|
|
|
for point in point_list:
|
|
|
|
same_point = np.allclose(new_point, point)
|
|
|
|
if same_point:
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
if not found:
|
|
|
|
point_list = np.concatenate((point_list, [new_point]))
|
|
|
|
return point_list
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _point_cloud_to_mesh(point_list, normal_list):
|
|
|
|
# Return a mesh composed only by triangles
|
|
|
|
pcd = o3d.geometry.PointCloud()
|
|
|
|
pcd.points = o3d.utility.Vector3dVector(point_list)
|
|
|
|
pcd.normals = o3d.utility.Vector3dVector(normal_list)
|
|
|
|
distances = pcd.compute_nearest_neighbor_distance()
|
|
|
|
avg_dist = np.mean(distances)
|
|
|
|
radius = 3 * avg_dist
|
|
|
|
bpa_mesh = o3d.geometry.TriangleMesh().create_from_point_cloud_ball_pivoting(
|
|
|
|
pcd, o3d.utility.DoubleVector([radius, radius * 2]))
|
|
|
|
mesh_result = Trimesh(vertices=np.asarray(bpa_mesh.vertices), faces=np.asarray(bpa_mesh.triangles))
|
|
|
|
return mesh_result
|
|
|
|
|
|
|
|
@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)
|
|
|
|
|
|
|
|
return mesh_merge
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def divide_mesh_by_plane(mesh, normal_plane, point_plane):
|
2020-06-11 15:45:11 -04:00
|
|
|
"""
|
|
|
|
Divide a mesh by a plane
|
|
|
|
:param mesh: Trimesh
|
|
|
|
: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.
|
|
|
|
normal_plane_opp = [None] * len(normal_plane)
|
|
|
|
for i in range(0, len(normal_plane)):
|
|
|
|
normal_plane_opp[i] = - normal_plane[i]
|
|
|
|
|
|
|
|
normal = [normal_plane, normal_plane_opp]
|
|
|
|
normal_opp = [normal_plane_opp, normal_plane]
|
|
|
|
mesh_final = []
|
|
|
|
for i in range(0, 2):
|
|
|
|
mesh_1 = intersections.slice_mesh_plane(mesh, normal[i], point_plane)
|
|
|
|
mesh_1_segments = intersections.mesh_plane(mesh, normal[i], point_plane)
|
2020-06-16 10:34:17 -04:00
|
|
|
mesh.difference(mesh_1, engine='blender')
|
2020-06-09 11:34:12 -04:00
|
|
|
quit()
|
|
|
|
if len(mesh_1_segments) <= 0 or len(mesh_1.faces) == len(mesh.faces):
|
|
|
|
mesh_final.append(mesh)
|
|
|
|
break
|
|
|
|
else:
|
2020-06-11 16:22:58 -04:00
|
|
|
points = GeometryHelper._segment_list_to_point_cloud(mesh_1_segments)
|
2020-06-09 11:34:12 -04:00
|
|
|
points_normals = [[None] * 3] * len(points)
|
|
|
|
for j in range(0, len(points_normals)):
|
|
|
|
points_normals[j] = normal_opp[i]
|
2020-06-11 16:22:58 -04:00
|
|
|
mesh_2 = GeometryHelper._point_cloud_to_mesh(points, points_normals)
|
|
|
|
mesh_final.append(GeometryHelper._merge_meshes(mesh_1, mesh_2))
|
2020-06-09 11:34:12 -04:00
|
|
|
|
|
|
|
return mesh_final
|
2020-06-22 13:26:50 -04:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def gml_surface_to_libs(surface):
|
|
|
|
if surface == 'WallSurface':
|
|
|
|
return 'Wall'
|
|
|
|
elif surface == 'GroundSurface':
|
|
|
|
return 'Ground'
|
|
|
|
else:
|
|
|
|
return 'Roof'
|