CGAL / cgal

The public CGAL repository, see the README below
https://github.com/CGAL/cgal#readme
Other
4.85k stars 1.38k forks source link

Transformation #5966

Closed petrasvestartas closed 2 years ago

petrasvestartas commented 3 years ago

Searching for rotation transformation examples.

Issue Details

I have hard time understanding CGAL transformation documentation. For simple scaling, translation it is straight forward. But I cannot find any rotation examples. I would like to rotate a vector or a point around and axis (point and vector). Since I could not find any samples, I started filling rotation matrix from scratch using other github repositories. But it does not feel right... since CGAL is such a powerful library.

Are the any samples regarding rotation transformations?

lrineau commented 3 years ago

You are right that there might be something lacking. To create a rotation using CGAL::Aff_transformation_3, you need to write your own transformation matrix.

petrasvestartas commented 3 years ago

Thank you for clarification. I try to transfer code from OpenNurbs. I wish there would be something as user friendly as this repo: https://github.com/mcneel/opennurbs/blob/484ba88836bbedff8fe0b9e574fcd6434b49c21c/opennurbs_xform.cpp

afabri commented 3 years ago

To give it historical perspective (and don't take it as an excuse), the exact computing paradigm, namely to do computations with, say arbitrary precision rational numbers, and to combine it with interval arithmetic to make it faster, does not go well along with trigonometric functions. That's why you see functions such as rational_rotation_approximation().

petrasvestartas commented 3 years ago

Dear @afabri,

Thank you for a reply. Generally it feels that what I am doing is wrong, but at the same time I believe that these transformations must be implemented by multiple people before me and I really want to use CGAL only. And I understand that these arithmetic operations are not precise.

Are there any examples you did before for rotations that I could somehow reuse?

sloriot commented 3 years ago

Does that help?

void get_rotation(Kernel::FT angle, Kernel::Vector_3 axis, Kernel::Aff_transformation_3& rot)
{
  //create matrix of the rotation
  Kernel::RT c = cos(angle),
      s = sin(angle),
      ux(axis.x()),uy(axis.y()),uz(axis.z());
  Kernel::RT matrix[12] =
  {
    ux*ux*(1-c)+c, ux*uy*(1-c)-uz*s, ux*uz*(1-c)+uy*s, 0,
    ux*uy*(1-c)+uz*s, uy*uy*(1-c)+c, uy*uz*(1-c)-ux*s, 0,
    ux*uz*(1-c)-uy*s, uy*uz*(1-c)+ux*s, uz*uz*(1-c)+c, 0
  };
  rot = Kernel::Aff_transformation_3(matrix[0],matrix[1],matrix[2],
      matrix[3],matrix[4],matrix[5],
      matrix[6],matrix[7],matrix[8],
      matrix[9],matrix[10],matrix[11]);
}
petrasvestartas commented 3 years ago

Thank you very much @sloriot .

Is the angle in radians or degrees? And if I understand correctly, the rotation happens around origin point 0,0,0?

sloriot commented 3 years ago

radians

petrasvestartas commented 3 years ago

Thank you