public void CustomIntervalKDTreeTests()
        {
            IntervalKDTree <string> tree = new IntervalKDTree <string>(100, 10);

            tree.Put(-20, -20, -20, 20, 20, 20, "test");

            HashSet <string> values = new HashSet <string>();

            tree.GetValues(-1, -21, -1, 1, -19, 1, values);
            Assert.AreEqual(1, values.Count);
        }
Exemple #2
0
        public void PutObject(CloudObject cloudObject, bool publicUpdate)
        {
            if (!objects.ContainsKey(cloudObject.ObjectId))
            {
                // Assigning local cache index to the object. This will be used when object is sent to clients.
                cloudObject.LastUpdated = DateTime.Now;
                localObjectIndexCounter++;
                cloudObject.LocalObjectIndex = localObjectIndexCounter;
                if (cloudObject.RemoteObjectIndex == 0)
                {
                    // For participant lets set remote object index.
                    cloudObject.RemoteObjectIndex = localObjectIndexCounter;
                }

                objects.Add(cloudObject.ObjectId, cloudObject);
                objectList.Add(cloudObject.ObjectId);
                if (!bubbleObjects.ContainsKey(cloudObject.BubbleId))
                {
                    bubbleObjects.Add(cloudObject.BubbleId, new Dictionary <Guid, Guid>());
                    bubbleObjectGuidIndexDictionary.Add(cloudObject.BubbleId, new Dictionary <Guid, uint>());
                    bubbleObjectIndexGuidDictionary.Add(cloudObject.BubbleId, new Dictionary <uint, Guid>());
                }
                bubbleObjects[cloudObject.BubbleId].Add(cloudObject.ObjectId, cloudObject.ObjectId);
                bubbleObjectGuidIndexDictionary[cloudObject.BubbleId].Add(cloudObject.ObjectId, cloudObject.RemoteObjectIndex);
                bubbleObjectIndexGuidDictionary[cloudObject.BubbleId].Add(cloudObject.RemoteObjectIndex, cloudObject.ObjectId);

                objectBubbleDictionary.Add(cloudObject.ObjectId, cloudObject.BubbleId);
                objectKDTree.Put(cloudObject.Location.X - cloudObject.BoundingSphereRadius,
                                 cloudObject.Location.Y - cloudObject.BoundingSphereRadius,
                                 cloudObject.Location.Z - cloudObject.BoundingSphereRadius,
                                 cloudObject.Location.X + cloudObject.BoundingSphereRadius,
                                 cloudObject.Location.Y + cloudObject.BoundingSphereRadius,
                                 cloudObject.Location.Z + cloudObject.BoundingSphereRadius,
                                 cloudObject.ObjectId
                                 );

                if (!participantObjects.ContainsKey(cloudObject.OwnerId))
                {
                    participantObjects.Add(cloudObject.OwnerId, new HashSet <Guid>());
                }
                participantObjects[cloudObject.OwnerId].Add(cloudObject.ObjectId);
                objectParticipantDictionary.Add(cloudObject.ObjectId, cloudObject.OwnerId);
            }
            else
            {
                if (cloudObject.RemoteObjectIndex == 0)
                {
                    // For participant lets set remote object index.
                    cloudObject.RemoteObjectIndex = cloudObject.LocalObjectIndex;
                }

                if (publicUpdate)
                {
                    cloudObject.LastUpdated = DateTime.Now;
                }

                if (objects[cloudObject.ObjectId] != cloudObject)
                {
                    throw new Exception("Cache must be updated with same cache object.");
                }

                // Updating changed bubble id.
                if (objectBubbleDictionary[cloudObject.ObjectId] != cloudObject.BubbleId)
                {
                    Guid oldBubbleId = objectBubbleDictionary[cloudObject.ObjectId];
                    bubbleObjects[oldBubbleId].Remove(cloudObject.ObjectId);
                    uint oldIndex = bubbleObjectGuidIndexDictionary[oldBubbleId][cloudObject.ObjectId];
                    bubbleObjectGuidIndexDictionary[oldBubbleId].Remove(cloudObject.ObjectId);
                    bubbleObjectIndexGuidDictionary[oldBubbleId].Remove(oldIndex);
                    if (bubbleObjects[objectBubbleDictionary[cloudObject.ObjectId]].Count == 0)
                    {
                        bubbleObjects.Remove(oldBubbleId);
                        bubbleObjectGuidIndexDictionary.Remove(oldBubbleId);
                        bubbleObjectIndexGuidDictionary.Remove(oldBubbleId);
                    }

                    if (!bubbleObjects.ContainsKey(cloudObject.BubbleId))
                    {
                        bubbleObjects.Add(cloudObject.BubbleId, new Dictionary <Guid, Guid>());
                        bubbleObjectGuidIndexDictionary.Add(cloudObject.BubbleId, new Dictionary <Guid, uint>());
                        bubbleObjectIndexGuidDictionary.Add(cloudObject.BubbleId, new Dictionary <uint, Guid>());
                    }
                    bubbleObjects[cloudObject.BubbleId].Add(cloudObject.ObjectId, cloudObject.ObjectId);
                    bubbleObjectGuidIndexDictionary[cloudObject.BubbleId].Add(cloudObject.ObjectId, cloudObject.RemoteObjectIndex);
                    bubbleObjectIndexGuidDictionary[cloudObject.BubbleId].Add(cloudObject.RemoteObjectIndex, cloudObject.ObjectId);
                    objectBubbleDictionary[cloudObject.ObjectId] = cloudObject.BubbleId;
                }

                // Updating changed object index.
                if (bubbleObjectGuidIndexDictionary[cloudObject.BubbleId][cloudObject.ObjectId] != cloudObject.RemoteObjectIndex)
                {
                    uint oldRemoteIndex = bubbleObjectGuidIndexDictionary[cloudObject.BubbleId][cloudObject.ObjectId];
                    bubbleObjectIndexGuidDictionary[cloudObject.BubbleId].Remove(oldRemoteIndex);
                    bubbleObjectGuidIndexDictionary[cloudObject.BubbleId][cloudObject.ObjectId] = cloudObject.RemoteObjectIndex;
                    bubbleObjectIndexGuidDictionary[cloudObject.BubbleId].Add(cloudObject.RemoteObjectIndex, cloudObject.ObjectId);
                }

                objectKDTree.Put(cloudObject.Location.X - cloudObject.BoundingSphereRadius,
                                 cloudObject.Location.Y - cloudObject.BoundingSphereRadius,
                                 cloudObject.Location.Z - cloudObject.BoundingSphereRadius,
                                 cloudObject.Location.X + cloudObject.BoundingSphereRadius,
                                 cloudObject.Location.Y + cloudObject.BoundingSphereRadius,
                                 cloudObject.Location.Z + cloudObject.BoundingSphereRadius,
                                 cloudObject.ObjectId
                                 );

                // Updating changed object participantId
                if (cloudObject.OwnerId != objectParticipantDictionary[cloudObject.ObjectId])
                {
                    participantObjects[objectParticipantDictionary[cloudObject.ObjectId]].Remove(cloudObject.ObjectId);
                    if (participantObjects[objectParticipantDictionary[cloudObject.ObjectId]].Count == 0)
                    {
                        participantObjects.Remove(objectParticipantDictionary[cloudObject.ObjectId]);
                    }
                    if (!participantObjects.ContainsKey(cloudObject.OwnerId))
                    {
                        participantObjects.Add(cloudObject.OwnerId, new HashSet <Guid>());
                    }
                    participantObjects[cloudObject.OwnerId].Add(cloudObject.ObjectId);
                    objectParticipantDictionary[cloudObject.ObjectId] = cloudObject.OwnerId;
                }
            }
            if (publicUpdate)
            {
                CacheObjectPut(cloudObject);
            }
        }