aboutsummaryrefslogtreecommitdiff
path: root/ink_stroke_modeler/types.h
blob: 922c0f73dc31795554d392e72e3d8676dfac7b92 (plain)
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
/*
 * 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>

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::sqrt(x * x + y * 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::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::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::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);

std::ostream &operator<<(std::ostream &s, Input::EventType event_type);
std::ostream &operator<<(std::ostream &s, const Input &input);

// A modeled input produced by the stroke modeler.
struct Result {
  // The position and velocity of the stroke tip.
  Vec2 position{0};
  Vec2 velocity{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;
};

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 << "(" << v.x << ", " << v.y << ")";
}

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 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 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 std::ostream &operator<<(std::ostream &s, Duration duration) {
  return s << duration.Value();
}

inline std::ostream &operator<<(std::ostream &s, Time time) {
  return s << time.Value();
}

inline std::ostream &operator<<(std::ostream &s, Input::EventType event_type) {
  switch (event_type) {
    case Input::EventType::kDown:
      return s << "Down";
    case Input::EventType::kMove:
      return s << "Move";
    case Input::EventType::kUp:
      return s << "Up";
  }
  return s << "UnknownEventType<" << event_type << ">";
}

inline std::ostream &operator<<(std::ostream &s, const Input &input) {
  return s << "<Input: " << input.event_type << ", pos: " << input.position
           << ", time: " << input.time << ", pressure: " << input.pressure
           << ", tilt: " << input.tilt << ", orientation: " << input.orientation
           << ">";
}

inline std::ostream &operator<<(std::ostream &s, const Result &result) {
  return s << "<Result: pos: " << result.position
           << ", velocity: " << result.velocity << ", time: " << result.time
           << ", pressure: " << result.pressure << ", tilt: " << result.tilt
           << ", orientation: " << result.orientation << ">";
}

}  // namespace stroke_model
}  // namespace ink

#endif  // INK_STROKE_MODELER_TYPES_H_