ClippingPlane.js 6.6 KB

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