Corrected obj and glb exporters, building now exposes lower and upper corner

This commit is contained in:
Guille Gutierrez 2023-10-19 07:39:35 +02:00
parent f865490ff9
commit 2afce4acce
3 changed files with 21 additions and 17 deletions

View File

@ -810,3 +810,17 @@ class Building(CityObject):
orientation_losses_factor[_key]['south'])] orientation_losses_factor[_key]['south'])]
self._onsite_electrical_production[_key] = _results self._onsite_electrical_production[_key] = _results
return self._onsite_electrical_production return self._onsite_electrical_production
@property
def lower_corner(self):
"""
Get building lower corner.
"""
return [self._min_x, self._min_y, self._min_z]
@property
def upper_corner(self):
"""
Get building upper corner.
"""
return [self._max_x, self._max_y, self._max_z]

View File

@ -1,4 +1,3 @@
import glob
import os import os
import shutil import shutil
import subprocess import subprocess
@ -33,24 +32,18 @@ class Glb:
def _export(self): def _export(self):
try: try:
for building in self._city.buildings: for building in self._city.buildings:
print(self._city.lower_corner, self._city.upper_corner)
city = City(self._city.lower_corner, self._city.upper_corner, self._city.srs_name) city = City(self._city.lower_corner, self._city.upper_corner, self._city.srs_name)
city.name = building.name
city.add_city_object(building) city.add_city_object(building)
city.name = building.name
Obj(city, self._path) Obj(city, self._path)
glb = f'{self._path}/{building.name}.glb' glb = f'{self._path}/{building.name}.glb'
subprocess.run([ subprocess.run([
self._obj2gtl, self._obj2gtl,
'-i', f'{self._path}/{building.name}.obj', '-i', f'{self._path}/{building.name}.obj',
'-b', '-b',
'-o', f'{glb}', '-o', f'{glb}'
'--triangleWindingOrderSanitization',
'--inputUpAxis', 'Y',
'--outputUpAxis', 'Y'
]) ])
os.unlink(f'{self._path}/{building.name}.obj') os.unlink(f'{self._path}/{building.name}.obj')
os.unlink(f'{self._path}/{building.name}.mtl') os.unlink(f'{self._path}/{building.name}.mtl')
except (subprocess.SubprocessError, subprocess.TimeoutExpired, subprocess.CalledProcessError) as err: except (subprocess.SubprocessError, subprocess.TimeoutExpired, subprocess.CalledProcessError) as err:
raise GltExceptionError from err raise GltExceptionError from err

View File

@ -4,7 +4,6 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group Copyright © 2022 Concordia CERC group
Project Coder Guille Gutierrez guillermo.gutierrezmorote@concordia.ca Project Coder Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
""" """
import math
from pathlib import Path from pathlib import Path
import numpy as np import numpy as np
@ -27,7 +26,7 @@ class Obj:
def _to_vertex(self, coordinate): def _to_vertex(self, coordinate):
x, y, z = self._ground(coordinate) x, y, z = self._ground(coordinate)
return f'v {x} {z} {y}\n' return f'v {x} {z} -{y}\n' # to match opengl expectations
def _to_texture_vertex(self, coordinate): def _to_texture_vertex(self, coordinate):
u, v, _ = self._ground(coordinate) u, v, _ = self._ground(coordinate)
@ -55,22 +54,22 @@ class Obj:
with open(mtl_file_path, 'w', encoding='utf-8') as mtl: with open(mtl_file_path, 'w', encoding='utf-8') as mtl:
mtl.write("newmtl cerc_base_material\n") mtl.write("newmtl cerc_base_material\n")
mtl.write("Ka 1.0 1.0 1.0 # Ambient color (white)\n") mtl.write("Ka 1.0 1.0 1.0 # Ambient color (white)\n")
mtl.write("Kd 0.3 0.1 0.3 # Diffuse color (greenish)\n") mtl.write("Kd 0.1 0.3 0.1 # Diffuse color (greenish)\n")
mtl.write("Ks 1.0 1.0 1.0 # Specular color (white)\n") mtl.write("Ks 1.0 1.0 1.0 # Specular color (white)\n")
mtl.write("Ns 400.0 # Specular exponent (defines shininess)\n") mtl.write("Ns 400.0 # Specular exponent (defines shininess)\n")
vertices = {} vertices = {}
normals_index = {}
faces = [] faces = []
vertex_index = 0 vertex_index = 0
normal_index = 0 normal_index = 0
with open(obj_file_path, 'w', encoding='utf-8') as obj: with open(obj_file_path, 'w', encoding='utf-8') as obj:
obj.write("# cerc-hub export\n") obj.write("# cerc-hub export\n")
obj.write(f'mtllib {mtl_name}') obj.write(f'mtllib {mtl_name}\n')
for building in self._city.buildings: for building in self._city.buildings:
obj.write(f'# building {building.name}\n') obj.write(f'# building {building.name}\n')
obj.write(f'g {building.name}\n') obj.write(f'g {building.name}\n')
obj.write('s off\n') obj.write('s off\n')
for surface in building.surfaces: for surface in building.surfaces:
obj.write(f'# surface {surface.name}\n') obj.write(f'# surface {surface.name}\n')
face = [] face = []
@ -79,7 +78,6 @@ class Obj:
textures = [] textures = []
for coordinate in surface.perimeter_polygon.coordinates: for coordinate in surface.perimeter_polygon.coordinates:
vertex = self._to_vertex(coordinate) vertex = self._to_vertex(coordinate)
if vertex not in vertices: if vertex not in vertices:
vertex_index += 1 vertex_index += 1
vertices[vertex] = vertex_index vertices[vertex] = vertex_index
@ -88,8 +86,7 @@ class Obj:
textures.append(self._to_texture_vertex(coordinate)) # only append if non-existing textures.append(self._to_texture_vertex(coordinate)) # only append if non-existing
else: else:
current = vertices[vertex] current = vertices[vertex]
face.append(f'{current}/{current}/{normal_index}') # insert clockwise
face.insert(0, f'{current}/{current}/{normal_index}') # insert counterclockwise
obj.writelines(normal) # add the normal obj.writelines(normal) # add the normal
obj.writelines(textures) # add the texture vertex obj.writelines(textures) # add the texture vertex