;;; This software is being provided to you, the LICENSEE, by the
;;; Massachusetts Institute of Technology (M.I.T.), and the NEC Research
;;; Institute (NECI) under the following license.  By obtaining, using
;;; and/or copying this software, you agree that you have read,
;;; understood, and will comply with these terms and conditions:
;;; 
;;; Permission to use, copy, modify and distribute, including the right to
;;; grant others rights to distribute at any tier, this software and its
;;; documentation for research purposes only and without fee or royalty is
;;; hereby granted, provided that you agree to comply with the following
;;; copyright notice and statements, including the disclaimer, and that
;;; the same appear on ALL copies of the software and documentation,
;;; including modifications that you make for internal use or for
;;; distribution:
;;; 
;;; Copyright 1995 by the Massachusetts Institute of Technology.  All
;;; rights reserved.  Copyright 1995 by NEC Research Institute Inc. All
;;; rights reserved
;;; 
;;; THIS SOFTWARE IS PROVIDED "AS IS", AND M.I.T. and NECI MAKE NO
;;; REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.  By way of example,
;;; but not limitation, M.I.T. and NECI MAKE NO REPRESENTATIONS OR
;;; WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR
;;; THAT THE USE OF THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT
;;; INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER
;;; RIGHTS.
;;; 
;;; The name of the Massachusetts Institute of Technology or M.I.T., or
;;; NEC Research Institute, or NECI may NOT be used in advertising or
;;; publicity pertaining to distribution of the software.  Title to
;;; copyright in this software and any associated documentation shall at
;;; all times remain with M.I.T. and NECI, and USER agrees to preserve
;;; same.
;;; 

;;; -*- Mode: LISP; Base: 10; Package: 2DG; Syntax: Common-Lisp -*-
(in-package '2DG)

(defun BIGGEST-STRING-SMOOTHED (image sigma)
  (let* ((strings (edge-image-strings image))
	 (biggest-string (car strings)))
    (loop for s in (cdr strings)
	  doing
      (when (< (length biggest-string) (length s))
	(setf biggest-string s)))
    (let* ((theta-s (theta-s-from-string biggest-string))
	   (smoothed-theta-s (gaussian-smooth-uniform-theta-s theta-s sigma))
	   (reconstructed-string (string-from-theta-s theta-s))
	   (reconstructed-smoothed-string (string-from-theta-s smoothed-theta-s)))
      (list biggest-string reconstructed-string reconstructed-smoothed-string theta-s smoothed-theta-s))))

(defun TEST-SPRING-2D-SMOOTH (n k)
  (let ((string (make-quadratic-string 0 10 10)))
    (let ((smoothed-string (spring-2d-smooth string nil n k)))
      (loop for pt1 in string
	    for pt2 in smoothed-string
	    collecting
	    (distance pt1 pt2)))))

(defun MAKE-STRING-FROM-FUNCTION (start end function pixels-per-number)
  "Forms a string of points that satisfy y = (function x).  If pixels-per-number is 
   10, for example, the point (20 40) corresponds to the xy coordinate (2 4)."
  (let ((last-j (round (1- (* pixels-per-number (apply function (list start)))))))
    (loop for x from start to end by (/ 1 pixels-per-number)
	  for i = (round (* pixels-per-number x))
	  appending
	    (let ((j (round (* pixels-per-number (apply function (list x))))))
	      (if (= last-j j) (list (make-point i j))
		  (let ((points
			  (loop for intermediate-j from (1+ last-j) to j
				collecting
				  (make-point i intermediate-j))))
		    (setf last-j j)
		    points))))))

(defun MAKE-QUADRATIC-STRING (start end pixels-per-number)
  (make-string-from-function start end (lambda (x) (square x)) pixels-per-number))

(defun SMOOTH-LINE-TO-QUADRATIC ()
  "Take a curve which is a line segment from (0 0) to (2 4) and quadratic
   from (2 4) to (4 16).  Returns the curve and its theta-s graph, the
   smoothed curve and its theta-s graph, and the spring-smoothed, then smoothed
   curve and its theta-s graph."
  (let* ((string (make-string-from-function
		   0 4
		  (lambda (x) (if (< x 2) (* 2 x) (square x)))
		  20))
	 (theta-s (theta-s-from-string string))
	 (smooth-theta-s (gaussian-smooth-uniform-theta-s theta-s 2))
	 (smooth-string (string-from-theta-s smooth-theta-s))
	 (spring-string (spring-2d-smooth string nil 10 1))
	 (spring-theta-s (theta-s-from-string spring-string))
	 (smooth-spring-theta-s (gaussian-smooth-uniform-theta-s spring-theta-s 2))
	 (smooth-spring-string (string-from-theta-s smooth-spring-theta-s)))
    (list theta-s smooth-theta-s spring-theta-s smooth-spring-theta-s
