Mercurial > cortex
view org/test-creature.org @ 126:0efe6f04bc26
greatly enhanced touch sensitivity
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Wed, 25 Jan 2012 04:54:48 -0700 |
parents | 3d65633dd736 |
children | bc49d452c42a |
line wrap: on
line source
1 #+title: First attempt at a creature!2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description:5 #+keywords: simulation, jMonkeyEngine3, clojure6 #+SETUPFILE: ../../aurellem/org/setup.org7 #+INCLUDE: ../../aurellem/org/level-0.org9 * ideas11 ** have to get done before winston12 - [ ] write an explination for why greyscale bitmaps for senses is13 appropiate -- 1/2 day14 - [ ] muscle control -- day15 - [ ] proprioception sensor map in the style of the other senses -- day16 - [ ] refactor integration code to distribute to each of the senses17 -- day18 - [ ] create video showing all the senses for Winston -- 2 days19 - [ ] write summary of project for Winston \20 - [ ] project proposals for Winston \21 - [ ] additional senses to be implemented for Winston | -- 2 days22 - [ ] send Winston package /24 ** would be cool to get done before winston25 - [X] enable greyscale bitmaps for touch -- 2 hours26 - [X] use sawfish to auto-tile sense windows -- 6 hours27 - [X] sawfish keybinding to automatically delete all sense windows28 - [ ] directly change the UV-pixels to show sensor activation -- 229 days30 - [ ] proof of concept C sense manipulation -- 2 days31 - [ ] proof of concept GPU sense manipulation -- week32 - [ ] fourier view of sound -- 2 or 3 days33 - [ ] dancing music generator35 ** don't have to get done before winston36 - [ ] write tests for integration -- 3 days37 - [ ] usertime/gametime clock HUD display -- day38 - [ ] find papers for each of the senses justifying my own39 representation -- week40 - [ ] show sensor maps in HUD display? -- 4 days41 - [ ] show sensor maps in AWT display? -- 2 days44 * Intro45 So far, I've made the following senses --46 - Vision47 - Hearing48 - Touch49 - Proprioception51 And one effector:52 - Movement54 However, the code so far has only enabled these senses, but has not55 actually implemented them. For example, there is still a lot of work56 to be done for vision. I need to be able to create an /eyeball/ in57 simulation that can be moved around and see the world from different58 angles. I also need to determine weather to use log-polar or cartesian59 for the visual input, and I need to determine how/wether to60 disceritise the visual input.62 I also want to be able to visualize both the sensors and the63 effectors in pretty pictures. This semi-retarted creature will be my64 first attempt at bringing everything together.66 * The creature's body68 Still going to do an eve-like body in blender, but due to problems69 importing the joints, etc into jMonkeyEngine3, I'm going to do all70 the connecting here in clojure code, using the names of the individual71 components and trial and error. Later, I'll maybe make some sort of72 creature-building modifications to blender that support whatever73 discreitized senses I'm going to make.75 #+name: body-176 #+begin_src clojure77 (ns cortex.silly78 "let's play!"79 {:author "Robert McIntyre"})81 ;; TODO remove this!82 (require 'cortex.import)83 (cortex.import/mega-import-jme3)84 (use '(cortex world util body hearing touch vision))86 (rlm.rlm-commands/help)87 (import java.awt.image.BufferedImage)88 (import javax.swing.JPanel)89 (import javax.swing.SwingUtilities)90 (import java.awt.Dimension)91 (import javax.swing.JFrame)92 (import java.awt.Dimension)93 (import com.aurellem.capture.RatchetTimer)94 (declare joint-create)95 (use 'clojure.contrib.def)97 (defn points->image98 "Take a sparse collection of points and visuliaze it as a99 BufferedImage."101 ;; TODO maybe parallelize this since it's easy103 [points]104 (if (empty? points)105 (BufferedImage. 1 1 BufferedImage/TYPE_BYTE_BINARY)106 (let [xs (vec (map first points))107 ys (vec (map second points))108 x0 (apply min xs)109 y0 (apply min ys)110 width (- (apply max xs) x0)111 height (- (apply max ys) y0)112 image (BufferedImage. (inc width) (inc height)113 BufferedImage/TYPE_INT_RGB)]114 (dorun115 (for [x (range (.getWidth image))116 y (range (.getHeight image))]117 (.setRGB image x y 0xFF0000)))118 (dorun119 (for [index (range (count points))]120 (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))122 image)))124 (defn average [coll]125 (/ (reduce + coll) (count coll)))127 (defn collapse-1d128 "One dimensional analogue of collapse"129 [center line]130 (let [length (count line)131 num-above (count (filter (partial < center) line))132 num-below (- length num-above)]133 (range (- center num-below)134 (+ center num-above))))136 (defn collapse137 "Take a set of pairs of integers and collapse them into a138 contigous bitmap."139 [points]140 (if (empty? points) []141 (let142 [num-points (count points)143 center (vector144 (int (average (map first points)))145 (int (average (map first points))))146 flattened147 (reduce148 concat149 (map150 (fn [column]151 (map vector152 (map first column)153 (collapse-1d (second center)154 (map second column))))155 (partition-by first (sort-by first points))))156 squeezed157 (reduce158 concat159 (map160 (fn [row]161 (map vector162 (collapse-1d (first center)163 (map first row))164 (map second row)))165 (partition-by second (sort-by second flattened))))166 relocate167 (let [min-x (apply min (map first squeezed))168 min-y (apply min (map second squeezed))]169 (map (fn [[x y]]170 [(- x min-x)171 (- y min-y)])172 squeezed))]173 relocate)))175 (defn load-bullet []176 (let [sim (world (Node.) {} no-op no-op)]177 (doto sim178 (.enqueue179 (fn []180 (.stop sim)))181 (.start))))183 (defn load-blender-model184 "Load a .blend file using an asset folder relative path."185 [^String model]186 (.loadModel187 (doto (asset-manager)188 (.registerLoader BlenderModelLoader (into-array String ["blend"])))189 model))191 (defn meta-data [blender-node key]192 (if-let [data (.getUserData blender-node "properties")]193 (.findValue data key)194 nil))196 (defn blender-to-jme197 "Convert from Blender coordinates to JME coordinates"198 [#^Vector3f in]199 (Vector3f. (.getX in)200 (.getZ in)201 (- (.getY in))))203 (defn jme-to-blender204 "Convert from JME coordinates to Blender coordinates"205 [#^Vector3f in]206 (Vector3f. (.getX in)207 (- (.getZ in))208 (.getY in)))210 (defn joint-targets211 "Return the two closest two objects to the joint object, ordered212 from bottom to top according to the joint's rotation."213 [#^Node parts #^Node joint]214 (loop [radius (float 0.01)]215 (let [results (CollisionResults.)]216 (.collideWith217 parts218 (BoundingBox. (.getWorldTranslation joint)219 radius radius radius)220 results)221 (let [targets222 (distinct223 (map #(.getGeometry %) results))]224 (if (>= (count targets) 2)225 (sort-by226 #(let [v227 (jme-to-blender228 (.mult229 (.inverse (.getWorldRotation joint))230 (.subtract (.getWorldTranslation %)231 (.getWorldTranslation joint))))]232 (println-repl (.getName %) ":" v)233 (.dot (Vector3f. 1 1 1)234 v))235 (take 2 targets))236 (recur (float (* radius 2))))))))238 (defn world-to-local239 "Convert the world coordinates into coordinates relative to the240 object (i.e. local coordinates), taking into account the rotation241 of object."242 [#^Spatial object world-coordinate]243 (let [out (Vector3f.)]244 (.worldToLocal object world-coordinate out) out))246 (defn local-to-world247 "Convert the local coordinates into coordinates into world relative248 coordinates"249 [#^Spatial object local-coordinate]250 (let [world-coordinate (Vector3f.)]251 (.localToWorld object local-coordinate world-coordinate)252 world-coordinate))254 (defmulti joint-dispatch255 "Translate blender pseudo-joints into real JME joints."256 (fn [constraints & _]257 (:type constraints)))259 (defmethod joint-dispatch :point260 [constraints control-a control-b pivot-a pivot-b rotation]261 (println-repl "creating POINT2POINT joint")262 (Point2PointJoint.263 control-a264 control-b265 pivot-a266 pivot-b))268 (defmethod joint-dispatch :hinge269 [constraints control-a control-b pivot-a pivot-b rotation]270 (println-repl "creating HINGE joint")271 (let [axis272 (if-let273 [axis (:axis constraints)]274 axis275 Vector3f/UNIT_X)276 [limit-1 limit-2] (:limit constraints)277 hinge-axis278 (.mult279 rotation280 (blender-to-jme axis))]281 (doto282 (HingeJoint.283 control-a284 control-b285 pivot-a286 pivot-b287 hinge-axis288 hinge-axis)289 (.setLimit limit-1 limit-2))))291 (defmethod joint-dispatch :cone292 [constraints control-a control-b pivot-a pivot-b rotation]293 (let [limit-xz (:limit-xz constraints)294 limit-xy (:limit-xy constraints)295 twist (:twist constraints)]297 (println-repl "creating CONE joint")298 (println-repl rotation)299 (println-repl300 "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0)))301 (println-repl302 "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0)))303 (println-repl304 "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1)))305 (doto306 (ConeJoint.307 control-a308 control-b309 pivot-a310 pivot-b311 rotation312 rotation)313 (.setLimit (float limit-xz)314 (float limit-xy)315 (float twist)))))317 (defn connect318 "here are some examples:319 {:type :point}320 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}321 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)323 {:type :cone :limit-xz 0]324 :limit-xy 0]325 :twist 0]} (use XZY rotation mode in blender!)"326 [#^Node obj-a #^Node obj-b #^Node joint]327 (let [control-a (.getControl obj-a RigidBodyControl)328 control-b (.getControl obj-b RigidBodyControl)329 joint-center (.getWorldTranslation joint)330 joint-rotation (.toRotationMatrix (.getWorldRotation joint))331 pivot-a (world-to-local obj-a joint-center)332 pivot-b (world-to-local obj-b joint-center)]334 (if-let [constraints335 (map-vals336 eval337 (read-string338 (meta-data joint "joint")))]339 ;; A side-effect of creating a joint registers340 ;; it with both physics objects which in turn341 ;; will register the joint with the physics system342 ;; when the simulation is started.343 (do344 (println-repl "creating joint between"345 (.getName obj-a) "and" (.getName obj-b))346 (joint-dispatch constraints347 control-a control-b348 pivot-a pivot-b349 joint-rotation))350 (println-repl "could not find joint meta-data!"))))352 (defn assemble-creature [#^Node pieces joints]353 (dorun354 (map355 (fn [geom]356 (let [physics-control357 (RigidBodyControl.358 (HullCollisionShape.359 (.getMesh geom))360 (if-let [mass (meta-data geom "mass")]361 (do362 (println-repl363 "setting" (.getName geom) "mass to" (float mass))364 (float mass))365 (float 1)))]367 (.addControl geom physics-control)))368 (filter #(isa? (class %) Geometry )369 (node-seq pieces))))370 (dorun371 (map372 (fn [joint]373 (let [[obj-a obj-b]374 (joint-targets pieces joint)]375 (connect obj-a obj-b joint)))376 joints))377 pieces)379 (declare blender-creature)381 (def hand "Models/creature1/one.blend")383 (def worm "Models/creature1/try-again.blend")385 (def touch "Models/creature1/touch.blend")387 (defn worm-model [] (load-blender-model worm))389 (defn x-ray [#^ColorRGBA color]390 (doto (Material. (asset-manager)391 "Common/MatDefs/Misc/Unshaded.j3md")392 (.setColor "Color" color)393 (-> (.getAdditionalRenderState)394 (.setDepthTest false))))396 (defn colorful []397 (.getChild (worm-model) "worm-21"))399 (import jme3tools.converters.ImageToAwt)401 (import ij.ImagePlus)403 ;; Every Mesh has many triangles, each with its own index.404 ;; Every vertex has its own index as well.406 (defn tactile-sensor-image407 "Return the touch-sensor distribution image in BufferedImage format,408 or nil if it does not exist."409 [#^Geometry obj]410 (if-let [image-path (meta-data obj "touch")]411 (ImageToAwt/convert412 (.getImage413 (.loadTexture414 (asset-manager)415 image-path))416 false false 0)))418 (import ij.process.ImageProcessor)419 (import java.awt.image.BufferedImage)421 (def white -1)423 (defn filter-pixels424 "List the coordinates of all pixels matching pred, within the bounds425 provided. Bounds -> [x0 y0 width height]"426 {:author "Dylan Holmes"}427 ([pred #^BufferedImage image]428 (filter-pixels pred image [0 0 (.getWidth image) (.getHeight image)]))429 ([pred #^BufferedImage image [x0 y0 width height]]430 ((fn accumulate [x y matches]431 (cond432 (>= y (+ height y0)) matches433 (>= x (+ width x0)) (recur 0 (inc y) matches)434 (pred (.getRGB image x y))435 (recur (inc x) y (conj matches [x y]))436 :else (recur (inc x) y matches)))437 x0 y0 [])))439 (defn white-coordinates440 "Coordinates of all the white pixels in a subset of the image."441 ([#^BufferedImage image bounds]442 (filter-pixels #(= % white) image bounds))443 ([#^BufferedImage image]444 (filter-pixels #(= % white) image)))446 (defn triangle447 "Get the triangle specified by triangle-index from the mesh within448 bounds."449 [#^Mesh mesh triangle-index]450 (let [scratch (Triangle.)]451 (.getTriangle mesh triangle-index scratch)452 scratch))454 (defn triangle-vertex-indices455 "Get the triangle vertex indices of a given triangle from a given456 mesh."457 [#^Mesh mesh triangle-index]458 (let [indices (int-array 3)]459 (.getTriangle mesh triangle-index indices)460 (vec indices)))462 (defn vertex-UV-coord463 "Get the uv-coordinates of the vertex named by vertex-index"464 [#^Mesh mesh vertex-index]465 (let [UV-buffer466 (.getData467 (.getBuffer468 mesh469 VertexBuffer$Type/TexCoord))]470 [(.get UV-buffer (* vertex-index 2))471 (.get UV-buffer (+ 1 (* vertex-index 2)))]))473 (defn triangle-UV-coord474 "Get the uv-cooridnates of the triangle's verticies."475 [#^Mesh mesh width height triangle-index]476 (map (fn [[u v]] (vector (* width u) (* height v)))477 (map (partial vertex-UV-coord mesh)478 (triangle-vertex-indices mesh triangle-index))))480 (defn same-side?481 "Given the points p1 and p2 and the reference point ref, is point p482 on the same side of the line that goes through p1 and p2 as ref is?"483 [p1 p2 ref p]484 (<=485 0486 (.dot487 (.cross (.subtract p2 p1) (.subtract p p1))488 (.cross (.subtract p2 p1) (.subtract ref p1)))))490 (defn triangle-seq [#^Triangle tri]491 [(.get1 tri) (.get2 tri) (.get3 tri)])493 (defn vector3f-seq [#^Vector3f v]494 [(.getX v) (.getY v) (.getZ v)])496 (defn inside-triangle?497 "Is the point inside the triangle?"498 {:author "Dylan Holmes"}499 [#^Triangle tri #^Vector3f p]500 (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]501 (and502 (same-side? vert-1 vert-2 vert-3 p)503 (same-side? vert-2 vert-3 vert-1 p)504 (same-side? vert-3 vert-1 vert-2 p))))506 (defn triangle->matrix4f507 "Converts the triangle into a 4x4 matrix: The first three columns508 contain the vertices of the triangle; the last contains the unit509 normal of the triangle. The bottom row is filled with 1s."510 [#^Triangle t]511 (let [mat (Matrix4f.)512 [vert-1 vert-2 vert-3]513 ((comp vec map) #(.get t %) (range 3))514 unit-normal (do (.calculateNormal t)(.getNormal t))515 vertices [vert-1 vert-2 vert-3 unit-normal]]516 (dorun517 (for [row (range 4) col (range 3)]518 (do519 (.set mat col row (.get (vertices row)col))520 (.set mat 3 row 1))))521 mat))523 (defn triangle-transformation524 "Returns the affine transformation that converts each vertex in the525 first triangle into the corresponding vertex in the second526 triangle."527 [#^Triangle tri-1 #^Triangle tri-2]528 (.mult529 (triangle->matrix4f tri-2)530 (.invert (triangle->matrix4f tri-1))))532 (defn point->vector2f [[u v]]533 (Vector2f. u v))535 (defn vector2f->vector3f [v]536 (Vector3f. (.getX v) (.getY v) 0))538 (defn map-triangle [f #^Triangle tri]539 (Triangle.540 (f 0 (.get1 tri))541 (f 1 (.get2 tri))542 (f 2 (.get3 tri))))544 (defn points->triangle545 "Convert a list of points into a triangle."546 [points]547 (apply #(Triangle. %1 %2 %3)548 (map (fn [point]549 (let [point (vec point)]550 (Vector3f. (get point 0 0)551 (get point 1 0)552 (get point 2 0))))553 (take 3 points))))555 (defn convex-bounds556 "Dimensions of the smallest integer bounding square of the list of557 2D verticies in the form: [x y width height]."558 [uv-verts]559 (let [xs (map first uv-verts)560 ys (map second uv-verts)561 x0 (Math/floor (apply min xs))562 y0 (Math/floor (apply min ys))563 x1 (Math/ceil (apply max xs))564 y1 (Math/ceil (apply max ys))]565 [x0 y0 (- x1 x0) (- y1 y0)]))567 (defn sensors-in-triangle568 "Find the locations of the touch sensors within a triangle in both569 UV and gemoetry relative coordinates."570 [image mesh tri-index]571 (let [width (.getWidth image)572 height (.getHeight image)573 UV-vertex-coords (triangle-UV-coord mesh width height tri-index)574 bounds (convex-bounds UV-vertex-coords)576 cutout-triangle (points->triangle UV-vertex-coords)577 UV-sensor-coords578 (filter (comp (partial inside-triangle? cutout-triangle)579 (fn [[u v]] (Vector3f. u v 0)))580 (white-coordinates image bounds))581 UV->geometry (triangle-transformation582 cutout-triangle583 (triangle mesh tri-index))584 geometry-sensor-coords585 (map (fn [[u v]] (.mult UV->geometry (Vector3f. u v 0)))586 UV-sensor-coords)]587 {:UV UV-sensor-coords :geometry geometry-sensor-coords}))589 (defn-memo locate-feelers590 "Search the geometry's tactile UV image for touch sensors, returning591 their positions in geometry-relative coordinates."592 [#^Geometry geo]593 (let [mesh (.getMesh geo)594 num-triangles (.getTriangleCount mesh)]595 (if-let [image (tactile-sensor-image geo)]596 (map597 (partial sensors-in-triangle image mesh)598 (range num-triangles))599 (repeat (.getTriangleCount mesh) {:UV nil :geometry nil}))))601 (use 'clojure.contrib.def)603 (defn-memo touch-topology [#^Gemoetry geo]604 (vec (collapse (reduce concat (map :UV (locate-feelers geo))))))606 (defn-memo feeler-coordinates [#^Geometry geo]607 (vec (map :geometry (locate-feelers geo))))609 (defn enable-touch [#^Geometry geo]610 (let [feeler-coords (feeler-coordinates geo)611 tris (triangles geo)612 limit 0.1613 ;;results (CollisionResults.)614 ]615 (if (empty? (touch-topology geo))616 nil617 (fn [node]618 (let [sensor-origins619 (map620 #(map (partial local-to-world geo) %)621 feeler-coords)622 triangle-normals623 (map (partial get-ray-direction geo)624 tris)625 rays626 (flatten627 (map (fn [origins norm]628 (map #(doto (Ray. % norm)629 (.setLimit limit)) origins))630 sensor-origins triangle-normals))]631 (vector632 (touch-topology geo)633 (vec634 (for [ray rays]635 (do636 (let [results (CollisionResults.)]637 (.collideWith node ray results)638 (let [touch-objects639 (filter #(not (= geo (.getGeometry %)))640 results)]641 (- 255642 (if (empty? touch-objects) 255643 (rem644 (int645 (* 255 (/ (.getDistance646 (first touch-objects)) limit)))647 256))))))))))))))650 (defn touch [#^Node pieces]651 (filter (comp not nil?)652 (map enable-touch653 (filter #(isa? (class %) Geometry)654 (node-seq pieces)))))657 ;; human eye transmits 62kb/s to brain Bandwidth is 8.75 Mb/s658 ;; http://en.wikipedia.org/wiki/Retina660 (defn test-eye []661 (.getChild662 (.getChild (worm-model) "eyes")663 "eye"))666 (defn retina-sensor-image667 "Return a map of pixel selection functions to BufferedImages668 describing the distribution of light-sensitive components on this669 geometry's surface. Each function creates an integer from the rgb670 values found in the pixel. :red, :green, :blue, :gray are already671 defined as extracting the red green blue and average components672 respectively."673 [#^Spatial eye]674 (if-let [eye-map (meta-data eye "eye")]675 (map-vals676 #(ImageToAwt/convert677 (.getImage (.loadTexture (asset-manager) %))678 false false 0)679 (eval (read-string eye-map)))))681 (defn eye-dimensions682 "returns the width and height specified in the metadata of the eye"683 [#^Spatial eye]684 (let [dimensions685 (map #(vector (.getWidth %) (.getHeight %))686 (vals (retina-sensor-image eye)))]687 [(apply max (map first dimensions))688 (apply max (map second dimensions))]))690 (defn creature-eyes691 "The eye nodes which are children of the \"eyes\" node in the692 creature."693 [#^Node creature]694 (if-let [eye-node (.getChild creature "eyes")]695 (seq (.getChildren eye-node))696 (do (println-repl "could not find eyes node") [])))698 ;; Here's how vision will work.700 ;; Make the continuation in scene-processor take FrameBuffer,701 ;; byte-buffer, BufferedImage already sized to the correct702 ;; dimensions. the continuation will decide wether to "mix" them703 ;; into the BufferedImage, lazily ignore them, or mix them halfway704 ;; and call c/graphics card routines.706 ;; (vision creature) will take an optional :skip argument which will707 ;; inform the continuations in scene processor to skip the given708 ;; number of cycles; 0 means that no cycles will be skipped.710 ;; (vision creature) will return [init-functions sensor-functions].711 ;; The init-functions are each single-arg functions that take the712 ;; world and register the cameras and must each be called before the713 ;; corresponding sensor-functions. Each init-function returns the714 ;; viewport for that eye which can be manipulated, saved, etc. Each715 ;; sensor-function is a thunk and will return data in the same716 ;; format as the tactile-sensor functions; the structure is717 ;; [topology, sensor-data]. Internally, these sensor-functions718 ;; maintain a reference to sensor-data which is periodically updated719 ;; by the continuation function established by its init-function.720 ;; They can be queried every cycle, but their information may not721 ;; necessairly be different every cycle.723 ;; Each eye in the creature in blender will work the same way as724 ;; joints -- a zero dimensional object with no geometry whose local725 ;; coordinate system determines the orientation of the resulting726 ;; eye. All eyes will have a parent named "eyes" just as all joints727 ;; have a parent named "joints". The resulting camera will be a728 ;; ChaseCamera or a CameraNode bound to the geo that is closest to729 ;; the eye marker. The eye marker will contain the metadata for the730 ;; eye, and will be moved by it's bound geometry. The dimensions of731 ;; the eye's camera are equal to the dimensions of the eye's "UV"732 ;; map.735 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;737 ;; Ears work the same way as vision.739 ;; (hearing creature) will return [init-functions740 ;; sensor-functions]. The init functions each take the world and741 ;; register a SoundProcessor that does foureier transforms on the742 ;; incommong sound data, making it available to each sensor function.744 (defn creature-ears745 "The ear nodes which are children of the \"ears\" node in the746 creature."747 [#^Node creature]748 (if-let [ear-node (.getChild creature "ears")]749 (seq (.getChildren ear-node))750 (do (println-repl "could not find ears node") [])))752 (defn closest-node753 "The closest object in creature to the given node."754 [#^Node creature #^Node eye]755 (loop [radius (float 0.01)]756 (let [results (CollisionResults.)]757 (.collideWith758 creature759 (BoundingBox. (.getWorldTranslation eye)760 radius radius radius)761 results)762 (if-let [target (first results)]763 (.getGeometry target)764 (recur (float (* 2 radius)))))))766 (defn bind-sense767 "Bind the sense to the Spatial such that it will maintain its768 current position relative to the Spatial no matter how the spatial769 moves. 'sense can be either a Camera or Listener object."770 [#^Spatial obj sense]771 (let [sense-offset (.subtract (.getLocation sense)772 (.getWorldTranslation obj))773 initial-sense-rotation (Quaternion. (.getRotation sense))774 base-anti-rotation (.inverse (.getWorldRotation obj))]775 (.addControl776 obj777 (proxy [AbstractControl] []778 (controlUpdate [tpf]779 (let [total-rotation780 (.mult base-anti-rotation (.getWorldRotation obj))]781 (.setLocation sense782 (.add783 (.mult total-rotation sense-offset)784 (.getWorldTranslation obj)))785 (.setRotation sense786 (.mult total-rotation initial-sense-rotation))))787 (controlRender [_ _])))))790 (defn update-listener-velocity791 "Update the listener's velocity every update loop."792 [#^Spatial obj #^Listener lis]793 (let [old-position (atom (.getLocation lis))]794 (.addControl795 obj796 (proxy [AbstractControl] []797 (controlUpdate [tpf]798 (let [new-position (.getLocation lis)]799 (.setVelocity800 lis801 (.mult (.subtract new-position @old-position)802 (float (/ tpf))))803 (reset! old-position new-position)))804 (controlRender [_ _])))))806 (import com.aurellem.capture.audio.AudioSendRenderer)808 (defn attach-ear809 [#^Application world #^Node creature #^Spatial ear continuation]810 (let [target (closest-node creature ear)811 lis (Listener.)812 audio-renderer (.getAudioRenderer world)813 sp (sound-processor continuation)]814 (.setLocation lis (.getWorldTranslation ear))815 (.setRotation lis (.getWorldRotation ear))816 (bind-sense target lis)817 (update-listener-velocity target lis)818 (.addListener audio-renderer lis)819 (.registerSoundProcessor audio-renderer lis sp)))821 (defn enable-hearing822 [#^Node creature #^Spatial ear]823 (let [hearing-data (atom [])]824 [(fn [world]825 (attach-ear world creature ear826 (fn [data]827 (reset! hearing-data (vec data)))))828 [(fn []829 (let [data @hearing-data830 topology831 (vec (map #(vector % 0) (range 0 (count data))))832 scaled-data833 (vec834 (map835 #(rem (int (* 255 (/ (+ 1 %) 2))) 256)836 data))]837 [topology scaled-data]))838 ]]))840 (defn hearing841 [#^Node creature]842 (reduce843 (fn [[init-a senses-a]844 [init-b senses-b]]845 [(conj init-a init-b)846 (into senses-a senses-b)])847 [[][]]848 (for [ear (creature-ears creature)]849 (enable-hearing creature ear))))851 (defn attach-eye852 "Attach a Camera to the appropiate area and return the Camera."853 [#^Node creature #^Spatial eye]854 (let [target (closest-node creature eye)855 [cam-width cam-height] (eye-dimensions eye)856 cam (Camera. cam-width cam-height)]857 (.setLocation cam (.getWorldTranslation eye))858 (.setRotation cam (.getWorldRotation eye))859 (.setFrustumPerspective860 cam 45 (/ (.getWidth cam) (.getHeight cam))861 1 1000)862 (bind-sense target cam)863 cam))865 (def presets866 {:all 0xFFFFFF867 :red 0xFF0000868 :blue 0x0000FF869 :green 0x00FF00})871 (defn enable-vision872 "return [init-function sensor-functions] for a particular eye"873 [#^Node creature #^Spatial eye & {skip :skip :or {skip 0}}]874 (let [retinal-map (retina-sensor-image eye)875 camera (attach-eye creature eye)876 vision-image877 (atom878 (BufferedImage. (.getWidth camera)879 (.getHeight camera)880 BufferedImage/TYPE_BYTE_BINARY))]881 [(fn [world]882 (add-eye883 world camera884 (let [counter (atom 0)]885 (fn [r fb bb bi]886 (if (zero? (rem (swap! counter inc) (inc skip)))887 (reset! vision-image (BufferedImage! r fb bb bi)))))))888 (vec889 (map890 (fn [[key image]]891 (let [whites (white-coordinates image)892 topology (vec (collapse whites))893 mask (presets key)]894 (fn []895 (vector896 topology897 (vec898 (for [[x y] whites]899 (bit-and900 mask (.getRGB @vision-image x y))))))))901 retinal-map))]))903 (defn vision904 [#^Node creature & {skip :skip :or {skip 0}}]905 (reduce906 (fn [[init-a senses-a]907 [init-b senses-b]]908 [(conj init-a init-b)909 (into senses-a senses-b)])910 [[][]]911 (for [eye (creature-eyes creature)]912 (enable-vision creature eye))))915 (defn blender-creature916 "Return a creature with all joints in place."917 [blender-path]918 (let [model (load-blender-model blender-path)919 joints920 (if-let [joint-node (.getChild model "joints")]921 (seq (.getChildren joint-node))922 (do (println-repl "could not find joints node") []))]923 (assemble-creature model joints)))925 (defn gray-scale [num]926 (+ num927 (bit-shift-left num 8)928 (bit-shift-left num 16)))930 (defn debug-window931 "creates function that offers a debug view of sensor data"932 []933 (let [vi (view-image)]934 (fn935 [[coords sensor-data]]936 (let [image (points->image coords)]937 (dorun938 (for [i (range (count coords))]939 (.setRGB image ((coords i) 0) ((coords i) 1)940 (gray-scale (sensor-data i)))))943 (vi image)))))945 (defn debug-vision-window946 "creates function that offers a debug view of sensor data"947 []948 (let [vi (view-image)]949 (fn950 [[coords sensor-data]]951 (let [image (points->image coords)]952 (dorun953 (for [i (range (count coords))]954 (.setRGB image ((coords i) 0) ((coords i) 1)955 (sensor-data i))))956 (vi image)))))958 (defn debug-hearing-window959 "view audio data"960 [height]961 (let [vi (view-image)]962 (fn [[coords sensor-data]]963 (let [image (BufferedImage. (count coords) height964 BufferedImage/TYPE_INT_RGB)]965 (dorun966 (for [x (range (count coords))]967 (dorun968 (for [y (range height)]969 (let [raw-sensor (sensor-data x)]970 (.setRGB image x y (gray-scale raw-sensor)))))))972 (vi image)))))976 ;;(defn test-touch [world creature]983 (defn test-creature [thing]984 (let [x-axis985 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)986 y-axis987 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)988 z-axis989 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)990 creature (blender-creature thing)991 touch-nerves (touch creature)992 touch-debug-windows (map (fn [_] (debug-window)) touch-nerves)993 [init-vision-fns vision-data] (vision creature)994 vision-debug (map (fn [_] (debug-vision-window)) vision-data)995 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)996 [init-hearing-fns hearing-senses] (hearing creature)997 hearing-windows (map (fn [_] (debug-hearing-window 50))998 hearing-senses)999 bell (AudioNode. (asset-manager)1000 "Sounds/ear-and-eye.wav" false)1001 ;; dream1003 ]1004 (world1005 (nodify [creature1006 (box 10 2 10 :position (Vector3f. 0 -9 0)1007 :color ColorRGBA/Gray :mass 0)1008 x-axis y-axis z-axis1009 me1010 ])1011 (merge standard-debug-controls1012 {"key-return"1013 (fn [_ value]1014 (if value1015 (do1016 (println-repl "play-sound")1017 (.play bell))))})1018 (fn [world]1019 (light-up-everything world)1020 (enable-debug world)1021 (dorun (map #(% world) init-vision-fns))1022 (dorun (map #(% world) init-hearing-fns))1024 (add-eye world1025 (attach-eye creature (test-eye))1026 (comp (view-image) BufferedImage!))1028 (add-eye world (.getCamera world) no-op)1030 ;;(com.aurellem.capture.Capture/captureVideo1031 ;; world (file-str "/home/r/proj/ai-videos/hand"))1032 ;;(.setTimer world (RatchetTimer. 60))1033 (speed-up world)1034 ;;(set-gravity world (Vector3f. 0 0 0))1035 )1036 (fn [world tpf]1037 ;;(dorun1038 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))1042 (dorun1043 (map #(%1 (%2 (.getRootNode world)))1044 touch-debug-windows touch-nerves))1046 (dorun1047 (map #(%1 (%2))1048 vision-debug vision-data))1049 (dorun1050 (map #(%1 (%2)) hearing-windows hearing-senses))1053 ;;(println-repl (vision-data))1054 (.setLocalTranslation me (.getLocation (.getCamera world)))1057 )1058 ;;(let [timer (atom 0)]1059 ;; (fn [_ _]1060 ;; (swap! timer inc)1061 ;; (if (= (rem @timer 60) 0)1062 ;; (println-repl (float (/ @timer 60))))))1063 )))1073 ;;; experiments in collisions1077 (defn collision-test []1078 (let [b-radius 11079 b-position (Vector3f. 0 0 0)1080 obj-b (box 1 1 1 :color ColorRGBA/Blue1081 :position b-position1082 :mass 0)1083 node (nodify [obj-b])1084 bounds-b1085 (doto (Picture.)1086 (.setHeight 50)1087 (.setWidth 50)1088 (.setImage (asset-manager)1089 "Models/creature1/hand.png"1090 false1091 ))1093 ;;(Ray. (Vector3f. 0 -5 0) (.normalize (Vector3f. 0 1 0)))1095 collisions1096 (let [cr (CollisionResults.)]1097 (.collideWith node bounds-b cr)1098 (println (map #(.getContactPoint %) cr))1099 cr)1101 ;;collision-points1102 ;;(map #(sphere 0.1 :position (.getContactPoint %))1103 ;; collisions)1105 ;;node (nodify (conj collision-points obj-b))1107 sim1108 (world node1109 {"key-space"1110 (fn [_ value]1111 (if value1112 (let [cr (CollisionResults.)]1113 (.collideWith node bounds-b cr)1114 (println-repl (map #(.getContactPoint %) cr))1115 cr)))}1116 no-op1117 no-op)1119 ]1120 sim1122 ))1125 ;; the camera will stay in its initial position/rotation with relation1126 ;; to the spatial.1129 (defn follow-test1130 "show a camera that stays in the same relative position to a blue cube."1131 []1132 (let [camera-pos (Vector3f. 0 30 0)1133 rock (box 1 1 1 :color ColorRGBA/Blue1134 :position (Vector3f. 0 10 0)1135 :mass 301136 )1137 rot (.getWorldRotation rock)1139 table (box 3 1 10 :color ColorRGBA/Gray :mass 01140 :position (Vector3f. 0 -3 0))]1142 (world1143 (nodify [rock table])1144 standard-debug-controls1145 (fn [world]1146 (let1147 [cam (doto (.clone (.getCamera world))1148 (.setLocation camera-pos)1149 (.lookAt Vector3f/ZERO1150 Vector3f/UNIT_X))]1151 (bind-sense rock cam)1153 (.setTimer world (RatchetTimer. 60))1154 (add-eye world cam (comp (view-image) BufferedImage!))1155 (add-eye world (.getCamera world) no-op))1156 )1157 (fn [_ _] (println-repl rot)))))1161 #+end_src1163 #+results: body-11164 : #'cortex.silly/test-creature1167 * COMMENT purgatory1168 #+begin_src clojure1169 (defn bullet-trans []1170 (let [obj-a (sphere 0.5 :color ColorRGBA/Red1171 :position (Vector3f. -10 5 0))1172 obj-b (sphere 0.5 :color ColorRGBA/Blue1173 :position (Vector3f. -10 -5 0)1174 :mass 0)1175 control-a (.getControl obj-a RigidBodyControl)1176 control-b (.getControl obj-b RigidBodyControl)1177 swivel1178 (.toRotationMatrix1179 (doto (Quaternion.)1180 (.fromAngleAxis (/ Math/PI 2)1181 Vector3f/UNIT_X)))]1182 (doto1183 (ConeJoint.1184 control-a control-b1185 (Vector3f. 0 5 0)1186 (Vector3f. 0 -5 0)1187 swivel swivel)1188 (.setLimit (* 0.6 (/ Math/PI 4))1189 (/ Math/PI 4)1190 (* Math/PI 0.8)))1191 (world (nodify1192 [obj-a obj-b])1193 standard-debug-controls1194 enable-debug1195 no-op)))1198 (defn bullet-trans* []1199 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red1200 :position (Vector3f. 5 0 0)1201 :mass 90)1202 obj-b (sphere 0.5 :color ColorRGBA/Blue1203 :position (Vector3f. -5 0 0)1204 :mass 0)1205 control-a (.getControl obj-a RigidBodyControl)1206 control-b (.getControl obj-b RigidBodyControl)1207 move-up? (atom nil)1208 move-down? (atom nil)1209 move-left? (atom nil)1210 move-right? (atom nil)1211 roll-left? (atom nil)1212 roll-right? (atom nil)1213 force 1001214 swivel1215 (.toRotationMatrix1216 (doto (Quaternion.)1217 (.fromAngleAxis (/ Math/PI 2)1218 Vector3f/UNIT_X)))1219 x-move1220 (doto (Matrix3f.)1221 (.fromStartEndVectors Vector3f/UNIT_X1222 (.normalize (Vector3f. 1 1 0))))1224 timer (atom 0)]1225 (doto1226 (ConeJoint.1227 control-a control-b1228 (Vector3f. -8 0 0)1229 (Vector3f. 2 0 0)1230 ;;swivel swivel1231 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY1232 x-move Matrix3f/IDENTITY1233 )1234 (.setCollisionBetweenLinkedBodys false)1235 (.setLimit (* 1 (/ Math/PI 4)) ;; twist1236 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane1237 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane1238 (world (nodify1239 [obj-a obj-b])1240 (merge standard-debug-controls1241 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))1242 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))1243 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))1244 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))1245 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))1246 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})1248 (fn [world]1249 (enable-debug world)1250 (set-gravity world Vector3f/ZERO)1251 )1253 (fn [world _]1255 (if @move-up?1256 (.applyForce control-a1257 (Vector3f. force 0 0)1258 (Vector3f. 0 0 0)))1259 (if @move-down?1260 (.applyForce control-a1261 (Vector3f. (- force) 0 0)1262 (Vector3f. 0 0 0)))1263 (if @move-left?1264 (.applyForce control-a1265 (Vector3f. 0 force 0)1266 (Vector3f. 0 0 0)))1267 (if @move-right?1268 (.applyForce control-a1269 (Vector3f. 0 (- force) 0)1270 (Vector3f. 0 0 0)))1272 (if @roll-left?1273 (.applyForce control-a1274 (Vector3f. 0 0 force)1275 (Vector3f. 0 0 0)))1276 (if @roll-right?1277 (.applyForce control-a1278 (Vector3f. 0 0 (- force))1279 (Vector3f. 0 0 0)))1281 (if (zero? (rem (swap! timer inc) 100))1282 (.attachChild1283 (.getRootNode world)1284 (sphere 0.05 :color ColorRGBA/Yellow1285 :physical? false :position1286 (.getWorldTranslation obj-a)))))1287 )1288 ))1290 (defn transform-trianglesdsd1291 "Transform that converts each vertex in the first triangle1292 into the corresponding vertex in the second triangle."1293 [#^Triangle tri-1 #^Triangle tri-2]1294 (let [in [(.get1 tri-1)1295 (.get2 tri-1)1296 (.get3 tri-1)]1297 out [(.get1 tri-2)1298 (.get2 tri-2)1299 (.get3 tri-2)]]1300 (let [translate (doto (Matrix4f.) (.setTranslation (.negate (in 0))))1301 in* [(.mult translate (in 0))1302 (.mult translate (in 1))1303 (.mult translate (in 2))]1304 final-translation1305 (doto (Matrix4f.)1306 (.setTranslation (out 1)))1308 rotate-11309 (doto (Matrix3f.)1310 (.fromStartEndVectors1311 (.normalize1312 (.subtract1313 (in* 1) (in* 0)))1314 (.normalize1315 (.subtract1316 (out 1) (out 0)))))1317 in** [(.mult rotate-1 (in* 0))1318 (.mult rotate-1 (in* 1))1319 (.mult rotate-1 (in* 2))]1320 scale-factor-11321 (.mult1322 (.normalize1323 (.subtract1324 (out 1)1325 (out 0)))1326 (/ (.length1327 (.subtract (out 1)1328 (out 0)))1329 (.length1330 (.subtract (in** 1)1331 (in** 0)))))1332 scale-1 (doto (Matrix4f.) (.setScale scale-factor-1))1333 in*** [(.mult scale-1 (in** 0))1334 (.mult scale-1 (in** 1))1335 (.mult scale-1 (in** 2))]1341 ]1343 (dorun (map println in))1344 (println)1345 (dorun (map println in*))1346 (println)1347 (dorun (map println in**))1348 (println)1349 (dorun (map println in***))1350 (println)1352 ))))1355 (defn world-setup [joint]1356 (let [joint-position (Vector3f. 0 0 0)1357 joint-rotation1358 (.toRotationMatrix1359 (.mult1360 (doto (Quaternion.)1361 (.fromAngleAxis1362 (* 1 (/ Math/PI 4))1363 (Vector3f. -1 0 0)))1364 (doto (Quaternion.)1365 (.fromAngleAxis1366 (* 1 (/ Math/PI 2))1367 (Vector3f. 0 0 1)))))1368 top-position (.mult joint-rotation (Vector3f. 8 0 0))1370 origin (doto1371 (sphere 0.1 :physical? false :color ColorRGBA/Cyan1372 :position top-position))1373 top (doto1374 (sphere 0.1 :physical? false :color ColorRGBA/Yellow1375 :position top-position)1377 (.addControl1378 (RigidBodyControl.1379 (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))1380 bottom (doto1381 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray1382 :position (Vector3f. 0 0 0))1383 (.addControl1384 (RigidBodyControl.1385 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))1386 table (box 10 2 10 :position (Vector3f. 0 -20 0)1387 :color ColorRGBA/Gray :mass 0)1388 a (.getControl top RigidBodyControl)1389 b (.getControl bottom RigidBodyControl)]1391 (cond1392 (= joint :cone)1394 (doto (ConeJoint.1395 a b1396 (world-to-local top joint-position)1397 (world-to-local bottom joint-position)1398 joint-rotation1399 joint-rotation1400 )1403 (.setLimit (* (/ 10) Math/PI)1404 (* (/ 4) Math/PI)1405 0)))1406 [origin top bottom table]))1408 (defn test-joint [joint]1409 (let [[origin top bottom floor] (world-setup joint)1410 control (.getControl top RigidBodyControl)1411 move-up? (atom false)1412 move-down? (atom false)1413 move-left? (atom false)1414 move-right? (atom false)1415 roll-left? (atom false)1416 roll-right? (atom false)1417 timer (atom 0)]1419 (world1420 (nodify [top bottom floor origin])1421 (merge standard-debug-controls1422 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))1423 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))1424 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))1425 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))1426 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))1427 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})1429 (fn [world]1430 (light-up-everything world)1431 (enable-debug world)1432 (set-gravity world (Vector3f. 0 0 0))1433 )1435 (fn [world _]1436 (if (zero? (rem (swap! timer inc) 100))1437 (do1438 ;; (println-repl @timer)1439 (.attachChild (.getRootNode world)1440 (sphere 0.05 :color ColorRGBA/Yellow1441 :position (.getWorldTranslation top)1442 :physical? false))1443 (.attachChild (.getRootNode world)1444 (sphere 0.05 :color ColorRGBA/LightGray1445 :position (.getWorldTranslation bottom)1446 :physical? false))))1448 (if @move-up?1449 (.applyTorque control1450 (.mult (.getPhysicsRotation control)1451 (Vector3f. 0 0 10))))1452 (if @move-down?1453 (.applyTorque control1454 (.mult (.getPhysicsRotation control)1455 (Vector3f. 0 0 -10))))1456 (if @move-left?1457 (.applyTorque control1458 (.mult (.getPhysicsRotation control)1459 (Vector3f. 0 10 0))))1460 (if @move-right?1461 (.applyTorque control1462 (.mult (.getPhysicsRotation control)1463 (Vector3f. 0 -10 0))))1464 (if @roll-left?1465 (.applyTorque control1466 (.mult (.getPhysicsRotation control)1467 (Vector3f. -1 0 0))))1468 (if @roll-right?1469 (.applyTorque control1470 (.mult (.getPhysicsRotation control)1471 (Vector3f. 1 0 0))))))))1475 (defprotocol Frame1476 (frame [this]))1478 (extend-type BufferedImage1479 Frame1480 (frame [image]1481 (merge1482 (apply1483 hash-map1484 (interleave1485 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]1486 (vector x y)))1487 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]1488 (let [data (.getRGB image x y)]1489 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)1490 :g (bit-shift-right (bit-and 0x00ff00 data) 8)1491 :b (bit-and 0x0000ff data)))))))1492 {:width (.getWidth image) :height (.getHeight image)})))1495 (extend-type ImagePlus1496 Frame1497 (frame [image+]1498 (frame (.getBufferedImage image+))))1501 #+end_src1504 * COMMENT generate source1505 #+begin_src clojure :tangle ../src/cortex/silly.clj1506 <<body-1>>1507 #+end_src