CoalescedSceneObjects.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Linq;
  30. using OpenMetaverse;
  31. namespace OpenSim.Region.Framework.Scenes
  32. {
  33. /// <summary>
  34. /// Represents a coalescene of scene objects. A coalescence occurs when objects that are not in the same linkset
  35. /// are grouped together.
  36. /// </summary>
  37. public class CoalescedSceneObjects
  38. {
  39. /// <summary>
  40. /// The creator of this coalesence, though not necessarily the objects within it.
  41. /// </summary>
  42. public UUID CreatorId { get; set; }
  43. /// <summary>
  44. /// The number of objects in this coalesence
  45. /// </summary>
  46. public int Count
  47. {
  48. get
  49. {
  50. lock (m_memberObjects)
  51. return m_memberObjects.Count;
  52. }
  53. }
  54. /// <summary>
  55. /// Does this coalesence have any member objects?
  56. /// </summary>
  57. public bool HasObjects { get { return Count > 0; } }
  58. /// <summary>
  59. /// Get the objects currently in this coalescence
  60. /// </summary>
  61. public List<SceneObjectGroup> Objects
  62. {
  63. get
  64. {
  65. lock (m_memberObjects)
  66. return new List<SceneObjectGroup>(m_memberObjects);
  67. }
  68. }
  69. /// <summary>
  70. /// Get the scene that contains the objects in this coalescence. If there are no objects then null is returned.
  71. /// </summary>
  72. public Scene Scene
  73. {
  74. get
  75. {
  76. if (!HasObjects)
  77. return null;
  78. else
  79. return Objects[0].Scene;
  80. }
  81. }
  82. /// <summary>
  83. /// At this point, we need to preserve the order of objects added to the coalescence, since the first
  84. /// one will end up matching the item name when rerezzed.
  85. /// </summary>
  86. protected List<SceneObjectGroup> m_memberObjects = new List<SceneObjectGroup>();
  87. public CoalescedSceneObjects(UUID creatorId)
  88. {
  89. CreatorId = creatorId;
  90. }
  91. public CoalescedSceneObjects(UUID creatorId, params SceneObjectGroup[] objs) : this(creatorId)
  92. {
  93. foreach (SceneObjectGroup obj in objs)
  94. Add(obj);
  95. }
  96. /// <summary>
  97. /// Add an object to the coalescence.
  98. /// </summary>
  99. /// <param name="obj"></param>
  100. /// <param name="offset">The offset of the object within the group</param>
  101. public void Add(SceneObjectGroup obj)
  102. {
  103. lock (m_memberObjects)
  104. m_memberObjects.Add(obj);
  105. }
  106. /// <summary>
  107. /// Removes a scene object from the coalescene
  108. /// </summary>
  109. /// <param name="sceneObjectId"></param>
  110. /// <returns>true if the object was there to be removed, false if not.</returns>
  111. public bool Remove(SceneObjectGroup obj)
  112. {
  113. lock (m_memberObjects)
  114. return m_memberObjects.Remove(obj);
  115. }
  116. /// <summary>
  117. /// Get the total size of the coalescence (the size required to cover all the objects within it) and the
  118. /// offsets of each of those objects.
  119. /// </summary>
  120. /// <param name="size"></param>
  121. /// <returns>
  122. /// An array of offsets. The order of objects is the same as returned from the Objects property
  123. /// </returns>
  124. public Vector3[] GetSizeAndOffsets(out Vector3 size)
  125. {
  126. float minX, minY, minZ;
  127. float maxX, maxY, maxZ;
  128. Vector3[] offsets
  129. = Scene.GetCombinedBoundingBox(
  130. Objects, out minX, out maxX, out minY, out maxY, out minZ, out maxZ);
  131. float sizeX = maxX - minX;
  132. float sizeY = maxY - minY;
  133. float sizeZ = maxZ - minZ;
  134. size = new Vector3(sizeX, sizeY, sizeZ);
  135. return offsets;
  136. }
  137. }
  138. }