Mercurial > cortex
comparison org/sense-util.org @ 181:0f1c7921d967
removed blender.org; got rid of magic constant in sense-utils.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 04 Feb 2012 07:31:08 -0700 |
parents | 9e6a30b8c99a |
children | f2552d61e8df |
comparison
equal
deleted
inserted
replaced
180:f1b078375484 | 181:0f1c7921d967 |
---|---|
12 (ns cortex.sense) | 12 (ns cortex.sense) |
13 (cortex.import/mega-import-jme3) | 13 (cortex.import/mega-import-jme3) |
14 (import ij.process.ImageProcessor) | 14 (import ij.process.ImageProcessor) |
15 (import java.awt.image.BufferedImage) | 15 (import java.awt.image.BufferedImage) |
16 (use 'cortex.util) | 16 (use 'cortex.util) |
17 (use 'cortex.world) | |
17 (import jme3tools.converters.ImageToAwt) | 18 (import jme3tools.converters.ImageToAwt) |
18 | 19 |
19 (defn meta-data [blender-node key] | 20 (defn meta-data |
21 "Get the meta-data for a node created with blender." | |
22 [blender-node key] | |
20 (if-let [data (.getUserData blender-node "properties")] | 23 (if-let [data (.getUserData blender-node "properties")] |
21 (.findValue data key) | 24 (.findValue data key) |
22 nil)) | 25 nil)) |
23 | 26 |
24 (defn closest-node | 27 (defn closest-node |
49 obj | 52 obj |
50 (proxy [AbstractControl] [] | 53 (proxy [AbstractControl] [] |
51 (controlUpdate [tpf] | 54 (controlUpdate [tpf] |
52 (let [total-rotation | 55 (let [total-rotation |
53 (.mult base-anti-rotation (.getWorldRotation obj))] | 56 (.mult base-anti-rotation (.getWorldRotation obj))] |
54 (.setLocation sense | 57 (.setLocation |
55 (.add | 58 sense |
56 (.mult total-rotation sense-offset) | 59 (.add |
57 (.getWorldTranslation obj))) | 60 (.mult total-rotation sense-offset) |
58 (.setRotation sense | 61 (.getWorldTranslation obj))) |
59 (.mult total-rotation initial-sense-rotation)))) | 62 (.setRotation |
63 sense | |
64 (.mult total-rotation initial-sense-rotation)))) | |
60 (controlRender [_ _]))))) | 65 (controlRender [_ _]))))) |
61 | 66 |
62 (def white -1) | 67 (def white 0xFFFFFF) |
63 | 68 |
69 (defn white? [rgb] | |
70 (= (bit-and white rgb) white)) | |
71 | |
64 (defn filter-pixels | 72 (defn filter-pixels |
65 "List the coordinates of all pixels matching pred, within the bounds | 73 "List the coordinates of all pixels matching pred, within the bounds |
66 provided. Bounds -> [x0 y0 width height]" | 74 provided. Bounds -> [x0 y0 width height]" |
67 {:author "Dylan Holmes"} | 75 {:author "Dylan Holmes"} |
68 ([pred #^BufferedImage image] | 76 ([pred #^BufferedImage image] |
78 x0 y0 []))) | 86 x0 y0 []))) |
79 | 87 |
80 (defn white-coordinates | 88 (defn white-coordinates |
81 "Coordinates of all the white pixels in a subset of the image." | 89 "Coordinates of all the white pixels in a subset of the image." |
82 ([#^BufferedImage image bounds] | 90 ([#^BufferedImage image bounds] |
83 (filter-pixels #(= % white) image bounds)) | 91 (filter-pixels white? image bounds)) |
84 ([#^BufferedImage image] | 92 ([#^BufferedImage image] |
85 (filter-pixels #(= % white) image))) | 93 (filter-pixels white? image))) |
86 | 94 |
87 (defn points->image | 95 (defn points->image |
88 "Take a sparse collection of points and visuliaze it as a | 96 "Take a sparse collection of points and visuliaze it as a |
89 BufferedImage." | 97 BufferedImage." |
90 | |
91 ;; TODO maybe parallelize this since it's easy | |
92 | |
93 [points] | 98 [points] |
94 (if (empty? points) | 99 (if (empty? points) |
95 (BufferedImage. 1 1 BufferedImage/TYPE_BYTE_BINARY) | 100 (BufferedImage. 1 1 BufferedImage/TYPE_BYTE_BINARY) |
96 (let [xs (vec (map first points)) | 101 (let [xs (vec (map first points)) |
97 ys (vec (map second points)) | 102 ys (vec (map second points)) |
106 y (range (.getHeight image))] | 111 y (range (.getHeight image))] |
107 (.setRGB image x y 0xFF0000))) | 112 (.setRGB image x y 0xFF0000))) |
108 (dorun | 113 (dorun |
109 (for [index (range (count points))] | 114 (for [index (range (count points))] |
110 (.setRGB image (- (xs index) x0) (- (ys index) y0) -1))) | 115 (.setRGB image (- (xs index) x0) (- (ys index) y0) -1))) |
111 | |
112 image))) | 116 image))) |
113 | 117 |
114 (defn average [coll] | 118 (defn average [coll] |
115 (/ (reduce + coll) (count coll))) | 119 (/ (reduce + coll) (count coll))) |
116 | 120 |
186 "Load an image as a BufferedImage using the asset-manager system." | 190 "Load an image as a BufferedImage using the asset-manager system." |
187 [asset-relative-path] | 191 [asset-relative-path] |
188 (ImageToAwt/convert | 192 (ImageToAwt/convert |
189 (.getImage (.loadTexture (asset-manager) asset-relative-path)) | 193 (.getImage (.loadTexture (asset-manager) asset-relative-path)) |
190 false false 0)) | 194 false false 0)) |
191 | 195 |
192 | 196 (defn jme-to-blender |
197 "Convert from JME coordinates to Blender coordinates" | |
198 [#^Vector3f in] | |
199 (Vector3f. (.getX in) | |
200 (- (.getZ in)) | |
201 (.getY in))) | |
202 | |
203 (defn blender-to-jme | |
204 "Convert from Blender coordinates to JME coordinates" | |
205 [#^Vector3f in] | |
206 (Vector3f. (.getX in) | |
207 (.getZ in) | |
208 (- (.getY in)))) | |
193 #+end_src | 209 #+end_src |
194 | 210 |
195 #+results: sense-util | 211 #+results: sense-util |
196 : #'cortex.sense/meta-data | 212 : #'cortex.sense/meta-data |
197 | 213 |