ClippingPlane.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import Cartesian3 from "../Core/Cartesian3.js";
  2. import Check from "../Core/Check.js";
  3. import defined from "../Core/defined.js";
  4. /**
  5. * A Plane in Hessian Normal form to be used with {@link ClippingPlaneCollection}.
  6. * Compatible with mathematics functions in {@link Plane}
  7. *
  8. * @alias ClippingPlane
  9. * @constructor
  10. *
  11. * @param {Cartesian3} normal The plane's normal (normalized).
  12. * @param {number} distance The shortest distance from the origin to the plane. The sign of
  13. * <code>distance</code> determines which side of the plane the origin
  14. * is on. If <code>distance</code> is positive, the origin is in the half-space
  15. * in the direction of the normal; if negative, the origin is in the half-space
  16. * opposite to the normal; if zero, the plane passes through the origin.
  17. */
  18. function ClippingPlane(normal, distance) {
  19. //>>includeStart('debug', pragmas.debug);
  20. Check.typeOf.object("normal", normal);
  21. Check.typeOf.number("distance", distance);
  22. //>>includeEnd('debug');
  23. this._distance = distance;
  24. this._normal = new UpdateChangedCartesian3(normal, this);
  25. this.onChangeCallback = undefined;
  26. this.index = -1; // to be set by ClippingPlaneCollection
  27. }
  28. Object.defineProperties(ClippingPlane.prototype, {
  29. /**
  30. * The shortest distance from the origin to the plane. The sign of
  31. * <code>distance</code> determines which side of the plane the origin
  32. * is on. If <code>distance</code> is positive, the origin is in the half-space
  33. * in the direction of the normal; if negative, the origin is in the half-space
  34. * opposite to the normal; if zero, the plane passes through the origin.
  35. *
  36. * @type {number}
  37. * @memberof ClippingPlane.prototype
  38. */
  39. distance: {
  40. get: function () {
  41. return this._distance;
  42. },
  43. set: function (value) {
  44. //>>includeStart('debug', pragmas.debug);
  45. Check.typeOf.number("value", value);
  46. //>>includeEnd('debug');
  47. if (defined(this.onChangeCallback) && value !== this._distance) {
  48. this.onChangeCallback(this.index);
  49. }
  50. this._distance = value;
  51. },
  52. },
  53. /**
  54. * The plane's normal.
  55. *
  56. * @type {Cartesian3}
  57. * @memberof ClippingPlane.prototype
  58. */
  59. normal: {
  60. get: function () {
  61. return this._normal;
  62. },
  63. set: function (value) {
  64. //>>includeStart('debug', pragmas.debug);
  65. Check.typeOf.object("value", value);
  66. //>>includeEnd('debug');
  67. if (
  68. defined(this.onChangeCallback) &&
  69. !Cartesian3.equals(this._normal._cartesian3, value)
  70. ) {
  71. this.onChangeCallback(this.index);
  72. }
  73. // Set without firing callback again
  74. Cartesian3.clone(value, this._normal._cartesian3);
  75. },
  76. },
  77. });
  78. /**
  79. * Create a ClippingPlane from a Plane object.
  80. *
  81. * @param {Plane} plane The plane containing parameters to copy
  82. * @param {ClippingPlane} [result] The object on which to store the result
  83. * @returns {ClippingPlane} The ClippingPlane generated from the plane's parameters.
  84. */
  85. ClippingPlane.fromPlane = function (plane, result) {
  86. //>>includeStart('debug', pragmas.debug);
  87. Check.typeOf.object("plane", plane);
  88. //>>includeEnd('debug');
  89. if (!defined(result)) {
  90. result = new ClippingPlane(plane.normal, plane.distance);
  91. } else {
  92. result.normal = plane.normal;
  93. result.distance = plane.distance;
  94. }
  95. return result;
  96. };
  97. /**
  98. * Clones the ClippingPlane without setting its ownership.
  99. * @param {ClippingPlane} clippingPlane The ClippingPlane to be cloned
  100. * @param {ClippingPlane} [result] The object on which to store the cloned parameters.
  101. * @returns {ClippingPlane} a clone of the input ClippingPlane
  102. */
  103. ClippingPlane.clone = function (clippingPlane, result) {
  104. if (!defined(result)) {
  105. return new ClippingPlane(clippingPlane.normal, clippingPlane.distance);
  106. }
  107. result.normal = clippingPlane.normal;
  108. result.distance = clippingPlane.distance;
  109. return result;
  110. };
  111. /**
  112. * Wrapper on Cartesian3 that allows detection of Plane changes from "members of members," for example:
  113. *
  114. * const clippingPlane = new ClippingPlane(...);
  115. * clippingPlane.normal.z = -1.0;
  116. *
  117. * @private
  118. */
  119. function UpdateChangedCartesian3(normal, clippingPlane) {
  120. this._clippingPlane = clippingPlane;
  121. this._cartesian3 = Cartesian3.clone(normal);
  122. }
  123. Object.defineProperties(UpdateChangedCartesian3.prototype, {
  124. x: {
  125. get: function () {
  126. return this._cartesian3.x;
  127. },
  128. set: function (value) {
  129. //>>includeStart('debug', pragmas.debug);
  130. Check.typeOf.number("value", value);
  131. //>>includeEnd('debug');
  132. if (
  133. defined(this._clippingPlane.onChangeCallback) &&
  134. value !== this._cartesian3.x
  135. ) {
  136. this._clippingPlane.onChangeCallback(this._clippingPlane.index);
  137. }
  138. this._cartesian3.x = value;
  139. },
  140. },
  141. y: {
  142. get: function () {
  143. return this._cartesian3.y;
  144. },
  145. set: function (value) {
  146. //>>includeStart('debug', pragmas.debug);
  147. Check.typeOf.number("value", value);
  148. //>>includeEnd('debug');
  149. if (
  150. defined(this._clippingPlane.onChangeCallback) &&
  151. value !== this._cartesian3.y
  152. ) {
  153. this._clippingPlane.onChangeCallback(this._clippingPlane.index);
  154. }
  155. this._cartesian3.y = value;
  156. },
  157. },
  158. z: {
  159. get: function () {
  160. return this._cartesian3.z;
  161. },
  162. set: function (value) {
  163. //>>includeStart('debug', pragmas.debug);
  164. Check.typeOf.number("value", value);
  165. //>>includeEnd('debug');
  166. if (
  167. defined(this._clippingPlane.onChangeCallback) &&
  168. value !== this._cartesian3.z
  169. ) {
  170. this._clippingPlane.onChangeCallback(this._clippingPlane.index);
  171. }
  172. this._cartesian3.z = value;
  173. },
  174. },
  175. });
  176. export default ClippingPlane;