Geometry.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include "mupdf/fitz.h"
  2. #include "mupdf/pdf.h"
  3. #include <cfloat>
  4. #include <math.h>
  5. #ifndef __GEOMETRY
  6. #define __GEOMETRY
  7. #pragma once
  8. using namespace System;
  9. namespace MuPDF {
  10. public value struct Point : IEquatable<Point> {
  11. public:
  12. initonly float X, Y;
  13. Point(float x, float y) { X = x; Y = y; }
  14. virtual int GetHashCode() override {
  15. return (static_cast<int>(X) << 16) | static_cast<int>(Y) ^ (static_cast<int>(X) >> 16);
  16. }
  17. virtual bool Equals(Object^ other) override {
  18. Point p;
  19. return other && other->GetType() == Point::typeid
  20. && (p = safe_cast<Point>(other)).X == X
  21. && Y == p.Y;
  22. }
  23. virtual bool Equals(Point other) {
  24. return *this == other;
  25. }
  26. static bool operator == (Point a, Point b) {
  27. return a.X == b.X && a.Y == b.Y;
  28. }
  29. static bool operator != (Point a, Point b) {
  30. return a.X != b.X || a.Y != b.Y;
  31. }
  32. static operator Point(fz_point p) {
  33. return Point(p.x, p.y);
  34. }
  35. virtual String^ ToString() override {
  36. return String::Concat(X, ",", Y);
  37. }
  38. };
  39. value struct BBox;
  40. value struct Matrix;
  41. public value struct Box {
  42. public:
  43. initonly float X0, Y0, X1, Y1;
  44. static initonly Box Unit = { 0, 0, 1, 1 };
  45. static initonly Box Invalid = { 0, 0, -1, -1 };
  46. Box(float x0, float y0, float x1, float y1) : X0(x0), X1(x1), Y0(y0), Y1(y1) {};
  47. property bool IsEmpty {
  48. bool get() {
  49. return X0 == X1 || Y0 == Y1;
  50. }
  51. }
  52. property float Width {
  53. float get() {
  54. return X1 > X0 ? X1 - X0 : 0;
  55. }
  56. }
  57. property float Height {
  58. float get() {
  59. return Y1 > Y0 ? Y1 - Y0 : 0;
  60. }
  61. }
  62. property float Top { float get() { return Y0; } }
  63. property float Left { float get() { return X0; } }
  64. property float Bottom { float get() { return Y1; } }
  65. property float Right { float get() { return X1; } }
  66. property bool IsInfinite {
  67. bool get() {
  68. return X0 == FZ_MIN_INF_RECT && X1 == FZ_MAX_INF_RECT &&
  69. Y0 == FZ_MIN_INF_RECT && Y1 == FZ_MAX_INF_RECT;
  70. }
  71. }
  72. property bool IsValid {
  73. bool get() { return X0 <= X1 && Y0 <= Y1; }
  74. }
  75. property float Area {
  76. float get() { return IsValid ? (X1 - X0) * (Y1 - Y0) : 0; }
  77. }
  78. bool Contains(Point p) {
  79. return p.X >= X0 && p.X < X1 && p.Y >= Y0 && p.Y < Y1;
  80. }
  81. Box Union(Box other) {
  82. return *this | other;
  83. }
  84. Box Intersect(Box other) {
  85. return *this & other;
  86. }
  87. Box Translate(float offsetX, float offsetY) {
  88. if (IsInfinite) return *this;
  89. return { X0 + offsetX, Y0 + offsetY, X1 + offsetX, Y1 + offsetY };
  90. }
  91. Box Transform(Matrix matrix);
  92. BBox Round();
  93. virtual String^ ToString() override {
  94. return String::Concat("(", X0, ",", Y0, ")-(", X1, ",", Y1, ")");
  95. }
  96. static explicit operator Box(BBox box);
  97. static operator Box(fz_rect rect) {
  98. return Box (rect.x0, rect.y0, rect.x1, rect.y1);
  99. }
  100. static Box operator & (Box a, Box b);
  101. static Box operator | (Box a, Box b);
  102. static operator fz_rect (Box b) {
  103. return { b.X0, b.Y0, b.X1, b.Y1 };
  104. }
  105. };
  106. public value struct BBox {
  107. public:
  108. initonly int X0, Y0, X1, Y1;
  109. static initonly BBox Unit = { 0, 0, 1, 1 };
  110. static initonly BBox Invalid = { 0, 0, -1, -1 };
  111. BBox(int x0, int y0, int x1, int y1) : X0(x0), X1(x1), Y0(y0), Y1(y1) {};
  112. property int Width {
  113. int get() {
  114. return abs(X1 - X0);
  115. }
  116. }
  117. property int Height {
  118. int get() {
  119. return abs(Y1 - Y0);
  120. }
  121. }
  122. property int Top { int get() { return Y0; } }
  123. property int Left { int get() { return X0; } }
  124. property int Bottom { int get() { return Y1; } }
  125. property int Right { int get() { return X1; } }
  126. property bool IsInfinite {
  127. bool get() {
  128. return X0 == FZ_MIN_INF_RECT && X1 == FZ_MAX_INF_RECT &&
  129. Y0 == FZ_MIN_INF_RECT && Y1 == FZ_MAX_INF_RECT;
  130. }
  131. }
  132. property bool IsValid {
  133. bool get() { return X0 <= X1 && Y0 <= Y1; }
  134. }
  135. bool ContainsPoint(int x, int y) {
  136. return x >= X0 && x < X1 && y >= Y0 && y < Y1;
  137. }
  138. BBox Intersect(BBox other) {
  139. return *this & other;
  140. }
  141. BBox Union(BBox other) {
  142. return *this | other;
  143. }
  144. virtual String^ ToString() override {
  145. return String::Concat("(", X0, ",", Y0, ")-(", X1, ",", Y1, ")");
  146. }
  147. static explicit operator BBox(Box box);
  148. static BBox operator & (BBox a, BBox b);
  149. static BBox operator | (BBox a, BBox b);
  150. static operator BBox(fz_irect rect) {
  151. return BBox(rect.x0, rect.y0, rect.x1, rect.y1);
  152. }
  153. static operator fz_irect (BBox b) {
  154. return { b.X0, b.Y0, b.X1, b.Y1 };
  155. }
  156. };
  157. public value struct Quad {
  158. public:
  159. initonly Point UpperLeft, UpperRight, LowerLeft, LowerRight;
  160. Quad(Point ul, Point ur, Point ll, Point lr) : UpperLeft(ul), UpperRight(ur), LowerLeft(ll), LowerRight(lr) {};
  161. bool Contains(Point p) {
  162. return
  163. IsPointInsideTriangle(p, UpperLeft, UpperRight, LowerRight) ||
  164. IsPointInsideTriangle(p, UpperLeft, LowerRight, LowerLeft);
  165. }
  166. bool Contains(Quad other) {
  167. return
  168. Contains(other.UpperLeft) &&
  169. Contains(other.UpperRight) &&
  170. Contains(other.LowerLeft) &&
  171. Contains(other.LowerRight);
  172. }
  173. Quad Union(Quad other);
  174. Box ToBox();
  175. virtual String^ ToString() override {
  176. return String::Concat("{(", UpperLeft.X, ",", UpperLeft.Y, "),(", UpperRight.X, ",", UpperRight.Y, "),(", LowerLeft.X, ",", LowerLeft.Y, "),(", LowerRight.X, ",", LowerRight.Y, ")}");
  177. }
  178. static operator Quad(fz_quad quad) {
  179. return Quad(quad.ul, quad.ur, quad.ll, quad.lr);
  180. }
  181. static operator fz_quad(Quad quad) {
  182. pin_ptr<Quad> p = &quad;
  183. return *(fz_quad*)p;
  184. }
  185. static operator Quad(fz_rect rect) {
  186. return Quad(Point(rect.x0, rect.y0), Point(rect.x1, rect.y0), Point(rect.x0, rect.y1), Point(rect.x1, rect.y1));
  187. }
  188. static operator Quad(Box rect) {
  189. return Quad(Point(rect.X0, rect.Y0), Point(rect.X1, rect.Y0), Point(rect.X0, rect.Y1), Point(rect.X1, rect.Y1));
  190. }
  191. static bool IsPointInsideTriangle(Point p, Point a, Point b, Point c);
  192. };
  193. public value struct Matrix {
  194. public:
  195. initonly float A, B, C, D, E, F;
  196. static initonly Matrix Identity = Matrix(1, 0, 0, 1, 0, 0);
  197. static initonly Matrix VerticalFlip = Matrix(1, 0, 0, -1, 0, 0);
  198. static initonly Matrix HorizontalFlip = Matrix(-1, 0, 0, 1, 0, 0);
  199. Matrix(float a, float b, float c, float d, float e, float f) : A(a), B(b), C(c), D(d), E(e), F(f) {};
  200. Matrix Concat(Matrix value);
  201. Matrix PreScale(float sx, float sy) {
  202. return Matrix(A * sx, B * sx, C * sy, D * sy, 0, 0);
  203. }
  204. Matrix PostScale(float sx, float sy) {
  205. return Matrix(A * sx, B * sx, C * sy, D * sy, E * sx, F * sy);
  206. }
  207. Matrix RotateTo(float theta);
  208. Matrix ShearTo(float h, float v);
  209. Matrix TranslateTo(float tx, float ty) {
  210. return Matrix(A, B, C, D, tx * A + ty * C + E, tx * B + ty * D + F);
  211. }
  212. static Matrix Rotate(float theta);
  213. static Matrix Scale(float x, float y) {
  214. return Matrix(x, 0, 0, y, 0, 0);
  215. }
  216. static Matrix Shear(float h, float v) {
  217. return Matrix(1, v, h, 1, 0, 0);
  218. }
  219. static Matrix Translate(float tx, float ty) {
  220. return Matrix(1, 0, 0, 1, tx, ty);
  221. }
  222. virtual String^ ToString() override {
  223. return String::Concat("[", A, ",", B, ",", C, ",", D, ",", E, ",", F, "]");
  224. }
  225. static operator fz_matrix(Matrix matrix) {
  226. pin_ptr<Matrix> p = &matrix;
  227. return *(fz_matrix*)p;
  228. //return { matrix.A,matrix.B,matrix.C,matrix.D,matrix.E,matrix.F };
  229. }
  230. };
  231. };
  232. #endif // !__GEOMETRY