MyScript Interactive Ink SDK  4.1.2
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 #include <iink/graphics/Rectangle.h>
8 
9 #include <cmath>
10 #include <stdexcept>
11 #include <iosfwd>
12 
13 namespace myscript {
14 namespace iink {
15 namespace graphics {
16 
30 struct Transform
31 {
36  double xx = 1.;
37 
42  double yx = 0.;
43 
48  double tx = 0.;
49 
54  double xy = 0.;
55 
60  double yy = 1.;
61 
66  double ty = 0.;
67 
71  Transform() =default;
72 
78  Transform(const Transform& other) =default;
79 
90  Transform(double xx, double yx, double tx, double xy, double yy, double ty) noexcept
91  : xx(xx)
92  , yx(yx)
93  , tx(tx)
94  , xy(xy)
95  , yy(yy)
96  , ty(ty)
97  {
98  }
99 
107  IINK_API bool isNear(const Transform& other) const noexcept;
108 
114  IINK_API bool isIdentity() const noexcept;
115 
120  IINK_API void invert();
121 
127  IINK_API void multiply(const Transform& other) noexcept;
128 
143  IINK_API void translate(double tx, double ty) noexcept;
144 
158  IINK_API void scale(double s) noexcept;
159 
174  IINK_API void scale(double sx, double sy) noexcept;
175 
189  IINK_API void rotate(double a);
190 
206  IINK_API void rotate(double a, double x0, double y0);
207 
222  IINK_API void rotate(double cosA, double sinA);
223 
240  IINK_API void rotate(double cosA, double sinA, double x0, double y0);
241 
249  Point apply(float x, float y) const noexcept
250  {
251  return Point((float)(x * xx + y * yx + tx), (float)(x * xy + y * yy + ty));
252  }
253 
259  void apply(Point& p) const noexcept
260  {
261  float x = (float)(p.x * xx + p.y * yx + tx);
262  float y = (float)(p.x * xy + p.y * yy + ty);
263  p.x = x;
264  p.y = y;
265  }
266 
273  void apply(Point* points, int count) const noexcept
274  {
275  apply(points, 0, count);
276  }
277 
285  void apply(Point* points, int offset, int count) const noexcept
286  {
287  for (int i = 0; i < count; ++i)
288  apply(points[offset + i]);
289  }
290 
299  void apply(Rectangle& rect) const
300  {
301  if (yx != 0 || xy != 0 || xx <= 0 || yy <= 0)
302  {
303  throw std::runtime_error("only positive scales and translations are allowed on rectangles");
304  }
305  graphics::Point bottomRight(rect.x + rect.width, rect.y + rect.height);
306  apply(bottomRight);
307  graphics::Point topLeft = apply(rect.x, rect.y);
308  rect.x = topLeft.x;
309  rect.y = topLeft.y;
310  rect.width = bottomRight.x - topLeft.x;
311  rect.height = bottomRight.y - topLeft.y;
312  }
313 
324  IINK_API static Transform getTransform(const Rectangle& fromRect, const Rectangle& toRect);
325 
333  bool operator==(const Transform& other) const noexcept
334  {
335  return xx == other.xx && yx == other.yx && tx == other.tx && xy == other.xy && yy == other.yy && ty == other.ty;
336  }
337 
345  bool operator!=(const Transform& other) const noexcept
346  {
347  return !this->operator==(other);
348  }
349 
358  IINK_API friend std::ostream& operator<<(std::ostream& os, const Transform& tr);
359 };
360 
361 }
362 }
363 }
Definition: Configuration.h:10
Definition: Point.h:18
float x
Definition: Point.h:19
float y
Definition: Point.h:20
Definition: Rectangle.h:17
float x
Definition: Rectangle.h:18
float y
Definition: Rectangle.h:19
float height
Definition: Rectangle.h:21
float width
Definition: Rectangle.h:20
Definition: Transform.h:31
void multiply(const Transform &other) noexcept
double xx
Definition: Transform.h:36
void scale(double s) noexcept
bool operator==(const Transform &other) const noexcept
Definition: Transform.h:333
Point apply(float x, float y) const noexcept
Definition: Transform.h:249
double yy
Definition: Transform.h:60
double xy
Definition: Transform.h:54
double yx
Definition: Transform.h:42
void apply(Rectangle &rect) const
Definition: Transform.h:299
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:90
void apply(Point *points, int offset, int count) const noexcept
Definition: Transform.h:285
double ty
Definition: Transform.h:66
void apply(Point &p) const noexcept
Definition: Transform.h:259
void translate(double tx, double ty) noexcept
double tx
Definition: Transform.h:48
friend std::ostream & operator<<(std::ostream &os, const Transform &tr)
bool isNear(const Transform &other) const noexcept
void apply(Point *points, int count) const noexcept
Definition: Transform.h:273
static Transform getTransform(const Rectangle &fromRect, const Rectangle &toRect)
bool operator!=(const Transform &other) const noexcept
Definition: Transform.h:345