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
import * as mars3d from "mars3d"
 
export let map // mars3d.Map三维地图对象
let measure
let measureVolume
 
export const mapOptions = {
  scene: {
    center: { lat: 30.883785, lng: 116.230883, alt: 8121, heading: 266, pitch: -62 },
    globe: { depthTestAgainstTerrain: true }
  }
}
 
export const eventTarget = new mars3d.BaseClass() // 事件对象,用于抛出事件到面板中
 
/**
 * 初始化地图业务,生命周期钩子函数(必须)
 * 框架在地图初始化完成后自动调用该函数
 * @param {mars3d.Map} mapInstance 地图对象
 * @returns {void} 无
 */
export function onMounted(mapInstance) {
  map = mapInstance // 记录map
  addMeasure()
}
 
/**
 * 释放当前地图业务的生命周期函数
 * @returns {void} 无
 */
export function onUnmounted() {
  map = null
}
 
function addMeasure() {
  measure = new mars3d.thing.Measure({
    label: {
      color: "#ffffff",
      font_family: "楷体",
      font_size: 20
    }
  })
  map.addThing(measure)
 
  // 直接传入坐标分析
  measure
    .volume({
      positions: mars3d.PointTrans.lonlats2cartesians([
        [116.191817, 30.864845, 309.3],
        [116.192869, 30.8757, 521.81],
        [116.190478, 30.886266, 672.79],
        [116.19247, 30.893748, 448.91],
        [116.200836, 30.889954, 379.92],
        [116.204063, 30.882578, 532.5],
        [116.203027, 30.873828, 498.8],
        [116.201795, 30.865941, 443.06]
      ]),
      splitNum: 6,
      height: 450
    })
    .then((e) => {
      measureVolume = e
      showHeightVal()
    })
 
  // 有模型时
  // tiles3dLayer.readyPromise.then((layer) => {
  //   // 关键代码,等模型readyPromise加载后执行volume
  //   measureVolume = measure.volume({
  //     positions: mars3d.PointTrans.lonlats2cartesians([
  //       [119.033856, 33.591473, 14.5],
  //       [119.033098, 33.591836, 13.2],
  //       [119.033936, 33.592146, 16.9]
  //     ]),
  //     has3dtiles: true, //关键代码,标识有模型
  //     splitNum: 6,
  //     height: 150
  //   })
  // })
 
  measure.on(mars3d.EventType.start, function (event) {
    console.log("开始分析", event)
    clearInterResult()
    showLoading()
    console.log("坐标为", JSON.stringify(mars3d.PointTrans.cartesians2lonlats(event.positions))) // 方便测试拷贝坐标
  })
 
  measure.on(mars3d.EventType.end, function (event) {
    console.log("分析完成", event)
    hideLoading()
  })
}
 
// 点选高度
function showHeightVal() {
  const baseHeight = measureVolume.height.toFixed(1)
  const minHeight = measureVolume.minHeight.toFixed(1)
  const maxHeight = getFixedNum(measureVolume.maxHeight)
 
  // 触发自定义事件 heightVal ,改变组件面板中的值
  eventTarget.fire("heightVal", { baseHeight, minHeight, maxHeight })
}
 
function getFixedNum(val) {
  return Math.ceil(val * 10) / 10
}
 
// 方量分析
export function analysisMeasure() {
  // 手动绘制的方式分析
  measure
    .volume({
      splitNum: 6 // 面内插值次数,控制精度[注意精度越大,分析时间越长]
      // minHeight: 50  //可以设置一个固定的最低高度
    })
    .then((e) => {
      measureVolume = e
      showHeightVal()
    })
}
 
// 清除
export function clear() {
  measure.clear()
  measureVolume = null
  clearInterResult()
}
 
export function showResult(reslut) {
  if (reslut && measureVolume && measureVolume.interPolygonObj) {
    showInterResult(measureVolume.interPolygonObj.list)
    return true
  } else {
    clearInterResult()
    return false
  }
}
 
// 修改基础高度
export function baseHeight(num) {
  measureVolume.height = num
  showHeightVal()
}
 
// 修改底高
export function txtMinHeight(num) {
  if (num > measureVolume.height) {
    globalMsg("墙底部高度不能高于基准面高")
    return
  }
  measureVolume.minHeight = num
}
 
// 修改顶高
export function txtMaxHeight(num) {
  const maxHeight = getFixedNum(measureVolume.polygonMaxHeight)
  if (num < maxHeight) {
    globalMsg("墙顶部高度不能低于区域内的地表高" + maxHeight)
    measureVolume.maxHeight = Number(maxHeight)
    return
  }
  if (num < measureVolume.height) {
    globalMsg("墙顶部高度不能低于基准面高")
    return
  }
  measureVolume.maxHeight = num
}
 
export function selHeight() {
  if (!measureVolume || !measure) {
    globalMsg("请先开始方量分析")
    return
  }
  measureVolume.selecteHeight().then((height) => {
    showHeightVal()
  })
}
 
// 显示mars3d.polygon.interPolygon处理后的面内插值分析结果,主要用于测试对比
 
// 显示面的插值计算结果,方便比较分析
let interGraphicLayer
 
function clearInterResult() {
  if (!interGraphicLayer) {
    interGraphicLayer = new mars3d.layer.GraphicLayer()
    map.addLayer(interGraphicLayer)
  }
 
  interGraphicLayer.clear()
}
 
function showInterResult(list) {
  // 分析结果用于测试分析的,不做太多处理,直接清除之前的,只保留一个
  clearInterResult()
 
  let pt1, pt2, pt3
  // var geometryInstances = [];
  for (let i = 0, len = list.length; i < len; i++) {
    const item = list[i]
 
    pt1 = item.point1.pointDM
    pt2 = item.point2.pointDM
    pt3 = item.point3.pointDM
 
    // 点
    for (const pt of [item.point1, item.point2, item.point3]) {
      const primitive = new mars3d.graphic.PointPrimitive({
        position: pt.pointDM,
        style: {
          pixelSize: 9,
          color: Cesium.Color.fromCssColorString("#ff0000").withAlpha(0.5)
        }
      })
      interGraphicLayer.addGraphic(primitive)
 
      primitive.bindTooltip("点高度:" + mars3d.MeasureUtil.formatDistance(pt.height))
    }
 
    // 横截面面积
    item.area = item.area || mars3d.MeasureUtil.getTriangleArea(pt1, pt2, pt3)
 
    // 三角网及边线
    const positions = [pt1, pt2, pt3, pt1]
 
    // 三角网面(单击用)
    const primitivePoly = new mars3d.graphic.PolygonPrimitive({
      positions: positions,
      style: {
        material: mars3d.MaterialUtil.createMaterial(mars3d.MaterialType.Color, {
          color: Cesium.Color.fromCssColorString("#ffffff").withAlpha(0.01)
        })
      }
    })
    interGraphicLayer.addGraphic(primitivePoly)
    primitivePoly.bindTooltip("三角面积:" + mars3d.MeasureUtil.formatArea(item.area) + "(第" + i + "个)")
 
    // 三角网边线
    const primitiveLine = new mars3d.graphic.PolylinePrimitive({
      positions: positions,
      style: {
        width: 1,
        material: mars3d.MaterialUtil.createMaterial(mars3d.MaterialType.Color, {
          color: Cesium.Color.fromCssColorString("#ffff00").withAlpha(0.3)
        })
      }
    })
    interGraphicLayer.addGraphic(primitiveLine)
  }
}