2021-03-16 16:58:52 -04:00
|
|
|
"""
|
|
|
|
Obj module parses obj files and import the geometry into the city model structure
|
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
2021-04-07 11:47:39 -04:00
|
|
|
Copyright © 2020 Project Author Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca
|
2021-03-16 16:58:52 -04:00
|
|
|
"""
|
2021-04-07 14:20:13 -04:00
|
|
|
import trimesh.exchange.load
|
|
|
|
from trimesh import Scene
|
|
|
|
import trimesh.geometry
|
2021-04-07 11:47:39 -04:00
|
|
|
from city_model_structure.city import City
|
|
|
|
from city_model_structure.building import Building
|
|
|
|
from city_model_structure.attributes.surface import Surface
|
|
|
|
from city_model_structure.attributes.polygon import Polygon
|
2021-03-16 16:58:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Obj:
|
|
|
|
"""
|
|
|
|
Obj class
|
|
|
|
"""
|
|
|
|
def __init__(self, path):
|
|
|
|
self._city = None
|
|
|
|
with open(path, 'r') as file:
|
2021-04-07 14:20:13 -04:00
|
|
|
self._scene = trimesh.exchange.load.load(file, file_type='obj', force='scene')
|
|
|
|
self._corners = self._scene.bounds_corners
|
|
|
|
_bound_corner_min = None
|
|
|
|
_bound_corner_max = None
|
|
|
|
for corner in self._corners:
|
|
|
|
if _bound_corner_min is None:
|
|
|
|
_bound_corner_min = corner
|
|
|
|
elif _bound_corner_max is None:
|
|
|
|
_bound_corner_max = corner
|
|
|
|
else:
|
|
|
|
_bound_corner_min[0] = min(_bound_corner_min[0], corner[0])
|
|
|
|
_bound_corner_min[1] = min(_bound_corner_min[1], corner[1])
|
|
|
|
_bound_corner_min[2] = min(_bound_corner_min[2], corner[2])
|
|
|
|
_bound_corner_max[0] = max(_bound_corner_max[0], corner[0])
|
|
|
|
_bound_corner_max[1] = max(_bound_corner_max[1], corner[1])
|
|
|
|
_bound_corner_max[2] = max(_bound_corner_max[2], corner[2])
|
|
|
|
self._lower_corner = _bound_corner_min
|
|
|
|
self._upper_corner = _bound_corner_max
|
2021-03-16 16:58:52 -04:00
|
|
|
|
|
|
|
@property
|
2021-04-07 14:20:13 -04:00
|
|
|
def scene(self) -> Scene:
|
2021-03-16 16:58:52 -04:00
|
|
|
return self._scene
|
2021-04-07 11:47:39 -04:00
|
|
|
|
|
|
|
@property
|
2021-04-07 14:20:13 -04:00
|
|
|
def city(self) -> City:
|
2021-04-07 11:47:39 -04:00
|
|
|
if self._city is None:
|
2021-04-07 14:20:13 -04:00
|
|
|
# todo: refactor this method to clearly choose the obj type
|
|
|
|
# todo: where do we get this information from?
|
|
|
|
srs_name = 'EPSG:26911'
|
2021-04-07 11:47:39 -04:00
|
|
|
|
2021-04-07 14:20:13 -04:00
|
|
|
self._city = City(self._lower_corner, self._upper_corner, srs_name)
|
|
|
|
scene = self.scene.geometry
|
|
|
|
keys = scene.keys()
|
|
|
|
for key in keys:
|
|
|
|
name = key
|
|
|
|
# todo: where do we get this information from?
|
|
|
|
lod = 1
|
|
|
|
year_of_construction = 0
|
|
|
|
function = ''
|
|
|
|
|
|
|
|
obj = scene[key]
|
|
|
|
surfaces = []
|
|
|
|
for face in obj.faces:
|
|
|
|
# todo: review for obj with windows
|
|
|
|
points = []
|
|
|
|
for vertex_index in face:
|
|
|
|
points.append(obj.vertices[vertex_index])
|
|
|
|
solid_polygon = Polygon(points)
|
|
|
|
perimeter_polygon = solid_polygon
|
|
|
|
surface = Surface(solid_polygon, perimeter_polygon)
|
|
|
|
surfaces.append(surface)
|
|
|
|
self._city.add_city_object(Building(name, lod, surfaces, year_of_construction, function,
|
|
|
|
self._lower_corner))
|
2021-04-07 11:47:39 -04:00
|
|
|
return self._city
|