def test_remove_actor(): """Test removing an actor from a scene.""" file_path = r'tests/assets/gridbased.hbjson' model = Model.from_hbjson(file_path, load_grids=SensorGridOptions.Mesh) actors = Actor.from_model(model=model) scene = Scene() scene.add_actors(actors) scene.remove_actor('Shade') scene.add_cameras(model.cameras) assert 'Shade' not in scene.actors
def test_load_legend_parameter(): """Testing load_legend_parameters function.""" model_grid_mesh = r'tests/assets/gridbased.hbjson' valid_json_path = r'tests/assets/config/valid.json' model = Model.from_hbjson(model_grid_mesh, load_grids=SensorGridOptions.Mesh) cameras = model.cameras actors = model.actors() scene = Scene() scene.add_cameras(cameras) scene.add_actors(actors)
def test_validate_data_file_lengths_mismatch(): model_grid_mesh = r'tests/assets/gridbased.hbjson' short_length = r'tests/assets/config/short_length.json' model_grids_loaded = Model.from_hbjson(model_grid_mesh, load_grids=SensorGridOptions.Mesh) scene_grids_loaded = Scene() cameras = model_grids_loaded.cameras actors = model_grids_loaded.actors() scene_grids_loaded.add_cameras(cameras) scene_grids_loaded.add_actors(actors) with pytest.raises(ValueError): model_grids_loaded.load_config(short_length, scene_grids_loaded)
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_validate_data_identifier_mismatch(): model_grid_mesh = r'tests/assets/gridbased.hbjson' identifier_mismatch = r'tests/assets/config/identifier_mismatch.json' model_grids_loaded = Model.from_hbjson(model_grid_mesh, load_grids=SensorGridOptions.Mesh) scene_grids_loaded = Scene() cameras = model_grids_loaded.cameras actors = model_grids_loaded.actors() scene_grids_loaded.add_cameras(cameras) scene_grids_loaded.add_actors(actors) with pytest.raises(AssertionError): model_grids_loaded.load_config(identifier_mismatch, scene_grids_loaded, validation=True)
def test_load_legend_parameter(): """Testing load_legend_parameters function.""" model = Model.from_hbjson(model_grid_mesh, load_grids=SensorGridOptions.Mesh) cameras = model.cameras actors = Actor.from_model(model) scene = Scene() scene.add_cameras(cameras) scene.add_actors(actors) # warning when loading data that is requested to be kept hidden with pytest.warns(Warning): load_config(valid_json_path, model, scene) # warning when loading data without min and max of legend specified with pytest.warns(Warning): load_config(range_json_path, model, scene)
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_properties(): """Test properties of a scene.""" file_path = r'tests/assets/gridbased.hbjson' model = Model.from_hbjson(file_path, load_grids=SensorGridOptions.Mesh) actors = Actor.from_model(model=model) scene = Scene() scene.add_cameras(model.cameras) scene.add_actors(actors) assert isinstance(scene.background_color, vtk.vtkColor3d) assert len(scene.cameras) == 6 for camera in scene.cameras: assert isinstance(camera, Camera) assert len(scene.actors) == 6 for actor in scene.actors: assert isinstance(actor, str)
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)
def test_grid_display_mode(): """Test if correct dosplay mode is being selected for grids based on the type of grids in the model.""" model_grid_mesh = r'tests/assets/gridbased.hbjson' valid_json_path = r'tests/assets/config/valid.json' model_grids_loaded = Model.from_hbjson(model_grid_mesh, load_grids=SensorGridOptions.Mesh) scene_grids_loaded = Scene() cameras = model_grids_loaded.cameras actors = model_grids_loaded.actors() scene_grids_loaded.add_cameras(cameras) scene_grids_loaded.add_actors(actors) # Here the DisplayMode.Shaded is set when the ModelDataSet is initialized model_grids_loaded.load_config(valid_json_path, scene_grids_loaded) assert model_grids_loaded.sensor_grids.display_mode == DisplayMode.Shaded model_sensors_loaded = Model.from_hbjson( model_grid_mesh, load_grids=SensorGridOptions.Sensors) # Here the DisplayMode.Shaded is set when the ModelDataSet is initialized model_sensors_loaded.load_config(valid_json_path, scene_grids_loaded) assert model_sensors_loaded.sensor_grids.display_mode == DisplayMode.Shaded
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_create_window(): interactor, window, renderer = assistant._create_window()
def test_validate_data_grids_not_loaded(): with pytest.raises(AssertionError): load_config(valid_json_path, model, scene) model_grids_loaded = Model.from_hbjson(model_grid_mesh, load_grids=SensorGridOptions.Mesh) model_sensors_loaded = Model.from_hbjson(model_grid_mesh, load_grids=SensorGridOptions.Sensors) scene_grids_loaded = Scene() cameras = model_grids_loaded.cameras actors = Actor.from_model(model_grids_loaded) scene_grids_loaded.add_cameras(cameras) scene_grids_loaded.add_actors(actors) def test_validate_data_number_of_grids_mismatch(): with pytest.raises(AssertionError): load_config(more_grids, model_grids_loaded, scene_grids_loaded) def test_validate_data_identifier_mismatch(): with pytest.raises(AssertionError): load_config(identifier_mismatch, model_grids_loaded, scene_grids_loaded) def test_validate_data_file_lengths_mismatch(): with pytest.raises(ValueError):
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 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)