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