public IBufferArray Resize(int newSize)
        {
            var newArr = new NativeArray <T>(newSize, Allocator.Persistent);

            if (this.arr.IsCreated == true)
            {
                NativeArrayUtils.Copy(this.arr, ref newArr, newSize > this.Length ? this.Length : newSize);
            }
            return(new NativeBufferArray <T>(newArr, newSize));
        }
        internal void CompleteRayCastHits()
        {
            m_Normals.Clear();

            var scaledMinimumDistance = m_MinHitDistance * m_DistanceScale;

            var length = m_Raycasts.Length;

            m_Normals.EnsureCapacity(length);
            NativeArrayUtils.EnsureCapacity(ref m_Identifiers, length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            NativeArrayUtils.EnsureCapacity(ref m_Positions, length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            NativeArrayUtils.EnsureCapacity(ref m_ConfidenceValues, length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);

            var hitCount = 0;

            for (var i = 0; i < length; i++)
            {
                var result = m_RaycastHits[i];
                if (result.collider == null)
                {
                    continue;
                }

                if (result.distance < scaledMinimumDistance)
                {
                    continue;
                }

                m_Normals.Add(this.ApplyInverseOffsetToDirection(result.normal));
                m_Identifiers[hitCount]      = (ulong)i;
                m_Positions[hitCount]        = this.ApplyInverseOffsetToPosition(result.point);
                m_ConfidenceValues[hitCount] = Random.Range(0f, 1f);
                hitCount++;
            }

            var data = new PointCloudData
            {
                Identifiers      = new NativeSlice <ulong>(m_Identifiers, 0, hitCount),
                Positions        = new NativeSlice <Vector3>(m_Positions, 0, hitCount),
                ConfidenceValues = new NativeSlice <float>(m_ConfidenceValues, 0, hitCount)
            };

            m_Data[m_TrackableId] = data;

            if (PointCloudUpdated != null)
            {
                PointCloudUpdated(GetPoints());
            }
        }
Exemple #3
0
 public void Clear()
 {
     #if ENABLE_UNITY_COLLECTIONS_CHECKS
     AtomicSafetyHandle.CheckWriteAndBumpSecondaryVersion(this.safetyHandle);
     #endif
     //this.lookup.Clear();
     //this.nodes.Clear();
     NativeArrayUtils.Clear(this.lookup);
     NativeArrayUtils.Clear(this.nodes);
     NativeArrayUtils.Clear(this.elements);
     //this.elements.Clear();
     //UnsafeUtility.MemClear(this.lookup->Ptr, this.lookup->Capacity * UnsafeUtility.SizeOf<int>());
     //UnsafeUtility.MemClear(this.nodes->Ptr, this.nodes->Capacity * UnsafeUtility.SizeOf<QuadNode>());
     //UnsafeUtility.MemClear(this.elements->Ptr, this.elements->Capacity * UnsafeUtility.SizeOf<QuadElement<T>>());
     this.elementsCount = 0;
 }
Exemple #4
0
    public void RemoveRuntimeCondition(RuntimeCondition condition)
    {
        positions     = NativeArrayUtils.RemoveRange(positions, condition.startIndex, condition.numberOfParticles);
        positionsCopy = NativeArrayUtils.RemoveRange(positionsCopy, condition.startIndex, condition.numberOfParticles);
        velocities    = NativeArrayUtils.RemoveRange(velocities, condition.startIndex, condition.numberOfParticles);
        masses        = NativeArrayUtils.RemoveRange(masses, condition.startIndex, condition.numberOfParticles);
        particles     = NativeArrayUtils.RemoveRange(particles, condition.startIndex, condition.numberOfParticles);

        nTotal -= condition.numberOfParticles;
        pSystem.SetParticles(particles, nTotal);

        int conditionIndex = runtimeConditionsSet.IndexOf(condition);

        for (int i = conditionIndex + 1; i < runtimeConditionsSet.Count; i++)
        {
            runtimeConditionsSet[i].startIndex -= condition.numberOfParticles;
        }

        runtimeConditionsSet.Remove(condition);
    }
Exemple #5
0
        public void ClearAndBulkInsert(NativeArray <QuadElement <T> > incomingElements, int incomingElementsLength)
        {
            // Always have to clear before bulk insert as otherwise the lookup and node allocations need to account
            // for existing data.
            this.Clear();

            #if ENABLE_UNITY_COLLECTIONS_CHECKS
            AtomicSafetyHandle.CheckWriteAndBumpSecondaryVersion(this.safetyHandle);
            #endif

            // Resize if needed
            if (this.elements.Length < this.elementsCount + incomingElementsLength)
            {
                NativeArrayUtils.Resize(math.max(incomingElementsLength, this.elements.Length * 2), ref this.elements, this.allocator);
                //this.elements.Resize(math.max(incomingElementsLength, this.elements.Capacity * 2), NativeArrayOptions.UninitializedMemory);
            }

            // Prepare morton codes
            var mortonCodes         = new NativeArray <int>(incomingElementsLength, Allocator.Temp);
            var depthExtentsScaling = LookupTables.DepthLookup[this.maxDepth] / this.bounds.Extents;
            for (var i = 0; i < incomingElementsLength; i++)
            {
                var incPos = incomingElements[i].pos;
                incPos  -= this.bounds.Center;                  // Offset by center
                incPos.y = -incPos.y;                           // World -> array
                var pos = (incPos + this.bounds.Extents) * .5f; // Make positive
                // Now scale into available space that belongs to the depth
                pos *= depthExtentsScaling;
                // And interleave the bits for the morton code
                mortonCodes[i] = LookupTables.MortonLookup[(int)pos.x] | (LookupTables.MortonLookup[(int)pos.y] << 1);
            }

            // Index total child element count per node (total, so parent's counts include those of child nodes)
            for (var i = 0; i < mortonCodes.Length; i++)
            {
                var atIndex = 0;
                var val     = mortonCodes[i];
                for (var depth = 0; depth <= this.maxDepth; depth++)
                {
                    // Increment the node on this depth that this element is contained in
                    //(*(int*)((IntPtr)this.lookup->Ptr + atIndex * sizeof(int)))++;
                    ++this.lookup.GetRef(atIndex);
                    atIndex = this.IncrementIndex(depth, val, atIndex);
                }
            }

            // Prepare the tree leaf nodes
            this.RecursivePrepareLeaves(1, 1);

            // Add elements to leaf nodes
            for (var i = 0; i < incomingElementsLength; i++)
            {
                var atIndex = 0;
                var val     = mortonCodes[i];
                for (var depth = 0; depth <= this.maxDepth; depth++)
                {
                    var node = this.nodes[atIndex]; //UnsafeUtility.ReadArrayElement<QuadNode>(this.nodes->Ptr, atIndex);
                    if (node.isLeaf)
                    {
                        // We found a leaf, add this element to it and move to the next element
                        //UnsafeUtility.WriteArrayElement(this.elements->Ptr, node.firstChildIndex + node.count, incomingElements[i]);
                        this.elements[node.firstChildIndex + node.count] = incomingElements[i];
                        node.count++;
                        this.nodes[atIndex] = node;
                        //UnsafeUtility.WriteArrayElement(this.nodes->Ptr, atIndex, node);
                        break;
                    }

                    // No leaf found, we keep going deeper until we find one
                    atIndex = this.IncrementIndex(depth, val, atIndex);
                }
            }

            mortonCodes.Dispose();
        }
Exemple #6
0
        public NativeArrayBurst(NativeArrayBurst <T> array, Allocator allocator)
        {
            NativeArrayBurst <T> .Allocate(array.Length, allocator, out this);

            NativeArrayUtils.Copy(array, ref this);
        }
        void ProcessPointCloudEvent(PointCloudEventMarker pointCloudEvent)
        {
            var idCount              = 0;
            var positionCount        = 0;
            var confidenceValueCount = 0;

            foreach (var pointCloud in pointCloudEvent.Data)
            {
                var identifiers = pointCloud.Identifiers;
                if (identifiers != null)
                {
                    idCount += identifiers.Length;
                }

                var positions = pointCloud.Positions;
                if (positions != null)
                {
                    positionCount += positions.Length;
                }

                var confidenceValues = pointCloud.ConfidenceValues;
                if (confidenceValues != null)
                {
                    confidenceValueCount += confidenceValues.Length;
                }
            }

            NativeArrayUtils.EnsureCapacity(ref m_Identifiers, idCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            NativeArrayUtils.EnsureCapacity(ref m_Positions, idCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            NativeArrayUtils.EnsureCapacity(ref m_ConfidenceValues, idCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);

            idCount              = 0;
            positionCount        = 0;
            confidenceValueCount = 0;
            m_Data.Clear();
            foreach (var pointCloud in pointCloudEvent.Data)
            {
                var data      = new PointCloudData();
                var positions = pointCloud.Positions;
                if (positions == null)
                {
                    continue;
                }

                var length = positions.Length;
                for (var i = 0; i < length; i++)
                {
                    m_Positions[i + positionCount] = positions[i];
                }

                data.Positions = new NativeSlice <Vector3>(m_Positions, positionCount, length);
                positionCount += length;

                var identifiers = pointCloud.Identifiers;
                if (identifiers != null && identifiers.Length == length)
                {
                    for (var i = 0; i < length; i++)
                    {
                        m_Identifiers[i + idCount] = identifiers[i];
                    }

                    data.Identifiers = new NativeSlice <ulong>(m_Identifiers, idCount, length);
                    idCount         += length;
                }

                var confidenceValues = pointCloud.ConfidenceValues;
                if (confidenceValues != null && confidenceValues.Length == length)
                {
                    for (var i = 0; i < length; i++)
                    {
                        m_ConfidenceValues[i + confidenceValueCount] = confidenceValues[i];
                    }

                    data.ConfidenceValues = new NativeSlice <float>(m_ConfidenceValues, confidenceValueCount, length);
                    confidenceValueCount += length;
                }

                m_Data[pointCloud.Id] = data;
            }

            PointCloudUpdated?.Invoke(GetPoints());
        }
Exemple #8
0
    public void AddInitialConditions(List <InitialCondition> initialConditions)
    {
        int nTotalOld = nTotal;
        int n         = nTotalOld;

        for (int j = 0; j < initialConditions.Count; j++)
        {
            runtimeConditionsSet.Add(
                new RuntimeCondition
            {
                startIndex        = n,
                numberOfParticles = initialConditions[j].numberOfParticles
            }
                );

            for (int i = 0; i < initialConditions[j].numberOfParticles; i++)
            {
                n++;
            }
        }

        nTotal = n;

        if (!positions.IsCreated || positions.Length == 0)
        {
            positions     = new NativeArray <double3>(n, Allocator.Persistent);
            positionsCopy = new NativeArray <double3>(n, Allocator.Persistent);
            velocities    = new NativeArray <double3>(n, Allocator.Persistent);
            masses        = new NativeArray <double>(n, Allocator.Persistent);

            particles = new ParticleSystem.Particle[n];
        }
        else
        {
            positions     = NativeArrayUtils.Append(positions, new NativeArray <double3>(nTotal - nTotalOld, Allocator.Persistent));
            positionsCopy = NativeArrayUtils.Append(positionsCopy, new NativeArray <double3>(nTotal - nTotalOld, Allocator.Persistent));
            velocities    = NativeArrayUtils.Append(velocities, new NativeArray <double3>(nTotal - nTotalOld, Allocator.Persistent));
            masses        = NativeArrayUtils.Append(masses, new NativeArray <double>(nTotal - nTotalOld, Allocator.Persistent));

            particles = NativeArrayUtils.Append(particles, new ParticleSystem.Particle[nTotal - nTotalOld]);
        }

        n = 0;

        for (int j = 0; j < initialConditions.Count; j++)
        {
            UnityEngine.Random.InitState(initialConditions[j].seed);

            for (int i = 0; i < initialConditions[j].numberOfParticles; i++)
            {
                float3 randPos = ElipsoidRandom(initialConditions[j].shapeElipsoid, initialConditions[j].center, initialConditions[j].radius);

                int k = n + nTotalOld;

                positions[k]     = randPos;
                positionsCopy[k] = randPos;

                float3 crossProd = initialConditions[j].angularVelocity * Vector3.Cross(Vector3.up, randPos);
                velocities[k] = crossProd;

                float3 turbulence = ElipsoidRandom(initialConditions[j].turbulenceElipsoid, float3.zero, initialConditions[j].turbulenceStrength);
                velocities[k] += turbulence;

                masses[k] = UnityEngine.Random.Range(0.1f, 100f);

                particles[k].position          = randPos;
                particles[k].angularVelocity   = 0f;
                particles[k].rotation          = 0f;
                particles[k].velocity          = new Vector3(0f, 0f, 0f);
                particles[k].startLifetime     = 10f;
                particles[k].remainingLifetime = 10f - UnityEngine.Random.Range(0f, 1f);
                particles[k].startSize         = 0.1f;
                particles[k].startColor        = Color.white;

                n++;
            }
        }

        pSystem.SetParticles(particles, nTotal);
    }
 public void Clear(int index, int length)
 {
     NativeArrayUtils.Clear(this.arr, index, length);
 }
 public void Clear()
 {
     NativeArrayUtils.Clear(this.arr);
 }