easy-three-utils 0.0.398 → 0.0.400

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.
@@ -42,7 +42,7 @@ const SunshineLine: React.FC<{
42
42
 
43
43
  const sliderBoxRef = useRef(null)
44
44
  useEffect(() => {
45
- sliderBoxRef.current.style.bottom = `calc(-100vh + ${sliderBoxRef.current.clientHeight + 20}px)`
45
+ sliderBoxRef.current!.style.bottom = `${sliderBoxRef.current!.clientHeight + 20}px`
46
46
  }, [])
47
47
 
48
48
  return (
@@ -29,7 +29,7 @@ const TerrainLine: React.FC<{
29
29
 
30
30
  const sliderBoxRef = useRef(null)
31
31
  useEffect(() => {
32
- sliderBoxRef.current.style.bottom = `calc(-100vh + ${sliderBoxRef.current.clientHeight + 20}px)`
32
+ sliderBoxRef.current!.style.bottom = `${sliderBoxRef.current!.clientHeight + 20}px`
33
33
  }, [])
34
34
 
35
35
  return (
@@ -1,303 +1,272 @@
1
1
  import * as Cesium from 'cesium'
2
- import glsl from "../glsl/viewable"
3
2
 
4
- const useViewshedAnalysis = (viewer: Cesium.Viewer, options: {
5
- collection: Cesium.CustomDataSource
3
+ const useProfileAnalysis = (viewer: Cesium.Viewer, options: {
4
+ collection: Cesium.CustomDataSource
6
5
  }) => {
7
6
 
8
- let i = 0
9
- let handler: Cesium.ScreenSpaceEventHandler | null = null
10
- let viewPosition = null
11
- let viewDistance = 100.0
12
- let viewHeading = 0.0
13
- let viewPitch = 0.0
14
- let horizontalViewAngle = 90.0
15
- let verticalViewAngle = 60.0
16
- let visibleAreaColor = Cesium.Color.GREEN
17
- let invisibleAreaColor = Cesium.Color.RED
18
- let enabled = true
19
- let softShadows = true
20
- let size = 2048
21
- let lightCamera = null
22
- let shadowMap = null
23
- let sketch = null
24
- let postStage = null
25
- let frustumOutline = null
7
+ let handler = null
8
+ let positions = []
9
+ let positionsCartographic = []
10
+ let positions_Inter = []
11
+ let poly = null
12
+ let distance = null
13
+ let cartesian = null
14
+ let DistanceArray = []
15
+ let profileItem: {
16
+ distance: number
17
+ point: ReturnType<typeof cartesian3ToDegrees>
18
+ }[] = []
26
19
 
27
- const add = () => {
28
- createLightCamera()
29
- createShadowMap()
30
- drawFrustumOutline()
31
- drawSketch()
32
- createPostStage()
33
- }
20
+ class PolyLinePrimitive {
21
+ options: Cesium.Entity.ConstructorOptions
22
+ positions: Cesium.Cartesian3[]
34
23
 
35
- const update = () => {
36
- removeAll()
37
- add()
38
- }
24
+ constructor(positions: Cesium.Cartesian3[]) {
25
+ this.options = {
26
+ polyline: {
27
+ show: true,
28
+ positions: [],
29
+ width: 3,
30
+ material: Cesium.Color.fromCssColorString("rgb(249, 157, 11)"),
31
+ clampToGround: true
32
+ }
33
+ }
34
+ this.positions = positions
35
+ this.init()
36
+ }
39
37
 
40
- const removeAll = () => {
41
- stopDraw()
38
+ init = () => {
39
+ const _self = this
40
+ const _update = function () {
41
+ return _self.positions
42
+ }
43
+ this.options.polyline.positions = new Cesium.CallbackProperty(_update, false)
44
+ options.collection.entities.add(this.options)
45
+ }
46
+ }
42
47
 
43
- if (sketch) {
44
- options.collection.entities.remove(sketch)
45
- sketch = null
48
+ const draw = (cb: (data: {
49
+ min: number;
50
+ value: (number | string)[][]
51
+ }) => void) => {
52
+ if (handler) {
53
+ console.log('请使用右键结束上次测量!')
54
+ return
55
+ }
56
+ options.collection.entities.removeAll()
57
+ handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas)
58
+ leftClickEvent()
59
+ mouseMoveEvent()
60
+ rightClickEvent(cb)
46
61
  }
47
- if (postStage) {
48
- viewer.scene.postProcessStages.remove(postStage)
49
- postStage = null
62
+
63
+ const stopDraw = () => {
64
+ if (handler) {
65
+ handler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK)
66
+ handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
67
+ handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
68
+ handler.destroy()
69
+ handler = null
70
+ }
71
+
72
+ positions = []
73
+ positionsCartographic = []
74
+ positions_Inter = []
75
+ poly = null
76
+ distance = null
77
+ cartesian = null
78
+ DistanceArray = []
79
+ profileItem = []
50
80
  }
51
- if (frustumOutline) {
52
- viewer.scene.primitives.remove(frustumOutline)
53
- frustumOutline = null
81
+
82
+ const removeAll = () => {
83
+ stopDraw()
84
+ options.collection.entities.removeAll()
54
85
  }
55
- }
56
86
 
57
- const updatePosition = (position) => {
58
- viewDistance = Cesium.Cartesian3.distance(viewPosition, position)
59
- viewHeading = getHeading(viewPosition, position)
60
- viewPitch = getPitch(viewPosition, position)
61
- }
87
+ const leftClickEvent = () => {
88
+ handler.setInputAction(((movement) => {
89
+ cartesian = viewer.scene.pickPosition(movement.position)
90
+ console.log(cartesian,'123')
91
+ if (positions.length == 0) {
92
+ positions.push(cartesian.clone())
93
+ }
94
+ positions.push(cartesian)
95
+ if (poly) {
96
+ interPoints(poly.positions)
97
+ distance = getSpaceDistance(positions_Inter)
98
+ } else {
99
+ distance = getSpaceDistance(positions)
100
+ }
62
101
 
63
- const createLightCamera = () => {
64
- const camera = new Cesium.Camera(viewer.scene)
65
- camera.position = viewPosition
66
- camera.frustum.near = viewDistance * 0.001
67
- camera.frustum.far = viewDistance
102
+ const textDisance = distance + "米"
103
+ DistanceArray.push(distance)
104
+ options.collection.entities.add({
105
+ position: positions[positions.length - 1],
106
+ point: {
107
+ color: Cesium.Color.fromCssColorString("rgb(249, 157, 11)"),
108
+ outlineColor: Cesium.Color.WHITE,
109
+ outlineWidth: 2,
110
+ pixelSize: 10
111
+ },
112
+ label: {
113
+ text: textDisance,
114
+ scale: 0.5,
115
+ font: "normal 28px MicroSoft YaHei",
116
+ style: Cesium.LabelStyle.FILL_AND_OUTLINE,
117
+ pixelOffset: new Cesium.Cartesian2(0, -30),
118
+ outlineWidth: 9,
119
+ outlineColor: Cesium.Color.WHITE,
120
+ heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND,
121
+ disableDepthTestDistance: Number.POSITIVE_INFINITY
122
+ }
123
+ })
124
+ }) as Cesium.ScreenSpaceEventHandler.PositionedEventCallback, Cesium.ScreenSpaceEventType.LEFT_CLICK)
125
+ }
68
126
 
69
- const hr = Cesium.Math.toRadians(horizontalViewAngle)
70
- const vr = Cesium.Math.toRadians(verticalViewAngle)
71
- const aspectRatio = (viewDistance * Math.tan(hr / 2) * 2) / (viewDistance * Math.tan(vr / 2) * 2);
72
- (camera.frustum as any).aspectRatio = aspectRatio;
73
- (camera.frustum as any).fov = Math.max(hr, vr)
127
+ const mouseMoveEvent = () => {
128
+ handler.setInputAction(((movement) => {
129
+ cartesian = viewer.scene.pickPosition(movement.endPosition)
130
+ if (positions.length >= 2) {
131
+ if (!Cesium.defined(poly)) {
132
+ poly = new PolyLinePrimitive(positions)
133
+ } else {
134
+ positions.pop()
135
+ positions.push(cartesian)
136
+ }
137
+ }
138
+ }) as Cesium.ScreenSpaceEventHandler.MotionEventCallback, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
139
+ }
74
140
 
75
- camera.setView({
76
- destination: viewPosition,
77
- orientation: {
78
- heading: Cesium.Math.toRadians(viewHeading || 0),
79
- pitch: Cesium.Math.toRadians(viewPitch || 0),
80
- roll: 0
81
- }
82
- })
83
- lightCamera = camera
84
- }
141
+ const rightClickEvent = (cb: (data: {
142
+ min: number;
143
+ value: (number | string)[][]
144
+ }) => void) => {
145
+ handler.setInputAction((() => {
146
+ positions.pop()
147
+ createProfileChart(profileItem, cb)
148
+ stopDraw()
149
+ }) as Cesium.ScreenSpaceEventHandler.PositionedEventCallback, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
150
+ }
85
151
 
86
- const createShadowMap = () => {
87
- const shadow = new (Cesium.ShadowMap as any)({
88
- context: (viewer.scene as any).context,
89
- lightCamera: lightCamera,
90
- enabled: enabled,
91
- isPointLight: true,
92
- pointLightRadius: viewDistance,
93
- cascadesEnabled: false,
94
- size: size,
95
- softShadows: softShadows,
96
- normalOffset: false,
97
- fromLightSource: false
98
- })
99
- viewer.scene.shadowMap = shadow
100
- shadowMap = shadow
101
- }
152
+ const cartesian3ToDegrees = (pos: Cesium.Cartesian3) => {
153
+ const ellipsoid = viewer.scene.globe.ellipsoid
154
+ const cartographic = ellipsoid.cartesianToCartographic(pos)
155
+ const lat = Cesium.Math.toDegrees(cartographic.latitude)
156
+ const lon = Cesium.Math.toDegrees(cartographic.longitude)
157
+ const height = cartographic.height
102
158
 
103
- const createPostStage = () => {
104
- const fs = glsl
105
- const cutomPostStage = new Cesium.PostProcessStage({
106
- fragmentShader: fs,
107
- uniforms: {
108
- shadowMap_textureCube: () => {
109
- shadowMap.update(Reflect.get(viewer.scene, '_frameState'))
110
- return Reflect.get(shadowMap, '_shadowMapTexture')
111
- },
112
- shadowMap_matrix: () => {
113
- shadowMap.update(Reflect.get(viewer.scene, '_frameState'))
114
- return Reflect.get(shadowMap, '_shadowMapMatrix')
115
- },
116
- shadowMap_lightPositionEC: () => {
117
- shadowMap.update(Reflect.get(viewer.scene, '_frameState'))
118
- return Reflect.get(shadowMap, '_lightPositionEC')
119
- },
120
- shadowMap_normalOffsetScaleDistanceMaxDistanceAndDarkness: () => {
121
- shadowMap.update(Reflect.get(viewer.scene, '_frameState'))
122
- const bias = shadowMap._pointBias
123
- return Cesium.Cartesian4.fromElements(
124
- bias.normalOffsetScale,
125
- shadowMap._distance,
126
- shadowMap.maximumDistance,
127
- 0.0,
128
- new Cesium.Cartesian4()
129
- )
130
- },
131
- shadowMap_texelSizeDepthBiasAndNormalShadingSmooth: () => {
132
- shadowMap.update(Reflect.get(viewer.scene, '_frameState'))
133
- const bias = shadowMap._pointBias
134
- const texelStepSize = new Cesium.Cartesian2(
135
- 1.0 / shadowMap._textureSize.x,
136
- 1.0 / shadowMap._textureSize.y
137
- )
138
- return Cesium.Cartesian4.fromElements(
139
- texelStepSize.x,
140
- texelStepSize.y,
141
- bias.depthBias,
142
- bias.normalShadingSmooth,
143
- new Cesium.Cartesian4()
144
- )
145
- },
146
- camera_projection_matrix: lightCamera.frustum.projectionMatrix,
147
- camera_view_matrix: lightCamera.viewMatrix,
148
- helsing_viewDistance: () => viewDistance,
149
- helsing_visibleAreaColor: visibleAreaColor,
150
- helsing_invisibleAreaColor: invisibleAreaColor
151
- }
152
- })
153
- postStage = viewer.scene.postProcessStages.add(cutomPostStage)
154
- }
159
+ return {
160
+ lat,
161
+ lon,
162
+ height
163
+ }
164
+ }
155
165
 
156
- const drawFrustumOutline = () => {
157
- const direction = lightCamera.directionWC
158
- const up = lightCamera.upWC
159
- const right = Cesium.Cartesian3.negate(lightCamera.rightWC, new Cesium.Cartesian3())
160
- const rotation = Cesium.Matrix3.setColumn(
161
- new Cesium.Matrix3(),
162
- 0,
163
- right,
164
- new Cesium.Matrix3()
165
- )
166
- Cesium.Matrix3.setColumn(rotation, 1, up, rotation)
167
- Cesium.Matrix3.setColumn(rotation, 2, direction, rotation)
168
- const orientation = Cesium.Quaternion.fromRotationMatrix(rotation, new Cesium.Quaternion())
166
+ const getSpaceDistance = (positions: Cesium.Cartesian3[]) => {
167
+ profileItem = [
168
+ {
169
+ point: cartesian3ToDegrees(positions[0]),
170
+ distance: 0
171
+ }
172
+ ]
173
+ let distance = 0
174
+ for (let i = 0; i < positions.length - 1; i++) {
175
+ const point1cartographic = Cesium.Cartographic.fromCartesian(positions[i])
176
+ const point2cartographic = Cesium.Cartographic.fromCartesian(positions[i + 1])
177
+ const geodesic = new Cesium.EllipsoidGeodesic()
178
+ geodesic.setEndPoints(point1cartographic, point2cartographic)
179
+ let s = geodesic.surfaceDistance
180
+ s = Math.sqrt(Math.pow(s, 2) + Math.pow(point2cartographic.height - point1cartographic.height, 2))
181
+ distance = distance + s
169
182
 
170
- const instance = new Cesium.GeometryInstance({
171
- geometry: new Cesium.FrustumOutlineGeometry({
172
- frustum: lightCamera.frustum,
173
- origin: viewPosition,
174
- orientation
175
- }),
176
- attributes: {
177
- color: Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.YELLOWGREEN),
178
- show: new Cesium.ShowGeometryInstanceAttribute(true)
179
- }
180
- })
183
+ const m_Item = {
184
+ point: cartesian3ToDegrees(positions[i + 1]),
185
+ distance: distance
186
+ }
187
+ profileItem.push(m_Item)
188
+ }
189
+ return distance.toFixed(2)
190
+ }
181
191
 
182
- frustumOutline = viewer.scene.primitives.add(
183
- new Cesium.Primitive({
184
- geometryInstances: [instance],
185
- appearance: new Cesium.PerInstanceColorAppearance({
186
- flat: true,
187
- translucent: false
188
- })
189
- })
190
- )
191
- }
192
+ const interPoints = (positions: Cesium.Cartesian3[]) => {
193
+ positionsCartographic = []
194
+ var terrainSamplePositions = []
195
+ for (let index = 0; index < positions.length - 1; index++) {
196
+ const element = positions[index]
197
+ var ellipsoid = viewer.scene.globe.ellipsoid
198
+ var cartographic = ellipsoid.cartesianToCartographic(element)
199
+ positionsCartographic.push(cartographic)
200
+ }
201
+ for (let i = 0; i < positionsCartographic.length; i++) {
202
+ const m_Cartographic0 = positionsCartographic[i]
203
+ const m_Cartographic1 = positionsCartographic[i + 1]
204
+ if (m_Cartographic1) {
205
+ var a = Math.abs(m_Cartographic0.longitude - m_Cartographic1.longitude) * 10000000
206
+ var b = Math.abs(m_Cartographic0.latitude - m_Cartographic1.latitude) * 10000000
207
+ if (a > b) b = a
208
+ let length = parseInt((b / 2) as any)
209
+ if (length > 1000) length = 1000
210
+ if (length < 2) length = 2
211
+ for (var j = 0; j < length; j++) {
212
+ terrainSamplePositions.push(
213
+ new Cesium.Cartographic(
214
+ Cesium.Math.lerp(m_Cartographic0.longitude, m_Cartographic1.longitude, j / (length - 1)),
215
+ Cesium.Math.lerp(m_Cartographic0.latitude, m_Cartographic1.latitude, j / (length - 1))
216
+ )
217
+ )
218
+ }
219
+ terrainSamplePositions.pop()
220
+ } else {
221
+ terrainSamplePositions.push(m_Cartographic0)
222
+ }
223
+ }
224
+ positions_Inter = []
225
+ for (var n = 0; n < terrainSamplePositions.length; n++) {
226
+ var m_cartographic = terrainSamplePositions[n]
227
+ var height = viewer.scene.globe.getHeight(m_cartographic)
228
+ var point = Cesium.Cartesian3.fromDegrees(m_cartographic.longitude / Math.PI * 180, m_cartographic.latitude / Math.PI * 180, height)
229
+ positions_Inter.push(point)
230
+ }
231
+ }
192
232
 
193
- const drawSketch = () => {
194
- sketch = options.collection.entities.add({
195
- name: 'sketch',
196
- position: viewPosition,
197
- orientation: new Cesium.CallbackProperty(() => {
198
- return Cesium.Transforms.headingPitchRollQuaternion(
199
- viewPosition,
200
- Cesium.HeadingPitchRoll.fromDegrees(
201
- viewHeading - horizontalViewAngle,
202
- viewPitch,
203
- 0.5
204
- )
205
- )
206
- }, false),
207
- ellipsoid: {
208
- radii: new Cesium.CallbackProperty(
209
- () => new Cesium.Cartesian3(viewDistance, viewDistance, viewDistance),
210
- false
211
- ),
212
- innerRadii: new Cesium.Cartesian3(2.0, 2.0, 2.0),
213
- minimumClock: Cesium.Math.toRadians(-horizontalViewAngle / 2),
214
- maximumClock: Cesium.Math.toRadians(horizontalViewAngle / 2),
215
- minimumCone: Cesium.Math.toRadians(verticalViewAngle + 7.75),
216
- maximumCone: Cesium.Math.toRadians(180 - verticalViewAngle - 7.75),
217
- fill: false,
218
- outline: true,
219
- subdivisions: 256,
220
- stackPartitions: 64,
221
- slicePartitions: 64,
222
- outlineColor: Cesium.Color.YELLOWGREEN
223
- }
224
- })
225
- }
233
+ const createProfileChart = (pos: typeof profileItem, cb: (data: {
234
+ min: number;
235
+ value: (number | string)[][]
236
+ }) => void) => {
237
+ const ProfileData = []
238
+ const ProfileData_Lon = []
226
239
 
227
- const getHeading = (from, to) => {
228
- const result = new Cesium.Cartesian3()
229
- const matrix4 = Cesium.Matrix4.inverse(Cesium.Transforms.eastNorthUpToFixedFrame(from), new Cesium.Matrix4())
230
- Cesium.Matrix4.multiplyByPoint(matrix4, to, result)
231
- Cesium.Cartesian3.normalize(result, result)
232
- return Cesium.Math.toDegrees(Math.atan2(result.x, result.y))
233
- }
240
+ let min = 0
234
241
 
235
- const getPitch = (from, to) => {
236
- const result = new Cesium.Cartesian3()
237
- const matrix4 = Cesium.Matrix4.inverse(Cesium.Transforms.eastNorthUpToFixedFrame(from), new Cesium.Matrix4())
238
- Cesium.Matrix4.multiplyByPoint(matrix4, to, result)
239
- Cesium.Cartesian3.normalize(result, result)
240
- return Cesium.Math.toDegrees(Math.asin(result.z))
241
- }
242
+ for (let index = 0; index < pos.length; index++) {
243
+ const el = pos[index]
244
+ const m_distance = el.distance.toFixed(2)
242
245
 
243
- const leftClickEvent = () => {
244
- handler.setInputAction(((movement) => {
245
- i++
246
- if (i === 1) {
247
- let startPosition = viewer.scene.pickPosition(movement.position)
248
- if (!startPosition) return
249
- viewPosition = startPosition
250
- mouseMoveEvent()
251
- }
252
- if (i === 2) {
253
- i = 0
254
- let endPosition = viewer.scene.pickPosition(movement.position)
255
- updatePosition(endPosition)
256
- update()
257
- stopDraw()
258
- }
259
- }) as Cesium.ScreenSpaceEventHandler.PositionedEventCallback, Cesium.ScreenSpaceEventType.LEFT_CLICK)
260
- }
246
+ const m_Lon = el.point.lon.toFixed(5)
247
+ const m_Lat = el.point.lat.toFixed(5)
248
+ const m_height = Number(el.point.height.toFixed(2))
261
249
 
262
- const mouseMoveEvent = () => {
263
- handler.setInputAction(((movement) => {
264
- const endPosition = viewer.scene.pickPosition(movement.endPosition)
265
- if (!endPosition) return
266
- updatePosition(endPosition)
267
- if (!sketch) {
268
- drawSketch()
269
- }
270
- }) as Cesium.ScreenSpaceEventHandler.MotionEventCallback, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
271
- }
250
+ if (m_height < min) {
251
+ min = m_height
252
+ }
253
+ var m_data = [m_distance, m_height]
254
+ ProfileData.push(m_data)
255
+ ProfileData_Lon.push([m_Lon, m_Lat])
256
+ }
272
257
 
273
- const draw = () => {
274
- if (handler) {
275
- console.log('请先结束上次测量!')
276
- return
258
+ cb({
259
+ min,
260
+ value: ProfileData
261
+ })
277
262
  }
278
- handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas)
279
- leftClickEvent()
280
- viewer.scene.camera.frustum.near = 100.0
281
- viewer.scene.logarithmicDepthBuffer = true
282
- viewer.scene.globe.enableLighting = false
283
- viewer.scene.globe.showGroundAtmosphere = false
284
- }
285
263
 
286
- const stopDraw = () => {
287
- if (handler) {
288
- handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
289
- handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
290
- handler.destroy()
291
- handler = null
264
+ return {
265
+ draw,
266
+ removeAll
292
267
  }
293
- }
294
-
295
- return {
296
- draw,
297
- removeAll
298
- }
299
268
  }
300
269
 
301
270
  export {
302
- useViewshedAnalysis
271
+ useProfileAnalysis
303
272
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "easy-three-utils",
3
- "version": "0.0.398",
3
+ "version": "0.0.400",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -8,5 +8,5 @@
8
8
  "author": "",
9
9
  "license": "ISC",
10
10
  "types": "./index.d.ts",
11
- "description": "修复剖面分析点击位置不正确的bug"
11
+ "description": ""
12
12
  }