find identical faces

Need help, or want to share a macro? Post here!
User avatar
bernd
Posts: 8233
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

find identical faces

Postby bernd » Mon Jan 18, 2016 10:08 am

Asumed I have a compound off lots off faces. What would be the cheapest way to find two identical faces? What would be best to check for during iteration through all the faces ?
wmayer
Site Admin
Posts: 14640
Joined: Thu Feb 19, 2009 10:32 am

Re: find identical faces

Postby wmayer » Mon Jan 18, 2016 2:01 pm

First, you have to define what you consider an identical face. In OCC if the two shapes reference the same TShape you can use one of these methods:
Standard_Boolean IsPartner(const TopoDS_Shape& other) const;
//! Returns True if two shapes are same, i.e. if they <br>
//! share the same TShape with the same Locations. <br>
//! Orientations may differ. <br>
Standard_Boolean IsSame(const TopoDS_Shape& other) const;
//! Returns True if two shapes are equal, i.e. if they <br>
//! share the same TShape with the same Locations and <br>
//! Orientations. <br>
//! <br>
Standard_Boolean IsEqual(const TopoDS_Shape& other) const;
If you have two faces that only look identical but don't share a common TShape you must implement some geometrical matching function if you want that too.
User avatar
bernd
Posts: 8233
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: find identical faces

Postby bernd » Mon Jan 18, 2016 3:29 pm

I'm playing with compsolids (viewtopic.php?f=8&t=13830) I just would like to test by python if I had success or not in building c compsolid by iterating through all faces and find duplicates which indicates a non valid compsolid.
wmayer wrote:First, you have to define what you consider an identical face ...
If you have two faces that only look identical but don't share a common TShape you must implement some geometrical matching function if you want that too.
That was my question about. But may be bad written down. What is the best geometrical matching function to compare faces. First idea was compare the cound of edges, vertexes and the centre of mass and the area. But surely there are much better geometrical matches.

wmayer wrote: In OCC if the two shapes reference the same TShape you can use one of these methods:
Standard_Boolean IsPartner(const TopoDS_Shape& other) const;
//! Returns True if two shapes are same, i.e. if they <br>
//! share the same TShape with the same Locations. <br>
//! Orientations may differ. <br>
Standard_Boolean IsSame(const TopoDS_Shape& other) const;
//! Returns True if two shapes are equal, i.e. if they <br>
//! share the same TShape with the same Locations and <br>
//! Orientations. <br>
//! <br>
Standard_Boolean IsEqual(const TopoDS_Shape& other) const;
I assume there is no python function implemented in FreeCAD to check if two faces use the same TShape ?!
jmaustpc
Posts: 9558
Joined: Tue Jul 26, 2011 6:28 am
Location: Australia

Re: find identical faces

Postby jmaustpc » Mon Jan 18, 2016 3:55 pm

bernd wrote:I'm playing with compsolids (viewtopic.php?f=8&t=13830) I just would like to test by python if I had success or not in building c compsolid by iterating through all faces and find duplicates which indicates a non valid compsolid.
If you are creating them with code something like this

Code: Select all

compSolid = Part.CompSolid([solid1, solid2])
then couldn't you just count the total number of faces in the compsolid and compare that the total of the two component solids and see if the total is one less?

e.g. Two cubes 6 face each = 12 and the compsolid would have 11.

Of course since this would only work in cases like above where you know how many face to expect in the compsolid, I don't know if it would help much?

Jim
wmayer
Site Admin
Posts: 14640
Joined: Thu Feb 19, 2009 10:32 am

Re: find identical faces

Postby wmayer » Mon Jan 18, 2016 4:23 pm

Code: Select all

box=Part.makeBox(10,10,10)
sphere=Part.makeSphere(3,App.Vector(5,5,5))
cut=box.cut(sphere)
compSolid = Part.CompSolid([cut.Solids[0],sphere])
Part.show(compSolid)

# -----------------------------------------------

s1=compSolid.Solids[0] # 7 faces
s2=compSolid.Solids[1] # 1 face

for i in s1.Faces:
  for j in s2.Faces:
    print("Is same: %s" % (i.isSame(j)))
User avatar
bernd
Posts: 8233
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: find identical faces

Postby bernd » Tue Jan 19, 2016 1:16 pm

Thanks werner and jim for your help. A compsolid really is what it says a container for solids sharing the same face. If the solids do not share the same face the compsolid is invalid

Code: Select all

import Part
box=Part.makeBox(10,10,10)
sphere=Part.makeSphere(3,App.Vector(5,5,5))
cut=box.cut(sphere)
s1 = cut.Solids[0]
has_same_faces = False
for i in s1.Faces:
  for j in sphere.Faces:
    print("Is same: %s" % (i.isSame(j)))
    if i.isSame(j):
        Part.show(j)
        has_same_faces = True

if has_same_faces:
    print "Found same faces in the solids, the CompSolid could be valid!"
    compSolid = Part.CompSolid([s1,sphere])
    docobj = App.ActiveDocument.addObject("Part::Feature","Sphere_in_Box")
    docobj.Shape = compSolid


box2=Part.makeBox(10,10,10,App.Vector(10,0,0))
has_same_faces = False
for i in s1.Faces:
  for j in box2.Faces:
    print("Is same: %s" % (i.isSame(j)))
    if i.isSame(j):
        Part.show(j)
        has_same_faces = True

if has_same_faces:
    print "Found same faces in the solids, the CompSolid could be valid!"
else:
    print "Found no same faces in the solids, the CompSolid will not be valid!"
    compSolid = Part.CompSolid([box,box2])
    docobj = App.ActiveDocument.addObject("Part::Feature","Box_on_Box")
    docobj.Shape = compSolid
The two boxes have contact faces which have the same geometry, but they do not share the same geometry. Am i right?

(it is difficault in englich in german there are two words. It would be: Die faces haben die Gleiche geometrie, aber nicht dieselbe geometrie. http://www.selbe-gleiche.de/ Thus it is no CompSolid.)
wmayer
Site Admin
Posts: 14640
Joined: Thu Feb 19, 2009 10:32 am

Re: find identical faces

Postby wmayer » Tue Jan 19, 2016 1:59 pm

The two boxes have contact faces which have the same geometry, but they do not share the same geometry. Am i right?
The two boxes have contact faces which have the equal geometry, but they do not share the same geometry. Am i right?
User avatar
bernd
Posts: 8233
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: find identical faces

Postby bernd » Tue Jan 19, 2016 2:13 pm

wmayer wrote:
The two boxes have contact faces which have the same geometry, but they do not share the same geometry. Am i right?
The two boxes have contact faces which have the equal geometry, but they do not share the same geometry. Am i right?
Exact ! The code you provided earlier (isSame) shows it very well.

I have tried to rebuild the solids by shape builder gui and by python but I'm not able to make them sharing the same geometry for the contact face.
User avatar
bernd
Posts: 8233
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: find identical faces

Postby bernd » Tue Jan 19, 2016 2:15 pm

BTW: the boolean cut works like a charm for compsolids ... viewtopic.php?f=8&t=13830&p=111039#p111039
User avatar
DeepSOIC
Posts: 6872
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: find identical faces

Postby DeepSOIC » Tue Jan 19, 2016 7:54 pm

If you want to check if there are equal but not shared faces in a compsolid, you can try to fuse all faces together, and check if the fusion has the same number of faces as the input. It's going to be slow, I guess..