require 'sketchup.rb'



class ConicCurveInFace

	def initialize
		@@dek=25.mm
		@@po=10
		@@weight=1.0
		
		@ltcursor = 0
		if @ltcursor == 0
			cursor_img = File.join File.dirname(__FILE__), "mouse.png"
			@ltcursor = UI.create_cursor(cursor_img,  0, 0)
		end	
	
	end #initialize
	
	    def onSetCursor
    UI.set_cursor(@ltcursor)
    end
	
	
	def activate
	end #activate
	def onKeyUp(key, repeat, flags, view)
		if key==9 or key==15 ###or key==48 ### TAB =9 for PC, MAC 15/48 for some locales ?
			prompts = ["offset in mm", "number of points", "Weight"]
			defaults = [@@dek.to_mm,@@po,@@weight]
			results = UI.inputbox prompts, defaults, "Define parameters"
			@@dek=results[0].to_f.mm
			@@po=results[1].to_i
			@@weight=results[2].to_f
		end
	end
	def onMouseMove(flags, x, y, view)
		Sketchup.set_status_text "Pick Face, Offset = "+(@@dek.to_mm).to_s+" mm, Points = "+@@po.to_s+", weight = "+@@weight.to_s+"; TAB to change"
	end # onMouseMove
	def onLButtonDown(flags, x, y, view)
	# flags: 	Ctrl = 9
	# 			Shift = 5
	# 			ALT L = 33
	# 			Shift+Ctrl = 13
	#			Shift+ALT L = 37
	#			Ctrl+ALT L = 41
	#			Ctrl+Shift+ALT L = 45
	
		ph = Sketchup.active_model.active_view.pick_helper
		ph.do_pick x, y
		ref = ph.best_picked
		if ref.typename == "Face"
			ver=[]
			ver=ref.vertices
			conic(ver,@@dek,@@po,@@weight)
		end
	end
	def conic(ver,ofs,pc,wght)
		ofsver=[]
		for j in 0..ver.length-1
			if j == 0
				i=ver.length-1
				k=j+1
			else
				if j == ver.length-1
					i=ver.length-2
					k=0
				else
					i=j-1
					k=j+1
				end
			end
			p1=ver[i].position
			p2=ver[j].position
			p3=ver[k].position
			vec23=p2.vector_to(p3)
			vec21=p2.vector_to(p1)
			vecn=vec23.cross(vec21)
			ofsvec23=vec23.cross(vecn)
			ofsvec21=vec21.cross(vecn)
			angle=vec21.angle_between(vec23)
			ofsvec=ofsvec23.normalize!-ofsvec21.normalize!
			ofsvec.length = ofs/Math.sin(angle/2)
			ofsver<<p2-ofsvec
		end
		# UI.messagebox ofsver
		# ofsver.each do |p|
			# Sketchup.active_model.entities.add_cpoint(p)
		# end
		curvepoints=[]
		for j in 0..ofsver.length-1
			if j == 0
				i=ofsver.length-1
				k=j+1
			else
				if j == ofsver.length-1
					i=ofsver.length-2
					k=0
				else
					i=j-1
					k=j+1
				end
			end
			p1=Geom.linear_combination 0.5, ofsver[i], 0.5, ofsver[j]
			p2=ofsver[j]
			p3=Geom.linear_combination 0.5, ofsver[k], 0.5, ofsver[j]
			for n in 0..pc-1
				t=1.0*n/pc
				curvepoints<<Geom::Point3d.new(((wf1(t)*p1[0]+wf2(t)*p2[0]*wght+wf3(t)*p3[0])/(wf1(t)+wght*wf2(t)+wf3(t))),((wf1(t)*p1[1]+wf2(t)*p2[1]*wght+wf3(t)*p3[1])/(wf1(t)+wght*wf2(t)+wf3(t))),((wf1(t)*p1[2]+wf2(t)*p2[2]*wght+wf3(t)*p3[2])/(wf1(t)+wght*wf2(t)+wf3(t))))
			end
		end
		# UI.messagebox curvepoints
		Sketchup.active_model.entities.add_group(Sketchup.active_model.entities.add_face(curvepoints))
	end
	def wf1(t)
		(1-t)*(1-t)
	end
	def wf2(t)
		2*(1-t)*t
	end
	def wf3(t)
		t*t
	end
	# CCurveInFace = UI::Command.new("Conic Curve In Face") {Sketchup.active_model.select_tool ConicCurveInFace.new}
		# CCurveInFace.small_icon = "./CCurve/CCL.png"
		# CCurveInFace.large_icon = "./CCurve/CCL.png"
		# CCurveInFace.tooltip = "Create Conic Curve In Face"
	# CONIC = UI::Toolbar.new("Create Conic Curve In Face")
		# CONIC.add_item(CCurveInFace)
		# CONIC.show
	menu_name = File.basename(__FILE__)
end