2021-03-16 20:14:40 -04:00
|
|
|
"""
|
|
|
|
export a city into Obj format
|
|
|
|
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
|
2021-03-16 20:14:40 -04:00
|
|
|
"""
|
2023-09-28 09:20:28 -04:00
|
|
|
import math
|
2021-03-16 16:58:52 -04:00
|
|
|
from pathlib import Path
|
|
|
|
|
2023-09-28 09:20:28 -04:00
|
|
|
import numpy as np
|
|
|
|
|
2021-03-16 12:33:22 -04:00
|
|
|
|
2023-03-16 10:51:41 -04:00
|
|
|
class Obj:
|
2021-08-26 13:27:43 -04:00
|
|
|
"""
|
|
|
|
Export to obj format
|
|
|
|
"""
|
2021-03-16 12:33:22 -04:00
|
|
|
def __init__(self, city, path):
|
2023-03-16 10:51:41 -04:00
|
|
|
self._city = city
|
|
|
|
self._path = path
|
|
|
|
self._export()
|
|
|
|
|
2023-09-28 09:20:28 -04:00
|
|
|
def _ground(self, coordinate):
|
2023-03-16 10:51:41 -04:00
|
|
|
x = coordinate[0] - self._city.lower_corner[0]
|
|
|
|
y = coordinate[1] - self._city.lower_corner[1]
|
|
|
|
z = coordinate[2] - self._city.lower_corner[2]
|
2023-09-28 09:20:28 -04:00
|
|
|
return x, y, z
|
|
|
|
|
|
|
|
def _to_vertex(self, coordinate):
|
|
|
|
x, y, z = self._ground(coordinate)
|
|
|
|
return f'v {x} {z} {y}\n'
|
|
|
|
|
|
|
|
def _to_texture_vertex(self, coordinate):
|
|
|
|
u, v, _ = self._ground(coordinate)
|
|
|
|
return f'vt {u} {v}\n'
|
|
|
|
|
|
|
|
def _to_normal_vertex(self, coordinates):
|
|
|
|
ground_vertex = []
|
|
|
|
for coordinate in coordinates:
|
|
|
|
x, y, z = self._ground(coordinate)
|
|
|
|
ground_vertex.append(np.array([x, y, z]))
|
|
|
|
# recalculate the normal to get grounded values
|
|
|
|
edge_1 = ground_vertex[1] - ground_vertex[0]
|
|
|
|
edge_2 = ground_vertex[2] - ground_vertex[0]
|
|
|
|
normal = np.cross(edge_1, edge_2)
|
|
|
|
normal = normal / np.linalg.norm(normal)
|
|
|
|
return f'vn {normal[0]} {normal[1]} {normal[2]}\n'
|
2023-03-16 10:51:41 -04:00
|
|
|
|
|
|
|
def _export(self):
|
|
|
|
if self._city.name is None:
|
|
|
|
self._city.name = 'unknown_city'
|
2023-09-28 09:20:28 -04:00
|
|
|
obj_name = f'{self._city.name}.obj'
|
|
|
|
mtl_name = f'{self._city.name}.mtl'
|
|
|
|
obj_file_path = (Path(self._path).resolve() / obj_name).resolve()
|
|
|
|
mtl_file_path = (Path(self._path).resolve() / mtl_name).resolve()
|
|
|
|
with open(mtl_file_path, 'w', encoding='utf-8') as mtl:
|
|
|
|
mtl.write("newmtl cerc_base_material\n")
|
|
|
|
mtl.write("Ka 1.0 1.0 1.0 # Ambient color (white)\n")
|
|
|
|
mtl.write("Kd 0.3 0.8 0.3 # Diffuse color (greenish)\n")
|
|
|
|
mtl.write("Ks 1.0 1.0 1.0 # Specular color (white)\n")
|
|
|
|
mtl.write("Ns 400.0 # Specular exponent (defines shininess)\n")
|
2023-03-16 10:51:41 -04:00
|
|
|
vertices = {}
|
2023-09-28 09:20:28 -04:00
|
|
|
normals_index = {}
|
|
|
|
faces = []
|
|
|
|
vertex_index = 0
|
|
|
|
normal_index = 0
|
|
|
|
with open(obj_file_path, 'w', encoding='utf-8') as obj:
|
2023-03-16 10:51:41 -04:00
|
|
|
obj.write("# cerc-hub export\n")
|
2023-09-28 09:20:28 -04:00
|
|
|
obj.write(f'mtllib {mtl_name}')
|
|
|
|
|
2023-03-16 10:51:41 -04:00
|
|
|
for building in self._city.buildings:
|
|
|
|
obj.write(f'# building {building.name}\n')
|
2023-03-17 13:36:43 -04:00
|
|
|
obj.write(f'g {building.name}\n')
|
|
|
|
obj.write('s off\n')
|
2023-03-16 10:51:41 -04:00
|
|
|
for surface in building.surfaces:
|
|
|
|
obj.write(f'# surface {surface.name}\n')
|
2023-09-28 09:20:28 -04:00
|
|
|
face = []
|
|
|
|
normal = self._to_normal_vertex(surface.perimeter_polygon.coordinates)
|
|
|
|
normal_index += 1
|
|
|
|
textures = []
|
2023-03-16 10:51:41 -04:00
|
|
|
for coordinate in surface.perimeter_polygon.coordinates:
|
|
|
|
vertex = self._to_vertex(coordinate)
|
2023-09-28 09:20:28 -04:00
|
|
|
|
2023-05-31 13:51:35 -04:00
|
|
|
if vertex not in vertices:
|
2023-03-16 10:51:41 -04:00
|
|
|
vertex_index += 1
|
|
|
|
vertices[vertex] = vertex_index
|
|
|
|
current = vertex_index
|
|
|
|
obj.write(vertex)
|
2023-09-28 09:20:28 -04:00
|
|
|
textures.append(self._to_texture_vertex(coordinate)) # only append if non-existing
|
2023-03-16 10:51:41 -04:00
|
|
|
else:
|
|
|
|
current = vertices[vertex]
|
|
|
|
|
2023-09-28 09:20:28 -04:00
|
|
|
face.insert(0, f'{current}/{current}/{normal_index}') # insert counterclockwise
|
|
|
|
obj.writelines(normal) # add the normal
|
|
|
|
obj.writelines(textures) # add the texture vertex
|
2023-03-16 10:51:41 -04:00
|
|
|
|
2023-09-28 09:20:28 -04:00
|
|
|
faces.append(f"f {' '.join(face)}\n")
|
2023-03-16 10:51:41 -04:00
|
|
|
obj.writelines(faces)
|
|
|
|
faces = []
|