changeset 102:7eeb940bcbc8

data format for touch implemented
author Robert McIntyre <rlm@mit.edu>
date Sat, 14 Jan 2012 20:44:13 -0700
parents 65332841b7d9
children 85ee8bb80edf
files org/test-creature.org
diffstat 1 files changed, 63 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/org/test-creature.org	Sat Jan 14 04:28:37 2012 -0700
     1.2 +++ b/org/test-creature.org	Sat Jan 14 20:44:13 2012 -0700
     1.3 @@ -97,6 +97,9 @@
     1.4  (defn points->image
     1.5    "Take a sparse collection of points and visuliaze it as a
     1.6     BufferedImage."
     1.7 +
     1.8 +  ;; TODO maybe parallelize this since it's easy
     1.9 +
    1.10    [points]
    1.11    (let [xs (vec (map first points))
    1.12          ys (vec (map second points))
    1.13 @@ -112,16 +115,11 @@
    1.14  
    1.15      image))
    1.16  
    1.17 -  
    1.18 -  
    1.19 -
    1.20 -;;(defn visualize [points]
    1.21 -  
    1.22  (defn test-data
    1.23    []
    1.24    (vec
    1.25 -   (for [a (range 0 100 2)
    1.26 -         b (range 0 100 2)]
    1.27 +   (for [a (range 0 1000 2)
    1.28 +         b (range 0 1000 2)]
    1.29       (vector a b))
    1.30    ))
    1.31  
    1.32 @@ -145,8 +143,8 @@
    1.33    (let
    1.34        [num-points (count points)
    1.35         center (vector
    1.36 -               (average (map first points))
    1.37 -               (average (map first points)))
    1.38 +               (int (average (map first points)))
    1.39 +               (int (average (map first points))))
    1.40         flattened
    1.41         (reduce
    1.42          concat 
    1.43 @@ -166,19 +164,31 @@
    1.44                  (collapse-1d (first center)
    1.45                               (map first row))
    1.46                  (map second row)))
    1.47 -         (partition-by second (sort-by second flattened))))]
    1.48 -   squeezed
    1.49 +         (partition-by second (sort-by second flattened))))
    1.50 +       ;;vi (view-image)
    1.51 +       relocate
    1.52 +       (let [min-x (apply min (map first squeezed))
    1.53 +             min-y (apply min (map second squeezed))]
    1.54 +         (map (fn [[x y]]
    1.55 +                [(- x min-x)
    1.56 +                 (- y min-y)])
    1.57 +              squeezed))
    1.58 +       ]
    1.59 +    ;;(vi (points->image points))
    1.60 +    ;;(Thread/sleep 1000)
    1.61 +    ;;(vi (points->image flattened))
    1.62 +    ;;(Thread/sleep 1000)
    1.63 +    ;;(vi (points->image squeezed))
    1.64 +    relocate
    1.65     ))
    1.66 -        
    1.67 -
    1.68  
    1.69  (defn load-bullet []
    1.70    (let [sim (world (Node.) {} no-op no-op)]
    1.71 -    (.enqueue
    1.72 -     sim
    1.73 -     (fn []
    1.74 -       (.stop sim)))
    1.75 -    (.start sim)))
    1.76 +    (doto sim
    1.77 +      (.enqueue
    1.78 +       (fn []
    1.79 +         (.stop sim)))
    1.80 +      (.start))))
    1.81  
    1.82  (defn load-blender-model
    1.83    "Load a .blend file using an asset folder relative path."
    1.84 @@ -536,14 +546,16 @@
    1.85    [#^ImageProcessor ip]
    1.86    (filter-pixels #(= % white) ip))
    1.87    
    1.88 -(defn same-side? [p1 p2 ref p]
    1.89 +(defn same-side?
    1.90 +  "Given the points p1 and p2 and the reference point ref, is point p
    1.91 +  on the same side of the line that goes through p1 and p2 as ref is?" 
    1.92 +  [p1 p2 ref p]
    1.93    (<=
    1.94     0
    1.95     (.dot 
    1.96      (.cross (.subtract p2 p1) (.subtract p p1))
    1.97      (.cross (.subtract p2 p1) (.subtract ref p1)))))
    1.98  
    1.99 -
   1.100  (defn triangle->matrix4f
   1.101    "Converts the triangle into a 4x4 matrix of vertices: The first
   1.102     three columns contain the vertices of the triangle; the last
   1.103 @@ -555,7 +567,6 @@
   1.104          ((comp vec map) #(.get t %) (range 3))
   1.105          unit-normal (do (.calculateNormal t)(.getNormal t))
   1.106          vertices [vert-1 vert-2 vert-3 unit-normal]]
   1.107 -
   1.108      (dorun 
   1.109       (for [row (range 4) col (range 3)]
   1.110         (do
   1.111 @@ -585,14 +596,12 @@
   1.112  (defn vector2f->vector3f [v]
   1.113    (Vector3f. (.getX v) (.getY v) 0))
   1.114  
   1.115 -
   1.116  (extend-type Triangle
   1.117    Textual
   1.118    (text [t]
   1.119      (println "Triangle: " \newline (.get1 t) \newline
   1.120               (.get2 t) \newline (.get3 t))))
   1.121  
   1.122 -
   1.123  (defn map-triangle [f #^Triangle tri]
   1.124    (Triangle.
   1.125     (f 0 (.get1 tri))
   1.126 @@ -625,7 +634,7 @@
   1.127                (tri-uv-coord mesh tri))))
   1.128  
   1.129  (defn pixel-triangle
   1.130 -  "Convert the mesh triange into the corresponding triangle in
   1.131 +  "Convert the mesh triangle into the corresponding triangle in
   1.132    UV-pixel-space. Z compenent will be zero."
   1.133    [#^Mesh mesh #^Triangle tri width height]
   1.134    (map-triangle (fn [_ v]
   1.135 @@ -645,7 +654,6 @@
   1.136    (let [verts (map vector3f-seq (triangle-seq tri))
   1.137          x (apply min (map first verts))
   1.138          y (apply min (map second verts))]
   1.139 -
   1.140      [x y 
   1.141       (- (apply max (map first verts)) x)
   1.142       (- (apply max (map second verts)) y)
   1.143 @@ -659,8 +667,7 @@
   1.144    (if-let [image (touch-receptor-image geo)]
   1.145      (let [mesh (.getMesh geo)
   1.146            tris (triangles geo)
   1.147 -          
   1.148 -          
   1.149 +
   1.150            width (.getWidth image)
   1.151            height (.getHeight image)
   1.152            
   1.153 @@ -692,7 +699,17 @@
   1.154                  (map #(.mult transform %) whites))))]
   1.155        (vec (map sensor-coords tris)))
   1.156      (repeat (count (triangles geo)) [])))
   1.157 -  
   1.158 +
   1.159 +(use 'clojure.contrib.def)
   1.160 +
   1.161 +(defn-memo touch-topology [#^Gemoetry geo]
   1.162 +  (let [feeler-coords
   1.163 +        (map
   1.164 +         #(vector (int (.getX %)) (int (.getY %)))
   1.165 +         (white-coordinates
   1.166 +          (.getProcessor (touch-receptor-image (colorful)))))]
   1.167 +    (vec (collapse feeler-coords))))
   1.168 +
   1.169  (defn enable-touch [#^Geometry geo]
   1.170    (let [feeler-coords (locate-feelers geo)
   1.171          tris (triangles geo)
   1.172 @@ -711,24 +728,25 @@
   1.173                       (map #(doto (Ray. % norm)
   1.174                                (.setLimit limit)) origins))
   1.175                     sensor-origins triangle-normals))]
   1.176 -        (for [ray rays]
   1.177 -          (do
   1.178 -            (let [results (CollisionResults.)]
   1.179 -              (.collideWith node ray results)
   1.180 -              (let [touch-objects
   1.181 -                    (set
   1.182 -                     (filter #(not (= geo %))
   1.183 -                             (map #(.getGeometry %) results)))]
   1.184 -                (if (> (count touch-objects) 0)
   1.185 -                  1 0)))))))))
   1.186 +        (vector
   1.187 +         (touch-topology geo)
   1.188 +         (vec
   1.189 +          (for [ray rays]
   1.190 +            (do
   1.191 +              (let [results (CollisionResults.)]
   1.192 +                (.collideWith node ray results)
   1.193 +                (let [touch-objects
   1.194 +                      (set
   1.195 +                       (filter #(not (= geo %))
   1.196 +                               (map #(.getGeometry %) results)))]
   1.197 +                  (if (> (count touch-objects) 0)
   1.198 +                    1 0)))))))))))
   1.199  
   1.200  (defn touch [#^Node pieces]
   1.201 -  (let [touch-components
   1.202 -        (map enable-touch
   1.203 -             (filter #(isa? (class %) Geometry)
   1.204 -                     (node-seq pieces)))]
   1.205 -    (fn [node]
   1.206 -      (reduce into [] (map #(% node) touch-components))))) 
   1.207 +  (map enable-touch
   1.208 +       (filter #(isa? (class %) Geometry)
   1.209 +               (node-seq pieces))))
   1.210 +
   1.211      
   1.212  (defn all-names []
   1.213    (concat 
   1.214 @@ -910,9 +928,7 @@
   1.215                    ;; translate pixel coordinates to world-space
   1.216                    transform (triangle-transformation cutout-tri tri)]
   1.217                (map #(.mult transform %) whites))))]
   1.218 -
   1.219 -
   1.220 -      
   1.221 +     
   1.222      (for [mesh-tri mesh-tris]
   1.223       
   1.224        (let [uv-tri (rasterize mesh mesh-tri width height)