[vtkusers] Aligning a cylinder between two points
Kevin Wright
krw at viz-solutions.com
Thu Aug 29 18:27:01 EDT 2002
>I've got a bit of a tranformation/rotation problem at hand. I want to
>draw a cylinder where the top and bottom are aligned to two points,
>p1=(x0, y0, z0) and p2=(x1, y1, z1), something like:
>
> ---------
> p1 -> | | <- p2
> ---------
>
>I'm using a vtkCylinderSource, vtkPolyDataMapper, and a vtkActor
lets say x = x1-x0 y = y1-y0 z = z1-z0
1. Set the height of the cylinder to be sqrt(x^2 + y ^2 + z^2) using
vtkCylinderSource->SetHeight
3. Cross the vector (x,y,z) with the default axis of the cylinder (0,1,0)
to get the axis to rotate about, which would be
(z,0,-x)
4. Get the angle between the two vectors (using the cosine law where one
vector is 0,1,0):
sin(angle) = y / sqrt(x^2 + y^2 + z^2)
5. Create a vtkTransform object
6. Apply a translation to the vtkTransform using Translate() to (x0 +
x/2,y0 + y/2,z0 + z/2)
7. Apply a rotation to the transform using RotateWXYZ with the angle from 4
and the vector from 3
8. Create a vtkTransformFilter and apply the vtkTransform from 5 to it.
9. Pass the vtkCylinderSource through the transform filter.
I'm no geometry expert, that may not be the most efficient way to do
things, but I think it will get the job done for you, unless I've made a
stupid math error. If you look at the examples for vtkTransformFilter,
I'll bet you find some code doing something similar to what you want to do.
Kevin.
More information about the vtkusers
mailing list