def check_surface_properties(surface): assert isinstance(surface, dict) file_name = surface['file'] assert file_name is not None vf.validate_is_file(file_name) colour_value = surface['colour'] assert isinstance(colour_value, list) assert len(colour_value) == 3 assert isinstance(colour_value[0], int) assert colour_value[0] >= 0 assert colour_value[0] <= 255 assert isinstance(colour_value[1], int) assert colour_value[1] >= 0 assert colour_value[1] <= 255 assert isinstance(colour_value[2], int) assert colour_value[2] >= 0 assert colour_value[2] <= 255 opacity_value = surface['opacity'] assert isinstance(opacity_value, float) assert opacity_value >= 0.0 assert opacity_value <= 1.0 visibility_value = surface['visibility'] assert isinstance(visibility_value, bool) pickable_value = surface['pickable'] assert isinstance(pickable_value, bool)
def __init__(self, filename, colour=[1.0, 0.0, 0.0], visibility=True, opacity=1.0, pickable=True): """ Creates a new surface model. :param filename: path to vtk grid file. :param colour: (R,G,B) where each are floats [0-1] :param visibility: boolean, True|False :param opacity: float [0,1] :param pickable: boolean, True|False """ super(VTKUnstructuredGridModel, self).__init__(colour, visibility, opacity, pickable) self.source_file = None self.reader = None self.source = None self.cell_data_name = None self.threshold = None if filename is not None: vf.validate_is_file(filename) if filename.endswith(".vtk"): self.reader = vtk.vtkUnstructuredGridReader() elif filename.endswith(".vtu"): self.reader = vtk.vtkXMLUnstructuredGridReader() else: raise TypeError("File is not .vtu or .vtk extension") self.reader.SetFileName(filename) self.reader.Update() self.source = self.reader.GetOutput() self.source_file = filename self.name = os.path.basename(self.source_file) else: raise ValueError("Filename not specified") self.threshold = vtk.vtkThreshold() self.threshold.SetInputData(self.source) self.threshold.Update() self.thresholded_data = self.threshold.GetOutput() self.mapper = vtk.vtkDataSetMapper() self.mapper.SetInputData(self.thresholded_data) self.mapper.Update() self.actor.SetMapper(self.mapper) self.cell_data_name = self.source.GetCellData().GetArrayName(0)
def __init__(self, file_name, write_on_setter=False): abs_file = fu.get_absolute_path_of_file(file_name) f.validate_is_file(abs_file) if write_on_setter: f.validate_is_writable_file(abs_file) with open(abs_file, "r") as read_file: self.config_data = json.load(read_file) self.file_name = abs_file self.write_on_setter = write_on_setter
def set_texture(self, filename): """ Sets an image from a file as a texture for the model. :param filename: :return: """ # Read the texture image from a file. # Currently supports png and jpeg formats. if filename is not None: vf.validate_is_file(filename) if filename.endswith('.png'): self.texture_reader = vtk.vtkPNGReader() elif np.logical_or(filename.endswith('.jpeg'), filename.endswith('.jpg')): self.texture_reader = vtk.vtkJPEGReader() else: raise ValueError( 'File type not supported for texture loading: {}'.format( filename)) else: # Unset texture when the function is called with None. self.texture_reader = None self.texture_file = None self.texture_name = None self.texture = None self.actor.SetTexture(None) return self.texture_reader.SetFileName(filename) self.texture_file = filename self.texture_name = os.path.basename(self.texture_file) # Create texture object. self.texture = vtk.vtkTexture() if vtk.VTK_MAJOR_VERSION <= 5: self.texture.SetInput(self.texture_reader.GetOutput()) else: self.texture.SetInputConnection( self.texture_reader.GetOutputPort()) # Set the texture self.actor.SetTexture(self.texture)
def test_valid_file_name(): result = f.validate_is_file("tests/data/FordPrefect.json") assert result
def test_invalid_file_name_because_its_a_directory(): with pytest.raises(ValueError): f.validate_is_file(".")
def test_invalid_file_name_because_non_existent(): with pytest.raises(ValueError): f.validate_is_file("/invalid/file/name")
def test_invalid_file_name_because_its_an_integer(): with pytest.raises(TypeError): f.validate_is_file(1)
def test_invalid_file_name_because_its_empty(): with pytest.raises(ValueError): f.validate_is_file("")
def test_invalid_file_name_because_its_null(): with pytest.raises(ValueError): f.validate_is_file(None)
def __init__(self, filename, colour, visibility=True, opacity=1.0, pickable=True): """ Creates a new surface model. :param filename: if None a default, empty, vtkPolyData is created. :param colour: (R,G,B) where each are floats [0-1] :param visibility: boolean, True|False :param opacity: float [0,1] :param pickable: boolean, True|False """ super(VTKSurfaceModel, self).__init__(colour, visibility, opacity, pickable) self.source_file = None self.reader = None self.source = None self.texture_file = None self.texture_name = None self.texture_reader = None self.texture = None # Works like FactoryMethod. Could be refactored elsewhere. if filename is not None: vf.validate_is_file(filename) if filename.endswith('.vtk'): self.reader = vtk.vtkPolyDataReader() elif filename.endswith('.stl'): self.reader = vtk.vtkSTLReader() elif filename.endswith('.ply'): self.reader = vtk.vtkPLYReader() elif filename.endswith('.vtp'): self.reader = vtk.vtkXMLPolyDataReader() else: raise ValueError( f'File type not supported for model loading: {filename}') self.reader.SetFileName(filename) self.reader.Update() self.source = self.reader.GetOutput() self.source_file = filename self.name = os.path.basename(self.source_file) else: # Creates a new empty vtkPolyData, that the client # can dynamically fill with new data. self.source = vtk.vtkPolyData() self.source_file = "" self.name = "" # Only create normals if there are none on input self.normals = None if self.source.GetPointData().GetNormals() is None: self.normals = vtk.vtkPolyDataNormals() self.normals.SetInputData(self.source) self.normals.SetAutoOrientNormals(True) self.normals.SetFlipNormals(False) self.transform = vtk.vtkTransform() self.transform.Identity() self.transform_filter = vtk.vtkTransformPolyDataFilter() if self.normals is None: self.transform_filter.SetInputData(self.source) else: self.transform_filter.SetInputConnection( self.normals.GetOutputPort()) self.transform_filter.SetTransform(self.transform) self.mapper = vtk.vtkPolyDataMapper() self.mapper.SetInputConnection(self.transform_filter.GetOutputPort()) self.mapper.Update() self.actor.SetMapper(self.mapper) self.no_shading = False self.ambient = self.actor.GetProperty().GetAmbient() self.diffuse = self.actor.GetProperty().GetDiffuse() self.specular = self.actor.GetProperty().GetSpecular()