annotate org/test-creature.org @ 119:ebfd62779ab4

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