Commit 3c0a0d7a authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

Unit test for face/vertex normal, and fix winding logic in TriangleMesh class.

parent 949c92a8
......@@ -13,6 +13,7 @@ import tempfile
import numpy as np
import pytest
import fsl.utils.transform as transform
import fsl.data.mesh as fslmesh
......@@ -131,3 +132,67 @@ def test_findReferenceImage():
finally:
shutil.rmtree(testdir)
def test_normals():
# vertices of a cube
verts = np.array([
[-1, -1, -1],
[-1, -1, 1],
[-1, 1, -1],
[-1, 1, 1],
[ 1, -1, -1],
[ 1, -1, 1],
[ 1, 1, -1],
[ 1, 1, 1],
])
# triangles
# cw == clockwise, when facing outwards
# from the centre of the mesh
triangles_cw = np.array([
[0, 4, 6], [0, 6, 2],
[1, 3, 5], [3, 7, 5],
[0, 1, 4], [1, 5, 4],
[2, 6, 7], [2, 7, 3],
[0, 2, 1], [1, 2, 3],
[4, 5, 7], [4, 7, 6],
])
# ccw == counter-clockwise
triangles_ccw = np.array(triangles_cw)
triangles_ccw[:, [1, 2]] = triangles_ccw[:, [2, 1]]
# face normals
fnormals = np.array([
[ 0, 0, -1], [ 0, 0, -1],
[ 0, 0, 1], [ 0, 0, 1],
[ 0, -1, 0], [ 0, -1, 0],
[ 0, 1, 0], [ 0, 1, 0],
[-1, 0, 0], [-1, 0, 0],
[ 1, 0, 0], [ 1, 0, 0],
])
# vertex normals
vnormals = np.zeros((8, 3))
for i in range(8):
faces = np.where(triangles_cw == i)[0]
vnormals[i] = fnormals[faces].sum(axis=0)
vnormals = transform.normalise(vnormals)
cw_nofix = fslmesh.TriangleMesh(verts, triangles_cw)
cw_fix = fslmesh.TriangleMesh(verts, triangles_cw, fixWinding=True)
ccw_nofix = fslmesh.TriangleMesh(verts, triangles_ccw)
ccw_fix = fslmesh.TriangleMesh(verts, triangles_ccw, fixWinding=True)
# ccw triangles should give correct
# normals without unwinding
assert np.all(np.isclose(cw_nofix .normals, -fnormals))
assert np.all(np.isclose(cw_nofix .vnormals, -vnormals))
assert np.all(np.isclose(cw_fix .normals, fnormals))
assert np.all(np.isclose(cw_fix .vnormals, vnormals))
assert np.all(np.isclose(ccw_nofix.normals, fnormals))
assert np.all(np.isclose(ccw_nofix.vnormals, vnormals))
assert np.all(np.isclose(ccw_fix .normals, fnormals))
assert np.all(np.isclose(ccw_fix .vnormals, vnormals))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment