标题可能是错的,因为我不知道足够的数学来用一个小句子来描述我的问题。
我目前的尝试如下:
问题是,这不起作用,罪魁祸首似乎是我得到'轴对齐坐标的部分,因为将它们立即恢复会产生错误的结果
public static List<Vector2> Planify3Dto2DPoints2(Vector3[] points, Vector3 centroid, Plane ply, out Vector3[] oldHeights) {
var pz = ply.normal.z;
var px = ply.normal.x;
var py = ply.normal.y;
Plane plx = new Plane(new Vector3(pz, px, py), 0);
Plane plz = new Plane(new Vector3(py, pz, px), 0);
oldHeights = new Vector3[points.Length];
List<Vector2> m_points = new List<Vector2>();
int i = 0;
foreach (Vector3 v3 in points) {
Vector3 v4 = v3 - centroid;
float x = plx.GetDistanceToPoint(v4);//this part is wrong, attempting to get the v4
float z = plz.GetDistanceToPoint(v4);//vector back from the x, z, y coordinates is not
float y = ply.GetDistanceToPoint(v4);//working. removing x * plx.Normal from v4 before
m_points.Add(new Vector2(x, z));// extracting the z coordinate reduces the error, but does not remove it
oldHeights[i++] = new Vector3(x, z, y);
}
return m_points;
}
public static List<Vector3> Spacefy2Dto3DPoints(Vector2[] points, Vector3 centroid, Plane ply, Vector3[] oldHeights = null) {
List<Vector3> m_points = new List<Vector3>();
var pn = new Vector3(ply.normal.x, ply.normal.y, ply.normal.z);
for (int i = 0; i < points.Length; i++) {
Vector3 mp = MoveInPlane(ply, points[i]);
if (oldHeights != null) {
mp += pn * oldHeights[i].z;//AverageOf3ClosestHeight(points[i], oldHeights); not needed yet, but working fine, it's weighted average
}
mp += centroid;
m_points.Add(mp);
}
return m_points;
}
private static Vector3 MoveInPlane(Plane plane, Vector2 vector2) {
var z = plane.normal.z;
var x = plane.normal.x;
var y = plane.normal.y;
return new Vector3(z, x, y) * vector2.x + new Vector3(y, z, x) * vector2.y;
}
答案 0 :(得分:3)
问题出在这一步:
- 通过移动平面法线坐标
获取两个垂直平面 醇>
这不会给出垂直平面。
您可能错误地认为这会因为一个简单的具体示例而起作用,例如: (1, 0, 0) => (0, 1, 0) & (0, 0, 1)
,或围绕坐标切换有效地切换轴的角色,这相当于旋转90度。但试试看,例如(1, 1, 0)
你立即发现这不起作用。
一种方法是:
P
和X
轴(任意选择)的点积。abs(dot(X, P)) > 0.5
),则设置矢量变量Q <- Z
轴(再次,任意)。否则,请设置Q <- X
。U = P ^ Q
和V = P ^ U
给出了两个垂直平面的法线。请注意,它们未规范化,{U, V, P}
提供了一组右手轴。您可以做的另一个小优化是将- centeroid
合并到平面方程本身中,以避免必须明确地为每个点进行此操作。
Vector3 Q = (Math.Abs(ply.normal.x) > 0.5) ? new Vector3D(0.0, 1.0, 0.0)
: new Vector3D(1.0, 0.0, 0.0);
Vector3 U = Vector3.Normalize(Vector3.CrossProduct(ply.normal, Q));
Vector3 V = Vector3.CrossProduct(ply.normal, U);
// no need to normalize V because U and P are already orthonormal
Plane plx = new Plane(U, Vector3.DotProduct(U, centeroid));
Plane plz = new Plane(V, Vector3.DotProduct(V, centeroid));
// ...
foreach (Vector3 v3 in points) {
/* Vector3 v4 = v3 - centroid; // erase this line */
float x = plx.GetDistanceToPoint(v3); // v4 -> v3 for all code following