-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): Add a cli in that can be used from different interfaces
- Loading branch information
1 parent
c4863c6
commit fdf75eb
Showing
7 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
click>=7.1.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from ladybug_geometry_polyskel.cli import geometry | ||
|
||
if __name__ == '__main__': | ||
geometry() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
"""ladybug-geometry-polyskel commands.""" | ||
import click | ||
import sys | ||
import logging | ||
import json | ||
|
||
from ladybug_geometry.geometry3d import Face3D | ||
from ladybug_geometry_polyskel.polysplit import perimeter_core_subfaces as \ | ||
lgp_perimeter_core_subfaces | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
# command group for all geometry extension commands. | ||
@click.group(help='ladybug geometry polyskel commands.') | ||
@click.version_option() | ||
def main(): | ||
pass | ||
|
||
|
||
@main.command('perimeter-core-subfaces') | ||
@click.argument('face3d-file', type=click.Path( | ||
exists=True, file_okay=True, dir_okay=False, resolve_path=True)) | ||
@click.argument('perimeter-offset', type=float) | ||
@click.option( | ||
'--tolerance', '-t', help='The maximum difference between x, y, and z ' | ||
'values at which vertices are considered equivalent.', | ||
type=float, default=1e-5) | ||
@click.option( | ||
'--output-file', help='Optional file to output the he string of the visualization ' | ||
'file contents. By default, it will be printed out to stdout', | ||
type=click.File('w'), default='-', show_default=True) | ||
def perimeter_core_subfaces_cli(face3d_file, perimeter_offset, tolerance, output_file): | ||
"""Compute perimeter and core Face3Ds using the straight skeleton of input Face3Ds. | ||
\b | ||
Args: | ||
face3d_file: Full path to a JSON file containing an array of Face3D | ||
objects for which core/perimeter Face3Ds will be computed. | ||
perimeter_offset: Distance to offset perimeter sub-faces. | ||
""" | ||
try: | ||
perimeter_core_subfaces(face3d_file, perimeter_offset, tolerance, output_file) | ||
except Exception as e: | ||
_logger.exception('Failed to compute core/perimeter Face3Ds.\n{}'.format(e)) | ||
sys.exit(1) | ||
else: | ||
sys.exit(0) | ||
|
||
|
||
def perimeter_core_subfaces( | ||
face3d_file, perimeter_offset, tolerance=1e-5, output_file=None | ||
): | ||
"""Compute perimeter and core Face3Ds using the straight skeleton of input Face3Ds. | ||
Args: | ||
face3d_file: Full path to a JSON file containing an array of Face3D | ||
objects for which core/perimeter Face3Ds will be computed. | ||
perimeter_offset: Distance to offset perimeter sub-faces. | ||
tolerance: The maximum difference between x, y, and z values at which | ||
vertices are considered equivalent. | ||
output_file: Optional file to output the string of the visualization | ||
file contents. If None, the string will simply be returned from | ||
this method. | ||
""" | ||
# load the Face3D objects | ||
with open(face3d_file) as inf: | ||
data = json.load(inf) | ||
face_3ds = [Face3D.from_dict(geo) for geo in data] | ||
|
||
# compute core/perimeter Face3Ds | ||
core_per_geos = [] | ||
for geo_obj in face_3ds: | ||
perimeter_sub_faces, core_sub_faces = \ | ||
lgp_perimeter_core_subfaces(geo_obj, perimeter_offset, tolerance) | ||
core_per_geos.extend([f.to_dict() for f in perimeter_sub_faces]) | ||
core_per_geos.extend([f.to_dict() for f in core_sub_faces]) | ||
|
||
# write the resulting geometries into the output | ||
if output_file is None: | ||
return json.dumps(core_per_geos) | ||
else: | ||
output_file.write(json.dumps(core_per_geos)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"type": "Face3D", "plane": {"o": [-3.1066183692618212, 19.759722547974839, 0.0], "n": [0.0, 0.0, -1.0], "type": "Plane", "x": [1.0, 0.0, 0.0]}, "boundary": [[-3.1066183692618212, 19.759722547974839, 0.0], [-7.5979797625430354, 19.759722547974839, 0.0], [-7.5979797625430354, 21.392381536601498, 0.0], [-6.5966265078992699, 21.392381536601498, 0.0], [-6.5966265078992699, 21.824013947431094, 0.0], [-4.1144432017329446, 21.824013947431094, 0.0], [-4.1144432017329446, 21.392381536601498, 0.0], [-3.1066183692618239, 21.392381536601498, 0.0]]}, {"type": "Face3D", "plane": {"o": [-3.6999621911860219, 15.90298770546752, 0.0], "n": [0.0, 0.0, -1.0], "type": "Plane", "x": [1.0, 0.0, 0.0]}, "boundary": [[-3.6999621911860219, 15.90298770546752, 0.0], [-8.1913235844672361, 15.90298770546752, 0.0], [-8.1913235844672361, 17.535646694094179, 0.0], [-7.1899703298234705, 17.535646694094179, 0.0], [-7.1899703298234705, 17.967279104923776, 0.0], [-4.7077870236571453, 17.967279104923776, 0.0], [-4.7077870236571453, 17.082447849590533, 0.0], [-3.6999621911860245, 17.082447849590533, 0.0]]}, {"type": "Face3D", "plane": {"o": [20.911599710612037, -12.469149222792852, 0.0], "n": [0.0, 0.0, 1.0], "type": "Plane", "x": [1.0, 0.0, 0.0]}, "boundary": [[20.911599710612037, -12.469149222792852, 0.0], [27.292069203710405, -12.469149222792852, 0.0], [27.292069203710405, -12.05468237864326, 0.0], [29.593621187576694, -12.05468237864326, 0.0], [29.593621187576694, -9.5928062416062296, 0.0], [29.241924596571408, -9.5928062416062296, 0.0], [29.241924596571408, -6.8797182538511352, 0.0], [26.126897647667406, -6.8797182538511352, 0.0], [26.126897647667406, -4.3843472033850315, 0.0], [23.832496077775755, -4.3843472033850315, 0.0], [23.832496077775755, -8.2865046178722963, 0.0], [21.990275839176618, -8.2865046178722963, 0.0], [21.990275839176618, -11.887207811497882, 0.0], [20.911599710612037, -11.887207811497882, 0.0]]}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"""Test cli.""" | ||
from click.testing import CliRunner | ||
import json | ||
|
||
from ladybug_geometry.geometry3d import Face3D | ||
from ladybug_geometry_polyskel.cli import perimeter_core_subfaces_cli | ||
|
||
|
||
def test_perimeter_core_subfaces(): | ||
input_file = './tests/assets/sample_geometries.json' | ||
runner = CliRunner() | ||
result = runner.invoke( | ||
perimeter_core_subfaces_cli, [input_file, '1.0', '--tolerance', '0.01']) | ||
assert result.exit_code == 0 | ||
|
||
result_dict = json.loads(result.output) | ||
new_faces = [Face3D.from_dict(f) for f in result_dict] | ||
assert len(new_faces) == 33 |