Exemplo n.º 1
0
        /// <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="SharpDX.OrientedBoundingBox"/> to merge into it.</param>
        /// <param name="B">The <see cref="SharpDX.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)
        {
            Matrix AtoB_Matrix = GetBoxToBoxMatrix(ref A, ref B, NoMatrixScaleApplied);

            //Get B corners in A Space
            var bCorners = B.GetLocalCorners();

            Vector3.TransformCoordinate(bCorners, ref AtoB_Matrix, bCorners);

            //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;
            Vector3.TransformCoordinate(ref newCenter, ref A.Transformation, out newCenter);
            A.Transformation.TranslationVector = newCenter;
        }
Exemplo n.º 2
0
        public void SetRenderMeshes(params Mesh[] meshes)
        {
            Models.Children.Clear();
            bool        firstMesh = true;
            BoundingBox bb        = new BoundingBox();

            _centerOfMass     = new Point3D();
            _centerOfGeometry = new Point3D();
            foreach (var mesh in meshes)
            {
                var bakedMesh = ResourceCache.GetModel(mesh);
                Models.Children.Add(bakedMesh.Mesh);
                _centerOfMass = Point3D.Add(_centerOfMass, bakedMesh.CenterOfMass.ToVector3D());
                if (firstMesh)
                {
                    bb        = bakedMesh.BoundingBox;
                    firstMesh = false;
                }
                else
                {
                    bb = BoundingBox.Merge(bb, bakedMesh.BoundingBox);
                }
                //bb = BoundingBox.Merge(bb, mesh.BoundingBox.ToSharpDxBoundingBox());
            }

            if (meshes.Length > 0)
            {
                _centerOfMass.X /= meshes.Length;
                _centerOfMass.Y /= meshes.Length;
                _centerOfMass.Z /= meshes.Length;
            }

            ClampBoundingBox(ref bb);
            _modelBoundingBox = bb;
            _centerOfGeometry = _modelBoundingBox.Center.ToPoint3D();
            ReferenceScale    = CalculateReferenceScale(_modelBoundingBox);
            //UpdateGridLines(bb);

            RaisePropertyChanged("Models");
            RefocusCenter();
            RaisePropertyChanged("ReferenceScale");
        }