Commit f6162ac9 authored by Dario Seyb's avatar Dario Seyb

added point normal calculation

parent 07b82258
......@@ -2,225 +2,22 @@
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 1,
"metadata": {
"scrolled": true
"scrolled": false
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4cceae7b818f43cb9a696b241235f8a3",
"version_major": 2,
"version_minor": 0
},
"text/html": [
"<p>Failed to display Jupyter Widget of type <code>Renderer</code>.</p>\n",
"<p>\n",
" If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n",
" that the widgets JavaScript is still loading. If this message persists, it\n",
" likely means that the widgets JavaScript library is either not installed or\n",
" not enabled. See the <a href=\"https://ipywidgets.readthedocs.io/en/stable/user_install.html\">Jupyter\n",
" Widgets Documentation</a> for setup instructions.\n",
"</p>\n",
"<p>\n",
" If you're reading this message in another frontend (for example, a static\n",
" rendering on GitHub or <a href=\"https://nbviewer.jupyter.org/\">NBViewer</a>),\n",
" it may mean that your frontend doesn't currently support widgets.\n",
"</p>\n"
],
"text/plain": [
"Renderer(background='#dddddd', camera=PerspectiveCamera(aspect=1.3333333333333333, children=(DirectionalLight(position=(-30.0, 50.0, 10.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)),), fov=20.0, position=(3.0, 3.0, 3.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), controls=[OrbitControls(controlling=PerspectiveCamera(aspect=1.3333333333333333, children=(DirectionalLight(position=(-30.0, 50.0, 10.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)),), fov=20.0, position=(3.0, 3.0, 3.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)))], scene=Scene(background=u'#dddddd', children=(AmbientLight(color=u'#aaaaaa', quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), PerspectiveCamera(aspect=1.3333333333333333, children=(DirectionalLight(position=(-30.0, 50.0, 10.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)),), fov=20.0, position=(3.0, 3.0, 3.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), Mesh(geometry=BufferGeometry(attributes={'color': BufferAttribute(array=array([[0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.]], dtype=float32)), 'index': BufferAttribute(array=array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n",
" 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,\n",
" 34, 35], dtype=uint16), normalized=False), 'normal': BufferAttribute(array=array([[ 0., 0., -1.],\n",
" [ 0., 0., -1.],\n",
" [ 0., 0., -1.],\n",
" [ 0., 0., -1.],\n",
" [ 0., 0., -1.],\n",
" [ 0., 0., -1.],\n",
" [-1., 0., 0.],\n",
" [-1., 0., 0.],\n",
" [-1., 0., 0.],\n",
" [-1., 0., 0.],\n",
" [-1., 0., 0.],\n",
" [-1., 0., 0.],\n",
" [ 0., 1., 0.],\n",
" [ 0., 1., 0.],\n",
" [ 0., 1., 0.],\n",
" [ 0., 1., 0.],\n",
" [ 0., 1., 0.],\n",
" [ 0., 1., 0.],\n",
" [ 1., 0., 0.],\n",
" [ 1., 0., 0.],\n",
" [ 1., 0., 0.],\n",
" [ 1., 0., 0.],\n",
" [ 1., 0., 0.],\n",
" [ 1., 0., 0.],\n",
" [ 0., -1., 0.],\n",
" [ 0., -1., 0.],\n",
" [ 0., -1., 0.],\n",
" [ 0., -1., 0.],\n",
" [ 0., -1., 0.],\n",
" [ 0., -1., 0.],\n",
" [ 0., 0., 1.],\n",
" [ 0., 0., 1.],\n",
" [ 0., 0., 1.],\n",
" [ 0., 0., 1.],\n",
" [ 0., 0., 1.],\n",
" [ 0., 0., 1.]], dtype=float32)), 'position': BufferAttribute(array=array([[0., 0., 0.],\n",
" [1., 1., 0.],\n",
" [1., 0., 0.],\n",
" [0., 0., 0.],\n",
" [0., 1., 0.],\n",
" [1., 1., 0.],\n",
" [0., 0., 0.],\n",
" [0., 1., 1.],\n",
" [0., 1., 0.],\n",
" [0., 0., 0.],\n",
" [0., 0., 1.],\n",
" [0., 1., 1.],\n",
" [0., 1., 0.],\n",
" [1., 1., 1.],\n",
" [1., 1., 0.],\n",
" [0., 1., 0.],\n",
" [0., 1., 1.],\n",
" [1., 1., 1.],\n",
" [1., 0., 0.],\n",
" [1., 1., 0.],\n",
" [1., 1., 1.],\n",
" [1., 0., 0.],\n",
" [1., 1., 1.],\n",
" [1., 0., 1.],\n",
" [0., 0., 0.],\n",
" [1., 0., 0.],\n",
" [1., 0., 1.],\n",
" [0., 0., 0.],\n",
" [1., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [1., 0., 1.],\n",
" [1., 1., 1.],\n",
" [0., 0., 1.],\n",
" [1., 1., 1.],\n",
" [0., 1., 1.]], dtype=float32), normalized=False)}), material=MeshLambertMaterial(alphaMap=None, aoMap=None, color=u'white', emissiveMap=None, envMap=None, lightMap=None, map=None, polygonOffset=True, polygonOffsetFactor=0.5, polygonOffsetUnits=0.1, specularMap=None, vertexColors='VertexColors'), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), LineSegments(geometry=BufferGeometry(attributes={'color': BufferAttribute(array=array([[0.5949737 , 0.4317956 , 0.13914175],\n",
" [0.5949737 , 0.4317956 , 0.13914175],\n",
" [0.59724146, 0.4947955 , 0.18033886],\n",
" [0.59724146, 0.4947955 , 0.18033886],\n",
" [0.98003805, 0.3224531 , 0.8646538 ],\n",
" [0.98003805, 0.3224531 , 0.8646538 ],\n",
" [0.99827355, 0.75191396, 0.08575644],\n",
" [0.99827355, 0.75191396, 0.08575644],\n",
" [0.89789945, 0.7949212 , 0.3202241 ],\n",
" [0.89789945, 0.7949212 , 0.3202241 ],\n",
" [0.8110034 , 0.7460238 , 0.17039952],\n",
" [0.8110034 , 0.7460238 , 0.17039952],\n",
" [0.54435354, 0.17887402, 0.4859149 ],\n",
" [0.54435354, 0.17887402, 0.4859149 ],\n",
" [0.93637073, 0.816555 , 0.2179866 ],\n",
" [0.93637073, 0.816555 , 0.2179866 ],\n",
" [0.3925988 , 0.31896043, 0.6568851 ],\n",
" [0.3925988 , 0.31896043, 0.6568851 ],\n",
" [0.0771804 , 0.15173675, 0.09374394],\n",
" [0.0771804 , 0.15173675, 0.09374394],\n",
" [0.02774016, 0.06720248, 0.5104477 ],\n",
" [0.02774016, 0.06720248, 0.5104477 ],\n",
" [0.8880325 , 0.12066253, 0.20826603],\n",
" [0.8880325 , 0.12066253, 0.20826603],\n",
" [0.6779794 , 0.70399 , 0.8883923 ],\n",
" [0.6779794 , 0.70399 , 0.8883923 ],\n",
" [0.32955176, 0.44520178, 0.6428352 ],\n",
" [0.32955176, 0.44520178, 0.6428352 ],\n",
" [0.42922875, 0.06898169, 0.04839262],\n",
" [0.42922875, 0.06898169, 0.04839262],\n",
" [0.62022823, 0.59997374, 0.47011045],\n",
" [0.62022823, 0.59997374, 0.47011045],\n",
" [0.4546548 , 0.49446616, 0.30896202],\n",
" [0.4546548 , 0.49446616, 0.30896202],\n",
" [0.5437848 , 0.2812152 , 0.59819496],\n",
" [0.5437848 , 0.2812152 , 0.59819496]], dtype=float32)), 'index': BufferAttribute(array=array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n",
" 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,\n",
" 34, 35], dtype=uint16), normalized=False), 'position': BufferAttribute(array=array([[0., 0., 0.],\n",
" [1., 1., 0.],\n",
" [1., 1., 0.],\n",
" [1., 0., 0.],\n",
" [1., 0., 0.],\n",
" [0., 0., 0.],\n",
" [0., 0., 0.],\n",
" [0., 1., 0.],\n",
" [0., 1., 0.],\n",
" [1., 1., 0.],\n",
" [0., 0., 0.],\n",
" [0., 1., 1.],\n",
" [0., 1., 1.],\n",
" [0., 1., 0.],\n",
" [0., 0., 0.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 1., 1.],\n",
" [0., 1., 0.],\n",
" [1., 1., 1.],\n",
" [1., 1., 1.],\n",
" [1., 1., 0.],\n",
" [0., 1., 1.],\n",
" [1., 1., 1.],\n",
" [1., 1., 1.],\n",
" [1., 0., 0.],\n",
" [1., 1., 1.],\n",
" [1., 0., 1.],\n",
" [1., 0., 1.],\n",
" [1., 0., 0.],\n",
" [1., 0., 1.],\n",
" [0., 0., 0.],\n",
" [1., 0., 1.],\n",
" [0., 0., 1.],\n",
" [1., 1., 1.],\n",
" [0., 0., 1.]], dtype=float32), normalized=False)}), material=LineBasicMaterial(color=u'white', linewidth=3.0, vertexColors='VertexColors'), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), Points(geometry=BufferGeometry(attributes={'position': BufferAttribute(array=array([[0., 0., 0.],\n",
" [0., 0., 1.],\n",
" [0., 1., 0.],\n",
" [0., 1., 1.],\n",
" [1., 0., 0.],\n",
" [1., 0., 1.],\n",
" [1., 1., 0.],\n",
" [1., 1., 1.]], dtype=float32), normalized=False)}), material=PointsMaterial(color=u'red', map=None, size=4.0, sizeAttenuation=False), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0))), fog=None, overrideMaterial=None, quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), shadowMap=WebGLShadowMap())"
]
},
"metadata": {},
"output_type": "display_data"
"ename": "TypeError",
"evalue": "Cannot cast ufunc add output from dtype('float64') to dtype('int32') with casting rule 'same_kind'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-1-93ec749cebc3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[0mctx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmeshvis\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mContext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mwidth\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m640\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mheight\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m480\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[0mnormals\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmeshvis\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcalculatePointNormals\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvertices\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[0mface_indices\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mm\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mface_vertex_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[0m\u001b[0;32m 10\u001b[0m ctx.draw_faces(m.points(), m.face_vertex_indices(), \n\u001b[0;32m 11\u001b[0m \u001b[0mnormals\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnormals\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\dev\\meshvis\\meshvis\\mesh_helper.pyc\u001b[0m in \u001b[0;36mcalculatePointNormals\u001b[1;34m(vertices, face_indices)\u001b[0m\n\u001b[0;32m 50\u001b[0m \u001b[0mnorm\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnorm\u001b[0m \u001b[1;33m/\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlinalg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnorm\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnorm\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 51\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 52\u001b[1;33m \u001b[0mvalues\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mface\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\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[0mnorm\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 53\u001b[0m \u001b[0mvalues\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mface\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 54\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mTypeError\u001b[0m: Cannot cast ufunc add output from dtype('float64') to dtype('int32') with casting rule 'same_kind'"
]
}
],
"source": [
......@@ -232,14 +29,21 @@
"\n",
"ctx = meshvis.Context(width=640, height=480)\n",
"\n",
"normals = meshvis.calculatePointNormals(vertices=m.points(), face_indices=m.face_vertex_indices())\n",
"ctx.draw_faces(m.points(), m.face_vertex_indices(), \n",
" #normals = meshvis.FaceAttribute(m.face_normals()),\n",
" normals = normals,\n",
" colors = meshvis.UniformAttribute([0, 0, 1]))\n",
"\n",
"ctx.draw_edges(m.points(), m.ev_indices(), \n",
" colors = meshvis.FaceAttribute(np.random.rand(len(m.ev_indices()), 3)),\n",
" linewidth=3)\n",
"\n",
"normal_vis_verts, normal_vis_edges = meshvis.calculateNormalEdges(m.points(), m.face_vertex_indices(), normals, 0.1)\n",
"\n",
"ctx.draw_edges(normal_vis_verts, normal_vis_edges)\n",
"\n",
"\n",
"\n",
"ctx.draw_vertices(m.points(), point_size=4, colors='red')\n",
"\n",
"ctx.display()"
......
......@@ -2,225 +2,22 @@
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 1,
"metadata": {
"scrolled": true
"scrolled": false
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4cceae7b818f43cb9a696b241235f8a3",
"version_major": 2,
"version_minor": 0
},
"text/html": [
"<p>Failed to display Jupyter Widget of type <code>Renderer</code>.</p>\n",
"<p>\n",
" If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n",
" that the widgets JavaScript is still loading. If this message persists, it\n",
" likely means that the widgets JavaScript library is either not installed or\n",
" not enabled. See the <a href=\"https://ipywidgets.readthedocs.io/en/stable/user_install.html\">Jupyter\n",
" Widgets Documentation</a> for setup instructions.\n",
"</p>\n",
"<p>\n",
" If you're reading this message in another frontend (for example, a static\n",
" rendering on GitHub or <a href=\"https://nbviewer.jupyter.org/\">NBViewer</a>),\n",
" it may mean that your frontend doesn't currently support widgets.\n",
"</p>\n"
],
"text/plain": [
"Renderer(background='#dddddd', camera=PerspectiveCamera(aspect=1.3333333333333333, children=(DirectionalLight(position=(-30.0, 50.0, 10.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)),), fov=20.0, position=(3.0, 3.0, 3.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), controls=[OrbitControls(controlling=PerspectiveCamera(aspect=1.3333333333333333, children=(DirectionalLight(position=(-30.0, 50.0, 10.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)),), fov=20.0, position=(3.0, 3.0, 3.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)))], scene=Scene(background=u'#dddddd', children=(AmbientLight(color=u'#aaaaaa', quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), PerspectiveCamera(aspect=1.3333333333333333, children=(DirectionalLight(position=(-30.0, 50.0, 10.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)),), fov=20.0, position=(3.0, 3.0, 3.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), Mesh(geometry=BufferGeometry(attributes={'color': BufferAttribute(array=array([[0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.]], dtype=float32)), 'index': BufferAttribute(array=array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n",
" 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,\n",
" 34, 35], dtype=uint16), normalized=False), 'normal': BufferAttribute(array=array([[ 0., 0., -1.],\n",
" [ 0., 0., -1.],\n",
" [ 0., 0., -1.],\n",
" [ 0., 0., -1.],\n",
" [ 0., 0., -1.],\n",
" [ 0., 0., -1.],\n",
" [-1., 0., 0.],\n",
" [-1., 0., 0.],\n",
" [-1., 0., 0.],\n",
" [-1., 0., 0.],\n",
" [-1., 0., 0.],\n",
" [-1., 0., 0.],\n",
" [ 0., 1., 0.],\n",
" [ 0., 1., 0.],\n",
" [ 0., 1., 0.],\n",
" [ 0., 1., 0.],\n",
" [ 0., 1., 0.],\n",
" [ 0., 1., 0.],\n",
" [ 1., 0., 0.],\n",
" [ 1., 0., 0.],\n",
" [ 1., 0., 0.],\n",
" [ 1., 0., 0.],\n",
" [ 1., 0., 0.],\n",
" [ 1., 0., 0.],\n",
" [ 0., -1., 0.],\n",
" [ 0., -1., 0.],\n",
" [ 0., -1., 0.],\n",
" [ 0., -1., 0.],\n",
" [ 0., -1., 0.],\n",
" [ 0., -1., 0.],\n",
" [ 0., 0., 1.],\n",
" [ 0., 0., 1.],\n",
" [ 0., 0., 1.],\n",
" [ 0., 0., 1.],\n",
" [ 0., 0., 1.],\n",
" [ 0., 0., 1.]], dtype=float32)), 'position': BufferAttribute(array=array([[0., 0., 0.],\n",
" [1., 1., 0.],\n",
" [1., 0., 0.],\n",
" [0., 0., 0.],\n",
" [0., 1., 0.],\n",
" [1., 1., 0.],\n",
" [0., 0., 0.],\n",
" [0., 1., 1.],\n",
" [0., 1., 0.],\n",
" [0., 0., 0.],\n",
" [0., 0., 1.],\n",
" [0., 1., 1.],\n",
" [0., 1., 0.],\n",
" [1., 1., 1.],\n",
" [1., 1., 0.],\n",
" [0., 1., 0.],\n",
" [0., 1., 1.],\n",
" [1., 1., 1.],\n",
" [1., 0., 0.],\n",
" [1., 1., 0.],\n",
" [1., 1., 1.],\n",
" [1., 0., 0.],\n",
" [1., 1., 1.],\n",
" [1., 0., 1.],\n",
" [0., 0., 0.],\n",
" [1., 0., 0.],\n",
" [1., 0., 1.],\n",
" [0., 0., 0.],\n",
" [1., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [1., 0., 1.],\n",
" [1., 1., 1.],\n",
" [0., 0., 1.],\n",
" [1., 1., 1.],\n",
" [0., 1., 1.]], dtype=float32), normalized=False)}), material=MeshLambertMaterial(alphaMap=None, aoMap=None, color=u'white', emissiveMap=None, envMap=None, lightMap=None, map=None, polygonOffset=True, polygonOffsetFactor=0.5, polygonOffsetUnits=0.1, specularMap=None, vertexColors='VertexColors'), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), LineSegments(geometry=BufferGeometry(attributes={'color': BufferAttribute(array=array([[0.5949737 , 0.4317956 , 0.13914175],\n",
" [0.5949737 , 0.4317956 , 0.13914175],\n",
" [0.59724146, 0.4947955 , 0.18033886],\n",
" [0.59724146, 0.4947955 , 0.18033886],\n",
" [0.98003805, 0.3224531 , 0.8646538 ],\n",
" [0.98003805, 0.3224531 , 0.8646538 ],\n",
" [0.99827355, 0.75191396, 0.08575644],\n",
" [0.99827355, 0.75191396, 0.08575644],\n",
" [0.89789945, 0.7949212 , 0.3202241 ],\n",
" [0.89789945, 0.7949212 , 0.3202241 ],\n",
" [0.8110034 , 0.7460238 , 0.17039952],\n",
" [0.8110034 , 0.7460238 , 0.17039952],\n",
" [0.54435354, 0.17887402, 0.4859149 ],\n",
" [0.54435354, 0.17887402, 0.4859149 ],\n",
" [0.93637073, 0.816555 , 0.2179866 ],\n",
" [0.93637073, 0.816555 , 0.2179866 ],\n",
" [0.3925988 , 0.31896043, 0.6568851 ],\n",
" [0.3925988 , 0.31896043, 0.6568851 ],\n",
" [0.0771804 , 0.15173675, 0.09374394],\n",
" [0.0771804 , 0.15173675, 0.09374394],\n",
" [0.02774016, 0.06720248, 0.5104477 ],\n",
" [0.02774016, 0.06720248, 0.5104477 ],\n",
" [0.8880325 , 0.12066253, 0.20826603],\n",
" [0.8880325 , 0.12066253, 0.20826603],\n",
" [0.6779794 , 0.70399 , 0.8883923 ],\n",
" [0.6779794 , 0.70399 , 0.8883923 ],\n",
" [0.32955176, 0.44520178, 0.6428352 ],\n",
" [0.32955176, 0.44520178, 0.6428352 ],\n",
" [0.42922875, 0.06898169, 0.04839262],\n",
" [0.42922875, 0.06898169, 0.04839262],\n",
" [0.62022823, 0.59997374, 0.47011045],\n",
" [0.62022823, 0.59997374, 0.47011045],\n",
" [0.4546548 , 0.49446616, 0.30896202],\n",
" [0.4546548 , 0.49446616, 0.30896202],\n",
" [0.5437848 , 0.2812152 , 0.59819496],\n",
" [0.5437848 , 0.2812152 , 0.59819496]], dtype=float32)), 'index': BufferAttribute(array=array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n",
" 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,\n",
" 34, 35], dtype=uint16), normalized=False), 'position': BufferAttribute(array=array([[0., 0., 0.],\n",
" [1., 1., 0.],\n",
" [1., 1., 0.],\n",
" [1., 0., 0.],\n",
" [1., 0., 0.],\n",
" [0., 0., 0.],\n",
" [0., 0., 0.],\n",
" [0., 1., 0.],\n",
" [0., 1., 0.],\n",
" [1., 1., 0.],\n",
" [0., 0., 0.],\n",
" [0., 1., 1.],\n",
" [0., 1., 1.],\n",
" [0., 1., 0.],\n",
" [0., 0., 0.],\n",
" [0., 0., 1.],\n",
" [0., 0., 1.],\n",
" [0., 1., 1.],\n",
" [0., 1., 0.],\n",
" [1., 1., 1.],\n",
" [1., 1., 1.],\n",
" [1., 1., 0.],\n",
" [0., 1., 1.],\n",
" [1., 1., 1.],\n",
" [1., 1., 1.],\n",
" [1., 0., 0.],\n",
" [1., 1., 1.],\n",
" [1., 0., 1.],\n",
" [1., 0., 1.],\n",
" [1., 0., 0.],\n",
" [1., 0., 1.],\n",
" [0., 0., 0.],\n",
" [1., 0., 1.],\n",
" [0., 0., 1.],\n",
" [1., 1., 1.],\n",
" [0., 0., 1.]], dtype=float32), normalized=False)}), material=LineBasicMaterial(color=u'white', linewidth=3.0, vertexColors='VertexColors'), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), Points(geometry=BufferGeometry(attributes={'position': BufferAttribute(array=array([[0., 0., 0.],\n",
" [0., 0., 1.],\n",
" [0., 1., 0.],\n",
" [0., 1., 1.],\n",
" [1., 0., 0.],\n",
" [1., 0., 1.],\n",
" [1., 1., 0.],\n",
" [1., 1., 1.]], dtype=float32), normalized=False)}), material=PointsMaterial(color=u'red', map=None, size=4.0, sizeAttenuation=False), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0))), fog=None, overrideMaterial=None, quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), shadowMap=WebGLShadowMap())"
]
},
"metadata": {},
"output_type": "display_data"
"ename": "TypeError",
"evalue": "'tuple' object does not support item assignment",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-1-93ec749cebc3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[0mctx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmeshvis\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mContext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mwidth\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m640\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mheight\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m480\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[0mnormals\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmeshvis\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcalculatePointNormals\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvertices\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[0mface_indices\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mm\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mface_vertex_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[0m\u001b[0;32m 10\u001b[0m ctx.draw_faces(m.points(), m.face_vertex_indices(), \n\u001b[0;32m 11\u001b[0m \u001b[0mnormals\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnormals\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\dev\\meshvis\\meshvis\\mesh_helper.pyc\u001b[0m in \u001b[0;36mcalculatePointNormals\u001b[1;34m(vertices, face_indices)\u001b[0m\n\u001b[0;32m 50\u001b[0m \u001b[0mnorm\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnorm\u001b[0m \u001b[1;33m/\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlinalg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnorm\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnorm\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 51\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 52\u001b[1;33m \u001b[0mvalues\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mface\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\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[0mnorm\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 53\u001b[0m \u001b[0mvalues\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mface\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 54\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
]
}
],
"source": [
......@@ -232,14 +29,21 @@
"\n",
"ctx = meshvis.Context(width=640, height=480)\n",
"\n",
"normals = meshvis.calculatePointNormals(vertices=m.points(), face_indices=m.face_vertex_indices())\n",
"ctx.draw_faces(m.points(), m.face_vertex_indices(), \n",
" #normals = meshvis.FaceAttribute(m.face_normals()),\n",
" normals = normals,\n",
" colors = meshvis.UniformAttribute([0, 0, 1]))\n",
"\n",
"ctx.draw_edges(m.points(), m.ev_indices(), \n",
" colors = meshvis.FaceAttribute(np.random.rand(len(m.ev_indices()), 3)),\n",
" linewidth=3)\n",
"\n",
"normal_vis_verts, normal_vis_edges = meshvis.calculateNormalEdges(m.points(), m.face_vertex_indices(), normals, 0.1)\n",
"\n",
"ctx.draw_edges(normal_vis_verts, normal_vis_edges)\n",
"\n",
"\n",
"\n",
"ctx.draw_vertices(m.points(), point_size=4, colors='red')\n",
"\n",
"ctx.display()"
......
......@@ -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