Commit 809bfbc8 authored by Dario Seyb's avatar Dario Seyb

automatically computing face normals when they are not provided

parent 3b9dd711
......@@ -2,29 +2,50 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"scrolled": false
"scrolled": true
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n",
"[]\n"
]
},
{
"ename": "AssertionError",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-1-8563f6e01c24>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 11\u001b[0m ctx.draw_faces(m.points(), m.face_vertex_indices(), \n\u001b[0;32m 12\u001b[0m \u001b[1;31m#normals = meshvis.FaceAttribute(m.face_normals()),\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 13\u001b[1;33m colors = meshvis.UniformAttribute([1, 0, 1]))\n\u001b[0m\u001b[0;32m 14\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[0mctx\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdraw_edges\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mm\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpoints\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mm\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mev_indices\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\dev\\meshvis\\meshvis\\context.pyc\u001b[0m in \u001b[0;36mdraw_faces\u001b[1;34m(self, vertices, face_indices, normals, colors, uvs, shading, z_offset)\u001b[0m\n\u001b[0;32m 30\u001b[0m shading='flat', z_offset=0.5): \n\u001b[0;32m 31\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 32\u001b[1;33m \u001b[1;32massert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mface_indices\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m0\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvertices\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 33\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 34\u001b[0m \u001b[0mface_indices\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mface_indices\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mAssertionError\u001b[0m: "
]
}
],
"source": [
"import meshvis\n",
"import openmesh as om\n",
"import numpy as np\n",
"m = om.TriMesh()\n",
"om.read_mesh(m, \"C:\\Users\\dario\\OneDrive\\Dokumente\\ANIMVR\\Import\\Models\\Mickey_Mouse.obj\")\n",
"print om.read_mesh(m, \"C:/dev/openmesh-python/tests/TestFiles/cube-minimal-vertex-colors-after-vertex-definition.obj\")\n",
"\n",
"ctx = meshvis.Context(width=640, height=480)\n",
"\n",
"m.face_colors()\n",
"print m.points()\n",
"\n",
"ctx.draw_faces(m.points(), m.face_vertex_indices(), \n",
" normals = meshvis.FaceAttribute(m.face_normals()),\n",
" colors = meshvis.PointAttribute(np.random.rand(len(m.points()),3)))\n",
" #normals = meshvis.FaceAttribute(m.face_normals()),\n",
" colors = meshvis.UniformAttribute([1, 0, 1]))\n",
"\n",
"ctx.draw_edges(m.points(), m.ev_indices())\n",
"\n",
"ctx.draw_vertices(m.points(), point_size=5, colors='red')\n",
"ctx.draw_vertices(m.points(), point_size=2, colors='red')\n",
"\n",
"ctx.display()"
]
......@@ -50,7 +71,7 @@
"\n",
"meshvis.display_edges([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]], \n",
" [[0, 1], [2,3]], \n",
" colors = vcolors)"
" colors = meshvis.FaceAttribute(fcolors))"
]
},
{
......@@ -94,7 +115,7 @@
"\n",
"ctx = meshvis.Context(width=300, height=300)\n",
"\n",
"ctx.draw_faces(verts, indices, per_vertex_colors=vcolors)\n",
"ctx.draw_faces(verts, indices, colors= meshvis.FaceAttribute(fcolors))\n",
"\n",
"ctx.draw_vertices(verts, point_size=5, colors=vcolors)\n",
"\n",
......
This diff is collapsed.
......@@ -3,15 +3,16 @@ import pythreejs as three
import numpy as np
from IPython.display import display
from .indexed_attribute import *
from .mesh_helper import calculateFaceNormals
class Context(object):
def __init__(self, width=600, height=400, background_color = '#dddddd'):
self.camera = three.PerspectiveCamera(position=[3,3,3], fov=20,
children=[three.DirectionalLight(color='#ffffff', position=[-3, 5, 1], intensity=0.5)])
children=[three.DirectionalLight(color='#ffffff', position=[-30, 50, 10], intensity=1.0)])
self.camera.aspect = width/float(height)
self.scene = three.Scene(children=[three.AmbientLight(color='#dddddd')])
self.scene = three.Scene(children=[three.AmbientLight(color='#aaaaaa'), self.camera])
self.scene.background = background_color
self.orbit_controls = three.OrbitControls(controlling=self.camera)
......@@ -19,9 +20,17 @@ class Context(object):
scene=self.scene, controls=[self.orbit_controls],
width=width, height=height, antialias=True )
def draw_sphere(self):
mesh_obj = three.Mesh(three.SphereBufferGeometry(20, 16, 16),
three.MeshPhysicalMaterial(color='red'),
position=[0, 0, 0])
self.scene.add(mesh_obj)
def draw_faces(self, vertices, face_indices, normals=None, colors=None, uvs=None,
shading='flat', z_offset=0.5):
assert(len(face_indices) > 0 and len(vertices) > 0)
face_indices = np.array(face_indices)
# Setup Material
......@@ -33,6 +42,9 @@ class Context(object):
mat.polygonOffsetFactor = z_offset
mat.polygonOffsetUnits = 0.1
if normals is None:
normals = calculateFaceNormals(vertices, face_indices)
# Resolve the given attributes, it's ok if they are None
resolved_attribs = resolve_attributes(face_indices, [normals, colors, uvs])
resolved_normals = resolved_attribs[0]
......
from .indexed_attribute import FaceAttribute
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 calculateFaceNormals(vertices, face_indices):
values = []
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)
norm = norm / np.linalg.norm(norm)
values.append(norm)
return FaceAttribute(values)
\ 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