=begin
#-------------------------------------------------------------------------------------------------------------------------------------------------
#*************************************************************************************************
# Designed by Fredo6 - Copyright July 2015

# Permission to use this software for any purpose and without fee is hereby granted
# Distribution of this software for commercial purpose is subject to:
#  - the expressed, written consent of the author
#  - the inclusion of the present copyright notice in all copies.

# THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#-----------------------------------------------------------------------------
# Name			:   body_Lib6Pacing.rb
# Original Date	:   14 Jul 2015
# Description	:   Management of Pacing Interpolation functions (implementation)
#-------------------------------------------------------------------------------------------------------------------------------------------------
#*************************************************************************************************
=end


module Traductor

#--------------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------------
# class Pacing: Management of a Pacing Function
#--------------------------------------------------------------------------------------------------------------			 
#--------------------------------------------------------------------------------------------------------------			 

class Pacing

#INIT: Class instance initialization
def initialize__(*hargs)
	puts "Pacing Init"
	
	#Parsing the arguments
	hargs.each { |harg| harg.each { |key, value| parse_args(key, value) } if harg.class == Hash }
	
	#Initialization
	@type = :spline unless @type
	case @type
	when :spline
		spline_init
	else
	
	end
end

#INIT: Parsing of specifications
def parse_args(key, value)
	case key
	when :type
		@type = value
	end
end
	
#----------------------------------------------
# SPLINE: Spline type pacing
#----------------------------------------------

def spline_init
	@origin = Geom::Point3d.new 0, 0
	@final = Geom::Point3d.new 1, 1
	a13 = 1.0 / 3
	a23 = 2.0 / 3
	a12 = 0.5
	@point_13 = Geom::Point3d.new a13, a13
	@point_23 = Geom::Point3d.new a23, a23
	@point_12 = Geom::Point3d.new a12, a12
end
	
def interpolate(t, *args)
	#return t
	code, = args
	return t unless code
	vproc = @hsh_easings[code]
	return t unless vproc
	vproc.call(t)
end

def bezier_single_create(a, name=nil)
	#Creating the Easing function
	vec = Y_AXIS - X_AXIS
	pt_beg = ORIGIN
	pt_end = Geom::Point3d.new 1, 1, 0
	pt_mid = Geom::Point3d.new(0.5, 0.5, 0).offset(vec, 0.5 * Math.sqrt(2) * a)
	pts = [pt_beg, pt_mid, pt_end]
	eval_proc = proc { |t| G6::BezierCurve.evaluate(pts, t).x.to_f }
	
	#Storing the Easing function
	name = "bezier_single_#{a}" unless name
	@hsh_easings[name] = eval_proc
end

def bezier_double_create(a, name=nil)
	#Creating the Easing function
	vec = Y_AXIS - X_AXIS
	pt_beg = ORIGIN
	pt_end = Geom::Point3d.new 1, 1, 0
	pt1 = Geom.linear_combination 0.25, pt_beg, 0.75, pt_end
	pt2 = Geom.linear_combination 0.75, pt_beg, 0.25, pt_end
	pt1 = pt1.offset(vec, 0.25 * Math.sqrt(2) * a)
	pt2 = pt2.offset(vec, -0.25 * Math.sqrt(2) * a)
	pts = [pt_beg, pt1, pt2, pt_end]
	eval_proc = proc { |t| G6::BezierCurve.evaluate(pts, t).x.to_f }
	
	#Storing the Easing function
	name = "bezier_double_#{a}" unless name
	@hsh_easings[name] = eval_proc
end

def bezier_sinus_create(n, name=nil)
	#Creating the Easing function
	pi = Math::PI
	period = pi * n	
	eval_proc = proc { |t| Math.sin(t * period).abs }
	
	#Storing the Easing function
	name = "sinus_#{n}" unless name
	@hsh_easings[name] = eval_proc
end

def eval_all(t)
	@hsh_easings.each do |code, ls|
		puts "#{code} - #{t} -> #{interpolate(t, code)}"
	end	
	nil
end

end	#class PacingManager

end	#module Traductor

