Ejemplo n.º 1
0
        private void Grow(Vector3 direction)
        {
            int     xDirection = direction.x >= 0 ? 1 : -1;
            int     yDirection = direction.y >= 0 ? 1 : -1;
            int     zDirection = direction.z >= 0 ? 1 : -1;
            Octant  oldRoot    = _root;
            float   half       = _root.sideLength / 2;
            float   newLength  = _root.sideLength * 2;
            Vector3 newCenter  = _root.center + new Vector3(xDirection * half, yDirection * half, zDirection * half);

            // Create a new, bigger octree root node
            _root = new Octant(this, newLength, newCenter);

            // Create 7 new octree children to go with the old root as children of the new root
            int rootPos = GetRootPosIndex(xDirection, yDirection, zDirection);

            Octant[] children = new Octant[8];
            for (int i = 0; i < 8; i++)
            {
                if (i == rootPos)
                {
                    children[i] = oldRoot;
                }
                else
                {
                    xDirection  = i % 2 == 0 ? -1 : 1;
                    yDirection  = i > 3 ? -1 : 1;
                    zDirection  = (i < 2 || (i > 3 && i < 6)) ? -1 : 1;
                    children[i] = new Octant(this, _root.sideLength, newCenter + new Vector3(xDirection * half, yDirection * half, zDirection * half));
                }
            }

            // Attach the new children to the new root node
            _root.children = children;
        }
        /// <summary>
        /// Hits the test current node exclude child.
        /// </summary>
        /// <param name="octant">The octant.</param>
        /// <param name="context">The context.</param>
        /// <param name="model">The model.</param>
        /// <param name="geometry"></param>
        /// <param name="modelMatrix">The model matrix.</param>
        /// <param name="rayWS">The ray ws.</param>
        /// <param name="rayModel">The ray model.</param>
        /// <param name="hits">The hits.</param>
        /// <param name="isIntersect">if set to <c>true</c> [is intersect].</param>
        /// <param name="hitThickness">The hit thickness.</param>
        /// <returns></returns>
        protected override bool HitTestCurrentNodeExcludeChild(ref Octant octant, RenderContext context, object model, Geometry3D geometry, Matrix modelMatrix, ref Ray rayWS, ref Ray rayModel, ref List <HitTestResult> hits, ref bool isIntersect, float hitThickness)
        {
            isIntersect = false;
            if (!octant.IsBuilt)
            {
                return(false);
            }
            bool isHit = false;
            var  bound = octant.Bound.Transform(modelMatrix);

            if (rayWS.Intersects(ref bound))
            {
                isIntersect = true;
                for (int i = octant.Start; i < octant.End; ++i)
                {
                    var b = Objects[i].Value.Transform(modelMatrix);
                    if (b.Intersects(ref rayWS))
                    {
                        var geo = Geometries[Objects[i].Key];
                        if (geo.Geometry is MeshGeometry3D mesh)
                        {
                            isHit |= mesh.HitTest(context, geo.ModelTransform * modelMatrix, ref rayWS, ref hits, model);
                        }
                    }
                }
            }
            return(isHit);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Updates the positions of all the given <see cref="Body"/>s with O(n^2) time complexity, with the given time step.
        /// </summary>
        /// <param name="bodies">The collection of <see cref="Body"/>s whose positions to update.</param>
        /// <param name="deltaTime">The time step.</param>
        public static void UpdateBodiesBarnesHut(IEnumerable <Body> bodies, double deltaTime)
        {
            IEnumerable <Body> bodyEnumerable = bodies as Body[] ?? bodies.ToArray();

            // root octant represents the main universe. Anything outside this octant of space is not updated
            Octant universeOctant = new Octant(Constants.UniverseOctant);

            OctantTree barnesHutTree = new OctantTree(universeOctant);

            // we construct our barnes-hut octant tree
            foreach (Body body in bodyEnumerable)
            {
                if (body.IsInOctant(universeOctant))
                {
                    // only update a body's position if it is within the bounds of the universe
                    barnesHutTree.AddBody(body);
                }
            }

            // we update the positions of the bodies in the populated tree in a parallel manner, using the thread pool
            Parallel.ForEach(bodyEnumerable, body =>
            {
                if (body != null)
                {
                    body.ResetForce();

                    if (body.IsInOctant(universeOctant))
                    {
                        barnesHutTree.UpdateForces(body);

                        body.Update(deltaTime);
                    }
                }
            });
        }
Ejemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="octant"></param>
        /// <param name="context"></param>
        /// <param name="sphere"></param>
        /// <param name="result"></param>
        /// <param name="isIntersect"></param>
        /// <returns></returns>
        protected override bool FindNearestPointBySphereExcludeChild(ref Octant octant, RenderContext context,
                                                                     ref BoundingSphere sphere, ref List <HitTestResult> result, ref bool isIntersect)
        {
            bool isHit      = false;
            var  tempResult = new HitTestResult();

            tempResult.Distance = float.MaxValue;
            if (!BoxDisjointSphere(octant.Bound, ref sphere))
            {
                isIntersect = true;
                for (int i = octant.Start; i < octant.End; ++i)
                {
                    if (!BoxDisjointSphere(Objects[i].Value, ref sphere))
                    {
                        Vector3 cloestPoint;

                        var idx = Objects[i].Key * 3;
                        var t1  = Indices[idx];
                        var t2  = Indices[idx + 1];
                        var t3  = Indices[idx + 2];
                        var v0  = Positions[t1];
                        var v1  = Positions[t2];
                        var v2  = Positions[t3];
                        Collision.ClosestPointPointTriangle(ref sphere.Center, ref v0, ref v1, ref v2, out cloestPoint);
                        var d = (cloestPoint - sphere.Center).Length();
                        if (tempResult.Distance > d)
                        {
                            tempResult.Distance        = d;
                            tempResult.IsValid         = true;
                            tempResult.PointHit        = cloestPoint;
                            tempResult.TriangleIndices = new Tuple <int, int, int>(t1, t2, t3);
                            tempResult.Tag             = Objects[i].Key;
                            isHit = true;
                        }
                    }
                }
                if (isHit)
                {
                    isHit = false;
                    if (result.Count > 0)
                    {
                        if (result[0].Distance > tempResult.Distance)
                        {
                            result[0] = tempResult;
                            isHit     = true;
                        }
                    }
                    else
                    {
                        result.Add(tempResult);
                        isHit = true;
                    }
                }
            }
            else
            {
                isIntersect = false;
            }
            return(isHit);
        }
        /// <summary>
        /// Hits the test current node exclude child.
        /// </summary>
        /// <param name="octant">The octant.</param>
        /// <param name="context">The context.</param>
        /// <param name="model">The model.</param>
        /// <param name="geometry"></param>
        /// <param name="modelMatrix">The model matrix.</param>
        /// <param name="rayWS">The ray ws.</param>
        /// <param name="rayModel">The ray model.</param>
        /// <param name="hits">The hits.</param>
        /// <param name="isIntersect">if set to <c>true</c> [is intersect].</param>
        /// <param name="hitThickness">The hit thickness.</param>
        /// <returns></returns>
        protected override bool HitTestCurrentNodeExcludeChild(ref Octant octant, RenderContext context, object model, Geometry3D geometry, Matrix modelMatrix, ref Ray rayWS, ref Ray rayModel, ref List <HitTestResult> hits, ref bool isIntersect, float hitThickness)
        {
            isIntersect = false;
            if (!octant.IsBuilt)
            {
                return(false);
            }
            bool isHit = false;
            var  bound = octant.Bound.Transform(modelMatrix);

            if (rayWS.Intersects(ref bound))
            {
                isIntersect = true;
                for (int i = octant.Start; i < octant.End; ++i)
                {
                    var b = Objects[i].Value.Transform(modelMatrix);
                    if (b.Intersects(ref rayWS))
                    {
                        var result = new HitTestResult()
                        {
                            Tag = Objects[i].Key
                        };
                        hits.Add(result);
                        isHit = true;
                    }
                }
            }
            return(isHit);
        }
Ejemplo n.º 6
0
    public int trackOctant(List <int> octList)
    {
        // redundant judgment here...
        if (trackedOctant [CommonUtility.getStringFromList(octList)] == null)
        {
            Octant oct = instance.getOctantByIndex(octList);
            if (oct == null)
            {
                oct = instance.addOctant(octList);
            }
            instance.trackOctant(oct);

            int xmin = 0;
            int ymin = 0;
            int zmin = 0;

            // instantiate the octant being tracked.
            // GetBoundaries uses ref ints
            string octantName = CommonUtility.getStringFromList(octList);
            CommonUtility.GetBoundaries(octantName, ref xmin, ref ymin, ref zmin);

            instantiateOctant(UnityConstants.minimapPrefix + octantName, xmin + 256, ymin + 256, zmin + 256);
            trackedOctant.Add(CommonUtility.getStringFromList(octList), new UnityEngine.Vector3(xmin + 256, ymin + 256, zmin + 256));
        }
        else
        {
        }
        return(1);
    }
        /// <summary>
        ///
        /// </summary>
        /// <param name="octant"></param>
        /// <param name="context"></param>
        /// <param name="sphere"></param>
        /// <param name="result"></param>
        /// <param name="isIntersect"></param>
        /// <returns></returns>
        protected override bool FindNearestPointBySphereExcludeChild(ref Octant octant, RenderContext context, ref BoundingSphere sphere,
                                                                     ref List <HitTestResult> result, ref bool isIntersect)
        {
            bool isHit      = false;
            var  tempResult = new LineHitTestResult();

            tempResult.Distance = float.MaxValue;
            if (!BoxDisjointSphere(octant.Bound, ref sphere))
            {
                isIntersect = true;
                for (int i = octant.Start; i < octant.End; ++i)
                {
                    if (!BoxDisjointSphere(Objects[i].Value, ref sphere))
                    {
                        Vector3 cloestPoint;

                        var   idx = Objects[i].Key * 3;
                        var   t1  = Indices[idx];
                        var   t2  = Indices[idx + 1];
                        var   v0  = Positions[t1];
                        var   v1  = Positions[t2];
                        float t;
                        float distance = LineBuilder.GetPointToLineDistance2D(ref sphere.Center, ref v0, ref v1, out cloestPoint, out t);
                        if (tempResult.Distance > distance)
                        {
                            tempResult.Distance          = distance;
                            tempResult.IsValid           = true;
                            tempResult.PointHit          = cloestPoint;
                            tempResult.TriangleIndices   = null;
                            tempResult.RayHitPointScalar = t;
                            tempResult.Tag = tempResult.LineIndex = Objects[i].Key;
                            isHit          = true;
                        }
                    }
                }
                if (isHit)
                {
                    isHit = false;
                    if (result.Count > 0)
                    {
                        if (result[0].Distance > tempResult.Distance)
                        {
                            result[0] = tempResult;
                            isHit     = true;
                        }
                    }
                    else
                    {
                        result.Add(tempResult);
                        isHit = true;
                    }
                }
            }
            else
            {
                isIntersect = false;
            }
            return(isHit);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Get child node by quadrant
 /// </summary>
 /// <returns></returns>
 public OctNode <T> this[Octant oc] {
     get {
         return(this[(int)oc]);
     }
     set{
         this[(int)oc] = value;
     }
 }
Ejemplo n.º 9
0
        /// <returns> returns number between 0-7 which represents what octant
        /// the largest bit represents x, middle represents y, smallest bit z</returns>
        public Octant OctantAtPosition(Vector3 point)
        {
            Octant x = point.x > AABB.Center.x ? Octant.X__ : 0;
            Octant y = point.y > AABB.Center.y ? Octant._Y_ : 0;
            Octant z = point.z > AABB.Center.z ? Octant.__Z : 0;

            return(x | y | z);
        }
Ejemplo n.º 10
0
        private void CheckNodePosition(LineSegment seg, Coordinate p0, Coordinate p1, int expectedPositionValue)
        {
            var octant   = Octant.GetOctant(seg.P0, seg.P1);
            int posValue = SegmentPointComparator.Compare(octant, p0, p1);

            //TestContext.WriteLine(octant + " " + p0 + " " + p1 + " " + posValue);
            Assert.IsTrue(posValue == expectedPositionValue);
        }
Ejemplo n.º 11
0
            /// <summary>
            /// Initializes a new instance of the <see cref="OctantArray"/> class.
            /// </summary>
            /// <param name="bound">The bound.</param>
            /// <param name="length">The length.</param>
            public OctantArray(BoundingBox bound, int length)
            {
                var octant = new Octant(-1, 0, ref bound);

                octant.Start = 0;
                octant.End   = length;
                array[0]     = octant;
                ++Count;
                //var size = System.Runtime.InteropServices.Marshal.SizeOf(octant);
            }
Ejemplo n.º 12
0
        ///<returns>returns values between [-1,-1,-1] and [1,1,1]</returns>
        public static Vector3Int OctantToVector3Int(Octant octant)
        {
            int x = (int)(octant & Octant.X__) >> 2;
            int y = (int)(octant & Octant._Y_) >> 1;
            int z = (int)(octant & Octant.__Z) >> 0;

            var vec = (new Vector3Int(x, y, z));
            var scaledBetweenOneAndMinusOne = (vec * 2) - new Vector3Int(1, 1, 1);

            return(scaledBetweenOneAndMinusOne);
        }
    void PushBothAxisToOctant(Vector3 point)
    {
        int horOctant;
        int vertOctant;

        Octant.PointsToOctant(parentTransform.position, point,
                              parentTransform.right, parentTransform.up, out horOctant, out vertOctant);

        virtualController.PushHorAxis(horOctant);
        virtualController.PushVertAxis(vertOctant);
    }
Ejemplo n.º 14
0
 public FieldOfView()
 {
     octants    = new Octant[8];
     octants[0] = new Octant(-1, 0, 0, 1);
     octants[1] = new Octant(0, -1, 1, 0);
     octants[2] = new Octant(0, -1, -1, 0);
     octants[3] = new Octant(-1, 0, 0, -1);
     octants[4] = new Octant(1, 0, 0, -1);
     octants[5] = new Octant(0, 1, -1, 0);
     octants[6] = new Octant(0, 1, 1, 0);
     octants[7] = new Octant(1, 0, 0, 1);
 }
Ejemplo n.º 15
0
    public void RemoveBlockFromOctree(Vector3 localPosition, Octant octant)
    {
        if (!octant.IsLeaf())
        {
            this.RemoveBlockFromOctree(localPosition, octant.Octants [this.GetIndexOfPosition(localPosition, octant.Position)]);
        }
        else
        {
        }

        octant.objectCount--;
    }
Ejemplo n.º 16
0
        public PointOctree(float minSize, System.Func <T, Vector3> getPosFunc, IEqualityComparer <T> comparer)
        {
            if (getPosFunc == null)
            {
                throw new System.ArgumentNullException("getPosFunc");
            }
            _minSize         = minSize;
            _getPositionFunc = getPosFunc;
            _comparer        = comparer ?? EqualityComparer <T> .Default;

            _root = new Octant(this, _minSize, Vector3.zero);
        }
Ejemplo n.º 17
0
        public PointOctree(System.Func <T, Vector3> getPosFunc)
        {
            if (getPosFunc == null)
            {
                throw new System.ArgumentNullException("getPosFunc");
            }
            _minSize         = DEFAULT_MINSIZE;
            _getPositionFunc = getPosFunc;
            _comparer        = EqualityComparer <T> .Default;

            _root = new Octant(this, _minSize, Vector3.zero);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Initialises a new instance of the <see cref="Config"/> class.
        /// </summary>
        /// <param name="acceptedEmailProviders">The value to use for the <see cref="AcceptedEmailProviders"/> property.</param>
        /// <param name="astronomicalUnit">The value to use for the <see cref="AstronomicalUnit"/> property.</param>
        /// <param name="bodyCount">The value to use for the <see cref="BodyCount"/> property.</param>
        /// <param name="centralBodyMass">The value to use for the <see cref="CentralBodyMass"/> property.</param>
        /// <param name="eulerRotationStep">The value to use for the <see cref="EulerRotationStep"/> property.</param>
        /// <param name="frameRate">The value to use for the <see cref="FrameRate"/> property.</param>
        /// <param name="g">The value to use for the <see cref="G"/> property.</param>
        /// <param name="minimumTreeWidth">The value to use for the <see cref="MinimumTreeWidth"/> property.</param>
        /// <param name="outputFileDirectory">The value to use for the <see cref="OutputFileDirectory"/> property.</param>
        /// <param name="secondsPerTick">The value to use for the <see cref="SecondsPerTick"/> property.</param>
        /// <param name="simulationRate">The value to use for the <see cref="SimulationRate"/> property.</param>
        /// <param name="softeningFactor">The value to use for the <see cref="SofteningFactor"/> property.</param>
        /// <param name="softeningFactor2">The value to use for the <see cref="SofteningFactor2"/> property.</param>
        /// <param name="solarMass">The value to use for the <see cref="SolarMass"/> property.</param>
        /// <param name="storedPreviousPositionCount">The value to use for the <see cref="StoredPreviousPositionCount"/> property.</param>
        /// <param name="timeStep">The value to use for the <see cref="TimeStep"/> property.</param>
        /// <param name="treeTheta">The value to use for the <see cref="TreeTheta"/> property.</param>
        /// <param name="universeOctant">The value to use for the <see cref="UniverseOctant"/> property.</param>
        /// <param name="universeSize">The value to use for the <see cref="UniverseSize"/> property.</param>
        /// <param name="zoomStep">The value to use for the <see cref="ZoomStep"/> property.</param>
        /// <remarks>
        /// Intended to only be used with serialisers when setting the read-only properties on this instance.
        /// </remarks>
        public Config(string[] acceptedEmailProviders, double astronomicalUnit, int bodyCount,
                      double centralBodyMass, double eulerRotationStep, uint frameRate, double g,
                      double minimumTreeWidth, string outputFileDirectory, double secondsPerTick,
                      uint simulationRate, double softeningFactor, double softeningFactor2, double solarMass,
                      int storedPreviousPositionCount, double timeStep, double treeTheta, Octant universeOctant,
                      double universeSize, double zoomStep)
        {
            #region Setting values from parameters

            AcceptedEmailProviders = acceptedEmailProviders;

            AstronomicalUnit = astronomicalUnit;

            BodyCount = bodyCount;

            CentralBodyMass = centralBodyMass;

            EulerRotationStep = eulerRotationStep;

            FrameRate = frameRate;

            G = g;

            MinimumTreeWidth = minimumTreeWidth;

            OutputFileDirectory = outputFileDirectory;

            SecondsPerTick = secondsPerTick;

            SimulationRate = simulationRate;

            SofteningFactor = softeningFactor;

            SofteningFactor2 = softeningFactor2;

            SolarMass = solarMass;

            StoredPreviousPositionCount = storedPreviousPositionCount;

            TimeStep = timeStep;

            TreeTheta = treeTheta;

            UniverseOctant = universeOctant;

            UniverseSize = universeSize;

            ZoomStep = zoomStep;

            #endregion Setting values from parameters
        }
            /// <summary>
            ///
            /// </summary>
            /// <param name="octant"></param>
            /// <param name="context"></param>
            /// <param name="sphere"></param>
            /// <param name="result"></param>
            /// <param name="isIntersect"></param>
            /// <returns></returns>
            protected override bool FindNearestPointBySphereExcludeChild(ref Octant octant, IRenderMatrices context,
                                                                         ref BoundingSphere sphere, ref List <HitTestResult> result, ref bool isIntersect)
            {
                bool isHit      = false;
                var  resultTemp = new HitTestResult();

                resultTemp.Distance = float.MaxValue;
                if (!BoxDisjointSphere(octant.Bound, ref sphere))
                {
                    isIntersect = true;
                    for (int i = octant.Start; i < octant.End; ++i)
                    {
                        var p = Positions[Objects[i]];
                        if (sphere.Contains(ref p) != ContainmentType.Disjoint)
                        {
                            var d = (p - sphere.Center).Length();
                            if (resultTemp.Distance > d)
                            {
                                resultTemp.Distance = d;
                                resultTemp.IsValid  = true;
                                resultTemp.PointHit = p;
                                resultTemp.Tag      = Objects[i];
                                isHit = true;
                            }
                        }
                    }
                    if (isHit)
                    {
                        isHit = false;
                        if (result.Count > 0)
                        {
                            if (result[0].Distance > resultTemp.Distance)
                            {
                                result[0] = resultTemp;
                                isHit     = true;
                            }
                        }
                        else
                        {
                            result.Add(resultTemp);
                            isHit = true;
                        }
                    }
                }
                else
                {
                    isIntersect = false;
                }
                return(isHit);
            }
Ejemplo n.º 20
0
 public bool Add(int parentIndex, int childIndex, BoundingBox bound, ref Octant newParent)
 {
     if (array.Length < Count + OctantSize)
     {
         var newSize = array.Length * 2;
         if (newSize > int.MaxValue / 4) //Size is too big
         {
             return(false);
         }
         var newArray = new Octant[array.Length * 2];
         Array.Copy(array, newArray, Count);
         array = newArray;
     }
     ref var parent = ref array[parentIndex];
Ejemplo n.º 21
0
    public bool Subdivide()
    {
        if (this.depth == 0)
        {
            return(false);
        }

        this.octants = new Octant[8];

        float   quarterSize = this.octantSize * 0.25f;
        float   haftSize    = this.octantSize * 0.5f;
        Vector3 newPosition;

        for (int i = 0; i < 8; i++)
        {
            newPosition = this.octantPosition;

            if ((i & 4) == 4)                           //Make new octant position
            {
                newPosition.y += quarterSize;
            }
            else
            {
                newPosition.y -= quarterSize;
            }

            if ((i & 2) == 2)
            {
                newPosition.x += quarterSize;
            }
            else
            {
                newPosition.x -= quarterSize;
            }

            if ((i & 1) == 1)
            {
                newPosition.z += quarterSize;
            }
            else
            {
                newPosition.z -= quarterSize;
            }

            octants [i] = new Octant(newPosition, haftSize, this.depth - 1);
        }

        return(true);
    }
Ejemplo n.º 22
0
            private void Split()
            {
                float quarter   = this.sideLength / 4f;
                float newLength = this.sideLength / 2;

                children    = new Octant[8];
                children[0] = new Octant(_owner, newLength, center + new Vector3(-quarter, quarter, -quarter));
                children[1] = new Octant(_owner, newLength, center + new Vector3(quarter, quarter, -quarter));
                children[2] = new Octant(_owner, newLength, center + new Vector3(-quarter, quarter, quarter));
                children[3] = new Octant(_owner, newLength, center + new Vector3(quarter, quarter, quarter));
                children[4] = new Octant(_owner, newLength, center + new Vector3(-quarter, -quarter, -quarter));
                children[5] = new Octant(_owner, newLength, center + new Vector3(quarter, -quarter, -quarter));
                children[6] = new Octant(_owner, newLength, center + new Vector3(-quarter, -quarter, quarter));
                children[7] = new Octant(_owner, newLength, center + new Vector3(quarter, -quarter, quarter));
            }
Ejemplo n.º 23
0
    void DrawNode(Octant node, int nodeDepth = 0)
    {
        if (!node.IsLeaf())
        {
            for (int i = 0; i < 8; i++)
            {
                DrawNode(node.Octants [i], nodeDepth + 1);
            }
        }

        //if (!node.IsEmpty ()) {
        Gizmos.color = Color.Lerp(minColor, maxColor, nodeDepth / (float)4);
        Gizmos.DrawWireCube(node.Position, Vector3.one * node.OctantSize);
        //}
    }
Ejemplo n.º 24
0
    public int untrackOctant(List <int> octList)
    {
        Octant oct = instance.getOctantByIndex(octList);

        // what is the relationship between tracked octant and kept octant in DiscoverModule
        if (oct != null)
        {
            instance.untrackOctant(oct);
        }
        string octantName = CommonUtility.getStringFromList(octList);

        // Added for the purpose of hinding everything in that octant
        int xmin = 0;
        int ymin = 0;
        int zmin = 0;

        CommonUtility.GetBoundaries(octantName, ref xmin, ref ymin, ref zmin);

        destroyOctant(UnityConstants.minimapPrefix + octantName, xmin, ymin, zmin);
        return(1);
    }
Ejemplo n.º 25
0
    public void AddBlockToOctree(Vector3 localPosition, Octant octant)
    {
        if (!octant.IsLeaf())
        {
            this.AddBlockToOctree(localPosition, octant.Octants [this.GetIndexOfPosition(localPosition, octant.Position)]);
        }
        else
        {
            if (octant.objectCount == 0)
            {
                octant.data = localPosition;
            }
            else if (octant.Subdivide())
            {
                this.AddBlockToOctree(localPosition, octant.Octants [this.GetIndexOfPosition(localPosition, octant.Position)]);
                this.AddBlockToOctree(octant.data, octant.Octants [this.GetIndexOfPosition(octant.data, octant.Position)]);
            }
        }

        octant.objectCount++;
    }
Ejemplo n.º 26
0
            /// <summary>
            /// Hits the test current node exclude child.
            /// </summary>
            /// <param name="octant">The octant.</param>
            /// <param name="context">The context.</param>
            /// <param name="model">The model.</param>
            /// <param name="geometry"></param>
            /// <param name="modelMatrix">The model matrix.</param>
            /// <param name="rayModel">The ray model.</param>
            /// <param name="returnMultiple"></param>
            /// <param name="hits">The hits.</param>
            /// <param name="isIntersect">if set to <c>true</c> [is intersect].</param>
            /// <param name="hitThickness">The hit thickness.</param>
            /// <returns></returns>
            protected override bool HitTestCurrentNodeExcludeChild(ref Octant octant, HitTestContext context,
                                                                   object model, Geometry3D geometry, Matrix modelMatrix, ref Ray rayModel, bool returnMultiple,
                                                                   ref List <HitTestResult> hits, ref bool isIntersect, float hitThickness)
            {
                isIntersect = false;
                if (!octant.IsBuilt)
                {
                    return(false);
                }
                var isHit = false;
                var bound = octant.Bound.Transform(modelMatrix);
                var rayWS = context.RayWS;

                if (rayWS.Intersects(ref bound))
                {
                    isIntersect = true;
                    for (var i = octant.Start; i < octant.End; ++i)
                    {
                        var b = Objects[i].Value.Transform(modelMatrix);
                        if (b.Intersects(ref rayWS))
                        {
                            ref var geo = ref Geometries[Objects[i].Key];
                            if (geo.Geometry is MeshGeometry3D mesh)
                            {
                                var currCount = hits.Count;
                                var hasHit    = mesh.HitTest(context, geo.ModelTransform * modelMatrix, ref hits, model);
                                if (hasHit)
                                {
                                    var newCount = hits.Count;
                                    for (var j = currCount; j < newCount; ++j)
                                    {
                                        hits.Add(new BatchedMeshHitTestResult(Objects[i].Key, ref geo, hits[j]));
                                    }
                                    hits.RemoveRange(currCount, newCount - currCount);
                                }
                                isHit |= hasHit;
                            }
                        }
                    }
                }
Ejemplo n.º 27
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="octant"></param>
        /// <returns> returns -1 if no child exists, otherwise the index into the nodes element</returns>
        public IndexToOctNode GetChildNodeFromOctant(Octant octant)
        {
            switch (octant)
            {
            case Octant.XYZ: return(ChildXYZ);

            case Octant._YZ: return(Child_YZ);

            case Octant.X_Z: return(ChildX_Z);

            case Octant.XY_: return(ChildXY_);

            case Octant.__Z: return(Child__Z);

            case Octant.X__: return(ChildX__);

            case Octant._Y_: return(Child_Y_);

            case Octant.___: return(Child___);

            default: throw new ArgumentException("octant must be between values 0 to 7!");
            }
        }
 void FindTargetDistances()
 {
     Octant.PointsToDistances(parentTransform.position, goals.GetTargetPosition(),
                              parentTransform.right, parentTransform.up, out horDistance, out vertDistance);
 }
 void FindTargetOctant()
 {
     Octant.PointsToOctant(parentTransform.position, goals.GetTargetPosition(),
                           parentTransform.right, parentTransform.up, out horOctant, out vertOctant);
 }
 /// <summary>
 /// Finds the nearest point by sphere exclude child.
 /// </summary>
 /// <param name="octant">The octant.</param>
 /// <param name="context">The context.</param>
 /// <param name="sphere">The sphere.</param>
 /// <param name="points">The points.</param>
 /// <param name="isIntersect">if set to <c>true</c> [is intersect].</param>
 /// <returns></returns>
 protected override bool FindNearestPointBySphereExcludeChild(ref Octant octant, RenderContext context, ref BoundingSphere sphere, ref List <HitTestResult> points, ref bool isIntersect)
 {
     return(false);
 }