/// <summary> /// Merge an OrientedBoundingBox B into another OrientedBoundingBox A, by expanding A to contain B and keeping A orientation. /// </summary> /// <param name="A">The <see cref="SeeingSharp.OrientedBoundingBox"/> to merge into it.</param> /// <param name="B">The <see cref="SeeingSharp.OrientedBoundingBox"/> to be merged</param> /// <param name="NoMatrixScaleApplied"> /// If true, the method will use a fast algorithm which is inapplicable if a scale is applied to the transformation matrix of the OrientedBoundingBox. /// </param> /// <remarks> /// Unlike merging axis aligned boxes, The operation is not interchangeable, because it keeps A orientation and merge B into it. /// </remarks> public static void Merge(ref OrientedBoundingBox A, ref OrientedBoundingBox B, bool NoMatrixScaleApplied = false) { Matrix4x4 AtoB_Matrix = GetBoxToBoxMatrix(ref A, ref B, NoMatrixScaleApplied); //Get B corners in A Space var bCorners = B.GetLocalCorners(); for (int loop = 0; loop < bCorners.Length; loop++) { bCorners[loop] = Vector3.Transform(bCorners[loop], AtoB_Matrix); } //Get A local Bounding Box var A_LocalBB = new BoundingBox(-A.Extents, A.Extents); //Find B BoundingBox in A Space var B_LocalBB = BoundingBox.FromPoints(bCorners); //Merger A and B local Bounding Boxes BoundingBox mergedBB; BoundingBox.Merge(ref B_LocalBB, ref A_LocalBB, out mergedBB); //Find the new Extents and Center, Transform Center back to world var newCenter = mergedBB.Minimum + (mergedBB.Maximum - mergedBB.Minimum) / 2f; A.Extents = mergedBB.Maximum - newCenter; newCenter = Vector3.Transform(newCenter, A.Transformation); A.Transformation.Translation = newCenter; }
/// <summary> /// Get the axis-aligned <see cref="SeeingSharp.BoundingBox"/> which contains all <see cref="SeeingSharp.OrientedBoundingBox"/> corners. /// </summary> /// <returns>The axis-aligned BoundingBox of this OrientedBoundingBox.</returns> public BoundingBox GetBoundingBox() { return(BoundingBox.FromPoints(GetCorners())); }