view org/skin.org @ 59:63951929fe44

cleaned up imports for skin
author Robert McIntyre <rlm@mit.edu>
date Sat, 19 Nov 2011 23:59:12 -0700
parents 25142dad240a
children 1381a6ebd08b
line wrap: on
line source
1 #+title: Simulated Sense of Touch
2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu
4 #+description: Simulated touch for AI research using JMonkeyEngine and clojure.
5 #+keywords: simulation, tactile sense, jMonkeyEngine3, clojure
6 #+SETUPFILE: ../../aurellem/org/setup.org
7 #+INCLUDE: ../../aurellem/org/level-0.org
10 * Touch
12 My creatures need to be able to feel their environments. The idea here
13 is to create thousands of small /touch receptors/ along the geometries
14 which make up the creature's body. The number of touch receptors in a
15 given area is determined by how complicated that area is, as
16 determined by the total number of triangles in that region. This way,
17 complicated regions like the hands/face, etc. get more touch receptors
18 than simpler areas of the body.
20 #+srcname: skin-main
21 #+begin_src clojure
22 (ns cortex.touch
23 "Simulate the sense of touch in jMonkeyEngine3. Enables any Geometry
24 to be outfitted with touch sensors with density proportional to the
25 density of triangles along the surface of the Geometry. Enables a
26 Geometry to know what parts of itself are touching nearby objects."
27 {:author "Robert McIntyre"}
28 (:use (cortex world util))
29 (:import com.jme3.scene.Geometry)
30 (:import com.jme3.collision.CollisionResults)
31 (:import (com.jme3.math Triangle Vector3f Ray)))
33 (defn triangles
34 "Return a sequence of all the Triangles which compose a given
35 Geometry."
36 [#^Geometry geom]
37 (let
38 [mesh (.getMesh geom)
39 triangles (transient [])]
40 (dorun
41 (for [n (range (.getTriangleCount mesh))]
42 (let [tri (Triangle.)]
43 (.getTriangle mesh n tri)
44 ;; (.calculateNormal tri)
45 ;; (.calculateCenter tri)
46 (conj! triangles tri))))
47 (persistent! triangles)))
49 (defn get-ray-origin
50 "Return the origin which a Ray would have to have to be in the exact
51 center of a particular Triangle in the Geometry in World
52 Coordinates."
53 [geom tri]
54 (let [new (Vector3f.)]
55 (.calculateCenter tri)
56 (.localToWorld geom (.getCenter tri) new) new))
58 (defn get-ray-direction
59 "Return the direction which a Ray would have to have to be in the
60 exact center of a particular Triangle in the Geometry, pointing
61 normal to the Triangle, in coordinates relative to the center of the
62 Triangle."
63 [geom tri]
64 (let [n+c (Vector3f.)]
65 (.calculateNormal tri)
66 (.calculateCenter tri)
67 (.localToWorld
68 geom
69 (.add (.getCenter tri) (.getNormal tri)) n+c)
70 (.subtract n+c (get-ray-origin geom tri))))
72 (defn normal-rays
73 "For each Triangle which comprises the Geometry, returns a Ray which
74 is centered on that Triangle, points outward in a normal direction,
75 and extends for =limit= distance."
76 [limit #^Geometry geom]
77 (vec
78 (map
79 (fn [tri]
80 (doto
81 (Ray. (get-ray-origin geom tri)
82 (get-ray-direction geom tri))
83 (.setLimit limit)))
84 (triangles geom))))
86 (defn touch-percieve
87 "Augment a Geometry with the sense of touch. Returns a sequence of
88 non-negative integers, one for each triangle, with the value of the
89 integer describing how many objects a ray of length =limit=, normal
90 to the triangle and originating from its center, encountered. The
91 Geometry itself is not counted among the results."
92 [limit geom node]
93 (let [normals (normal-rays limit geom)]
94 (doall
95 (for [ray normals]
96 (do
97 (let [results (CollisionResults.)]
98 (.collideWith node ray results)
99 (let [touch-objects
100 (set (filter #(not (= geom %))
101 (map #(.getGeometry %) results)))]
102 (count touch-objects))))))))
103 #+end_src
106 * Example
108 #+srcname: touch-test
109 #+begin_src clojure
110 (ns test.touch
111 (:use (cortex world util touch))
112 (:import
113 com.jme3.scene.shape.Sphere
114 com.jme3.math.ColorRGBA
115 com.jme3.math.Vector3f
116 com.jme3.material.RenderState$BlendMode
117 com.jme3.renderer.queue.RenderQueue$Bucket
118 com.jme3.scene.shape.Box
119 com.jme3.scene.Node
120 ))
124 (defn ray-origin-debug
125 [ray color]
126 (make-shape
127 (assoc base-shape
128 :shape (Sphere. 5 5 0.05)
129 :name "arrow"
130 :color color
131 :texture false
132 :physical? false
133 :position
134 (.getOrigin ray))))
136 (defn ray-debug [ray color]
137 (make-shape
138 (assoc
139 base-shape
140 :name "debug-ray"
141 :physical? false
142 :shape (com.jme3.scene.shape.Line.
143 (.getOrigin ray)
144 (.add
145 (.getOrigin ray)
146 (.mult (.getDirection ray)
147 (float (.getLimit ray))))))))
150 (defn contact-color [contacts]
151 (case contacts
152 0 ColorRGBA/Gray
153 1 ColorRGBA/Red
154 2 ColorRGBA/Green
155 3 ColorRGBA/Yellow
156 4 ColorRGBA/Orange
157 5 ColorRGBA/Red
158 6 ColorRGBA/Magenta
159 7 ColorRGBA/Pink
160 8 ColorRGBA/White))
162 (defn update-ray-debug [node ray contacts]
163 (let [origin (.getChild node 0)]
164 (.setLocalTranslation origin (.getOrigin ray))
165 (.setColor (.getMaterial origin) "Color" (contact-color contacts))))
167 (defn init-node
168 [debug-node rays]
169 (.detachAllChildren debug-node)
170 (dorun
171 (for [ray rays]
172 (do
173 (.attachChild
174 debug-node
175 (doto (Node.)
176 (.attachChild (ray-origin-debug ray ColorRGBA/Gray))
177 (.attachChild (ray-debug ray ColorRGBA/Gray))
178 ))))))
180 (defn manage-ray-debug-node [debug-node geom touch-data limit]
181 (let [rays (normal-rays limit geom)]
182 (if (not= (count (.getChildren debug-node)) (count touch-data))
183 (init-node debug-node rays))
184 (dorun
185 (for [n (range (count touch-data))]
186 (update-ray-debug
187 (.getChild debug-node n) (nth rays n) (nth touch-data n))))))
189 (defn transparent-sphere []
190 (doto
191 (make-shape
192 (merge base-shape
193 {:position (Vector3f. 0 2 0)
194 :name "the blob."
195 :material "Common/MatDefs/Misc/Unshaded.j3md"
196 :texture "Textures/purpleWisp.png"
197 :physical? true
198 :mass 70
199 :color ColorRGBA/Blue
200 :shape (Sphere. 10 10 1)}))
201 (-> (.getMaterial)
202 (.getAdditionalRenderState)
203 (.setBlendMode RenderState$BlendMode/Alpha))
204 (.setQueueBucket RenderQueue$Bucket/Transparent)))
206 (defn transparent-box []
207 (doto
208 (make-shape
209 (merge base-shape
210 {:position (Vector3f. 0 2 0)
211 :name "box"
212 :material "Common/MatDefs/Misc/Unshaded.j3md"
213 :texture "Textures/purpleWisp.png"
214 :physical? true
215 :mass 70
216 :color ColorRGBA/Blue
217 :shape (Box. 1 1 1)}))
218 (-> (.getMaterial)
219 (.getAdditionalRenderState)
220 (.setBlendMode RenderState$BlendMode/Alpha))
221 (.setQueueBucket RenderQueue$Bucket/Transparent)))
223 (defn transparent-floor []
224 (doto
225 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)
226 :material "Common/MatDefs/Misc/Unshaded.j3md"
227 :texture "Textures/redWisp.png"
228 :name "floor")
229 (-> (.getMaterial)
230 (.getAdditionalRenderState)
231 (.setBlendMode RenderState$BlendMode/Alpha))
232 (.setQueueBucket RenderQueue$Bucket/Transparent)))
234 (defn test-skin []
235 (let [b
236 ;;(transparent-box)
237 (transparent-sphere)
238 ;;(sphere)
239 f (transparent-floor)
240 debug-node (Node.)
241 node (doto (Node.) (.attachChild b) (.attachChild f))
242 root-node (doto (Node.) (.attachChild node)
243 (.attachChild debug-node))
244 ]
246 (world
247 root-node
248 {"key-return" (fire-cannon-ball node)}
249 (fn [world]
250 ;; (Capture/SimpleCaptureVideo
251 ;; world
252 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))
253 ;; (no-logging)
254 ;;(enable-debug world)
255 ;; (set-accuracy world (/ 1 60))
256 )
258 (fn [& _]
259 (let [sensitivity 0.2
260 touch-data (touch-percieve sensitivity b node)]
261 (manage-ray-debug-node debug-node b touch-data sensitivity)
262 )
263 (Thread/sleep 10)
264 ))))
267 #+end_src
273 * COMMENT code generation
274 #+begin_src clojure :tangle ../src/cortex/touch.clj
275 <<skin-main>>
276 #+end_src
278 #+begin_src clojure :tangle ../src/test/touch.clj
279 <<touch-test>>
280 #+end_src