2017-06-28 38 views
1

我目前有代码可以生成分割点,并且在每个分割点将在点周围的短圆柱体内生成一些点(在3D中,分割位置全部具有0.0F的az值我想有不同的z值在一行中 - 即它仍然在一行中,但例如在z = 3x的行中),但x和y是随机的。然而,目前所有生成的点都位于面向上的圆柱体中,我希望能够旋转这些点,使它们“生成”的圆柱体面向两个分段之间的方向。 Here's它应该看起来像什么与它目前看起来像什么的图像c# - 关于轴的旋转点

我发现this有关围绕轴的旋转点的类似问题;我接受了答案,并将该代码用于我的RotatePoints()函数,但它似乎不能正常工作,我不知道为什么。下面是我的伪代码,我需要做些什么才能让这个函数正常工作?有一个更好的方法吗?这些点只需要在一个旋转的圆柱体内生成,那么完全不同的方法会更加高效和容易?

我所拥有的是每个片段的位置,每个点在本地空间中存储为Vector3 {x,y,z}。

伪代码

double radius; 
// Generates the positions where the points will be generated around 
// These are just the x,y,z positions of the object in world space 
Vector3[] segmentLocations = GenerateSegmentPositions(numSegments); 

for (int i = 0; i < numSegments; i++) { 
    // Generates points in a cylinder facing up the +ve y-axis 
    // This works fine   
    Vector3[][] pointsAroundSegment = GeneratePoints(segmentLocations[i], radius); 

    if (i != numSegments - 1 && i > 0) { 
     // Generate a normalise direction vector for the new direction 
     Vector3 newDir = Vector3.Normalise(segmentLocations[i + 1] - segmentLocations[i]); 
     double theta = Vector3.AngleBetween(newDir - Vector3.Normalise(segmentLocations[i] - segmentLocations[i - 1])); 
     // Rotates points (this currently rotates the points so they 'should' be facing the new direction, I haven't yet modified this to face the halfway point) 
     // This doesn't work 
     pointsAroundSegment = RotatePoints(pointsAroundSegment, newDir, theta/2); 
    } else if (i == numSegments - 1) { 
     // Generate final point 
     // This works fine 
     pointsAboutSegment = GenerateFinalPoint(segmentLocations[i]); 
    } 
} 

// This is the actual rotation function 
// RotatePoints() effectively just calls this for each point in the array 
public static double[] Rotate(double x, double y, double z, double u, double v, double w, double theta) { 
    double[] c = new double[3]; 
    c [0] = u * (u * x + v * y + w * z) * (1 - Math.Cos (theta)) + x * Math.Cos (theta) + (-w * y + v * z) * Math.Sin (theta); 
    c [1] = v * (u * x + v * y + w * z) * (1 - Math.Cos (theta)) + y * Math.Cos (theta) + (w * x - u * z) * Math.Sin (theta); 
    c [2] = w * (u * x + v * y + w * z) * (1 - Math.Cos (theta)) + z * Math.Cos (theta) + (-v * x + u * y) * Math.Sin (theta); 

    return c; 
} 
+0

我认为该公式旋转围绕原点的段周围的点,我可能是错的,但我认为你想旋转你的当前段的位置。尝试简化公式[f(x,y,z,a,b,c,u,v,w,θ)](https://sites.google.com/site/glennmurray/Home/rotation-matrices-and (a,b,c)是当前分段的位置。 – Poosh

+0

你说得对。该简化的公式起作用。谢谢! –

+0

很高兴帮助! – Poosh

回答

1

Poosh的回答礼貌;

要使用标准化(u^2 + v^2 + w^2 = 1)方向向量绕(a,b,c)线旋转点(x,y,z)使用以下功能:

public static double[] Rotate(double x, double y, double z, double a, double b, double c, double nu, double nv, double nw, double theta) { 
    double[] rP = new double[3]; 

    rP [0] = (a * (nv * nv + nw * nw) - nu * (b * nv + c * nw - nu * x - nv * y - nw * z)) * (1 - Math.Cos (theta)) + x * Math.Cos (theta) + (-c * nv + b * nw - nw * y + nv * z) * Math.Sin (theta); 
    rP [1] = (b * (nu * nu + nw * nw) - nv * (a * nu + c * nw - nu * x - nv * y - nw * z)) * (1 - Math.Cos (theta)) + y * Math.Cos (theta) + (c * nu - a * nw + nw * x - nu * z) * Math.Sin (theta); 
    rP [2] = (c * (nu * nu + nv * nv) - nw * (a * nu + b * nv - nu * x - nv * y - nw * z)) * (1 - Math.Cos (theta)) + z * Math.Cos (theta) + (-b * nu + a * nv - nv * x + nu * y) * Math.Sin (theta); 

    return rP; 
}