Commit 5b5f51cd authored by Dario Seyb's avatar Dario Seyb

fixed point normals for non-tri faces

parent 3ff4f00f
......@@ -19,9 +19,10 @@
"\n",
"mesh = meshvis.Mesh(vertices, faces)\n",
"mesh.colors = meshvis.FaceAttribute(np.random.rand(len(faces), 3))\n",
"mesh.normals = meshvis.calculateFaceNormals(vertices, faces)\n",
"mesh.normals = meshvis.calculatePointNormals(vertices, faces)\n",
"\n",
"normal_vis_verts, normal_vis_edges = meshvis.calculateNormalEdges(vertices, faces, mesh.normals, 1)\n",
"\n",
"normal_vis_verts, normal_vis_edges = meshvis.calculateNormalEdges(vertices, faces, mesh.normals, 0.5)\n",
"\n",
"ctx = meshvis.Context()\n",
"\n",
......
......@@ -19,9 +19,10 @@
"\n",
"mesh = meshvis.Mesh(vertices, faces)\n",
"mesh.colors = meshvis.FaceAttribute(np.random.rand(len(faces), 3))\n",
"mesh.normals = meshvis.calculateFaceNormals(vertices, faces)\n",
"mesh.normals = meshvis.calculatePointNormals(vertices, faces)\n",
"\n",
"normal_vis_verts, normal_vis_edges = meshvis.calculateNormalEdges(vertices, faces, mesh.normals, 1)\n",
"\n",
"normal_vis_verts, normal_vis_edges = meshvis.calculateNormalEdges(vertices, faces, mesh.normals, 0.5)\n",
"\n",
"ctx = meshvis.Context()\n",
"\n",
......
......@@ -5,7 +5,8 @@ def stretch_vertices(vertices, face_indices):
for face in face_indices:
for vidx in face:
stretched_verts.append(vertices[vidx])
if vidx != -1:
stretched_verts.append(vertices[vidx])
return np.array(stretched_verts), np.arange(0, len(stretched_verts))
......@@ -24,11 +25,14 @@ def resolve_attributes(face_indices, attribs):
return final_attributes
def stretch_attribute(face_indices, resolved_attrib):
vs_per_face = len(face_indices[0])
stretched_attrib = [None] * (len(face_indices) * vs_per_face)
face_valence = (face_indices != -1).sum(1)
totalVertexCount = face_valence.sum()
flattened_face_start = np.cumsum(face_valence) - face_valence
stretched_attrib = [None] * totalVertexCount
for val in resolved_attrib:
stretched_attrib[val[0] * vs_per_face + val[1]] = val[2]
stretched_attrib[ flattened_face_start[val[0]] + val[1]] = val[2]
return np.array(stretched_attrib)
......
......@@ -10,13 +10,13 @@ def calculateNormalEdges(vertices, face_indices, normals, length):
resolved_normals = normals.values
face_centers = []
for face in face_indices:
face_vertices = vertices[face]
face_centers.append(np.average(face_vertices, axis=0, weights=face!=-1))
face_centers.append(np.average(vertices[face], axis=0, weights=face!=-1))
vertices = face_centers
else:
resolved_normals = resolve_attributes(face_indices, [normals])[0]
vertices, face_indices = stretch_vertices(vertices, face_indices)
edge_vertices = []
edge_indices = []
for i, vert in enumerate(vertices):
......@@ -34,6 +34,7 @@ def calculateNormalEdges(vertices, face_indices, normals, length):
def calculateFaceNormals(vertices, face_indices):
values = []
for face in face_indices:
# Assuming face is planar
v1 = np.array(vertices[face[0]])
v2 = np.array(vertices[face[1]])
v3 = np.array(vertices[face[2]])
......@@ -52,6 +53,7 @@ def calculatePointNormals(vertices, face_indices):
counts = np.zeros(vertices.shape[0])
for face in face_indices:
# Assuming face is planar
v1 = np.array(vertices[face[0]])
v2 = np.array(vertices[face[1]])
v3 = np.array(vertices[face[2]])
......@@ -61,14 +63,9 @@ def calculatePointNormals(vertices, face_indices):
norm = np.cross(v12, v13)
normals[face[0]] += norm
normals[face[1]] += norm
normals[face[2]] += norm
counts[face[0]] += 1
counts[face[1]] += 1
counts[face[2]] += 1
filtered_face = face[np.where(face != -1)]
normals[filtered_face] += norm
counts[filtered_face] += 1
normals /= counts.reshape(-1, 1)
normals /= np.linalg.norm(normals, axis=1).reshape(-1, 1)
......
Markdown is supported
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