Generating 3D Cylinder with Rotationmatrix

。_饼干妹妹 提交于 2021-02-04 08:14:25

问题


I am trying to create a cylinder in 3D space. I get a start and endpoint in 3D and to place the vertices, I need to create 2 circles for the bottom and the top of the cylinder. I thought about making a vector u with coordinates (1,0,0) and calculate a direction vector dir = end - start. Now I make the cross product to get a vector w = u x dir: this way I am 90 degrees away from the dir vector with w. The problem is, how do I calculate the next position w' with an arbitrary angle between w and w'. I Read a lot about rotation matrix, and that i can rotate around X-Axis with something like:

Rx(angle) =   
(1   0          0          )  
(0   cos(angle) -sin(angle))  
(0   sin(angle) cos(angle) ) 

but the direction can be something like (1.442, -3.22, 7.23). So it isn't always the x-axis but maybe I need to rotate for every new w' in every axis. And I don't know how to figure out what angle I need to calculate, how to calculate and which angle belongs to witch rotation matrix.


回答1:


You need basis vectors for this (which can be encoded into transform matrix).

So let assume you got:


p0[3],p1[3] - 3D center points (x,y,z) of your cylinder bases
r - radius

To construct transform matrix you need


O[3] - origin position
X[3],Y[3],Z[3] - basis vectors

So you can exploit cross product to get parallel vectors:

O = p0;
Z = p1-p0;
Y = (1.0,0.0,0.0);
if (|dot(Y,Z)/|Z||>0.75) Y = (0.0,1.0,0.0); // this just make sure Z and Y are not parallel
X = cross(Y,Z);
Y = cross(Z,X);
X = r*X/|X|;
Y = r*Y/|Y|;  

Now construct your 4x4 transform matrix M or use the X,Y,Z,O directly to compute the vertexes:

base0:

x=cos(a);
y=sin(a);
z=0.0;

base1:

x=cos(a);
y=sin(a);
z=1.0;

Where a = <0.0,2.0*PI> is angle. XY plane is parallel to bases and z is cylinder axis. To convert these (x,y,z) to World coordinates use:

(x',y',z') = M * (x,y,z)

Which can be done natively in OpenGL by using M as part of GL_MODELVIEW or do this instead:

x' = O[0] + x*X[0] + y*Y[0] + z*Z[0];
y' = O[1] + x*X[1] + y*Y[1] + z*Z[1];
z' = O[2] + x*X[2] + y*Y[2] + z*Z[2];

Take a look at related QAs:

  • my simple C++ glCircle3D
  • Understanding 4x4 homogenous transform matrices


来源:https://stackoverflow.com/questions/41494329/generating-3d-cylinder-with-rotationmatrix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!