-
Notifications
You must be signed in to change notification settings - Fork 14
/
types.h
410 lines (338 loc) · 12.3 KB
/
types.h
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* Copyright 2022 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.
*/
#ifndef INK_STROKE_MODELER_TYPES_H_
#define INK_STROKE_MODELER_TYPES_H_
#include <cmath>
#include <ostream>
#include <string>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
namespace ink {
namespace stroke_model {
// A vector (or point) in 2D space.
struct Vec2 {
float x = 0;
float y = 0;
// The length of the vector, i.e. its distance from the origin.
float Magnitude() const { return std::hypot(x, y); }
// The difference in angle between the vector and another vector in radians
// [0, pi]. Returns an error status if either of the inputs is non-finite.
absl::StatusOr<float> AbsoluteAngleTo(Vec2 other) const;
// This calculates the dot product between the two vectors.
static float DotProduct(Vec2 a, Vec2 b);
bool IsFinite() const { return std::isfinite(x) && std::isfinite(y); }
};
bool operator==(Vec2 lhs, Vec2 rhs);
bool operator!=(Vec2 lhs, Vec2 rhs);
Vec2 operator+(Vec2 lhs, Vec2 rhs);
Vec2 operator-(Vec2 lhs, Vec2 rhs);
Vec2 operator*(float scalar, Vec2 v);
Vec2 operator*(Vec2 v, float scalar);
Vec2 operator/(Vec2 v, float scalar);
Vec2 &operator+=(Vec2 &lhs, Vec2 rhs);
Vec2 &operator-=(Vec2 &lhs, Vec2 rhs);
Vec2 &operator*=(Vec2 &lhs, float scalar);
Vec2 &operator/=(Vec2 &lhs, float scalar);
std::string ToFormattedString(Vec2 vec);
template <typename Sink>
void AbslStringify(Sink &sink, Vec2 vec) {
sink.Append(ToFormattedString(vec));
}
std::ostream &operator<<(std::ostream &stream, Vec2 v);
// This represents a duration of time, i.e. the difference between two points in
// time (as represented by class Time, below). This class is unit-agnostic; it
// could represent e.g. hours, seconds, or years.
class Duration {
public:
Duration() : Duration(0) {}
explicit Duration(double value) : value_(value) {}
double Value() const { return value_; }
private:
double value_ = 0;
};
Duration operator+(Duration lhs, Duration rhs);
Duration operator-(Duration lhs, Duration rhs);
Duration operator*(Duration duration, double scalar);
Duration operator*(double scalar, Duration duration);
Duration operator/(Duration duration, double scalar);
Duration &operator+=(Duration &lhs, Duration rhs);
Duration &operator-=(Duration &lhs, Duration rhs);
Duration &operator*=(Duration &duration, double scalar);
Duration &operator/=(Duration &duration, double scalar);
bool operator==(Duration lhs, Duration rhs);
bool operator!=(Duration lhs, Duration rhs);
bool operator<(Duration lhs, Duration rhs);
bool operator>(Duration lhs, Duration rhs);
bool operator<=(Duration lhs, Duration rhs);
bool operator>=(Duration lhs, Duration rhs);
std::string ToFormattedString(Duration duration);
template <typename Sink>
void AbslStringify(Sink &sink, Duration duration) {
sink.Append(ToFormattedString(duration));
}
std::ostream &operator<<(std::ostream &s, Duration duration);
// This represents a point in time. This class is unit- and offset-agnostic; it
// could be measured in e.g. hours, seconds, or years, and Time(0) has no
// specific meaning outside of the context it is used in.
class Time {
public:
Time() : Time(0) {}
explicit Time(double value) : value_(value) {}
double Value() const { return value_; }
private:
double value_ = 0;
};
Time operator+(Time time, Duration duration);
Time operator+(Duration duration, Time time);
Time operator-(Time time, Duration duration);
Duration operator-(Time lhs, Time rhs);
Time &operator+=(Time &time, Duration duration);
Time &operator-=(Time &time, Duration duration);
bool operator==(Time lhs, Time rhs);
bool operator!=(Time lhs, Time rhs);
bool operator<(Time lhs, Time rhs);
bool operator>(Time lhs, Time rhs);
bool operator<=(Time lhs, Time rhs);
bool operator>=(Time lhs, Time rhs);
std::string ToFormattedString(Time time);
template <typename Sink>
void AbslStringify(Sink &sink, Time time) {
sink.Append(ToFormattedString(time));
}
std::ostream &operator<<(std::ostream &s, Time time);
// The input passed to the stroke modeler.
struct Input {
// The type of event represented by the input. A "kDown" event represents
// the beginning of the stroke, a "kUp" event represents the end of the
// stroke, and all events in between are "kMove" events.
enum class EventType { kDown, kMove, kUp };
EventType event_type;
// The position of the input.
Vec2 position{0};
// The time at which the input occurs.
Time time{0};
// The amount of pressure applied to the stylus. This is expected to lie in
// the range [0, 1]. A negative value indicates unknown pressure.
float pressure = -1;
// The angle between the stylus and the plane of the device's screen. This
// is expected to lie in the range [0, π/2]. A value of 0 indicates that the
// stylus is perpendicular to the screen, while a value of π/2 indicates
// that it is flush with the screen. A negative value indicates unknown
// tilt.
float tilt = -1;
// The angle between the projection of the stylus onto the screen and the
// positive x-axis, measured counter-clockwise. This is expected to lie in
// the range [0, 2π). A negative value indicates unknown orientation.
float orientation = -1;
};
bool operator==(const Input &lhs, const Input &rhs);
bool operator!=(const Input &lhs, const Input &rhs);
absl::Status ValidateInput(const Input &input);
std::string ToFormattedString(Input::EventType event_type);
template <typename Sink>
void AbslStringify(Sink &sink, Input::EventType event_type) {
sink.Append(ToFormattedString(event_type));
}
std::ostream &operator<<(std::ostream &s, Input::EventType event_type);
std::string ToFormattedString(const Input &input);
template <typename Sink>
void AbslStringify(Sink &sink, const Input &input) {
sink.Append(ToFormattedString(input));
}
std::ostream &operator<<(std::ostream &s, const Input &input);
// A modeled input produced by the stroke modeler.
struct Result {
// The position/velocity/acceleration of the stroke tip.
Vec2 position{0};
Vec2 velocity{0};
Vec2 acceleration{0};
// The time at which this input occurs.
Time time{0};
// These pressure, tilt, and orientation of the stylus. See the
// corresponding fields on the Input struct for more info.
float pressure = -1;
float tilt = -1;
float orientation = -1;
};
bool operator==(const Result &lhs, const Result &rhs);
bool operator!=(const Result &lhs, const Result &rhs);
std::string ToFormattedString(const Result &result);
template <typename Sink>
void AbslStringify(Sink &sink, const Result &result) {
sink.Append(ToFormattedString(result));
}
std::ostream &operator<<(std::ostream &s, const Result &result);
////////////////////////////////////////////////////////////////////////////////
// Inline function definitions
////////////////////////////////////////////////////////////////////////////////
inline bool operator==(Vec2 lhs, Vec2 rhs) {
return lhs.x == rhs.x && lhs.y == rhs.y;
}
inline bool operator!=(Vec2 lhs, Vec2 rhs) { return !(lhs == rhs); }
inline Vec2 operator+(Vec2 lhs, Vec2 rhs) {
return {.x = lhs.x + rhs.x, .y = lhs.y + rhs.y};
}
inline Vec2 operator-(Vec2 lhs, Vec2 rhs) {
return {.x = lhs.x - rhs.x, .y = lhs.y - rhs.y};
}
inline Vec2 operator*(float scalar, Vec2 v) {
return {.x = scalar * v.x, .y = scalar * v.y};
}
inline Vec2 operator*(Vec2 v, float scalar) { return scalar * v; }
inline Vec2 operator/(Vec2 v, float scalar) {
return {.x = v.x / scalar, .y = v.y / scalar};
}
inline Vec2 &operator+=(Vec2 &lhs, Vec2 rhs) {
lhs.x += rhs.x;
lhs.y += rhs.y;
return lhs;
}
inline Vec2 &operator-=(Vec2 &lhs, Vec2 rhs) {
lhs.x -= rhs.x;
lhs.y -= rhs.y;
return lhs;
}
inline Vec2 &operator*=(Vec2 &lhs, float scalar) {
lhs.x *= scalar;
lhs.y *= scalar;
return lhs;
}
inline Vec2 &operator/=(Vec2 &lhs, float scalar) {
lhs.x /= scalar;
lhs.y /= scalar;
return lhs;
}
inline std::ostream &operator<<(std::ostream &stream, Vec2 v) {
return stream << ToFormattedString(v);
}
inline Duration operator+(Duration lhs, Duration rhs) {
return Duration(lhs.Value() + rhs.Value());
}
inline Duration operator-(Duration lhs, Duration rhs) {
return Duration(lhs.Value() - rhs.Value());
}
inline Duration operator*(Duration duration, double scalar) {
return Duration(duration.Value() * scalar);
}
inline Duration operator*(double scalar, Duration duration) {
return Duration(scalar * duration.Value());
}
inline Duration operator/(Duration duration, double scalar) {
return Duration(duration.Value() / scalar);
}
inline Duration &operator+=(Duration &lhs, Duration rhs) {
lhs = lhs + rhs;
return lhs;
}
inline Duration &operator-=(Duration &lhs, Duration rhs) {
lhs = lhs - rhs;
return lhs;
}
inline Duration &operator*=(Duration &duration, double scalar) {
duration = duration * scalar;
return duration;
}
inline Duration &operator/=(Duration &duration, double scalar) {
duration = duration / scalar;
return duration;
}
inline bool operator==(Duration lhs, Duration rhs) {
return lhs.Value() == rhs.Value();
}
inline bool operator!=(Duration lhs, Duration rhs) {
return lhs.Value() != rhs.Value();
}
inline bool operator<(Duration lhs, Duration rhs) {
return lhs.Value() < rhs.Value();
}
inline bool operator>(Duration lhs, Duration rhs) {
return lhs.Value() > rhs.Value();
}
inline bool operator<=(Duration lhs, Duration rhs) {
return lhs.Value() <= rhs.Value();
}
inline bool operator>=(Duration lhs, Duration rhs) {
return lhs.Value() >= rhs.Value();
}
inline std::ostream &operator<<(std::ostream &s, Duration duration) {
return s << ToFormattedString(duration);
}
inline Time operator+(Time time, Duration duration) {
return Time(time.Value() + duration.Value());
}
inline Time operator+(Duration duration, Time time) {
return Time(time.Value() + duration.Value());
}
inline Time operator-(Time time, Duration duration) {
return Time(time.Value() - duration.Value());
}
inline Duration operator-(Time lhs, Time rhs) {
return Duration(lhs.Value() - rhs.Value());
}
inline Time &operator+=(Time &time, Duration duration) {
time = time + duration;
return time;
}
inline Time &operator-=(Time &time, Duration duration) {
time = time - duration;
return time;
}
inline bool operator==(Time lhs, Time rhs) {
return lhs.Value() == rhs.Value();
}
inline bool operator!=(Time lhs, Time rhs) {
return lhs.Value() != rhs.Value();
}
inline bool operator<(Time lhs, Time rhs) { return lhs.Value() < rhs.Value(); }
inline bool operator>(Time lhs, Time rhs) { return lhs.Value() > rhs.Value(); }
inline bool operator<=(Time lhs, Time rhs) {
return lhs.Value() <= rhs.Value();
}
inline bool operator>=(Time lhs, Time rhs) {
return lhs.Value() >= rhs.Value();
}
inline std::ostream &operator<<(std::ostream &s, Time time) {
return s << ToFormattedString(time);
}
inline bool operator==(const Input &lhs, const Input &rhs) {
return lhs.event_type == rhs.event_type && lhs.position == rhs.position &&
lhs.time == rhs.time && lhs.pressure == rhs.pressure &&
lhs.tilt == rhs.tilt && lhs.orientation == rhs.orientation;
}
inline bool operator!=(const Input &lhs, const Input &rhs) {
return !(lhs == rhs);
}
inline bool operator==(const Result &lhs, const Result &rhs) {
return lhs.position == rhs.position && lhs.velocity == rhs.velocity &&
lhs.acceleration == rhs.acceleration && lhs.time == rhs.time &&
lhs.pressure == rhs.pressure && lhs.tilt == rhs.tilt &&
lhs.orientation == rhs.orientation;
}
inline bool operator!=(const Result &lhs, const Result &rhs) {
return !(lhs == rhs);
}
inline std::ostream &operator<<(std::ostream &s, Input::EventType event_type) {
return s << ToFormattedString(event_type);
}
inline std::ostream &operator<<(std::ostream &s, const Input &input) {
return s << ToFormattedString(input);
}
inline std::ostream &operator<<(std::ostream &s, const Result &result) {
return s << ToFormattedString(result);
}
} // namespace stroke_model
} // namespace ink
#endif // INK_STROKE_MODELER_TYPES_H_