user3748950
user3748950

Reputation: 19

Python-OCC BRepBuilderAPI_Transform causing vertex distribution to change

I am currently developing a geometric application that utilizes 3D Zernike moments to identify similar shapes. While these moments are designed to be rotationally invariant, I have observed an issue after applying the following transformation using OCC:

modelName2 = "0_20.stp"
modelPath2 = dataPath + modelName2

# returns a TopoDS_Shape 
model2 = readModel(modelPath2)
    
rotationAxis = gp_Ax1(gp_Pnt(0, 0, 0), gp_Dir(0, 1, 1))

rotationAngle1 = 80.0 
rotation_transform = gp_Trsf()
rotation_transform.SetRotation(rotationAxis, rotationAngle1 * (math.pi / 180.0))

rotated_model2 = BRepBuilderAPI_Transform(model2, rotation_transform).Shape()

It appears that the vertex distribution changes after this transformation. This was confirmed by observing differences in the principal axes between the original and rotated models. It's also worth noting, that using freeCAD to rotate the model, then import it via OCC and perform PCA results in receiving the same axes in the rotated and non-rotated case.

Here is the code used to extract vertices and align the model to its principal axes:

def getVertices(shape):
  if not isinstance(shape, TopoDS_Shape):
      raise TypeError("The provided shape is not a valid TopoDS_Shape")

  vertices = []
  explorer = TopExp_Explorer(shape, TopAbs_VERTEX)

  while explorer.More():
      vertex = topods.Vertex(explorer.Current())
      pnt = BRep_Tool.Pnt(vertex)
      vertices.append([pnt.X(), pnt.Y(), pnt.Z()])
      explorer.Next()

  return np.array(vertices)

def alignModelToPrincipalAxes(shape, use_sklearn=True):

"""
   Align the model to its principal axes using PCA.
"""

vertices = getVertices(shape)

# Center the vertices around the origin
centroid = np.mean(vertices, axis=0)
centered_vertices = vertices - centroid

# Normalize the vertices
max_distance = np.max(np.linalg.norm(centered_vertices, axis=1))
normalized_vertices = centered_vertices / max_distance

if use_sklearn:
    pca = PCA()
    pca.fit(normalized_vertices)
    principal_axes = pca.components_.T

I am trying to understand why the applied rotation alters the symmetry axes and, consequently, the vertex distribution. Any insights or suggestions on this matter would be greatly appreciated.

Upvotes: 0

Views: 49

Answers (0)

Related Questions