=begin

(c) Julia Christina Eneroth 2013

Trying to replicate the exact same thing Sketchup does after using Move, Rotate, Scale, Offset, Line, Freehand, Arc, Circle, Polygon, Rectngle, Push-Pull or Follow-Me Tool.
Since the API lacks a call for this, and the group-explode workaround might cause Sketchup to crash, it has to be reinvented.

=end

module Ene_3dRotate

def self.on_edge?(e, p, include_vertices)
	#Checks if point is between edge vertices
	#Doesn't check if on same line, just if closer to edge's vertices than they are to each other.
	#Definition might need a better name
			
	p_start = e.start.position
	p_end = e.end.position

	if include_vertices
		p_start.distance(p) <= p_start.distance(p_end) && p_end.distance(p) <= p_end.distance(p_start)
	else
		p_start.distance(p) < p_start.distance(p_end) && p_end.distance(p) < p_end.distance(p_start)
	end
	
end#def

def self.break_edge_on_vertices(entities1, entities2, also_break_on_edge = true)
	#Call this after a transformations is done
	#Very similar to what the native tools do but lacks API call to it
	
	#Turn entities objects into entity arrays
	if entities1.class == Sketchup::Entities
		entities = []
		entities1.each { |i| entities << i}
		entities1 = entities
	end
	if entities2.class == Sketchup::Entities
		entities = []
		entities2.each { |i| entities << i}
		entities2 = entities
	end
	
	#Add bounding edges of faces
	entities1edges = []
	entities1.each { |i| i.edges.each{|j| entities1edges << j} if i.class == Sketchup::Face}
	entities1+= entities1edges
	entities1.uniq!
	
	entities2edges = []
	entities2.each { |i| i.edges.each{|j| entities2edges << j} if i.class == Sketchup::Face}
	entities2+= entities2edges
	entities2.uniq!
		
	#Find intersections
	#Don't split edges here since that would prevent the new edges from being intersected with the part of the edge chopped off
	to_split = []
	#Each element of to_split is an array containing and edge and a point
	entities1.each do |e1|
		next unless e1.class == Sketchup::Edge
		entities2.each do |e2|
			next unless e2.class == Sketchup::Edge
		
			l1 = e1.line
			l2 = e2.line
			p = Geom.intersect_line_line l1, l2
			
			next unless p
			next if !on_edge?(e1, p, true) || !on_edge?(e2, p, true)
			
			if also_break_on_edge
				#Sketchup 7+ behavior. break intersecting edges
				
				if on_edge?(e1, p, false)
					#intersection is on edge 1 and not on one of its vertices
					to_split << [e1, p]
				end
				if on_edge?(e2, p, false)
					#intersection is on edge 2 and not on one of its vertices
					to_split << [e2, p]
				end
			else
				#Sketchup 6- behavior. Only break edges on endpoints
				
				e1.vertices.each do |v|
					to_split << [e2, p] if p == v.position
				end
				e2.vertices.each do |v|
					to_split << [e1, p] if p == v.position
				end
				
			end
			
		end
	end
	
	#Group edges in to_split
	#Each element in to_split should now be an array containing an edge and an array of points where to split it
	#Every time the edge is split the new edge is added to the edge sub-array
	#Later the points try splitting the split-off edges as well as the edge it originally were meant to split
	to_split_new = []
	to_split.each do |i|
		#Find element in new array whose edge is the one of this element in old array
		element_in_new = (to_split_new.select { |j| j[0][0] == i[0] } )[0]
		if element_in_new
			#If corresponding element were found, add intersection point from this
			element_in_new[1] << i[1]
		else
			#Otherwise, create a new element corresponding this element
			to_split_new << [[i[0]], [i[1]]]
		end
	
	end
	to_split = to_split_new
	
	#Split edges
	to_split.each do |i|
		i[1].each do |p|
			i[0].each do |e|
				new_e = e.split p
				if new_e
					i[0] << new_e#Save split off edge to array so other points meant to split the same edge can be tried on the split off edges too
					break
				end
			end#each
		end#each
	end#each
	
	#Split faces
	if also_break_on_edge		
		
		#Simple 1d array of edges split
		edges_split = []
		to_split.each do |i|
			i[0].each { |j| edges_split << j}
		end#each
		
		#Find all faces connected to a split edge
		faces = []
		edges_split.each do |i|
			i.faces.each { |j| faces << j}
		end#each
		faces.uniq!
		
		#NOTE: only intersect faces in same plane
		
		#Find all bounding edges to faces
		faces_and_edges = []
		faces.each do |i|
			i.edges.each { |j| faces_and_edges << j }
		end#each
		faces_and_edges.uniq!
		
		ents = Sketchup.active_model.entities
		i_t = Geom::Transformation.new
		ents.intersect_with false, i_t, ents, i_t, true, faces_and_edges
		
	end#if

end#def

end#module