Skip to content

Commit

Permalink
Merge 0a113bc into 071a68e
Browse files Browse the repository at this point in the history
  • Loading branch information
devang-chauhan committed Jun 9, 2021
2 parents 071a68e + 0a113bc commit 2c43bb7
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 67 deletions.
47 changes: 34 additions & 13 deletions honeybee_vtk/camera.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""A VTK camera object."""
from __future__ import annotations
import vtk
from pathlib import Path
from typing import List, Tuple
from ladybug_geometry.geometry3d.pointvector import Point3D
from honeybee_radiance.view import View
Expand Down Expand Up @@ -46,13 +47,13 @@ def __init__(
up_vector=up_vector, h_size=h_size, v_size=v_size, type=type)

self._flat_view_directions = {
(0, 0, -1): [2, '+'],
(0, 0, 1): [2, '-'],
(0, 1, 0): [1, '+'],
(0, -1, 0): [1, '-'],
(1, 0, 0): [0, '+'],
(-1, 0, 0): [0, '-'],
}
(0, 0, -1): [2, '+'],
(0, 0, 1): [2, '-'],
(0, 1, 0): [1, '+'],
(0, -1, 0): [1, '-'],
(1, 0, 0): [0, '+'],
(-1, 0, 0): [0, '-'],
}

@property
def flat_view_direction(self) -> dict:
Expand Down Expand Up @@ -201,9 +202,29 @@ def from_view(cls: Camera, view: View) -> Camera:
A Camera object.
"""
return cls(
position=view.position,
direction=view.direction,
up_vector=view.up_vector,
h_size=view.h_size,
v_size=view.v_size,
type=view.type)
position=view.position,
direction=view.direction,
up_vector=view.up_vector,
h_size=view.h_size,
v_size=view.v_size,
type=view.type)

@classmethod
def from_view_file(cls: Camera, file_path: str) -> Camera:
"""Create a Camera object from a radiance view file.
Args:
file_path: A valid path to a radiance view file with .vf extension.
Returns:
A Camera object.
"""

view_file = Path(file_path)

if view_file.is_file() and view_file.as_posix()[-3:] == '.vf':
return Camera.from_view(view=View.from_file(view_file.as_posix()))
else:
raise FileNotFoundError(
'Radiance view file not found.'
)
35 changes: 25 additions & 10 deletions honeybee_vtk/cli/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def export():
"""Honeybee VTK commands entry point."""
pass


@export.command('export-images')
@click.argument('hbjson-file')
@click.option(
Expand All @@ -26,11 +27,11 @@ def export():
@click.option(
'--folder', '-f', help='Path to target folder.',
type=click.Path(exists=False, file_okay=False, resolve_path=True,
dir_okay=True), default='.', show_default=True
dir_okay=True), default='.', show_default=True
)
@click.option(
'--image-type', '-it', type=click.Choice(['png', 'jpg', 'ps', 'tiff', 'bmp', 'pnm'],
case_sensitive=False), default='jpg',
case_sensitive=False), default='jpg',
help='choose the type of image file.', show_default=True
)
@click.option(
Expand All @@ -47,22 +48,27 @@ def export():
)
@click.option(
'--display-mode-model', '-dmm', type=click.Choice(['shaded', 'surface',
'surfacewithedges', 'wireframe', 'points'], case_sensitive=False),
'surfacewithedges', 'wireframe', 'points'], case_sensitive=False),
default='shaded', help='Set display mode for the model.', show_default=True
)
@click.option(
'--grid-options', '-go', type=click.Choice(['ignore', 'points', 'meshes'],
case_sensitive=False), default='ignore', help='Export sensor grids as either'
case_sensitive=False), default='ignore', help='Export sensor grids as either'
' points or meshes.', show_default=True,
)
@click.option(
'--display-mode-grid', '-dmg', type=click.Choice(['shaded', 'surface',
'surfacewithedges', 'wireframe', 'points'], case_sensitive=False),
'surfacewithedges', 'wireframe', 'points'], case_sensitive=False),
default='shaded', help='Set display mode for the Sensorgrids.', show_default=True
)
@click.option(
'--view', '-vf', help='File Path to the Radiance view file.',
type=click.Path(exists=True),
default=None, show_default=True, multiple=True
)
def export(
hbjson_file, name, folder, image_type, image_width, image_height,
background_color, display_mode_model, grid_options, display_mode_grid):
background_color, display_mode_model, grid_options, display_mode_grid, view):
"""Export images from radiance views in a HBJSON file.
\b
Expand Down Expand Up @@ -128,15 +134,24 @@ def export(
scene.add_actors(actors)

# Set a default camera if there are not cameras in the model
if not model.cameras:
if not model.cameras and not view:
# Use the centroid of the model for the camera position
actors = Actor.from_model(model=model)
position = Actor.get_centroid(actors)
camera = Camera(position=position, type='l')
scene.add_cameras(camera)

else:
cameras = model.cameras
scene.add_cameras(cameras)
# Collection cameras from model, if the model has it
if len(model.cameras) != 0:
cameras = model.cameras
scene.add_cameras(cameras)

# if view files are provided collect them
if view:
for vf in view:
camera = Camera.from_view_file(file_path=vf)
scene.add_cameras(camera)

output = scene.export_images(
folder=folder, name=name, image_type=image_type,
Expand All @@ -146,4 +161,4 @@ def export(
raise ClickException(f'Translation failed:\n{e}')
else:
print(f'Success: {output}', file=sys.stderr)
return sys.exit(0)
return sys.exit(0)
1 change: 1 addition & 0 deletions tests/assets/view.vf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rvu -vtv -vp 0.000 0.000 0.000 -vd 0.000 0.000 1.000 -vu 0.000 1.000 0.000 -vh 29.341 -vv 32.204 -vs -0.500 -vl -0.500 -vo 100.000 -va 200.000
1 change: 1 addition & 0 deletions tests/assets/view1.vf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rvu -vtv -vp 0.000 0.000 0.000 -vd 0.000 0.000 1.000 -vu 0.000 1.000 0.000 -vh 29.341 -vv 32.204 -vs -0.500 -vl -0.500 -vo 100.000 -va 200.000
7 changes: 7 additions & 0 deletions tests/camera_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,10 @@ def test_adjustable_postion():
camera = Camera(position=(0, 0, 5000), direction=(0, 0, -1))
assert camera._outermost_point(bounds=bounds) == Point3D(12.00, 20.00, 15.00)
assert camera._adjusted_position(bounds=bounds) == (0, 0, 16)


def test_from_view_file():
"""Test creation of a camera object from a Radiance view file."""
vf = r'./tests/assets/view.vf'
camera = Camera.from_view_file(vf)
assert isinstance(camera, Camera)
16 changes: 10 additions & 6 deletions tests/cli/export_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
# def test_export_image():
# """Testing command with defaults."""
# runner = CliRunner()
# file_path = './tests/assets/gridbased.hbjson'
# file_path = './tests/assets/gridbased.hbjson'
# target_folder = './tests/assets/target'
# view_file = './tests/assets/view.vf'
# view_file_1 = './tests/assets/view1.vf'

# result = runner.invoke(export, [file_path, '--folder', target_folder, '--name',
# 'Model', '--image-type', 'PNG', '--image-width', 200, '--image-height', 500,
# '--background-color', 255, 255, 255, '--grid-options', 'Meshes',
# '--display-mode-model', 'Shaded', '--display-mode-grid', 'SurfaceWithEdges'])
# result = runner.invoke(
# export, [file_path, '--folder', target_folder, '--name', 'Model', '--image-type',
# 'PNG', '--image-width', 200, '--image-height', 500,
# '--background-color', 255, 255, 255, '--grid-options', 'Meshes',
# '--display-mode-model', 'Shaded', '--display-mode-grid',
# 'SurfaceWithEdges', '--view', view_file, '--view', view_file_1])

# assert result.exit_code == 0
# images_path = os.listdir(target_folder)
# assert all([item[:5] == 'Model' and item[-4:] == '.png' for item in images_path])
# nukedir(target_folder, True)
# nukedir(target_folder, True)
45 changes: 7 additions & 38 deletions tests/scene_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
from honeybee_vtk.vtkjs.schema import SensorGridOptions, DisplayMode
from honeybee_vtk.camera import Camera
from honeybee_vtk.actor import Actor
from honeybee_vtk.types import ImageTypes, Colors
from honeybee_vtk.legend_parameter import LabelFormat, Orientation
from honeybee_vtk.types import ImageTypes


def test_class_initialization():
Expand Down Expand Up @@ -156,12 +155,12 @@ def test_adding_data():
assert 'Daylight-factor' in model.sensor_grids.fields_info.keys()


# def test_export_images():
# """Test export images method."""
# def test_export_images_from_view_file():
# """Test export images method using the from_view_file classmethod."""
# file_path = r'./tests/assets/gridbased.hbjson'
# results_folder = r'./tests/assets/df_results'
# target_folder = r'./tests/assets/temp'
# csv_path = r'./tests/assets/radiation.csv'
# view_file_path = r'./tests/assets/view.vf'

# model = Model.from_hbjson(file_path, load_grids=SensorGridOptions.Mesh)
# model.update_display_mode(DisplayMode.Wireframe)
Expand All @@ -177,48 +176,18 @@ def test_adding_data():
# model.sensor_grids.color_by = 'Daylight-factor'
# model.sensor_grids.display_mode = DisplayMode.SurfaceWithEdges

# radiation = []
# with open(csv_path) as csvfile:
# csvreader = csv.reader(csvfile)
# for data in csvreader:
# radiation.append([float(data[0])])

# model.shades.add_data_fields(radiation, name='Radiation', data_range=(0, 2000),
# colors=Colors.original)
# model.shades.color_by = 'Radiation'
# model.shades.display_mode = DisplayMode.SurfaceWithEdges

# # actors
# actors = Actor.from_model(model=model)

# # Initialize a scene
# scene = Scene(background_color=(255, 255, 255))
# scene.add_actors(actors)

# scene.legend_parameters['Daylight-factor'].orientation = Orientation.horizontal
# scene.legend_parameters['Daylight-factor'].show_legend = True
# scene.legend_parameters['Daylight-factor'].position = (0.0, 0.1)

# rd = scene.legend_parameter('Radiation')
# rd.orientation = Orientation.vertical
# rd.height = 0.45
# rd.width = 0.05
# rd.label_format = LabelFormat.integer
# rd.show_legend = True
# rd.position = (0.90, 0.1)

# # A camera setup using the constructor
# camera = Camera(position=(-50.28, -30.32, 58.64), direction=(0.59, 0.44, -0.67),
# up_vector=(0.53, 0.40, 0.74), h_size=52.90)

# # Cameras extracted from hbjson
# cameras = model.cameras

# # Gather all the cameras
# cameras.append(camera)
# # A camera setup using the classmethod
# camera = Camera.from_view_file(file_path=view_file_path)

# # Add all the cameras to the scene
# scene.add_cameras(cameras)
# scene.add_cameras(camera)

# # if target folder exists, delete it and create a fresh new folder
# if os.path.isdir(target_folder):
Expand Down

0 comments on commit 2c43bb7

Please sign in to comment.