Commit f6162ac9 authored by Dario Seyb's avatar Dario Seyb

added point normal calculation

parent 07b82258
This diff is collapsed.
......@@ -8,3 +8,4 @@ except ImportError as error:
from .context import *
from .immediate import *
from .indexed_attribute import UniformAttribute, PointAttribute, FaceAttribute, HalfEdgeAttribute
from .mesh_helper import *
from .indexed_attribute import FaceAttribute
from .indexed_attribute import FaceAttribute, PointAttribute, resolve_attributes, stretch_vertices
import numpy as np
def cross(a, b):
c = [a[1]*b[2] - a[2]*b[1],
a[2]*b[0] - a[0]*b[2],
a[0]*b[1] - a[1]*b[0]]
return c
def calculateNormalEdges(vertices, face_indices, normals, length):
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):
norm = np.array(resolved_normals[i])
vert = np.array(vert)
edge_vertices.append(vert)
edge_vertices.append(np.add(norm*length, vert))
edge_indices.append([i*2, i*2 + 1])
return edge_vertices, edge_indices
def calculateFaceNormals(vertices, face_indices):
values = []
......@@ -22,4 +34,30 @@ def calculateFaceNormals(vertices, face_indices):
norm = norm / np.linalg.norm(norm)
values.append(norm)
return FaceAttribute(values)
\ No newline at end of file
return FaceAttribute(values)
def calculatePointNormals(vertices, face_indices):
values = [(np.array([0.0, 0.0, 0.0]), 0)] * len(vertices)
for face in face_indices:
v1 = np.array(vertices[face[0]])
v2 = np.array(vertices[face[1]])
v3 = np.array(vertices[face[2]])
v12 = np.subtract(v2, v1)
v13 = np.subtract(v3, v1)
norm = np.cross(v12, v13)
values[face[0]] = ( values[face[0]][0] + norm, values[face[0]][1] + 1)
values[face[1]] = ( values[face[1]][0] + norm, values[face[1]][1] + 1)
values[face[2]] = ( values[face[2]][0] + norm, values[face[2]][1] + 1)
result = []
for val in values:
  • Try to avoid iterating over numpy arrays if possible. It's faster to work on them directly.

    I would suggest splitting values into two arrays:

    normals = np.zeros_like(vertices)
    counts = np.zeros(vertices.shape[0])

    and then you can do something similar to this:

    normals /= counts.reshape(-1,1)
    normals /= np.linalg.norm(normals, axis=1).reshape(-1,1)
    return PointAttribute(normals)
Please register or sign in to reply
norm = val[0]/val[1]
norm = norm / np.linalg.norm(norm)
result.append(norm)
return PointAttribute(result)
\ No newline at end of file
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