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_bounds(): """Test bounds.""" model = Model.from_hbjson(file_path) actors = Actor.from_model(model) bounds = Actor.get_bounds(actors) assert isinstance(bounds, list) check = [isinstance(point, Point3D) for point in bounds] assert check.count(True) == len(bounds)
def test_monochrome(): """Test setting actors to monochrome colors.""" model = Model.from_hbjson(file_path) actors = Actor.from_model(model) with pytest.raises(ValueError): actors[0].get_monochrome((255, 123, 33)) actors[0].get_monochrome((0.34, 0.44, 0.55)) assert actors[0].monochrome_color == (0.34, 0.44, 0.55)
def test_class_initialization(): """Test default properties of a Actors object.""" with pytest.raises(TypeError): actors = Actor() model = Model.from_hbjson(file_path) actors = Actor.from_model(model) for actor in actors: assert not actor.monochrome_color
def test_assign_bounds(): """Test bounds assignment.""" file_path = r'tests/assets/viewbased.hbjson' model = Model.from_hbjson(file_path, load_grids=SensorGridOptions.Mesh) actors = Actor.from_model(model) bounds = Actor.get_bounds(actors) check = [isinstance(point, Point3D) for point in bounds] assert check.count(True) == len(bounds)
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_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_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)
load_config(invalid_json_path, model, scene) 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_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_to_vtk(): """Test the to_vtk method.""" model = Model.from_hbjson(file_path) actors = Actor.from_model(model) for actor in actors: assert isinstance(actor.to_vtk(), vtk.vtkActor)
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)