jxdnsong
2023-04-05 be147d2a99d60a30b20fade742e85b45f06172ae
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
/**
 * @Author: Caven
 * @Date: 2020-08-30 16:22:50
 */
 
import Parse from '@/utils/parse/Parse'
import PlotUtil from '@/utils/PlotUtil'
 
const HALF_PI = Math.PI / 2
 
class AttackArrowGraphics {
    constructor(options) {
        this._positions = options?.positions || []
        this.headHeightFactor = 0.18
        this.headWidthFactor = 0.3
        this.neckHeightFactor = 0.85
        this.neckWidthFactor = 0.15
        this.headTailFactor = 0.8
    }
 
    set positions (positions) {
        this._positions = positions
    }
 
    get positions () {
        return this._positions
    }
 
    get hierarchy () {
        return this._createHierarchy()
    }
 
    _getArrowHeadPoints (points, tailLeft, tailRight) {
        let len = PlotUtil.getBaseLength(points)
        let headHeight = len * this.headHeightFactor
        let headPnt = points[points.length - 1]
        len = PlotUtil.distance(headPnt, points[points.length - 2])
        let tailWidth = PlotUtil.distance(tailLeft, tailRight)
        if (headHeight > tailWidth * this.headTailFactor) {
            headHeight = tailWidth * this.headTailFactor
        }
        let headWidth = headHeight * this.headWidthFactor
        let neckWidth = headHeight * this.neckWidthFactor
        headHeight = headHeight > len ? len : headHeight
        let neckHeight = headHeight * this.neckHeightFactor
        let headEndPnt = PlotUtil.getThirdPoint(
            points[points.length - 2],
            headPnt,
            0,
            headHeight,
            true
        )
        let neckEndPnt = PlotUtil.getThirdPoint(
            points[points.length - 2],
            headPnt,
            0,
            neckHeight,
            true
        )
        let headLeft = PlotUtil.getThirdPoint(
            headPnt,
            headEndPnt,
            HALF_PI,
            headWidth,
            false
        )
        let headRight = PlotUtil.getThirdPoint(
            headPnt,
            headEndPnt,
            HALF_PI,
            headWidth,
            true
        )
        let neckLeft = PlotUtil.getThirdPoint(
            headPnt,
            neckEndPnt,
            HALF_PI,
            neckWidth,
            false
        )
        let neckRight = PlotUtil.getThirdPoint(
            headPnt,
            neckEndPnt,
            HALF_PI,
            neckWidth,
            true
        )
        return [neckLeft, headLeft, headPnt, headRight, neckRight]
    }
 
    _getArrowBodyPoints (points, neckLeft, neckRight, tailWidthFactor) {
        let allLen = PlotUtil.wholeDistance(points)
        let len = PlotUtil.getBaseLength(points)
        let tailWidth = len * tailWidthFactor
        let neckWidth = PlotUtil.distance(neckLeft, neckRight)
        let widthDif = (tailWidth - neckWidth) / 2
        let tempLen = 0
        let leftBodyPnts = []
        let rightBodyPnts = []
        for (let i = 1; i < points.length - 1; i++) {
            let angle =
                PlotUtil.getAngleOfThreePoints(
                    points[i - 1],
                    points[i],
                    points[i + 1]
                ) / 2
            tempLen += PlotUtil.distance(points[i - 1], points[i])
            let w = (tailWidth / 2 - (tempLen / allLen) * widthDif) / Math.sin(angle)
            let left = PlotUtil.getThirdPoint(
                points[i - 1],
                points[i],
                Math.PI - angle,
                w,
                true
            )
            let right = PlotUtil.getThirdPoint(
                points[i - 1],
                points[i],
                angle,
                w,
                false
            )
            leftBodyPnts.push(left)
            rightBodyPnts.push(right)
        }
        return leftBodyPnts.concat(rightBodyPnts)
    }
 
    _createHierarchy () {
        let pnts = Parse.parsePolygonCoordToArray(
            global.DC.Transform.transformCartesianArrayToWGS84Array(this._positions)
        )[0]
        let tailLeft = pnts[0]
        let tailRight = pnts[1]
        if (PlotUtil.isClockWise(pnts[0], pnts[1], pnts[2])) {
            tailLeft = pnts[1]
            tailRight = pnts[0]
        }
        let midTail = PlotUtil.mid(tailLeft, tailRight)
        let bonePnts = [midTail].concat(pnts.slice(2))
        // 计算箭头
        let headPnts = this._getArrowHeadPoints(bonePnts, tailLeft, tailRight)
        let neckLeft = headPnts[0]
        let neckRight = headPnts[4]
        let tailWidthFactor =
            PlotUtil.distance(tailLeft, tailRight) / PlotUtil.getBaseLength(bonePnts)
        // 计算箭身
        let bodyPnts = this._getArrowBodyPoints(
            bonePnts,
            neckLeft,
            neckRight,
            tailWidthFactor
        )
 
        // 整合
        let count = bodyPnts.length
        let leftPnts = [tailLeft].concat(bodyPnts.slice(0, count / 2))
        leftPnts.push(neckLeft)
        let rightPnts = [tailRight].concat(bodyPnts.slice(count / 2, count))
        rightPnts.push(neckRight)
        leftPnts = PlotUtil.getQBSplinePoints(leftPnts)
        rightPnts = PlotUtil.getQBSplinePoints(rightPnts)
 
        leftPnts = leftPnts.map(item => (item[2] = pnts[0][2], item))
        headPnts = headPnts.map(item => (item[2] = pnts[0][2], item))
        rightPnts = rightPnts.map(item => (item[2] = pnts[0][2], item))
 
 
        return new global.DC.Namespace.Cesium.PolygonHierarchy(
            global.DC.Transform.transformWGS84ArrayToCartesianArray(
                Parse.parsePositions(leftPnts.concat(headPnts, rightPnts.reverse()))
            )
        )
    }
}
 
export default AttackArrowGraphics