Commit 9c3d736b authored by Dario Seyb's avatar Dario Seyb

nicer face normal display

parent f6162ac9
This diff is collapsed.
......@@ -3,7 +3,7 @@ import pythreejs as three
import numpy as np
from IPython.display import display
from .indexed_attribute import *
from .mesh_helper import calculateFaceNormals
from .mesh_helper import calculateFaceNormals, calculatePointNormals
class Context(object):
def __init__(self, width=600, height=400, background_color = '#dddddd'):
......@@ -33,8 +33,13 @@ class Context(object):
face_indices = np.array(face_indices)
mat = None
# Setup Material
mat = three.MeshLambertMaterial(color='#dddddd', colorWrite=shading is not 'hidden')
if shading == 'flat' or shading == 'hidden' or shading == 'wireframe':
mat = three.MeshLambertMaterial(color='#dddddd', colorWrite=shading is not 'hidden')
elif shading == 'phong':
mat = three.MeshPhongMaterial(color='#dddddd')
mat.wireframe = shading is 'wireframe'
if z_offset is not 0:
......@@ -42,7 +47,9 @@ class Context(object):
mat.polygonOffsetFactor = z_offset
mat.polygonOffsetUnits = 0.1
if normals is None:
if normals is 'vertex':
normals = calculatePointNormals(vertices, face_indices)
elif normals is 'face':
normals = calculateFaceNormals(vertices, face_indices)
# Resolve the given attributes, it's ok if they are None
......
......@@ -3,8 +3,21 @@ import numpy as np
def calculateNormalEdges(vertices, face_indices, normals, length):
resolved_normals = resolve_attributes(face_indices, [normals])[0]
vertices, face_indices = stretch_vertices(vertices, face_indices)
resolved_normals = None
if isinstance(normals, FaceAttribute):
resolved_normals = normals.values
face_centers = []
for face in face_indices:
v1 = np.array(vertices[face[0]])
v2 = np.array(vertices[face[1]])
v3 = np.array(vertices[face[2]])
face_centers.append((v1 + v2 + v3)/3)
vertices = face_centers
else:
resolved_normals = resolve_attributes(face_indices, [normals])[0]
vertices, face_indices = stretch_vertices(vertices, face_indices)
edge_vertices = []
edge_indices = []
......@@ -36,7 +49,7 @@ def calculateFaceNormals(vertices, face_indices):
return FaceAttribute(values)
def calculatePointNormals(vertices, face_indices):
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]])
......@@ -47,7 +60,6 @@ def calculatePointNormals(vertices, face_indices):
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)
......
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