annotate org/test-creature.org @ 104:ee302b65213b

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