annotate org/test-creature.org @ 94:69174ed0f9f6

Working sensor coordinate code. Dylan helped!
author Robert McIntyre <rlm@mit.edu>
date Tue, 10 Jan 2012 08:38:04 -0700
parents 7b739503836a
children e4bcd0c481ba
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@73 9 * Intro
rlm@73 10 So far, I've made the following senses --
rlm@73 11 - Vision
rlm@73 12 - Hearing
rlm@73 13 - Touch
rlm@73 14 - Proprioception
rlm@73 15
rlm@73 16 And one effector:
rlm@73 17 - Movement
rlm@73 18
rlm@73 19 However, the code so far has only enabled these senses, but has not
rlm@73 20 actually implemented them. For example, there is still a lot of work
rlm@73 21 to be done for vision. I need to be able to create an /eyeball/ in
rlm@73 22 simulation that can be moved around and see the world from different
rlm@73 23 angles. I also need to determine weather to use log-polar or cartesian
rlm@73 24 for the visual input, and I need to determine how/wether to
rlm@73 25 disceritise the visual input.
rlm@73 26
rlm@73 27 I also want to be able to visualize both the sensors and the
rlm@73 28 effectors in pretty pictures. This semi-retarted creature will by my
rlm@73 29 first attempt at bringing everything together.
rlm@73 30
rlm@73 31 * The creature's body
rlm@73 32
rlm@73 33 Still going to do an eve-like body in blender, but due to problems
rlm@73 34 importing the joints, etc into jMonkeyEngine3, I',m going to do all
rlm@73 35 the connecting here in clojure code, using the names of the individual
rlm@73 36 components and trial and error. Later, I'll maybe make some sort of
rlm@73 37 creature-building modifications to blender that support whatever
rlm@73 38 discreitized senses I'm going to make.
rlm@73 39
rlm@73 40 #+name: body-1
rlm@73 41 #+begin_src clojure
rlm@73 42 (ns cortex.silly
rlm@73 43 "let's play!"
rlm@73 44 {:author "Robert McIntyre"})
rlm@73 45
rlm@73 46 ;; TODO remove this!
rlm@73 47 (require 'cortex.import)
rlm@73 48 (cortex.import/mega-import-jme3)
rlm@73 49 (use '(cortex world util body hearing touch vision))
rlm@73 50
rlm@73 51 (rlm.rlm-commands/help)
rlm@73 52
rlm@87 53 (declare joint-create)
rlm@83 54
rlm@83 55 (defn load-bullet []
rlm@84 56 (let [sim (world (Node.) {} no-op no-op)]
rlm@84 57 (.enqueue
rlm@84 58 sim
rlm@84 59 (fn []
rlm@84 60 (.stop sim)))
rlm@84 61 (.start sim)))
rlm@83 62
rlm@73 63 (defn load-blender-model
rlm@73 64 "Load a .blend file using an asset folder relative path."
rlm@73 65 [^String model]
rlm@73 66 (.loadModel
rlm@73 67 (doto (asset-manager)
rlm@73 68 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@73 69 model))
rlm@73 70
rlm@74 71 (defn meta-data [blender-node key]
rlm@74 72 (if-let [data (.getUserData blender-node "properties")]
rlm@74 73 (.findValue data key)
rlm@74 74 nil))
rlm@73 75
rlm@78 76 (defn blender-to-jme
rlm@78 77 "Convert from Blender coordinates to JME coordinates"
rlm@78 78 [#^Vector3f in]
rlm@78 79 (Vector3f. (.getX in)
rlm@78 80 (.getZ in)
rlm@78 81 (- (.getY in))))
rlm@74 82
rlm@79 83 (defn jme-to-blender
rlm@79 84 "Convert from JME coordinates to Blender coordinates"
rlm@79 85 [#^Vector3f in]
rlm@79 86 (Vector3f. (.getX in)
rlm@79 87 (- (.getZ in))
rlm@79 88 (.getY in)))
rlm@79 89
rlm@78 90 (defn joint-targets
rlm@78 91 "Return the two closest two objects to the joint object, ordered
rlm@78 92 from bottom to top according to the joint's rotation."
rlm@78 93 [#^Node parts #^Node joint]
rlm@78 94 ;;(println (meta-data joint "joint"))
rlm@78 95 (.getWorldRotation joint)
rlm@78 96 (loop [radius (float 0.01)]
rlm@78 97 (let [results (CollisionResults.)]
rlm@78 98 (.collideWith
rlm@78 99 parts
rlm@78 100 (BoundingBox. (.getWorldTranslation joint)
rlm@78 101 radius radius radius)
rlm@78 102 results)
rlm@78 103 (let [targets
rlm@78 104 (distinct
rlm@78 105 (map #(.getGeometry %) results))]
rlm@78 106 (if (>= (count targets) 2)
rlm@78 107 (sort-by
rlm@79 108 #(let [v
rlm@79 109 (jme-to-blender
rlm@79 110 (.mult
rlm@79 111 (.inverse (.getWorldRotation joint))
rlm@79 112 (.subtract (.getWorldTranslation %)
rlm@79 113 (.getWorldTranslation joint))))]
rlm@79 114 (println-repl (.getName %) ":" v)
rlm@79 115 (.dot (Vector3f. 1 1 1)
rlm@79 116 v))
rlm@78 117 (take 2 targets))
rlm@78 118 (recur (float (* radius 2))))))))
rlm@74 119
rlm@87 120 (defn world-to-local
rlm@87 121 "Convert the world coordinates into coordinates relative to the
rlm@87 122 object (i.e. local coordinates), taking into account the rotation
rlm@87 123 of object."
rlm@87 124 [#^Spatial object world-coordinate]
rlm@87 125 (let [out (Vector3f.)]
rlm@88 126 (.worldToLocal object world-coordinate out) out))
rlm@87 127
rlm@87 128 (defmulti joint-dispatch
rlm@87 129 "Translate blender pseudo-joints into real JME joints."
rlm@88 130 (fn [constraints & _]
rlm@87 131 (:type constraints)))
rlm@87 132
rlm@87 133 (defmethod joint-dispatch :point
rlm@87 134 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 135 (println-repl "creating POINT2POINT joint")
rlm@87 136 (Point2PointJoint.
rlm@87 137 control-a
rlm@87 138 control-b
rlm@87 139 pivot-a
rlm@87 140 pivot-b))
rlm@87 141
rlm@87 142 (defmethod joint-dispatch :hinge
rlm@87 143 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 144 (println-repl "creating HINGE joint")
rlm@87 145 (let [axis
rlm@87 146 (if-let
rlm@87 147 [axis (:axis constraints)]
rlm@87 148 axis
rlm@87 149 Vector3f/UNIT_X)
rlm@87 150 [limit-1 limit-2] (:limit constraints)
rlm@87 151 hinge-axis
rlm@87 152 (.mult
rlm@87 153 rotation
rlm@87 154 (blender-to-jme axis))]
rlm@87 155 (doto
rlm@87 156 (HingeJoint.
rlm@87 157 control-a
rlm@87 158 control-b
rlm@87 159 pivot-a
rlm@87 160 pivot-b
rlm@87 161 hinge-axis
rlm@87 162 hinge-axis)
rlm@87 163 (.setLimit limit-1 limit-2))))
rlm@87 164
rlm@87 165 (defmethod joint-dispatch :cone
rlm@87 166 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 167 (let [limit-xz (:limit-xz constraints)
rlm@87 168 limit-xy (:limit-xy constraints)
rlm@87 169 twist (:twist constraints)]
rlm@87 170
rlm@87 171 (println-repl "creating CONE joint")
rlm@87 172 (println-repl rotation)
rlm@87 173 (println-repl
rlm@87 174 "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0)))
rlm@87 175 (println-repl
rlm@87 176 "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0)))
rlm@87 177 (println-repl
rlm@87 178 "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1)))
rlm@87 179 (doto
rlm@87 180 (ConeJoint.
rlm@87 181 control-a
rlm@87 182 control-b
rlm@87 183 pivot-a
rlm@87 184 pivot-b
rlm@87 185 rotation
rlm@87 186 rotation)
rlm@87 187 (.setLimit (float limit-xz)
rlm@87 188 (float limit-xy)
rlm@87 189 (float twist)))))
rlm@87 190
rlm@88 191 (defn connect
rlm@87 192 "here are some examples:
rlm@87 193 {:type :point}
rlm@87 194 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
rlm@87 195 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
rlm@87 196
rlm@89 197 {:type :cone :limit-xz 0]
rlm@89 198 :limit-xy 0]
rlm@89 199 :twist 0]} (use XZY rotation mode in blender!)"
rlm@87 200 [#^Node obj-a #^Node obj-b #^Node joint]
rlm@87 201 (let [control-a (.getControl obj-a RigidBodyControl)
rlm@87 202 control-b (.getControl obj-b RigidBodyControl)
rlm@87 203 joint-center (.getWorldTranslation joint)
rlm@87 204 joint-rotation (.toRotationMatrix (.getWorldRotation joint))
rlm@87 205 pivot-a (world-to-local obj-a joint-center)
rlm@87 206 pivot-b (world-to-local obj-b joint-center)]
rlm@89 207
rlm@87 208 (if-let [constraints
rlm@87 209 (map-vals
rlm@87 210 eval
rlm@87 211 (read-string
rlm@87 212 (meta-data joint "joint")))]
rlm@89 213 ;; A side-effect of creating a joint registers
rlm@89 214 ;; it with both physics objects which in turn
rlm@89 215 ;; will register the joint with the physics system
rlm@89 216 ;; when the simulation is started.
rlm@87 217 (do
rlm@87 218 (println-repl "creating joint between"
rlm@87 219 (.getName obj-a) "and" (.getName obj-b))
rlm@87 220 (joint-dispatch constraints
rlm@87 221 control-a control-b
rlm@87 222 pivot-a pivot-b
rlm@87 223 joint-rotation))
rlm@87 224 (println-repl "could not find joint meta-data!"))))
rlm@87 225
rlm@78 226 (defn assemble-creature [#^Node pieces joints]
rlm@78 227 (dorun
rlm@78 228 (map
rlm@78 229 (fn [geom]
rlm@78 230 (let [physics-control
rlm@78 231 (RigidBodyControl.
rlm@78 232 (HullCollisionShape.
rlm@78 233 (.getMesh geom))
rlm@78 234 (if-let [mass (meta-data geom "mass")]
rlm@78 235 (do
rlm@78 236 (println-repl
rlm@78 237 "setting" (.getName geom) "mass to" (float mass))
rlm@78 238 (float mass))
rlm@78 239 (float 1)))]
rlm@78 240
rlm@78 241 (.addControl geom physics-control)))
rlm@78 242 (filter #(isa? (class %) Geometry )
rlm@78 243 (node-seq pieces))))
rlm@77 244
rlm@78 245 (dorun
rlm@78 246 (map
rlm@78 247 (fn [joint]
rlm@78 248 (let [[obj-a obj-b]
rlm@78 249 (joint-targets pieces joint)]
rlm@88 250 (connect obj-a obj-b joint)))
rlm@78 251 joints))
rlm@78 252 pieces)
rlm@74 253
rlm@78 254 (defn blender-creature [blender-path]
rlm@78 255 (let [model (load-blender-model blender-path)
rlm@78 256 joints
rlm@78 257 (if-let [joint-node (.getChild model "joints")]
rlm@78 258 (seq (.getChildren joint-node))
rlm@78 259 (do (println-repl "could not find joints node")
rlm@78 260 []))]
rlm@78 261 (assemble-creature model joints)))
rlm@74 262
rlm@78 263 (def hand "Models/creature1/one.blend")
rlm@74 264
rlm@78 265 (def worm "Models/creature1/try-again.blend")
rlm@78 266
rlm@90 267 (def touch "Models/creature1/touch.blend")
rlm@90 268
rlm@90 269 (defn worm-model [] (load-blender-model worm))
rlm@90 270
rlm@80 271 (defn x-ray [#^ColorRGBA color]
rlm@80 272 (doto (Material. (asset-manager)
rlm@80 273 "Common/MatDefs/Misc/Unshaded.j3md")
rlm@80 274 (.setColor "Color" color)
rlm@80 275 (-> (.getAdditionalRenderState)
rlm@80 276 (.setDepthTest false))))
rlm@80 277
rlm@78 278 (defn test-creature [thing]
rlm@80 279 (let [x-axis
rlm@80 280 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
rlm@80 281 y-axis
rlm@80 282 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
rlm@80 283 z-axis
rlm@80 284 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)]
rlm@78 285 (world
rlm@78 286 (nodify [(blender-creature thing)
rlm@81 287 (box 10 2 10 :position (Vector3f. 0 -9 0)
rlm@80 288 :color ColorRGBA/Gray :mass 0)
rlm@80 289 x-axis y-axis z-axis
rlm@80 290 ])
rlm@78 291 standard-debug-controls
rlm@90 292 (fn [world]
rlm@90 293 (light-up-everything world)
rlm@90 294 (enable-debug world)
rlm@90 295 ;;(com.aurellem.capture.Capture/captureVideo
rlm@90 296 ;; world (file-str "/home/r/proj/ai-videos/hand"))
rlm@90 297 (.setTimer world (NanoTimer.))
rlm@93 298 (set-gravity world (Vector3f. 0 0 0))
rlm@90 299 (speed-up world)
rlm@90 300 )
rlm@90 301 no-op
rlm@90 302 ;;(let [timer (atom 0)]
rlm@90 303 ;; (fn [_ _]
rlm@90 304 ;; (swap! timer inc)
rlm@90 305 ;; (if (= (rem @timer 60) 0)
rlm@90 306 ;; (println-repl (float (/ @timer 60))))))
rlm@90 307 )))
rlm@90 308
rlm@90 309
rlm@91 310 (defn colorful []
rlm@91 311 (.getChild (worm-model) "worm-21"))
rlm@90 312
rlm@90 313 (import jme3tools.converters.ImageToAwt)
rlm@90 314
rlm@90 315 (import ij.ImagePlus)
rlm@90 316
rlm@90 317 (defn triangle-indices
rlm@90 318 "Get the triangle vertex indices of a given triangle from a given
rlm@90 319 mesh."
rlm@90 320 [#^Mesh mesh triangle-index]
rlm@90 321 (let [indices (int-array 3)]
rlm@90 322 (.getTriangle mesh triangle-index indices)
rlm@90 323 (vec indices)))
rlm@90 324
rlm@90 325 (defn uv-coord
rlm@90 326 "Get the uv-coordinates of the vertex named by vertex-index"
rlm@90 327 [#^Mesh mesh vertex-index]
rlm@90 328 (let [UV-buffer
rlm@90 329 (.getData
rlm@90 330 (.getBuffer
rlm@90 331 mesh
rlm@90 332 VertexBuffer$Type/TexCoord))]
rlm@91 333 (Vector2f.
rlm@91 334 (.get UV-buffer (* vertex-index 2))
rlm@91 335 (.get UV-buffer (+ 1 (* vertex-index 2))))))
rlm@90 336
rlm@93 337 (defn tri-uv-coord
rlm@93 338 "Get the uv-cooridnates of the triangle's verticies."
rlm@93 339 [#^Mesh mesh #^Triangle triangle]
rlm@93 340 (map (partial uv-coord mesh)
rlm@93 341 (triangle-indices mesh (.getIndex triangle))))
rlm@93 342
rlm@91 343 (defn touch-receptor-image
rlm@91 344 "Return the touch-sensor distribution image in ImagePlus format."
rlm@91 345 [#^Geometry obj]
rlm@90 346 (let
rlm@91 347 [mat (.getMaterial obj)
rlm@90 348 texture
rlm@90 349 (.getTextureValue
rlm@90 350 (.getTextureParam
rlm@90 351 mat
rlm@90 352 MaterialHelper/TEXTURE_TYPE_DIFFUSE))
rlm@90 353 im (.getImage texture)]
rlm@90 354 (ImagePlus.
rlm@90 355 "UV-map"
rlm@90 356 (ImageToAwt/convert im false false 0))))
rlm@90 357
rlm@91 358
rlm@91 359 (import ij.process.ImageProcessor)
rlm@91 360 (import java.awt.image.BufferedImage)
rlm@91 361
rlm@91 362 (defprotocol Frame
rlm@91 363 (frame [this]))
rlm@91 364
rlm@91 365 (extend-type BufferedImage
rlm@91 366 Frame
rlm@91 367 (frame [image]
rlm@91 368 (merge
rlm@91 369 (apply
rlm@91 370 hash-map
rlm@91 371 (interleave
rlm@91 372 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
rlm@91 373 (vector x y)))
rlm@91 374 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
rlm@91 375 (let [data (.getRGB image x y)]
rlm@91 376 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)
rlm@91 377 :g (bit-shift-right (bit-and 0x00ff00 data) 8)
rlm@91 378 :b (bit-and 0x0000ff data)))))))
rlm@91 379 {:width (.getWidth image) :height (.getHeight image)})))
rlm@91 380
rlm@91 381
rlm@91 382 (extend-type ImagePlus
rlm@91 383 Frame
rlm@91 384 (frame [image+]
rlm@91 385 (frame (.getBufferedImage image+))))
rlm@91 386
rlm@94 387
rlm@92 388 (def white -1)
rlm@94 389
rlm@91 390 (defn filter-pixels
rlm@91 391 "List the coordinates of all pixels matching pred."
rlm@92 392 {:author "Dylan Holmes"}
rlm@91 393 [pred #^ImageProcessor ip]
rlm@91 394 (let
rlm@91 395 [width (.getWidth ip)
rlm@91 396 height (.getHeight ip)]
rlm@91 397 ((fn accumulate [x y matches]
rlm@91 398 (cond
rlm@91 399 (>= y height) matches
rlm@91 400 (>= x width) (recur 0 (inc y) matches)
rlm@91 401 (pred (.getPixel ip x y))
rlm@91 402 (recur (inc x) y (conj matches (Vector2f. x y)))
rlm@91 403 :else (recur (inc x) y matches)))
rlm@91 404 0 0 [])))
rlm@91 405
rlm@91 406 (defn white-coordinates
rlm@91 407 "List the coordinates of all the white pixels in an image."
rlm@91 408 [#^ImageProcessor ip]
rlm@92 409 (filter-pixels #(= % white) ip))
rlm@91 410
rlm@91 411 (defn same-side? [p1 p2 ref p]
rlm@91 412 (<=
rlm@91 413 0
rlm@91 414 (.dot
rlm@91 415 (.cross (.subtract p2 p1) (.subtract p p1))
rlm@91 416 (.cross (.subtract p2 p1) (.subtract ref p1)))))
rlm@91 417
rlm@94 418
rlm@94 419 (defn triangle->matrix4f
rlm@94 420 "Converts the triangle into a 4x4 matrix of vertices: The first
rlm@94 421 three columns contain the vertices of the triangle; the last
rlm@94 422 contains the unit normal of the triangle. The bottom row is filled
rlm@94 423 with 1s."
rlm@94 424 [#^Triangle t]
rlm@94 425 (let [mat (Matrix4f.)
rlm@94 426 [vert-1 vert-2 vert-3]
rlm@94 427 ((comp vec map) #(.get t %) (range 3))
rlm@94 428 unit-normal (do (.calculateNormal t)(.getNormal t))
rlm@94 429 vertices [vert-1 vert-2 vert-3 unit-normal]]
rlm@94 430
rlm@94 431 (dorun
rlm@94 432 (for [row (range 4) col (range 3)]
rlm@94 433 (do
rlm@94 434 (.set mat col row (.get (vertices row)col))
rlm@94 435 (.set mat 3 row 1))))
rlm@94 436 mat))
rlm@94 437
rlm@94 438 (defn triangle-transformation
rlm@94 439 "Returns the affine transformation that converts each vertex in the
rlm@94 440 first triangle into the corresponding vertex in the second
rlm@94 441 triangle."
rlm@94 442 [#^Triangle tri-1 #^Triangle tri-2]
rlm@94 443 (.mult
rlm@94 444 (triangle->matrix4f tri-2)
rlm@94 445 (.invert (triangle->matrix4f tri-1))))
rlm@94 446
rlm@94 447 (def death (Triangle.
rlm@94 448 (Vector3f. 1 1 1)
rlm@94 449 (Vector3f. 1 2 3)
rlm@94 450 (Vector3f. 5 6 7)))
rlm@94 451
rlm@94 452 (def death-2 (Triangle.
rlm@94 453 (Vector3f. 2 2 2)
rlm@94 454 (Vector3f. 1 1 1)
rlm@94 455 (Vector3f. 0 1 0)))
rlm@94 456
rlm@94 457 (defn vector2f->vector3f [v]
rlm@94 458 (Vector3f. (.getX v) (.getY v) 0))
rlm@94 459
rlm@94 460
rlm@94 461 (extend-type Triangle
rlm@94 462 Textual
rlm@94 463 (text [t]
rlm@94 464 (println "Triangle: " \newline (.get1 t) \newline
rlm@94 465 (.get2 t) \newline (.get3 t))))
rlm@94 466
rlm@94 467
rlm@94 468 (defn map-triangle [f #^Triangle tri]
rlm@94 469 (Triangle.
rlm@94 470 (f 0 (.get1 tri))
rlm@94 471 (f 1 (.get2 tri))
rlm@94 472 (f 2 (.get3 tri))))
rlm@94 473
rlm@94 474 (defn triangle-seq [#^Triangle tri]
rlm@94 475 [(.get1 tri) (.get2 tri) (.get3 tri)])
rlm@94 476
rlm@94 477 (defn vector3f-seq [#^Vector3f v]
rlm@94 478 [(.getX v) (.getY v) (.getZ v)])
rlm@94 479
rlm@91 480 (defn inside-triangle?
rlm@94 481 "Is the point inside the triangle? Now what do we do?
rlm@94 482 You might want to hold on there"
rlm@94 483 {:author "God"}
rlm@94 484 [tri p]
rlm@94 485 (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]
rlm@94 486 (and
rlm@94 487 (same-side? vert-1 vert-2 vert-3 p)
rlm@94 488 (same-side? vert-2 vert-3 vert-1 p)
rlm@94 489 (same-side? vert-3 vert-1 vert-2 p))))
rlm@91 490
rlm@94 491 (defn uv-triangle
rlm@94 492 "Convert the mesh triangle into the cooresponding triangle in
rlm@94 493 UV-space. Z-component of these triangles is always zero."
rlm@94 494 [#^Mesh mesh #^Triangle tri]
rlm@94 495 (apply #(Triangle. %1 %2 %3)
rlm@94 496 (map vector2f->vector3f
rlm@94 497 (tri-uv-coord mesh tri))))
rlm@91 498
rlm@94 499 (defn pixel-triangle
rlm@94 500 "Convert the mesh triange into the corresponding triangle in
rlm@94 501 UV-pixel-space. Z compenent will be zero."
rlm@94 502 [#^Mesh mesh #^Triangle tri width height]
rlm@94 503 (map-triangle (fn [_ v]
rlm@94 504 (Vector3f. (* width (.getX v))
rlm@94 505 (* height (.getY v))
rlm@94 506 0))
rlm@94 507 (uv-triangle mesh tri)))
rlm@93 508
rlm@94 509 (defn triangle-bounds
rlm@94 510 "Dimensions of the bounding square of the triangle in the form
rlm@94 511 [x y width height].
rlm@94 512 Assumes that the triangle lies in the XY plane."
rlm@94 513 [#^Triangle tri]
rlm@94 514 (let [verts (map vector3f-seq (triangle-seq tri))
rlm@94 515 x (apply min (map first verts))
rlm@94 516 y (apply min (map second verts))]
rlm@93 517
rlm@94 518 [x y
rlm@94 519 (- (apply max (map first verts)) x)
rlm@94 520 (- (apply max (map second verts)) y)
rlm@94 521 ]))
rlm@93 522
rlm@93 523
rlm@94 524 (defn locate-tactile-sensors
rlm@94 525 "Search the geometry's tactile UV image for touch sensors, returning
rlm@94 526 their positions in geometry-relative coordinates."
rlm@94 527 [#^Geometry geo]
rlm@93 528
rlm@94 529 ;; inside-triangle? white-coordinates triangle-transformation
rlm@94 530 ;; tri-uv-coord touch-receptor-image
rlm@94 531 (let [mesh (.getMesh geo)
rlm@94 532 image (touch-receptor-image geo)
rlm@94 533 width (.getWidth image)
rlm@94 534 height (.getHeight image)
rlm@94 535 tris (triangles geo)
rlm@94 536
rlm@94 537 ;; for each triangle
rlm@94 538 sensor-coords
rlm@94 539 (fn [tri]
rlm@94 540 ;; translate triangle to uv-pixel-space
rlm@94 541 (let [uv-tri
rlm@94 542 (pixel-triangle mesh tri width height)
rlm@94 543 bounds (vec (triangle-bounds uv-tri))]
rlm@94 544
rlm@94 545 ;; get that part of the picture
rlm@94 546
rlm@94 547 (apply #(.setRoi image %1 %2 %3 %4) bounds)
rlm@94 548 (let [cutout (.crop (.getProcessor image))
rlm@94 549 ;; extract white pixels inside triangle
rlm@94 550 cutout-tri
rlm@94 551 (map-triangle
rlm@94 552 (fn [_ v]
rlm@94 553 (.subtract
rlm@94 554 v
rlm@94 555 (Vector3f. (bounds 0) (bounds 1) (float 0))))
rlm@94 556 uv-tri)
rlm@94 557 whites (filter (partial inside-triangle? cutout-tri)
rlm@94 558 (map vector2f->vector3f
rlm@94 559 (white-coordinates cutout)))
rlm@94 560 ;; translate pixel coordinates to world-space
rlm@94 561 transform (triangle-transformation cutout-tri tri)]
rlm@94 562 (map #(.mult transform %) whites))))]
rlm@94 563 (map sensor-coords tris)))
rlm@94 564
rlm@94 565
rlm@94 566
rlm@94 567
rlm@94 568
rlm@94 569
rlm@94 570
rlm@94 571
rlm@94 572
rlm@94 573
rlm@93 574
rlm@93 575
rlm@91 576
rlm@90 577 ;; for each triangle in the mesh,
rlm@90 578 ;; get the normal to the triangle,
rlm@90 579 ;; look at the UV touch map, restricted to that triangle,
rlm@90 580 ;; get the positions of those touch sensors in geometry-relative
rlm@90 581 ;; coordinates.
rlm@91 582 (defn tactile-coords [#^Geometry obj]
rlm@91 583 (let [mesh (.getMesh obj)
rlm@91 584 num-triangles (.getTriangleCount mesh)
rlm@91 585 num-verticies (.getVertexCount mesh)
rlm@91 586 uv-coord (partial uv-coord mesh)
rlm@91 587 triangle-indices (partial triangle-indices mesh)
rlm@91 588 receptors (touch-receptor-image obj)
rlm@93 589 tris (triangles obj)
rlm@91 590 ]
rlm@91 591 (map
rlm@91 592 (fn [[tri-1 tri-2 tri-3]]
rlm@91 593 (let [width (.getWidth receptors)
rlm@91 594 height (.getHeight receptors)
rlm@91 595 uv-1 (uv-coord tri-1)
rlm@91 596 uv-2 (uv-coord tri-2)
rlm@91 597 uv-3 (uv-coord tri-3)
rlm@91 598 x-coords (map #(.getX %) [uv-1 uv-2 uv-3])
rlm@91 599 y-coords (map #(.getY %) [uv-1 uv-2 uv-3])
rlm@91 600 max-x (Math/ceil (* width (apply max x-coords)))
rlm@91 601 min-x (Math/floor (* width (apply min x-coords)))
rlm@91 602 max-y (Math/ceil (* height (apply max y-coords)))
rlm@91 603 min-y (Math/floor (* height (apply min y-coords)))
rlm@91 604
rlm@91 605 image-1 (Vector2f. (* width (.getX uv-1))
rlm@91 606 (* height (.getY uv-1)))
rlm@91 607 image-2 (Vector2f. (* width (.getX uv-2))
rlm@91 608 (* height (.getY uv-2)))
rlm@91 609 image-3 (Vector2f. (* width (.getX uv-3))
rlm@91 610 (* height (.getY uv-3)))
rlm@91 611 left-corner
rlm@91 612 (Vector2f. min-x min-y)
rlm@91 613 ]
rlm@91 614
rlm@91 615 (.setRoi receptors min-x min-y (- max-x min-x) (- max-y min-y))
rlm@93 616 (let [processor (.crop (.getProcessor receptors))]
rlm@93 617 (map
rlm@93 618 #(.add left-corner %)
rlm@93 619
rlm@93 620 (filter
rlm@93 621 (partial
rlm@93 622 inside-triangle?
rlm@93 623 (.subtract image-1 left-corner)
rlm@93 624 (.subtract image-2 left-corner)
rlm@93 625 (.subtract image-3 left-corner))
rlm@93 626 (white-coordinates processor))))
rlm@91 627 )) (map triangle-indices (range num-triangles)))))
rlm@93 628
rlm@91 629
rlm@91 630
rlm@91 631
rlm@91 632
rlm@91 633
rlm@91 634
rlm@91 635 (defn all-names []
rlm@91 636 (concat
rlm@91 637 (re-split #"\n" (slurp (file-str
rlm@91 638 "/home/r/proj/names/dist.female.first")))
rlm@91 639 (re-split #"\n" (slurp (file-str
rlm@91 640 "/home/r/proj/names/dist.male.first")))
rlm@91 641 (re-split #"\n" (slurp (file-str
rlm@91 642 "/home/r/proj/names/dist.all.last")))))
rlm@90 643
rlm@90 644
rlm@90 645
rlm@90 646
rlm@90 647
rlm@90 648
rlm@90 649
rlm@90 650
rlm@90 651
rlm@90 652 (defrecord LulzLoader [])
rlm@90 653 (defprotocol Lulzable (load-lulz [this]))
rlm@90 654 (extend-type LulzLoader
rlm@90 655 Lulzable
rlm@90 656 (load-lulz [this] (println "the lulz have arrived!")))
rlm@90 657
rlm@78 658
rlm@78 659 (defn world-setup [joint]
rlm@90 660 (let [joint-position (Vector3f. 0 0 0)
rlm@83 661 joint-rotation
rlm@83 662 (.toRotationMatrix
rlm@83 663 (.mult
rlm@83 664 (doto (Quaternion.)
rlm@83 665 (.fromAngleAxis
rlm@83 666 (* 1 (/ Math/PI 4))
rlm@85 667 (Vector3f. -1 0 0)))
rlm@85 668 (doto (Quaternion.)
rlm@85 669 (.fromAngleAxis
rlm@85 670 (* 1 (/ Math/PI 2))
rlm@85 671 (Vector3f. 0 0 1)))))
rlm@84 672 top-position (.mult joint-rotation (Vector3f. 8 0 0))
rlm@83 673
rlm@83 674 origin (doto
rlm@83 675 (sphere 0.1 :physical? false :color ColorRGBA/Cyan
rlm@84 676 :position top-position))
rlm@83 677 top (doto
rlm@78 678 (sphere 0.1 :physical? false :color ColorRGBA/Yellow
rlm@84 679 :position top-position)
rlm@83 680
rlm@78 681 (.addControl
rlm@78 682 (RigidBodyControl.
rlm@83 683 (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))
rlm@78 684 bottom (doto
rlm@78 685 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
rlm@83 686 :position (Vector3f. 0 0 0))
rlm@83 687 (.addControl
rlm@78 688 (RigidBodyControl.
rlm@78 689 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
rlm@83 690 table (box 10 2 10 :position (Vector3f. 0 -20 0)
rlm@78 691 :color ColorRGBA/Gray :mass 0)
rlm@78 692 a (.getControl top RigidBodyControl)
rlm@78 693 b (.getControl bottom RigidBodyControl)]
rlm@83 694
rlm@78 695 (cond
rlm@83 696 (= joint :cone)
rlm@83 697
rlm@83 698 (doto (ConeJoint.
rlm@83 699 a b
rlm@87 700 (world-to-local top joint-position)
rlm@87 701 (world-to-local bottom joint-position)
rlm@83 702 joint-rotation
rlm@83 703 joint-rotation
rlm@83 704 )
rlm@78 705
rlm@83 706
rlm@83 707 (.setLimit (* (/ 10) Math/PI)
rlm@83 708 (* (/ 4) Math/PI)
rlm@83 709 0)))
rlm@83 710 [origin top bottom table]))
rlm@78 711
rlm@78 712 (defn test-joint [joint]
rlm@83 713 (let [[origin top bottom floor] (world-setup joint)
rlm@78 714 control (.getControl top RigidBodyControl)
rlm@78 715 move-up? (atom false)
rlm@78 716 move-down? (atom false)
rlm@78 717 move-left? (atom false)
rlm@78 718 move-right? (atom false)
rlm@78 719 roll-left? (atom false)
rlm@78 720 roll-right? (atom false)
rlm@78 721 timer (atom 0)]
rlm@78 722
rlm@78 723 (world
rlm@83 724 (nodify [top bottom floor origin])
rlm@78 725 (merge standard-debug-controls
rlm@78 726 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@78 727 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@78 728 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@78 729 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@78 730 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@78 731 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@78 732
rlm@78 733 (fn [world]
rlm@78 734 (light-up-everything world)
rlm@78 735 (enable-debug world)
rlm@78 736 (set-gravity world (Vector3f. 0 0 0))
rlm@78 737 )
rlm@78 738
rlm@78 739 (fn [world _]
rlm@78 740 (if (zero? (rem (swap! timer inc) 100))
rlm@78 741 (do
rlm@78 742 ;; (println-repl @timer)
rlm@78 743 (.attachChild (.getRootNode world)
rlm@78 744 (sphere 0.05 :color ColorRGBA/Yellow
rlm@78 745 :position (.getWorldTranslation top)
rlm@83 746 :physical? false))
rlm@83 747 (.attachChild (.getRootNode world)
rlm@83 748 (sphere 0.05 :color ColorRGBA/LightGray
rlm@83 749 :position (.getWorldTranslation bottom)
rlm@83 750 :physical? false))))
rlm@83 751
rlm@78 752 (if @move-up?
rlm@78 753 (.applyTorque control
rlm@78 754 (.mult (.getPhysicsRotation control)
rlm@78 755 (Vector3f. 0 0 10))))
rlm@78 756 (if @move-down?
rlm@78 757 (.applyTorque control
rlm@78 758 (.mult (.getPhysicsRotation control)
rlm@78 759 (Vector3f. 0 0 -10))))
rlm@78 760 (if @move-left?
rlm@78 761 (.applyTorque control
rlm@78 762 (.mult (.getPhysicsRotation control)
rlm@78 763 (Vector3f. 0 10 0))))
rlm@78 764 (if @move-right?
rlm@78 765 (.applyTorque control
rlm@78 766 (.mult (.getPhysicsRotation control)
rlm@78 767 (Vector3f. 0 -10 0))))
rlm@78 768 (if @roll-left?
rlm@78 769 (.applyTorque control
rlm@78 770 (.mult (.getPhysicsRotation control)
rlm@78 771 (Vector3f. -1 0 0))))
rlm@78 772 (if @roll-right?
rlm@78 773 (.applyTorque control
rlm@78 774 (.mult (.getPhysicsRotation control)
rlm@78 775 (Vector3f. 1 0 0))))))))
rlm@83 776
rlm@83 777
rlm@83 778
rlm@87 779 #+end_src
rlm@83 780
rlm@87 781 #+results: body-1
rlm@87 782 : #'cortex.silly/test-joint
rlm@78 783
rlm@78 784
rlm@78 785 * COMMENT purgatory
rlm@78 786 #+begin_src clojure
rlm@77 787 (defn bullet-trans []
rlm@77 788 (let [obj-a (sphere 0.5 :color ColorRGBA/Red
rlm@77 789 :position (Vector3f. -10 5 0))
rlm@77 790 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 791 :position (Vector3f. -10 -5 0)
rlm@77 792 :mass 0)
rlm@77 793 control-a (.getControl obj-a RigidBodyControl)
rlm@77 794 control-b (.getControl obj-b RigidBodyControl)
rlm@77 795 swivel
rlm@77 796 (.toRotationMatrix
rlm@77 797 (doto (Quaternion.)
rlm@77 798 (.fromAngleAxis (/ Math/PI 2)
rlm@77 799 Vector3f/UNIT_X)))]
rlm@77 800 (doto
rlm@77 801 (ConeJoint.
rlm@77 802 control-a control-b
rlm@77 803 (Vector3f. 0 5 0)
rlm@77 804 (Vector3f. 0 -5 0)
rlm@77 805 swivel swivel)
rlm@77 806 (.setLimit (* 0.6 (/ Math/PI 4))
rlm@77 807 (/ Math/PI 4)
rlm@77 808 (* Math/PI 0.8)))
rlm@77 809 (world (nodify
rlm@77 810 [obj-a obj-b])
rlm@77 811 standard-debug-controls
rlm@77 812 enable-debug
rlm@77 813 no-op)))
rlm@74 814
rlm@74 815
rlm@77 816 (defn bullet-trans* []
rlm@77 817 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
rlm@77 818 :position (Vector3f. 5 0 0)
rlm@77 819 :mass 90)
rlm@77 820 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 821 :position (Vector3f. -5 0 0)
rlm@77 822 :mass 0)
rlm@77 823 control-a (.getControl obj-a RigidBodyControl)
rlm@77 824 control-b (.getControl obj-b RigidBodyControl)
rlm@77 825 move-up? (atom nil)
rlm@77 826 move-down? (atom nil)
rlm@77 827 move-left? (atom nil)
rlm@77 828 move-right? (atom nil)
rlm@77 829 roll-left? (atom nil)
rlm@77 830 roll-right? (atom nil)
rlm@77 831 force 100
rlm@77 832 swivel
rlm@77 833 (.toRotationMatrix
rlm@77 834 (doto (Quaternion.)
rlm@77 835 (.fromAngleAxis (/ Math/PI 2)
rlm@77 836 Vector3f/UNIT_X)))
rlm@77 837 x-move
rlm@77 838 (doto (Matrix3f.)
rlm@77 839 (.fromStartEndVectors Vector3f/UNIT_X
rlm@77 840 (.normalize (Vector3f. 1 1 0))))
rlm@77 841
rlm@77 842 timer (atom 0)]
rlm@77 843 (doto
rlm@77 844 (ConeJoint.
rlm@77 845 control-a control-b
rlm@77 846 (Vector3f. -8 0 0)
rlm@77 847 (Vector3f. 2 0 0)
rlm@77 848 ;;swivel swivel
rlm@77 849 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
rlm@77 850 x-move Matrix3f/IDENTITY
rlm@77 851 )
rlm@77 852 (.setCollisionBetweenLinkedBodys false)
rlm@77 853 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
rlm@77 854 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
rlm@77 855 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
rlm@77 856 (world (nodify
rlm@77 857 [obj-a obj-b])
rlm@77 858 (merge standard-debug-controls
rlm@77 859 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@77 860 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@77 861 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@77 862 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@77 863 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@77 864 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@77 865
rlm@77 866 (fn [world]
rlm@77 867 (enable-debug world)
rlm@77 868 (set-gravity world Vector3f/ZERO)
rlm@77 869 )
rlm@77 870
rlm@77 871 (fn [world _]
rlm@77 872
rlm@77 873 (if @move-up?
rlm@77 874 (.applyForce control-a
rlm@77 875 (Vector3f. force 0 0)
rlm@77 876 (Vector3f. 0 0 0)))
rlm@77 877 (if @move-down?
rlm@77 878 (.applyForce control-a
rlm@77 879 (Vector3f. (- force) 0 0)
rlm@77 880 (Vector3f. 0 0 0)))
rlm@77 881 (if @move-left?
rlm@77 882 (.applyForce control-a
rlm@77 883 (Vector3f. 0 force 0)
rlm@77 884 (Vector3f. 0 0 0)))
rlm@77 885 (if @move-right?
rlm@77 886 (.applyForce control-a
rlm@77 887 (Vector3f. 0 (- force) 0)
rlm@77 888 (Vector3f. 0 0 0)))
rlm@77 889
rlm@77 890 (if @roll-left?
rlm@77 891 (.applyForce control-a
rlm@77 892 (Vector3f. 0 0 force)
rlm@77 893 (Vector3f. 0 0 0)))
rlm@77 894 (if @roll-right?
rlm@77 895 (.applyForce control-a
rlm@77 896 (Vector3f. 0 0 (- force))
rlm@77 897 (Vector3f. 0 0 0)))
rlm@77 898
rlm@77 899 (if (zero? (rem (swap! timer inc) 100))
rlm@77 900 (.attachChild
rlm@77 901 (.getRootNode world)
rlm@77 902 (sphere 0.05 :color ColorRGBA/Yellow
rlm@77 903 :physical? false :position
rlm@77 904 (.getWorldTranslation obj-a)))))
rlm@77 905 )
rlm@77 906 ))
rlm@77 907
rlm@77 908
rlm@77 909
rlm@73 910 #+end_src
rlm@73 911
rlm@73 912
rlm@73 913 * COMMENT generate source
rlm@73 914 #+begin_src clojure :tangle ../src/cortex/silly.clj
rlm@73 915 <<body-1>>
rlm@73 916 #+end_src
rlm@73 917
rlm@94 918
rlm@94 919
rlm@94 920
rlm@94 921
rlm@94 922 (defn transform-trianglesdsd
rlm@94 923 "Transform that converts each vertex in the first triangle
rlm@94 924 into the corresponding vertex in the second triangle."
rlm@94 925 [#^Triangle tri-1 #^Triangle tri-2]
rlm@94 926 (let [in [(.get1 tri-1)
rlm@94 927 (.get2 tri-1)
rlm@94 928 (.get3 tri-1)]
rlm@94 929 out [(.get1 tri-2)
rlm@94 930 (.get2 tri-2)
rlm@94 931 (.get3 tri-2)]]
rlm@94 932 (let [translate (doto (Matrix4f.) (.setTranslation (.negate (in 0))))
rlm@94 933 in* [(.mult translate (in 0))
rlm@94 934 (.mult translate (in 1))
rlm@94 935 (.mult translate (in 2))]
rlm@94 936 final-translation
rlm@94 937 (doto (Matrix4f.)
rlm@94 938 (.setTranslation (out 1)))
rlm@94 939
rlm@94 940 rotate-1
rlm@94 941 (doto (Matrix3f.)
rlm@94 942 (.fromStartEndVectors
rlm@94 943 (.normalize
rlm@94 944 (.subtract
rlm@94 945 (in* 1) (in* 0)))
rlm@94 946 (.normalize
rlm@94 947 (.subtract
rlm@94 948 (out 1) (out 0)))))
rlm@94 949 in** [(.mult rotate-1 (in* 0))
rlm@94 950 (.mult rotate-1 (in* 1))
rlm@94 951 (.mult rotate-1 (in* 2))]
rlm@94 952 scale-factor-1
rlm@94 953 (.mult
rlm@94 954 (.normalize
rlm@94 955 (.subtract
rlm@94 956 (out 1)
rlm@94 957 (out 0)))
rlm@94 958 (/ (.length
rlm@94 959 (.subtract (out 1)
rlm@94 960 (out 0)))
rlm@94 961 (.length
rlm@94 962 (.subtract (in** 1)
rlm@94 963 (in** 0)))))
rlm@94 964 scale-1 (doto (Matrix4f.) (.setScale scale-factor-1))
rlm@94 965 in*** [(.mult scale-1 (in** 0))
rlm@94 966 (.mult scale-1 (in** 1))
rlm@94 967 (.mult scale-1 (in** 2))]
rlm@94 968
rlm@94 969
rlm@94 970
rlm@94 971
rlm@94 972
rlm@94 973 ]
rlm@94 974
rlm@94 975 (dorun (map println in))
rlm@94 976 (println)
rlm@94 977 (dorun (map println in*))
rlm@94 978 (println)
rlm@94 979 (dorun (map println in**))
rlm@94 980 (println)
rlm@94 981 (dorun (map println in***))
rlm@94 982 (println)
rlm@94 983
rlm@94 984 )))
rlm@94 985
rlm@94 986
rlm@94 987
rlm@94 988
rlm@94 989
rlm@94 990
rlm@94 991
rlm@94 992
rlm@94 993
rlm@94 994
rlm@94 995
rlm@94 996
rlm@94 997
rlm@94 998
rlm@94 999
rlm@94 1000
rlm@94 1001
rlm@94 1002
rlm@94 1003 )
rlm@94 1004
rlm@94 1005