-
Notifications
You must be signed in to change notification settings - Fork 11
/
orientation.go
215 lines (191 loc) · 6 KB
/
orientation.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
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package math
import (
"errors"
"fmt"
"strings"
)
// Orientation represents a transformation matrix, written as a right and a down vector.
type Orientation struct {
Right Delta
Down Delta
}
// Concat returns the orientation o * o2 so that o.Concat(o2).Apply(d) == o.Apply(o2.Apply(d)).
func (o Orientation) Concat(o2 Orientation) Orientation {
return Orientation{
Right: o.Apply(o2.Right),
Down: o.Apply(o2.Down),
}
}
// Apply rotates a delta by an orientation.
func (o Orientation) Apply(d Delta) Delta {
return Delta{
DX: o.Right.DX*d.DX + o.Down.DX*d.DY,
DY: o.Right.DY*d.DX + o.Down.DY*d.DY,
}
}
// ApplyToRect2 rotates a rectangle by the given orientation, mapping the pivot to itself.
// The pivot is given in doubled coordinates to support half-pixel pivots.
// Note: odd numbers are pixel centers, even numbers are pixel corners!
func (o Orientation) ApplyToRect2(pivot2 Pos, r Rect) Rect {
return Rect{
Origin: pivot2.Add(o.Apply(r.Origin.Mul(2).Delta(pivot2))).Div(2),
Size: o.Apply(r.Size),
}.Normalized()
}
// Inverse returns an orientation so that o.Concat(o.Invert()) == Identity().
func (o Orientation) Inverse() Orientation {
// There is probably a more efficient way, but all our orientations are identity when applied four times.
return o.Concat(o).Concat(o)
}
// Identity yields the default orientation.
func Identity() Orientation {
return Orientation{Right: East(), Down: South()}
}
// FlipX yields an orientation where X is flipped.
func FlipX() Orientation {
return Orientation{Right: West(), Down: South()}
}
// FlipY yields an orientation where Y is flipped.
func FlipY() Orientation {
return Orientation{Right: East(), Down: North()}
}
// FlipD yields an orientation where X/Y are swapped.
func FlipD() Orientation {
return Orientation{Right: South(), Down: East()}
}
// Left yields an orientation that turns left.
func Left() Orientation {
return Orientation{Right: North(), Down: East()}
}
// Right yields an orientation that turns right.
func Right() Orientation {
return Orientation{Right: South(), Down: West()}
}
// Left yields an orientation that turns left.
func TurnAround() Orientation {
return Orientation{Right: West(), Down: North()}
}
// ParseOrientation parses an orientation from a string. It is given by the right and down directions in that order.
func ParseOrientation(s string) (Orientation, error) {
switch s {
case "EN":
return Orientation{Right: East(), Down: North()}, nil
case "ES":
return Orientation{Right: East(), Down: South()}, nil
case "NE":
return Orientation{Right: North(), Down: East()}, nil
case "NW":
return Orientation{Right: North(), Down: West()}, nil
case "SE":
return Orientation{Right: South(), Down: East()}, nil
case "SW":
return Orientation{Right: South(), Down: West()}, nil
case "WN":
return Orientation{Right: West(), Down: North()}, nil
case "WS":
return Orientation{Right: West(), Down: South()}, nil
default:
return Orientation{}, fmt.Errorf("unsupported orientation %q; want <right><down> direction like ES", s)
}
}
type Orientations []Orientation
var AllOrientations = Orientations{
Orientation{Right: East(), Down: North()},
Orientation{Right: East(), Down: South()},
Orientation{Right: North(), Down: East()},
Orientation{Right: North(), Down: West()},
Orientation{Right: South(), Down: East()},
Orientation{Right: South(), Down: West()},
Orientation{Right: West(), Down: North()},
Orientation{Right: West(), Down: South()},
}
func ParseOrientations(s string) (Orientations, error) {
if s == "" {
return Orientations{}, nil
}
orientations := strings.Split(s, " ")
if len(orientations) == 0 {
return nil, errors.New("unsupported orientation list: empty")
}
out := make([]Orientation, len(orientations))
for i, orientation := range orientations {
var err error
out[i], err = ParseOrientation(orientation)
if err != nil {
return nil, err
}
}
return out, nil
}
func (o Orientation) Determinant() int {
return o.Right.DX*o.Down.DY - o.Right.DY*o.Down.DX
}
func (o Orientation) MarshalText() ([]byte, error) {
switch o {
case Orientation{Right: East(), Down: North()}:
return []byte("EN"), nil
case Orientation{Right: East(), Down: South()}:
return []byte("ES"), nil
case Orientation{Right: North(), Down: East()}:
return []byte("NE"), nil
case Orientation{Right: North(), Down: West()}:
return []byte("NW"), nil
case Orientation{Right: South(), Down: East()}:
return []byte("SE"), nil
case Orientation{Right: South(), Down: West()}:
return []byte("SW"), nil
case Orientation{Right: West(), Down: North()}:
return []byte("WN"), nil
case Orientation{Right: West(), Down: South()}:
return []byte("WS"), nil
default:
return nil, fmt.Errorf("unsupported Orientation{Right: %v, Down: %v}", o.Right, o.Down)
}
}
func (o Orientation) String() string {
text, err := o.MarshalText()
if err != nil {
return err.Error()
}
return string(text)
}
func (o *Orientation) UnmarshalText(text []byte) error {
var err error
*o, err = ParseOrientation(string(text))
return err
}
func (o Orientations) MarshalText() ([]byte, error) {
if len(o) == 0 {
return []byte{}, nil
}
out := make([]byte, 0, 3*len(o)-1)
for _, o1 := range o {
out1, err := o1.MarshalText()
if err != nil {
return nil, err
}
if len(out) != 0 {
out = append(out, ' ')
}
out = append(out, out1...)
}
return out, nil
}
func (o *Orientations) UnmarshalText(text []byte) error {
var err error
*o, err = ParseOrientations(string(text))
return err
}