Misc refactor

This commit is contained in:
hodapp 2019-09-18 10:47:17 -04:00
parent dfbfbe19f2
commit afacd3d915
2 changed files with 24 additions and 84 deletions

View File

@ -4,23 +4,7 @@
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 1,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.7/site-packages/quaternion/calculus.py:475: UserWarning: \n",
"\n",
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n",
"Could not import from scipy, which means that derivatives\n",
"and integrals will use less accurate finite-differencing\n",
"techniques. You may want to install scipy.\n",
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n",
"\n",
" warnings.warn(warning_text)\n"
]
}
],
"source": [ "source": [
"import meshutil\n", "import meshutil\n",
"import stl.mesh\n", "import stl.mesh\n",
@ -30,70 +14,27 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 56,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"c = meshutil.cube(open_xz=False)\n",
"mesh_fname = \"cube_test.stl\"\n", "mesh_fname = \"cube_test.stl\"\n",
"#c2 = meshutil.cube_distort(0.3)\n", "# center at (0,0,0) for sanity reasons\n",
"#c2.v = c2.v/2 + [0,1,0]\n", "center = meshutil.TransformStack().translate(-0.5,-0.5,-0.5)\n",
"#c = c.concat(c2)\n", "base = meshutil.cube(open_xz=False).transform(center.mtx)\n",
"m = c.to_stl_mesh()\n", "xform = meshutil.TransformStack()#.translate(0,1,0).scale(0.5).translate(-0.5, -0.5, -0.5)\n",
"\"\"\"\n", "mesh = base\n",
"c1 = meshutil.cube_distort(0.3)\n", "l2 = meshutil.cube_distort(0.1, open_xz=False)\n",
"c1[\"vectors\"] = c1[\"vectors\"]/2 + [0,1,0]\n", "for i in range(2):\n",
"c2 = meshutil.cube_distort(0.3)\n", " m = l2.transform(xform.scale(2).rotate([0,1,0], i*numpy.pi/2).mtx)\n",
"c2[\"vectors\"] = c2[\"vectors\"]/2 + [0.5,1,0]\n", " mesh = mesh.concat(m)\n",
"c3 = meshutil.cube_distort(0.3)\n", "mesh.to_stl_mesh().save(mesh_fname)\n",
"c3[\"vectors\"] = c3[\"vectors\"]/2 + [0,1,0.5]\n", "# WTF, higher ranges make visualizer fail"
"c4 = meshutil.cube_distort(0.3)\n",
"c4[\"vectors\"] = c4[\"vectors\"]/2 + [0.5,1,0.5]\n",
"\"\"\"\n",
"m.save(mesh_fname)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 4., 0., 0., -4.],\n",
" [ 0., 4., 0., 0.],\n",
" [ 0., 0., 4., 0.],\n",
" [ 0., 0., 0., 1.]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = meshutil.TransformStack()\n",
"mtx = s.translate(-1, 0, 0).scale(4).rotate().mtx\n",
"#mtx = meshutil.mtx_rotate(numpy.array([0,1,0]), numpy.pi/4) @ meshutil.mtx_scale(2) @ meshutil.mtx_translate(-1, 0, 0)\n",
"#mtx = meshutil.mtx_translate(-1, 0, 0)\n",
"mtx"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"ct = c.transform(mtx)\n",
"c2 = ct.concat(c)\n",
"c2.to_stl_mesh().save(mesh_fname)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -298,7 +239,7 @@
"Math.pow(distanceToCamera,2)+\n", "Math.pow(distanceToCamera,2)+\n",
"Math.pow(distanceToCamera,2));camera.position.set(len,len,len);controls.update();camera.lookAt(boundingSphere.center);controls.target.set(boundingSphere.center.x,boundingSphere.center.y,boundingSphere.center.z);camera.updateProjectionMatrix();}\n", "Math.pow(distanceToCamera,2));camera.position.set(len,len,len);controls.update();camera.lookAt(boundingSphere.center);controls.target.set(boundingSphere.center.x,boundingSphere.center.y,boundingSphere.center.z);camera.updateProjectionMatrix();}\n",
"function centerControls(obj,camera,controls){const boundingBox=new THREE.Box3().setFromObject(obj);const boundingSphere=new THREE.Sphere();boundingBox.getBoundingSphere((target=boundingSphere));controls.update();controls.target.set(boundingSphere.center.x,boundingSphere.center.y,boundingSphere.center.z);}\n", "function centerControls(obj,camera,controls){const boundingBox=new THREE.Box3().setFromObject(obj);const boundingSphere=new THREE.Sphere();boundingBox.getBoundingSphere((target=boundingSphere));controls.update();controls.target.set(boundingSphere.center.x,boundingSphere.center.y,boundingSphere.center.z);}\n",
"function init(){scene=new THREE.Scene();scene.background=new THREE.Color(0xffffff);tracklight=new THREE.DirectionalLight(0xffffff,1.75);scene.add(tracklight);base64_data="Z2xURgIAAABwBQAAgAMAAEpTT057InNjZW5lIjogMCwgInNjZW5lcyI6IFt7Im5vZGVzIjogWzBdfV0sICJhc3NldCI6IHsidmVyc2lvbiI6ICIyLjAiLCAiZ2VuZXJhdG9yIjogImh0dHBzOi8vZ2l0aHViLmNvbS9taWtlZGgvdHJpbWVzaCJ9LCAiYWNjZXNzb3JzIjogW3siYnVmZmVyVmlldyI6IDAsICJjb21wb25lbnRUeXBlIjogNTEyNSwgImNvdW50IjogNzIsICJtYXgiOiBbMTRdLCAibWluIjogWzBdLCAidHlwZSI6ICJTQ0FMQVIifSwgeyJidWZmZXJWaWV3IjogMSwgImNvbXBvbmVudFR5cGUiOiA1MTI2LCAiY291bnQiOiAxNSwgInR5cGUiOiAiVkVDMyIsICJieXRlT2Zmc2V0IjogMCwgIm1heCI6IFsxLjAsIDQuMCwgNC4wXSwgIm1pbiI6IFstNC4wLCAwLjAsIDAuMF19XSwgIm1lc2hlcyI6IFt7Im5hbWUiOiAiY3ViZV90ZXN0LnN0bCIsICJwcmltaXRpdmVzIjogW3siYXR0c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AAgEAAAAAAAAAAAAAAgEAAAIBAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAIA/AACAwAAAAAAAAAAAAACAwAAAAAAAAIBAAACAwAAAgEAAAAAAAACAwAAAgEAAAIBAAACAPwAAAAAAAAAAAACAPwAAAAAAAIA/AACAPwAAgD8AAAAAAACAPwAAgD8AAIA/";;renderer=new THREE.WebGLRenderer({antialias:true});renderer.setPixelRatio(window.devicePixelRatio);renderer.setSize(window.innerWidth,window.innerHeight);document.body.appendChild(renderer.domElement);loader=new THREE.GLTFLoader();loader.load("data:text/plain;base64,"+base64_data,function(gltf){scene.add(gltf.scene);camera=gltf.cameras[0];controls=new THREE.TrackballControls(camera);controls.rotateSpeed=1.0;controls.zoomSpeed=1.2;controls.panSpeed=0.8;controls.noZoom=false;controls.noPan=false;controls.staticMoving=true;controls.dynamicDampingFactor=0.3;controls.keys=[65,83,68];controls.addEventListener("change",render);centerControls(scene,camera,controls);render();window.addEventListener("resize",onWindowResize,false);animate();});}\n", "function init(){scene=new THREE.Scene();scene.background=new THREE.Color(0xffffff);tracklight=new THREE.DirectionalLight(0xffffff,1.75);scene.add(tracklight);base64_data="Z2xURgIAAABkBgAAkAMAAEpTT057InNjZW5lIjogMCwgInNjZW5lcyI6IFt7Im5vZGVzIjogWzBdfV0sICJhc3NldCI6IHsidmVyc2lvbiI6ICIyLjAiLCAiZ2VuZXJhdG9yIjogImh0dHBzOi8vZ2l0aHViLmNvbS9taWtlZGgvdHJpbWVzaCJ9LCAiYWNjZXNzb3JzIjogW3siYnVmZmVyVmlldyI6IDAsICJjb21wb25lbnRUeXBlIjogNTEyNSwgImNvdW50IjogMTA4LCAibWF4IjogWzIxXSwgIm1pbiI6IFswXSwgInR5cGUiOiAiU0NBTEFSIn0sIHsiYnVmZmVyVmlldyI6IDEsICJjb21wb25lbnRUeXBlIjogNTEyNiwgImNvdW50IjogMjIsICJ0eXBlIjogIlZFQzMiLCAiYnl0ZU9mZnNldCI6IDAsICJtYXgiOiBb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AAACQAAAAIAAAAGAAAAAAAAAAIAAAAJAAAABgAAAAYAAAAVAAAAEgAAAAYAAAAJAAAAFQAAAAAAAAADAAAABQAAAAAAAAAFAAAAAgAAAAAAAAAIAAAAAQAAAAAAAAAHAAAACAAAAAEAAAAUAAAABAAAAAEAAAAIAAAAFAAAAAQAAAATAAAAAwAAAAQAAAAUAAAAEwAAAAMAAAAHAAAAAAAAAAMAAAATAAAABwAAAAcAAAAUAAAACAAAAAcAAAATAAAAFAAAAAAAAAABAAAABAAAAAAAAAAEAAAAAwAAAAAAAAAAAAAAAAAAAAAAACYAAAAAAAAAwAAAAAAAAAAAAAAAQAAAAEAAAAAAAAAAAAAAAEAAAAAAAAAAwAAAAEAAAAAAAAAAQHZ1TL7kFP4/dnVMvnZ1TL7kFP4/dnVMPriwVr7J0QtAnc3lv7iwVr7J0QtAnc3lPwAAAL8AAAC/AAAAvwAAAL8AAAC/AAAAPwAAAL8AAAA/AAAAvwAAAL8AAAA/AAAAPwAAAD8AAAC/AAAAvwAAAD8AAAC/AAAAPwAAAD8AAAA/AAAAvwAAAD8AAAA/AAAAP53N5T/J0QtAuLBWvp3N5T/J0QtAuLBWPjWG5D8hmRhANYbkvzWG5D8hmRhANYbkPw==";;renderer=new THREE.WebGLRenderer({antialias:true});renderer.setPixelRatio(window.devicePixelRatio);renderer.setSize(window.innerWidth,window.innerHeight);document.body.appendChild(renderer.domElement);loader=new THREE.GLTFLoader();loader.load("data:text/plain;base64,"+base64_data,function(gltf){scene.add(gltf.scene);camera=gltf.cameras[0];controls=new THREE.TrackballControls(camera);controls.rotateSpeed=1.0;controls.zoomSpeed=1.2;controls.panSpeed=0.8;controls.noZoom=false;controls.noPan=false;controls.staticMoving=true;controls.dynamicDampingFactor=0.3;controls.keys=[65,83,68];controls.addEventListener("change",render);centerControls(scene,camera,controls);render();window.addEventListener("resize",onWindowResize,false);animate();});}\n",
"function onWindowResize(){camera.aspect=window.innerWidth/window.innerHeight;camera.updateProjectionMatrix();renderer.setSize(window.innerWidth,window.innerHeight);controls.handleResize();render();}\n", "function onWindowResize(){camera.aspect=window.innerWidth/window.innerHeight;camera.updateProjectionMatrix();renderer.setSize(window.innerWidth,window.innerHeight);controls.handleResize();render();}\n",
"function animate(){requestAnimationFrame(animate);controls.update();}\n", "function animate(){requestAnimationFrame(animate);controls.update();}\n",
"function render(){tracklight.position.copy(camera.position);renderer.render(scene,camera);}\n", "function render(){tracklight.position.copy(camera.position);renderer.render(scene,camera);}\n",
@ -309,7 +250,7 @@
"<IPython.core.display.HTML object>" "<IPython.core.display.HTML object>"
] ]
}, },
"execution_count": 11, "execution_count": 57,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }

View File

@ -46,15 +46,14 @@ class TransformStack(object):
self.mtx = numpy.identity(4) self.mtx = numpy.identity(4)
else: else:
self.mtx = mtx self.mtx = mtx
def _compose(self, mtx2):
return TransformStack(mtx2 @ self.mtx)
def scale(self, *a, **kw): def scale(self, *a, **kw):
mtx = mtx_scale(*a, **kw) return self._compose(mtx_scale(*a, **kw))
return TransformStack(mtx @ self.mtx)
def translate(self, *a, **kw): def translate(self, *a, **kw):
mtx = mtx_translate(*a, **kw) return self._compose(mtx_translate(*a, **kw))
return TransformStack(mtx @ self.mtx)
def rotate(self, *a, **kw): def rotate(self, *a, **kw):
mtx = mtx_rotate(*a, **kw) return self._compose(mtx_rotate(*a, **kw))
return TransformStack(mtx @ self.mtx)
def mtx_scale(sx, sy=None, sz=None): def mtx_scale(sx, sy=None, sz=None):
if sy is None: if sy is None:
@ -105,7 +104,7 @@ def cube(open_xz=False):
# winding order? # winding order?
return FaceVertexMesh(verts, faces) return FaceVertexMesh(verts, faces)
def cube_distort(angle): def cube_distort(angle, open_xz=False):
q = quat.rotation_quaternion(numpy.array([-1,0,1]), angle) q = quat.rotation_quaternion(numpy.array([-1,0,1]), angle)
ltf2 = quat.conjugate_by(ltf, q)[0,:] ltf2 = quat.conjugate_by(ltf, q)[0,:]
rtf2 = quat.conjugate_by(rtf, q)[0,:] rtf2 = quat.conjugate_by(rtf, q)[0,:]
@ -116,7 +115,7 @@ def cube_distort(angle):
lbf, rbf, ltf2, rtf2, lbf, rbf, ltf2, rtf2,
lbb, rbb, ltb2, rtb2, lbb, rbb, ltb2, rtb2,
], dtype=numpy.float64) ], dtype=numpy.float64)
if True: #open_xz: if open_xz:
faces = numpy.zeros((8,3), dtype=int) faces = numpy.zeros((8,3), dtype=int)
else: else:
faces = numpy.zeros((12,3), dtype=int) faces = numpy.zeros((12,3), dtype=int)
@ -128,7 +127,7 @@ def cube_distort(angle):
faces[5,:] = [5, 7, 6] faces[5,:] = [5, 7, 6]
faces[6,:] = [4, 2, 0] faces[6,:] = [4, 2, 0]
faces[7,:] = [4, 6, 2] faces[7,:] = [4, 6, 2]
if False: # not open_xz: if not open_xz:
faces[8,:] = [2, 7, 3] faces[8,:] = [2, 7, 3]
faces[9,:] = [2, 6, 7] faces[9,:] = [2, 6, 7]
faces[10,:] = [0, 1, 5] faces[10,:] = [0, 1, 5]