示例#1
0
    def import_obj(cls, filename, scaling=1.0, **kwargs):
        """
        Create a mesh instance from a Wavefront OBJ mesh file (.obj).

        :param str filename: Mesh file path.
        :param double scaling: Scale the mesh by this factor (default=1.0).
        :param kwargs: Accepts optional keyword arguments from the Mesh class.
        :rtype: Mesh

        .. code-block:: pycon

            >>> from raysect.optical import World, translate, rotate, ConstantSF, Sellmeier, Dielectric
            >>> from raysect.primitive import import_obj
            >>>
            >>> world = World()
            >>>
            >>> diamond = Dielectric(Sellmeier(0.3306, 4.3356, 0.0, 0.1750**2, 0.1060**2, 0.0),
            >>>                      ConstantSF(1.0))
            >>>
            >>> bunny_mesh = import_obj("resources/stanford_bunny.obj", scaling=1, parent=world,
            >>>                         transform=translate(0, 0, 0)*rotate(165, 0, 0), material=diamond)
        """

        vertices = []
        normals = []
        triangles = []

        with open(filename) as f:
            for line in f:

                # skip comments
                if line[0] == "#":
                    continue

                # clean up and tokenise
                tokens = line.strip().split(" ")

                # parse tokens
                cmd = tokens[0]
                tokens = tokens[1:]

                if cmd == "v":
                    vertex = cls._to_point(tokens, scaling)
                    vertices.append(vertex)

                elif cmd == "vt":
                    # texture coordinates not currently supported
                    continue

                elif cmd == "vn":
                    normal = cls._to_normal(tokens)
                    normals.append(normal)

                elif cmd == "f":
                    triangle = cls._to_triangle(tokens)
                    triangles.append(triangle)

        if normals:
            return Mesh(vertices, triangles, normals, **kwargs)
        return Mesh(vertices, triangles, **kwargs)
示例#2
0
    def import_vtk(cls, filename, scaling=1.0, mode=VTK_AUTOMATIC, **kwargs):
        """
        Create a mesh instance from a VTK mesh data file (.vtk).

        .. warning ::
           Currently only supports VTK DataFile v2.0 and unstructured grid data with
           3 element (triangular) cells.

        :param str filename: Mesh file path.
        :param double scaling: Scale the mesh by this factor (default=1.0).
        :param str mode: The file format to load: 'ascii', 'binary', 'auto' (default='auto').
        :param kwargs: Accepts optional keyword arguments from the Mesh class.
        :rtype: Mesh
        """

        mode = mode.lower()
        if mode == VTK_ASCII:
            vertices, triangles, mesh_name = cls._load_ascii(filename, scaling)
        elif mode == VTK_BINARY:
            raise NotImplementedError('The binary .vtk loading routine has not been implemented yet.')
        elif mode == VTK_AUTOMATIC:
            try:
                vertices, triangles, mesh_name = cls._load_ascii(filename, scaling)
            except ValueError:
                # vertices, triangles, mesh_name = cls._load_binary(filename, scaling)
                raise NotImplementedError('The binary .vtk loading routine has not been implemented yet.')
        else:
            modes = (VTK_ASCII, VTK_BINARY)
            raise ValueError('Unrecognised import mode, valid values are: {}'.format(modes))

        if 'name' not in kwargs.keys():
            kwargs['name'] = mesh_name or "VTKMesh"

        return Mesh(vertices, triangles, smoothing=False, **kwargs)
示例#3
0
    def import_ply(cls, filename, scaling=1.0, mode=PLY_AUTOMATIC, **kwargs):
        """
        Create a mesh instance from a Polygon File Format (PLY) mesh file (.ply).
        Note PLY is also known as the Stanford Triangle Format.

        Some engineering meshes are exported in different units (mm for example)
        whereas Raysect units are in m. Applying a scale factor of 0.001 would
        convert the mesh into m for use in Raysect.

        :param str filename: Mesh file path.
        :param double scaling: Scale the mesh by this factor (default=1.0).
        :param str mode: The file format to load: 'ascii', 'binary', 'auto' (default='auto').
        :param kwargs: Accepts optional keyword arguments from the Mesh class.
        :rtype: Mesh
        
        .. code-block:: pycon

            >>> from raysect.optical import World, translate, rotate, ConstantSF, Sellmeier, Dielectric
            >>> from raysect.primitive import import_ply
            >>>
            >>> world = World()
            >>>
            >>> diamond = Dielectric(Sellmeier(0.3306, 4.3356, 0.0, 0.1750**2, 0.1060**2, 0.0),
            >>>                      ConstantSF(1.0))
            >>>
            >>> mesh = import_ply("your_mesh.ply", scaling=1, mode='binary', parent=world,
            >>>                   transform=translate(0, 0, 0)*rotate(165, 0, 0), material=diamond)
        """

        mode = mode.lower()
        if mode == PLY_ASCII:
            vertices, triangles = cls._load_ascii(filename, scaling)

        elif mode == PLY_BINARY:
            vertices, triangles = cls._load_binary(filename, scaling)

        elif mode == PLY_AUTOMATIC:
            try:
                vertices, triangles = cls._load_ascii(filename, scaling)
            except ValueError:
                vertices, triangles = cls._load_binary(filename, scaling)

        else:
            modes = (PLY_AUTOMATIC, PLY_ASCII, PLY_BINARY)
            raise ValueError(
                'Unrecognised import mode, valid values are: {}'.format(modes))

        return Mesh(vertices, triangles, smoothing=False, **kwargs)