def test_aerial_cameras(): """Test creation of default aerial cameras.""" file_path = r'tests/assets/revit_model/model.hbjson' model = Model.from_hbjson(file_path) actors = Actor.from_model(model) bounds = Actor.get_bounds(actors) centroid = Actor.get_centroid(actors) cameras = Camera.aerial_cameras(bounds, centroid) assert len(cameras) == 4 def rounded(tup): return list(map(lambda x: round(x, 4), tup)) cam_01, cam_02, cam_03, cam_04 = cameras assert rounded((cam_01.position)) == rounded( (124.35035099551129, 87.54659771487164, 56.45049285888672)) assert rounded((cam_01.direction)) == rounded( (-108.78812527224468, -108.78812527224468, -41.03615807294845)) assert cam_01.up_vector == (0.0, 0.0, 1.0) assert rounded((cam_02.position)) == rounded( (-93.22589954897808, 87.54659771487164, 56.45049285888672)) assert rounded((cam_02.direction)) == rounded( (108.78812527224468, -108.78812527224468, -41.03615807294845)) assert cam_02.up_vector == (0.0, 0.0, 1.0) assert rounded((cam_03.position)) == rounded( (-93.22589954897808, -130.02965282961773, 56.45049285888672)) assert rounded((cam_03.direction)) == rounded( (108.78812527224468, 108.78812527224468, -41.03615807294845)) assert cam_03.up_vector == (0.0, 0.0, 1.0) assert rounded((cam_04.position)) == rounded( (124.35035099551129, -130.02965282961773, 56.45049285888672)) assert rounded((cam_04.direction)) == rounded( (-108.78812527224468, 108.78812527224468, -41.03615807294845)) assert cam_04.up_vector == (0.0, 0.0, 1.0)
def test_to_vtk(): "Test if default properties of the camera object." # Initialize a camera object and assess all the default properties camera = Camera() assert camera.identifier[0:6] == 'camera' assert camera.position == (0, 0, 100) assert camera.direction == (0, 0, -1) assert camera.up_vector == (0, 1, 0) assert camera.h_size == 60 assert camera.v_size == 60 assert camera.type == 'v' # Assess type of the outcome of the to_vtk method camera = camera.to_vtk() assert isinstance(camera, vtk.vtkCamera)
def test_scene_camera(): "Test a scene constructed with a camera object." 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) scene = Scene(background_color=(255, 255, 255)) scene.add_cameras(camera) assert len(scene.cameras) == 1 assert scene.cameras[0].position == (-50.28, -30.32, 58.64)
def test_adjustable_postion(): """Test if correct adjustable position is being returned.""" bounds = [ Point3D(-7.00, -4.00, 1.00), Point3D(12.00, 9.00, 5.00), Point3D(-8.00, -4.00, 0.00), Point3D(13.00, 9.00, 5.00), Point3D(-8.00, -7.00, 0.00), Point3D(12.00, 20.00, 15.00), Point3D(-8.00, -4.00, 0.00), Point3D(13.00, 9.00, 0.00), Point3D(-8.00, -4.00, 3.00), Point3D(13.00, 9.00, 5.00) ] 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_to_vtk(): "Test if default properties of the camera object." # Initialize a camera object and assess all the default properties camera = Camera() assert camera.identifier[0:6] == 'camera' assert camera.position.value == (0, 0, 100) assert camera.direction.value == (0, 0, -1) assert camera.up_vector.value == (0, 1, 0) assert camera.h_size.value == 60 assert camera.v_size.value == 30 assert camera.type.value == 'v' assert camera.flat_view_direction[(0, 0, -1)] == [2, '+'] assert camera.flat_view_direction[(0, 0, 1)] == [2, '-'] assert camera.flat_view_direction[(0, 1, 0)] == [1, '+'] assert camera.flat_view_direction[(0, -1, 0)] == [1, '-'] assert camera.flat_view_direction[(-1, 0, 0)] == [0, '-'] assert camera.flat_view_direction[(1, 0, 0)] == [0, '+'] # Assess type of the outcome of the to_vtk method camera = camera.to_vtk() assert isinstance(camera, vtk.vtkCamera)
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' view_file_path = r'tests/assets/view.vf' model = Model.from_hbjson(file_path, load_grids=SensorGridOptions.Mesh) model.update_display_mode(DisplayMode.Wireframe) daylight_factor = [] for grid in model.sensor_grids.data: res_file = pathlib.Path(results_folder, f'{grid.identifier}.res') grid_res = [float(v) for v in res_file.read_text().splitlines()] daylight_factor.append(grid_res) model.sensor_grids.add_data_fields(daylight_factor, name='Daylight-factor', per_face=True, data_range=(0, 20)) model.sensor_grids.color_by = 'Daylight-factor' model.sensor_grids.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) # 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(camera) # if target folder exists, delete it and create a fresh new folder if os.path.isdir(target_folder): shutil.rmtree(target_folder) os.mkdir(target_folder) # Export images for all the cameras images_path = scene.export_images(folder=target_folder, image_type=ImageTypes.png, name='camera') for path in images_path: assert os.path.isfile(path) shutil.rmtree(target_folder)
def test_add_cameras_from_model(): """Test adding a list of cameras.""" file_path = r'tests/assets/gridbased.hbjson' model = Model.from_hbjson(file_path, load_grids=SensorGridOptions.Mesh) assert len(model.cameras) == 6 cameras = model.cameras 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.append(camera) scene = Scene(background_color=(255, 255, 255)) scene.add_cameras(cameras) assert len(scene.cameras) == 7
def test_initialization(): """Test objct initialization.""" model = Model.from_hbjson(hbjson=file_path) actors = Actor.from_model(model=model) camera = Camera() scene = Scene() scene.add_actors(actors) scene.add_cameras(camera) assistant = Assistant(background_color=(0, 0, 0), camera=camera, actors=scene._actors, legend_parameters=scene.legend_parameters) assert isinstance(assistant._interactor, vtk.vtkRenderWindowInteractor) assert isinstance(assistant._window, vtk.vtkRenderWindow) assert isinstance(assistant._renderer, vtk.vtkRenderer) assert isinstance(assistant._legend_params, dict)
def test_aerial_cameras(): """Test creation of default aerial cameras.""" file_path = r'tests/assets/revit_model/model.hbjson' model = Model.from_hbjson(file_path) actors = model.actors() bounds = Actor.get_bounds(actors) centroid = Actor.get_centroid(actors) cameras = Camera.aerial_cameras(bounds, centroid) assert len(cameras) == 4 def rounded(tup): return list(map(lambda x: round(x, 4), tup)) cam_01, cam_02, cam_03, cam_04 = cameras assert rounded((cam_01.position)) == rounded( (124.2538248261054, 87.99674601954615, 56.45049285888672)) assert rounded((cam_01.direction)) == rounded( (-109.0614780942837, -109.0614780942837, -42.199789583683014)) assert cam_01.up_vector == (0.0, 0.0, 1.0) assert rounded((cam_02.position)) == rounded( (-93.869131362462, 87.99674601954615, 56.45049285888672)) assert rounded((cam_02.direction)) == rounded( (109.0614780942837, -109.0614780942837, -42.199789583683014)) assert cam_02.up_vector == (0.0, 0.0, 1.0) assert rounded((cam_03.position)) == rounded( (-93.869131362462, -130.12621016902125, 56.45049285888672)) assert rounded((cam_03.direction)) == rounded( (109.0614780942837, 109.0614780942837, -42.199789583683014)) assert cam_03.up_vector == (0.0, 0.0, 1.0) assert rounded((cam_04.position)) == rounded( (124.2538248261054, -130.12621016902125, 56.45049285888672)) assert rounded((cam_04.direction)) == rounded( (-109.0614780942837, 109.0614780942837, -42.199789583683014)) assert cam_04.up_vector == (0.0, 0.0, 1.0)
def test_write_gltf(): """Test if a gltf file can be successfully written.""" file_path = r'tests/assets/gridbased.hbjson' results_folder = r'tests/assets/df_results' target_folder = r'tests/assets/temp' model = Model.from_hbjson(file_path, load_grids=SensorGridOptions.Mesh) daylight_factor = [] for grid in model.sensor_grids.data: res_file = pathlib.Path(results_folder, f'{grid.identifier}.res') grid_res = [float(v) for v in res_file.read_text().splitlines()] daylight_factor.append(grid_res) model.sensor_grids.add_data_fields(daylight_factor, name='Daylight-factor', per_face=True, data_range=(0, 20)) model.sensor_grids.color_by = 'Daylight-factor' model.sensor_grids.display_mode = DisplayMode.SurfaceWithEdges model.update_display_mode(DisplayMode.Shaded) camera = Camera() actors = Actor.from_model(model=model) scene = Scene(background_color=(0, 0, 0)) scene.add_actors(actors) scene.add_cameras(camera) if os.path.isdir(target_folder): shutil.rmtree(target_folder) os.mkdir(target_folder) scene.export_gltf(target_folder, name='daylight-factor') gltf_path = os.path.join(target_folder, 'daylight-factor.gltf') assert os.path.isfile(gltf_path) shutil.rmtree(target_folder)
import pytest import vtk from honeybee_vtk.model import Model from honeybee_vtk.assistant import Assistant from honeybee_vtk.actor import Actor from honeybee_vtk.camera import Camera from honeybee_vtk.scene import Scene from honeybee_vtk.vtkjs.schema import SensorGridOptions file_path = r'tests/assets/gridbased.hbjson' valid_json_path = r'tests/assets/config/valid.json' model = Model.from_hbjson(file_path, load_grids=SensorGridOptions.Sensors) actors = model.actors() camera = Camera() scene = Scene() scene.add_actors(actors) scene.add_cameras(camera) model.load_config(valid_json_path, scene, legend=True) scene.update_scene() assistant = scene.assistants[0] def test_initialization(): assert isinstance(assistant._actors, list) assert isinstance(assistant._legend_params[0], LegendParameter) assert isinstance(assistant._camera, Camera) assert isinstance(assistant._background_color, vtk.vtkColor3d)
def test_export_images(): """Test export images method.""" 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_results/radiation.csv' model = Model.from_hbjson(file_path, load_grids=SensorGridOptions.Mesh) model.update_display_mode(DisplayMode.Wireframe) daylight_factor = [] for grid in model.sensor_grids.data: res_file = pathlib.Path(results_folder, f'{grid.identifier}.res') grid_res = [float(v) for v in res_file.read_text().splitlines()] daylight_factor.append(grid_res) model.sensor_grids.add_data_fields(daylight_factor, name='Daylight-factor', per_face=True, data_range=(0, 20)) 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=ColorSets.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'].hide_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.decimal_count = DecimalCount.integer rd.hide_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) # Add all the cameras to the scene scene.add_cameras(cameras) # if target folder exists, delete it and create a fresh new folder if os.path.isdir(target_folder): shutil.rmtree(target_folder) os.mkdir(target_folder) # Export images for all the cameras images_path = scene.export_images(folder=target_folder, image_type=ImageTypes.png) for path in images_path: assert os.path.isfile(path) shutil.rmtree(target_folder)
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)
def translate(hbjson_file, name, folder, file_type, display_mode, grid_options, show_html, config): """Translate a HBJSON file to an HTML or a vtkjs file. \b Args: hbjson-file: Path to an HBJSON file. """ folder = pathlib.Path(folder) folder.mkdir(exist_ok=True) # Set Sensor grids if grid_options == 'ignore': grid_options = SensorGridOptions.Ignore elif grid_options == 'points': grid_options = SensorGridOptions.Sensors elif grid_options == 'meshes': grid_options = SensorGridOptions.Mesh try: model = Model.from_hbjson(hbjson=hbjson_file, load_grids=grid_options) # Set display style if display_mode == 'shaded': model.update_display_mode(DisplayMode.Shaded) elif display_mode == 'surface': model.update_display_mode(DisplayMode.Surface) elif display_mode == 'surfacewithedges': model.update_display_mode(DisplayMode.SurfaceWithEdges) elif display_mode == 'wireframe': model.update_display_mode(DisplayMode.Wireframe) elif display_mode == 'points': model.update_display_mode(DisplayMode.Points) # load data if config: scene = Scene() actors = Actor.from_model(model) bounds = Actor.get_bounds(actors) centroid = Actor.get_centroid(actors) cameras = Camera.aerial_cameras(bounds=bounds, centroid=centroid) scene.add_actors(actors) scene.add_cameras(cameras) model = load_config(config, model, scene) # Set file type if file_type.lower() == 'html': output = model.to_html(folder=folder, name=name, show=show_html) elif file_type.lower() == 'vtkjs': output = model.to_vtkjs(folder=folder, name=name) elif file_type.lower() == 'vtk': output = model.to_files(folder=folder, name=name, writer=VTKWriters.legacy) elif file_type.lower() == 'vtp': output = model.to_files(folder=folder, name=name, writer=VTKWriters.binary) except Exception as e: traceback.print_exc() sys.exit(1) else: print(f'Success: {output}', file=sys.stderr) return sys.exit(0)
def export(hbjson_file, name, folder, image_type, image_width, image_height, background_color, display_mode_model, grid_options, display_mode_grid, view, config): """Export images from radiance views in a HBJSON file. \b Args: hbjson-file: Path to an HBJSON file. """ folder = pathlib.Path(folder) folder.mkdir(exist_ok=True) # Set image types if image_type == 'png': image_type = ImageTypes.png elif image_type == 'jpg': image_type = ImageTypes.jpg elif image_type == 'ps': image_type == ImageTypes.ps elif image_type == 'tiff': image_type == ImageTypes.tiff elif image_type == 'ps': image_type == ImageTypes.ps elif image_type == 'pnm': image_type == ImageTypes.pnm # Set Sensor grids if grid_options == 'ignore': grid_options = SensorGridOptions.Ignore elif grid_options == 'points': grid_options = SensorGridOptions.Sensors elif grid_options == 'meshes': grid_options = SensorGridOptions.Mesh try: model = Model.from_hbjson(hbjson=hbjson_file, load_grids=grid_options) # Set model's display mode if display_mode_model == 'shaded': model.update_display_mode(DisplayMode.Shaded) elif display_mode_model == 'surface': model.update_display_mode(DisplayMode.Surface) elif display_mode_model == 'surfacewithedges': model.update_display_mode(DisplayMode.SurfaceWithEdges) elif display_mode_model == 'wireframe': model.update_display_mode(DisplayMode.Wireframe) elif display_mode_model == 'points': model.update_display_mode(DisplayMode.Points) # Set model's grid's display mode if display_mode_grid == 'shaded': model.sensor_grids.display_mode = DisplayMode.Shaded elif display_mode_model == 'surface': model.sensor_grids.display_mode = DisplayMode.Surface elif display_mode_model == 'surfacewithedges': model.sensor_grids.display_mode = DisplayMode.SurfaceWithEdges elif display_mode_model == 'wireframe': model.sensor_grids.display_mode = DisplayMode.Wireframe elif display_mode_model == 'points': model.sensor_grids.display_mode = DisplayMode.Points actors = Actor.from_model(model) scene = Scene(background_color=background_color) scene.add_actors(actors) # Set a default camera if there are not cameras in the model 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: # 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) # load config if provided if config: model.load_config(config) output = scene.export_images(folder=folder, name=name, image_type=image_type, image_width=image_width, image_height=image_height) except Exception: traceback.print_exc() sys.exit(1) else: print(f'Success: {output}', file=sys.stderr) return sys.exit(0)