easy-three-utils 0.0.331 → 0.0.333

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,194 @@
1
+ import * as Cesium from 'cesium'
2
+
3
+ const useMeasureTools = (viewer: Cesium.Viewer) => {
4
+ let handler = new Cesium.ScreenSpaceEventHandler((viewer.scene as any)._imageryLayerCollection);
5
+ const positions = []
6
+ let positionsCartographic = []
7
+ let positions_Inter = []
8
+ let poly = null
9
+ let distance = null
10
+ let cartesian = null
11
+ const DistanceArray = []
12
+ let profileItem = []
13
+
14
+ const measureDistance = () => {
15
+ handler.setInputAction(((movement) => {
16
+ cartesian = viewer.scene.pickPosition(movement.endPosition);
17
+ if (positions.length >= 2) {
18
+ if (!Cesium.defined(poly)) {
19
+ poly = new PolyLinePrimitive(positions);
20
+ } else {
21
+ positions.pop();
22
+ positions.push(cartesian);
23
+ }
24
+ }
25
+ }) as Cesium.ScreenSpaceEventHandler.MotionEventCallback, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
26
+
27
+ handler.setInputAction(((movement) => {
28
+ cartesian = viewer.scene.pickPosition(movement.position);
29
+ if (positions.length == 0) {
30
+ positions.push(cartesian.clone());
31
+ }
32
+ positions.push(cartesian);
33
+ if (poly) {
34
+ interPoints(poly.positions);
35
+ distance = getSpaceDistance(positions_Inter);
36
+ } else {
37
+ distance = getSpaceDistance(positions);
38
+ }
39
+
40
+ const textDisance = distance + "米";
41
+ DistanceArray.push(distance);
42
+ viewer.entities.add({
43
+ position: positions[positions.length - 1],
44
+ point: {
45
+ color: Cesium.Color.fromCssColorString("rgb(249, 157, 11)"),
46
+ outlineColor: Cesium.Color.WHITE,
47
+ outlineWidth: 2,
48
+ pixelSize: 10
49
+ },
50
+ label: {
51
+ text: textDisance,
52
+ font: '18px sans-serif',
53
+ fillColor: Cesium.Color.GOLD,
54
+ style: Cesium.LabelStyle.FILL_AND_OUTLINE,
55
+ outlineWidth: 2,
56
+ verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
57
+ pixelOffset: new Cesium.Cartesian2(20, -20),
58
+ heightReference: Cesium.HeightReference.NONE
59
+ }
60
+ });
61
+ }) as Cesium.ScreenSpaceEventHandler.PositionedEventCallback, Cesium.ScreenSpaceEventType.LEFT_CLICK);
62
+
63
+ handler.setInputAction((() => {
64
+ destoryHandler()
65
+ }) as Cesium.ScreenSpaceEventHandler.PositionedEventCallback, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
66
+
67
+ class PolyLinePrimitive {
68
+ options: any;
69
+ positions: any;
70
+
71
+ constructor(positions) {
72
+ this.options = {
73
+ polyline: {
74
+ show: true,
75
+ positions: [],
76
+ material: Cesium.Color.fromCssColorString("rgb(249, 157, 11)"),
77
+ width: 2,
78
+ clampToGround: true
79
+ }
80
+ };
81
+ this.positions = positions;
82
+ this.init()
83
+ }
84
+
85
+ init = () => {
86
+ const _self = this;
87
+ const _update = function () {
88
+ return _self.positions;
89
+ };
90
+ this.options.polyline.positions = new Cesium.CallbackProperty(_update, false);
91
+ viewer.entities.add(this.options);
92
+ }
93
+ }
94
+
95
+ const cartesian3ToDegrees = (pos) => {
96
+ const ellipsoid = viewer.scene.globe.ellipsoid;
97
+ const cartographic = ellipsoid.cartesianToCartographic(pos);
98
+ const lat = Cesium.Math.toDegrees(cartographic.latitude);
99
+ const lon = Cesium.Math.toDegrees(cartographic.longitude);
100
+ const height = cartographic.height;
101
+
102
+ return {
103
+ lat,
104
+ lon,
105
+ height
106
+ }
107
+ }
108
+
109
+ const getSpaceDistance = (positions) => {
110
+ profileItem = [
111
+ {
112
+ point: cartesian3ToDegrees(positions[0]),
113
+ distance: 0
114
+ }
115
+ ];
116
+ let distance = 0;
117
+ for (let i = 0; i < positions.length - 1; i++) {
118
+ const point1cartographic = Cesium.Cartographic.fromCartesian(positions[i]);
119
+ const point2cartographic = Cesium.Cartographic.fromCartesian(positions[i + 1]);
120
+ const geodesic = new Cesium.EllipsoidGeodesic();
121
+ geodesic.setEndPoints(point1cartographic, point2cartographic);
122
+ let s = geodesic.surfaceDistance;
123
+ s = Math.sqrt(Math.pow(s, 2) + Math.pow(point2cartographic.height - point1cartographic.height, 2));
124
+ distance = distance + s;
125
+
126
+ const m_Item = {
127
+ point: cartesian3ToDegrees(positions[i + 1]),
128
+ distance: distance
129
+ };
130
+ profileItem.push(m_Item);
131
+ }
132
+ return distance.toFixed(2);
133
+ }
134
+
135
+ const interPoints = (positions) => {
136
+ positionsCartographic = [];
137
+ var terrainSamplePositions = [];
138
+ for (let index = 0; index < positions.length - 1; index++) {
139
+ const element = positions[index];
140
+ var ellipsoid = viewer.scene.globe.ellipsoid;
141
+ var cartographic = ellipsoid.cartesianToCartographic(element);
142
+ positionsCartographic.push(cartographic);
143
+ }
144
+ for (let i = 0; i < positionsCartographic.length; i++) {
145
+ const m_Cartographic0 = positionsCartographic[i];
146
+ const m_Cartographic1 = positionsCartographic[i + 1];
147
+ if (m_Cartographic1) {
148
+ var a = Math.abs(m_Cartographic0.longitude - m_Cartographic1.longitude) * 10000000;
149
+ var b = Math.abs(m_Cartographic0.latitude - m_Cartographic1.latitude) * 10000000;
150
+ if (a > b) b = a;
151
+ let length = parseInt((b / 2) as any);
152
+ if (length > 1000) length = 1000;
153
+ if (length < 2) length = 2;
154
+ for (var j = 0; j < length; j++) {
155
+ terrainSamplePositions.push(
156
+ new Cesium.Cartographic(
157
+ Cesium.Math.lerp(m_Cartographic0.longitude, m_Cartographic1.longitude, j / (length - 1)),
158
+ Cesium.Math.lerp(m_Cartographic0.latitude, m_Cartographic1.latitude, j / (length - 1))
159
+ )
160
+ );
161
+ }
162
+ terrainSamplePositions.pop();
163
+ } else {
164
+ terrainSamplePositions.push(m_Cartographic0);
165
+ }
166
+ }
167
+ positions_Inter = [];
168
+ for (var n = 0; n < terrainSamplePositions.length; n++) {
169
+ var m_cartographic = terrainSamplePositions[n];
170
+ var height = viewer.scene.globe.getHeight(m_cartographic);
171
+ var point = Cesium.Cartesian3.fromDegrees(m_cartographic.longitude / Math.PI * 180, m_cartographic.latitude / Math.PI * 180, height);
172
+ positions_Inter.push(point);
173
+ }
174
+ }
175
+
176
+ const destoryHandler = () => {
177
+ if (handler) {
178
+ handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
179
+ handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
180
+ handler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK)
181
+ handler.destroy()
182
+ handler = null
183
+ }
184
+ }
185
+ }
186
+
187
+ return {
188
+ measureDistance
189
+ }
190
+ }
191
+
192
+ export {
193
+ useMeasureTools
194
+ }
@@ -0,0 +1,379 @@
1
+ import CreatePolygonOnGround from "./lib/CreatePolygonOnGround";
2
+ import CreateRemindertip from "./lib/ReminderTip";
3
+ import * as turf from "@turf/turf";
4
+ import * as Cesium from 'cesium'
5
+
6
+ class SlopeAspect {
7
+ constructor(viewer, cb, cb1) {
8
+ if (!viewer) throw new Error("no viewer object!");
9
+ this.viewer = viewer;
10
+ this.result = []; //存储创建的坡度分析结果,primitive集合
11
+ this.handler = undefined;
12
+ this.toolTip = "";
13
+ this.cb = cb
14
+ this.cb1 = cb1
15
+ }
16
+ openTip() {
17
+ const $this = this;
18
+ const viewer = this.viewer;
19
+ this.handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
20
+ this.handler.setInputAction(function (movement) {
21
+ let endPos = movement.endPosition;
22
+ var pick = viewer.scene.pick(endPos);
23
+ if (pick && pick.id && pick.id.type === "SlopeAspect") {
24
+ $this.toolTip = pick.id.value.toFixed(2);
25
+ // CreateRemindertip($this.toolTip, endPos, true);
26
+ } else {
27
+ $this.toolTip = "";
28
+ // CreateRemindertip($this.toolTip, endPos, false);
29
+ }
30
+ }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
31
+ }
32
+ closeTip() {
33
+ if (this.handler) {
34
+ this.handler.destroy();
35
+ this.handler = undefined;
36
+ }
37
+ }
38
+ //等距离切分网格
39
+ createNew4Distance(distance) {
40
+ distance = distance || 0.1; //默认0.1km精度
41
+ let width = distance * 200 > 35 ? 35 : distance * 200;
42
+ this.arrowWidth = width < 15 ? 15 : width;
43
+ const $this = this;
44
+ const viewer = this.viewer;
45
+ CreatePolygonOnGround(
46
+ viewer,
47
+ [],
48
+ {
49
+ color: Cesium.Color.RED.withAlpha(0.1),
50
+ outlineColor: Cesium.Color.YELLOW,
51
+ outlineWidth: 2,
52
+ },
53
+ function (polygon) {
54
+ let degrees = $this.Cartesian3ListToWGS84(polygon.pottingPoint);
55
+ viewer.entities.remove(polygon);
56
+ let boundary = [];
57
+ let minX = 10000,
58
+ minY = 10000,
59
+ maxX = -10000,
60
+ maxY = -1000;
61
+ for (let index = 0; index < degrees.length; index++) {
62
+ const element = degrees[index];
63
+ const x = element.lng;
64
+ const y = element.lat;
65
+ boundary.push([x, y]);
66
+ minX = x < minX ? x : minX;
67
+ minY = y < minY ? y : minY;
68
+ maxX = x > maxX ? x : maxX;
69
+ maxY = y > maxY ? y : maxY;
70
+ }
71
+ boundary.push(boundary[0]);
72
+ let bbox = [minX, minY, maxX, maxY];
73
+ let mask = turf.polygon([boundary]);
74
+ let gridSquare = turf.squareGrid(bbox, distance, { mask: mask });
75
+ $this.createEllipse(gridSquare);
76
+ $this.cb && $this.cb()
77
+ },
78
+ function (handler) {
79
+ $this.cb1(handler)
80
+ }
81
+ );
82
+ }
83
+ // 等分切分网格,切分成一个num*num的网格
84
+ createNew4Num(num) {
85
+ const $this = this;
86
+ const viewer = this.viewer;
87
+ CreatePolygonOnGround(
88
+ viewer,
89
+ [],
90
+ {
91
+ color: Cesium.Color.RED.withAlpha(0.1),
92
+ outlineColor: Cesium.Color.YELLOW,
93
+ outlineWidth: 2,
94
+ },
95
+ function (polygon) {
96
+ let degrees = $this.Cartesian3ListToWGS84(polygon.pottingPoint);
97
+ viewer.entities.remove(polygon);
98
+ let boundary = [];
99
+ let minX = 10000,
100
+ minY = 10000,
101
+ maxX = -10000,
102
+ maxY = -1000;
103
+ for (let index = 0; index < degrees.length; index++) {
104
+ const element = degrees[index];
105
+ const x = element.lng;
106
+ const y = element.lat;
107
+ boundary.push([x, y]);
108
+ minX = x < minX ? x : minX;
109
+ minY = y < minY ? y : minY;
110
+ maxX = x > maxX ? x : maxX;
111
+ maxY = y > maxY ? y : maxY;
112
+ }
113
+ boundary.push(boundary[0]);
114
+ let bbox = [minX, minY, maxX, maxY];
115
+ let a = maxX - minX;
116
+ let b = maxY - minY;
117
+ b = b > a ? b : a;
118
+ const step = b / num;
119
+ let width = step * 2000 > 35 ? 35 : step * 2000;
120
+ $this.arrowWidth = width < 15 ? 15 : width;
121
+ let mask = turf.polygon([boundary]);
122
+ let gridSquare = turf.squareGrid(bbox, step, {
123
+ units: "degrees",
124
+ mask: mask,
125
+ });
126
+ $this.createEllipse(gridSquare);
127
+ $this.cb && $this.cb()
128
+ },
129
+ function (handler) {
130
+ $this.cb1(handler)
131
+ }
132
+ );
133
+ }
134
+ createEllipse(gridSquare) {
135
+ let boxResults = [];
136
+ for (let index = 0; index < gridSquare.features.length; index++) {
137
+ const feature = gridSquare.features[index];
138
+ const coordinates = feature.geometry.coordinates[0];
139
+ const centerdegree = [
140
+ (coordinates[0][0] + coordinates[2][0]) / 2,
141
+ (coordinates[0][1] + coordinates[2][1]) / 2,
142
+ ];
143
+ let centerCartographic = Cesium.Cartographic.fromDegrees(
144
+ centerdegree[0],
145
+ centerdegree[1]
146
+ );
147
+ boxResults.push(centerCartographic);
148
+ for (let i = 0; i < coordinates.length; i++) {
149
+ const coord = coordinates[i];
150
+ let cartographic = Cesium.Cartographic.fromDegrees(coord[0], coord[1]);
151
+ boxResults.push(cartographic);
152
+ const coord1 = coordinates[i + 1];
153
+ if (coord1) {
154
+ let newCoord = [
155
+ (coord[0] + coord1[0]) / 2,
156
+ (coord[1] + coord1[1]) / 2,
157
+ ];
158
+ let newCartographic = Cesium.Cartographic.fromDegrees(
159
+ newCoord[0],
160
+ newCoord[1]
161
+ );
162
+ boxResults.push(newCartographic);
163
+ }
164
+ }
165
+ }
166
+ Cesium.sampleTerrainMostDetailed(
167
+ this.viewer.scene.terrainProvider,
168
+ boxResults
169
+ ).then((updatePositions) => {
170
+ let arrr = [];
171
+ let ellipseResults = updatePositions.reduce(function (
172
+ pre,
173
+ item,
174
+ index,
175
+ updatePositions
176
+ ) {
177
+ var begin = index * 10;
178
+ var end = begin + 10;
179
+ var res = updatePositions.slice(begin, end);
180
+ if (res.length != 0) {
181
+ arrr[index] = res;
182
+ }
183
+ return arrr;
184
+ },
185
+ []);
186
+ this.calculateSlope(ellipseResults);
187
+ });
188
+ }
189
+
190
+ createPolygonInsrance(points, color) {
191
+ let positions = [];
192
+ for (let index = 1; index < points.length - 1; index++) {
193
+ const element = points[index];
194
+ positions.push(Cesium.Cartographic.toCartesian(element));
195
+ }
196
+ let polygon = new Cesium.PolygonGeometry({
197
+ polygonHierarchy: new Cesium.PolygonHierarchy(positions),
198
+ });
199
+
200
+ let polygonInstance = new Cesium.GeometryInstance({
201
+ geometry: polygon,
202
+ attributes: {
203
+ color: Cesium.ColorGeometryInstanceAttribute.fromColor(
204
+ Cesium.Color.fromCssColorString(color)
205
+ ),
206
+ show: new Cesium.ShowGeometryInstanceAttribute(true), //显示或者隐藏
207
+ },
208
+ });
209
+ return polygonInstance;
210
+ }
211
+ createArrowInstance(
212
+ targetPoint,
213
+ center,
214
+ diagonalPoint,
215
+ heightDifference,
216
+ curSlope
217
+ ) {
218
+ let cartographic_0 = new Cesium.Cartographic(
219
+ (targetPoint.longitude + center.longitude) / 2,
220
+ (targetPoint.latitude + center.latitude) / 2,
221
+ (targetPoint.height + center.height) / 2
222
+ );
223
+ let cartographic_1 = new Cesium.Cartographic(
224
+ (diagonalPoint.longitude + center.longitude) / 2,
225
+ (diagonalPoint.latitude + center.latitude) / 2,
226
+ (diagonalPoint.height + center.height) / 2
227
+ );
228
+ //偏移的
229
+ let positions1 =
230
+ heightDifference > 0
231
+ ? [
232
+ Cesium.Cartographic.toCartesian(cartographic_0),
233
+ Cesium.Cartographic.toCartesian(cartographic_1),
234
+ ]
235
+ : [
236
+ Cesium.Cartographic.toCartesian(cartographic_1),
237
+ Cesium.Cartographic.toCartesian(cartographic_0),
238
+ ];
239
+ //箭头线
240
+ const instance = new Cesium.GeometryInstance({
241
+ id: {
242
+ type: "SlopeAspect",
243
+ value: curSlope,
244
+ },
245
+ geometry: new Cesium.GroundPolylineGeometry({
246
+ positions: positions1,
247
+ width: this.arrowWidth,
248
+ }),
249
+ attributes: {
250
+ color: Cesium.ColorGeometryInstanceAttribute.fromColor(
251
+ Cesium.Color.BLUE.withAlpha(0.6)
252
+ ),
253
+ show: new Cesium.ShowGeometryInstanceAttribute(true), //显示或者隐藏
254
+ },
255
+ });
256
+ return instance;
257
+ }
258
+ calculateSlope(ellipseResults) {
259
+ let instances = [];
260
+ let polygonInstance = [];
261
+ for (let index = 0; index < ellipseResults.length; index++) {
262
+ const ellipse = ellipseResults[index];
263
+
264
+ const center = ellipse[0];
265
+ let heightDifference = 0;
266
+ let maxIndex = 0;
267
+ for (let i = 1; i < ellipse.length - 1; i++) {
268
+ const point = ellipse[i];
269
+ let curHD = point.height - center.height;
270
+ if (Math.abs(curHD) > heightDifference) {
271
+ heightDifference = curHD;
272
+ maxIndex = i;
273
+ }
274
+ }
275
+ let pos0 = new Cesium.Cartographic(center.longitude, center.latitude, 0);
276
+ let pos1 = new Cesium.Cartographic(
277
+ ellipse[maxIndex].longitude,
278
+ ellipse[maxIndex].latitude,
279
+ 0
280
+ );
281
+ let distance = Cesium.Cartesian3.distance(
282
+ Cesium.Cartographic.toCartesian(pos0),
283
+ Cesium.Cartographic.toCartesian(pos1)
284
+ );
285
+ let curSlope = Math.abs(heightDifference / distance); //坡度的tan值
286
+ let curColor = this.calculateSlopeColor(curSlope, 0.4);
287
+ const curPolygonInstance = this.createPolygonInsrance(ellipse, curColor);
288
+ polygonInstance.push(curPolygonInstance);
289
+
290
+ let diagonalPoint =
291
+ maxIndex > 4 ? ellipse[maxIndex - 4] : ellipse[maxIndex + 4]; //对角点
292
+ let targetPoint = ellipse[maxIndex];
293
+ const arrowInstance = this.createArrowInstance(
294
+ targetPoint,
295
+ center,
296
+ diagonalPoint,
297
+ heightDifference,
298
+ curSlope
299
+ );
300
+ instances.push(arrowInstance);
301
+ }
302
+ const mapPrimitive = this.viewer.scene.primitives.add(
303
+ new Cesium.GroundPrimitive({
304
+ geometryInstances: polygonInstance,
305
+ appearance: new Cesium.PerInstanceColorAppearance({
306
+ translucent: true, //false时透明度无效
307
+ closed: false,
308
+ }),
309
+ })
310
+ );
311
+ const arrowPrimitive = this.viewer.scene.primitives.add(
312
+ new Cesium.GroundPolylinePrimitive({
313
+ geometryInstances: instances,
314
+ appearance: new Cesium.PolylineMaterialAppearance({
315
+ material: new Cesium.Material({
316
+ fabric: {
317
+ type: "PolylineArrow",
318
+ uniforms: {
319
+ color: new Cesium.Color(1.0, 1.0, 0.0, 0.8),
320
+ },
321
+ },
322
+ }),
323
+ }),
324
+ })
325
+ );
326
+ this.result.push(arrowPrimitive, mapPrimitive);
327
+ }
328
+ clearAll() {
329
+ this.result.forEach((element) => {
330
+ this.viewer.scene.primitives.remove(element);
331
+ });
332
+ this.result = [];
333
+ }
334
+ //根据坡度值赋值颜色
335
+ calculateSlopeColor(value, alpha) {
336
+ // 0°~0.5°为平原0.00872686779075879,rgb(85,182,43)
337
+ // 0.5°~2°为微斜坡0.03492076949174773,rgb(135,211,43)
338
+ // 2°~5°为缓斜坡0.08748866352592401,rgb(204,244,44)
339
+ // 5°~15°为斜坡0.2679491924311227,rgb(245,233,44)
340
+ // 15°~35°为陡坡0.7002075382097097,rgb(255,138,43)
341
+ // 35°~55°为峭坡1.4281480067421144,rgb(255,84,43)
342
+ // 55°~90°为垂直壁,rgb(255,32,43)
343
+ if (value < 0.00872686779075879) {
344
+ return "rgba(85,182,43," + alpha + ")";
345
+ } else if (value < 0.03492076949174773) {
346
+ return "rgba(135,211,43," + alpha + ")";
347
+ } else if (value < 0.08748866352592401) {
348
+ return "rgba(204,244,44," + alpha + ")";
349
+ } else if (value < 0.2679491924311227) {
350
+ return "rgba(245,233,44," + alpha + ")";
351
+ } else if (value < 0.7002075382097097) {
352
+ return "rgba(255,138,43," + alpha + ")";
353
+ } else if (value < 1.4281480067421144) {
354
+ return "rgba(255,84,43," + alpha + ")";
355
+ } else {
356
+ return "rgba(255,32,43," + alpha + ")";
357
+ }
358
+ }
359
+ /**
360
+ * 笛卡尔坐标数组转WGS84
361
+ * @param {Array} cartesianList 笛卡尔坐标数组
362
+ * @returns {Array} WGS84经纬度坐标数组
363
+ */
364
+ Cartesian3ListToWGS84(cartesianList) {
365
+ let ellipsoid = Cesium.Ellipsoid.WGS84;
366
+ let result = [];
367
+ for (let index = 0; index < cartesianList.length; index++) {
368
+ const cartesian = cartesianList[index];
369
+ let cartographic = ellipsoid.cartesianToCartographic(cartesian);
370
+ result.push({
371
+ lng: Cesium.Math.toDegrees(cartographic.longitude),
372
+ lat: Cesium.Math.toDegrees(cartographic.latitude),
373
+ alt: cartographic.height,
374
+ });
375
+ }
376
+ return result;
377
+ }
378
+ }
379
+ export default SlopeAspect;
@@ -0,0 +1,20 @@
1
+ import { useCustomCollection, ECollectionWhiteListNames } from './useCustomCollection'
2
+ import { useDrawPolygon } from './useDrawPolygon'
3
+ import { useClipping } from './useClipping'
4
+ import { useWeather, EWeatherType } from './useWeather'
5
+ import { useMeasure } from './useMeasure'
6
+
7
+ const dict = {
8
+ ECollectionWhiteListNames,
9
+ EWeatherType
10
+ }
11
+
12
+ export {
13
+ useCustomCollection,
14
+ useDrawPolygon,
15
+ useClipping,
16
+ useWeather,
17
+ useMeasure,
18
+
19
+ dict
20
+ }
@@ -0,0 +1,90 @@
1
+ import * as Cesium from 'cesium'
2
+
3
+ const useClipping = (viewer: Cesium.Viewer) => {
4
+
5
+ const setClippingPlane = (points: Cesium.Cartesian3[], type: boolean) => {
6
+
7
+ const clippingPlanes = []
8
+
9
+ const pointsCoor = points.map(({ x, y, z }) => new Cesium.Cartesian3(x, y, z))
10
+ let sum = 0
11
+ for (let i = 0; i < pointsCoor.length; i++) {
12
+ const pointA = pointsCoor[i]
13
+ const pointB = pointsCoor[(i + 1) % pointsCoor.length]
14
+ const crossProduct = Cesium.Cartesian3.cross(pointA, pointB, new Cesium.Cartesian3())
15
+ sum += crossProduct.z
16
+ }
17
+
18
+ if (sum > 0 && type) {
19
+ points.reverse()
20
+ } else if (sum < 0 && !type) {
21
+ points.reverse()
22
+ }
23
+
24
+ for (let i = 0; i < points.length; ++i) {
25
+ const nextIndex = (i + 1) % points.length
26
+
27
+ const midpoint = Cesium.Cartesian3.add(points[i], points[nextIndex], new Cesium.Cartesian3())
28
+ Cesium.Cartesian3.multiplyByScalar(midpoint, 0.5, midpoint)
29
+
30
+ const up = Cesium.Cartesian3.normalize(midpoint, new Cesium.Cartesian3())
31
+
32
+ const right = Cesium.Cartesian3.subtract(points[nextIndex], midpoint, new Cesium.Cartesian3())
33
+ Cesium.Cartesian3.normalize(right, right)
34
+
35
+ const normal = Cesium.Cartesian3.cross(right, up, new Cesium.Cartesian3())
36
+ Cesium.Cartesian3.normalize(normal, normal)
37
+
38
+ const originCenteredPlane = new Cesium.Plane(normal, 0.0)
39
+ const distance = Cesium.Plane.getPointDistance(originCenteredPlane, midpoint)
40
+
41
+ clippingPlanes.push(new Cesium.ClippingPlane(normal, distance))
42
+ }
43
+
44
+ return clippingPlanes
45
+ }
46
+
47
+ const dig = (points: Cesium.Cartesian3[], type?: boolean) => {
48
+
49
+ viewer.scene.globe.clippingPolygons = null
50
+
51
+ const planes = setClippingPlane(points, type)
52
+
53
+ viewer.scene.globe.clippingPlanes = new Cesium.ClippingPlaneCollection({
54
+ planes,
55
+ enabled: true,
56
+ modelMatrix: Cesium.Matrix4.IDENTITY,
57
+ unionClippingRegions: type === undefined ? false : true,
58
+ edgeColor: Cesium.Color.YELLOW,
59
+ edgeWidth: 1.0
60
+ })
61
+ }
62
+
63
+ const digMultiple = (pointsArr: Cesium.Cartesian3[][]) => {
64
+
65
+ viewer.scene.globe.clippingPlanes = null
66
+
67
+ const polygons = pointsArr.map(positions => new Cesium.ClippingPolygon({ positions }))
68
+
69
+ const clippingCollection = new Cesium.ClippingPolygonCollection({
70
+ polygons
71
+ });
72
+
73
+ viewer.scene.globe.clippingPolygons = clippingCollection;
74
+ }
75
+
76
+ const clear = () => {
77
+ viewer.scene.globe.clippingPlanes = null
78
+ viewer.scene.globe.clippingPolygons = null
79
+ }
80
+
81
+ return {
82
+ dig,
83
+ digMultiple,
84
+ clear
85
+ }
86
+ }
87
+
88
+ export {
89
+ useClipping
90
+ }