-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharpness.go
56 lines (46 loc) · 1.53 KB
/
sharpness.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
package unit
import (
"encoding"
"fmt"
"strconv"
"strings"
)
// Sharpness is a numerical measurement of angular change per physical length.
// And example is the sharpness of a curve.
type Sharpness float64
var _ encoding.TextUnmarshaler = (*Sharpness)(nil)
// RadianPerMeter is the SI unit for measuring Sharpness.
const RadianPerMeter Sharpness = 1
const radianPerMeterSymol = "rad/m"
// RadianPerMeter returns sharpness as a float64 with the unit of rad/m.
func (s Sharpness) RadianPerMeter() float64 {
return float64(s)
}
// Get returns the sharpness with the unit of as.
func (s Sharpness) Get(as Sharpness) float64 {
return float64(s) / float64(as)
}
// String implements fmt.Stringer.
func (s Sharpness) String() string {
return format(float64(s), radianPerMeterSymol)
}
// UnmarshalString sets *c from s.
func (s *Sharpness) UnmarshalString(str string) error {
if !strings.HasSuffix(str, radianPerMeterSymol) {
return fmt.Errorf("unmarshal sharpness: input '%s' doesn't have the '%s' suffix", s, radianPerMeterSymol)
}
strNoUnit := strings.TrimSuffix(str, radianPerMeterSymol)
if strNoUnit != strings.TrimSpace(strNoUnit) {
return fmt.Errorf("unmarshal sharpness: input '%s' has invalid whitespace", s)
}
magnitude, err := strconv.ParseFloat(strNoUnit, 64)
if err != nil {
return fmt.Errorf("unmarshal sharpness: parse '%s': %w", s, err)
}
*s = Sharpness(magnitude)
return nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (s *Sharpness) UnmarshalText(text []byte) error {
return s.UnmarshalString(string(text))
}