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@110
|
426 "Return the touch-sensor distribution image in BufferedImage format,
|
rlm@110
|
427 or nil if it does not exist."
|
rlm@91
|
428 [#^Geometry obj]
|
rlm@110
|
429 (if-let [image-path (meta-data obj "touch")]
|
rlm@110
|
430 (ImageToAwt/convert
|
rlm@110
|
431 (.getImage
|
rlm@110
|
432 (.loadTexture
|
rlm@110
|
433 (asset-manager)
|
rlm@110
|
434 image-path))
|
rlm@110
|
435 false false 0)))
|
rlm@110
|
436
|
rlm@91
|
437 (import ij.process.ImageProcessor)
|
rlm@91
|
438 (import java.awt.image.BufferedImage)
|
rlm@91
|
439
|
rlm@92
|
440 (def white -1)
|
rlm@94
|
441
|
rlm@91
|
442 (defn filter-pixels
|
rlm@108
|
443 "List the coordinates of all pixels matching pred, within the bounds
|
rlm@108
|
444 provided. Bounds -> [x0 y0 width height]"
|
rlm@92
|
445 {:author "Dylan Holmes"}
|
rlm@108
|
446 ([pred #^BufferedImage image]
|
rlm@108
|
447 (filter-pixels pred image [0 0 (.getWidth image) (.getHeight image)]))
|
rlm@108
|
448 ([pred #^BufferedImage image [x0 y0 width height]]
|
rlm@108
|
449 ((fn accumulate [x y matches]
|
rlm@108
|
450 (cond
|
rlm@108
|
451 (>= y (+ height y0)) matches
|
rlm@108
|
452 (>= x (+ width x0)) (recur 0 (inc y) matches)
|
rlm@108
|
453 (pred (.getRGB image x y))
|
rlm@108
|
454 (recur (inc x) y (conj matches [x y]))
|
rlm@108
|
455 :else (recur (inc x) y matches)))
|
rlm@108
|
456 x0 y0 [])))
|
rlm@91
|
457
|
rlm@91
|
458 (defn white-coordinates
|
rlm@108
|
459 "Coordinates of all the white pixels in a subset of the image."
|
rlm@108
|
460 [#^BufferedImage image bounds]
|
rlm@108
|
461 (filter-pixels #(= % white) image bounds))
|
rlm@108
|
462
|
rlm@108
|
463 (defn triangle
|
rlm@108
|
464 "Get the triangle specified by triangle-index from the mesh"
|
rlm@108
|
465 [#^Mesh mesh triangle-index]
|
rlm@108
|
466 (let [scratch (Triangle.)]
|
rlm@108
|
467 (.getTriangle mesh triangle-index scratch)
|
rlm@108
|
468 scratch))
|
rlm@108
|
469
|
rlm@108
|
470 (defn triangle-vertex-indices
|
rlm@108
|
471 "Get the triangle vertex indices of a given triangle from a given
|
rlm@108
|
472 mesh."
|
rlm@108
|
473 [#^Mesh mesh triangle-index]
|
rlm@108
|
474 (let [indices (int-array 3)]
|
rlm@108
|
475 (.getTriangle mesh triangle-index indices)
|
rlm@108
|
476 (vec indices)))
|
rlm@108
|
477
|
rlm@108
|
478 (defn vertex-UV-coord
|
rlm@108
|
479 "Get the uv-coordinates of the vertex named by vertex-index"
|
rlm@108
|
480 [#^Mesh mesh vertex-index]
|
rlm@108
|
481 (let [UV-buffer
|
rlm@108
|
482 (.getData
|
rlm@108
|
483 (.getBuffer
|
rlm@108
|
484 mesh
|
rlm@108
|
485 VertexBuffer$Type/TexCoord))]
|
rlm@108
|
486 [(.get UV-buffer (* vertex-index 2))
|
rlm@108
|
487 (.get UV-buffer (+ 1 (* vertex-index 2)))]))
|
rlm@108
|
488
|
rlm@108
|
489 (defn triangle-UV-coord
|
rlm@108
|
490 "Get the uv-cooridnates of the triangle's verticies."
|
rlm@108
|
491 [#^Mesh mesh width height triangle-index]
|
rlm@108
|
492 (map (fn [[u v]] (vector (* width u) (* height v)))
|
rlm@108
|
493 (map (partial vertex-UV-coord mesh)
|
rlm@108
|
494 (triangle-vertex-indices mesh triangle-index))))
|
rlm@91
|
495
|
rlm@102
|
496 (defn same-side?
|
rlm@102
|
497 "Given the points p1 and p2 and the reference point ref, is point p
|
rlm@102
|
498 on the same side of the line that goes through p1 and p2 as ref is?"
|
rlm@102
|
499 [p1 p2 ref p]
|
rlm@91
|
500 (<=
|
rlm@91
|
501 0
|
rlm@91
|
502 (.dot
|
rlm@91
|
503 (.cross (.subtract p2 p1) (.subtract p p1))
|
rlm@91
|
504 (.cross (.subtract p2 p1) (.subtract ref p1)))))
|
rlm@91
|
505
|
rlm@108
|
506 (defn triangle-seq [#^Triangle tri]
|
rlm@108
|
507 [(.get1 tri) (.get2 tri) (.get3 tri)])
|
rlm@108
|
508
|
rlm@108
|
509 (defn vector3f-seq [#^Vector3f v]
|
rlm@108
|
510 [(.getX v) (.getY v) (.getZ v)])
|
rlm@108
|
511
|
rlm@108
|
512 (defn inside-triangle?
|
rlm@108
|
513 "Is the point inside the triangle?"
|
rlm@108
|
514 {:author "Dylan Holmes"}
|
rlm@108
|
515 [#^Triangle tri #^Vector3f p]
|
rlm@108
|
516 (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]
|
rlm@108
|
517 (and
|
rlm@108
|
518 (same-side? vert-1 vert-2 vert-3 p)
|
rlm@108
|
519 (same-side? vert-2 vert-3 vert-1 p)
|
rlm@108
|
520 (same-side? vert-3 vert-1 vert-2 p))))
|
rlm@108
|
521
|
rlm@94
|
522 (defn triangle->matrix4f
|
rlm@108
|
523 "Converts the triangle into a 4x4 matrix: The first three columns
|
rlm@108
|
524 contain the vertices of the triangle; the last contains the unit
|
rlm@108
|
525 normal of the triangle. The bottom row is filled with 1s."
|
rlm@94
|
526 [#^Triangle t]
|
rlm@94
|
527 (let [mat (Matrix4f.)
|
rlm@94
|
528 [vert-1 vert-2 vert-3]
|
rlm@94
|
529 ((comp vec map) #(.get t %) (range 3))
|
rlm@94
|
530 unit-normal (do (.calculateNormal t)(.getNormal t))
|
rlm@94
|
531 vertices [vert-1 vert-2 vert-3 unit-normal]]
|
rlm@94
|
532 (dorun
|
rlm@94
|
533 (for [row (range 4) col (range 3)]
|
rlm@94
|
534 (do
|
rlm@94
|
535 (.set mat col row (.get (vertices row)col))
|
rlm@94
|
536 (.set mat 3 row 1))))
|
rlm@94
|
537 mat))
|
rlm@94
|
538
|
rlm@94
|
539 (defn triangle-transformation
|
rlm@94
|
540 "Returns the affine transformation that converts each vertex in the
|
rlm@94
|
541 first triangle into the corresponding vertex in the second
|
rlm@94
|
542 triangle."
|
rlm@94
|
543 [#^Triangle tri-1 #^Triangle tri-2]
|
rlm@94
|
544 (.mult
|
rlm@94
|
545 (triangle->matrix4f tri-2)
|
rlm@94
|
546 (.invert (triangle->matrix4f tri-1))))
|
rlm@94
|
547
|
rlm@108
|
548 (defn point->vector2f [[u v]]
|
rlm@108
|
549 (Vector2f. u v))
|
rlm@94
|
550
|
rlm@94
|
551 (defn vector2f->vector3f [v]
|
rlm@94
|
552 (Vector3f. (.getX v) (.getY v) 0))
|
rlm@94
|
553
|
rlm@94
|
554 (defn map-triangle [f #^Triangle tri]
|
rlm@94
|
555 (Triangle.
|
rlm@94
|
556 (f 0 (.get1 tri))
|
rlm@94
|
557 (f 1 (.get2 tri))
|
rlm@94
|
558 (f 2 (.get3 tri))))
|
rlm@94
|
559
|
rlm@108
|
560 (defn points->triangle
|
rlm@108
|
561 "Convert a list of points into a triangle."
|
rlm@108
|
562 [points]
|
rlm@108
|
563 (apply #(Triangle. %1 %2 %3)
|
rlm@108
|
564 (map (fn [point]
|
rlm@108
|
565 (let [point (vec point)]
|
rlm@108
|
566 (Vector3f. (get point 0 0)
|
rlm@108
|
567 (get point 1 0)
|
rlm@108
|
568 (get point 2 0))))
|
rlm@108
|
569 (take 3 points))))
|
rlm@94
|
570
|
rlm@108
|
571 (defn convex-bounds
|
rlm@108
|
572 "Dimensions of the smallest integer bounding square of the list of
|
rlm@108
|
573 2D verticies in the form: [x y width height]."
|
rlm@108
|
574 [uv-verts]
|
rlm@108
|
575 (let [xs (map first uv-verts)
|
rlm@108
|
576 ys (map second uv-verts)
|
rlm@108
|
577 x0 (Math/floor (apply min xs))
|
rlm@108
|
578 y0 (Math/floor (apply min ys))
|
rlm@108
|
579 x1 (Math/ceil (apply max xs))
|
rlm@108
|
580 y1 (Math/ceil (apply max ys))]
|
rlm@108
|
581 [x0 y0 (- x1 x0) (- y1 y0)]))
|
rlm@93
|
582
|
rlm@106
|
583 (defn sensors-in-triangle
|
rlm@107
|
584 "Find the locations of the touch sensors within a triangle in both
|
rlm@107
|
585 UV and gemoetry relative coordinates."
|
rlm@107
|
586 [image mesh tri-index]
|
rlm@107
|
587 (let [width (.getWidth image)
|
rlm@108
|
588 height (.getHeight image)
|
rlm@108
|
589 UV-vertex-coords (triangle-UV-coord mesh width height tri-index)
|
rlm@108
|
590 bounds (convex-bounds UV-vertex-coords)
|
rlm@108
|
591
|
rlm@108
|
592 cutout-triangle (points->triangle UV-vertex-coords)
|
rlm@108
|
593 UV-sensor-coords
|
rlm@108
|
594 (filter (comp (partial inside-triangle? cutout-triangle)
|
rlm@108
|
595 (fn [[u v]] (Vector3f. u v 0)))
|
rlm@108
|
596 (white-coordinates image bounds))
|
rlm@108
|
597 UV->geometry (triangle-transformation
|
rlm@108
|
598 cutout-triangle
|
rlm@108
|
599 (triangle mesh tri-index))
|
rlm@108
|
600 geometry-sensor-coords
|
rlm@108
|
601 (map (fn [[u v]] (.mult UV->geometry (Vector3f. u v 0)))
|
rlm@108
|
602 UV-sensor-coords)]
|
rlm@108
|
603 {:UV UV-sensor-coords :geometry geometry-sensor-coords}))
|
rlm@107
|
604
|
rlm@108
|
605 (defn-memo locate-feelers
|
rlm@94
|
606 "Search the geometry's tactile UV image for touch sensors, returning
|
rlm@94
|
607 their positions in geometry-relative coordinates."
|
rlm@94
|
608 [#^Geometry geo]
|
rlm@108
|
609 (let [mesh (.getMesh geo)
|
rlm@108
|
610 num-triangles (.getTriangleCount mesh)]
|
rlm@108
|
611 (if-let [image (tactile-sensor-image geo)]
|
rlm@108
|
612 (map
|
rlm@108
|
613 (partial sensors-in-triangle image mesh)
|
rlm@108
|
614 (range num-triangles))
|
rlm@108
|
615 (repeat (.getTriangleCount mesh) {:UV nil :geometry nil}))))
|
rlm@102
|
616
|
rlm@102
|
617 (use 'clojure.contrib.def)
|
rlm@102
|
618
|
rlm@102
|
619 (defn-memo touch-topology [#^Gemoetry geo]
|
rlm@108
|
620 (vec (collapse (reduce concat (map :UV (locate-feelers geo))))))
|
rlm@108
|
621
|
rlm@108
|
622 (defn-memo feeler-coordinates [#^Geometry geo]
|
rlm@108
|
623 (vec (map :geometry (locate-feelers geo))))
|
rlm@102
|
624
|
rlm@97
|
625 (defn enable-touch [#^Geometry geo]
|
rlm@108
|
626 (let [feeler-coords (feeler-coordinates geo)
|
rlm@96
|
627 tris (triangles geo)
|
rlm@109
|
628 limit 0.1
|
rlm@109
|
629 ;;results (CollisionResults.)
|
rlm@109
|
630 ]
|
rlm@97
|
631 (fn [node]
|
rlm@97
|
632 (let [sensor-origins
|
rlm@97
|
633 (map
|
rlm@97
|
634 #(map (partial local-to-world geo) %)
|
rlm@97
|
635 feeler-coords)
|
rlm@97
|
636 triangle-normals
|
rlm@97
|
637 (map (partial get-ray-direction geo)
|
rlm@97
|
638 tris)
|
rlm@97
|
639 rays
|
rlm@97
|
640 (flatten
|
rlm@97
|
641 (map (fn [origins norm]
|
rlm@97
|
642 (map #(doto (Ray. % norm)
|
rlm@97
|
643 (.setLimit limit)) origins))
|
rlm@97
|
644 sensor-origins triangle-normals))]
|
rlm@102
|
645 (vector
|
rlm@102
|
646 (touch-topology geo)
|
rlm@102
|
647 (vec
|
rlm@102
|
648 (for [ray rays]
|
rlm@102
|
649 (do
|
rlm@102
|
650 (let [results (CollisionResults.)]
|
rlm@102
|
651 (.collideWith node ray results)
|
rlm@102
|
652 (let [touch-objects
|
rlm@102
|
653 (set
|
rlm@102
|
654 (filter #(not (= geo %))
|
rlm@102
|
655 (map #(.getGeometry %) results)))]
|
rlm@102
|
656 (if (> (count touch-objects) 0)
|
rlm@102
|
657 1 0)))))))))))
|
rlm@94
|
658
|
rlm@109
|
659
|
rlm@109
|
660
|
rlm@97
|
661 (defn touch [#^Node pieces]
|
rlm@102
|
662 (map enable-touch
|
rlm@102
|
663 (filter #(isa? (class %) Geometry)
|
rlm@102
|
664 (node-seq pieces))))
|
rlm@102
|
665
|
rlm@103
|
666 (defn debug-window
|
rlm@103
|
667 "creates function that offers a debug view of sensor data"
|
rlm@103
|
668 []
|
rlm@103
|
669 (let [vi (view-image)]
|
rlm@103
|
670 (fn
|
rlm@103
|
671 [[coords sensor-data]]
|
rlm@103
|
672 (let [image (points->image coords)]
|
rlm@103
|
673 (dorun
|
rlm@103
|
674 (for [i (range (count coords))]
|
rlm@103
|
675 (.setRGB image ((coords i) 0) ((coords i) 1)
|
rlm@103
|
676 ({0 -16777216
|
rlm@103
|
677 1 -1} (sensor-data i)))))
|
rlm@103
|
678 (vi image)))))
|
rlm@103
|
679
|
rlm@83
|
680
|
rlm@106
|
681 ;;(defn test-touch [world creature]
|
rlm@83
|
682
|
rlm@78
|
683
|
rlm@106
|
684 (defn test-creature [thing]
|
rlm@106
|
685 (let [x-axis
|
rlm@106
|
686 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
|
rlm@106
|
687 y-axis
|
rlm@106
|
688 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
|
rlm@106
|
689 z-axis
|
rlm@106
|
690 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
|
rlm@106
|
691 creature (blender-creature thing)
|
rlm@106
|
692 touch-nerves (touch creature)
|
rlm@106
|
693 touch-debug-windows (map (fn [_] (debug-window)) touch-nerves)
|
rlm@106
|
694 ]
|
rlm@106
|
695 (world
|
rlm@106
|
696 (nodify [creature
|
rlm@106
|
697 (box 10 2 10 :position (Vector3f. 0 -9 0)
|
rlm@106
|
698 :color ColorRGBA/Gray :mass 0)
|
rlm@106
|
699 x-axis y-axis z-axis
|
rlm@106
|
700 ])
|
rlm@106
|
701 standard-debug-controls
|
rlm@106
|
702 (fn [world]
|
rlm@106
|
703 (light-up-everything world)
|
rlm@106
|
704 (enable-debug world)
|
rlm@106
|
705 ;;(com.aurellem.capture.Capture/captureVideo
|
rlm@106
|
706 ;; world (file-str "/home/r/proj/ai-videos/hand"))
|
rlm@110
|
707 ;;(.setTimer world (RatchetTimer. 60))
|
rlm@110
|
708 ;;(speed-up world)
|
rlm@106
|
709 ;;(set-gravity world (Vector3f. 0 0 0))
|
rlm@106
|
710 )
|
rlm@106
|
711 (fn [world tpf]
|
rlm@109
|
712 ;;(dorun
|
rlm@109
|
713 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))
|
rlm@110
|
714
|
rlm@106
|
715 (dorun
|
rlm@109
|
716 (map #(%1 (%2 (.getRootNode world)))
|
rlm@110
|
717 touch-debug-windows touch-nerves)
|
rlm@110
|
718 )
|
rlm@109
|
719
|
rlm@106
|
720 )
|
rlm@106
|
721 ;;(let [timer (atom 0)]
|
rlm@106
|
722 ;; (fn [_ _]
|
rlm@106
|
723 ;; (swap! timer inc)
|
rlm@106
|
724 ;; (if (= (rem @timer 60) 0)
|
rlm@106
|
725 ;; (println-repl (float (/ @timer 60))))))
|
rlm@106
|
726 )))
|
rlm@83
|
727
|
rlm@109
|
728
|
rlm@109
|
729
|
rlm@109
|
730
|
rlm@109
|
731
|
rlm@109
|
732
|
rlm@109
|
733
|
rlm@109
|
734
|
rlm@109
|
735
|
rlm@109
|
736 ;;; experiments in collisions
|
rlm@109
|
737
|
rlm@109
|
738
|
rlm@109
|
739
|
rlm@109
|
740 (defn collision-test []
|
rlm@110
|
741 (let [b-radius 1
|
rlm@110
|
742 b-position (Vector3f. 0 0 0)
|
rlm@109
|
743 obj-b (box 1 1 1 :color ColorRGBA/Blue
|
rlm@109
|
744 :position b-position
|
rlm@110
|
745 :mass 0)
|
rlm@110
|
746 node (nodify [obj-b])
|
rlm@110
|
747 bounds-b
|
rlm@110
|
748 (doto (Picture.)
|
rlm@110
|
749 (.setHeight 50)
|
rlm@110
|
750 (.setWidth 50)
|
rlm@110
|
751 (.setImage (asset-manager)
|
rlm@110
|
752 "Models/creature1/hand.png"
|
rlm@110
|
753 false
|
rlm@110
|
754 ))
|
rlm@110
|
755
|
rlm@110
|
756 ;;(Ray. (Vector3f. 0 -5 0) (.normalize (Vector3f. 0 1 0)))
|
rlm@110
|
757
|
rlm@110
|
758 collisions
|
rlm@110
|
759 (let [cr (CollisionResults.)]
|
rlm@110
|
760 (.collideWith node bounds-b cr)
|
rlm@110
|
761 (println (map #(.getContactPoint %) cr))
|
rlm@110
|
762 cr)
|
rlm@110
|
763
|
rlm@110
|
764 ;;collision-points
|
rlm@110
|
765 ;;(map #(sphere 0.1 :position (.getContactPoint %))
|
rlm@110
|
766 ;; collisions)
|
rlm@110
|
767
|
rlm@110
|
768 ;;node (nodify (conj collision-points obj-b))
|
rlm@110
|
769
|
rlm@109
|
770 sim
|
rlm@109
|
771 (world node
|
rlm@110
|
772 {"key-space"
|
rlm@110
|
773 (fn [_ value]
|
rlm@110
|
774 (if value
|
rlm@110
|
775 (let [cr (CollisionResults.)]
|
rlm@110
|
776 (.collideWith node bounds-b cr)
|
rlm@110
|
777 (println-repl (map #(.getContactPoint %) cr))
|
rlm@110
|
778 cr)))}
|
rlm@109
|
779 no-op
|
rlm@109
|
780 no-op)
|
rlm@109
|
781
|
rlm@109
|
782 ]
|
rlm@110
|
783 sim
|
rlm@109
|
784
|
rlm@109
|
785 ))
|
rlm@109
|
786
|
rlm@109
|
787
|
rlm@109
|
788
|
rlm@109
|
789
|
rlm@87
|
790 #+end_src
|
rlm@83
|
791
|
rlm@87
|
792 #+results: body-1
|
rlm@109
|
793 : #'cortex.silly/test-creature
|
rlm@78
|
794
|
rlm@78
|
795
|
rlm@78
|
796 * COMMENT purgatory
|
rlm@78
|
797 #+begin_src clojure
|
rlm@77
|
798 (defn bullet-trans []
|
rlm@77
|
799 (let [obj-a (sphere 0.5 :color ColorRGBA/Red
|
rlm@77
|
800 :position (Vector3f. -10 5 0))
|
rlm@77
|
801 obj-b (sphere 0.5 :color ColorRGBA/Blue
|
rlm@77
|
802 :position (Vector3f. -10 -5 0)
|
rlm@77
|
803 :mass 0)
|
rlm@77
|
804 control-a (.getControl obj-a RigidBodyControl)
|
rlm@77
|
805 control-b (.getControl obj-b RigidBodyControl)
|
rlm@77
|
806 swivel
|
rlm@77
|
807 (.toRotationMatrix
|
rlm@77
|
808 (doto (Quaternion.)
|
rlm@77
|
809 (.fromAngleAxis (/ Math/PI 2)
|
rlm@77
|
810 Vector3f/UNIT_X)))]
|
rlm@77
|
811 (doto
|
rlm@77
|
812 (ConeJoint.
|
rlm@77
|
813 control-a control-b
|
rlm@77
|
814 (Vector3f. 0 5 0)
|
rlm@77
|
815 (Vector3f. 0 -5 0)
|
rlm@77
|
816 swivel swivel)
|
rlm@77
|
817 (.setLimit (* 0.6 (/ Math/PI 4))
|
rlm@77
|
818 (/ Math/PI 4)
|
rlm@77
|
819 (* Math/PI 0.8)))
|
rlm@77
|
820 (world (nodify
|
rlm@77
|
821 [obj-a obj-b])
|
rlm@77
|
822 standard-debug-controls
|
rlm@77
|
823 enable-debug
|
rlm@77
|
824 no-op)))
|
rlm@74
|
825
|
rlm@74
|
826
|
rlm@77
|
827 (defn bullet-trans* []
|
rlm@77
|
828 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
|
rlm@77
|
829 :position (Vector3f. 5 0 0)
|
rlm@77
|
830 :mass 90)
|
rlm@77
|
831 obj-b (sphere 0.5 :color ColorRGBA/Blue
|
rlm@77
|
832 :position (Vector3f. -5 0 0)
|
rlm@77
|
833 :mass 0)
|
rlm@77
|
834 control-a (.getControl obj-a RigidBodyControl)
|
rlm@77
|
835 control-b (.getControl obj-b RigidBodyControl)
|
rlm@77
|
836 move-up? (atom nil)
|
rlm@77
|
837 move-down? (atom nil)
|
rlm@77
|
838 move-left? (atom nil)
|
rlm@77
|
839 move-right? (atom nil)
|
rlm@77
|
840 roll-left? (atom nil)
|
rlm@77
|
841 roll-right? (atom nil)
|
rlm@77
|
842 force 100
|
rlm@77
|
843 swivel
|
rlm@77
|
844 (.toRotationMatrix
|
rlm@77
|
845 (doto (Quaternion.)
|
rlm@77
|
846 (.fromAngleAxis (/ Math/PI 2)
|
rlm@77
|
847 Vector3f/UNIT_X)))
|
rlm@77
|
848 x-move
|
rlm@77
|
849 (doto (Matrix3f.)
|
rlm@77
|
850 (.fromStartEndVectors Vector3f/UNIT_X
|
rlm@77
|
851 (.normalize (Vector3f. 1 1 0))))
|
rlm@77
|
852
|
rlm@77
|
853 timer (atom 0)]
|
rlm@77
|
854 (doto
|
rlm@77
|
855 (ConeJoint.
|
rlm@77
|
856 control-a control-b
|
rlm@77
|
857 (Vector3f. -8 0 0)
|
rlm@77
|
858 (Vector3f. 2 0 0)
|
rlm@77
|
859 ;;swivel swivel
|
rlm@77
|
860 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
|
rlm@77
|
861 x-move Matrix3f/IDENTITY
|
rlm@77
|
862 )
|
rlm@77
|
863 (.setCollisionBetweenLinkedBodys false)
|
rlm@77
|
864 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
|
rlm@77
|
865 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
|
rlm@77
|
866 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
|
rlm@77
|
867 (world (nodify
|
rlm@77
|
868 [obj-a obj-b])
|
rlm@77
|
869 (merge standard-debug-controls
|
rlm@77
|
870 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
|
rlm@77
|
871 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
|
rlm@77
|
872 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
|
rlm@77
|
873 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
|
rlm@77
|
874 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
|
rlm@77
|
875 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
|
rlm@77
|
876
|
rlm@77
|
877 (fn [world]
|
rlm@77
|
878 (enable-debug world)
|
rlm@77
|
879 (set-gravity world Vector3f/ZERO)
|
rlm@77
|
880 )
|
rlm@77
|
881
|
rlm@77
|
882 (fn [world _]
|
rlm@77
|
883
|
rlm@77
|
884 (if @move-up?
|
rlm@77
|
885 (.applyForce control-a
|
rlm@77
|
886 (Vector3f. force 0 0)
|
rlm@77
|
887 (Vector3f. 0 0 0)))
|
rlm@77
|
888 (if @move-down?
|
rlm@77
|
889 (.applyForce control-a
|
rlm@77
|
890 (Vector3f. (- force) 0 0)
|
rlm@77
|
891 (Vector3f. 0 0 0)))
|
rlm@77
|
892 (if @move-left?
|
rlm@77
|
893 (.applyForce control-a
|
rlm@77
|
894 (Vector3f. 0 force 0)
|
rlm@77
|
895 (Vector3f. 0 0 0)))
|
rlm@77
|
896 (if @move-right?
|
rlm@77
|
897 (.applyForce control-a
|
rlm@77
|
898 (Vector3f. 0 (- force) 0)
|
rlm@77
|
899 (Vector3f. 0 0 0)))
|
rlm@77
|
900
|
rlm@77
|
901 (if @roll-left?
|
rlm@77
|
902 (.applyForce control-a
|
rlm@77
|
903 (Vector3f. 0 0 force)
|
rlm@77
|
904 (Vector3f. 0 0 0)))
|
rlm@77
|
905 (if @roll-right?
|
rlm@77
|
906 (.applyForce control-a
|
rlm@77
|
907 (Vector3f. 0 0 (- force))
|
rlm@77
|
908 (Vector3f. 0 0 0)))
|
rlm@77
|
909
|
rlm@77
|
910 (if (zero? (rem (swap! timer inc) 100))
|
rlm@77
|
911 (.attachChild
|
rlm@77
|
912 (.getRootNode world)
|
rlm@77
|
913 (sphere 0.05 :color ColorRGBA/Yellow
|
rlm@77
|
914 :physical? false :position
|
rlm@77
|
915 (.getWorldTranslation obj-a)))))
|
rlm@77
|
916 )
|
rlm@77
|
917 ))
|
rlm@77
|
918
|
rlm@94
|
919 (defn transform-trianglesdsd
|
rlm@94
|
920 "Transform that converts each vertex in the first triangle
|
rlm@94
|
921 into the corresponding vertex in the second triangle."
|
rlm@94
|
922 [#^Triangle tri-1 #^Triangle tri-2]
|
rlm@94
|
923 (let [in [(.get1 tri-1)
|
rlm@94
|
924 (.get2 tri-1)
|
rlm@94
|
925 (.get3 tri-1)]
|
rlm@94
|
926 out [(.get1 tri-2)
|
rlm@94
|
927 (.get2 tri-2)
|
rlm@94
|
928 (.get3 tri-2)]]
|
rlm@94
|
929 (let [translate (doto (Matrix4f.) (.setTranslation (.negate (in 0))))
|
rlm@94
|
930 in* [(.mult translate (in 0))
|
rlm@94
|
931 (.mult translate (in 1))
|
rlm@94
|
932 (.mult translate (in 2))]
|
rlm@94
|
933 final-translation
|
rlm@94
|
934 (doto (Matrix4f.)
|
rlm@94
|
935 (.setTranslation (out 1)))
|
rlm@94
|
936
|
rlm@94
|
937 rotate-1
|
rlm@94
|
938 (doto (Matrix3f.)
|
rlm@94
|
939 (.fromStartEndVectors
|
rlm@94
|
940 (.normalize
|
rlm@94
|
941 (.subtract
|
rlm@94
|
942 (in* 1) (in* 0)))
|
rlm@94
|
943 (.normalize
|
rlm@94
|
944 (.subtract
|
rlm@94
|
945 (out 1) (out 0)))))
|
rlm@94
|
946 in** [(.mult rotate-1 (in* 0))
|
rlm@94
|
947 (.mult rotate-1 (in* 1))
|
rlm@94
|
948 (.mult rotate-1 (in* 2))]
|
rlm@94
|
949 scale-factor-1
|
rlm@94
|
950 (.mult
|
rlm@94
|
951 (.normalize
|
rlm@94
|
952 (.subtract
|
rlm@94
|
953 (out 1)
|
rlm@94
|
954 (out 0)))
|
rlm@94
|
955 (/ (.length
|
rlm@94
|
956 (.subtract (out 1)
|
rlm@94
|
957 (out 0)))
|
rlm@94
|
958 (.length
|
rlm@94
|
959 (.subtract (in** 1)
|
rlm@94
|
960 (in** 0)))))
|
rlm@94
|
961 scale-1 (doto (Matrix4f.) (.setScale scale-factor-1))
|
rlm@94
|
962 in*** [(.mult scale-1 (in** 0))
|
rlm@94
|
963 (.mult scale-1 (in** 1))
|
rlm@94
|
964 (.mult scale-1 (in** 2))]
|
rlm@94
|
965
|
rlm@94
|
966
|
rlm@94
|
967
|
rlm@94
|
968
|
rlm@94
|
969
|
rlm@94
|
970 ]
|
rlm@94
|
971
|
rlm@94
|
972 (dorun (map println in))
|
rlm@94
|
973 (println)
|
rlm@94
|
974 (dorun (map println in*))
|
rlm@94
|
975 (println)
|
rlm@94
|
976 (dorun (map println in**))
|
rlm@94
|
977 (println)
|
rlm@94
|
978 (dorun (map println in***))
|
rlm@94
|
979 (println)
|
rlm@94
|
980
|
rlm@99
|
981 ))))
|
rlm@94
|
982
|
rlm@94
|
983
|
rlm@106
|
984 (defn world-setup [joint]
|
rlm@106
|
985 (let [joint-position (Vector3f. 0 0 0)
|
rlm@106
|
986 joint-rotation
|
rlm@106
|
987 (.toRotationMatrix
|
rlm@106
|
988 (.mult
|
rlm@106
|
989 (doto (Quaternion.)
|
rlm@106
|
990 (.fromAngleAxis
|
rlm@106
|
991 (* 1 (/ Math/PI 4))
|
rlm@106
|
992 (Vector3f. -1 0 0)))
|
rlm@106
|
993 (doto (Quaternion.)
|
rlm@106
|
994 (.fromAngleAxis
|
rlm@106
|
995 (* 1 (/ Math/PI 2))
|
rlm@106
|
996 (Vector3f. 0 0 1)))))
|
rlm@106
|
997 top-position (.mult joint-rotation (Vector3f. 8 0 0))
|
rlm@106
|
998
|
rlm@106
|
999 origin (doto
|
rlm@106
|
1000 (sphere 0.1 :physical? false :color ColorRGBA/Cyan
|
rlm@106
|
1001 :position top-position))
|
rlm@106
|
1002 top (doto
|
rlm@106
|
1003 (sphere 0.1 :physical? false :color ColorRGBA/Yellow
|
rlm@106
|
1004 :position top-position)
|
rlm@106
|
1005
|
rlm@106
|
1006 (.addControl
|
rlm@106
|
1007 (RigidBodyControl.
|
rlm@106
|
1008 (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))
|
rlm@106
|
1009 bottom (doto
|
rlm@106
|
1010 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
|
rlm@106
|
1011 :position (Vector3f. 0 0 0))
|
rlm@106
|
1012 (.addControl
|
rlm@106
|
1013 (RigidBodyControl.
|
rlm@106
|
1014 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
|
rlm@106
|
1015 table (box 10 2 10 :position (Vector3f. 0 -20 0)
|
rlm@106
|
1016 :color ColorRGBA/Gray :mass 0)
|
rlm@106
|
1017 a (.getControl top RigidBodyControl)
|
rlm@106
|
1018 b (.getControl bottom RigidBodyControl)]
|
rlm@106
|
1019
|
rlm@106
|
1020 (cond
|
rlm@106
|
1021 (= joint :cone)
|
rlm@106
|
1022
|
rlm@106
|
1023 (doto (ConeJoint.
|
rlm@106
|
1024 a b
|
rlm@106
|
1025 (world-to-local top joint-position)
|
rlm@106
|
1026 (world-to-local bottom joint-position)
|
rlm@106
|
1027 joint-rotation
|
rlm@106
|
1028 joint-rotation
|
rlm@106
|
1029 )
|
rlm@106
|
1030
|
rlm@106
|
1031
|
rlm@106
|
1032 (.setLimit (* (/ 10) Math/PI)
|
rlm@106
|
1033 (* (/ 4) Math/PI)
|
rlm@106
|
1034 0)))
|
rlm@106
|
1035 [origin top bottom table]))
|
rlm@106
|
1036
|
rlm@106
|
1037 (defn test-joint [joint]
|
rlm@106
|
1038 (let [[origin top bottom floor] (world-setup joint)
|
rlm@106
|
1039 control (.getControl top RigidBodyControl)
|
rlm@106
|
1040 move-up? (atom false)
|
rlm@106
|
1041 move-down? (atom false)
|
rlm@106
|
1042 move-left? (atom false)
|
rlm@106
|
1043 move-right? (atom false)
|
rlm@106
|
1044 roll-left? (atom false)
|
rlm@106
|
1045 roll-right? (atom false)
|
rlm@106
|
1046 timer (atom 0)]
|
rlm@106
|
1047
|
rlm@106
|
1048 (world
|
rlm@106
|
1049 (nodify [top bottom floor origin])
|
rlm@106
|
1050 (merge standard-debug-controls
|
rlm@106
|
1051 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
|
rlm@106
|
1052 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
|
rlm@106
|
1053 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
|
rlm@106
|
1054 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
|
rlm@106
|
1055 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
|
rlm@106
|
1056 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
|
rlm@106
|
1057
|
rlm@106
|
1058 (fn [world]
|
rlm@106
|
1059 (light-up-everything world)
|
rlm@106
|
1060 (enable-debug world)
|
rlm@106
|
1061 (set-gravity world (Vector3f. 0 0 0))
|
rlm@106
|
1062 )
|
rlm@106
|
1063
|
rlm@106
|
1064 (fn [world _]
|
rlm@106
|
1065 (if (zero? (rem (swap! timer inc) 100))
|
rlm@106
|
1066 (do
|
rlm@106
|
1067 ;; (println-repl @timer)
|
rlm@106
|
1068 (.attachChild (.getRootNode world)
|
rlm@106
|
1069 (sphere 0.05 :color ColorRGBA/Yellow
|
rlm@106
|
1070 :position (.getWorldTranslation top)
|
rlm@106
|
1071 :physical? false))
|
rlm@106
|
1072 (.attachChild (.getRootNode world)
|
rlm@106
|
1073 (sphere 0.05 :color ColorRGBA/LightGray
|
rlm@106
|
1074 :position (.getWorldTranslation bottom)
|
rlm@106
|
1075 :physical? false))))
|
rlm@106
|
1076
|
rlm@106
|
1077 (if @move-up?
|
rlm@106
|
1078 (.applyTorque control
|
rlm@106
|
1079 (.mult (.getPhysicsRotation control)
|
rlm@106
|
1080 (Vector3f. 0 0 10))))
|
rlm@106
|
1081 (if @move-down?
|
rlm@106
|
1082 (.applyTorque control
|
rlm@106
|
1083 (.mult (.getPhysicsRotation control)
|
rlm@106
|
1084 (Vector3f. 0 0 -10))))
|
rlm@106
|
1085 (if @move-left?
|
rlm@106
|
1086 (.applyTorque control
|
rlm@106
|
1087 (.mult (.getPhysicsRotation control)
|
rlm@106
|
1088 (Vector3f. 0 10 0))))
|
rlm@106
|
1089 (if @move-right?
|
rlm@106
|
1090 (.applyTorque control
|
rlm@106
|
1091 (.mult (.getPhysicsRotation control)
|
rlm@106
|
1092 (Vector3f. 0 -10 0))))
|
rlm@106
|
1093 (if @roll-left?
|
rlm@106
|
1094 (.applyTorque control
|
rlm@106
|
1095 (.mult (.getPhysicsRotation control)
|
rlm@106
|
1096 (Vector3f. -1 0 0))))
|
rlm@106
|
1097 (if @roll-right?
|
rlm@106
|
1098 (.applyTorque control
|
rlm@106
|
1099 (.mult (.getPhysicsRotation control)
|
rlm@106
|
1100 (Vector3f. 1 0 0))))))))
|
rlm@106
|
1101
|
rlm@99
|
1102
|
rlm@99
|
1103
|
rlm@107
|
1104 (defprotocol Frame
|
rlm@107
|
1105 (frame [this]))
|
rlm@107
|
1106
|
rlm@107
|
1107 (extend-type BufferedImage
|
rlm@107
|
1108 Frame
|
rlm@107
|
1109 (frame [image]
|
rlm@107
|
1110 (merge
|
rlm@107
|
1111 (apply
|
rlm@107
|
1112 hash-map
|
rlm@107
|
1113 (interleave
|
rlm@107
|
1114 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
|
rlm@107
|
1115 (vector x y)))
|
rlm@107
|
1116 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
|
rlm@107
|
1117 (let [data (.getRGB image x y)]
|
rlm@107
|
1118 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)
|
rlm@107
|
1119 :g (bit-shift-right (bit-and 0x00ff00 data) 8)
|
rlm@107
|
1120 :b (bit-and 0x0000ff data)))))))
|
rlm@107
|
1121 {:width (.getWidth image) :height (.getHeight image)})))
|
rlm@107
|
1122
|
rlm@107
|
1123
|
rlm@107
|
1124 (extend-type ImagePlus
|
rlm@107
|
1125 Frame
|
rlm@107
|
1126 (frame [image+]
|
rlm@107
|
1127 (frame (.getBufferedImage image+))))
|
rlm@107
|
1128
|
rlm@107
|
1129
|
rlm@99
|
1130 #+end_src
|
rlm@99
|
1131
|
rlm@99
|
1132
|
rlm@99
|
1133 * COMMENT generate source
|
rlm@99
|
1134 #+begin_src clojure :tangle ../src/cortex/silly.clj
|
rlm@99
|
1135 <<body-1>>
|
rlm@99
|
1136 #+end_src
|
rlm@99
|
1137
|
rlm@99
|
1138
|
rlm@94
|
1139
|
rlm@94
|
1140
|