def load_texture_to_omf(filename, name, description):
    """Loads a PNG Image texture to an ``omf.ImageTexture`` object"""
    # Load a raster
    import gdal
    ds = gdal.Open(filename.replace('.png', '.tif'))
    # Grab the Groung Control Points
    points = np.array([get_point(gcp) for gcp in ds.GetGCPs()])
    if points.size < 1:
        raise RuntimeError(
            'No associated tif file to recover spatial reference.')
    # Now Grab the three corners of their bounding box
    #-- This guarantees we grab the right points
    bounds = pyvista.PolyData(points).bounds
    origin = np.array([bounds[0], bounds[2], bounds[4]])  # BOTTOM LEFT CORNER
    point_u = np.array([bounds[1], bounds[2],
                        bounds[4]])  # BOTTOM RIGHT CORNER
    point_v = np.array([bounds[0], bounds[3], bounds[4]])  # TOP LEFT CORNER
    axis_u = point_u - origin
    axis_v = point_v - origin
    the_texture = omf.ImageTexture(
        origin=origin,
        axis_u=axis_u,
        axis_v=axis_v,
        name=name,
        description=description,
        image=filename,
    )
    return the_texture
Example #2
0
def test_texture():
    try:
        dirname, _ = os.path.split(os.path.abspath(__file__))
        png_file = os.path.sep.join(dirname.split(os.path.sep) + ['temp.png'])
        s = ['110010010011', '101011010100', '110010110101', '100010010011']
        s = [[int(v) for v in val] for val in s]
        f = open(png_file, 'wb')
        w = png.Writer(len(s[0]), len(s), greyscale=True, bitdepth=16)
        w.write(f, s)
        f.close()

        proj = omf.Project()

        surf = omf.SurfaceElement(
            geometry=omf.SurfaceGridGeometry(
                tensor_u=[1., 1., 1., 1., 1.],
                tensor_v=[1., 1., 1., 1., 1.],
            ),
            textures=[
                omf.ImageTexture(
                    origin=[0., 0, 0],
                    axis_u=[5., 0, 0],
                    axis_v=[0., 5, 0],
                    image=png_file,
                )
            ],
        )
        proj.elements = [surf]
        proj.validate()
        view = get_view_from_proj(proj)
        view.validate()
        assert len(view.contents) == 3

    finally:
        os.remove(png_file)
Example #3
0
 def to_omf(self):
     self.validate()
     omf_texture = omf.ImageTexture(
         name=self.name or '',
         description=self.description or '',
         origin=self.origin,
         axis_u=self.axis_u,
         axis_v=self.axis_v,
         image=self.image.image,
     )
     return omf_texture
Example #4
0
 def _to_omf(self):
     import omf
     tex = omf.ImageTexture(
         name=self.title or '',
         description=self.description or '',
         origin=self.O,
         axis_u=self.U,
         axis_v=self.V,
         image=self.image,
     )
     return tex
Example #5
0
    def test_doc_ex(self):
        dirname, _ = os.path.split(os.path.abspath(__file__))
        pngfile = os.path.sep.join(dirname.split(os.path.sep)[:-1] +
                                   ['docs', 'images', 'PointSetGeometry.png'])

        proj = omf.Project(
            name='Test project',
            description='Just some assorted elements'
        )

        pts = omf.PointSetElement(
            name='Random Points',
            description='Just random points',
            geometry=omf.PointSetGeometry(
                vertices=np.random.rand(100, 3)
            ),
            data=[
                omf.ScalarData(
                    name='rand data',
                    array=np.random.rand(100),
                    location='vertices'
                ),
                omf.ScalarData(
                    name='More rand data',
                    array=np.random.rand(100),
                    location='vertices'
                )
            ],
            textures=[
                omf.ImageTexture(
                    name='test image',
                    image=pngfile,
                    origin=[0, 0, 0],
                    axis_u=[1, 0, 0],
                    axis_v=[0, 1, 0]
                ),
                omf.ImageTexture(
                    name='test image',
                    image=pngfile,
                    origin=[0, 0, 0],
                    axis_u=[1, 0, 0],
                    axis_v=[0, 0, 1]
                )
            ],
            color='green'
        )

        lin = omf.LineSetElement(
            name='Random Line',
            geometry=omf.LineSetGeometry(
                vertices=np.random.rand(100, 3),
                segments=np.floor(np.random.rand(50, 2)*100).astype(int)
            ),
            data=[
                omf.ScalarData(
                    name='rand vert data',
                    array=np.random.rand(100),
                    location='vertices'
                ),
                omf.ScalarData(
                    name='rand segment data',
                    array=np.random.rand(50),
                    location='segments'
                )
            ],
            color='#0000FF'
        )

        surf = omf.SurfaceElement(
            name='trisurf',
            geometry=omf.SurfaceGeometry(
                vertices=np.random.rand(100, 3),
                triangles=np.floor(np.random.rand(50, 3)*100).astype(int)
            ),
            data=[
                omf.ScalarData(
                    name='rand vert data',
                    array=np.random.rand(100),
                    location='vertices'
                ),
                omf.ScalarData(
                    name='rand face data',
                    array=np.random.rand(50),
                    location='faces'
                )
            ],
            color=[100, 200, 200]
        )

        grid = omf.SurfaceElement(
            name='gridsurf',
            geometry=omf.SurfaceGridGeometry(
                tensor_u=np.ones(10).astype(float),
                tensor_v=np.ones(15).astype(float),
                origin=[50., 50., 50.],
                axis_u=[1., 0, 0],
                axis_v=[0, 0, 1.],
                offset_w=np.random.rand(11, 16).flatten()
            ),
            data=[
                omf.ScalarData(
                    name='rand vert data',
                    array=np.random.rand(11, 16).flatten(),
                    location='vertices'
                ),
                omf.ScalarData(
                    name='rand face data',
                    array=np.random.rand(10, 15).flatten(order='f'),
                    location='faces'
                )
            ],
            textures=[
                omf.ImageTexture(
                    name='test image',
                    image=pngfile,
                    origin=[2., 2., 2.],
                    axis_u=[5., 0, 0],
                    axis_v=[0, 2., 5.]
                )
            ]
        )

        vol = omf.VolumeElement(
            name='vol',
            geometry=omf.VolumeGridGeometry(
                tensor_u=np.ones(10).astype(float),
                tensor_v=np.ones(15).astype(float),
                tensor_w=np.ones(20).astype(float),
                origin=[10., 10., -10]
            ),
            data=[
                omf.ScalarData(
                    name='Random Data',
                    location='cells',
                    array=np.random.rand(10, 15, 20).flatten()
                )
            ]
        )

        proj.elements = [pts, lin, surf, grid, vol]

        assert proj.validate()

        serial_file = os.path.sep.join([dirname, 'out.omf'])
        omf.OMFWriter(proj, serial_file)
        reader = omf.OMFReader(serial_file)
        new_proj = reader.get_project()

        assert new_proj.validate()
        assert str(new_proj.elements[3].textures[0].uid) == \
            str(proj.elements[3].textures[0].uid)
        del reader
        os.remove(serial_file)