system_assignation/hub/exports/formats/obj.py

66 lines
1.9 KiB
Python
Raw Normal View History

2021-03-16 20:14:40 -04:00
"""
export a city into Obj format
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project Coder Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
2021-03-16 20:14:40 -04:00
"""
2021-03-16 16:58:52 -04:00
from pathlib import Path
import trimesh.exchange.obj
2023-01-24 10:51:50 -05:00
from hub.exports.formats.triangular import Triangular
from hub.imports.geometry_factory import GeometryFactory
2021-03-16 16:58:52 -04:00
2023-03-16 10:51:41 -04:00
class Obj:
2021-08-26 13:27:43 -04:00
"""
Export to obj format
"""
def __init__(self, city, path):
2023-03-16 10:51:41 -04:00
self._city = city
self._path = path
self._export()
def _to_vertex(self, coordinate):
x = coordinate[0] - self._city.lower_corner[0]
y = coordinate[1] - self._city.lower_corner[1]
z = coordinate[2] - self._city.lower_corner[2]
return f'v {x} {y} {z}\n'
def _export(self):
if self._city.name is None:
self._city.name = 'unknown_city'
file_name = self._city.name + '.obj'
file_path = (Path(self._path).resolve() / file_name).resolve()
vertices = {}
with open(file_path, 'w') as obj:
obj.write("# cerc-hub export\n")
vertex_index = 0
faces = []
for building in self._city.buildings:
obj.write(f'# building {building.name}\n')
for surface in building.surfaces:
obj.write(f'# surface {surface.name}\n')
face = 'f '
len_ = len(surface.perimeter_polygon.coordinates)
if len_ > 40:
print(len_, building.name)
for coordinate in surface.perimeter_polygon.coordinates:
vertex = self._to_vertex(coordinate)
if vertex not in vertices.keys():
vertex_index += 1
vertices[vertex] = vertex_index
current = vertex_index
obj.write(vertex)
else:
current = vertices[vertex]
face = f'{face} {current}'
faces.append(f'{face} {face.split(" ")[1]}\n')
obj.writelines(faces)
faces = []