Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change of Vertice distribution after applying rotation transformation with BRepBuilderAPI_Transform #1359

Open
chrisfeldkircher opened this issue Aug 8, 2024 · 0 comments

Comments

@chrisfeldkircher
Copy link

chrisfeldkircher commented Aug 8, 2024

Hello,

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.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant