Cleanup: Move "Plane.GenerateThreePoints" into PlaneExtensions.

This commit is contained in:
Will
2015-10-31 00:10:55 -06:00
parent d174843ce9
commit 5f1c85ea65
2 changed files with 70 additions and 80 deletions

View File

@@ -157,6 +157,76 @@ namespace LibBSP {
return Intersection(p1, p2);
}
/// <summary>
/// Generates three points which can be used to define this <c>Plane</c>.
/// </summary>
/// <param name="p">This <c>Plane</c>.</param>
/// <param name="planePointCoef">Scale of distance between the generated points. The points will define the same <c>Plane</c> but will be farther apart the larger this value is. May not be zero.</param>
/// <returns>Three points which define this <c>Plane</c>.</returns>
public static Vector3[] GenerateThreePoints(this Plane p, float planePointCoef = 16) {
Vector3[] points = new Vector3[3];
// Figure out if the plane is parallel to two of the axes. If so it can be reproduced easily
if (p.normal.y == 0 && p.normal.z == 0) {
// parallel to plane YZ
points[0] = new Vector3(p.distance / p.normal.x, -planePointCoef, planePointCoef);
points[1] = new Vector3(p.distance / p.normal.x, 0, 0);
points[2] = new Vector3(p.distance / p.normal.x, planePointCoef, planePointCoef);
if (p.normal.x > 0) {
Array.Reverse(points);
}
} else if (p.normal.x == 0 && p.normal.z == 0) {
// parallel to plane XZ
points[0] = new Vector3(planePointCoef, p.distance / p.normal.y, -planePointCoef);
points[1] = new Vector3(0, p.distance / p.normal.y, 0);
points[2] = new Vector3(planePointCoef, p.distance / p.normal.y, planePointCoef);
if (p.normal.y > 0) {
Array.Reverse(points);
}
} else if (p.normal.x == 0 && p.normal.y == 0) {
// parallel to plane XY
points[0] = new Vector3(-planePointCoef, planePointCoef, p.distance / p.normal.z);
points[1] = new Vector3(0, 0, p.distance / p.normal.z);
points[2] = new Vector3(planePointCoef, planePointCoef, p.distance / p.normal.z);
if (p.normal.z > 0) {
Array.Reverse(points);
}
} else if (p.normal.x == 0) {
// If you reach this point the plane is not parallel to any two-axis plane.
// parallel to X axis
points[0] = new Vector3(-planePointCoef, planePointCoef * planePointCoef, (-(planePointCoef * planePointCoef * p.normal.y - p.distance)) / p.normal.z);
points[1] = new Vector3(0, 0, p.distance / p.normal.z);
points[2] = new Vector3(planePointCoef, planePointCoef * planePointCoef, (-(planePointCoef * planePointCoef * p.normal.y - p.distance)) / p.normal.z);
if (p.normal.z > 0) {
Array.Reverse(points);
}
} else if (p.normal.y == 0) {
// parallel to Y axis
points[0] = new Vector3((-(planePointCoef * planePointCoef * p.normal.z - p.distance)) / p.normal.x, -planePointCoef, planePointCoef * planePointCoef);
points[1] = new Vector3(p.distance / p.normal.x, 0, 0);
points[2] = new Vector3((-(planePointCoef * planePointCoef * p.normal.z - p.distance)) / p.normal.x, planePointCoef, planePointCoef * planePointCoef);
if (p.normal.x > 0) {
Array.Reverse(points);
}
} else if (p.normal.z == 0) {
// parallel to Z axis
points[0] = new Vector3(planePointCoef * planePointCoef, (-(planePointCoef * planePointCoef * p.normal.x - p.distance)) / p.normal.y, -planePointCoef);
points[1] = new Vector3(0, p.distance / p.normal.y, 0);
points[2] = new Vector3(planePointCoef * planePointCoef, (-(planePointCoef * planePointCoef * p.normal.x - p.distance)) / p.normal.y, planePointCoef);
if (p.normal.y > 0) {
Array.Reverse(points);
}
} else {
// If you reach this point the plane is not parallel to any axis. Therefore, any two coordinates will give a third.
points[0] = new Vector3(-planePointCoef, planePointCoef * planePointCoef, -(-planePointCoef * p.normal.x + planePointCoef * planePointCoef * p.normal.y - p.distance) / p.normal.z);
points[1] = new Vector3(0, 0, p.distance / p.normal.z);
points[2] = new Vector3(planePointCoef, planePointCoef * planePointCoef, -(planePointCoef * p.normal.x + planePointCoef * planePointCoef * p.normal.y - p.distance) / p.normal.z);
if (p.normal.z > 0) {
Array.Reverse(points);
}
}
return points;
}
/// <summary>
/// Factory method to parse a <c>byte</c> array into a <c>List</c> of <c>Plane</c> objects.
/// </summary>

View File

@@ -260,86 +260,6 @@ namespace LibBSP {
enter = (-1 * (Vector3d.Dot(ray.origin, normal) + distance)) / denom;
return enter > 0;
}
/// <summary>
/// Generates three points which can be used to define this <c>Plane</c>.
/// </summary>
/// <returns>Three points which define this <c>Plane</c></returns>
public Vector3d[] GenerateThreePoints() {
//DecompilerThread.OnMessage(this, "Calculating arbitrary plane points");
double planePointCoef = 32;
Vector3d[] plane = new Vector3d[3];
// Figure out if the plane is parallel to two of the axes. If so it can be reproduced easily
if (normal.y == 0 && normal.z == 0) {
// parallel to plane YZ
plane[0] = new Vector3d(this.distance / normal.x, -planePointCoef, planePointCoef);
plane[1] = new Vector3d(this.distance / normal.x, 0, 0);
plane[2] = new Vector3d(this.distance / normal.x, planePointCoef, planePointCoef);
if (normal.x > 0) {
Array.Reverse(plane);
}
} else {
if (normal.x == 0 && normal.z == 0) {
// parallel to plane XZ
plane[0] = new Vector3d(planePointCoef, this.distance / normal.y, -planePointCoef);
plane[1] = new Vector3d(0, this.distance / normal.y, 0);
plane[2] = new Vector3d(planePointCoef, this.distance / normal.y, planePointCoef);
if (normal.y > 0) {
Array.Reverse(plane);
}
} else {
if (normal.x == 0 && normal.y == 0) {
// parallel to plane XY
plane[0] = new Vector3d(-planePointCoef, planePointCoef, this.distance / normal.z);
plane[1] = new Vector3d(0, 0, this.distance / normal.z);
plane[2] = new Vector3d(planePointCoef, planePointCoef, this.distance / normal.z);
if (normal.z > 0) {
Array.Reverse(plane);
}
} else {
// If you reach this point the plane is not parallel to any two-axis plane.
if (normal.x == 0) {
// parallel to X axis
plane[0] = new Vector3d(-planePointCoef, planePointCoef * planePointCoef, (-(planePointCoef * planePointCoef * normal.y - this.distance)) / normal.z);
plane[1] = new Vector3d(0, 0, this.distance / normal.z);
plane[2] = new Vector3d(planePointCoef, planePointCoef * planePointCoef, (-(planePointCoef * planePointCoef * normal.y - this.distance)) / normal.z);
if (normal.z > 0) {
Array.Reverse(plane);
}
} else {
if (normal.y == 0) {
// parallel to Y axis
plane[0] = new Vector3d((-(planePointCoef * planePointCoef * normal.z - this.distance)) / normal.x, -planePointCoef, planePointCoef * planePointCoef);
plane[1] = new Vector3d(this.distance / normal.x, 0, 0);
plane[2] = new Vector3d((-(planePointCoef * planePointCoef * normal.z - this.distance)) / normal.x, planePointCoef, planePointCoef * planePointCoef);
if (normal.x > 0) {
Array.Reverse(plane);
}
} else {
if (normal.z == 0) {
// parallel to Z axis
plane[0] = new Vector3d(planePointCoef * planePointCoef, (-(planePointCoef * planePointCoef * normal.x - this.distance)) / normal.y, -planePointCoef);
plane[1] = new Vector3d(0, this.distance / normal.y, 0);
plane[2] = new Vector3d(planePointCoef * planePointCoef, (-(planePointCoef * planePointCoef * normal.x - this.distance)) / normal.y, planePointCoef);
if (normal.y > 0) {
Array.Reverse(plane);
}
} else {
// If you reach this point the plane is not parallel to any axis. Therefore, any two coordinates will give a third.
plane[0] = new Vector3d(-planePointCoef, planePointCoef * planePointCoef, -(-planePointCoef * normal.x + planePointCoef * planePointCoef * normal.y - this.distance) / normal.z);
plane[1] = new Vector3d(0, 0, this.distance / normal.z);
plane[2] = new Vector3d(planePointCoef, planePointCoef * planePointCoef, -(planePointCoef * normal.x + planePointCoef * planePointCoef * normal.y - this.distance) / normal.z);
if (normal.z > 0) {
Array.Reverse(plane);
}
}
}
}
}
}
}
return plane;
}
}
}
#endif