MyScript Interactive Ink SDK  3.0.1
Transform.h
1 // Copyright @ MyScript. All rights reserved.
2 
3 #pragma once
4 
5 #include <iink/Engine.h>
6 #include <iink/graphics/Point.h>
7 
8 #include <cmath>
9 #include <stdexcept>
10 #include <iostream>
11 
12 namespace myscript {
13 namespace iink {
14 namespace graphics {
15 
29 struct Transform
30 {
35  double xx = 1.;
36 
41  double yx = 0.;
42 
47  double tx = 0.;
48 
53  double xy = 0.;
54 
59  double yy = 1.;
60 
65  double ty = 0.;
66 
70  Transform() =default;
71 
77  Transform(const Transform& other) =default;
78 
89  Transform(double xx, double yx, double tx, double xy, double yy, double ty) noexcept
90  : xx(xx)
91  , yx(yx)
92  , tx(tx)
93  , xy(xy)
94  , yy(yy)
95  , ty(ty)
96  {
97  }
98 
106  IINK_API bool isNear(const Transform& other) const noexcept;
107 
113  IINK_API bool isIdentity() const noexcept;
114 
119  IINK_API void invert();
120 
126  IINK_API void multiply(const Transform& other) noexcept;
127 
142  IINK_API void translate(double tx, double ty) noexcept;
143 
157  IINK_API void scale(double s) noexcept;
158 
173  IINK_API void scale(double sx, double sy) noexcept;
174 
188  IINK_API void rotate(double a);
189 
205  IINK_API void rotate(double a, double x0, double y0);
206 
221  IINK_API void rotate(double cosA, double sinA);
222 
239  IINK_API void rotate(double cosA, double sinA, double x0, double y0);
240 
248  Point apply(float x, float y) const noexcept
249  {
250  return Point((float)(x * xx + y * yx + tx), (float)(x * xy + y * yy + ty));
251  }
252 
258  void apply(Point& p) const noexcept
259  {
260  float x = (float)(p.x * xx + p.y * yx + tx);
261  float y = (float)(p.x * xy + p.y * yy + ty);
262  p.x = x;
263  p.y = y;
264  }
265 
272  void apply(Point* points, int count) const noexcept
273  {
274  apply(points, 0, count);
275  }
276 
284  void apply(Point* points, int offset, int count) const noexcept
285  {
286  for (int i = 0; i < count; ++i)
287  apply(points[offset + i]);
288  }
289 
293  friend std::ostream& operator<<(std::ostream& os, const Transform& tr);
294 
302  bool operator==(const Transform& other) const noexcept
303  {
304  return xx == other.xx && yx == other.yx && tx == other.tx && xy == other.xy && yy == other.yy && ty == other.ty;
305  }
306 
314  bool operator!=(const Transform& other) const noexcept
315  {
316  return !this->operator==(other);
317  }
318 };
319 
328 inline
329 std::ostream& operator<<(std::ostream& os, const Transform& tr)
330 {
331  os << "Transform([" << tr.xx << ", " << tr.yx << ", " << tr.tx << "], [" << tr.xy << ", " << tr.yy << ", " << tr.ty << "])";
332  return os;
333 }
334 
335 }
336 }
337 }
std::ostream & operator<<(std::ostream &os, const Color &c)
Definition: Color.h:134
Definition: Configuration.h:11
Definition: Point.h:15
Definition: Transform.h:30
void multiply(const Transform &other) noexcept
double xx
Definition: Transform.h:35
void scale(double s) noexcept
bool operator==(const Transform &other) const noexcept
Definition: Transform.h:302
Point apply(float x, float y) const noexcept
Definition: Transform.h:248
double yy
Definition: Transform.h:59
double xy
Definition: Transform.h:53
double yx
Definition: Transform.h:41
bool isIdentity() const noexcept
Transform(const Transform &other)=default
Transform(double xx, double yx, double tx, double xy, double yy, double ty) noexcept
Definition: Transform.h:89
void apply(Point *points, int offset, int count) const noexcept
Definition: Transform.h:284
double ty
Definition: Transform.h:65
void apply(Point &p) const noexcept
Definition: Transform.h:258
void translate(double tx, double ty) noexcept
double tx
Definition: Transform.h:47
friend std::ostream & operator<<(std::ostream &os, const Transform &tr)
Definition: Transform.h:329
bool isNear(const Transform &other) const noexcept
void apply(Point *points, int count) const noexcept
Definition: Transform.h:272
bool operator!=(const Transform &other) const noexcept
Definition: Transform.h:314