Plane.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* The MIT License
  2. *
  3. * Copyright (c) 2010 Intel Corporation.
  4. * All rights reserved.
  5. *
  6. * Based on the convexdecomposition library from
  7. * <http://codesuppository.googlecode.com> by John W. Ratcliff and Stan Melax.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. using System;
  28. namespace OpenSim.Region.Physics.ConvexDecompositionDotNet
  29. {
  30. public class Plane
  31. {
  32. public float3 normal = new float3();
  33. public float dist; // distance below origin - the D from plane equasion Ax+By+Cz+D=0
  34. public Plane(float3 n, float d)
  35. {
  36. normal = new float3(n);
  37. dist = d;
  38. }
  39. public Plane(Plane p)
  40. {
  41. normal = new float3(p.normal);
  42. dist = p.dist;
  43. }
  44. public Plane()
  45. {
  46. dist = 0;
  47. }
  48. public void Transform(float3 position, Quaternion orientation)
  49. {
  50. // Transforms the plane to the space defined by the
  51. // given position/orientation
  52. float3 newNormal = Quaternion.Inverse(orientation) * normal;
  53. float3 origin = Quaternion.Inverse(orientation) * (-normal * dist - position);
  54. normal = newNormal;
  55. dist = -float3.dot(newNormal, origin);
  56. }
  57. public override int GetHashCode()
  58. {
  59. return normal.GetHashCode() ^ dist.GetHashCode();
  60. }
  61. public override bool Equals(object obj)
  62. {
  63. Plane p = obj as Plane;
  64. if (p == null)
  65. return false;
  66. return this == p;
  67. }
  68. public static bool operator ==(Plane a, Plane b)
  69. {
  70. return (a.normal == b.normal && a.dist == b.dist);
  71. }
  72. public static bool operator !=(Plane a, Plane b)
  73. {
  74. return !(a == b);
  75. }
  76. public static Plane PlaneFlip(Plane plane)
  77. {
  78. return new Plane(-plane.normal, -plane.dist);
  79. }
  80. public static bool coplanar(Plane a, Plane b)
  81. {
  82. return (a == b || a == PlaneFlip(b));
  83. }
  84. }
  85. }