SceneObjectGroupDiff.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 OpenSim 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. #region Header
  28. // SceneObjectGroupDiff.cs
  29. // User: bongiojp
  30. #endregion Header
  31. using System;
  32. using System.Collections.Generic;
  33. using System.Diagnostics;
  34. using System.Drawing;
  35. using OpenMetaverse;
  36. using Nini.Config;
  37. using OpenSim.Framework;
  38. using OpenSim.Region.Environment.Interfaces;
  39. using OpenSim.Region.Environment.Scenes;
  40. using OpenSim.Region.Physics.Manager;
  41. using log4net;
  42. namespace OpenSim.Region.Environment.Modules.ContentManagement
  43. {
  44. #region Enumerations
  45. [Flags]
  46. public enum Diff
  47. {
  48. NONE = 0,
  49. FACECOLOR = 1,
  50. SHAPE = 1<<1,
  51. MATERIAL = 1<<2,
  52. TEXTURE = 1<<3,
  53. SCALE = 1<<4,
  54. POSITION = 1<<5,
  55. OFFSETPOSITION = 1<<6,
  56. ROTATIONOFFSET = 1<<7,
  57. ROTATIONALVELOCITY = 1<<8,
  58. ACCELERATION = 1<<9,
  59. ANGULARVELOCITY = 1<<10,
  60. VELOCITY = 1<<11,
  61. OBJECTOWNER = 1<<12,
  62. PERMISSIONS = 1<<13,
  63. DESCRIPTION = 1<<14,
  64. NAME = 1<<15,
  65. SCRIPT = 1<<16,
  66. CLICKACTION = 1<<17,
  67. PARTICLESYSTEM = 1<<18,
  68. GLOW = 1<<19,
  69. SALEPRICE = 1<<20,
  70. SITNAME = 1<<21,
  71. SITTARGETORIENTATION = 1<<22,
  72. SITTARGETPOSITION = 1<<23,
  73. TEXT = 1<<24,
  74. TOUCHNAME = 1<<25
  75. }
  76. #endregion Enumerations
  77. public static class Difference
  78. {
  79. #region Static Fields
  80. static float TimeToDiff = 0;
  81. // private static readonly ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  82. #endregion Static Fields
  83. #region Private Methods
  84. private static bool AreQuaternionsEquivalent(Quaternion first, Quaternion second)
  85. {
  86. Vector3 firstVector = llRot2Euler(first);
  87. Vector3 secondVector = llRot2Euler(second);
  88. return AreVectorsEquivalent(firstVector, secondVector);
  89. }
  90. private static bool AreVectorsEquivalent(Vector3 first, Vector3 second)
  91. {
  92. if (TruncateSignificant(first.X, 2) == TruncateSignificant(second.X, 2)
  93. && TruncateSignificant(first.Y, 2) == TruncateSignificant(second.Y, 2)
  94. && TruncateSignificant(first.Z, 2) == TruncateSignificant(second.Z, 2)
  95. )
  96. return true;
  97. else
  98. return false;
  99. }
  100. // Taken from Region/ScriptEngine/Common/LSL_BuiltIn_Commands.cs
  101. private static double NormalizeAngle(double angle)
  102. {
  103. angle = angle % (Math.PI * 2);
  104. if (angle < 0) angle = angle + Math.PI * 2;
  105. return angle;
  106. }
  107. private static int TruncateSignificant(float num, int digits)
  108. {
  109. return (int) Math.Ceiling((Math.Truncate(num * 10 * digits)/10*digits));
  110. // return (int) ((num * (10*digits))/10*digits);
  111. }
  112. // Taken from Region/ScriptEngine/Common/LSL_BuiltIn_Commands.cs
  113. // Also changed the original function from LSL_Types to LL types
  114. private static Vector3 llRot2Euler(Quaternion r)
  115. {
  116. Quaternion t = new Quaternion(r.X * r.X, r.Y * r.Y, r.Z * r.Z, r.W * r.W);
  117. double m = (t.X + t.Y + t.Z + t.W);
  118. if (m == 0) return new Vector3();
  119. double n = 2 * (r.Y * r.W + r.X * r.Z);
  120. double p = m * m - n * n;
  121. if (p > 0)
  122. return new Vector3((float)NormalizeAngle(Math.Atan2(2.0 * (r.X * r.W - r.Y * r.Z), (-t.X - t.Y + t.Z + t.W))),
  123. (float)NormalizeAngle(Math.Atan2(n, Math.Sqrt(p))),
  124. (float)NormalizeAngle(Math.Atan2(2.0 * (r.Z * r.W - r.X * r.Y), (t.X - t.Y - t.Z + t.W))));
  125. else if (n > 0)
  126. return new Vector3(0.0f, (float)(Math.PI / 2), (float)NormalizeAngle(Math.Atan2((r.Z * r.W + r.X * r.Y), 0.5 - t.X - t.Z)));
  127. else
  128. return new Vector3(0.0f, (float)(-Math.PI / 2), (float)NormalizeAngle(Math.Atan2((r.Z * r.W + r.X * r.Y), 0.5 - t.X - t.Z)));
  129. }
  130. #endregion Private Methods
  131. #region Public Methods
  132. /// <summary>
  133. /// Compares the attributes (Vectors, Quaternions, Strings, etc.) between two scene object parts
  134. /// and returns a Diff bitmask which details what the differences are.
  135. /// </summary>
  136. public static Diff FindDifferences(SceneObjectPart first, SceneObjectPart second)
  137. {
  138. Stopwatch x = new Stopwatch();
  139. x.Start();
  140. Diff result = 0;
  141. // VECTOR COMPARISONS
  142. if (!AreVectorsEquivalent(first.Acceleration, second.Acceleration))
  143. result |= Diff.ACCELERATION;
  144. if (!AreVectorsEquivalent(first.AbsolutePosition, second.AbsolutePosition))
  145. result |= Diff.POSITION;
  146. if (!AreVectorsEquivalent(first.AngularVelocity, second.AngularVelocity))
  147. result |= Diff.ANGULARVELOCITY;
  148. if (!AreVectorsEquivalent(first.OffsetPosition, second.OffsetPosition))
  149. result |= Diff.OFFSETPOSITION;
  150. if (!AreVectorsEquivalent(first.RotationalVelocity, second.RotationalVelocity))
  151. result |= Diff.ROTATIONALVELOCITY;
  152. if (!AreVectorsEquivalent(first.Scale, second.Scale))
  153. result |= Diff.SCALE;
  154. if (!AreVectorsEquivalent(first.Velocity, second.Velocity))
  155. result |= Diff.VELOCITY;
  156. // QUATERNION COMPARISONS
  157. if (!AreQuaternionsEquivalent(first.RotationOffset, second.RotationOffset))
  158. result |= Diff.ROTATIONOFFSET;
  159. // MISC COMPARISONS (UUID, Byte)
  160. if (first.ClickAction != second.ClickAction)
  161. result |= Diff.CLICKACTION;
  162. if (first.ObjectOwner != second.ObjectOwner)
  163. result |= Diff.OBJECTOWNER;
  164. // STRING COMPARISONS
  165. if (first.Description != second.Description)
  166. result |= Diff.DESCRIPTION;
  167. if (first.Material != second.Material)
  168. result |= Diff.MATERIAL;
  169. if (first.Name != second.Name)
  170. result |= Diff.NAME;
  171. if (first.SitName != second.SitName)
  172. result |= Diff.SITNAME;
  173. if (first.Text != second.Text)
  174. result |= Diff.TEXT;
  175. if (first.TouchName != second.TouchName)
  176. result |= Diff.TOUCHNAME;
  177. x.Stop();
  178. TimeToDiff += x.ElapsedMilliseconds;
  179. //m_log.Info("[DIFFERENCES] Time spent diffing objects so far" + TimeToDiff);
  180. return result;
  181. }
  182. #endregion Public Methods
  183. }
  184. }