-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
lines.go
272 lines (229 loc) · 6.54 KB
/
lines.go
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
// Copyright 2022 Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vshape
import (
"image/color"
"log"
"math"
"cogentcore.org/core/mat32"
)
// Lines are lines rendered as long thin boxes defined by points
// and width parameters. The Mesh must be drawn in the XY plane (i.e., use Z = 0
// or a constant unless specifically relevant to have full 3D variation).
// Rotate the solid to put into other planes.
type Lines struct {
ShapeBase
// line points (must be 2 or more)
Points []mat32.Vec3
// line width, Y = height perpendicular to line direction, and X = depth
Width mat32.Vec2
// optional colors for each point -- actual color interpolates between
Colors []color.Color
// if true, connect the first and last points to form a closed shape
Closed bool
}
// NewLines returns a Lines shape with given size
func NewLines(points []mat32.Vec3, width mat32.Vec2, closed bool) *Lines {
ln := &Lines{}
ln.Defaults()
ln.Points = points
ln.Width = width
ln.Closed = closed
return ln
}
func (ln *Lines) Defaults() {
ln.Width.Set(.1, .1)
}
func (ln *Lines) N() (nVtx, nIdx int) {
nVtx, nIdx = LinesN(len(ln.Points), ln.Closed)
return
}
// Set sets points in given allocated arrays
func (ln *Lines) Set(vtxAry, normAry, texAry mat32.ArrayF32, idxAry mat32.ArrayU32) {
ln.CBBox = SetLines(vtxAry, normAry, texAry, idxAry, ln.VtxOff, ln.IdxOff, ln.Points, ln.Width, ln.Closed, ln.Pos)
// todo: colors!
}
/////////////////////////////////////////////////////////////////////
// LinesN returns number of vertex and idx points
func LinesN(npoints int, closed bool) (nVtx, nIdx int) {
qvn, qin := QuadN()
nv, ni := 4*(npoints-1)*qvn, 4*(npoints-1)*qin
if closed {
nv += 4 * qvn
ni += 4 * qin
} else {
nv += 2 * qvn
ni += 2 * qin
}
return nv, ni
}
// SetLines sets lines rendered as long thin boxes defined by points
// and width parameters. The Mesh must be drawn in the XY plane (i.e., use Z = 0
// or a constant unless specifically relevant to have full 3D variation).
// Rotate to put into other planes.
func SetLines(vtxAry, normAry, texAry mat32.ArrayF32, idxAry mat32.ArrayU32, vtxOff, idxOff int, points []mat32.Vec3, width mat32.Vec2, closed bool, pos mat32.Vec3) mat32.Box3 {
np := len(points)
if np < 2 {
log.Printf("vshape.SetLines: need 2 or more Points\n")
return mat32.Box3{}
}
pts := points
if closed {
pts = append(pts, points[0])
np++
}
vidx := vtxOff * 3
voff := vidx
ioff := idxOff
qvn, qin := QuadN()
wdy := width.Y / 2
wdz := width.X / 2
pi2 := float32(math.Pi / 2)
// logic for miter joins: https://math.stackexchange.com/questions/1849784/calculate-miter-points-of-stroked-vectors-in-cartesian-plane
for li := 0; li < np-1; li++ {
sp := pts[li]
sp.SetAdd(pos)
ep := pts[li+1]
ep.SetAdd(pos)
spSt := !closed && li == 0
epEd := !closed && li == np-2
swap := false
if ep.X < sp.X {
sp, ep = ep, sp
spSt, epEd = epEd, spSt
swap = true
}
v := ep.Sub(sp)
vn := v.Normal()
xyang := mat32.Atan2(vn.Y, vn.X)
xy := mat32.V2(wdy*mat32.Cos(xyang+pi2), wdy*mat32.Sin(xyang+pi2))
// sypzm --- eypzm
// / | / |
// sypzp -- eypzp |// ToAlphaPreMult converts a non-alpha-premultiplied color to a premultiplied one.
// | symzm --| eymzm
// | / | /
// symzp -- eymzp
sypzp, sypzm, symzp, symzm := sp, sp, sp, sp
eypzp, eypzm, eymzp, eymzm := ep, ep, ep, ep
if !spSt {
pp := sp
if swap {
if closed && li == np-2 {
pp = pts[1]
} else {
pp = pts[li+2]
}
pp.SetAdd(pos)
} else {
if closed && li == 0 {
pp = pts[np-2]
} else {
pp = pts[li-1]
}
pp.SetAdd(pos)
}
xy = MiterPts(pp.X, pp.Y, sp.X, sp.Y, ep.X, ep.Y, wdy)
}
sypzp.X += xy.X
sypzp.Y += xy.Y
sypzp.Z += wdz
sypzm.X += xy.X
sypzm.Y += xy.Y
sypzm.Z += -wdz
symzp.X += -xy.X
symzp.Y += -xy.Y
symzp.Z += wdz
symzm.X += -xy.X
symzm.Y += -xy.Y
symzm.Z += -wdz
if !epEd {
xp := ep
if swap {
if closed && li == 0 {
xp = pts[np-2]
} else {
xp = pts[li-1]
}
xp.SetAdd(pos)
} else {
if closed && li == np-2 {
xp = pts[1]
} else {
xp = pts[li+2]
}
xp.SetAdd(pos)
}
xy = MiterPts(xp.X, xp.Y, ep.X, ep.Y, sp.X, sp.Y, wdy)
}
eypzp.X += xy.X
eypzp.Y += xy.Y
eypzp.Z += wdz
eypzm.X += xy.X
eypzm.Y += xy.Y
eypzm.Z += -wdz
eymzp.X += -xy.X
eymzp.Y += -xy.Y
eymzp.Z += wdz
eymzm.X += -xy.X
eymzm.Y += -xy.Y
eymzm.Z += -wdz
// front back
// 0 3 1 2
// 1 2 0 3
// two triangles are: 0,1,2; 0,2,3
if swap {
SetQuad(vtxAry, normAry, texAry, idxAry, voff, ioff, []mat32.Vec3{sypzm, symzm, eymzm, eypzm}, nil, pos)
voff += qvn
ioff += qin
SetQuad(vtxAry, normAry, texAry, idxAry, voff, ioff, []mat32.Vec3{sypzp, sypzm, eypzm, eypzp}, nil, pos) // bottom (yp, upside down)
voff += qvn
ioff += qin
SetQuad(vtxAry, normAry, texAry, idxAry, voff, ioff, []mat32.Vec3{symzm, symzp, eymzp, eymzm}, nil, pos) // top (ym)
voff += qvn
ioff += qin
SetQuad(vtxAry, normAry, texAry, idxAry, voff, ioff, []mat32.Vec3{symzp, sypzp, eypzp, eymzp}, nil, pos) // front (zp)
voff += qvn
ioff += qin
} else {
SetQuad(vtxAry, normAry, texAry, idxAry, voff, ioff, []mat32.Vec3{symzm, sypzm, eypzm, eymzm}, nil, pos) // back (zm)
voff += qvn
ioff += qin
SetQuad(vtxAry, normAry, texAry, idxAry, voff, ioff, []mat32.Vec3{symzp, symzm, eymzm, eymzp}, nil, pos) // bottom (ym)
voff += qvn
ioff += qin
SetQuad(vtxAry, normAry, texAry, idxAry, voff, ioff, []mat32.Vec3{sypzm, sypzp, eypzp, eypzm}, nil, pos) // top (yp)
voff += qvn
ioff += qin
SetQuad(vtxAry, normAry, texAry, idxAry, voff, ioff, []mat32.Vec3{sypzp, symzp, eymzp, eypzp}, nil, pos) // front (zp)
voff += qvn
ioff += qin
}
if spSt { // do cap
SetQuad(vtxAry, normAry, texAry, idxAry, voff, ioff, []mat32.Vec3{sypzm, symzm, symzp, sypzp}, nil, pos)
voff += qvn
ioff += qin
}
if epEd {
SetQuad(vtxAry, normAry, texAry, idxAry, voff, ioff, []mat32.Vec3{eypzp, eymzp, eymzm, eypzm}, nil, pos)
voff += qvn
ioff += qin
}
}
vn := voff - vtxOff
return BBoxFromVtxs(vtxAry, vtxOff, vn)
}
// MiterPts returns the miter points
func MiterPts(ax, ay, bx, by, cx, cy, w2 float32) mat32.Vec2 {
ppd := mat32.V2(ax-bx, ay-by)
ppu := ppd.Normal()
epd := mat32.V2(cx-bx, cy-by)
epv := epd.Normal()
dp := ppu.Dot(epv)
jang := mat32.Acos(dp)
wfact := w2 / mat32.Sin(jang)
uv := ppu.MulScalar(-wfact)
vv := epv.MulScalar(-wfact)
sv := uv.Add(vv)
return sv
}