Example #1
0
        public Sphere ComputeBoundingSphereNaive()
        {
            var pointPositions = PointAttributes.FindOrCreate <Vector3>("point_locations");

            //  FIRST PASS: Find 6 minima/maxima points
            BoundingBox box = new BoundingBox();

            box.Clear();
            foreach (Point point in Points)
            {
                box.ExtendBy(pointPositions[point]);
            }

            float maxRadiusSquare = 0.0f;

            foreach (Point point in Points)
            {
                Vector3 p            = pointPositions[point];
                float   radiusSquare = box.Center.DistanceSquared(pointPositions[point]);
                if (radiusSquare > maxRadiusSquare)
                {
                    maxRadiusSquare = radiusSquare;
                }
            }
            return(new Sphere(box.Center, (float)System.Math.Sqrt(maxRadiusSquare)));
        }
Example #2
0
 protected virtual void Invalidate()
 {
     _bbox.Clear();
     _points = null;
     _inout  = null;
     _len    = -1;
 }
        public void Return_A_BBox_NotInitialized()
        {
            BoundingBox bBox = new BoundingBox(BoundingBoxCollection.BoundingBoxFrom5Points());

            bBox.Clear();

            bBox.IsValid.Should().BeFalse();
        }
Example #4
0
 private void UpdateFrustumPoints(Vector3f[] pts)
 {
     bb.Clear();
     bb.Extend(MathUtil.Round(v_cam.Translation.Add(new Vector3f(s_cam.Far, s_cam.Far, s_cam.Far))));
     bb.Extend(MathUtil.Round(v_cam.Translation.Add(new Vector3f(-s_cam.Far, -s_cam.Far, -s_cam.Far))));
     for (int i = 0; i < bb.Corners.Length; i++)
     {
         pts[i].Set(bb.Corners[i]);
     }
 }
            public void GetCellBoundingBox(int j, BoundingBox bb)
            {
                BoundingBox bbTemp = new BoundingBox(bb.D);

                bb.Clear();
                foreach (int jPart in this.AggregateCellToParts[j])
                {
                    m_Owner.m_GeomCellData.GetCellBoundingBox(jPart, bbTemp);
                    bb.AddBB(bbTemp);
                }
            }
Example #6
0
 public override void UpdateWorldBoundingBox()
 {
     if (worldBoundingBox != null)
     {
         worldBoundingBox.Clear();
         foreach (GameObject child in children)
         {
             worldBoundingBox.Extend(child.GetWorldBoundingBox());
         }
     }
 }
Example #7
0
 public void UpdateChunksAround(long wx, long wz)
 {
     wx &= ~0xf;
     wz &= ~0xf;
     bounds.Clear();
     for (long x = wx; x < wx + 16; ++x)
     {
         for (long z = wz; z < wz + 16; ++z)
         {
             FindOrCreateChunk(x, z);
             bounds.ExtendBy((float)(x), 0.0f, (float)(z));
             bounds.ExtendBy((float)(x + 16), 128.0f, (float)(z + 16));
         }
     }
 }
            /// <summary>
            /// Computes the bounding box of cell <paramref name="j"/>
            /// </summary>
            /// <param name="j">local cell index</param>
            /// <param name="bb">
            /// on exit, the bounding box of cell j.
            /// </param>
            public void GetCellBoundingBox(int j, BoundingBox bb)
            {
                int D = bb.D;

                if (bb.D != m_Owner.SpatialDimension)
                {
                    throw new ArgumentException("wrong dimension of bounding box.");
                }
                bb.Clear();

                MultidimensionalArray Points = m_Owner.m_VertexData.Coordinates;

                double[] pt = new double[D];

                foreach (int iVtx in this.CellVertices[j])
                {
                    Points.GetRow(iVtx, pt, 0, 1);
                    bb.AddPoint(pt);
                }
            }
Example #9
0
            /// <summary>
            /// Computes the bounding box of cell <paramref name="j"/>.
            /// </summary>
            /// <param name="j">local cell index.</param>
            /// <param name="bb">
            /// on exit, the bounding box of cell j.
            /// </param>
            public void GetCellBoundingBox(int j, BoundingBox bb)
            {
                int D = bb.D;

                if (bb.D != m_owner.SpatialDimension)
                {
                    throw new ArgumentException("wrong dimension of bounding box.");
                }
                bb.Clear();

                Cell       Cj          = this.GetCell(j);
                RefElement Kref        = this.GetRefElement(j);
                NodeSet    verticesLoc = Kref.GetInterpolationNodes(Cj.Type);

                MultidimensionalArray verticesGlob = MultidimensionalArray.Create(1, verticesLoc.GetLength(0), verticesLoc.GetLength(1));

                m_owner.TransformLocal2Global(verticesLoc, j, 1, verticesGlob, 0);
                bb.AddPoints(verticesGlob
                             .ExtractSubArrayShallow(0, -1, -1));
            }
Example #10
0
        public Brush(BrushManager brushManager, string name, GeometryMesh mesh)
        {
            this.brushManager = brushManager;
            this.Name         = name;

            Geometry g = mesh.Geometry;

            /*g = new SubdivideGeometryOperation(g).Destination;
             * g = new SubdivideGeometryOperation(g).Destination;
             * g = new CatmullClarkGeometryOperation(g).Destination;
             * Model = new Model(name,  new GeometryMesh(g, NormalStyle.PointNormals), brushManager.MaterialManager.Materials["EdgeLines"]);
             */

            Model = new Model(name, mesh, brushManager.MaterialManager.Materials["BrushDefault"]);
            BoundingBox.Clear();

            var pointLocations = mesh.Geometry.PointAttributes.Find <Vector3>("point_locations");

            //  Setup polygon dictionary
            foreach (Polygon polygon in mesh.Geometry.Polygons)
            {
                int cornerCount = polygon.Corners.Count;
                if (PolygonDictionary.ContainsKey(cornerCount) == false)
                {
                    PolygonDictionary[cornerCount] = polygon;
                }
            }

            //  Compute bounding box
            foreach (Point point in mesh.Geometry.Points)
            {
                BoundingBox.ExtendBy(pointLocations[point]);
            }

            if (Configuration.physics)
            {
                CreateScaledShape(1.0f);
                Model.PhysicsShape = ScaledShape(1.0f);
            }
        }
        public void Merge()
        {
            // \todo
#if true
            if (selectionManager == null)
            {
                return;
            }

            if (selectionManager.Models.Count <= 1)
            {
                return;
            }

            //  First pass: Find which model to use as new reference
            //  There should be one model which parent is not any
            //  of the models in selection.
            Model referenceModel = null;
            foreach (Model referenceCandidate in selectionManager.Models)
            {
                if (referenceCandidate == null)
                {
                    continue;
                }

                bool candidateHasParentInSelection = false;
                foreach (Model otherModel in selectionManager.Models)
                {
                    if (otherModel == null)
                    {
                        continue;
                    }
                    if (referenceCandidate.Frame.Parent == otherModel.Frame)
                    {
                        candidateHasParentInSelection = true;
                        break;
                    }
                }

                if (candidateHasParentInSelection == false)
                {
                    referenceModel = referenceCandidate;
                    break;
                }
            }
            if (referenceModel == null)
            {
                throw new Exception("Unable to pick selection root model for merge operation");
            }

            //  This is where compound shapes are collected.
            //  All shapes are added in the coordinate system of the reference model.
            //  Models that have no rigid bodies are skipped
            List <CompoundShape.TransformedShape> shapes = new List <CompoundShape.TransformedShape>();

            //  Add reference model (if it has rigidbody)
            if (referenceModel.RigidBody != null)
            {
                //var shape = new CompoundShape.TransformedShape(
                //    referenceModel.RigidBody.Shape,
                //    JMatrix.Identity,
                //    JVector.Zero
                //);
                //shapes.Add(shape);
                Shape         oldShape = referenceModel.RigidBody.Shape;
                CompoundShape compound = oldShape as CompoundShape;
                if (compound != null)
                {
                    foreach (var part in compound.Shapes)
                    {
                        var shape = new CompoundShape.TransformedShape(
                            part.Shape,
                            part.Transform
                            //part.Orientation,
                            //part.Position
                            );
                        shapes.Add(shape);
                    }
                }
                else
                {
                    var shape = new CompoundShape.TransformedShape(
                        referenceModel.RigidBody.Shape,
                        Matrix4.Identity
                        //JMatrix.Identity,
                        //JVector.Zero
                        );
                    shapes.Add(shape);
                }
            }

            GeometryMesh referencePolymesh = referenceModel.Batch.MeshSource as GeometryMesh;
            Geometry     mergedGeometry    = new CloneGeometryOperation(referencePolymesh.Geometry, null).Destination;

            //  Remove original reference model from scene graph
            sceneManager.RemoveModel(referenceModel);
            foreach (Model mergeModel in selectionManager.Models)
            {
                if (mergeModel == null || mergeModel == referenceModel)
                {
                    continue;
                }
                GeometryMesh addPolymesh = mergeModel.Batch.MeshSource as GeometryMesh;
                Geometry     addGeometry = new CloneGeometryOperation(addPolymesh.Geometry, null).Destination;
                Matrix4      toSameSpace = referenceModel.Frame.LocalToWorld.InverseMatrix * mergeModel.Frame.LocalToWorld.Matrix;
                addGeometry.Transform(toSameSpace);

                if (mergeModel.RigidBody != null)
                {
                    //  TODO: Decompose - for now we only have rotations and translations so this should work

                    /*JMatrix orientation;
                     * orientation.M11 = toSameSpace._00;
                     * orientation.M21 = toSameSpace._01;
                     * orientation.M31 = toSameSpace._02;
                     * orientation.M12 = toSameSpace._10;
                     * orientation.M22 = toSameSpace._11;
                     * orientation.M32 = toSameSpace._12;
                     * orientation.M13 = toSameSpace._20;
                     * orientation.M23 = toSameSpace._21;
                     * orientation.M33 = toSameSpace._22;
                     * orientation.M13 = toSameSpace._20;
                     * orientation.M23 = toSameSpace._21;
                     * orientation.M33 = toSameSpace._22;
                     * Vector3 p = toSameSpace.GetColumn3(3);
                     * JVector position = new JVector(p.X, p.Y, p.Z);*/

                    Shape         oldShape = mergeModel.RigidBody.Shape;
                    CompoundShape compound = oldShape as CompoundShape;
                    if (compound != null)
                    {
                        foreach (var part in compound.Shapes)
                        {
                            var shape = new CompoundShape.TransformedShape(
                                part.Shape,
                                toSameSpace * part.Transform
                                //JMatrix.Multiply(orientation, part.Orientation),
                                //position + JVector.Transform(part.Position, orientation)
                                );
                            shapes.Add(shape);
                        }
                    }
                    else
                    {
                        var shape = new CompoundShape.TransformedShape(
                            oldShape,
                            toSameSpace
                            //orientation,
                            //position
                            );
                        shapes.Add(shape);
                    }
                }

                mergedGeometry.Merge(addGeometry);
                sceneManager.RemoveModel(mergeModel);
            }

            Matrix4       modelOffset   = Matrix4.Identity;
            CompoundShape compoundShape = null;
            if (shapes.Count > 0)
            {
                compoundShape = new CompoundShape(shapes);

                Matrix4 vertexOffset = Matrix4.CreateTranslation(compoundShape.Shift.X, compoundShape.Shift.Y, compoundShape.Shift.Z);
                modelOffset = Matrix4.CreateTranslation(-compoundShape.Shift.X, -compoundShape.Shift.Y, -compoundShape.Shift.Z);

                mergedGeometry.Transform(vertexOffset);
            }
            mergedGeometry.ComputePolygonCentroids();
            mergedGeometry.ComputePolygonNormals();
            //mergedGeometry.ComputeCornerNormals(0.0f);
            mergedGeometry.SmoothNormalize("corner_normals", "polygon_normals", (0.0f * (float)Math.PI));
            mergedGeometry.BuildEdges();

            GeometryMesh mergedPolyMesh = new GeometryMesh(mergedGeometry, NormalStyle.PolygonNormals);

            //  Compute bounding box - debug
            {
                var bbox           = new BoundingBox();
                var pointLocations = mergedGeometry.PointAttributes.Find <Vector3>("point_locations");
                bbox.Clear();
                foreach (Point point in mergedGeometry.Points)
                {
                    bbox.ExtendBy(pointLocations[point]);
                }
            }

            Model newModel = new Model(
                "Merge(" + referenceModel.Name + ", " + selectionManager.Models.Count + ")",
                mergedPolyMesh,
                referenceModel.Batch.Material,
                referenceModel.Frame.LocalToParent.Matrix * modelOffset
                );
            if (compoundShape != null)
            {
                newModel.PhysicsShape = compoundShape;
            }
            //sceneManager.MakeModelPhysicsConvexHull(newModel);  This won't work as Shift is not properly taken into account
            newModel.Frame.Parent = referenceModel.Frame.Parent;
            newModel.Static       = referenceModel.Static;

            selectionManager.ClearSelection();

            //  This will create rigidbody using .PhysicsShape if set, otherwise it will call
            //  MakeModelPhysicsConvexHull().
            sceneManager.AddModel(newModel);

            selectionManager.HoverModel   = null;
            selectionManager.HoverPolygon = null;
#endif
        }