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