changeset 96:4a9096f31017

working on touch
author Robert McIntyre <rlm@mit.edu>
date Tue, 10 Jan 2012 21:26:41 -0700
parents e4bcd0c481ba
children 2ff8c7c4e64d
files assets/Models/creature1/tip.png org/test-creature.org
diffstat 2 files changed, 105 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
     1.1 Binary file assets/Models/creature1/tip.png has changed
     2.1 --- a/org/test-creature.org	Tue Jan 10 08:38:50 2012 -0700
     2.2 +++ b/org/test-creature.org	Tue Jan 10 21:26:41 2012 -0700
     2.3 @@ -125,6 +125,15 @@
     2.4     (let [out (Vector3f.)]
     2.5       (.worldToLocal object world-coordinate out) out))
     2.6  
     2.7 +(defn local-to-world
     2.8 +   "Convert the local coordinates into coordinates into world relative
     2.9 +    coordinates" 
    2.10 +   [#^Spatial object local-coordinate]
    2.11 +   (let [world-coordinate (Vector3f.)]
    2.12 +     (.localToWorld object local-coordinate world-coordinate)
    2.13 +     world-coordinate))
    2.14 +
    2.15 +
    2.16  (defmulti joint-dispatch
    2.17    "Translate blender pseudo-joints into real JME joints."
    2.18    (fn [constraints & _] 
    2.19 @@ -506,6 +515,9 @@
    2.20                               0))
    2.21                  (uv-triangle mesh tri)))
    2.22  
    2.23 +(def rasterize pixel-triangle)
    2.24 +
    2.25 +
    2.26  (defn triangle-bounds
    2.27    "Dimensions of the bounding square of the triangle in the form
    2.28     [x y width height].
    2.29 @@ -525,14 +537,13 @@
    2.30    "Search the geometry's tactile UV image for touch sensors, returning
    2.31    their positions in geometry-relative coordinates."
    2.32    [#^Geometry geo]
    2.33 -
    2.34 -  ;; inside-triangle? white-coordinates triangle-transformation 
    2.35 -  ;; tri-uv-coord touch-receptor-image
    2.36    (let [mesh (.getMesh geo)
    2.37 +        tris (triangles geo)
    2.38 +        
    2.39          image (touch-receptor-image geo)
    2.40          width (.getWidth image)
    2.41          height (.getHeight image)
    2.42 -        tris (triangles geo)
    2.43 +        
    2.44  
    2.45          ;; for each triangle
    2.46          sensor-coords
    2.47 @@ -560,11 +571,99 @@
    2.48                    ;; translate pixel coordinates to world-space
    2.49                    transform (triangle-transformation cutout-tri tri)]
    2.50                (map #(.mult transform %) whites))))]
    2.51 -    (map sensor-coords tris)))
    2.52  
    2.53  
    2.54 +    
    2.55 +    (vec (map sensor-coords tris))))
    2.56 +
    2.57 +(defn locate-tactile-sensors*
    2.58 +  "Search the geometry's tactile UV image for touch sensors, returning
    2.59 +  their positions in geometry-relative coordinates."
    2.60 +  [#^Geometry geo]
    2.61 +  (let [uv-image (touch-receptor-image geo)
    2.62 +        width (.getWidth uv-image)
    2.63 +        height (.getHeight uv-image)
    2.64 +
    2.65 +        mesh (.getMesh geo)
    2.66 +        mesh-tris (triangles geo)
    2.67 +
    2.68 +        ;; for each triangle
    2.69 +        sensor-coords
    2.70 +        (fn [tri]
    2.71 +          ;; translate triangle to uv-pixel-space
    2.72 +          (let [uv-tri
    2.73 +                (rasterize mesh tri width height)
    2.74 +                bounds (vec (triangle-bounds uv-tri))]
    2.75 +            
    2.76 +            ;; get that part of the picture
    2.77 +            
    2.78 +            (apply (partial (memfn setRoi) uv-image) bounds)
    2.79 +            (let [cutout (.crop (.getProcessor uv-image))
    2.80 +                  ;; extract white pixels inside triangle
    2.81 +                  cutout-tri
    2.82 +                  (map-triangle
    2.83 +                   (fn [_ v]
    2.84 +                     (.subtract
    2.85 +                      v
    2.86 +                      (Vector3f. (bounds 0) (bounds 1) (float 0))))
    2.87 +                   uv-tri)
    2.88 +                  whites (filter (partial inside-triangle? cutout-tri)
    2.89 +                                 (map vector2f->vector3f
    2.90 +                                      (white-coordinates cutout)))
    2.91 +                  ;; translate pixel coordinates to world-space
    2.92 +                  transform (triangle-transformation cutout-tri tri)]
    2.93 +              (map #(.mult transform %) whites))))]
    2.94 +
    2.95 +
    2.96 +      
    2.97 +    (for [mesh-tri mesh-tris]
    2.98 +     
    2.99 +      (let [uv-tri (rasterize mesh mesh-tri width height)
   2.100 +            bounding-box (vec (triangle-bounds uv-tri))]
   2.101 +        (apply (partial (memfn setRoi) uv-image) bounding-box)
   2.102 +
   2.103 +        
   2.104 +        
   2.105 +        
   2.106 +        ))   
   2.107 +    (vec (map sensor-coords mesh-tris))))
   2.108 +
   2.109 +
   2.110 +
   2.111 +(defn measure-touchies [#^Geometry geo]
   2.112 +  (let [tactile-sensor-coords (locate-tactile-sensors geo)
   2.113 +        tris (triangles geo)]
   2.114 +    (fn [world]
   2.115 +      (let [sensor-origins (vec
   2.116 +                         (map
   2.117 +                          #(map (partial local-to-world geo) %)
   2.118 +                          tactile-sensor-coords))
   2.119 +            triangle-normals (vec
   2.120 +                         (map (partial get-ray-direction geo)
   2.121 +                              tris))
   2.122 +            rays (flatten
   2.123 +                  (map
   2.124 +                   (fn [origins normals]
   2.125 +                     (map
   2.126 +                      #(Ray. %1 %2)
   2.127 +                      origins
   2.128 +                      normals))
   2.129 +                   sensor-origins
   2.130 +                   (map repeat triangle-normals)))
   2.131 +
   2.132              
   2.133 -          
   2.134 +            ]
   2.135 +        rays))))
   2.136 +                 
   2.137 +              
   2.138 +                   
   2.139 +            
   2.140 +                   
   2.141 +        
   2.142 +        
   2.143 +      
   2.144 +      
   2.145 +      
   2.146            
   2.147          
   2.148