rlm@37
|
1 #+title: Simulated Sense of Touch
|
rlm@0
|
2 #+author: Robert McIntyre
|
rlm@0
|
3 #+email: rlm@mit.edu
|
rlm@37
|
4 #+description: Simulated touch for AI research using JMonkeyEngine and clojure.
|
rlm@37
|
5 #+keywords: simulation, tactile sense, jMonkeyEngine3, clojure
|
rlm@4
|
6 #+SETUPFILE: ../../aurellem/org/setup.org
|
rlm@4
|
7 #+INCLUDE: ../../aurellem/org/level-0.org
|
rlm@0
|
8
|
rlm@229
|
9
|
rlm@229
|
10
|
rlm@37
|
11 * Touch
|
rlm@0
|
12
|
rlm@226
|
13 Touch is critical to navigation and spatial reasoning and as such I
|
rlm@226
|
14 need a simulated version of it to give to my AI creatures.
|
rlm@0
|
15
|
rlm@228
|
16 However, touch in my virtual can not exactly correspond to human touch
|
rlm@228
|
17 because my creatures are made out of completely rigid segments that
|
rlm@228
|
18 don't deform like human skin.
|
rlm@228
|
19
|
rlm@228
|
20 Human skin has a wide array of touch sensors, each of which speciliaze
|
rlm@228
|
21 in detecting different vibrational modes and pressures. These sensors
|
rlm@228
|
22 can integrate a vast expanse of skin (i.e. your entire palm), or a
|
rlm@228
|
23 tiny patch of skin at the tip of your finger. The hairs of the skin
|
rlm@228
|
24 help detect objects before they even come into contact with the skin
|
rlm@228
|
25 proper.
|
rlm@228
|
26
|
rlm@228
|
27 Instead of measuring deformation or vibration, I surround each rigid
|
rlm@228
|
28 part with a plenitude of hair-like objects which do not interact with
|
rlm@228
|
29 the physical world. Physical objects can pass through them with no
|
rlm@228
|
30 effect. The hairs are able to measure contact with other objects, and
|
rlm@228
|
31 constantly report how much of their extent is covered. So, even though
|
rlm@228
|
32 the creature's body parts do not deform, the hairs create a margin
|
rlm@228
|
33 around those body parts which achieves a sense of touch which is a
|
rlm@228
|
34 hybrid between a human's sense of deformation and sense from hairs.
|
rlm@228
|
35
|
rlm@228
|
36 Implementing touch in jMonkeyEngine follows a different techinal route
|
rlm@228
|
37 than vision and hearing. Those two senses piggybacked off
|
rlm@228
|
38 jMonkeyEngine's 3D audio and video rendering subsystems. To simulate
|
rlm@228
|
39 Touch, I use jMonkeyEngine's physics system to execute many small
|
rlm@229
|
40 collision detections, one for each "hair". The placement of the
|
rlm@229
|
41 "hairs" is determined by a UV-mapped image which shows where each hair
|
rlm@229
|
42 should be on the 3D surface of the body.
|
rlm@228
|
43
|
rlm@229
|
44
|
rlm@229
|
45 * Defining Touch Meta-Data in Blender
|
rlm@229
|
46
|
rlm@229
|
47 Each geometry can have a single UV map which describes the position
|
rlm@229
|
48 and length of the "hairs" which will constitute its sense of
|
rlm@229
|
49 touch. This image path is stored under the "touch" key. The image
|
rlm@229
|
50 itself is grayscale, with black meaning a hair length of 0 (no hair is
|
rlm@229
|
51 present) and white meaning a hair length of =scale=, which is a float
|
rlm@229
|
52 stored under the key "scale". If the pixel is gray then the resultant
|
rlm@238
|
53 hair length is linearly interpolated between 0 and =scale=. I call
|
rlm@238
|
54 these "hairs" /feelers/.
|
rlm@229
|
55
|
rlm@231
|
56 #+name: meta-data
|
rlm@0
|
57 #+begin_src clojure
|
rlm@229
|
58 (defn tactile-sensor-profile
|
rlm@229
|
59 "Return the touch-sensor distribution image in BufferedImage format,
|
rlm@229
|
60 or nil if it does not exist."
|
rlm@229
|
61 [#^Geometry obj]
|
rlm@229
|
62 (if-let [image-path (meta-data obj "touch")]
|
rlm@229
|
63 (load-image image-path)))
|
rlm@233
|
64
|
rlm@233
|
65 (defn tactile-scale
|
rlm@233
|
66 "Return the maximum length of a hair. All hairs are scalled between
|
rlm@233
|
67 0.0 and this length, depending on their color. Black is 0, and
|
rlm@233
|
68 white is maximum length, and everything in between is scalled
|
rlm@233
|
69 linearlly. Default scale is 0.01 jMonkeyEngine units."
|
rlm@233
|
70 [#^Geometry obj]
|
rlm@233
|
71 (if-let [scale (meta-data obj "scale")]
|
rlm@233
|
72 scale 0.1))
|
rlm@228
|
73 #+end_src
|
rlm@156
|
74
|
rlm@229
|
75 ** TODO add image showing example touch-uv map
|
rlm@229
|
76 ** TODO add metadata display for worm
|
rlm@229
|
77
|
rlm@234
|
78
|
rlm@233
|
79 * Skin Creation
|
rlm@234
|
80 * TODO get the actual lengths for each hair
|
rlm@234
|
81
|
rlm@234
|
82 #+begin_src clojure
|
rlm@234
|
83 pixel-triangles
|
rlm@234
|
84 xyz-triangles
|
rlm@234
|
85 conversions (map triangles->affine-transform pixel-triangles
|
rlm@234
|
86 xyz-triangles)
|
rlm@234
|
87
|
rlm@234
|
88 #+end_src
|
rlm@234
|
89
|
rlm@238
|
90
|
rlm@238
|
91 =(touch-kernel)= generates the functions which implement the sense of
|
rlm@238
|
92 touch for a creature. These functions must do 6 things to obtain touch
|
rlm@238
|
93 data.
|
rlm@238
|
94
|
rlm@238
|
95 - Get the tactile profile image and scale paramaters which describe
|
rlm@238
|
96 the layout of feelers along the object's surface.
|
rlm@239
|
97 =(tactile-sensor-profile)=, =(tactile-scale)=
|
rlm@239
|
98
|
rlm@238
|
99 - Get the lengths of each feeler by analyzing the color of the
|
rlm@238
|
100 pixels in the tactile profile image.
|
rlm@239
|
101 NOT IMPLEMENTED YET
|
rlm@239
|
102
|
rlm@238
|
103 - Find the triangles which make up the mesh in pixel-space and in
|
rlm@238
|
104 world-space.
|
rlm@239
|
105 =(triangles)= =(pixel-triangles)=
|
rlm@239
|
106
|
rlm@239
|
107 - Find the coordinates of each pixel in pixel space. These
|
rlm@239
|
108 coordinates are used to make the touch-topology.
|
rlm@240
|
109 =(feeler-pixel-coords)=
|
rlm@239
|
110
|
rlm@238
|
111 - Find the coordinates of each pixel in world-space. These
|
rlm@240
|
112 coordinates are the origins of the feelers. =(feeler-origins)=
|
rlm@239
|
113
|
rlm@238
|
114 - Calculate the normals of the triangles in world space, and add
|
rlm@238
|
115 them to each of the origins of the feelers. These are the
|
rlm@238
|
116 normalized coordinates of the tips of the feelers.
|
rlm@240
|
117 For both of these, =(feeler-tips)=
|
rlm@239
|
118
|
rlm@238
|
119 - Generate some sort of topology for the sensors.
|
rlm@239
|
120 =(touch-topology)=
|
rlm@239
|
121
|
rlm@239
|
122 #+begin_src clojure
|
rlm@239
|
123
|
rlm@239
|
124
|
rlm@239
|
125
|
rlm@239
|
126
|
rlm@239
|
127 #+end_src
|
rlm@239
|
128
|
rlm@239
|
129
|
rlm@238
|
130
|
rlm@233
|
131 #+name: kernel
|
rlm@233
|
132 #+begin_src clojure
|
rlm@233
|
133 (in-ns 'cortex.touch)
|
rlm@233
|
134
|
rlm@239
|
135 (declare touch-topology feelers set-ray)
|
rlm@234
|
136
|
rlm@233
|
137 (defn touch-kernel
|
rlm@234
|
138 "Constructs a function which will return tactile sensory data from
|
rlm@234
|
139 'geo when called from inside a running simulation"
|
rlm@234
|
140 [#^Geometry geo]
|
rlm@234
|
141 (let [[ray-reference-origins
|
rlm@234
|
142 ray-reference-tips
|
rlm@239
|
143 ray-lengths] (feelers geo)
|
rlm@234
|
144 current-rays (map (fn [] (Ray.)) ray-reference-origins)
|
rlm@234
|
145 topology (touch-topology geo)]
|
rlm@234
|
146 (if (empty? ray-reference-origins) nil
|
rlm@234
|
147 (fn [node]
|
rlm@234
|
148 (let [transform (.getWorldMatrix geo)]
|
rlm@234
|
149 (dorun
|
rlm@234
|
150 (map (fn [ray ref-origin ref-tip length]
|
rlm@234
|
151 (set-ray ray transform ref-origin ref-tip length))
|
rlm@234
|
152 current-rays ray-reference-origins
|
rlm@234
|
153 ray-reference-tips ray-lengths))
|
rlm@234
|
154 (vector
|
rlm@234
|
155 topology
|
rlm@234
|
156 (vec
|
rlm@234
|
157 (for [ray current-rays]
|
rlm@234
|
158 (do
|
rlm@234
|
159 (let [results (CollisionResults.)]
|
rlm@234
|
160 (.collideWith node ray results)
|
rlm@234
|
161 (let [touch-objects
|
rlm@234
|
162 (filter #(not (= geo (.getGeometry %)))
|
rlm@234
|
163 results)]
|
rlm@234
|
164 [(if (empty? touch-objects)
|
rlm@234
|
165 (.getLimit ray)
|
rlm@234
|
166 (.getDistance (first touch-objects)))
|
rlm@234
|
167 (.getLimit ray)])))))))))))
|
rlm@234
|
168
|
rlm@234
|
169 (defn touch-kernel*
|
rlm@233
|
170 "Returns a function which returns tactile sensory data when called
|
rlm@233
|
171 inside a running simulation."
|
rlm@233
|
172 [#^Geometry geo]
|
rlm@233
|
173 (let [feeler-coords (feeler-coordinates geo)
|
rlm@233
|
174 tris (triangles geo)
|
rlm@233
|
175 limit (tactile-scale geo)]
|
rlm@233
|
176 (if (empty? (touch-topology geo))
|
rlm@233
|
177 nil
|
rlm@233
|
178 (fn [node]
|
rlm@233
|
179 (let [sensor-origins
|
rlm@233
|
180 (map
|
rlm@233
|
181 #(map (partial local-to-world geo) %)
|
rlm@233
|
182 feeler-coords)
|
rlm@233
|
183 triangle-normals
|
rlm@233
|
184 (map (partial get-ray-direction geo)
|
rlm@233
|
185 tris)
|
rlm@233
|
186 rays
|
rlm@233
|
187 (flatten
|
rlm@233
|
188 (map (fn [origins norm]
|
rlm@233
|
189 (map #(doto (Ray. % norm)
|
rlm@233
|
190 (.setLimit limit)) origins))
|
rlm@233
|
191 sensor-origins triangle-normals))]
|
rlm@233
|
192 (vector
|
rlm@233
|
193 (touch-topology geo)
|
rlm@233
|
194 (vec
|
rlm@233
|
195 (for [ray rays]
|
rlm@233
|
196 (do
|
rlm@233
|
197 (let [results (CollisionResults.)]
|
rlm@233
|
198 (.collideWith node ray results)
|
rlm@233
|
199 (let [touch-objects
|
rlm@233
|
200 (filter #(not (= geo (.getGeometry %)))
|
rlm@233
|
201 results)]
|
rlm@233
|
202 [(if (empty? touch-objects)
|
rlm@233
|
203 limit (.getDistance (first touch-objects)))
|
rlm@233
|
204 limit])))))))))))
|
rlm@233
|
205
|
rlm@233
|
206 (defn touch!
|
rlm@233
|
207 "Endow the creature with the sense of touch. Returns a sequence of
|
rlm@233
|
208 functions, one for each body part with a tactile-sensor-proile,
|
rlm@233
|
209 each of which when called returns sensory data for that body part."
|
rlm@233
|
210 [#^Node creature]
|
rlm@233
|
211 (filter
|
rlm@233
|
212 (comp not nil?)
|
rlm@233
|
213 (map touch-kernel
|
rlm@233
|
214 (filter #(isa? (class %) Geometry)
|
rlm@233
|
215 (node-seq creature)))))
|
rlm@233
|
216 #+end_src
|
rlm@233
|
217
|
rlm@238
|
218 * Sensor Related Functions
|
rlm@238
|
219
|
rlm@238
|
220 These functions analyze the touch-sensor-profile image convert the
|
rlm@238
|
221 location of each touch sensor from pixel coordinates to UV-coordinates
|
rlm@238
|
222 and XYZ-coordinates.
|
rlm@238
|
223
|
rlm@238
|
224 #+name: sensors
|
rlm@238
|
225 #+begin_src clojure
|
rlm@240
|
226 (in-ns 'cortex.touch)
|
rlm@240
|
227
|
rlm@240
|
228 (defn feeler-pixel-coords
|
rlm@239
|
229 "Returns the coordinates of the feelers in pixel space in lists, one
|
rlm@239
|
230 list for each triangle, ordered in the same way as (triangles) and
|
rlm@239
|
231 (pixel-triangles)."
|
rlm@239
|
232 [#^Geometry geo image]
|
rlm@240
|
233 (map
|
rlm@240
|
234 (fn [pixel-triangle]
|
rlm@240
|
235 (filter
|
rlm@240
|
236 (fn [coord]
|
rlm@240
|
237 (inside-triangle? (->triangle pixel-triangle)
|
rlm@240
|
238 (->vector3f coord)))
|
rlm@240
|
239 (white-coordinates image (convex-bounds pixel-triangle))))
|
rlm@240
|
240 (pixel-triangles geo image)))
|
rlm@239
|
241
|
rlm@240
|
242 (defn feeler-origins [#^Geometry geo image]
|
rlm@240
|
243 (let [transforms
|
rlm@240
|
244 (map #(triangles->affine-transform
|
rlm@240
|
245 (->triangle %1) (->triangle %2))
|
rlm@240
|
246 (pixel-triangles geo image)
|
rlm@240
|
247 (triangles geo))]
|
rlm@240
|
248 (mapcat (fn [transform coords]
|
rlm@240
|
249 (map #(.mult transform (->vector3f %)) coords))
|
rlm@240
|
250 transforms (feeler-pixel-coords geo image))))
|
rlm@239
|
251
|
rlm@240
|
252 (defn feeler-tips [#^Geometry geo image]
|
rlm@241
|
253 (let [origins (feeler-origins geo image)
|
rlm@241
|
254 normals
|
rlm@241
|
255 (map
|
rlm@241
|
256 (fn [triangle]
|
rlm@241
|
257 (.calculateNormal triangle)
|
rlm@241
|
258 (.clone (.getNormal triangle)))
|
rlm@241
|
259 (map ->triangle (triangles geo)))]
|
rlm@241
|
260 (map #(.add %1 %2) origins normals)))
|
rlm@241
|
261
|
rlm@241
|
262 (defn touch-topology [#^Geometry geo image]
|
rlm@241
|
263 (collapse (feeler-pixel-coords geo image)))
|
rlm@240
|
264
|
rlm@239
|
265
|
rlm@238
|
266 (defn sensors-in-triangle
|
rlm@238
|
267 "Locate the touch sensors in the triangle, returning a map of their
|
rlm@238
|
268 UV and geometry-relative coordinates."
|
rlm@238
|
269 [image mesh tri-index]
|
rlm@238
|
270 (let [width (.getWidth image)
|
rlm@238
|
271 height (.getHeight image)
|
rlm@238
|
272 UV-vertex-coords (triangle-UV-coord mesh width height tri-index)
|
rlm@238
|
273 bounds (convex-bounds UV-vertex-coords)
|
rlm@238
|
274
|
rlm@238
|
275 cutout-triangle (points->triangle UV-vertex-coords)
|
rlm@238
|
276 UV-sensor-coords
|
rlm@238
|
277 (filter (comp (partial inside-triangle? cutout-triangle)
|
rlm@238
|
278 (fn [[u v]] (Vector3f. u v 0)))
|
rlm@238
|
279 (white-coordinates image bounds))
|
rlm@238
|
280 UV->geometry (triangle-transformation
|
rlm@238
|
281 cutout-triangle
|
rlm@238
|
282 (mesh-triangle mesh tri-index))
|
rlm@238
|
283 geometry-sensor-coords
|
rlm@238
|
284 (map (fn [[u v]] (.mult UV->geometry (Vector3f. u v 0)))
|
rlm@238
|
285 UV-sensor-coords)]
|
rlm@238
|
286 {:UV UV-sensor-coords :geometry geometry-sensor-coords}))
|
rlm@238
|
287
|
rlm@238
|
288 (defn-memo locate-feelers
|
rlm@238
|
289 "Search the geometry's tactile UV profile for touch sensors,
|
rlm@238
|
290 returning their positions in geometry-relative coordinates."
|
rlm@238
|
291 [#^Geometry geo]
|
rlm@238
|
292 (let [mesh (.getMesh geo)
|
rlm@238
|
293 num-triangles (.getTriangleCount mesh)]
|
rlm@238
|
294 (if-let [image (tactile-sensor-profile geo)]
|
rlm@238
|
295 (map
|
rlm@238
|
296 (partial sensors-in-triangle image mesh)
|
rlm@238
|
297 (range num-triangles))
|
rlm@238
|
298 (repeat (.getTriangleCount mesh) {:UV nil :geometry nil}))))
|
rlm@238
|
299
|
rlm@238
|
300 (defn-memo touch-topology
|
rlm@238
|
301 "Return a sequence of vectors of the form [x y] describing the
|
rlm@238
|
302 \"topology\" of the tactile sensors. Points that are close together
|
rlm@238
|
303 in the touch-topology are generally close together in the simulation."
|
rlm@238
|
304 [#^Gemoetry geo]
|
rlm@238
|
305 (vec (collapse (reduce concat (map :UV (locate-feelers geo))))))
|
rlm@238
|
306
|
rlm@238
|
307 (defn-memo feeler-coordinates
|
rlm@238
|
308 "The location of the touch sensors in world-space coordinates."
|
rlm@238
|
309 [#^Geometry geo]
|
rlm@238
|
310 (vec (map :geometry (locate-feelers geo))))
|
rlm@238
|
311 #+end_src
|
rlm@238
|
312
|
rlm@238
|
313
|
rlm@238
|
314
|
rlm@238
|
315
|
rlm@233
|
316 * Visualizing Touch
|
rlm@233
|
317 #+name: visualization
|
rlm@233
|
318 #+begin_src clojure
|
rlm@233
|
319 (in-ns 'cortex.touch)
|
rlm@233
|
320
|
rlm@233
|
321 (defn touch->gray
|
rlm@233
|
322 "Convert a pair of [distance, max-distance] into a grayscale pixel"
|
rlm@233
|
323 [distance max-distance]
|
rlm@233
|
324 (gray
|
rlm@233
|
325 (- 255
|
rlm@233
|
326 (rem
|
rlm@233
|
327 (int
|
rlm@233
|
328 (* 255 (/ distance max-distance)))
|
rlm@233
|
329 256))))
|
rlm@233
|
330
|
rlm@233
|
331 (defn view-touch
|
rlm@233
|
332 "Creates a function which accepts a list of touch sensor-data and
|
rlm@233
|
333 displays each element to the screen."
|
rlm@233
|
334 []
|
rlm@233
|
335 (view-sense
|
rlm@233
|
336 (fn
|
rlm@233
|
337 [[coords sensor-data]]
|
rlm@233
|
338 (let [image (points->image coords)]
|
rlm@233
|
339 (dorun
|
rlm@233
|
340 (for [i (range (count coords))]
|
rlm@233
|
341 (.setRGB image ((coords i) 0) ((coords i) 1)
|
rlm@233
|
342 (apply touch->gray (sensor-data i)))))
|
rlm@233
|
343 image))))
|
rlm@233
|
344 #+end_src
|
rlm@233
|
345
|
rlm@233
|
346
|
rlm@233
|
347
|
rlm@228
|
348 * Triangle Manipulation Functions
|
rlm@228
|
349
|
rlm@229
|
350 The rigid bodies which make up a creature have an underlying
|
rlm@229
|
351 =Geometry=, which is a =Mesh= plus a =Material= and other important
|
rlm@229
|
352 data involved with displaying the body.
|
rlm@229
|
353
|
rlm@229
|
354 A =Mesh= is composed of =Triangles=, and each =Triangle= has three
|
rlm@229
|
355 verticies which have coordinates in XYZ space and UV space.
|
rlm@229
|
356
|
rlm@229
|
357 Here, =(triangles)= gets all the triangles which compose a mesh, and
|
rlm@229
|
358 =(triangle-UV-coord)= returns the the UV coordinates of the verticies
|
rlm@229
|
359 of a triangle.
|
rlm@229
|
360
|
rlm@231
|
361 #+name: triangles-1
|
rlm@228
|
362 #+begin_src clojure
|
rlm@239
|
363 (in-ns 'cortex.touch)
|
rlm@239
|
364
|
rlm@239
|
365 (defn vector3f-seq [#^Vector3f v]
|
rlm@239
|
366 [(.getX v) (.getY v) (.getZ v)])
|
rlm@239
|
367
|
rlm@239
|
368 (defn triangle-seq [#^Triangle tri]
|
rlm@239
|
369 [(vector3f-seq (.get1 tri))
|
rlm@239
|
370 (vector3f-seq (.get2 tri))
|
rlm@239
|
371 (vector3f-seq (.get3 tri))])
|
rlm@239
|
372
|
rlm@240
|
373 (defn ->vector3f
|
rlm@240
|
374 ([coords] (Vector3f. (nth coords 0 0)
|
rlm@240
|
375 (nth coords 1 0)
|
rlm@240
|
376 (nth coords 2 0))))
|
rlm@239
|
377
|
rlm@239
|
378 (defn ->triangle [points]
|
rlm@239
|
379 (apply #(Triangle. %1 %2 %3) (map ->vector3f points)))
|
rlm@239
|
380
|
rlm@239
|
381 (defn triangle
|
rlm@239
|
382 "Get the triangle specified by triangle-index from the mesh within
|
rlm@239
|
383 bounds."
|
rlm@239
|
384 [#^Geometry geo triangle-index]
|
rlm@239
|
385 (triangle-seq
|
rlm@239
|
386 (let [scratch (Triangle.)]
|
rlm@239
|
387 (.getTriangle (.getMesh geo) triangle-index scratch) scratch)))
|
rlm@239
|
388
|
rlm@228
|
389 (defn triangles
|
rlm@228
|
390 "Return a sequence of all the Triangles which compose a given
|
rlm@228
|
391 Geometry."
|
rlm@239
|
392 [#^Geometry geo]
|
rlm@239
|
393 (map (partial triangle geo) (range (.getTriangleCount (.getMesh geo)))))
|
rlm@228
|
394
|
rlm@228
|
395 (defn triangle-vertex-indices
|
rlm@228
|
396 "Get the triangle vertex indices of a given triangle from a given
|
rlm@228
|
397 mesh."
|
rlm@228
|
398 [#^Mesh mesh triangle-index]
|
rlm@228
|
399 (let [indices (int-array 3)]
|
rlm@228
|
400 (.getTriangle mesh triangle-index indices)
|
rlm@228
|
401 (vec indices)))
|
rlm@228
|
402
|
rlm@228
|
403 (defn vertex-UV-coord
|
rlm@228
|
404 "Get the UV-coordinates of the vertex named by vertex-index"
|
rlm@228
|
405 [#^Mesh mesh vertex-index]
|
rlm@228
|
406 (let [UV-buffer
|
rlm@228
|
407 (.getData
|
rlm@228
|
408 (.getBuffer
|
rlm@228
|
409 mesh
|
rlm@228
|
410 VertexBuffer$Type/TexCoord))]
|
rlm@228
|
411 [(.get UV-buffer (* vertex-index 2))
|
rlm@228
|
412 (.get UV-buffer (+ 1 (* vertex-index 2)))]))
|
rlm@228
|
413
|
rlm@239
|
414 (defn pixel-triangle [#^Geometry geo image index]
|
rlm@239
|
415 (let [mesh (.getMesh geo)
|
rlm@239
|
416 width (.getWidth image)
|
rlm@239
|
417 height (.getHeight image)]
|
rlm@239
|
418 (vec (map (fn [[u v]] (vector (* width u) (* height v)))
|
rlm@239
|
419 (map (partial vertex-UV-coord mesh)
|
rlm@239
|
420 (triangle-vertex-indices mesh index))))))
|
rlm@228
|
421
|
rlm@239
|
422 (defn pixel-triangles [#^Geometry geo image]
|
rlm@239
|
423 (let [height (.getHeight image)
|
rlm@239
|
424 width (.getWidth image)]
|
rlm@239
|
425 (map (partial pixel-triangle geo image)
|
rlm@239
|
426 (range (.getTriangleCount (.getMesh geo))))))
|
rlm@229
|
427
|
rlm@228
|
428 #+end_src
|
rlm@228
|
429
|
rlm@228
|
430 * Triangle Affine Transforms
|
rlm@228
|
431
|
rlm@229
|
432 The position of each hair is stored in a 2D image in UV
|
rlm@229
|
433 coordinates. To place the hair in 3D space we must convert from UV
|
rlm@229
|
434 coordinates to XYZ coordinates. Each =Triangle= has coordinates in
|
rlm@229
|
435 both UV-space and XYZ-space, which defines a unique [[http://mathworld.wolfram.com/AffineTransformation.html ][Affine Transform]]
|
rlm@229
|
436 for translating any coordinate within the UV triangle to the
|
rlm@229
|
437 cooresponding coordinate in the XYZ triangle.
|
rlm@229
|
438
|
rlm@231
|
439 #+name: triangles-3
|
rlm@228
|
440 #+begin_src clojure
|
rlm@228
|
441 (defn triangle->matrix4f
|
rlm@228
|
442 "Converts the triangle into a 4x4 matrix: The first three columns
|
rlm@228
|
443 contain the vertices of the triangle; the last contains the unit
|
rlm@228
|
444 normal of the triangle. The bottom row is filled with 1s."
|
rlm@228
|
445 [#^Triangle t]
|
rlm@228
|
446 (let [mat (Matrix4f.)
|
rlm@228
|
447 [vert-1 vert-2 vert-3]
|
rlm@228
|
448 ((comp vec map) #(.get t %) (range 3))
|
rlm@228
|
449 unit-normal (do (.calculateNormal t)(.getNormal t))
|
rlm@228
|
450 vertices [vert-1 vert-2 vert-3 unit-normal]]
|
rlm@228
|
451 (dorun
|
rlm@228
|
452 (for [row (range 4) col (range 3)]
|
rlm@228
|
453 (do
|
rlm@228
|
454 (.set mat col row (.get (vertices row)col))
|
rlm@228
|
455 (.set mat 3 row 1))))
|
rlm@228
|
456 mat))
|
rlm@228
|
457
|
rlm@240
|
458 (defn triangles->affine-transform
|
rlm@228
|
459 "Returns the affine transformation that converts each vertex in the
|
rlm@228
|
460 first triangle into the corresponding vertex in the second
|
rlm@228
|
461 triangle."
|
rlm@228
|
462 [#^Triangle tri-1 #^Triangle tri-2]
|
rlm@228
|
463 (.mult
|
rlm@228
|
464 (triangle->matrix4f tri-2)
|
rlm@228
|
465 (.invert (triangle->matrix4f tri-1))))
|
rlm@228
|
466 #+end_src
|
rlm@228
|
467
|
rlm@239
|
468
|
rlm@239
|
469 * Schrapnel Conversion Functions
|
rlm@239
|
470
|
rlm@239
|
471 It is convienent to treat a =Triangle= as a sequence of verticies, and
|
rlm@239
|
472 a =Vector2f= and =Vector3f= as a sequence of floats. These conversion
|
rlm@239
|
473 functions make this easy. If these classes implemented =Iterable= then
|
rlm@239
|
474 this code would not be necessary. Hopefully they will in the future.
|
rlm@239
|
475
|
rlm@239
|
476 #+name: triangles-2
|
rlm@239
|
477 #+begin_src clojure
|
rlm@239
|
478 (defn point->vector2f [[u v]]
|
rlm@239
|
479 (Vector2f. u v))
|
rlm@239
|
480
|
rlm@239
|
481 (defn vector2f->vector3f [v]
|
rlm@239
|
482 (Vector3f. (.getX v) (.getY v) 0))
|
rlm@239
|
483
|
rlm@239
|
484 (defn map-triangle [f #^Triangle tri]
|
rlm@239
|
485 (Triangle.
|
rlm@239
|
486 (f 0 (.get1 tri))
|
rlm@239
|
487 (f 1 (.get2 tri))
|
rlm@239
|
488 (f 2 (.get3 tri))))
|
rlm@239
|
489
|
rlm@239
|
490 (defn points->triangle
|
rlm@239
|
491 "Convert a list of points into a triangle."
|
rlm@239
|
492 [points]
|
rlm@239
|
493 (apply #(Triangle. %1 %2 %3)
|
rlm@239
|
494 (map (fn [point]
|
rlm@239
|
495 (let [point (vec point)]
|
rlm@239
|
496 (Vector3f. (get point 0 0)
|
rlm@239
|
497 (get point 1 0)
|
rlm@239
|
498 (get point 2 0))))
|
rlm@239
|
499 (take 3 points))))
|
rlm@239
|
500 #+end_src
|
rlm@239
|
501
|
rlm@239
|
502
|
rlm@229
|
503 * Triangle Boundaries
|
rlm@229
|
504
|
rlm@229
|
505 For efficiency's sake I will divide the UV-image into small squares
|
rlm@229
|
506 which inscribe each UV-triangle, then extract the points which lie
|
rlm@229
|
507 inside the triangle and map them to 3D-space using
|
rlm@229
|
508 =(triangle-transform)= above. To do this I need a function,
|
rlm@229
|
509 =(inside-triangle?)=, which determines whether a point is inside a
|
rlm@229
|
510 triangle in 2D UV-space.
|
rlm@228
|
511
|
rlm@231
|
512 #+name: triangles-4
|
rlm@228
|
513 #+begin_src clojure
|
rlm@229
|
514 (defn convex-bounds
|
rlm@229
|
515 "Returns the smallest square containing the given vertices, as a
|
rlm@229
|
516 vector of integers [left top width height]."
|
rlm@240
|
517 [verts]
|
rlm@240
|
518 (let [xs (map first verts)
|
rlm@240
|
519 ys (map second verts)
|
rlm@229
|
520 x0 (Math/floor (apply min xs))
|
rlm@229
|
521 y0 (Math/floor (apply min ys))
|
rlm@229
|
522 x1 (Math/ceil (apply max xs))
|
rlm@229
|
523 y1 (Math/ceil (apply max ys))]
|
rlm@229
|
524 [x0 y0 (- x1 x0) (- y1 y0)]))
|
rlm@229
|
525
|
rlm@229
|
526 (defn same-side?
|
rlm@229
|
527 "Given the points p1 and p2 and the reference point ref, is point p
|
rlm@229
|
528 on the same side of the line that goes through p1 and p2 as ref is?"
|
rlm@229
|
529 [p1 p2 ref p]
|
rlm@229
|
530 (<=
|
rlm@229
|
531 0
|
rlm@229
|
532 (.dot
|
rlm@229
|
533 (.cross (.subtract p2 p1) (.subtract p p1))
|
rlm@229
|
534 (.cross (.subtract p2 p1) (.subtract ref p1)))))
|
rlm@229
|
535
|
rlm@229
|
536 (defn inside-triangle?
|
rlm@229
|
537 "Is the point inside the triangle?"
|
rlm@229
|
538 {:author "Dylan Holmes"}
|
rlm@229
|
539 [#^Triangle tri #^Vector3f p]
|
rlm@240
|
540 (let [[vert-1 vert-2 vert-3] [(.get1 tri) (.get2 tri) (.get3 tri)]]
|
rlm@229
|
541 (and
|
rlm@229
|
542 (same-side? vert-1 vert-2 vert-3 p)
|
rlm@229
|
543 (same-side? vert-2 vert-3 vert-1 p)
|
rlm@229
|
544 (same-side? vert-3 vert-1 vert-2 p))))
|
rlm@229
|
545 #+end_src
|
rlm@229
|
546
|
rlm@240
|
547 #+results: triangles-4
|
rlm@240
|
548 : #'cortex.touch/inside-triangle?
|
rlm@240
|
549
|
rlm@229
|
550
|
rlm@228
|
551 * Physics Collision Objects
|
rlm@230
|
552
|
rlm@234
|
553 The "hairs" are actually =Rays= which extend from a point on a
|
rlm@230
|
554 =Triangle= in the =Mesh= normal to the =Triangle's= surface.
|
rlm@230
|
555
|
rlm@231
|
556 #+name: rays
|
rlm@228
|
557 #+begin_src clojure
|
rlm@228
|
558 (defn get-ray-origin
|
rlm@228
|
559 "Return the origin which a Ray would have to have to be in the exact
|
rlm@228
|
560 center of a particular Triangle in the Geometry in World
|
rlm@228
|
561 Coordinates."
|
rlm@228
|
562 [geom tri]
|
rlm@228
|
563 (let [new (Vector3f.)]
|
rlm@228
|
564 (.calculateCenter tri)
|
rlm@228
|
565 (.localToWorld geom (.getCenter tri) new) new))
|
rlm@228
|
566
|
rlm@228
|
567 (defn get-ray-direction
|
rlm@228
|
568 "Return the direction which a Ray would have to have to be to point
|
rlm@228
|
569 normal to the Triangle, in coordinates relative to the center of the
|
rlm@228
|
570 Triangle."
|
rlm@228
|
571 [geom tri]
|
rlm@228
|
572 (let [n+c (Vector3f.)]
|
rlm@228
|
573 (.calculateNormal tri)
|
rlm@228
|
574 (.calculateCenter tri)
|
rlm@228
|
575 (.localToWorld
|
rlm@228
|
576 geom
|
rlm@228
|
577 (.add (.getCenter tri) (.getNormal tri)) n+c)
|
rlm@228
|
578 (.subtract n+c (get-ray-origin geom tri))))
|
rlm@228
|
579 #+end_src
|
rlm@226
|
580 * Headers
|
rlm@231
|
581
|
rlm@231
|
582 #+name: touch-header
|
rlm@226
|
583 #+begin_src clojure
|
rlm@226
|
584 (ns cortex.touch
|
rlm@226
|
585 "Simulate the sense of touch in jMonkeyEngine3. Enables any Geometry
|
rlm@226
|
586 to be outfitted with touch sensors with density determined by a UV
|
rlm@226
|
587 image. In this way a Geometry can know what parts of itself are
|
rlm@226
|
588 touching nearby objects. Reads specially prepared blender files to
|
rlm@226
|
589 construct this sense automatically."
|
rlm@226
|
590 {:author "Robert McIntyre"}
|
rlm@226
|
591 (:use (cortex world util sense))
|
rlm@226
|
592 (:use clojure.contrib.def)
|
rlm@226
|
593 (:import (com.jme3.scene Geometry Node Mesh))
|
rlm@226
|
594 (:import com.jme3.collision.CollisionResults)
|
rlm@226
|
595 (:import com.jme3.scene.VertexBuffer$Type)
|
rlm@226
|
596 (:import (com.jme3.math Triangle Vector3f Vector2f Ray Matrix4f)))
|
rlm@226
|
597 #+end_src
|
rlm@37
|
598
|
rlm@232
|
599 * Adding Touch to the Worm
|
rlm@232
|
600
|
rlm@232
|
601 #+name: test-touch
|
rlm@232
|
602 #+begin_src clojure
|
rlm@232
|
603 (ns cortex.test.touch
|
rlm@232
|
604 (:use (cortex world util sense body touch))
|
rlm@232
|
605 (:use cortex.test.body))
|
rlm@232
|
606
|
rlm@232
|
607 (cortex.import/mega-import-jme3)
|
rlm@232
|
608
|
rlm@232
|
609 (defn test-touch []
|
rlm@232
|
610 (let [the-worm (doto (worm) (body!))
|
rlm@232
|
611 touch (touch! the-worm)
|
rlm@232
|
612 touch-display (view-touch)]
|
rlm@232
|
613 (world (nodify [the-worm (floor)])
|
rlm@232
|
614 standard-debug-controls
|
rlm@232
|
615
|
rlm@232
|
616 (fn [world]
|
rlm@232
|
617 (light-up-everything world))
|
rlm@232
|
618
|
rlm@232
|
619 (fn [world tpf]
|
rlm@232
|
620 (touch-display (map #(% (.getRootNode world)) touch))))))
|
rlm@232
|
621 #+end_src
|
rlm@228
|
622 * Source Listing
|
rlm@228
|
623 * Next
|
rlm@228
|
624
|
rlm@228
|
625
|
rlm@226
|
626 * COMMENT Code Generation
|
rlm@39
|
627 #+begin_src clojure :tangle ../src/cortex/touch.clj
|
rlm@231
|
628 <<touch-header>>
|
rlm@231
|
629 <<meta-data>>
|
rlm@231
|
630 <<triangles-1>>
|
rlm@231
|
631 <<triangles-2>>
|
rlm@231
|
632 <<triangles-3>>
|
rlm@231
|
633 <<triangles-4>>
|
rlm@231
|
634 <<sensors>>
|
rlm@231
|
635 <<rays>>
|
rlm@231
|
636 <<kernel>>
|
rlm@231
|
637 <<visualization>>
|
rlm@0
|
638 #+end_src
|
rlm@0
|
639
|
rlm@232
|
640
|
rlm@68
|
641 #+begin_src clojure :tangle ../src/cortex/test/touch.clj
|
rlm@232
|
642 <<test-touch>>
|
rlm@39
|
643 #+end_src
|
rlm@39
|
644
|
rlm@0
|
645
|
rlm@0
|
646
|
rlm@0
|
647
|
rlm@32
|
648
|
rlm@32
|
649
|
rlm@226
|
650
|