changeset 94:69174ed0f9f6

Working sensor coordinate code. Dylan helped!
author Robert McIntyre <rlm@mit.edu>
date Tue, 10 Jan 2012 08:38:04 -0700
parents 7b739503836a
children e4bcd0c481ba
files assets/Models/creature1/tip.png assets/Models/creature1/try-again.blend org/test-creature.org
diffstat 3 files changed, 238 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
     1.1 Binary file assets/Models/creature1/tip.png has changed
     2.1 Binary file assets/Models/creature1/try-again.blend has changed
     3.1 --- a/org/test-creature.org	Mon Jan 09 19:14:47 2012 -0700
     3.2 +++ b/org/test-creature.org	Tue Jan 10 08:38:04 2012 -0700
     3.3 @@ -384,8 +384,9 @@
     3.4    (frame [image+]
     3.5      (frame (.getBufferedImage image+))))
     3.6  
     3.7 +
     3.8  (def white -1)
     3.9 -
    3.10 +  
    3.11  (defn filter-pixels
    3.12    "List the coordinates of all pixels matching pred."
    3.13    {:author "Dylan Holmes"}
    3.14 @@ -414,38 +415,162 @@
    3.15      (.cross (.subtract p2 p1) (.subtract p p1))
    3.16      (.cross (.subtract p2 p1) (.subtract ref p1)))))
    3.17  
    3.18 +
    3.19 +(defn triangle->matrix4f
    3.20 +  "Converts the triangle into a 4x4 matrix of vertices: The first
    3.21 +   three columns contain the vertices of the triangle; the last
    3.22 +   contains the unit normal of the triangle. The bottom row is filled
    3.23 +   with 1s."
    3.24 +  [#^Triangle t]
    3.25 +  (let [mat (Matrix4f.)
    3.26 +        [vert-1 vert-2 vert-3]
    3.27 +        ((comp vec map) #(.get t %) (range 3))
    3.28 +        unit-normal (do (.calculateNormal t)(.getNormal t))
    3.29 +        vertices [vert-1 vert-2 vert-3 unit-normal]]
    3.30 +
    3.31 +    (dorun 
    3.32 +     (for [row (range 4) col (range 3)]
    3.33 +       (do
    3.34 +         (.set mat col row (.get (vertices row)col))
    3.35 +         (.set mat 3 row 1))))
    3.36 +    mat))
    3.37 +
    3.38 +(defn triangle-transformation
    3.39 +  "Returns the affine transformation that converts each vertex in the
    3.40 +   first triangle into the corresponding vertex in the second
    3.41 +   triangle."
    3.42 +  [#^Triangle tri-1 #^Triangle tri-2]
    3.43 +  (.mult 
    3.44 +   (triangle->matrix4f tri-2)
    3.45 +   (.invert (triangle->matrix4f tri-1))))
    3.46 +
    3.47 +(def death (Triangle. 
    3.48 +            (Vector3f. 1 1 1)
    3.49 +            (Vector3f. 1 2 3)
    3.50 +            (Vector3f. 5 6 7)))
    3.51 +
    3.52 +(def death-2 (Triangle. 
    3.53 +             (Vector3f. 2 2 2)
    3.54 +             (Vector3f. 1 1 1)
    3.55 +             (Vector3f. 0 1 0)))
    3.56 +
    3.57 +(defn vector2f->vector3f [v]
    3.58 +  (Vector3f. (.getX v) (.getY v) 0))
    3.59 +
    3.60 +
    3.61 +(extend-type Triangle
    3.62 +  Textual
    3.63 +  (text [t]
    3.64 +    (println "Triangle: " \newline (.get1 t) \newline
    3.65 +             (.get2 t) \newline (.get3 t))))
    3.66 +
    3.67 +
    3.68 +(defn map-triangle [f #^Triangle tri]
    3.69 +  (Triangle.
    3.70 +   (f 0 (.get1 tri))
    3.71 +   (f 1 (.get2 tri))
    3.72 +   (f 2 (.get3 tri))))
    3.73 +
    3.74 +(defn triangle-seq [#^Triangle tri]
    3.75 +  [(.get1 tri) (.get2 tri) (.get3 tri)])
    3.76 +
    3.77 +(defn vector3f-seq [#^Vector3f v]
    3.78 +  [(.getX v) (.getY v) (.getZ v)])
    3.79 +
    3.80  (defn inside-triangle?
    3.81 -  [vert-1 vert-2 vert-3 p]
    3.82 -   (and
    3.83 -    (same-side? vert-1 vert-2 vert-3 p)
    3.84 -    (same-side? vert-2 vert-3 vert-1 p)
    3.85 -    (same-side? vert-3 vert-1 vert-2 p)))
    3.86 +  "Is the point inside the triangle? Now what do we do?
    3.87 +   You might want to hold on there"
    3.88 +  {:author "God"}
    3.89 +  [tri p]
    3.90 +  (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]
    3.91 +    (and
    3.92 +     (same-side? vert-1 vert-2 vert-3 p)
    3.93 +     (same-side? vert-2 vert-3 vert-1 p)
    3.94 +     (same-side? vert-3 vert-1 vert-2 p))))
    3.95  
    3.96 +(defn uv-triangle
    3.97 +  "Convert the mesh triangle into the cooresponding triangle in
    3.98 +   UV-space. Z-component of these triangles is always zero." 
    3.99 +  [#^Mesh mesh #^Triangle tri]
   3.100 +  (apply #(Triangle. %1 %2 %3)
   3.101 +         (map vector2f->vector3f
   3.102 +              (tri-uv-coord mesh tri))))
   3.103  
   3.104 -(defn analyze-triangle [#^Geometry obj #^Triangle tri]
   3.105 -  ;; first, calculate the transformation matrix that will take us
   3.106 -  ;; from uv-coordinates to the real world coordinates
   3.107 -  (let [mesh (.getMesh obj)
   3.108 -        world [(.get1 tri) (.get2 tri) (.get3 tri)]
   3.109 -        uv    (tri-uv-coord mesh tri)
   3.110 +(defn pixel-triangle
   3.111 +  "Convert the mesh triange into the corresponding triangle in
   3.112 +  UV-pixel-space. Z compenent will be zero."
   3.113 +  [#^Mesh mesh #^Triangle tri width height]
   3.114 +  (map-triangle (fn [_ v]
   3.115 +                  (Vector3f. (* width (.getX v))
   3.116 +                             (* height (.getY v))
   3.117 +                             0))
   3.118 +                (uv-triangle mesh tri)))
   3.119  
   3.120 -    
   3.121 +(defn triangle-bounds
   3.122 +  "Dimensions of the bounding square of the triangle in the form
   3.123 +   [x y width height].
   3.124 +   Assumes that the triangle lies in the XY plane."
   3.125 +  [#^Triangle tri]
   3.126 +  (let [verts (map vector3f-seq (triangle-seq tri))
   3.127 +        x (apply min (map first verts))
   3.128 +        y (apply min (map second verts))]
   3.129  
   3.130 -    
   3.131 -   (println-repl world uv)))
   3.132 -  
   3.133 -  
   3.134 +    [x y 
   3.135 +     (- (apply max (map first verts)) x)
   3.136 +     (- (apply max (map second verts)) y)
   3.137 +     ]))
   3.138    
   3.139  
   3.140 +(defn locate-tactile-sensors
   3.141 +  "Search the geometry's tactile UV image for touch sensors, returning
   3.142 +  their positions in geometry-relative coordinates."
   3.143 +  [#^Geometry geo]
   3.144  
   3.145 -(defn tactile-coords* 
   3.146 -  [#^Geometry obj]
   3.147 -  (let
   3.148 -      [tris (triangles obj)
   3.149 -       mesh (.getMesh obj)
   3.150 -       
   3.151 -    
   3.152 -  )
   3.153 +  ;; inside-triangle? white-coordinates triangle-transformation 
   3.154 +  ;; tri-uv-coord touch-receptor-image
   3.155 +  (let [mesh (.getMesh geo)
   3.156 +        image (touch-receptor-image geo)
   3.157 +        width (.getWidth image)
   3.158 +        height (.getHeight image)
   3.159 +        tris (triangles geo)
   3.160 +
   3.161 +        ;; for each triangle
   3.162 +        sensor-coords
   3.163 +        (fn [tri]
   3.164 +          ;; translate triangle to uv-pixel-space
   3.165 +          (let [uv-tri
   3.166 +                (pixel-triangle mesh tri width height)
   3.167 +                bounds (vec (triangle-bounds uv-tri))]
   3.168 +            
   3.169 +            ;; get that part of the picture
   3.170 +            
   3.171 +            (apply #(.setRoi image %1 %2 %3 %4) bounds)
   3.172 +            (let [cutout (.crop (.getProcessor image))
   3.173 +                  ;; extract white pixels inside triangle
   3.174 +                  cutout-tri
   3.175 +                  (map-triangle
   3.176 +                   (fn [_ v]
   3.177 +                     (.subtract
   3.178 +                      v
   3.179 +                      (Vector3f. (bounds 0) (bounds 1) (float 0))))
   3.180 +                   uv-tri)
   3.181 +                  whites (filter (partial inside-triangle? cutout-tri)
   3.182 +                                 (map vector2f->vector3f
   3.183 +                                      (white-coordinates cutout)))
   3.184 +                  ;; translate pixel coordinates to world-space
   3.185 +                  transform (triangle-transformation cutout-tri tri)]
   3.186 +              (map #(.mult transform %) whites))))]
   3.187 +    (map sensor-coords tris)))
   3.188 +
   3.189 +
   3.190 +            
   3.191 +          
   3.192 +          
   3.193 +        
   3.194 +        
   3.195 + 
   3.196 +
   3.197 +
   3.198  
   3.199  
   3.200  
   3.201 @@ -485,7 +610,6 @@
   3.202                                  (* height (.getY uv-3)))
   3.203               left-corner
   3.204               (Vector2f. min-x min-y)
   3.205 -             
   3.206               ]
   3.207  
   3.208           (.setRoi receptors min-x min-y (- max-x min-x) (- max-y min-y))
   3.209 @@ -791,3 +915,91 @@
   3.210  <<body-1>>
   3.211  #+end_src
   3.212  
   3.213 +
   3.214 +
   3.215 +
   3.216 +
   3.217 +(defn transform-trianglesdsd
   3.218 +  "Transform that converts each vertex in the first triangle
   3.219 +  into the corresponding vertex in the second triangle."
   3.220 +  [#^Triangle tri-1 #^Triangle tri-2]
   3.221 +  (let [in  [(.get1 tri-1)
   3.222 +             (.get2 tri-1)
   3.223 +             (.get3 tri-1)]
   3.224 +        out [(.get1 tri-2)
   3.225 +             (.get2 tri-2)
   3.226 +             (.get3 tri-2)]]
   3.227 +    (let [translate (doto (Matrix4f.) (.setTranslation (.negate (in 0))))
   3.228 +          in* [(.mult translate (in 0))
   3.229 +               (.mult translate (in 1))
   3.230 +               (.mult translate (in 2))]
   3.231 +          final-translation
   3.232 +          (doto (Matrix4f.)
   3.233 +            (.setTranslation (out 1)))
   3.234 +          
   3.235 +          rotate-1
   3.236 +          (doto (Matrix3f.)
   3.237 +            (.fromStartEndVectors
   3.238 +             (.normalize
   3.239 +              (.subtract
   3.240 +               (in* 1) (in* 0)))
   3.241 +             (.normalize
   3.242 +              (.subtract
   3.243 +               (out 1) (out 0)))))
   3.244 +          in** [(.mult rotate-1 (in* 0))
   3.245 +                (.mult rotate-1 (in* 1))
   3.246 +                (.mult rotate-1 (in* 2))]
   3.247 +          scale-factor-1
   3.248 +          (.mult
   3.249 +           (.normalize
   3.250 +            (.subtract
   3.251 +             (out 1)
   3.252 +             (out 0)))
   3.253 +           (/ (.length
   3.254 +               (.subtract (out 1)
   3.255 +                          (out 0)))
   3.256 +              (.length
   3.257 +               (.subtract (in** 1)
   3.258 +                          (in** 0)))))
   3.259 +          scale-1 (doto (Matrix4f.) (.setScale scale-factor-1))
   3.260 +          in*** [(.mult scale-1 (in** 0))
   3.261 +                 (.mult scale-1 (in** 1))
   3.262 +                 (.mult scale-1 (in** 2))]
   3.263 +          
   3.264 +          
   3.265 +          
   3.266 +          
   3.267 +          
   3.268 +          ]
   3.269 +      
   3.270 +      (dorun (map println in))
   3.271 +      (println)
   3.272 +      (dorun (map println in*))
   3.273 +      (println)
   3.274 +      (dorun (map println in**))
   3.275 +      (println)
   3.276 +      (dorun (map println in***))
   3.277 +      (println)
   3.278 +      
   3.279 +    )))
   3.280 +        
   3.281 +                   
   3.282 +                     
   3.283 +                                
   3.284 +                        
   3.285 +                                   
   3.286 +        
   3.287 +                 
   3.288 +        
   3.289 +
   3.290 +
   3.291 +        
   3.292 +                     
   3.293 +                      
   3.294 +                   
   3.295 +                   
   3.296 +    
   3.297 +  
   3.298 +  )
   3.299 +  
   3.300 +