diff org/test-creature.org @ 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
line wrap: on
line diff
     1.1 --- a/org/test-creature.org	Mon Jan 09 19:14:47 2012 -0700
     1.2 +++ b/org/test-creature.org	Tue Jan 10 08:38:04 2012 -0700
     1.3 @@ -384,8 +384,9 @@
     1.4    (frame [image+]
     1.5      (frame (.getBufferedImage image+))))
     1.6  
     1.7 +
     1.8  (def white -1)
     1.9 -
    1.10 +  
    1.11  (defn filter-pixels
    1.12    "List the coordinates of all pixels matching pred."
    1.13    {:author "Dylan Holmes"}
    1.14 @@ -414,38 +415,162 @@
    1.15      (.cross (.subtract p2 p1) (.subtract p p1))
    1.16      (.cross (.subtract p2 p1) (.subtract ref p1)))))
    1.17  
    1.18 +
    1.19 +(defn triangle->matrix4f
    1.20 +  "Converts the triangle into a 4x4 matrix of vertices: The first
    1.21 +   three columns contain the vertices of the triangle; the last
    1.22 +   contains the unit normal of the triangle. The bottom row is filled
    1.23 +   with 1s."
    1.24 +  [#^Triangle t]
    1.25 +  (let [mat (Matrix4f.)
    1.26 +        [vert-1 vert-2 vert-3]
    1.27 +        ((comp vec map) #(.get t %) (range 3))
    1.28 +        unit-normal (do (.calculateNormal t)(.getNormal t))
    1.29 +        vertices [vert-1 vert-2 vert-3 unit-normal]]
    1.30 +
    1.31 +    (dorun 
    1.32 +     (for [row (range 4) col (range 3)]
    1.33 +       (do
    1.34 +         (.set mat col row (.get (vertices row)col))
    1.35 +         (.set mat 3 row 1))))
    1.36 +    mat))
    1.37 +
    1.38 +(defn triangle-transformation
    1.39 +  "Returns the affine transformation that converts each vertex in the
    1.40 +   first triangle into the corresponding vertex in the second
    1.41 +   triangle."
    1.42 +  [#^Triangle tri-1 #^Triangle tri-2]
    1.43 +  (.mult 
    1.44 +   (triangle->matrix4f tri-2)
    1.45 +   (.invert (triangle->matrix4f tri-1))))
    1.46 +
    1.47 +(def death (Triangle. 
    1.48 +            (Vector3f. 1 1 1)
    1.49 +            (Vector3f. 1 2 3)
    1.50 +            (Vector3f. 5 6 7)))
    1.51 +
    1.52 +(def death-2 (Triangle. 
    1.53 +             (Vector3f. 2 2 2)
    1.54 +             (Vector3f. 1 1 1)
    1.55 +             (Vector3f. 0 1 0)))
    1.56 +
    1.57 +(defn vector2f->vector3f [v]
    1.58 +  (Vector3f. (.getX v) (.getY v) 0))
    1.59 +
    1.60 +
    1.61 +(extend-type Triangle
    1.62 +  Textual
    1.63 +  (text [t]
    1.64 +    (println "Triangle: " \newline (.get1 t) \newline
    1.65 +             (.get2 t) \newline (.get3 t))))
    1.66 +
    1.67 +
    1.68 +(defn map-triangle [f #^Triangle tri]
    1.69 +  (Triangle.
    1.70 +   (f 0 (.get1 tri))
    1.71 +   (f 1 (.get2 tri))
    1.72 +   (f 2 (.get3 tri))))
    1.73 +
    1.74 +(defn triangle-seq [#^Triangle tri]
    1.75 +  [(.get1 tri) (.get2 tri) (.get3 tri)])
    1.76 +
    1.77 +(defn vector3f-seq [#^Vector3f v]
    1.78 +  [(.getX v) (.getY v) (.getZ v)])
    1.79 +
    1.80  (defn inside-triangle?
    1.81 -  [vert-1 vert-2 vert-3 p]
    1.82 -   (and
    1.83 -    (same-side? vert-1 vert-2 vert-3 p)
    1.84 -    (same-side? vert-2 vert-3 vert-1 p)
    1.85 -    (same-side? vert-3 vert-1 vert-2 p)))
    1.86 +  "Is the point inside the triangle? Now what do we do?
    1.87 +   You might want to hold on there"
    1.88 +  {:author "God"}
    1.89 +  [tri p]
    1.90 +  (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]
    1.91 +    (and
    1.92 +     (same-side? vert-1 vert-2 vert-3 p)
    1.93 +     (same-side? vert-2 vert-3 vert-1 p)
    1.94 +     (same-side? vert-3 vert-1 vert-2 p))))
    1.95  
    1.96 +(defn uv-triangle
    1.97 +  "Convert the mesh triangle into the cooresponding triangle in
    1.98 +   UV-space. Z-component of these triangles is always zero." 
    1.99 +  [#^Mesh mesh #^Triangle tri]
   1.100 +  (apply #(Triangle. %1 %2 %3)
   1.101 +         (map vector2f->vector3f
   1.102 +              (tri-uv-coord mesh tri))))
   1.103  
   1.104 -(defn analyze-triangle [#^Geometry obj #^Triangle tri]
   1.105 -  ;; first, calculate the transformation matrix that will take us
   1.106 -  ;; from uv-coordinates to the real world coordinates
   1.107 -  (let [mesh (.getMesh obj)
   1.108 -        world [(.get1 tri) (.get2 tri) (.get3 tri)]
   1.109 -        uv    (tri-uv-coord mesh tri)
   1.110 +(defn pixel-triangle
   1.111 +  "Convert the mesh triange into the corresponding triangle in
   1.112 +  UV-pixel-space. Z compenent will be zero."
   1.113 +  [#^Mesh mesh #^Triangle tri width height]
   1.114 +  (map-triangle (fn [_ v]
   1.115 +                  (Vector3f. (* width (.getX v))
   1.116 +                             (* height (.getY v))
   1.117 +                             0))
   1.118 +                (uv-triangle mesh tri)))
   1.119  
   1.120 -    
   1.121 +(defn triangle-bounds
   1.122 +  "Dimensions of the bounding square of the triangle in the form
   1.123 +   [x y width height].
   1.124 +   Assumes that the triangle lies in the XY plane."
   1.125 +  [#^Triangle tri]
   1.126 +  (let [verts (map vector3f-seq (triangle-seq tri))
   1.127 +        x (apply min (map first verts))
   1.128 +        y (apply min (map second verts))]
   1.129  
   1.130 -    
   1.131 -   (println-repl world uv)))
   1.132 -  
   1.133 -  
   1.134 +    [x y 
   1.135 +     (- (apply max (map first verts)) x)
   1.136 +     (- (apply max (map second verts)) y)
   1.137 +     ]))
   1.138    
   1.139  
   1.140 +(defn locate-tactile-sensors
   1.141 +  "Search the geometry's tactile UV image for touch sensors, returning
   1.142 +  their positions in geometry-relative coordinates."
   1.143 +  [#^Geometry geo]
   1.144  
   1.145 -(defn tactile-coords* 
   1.146 -  [#^Geometry obj]
   1.147 -  (let
   1.148 -      [tris (triangles obj)
   1.149 -       mesh (.getMesh obj)
   1.150 -       
   1.151 -    
   1.152 -  )
   1.153 +  ;; inside-triangle? white-coordinates triangle-transformation 
   1.154 +  ;; tri-uv-coord touch-receptor-image
   1.155 +  (let [mesh (.getMesh geo)
   1.156 +        image (touch-receptor-image geo)
   1.157 +        width (.getWidth image)
   1.158 +        height (.getHeight image)
   1.159 +        tris (triangles geo)
   1.160 +
   1.161 +        ;; for each triangle
   1.162 +        sensor-coords
   1.163 +        (fn [tri]
   1.164 +          ;; translate triangle to uv-pixel-space
   1.165 +          (let [uv-tri
   1.166 +                (pixel-triangle mesh tri width height)
   1.167 +                bounds (vec (triangle-bounds uv-tri))]
   1.168 +            
   1.169 +            ;; get that part of the picture
   1.170 +            
   1.171 +            (apply #(.setRoi image %1 %2 %3 %4) bounds)
   1.172 +            (let [cutout (.crop (.getProcessor image))
   1.173 +                  ;; extract white pixels inside triangle
   1.174 +                  cutout-tri
   1.175 +                  (map-triangle
   1.176 +                   (fn [_ v]
   1.177 +                     (.subtract
   1.178 +                      v
   1.179 +                      (Vector3f. (bounds 0) (bounds 1) (float 0))))
   1.180 +                   uv-tri)
   1.181 +                  whites (filter (partial inside-triangle? cutout-tri)
   1.182 +                                 (map vector2f->vector3f
   1.183 +                                      (white-coordinates cutout)))
   1.184 +                  ;; translate pixel coordinates to world-space
   1.185 +                  transform (triangle-transformation cutout-tri tri)]
   1.186 +              (map #(.mult transform %) whites))))]
   1.187 +    (map sensor-coords tris)))
   1.188 +
   1.189 +
   1.190 +            
   1.191 +          
   1.192 +          
   1.193 +        
   1.194 +        
   1.195 + 
   1.196 +
   1.197 +
   1.198  
   1.199  
   1.200  
   1.201 @@ -485,7 +610,6 @@
   1.202                                  (* height (.getY uv-3)))
   1.203               left-corner
   1.204               (Vector2f. min-x min-y)
   1.205 -             
   1.206               ]
   1.207  
   1.208           (.setRoi receptors min-x min-y (- max-x min-x) (- max-y min-y))
   1.209 @@ -791,3 +915,91 @@
   1.210  <<body-1>>
   1.211  #+end_src
   1.212  
   1.213 +
   1.214 +
   1.215 +
   1.216 +
   1.217 +(defn transform-trianglesdsd
   1.218 +  "Transform that converts each vertex in the first triangle
   1.219 +  into the corresponding vertex in the second triangle."
   1.220 +  [#^Triangle tri-1 #^Triangle tri-2]
   1.221 +  (let [in  [(.get1 tri-1)
   1.222 +             (.get2 tri-1)
   1.223 +             (.get3 tri-1)]
   1.224 +        out [(.get1 tri-2)
   1.225 +             (.get2 tri-2)
   1.226 +             (.get3 tri-2)]]
   1.227 +    (let [translate (doto (Matrix4f.) (.setTranslation (.negate (in 0))))
   1.228 +          in* [(.mult translate (in 0))
   1.229 +               (.mult translate (in 1))
   1.230 +               (.mult translate (in 2))]
   1.231 +          final-translation
   1.232 +          (doto (Matrix4f.)
   1.233 +            (.setTranslation (out 1)))
   1.234 +          
   1.235 +          rotate-1
   1.236 +          (doto (Matrix3f.)
   1.237 +            (.fromStartEndVectors
   1.238 +             (.normalize
   1.239 +              (.subtract
   1.240 +               (in* 1) (in* 0)))
   1.241 +             (.normalize
   1.242 +              (.subtract
   1.243 +               (out 1) (out 0)))))
   1.244 +          in** [(.mult rotate-1 (in* 0))
   1.245 +                (.mult rotate-1 (in* 1))
   1.246 +                (.mult rotate-1 (in* 2))]
   1.247 +          scale-factor-1
   1.248 +          (.mult
   1.249 +           (.normalize
   1.250 +            (.subtract
   1.251 +             (out 1)
   1.252 +             (out 0)))
   1.253 +           (/ (.length
   1.254 +               (.subtract (out 1)
   1.255 +                          (out 0)))
   1.256 +              (.length
   1.257 +               (.subtract (in** 1)
   1.258 +                          (in** 0)))))
   1.259 +          scale-1 (doto (Matrix4f.) (.setScale scale-factor-1))
   1.260 +          in*** [(.mult scale-1 (in** 0))
   1.261 +                 (.mult scale-1 (in** 1))
   1.262 +                 (.mult scale-1 (in** 2))]
   1.263 +          
   1.264 +          
   1.265 +          
   1.266 +          
   1.267 +          
   1.268 +          ]
   1.269 +      
   1.270 +      (dorun (map println in))
   1.271 +      (println)
   1.272 +      (dorun (map println in*))
   1.273 +      (println)
   1.274 +      (dorun (map println in**))
   1.275 +      (println)
   1.276 +      (dorun (map println in***))
   1.277 +      (println)
   1.278 +      
   1.279 +    )))
   1.280 +        
   1.281 +                   
   1.282 +                     
   1.283 +                                
   1.284 +                        
   1.285 +                                   
   1.286 +        
   1.287 +                 
   1.288 +        
   1.289 +
   1.290 +
   1.291 +        
   1.292 +                     
   1.293 +                      
   1.294 +                   
   1.295 +                   
   1.296 +    
   1.297 +  
   1.298 +  )
   1.299 +  
   1.300 +