From 62d0fc23fd9a800fe559365d938ecaa9973fae01 Mon Sep 17 00:00:00 2001 From: Pilar Date: Fri, 10 Feb 2023 06:09:48 -0500 Subject: [PATCH] modified triangulation method --- .../attributes/polygon.py | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/hub/city_model_structure/attributes/polygon.py b/hub/city_model_structure/attributes/polygon.py index 789a2f2b..2755d399 100644 --- a/hub/city_model_structure/attributes/polygon.py +++ b/hub/city_model_structure/attributes/polygon.py @@ -9,6 +9,7 @@ from __future__ import annotations import math import sys from typing import List +from hub.hub_logger import logger import numpy as np from trimesh import Trimesh import trimesh.intersections @@ -242,24 +243,32 @@ class Polygon: polygon = shapley_polygon(coordinates) - vertices_2d, faces = trimesh.creation.triangulate_polygon(polygon, engine='triangle') - mesh = Trimesh(vertices=vertices, faces=faces) + try: + vertices_2d, faces = trimesh.creation.triangulate_polygon(polygon, engine='triangle') + mesh = Trimesh(vertices=vertices, faces=faces) - # check orientation - normal_sum = 0 - for i in range(0, 3): - normal_sum += normal[i] + mesh.face_normals[0][i] + # check orientation + normal_sum = 0 + for i in range(0, 3): + normal_sum += normal[i] + mesh.face_normals[0][i] - if abs(normal_sum) <= 1E-10: - new_faces = [] - for face in faces: - new_face = [] - for i in range(0, len(face)): - new_face.append(face[len(face)-i-1]) - new_faces.append(new_face) - mesh = Trimesh(vertices=vertices, faces=new_faces) + if abs(normal_sum) <= 1E-10: + new_faces = [] + for face in faces: + new_face = [] + for i in range(0, len(face)): + new_face.append(face[len(face)-i-1]) + new_faces.append(new_face) + mesh = Trimesh(vertices=vertices, faces=new_faces) - return mesh + return mesh + + except ValueError: + logger.error(f'Not able to triangulate polygon\n') + sys.stderr.write(f'Not able to triangulate polygon\n') + _vertices = [[0, 0, 0], [0, 0, 1], [0, 1, 0]] + _faces = [[0, 1, 2]] + return Trimesh(vertices=_vertices, faces=_faces) @property def triangles(self) -> List[Polygon]: