def to_omf(self):
     self.validate()
     omf_grid_surface = omf.SurfaceElement(
         name=self.name or '',
         description=self.description or '',
         geometry=omf.SurfaceGridGeometry(
             origin=self.origin,
             tensor_u=self.tensor_u,
             tensor_v=self.tensor_v,
             axis_u=self.axis_u,
             axis_v=self.axis_v,
         ),
         data=[
             attr.to_omf(cell_location='faces') for attr in self.data
             if not isinstance(attr, TextureProjection)
         ],
         textures=[
             tex.to_omf() for tex in self.data
             if isinstance(tex, TextureProjection)
         ],
         color=self.defaults.color.value,
     )
     if self.offset_w is not None:
         omf_grid_surface.offset_w = self.offset_w.array
     return omf_grid_surface
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)
Exemple #3
0
 def _to_omf(self):
     import omf
     geometry = omf.SurfaceGridGeometry(
         tensor_u=self.h1,
         tensor_v=self.h2,
         axis_u=self.U,
         axis_v=self.V,
         origin=self.O,
         offset_w=omf.ScalarArray(self.Z, )
         if self.Z is not None else properties.undefined,
     )
     return geometry
def test_data():
    arr = [float(val) for val in range(25)]
    arr_int = [int(val % 4) for val in range(25)]

    proj = omf.Project()

    surf = omf.SurfaceElement(
        geometry=omf.SurfaceGridGeometry(
            tensor_u=[1., 1., 1., 1., 1.],
            tensor_v=[1., 1., 1., 1., 1.],
        ),
        data=[
            omf.ScalarData(name='my data',
                           description='my desc',
                           location='faces',
                           array=arr,
                           colormap=omf.ScalarColormap(
                               gradient=[
                                   'red',
                                   'blue',
                                   'black',
                                   'orange',
                               ] * 32,
                               limits=[min(arr), max(arr)],
                           )),
            omf.MappedData(
                name='my data',
                description='my desc',
                location='faces',
                array=arr_int,
                legends=[
                    omf.Legend(values=[
                        'yellow',
                        'black',
                        'brown',
                        'green',
                    ], ),
                    omf.Legend(values=[
                        'yellow!',
                        'black!!',
                        'brown!!!',
                        'green!!!!',
                    ], ),
                ],
            )
        ],
    )
    proj.elements = [surf]
    proj.validate()
    view = get_view_from_proj(proj)
    view.validate()
    assert len(view.contents) == 9
def test_surfaces():
    proj = omf.Project(origin=[5., 5, 5])
    surf_0 = omf.SurfaceElement(
        name='my elem',
        description='my desc',
        geometry=omf.SurfaceGeometry(
            vertices=[[i / 3, i / 4, i / 5] for i in range(10)],
            triangles=[[i, i + 1, i + 2] for i in range(8)],
            origin=[5., 5, 5],
        ),
        color='red',
    )
    surf_1 = omf.SurfaceElement(
        name='my elem',
        description='my desc',
        geometry=omf.SurfaceGridGeometry(
            tensor_u=[1., 1., 1., 1., 1.],
            tensor_v=[1., 1., 1., 1., 1.],
            offset_w=[1.] * 36,
            origin=[5., 5, 5],
        ),
        color='red',
    )
    proj.elements = [surf_0, surf_1]
    proj.validate()
    view = get_view_from_proj(proj)
    view.validate()
    assert len(view.contents) == 5
    for cls in (spatial.ElementSurface, spatial.ElementSurfaceGrid):
        surface = find(view, cls)
        defaults = surface.defaults.serialize()
        assert defaults['__class__'] == 'OptionsSurface'
        assert defaults['color']['value'] == '#FF0000'
        assert surface.name == 'my elem'
        assert surface.description == 'my desc'
    assert list(find(
        view, spatial.ElementSurface).vertices.array[0]) == [10., 10, 10]
    assert list(find(view, spatial.ElementSurfaceGrid).origin) == [10., 10, 10]
Exemple #6
0
                                     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='test_image.png',
Exemple #7
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)