1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
# -*- coding: utf-8 -*-
import numpy as np
import pytest
from vispy import scene
from vispy.geometry import create_sphere
from vispy.testing import (TestingCanvas, requires_application,
run_tests_if_main, requires_pyopengl)
@requires_pyopengl()
@requires_application()
def test_mesh_normals():
size = (45, 40)
with TestingCanvas(size=size, bgcolor="k") as c:
v = c.central_widget.add_view(border_width=0)
v.camera = 'arcball'
# Create visual.
mdata = create_sphere(radius=1.0)
mesh = scene.visuals.Mesh(meshdata=mdata,
shading=None,
color=(0.1, 0.1, 0.1, 1.0))
v.add(mesh)
rendered_without_normals = c.render()
# The color should be of low intensity.
assert np.all((rendered_without_normals[..., 0:3]) < 32)
face_normals = scene.visuals.MeshNormals(mdata, primitive="face",
color=(1, 0, 0))
face_normals.parent = mesh
rendered_with_face_normals = c.render()
face_normals.parent = None
# There should be some pixels with brighter red.
assert np.sum(rendered_with_face_normals[..., 0] > 128) > 64
pytest.raises(AssertionError, np.testing.assert_allclose,
rendered_without_normals, rendered_with_face_normals)
vertex_normals = scene.visuals.MeshNormals(mdata, primitive="vertex",
color=(0, 1, 0))
vertex_normals.parent = mesh
rendered_with_vertex_normals = c.render()
vertex_normals.parent = None
# There should be some pixels with brighter green.
assert np.sum(rendered_with_vertex_normals[..., 1] > 128) > 64
pytest.raises(AssertionError, np.testing.assert_allclose,
rendered_without_normals, rendered_with_vertex_normals)
pytest.raises(AssertionError, np.testing.assert_allclose,
rendered_with_face_normals, rendered_with_vertex_normals)
@requires_pyopengl()
@requires_application()
def test_mesh_normals_length_scalar():
size = (45, 40)
with TestingCanvas(size=size, bgcolor="k") as c:
v = c.central_widget.add_view(border_width=0)
v.camera = 'arcball'
# Create visual.
mdata = create_sphere(radius=1.0)
mesh = scene.visuals.Mesh(meshdata=mdata,
shading=None,
color=(0.1, 0.1, 0.1, 1.0))
v.add(mesh)
length = 0.5
normals_0_5 = scene.visuals.MeshNormals(mdata, color=(1, 0, 0),
length=length)
normals_0_5.parent = mesh
rendered_length_0_5 = c.render()
normals_0_5.parent = None
length = 1.0
normals_1_0 = scene.visuals.MeshNormals(mdata, color=(1, 0, 0),
length=length)
normals_1_0.parent = mesh
rendered_length_1_0 = c.render()
normals_1_0.parent = None
# There should be more red pixels with the longer normals.
n_pixels_0_5 = np.sum(rendered_length_0_5[..., 0] > 128)
n_pixels_1_0 = np.sum(rendered_length_1_0[..., 0] > 128)
assert n_pixels_1_0 > n_pixels_0_5
@requires_pyopengl()
@requires_application()
@pytest.mark.parametrize('primitive', ['face', 'vertex'])
def test_mesh_normals_length_array(primitive):
size = (45, 40)
with TestingCanvas(size=size, bgcolor="k") as c:
v = c.central_widget.add_view(border_width=0)
v.camera = 'arcball'
v.camera.fov = 90
# Create visual.
meshdata = create_sphere(radius=1.0)
mesh = scene.visuals.Mesh(meshdata=meshdata,
shading=None,
color=(0.1, 0.1, 0.1, 1.0))
v.add(mesh)
if primitive == 'face':
n_normals = len(meshdata.get_faces())
elif primitive == 'vertex':
n_normals = len(meshdata.get_vertices())
lengths_0_5 = np.full(n_normals, 0.5, dtype=float)
normals_0_5 = scene.visuals.MeshNormals(meshdata, primitive=primitive,
color=(1, 0, 0),
length=lengths_0_5)
normals_0_5.parent = mesh
rendered_lengths_0_5 = c.render()
normals_0_5.parent = None
lengths_1_0 = np.full(n_normals, 1.0, dtype=float)
normals_1_0 = scene.visuals.MeshNormals(meshdata, primitive=primitive,
color=(1, 0, 0),
length=lengths_1_0)
normals_1_0.parent = mesh
rendered_lengths_1_0 = c.render()
normals_1_0.parent = None
# There should be more red pixels with the longer normals.
n_pixels_0_5 = np.sum(rendered_lengths_0_5[..., 0] > 128)
n_pixels_1_0 = np.sum(rendered_lengths_1_0[..., 0] > 128)
assert n_pixels_1_0 > n_pixels_0_5
lengths_ramp = np.linspace(0.5, 1.0, n_normals, dtype=float)
normals_ramp = scene.visuals.MeshNormals(meshdata, primitive=primitive,
color=(1, 0, 0),
length=lengths_ramp)
normals_ramp.parent = mesh
rendered_lengths_ramp = c.render()
normals_ramp.parent = None
# With the normals lengths from a ramp in [0.5, 1.0], there should be
# more red pixels than with all normals of length 0.5 and less than
# with all normals of length 1.0.
n_pixels_ramp = np.sum(rendered_lengths_ramp[..., 0] > 128)
assert n_pixels_0_5 < n_pixels_ramp < n_pixels_1_0
@requires_pyopengl()
@requires_application()
def test_mesh_normals_length_scale():
size = (45, 40)
with TestingCanvas(size=size, bgcolor="k") as c:
v = c.central_widget.add_view(border_width=0)
v.camera = 'arcball'
# Create visual.
meshdata = create_sphere(radius=1.0)
mesh = scene.visuals.Mesh(meshdata=meshdata,
shading=None,
color=(0.1, 0.1, 0.1, 1.0))
v.add(mesh)
length = 1.0
length_scale_up = 2.0
length_scale_down = 0.5
normals = scene.visuals.MeshNormals(meshdata, color=(1, 0, 0),
length=length)
normals.parent = mesh
rendered_length_default = c.render()
normals.parent = None
normals_scaled_up = scene.visuals.MeshNormals(
meshdata, color=(1, 0, 0), length=length,
length_scale=length_scale_up)
normals_scaled_up.parent = mesh
rendered_length_scaled_up = c.render()
normals_scaled_up.parent = None
normals_scaled_down = scene.visuals.MeshNormals(
meshdata, color=(1, 0, 0), length=length,
length_scale=length_scale_down)
normals_scaled_down.parent = mesh
rendered_length_scaled_down = c.render()
normals_scaled_down.parent = None
# There should be more red pixels with the scaled-up normals and less
# with the ones scaled down.
n_pixels_default = np.sum(rendered_length_default[..., 0] > 128)
n_pixels_scaled_up = np.sum(rendered_length_scaled_up[..., 0] > 128)
n_pixels_scaled_down = np.sum(rendered_length_scaled_down[..., 0] > 128)
assert n_pixels_scaled_down < n_pixels_default < n_pixels_scaled_up
@requires_pyopengl()
@requires_application()
@pytest.mark.parametrize('length_method', ['median_edge', 'max_extent'])
def test_mesh_normals_length_method(length_method):
size = (45, 40)
with TestingCanvas(size=size, bgcolor="k") as c:
v = c.central_widget.add_view(border_width=0)
v.camera = 'arcball'
# Create visual.
meshdata = create_sphere(radius=1.0)
mesh = scene.visuals.Mesh(meshdata=meshdata,
shading=None,
color=(0.1, 0.1, 0.1, 1.0))
v.add(mesh)
# The code below should not raise.
# XXX(asnt): Not sure how to better test `length_method`.
normals = scene.visuals.MeshNormals(meshdata, color=(1, 0, 0),
length_method=length_method)
normals.parent = mesh
_ = c.render()
def test_mesh_normals_empty():
mesh = scene.visuals.Mesh()
scene.visuals.MeshNormals(mesh.mesh_data)
run_tests_if_main()
|