智慧水务管理系统 - 精河县供水工程综合管理平台

Polyline.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. import arrayRemoveDuplicates from "../Core/arrayRemoveDuplicates.js";
  2. import BoundingSphere from "../Core/BoundingSphere.js";
  3. import Cartesian3 from "../Core/Cartesian3.js";
  4. import Color from "../Core/Color.js";
  5. import Frozen from "../Core/Frozen.js";
  6. import defined from "../Core/defined.js";
  7. import DeveloperError from "../Core/DeveloperError.js";
  8. import DistanceDisplayCondition from "../Core/DistanceDisplayCondition.js";
  9. import Matrix4 from "../Core/Matrix4.js";
  10. import PolylinePipeline from "../Core/PolylinePipeline.js";
  11. import Material from "./Material.js";
  12. /**
  13. * <div class="notice">
  14. * Create this by calling {@link PolylineCollection#add}. Do not call the constructor directly.
  15. * </div>
  16. *
  17. * A renderable polyline.
  18. *
  19. * @alias Polyline
  20. * @internalConstructor
  21. * @class
  22. *
  23. * @privateParam {object} options Object with the following properties:
  24. * @privateParam {boolean} [options.show=true] <code>true</code> if this polyline will be shown; otherwise, <code>false</code>.
  25. * @privateParam {number} [options.width=1.0] The width of the polyline in pixels.
  26. * @privateParam {boolean} [options.loop=false] Whether a line segment will be added between the last and first line positions to make this line a loop.
  27. * @privateParam {Material} [options.material=Material.ColorType] The material.
  28. * @privateParam {Cartesian3[]} [options.positions] The positions.
  29. * @privateParam {object} [options.id] The user-defined object to be returned when this polyline is picked.
  30. * @privateParam {DistanceDisplayCondition} [options.distanceDisplayCondition] The condition specifying at what distance from the camera that this polyline will be displayed.
  31. * @privateParam {PolylineCollection} polylineCollection The renderable polyline collection.
  32. *
  33. * @see PolylineCollection
  34. *
  35. */
  36. function Polyline(options, polylineCollection) {
  37. options = options ?? Frozen.EMPTY_OBJECT;
  38. this._show = options.show ?? true;
  39. this._width = options.width ?? 1.0;
  40. this._loop = options.loop ?? false;
  41. this._distanceDisplayCondition = options.distanceDisplayCondition;
  42. this._material = options.material;
  43. if (!defined(this._material)) {
  44. this._material = Material.fromType(Material.ColorType, {
  45. color: new Color(1.0, 1.0, 1.0, 1.0),
  46. });
  47. }
  48. let positions = options.positions;
  49. if (!defined(positions)) {
  50. positions = [];
  51. }
  52. this._positions = positions;
  53. this._actualPositions = arrayRemoveDuplicates(
  54. positions,
  55. Cartesian3.equalsEpsilon,
  56. );
  57. if (this._loop && this._actualPositions.length > 2) {
  58. if (this._actualPositions === this._positions) {
  59. this._actualPositions = positions.slice();
  60. }
  61. this._actualPositions.push(Cartesian3.clone(this._actualPositions[0]));
  62. }
  63. this._length = this._actualPositions.length;
  64. this._id = options.id;
  65. let modelMatrix;
  66. if (defined(polylineCollection)) {
  67. modelMatrix = Matrix4.clone(polylineCollection.modelMatrix);
  68. }
  69. this._modelMatrix = modelMatrix;
  70. this._segments = PolylinePipeline.wrapLongitude(
  71. this._actualPositions,
  72. modelMatrix,
  73. );
  74. this._actualLength = undefined;
  75. this._propertiesChanged = new Uint32Array(NUMBER_OF_PROPERTIES);
  76. this._polylineCollection = polylineCollection;
  77. this._dirty = false;
  78. this._pickId = undefined;
  79. this._boundingVolume = BoundingSphere.fromPoints(this._actualPositions);
  80. this._boundingVolumeWC = BoundingSphere.transform(
  81. this._boundingVolume,
  82. this._modelMatrix,
  83. );
  84. this._boundingVolume2D = new BoundingSphere(); // modified in PolylineCollection
  85. }
  86. const POSITION_INDEX = (Polyline.POSITION_INDEX = 0);
  87. const SHOW_INDEX = (Polyline.SHOW_INDEX = 1);
  88. const WIDTH_INDEX = (Polyline.WIDTH_INDEX = 2);
  89. const MATERIAL_INDEX = (Polyline.MATERIAL_INDEX = 3);
  90. const POSITION_SIZE_INDEX = (Polyline.POSITION_SIZE_INDEX = 4);
  91. const DISTANCE_DISPLAY_CONDITION = (Polyline.DISTANCE_DISPLAY_CONDITION = 5);
  92. const NUMBER_OF_PROPERTIES = (Polyline.NUMBER_OF_PROPERTIES = 6);
  93. function makeDirty(polyline, propertyChanged) {
  94. ++polyline._propertiesChanged[propertyChanged];
  95. const polylineCollection = polyline._polylineCollection;
  96. if (defined(polylineCollection)) {
  97. polylineCollection._updatePolyline(polyline, propertyChanged);
  98. polyline._dirty = true;
  99. }
  100. }
  101. Object.defineProperties(Polyline.prototype, {
  102. /**
  103. * Determines if this polyline will be shown. Use this to hide or show a polyline, instead
  104. * of removing it and re-adding it to the collection.
  105. * @memberof Polyline.prototype
  106. * @type {boolean}
  107. */
  108. show: {
  109. get: function () {
  110. return this._show;
  111. },
  112. set: function (value) {
  113. //>>includeStart('debug', pragmas.debug);
  114. if (!defined(value)) {
  115. throw new DeveloperError("value is required.");
  116. }
  117. //>>includeEnd('debug');
  118. if (value !== this._show) {
  119. this._show = value;
  120. makeDirty(this, SHOW_INDEX);
  121. }
  122. },
  123. },
  124. /**
  125. * Gets or sets the positions of the polyline.
  126. * @memberof Polyline.prototype
  127. * @type {Cartesian3[]}
  128. * @example
  129. * polyline.positions = Cesium.Cartesian3.fromDegreesArray([
  130. * 0.0, 0.0,
  131. * 10.0, 0.0,
  132. * 0.0, 20.0
  133. * ]);
  134. */
  135. positions: {
  136. get: function () {
  137. return this._positions;
  138. },
  139. set: function (value) {
  140. //>>includeStart('debug', pragmas.debug);
  141. if (!defined(value)) {
  142. throw new DeveloperError("value is required.");
  143. }
  144. //>>includeEnd('debug');
  145. let positions = arrayRemoveDuplicates(value, Cartesian3.equalsEpsilon);
  146. if (this._loop && positions.length > 2) {
  147. if (positions === value) {
  148. positions = value.slice();
  149. }
  150. positions.push(Cartesian3.clone(positions[0]));
  151. }
  152. if (
  153. this._actualPositions.length !== positions.length ||
  154. this._actualPositions.length !== this._length
  155. ) {
  156. makeDirty(this, POSITION_SIZE_INDEX);
  157. }
  158. this._positions = value;
  159. this._actualPositions = positions;
  160. this._length = positions.length;
  161. this._boundingVolume = BoundingSphere.fromPoints(
  162. this._actualPositions,
  163. this._boundingVolume,
  164. );
  165. this._boundingVolumeWC = BoundingSphere.transform(
  166. this._boundingVolume,
  167. this._modelMatrix,
  168. this._boundingVolumeWC,
  169. );
  170. makeDirty(this, POSITION_INDEX);
  171. this.update();
  172. },
  173. },
  174. /**
  175. * Gets or sets the surface appearance of the polyline. This can be one of several built-in {@link Material} objects or a custom material, scripted with
  176. * {@link https://github.com/CesiumGS/cesium/wiki/Fabric|Fabric}.
  177. * @memberof Polyline.prototype
  178. * @type {Material}
  179. */
  180. material: {
  181. get: function () {
  182. return this._material;
  183. },
  184. set: function (material) {
  185. //>>includeStart('debug', pragmas.debug);
  186. if (!defined(material)) {
  187. throw new DeveloperError("material is required.");
  188. }
  189. //>>includeEnd('debug');
  190. if (this._material !== material) {
  191. this._material = material;
  192. makeDirty(this, MATERIAL_INDEX);
  193. }
  194. },
  195. },
  196. /**
  197. * Gets or sets the width of the polyline.
  198. * @memberof Polyline.prototype
  199. * @type {number}
  200. */
  201. width: {
  202. get: function () {
  203. return this._width;
  204. },
  205. set: function (value) {
  206. //>>includeStart('debug', pragmas.debug)
  207. if (!defined(value)) {
  208. throw new DeveloperError("value is required.");
  209. }
  210. //>>includeEnd('debug');
  211. const width = this._width;
  212. if (value !== width) {
  213. this._width = value;
  214. makeDirty(this, WIDTH_INDEX);
  215. }
  216. },
  217. },
  218. /**
  219. * Gets or sets whether a line segment will be added between the first and last polyline positions.
  220. * @memberof Polyline.prototype
  221. * @type {boolean}
  222. */
  223. loop: {
  224. get: function () {
  225. return this._loop;
  226. },
  227. set: function (value) {
  228. //>>includeStart('debug', pragmas.debug)
  229. if (!defined(value)) {
  230. throw new DeveloperError("value is required.");
  231. }
  232. //>>includeEnd('debug');
  233. if (value !== this._loop) {
  234. let positions = this._actualPositions;
  235. if (value) {
  236. if (
  237. positions.length > 2 &&
  238. !Cartesian3.equals(positions[0], positions[positions.length - 1])
  239. ) {
  240. if (positions.length === this._positions.length) {
  241. this._actualPositions = positions = this._positions.slice();
  242. }
  243. positions.push(Cartesian3.clone(positions[0]));
  244. }
  245. } else if (
  246. positions.length > 2 &&
  247. Cartesian3.equals(positions[0], positions[positions.length - 1])
  248. ) {
  249. if (positions.length - 1 === this._positions.length) {
  250. this._actualPositions = this._positions;
  251. } else {
  252. positions.pop();
  253. }
  254. }
  255. this._loop = value;
  256. makeDirty(this, POSITION_SIZE_INDEX);
  257. }
  258. },
  259. },
  260. /**
  261. * Gets or sets the user-defined value returned when the polyline is picked.
  262. * @memberof Polyline.prototype
  263. * @type {*}
  264. */
  265. id: {
  266. get: function () {
  267. return this._id;
  268. },
  269. set: function (value) {
  270. this._id = value;
  271. if (defined(this._pickId)) {
  272. this._pickId.object.id = value;
  273. }
  274. },
  275. },
  276. /**
  277. * @private
  278. */
  279. pickId: {
  280. get: function () {
  281. return this._pickId;
  282. },
  283. },
  284. /**
  285. * Gets the destruction status of this polyline
  286. * @memberof Polyline.prototype
  287. * @type {boolean}
  288. * @default false
  289. * @private
  290. */
  291. isDestroyed: {
  292. get: function () {
  293. return !defined(this._polylineCollection);
  294. },
  295. },
  296. /**
  297. * Gets or sets the condition specifying at what distance from the camera that this polyline will be displayed.
  298. * @memberof Polyline.prototype
  299. * @type {DistanceDisplayCondition}
  300. * @default undefined
  301. */
  302. distanceDisplayCondition: {
  303. get: function () {
  304. return this._distanceDisplayCondition;
  305. },
  306. set: function (value) {
  307. //>>includeStart('debug', pragmas.debug);
  308. if (defined(value) && value.far <= value.near) {
  309. throw new DeveloperError(
  310. "far distance must be greater than near distance.",
  311. );
  312. }
  313. //>>includeEnd('debug');
  314. if (
  315. !DistanceDisplayCondition.equals(value, this._distanceDisplayCondition)
  316. ) {
  317. this._distanceDisplayCondition = DistanceDisplayCondition.clone(
  318. value,
  319. this._distanceDisplayCondition,
  320. );
  321. makeDirty(this, DISTANCE_DISPLAY_CONDITION);
  322. }
  323. },
  324. },
  325. });
  326. /**
  327. * @private
  328. */
  329. Polyline.prototype.update = function () {
  330. let modelMatrix = Matrix4.IDENTITY;
  331. if (defined(this._polylineCollection)) {
  332. modelMatrix = this._polylineCollection.modelMatrix;
  333. }
  334. const segmentPositionsLength = this._segments.positions.length;
  335. const segmentLengths = this._segments.lengths;
  336. const positionsChanged =
  337. this._propertiesChanged[POSITION_INDEX] > 0 ||
  338. this._propertiesChanged[POSITION_SIZE_INDEX] > 0;
  339. if (!Matrix4.equals(modelMatrix, this._modelMatrix) || positionsChanged) {
  340. this._segments = PolylinePipeline.wrapLongitude(
  341. this._actualPositions,
  342. modelMatrix,
  343. );
  344. this._boundingVolumeWC = BoundingSphere.transform(
  345. this._boundingVolume,
  346. modelMatrix,
  347. this._boundingVolumeWC,
  348. );
  349. }
  350. this._modelMatrix = Matrix4.clone(modelMatrix, this._modelMatrix);
  351. if (this._segments.positions.length !== segmentPositionsLength) {
  352. // number of positions changed
  353. makeDirty(this, POSITION_SIZE_INDEX);
  354. } else {
  355. const length = segmentLengths.length;
  356. for (let i = 0; i < length; ++i) {
  357. if (segmentLengths[i] !== this._segments.lengths[i]) {
  358. // indices changed
  359. makeDirty(this, POSITION_SIZE_INDEX);
  360. break;
  361. }
  362. }
  363. }
  364. };
  365. /**
  366. * @private
  367. */
  368. Polyline.prototype.getPickId = function (context) {
  369. if (!defined(this._pickId)) {
  370. this._pickId = context.createPickId({
  371. primitive: this,
  372. collection: this._polylineCollection,
  373. id: this._id,
  374. });
  375. }
  376. return this._pickId;
  377. };
  378. Polyline.prototype._clean = function () {
  379. this._dirty = false;
  380. const properties = this._propertiesChanged;
  381. for (let k = 0; k < NUMBER_OF_PROPERTIES - 1; ++k) {
  382. properties[k] = 0;
  383. }
  384. };
  385. Polyline.prototype._destroy = function () {
  386. this._pickId = this._pickId && this._pickId.destroy();
  387. this._material = this._material && this._material.destroy();
  388. this._polylineCollection = undefined;
  389. };
  390. export default Polyline;