Rotation and Translation

DVN1991

CAD practitioner
HI everyone,

May I have some help on how to do rotations and translations of an AIS_Shape.
Thanks
regards,
 

JSlyadne

Administrator
Staff member
Hi! Could you please elaborate a bit on the case you're working on? Are you interesting in getting how to deal with AIS_Manipulator, or general mechanism? Did you check this thread?
 

DVN1991

CAD practitioner
Hi, sorry for skipping that info.
what I want to do is just modify the location of a previously created AIS_Shape. I am using a UI in which I have some spinboxes that will control the Rotation and Translation in X,Y,Z axis.
 

natalia

Moderator
Staff member
Hi @DVN1991,

Firstly, you should decide which structure you’d like to changed.

It might be the presentation object, AIS_Shape, or topology object (TopoDS_Shape). AIS_Shape is just a visual wrap for the topology with some visual parameters. The topology object is an internal data of the presentation that might be used in algorithms or data model or other.

In case, if you change the topology object (TopoDS_Shape), the presentation is just need to be redisplayed. Here, you may use the BRepBuilderAPI_Transform.

Or, you may change the presentation object only. It is more light-performance change. Here the local transformation of the AIS_Shape should be changed only, as it’s described in thread mentioned above. Here you need to prepare gp_Trsf on your values from spin boxes and call SetLocalTransformation() for your AIS_Shape. Just do not forget here to unite the current and the new one transformation.
 

natalia

Moderator
Staff member
hi @blobfish, good link)

Agree) In case when we needn't history of locations later (and usually we really needn't), using Located() of TopoDS_Shape looks better in performance. Be careful here, this case is appropriate for us only if we are ready to change basic TopoDS_Shape. If in our application we may not changing it, but would like working with the topology changed , it's also possible to create a copy of the shape and set location for this copy (the method is EmptyCopied of the shape - see internal implementation of AIS_ColoredShape or draw command as example of using it)
 

blobfish

CAD community veteran
Be careful here, this case is appropriate for us only if we are ready to change basic TopoDS_Shape. If in our application we may not changing it, but would like working with the topology changed , it's also possible to create a copy of the shape and set location for this copy (the method is EmptyCopied of the shape - see internal implementation of AIS_ColoredShape or draw command as example of using it)
I guess you are talking about working directly with the data structures combined with the implicit sharing(COW) nature of OpenCasCade? The modeling algorithms do, or at least should, handle COW. I mean the following works as expected. Did I not understand your point?
C++:
    TopoDS_Solid box0 = BRepPrimAPI_MakeBox(gp_Pnt(0.0, 0.0, 0.0), gp_Pnt(10.0, 10.0, 10.0)).Solid();
    gp_Trsf freshTransform;
    freshTransform.SetTranslation(gp_Vec(5.0, 5.0, 5.0));
    TopLoc_Location freshLocal(freshTransform);
    auto box1 = box0.Located(freshLocal);
    
    BRepAlgoAPI_Fuse bop0(box0, box1);
    auto out0 = bop0.Shape();
    assert(!out0.IsNull());
    ocs::writeBRep(out0, "locatedBool0.brep");
    
    BRepAlgoAPI_Fuse bop1(box0, box1);
    auto out1 = bop1.Shape();
    assert(!out1.IsNull());
    ocs::writeBRep(out1, "locatedBool1.brep");
    
    ocs::dumpShapes(out1);
    ocs::dumpCheck(out1);
 

DVN1991

CAD practitioner
The part I created is translating and rotating nicely, but do you know how could I rotate it according to its center point and not to the global one?
 

blobfish

CAD community veteran
The part I created is translating and rotating nicely, but do you know how could I rotate it according to its center point and not to the global one?
Rotations are understood to be at absolute origin. So what you can do is create a translation matrix from your desired rotation point to absolute, apply the rotation and then translate back. sudo code:
Code:
template <Matrix> Matrix rotationAboutPoint(const Matrix &rotation, const Matrix::vector_type &rotationPoint)
{
  Matrix translation = Matrix::createTranslation(rotationPoint);
  return -translation * rotation * translation;
}
 

DVN1991

CAD practitioner
I solved it in this way, in SetRotation can be defined the rotation point, which is previously translated.

C++:
    gp_Pnt pto2t(translation.x(),translation.y(),translation.z());
    rotxTrsf.SetRotation(gp_Ax1(pto2t,gp_Dir(1,0,0)),DEGREE2RAD( rotation.x()));     
    rotyTrsf.SetRotation(gp_Ax1(pto2t,gp_Dir(0,1,0)),DEGREE2RAD( rotation.y()));
    rotzTrsf.SetRotation(gp_Ax1(pto2t,gp_Dir(0,0,1)),DEGREE2RAD( rotation.z()));
    gp_Trsf transformacion = rotxTrsf*rotyTrsf*rotzTrsf;
 

soupyx

CAD practitioner
I solved it in this way, in SetRotation can be defined the rotation point, which is previously translated.

C++:
    gp_Pnt pto2t(translation.x(),translation.y(),translation.z());
    rotxTrsf.SetRotation(gp_Ax1(pto2t,gp_Dir(1,0,0)),DEGREE2RAD( rotation.x()));
    rotyTrsf.SetRotation(gp_Ax1(pto2t,gp_Dir(0,1,0)),DEGREE2RAD( rotation.y()));
    rotzTrsf.SetRotation(gp_Ax1(pto2t,gp_Dir(0,0,1)),DEGREE2RAD( rotation.z()));
    gp_Trsf transformacion = rotxTrsf*rotyTrsf*rotzTrsf;
Sorry for bumping this 1 year old topic, I don't wanna make a new topic for such a small question.

This works,but how do i translate it to a different location without loosing it's rotation with this method?


EDIT: found it myself, for anyone having issues:

Code:
// Define your rotation angles and center point
double rx = 45.0; // Angle of rotation around the X-axis in degrees
double ry = 30.0; // Angle of rotation around the Y-axis in degrees
double rz = 60.0; // Angle of rotation around the Z-axis in degrees
gp_Pnt centerPoint(0.0, 0.0, 0.0); // Center point for rotation

// Create transformation matrices for rotation
gp_Trsf trsfRx, trsfRy, trsfRz;
trsfRx.SetRotation(gp_Ax1(centerPoint, gp_Dir(1, 0, 0)), DegreeToRad(rx));
trsfRy.SetRotation(gp_Ax1(centerPoint, gp_Dir(0, 1, 0)), DegreeToRad(ry));
trsfRz.SetRotation(gp_Ax1(centerPoint, gp_Dir(0, 0, 1)), DegreeToRad(rz));

// Combine the rotation transformations
gp_Trsf rotationTransformation = trsfRx * trsfRy * trsfRz;


// Define the translation vector
gp_Vec translationVector(10.0, 20.0, 30.0); // Adjust the values as needed

// Create a translation transformation
gp_Trsf translationTransformation;
translationTransformation.SetTranslation(translationVector);

// Combine the rotation and translation transformations
gp_Trsf finalTransformation = translationTransformation * rotationTransformation;

// Apply the final transformation to your shape
 
Last edited:
Top