/// <summary> /// Generates a bounding box for a collection of vectors. /// </summary> /// <param name="vecs">The vectors to create a bounding box from.</param> /// <returns>A bounding box containing every vector.</returns> public static BBox3 GetBoundingBox(this IEnumerable <Vector3> vecs) { BBox3 bounds = new BBox3(); Vector3 v; foreach (Vector3 vec in vecs) { v = vec; ApplyVertexToBounds(ref v, ref bounds); } ApplyPaddingToBounds(1.0f, ref bounds); return(bounds); }
/// <summary> /// Generates a bounding box for a collection of triangles. /// </summary> /// <param name="tris">The triangles to create a bounding box from.</param> /// <param name="padding">Padding to the bounding box</param> /// <returns>A bounding box containing every triangle.</returns> public static BBox3 GetBoundingBox(this IEnumerable <Triangle3> tris, float padding) { BBox3 bounds = new BBox3(); Vector3 va, vb, vc; foreach (Triangle3 tri in tris) { va = tri.A; vb = tri.B; vc = tri.C; ApplyVertexToBounds(ref va, ref bounds); ApplyVertexToBounds(ref vb, ref bounds); ApplyVertexToBounds(ref vc, ref bounds); } //pad the bounding box a bit to make sure outer triangles are fully contained. ApplyPaddingToBounds(padding, ref bounds); return(bounds); }
/// <summary> /// Calculates the bounding box of a triangle from its vertices. /// </summary> /// <param name="a">The first vertex.</param> /// <param name="b">The second vertex.</param> /// <param name="c">The third vertex.</param> /// <param name="bbox">The bounding box between the points.</param> public static void GetBoundingBox(ref Vector3 a, ref Vector3 b, ref Vector3 c, out BBox3 bbox) { Vector3 min = a, max = a; if (b.X < min.X) { min.X = b.X; } if (b.Y < min.Y) { min.Y = b.Y; } if (b.Z < min.Z) { min.Z = b.Z; } if (c.X < min.X) { min.X = c.X; } if (c.Y < min.Y) { min.Y = c.Y; } if (c.Z < min.Z) { min.Z = c.Z; } if (b.X > max.X) { max.X = b.X; } if (b.Y > max.Y) { max.Y = b.Y; } if (b.Z > max.Z) { max.Z = b.Z; } if (c.X > max.X) { max.X = c.X; } if (c.Y > max.Y) { max.Y = c.Y; } if (c.Z > max.Z) { max.Z = c.Z; } bbox.Min = min; bbox.Max = max; }
/// <summary> /// Calculates the bounding box of a triangle. /// </summary> /// <param name="tri">A triangle.</param> /// <param name="bbox">The triangle's bounding box.</param> public static void GetBoundingBox(ref Triangle3 tri, out BBox3 bbox) { GetBoundingBox(ref tri.A, ref tri.B, ref tri.C, out bbox); }