   (:require [scad-clj.scad :refer :all]
             [scad-clj.model :refer :all]
             [unicode-math.core :refer :all]))
+
+
+(def nrows 6)
+(def ncols 6)                           ; it's not set up to change this
+(def lastrow (dec nrows))
+(def cornerrow (dec lastrow))
+(def α (/ π 12))                        ; curvature of the columns
+(def β (/ π (if (= nrows 4) 26 36)))    ; curvature of the rows
+(def centerrow (- cornerrow 1))         ; controls front-back tilt
+(def centercol 3)                       ; controls left-right tilt / tenting
+(def orthographic-x (> nrows 5))        ; for larger number of rows don't curve them in as much
+; (def orthographic-x true)
 
 ;;;;;;;;;;;;;;;;;
 ;; Switch Hole ;;

    (->> (hull side-cylinder
               (mirror [-1 0 0] side-cylinder))
         (rotate (/ π 2) [1 0 0])
         (translate [0 (/ teensy-length 2) (- side-radius)])
         (translate [0 0 (- 1)])
         (translate [0 0 (- teensy-offset-height)])
         (key-place 1/2 3/2))))

;;;;;;;;;;;;;;;;;;
;; Final Export ;;
;;;;;;;;;;;;;;;;;;

(def dactyl-bottom-right
  (difference
   (union
    teensy-cover
    (difference
     bottom-plate          
     (hull teensy-cover)
     new-case
     teensy-cover
     trrs-cutout
     (->> (cube 1000 1000 10) (translate [0 0 -5]))
     screw-holes))
   usb-cutout))

(def dactyl-bottom-left
  (mirror [-1 0 0]
          (union
           io-exp-cover
           (difference
            bottom-plate
            (hull io-exp-cover)
            new-case
            io-exp-cover
            trrs-cutout
            (->> (cube 1000 1000 10) (translate [0 0 -5]))
            screw-holes))))

(def dactyl-top-right
  (difference
   (union key-holes
          connectors
          thumb
          new-case            
          teensy-support)
   trrs-hole-just-circle
   screw-holes))

(def dactyl-top-left
  (mirror [-1 0 0]
          (difference
           (union key-holes
                  connectors
                  thumb
                  new-case)
           trrs-hole-just-circle
           screw-holes)))

(spit "things/dactyl-top-right.scad"
      (write-scad dactyl-top-right))

(spit "things/dactyl-bottom-right.scad"
      (write-scad dactyl-bottom-right))

(spit "things/dactyl-top-left.scad"
      (write-scad dactyl-top-left))

(spit "things/dactyl-bottom-left.scad"
      (write-scad dactyl-bottom-left))


(def column-radius (+ (/ (/ (+ mount-width 2.0) 2)
                         (Math/sin (/ β 2)))
                      cap-top-height))
(def column-x-delta (+ -1 (- (* column-radius (Math/sin β)))))

(defn key-place [column row shape]
  (let [row-placed-shape (->> shape
                              (translate [0 0 (- row-radius)])
                              (rotate (* α (- centerrow row)) [1 0 0])      
                              (translate [0 0 row-radius]))
        column-offset (cond
                        (= column 2) [0 2.82 -4.5]
                        (>= column 4) [0 -5.8 5.64]
                        ; (= column 22) [0 2.82 -4.5]
                        ; (>= column 24) [0 -5.8 5.64]
                        :else [0 0 0])
        column-angle (* β (- centercol column))   
        placed-shape (->> row-placed-shape
                          (translate [0 0 (- column-radius)])
                          (rotate column-angle [0 1 0])
                          (translate [0 0 column-radius])
                          (translate column-offset))
        column-z-delta (* column-radius (- 1 (Math/cos column-angle)))
        placed-shape-ortho (->> row-placed-shape
                                (rotate column-angle [0 1 0])
                                (translate [(- (* (- column centercol) column-x-delta)) 0 column-z-delta])
                                (translate column-offset))]
    (->> (if orthographic-x placed-shape-ortho placed-shape)
         (rotate (/ π 12) [0 1 0])
         (translate [0 0 24]))))

(def key-holes
  (apply union
         (for [column columns
               row rows
               :when (or (.contains [2 3] column)
                         (not= row lastrow))]
           (->> single-plate
                (key-place column row)))))

