From 5f1c85ea657086b058e12c29eeafaf812ebe923f Mon Sep 17 00:00:00 2001 From: Will Date: Sat, 31 Oct 2015 00:10:55 -0600 Subject: [PATCH] Cleanup: Move "Plane.GenerateThreePoints" into PlaneExtensions. --- LibBSP/src/Extensions/PlaneExtensions.cs | 70 +++++++++++++++++++++ LibBSP/src/Structs/Common/Plane.cs | 80 ------------------------ 2 files changed, 70 insertions(+), 80 deletions(-) diff --git a/LibBSP/src/Extensions/PlaneExtensions.cs b/LibBSP/src/Extensions/PlaneExtensions.cs index 257d342..1a16efc 100644 --- a/LibBSP/src/Extensions/PlaneExtensions.cs +++ b/LibBSP/src/Extensions/PlaneExtensions.cs @@ -157,6 +157,76 @@ namespace LibBSP { return Intersection(p1, p2); } + /// + /// Generates three points which can be used to define this Plane. + /// + /// This Plane. + /// Scale of distance between the generated points. The points will define the same Plane but will be farther apart the larger this value is. May not be zero. + /// Three points which define this Plane. + 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; + } + /// /// Factory method to parse a byte array into a List of Plane objects. /// diff --git a/LibBSP/src/Structs/Common/Plane.cs b/LibBSP/src/Structs/Common/Plane.cs index e0d9634..efade17 100644 --- a/LibBSP/src/Structs/Common/Plane.cs +++ b/LibBSP/src/Structs/Common/Plane.cs @@ -260,86 +260,6 @@ namespace LibBSP { enter = (-1 * (Vector3d.Dot(ray.origin, normal) + distance)) / denom; return enter > 0; } - - /// - /// Generates three points which can be used to define this Plane. - /// - /// Three points which define this Plane - 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