【问题标题】:Finding points on a cylinder in 3d room c#在 3d 房间 c# 中查找圆柱体上的点
【发布时间】:2019-11-02 18:14:29
【问题描述】:

Example Image here 我正在尝试找到一种方法来计算我的圆柱体顶部圆面上的点。我的情况看起来像这样,我有一个向量,它在 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 度的长度不同。

【问题讨论】:

  • 你应该考虑你的坐标系是否是右手的。据我所知.net 不提供这种数学计算,您使用哪个框架进行这些计算?
  • 不,它是左手坐标系。到目前为止,我正在使用 System.Numerics。那么有人可以帮助我吗?
  • 我可以尝试,但为此您应该发布更多代码,以便我可以重现您的问题。
  • 我已经添加了我尝试过的代码,如果你能重现问题那就太好了,事实上应该有像 (0,0,1), (0,-1, 0),(0,1,0),(0,0,-1) 我想。
  • 我刚刚在手机上看了看,但我会在我到达我的电脑后尝试一下。乍一看,我认为你可以通过改变你穿过的向量来从圆柱体中得到另外 3 个点(因为你总是考虑旋转 90 度)

标签: c# matrix vector 3d rotation


【解决方案1】:

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

        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 部分在圆柱体上制作的位置。我想世界轴和圆柱轴在同一方向上。此外,如果您的圆柱体在 X 轴上移动(增加),而不是像示例中的 Z 轴,只需在代码中切换 Z 与 X。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    相关资源
    最近更新 更多