Implement exports factories clases and export stl and obj city version.

This commit is contained in:
Guille Gutierrez 2021-03-16 12:33:22 -04:00
parent 4e4875392f
commit 490fcb9563
30 changed files with 159 additions and 28 deletions

View File

@ -6,7 +6,7 @@
<excludeFolder url="file://$MODULE_DIR$/venv2" /> <excludeFolder url="file://$MODULE_DIR$/venv2" />
<excludeFolder url="file://$MODULE_DIR$/venv38" /> <excludeFolder url="file://$MODULE_DIR$/venv38" />
</content> </content>
<orderEntry type="jdk" jdkName="Python 3.7 (venv)" jdkType="Python SDK" /> <orderEntry type="jdk" jdkName="Python 3.8 (libs) (2)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
<component name="PackageRequirementsSettings"> <component name="PackageRequirementsSettings">

View File

@ -25,7 +25,7 @@ class Polyhedron:
self._volume = None self._volume = None
self._faces = None self._faces = None
self._vertices = None self._vertices = None
self._mesh = None self._trimesh = None
self._centroid = None self._centroid = None
self._max_z = None self._max_z = None
self._max_y = None self._max_y = None
@ -348,10 +348,10 @@ class Polyhedron:
return self._faces return self._faces
@property @property
def polyhedron_trimesh(self): def trimesh(self) -> Trimesh:
if self._mesh is None: if self._trimesh is None:
self._mesh = Trimesh(vertices=self.vertices, faces=self.faces) self._trimesh = Trimesh(vertices=self.vertices, faces=self.faces)
return self._mesh return self._trimesh
@property @property
def volume(self): def volume(self):
@ -360,10 +360,10 @@ class Polyhedron:
:return: float :return: float
""" """
if self._volume is None: if self._volume is None:
if not self.polyhedron_trimesh.is_volume: if not self.trimesh.is_volume:
self._volume = np.inf self._volume = np.inf
else: else:
self._volume = self.polyhedron_trimesh.volume self._volume = self.trimesh.volume
return self._volume return self._volume
@property @property
@ -466,7 +466,7 @@ class Polyhedron:
:param full_path: str :param full_path: str
:return: None :return: None
""" """
self.polyhedron_trimesh.export(full_path, 'stl_ascii') self.trimesh.export(full_path, 'stl_ascii')
def obj_export(self, full_path): def obj_export(self, full_path):
""" """
@ -474,7 +474,7 @@ class Polyhedron:
:param full_path: str :param full_path: str
:return: None :return: None
""" """
self.polyhedron_trimesh.export(full_path, 'obj') self.trimesh.export(full_path, 'obj')
def show(self): def show(self):
self.polyhedron_trimesh.show() self.trimesh.show()

View File

@ -311,18 +311,18 @@ class Building(CityObject):
# height = self.average_storey_height # height = self.average_storey_height
number_of_storeys = 4 number_of_storeys = 4
height = 1.5 height = 1.5
mesh = self.simplified_polyhedron.polyhedron_trimesh trimesh = self.simplified_polyhedron.trimesh
normal_plane = [0, 0, -1] normal_plane = [0, 0, -1]
rest_mesh = mesh rest_trimesh = trimesh
for n in range(0, number_of_storeys - 1): for n in range(0, number_of_storeys - 1):
# todo: I need the lower corner of the building!! # todo: I need the lower corner of the building!!
# point_plane = [self._lower_corner[0], self._lower_corner[1], self._lower_corner[2] + height] # point_plane = [self._lower_corner[0], self._lower_corner[1], self._lower_corner[2] + height]
point_plane = [self._lower_corner[0] + 0.5, self._lower_corner[1] + 0.5, self._lower_corner[2] + height * (n + 1)] point_plane = [self._lower_corner[0] + 0.5, self._lower_corner[1] + 0.5, self._lower_corner[2] + height * (n + 1)]
meshes = gh.divide_mesh_by_plane(rest_mesh, normal_plane, point_plane) trimeshes = gh.divide_mesh_by_plane(rest_trimesh, normal_plane, point_plane)
storey = meshes[0] storey = trimeshes[0]
rest_mesh = meshes[1] rest_trimesh = trimeshes[1]
storeys.append(storey) storeys.append(storey)
storeys.append(rest_mesh) storeys.append(rest_trimesh)
return storeys return storeys
@property @property

View File

@ -0,0 +1,58 @@
"""
ExportsFactory export a city into several formats
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2020 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
"""
from exports.formats.stl import Stl
from exports.formats.obj import Obj
class ExportsFactory:
"""
Exports factory class
"""
def __init__(self, export_type, city, path):
self._city = city
self._export_type = '_' + export_type.lower()
self._path = path
@property
def _citygml(self):
"""
Export to citygml with application domain extensions
:return: None
"""
raise NotImplementedError()
@property
def _stl(self):
"""
Export the city geometry to stl
:return: None
"""
return Stl(self._city, self._path)
@property
def _obj(self):
"""
Export the city geometry to obj
:return: None
"""
return Obj(self._city, self._path)
@property
def _idf(self):
"""
Export the city to Energy+ idf format
:return:
"""
raise NotImplementedError()
@property
def export(self):
"""
Export the city model structure to the given export type
:return: City
"""
return getattr(self, self._export_type, lambda: None)

View File

@ -10,7 +10,7 @@ from pathlib import Path
import helpers.constants as cte import helpers.constants as cte
class IdfHelper: class Idf:
_THERMOSTAT = 'HVACTEMPLATE:THERMOSTAT' _THERMOSTAT = 'HVACTEMPLATE:THERMOSTAT'
_IDEAL_LOAD_AIR_SYSTEM = 'HVACTEMPLATE:ZONE:IDEALLOADSAIRSYSTEM' _IDEAL_LOAD_AIR_SYSTEM = 'HVACTEMPLATE:ZONE:IDEALLOADSAIRSYSTEM'
_SURFACE = 'BUILDINGSURFACE:DETAILED' _SURFACE = 'BUILDINGSURFACE:DETAILED'

5
exports/formats/obj.py Normal file
View File

@ -0,0 +1,5 @@
from exports.formats.triangular import Triangular
class Obj(Triangular):
def __init__(self, city, path):
super().__init__(city, path, 'obj')

5
exports/formats/stl.py Normal file
View File

@ -0,0 +1,5 @@
from exports.formats.triangular import Triangular
class Stl(Triangular):
def __init__(self, city, path):
super().__init__(city, path, 'stl')

View File

@ -0,0 +1,21 @@
from pathlib import Path
from trimesh import Trimesh
class Triangular:
def __init__(self, city, path, triangular_format):
self._city = city
self._path = path
self._triangular_format = triangular_format
self._files()
def _files(self):
if self._city.name is None:
self._city.name = 'unknown_city'
file_name = self._city.name + '.' + self._triangular_format
file_path = (Path(self._path).resolve() / file_name).resolve()
print(file_path)
trimesh = Trimesh()
for building in self._city.buildings:
trimesh = trimesh.union(building.simplified_polyhedron.trimesh)
with open(file_path, 'w') as file:
file.write(trimesh.export(file_type=self._triangular_format))

39
tests/test_exports.py Normal file
View File

@ -0,0 +1,39 @@
"""
TestExports test and validate the city export formats
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2020 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
"""
from pathlib import Path
from unittest import TestCase
from imports.geometry_factory import GeometryFactory
from exports.exports_factory import ExportsFactory
class TestExports(TestCase):
"""
TestGeometryFactory TestCase 1
"""
def setUp(self) -> None:
"""
Test setup
:return: None
"""
self._city_gml = None
self._example_path = (Path(__file__).parent / 'tests_data').resolve()
self._output_path = (Path(__file__).parent / 'tests_outputs').resolve()
def _get_city(self):
if self._city_gml is None:
file_path = (self._example_path / '20buildings.gml').resolve()
self._city_gml = GeometryFactory('citygml', file_path).city
return self._city_gml
def _export(self, export_type):
self._city_gml = self._get_city()
ExportsFactory(export_type, self._city_gml, self._output_path).export
def test_obj_export(self):
self._export('obj')
def test_stl_export(self):
self._export('stl')

View File

@ -21,7 +21,7 @@ class TestGeometryFactory(TestCase):
:return: None :return: None
""" """
self._city_gml = None self._city_gml = None
self._example_path = (Path(__file__).parent.parent / 'tests_data').resolve() self._example_path = (Path(__file__).parent / 'tests_data').resolve()
def _get_citygml(self, file): def _get_citygml(self, file):
if self._city_gml is None: if self._city_gml is None:

View File

@ -9,7 +9,6 @@ from imports.geometry_factory import GeometryFactory
from imports.physics_factory import PhysicsFactory from imports.physics_factory import PhysicsFactory
from imports.usage_factory import UsageFactory from imports.usage_factory import UsageFactory
from imports.schedules_factory import SchedulesFactory from imports.schedules_factory import SchedulesFactory
from exports.idf_helper import IdfHelper
import os import os
import glob import glob
@ -25,8 +24,8 @@ class TestIdf(TestCase):
:return: None :return: None
""" """
self._city_gml = None self._city_gml = None
self._example_path = (Path(__file__).parent.parent / 'tests_data').resolve() self._example_path = (Path(__file__).parent / 'tests_data').resolve()
self._output_path = (Path(__file__).parent / 'ep_outputs').resolve() self._output_path = (Path(__file__).parent / 'tests_outputs').resolve()
def _get_city(self): def _get_city(self):
if self._city_gml is None: if self._city_gml is None:

View File

@ -21,7 +21,7 @@ class TestPhysicsFactory(TestCase):
""" """
self._city_gml = None self._city_gml = None
self._nyc_with_physics = None self._nyc_with_physics = None
self._example_path = (Path(__file__).parent.parent / 'tests_data').resolve() self._example_path = (Path(__file__).parent / 'tests_data').resolve()
def _get_citygml(self, file_path): def _get_citygml(self, file_path):
if self._city_gml is None: if self._city_gml is None:

View File

@ -23,7 +23,7 @@ class TestSchedulesFactory(TestCase):
:return: None :return: None
""" """
self._city_gml_with_usage = None self._city_gml_with_usage = None
self._example_path = (Path(__file__).parent.parent / 'tests_data').resolve() self._example_path = (Path(__file__).parent / 'tests_data').resolve()
@property @property
def _handler(self): def _handler(self):

View File

@ -21,7 +21,7 @@ class TestUsageFactory(TestCase):
""" """
self._city_gml = None self._city_gml = None
self._nyc_with_usage = None self._nyc_with_usage = None
self._example_path = (Path(__file__).parent.parent / 'tests_data').resolve() self._example_path = (Path(__file__).parent / 'tests_data').resolve()
def _get_citygml(self, file_path): def _get_citygml(self, file_path):
if self._city_gml is None: if self._city_gml is None:

View File

@ -22,7 +22,7 @@ class TestWeatherFactory(TestCase):
self._city_gml = None self._city_gml = None
self._city_with_weather = None self._city_with_weather = None
self._city_name = 'new_york_city' self._city_name = 'new_york_city'
self._example_path = (Path(__file__).parent.parent / 'tests_data').resolve() self._example_path = (Path(__file__).parent / 'tests_data').resolve()
def _get_citygml(self, file_path): def _get_citygml(self, file_path):
if self._city_gml is None: if self._city_gml is None:
@ -32,7 +32,7 @@ class TestWeatherFactory(TestCase):
def _get_city_with_weather(self): def _get_city_with_weather(self):
if self._city_with_weather is None: if self._city_with_weather is None:
file_path = (Path(__file__).parent.parent / 'tests_data' / '20buildings.gml').resolve() file_path = (Path(__file__).parent / 'tests_data' / '20buildings.gml').resolve()
self._city_with_weather = self._get_citygml(file_path) self._city_with_weather = self._get_citygml(file_path)
WeatherFactory('dat', self._city_with_weather, city_name=self._city_name, base_path=self._example_path) WeatherFactory('dat', self._city_with_weather, city_name=self._city_name, base_path=self._example_path)
return self._city_with_weather return self._city_with_weather
@ -54,7 +54,7 @@ class TestWeatherFactory(TestCase):
self.assertFalse(values.empty, 'wrong value beam') self.assertFalse(values.empty, 'wrong value beam')
def test_weather_xls(self): def test_weather_xls(self):
file_path = (Path(__file__).parent.parent / 'tests_data' / 'iso_52016_1_2017_lod2.gml').resolve() file_path = (Path(__file__).parent / 'tests_data' / 'iso_52016_1_2017_lod2.gml').resolve()
city_with_weather = self._get_citygml(file_path) city_with_weather = self._get_citygml(file_path)
WeatherFactory('xls', city_with_weather, city_name=self._city_name, base_path=self._example_path) WeatherFactory('xls', city_with_weather, city_name=self._city_name, base_path=self._example_path)
for building in city_with_weather.buildings: for building in city_with_weather.buildings:

4
tests/tests_outputs/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore