繁体   English   中英

在 3d 房间 c# 中找到圆柱上的点

[英]Finding points on a cylinder in 3d room c#

此处的示例图像我正在尝试找到一种方法来计算我的圆柱体顶部圆形表面上的点。 我的情况是这样的,我有一个向量在 3d 房间中定义我的圆柱体方向。 然后我已经计算了一个垂直向量

Vector3.Cross(vector1, vector2)

现在我使用直径/2 来计算位于圆柱体圆形顶面边缘的点。 现在我想总是将我的向量旋转 90 度,以便在曲面边缘获得 4 个点。 定义它们的所有 4 个向量都应垂直于圆柱体方向。 你能帮我如何旋转第一个垂直线来实现这一点吗?

我已经尝试过:

Matrix4x4.CreateFromAxisAngle(vectorcylinderdirection, radiant)

然后我再次计算了叉积,但它不像我想要的那样工作。

编辑:

        public static void calculatePontsOnCylinder()
    {

        //Calculate Orthogonal Vector to Direction
        Vector3 tCylinderDirection = new Vector3(1, 0, 0);
        Vector3 tOrthogonal = Vector3.Cross(tCylinderDirection, new Vector3(-tCylinderDirection.Z,tCylinderDirection.X,tCylinderDirection.Y));
        Vector3 tNormOrthogonal = Vector3.Normalize(tOrthogonal);

        //Calculate point on surface circle of cylinder
        //10mm radius
        int tRadius = 10;
        Vector3 tPointFinder = tNormOrthogonal * tRadius;

        //tPointFinder add the cylinder start point
        //not yet implemented

        //now i need to rotate the vector always 90 degrees to find the 3 other points on the circular top surface of the cylinder
        //don't know how to do this
        // I thought this should do it
        Matrix4x4.CreateFromAxisAngle(tCylinderDirection, (float)DegreeToRadian(90));



    }

    private static double DegreeToRadian(double angle)
    {
        return Math.PI * angle / 180.0;
    }

在图片中你可以看到一个例子,vector1是我需要的,总是旋转90度,vector2是我的圆柱方向向量

我可能找到了正确的公式:

Vector3 tFinal = Vector3.Multiply((float)Math.Cos(DegreeToRadian(90)), tPointFinder) + Vector3.Multiply((float)Math.Sin(DegreeToRadian(90)), Vector3.Cross(tCylinderDirection, tPointFinder));
        Vector3 tFinal180 = Vector3.Multiply((float)Math.Cos(DegreeToRadian(180)), tPointFinder) + Vector3.Multiply((float)Math.Sin(DegreeToRadian(180)), Vector3.Cross(tCylinderDirection, tPointFinder));
        Vector3 tFinal270= Vector3.Multiply((float)Math.Cos(DegreeToRadian(270)), tPointFinder) + Vector3.Multiply((float)Math.Sin(DegreeToRadian(270)), Vector3.Cross(tCylinderDirection, tPointFinder));

有趣的是,如果我尝试使用 (1,1,0) 作为圆柱方向,它会给我正确的方向,但 90 度和 270 度的长度不同。

假设满足输入要求,这是应该解决您的问题的代码。

        float zCutPlaneLocation = 20; // should not get bigger than cylinder length
        float cylinderRadius = 100;
        Vector3 cylinderCenter = new Vector3(0, 0, 0); // or whatever you got as cylinder center point, given as Vector3 since Point type is not defined

        // will return 360 points on cylinder edge, corresponding to this z section (cut plane),
        // another z section will give another 360 points and so on
        List<Vector3> cylinderRotatedPointsIn3D = new List<Vector3>();

        for (int angleToRotate = 0; angleToRotate < 360; angleToRotate++)
        {
            cylinderRotatedPointsIn3D.Add(GetRotatedPoint(zCutPlaneLocation, angleToRotate, cylinderRadius, cylinderCenter));
        }

         ....

       private static Vector3 GetRotatedPoint(
        float zLocation, double rotationAngleInRadian, float cylinderRadius, Vector3 cylinderCenter)
       {
        Vector2 cylinderCenterInSection = new Vector2(cylinderCenter.X, cylinderCenter.Y);

        float xOfRotatedPoint = cylinderRadius * (float)Math.Cos(rotationAngleInRadian);
        float yOfRotatedPoint = cylinderRadius * (float)Math.Sin(rotationAngleInRadian);

        Vector2 rotatedVector = new Vector2(xOfRotatedPoint, yOfRotatedPoint);
        Vector2 rotatedSectionPointOnCylinder = rotatedVector + cylinderCenterInSection;

        Vector3 rotatedPointOnCylinderIn3D = new Vector3(
                                                    rotatedSectionPointOnCylinder.X,
                                                    rotatedSectionPointOnCylinder.Y,
                                                    zLocation + cylinderCenter.Z);

        return rotatedPointOnCylinderIn3D;
       }

我刚刚为此创建了一个控制台应用程序。 代码的第一部分应添加到 main 方法中。 使用这些矩阵似乎并不容易。 此外,我不确定您的解决方案是否适用于任何角度。 这里的想法是,圆柱体的旋转点是在圆柱体的一个截面中计算的,因此在 2D 中,结果在 3D 中移动,只需添加在圆柱体上制作 Z 截面的 z 即可。 我想世界轴和圆柱轴在同一个方向上。 此外,如果您的圆柱体在 X 轴上移动(增加),而不是像示例中的 Z 轴,只需在代码中用 X 切换 Z。

我还附上了一张图片图片 更多细节。 如果您有圆柱体中心、半径、旋转角度并且知道圆柱体的长度以便在圆柱体上创建有效的 Z 截面,这应该可以工作。 对于顺时针/逆时针的情况,这可能会变得很棘手,但让我们看看它是如何为您工作的。

如果你想用矩阵或其他任何东西来处理这个问题,我认为你最终会得到这种结果。 因此,我认为您不能仅在整个圆柱体表面的列表中拥有“所有”旋转点,它们将取决于圆柱体上 Z 截面的旋转点之类的东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM