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