/// <summary> /// Fits to centered cube. /// </summary> public void FitToCenteredCube(float cubeSideLength, FitToCuboidMode mode, SpacialOriginLocation fitOrigin) { this.FitToCenteredCuboid(cubeSideLength, cubeSideLength, cubeSideLength, mode, fitOrigin); }
/// <summary> /// Fits to centered cube. /// </summary> public void FitToCenteredCuboid(float cubeSideLengthX, float cubeSideLengthY, float cubeSideLengthZ, FitToCuboidMode fitMode, SpacialOriginLocation fitOrigin) { //Get whole bounding box var boundingBox = this.GenerateBoundingBox(); var boundingBoxSize = boundingBox.GetSize(); if (boundingBox.IsEmpty()) { return; } if (boundingBoxSize.X <= 0f) { return; } if (boundingBoxSize.Y <= 0f) { return; } if (boundingBoxSize.Z <= 0f) { return; } var targetCornerALocation = new Vector3( -boundingBoxSize.X / 2f, -boundingBoxSize.Y / 2f, -boundingBoxSize.Z / 2f); // Calculate resize factors var resizeFactorX = cubeSideLengthX / boundingBoxSize.X; var resizeFactorY = cubeSideLengthY / boundingBoxSize.Y; var resizeFactorZ = cubeSideLengthZ / boundingBoxSize.Z; if (fitMode == FitToCuboidMode.MaintainAspectRatio) { resizeFactorX = Math.Min(resizeFactorX, Math.Min(resizeFactorY, resizeFactorZ)); resizeFactorY = resizeFactorX; resizeFactorZ = resizeFactorX; } targetCornerALocation.X = targetCornerALocation.X * resizeFactorX; targetCornerALocation.Y = targetCornerALocation.Y * resizeFactorY; targetCornerALocation.Z = targetCornerALocation.Z * resizeFactorZ; switch (fitOrigin) { case SpacialOriginLocation.LowerCenter: targetCornerALocation.Y = 0f; break; } // Bring the geometry to origin based location and then scale it this.UpdateVerticesUsingTranslation(Vector3.Negate(boundingBox.CornerA)); this.UpdateVerticesUsingTranslation(actPosition => new Vector3( actPosition.X * resizeFactorX, actPosition.Y * resizeFactorY, actPosition.Z * resizeFactorZ)); this.UpdateVerticesUsingTranslation(targetCornerALocation); }
/// <summary> /// Fits to centered cube. /// </summary> public void FitToCenteredCube(float cubeSideLength, FitToCuboidMode mode) { FitToCenteredCuboid(cubeSideLength, cubeSideLength, cubeSideLength, mode, SpacialOriginLocation.Center); }