annotate org/test-creature.org @ 112:128fa71ee188

working on retina design
author Robert McIntyre <rlm@mit.edu>
date Thu, 19 Jan 2012 11:29:46 -0700
parents 61d9c0e8d188
children 9d0fe7f54e14
rev   line source
rlm@73 1 #+title: First attempt at a creature!
rlm@73 2 #+author: Robert McIntyre
rlm@73 3 #+email: rlm@mit.edu
rlm@73 4 #+description:
rlm@73 5 #+keywords: simulation, jMonkeyEngine3, clojure
rlm@73 6 #+SETUPFILE: ../../aurellem/org/setup.org
rlm@73 7 #+INCLUDE: ../../aurellem/org/level-0.org
rlm@73 8
rlm@99 9 * objectives
rlm@103 10 - [X] get an overall bitmap-like image for touch
rlm@103 11 - [X] write code to visuliaze this bitmap
rlm@99 12 - [ ] directly change the UV-pixels to show touch sensor activation
rlm@99 13 - [ ] write an explination for why b&w bitmaps for senses is appropiate
rlm@99 14 - [ ] clean up touch code and write visulazation test
rlm@99 15 - [ ] do the same for eyes
rlm@99 16
rlm@73 17 * Intro
rlm@73 18 So far, I've made the following senses --
rlm@73 19 - Vision
rlm@73 20 - Hearing
rlm@73 21 - Touch
rlm@73 22 - Proprioception
rlm@73 23
rlm@73 24 And one effector:
rlm@73 25 - Movement
rlm@73 26
rlm@73 27 However, the code so far has only enabled these senses, but has not
rlm@73 28 actually implemented them. For example, there is still a lot of work
rlm@73 29 to be done for vision. I need to be able to create an /eyeball/ in
rlm@73 30 simulation that can be moved around and see the world from different
rlm@73 31 angles. I also need to determine weather to use log-polar or cartesian
rlm@73 32 for the visual input, and I need to determine how/wether to
rlm@73 33 disceritise the visual input.
rlm@73 34
rlm@73 35 I also want to be able to visualize both the sensors and the
rlm@104 36 effectors in pretty pictures. This semi-retarted creature will be my
rlm@73 37 first attempt at bringing everything together.
rlm@73 38
rlm@73 39 * The creature's body
rlm@73 40
rlm@73 41 Still going to do an eve-like body in blender, but due to problems
rlm@104 42 importing the joints, etc into jMonkeyEngine3, I'm going to do all
rlm@73 43 the connecting here in clojure code, using the names of the individual
rlm@73 44 components and trial and error. Later, I'll maybe make some sort of
rlm@73 45 creature-building modifications to blender that support whatever
rlm@73 46 discreitized senses I'm going to make.
rlm@73 47
rlm@73 48 #+name: body-1
rlm@73 49 #+begin_src clojure
rlm@73 50 (ns cortex.silly
rlm@73 51 "let's play!"
rlm@73 52 {:author "Robert McIntyre"})
rlm@73 53
rlm@73 54 ;; TODO remove this!
rlm@73 55 (require 'cortex.import)
rlm@73 56 (cortex.import/mega-import-jme3)
rlm@73 57 (use '(cortex world util body hearing touch vision))
rlm@73 58
rlm@73 59 (rlm.rlm-commands/help)
rlm@99 60 (import java.awt.image.BufferedImage)
rlm@99 61 (import javax.swing.JPanel)
rlm@99 62 (import javax.swing.SwingUtilities)
rlm@99 63 (import java.awt.Dimension)
rlm@99 64 (import javax.swing.JFrame)
rlm@99 65 (import java.awt.Dimension)
rlm@106 66 (import com.aurellem.capture.RatchetTimer)
rlm@99 67 (declare joint-create)
rlm@108 68 (use 'clojure.contrib.def)
rlm@73 69
rlm@99 70 (defn view-image
rlm@99 71 "Initailizes a JPanel on which you may draw a BufferedImage.
rlm@99 72 Returns a function that accepts a BufferedImage and draws it to the
rlm@99 73 JPanel."
rlm@99 74 []
rlm@99 75 (let [image
rlm@99 76 (atom
rlm@99 77 (BufferedImage. 1 1 BufferedImage/TYPE_4BYTE_ABGR))
rlm@99 78 panel
rlm@99 79 (proxy [JPanel] []
rlm@99 80 (paint
rlm@99 81 [graphics]
rlm@99 82 (proxy-super paintComponent graphics)
rlm@99 83 (.drawImage graphics @image 0 0 nil)))
rlm@99 84 frame (JFrame. "Display Image")]
rlm@99 85 (SwingUtilities/invokeLater
rlm@99 86 (fn []
rlm@99 87 (doto frame
rlm@99 88 (-> (.getContentPane) (.add panel))
rlm@99 89 (.pack)
rlm@99 90 (.setLocationRelativeTo nil)
rlm@99 91 (.setResizable true)
rlm@99 92 (.setVisible true))))
rlm@99 93 (fn [#^BufferedImage i]
rlm@99 94 (reset! image i)
rlm@99 95 (.setSize frame (+ 8 (.getWidth i)) (+ 28 (.getHeight i)))
rlm@99 96 (.repaint panel 0 0 (.getWidth i) (.getHeight i)))))
rlm@99 97
rlm@100 98 (defn points->image
rlm@100 99 "Take a sparse collection of points and visuliaze it as a
rlm@100 100 BufferedImage."
rlm@102 101
rlm@102 102 ;; TODO maybe parallelize this since it's easy
rlm@102 103
rlm@100 104 [points]
rlm@106 105 (if (empty? points)
rlm@106 106 (BufferedImage. 1 1 BufferedImage/TYPE_BYTE_BINARY)
rlm@106 107 (let [xs (vec (map first points))
rlm@106 108 ys (vec (map second points))
rlm@106 109 x0 (apply min xs)
rlm@106 110 y0 (apply min ys)
rlm@106 111 width (- (apply max xs) x0)
rlm@106 112 height (- (apply max ys) y0)
rlm@106 113 image (BufferedImage. (inc width) (inc height)
rlm@106 114 BufferedImage/TYPE_BYTE_BINARY)]
rlm@106 115 (dorun
rlm@106 116 (for [index (range (count points))]
rlm@106 117 (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))
rlm@106 118
rlm@106 119 image)))
rlm@100 120
rlm@101 121 (defn test-data
rlm@101 122 []
rlm@101 123 (vec
rlm@102 124 (for [a (range 0 1000 2)
rlm@102 125 b (range 0 1000 2)]
rlm@101 126 (vector a b))
rlm@101 127 ))
rlm@99 128
rlm@101 129 (defn average [coll]
rlm@101 130 (/ (reduce + coll) (count coll)))
rlm@101 131
rlm@101 132 (defn collapse-1d
rlm@101 133 "One dimensional analogue of collapse"
rlm@101 134 [center line]
rlm@101 135 (let [length (count line)
rlm@101 136 num-above (count (filter (partial < center) line))
rlm@101 137 num-below (- length num-above)]
rlm@101 138 (range (- center num-below)
rlm@101 139 (+ center num-above))
rlm@101 140 ))
rlm@99 141
rlm@99 142 (defn collapse
rlm@99 143 "Take a set of pairs of integers and collapse them into a
rlm@99 144 contigous bitmap."
rlm@99 145 [points]
rlm@108 146 (if (empty? points) []
rlm@108 147 (let
rlm@108 148 [num-points (count points)
rlm@108 149 center (vector
rlm@108 150 (int (average (map first points)))
rlm@108 151 (int (average (map first points))))
rlm@108 152 flattened
rlm@108 153 (reduce
rlm@108 154 concat
rlm@108 155 (map
rlm@108 156 (fn [column]
rlm@108 157 (map vector
rlm@108 158 (map first column)
rlm@108 159 (collapse-1d (second center)
rlm@108 160 (map second column))))
rlm@108 161 (partition-by first (sort-by first points))))
rlm@108 162 squeezed
rlm@108 163 (reduce
rlm@108 164 concat
rlm@108 165 (map
rlm@108 166 (fn [row]
rlm@108 167 (map vector
rlm@108 168 (collapse-1d (first center)
rlm@108 169 (map first row))
rlm@108 170 (map second row)))
rlm@108 171 (partition-by second (sort-by second flattened))))
rlm@108 172 relocate
rlm@108 173 (let [min-x (apply min (map first squeezed))
rlm@108 174 min-y (apply min (map second squeezed))]
rlm@108 175 (map (fn [[x y]]
rlm@108 176 [(- x min-x)
rlm@108 177 (- y min-y)])
rlm@108 178 squeezed))]
rlm@108 179 relocate
rlm@108 180 )))
rlm@83 181
rlm@83 182 (defn load-bullet []
rlm@84 183 (let [sim (world (Node.) {} no-op no-op)]
rlm@102 184 (doto sim
rlm@102 185 (.enqueue
rlm@102 186 (fn []
rlm@102 187 (.stop sim)))
rlm@102 188 (.start))))
rlm@83 189
rlm@73 190 (defn load-blender-model
rlm@73 191 "Load a .blend file using an asset folder relative path."
rlm@73 192 [^String model]
rlm@73 193 (.loadModel
rlm@73 194 (doto (asset-manager)
rlm@73 195 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@73 196 model))
rlm@73 197
rlm@74 198 (defn meta-data [blender-node key]
rlm@74 199 (if-let [data (.getUserData blender-node "properties")]
rlm@74 200 (.findValue data key)
rlm@74 201 nil))
rlm@73 202
rlm@78 203 (defn blender-to-jme
rlm@78 204 "Convert from Blender coordinates to JME coordinates"
rlm@78 205 [#^Vector3f in]
rlm@78 206 (Vector3f. (.getX in)
rlm@78 207 (.getZ in)
rlm@78 208 (- (.getY in))))
rlm@74 209
rlm@79 210 (defn jme-to-blender
rlm@79 211 "Convert from JME coordinates to Blender coordinates"
rlm@79 212 [#^Vector3f in]
rlm@79 213 (Vector3f. (.getX in)
rlm@79 214 (- (.getZ in))
rlm@79 215 (.getY in)))
rlm@79 216
rlm@78 217 (defn joint-targets
rlm@78 218 "Return the two closest two objects to the joint object, ordered
rlm@78 219 from bottom to top according to the joint's rotation."
rlm@78 220 [#^Node parts #^Node joint]
rlm@78 221 ;;(println (meta-data joint "joint"))
rlm@78 222 (.getWorldRotation joint)
rlm@78 223 (loop [radius (float 0.01)]
rlm@78 224 (let [results (CollisionResults.)]
rlm@78 225 (.collideWith
rlm@78 226 parts
rlm@78 227 (BoundingBox. (.getWorldTranslation joint)
rlm@78 228 radius radius radius)
rlm@78 229 results)
rlm@78 230 (let [targets
rlm@78 231 (distinct
rlm@78 232 (map #(.getGeometry %) results))]
rlm@78 233 (if (>= (count targets) 2)
rlm@78 234 (sort-by
rlm@79 235 #(let [v
rlm@79 236 (jme-to-blender
rlm@79 237 (.mult
rlm@79 238 (.inverse (.getWorldRotation joint))
rlm@79 239 (.subtract (.getWorldTranslation %)
rlm@79 240 (.getWorldTranslation joint))))]
rlm@79 241 (println-repl (.getName %) ":" v)
rlm@79 242 (.dot (Vector3f. 1 1 1)
rlm@79 243 v))
rlm@78 244 (take 2 targets))
rlm@78 245 (recur (float (* radius 2))))))))
rlm@74 246
rlm@87 247 (defn world-to-local
rlm@87 248 "Convert the world coordinates into coordinates relative to the
rlm@87 249 object (i.e. local coordinates), taking into account the rotation
rlm@87 250 of object."
rlm@87 251 [#^Spatial object world-coordinate]
rlm@87 252 (let [out (Vector3f.)]
rlm@88 253 (.worldToLocal object world-coordinate out) out))
rlm@87 254
rlm@96 255 (defn local-to-world
rlm@96 256 "Convert the local coordinates into coordinates into world relative
rlm@96 257 coordinates"
rlm@96 258 [#^Spatial object local-coordinate]
rlm@96 259 (let [world-coordinate (Vector3f.)]
rlm@96 260 (.localToWorld object local-coordinate world-coordinate)
rlm@96 261 world-coordinate))
rlm@96 262
rlm@96 263
rlm@87 264 (defmulti joint-dispatch
rlm@87 265 "Translate blender pseudo-joints into real JME joints."
rlm@88 266 (fn [constraints & _]
rlm@87 267 (:type constraints)))
rlm@87 268
rlm@87 269 (defmethod joint-dispatch :point
rlm@87 270 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 271 (println-repl "creating POINT2POINT joint")
rlm@87 272 (Point2PointJoint.
rlm@87 273 control-a
rlm@87 274 control-b
rlm@87 275 pivot-a
rlm@87 276 pivot-b))
rlm@87 277
rlm@87 278 (defmethod joint-dispatch :hinge
rlm@87 279 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 280 (println-repl "creating HINGE joint")
rlm@87 281 (let [axis
rlm@87 282 (if-let
rlm@87 283 [axis (:axis constraints)]
rlm@87 284 axis
rlm@87 285 Vector3f/UNIT_X)
rlm@87 286 [limit-1 limit-2] (:limit constraints)
rlm@87 287 hinge-axis
rlm@87 288 (.mult
rlm@87 289 rotation
rlm@87 290 (blender-to-jme axis))]
rlm@87 291 (doto
rlm@87 292 (HingeJoint.
rlm@87 293 control-a
rlm@87 294 control-b
rlm@87 295 pivot-a
rlm@87 296 pivot-b
rlm@87 297 hinge-axis
rlm@87 298 hinge-axis)
rlm@87 299 (.setLimit limit-1 limit-2))))
rlm@87 300
rlm@87 301 (defmethod joint-dispatch :cone
rlm@87 302 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 303 (let [limit-xz (:limit-xz constraints)
rlm@87 304 limit-xy (:limit-xy constraints)
rlm@87 305 twist (:twist constraints)]
rlm@87 306
rlm@87 307 (println-repl "creating CONE joint")
rlm@87 308 (println-repl rotation)
rlm@87 309 (println-repl
rlm@87 310 "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0)))
rlm@87 311 (println-repl
rlm@87 312 "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0)))
rlm@87 313 (println-repl
rlm@87 314 "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1)))
rlm@87 315 (doto
rlm@87 316 (ConeJoint.
rlm@87 317 control-a
rlm@87 318 control-b
rlm@87 319 pivot-a
rlm@87 320 pivot-b
rlm@87 321 rotation
rlm@87 322 rotation)
rlm@87 323 (.setLimit (float limit-xz)
rlm@87 324 (float limit-xy)
rlm@87 325 (float twist)))))
rlm@87 326
rlm@88 327 (defn connect
rlm@87 328 "here are some examples:
rlm@87 329 {:type :point}
rlm@87 330 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
rlm@87 331 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
rlm@87 332
rlm@89 333 {:type :cone :limit-xz 0]
rlm@89 334 :limit-xy 0]
rlm@89 335 :twist 0]} (use XZY rotation mode in blender!)"
rlm@87 336 [#^Node obj-a #^Node obj-b #^Node joint]
rlm@87 337 (let [control-a (.getControl obj-a RigidBodyControl)
rlm@87 338 control-b (.getControl obj-b RigidBodyControl)
rlm@87 339 joint-center (.getWorldTranslation joint)
rlm@87 340 joint-rotation (.toRotationMatrix (.getWorldRotation joint))
rlm@87 341 pivot-a (world-to-local obj-a joint-center)
rlm@87 342 pivot-b (world-to-local obj-b joint-center)]
rlm@89 343
rlm@87 344 (if-let [constraints
rlm@87 345 (map-vals
rlm@87 346 eval
rlm@87 347 (read-string
rlm@87 348 (meta-data joint "joint")))]
rlm@89 349 ;; A side-effect of creating a joint registers
rlm@89 350 ;; it with both physics objects which in turn
rlm@89 351 ;; will register the joint with the physics system
rlm@89 352 ;; when the simulation is started.
rlm@87 353 (do
rlm@87 354 (println-repl "creating joint between"
rlm@87 355 (.getName obj-a) "and" (.getName obj-b))
rlm@87 356 (joint-dispatch constraints
rlm@87 357 control-a control-b
rlm@87 358 pivot-a pivot-b
rlm@87 359 joint-rotation))
rlm@87 360 (println-repl "could not find joint meta-data!"))))
rlm@87 361
rlm@78 362 (defn assemble-creature [#^Node pieces joints]
rlm@78 363 (dorun
rlm@78 364 (map
rlm@78 365 (fn [geom]
rlm@78 366 (let [physics-control
rlm@78 367 (RigidBodyControl.
rlm@78 368 (HullCollisionShape.
rlm@78 369 (.getMesh geom))
rlm@78 370 (if-let [mass (meta-data geom "mass")]
rlm@78 371 (do
rlm@78 372 (println-repl
rlm@78 373 "setting" (.getName geom) "mass to" (float mass))
rlm@78 374 (float mass))
rlm@78 375 (float 1)))]
rlm@78 376
rlm@78 377 (.addControl geom physics-control)))
rlm@78 378 (filter #(isa? (class %) Geometry )
rlm@78 379 (node-seq pieces))))
rlm@78 380 (dorun
rlm@78 381 (map
rlm@78 382 (fn [joint]
rlm@78 383 (let [[obj-a obj-b]
rlm@78 384 (joint-targets pieces joint)]
rlm@88 385 (connect obj-a obj-b joint)))
rlm@78 386 joints))
rlm@78 387 pieces)
rlm@74 388
rlm@78 389 (defn blender-creature [blender-path]
rlm@78 390 (let [model (load-blender-model blender-path)
rlm@78 391 joints
rlm@78 392 (if-let [joint-node (.getChild model "joints")]
rlm@78 393 (seq (.getChildren joint-node))
rlm@78 394 (do (println-repl "could not find joints node")
rlm@78 395 []))]
rlm@78 396 (assemble-creature model joints)))
rlm@74 397
rlm@78 398 (def hand "Models/creature1/one.blend")
rlm@74 399
rlm@78 400 (def worm "Models/creature1/try-again.blend")
rlm@78 401
rlm@90 402 (def touch "Models/creature1/touch.blend")
rlm@90 403
rlm@90 404 (defn worm-model [] (load-blender-model worm))
rlm@90 405
rlm@80 406 (defn x-ray [#^ColorRGBA color]
rlm@80 407 (doto (Material. (asset-manager)
rlm@80 408 "Common/MatDefs/Misc/Unshaded.j3md")
rlm@80 409 (.setColor "Color" color)
rlm@80 410 (-> (.getAdditionalRenderState)
rlm@80 411 (.setDepthTest false))))
rlm@80 412
rlm@91 413 (defn colorful []
rlm@91 414 (.getChild (worm-model) "worm-21"))
rlm@90 415
rlm@90 416 (import jme3tools.converters.ImageToAwt)
rlm@90 417
rlm@90 418 (import ij.ImagePlus)
rlm@90 419
rlm@108 420 ;; Every Mesh has many triangles, each with its own index.
rlm@108 421 ;; Every vertex has its own index as well.
rlm@90 422
rlm@108 423 (defn tactile-sensor-image
rlm@110 424 "Return the touch-sensor distribution image in BufferedImage format,
rlm@110 425 or nil if it does not exist."
rlm@91 426 [#^Geometry obj]
rlm@110 427 (if-let [image-path (meta-data obj "touch")]
rlm@110 428 (ImageToAwt/convert
rlm@110 429 (.getImage
rlm@110 430 (.loadTexture
rlm@110 431 (asset-manager)
rlm@110 432 image-path))
rlm@110 433 false false 0)))
rlm@110 434
rlm@91 435 (import ij.process.ImageProcessor)
rlm@91 436 (import java.awt.image.BufferedImage)
rlm@91 437
rlm@92 438 (def white -1)
rlm@94 439
rlm@91 440 (defn filter-pixels
rlm@108 441 "List the coordinates of all pixels matching pred, within the bounds
rlm@108 442 provided. Bounds -> [x0 y0 width height]"
rlm@92 443 {:author "Dylan Holmes"}
rlm@108 444 ([pred #^BufferedImage image]
rlm@108 445 (filter-pixels pred image [0 0 (.getWidth image) (.getHeight image)]))
rlm@108 446 ([pred #^BufferedImage image [x0 y0 width height]]
rlm@108 447 ((fn accumulate [x y matches]
rlm@108 448 (cond
rlm@108 449 (>= y (+ height y0)) matches
rlm@108 450 (>= x (+ width x0)) (recur 0 (inc y) matches)
rlm@108 451 (pred (.getRGB image x y))
rlm@108 452 (recur (inc x) y (conj matches [x y]))
rlm@108 453 :else (recur (inc x) y matches)))
rlm@108 454 x0 y0 [])))
rlm@91 455
rlm@91 456 (defn white-coordinates
rlm@108 457 "Coordinates of all the white pixels in a subset of the image."
rlm@112 458 ([#^BufferedImage image bounds]
rlm@112 459 (filter-pixels #(= % white) image bounds))
rlm@112 460 ([#^BufferedImage image]
rlm@112 461 (filter-pixels #(= % white) image)))
rlm@108 462
rlm@108 463 (defn triangle
rlm@112 464 "Get the triangle specified by triangle-index from the mesh within
rlm@112 465 bounds."
rlm@108 466 [#^Mesh mesh triangle-index]
rlm@108 467 (let [scratch (Triangle.)]
rlm@108 468 (.getTriangle mesh triangle-index scratch)
rlm@108 469 scratch))
rlm@108 470
rlm@108 471 (defn triangle-vertex-indices
rlm@108 472 "Get the triangle vertex indices of a given triangle from a given
rlm@108 473 mesh."
rlm@108 474 [#^Mesh mesh triangle-index]
rlm@108 475 (let [indices (int-array 3)]
rlm@108 476 (.getTriangle mesh triangle-index indices)
rlm@108 477 (vec indices)))
rlm@108 478
rlm@108 479 (defn vertex-UV-coord
rlm@108 480 "Get the uv-coordinates of the vertex named by vertex-index"
rlm@108 481 [#^Mesh mesh vertex-index]
rlm@108 482 (let [UV-buffer
rlm@108 483 (.getData
rlm@108 484 (.getBuffer
rlm@108 485 mesh
rlm@108 486 VertexBuffer$Type/TexCoord))]
rlm@108 487 [(.get UV-buffer (* vertex-index 2))
rlm@108 488 (.get UV-buffer (+ 1 (* vertex-index 2)))]))
rlm@108 489
rlm@108 490 (defn triangle-UV-coord
rlm@108 491 "Get the uv-cooridnates of the triangle's verticies."
rlm@108 492 [#^Mesh mesh width height triangle-index]
rlm@108 493 (map (fn [[u v]] (vector (* width u) (* height v)))
rlm@108 494 (map (partial vertex-UV-coord mesh)
rlm@108 495 (triangle-vertex-indices mesh triangle-index))))
rlm@91 496
rlm@102 497 (defn same-side?
rlm@102 498 "Given the points p1 and p2 and the reference point ref, is point p
rlm@102 499 on the same side of the line that goes through p1 and p2 as ref is?"
rlm@102 500 [p1 p2 ref p]
rlm@91 501 (<=
rlm@91 502 0
rlm@91 503 (.dot
rlm@91 504 (.cross (.subtract p2 p1) (.subtract p p1))
rlm@91 505 (.cross (.subtract p2 p1) (.subtract ref p1)))))
rlm@91 506
rlm@108 507 (defn triangle-seq [#^Triangle tri]
rlm@108 508 [(.get1 tri) (.get2 tri) (.get3 tri)])
rlm@108 509
rlm@108 510 (defn vector3f-seq [#^Vector3f v]
rlm@108 511 [(.getX v) (.getY v) (.getZ v)])
rlm@108 512
rlm@108 513 (defn inside-triangle?
rlm@108 514 "Is the point inside the triangle?"
rlm@108 515 {:author "Dylan Holmes"}
rlm@108 516 [#^Triangle tri #^Vector3f p]
rlm@108 517 (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]
rlm@108 518 (and
rlm@108 519 (same-side? vert-1 vert-2 vert-3 p)
rlm@108 520 (same-side? vert-2 vert-3 vert-1 p)
rlm@108 521 (same-side? vert-3 vert-1 vert-2 p))))
rlm@108 522
rlm@94 523 (defn triangle->matrix4f
rlm@108 524 "Converts the triangle into a 4x4 matrix: The first three columns
rlm@108 525 contain the vertices of the triangle; the last contains the unit
rlm@108 526 normal of the triangle. The bottom row is filled with 1s."
rlm@94 527 [#^Triangle t]
rlm@94 528 (let [mat (Matrix4f.)
rlm@94 529 [vert-1 vert-2 vert-3]
rlm@94 530 ((comp vec map) #(.get t %) (range 3))
rlm@94 531 unit-normal (do (.calculateNormal t)(.getNormal t))
rlm@94 532 vertices [vert-1 vert-2 vert-3 unit-normal]]
rlm@94 533 (dorun
rlm@94 534 (for [row (range 4) col (range 3)]
rlm@94 535 (do
rlm@94 536 (.set mat col row (.get (vertices row)col))
rlm@94 537 (.set mat 3 row 1))))
rlm@94 538 mat))
rlm@94 539
rlm@94 540 (defn triangle-transformation
rlm@94 541 "Returns the affine transformation that converts each vertex in the
rlm@94 542 first triangle into the corresponding vertex in the second
rlm@94 543 triangle."
rlm@94 544 [#^Triangle tri-1 #^Triangle tri-2]
rlm@94 545 (.mult
rlm@94 546 (triangle->matrix4f tri-2)
rlm@94 547 (.invert (triangle->matrix4f tri-1))))
rlm@94 548
rlm@108 549 (defn point->vector2f [[u v]]
rlm@108 550 (Vector2f. u v))
rlm@94 551
rlm@94 552 (defn vector2f->vector3f [v]
rlm@94 553 (Vector3f. (.getX v) (.getY v) 0))
rlm@94 554
rlm@94 555 (defn map-triangle [f #^Triangle tri]
rlm@94 556 (Triangle.
rlm@94 557 (f 0 (.get1 tri))
rlm@94 558 (f 1 (.get2 tri))
rlm@94 559 (f 2 (.get3 tri))))
rlm@94 560
rlm@108 561 (defn points->triangle
rlm@108 562 "Convert a list of points into a triangle."
rlm@108 563 [points]
rlm@108 564 (apply #(Triangle. %1 %2 %3)
rlm@108 565 (map (fn [point]
rlm@108 566 (let [point (vec point)]
rlm@108 567 (Vector3f. (get point 0 0)
rlm@108 568 (get point 1 0)
rlm@108 569 (get point 2 0))))
rlm@108 570 (take 3 points))))
rlm@94 571
rlm@108 572 (defn convex-bounds
rlm@108 573 "Dimensions of the smallest integer bounding square of the list of
rlm@108 574 2D verticies in the form: [x y width height]."
rlm@108 575 [uv-verts]
rlm@108 576 (let [xs (map first uv-verts)
rlm@108 577 ys (map second uv-verts)
rlm@108 578 x0 (Math/floor (apply min xs))
rlm@108 579 y0 (Math/floor (apply min ys))
rlm@108 580 x1 (Math/ceil (apply max xs))
rlm@108 581 y1 (Math/ceil (apply max ys))]
rlm@108 582 [x0 y0 (- x1 x0) (- y1 y0)]))
rlm@93 583
rlm@106 584 (defn sensors-in-triangle
rlm@107 585 "Find the locations of the touch sensors within a triangle in both
rlm@107 586 UV and gemoetry relative coordinates."
rlm@107 587 [image mesh tri-index]
rlm@107 588 (let [width (.getWidth image)
rlm@108 589 height (.getHeight image)
rlm@108 590 UV-vertex-coords (triangle-UV-coord mesh width height tri-index)
rlm@108 591 bounds (convex-bounds UV-vertex-coords)
rlm@108 592
rlm@108 593 cutout-triangle (points->triangle UV-vertex-coords)
rlm@108 594 UV-sensor-coords
rlm@108 595 (filter (comp (partial inside-triangle? cutout-triangle)
rlm@108 596 (fn [[u v]] (Vector3f. u v 0)))
rlm@108 597 (white-coordinates image bounds))
rlm@108 598 UV->geometry (triangle-transformation
rlm@108 599 cutout-triangle
rlm@108 600 (triangle mesh tri-index))
rlm@108 601 geometry-sensor-coords
rlm@108 602 (map (fn [[u v]] (.mult UV->geometry (Vector3f. u v 0)))
rlm@108 603 UV-sensor-coords)]
rlm@108 604 {:UV UV-sensor-coords :geometry geometry-sensor-coords}))
rlm@107 605
rlm@108 606 (defn-memo locate-feelers
rlm@94 607 "Search the geometry's tactile UV image for touch sensors, returning
rlm@94 608 their positions in geometry-relative coordinates."
rlm@94 609 [#^Geometry geo]
rlm@108 610 (let [mesh (.getMesh geo)
rlm@108 611 num-triangles (.getTriangleCount mesh)]
rlm@108 612 (if-let [image (tactile-sensor-image geo)]
rlm@108 613 (map
rlm@108 614 (partial sensors-in-triangle image mesh)
rlm@108 615 (range num-triangles))
rlm@108 616 (repeat (.getTriangleCount mesh) {:UV nil :geometry nil}))))
rlm@102 617
rlm@102 618 (use 'clojure.contrib.def)
rlm@102 619
rlm@102 620 (defn-memo touch-topology [#^Gemoetry geo]
rlm@108 621 (vec (collapse (reduce concat (map :UV (locate-feelers geo))))))
rlm@108 622
rlm@108 623 (defn-memo feeler-coordinates [#^Geometry geo]
rlm@108 624 (vec (map :geometry (locate-feelers geo))))
rlm@102 625
rlm@97 626 (defn enable-touch [#^Geometry geo]
rlm@108 627 (let [feeler-coords (feeler-coordinates geo)
rlm@96 628 tris (triangles geo)
rlm@109 629 limit 0.1
rlm@109 630 ;;results (CollisionResults.)
rlm@109 631 ]
rlm@111 632 (if (empty? (touch-topology geo))
rlm@111 633 nil
rlm@111 634 (fn [node]
rlm@111 635 (let [sensor-origins
rlm@111 636 (map
rlm@111 637 #(map (partial local-to-world geo) %)
rlm@111 638 feeler-coords)
rlm@111 639 triangle-normals
rlm@111 640 (map (partial get-ray-direction geo)
rlm@111 641 tris)
rlm@111 642 rays
rlm@111 643 (flatten
rlm@111 644 (map (fn [origins norm]
rlm@111 645 (map #(doto (Ray. % norm)
rlm@97 646 (.setLimit limit)) origins))
rlm@111 647 sensor-origins triangle-normals))]
rlm@111 648 (vector
rlm@111 649 (touch-topology geo)
rlm@111 650 (vec
rlm@111 651 (for [ray rays]
rlm@111 652 (do
rlm@111 653 (let [results (CollisionResults.)]
rlm@111 654 (.collideWith node ray results)
rlm@111 655 (let [touch-objects
rlm@111 656 (set
rlm@111 657 (filter #(not (= geo %))
rlm@111 658 (map #(.getGeometry %) results)))]
rlm@111 659 (if (> (count touch-objects) 0)
rlm@111 660 1 0))))))))))))
rlm@111 661
rlm@111 662 (defn touch [#^Node pieces]
rlm@111 663 (filter (comp not nil?)
rlm@111 664 (map enable-touch
rlm@111 665 (filter #(isa? (class %) Geometry)
rlm@111 666 (node-seq pieces)))))
rlm@94 667
rlm@109 668
rlm@111 669 ;; human eye transmits 62kb/s to brain Bandwidth is 8.75 Mb/s
rlm@111 670 ;; http://en.wikipedia.org/wiki/Retina
rlm@109 671
rlm@111 672 (defn test-eye []
rlm@111 673 (.getChild (worm-model) "worm-11"))
rlm@111 674
rlm@111 675
rlm@111 676 (defn retina-sensor-image
rlm@111 677 "Return a map of pixel selection functions to BufferedImages
rlm@111 678 describing the distribution of light-sensitive components on this
rlm@111 679 geometry's surface. Each function creates an integer from the rgb
rlm@111 680 values found in the pixel. :red, :green, :blue, :gray are already
rlm@111 681 defined as extracting the red green blue and average components
rlm@111 682 respectively."
rlm@111 683 [#^Geometry eye]
rlm@111 684 (if-let [eye-map (meta-data eye "eye")]
rlm@111 685 (map-vals
rlm@111 686 #(ImageToAwt/convert
rlm@111 687 (.getImage (.loadTexture (asset-manager) %))
rlm@111 688 false false 0)
rlm@111 689 (read-string
rlm@111 690 eye-map))))
rlm@111 691
rlm@111 692
rlm@112 693 (defn enable-vision
rlm@111 694
rlm@112 695 ;; need to create a camera based on uv image,
rlm@112 696 ;; update this camera every frame based on the position of this
rlm@112 697 ;; geometry. (maybe can get cam to follow the object)
rlm@111 698
rlm@112 699 ;; use a stack for the continuation to grab the image.
rlm@112 700
rlm@112 701
rlm@112 702 [#^Geometry eye]
rlm@112 703
rlm@112 704
rlm@112 705 ;; Here's how vision will work.
rlm@112 706
rlm@112 707 ;; Make the continuation in scene-processor take FrameBuffer,
rlm@112 708 ;; byte-buffer, BufferedImage already sized to the correct
rlm@112 709 ;; dimensions. the continuation will decide wether to "mix" them
rlm@112 710 ;; into the BufferedImage, lazily ignore them, or mix them halfway
rlm@112 711 ;; and call c/graphics card routines.
rlm@112 712
rlm@112 713 ;; (vision creature) will take an optional :skip argument which will
rlm@112 714 ;; inform the continuations in scene processor to skip the given
rlm@112 715 ;; number of cycles; 0 means that no cycles will be skipped.
rlm@112 716
rlm@112 717 ;; (vision creature) will return [init-functions sensor-functions].
rlm@112 718 ;; The init-functions are each single-arg functions that take the
rlm@112 719 ;; world and register the cameras and must each be called before the
rlm@112 720 ;; corresponding sensor-functions. Each init-function returns the
rlm@112 721 ;; viewport for that eye which can be manipulated, saved, etc. Each
rlm@112 722 ;; sensor-function is a thunk and will return data in the same
rlm@112 723 ;; format as the tactile-sensor functions; the structure is
rlm@112 724 ;; [topology, sensor-data]. Internally, these sensor-functions
rlm@112 725 ;; maintain a reference to sensor-data which is periodically updated
rlm@112 726 ;; by the continuation function established by its init-function.
rlm@112 727 ;; They can be queried every cycle, but their information may not
rlm@112 728 ;; necessairly be different every cycle.
rlm@112 729
rlm@112 730 ;; Each eye in the creature in blender will work the same way as
rlm@112 731 ;; joints -- a one dimensional object with no geometry whose local
rlm@112 732 ;; coordinate system determines the orientation of the resulting
rlm@112 733 ;; eye. All eyes will have a parent named "eyes" just as all joints
rlm@112 734 ;; have a parent named "joints". The resulting camera will be a
rlm@112 735 ;; ChaseCamera or a CameraNode bound to the geo that is closest to
rlm@112 736 ;; the eye marker. The eye marker will contain the metadata for the
rlm@112 737 ;; eye, and will be moved by it's bound geometry. The dimensions of
rlm@112 738 ;; the eye's camera are equal to the dimensions of the eye's "UV"
rlm@112 739 ;; map.
rlm@112 740
rlm@112 741
rlm@112 742 )
rlm@102 743
rlm@103 744 (defn debug-window
rlm@103 745 "creates function that offers a debug view of sensor data"
rlm@103 746 []
rlm@103 747 (let [vi (view-image)]
rlm@103 748 (fn
rlm@103 749 [[coords sensor-data]]
rlm@103 750 (let [image (points->image coords)]
rlm@103 751 (dorun
rlm@103 752 (for [i (range (count coords))]
rlm@103 753 (.setRGB image ((coords i) 0) ((coords i) 1)
rlm@103 754 ({0 -16777216
rlm@103 755 1 -1} (sensor-data i)))))
rlm@103 756 (vi image)))))
rlm@103 757
rlm@83 758
rlm@106 759 ;;(defn test-touch [world creature]
rlm@83 760
rlm@78 761
rlm@106 762 (defn test-creature [thing]
rlm@106 763 (let [x-axis
rlm@106 764 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
rlm@106 765 y-axis
rlm@106 766 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
rlm@106 767 z-axis
rlm@106 768 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
rlm@106 769 creature (blender-creature thing)
rlm@106 770 touch-nerves (touch creature)
rlm@106 771 touch-debug-windows (map (fn [_] (debug-window)) touch-nerves)
rlm@106 772 ]
rlm@106 773 (world
rlm@106 774 (nodify [creature
rlm@106 775 (box 10 2 10 :position (Vector3f. 0 -9 0)
rlm@106 776 :color ColorRGBA/Gray :mass 0)
rlm@106 777 x-axis y-axis z-axis
rlm@106 778 ])
rlm@106 779 standard-debug-controls
rlm@106 780 (fn [world]
rlm@106 781 (light-up-everything world)
rlm@106 782 (enable-debug world)
rlm@106 783 ;;(com.aurellem.capture.Capture/captureVideo
rlm@106 784 ;; world (file-str "/home/r/proj/ai-videos/hand"))
rlm@110 785 ;;(.setTimer world (RatchetTimer. 60))
rlm@110 786 ;;(speed-up world)
rlm@106 787 ;;(set-gravity world (Vector3f. 0 0 0))
rlm@106 788 )
rlm@106 789 (fn [world tpf]
rlm@109 790 ;;(dorun
rlm@109 791 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))
rlm@110 792
rlm@106 793 (dorun
rlm@109 794 (map #(%1 (%2 (.getRootNode world)))
rlm@110 795 touch-debug-windows touch-nerves)
rlm@110 796 )
rlm@109 797
rlm@106 798 )
rlm@106 799 ;;(let [timer (atom 0)]
rlm@106 800 ;; (fn [_ _]
rlm@106 801 ;; (swap! timer inc)
rlm@106 802 ;; (if (= (rem @timer 60) 0)
rlm@106 803 ;; (println-repl (float (/ @timer 60))))))
rlm@106 804 )))
rlm@83 805
rlm@109 806
rlm@109 807
rlm@109 808
rlm@109 809
rlm@109 810
rlm@109 811
rlm@109 812
rlm@109 813
rlm@109 814 ;;; experiments in collisions
rlm@109 815
rlm@109 816
rlm@109 817
rlm@109 818 (defn collision-test []
rlm@110 819 (let [b-radius 1
rlm@110 820 b-position (Vector3f. 0 0 0)
rlm@109 821 obj-b (box 1 1 1 :color ColorRGBA/Blue
rlm@109 822 :position b-position
rlm@110 823 :mass 0)
rlm@110 824 node (nodify [obj-b])
rlm@110 825 bounds-b
rlm@110 826 (doto (Picture.)
rlm@110 827 (.setHeight 50)
rlm@110 828 (.setWidth 50)
rlm@110 829 (.setImage (asset-manager)
rlm@110 830 "Models/creature1/hand.png"
rlm@110 831 false
rlm@110 832 ))
rlm@110 833
rlm@110 834 ;;(Ray. (Vector3f. 0 -5 0) (.normalize (Vector3f. 0 1 0)))
rlm@110 835
rlm@110 836 collisions
rlm@110 837 (let [cr (CollisionResults.)]
rlm@110 838 (.collideWith node bounds-b cr)
rlm@110 839 (println (map #(.getContactPoint %) cr))
rlm@110 840 cr)
rlm@110 841
rlm@110 842 ;;collision-points
rlm@110 843 ;;(map #(sphere 0.1 :position (.getContactPoint %))
rlm@110 844 ;; collisions)
rlm@110 845
rlm@110 846 ;;node (nodify (conj collision-points obj-b))
rlm@110 847
rlm@109 848 sim
rlm@109 849 (world node
rlm@110 850 {"key-space"
rlm@110 851 (fn [_ value]
rlm@110 852 (if value
rlm@110 853 (let [cr (CollisionResults.)]
rlm@110 854 (.collideWith node bounds-b cr)
rlm@110 855 (println-repl (map #(.getContactPoint %) cr))
rlm@110 856 cr)))}
rlm@109 857 no-op
rlm@109 858 no-op)
rlm@109 859
rlm@109 860 ]
rlm@110 861 sim
rlm@109 862
rlm@109 863 ))
rlm@109 864
rlm@109 865
rlm@109 866
rlm@109 867
rlm@87 868 #+end_src
rlm@83 869
rlm@87 870 #+results: body-1
rlm@109 871 : #'cortex.silly/test-creature
rlm@78 872
rlm@78 873
rlm@78 874 * COMMENT purgatory
rlm@78 875 #+begin_src clojure
rlm@77 876 (defn bullet-trans []
rlm@77 877 (let [obj-a (sphere 0.5 :color ColorRGBA/Red
rlm@77 878 :position (Vector3f. -10 5 0))
rlm@77 879 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 880 :position (Vector3f. -10 -5 0)
rlm@77 881 :mass 0)
rlm@77 882 control-a (.getControl obj-a RigidBodyControl)
rlm@77 883 control-b (.getControl obj-b RigidBodyControl)
rlm@77 884 swivel
rlm@77 885 (.toRotationMatrix
rlm@77 886 (doto (Quaternion.)
rlm@77 887 (.fromAngleAxis (/ Math/PI 2)
rlm@77 888 Vector3f/UNIT_X)))]
rlm@77 889 (doto
rlm@77 890 (ConeJoint.
rlm@77 891 control-a control-b
rlm@77 892 (Vector3f. 0 5 0)
rlm@77 893 (Vector3f. 0 -5 0)
rlm@77 894 swivel swivel)
rlm@77 895 (.setLimit (* 0.6 (/ Math/PI 4))
rlm@77 896 (/ Math/PI 4)
rlm@77 897 (* Math/PI 0.8)))
rlm@77 898 (world (nodify
rlm@77 899 [obj-a obj-b])
rlm@77 900 standard-debug-controls
rlm@77 901 enable-debug
rlm@77 902 no-op)))
rlm@74 903
rlm@74 904
rlm@77 905 (defn bullet-trans* []
rlm@77 906 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
rlm@77 907 :position (Vector3f. 5 0 0)
rlm@77 908 :mass 90)
rlm@77 909 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 910 :position (Vector3f. -5 0 0)
rlm@77 911 :mass 0)
rlm@77 912 control-a (.getControl obj-a RigidBodyControl)
rlm@77 913 control-b (.getControl obj-b RigidBodyControl)
rlm@77 914 move-up? (atom nil)
rlm@77 915 move-down? (atom nil)
rlm@77 916 move-left? (atom nil)
rlm@77 917 move-right? (atom nil)
rlm@77 918 roll-left? (atom nil)
rlm@77 919 roll-right? (atom nil)
rlm@77 920 force 100
rlm@77 921 swivel
rlm@77 922 (.toRotationMatrix
rlm@77 923 (doto (Quaternion.)
rlm@77 924 (.fromAngleAxis (/ Math/PI 2)
rlm@77 925 Vector3f/UNIT_X)))
rlm@77 926 x-move
rlm@77 927 (doto (Matrix3f.)
rlm@77 928 (.fromStartEndVectors Vector3f/UNIT_X
rlm@77 929 (.normalize (Vector3f. 1 1 0))))
rlm@77 930
rlm@77 931 timer (atom 0)]
rlm@77 932 (doto
rlm@77 933 (ConeJoint.
rlm@77 934 control-a control-b
rlm@77 935 (Vector3f. -8 0 0)
rlm@77 936 (Vector3f. 2 0 0)
rlm@77 937 ;;swivel swivel
rlm@77 938 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
rlm@77 939 x-move Matrix3f/IDENTITY
rlm@77 940 )
rlm@77 941 (.setCollisionBetweenLinkedBodys false)
rlm@77 942 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
rlm@77 943 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
rlm@77 944 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
rlm@77 945 (world (nodify
rlm@77 946 [obj-a obj-b])
rlm@77 947 (merge standard-debug-controls
rlm@77 948 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@77 949 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@77 950 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@77 951 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@77 952 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@77 953 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@77 954
rlm@77 955 (fn [world]
rlm@77 956 (enable-debug world)
rlm@77 957 (set-gravity world Vector3f/ZERO)
rlm@77 958 )
rlm@77 959
rlm@77 960 (fn [world _]
rlm@77 961
rlm@77 962 (if @move-up?
rlm@77 963 (.applyForce control-a
rlm@77 964 (Vector3f. force 0 0)
rlm@77 965 (Vector3f. 0 0 0)))
rlm@77 966 (if @move-down?
rlm@77 967 (.applyForce control-a
rlm@77 968 (Vector3f. (- force) 0 0)
rlm@77 969 (Vector3f. 0 0 0)))
rlm@77 970 (if @move-left?
rlm@77 971 (.applyForce control-a
rlm@77 972 (Vector3f. 0 force 0)
rlm@77 973 (Vector3f. 0 0 0)))
rlm@77 974 (if @move-right?
rlm@77 975 (.applyForce control-a
rlm@77 976 (Vector3f. 0 (- force) 0)
rlm@77 977 (Vector3f. 0 0 0)))
rlm@77 978
rlm@77 979 (if @roll-left?
rlm@77 980 (.applyForce control-a
rlm@77 981 (Vector3f. 0 0 force)
rlm@77 982 (Vector3f. 0 0 0)))
rlm@77 983 (if @roll-right?
rlm@77 984 (.applyForce control-a
rlm@77 985 (Vector3f. 0 0 (- force))
rlm@77 986 (Vector3f. 0 0 0)))
rlm@77 987
rlm@77 988 (if (zero? (rem (swap! timer inc) 100))
rlm@77 989 (.attachChild
rlm@77 990 (.getRootNode world)
rlm@77 991 (sphere 0.05 :color ColorRGBA/Yellow
rlm@77 992 :physical? false :position
rlm@77 993 (.getWorldTranslation obj-a)))))
rlm@77 994 )
rlm@77 995 ))
rlm@77 996
rlm@94 997 (defn transform-trianglesdsd
rlm@94 998 "Transform that converts each vertex in the first triangle
rlm@94 999 into the corresponding vertex in the second triangle."
rlm@94 1000 [#^Triangle tri-1 #^Triangle tri-2]
rlm@94 1001 (let [in [(.get1 tri-1)
rlm@94 1002 (.get2 tri-1)
rlm@94 1003 (.get3 tri-1)]
rlm@94 1004 out [(.get1 tri-2)
rlm@94 1005 (.get2 tri-2)
rlm@94 1006 (.get3 tri-2)]]
rlm@94 1007 (let [translate (doto (Matrix4f.) (.setTranslation (.negate (in 0))))
rlm@94 1008 in* [(.mult translate (in 0))
rlm@94 1009 (.mult translate (in 1))
rlm@94 1010 (.mult translate (in 2))]
rlm@94 1011 final-translation
rlm@94 1012 (doto (Matrix4f.)
rlm@94 1013 (.setTranslation (out 1)))
rlm@94 1014
rlm@94 1015 rotate-1
rlm@94 1016 (doto (Matrix3f.)
rlm@94 1017 (.fromStartEndVectors
rlm@94 1018 (.normalize
rlm@94 1019 (.subtract
rlm@94 1020 (in* 1) (in* 0)))
rlm@94 1021 (.normalize
rlm@94 1022 (.subtract
rlm@94 1023 (out 1) (out 0)))))
rlm@94 1024 in** [(.mult rotate-1 (in* 0))
rlm@94 1025 (.mult rotate-1 (in* 1))
rlm@94 1026 (.mult rotate-1 (in* 2))]
rlm@94 1027 scale-factor-1
rlm@94 1028 (.mult
rlm@94 1029 (.normalize
rlm@94 1030 (.subtract
rlm@94 1031 (out 1)
rlm@94 1032 (out 0)))
rlm@94 1033 (/ (.length
rlm@94 1034 (.subtract (out 1)
rlm@94 1035 (out 0)))
rlm@94 1036 (.length
rlm@94 1037 (.subtract (in** 1)
rlm@94 1038 (in** 0)))))
rlm@94 1039 scale-1 (doto (Matrix4f.) (.setScale scale-factor-1))
rlm@94 1040 in*** [(.mult scale-1 (in** 0))
rlm@94 1041 (.mult scale-1 (in** 1))
rlm@94 1042 (.mult scale-1 (in** 2))]
rlm@94 1043
rlm@94 1044
rlm@94 1045
rlm@94 1046
rlm@94 1047
rlm@94 1048 ]
rlm@94 1049
rlm@94 1050 (dorun (map println in))
rlm@94 1051 (println)
rlm@94 1052 (dorun (map println in*))
rlm@94 1053 (println)
rlm@94 1054 (dorun (map println in**))
rlm@94 1055 (println)
rlm@94 1056 (dorun (map println in***))
rlm@94 1057 (println)
rlm@94 1058
rlm@99 1059 ))))
rlm@94 1060
rlm@94 1061
rlm@106 1062 (defn world-setup [joint]
rlm@106 1063 (let [joint-position (Vector3f. 0 0 0)
rlm@106 1064 joint-rotation
rlm@106 1065 (.toRotationMatrix
rlm@106 1066 (.mult
rlm@106 1067 (doto (Quaternion.)
rlm@106 1068 (.fromAngleAxis
rlm@106 1069 (* 1 (/ Math/PI 4))
rlm@106 1070 (Vector3f. -1 0 0)))
rlm@106 1071 (doto (Quaternion.)
rlm@106 1072 (.fromAngleAxis
rlm@106 1073 (* 1 (/ Math/PI 2))
rlm@106 1074 (Vector3f. 0 0 1)))))
rlm@106 1075 top-position (.mult joint-rotation (Vector3f. 8 0 0))
rlm@106 1076
rlm@106 1077 origin (doto
rlm@106 1078 (sphere 0.1 :physical? false :color ColorRGBA/Cyan
rlm@106 1079 :position top-position))
rlm@106 1080 top (doto
rlm@106 1081 (sphere 0.1 :physical? false :color ColorRGBA/Yellow
rlm@106 1082 :position top-position)
rlm@106 1083
rlm@106 1084 (.addControl
rlm@106 1085 (RigidBodyControl.
rlm@106 1086 (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))
rlm@106 1087 bottom (doto
rlm@106 1088 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
rlm@106 1089 :position (Vector3f. 0 0 0))
rlm@106 1090 (.addControl
rlm@106 1091 (RigidBodyControl.
rlm@106 1092 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
rlm@106 1093 table (box 10 2 10 :position (Vector3f. 0 -20 0)
rlm@106 1094 :color ColorRGBA/Gray :mass 0)
rlm@106 1095 a (.getControl top RigidBodyControl)
rlm@106 1096 b (.getControl bottom RigidBodyControl)]
rlm@106 1097
rlm@106 1098 (cond
rlm@106 1099 (= joint :cone)
rlm@106 1100
rlm@106 1101 (doto (ConeJoint.
rlm@106 1102 a b
rlm@106 1103 (world-to-local top joint-position)
rlm@106 1104 (world-to-local bottom joint-position)
rlm@106 1105 joint-rotation
rlm@106 1106 joint-rotation
rlm@106 1107 )
rlm@106 1108
rlm@106 1109
rlm@106 1110 (.setLimit (* (/ 10) Math/PI)
rlm@106 1111 (* (/ 4) Math/PI)
rlm@106 1112 0)))
rlm@106 1113 [origin top bottom table]))
rlm@106 1114
rlm@106 1115 (defn test-joint [joint]
rlm@106 1116 (let [[origin top bottom floor] (world-setup joint)
rlm@106 1117 control (.getControl top RigidBodyControl)
rlm@106 1118 move-up? (atom false)
rlm@106 1119 move-down? (atom false)
rlm@106 1120 move-left? (atom false)
rlm@106 1121 move-right? (atom false)
rlm@106 1122 roll-left? (atom false)
rlm@106 1123 roll-right? (atom false)
rlm@106 1124 timer (atom 0)]
rlm@106 1125
rlm@106 1126 (world
rlm@106 1127 (nodify [top bottom floor origin])
rlm@106 1128 (merge standard-debug-controls
rlm@106 1129 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@106 1130 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@106 1131 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@106 1132 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@106 1133 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@106 1134 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@106 1135
rlm@106 1136 (fn [world]
rlm@106 1137 (light-up-everything world)
rlm@106 1138 (enable-debug world)
rlm@106 1139 (set-gravity world (Vector3f. 0 0 0))
rlm@106 1140 )
rlm@106 1141
rlm@106 1142 (fn [world _]
rlm@106 1143 (if (zero? (rem (swap! timer inc) 100))
rlm@106 1144 (do
rlm@106 1145 ;; (println-repl @timer)
rlm@106 1146 (.attachChild (.getRootNode world)
rlm@106 1147 (sphere 0.05 :color ColorRGBA/Yellow
rlm@106 1148 :position (.getWorldTranslation top)
rlm@106 1149 :physical? false))
rlm@106 1150 (.attachChild (.getRootNode world)
rlm@106 1151 (sphere 0.05 :color ColorRGBA/LightGray
rlm@106 1152 :position (.getWorldTranslation bottom)
rlm@106 1153 :physical? false))))
rlm@106 1154
rlm@106 1155 (if @move-up?
rlm@106 1156 (.applyTorque control
rlm@106 1157 (.mult (.getPhysicsRotation control)
rlm@106 1158 (Vector3f. 0 0 10))))
rlm@106 1159 (if @move-down?
rlm@106 1160 (.applyTorque control
rlm@106 1161 (.mult (.getPhysicsRotation control)
rlm@106 1162 (Vector3f. 0 0 -10))))
rlm@106 1163 (if @move-left?
rlm@106 1164 (.applyTorque control
rlm@106 1165 (.mult (.getPhysicsRotation control)
rlm@106 1166 (Vector3f. 0 10 0))))
rlm@106 1167 (if @move-right?
rlm@106 1168 (.applyTorque control
rlm@106 1169 (.mult (.getPhysicsRotation control)
rlm@106 1170 (Vector3f. 0 -10 0))))
rlm@106 1171 (if @roll-left?
rlm@106 1172 (.applyTorque control
rlm@106 1173 (.mult (.getPhysicsRotation control)
rlm@106 1174 (Vector3f. -1 0 0))))
rlm@106 1175 (if @roll-right?
rlm@106 1176 (.applyTorque control
rlm@106 1177 (.mult (.getPhysicsRotation control)
rlm@106 1178 (Vector3f. 1 0 0))))))))
rlm@106 1179
rlm@99 1180
rlm@99 1181
rlm@107 1182 (defprotocol Frame
rlm@107 1183 (frame [this]))
rlm@107 1184
rlm@107 1185 (extend-type BufferedImage
rlm@107 1186 Frame
rlm@107 1187 (frame [image]
rlm@107 1188 (merge
rlm@107 1189 (apply
rlm@107 1190 hash-map
rlm@107 1191 (interleave
rlm@107 1192 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
rlm@107 1193 (vector x y)))
rlm@107 1194 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
rlm@107 1195 (let [data (.getRGB image x y)]
rlm@107 1196 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)
rlm@107 1197 :g (bit-shift-right (bit-and 0x00ff00 data) 8)
rlm@107 1198 :b (bit-and 0x0000ff data)))))))
rlm@107 1199 {:width (.getWidth image) :height (.getHeight image)})))
rlm@107 1200
rlm@107 1201
rlm@107 1202 (extend-type ImagePlus
rlm@107 1203 Frame
rlm@107 1204 (frame [image+]
rlm@107 1205 (frame (.getBufferedImage image+))))
rlm@107 1206
rlm@107 1207
rlm@99 1208 #+end_src
rlm@99 1209
rlm@99 1210
rlm@99 1211 * COMMENT generate source
rlm@99 1212 #+begin_src clojure :tangle ../src/cortex/silly.clj
rlm@99 1213 <<body-1>>
rlm@99 1214 #+end_src
rlm@99 1215
rlm@99 1216
rlm@94 1217
rlm@94 1218