view org/depth-map.org @ 470:3401053124b0

integrating vision into thesis.
author Robert McIntyre <rlm@mit.edu>
date Fri, 28 Mar 2014 17:10:43 -0400
parents a86555b02916
children
line wrap: on
line source

2 #+begin_src clojure
3 (ns cortex.depth-map
4 "lol depth maps."
5 {:author "Robert McIntyre"}
6 (:use (cortex world util body sense vision touch))
7 (:import (com.jme3.scene Geometry Node Mesh))
8 (:import com.jme3.collision.CollisionResults)
9 (:import com.jme3.scene.VertexBuffer$Type)
11 (:import (com.jme3.math Triangle Vector3f Vector2f Ray Matrix4f)))
13 (cortex.import/mega-import-jme3)
15 (defn convert-pixel-to-ray
16 [camera ray x y]
17 (let [pixel-arrow
18 (.getWorldCoordinates
19 camera
20 (Vector2f. (float x) (float y))
21 ;; this is the "z depth" and can be anything, since
22 ;; we only care about direction
23 (float 1))
24 ;; now that we have the direction of a pixel, subtract
25 ray (Ray.)]
26 (set-ray ray Matrix4f/IDENTITY (.getLocation camera)
27 pixel-arrow) ray))
29 (defn generate-depth-map-points
30 "Generate a data structure representating the depth of the first
31 object to collide with a ray from the camera's current position."
32 [world camera]
33 (let [width (.getWidth camera)
34 height (.getHeight camera)
35 pixels (for [x (range width) y (range height)]
36 [x y])
37 temp-ray (Ray.)
38 depths
39 (for [[x y] pixels]
40 (let [ray (convert-pixel-to-ray camera temp-ray x y)
41 results (CollisionResults.)]
42 (.collideWith world ray results)
43 (if-let [closest (.getClosestCollision results)]
44 (.hashCode (.getGeometry closest)) 0)))]
46 (zipmap pixels depths)))
48 (defn test-world []
49 (nodify [(floor*) (brick-wall*)]))
51 (import com.aurellem.capture.RatchetTimer)
53 (defn depth-map-test []
54 (let [the-world (test-world)]
55 (world
56 the-world
57 ;;controls
58 standard-debug-controls
59 ;;init
60 (fn [world]
61 (let [timer (RatchetTimer. 60)]
62 (.setTimer world timer)
63 (display-dilated-time world timer)))
64 ;; update
65 no-op)))
67 #+end_src