/
profileChart.go
175 lines (153 loc) · 5.75 KB
/
profileChart.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
/**
*Copyright (c) 2018 Stephen Merrony
*
*This software is released under the MIT License.
*https://opensource.org/licenses/MIT
*/
package main
import (
"fmt"
"image"
"image/color"
"image/draw"
"strconv"
"time"
"github.com/mattn/go-gtk/gdkpixbuf"
"github.com/mattn/go-gtk/gtk"
)
type profileChartT struct {
*gtk.Image
track *telloTrackT
backingImage *image.RGBA
pbd gdkpixbuf.PixbufData
pixBuf *gdkpixbuf.Pixbuf
width, height, xOrigin, yOrigin int
bgCol, axesCol, labelCol, lineCol, faintCol color.Color
maxOffset float32
xScalePPS, yScalePPM float32 // scale factors expressed as Pixels Per Second/Metre
trackDuration time.Duration
}
func buildProfileChart(w, h int) (pc *profileChartT) {
pc = new(profileChartT)
pc.Image = gtk.NewImage()
pc.width, pc.height = w, h
pc.xOrigin = 20
pc.yOrigin = h / 2
pc.bgCol = color.White
pc.axesCol = color.RGBA{0, 0, 0, 255} // black
pc.labelCol = color.RGBA{128, 128, 128, 255} // dark grey
pc.lineCol = color.RGBA{255, 0, 0, 255} // red
pc.faintCol = color.RGBA{192, 192, 192, 64} // light grey
pc.maxOffset = 10
pc.yScalePPM = float32(pc.yOrigin) / 10
pc.trackDuration, _ = time.ParseDuration("1m")
pc.backingImage = image.NewRGBA(image.Rectangle{image.Point{0, 0}, image.Point{w, h}})
pc.pbd.Colorspace = gdkpixbuf.GDK_COLORSPACE_RGB
pc.pbd.HasAlpha = true
pc.pbd.BitsPerSample = 8
pc.pbd.Width = w
pc.pbd.Height = h
pc.pbd.RowStride = pc.backingImage.Stride
pc.pbd.Data = pc.backingImage.Pix
pc.pixBuf = gdkpixbuf.NewPixbufFromData(pc.pbd)
pc.track = newTrack()
pc.drawEmptyChart()
pc.SetFromPixbuf(pc.pixBuf)
return pc
}
func (pc *profileChartT) clearChart() {
draw.Draw(pc.backingImage, pc.backingImage.Bounds(), image.NewUniform(pc.bgCol), image.ZP, draw.Src)
pc.pbd.Data = pc.backingImage.Pix
pc.pixBuf = gdkpixbuf.NewPixbufFromData(pc.pbd)
pc.SetFromPixbuf(pc.pixBuf)
}
func (pc *profileChartT) calcScales() {
// vertical (height)
pc.maxOffset = pc.track.deriveHeightScale()
pc.yScalePPM = float32(pc.yOrigin) / pc.maxOffset
// horizontal (time)
if len(pc.track.positions) > 3 {
pc.trackDuration = pc.track.positions[len(pc.track.positions)-1].timeStamp.Sub(pc.track.positions[1].timeStamp)
} else {
pc.trackDuration = time.Minute
}
pc.xScalePPS = float32(float64(pc.width-20) / pc.trackDuration.Seconds())
// log.Printf("Debug: profileChart xScalePPS is: %f, from %f seconds\n", pc.xScalePPS, pc.trackDuration.Seconds())
}
// xToOrd converts a horizontal (time in secs) value to its physical equivalent on an image
func (pc *profileChartT) xToOrd(x float32) (xOrd int) {
xOrd = int(float32(pc.xOrigin) + x*pc.xScalePPS)
return xOrd
}
// yToOrd converts a vertical (height) value to its physical equivalent on an image
func (pc *profileChartT) yToOrd(y float32) (yOrd int) {
yOrd = int(float32(pc.yOrigin) - y*pc.yScalePPM)
return yOrd
}
// drawEmptyChart draws the custom 'graph paper' for blank and populated charts.
func (pc *profileChartT) drawEmptyChart() {
pc.clearChart()
// blank vertical axis
drawPhysLine(pc.backingImage, pc.xOrigin, 0, pc.xOrigin, pc.height, pc.axesCol)
// faint vertical grid lines - 1 per minute
for s := 60; s <= int(pc.trackDuration.Seconds()); s += 60 {
drawPhysLine(pc.backingImage, pc.xToOrd(float32(s)), 0, pc.xToOrd(float32(s)), pc.height, pc.faintCol)
drawPhysLabel(pc.backingImage, pc.xToOrd(float32(s)), pc.yToOrd(0)-10, fmt.Sprintf("%d'", s/60), pc.labelCol)
}
// y-axis labels
var yTickInterval float32 = 100.0
switch {
case pc.maxOffset < 11.1:
yTickInterval = 1.0
case pc.maxOffset < 101.1:
yTickInterval = 10.0
}
//log.Printf("Debug: drawEmptyChart() - maxOffset: %f, yTickInterval: %f\n", pc.maxOffset, yTickInterval)
for y := -pc.maxOffset; y <= pc.maxOffset; y += yTickInterval {
pc.backingImage.Set(pc.xOrigin-1, pc.yOrigin+int(y*pc.yScalePPM), pc.axesCol)
pc.backingImage.Set(pc.xOrigin+1, pc.yOrigin+int(y*pc.yScalePPM), pc.axesCol)
drawPhysLabel(pc.backingImage, 5, pc.yToOrd(y)+6, strconv.Itoa(int(y)), pc.labelCol)
drawPhysLine(pc.backingImage, pc.xOrigin, pc.yOrigin+int(y*pc.yScalePPM), pc.width, pc.yOrigin+int(y*pc.yScalePPM), pc.faintCol)
}
// blank horizontal axis
drawPhysLine(pc.backingImage, pc.xOrigin, pc.yOrigin, pc.width, pc.yOrigin, pc.axesCol)
pc.pbd.Data = pc.backingImage.Pix
pc.pixBuf = gdkpixbuf.NewPixbufFromData(pc.pbd)
pc.SetFromPixbuf(pc.pixBuf)
}
func (pc *profileChartT) drawTitles() {
const dateFmt = "Jan 2 2006 15:04:05"
if len(pc.track.positions) > 1 {
drawPhysLabel(pc.backingImage, 40, pc.height-40, fmt.Sprintf("Flight Profile from %s to %s",
pc.track.positions[1].timeStamp.Format(dateFmt),
pc.track.positions[len(pc.track.positions)-1].timeStamp.Format("15:04:05")),
pc.labelCol)
}
}
func (pc *profileChartT) drawProfile() {
if pc.track == nil || len(pc.track.positions) < 3 {
return
}
pc.calcScales()
pc.drawEmptyChart()
t0 := pc.track.positions[1].timeStamp
var lastT time.Duration
lastH := float32(pc.track.positions[1].heightDm) / 10
for n, pos := range pc.track.positions {
if n > 1 {
t := pos.timeStamp.Sub(t0) // how long in (fractional) seconds
h := float32(pos.heightDm) / 10
// log.Printf("Debug: drawing from %d, %d to %d, %d\n", pc.xToOrd(float32(lastT.Seconds())), pc.yToOrd(lastH),
// pc.xToOrd(float32(t.Seconds())), pc.yToOrd(h))
drawPhysLine(pc.backingImage,
pc.xToOrd(float32(lastT.Seconds())), pc.yToOrd(lastH),
pc.xToOrd(float32(t.Seconds())), pc.yToOrd(h),
pc.lineCol)
lastT = t
lastH = h
}
}
pc.drawTitles()
pc.pbd.Data = pc.backingImage.Pix
pc.SetFromPixbuf(pc.pixBuf)
}