如何在VRML中的原点和某些坐标之间创建圆柱体

时间:2015-04-06 08:08:24

标签: math 3d vrml

我以编程方式在VRML 2.0中创建一些文件。我需要构造一个圆柱体,其底部位于原点,顶部位于给定的坐标处,但我在计算旋转时遇到了一些问题。我用谷歌搜索过,但VRML 2.0的文档似乎非常缺乏。

我认为spherical coordinates最适合我尝试做的事情,因此我计算了目标点(x,y,z)的球面坐标(r,theta,phi)。然后我创建了下面的文件。

#VRML V2.0 utf8

DEF v1 Transform {
    translation 0 0 0
    children Shape {
        geometry Sphere {radius .5}
    }
}
DEF v2 Transform {
    translation x y z
    children Shape {
        geometry Sphere {radius .5}
    }
}
DEF edge Transform {
    translation 0 0 0
    children Transform {
        rotation 0 1 0 theta-pi/2
        children Transform {
            rotation 0 0 1 phi-pi/2
            children Transform {
                translation 0 r/2 0
                children Shape {
                    geometry Cylinder {
                        radius .08
                        height r
                    }
                }
            }
        }
    }
}

这是一个带有一些示例值的版本:

#VRML V2.0 utf8

DEF v1 Transform {
    translation 0 0 0
    children Shape {
        geometry Sphere {radius .5}
    }
}
DEF v2 Transform {
    translation 4 3 3
    children Shape {
        geometry Sphere {radius .5}
    }
}
DEF edge Transform {
    translation 0 0 0
    children Transform {
        rotation 0 1 0 -0.54041949679
        children Transform {
            rotation 0 0 1 -0.92729521779
            children Transform {
                translation 0 2.915475947 0
                children Shape {
                    geometry Cylinder {
                        radius .08
                        height 5.830951895
                    }
                }
            }
        }
    }
}

如果您查看最后一个文件,您会看到圆柱实际上非常接近,但还没有完全接近。

1 个答案:

答案 0 :(得分:1)

O.K。,很长一段时间以来我做过这些事情,但我想我知道你的方法不起作用以及如何做到这一点。正如您所尝试的那样,使用球面坐标计算来自FIXED参考框架,该参考框架本身不会旋转。但是一旦你在VRML中的代码中围绕y旋转,z轴就不再指向原来的位置,而是旋转。您的参考框架已更改。

现在一种方法是使用欧拉角和多个x,y和z旋转,但是一旦找出quaternion(代表x,y和y),你应该能够进行一次旋转。 z旋转矢量的坐标和旋转量)。有关公式的来源,请参阅this Q&A

方法: 您希望圆柱体坐标系中的重定向y轴与从原点到给定坐标的矢量对齐,因此您需要旋转将点0,r,0移动到新指定的x,y,z。这是如何做到的:

  1. v1是0,r,0(r是圆柱的高度)
  2. v2是您希望顶部中心
  3. 的坐标
  4. vector a = crossproduct(v1,v2)
  5. 标准化矢量a。 VRML规范。说它期望一个标准化的旋转矢量,所以更安全而不是遗憾。要标准化,计算向量a的长度,然后将x,y和z分量除以长度。
  6. 旋转角度为长度(v1)*长度(v2)+ dotproduct(v1,v2)
  7. 因此,您只需要一个旋转变换,在此处使用步骤4中计算的规范化向量的x,y和z值以及步骤5中计算的角度。