forked from fogleman/contourmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontourmap.go
89 lines (81 loc) · 2.22 KB
/
contourmap.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
package contourmap
import (
"image"
"math"
)
type ContourMap struct {
W int // width of the contour map in pixels
H int // height of the contour map in pixels
Min float64 // minimum value contained in this contour map
Max float64 // maximum value contained in this contour map
grid []float64
}
// FromFloat64s returns a new ContourMap for the provided 2D grid of values.
// len(grid) must equal w * h.
func FromFloat64s(w, h int, grid []float64) *ContourMap {
min := math.Inf(1)
max := math.Inf(-1)
for _, x := range grid {
if x == closed {
continue
}
min = math.Min(min, x)
max = math.Max(max, x)
}
return &ContourMap{w, h, min, max, grid}
}
// FromFloat64s returns a new ContourMap for the provided function.
// The function will be called for all points x = [0, w) and y = [0, h) to
// determine the Z value at each point.
func FromFunction(w, h int, f Function) *ContourMap {
grid := make([]float64, w*h)
i := 0
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
grid[i] = f(x, y)
i++
}
}
return FromFloat64s(w, h, grid)
}
// FromImage returns a new ContourMap for the provided image. The image is
// converted to 16-bit grayscale and will have Z values mapped from
// [0, 65535] to [0, 1].
func FromImage(im image.Image) *ContourMap {
gray := imageToGray16(im)
w := gray.Bounds().Size().X
h := gray.Bounds().Size().Y
grid := make([]float64, w*h)
j := 0
for i := range grid {
x := int(gray.Pix[j])<<8 | int(gray.Pix[j+1])
grid[i] = float64(x) / 0xffff
j += 2
}
return FromFloat64s(w, h, grid)
}
func (m *ContourMap) at(x, y int) float64 {
return m.grid[y*m.W+x]
}
// Contours returns a list of contours the represent isolines at the specified
// Z value.
func (m *ContourMap) Contours(z float64) []Contour {
return marchingSquares(m, m.W, m.H, z)
}
// Closed returns a new ContourMap that will ensure all Contours are closed
// paths by following the border when they would normally stop at the edge
// of the grid.
func (m *ContourMap) Closed() *ContourMap {
w := m.W + 2
h := m.H + 2
grid := make([]float64, w*h)
for i := range grid {
grid[i] = closed
}
for y := 0; y < m.H; y++ {
i := (y+1)*w + 1
j := y * m.W
copy(grid[i:], m.grid[j:j+m.W])
}
return FromFloat64s(w, h, grid)
}