19aeb5ea3df9

Switch to 3d-vectors (partially)
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Wed, 04 Apr 2018 23:37:07 -0400
parents e9fe0b053e81
children ebe16cb914fb
branches/tags (none)
files flax.asd package.lisp src/looms/001-triangles.lisp src/looms/002-wobbly-lines.lisp src/looms/005-simple-triangulations.lisp src/looms/007-stippling.lisp

Changes

--- a/flax.asd	Wed Apr 04 22:11:33 2018 -0400
+++ b/flax.asd	Wed Apr 04 23:37:07 2018 -0400
@@ -14,6 +14,8 @@
                :iterate
                :losh
                :zpng
+               :3d-vectors
+               :3d-matrices
 
                )
 
--- a/package.lisp	Wed Apr 04 22:11:33 2018 -0400
+++ b/package.lisp	Wed Apr 04 23:37:07 2018 -0400
@@ -44,13 +44,15 @@
 
 (defpackage :flax.looms.001-triangles
   (:use :cl :iterate :losh :flax.base :flax.quickutils
-    :flax.coordinates)
+    :flax.coordinates
+    :3d-vectors)
   (:export :loom))
 
 (defpackage :flax.looms.002-wobbly-lines
   (:use :cl :iterate :losh :flax.base :flax.quickutils
     :flax.colors
-    :flax.coordinates)
+    :flax.coordinates
+    :3d-vectors)
   (:export :loom))
 
 (defpackage :flax.looms.003-basic-l-systems
@@ -68,7 +70,8 @@
 (defpackage :flax.looms.005-simple-triangulations
   (:use :cl :iterate :losh :flax.base :flax.quickutils
     :flax.colors
-    :flax.coordinates)
+    :flax.coordinates
+    :3d-vectors)
   (:export :loom))
 
 (defpackage :flax.looms.006-tracing-lines
@@ -80,7 +83,8 @@
 (defpackage :flax.looms.007-stipple
   (:use :cl :iterate :losh :flax.base :flax.quickutils
     :flax.colors
-    :flax.coordinates)
+    :flax.coordinates
+    :3d-vectors)
   (:export :loom))
 
 
--- a/src/looms/001-triangles.lisp	Wed Apr 04 22:11:33 2018 -0400
+++ b/src/looms/001-triangles.lisp	Wed Apr 04 23:37:07 2018 -0400
@@ -12,9 +12,9 @@
 
 ;;;; Elements -----------------------------------------------------------------
 (defstruct (triangle (:conc-name ""))
-  (a (coord 0 0) :type coord)
-  (b (coord 0 0) :type coord)
-  (c (coord 0 0) :type coord))
+  (a (vec 0 0) :type vec2)
+  (b (vec 0 0) :type vec2)
+  (c (vec 0 0) :type vec2))
 
 (define-with-macro (triangle :conc-name "") a b c)
 
@@ -25,7 +25,9 @@
 ;;;; Element Conversion -------------------------------------------------------
 (defun convert-triangle (triangle)
   (with-triangle (triangle)
-    (flax.drawing:triangle a b c)))
+    (flax.drawing:triangle (coord (vx a) (vy a))
+                           (coord (vx b) (vy b))
+                           (coord (vx c) (vy c)))))
 
 (defun convert (universe)
   (mapcar #'convert-triangle universe))
@@ -33,19 +35,19 @@
 
 ;;;; Generation ---------------------------------------------------------------
 (defun initial-triangles ()
-  (list (triangle (coord 0 1)
-                  (coord 1 1)
-                  (coord 0 0))
-        (triangle (coord 1 0)
-                  (coord 1 1)
-                  (coord 0 0))))
+  (list (triangle (vec 0 1)
+                  (vec 1 1)
+                  (vec 0 0))
+        (triangle (vec 1 0)
+                  (vec 1 1)
+                  (vec 0 0))))
 
 
 (defun split-triangle-evenly (triangle)
   (with-triangle (triangle)
     (let* ((n 1/2)
-           (p (coord (lerp (x b) (x c) n)
-                     (lerp (y b) (y c) n))))
+           (p (vec2 (lerp (vx b) (vx c) n)
+                    (lerp (vy b) (vy c) n))))
       (list (triangle p b a)
             (triangle p a c)))))
 
@@ -57,9 +59,9 @@
 
 (defun find-longest-side (triangle)
   (with-triangle (triangle)
-    (let* ((ab (distance a b))
-           (bc (distance b c))
-           (ca (distance c a))
+    (let* ((ab (vdistance a b))
+           (bc (vdistance b c))
+           (ca (vdistance c a))
            (longest (max ab bc ca)))
       (cond
         ((= longest ab) (list c a b))
@@ -72,7 +74,7 @@
     (let ((p (-<> (random-gaussian 0.5 0.1 #'rand)
                (clamp 0.3 0.7 <>)
                (round-to <> 1/100)
-               (clerp b c <>))))
+               (vlerp b c <>))))
       (list (triangle p b a)
             (triangle p a c)))))
 
--- a/src/looms/002-wobbly-lines.lisp	Wed Apr 04 22:11:33 2018 -0400
+++ b/src/looms/002-wobbly-lines.lisp	Wed Apr 04 23:37:07 2018 -0400
@@ -18,7 +18,8 @@
 
 ;;;; Element Conversion -------------------------------------------------------
 (defun convert (line opacity)
-  (list (flax.drawing::path (coerce (points line) 'list)
+  (list (flax.drawing::path (iterate (for p :in-whatever (points line))
+                                     (collect (coord (vx p) (vy p))))
                             :color (hsv *hue* 0.9 1)
                             :opacity opacity)))
 
@@ -28,12 +29,12 @@
   (line
     (iterate
       (for x :from 0.0 :to (+ 1.0 least-positive-single-float) :by (/ 1.0 segments))
-      (collect (coord x 0.5) :result-type 'vector))))
+      (collect (vec x 0.5) :result-type 'vector))))
 
 
 ;;;; Tick ---------------------------------------------------------------------
 (defun perturb-point (point)
-  (incf (y point) (random-range-inclusive (- *swing*) *swing* #'rand)))
+  (incf (vy point) (random-range-inclusive (- *swing*) *swing* #'rand)))
 
 (defun perturb-line (line)
   (map nil #'perturb-point (points line)))
@@ -42,11 +43,11 @@
   (iterate
     (with points = (points line))
     (with final = (1- (length points)))
-    (for c :in-vector points :with-index i)
-    (for y = (y c))
-    (for l = (or (unless (zerop i) (y (aref points (1- i)))) y))
-    (for r = (or (unless (= final i) (y (aref points (1+ i)))) y))
-    (zapf (y c) (/ (+ % % l r) 4.0))))
+    (for p :in-vector points :with-index i)
+    (for y = (vy p))
+    (for l = (or (unless (zerop i) (vy (aref points (1- i)))) y))
+    (for r = (or (unless (= final i) (vy (aref points (1+ i)))) y))
+    (zapf (vy p) (/ (+ % % l r) 4.0))))
 
 (defun tick (line)
   (perturb-line line)
--- a/src/looms/005-simple-triangulations.lisp	Wed Apr 04 22:11:33 2018 -0400
+++ b/src/looms/005-simple-triangulations.lisp	Wed Apr 04 23:37:07 2018 -0400
@@ -5,14 +5,17 @@
 (defparameter *point-size* 0.003)
 
 (defun convert-point (point)
-  (flax.drawing:circle point (random-gaussian *point-size*
+  (flax.drawing:circle (coord (vx point) (vy point))
+                       (random-gaussian *point-size*
                                               (* 0.15 *point-size*)
                                               #'rand)))
 
 (defun convert-triangle (ratio tri)
   (when (randomp ratio #'rand)
     (destructuring-bind (a b c) tri
-      (list (flax.drawing:triangle a b c)))))
+      (list (flax.drawing:triangle (coord (vx a) (vy a))
+                                   (coord (vx b) (vy b))
+                                   (coord (vx c) (vy c)))))))
 
 (defun convert (points ratio)
   (append
@@ -22,22 +25,24 @@
 (defun triangulate (points)
   (mapcar (lambda (indexes)
             (map 'list (curry #'aref points) indexes))
-          (lofi.tri:triangulate (map 'vector #'coord-to-cons points))))
+          (lofi.tri:triangulate (map 'vector (lambda (p)
+                                               (cons (vx p) (vy p)))
+                                     points))))
 
 (defun gauss ()
   (clamp 0.0 1.0 (random-gaussian 0.5 0.15 #'rand)))
 
 (defun generate-point-uniform ()
-  (coord (rand 1.0) (rand 1.0)))
+  (vec2 (rand 1.0) (rand 1.0)))
 
 (defun generate-point-gaussian ()
-  (coord (gauss) (gauss)))
+  (vec2 (gauss) (gauss)))
 
 (defun generate-point-gaussian-vertical ()
-  (coord (rand 1.0) (gauss)))
+  (vec2 (rand 1.0) (gauss)))
 
 (defun generate-point-gaussian-horizontal ()
-  (coord (gauss) (rand 1.0)))
+  (vec2 (gauss) (rand 1.0)))
 
 (defun generate (generator n)
   (iterate (repeat n)
--- a/src/looms/007-stippling.lisp	Wed Apr 04 22:11:33 2018 -0400
+++ b/src/looms/007-stippling.lisp	Wed Apr 04 23:37:07 2018 -0400
@@ -3,7 +3,8 @@
 
 ;;;; Convert ------------------------------------------------------------------
 (defun convert (points)
-  (mapcar #'flax.drawing:point points))
+  (iterate (for p :in points)
+           (collect (flax.drawing:point (coord (vx p) (vy p))))))
 
 
 ;;;; Shapes -------------------------------------------------------------------
@@ -16,7 +17,7 @@
 
 
 (defun random-coord ()
-  (coord (rand 1.0) (rand 1.0)))
+  (vec (rand 1.0) (rand 1.0)))
 
 (defun gen-rectangle ()
   (make-rectangle :a (random-coord) :b (random-coord)))
@@ -42,19 +43,19 @@
 
 (defmethod bounding-box ((shape circle))
   (with-circle (shape c r)
-    (let ((x (x c))
-          (y (y c)))
-      (cons (coord (- x r) (- y r))
-            (coord (+ x r) (+ y r))))))
+    (let ((x (vx c))
+          (y (vy c)))
+      (cons (vec (- x r) (- y r))
+            (vec (+ x r) (+ y r))))))
 
 (defun random-point-in-bounding-box (bounding-box)
   (destructuring-bind (a . b) bounding-box
-    (let ((x1 (min (x a) (x b)))
-          (x2 (max (x a) (x b)))
-          (y1 (min (y a) (y b)))
-          (y2 (max (y a) (y b))))
-      (coord (random-range-inclusive x1 x2 #'rand)
-             (random-range-inclusive y1 y2 #'rand)))))
+    (let ((x1 (min (vx a) (vx b)))
+          (x2 (max (vx a) (vx b)))
+          (y1 (min (vy a) (vy b)))
+          (y2 (max (vy a) (vy b))))
+      (vec (random-range-inclusive x1 x2 #'rand)
+           (random-range-inclusive y1 y2 #'rand)))))
 
 
 ;;;; Area ---------------------------------------------------------------------
@@ -62,8 +63,8 @@
 
 (defmethod area ((shape rectangle))
   (with-rectangle (shape)
-    (* (abs (- (x a) (x b)))
-       (abs (- (y a) (y b))))))
+    (* (abs (- (vx a) (vx b)))
+       (abs (- (vy a) (vy b))))))
 
 (defmethod area ((shape circle))
   (* 1/2tau (square (radius shape))))
@@ -82,12 +83,12 @@
   t)
 
 (defmethod containsp ((shape circle) point)
-  (<= (distance point (center shape))
+  (<= (vdistance point (center shape))
       (radius shape)))
 
 (defun canvas-contains-p (point)
-  (and (<= 0 (x point) 1)
-       (<= 0 (y point) 1)))
+  (and (<= 0 (vx point) 1)
+       (<= 0 (vy point) 1)))
 
 (defun random-point-in-shape (shape)
   (iterate
@@ -125,5 +126,5 @@
         (flax.drawing:render canvas <>))
       (values shapes))))
 
-;; (time (loom 11 "out" :png 800 800))
+;; (time (loom 11 "out" :svg 800 800))
 ;; (time (loom 112 "out" :plot 800 800 :ratio 40000))