I have two possibly convex faces in two separate groups they both live on XY plane. I want to check analytically if they overlap (have common non-zero area).
I have explored solutions that look for vertices on the face and intersecting edges. This is sufficient for most cases but there are some where this is not enough, especially when the edges are overlapping (this can be partially solved by using edgeuse.reversed? and comparing edges of with parallel? and samedirection?), or when the intersections happen at the vertices (these are the most difficult as it seems that to solve them it is not enough to look at pairs of edges from two different faces, but rather more broadly).

Is there perhaps a library that solves this problem?
best
EDIT: faces can have holes
Below is one concept that may work for what you’re asking. In this case using the sketchup raytest method to walk around the perimeter of one shape, vertex by vertex, looking directly at the next vertex, and determining if there is any other object obscuring the view.
And here’s a model to test it with
raytest overlapped shapes.skp (100.1 KB)
model = Sketchup.active_model
ents = model.active_entities
sel = model.selection
shape1 = ents[0]
puts "Shape1 is #{shape1.inspect}"
sel.clear
sel.add(shape1)
edges = shape1.entities.grep(Sketchup::Edge)
puts "Shape1 has #{edges.size} edges"
puts
tr = shape1.transformation
overlap_found = false
edges.each { | edge |
point = edge.start.position.transform(tr)
puts "Startpoint #{point}"
vector = point.vector_to(edge.end.position.transform(tr))
puts "Vector #{vector}"
ray = [point, vector]
puts result = model.raytest(ray, false) # Consider hidden geometry when
puts "Raytest Result Size #{result.size}"
puts "Found Intersection at #{result[0]}"
item = result[1][1]
puts "Hit item is #{item}"
if !edges.include?(item)
overlap_found = true
break
else
puts "In same Container"
end
puts
}
puts
if overlap_found
puts "Overlapped Shape Found"
else
puts "No Overlapped Shape Found"
end
nil
There is a module method that could help:
There is also:
2 Likes
1• when the Faces are at the same level of the hierarchy, a 3-face appears at their intersection. Knowing the total number of faces, you can understand whether an extra face has appeared or not.
Sketchup.active_model.selection.grep(Sketchup::Group).each{|g| g.explode}
Sketchup.active_model.active_entities.grep(Sketchup::Face).count # 2-not 3-yes
2• You can put each face with edges in a separate group and perform subtraction.
model = Sketchup.active_model
faces=model.selection.grep(Sketchup::Group) #Group=face+edges
group = model.active_entities.add_group
transformation1 = Geom::Transformation.new
transformation2 = group.transformation
result=model.active_entities.intersect_with(true, transformation1, group, transformation2, true, [faces[0],faces[1]])
if result.empty?
p "-"
else
p "+"
group.erase!
end