forked from ezrec/uv3dp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprintable.go
66 lines (55 loc) · 1.39 KB
/
printable.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
//
// Copyright (c) 2020 Jason S. McMullan <[email protected]>
//
package uv3dp
import (
"image"
"runtime"
"sync"
"time"
)
type Printable interface {
Size() Size
Exposure() Exposure
Bottom() Bottom
Preview(index PreviewType) (image.Image, bool)
MetadataKeys() []string
Metadata(key string) (data interface{}, ok bool)
LayerZ(index int) float32
LayerExposure(index int) Exposure
LayerImage(index int) *image.Gray
}
// WithAllLayers executes a function in parallel over all of the layers
func WithAllLayers(p Printable, do func(p Printable, n int)) {
layers := p.Size().Layers
prog := NewProgress(layers)
defer prog.Close()
guard := make(chan struct{}, runtime.GOMAXPROCS(0))
for n := 0; n < layers; n++ {
guard <- struct{}{}
go func(p Printable, do func(p Printable, n int), n int) {
do(p, n)
prog.Indicate()
runtime.GC()
<-guard
}(p, do, n)
}
}
// WithEachLayer executes a function in over all of the layers, serially (but possibly out of order)
func WithEachLayer(p Printable, do func(p Printable, n int)) {
var mutex sync.Mutex
WithAllLayers(p, func(p Printable, n int) {
mutex.Lock()
do(p, n)
mutex.Unlock()
})
}
// Get the total print time for a printable
func PrintDuration(p Printable) (duration time.Duration) {
layers := p.Size().Layers
for n := 0; n < layers; n++ {
exposure := p.LayerExposure(n)
duration += exposure.Duration()
}
return
}