Mercurial > lasercutter
diff src/clojureDemo/appeture.clj @ 1:6d9bdaf919f7
added clojureDemo source
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 20 Aug 2010 00:32:44 -0400 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/clojureDemo/appeture.clj Fri Aug 20 00:32:44 2010 -0400 1.3 @@ -0,0 +1,103 @@ 1.4 +(ns clojureDemo.appeture) 1.5 + 1.6 +(use 'clojure.contrib.repl-utils) 1.7 +(use 'clojure.contrib.accumulators) 1.8 + 1.9 +"right now this only will work on odd square arrays" 1.10 + 1.11 +(def rrr {[0 0] 20 , [1 0] 20, [2 0] 20 1.12 + [0 1] 0 , [1 1] 0, [2 1] 0 1.13 + [0 2] 0 , [1 2] 0, [2 2] 0}) 1.14 + 1.15 +(def rrrr {[0 0] 20 , [1 0] 20, [2 0] 20 , [3 0] 20, [4 0] 20, 1.16 + [0 1] 20 , [1 1] 20, [2 1] 20 , [3 1] 20, [4 1] 20, 1.17 + [0 2] 0 , [1 2] 0, [2 2] 0 , [3 2] 0, [4 2] 0, 1.18 + [0 3] 0 , [1 3] 0, [2 3] 0 , [3 3] 0, [4 3] 0, 1.19 + [0 4] 0 , [1 4] 0, [2 4] 0 , [3 4] 0, [4 4] 0,}) 1.20 + 1.21 +(defn vector-mul 1.22 + [mul vect] 1.23 + (apply vector (map #(* mul %) vect)) ) 1.24 + 1.25 +(defn vector-sum 1.26 + ([] 0) 1.27 + ([& args] 1.28 + (apply vector (reduce #(map + %1 %2) args)))) 1.29 + 1.30 +(defn vector-sub 1.31 + [vector1 vector2] 1.32 + (vector-sum vector1 (vector-mul -1 vector2))) 1.33 + 1.34 +(defn vector-dot 1.35 + [vector1 vector2] 1.36 + (reduce + (map * vector1 vector2))) 1.37 + 1.38 +(defn center 1.39 + [window] 1.40 + (let [coords (keys window)] 1.41 + (vector-mul (/ 1 (count coords)) (apply vector-sum coords)))) 1.42 + 1.43 +(defn window-segmentate 1.44 + [window line] 1.45 + (let [center (center window)] 1.46 + (letfn [(path [window] (filter (fn [point] (apply = (line center point))) (keys window))) 1.47 + (top [window] (filter (fn [point] (apply > (line center point))) (keys window))) 1.48 + (bottom [window] (filter (fn [point] (apply < (line center point))) (keys window)))] 1.49 + {:top (top window) :bottom (bottom window) :line (path window)}))) 1.50 + 1.51 +(defn diag1 1.52 + [window] 1.53 + (window-segmentate window (fn [center point] (list (first (vector-sub point center)) (-(last (vector-sub point center))))))) 1.54 + 1.55 +(defn diag2 1.56 + [window] 1.57 +(window-segmentate window (fn [center point] (list (first (vector-sub point center)) (last (vector-sub point center)))))) 1.58 + 1.59 +(defn vert 1.60 + [window] 1.61 +(window-segmentate window (fn [center point] (list (first (vector-sub point center)) 0)))) 1.62 + 1.63 +(defn horiz 1.64 + [window] 1.65 +(window-segmentate window (fn [center point] (list 0 (last (vector-sub point center)))))) 1.66 + 1.67 + 1.68 + 1.69 + 1.70 + 1.71 + 1.72 +(defn lines 1.73 + [window] 1.74 + (let [lines (list (vert window) (horiz window) (diag1 window) (diag2 window))] 1.75 + lines)) 1.76 +;This is the wrong model. Higher level processors should set these paramaters, and 1.77 +; juggle them around if they aren't getting anything they understand. 1.78 + 1.79 + 1.80 + 1.81 +(defn stats-base 1.82 + [sections window sel-fun] 1.83 + (let [stats-top (add-items empty-mean-variance (map window (:top sections))) 1.84 + stats-bottom (add-items empty-mean-variance (map window (:bottom sections)))] 1.85 + (let [ var1 (:variance stats-top) mean1 (:mean stats-top) var2 (:variance stats-bottom) mean2 (:mean stats-bottom)] 1.86 + (sel-fun var1 mean1 var2 mean2)))) 1.87 + 1.88 +(defn window-line 1.89 + [window transformation detection] 1.90 + (let [x-window (transformation window)] 1.91 + (first (filter #(stats-base % x-window detection) (lines x-window))))) 1.92 + 1.93 +(defn window-stats 1.94 + ([window] (window-stats window identity)) 1.95 + ([window transformation] 1.96 + (let [x-window (transformation window)] 1.97 + (map (fn [line] (stats-base line x-window #(list %1 %2 %3 %4))) (lines x-window))))) 1.98 + 1.99 + 1.100 + 1.101 + 1.102 +(comment 1.103 + 1.104 +(do (use :reload-all 'clojureDemo.appeture) (in-ns 'clojureDemo.appeture)) 1.105 + 1.106 +)