; script: SphereFold.scm
; author: Tin Tran
; date: April 18, 2016
; experimental
(define (atan2 y x)
    (if (> x 0)
      (atan (/ y x))
      (if (< x 0)
        (if (< y 0)
          (- (atan (/ y x)) *pi*)
          (+ (atan (/ y x)) *pi*) )
        (cond ;; x is zero
          ((> y 0) (/ *pi* 2))
          ((< y 0) (- (/ *pi* 2)))
          (else ;; x==y==0 is typically undefined but we return 0 instead
            0 ) ) ) ) )
(define (script-fu-sphere-fold image layer 
            sections			
         )
	
		(let* 
		   (
		   (copied-layer 0)
		   (width (car (gimp-image-width image)))
		   (height (car (gimp-image-height image)))
		   (y-section 0)
		   (x-section 0)
		   (section-angle (/ 180 sections))
		   (section-width (/ width sections))
		   (section-height (/ height sections))
		   (x-radius (/ width 2))
		   (y-radius (/ height 2))
		   (x-angle-start 0)
		   (y-angle-start 0)
		   (x-angle-end 0)
		   (y-angle-end 0)
		   (x-select-start 0)
		   (y-select-start 0)
		   (x-select-end 0)
		   (y-select-end 0)
		   (transformed-item 0)
		   (x 0)
		   (y 0)
		   (nxn 0)
		   (nyn 0)
		   (theta 0)
		   (nr1 0)
		   (nr2 0)
		   (nr3 0)
		   (nr4 0)
		   (r1 0)
		   (r2 0)
		   (r3 0)
		   (r4 0)
		   (x_ 0)
		   (y_ 0)
		   (x1 0)
		   (y1 0)
		   (x2 0)
		   (y2 0)
		   (x3 0)
		   (y3 0)
		   (x4 0)
		   (y4 0)
		   )
			;(gimp-image-undo-disable image); DN = NO UNDO
			
			(gimp-image-undo-group-start image)                   ;undo-group in one step
			;(gimp-image-resize image (* width 2) (* height 2) (/ width 2) (/ height 2))
			;(gimp-layer-resize-to-image-size layer)
			(set! y-angle-start 180)
			(set! y-select-start 0)
			(while (< y-section sections)
			   
				(set! y-angle-end (- y-angle-start section-angle))
				(set! y-select-end (+ y-select-start section-height))
				
			    (set! x-section 0)
				(set! x-angle-start 180)
				(set! x-select-start 0)
				(while (< x-section sections)
					(set! x-angle-end (- x-angle-start section-angle))
					(set! x-select-end (+ x-select-start section-width))
					
					(gimp-image-select-rectangle image CHANNEL-OP-REPLACE x-select-start y-select-start section-width section-height)
					
					;top left
					;change below
					(set! x x-select-start)
					(set! y y-select-start)
					
					(set! nxn (- (/(* x 1.0) x-radius) 1.0))
					(set! nyn (- (/(* y 1.0) y-radius) 1.0))
					(set! theta (atan2 nyn nxn))
					;change below
					(set! nr1 (/ nxn (cos theta)))
					(gimp-message "before")
					(set! r1 (/
					           (+ (-(* 4.0 nr1) 2.0)
							      (* 2.0 (sqrt (+
								                (+ (- 0 (* (* nr1 nr1) 4.0))
								                   (* 4.0 nr1)
												)
												1
											 )
								       ) 
								  )
							   )
					          4.0
					         )
					)
					(gimp-message "after" )
					(set! x_ (+(*(* r1 (cos theta)) x-radius) x-radius))
					(set! y_ (+(*(* r1 (sin theta)) y-radius) y-radius))
					(set! x1 x_)
					(set! y1 y_)
					(gimp-message (number->string x1))
					(gimp-message (number->string y1))
					
					;top right ------------------
					;change below
					(set! x x-select-end)
					(set! y y-select-start)
					
					(set! nxn (- (/(* x 1.0) x-radius) 1.0))
					(set! nyn (- (/(* y 1.0) y-radius) 1.0))
					(set! theta (atan2 nyn nxn))
					;change below
					(set! nr2 (/ nxn (cos theta)))
					(set! r2 (/
					           (+ (-(* 4.0 nr2) 2.0)
							      (* 2.0 (sqrt (+
								                (+ (- 0 (* (* nr2 nr2) 4.0))
								                   (* 4.0 nr2)
												)
												1
											 )
								       ) 
								  )
							   )
					          4.0
					         )
					)
					(set! x_ (+(*(* r2 (cos theta)) x-radius) x-radius))
					(set! y_ (+(*(* r2 (sin theta)) y-radius) y-radius))
					(set! x2 x_)
					(set! y2 y_)
					
					;bottom left ------------------
					;change below
					(set! x x-select-start)
					(set! y y-select-end)
					
					(set! nxn (- (/(* x 1.0) x-radius) 1.0))
					(set! nyn (- (/(* y 1.0) y-radius) 1.0))
					(set! theta (atan2 nyn nxn))
					;change below
					(set! nr3 (/ nxn (cos theta)))
					(set! r3 (/
					           (+ (-(* 4.0 nr3) 2.0)
							      (* 2.0 (sqrt (+
								                (+ (- 0 (* (* nr3 nr3) 4.0))
								                   (* 4.0 nr3)
												)
												1
											 )
								       ) 
								  )
							   )
					          4.0
					         )
					)
					(set! x_ (+(*(* r3 (cos theta)) x-radius) x-radius))
					(set! y_ (+(*(* r3 (sin theta)) y-radius) y-radius))
					(set! x3 x_)
					(set! y3 y_)
					
					;bottom right ------------------
					;change below
					(set! x x-select-end)
					(set! y y-select-end)
					
					(set! nxn (- (/(* x 1.0) x-radius) 1.0))
					(set! nyn (- (/(* y 1.0) y-radius) 1.0))
					(set! theta (atan2 nyn nxn))
					;change below
					(set! nr4 (/ nxn (cos theta)))
					(set! r4 (/
					           (+ (-(* 4.0 nr4) 2.0)
							      (* 2.0 (sqrt (+
								                (+ (- 0 (* (* nr4 nr4) 4.0))
								                   (* 4.0 nr4)
												)
												1
											 )
								       ) 
								  )
							   )
					          4.0
					         )
					)
					(set! x_ (+(*(* r4 (cos theta)) x-radius) x-radius))
					(set! y_ (+(*(* r4 (sin theta)) y-radius) y-radius))
					(set! x4 x_)
					(set! y4 y_)
					
					(if (and (and (and (<= nr1 1.0) (<= nr2 1.0)) (<= nr3 1.0)) (<= nr4 1.0))
					   (begin
					     ;do nothing as we already calculated everything it's good to go
					   )
					   (begin
					      ;else hide this in the corner.
							(set! x1 0)
							(set! y1 0)
							(set! x2 0)
							(set! y2 0)
							(set! x3 0)
							(set! y3 0)
							(set! x4 0)
							(set! y4 0)
					   )
					)
					(set! transformed-item (car (gimp-item-transform-perspective layer
					                                x1 y1
													x2 y2
													x3 y3
													x4 y4
					                                ;(+ x-radius (*(cos top-left-angle) (- x-select-start x-radius) ))
													;(+ y-radius (*(sin top-left-angle) (- y-select-start y-radius ) ))
													
													;(+ x-radius (*(cos top-right-angle) (-  x-select-end x-radius) ))
													;(+ y-radius (*(sin top-right-angle) (-  y-select-start y-radius) ))
													
													;(+ x-radius (*(cos bottom-left-angle) (-  x-select-start x-radius) ))
													;(+ y-radius (*(sin bottom-left-angle) (-  y-select-end y-radius) ))
													
													;(+ x-radius (*(cos bottom-right-angle) (-  x-select-end x-radius) ))
													;(+ y-radius (*(sin bottom-right-angle) (-  y-select-end y-radius) ))
													
												)
										   )
					)
					
					(gimp-floating-sel-to-layer transformed-item)
					
					(set! x-angle-start (- x-angle-start section-angle))
					(set! x-select-start (+ x-select-start section-width))
					(set! x-section (+ x-section 1))
				)
				(set! y-angle-start (- y-angle-start section-angle))
				(set! y-select-start (+ y-select-start section-height))
				(set! y-section (+ y-section 1))
			)
			
		   ;(gimp-image-undo-enable image) ;DN = NO UNDO
			(gimp-image-undo-group-end image)                     ;undo group in one step
			(gimp-displays-flush)
	    )
	
	
    
) ;end of define
(script-fu-register
  "script-fu-sphere-fold"         ;function name
  "<Image>/Script-Fu/Create New/Sphere Fold ..."    ;menu register
  "Creates Map Fold effect"       ;description
  "Tin Tran"                          ;author name
  "copyright info and description"         ;copyright info or description
  "2016"                          ;date
  "RGB*, GRAY*"                        ;mode
  SF-IMAGE      "Image" 0                   
  SF-DRAWABLE   "Layer" 0
  SF-ADJUSTMENT "Fold sections" '(3 2 30 1 5 0 0)
)
