Example #1
0
def test_no_view():
    """Test if views are being read from hbjson."""
    file_path = r'tests/assets/unnamed.hbjson'

    model = Model.from_hbjson(file_path, load_grids=SensorGridOptions.Mesh)
    # Checking if valueerror is raised when from_model is called on a model with no views
    with pytest.warns(Warning):
        model = Model.from_hbjson(file_path, load_grids=SensorGridOptions.Mesh)
Example #2
0
def test_load_grids():
    """Test loading of grids from hbjson."""

    file_path = r'tests/assets/revit_model/model.hbjson'
    model = Model.from_hbjson(file_path, load_grids=SensorGridOptions.Ignore)
    assert model.sensor_grids.data == []

    model = Model.from_hbjson(file_path, load_grids=SensorGridOptions.Mesh)
    assert len(model.sensor_grids.data) == 15
Example #3
0
def test_get_grid_type():
    """Test if _get_grid_type returns correct grid type."""
    model_grid_mesh = r'tests/assets/gridbased.hbjson'
    model_grids_loaded = Model.from_hbjson(model_grid_mesh,
                                           load_grids=SensorGridOptions.Mesh)
    assert model_grids_loaded._get_grid_type() == 'meshes'

    model_sensors_loaded = Model.from_hbjson(
        model_grid_mesh, load_grids=SensorGridOptions.Sensors)
    assert model_sensors_loaded._get_grid_type() == 'points'
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)
Example #5
0
def test_validate_data_grids_not_loaded():
    model_grid_mesh = r'tests/assets/gridbased.hbjson'
    valid_json_path = r'tests/assets/config/valid.json'
    model = Model.from_hbjson(model_grid_mesh)
    scene = Scene()
    with pytest.raises(AssertionError):
        model.load_config(valid_json_path, scene)
Example #6
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)
Example #7
0
def test_monochrome():
    """Test setting actors to monochrome colors."""
    actors = Model.from_hbjson(file_path).actors()
    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)
Example #8
0
def test_class_initialization():
    """Test default properties of a Actors object."""
    with pytest.raises(TypeError):
        actors = Actor()

    actors = Model.from_hbjson(file_path).actors()
    for actor in actors:
        assert not actor.monochrome_color
Example #9
0
def test_properties():
    """Test properties of a model."""

    file_path = r'tests/assets/unnamed.hbjson'
    model = Model.from_hbjson(file_path, load_grids=SensorGridOptions.Mesh)

    # model is an iterator object. Hence, we're using a for loop to test properties
    for dataset in model:
        assert isinstance(dataset, ModelDataSet)
Example #10
0
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)
Example #11
0
def test_validate_data_invalid_json():
    """Tets if invalid json is detected."""
    model_grid_mesh = r'tests/assets/gridbased.hbjson'
    invalid_json_path = r'tests/assets/config/invalid.json'
    model = Model.from_hbjson(model_grid_mesh,
                              load_grids=SensorGridOptions.Mesh)
    scene = Scene()
    with pytest.raises(TypeError):
        model.load_config(invalid_json_path, scene)
def grid_type():
    """Test if validate_data returns correct grid type."""

    data = DataConfig()
    assert _validate_data(data, model_grids_loaded) == 'meshes'

    model_with_sensors_only = Model.from_hbjson(
        model_sensors_loaded, load_grids=SensorGridOptions.Mesh)
    assert _validate_data(data, model_with_sensors_only) == 'points'
Example #13
0
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
Example #14
0
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)
Example #15
0
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)
Example #16
0
def test_hbjson_vtk_conversion():
    """Test is hbjson is being converted into vtk polydata correctly."""
    file_path = r'tests/assets/gridbased.hbjson'
    model = Model.from_hbjson(file_path, load_grids=SensorGridOptions.Mesh)

    assert len(model.shades.data) == 40
    assert len(model.doors.data) == 0
    assert len(model.apertures.data) == 7
    assert len(model.walls.data) == 8
    assert len(model.floors.data) == 2
    assert len(model.roof_ceilings.data) == 2
    assert len(model.air_boundaries.data) == 0
    assert len(model.sensor_grids.data) == 2
Example #17
0
def test_default_colors():
    """Test default colors for the model objects."""
    file_path = r'tests/assets/revit_model/model.hbjson'
    model = Model.from_hbjson(file_path, load_grids=SensorGridOptions.Mesh)

    assert model.get_default_color('Aperture') == Color(63, 179, 255, 127)
    assert model.get_default_color('Door') == Color(159, 149, 99, 255)
    assert model.get_default_color('Shade') == Color(119, 74, 189, 255)
    assert model.get_default_color('Wall') == Color(229, 179, 59, 255)
    assert model.get_default_color('Floor') == Color(255, 127, 127, 255)
    assert model.get_default_color('RoofCeiling') == Color(127, 19, 19, 255)
    assert model.get_default_color('AirBoundary') == Color(255, 255, 199, 255)
    assert model.get_default_color('Grid') == Color(235, 63, 102, 255)
Example #18
0
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)
Example #19
0
def test_write_vtkjs():
    """Test if a vtkjs file can be successfully written."""

    file_path = r'tests/assets/unnamed.hbjson'
    model = Model.from_hbjson(file_path, load_grids=SensorGridOptions.Mesh)

    target_folder = r'tests/assets/temp'
    if os.path.isdir(target_folder):
        shutil.rmtree(target_folder)
    os.mkdir(target_folder)
    model.to_vtkjs(folder=target_folder, name='Model')
    html_path = os.path.join(target_folder, 'Model.vtkjs')
    assert os.path.isfile(html_path)
    shutil.rmtree(target_folder)
Example #20
0
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
Example #21
0
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)
Example #22
0
def test_config():
    """Test loading config file."""
    file_path = r'tests/assets/gridbased.hbjson'
    valid_json_path = r'tests/assets/config/valid.json'
    invalid_json_path = r'tests/assets/config/invalid.json'
    more_grids = r'tests/assets/config/more_grids.json'
    no_file = r'tests/assets/config/no_file.json'
    more_files = r'tests/assets/config/more_files.json'
    short_length = r'tests/assets/config/short_length.json'

    model = Model.from_hbjson(file_path)

    # invalid json file
    with pytest.raises(TypeError):
        model.load_config(invalid_json_path)

    # when grids are not loaded on a model and one is trying to mount data for grids
    with pytest.raises(AssertionError):
        model.load_config(valid_json_path)

    model = Model.from_hbjson(file_path, load_grids=SensorGridOptions.Mesh)
    # trying to load more grids than grids in the model
    with pytest.raises(ValueError):
        model.load_config(more_grids)

    # Empty list if provided in file_paths
    with pytest.raises(ValueError):
        model.load_config(no_file)

    # If more than one files are provided in file_path for non-grid object_type
    with pytest.raises(ValueError):
        model.load_config(more_files)

    # if file lengths do not match grid size
    with pytest.raises(ValueError):
        model.load_config(short_length)
Example #23
0
def test_grid_to_images_assertion_error():
    """Test if AssertionError is raised when grids are not loaded."""
    file_path = r'tests/assets/gridbased.hbjson'
    json_path = r'tests/assets/config/valid.json'
    model = Model.from_hbjson(file_path, load_grids=SensorGridOptions.Ignore)

    target_folder = r'tests/assets/temp'
    if os.path.isdir(target_folder):
        shutil.rmtree(target_folder)
    os.mkdir(target_folder)

    with pytest.raises(AssertionError):
        model.to_grid_images(config=json_path, folder=target_folder)

    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
Example #25
0
def translate(
        hbjson_file, name, folder, file_type, display_mode, grid_options, show_html):
    """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)

        # Set file type
        if file_type == 'html':
            output = model.to_html(folder=folder, name=name, show=show_html)
        else:
            output = model.to_vtkjs(folder=folder, name=name)
        print(output)
    except Exception as e:
        raise ClickException(f'Translation failed:\n{e}')
    else:
        print(f'Success: {output}', file=sys.stderr)
        return sys.exit(0)
Example #26
0
def test_displaymode():
    """Test displaymode assignment of model and model objects."""

    file_path = r'tests/assets/unnamed.hbjson'
    model = Model.from_hbjson(file_path, load_grids=SensorGridOptions.Mesh)

    # set display mode for model
    model.update_display_mode(DisplayMode.Shaded)

    # check display mode is assgined to all the objects in a model
    for dataset in model:
        assert dataset.display_mode == DisplayMode.Shaded

    # assign a different display model to the sensor grids
    model.sensor_grids.display_mode = DisplayMode.Wireframe
    with pytest.raises(AssertionError):
        for dataset in model:
            assert dataset.display_mode == DisplayMode.Shaded
Example #27
0
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_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)
Example #29
0
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)
Example #30
0
def test_get_legend_range():
    """Test if the _get_legend_range supplies correct range."""
    path = r'tests/assets/gridbased.hbjson'
    model = Model.from_hbjson(path)
    # if neither min nor max is set
    lc = LegendConfig()
    dc = DataConfig(identifier='test',
                    object_type=DataSetNames.grid,
                    unit='sample',
                    path='.',
                    legend_parameters=lc)
    legend_range = model._get_legend_range(dc)
    assert legend_range == [None, None]

    # if both min and max are set
    lc = LegendConfig(min=0, max=0)
    dc = DataConfig(identifier='test',
                    object_type=DataSetNames.grid,
                    unit='sample',
                    path='.',
                    legend_parameters=lc)
    legend_range = model._get_legend_range(dc)
    assert legend_range == [0.0, 0.0]

    # if only min is set
    lc = LegendConfig(min=0)
    dc = DataConfig(identifier='test',
                    object_type=DataSetNames.grid,
                    unit='sample',
                    path='.',
                    legend_parameters=lc)
    legend_range = model._get_legend_range(dc)
    assert legend_range == [0.0, None]

    # if only max is set
    lc = LegendConfig(max=0)
    dc = DataConfig(identifier='test',
                    object_type=DataSetNames.grid,
                    unit='sample',
                    path='.',
                    legend_parameters=lc)
    legend_range = model._get_legend_range(dc)
    assert legend_range == [None, 0.0]