fuchengshen
2022-07-09 3a25c05b5ab837714f30469cdcb1e2a77d8d7f6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import * as mars3d from "mars3d"
 
export let map // mars3d.Map三维地图对象
export let graphicLayer // 矢量图层对象
 
// 需要覆盖config.json中地图属性参数(当前示例框架中自动处理合并)
export const mapOptions = {
  scene: {
    center: {
      lat: 31.799613,
      lng: 117.27357,
      alt: 72.55,
      heading: 59.8,
      pitch: -18,
      roll: 0
    }
  },
  control: {
    clockAnimate: true, // 时钟动画控制(左下角)
    timeline: true, // 是否显示时间线控件
    compass: { bottom: "380px", left: "5px" }
  }
}
 
export const eventTarget = new mars3d.BaseClass() // 事件对象,用于抛出事件到面板中
 
// 时间控制参数
const args = {
  space: 100,
  time: 5,
  martTimeInter: null,
  cleanTimeInter: null
}
 
/**
 * 初始化地图业务,生命周期钩子函数(必须)
 * 框架在地图初始化完成后自动调用该函数
 * @param {mars3d.Map} mapInstance 地图对象
 * @returns {void} 无
 */
export function onMounted(mapInstance) {
  map = mapInstance // 记录map
  map.toolbar.style.bottom = "55px" // 修改toolbar控件的样式
 
  addLayer()
}
 
/**
 * 释放当前地图业务的生命周期函数
 * @returns {void} 无
 */
export function onUnmounted() {
  map = null
}
 
function addLayer() {
  // 创建Graphic图层
  graphicLayer = new mars3d.layer.GraphicLayer()
  map.addLayer(graphicLayer)
 
  // 合肥高铁
  const coors = [
    [117.277697, 31.800233, 45],
    [117.262022, 31.798983, 45],
    [117.229506, 31.793351, 45],
    [117.215719, 31.791085, 45],
    [117.207234, 31.79079, 45],
    [117.180246, 31.790688, 45],
    [117.168311, 31.789785, 45],
    [117.152322, 31.789855, 45],
    [117.125297, 31.788849, 45],
    [117.091144, 31.787516, 45]
  ]
 
  const positions = mars3d.PointTrans.lonlats2cartesians(coors)
 
  // 插值求新路线(按固定间隔米数插值) positions输入的值需为笛卡尔空间xyz坐标数组
  const positionsNew = mars3d.PolyUtil.interLine(positions, {
    minDistance: 20 // 间隔20米
  })
 
  // 求对比的贴地地面高度(用于echarts展示)
  mars3d.PolyUtil.computeSurfacePoints({
    scene: map.scene,
    positions: positionsNew // 需要计算的源路线坐标数组
  }).then((result) => {
    // raisedPositions为含高程信息的新坐标数组,noHeight为标识是否存在无地形数据。
    console.log("含高程信息的新坐标数组", result.positions)
 
    inintRoad(positionsNew, result.positions)
  })
}
 
// 构造动态高铁   positions:设计的路线    positionsTD地面的贴地路线(用于比较)
function inintRoad(positionsSJ, positionsTD) {
  const heightArray = []
  const heightTDArray = []
  const mpoints = []
  for (let i = 0; i < positionsSJ.length; i++) {
    const position = positionsSJ[i]
    const carto = Cesium.Cartographic.fromCartesian(position)
    const x = Cesium.Math.toDegrees(carto.longitude)
    const y = Cesium.Math.toDegrees(carto.latitude)
 
    const height = mars3d.Util.formatNum(carto.height) // 设计高度  当小数点后面的数字一致时,会省略小数点,不显示
    const tdHeight = mars3d.Util.formatNum(Cesium.Cartographic.fromCartesian(positionsTD[i]).height) // 地面高度
 
    heightArray.push(height)
    heightTDArray.push(tdHeight)
    mpoints.push([x, y, height, tdHeight])
  }
 
  //  距离数组
  const positionsLineFirst = positionsTD[0]
  const distanceArray = positionsTD.map(function (data) {
    return Math.round(Cesium.Cartesian3.distance(data, positionsLineFirst)) // 计算两点之间的距离,返回距离
  })
 
  // 显示echarts
  eventTarget.fire("dataLoaded", { heightArray, heightTDArray, distanceArray })
  //  画线
  const primitive = new mars3d.graphic.PolylinePrimitive({
    id: "设计路线",
    positions: positionsSJ,
    style: {
      width: 3,
      color: Cesium.Color.RED,
      materialType: mars3d.MaterialType.PolylineDash, // 虚线
      dashLength: 20
    }
  })
  graphicLayer.addGraphic(primitive)
 
  const primitiveTD = new mars3d.graphic.PolylinePrimitive({
    id: "贴地路线",
    positions: positionsTD,
    style: {
      width: 3,
      color: Cesium.Color.YELLOW
    }
  })
  graphicLayer.addGraphic(primitiveTD)
 
  // =================计算路线====================
  const start = map.clock.currentTime.clone()
 
  const counts = mpoints.length
 
  const arrProperty = []
 
  // 16组车身+头尾2个车头 共18组
  for (let j = 0; j < 18; j++) {
    const stime = Cesium.JulianDate.addSeconds(start, j, new Cesium.JulianDate()) // 每隔j秒,添加一次时间
 
    const property = new Cesium.SampledPositionProperty()
 
    for (let i = 0; i < counts; i++) {
      const time = Cesium.JulianDate.addSeconds(stime, i + 1, new Cesium.JulianDate())
      const point = Cesium.Cartesian3.fromDegrees(mpoints[i][0], mpoints[i][1], mpoints[i][2] + 0.5)
      property.addSample(time, point) // 添加新样本,时间、位置
    }
 
    property.setInterpolationOptions({
      interpolationDegree: 1,
      interpolationAlgorithm: Cesium.LinearApproximation
    })
 
    arrProperty.push(property)
  }
 
  // =================时间相关====================
 
  const stop = Cesium.JulianDate.addSeconds(start, counts + 60, new Cesium.JulianDate())
  map.clock.startTime = start.clone()
  map.clock.stopTime = stop.clone()
  map.clock.currentTime = start.clone()
  map.clock.multiplier = 1 // 当前速度,默认为1
  map.clock.shouldAnimate = true // 是否开启时钟动画,默认true
  //  map.clock.clockRange = Cesium.ClockRange.LOOP_STOP; // 到达终止时间后循环
 
  if (map.controls.timeline) {
    map.controls.timeline.zoomTo(start, stop)
  }
 
  const availability = new Cesium.TimeIntervalCollection([
    new Cesium.TimeInterval({
      start: start,
      stop: Cesium.JulianDate.addSeconds(start, counts, new Cesium.JulianDate())
    })
  ])
 
  // =================添加高铁车头================
  const graphicHead = addTrainHead(arrProperty[0], availability)
  map.trackedEntity = graphicHead.entity // 设置相机的视角跟随的Entity实例
 
  // =================添加车身====================
  const len = arrProperty.length
  for (let j = 1; j < len - 1; j++) {
    addTrainBody(arrProperty[j], availability)
  }
 
  // =================添加高铁车尾================
  addTrainHead(arrProperty[len - 1], availability, true) // 车尾部的反向车头
 
  // ==============添加铁路,定时更新================
  addRailway(graphicHead, mpoints)
 
  // ==============更新echarts================
  let lastDistance
 
  function locTrain() {
    const t = parseInt(map.clock.currentTime.secondsOfDay - map.clock.startTime.secondsOfDay) // 时间差
 
    if (t >= heightArray.length) {
      // 高铁运行结束之后
      clearInterval(args.martTimeInter)
      clearInterval(args.cleanTimeInter)
      return
    }
    if (lastDistance === t) {
      return
    }
    lastDistance = t
    updateEchartsDistance(t, heightArray[t])
  }
  args.martTimeInter = setInterval(locTrain, 100)
}
 
// 添加车头
function addTrainHead(position, availability, rotatePI) {
  const graphicModel = new mars3d.graphic.ModelEntity({
    name: "和谐号车头",
    position: position,
    orientation: new Cesium.VelocityOrientationProperty(position),
    availability: availability,
    style: {
      url: "//data.mars3d.cn/gltf/mars/train/heada.glb",
      scale: 0.001,
      minimumPixelSize: 16,
      heading: rotatePI ? 90 : -90
    }
  })
  graphicLayer.addGraphic(graphicModel)
  return graphicModel
}
 
// 添加车身
function addTrainBody(position, availability) {
  const graphicModel = new mars3d.graphic.ModelEntity({
    name: "和谐号车身",
    position: position,
    orientation: new Cesium.VelocityOrientationProperty(position),
    availability: availability,
    style: {
      url: "//data.mars3d.cn/gltf/mars/train/body.glb",
      scale: 0.001,
      minimumPixelSize: 16,
      heading: -90
    }
  })
  graphicLayer.addGraphic(graphicModel)
  return graphicModel
}
 
// 添加铁路,定时更新
function addRailway(graphicHead, mpoints) {
  const positions = []
  const orientations = []
 
  const times = graphicHead.position._property._times
  const start = times[0].clone()
  const counts = times.length
 
  for (let k = 1; k < counts; k++) {
    const time = times[k]
 
    const position = graphicHead.position.getValue(time)
    positions.push(position)
 
    const orientation = graphicHead.orientation.getValue(time)
    orientations.push(orientation)
  }
 
  let i = 0
  const roadNum = 80
 
  function addroad() {
    const space = Math.round(map.clock.currentTime.secondsOfDay - map.clock.startTime.secondsOfDay)
    let spa = space + args.space
    if (spa > counts) {
      spa = counts
    }
    for (; i < spa; i++) {
      const availability = new Cesium.TimeIntervalCollection([
        new Cesium.TimeInterval({
          start: Cesium.JulianDate.addSeconds(start, -roadNum + i, new Cesium.JulianDate()),
          stop: Cesium.JulianDate.addSeconds(start, roadNum + i, new Cesium.JulianDate())
        })
      ])
 
      //  当高度在地下时,添加地下隧道
      if (mpoints[i][2] - mpoints[i][3] < -20 || (i > 2 && mpoints[i - 3][2] - mpoints[i - 3][3] < -20)) {
        //  mpoints[i][2] -- 设计高度;mpoints[i][3] -- 贴地高度
        const id = "s" + i
        const graphic = graphicLayer.getGraphicById(id)
        if (!graphic) {
          const graphicModel = new mars3d.graphic.ModelEntity({
            id: id,
            position: positions[i],
            orientation: orientations[i],
            availability: availability,
            style: {
              url: "//data.mars3d.cn/gltf/mars/railway/suidao.glb",
              scale: 0.001
            }
          })
          graphicLayer.addGraphic(graphicModel)
        } else {
          graphic.entity.availability._intervals[0].stop.secondsOfDay = availability._intervals[0].stop.secondsOfDay
        }
      }
 
      //  添加轨道地面
      const id = "xl" + i
      const graphic = graphicLayer.getGraphicById(id)
      if (!graphic) {
        const graphicModel = new mars3d.graphic.ModelEntity({
          id: id,
          position: positions[i],
          orientation: orientations[i],
          availability: availability,
          style: {
            url: "//data.mars3d.cn/gltf/mars/railway/railway.glb",
            scale: 0.001
          }
        })
        graphicLayer.addGraphic(graphicModel)
      } else {
        graphic.entity.availability._intervals[0].stop.secondsOfDay = availability._intervals[0].stop.secondsOfDay
      }
 
      // 添加轨道支架
      if (mpoints[i][2] - mpoints[i][3] > 20 && i % 5 === 0) {
        const id = "xq" + i
        const graphic = graphicLayer.getGraphicById(id)
        if (!graphic) {
          const graphicModel = new mars3d.graphic.ModelEntity({
            id: id,
            position: positions[i],
            orientation: orientations[i],
            availability: availability,
            style: {
              url: "//data.mars3d.cn/gltf/mars/railway/bridge.glb",
              scale: 0.001
            }
          })
          graphicLayer.addGraphic(graphicModel)
        } else {
          graphic.entity.availability._intervals[0].stop.secondsOfDay = availability._intervals[0].stop.secondsOfDay
        }
      }
 
      // 添加轨道边的柱子
      if (i % 12 === 0) {
        const id = "xd" + i
        const graphic = graphicLayer.getGraphicById(id)
        if (!graphic) {
          const graphicModel = new mars3d.graphic.ModelEntity({
            id: id,
            position: positions[i],
            orientation: orientations[i],
            availability: availability,
            style: {
              url: "//data.mars3d.cn/gltf/mars/railway/jiazi.glb",
              scale: 0.001
            }
          })
          graphicLayer.addGraphic(graphicModel)
        } else {
          graphic.entity.availability._intervals[0].stop.secondsOfDay = availability._intervals[0].stop.secondsOfDay
        }
      }
    }
 
    // 移除铁路
    for (let j = args.statate; j < args.statate - args.space; j++) {
      removeGraphic("s" + j)
      removeGraphic("xl" + j)
      removeGraphic("xq" + j)
      removeGraphic("xd" + j)
      args.statate = j
    }
  }
 
  addroad()
 
  args.cleanTimeInter = setInterval(addroad, args.time)
  args.statate = 0
}
 
function removeGraphic(id) {
  const graphic = graphicLayer.getGraphicById(id)
  if (graphic) {
    if (graphic.entity.availability._intervals[0].stop.secondsOfDay < map.clock.currentTime.secondsOfDay) {
      graphic.remove(true)
    }
  }
}
 
function updateEchartsDistance(loc, height) {
  eventTarget.fire("dataUpdated", { loc, height })
}