def test01_create_mesh(variant_scalar_rgb): from mitsuba.render import Mesh m = Mesh("MyMesh", 3, 2) m.vertex_positions_buffer()[:] = [ 0.0, 0.0, 0.0, 1.0, 0.2, 0.0, 0.2, 1.0, 0.0 ] m.faces_buffer()[:] = [0, 1, 2, 1, 2, 0] m.parameters_changed() m.surface_area() # Ensure surface area computed assert str(m) == """Mesh[
def test08_mesh_add_attribute(variant_scalar_rgb): from mitsuba.core import Struct, float_dtype from mitsuba.render import Mesh m = Mesh("MyMesh", 3, 2) m.vertex_positions_buffer()[:] = [0.0, 0.0, 0.0, 1.0, 0.2, 0.0, 0.2, 1.0, 0.0] m.faces_buffer()[:] = [0, 1, 2, 1, 2, 0] m.parameters_changed() m.add_attribute("vertex_color", 3, [0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0]) assert str(m) == """Mesh[
def test04_normal_weighting_scheme(variant_scalar_rgb): from mitsuba.core import Vector3f from mitsuba.render import Mesh import numpy as np """Tests the weighting scheme that is used to compute surface normals.""" m = Mesh("MyMesh", 5, 2, has_vertex_normals=True) vertices = m.vertex_positions_buffer() normals = m.vertex_normals_buffer() a, b = 1.0, 0.5 vertices[:] = [0, 0, 0, -a, 1, 0, a, 1, 0, -b, 0, 1, b, 0, 1] n0 = Vector3f(0.0, 0.0, -1.0) n1 = Vector3f(0.0, 1.0, 0.0) angle_0 = ek.pi / 2.0 angle_1 = ek.acos(3.0 / 5.0) n2 = n0 * angle_0 + n1 * angle_1 n2 /= ek.norm(n2) n = np.vstack([n2, n0, n0, n1, n1]).transpose() m.faces_buffer()[:] = [0, 1, 2, 0, 3, 4] m.recompute_vertex_normals() for i in range(5): assert ek.allclose(normals[i * 3:(i + 1) * 3], n[:, i], 5e-4)
def create_stairs(num_steps): import numpy as np from mitsuba.render import Mesh size_step = 1.0 / num_steps m = Mesh("stairs", 4 * num_steps, 4 * num_steps - 2) params = traverse(m) v = np.zeros((4 * num_steps, 3)) f = np.zeros((4 * num_steps - 2, 3)) for i in range(num_steps): h = i * size_step s1 = i * size_step s2 = (i + 1) * size_step k = 4 * i v[k + 0] = [0.0, s1, h] v[k + 1] = [1.0, s1, h] v[k + 2] = [0.0, s2, h] v[k + 3] = [1.0, s2, h] f[k] = [k, k + 1, k + 2] f[k + 1] = [k + 1, k + 3, k + 2] if i < num_steps - 1: f[k + 2] = [k + 2, k + 3, k + 5] f[k + 3] = [k + 5, k + 4, k + 2] m.vertex_positions_buffer()[:] = v.reshape(-1) m.faces_buffer()[:] = f.reshape(-1) m.recompute_bbox() return m
def test01_create_mesh(variant_scalar_rgb): from mitsuba.core import Struct, float_dtype from mitsuba.render import Mesh vertex_struct = Struct() \ .append("x", Struct.Type.Float32) \ .append("y", Struct.Type.Float32) \ .append("z", Struct.Type.Float32) index_struct = Struct() \ .append("i0", Struct.Type.UInt32) \ .append("i1", Struct.Type.UInt32) \ .append("i2", Struct.Type.UInt32) m = Mesh("MyMesh", vertex_struct, 3, index_struct, 2) v = m.vertices() v[0] = (0.0, 0.0, 0.0) v[1] = (0.0, 0.0, 1.0) v[2] = (0.0, 1.0, 0.0) m.recompute_bbox() if float_dtype == 'f': assert str(m) == """Mesh[ name = "MyMesh", bbox = BoundingBox3f[ min = [0, 0, 0], max = [0, 1, 1] ], vertex_struct = Struct<12>[ float32 x; // @0 float32 y; // @4 float32 z; // @8 ], vertex_count = 3, vertices = [36 B of vertex data], face_struct = Struct<12>[ uint32 i0; // @0 uint32 i1; // @4 uint32 i2; // @8 ], face_count = 2, faces = [24 B of face data], disable_vertex_normals = 0, surface_area = 0 ]""" else: assert str(m) == """Mesh[
def create_single_triangle(): from mitsuba.render import Mesh m = Mesh("tri", 3, 1) m.vertex_positions_buffer()[:] = [ 0.0, 0.0, 0.0, 1.0, 0.2, 0.0, 0.2, 1.0, 0.0 ] m.faces_buffer()[:] = [0, 1, 2] m.recompute_bbox() return m
def create_regular_tetrahedron(): from mitsuba.render import Mesh m = Mesh("tetrahedron", 4, 4) params = traverse(m) m.vertex_positions_buffer()[:] = [ 0, 0, 0, 0.8, 0.8, 0, 0.8, 0, 0.8, 0, 0.8, 0.8 ] m.faces_buffer()[:] = [0, 1, 2, 1, 2, 3, 0, 2, 2, 1, 3, 3, 3, 1, 0] m.recompute_bbox() return m
def create_stairs_packet(num_steps): assert mitsuba.variant() == 'packet_rgb' from mitsuba.render import Mesh size_step = 1.0 / num_steps m = Mesh("stairs", vertex_struct, 4 * num_steps, index_struct, 4 * num_steps - 2) v = m.vertices() f = m.faces() for i in range(num_steps): h = i * size_step s1 = i * size_step s2 = (i + 1) * size_step k = 4 * i v[k + 0] = (0.0, s1, h) v[k + 1] = (1.0, s1, h) v[k + 2] = (0.0, s2, h) v[k + 3] = (1.0, s2, h) f[k] = (k, k + 1, k + 2) f[k + 1] = (k + 1, k + 3, k + 2) if i < num_steps - 1: f[k + 2] = (k + 2, k + 3, k + 5) f[k + 3] = (k + 5, k + 4, k + 2) m.recompute_bbox() return m
def create_single_triangle(): from mitsuba.render import Mesh m = Mesh("tri", vertex_struct, 3, index_struct, 1) v = m.vertices() f = m.faces() v[0] = (0, 0, 0) v[1] = (1, 0.2, 0) v[2] = (0.2, 1, 0) f[0] = (0, 1, 2) m.recompute_bbox() return m
def create_regular_tetrahedron(): from mitsuba.render import Mesh m = Mesh("tetrahedron", vertex_struct, 4, index_struct, 4) v = m.vertices() f = m.faces() v[0] = (0, 0, 0) v[1] = (0.8, 0.8, 0) v[2] = (0.8, 0, 0.8) v[3] = (0, 0.8, 0.8) f[0] = (0, 1, 2) f[1] = (2, 3, 0) f[2] = (2, 1, 3) f[3] = (3, 1, 0) m.recompute_bbox() return m
def test04_normal_weighting_scheme(variant_scalar_rgb): from mitsuba.core import Struct, float_dtype, Vector3f from mitsuba.render import Mesh import numpy as np """Tests the weighting scheme that is used to compute surface normals.""" vertex_struct = Struct() \ .append("x", Struct.Type.Float32) \ .append("y", Struct.Type.Float32) \ .append("z", Struct.Type.Float32) \ .append("nx", Struct.Type.Float32) \ .append("ny", Struct.Type.Float32) \ .append("nz", Struct.Type.Float32) index_struct = Struct() \ .append("i0", Struct.Type.UInt32) \ .append("i1", Struct.Type.UInt32) \ .append("i2", Struct.Type.UInt32) m = Mesh("MyMesh", vertex_struct, 5, index_struct, 2) v = m.vertices() a, b = 1.0, 0.5 v['x'] = Float([0, -a, a, -b, b]) v['y'] = Float([0, 1, 1, 0, 0]) v['z'] = Float([0, 0, 0, 1, 1]) n0 = Vector3f(0.0, 0.0, -1.0) n1 = Vector3f(0.0, 1.0, 0.0) angle_0 = ek.pi / 2.0 angle_1 = ek.acos(3.0 / 5.0) n2 = n0 * angle_0 + n1 * angle_1 n2 /= ek.norm(n2) n = np.vstack([n2, n0, n0, n1, n1]) f = m.faces() f[0] = (0, 1, 2) f[1] = (0, 3, 4) m.recompute_vertex_normals() assert ek.allclose(v['nx'], n[:, 0], 5e-4) assert ek.allclose(v['ny'], n[:, 1], 5e-4) assert ek.allclose(v['nz'], n[:, 2], 5e-4)
def create_rectangle(): from mitsuba.render import Mesh mesh = Mesh("rectangle", 4, 2, has_vertex_texcoords=True) mesh.vertex_positions_buffer()[:] = [ 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0 ] mesh.vertex_texcoords_buffer()[:] = [ 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0 ] mesh.faces_buffer()[:] = [0, 1, 2, 1, 3, 2] mesh.parameters_changed() mesh.add_attribute("vertex_mono", 1, [1.0, 2.0, 3.0, 4.0]) mesh.add_attribute( "vertex_color", 3, [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0]) mesh.add_attribute("face_mono", 1, [0.0, 1.0]) mesh.add_attribute("face_color", 3, [0.0, 0.0, 0.0, 0.0, 0.0, 1.0]) return mesh