diff org/test-creature.org @ 96:4a9096f31017

working on touch
author Robert McIntyre <rlm@mit.edu>
date Tue, 10 Jan 2012 21:26:41 -0700
parents e4bcd0c481ba
children 2ff8c7c4e64d
line wrap: on
line diff
     1.1 --- a/org/test-creature.org	Tue Jan 10 08:38:50 2012 -0700
     1.2 +++ b/org/test-creature.org	Tue Jan 10 21:26:41 2012 -0700
     1.3 @@ -125,6 +125,15 @@
     1.4     (let [out (Vector3f.)]
     1.5       (.worldToLocal object world-coordinate out) out))
     1.6  
     1.7 +(defn local-to-world
     1.8 +   "Convert the local coordinates into coordinates into world relative
     1.9 +    coordinates" 
    1.10 +   [#^Spatial object local-coordinate]
    1.11 +   (let [world-coordinate (Vector3f.)]
    1.12 +     (.localToWorld object local-coordinate world-coordinate)
    1.13 +     world-coordinate))
    1.14 +
    1.15 +
    1.16  (defmulti joint-dispatch
    1.17    "Translate blender pseudo-joints into real JME joints."
    1.18    (fn [constraints & _] 
    1.19 @@ -506,6 +515,9 @@
    1.20                               0))
    1.21                  (uv-triangle mesh tri)))
    1.22  
    1.23 +(def rasterize pixel-triangle)
    1.24 +
    1.25 +
    1.26  (defn triangle-bounds
    1.27    "Dimensions of the bounding square of the triangle in the form
    1.28     [x y width height].
    1.29 @@ -525,14 +537,13 @@
    1.30    "Search the geometry's tactile UV image for touch sensors, returning
    1.31    their positions in geometry-relative coordinates."
    1.32    [#^Geometry geo]
    1.33 -
    1.34 -  ;; inside-triangle? white-coordinates triangle-transformation 
    1.35 -  ;; tri-uv-coord touch-receptor-image
    1.36    (let [mesh (.getMesh geo)
    1.37 +        tris (triangles geo)
    1.38 +        
    1.39          image (touch-receptor-image geo)
    1.40          width (.getWidth image)
    1.41          height (.getHeight image)
    1.42 -        tris (triangles geo)
    1.43 +        
    1.44  
    1.45          ;; for each triangle
    1.46          sensor-coords
    1.47 @@ -560,11 +571,99 @@
    1.48                    ;; translate pixel coordinates to world-space
    1.49                    transform (triangle-transformation cutout-tri tri)]
    1.50                (map #(.mult transform %) whites))))]
    1.51 -    (map sensor-coords tris)))
    1.52  
    1.53  
    1.54 +    
    1.55 +    (vec (map sensor-coords tris))))
    1.56 +
    1.57 +(defn locate-tactile-sensors*
    1.58 +  "Search the geometry's tactile UV image for touch sensors, returning
    1.59 +  their positions in geometry-relative coordinates."
    1.60 +  [#^Geometry geo]
    1.61 +  (let [uv-image (touch-receptor-image geo)
    1.62 +        width (.getWidth uv-image)
    1.63 +        height (.getHeight uv-image)
    1.64 +
    1.65 +        mesh (.getMesh geo)
    1.66 +        mesh-tris (triangles geo)
    1.67 +
    1.68 +        ;; for each triangle
    1.69 +        sensor-coords
    1.70 +        (fn [tri]
    1.71 +          ;; translate triangle to uv-pixel-space
    1.72 +          (let [uv-tri
    1.73 +                (rasterize mesh tri width height)
    1.74 +                bounds (vec (triangle-bounds uv-tri))]
    1.75 +            
    1.76 +            ;; get that part of the picture
    1.77 +            
    1.78 +            (apply (partial (memfn setRoi) uv-image) bounds)
    1.79 +            (let [cutout (.crop (.getProcessor uv-image))
    1.80 +                  ;; extract white pixels inside triangle
    1.81 +                  cutout-tri
    1.82 +                  (map-triangle
    1.83 +                   (fn [_ v]
    1.84 +                     (.subtract
    1.85 +                      v
    1.86 +                      (Vector3f. (bounds 0) (bounds 1) (float 0))))
    1.87 +                   uv-tri)
    1.88 +                  whites (filter (partial inside-triangle? cutout-tri)
    1.89 +                                 (map vector2f->vector3f
    1.90 +                                      (white-coordinates cutout)))
    1.91 +                  ;; translate pixel coordinates to world-space
    1.92 +                  transform (triangle-transformation cutout-tri tri)]
    1.93 +              (map #(.mult transform %) whites))))]
    1.94 +
    1.95 +
    1.96 +      
    1.97 +    (for [mesh-tri mesh-tris]
    1.98 +     
    1.99 +      (let [uv-tri (rasterize mesh mesh-tri width height)
   1.100 +            bounding-box (vec (triangle-bounds uv-tri))]
   1.101 +        (apply (partial (memfn setRoi) uv-image) bounding-box)
   1.102 +
   1.103 +        
   1.104 +        
   1.105 +        
   1.106 +        ))   
   1.107 +    (vec (map sensor-coords mesh-tris))))
   1.108 +
   1.109 +
   1.110 +
   1.111 +(defn measure-touchies [#^Geometry geo]
   1.112 +  (let [tactile-sensor-coords (locate-tactile-sensors geo)
   1.113 +        tris (triangles geo)]
   1.114 +    (fn [world]
   1.115 +      (let [sensor-origins (vec
   1.116 +                         (map
   1.117 +                          #(map (partial local-to-world geo) %)
   1.118 +                          tactile-sensor-coords))
   1.119 +            triangle-normals (vec
   1.120 +                         (map (partial get-ray-direction geo)
   1.121 +                              tris))
   1.122 +            rays (flatten
   1.123 +                  (map
   1.124 +                   (fn [origins normals]
   1.125 +                     (map
   1.126 +                      #(Ray. %1 %2)
   1.127 +                      origins
   1.128 +                      normals))
   1.129 +                   sensor-origins
   1.130 +                   (map repeat triangle-normals)))
   1.131 +
   1.132              
   1.133 -          
   1.134 +            ]
   1.135 +        rays))))
   1.136 +                 
   1.137 +              
   1.138 +                   
   1.139 +            
   1.140 +                   
   1.141 +        
   1.142 +        
   1.143 +      
   1.144 +      
   1.145 +      
   1.146            
   1.147          
   1.148