geometry.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. #ifndef MUPDF_FITZ_MATH_H
  2. #define MUPDF_FITZ_MATH_H
  3. #include "mupdf/fitz/system.h"
  4. /*
  5. Multiply scaled two integers in the 0..255 range
  6. */
  7. static inline int fz_mul255(int a, int b)
  8. {
  9. /* see Jim Blinn's book "Dirty Pixels" for how this works */
  10. int x = a * b + 128;
  11. x += x >> 8;
  12. return x >> 8;
  13. }
  14. /*
  15. Expand a value A from the 0...255 range to the 0..256 range
  16. */
  17. #define FZ_EXPAND(A) ((A)+((A)>>7))
  18. /*
  19. Combine values A (in any range) and B (in the 0..256 range),
  20. to give a single value in the same range as A was.
  21. */
  22. #define FZ_COMBINE(A,B) (((A)*(B))>>8)
  23. /*
  24. Combine values A and C (in the same (any) range) and B and D (in
  25. the 0..256 range), to give a single value in the same range as A
  26. and C were.
  27. */
  28. #define FZ_COMBINE2(A,B,C,D) (((A) * (B) + (C) * (D))>>8)
  29. /*
  30. Blend SRC and DST (in the same range) together according to
  31. AMOUNT (in the 0...256 range).
  32. */
  33. #define FZ_BLEND(SRC, DST, AMOUNT) ((((SRC)-(DST))*(AMOUNT) + ((DST)<<8))>>8)
  34. /*
  35. Range checking atof
  36. */
  37. float fz_atof(const char *s);
  38. /*
  39. atoi that copes with NULL
  40. */
  41. int fz_atoi(const char *s);
  42. fz_off_t fz_atoo(const char *s);
  43. /*
  44. Some standard math functions, done as static inlines for speed.
  45. People with compilers that do not adequately implement inlines may
  46. like to reimplement these using macros.
  47. */
  48. static inline float fz_abs(float f)
  49. {
  50. return (f < 0 ? -f : f);
  51. }
  52. static inline int fz_absi(int i)
  53. {
  54. return (i < 0 ? -i : i);
  55. }
  56. static inline float fz_min(float a, float b)
  57. {
  58. return (a < b ? a : b);
  59. }
  60. static inline int fz_mini(int a, int b)
  61. {
  62. return (a < b ? a : b);
  63. }
  64. static inline size_t fz_minz(size_t a, size_t b)
  65. {
  66. return (a < b ? a : b);
  67. }
  68. static inline float fz_max(float a, float b)
  69. {
  70. return (a > b ? a : b);
  71. }
  72. static inline int fz_maxi(int a, int b)
  73. {
  74. return (a > b ? a : b);
  75. }
  76. static inline fz_off_t fz_maxo(fz_off_t a, fz_off_t b)
  77. {
  78. return (a > b ? a : b);
  79. }
  80. static inline float fz_clamp(float f, float min, float max)
  81. {
  82. return (f > min ? (f < max ? f : max) : min);
  83. }
  84. static inline int fz_clampi(int i, int min, int max)
  85. {
  86. return (i > min ? (i < max ? i : max) : min);
  87. }
  88. static inline double fz_clampd(double d, double min, double max)
  89. {
  90. return (d > min ? (d < max ? d : max) : min);
  91. }
  92. static inline void *fz_clampp(void *p, void *min, void *max)
  93. {
  94. return (p > min ? (p < max ? p : max) : min);
  95. }
  96. #define DIV_BY_ZERO(a, b, min, max) (((a) < 0) ^ ((b) < 0) ? (min) : (max))
  97. /*
  98. fz_point is a point in a two-dimensional space.
  99. */
  100. typedef struct fz_point_s fz_point;
  101. struct fz_point_s
  102. {
  103. float x, y;
  104. };
  105. /*
  106. fz_rect is a rectangle represented by two diagonally opposite
  107. corners at arbitrary coordinates.
  108. Rectangles are always axis-aligned with the X- and Y- axes.
  109. The relationship between the coordinates are that x0 <= x1 and
  110. y0 <= y1 in all cases except for infinite rectangles. The area
  111. of a rectangle is defined as (x1 - x0) * (y1 - y0). If either
  112. x0 > x1 or y0 > y1 is true for a given rectangle then it is
  113. defined to be infinite.
  114. To check for empty or infinite rectangles use fz_is_empty_rect
  115. and fz_is_infinite_rect.
  116. x0, y0: The top left corner.
  117. x1, y1: The bottom right corner.
  118. */
  119. typedef struct fz_rect_s fz_rect;
  120. struct fz_rect_s
  121. {
  122. float x0, y0;
  123. float x1, y1;
  124. };
  125. /*
  126. fz_rect_min: get the minimum point from a rectangle as a fz_point.
  127. */
  128. static inline fz_point *fz_rect_min(fz_rect *f)
  129. {
  130. return (fz_point *)&f->x0;
  131. }
  132. /*
  133. fz_rect_max: get the maximum point from a rectangle as a fz_point.
  134. */
  135. static inline fz_point *fz_rect_max(fz_rect *f)
  136. {
  137. return (fz_point *)&f->x1;
  138. }
  139. /*
  140. fz_irect is a rectangle using integers instead of floats.
  141. It's used in the draw device and for pixmap dimensions.
  142. */
  143. typedef struct fz_irect_s fz_irect;
  144. struct fz_irect_s
  145. {
  146. int x0, y0;
  147. int x1, y1;
  148. };
  149. /*
  150. A rectangle with sides of length one.
  151. The bottom left corner is at (0, 0) and the top right corner
  152. is at (1, 1).
  153. */
  154. extern const fz_rect fz_unit_rect;
  155. /*
  156. An empty rectangle with an area equal to zero.
  157. Both the top left and bottom right corner are at (0, 0).
  158. */
  159. extern const fz_rect fz_empty_rect;
  160. extern const fz_irect fz_empty_irect;
  161. /*
  162. An infinite rectangle with negative area.
  163. The corner (x0, y0) is at (1, 1) while the corner (x1, y1) is
  164. at (-1, -1).
  165. */
  166. extern const fz_rect fz_infinite_rect;
  167. extern const fz_irect fz_infinite_irect;
  168. /*
  169. fz_is_empty_rect: Check if rectangle is empty.
  170. An empty rectangle is defined as one whose area is zero.
  171. */
  172. static inline int
  173. fz_is_empty_rect(const fz_rect *r)
  174. {
  175. return ((r)->x0 == (r)->x1 || (r)->y0 == (r)->y1);
  176. }
  177. static inline int
  178. fz_is_empty_irect(const fz_irect *r)
  179. {
  180. return ((r)->x0 == (r)->x1 || (r)->y0 == (r)->y1);
  181. }
  182. /*
  183. fz_is_infinite_rect: Check if rectangle is infinite.
  184. An infinite rectangle is defined as one where either of the
  185. two relationships between corner coordinates are not true.
  186. */
  187. static inline int
  188. fz_is_infinite_rect(const fz_rect *r)
  189. {
  190. return ((r)->x0 > (r)->x1 || (r)->y0 > (r)->y1);
  191. }
  192. /*
  193. fz_is_infinite_irect: Check if an integer rectangle
  194. is infinite.
  195. An infinite rectangle is defined as one where either of the
  196. two relationships between corner coordinates are not true.
  197. */
  198. static inline int
  199. fz_is_infinite_irect(const fz_irect *r)
  200. {
  201. return ((r)->x0 > (r)->x1 || (r)->y0 > (r)->y1);
  202. }
  203. /*
  204. fz_matrix is a row-major 3x3 matrix used for representing
  205. transformations of coordinates throughout MuPDF.
  206. Since all points reside in a two-dimensional space, one vector
  207. is always a constant unit vector; hence only some elements may
  208. vary in a matrix. Below is how the elements map between
  209. different representations.
  210. / a b 0 \
  211. | c d 0 | normally represented as [ a b c d e f ].
  212. \ e f 1 /
  213. */
  214. typedef struct fz_matrix_s fz_matrix;
  215. struct fz_matrix_s
  216. {
  217. float a, b, c, d, e, f;
  218. };
  219. /*
  220. fz_identity: Identity transform matrix.
  221. */
  222. extern const fz_matrix fz_identity;
  223. static inline fz_matrix *fz_copy_matrix(fz_matrix *restrict m, const fz_matrix *restrict s)
  224. {
  225. *m = *s;
  226. return m;
  227. }
  228. /*
  229. fz_concat: Multiply two matrices.
  230. The order of the two matrices are important since matrix
  231. multiplication is not commutative.
  232. Returns result.
  233. Does not throw exceptions.
  234. */
  235. fz_matrix *fz_concat(fz_matrix *result, const fz_matrix *left, const fz_matrix *right);
  236. /*
  237. fz_scale: Create a scaling matrix.
  238. The returned matrix is of the form [ sx 0 0 sy 0 0 ].
  239. m: Pointer to the matrix to populate
  240. sx, sy: Scaling factors along the X- and Y-axes. A scaling
  241. factor of 1.0 will not cause any scaling along the relevant
  242. axis.
  243. Returns m.
  244. Does not throw exceptions.
  245. */
  246. fz_matrix *fz_scale(fz_matrix *m, float sx, float sy);
  247. /*
  248. fz_pre_scale: Scale a matrix by premultiplication.
  249. m: Pointer to the matrix to scale
  250. sx, sy: Scaling factors along the X- and Y-axes. A scaling
  251. factor of 1.0 will not cause any scaling along the relevant
  252. axis.
  253. Returns m (updated).
  254. Does not throw exceptions.
  255. */
  256. fz_matrix *fz_pre_scale(fz_matrix *m, float sx, float sy);
  257. /*
  258. fz_post_scale: Scale a matrix by postmultiplication.
  259. m: Pointer to the matrix to scale
  260. sx, sy: Scaling factors along the X- and Y-axes. A scaling
  261. factor of 1.0 will not cause any scaling along the relevant
  262. axis.
  263. Returns m (updated).
  264. Does not throw exceptions.
  265. */
  266. fz_matrix *fz_post_scale(fz_matrix *m, float sx, float sy);
  267. /*
  268. fz_shear: Create a shearing matrix.
  269. The returned matrix is of the form [ 1 sy sx 1 0 0 ].
  270. m: pointer to place to store returned matrix
  271. sx, sy: Shearing factors. A shearing factor of 0.0 will not
  272. cause any shearing along the relevant axis.
  273. Returns m.
  274. Does not throw exceptions.
  275. */
  276. fz_matrix *fz_shear(fz_matrix *m, float sx, float sy);
  277. /*
  278. fz_pre_shear: Premultiply a matrix with a shearing matrix.
  279. The shearing matrix is of the form [ 1 sy sx 1 0 0 ].
  280. m: pointer to matrix to premultiply
  281. sx, sy: Shearing factors. A shearing factor of 0.0 will not
  282. cause any shearing along the relevant axis.
  283. Returns m (updated).
  284. Does not throw exceptions.
  285. */
  286. fz_matrix *fz_pre_shear(fz_matrix *m, float sx, float sy);
  287. /*
  288. fz_rotate: Create a rotation matrix.
  289. The returned matrix is of the form
  290. [ cos(deg) sin(deg) -sin(deg) cos(deg) 0 0 ].
  291. m: Pointer to place to store matrix
  292. degrees: Degrees of counter clockwise rotation. Values less
  293. than zero and greater than 360 are handled as expected.
  294. Returns m.
  295. Does not throw exceptions.
  296. */
  297. fz_matrix *fz_rotate(fz_matrix *m, float degrees);
  298. /*
  299. fz_pre_rotate: Rotate a transformation by premultiplying.
  300. The premultiplied matrix is of the form
  301. [ cos(deg) sin(deg) -sin(deg) cos(deg) 0 0 ].
  302. m: Pointer to matrix to premultiply.
  303. degrees: Degrees of counter clockwise rotation. Values less
  304. than zero and greater than 360 are handled as expected.
  305. Returns m (updated).
  306. Does not throw exceptions.
  307. */
  308. fz_matrix *fz_pre_rotate(fz_matrix *m, float degrees);
  309. /*
  310. fz_translate: Create a translation matrix.
  311. The returned matrix is of the form [ 1 0 0 1 tx ty ].
  312. m: A place to store the created matrix.
  313. tx, ty: Translation distances along the X- and Y-axes. A
  314. translation of 0 will not cause any translation along the
  315. relevant axis.
  316. Returns m.
  317. Does not throw exceptions.
  318. */
  319. fz_matrix *fz_translate(fz_matrix *m, float tx, float ty);
  320. /*
  321. fz_pre_translate: Translate a matrix by premultiplication.
  322. m: The matrix to translate
  323. tx, ty: Translation distances along the X- and Y-axes. A
  324. translation of 0 will not cause any translation along the
  325. relevant axis.
  326. Returns m.
  327. Does not throw exceptions.
  328. */
  329. fz_matrix *fz_pre_translate(fz_matrix *m, float tx, float ty);
  330. /*
  331. fz_invert_matrix: Create an inverse matrix.
  332. inverse: Place to store inverse matrix.
  333. matrix: Matrix to invert. A degenerate matrix, where the
  334. determinant is equal to zero, can not be inverted and the
  335. original matrix is returned instead.
  336. Returns inverse.
  337. Does not throw exceptions.
  338. */
  339. fz_matrix *fz_invert_matrix(fz_matrix *inverse, const fz_matrix *matrix);
  340. /*
  341. fz_try_invert_matrix: Attempt to create an inverse matrix.
  342. inverse: Place to store inverse matrix.
  343. matrix: Matrix to invert. A degenerate matrix, where the
  344. determinant is equal to zero, can not be inverted.
  345. Returns 1 if matrix is degenerate (singular), or 0 otherwise.
  346. Does not throw exceptions.
  347. */
  348. int fz_try_invert_matrix(fz_matrix *inverse, const fz_matrix *matrix);
  349. /*
  350. fz_is_rectilinear: Check if a transformation is rectilinear.
  351. Rectilinear means that no shearing is present and that any
  352. rotations present are a multiple of 90 degrees. Usually this
  353. is used to make sure that axis-aligned rectangles before the
  354. transformation are still axis-aligned rectangles afterwards.
  355. Does not throw exceptions.
  356. */
  357. int fz_is_rectilinear(const fz_matrix *m);
  358. /*
  359. fz_matrix_expansion: Calculate average scaling factor of matrix.
  360. */
  361. float fz_matrix_expansion(const fz_matrix *m); /* sumatrapdf */
  362. /*
  363. fz_intersect_rect: Compute intersection of two rectangles.
  364. Given two rectangles, update the first to be the smallest
  365. axis-aligned rectangle that covers the area covered by both
  366. given rectangles. If either rectangle is empty then the
  367. intersection is also empty. If either rectangle is infinite
  368. then the intersection is simply the non-infinite rectangle.
  369. Should both rectangles be infinite, then the intersection is
  370. also infinite.
  371. Does not throw exceptions.
  372. */
  373. fz_rect *fz_intersect_rect(fz_rect *restrict a, const fz_rect *restrict b);
  374. /*
  375. fz_intersect_irect: Compute intersection of two bounding boxes.
  376. Similar to fz_intersect_rect but operates on two bounding
  377. boxes instead of two rectangles.
  378. Does not throw exceptions.
  379. */
  380. fz_irect *fz_intersect_irect(fz_irect *restrict a, const fz_irect *restrict b);
  381. /*
  382. fz_union_rect: Compute union of two rectangles.
  383. Given two rectangles, update the first to be the smallest
  384. axis-aligned rectangle that encompasses both given rectangles.
  385. If either rectangle is infinite then the union is also infinite.
  386. If either rectangle is empty then the union is simply the
  387. non-empty rectangle. Should both rectangles be empty, then the
  388. union is also empty.
  389. Does not throw exceptions.
  390. */
  391. fz_rect *fz_union_rect(fz_rect *restrict a, const fz_rect *restrict b);
  392. /*
  393. fz_irect_from_rect: Convert a rect into the minimal bounding box
  394. that covers the rectangle.
  395. bbox: Place to store the returned bbox.
  396. rect: The rectangle to convert to a bbox.
  397. Coordinates in a bounding box are integers, so rounding of the
  398. rects coordinates takes place. The top left corner is rounded
  399. upwards and left while the bottom right corner is rounded
  400. downwards and to the right.
  401. Returns bbox (updated).
  402. Does not throw exceptions.
  403. */
  404. fz_irect *fz_irect_from_rect(fz_irect *restrict bbox, const fz_rect *restrict rect);
  405. /*
  406. fz_round_rect: Round rectangle coordinates.
  407. Coordinates in a bounding box are integers, so rounding of the
  408. rects coordinates takes place. The top left corner is rounded
  409. upwards and left while the bottom right corner is rounded
  410. downwards and to the right.
  411. This differs from fz_irect_from_rect, in that fz_irect_from_rect
  412. slavishly follows the numbers (i.e any slight over/under calculations
  413. can cause whole extra pixels to be added). fz_round_rect
  414. allows for a small amount of rounding error when calculating
  415. the bbox.
  416. Does not throw exceptions.
  417. */
  418. fz_irect *fz_round_rect(fz_irect *restrict bbox, const fz_rect *restrict rect);
  419. /*
  420. fz_rect_from_irect: Convert a bbox into a rect.
  421. For our purposes, a rect can represent all the values we meet in
  422. a bbox, so nothing can go wrong.
  423. rect: A place to store the generated rectangle.
  424. bbox: The bbox to convert.
  425. Returns rect (updated).
  426. Does not throw exceptions.
  427. */
  428. fz_rect *fz_rect_from_irect(fz_rect *restrict rect, const fz_irect *restrict bbox);
  429. /*
  430. fz_expand_rect: Expand a bbox by a given amount in all directions.
  431. Does not throw exceptions.
  432. */
  433. fz_rect *fz_expand_rect(fz_rect *b, float expand);
  434. /*
  435. fz_include_point_in_rect: Expand a bbox to include a given point.
  436. To create a rectangle that encompasses a sequence of points, the
  437. rectangle must first be set to be the empty rectangle at one of
  438. the points before including the others.
  439. */
  440. fz_rect *fz_include_point_in_rect(fz_rect *r, const fz_point *p);
  441. /*
  442. fz_translate_irect: Translate bounding box.
  443. Translate a bbox by a given x and y offset. Allows for overflow.
  444. Does not throw exceptions.
  445. */
  446. fz_irect *fz_translate_irect(fz_irect *a, int xoff, int yoff);
  447. /*
  448. fz_contains_rect: Test rectangle inclusion.
  449. Return true if a entirely contains b.
  450. Does not throw exceptions.
  451. */
  452. int fz_contains_rect(const fz_rect *a, const fz_rect *b);
  453. /*
  454. fz_transform_point: Apply a transformation to a point.
  455. transform: Transformation matrix to apply. See fz_concat,
  456. fz_scale, fz_rotate and fz_translate for how to create a
  457. matrix.
  458. point: Pointer to point to update.
  459. Returns transform (unchanged).
  460. Does not throw exceptions.
  461. */
  462. fz_point *fz_transform_point(fz_point *restrict point, const fz_matrix *restrict transform);
  463. fz_point *fz_transform_point_xy(fz_point *restrict point, const fz_matrix *restrict transform, float x, float y);
  464. /*
  465. fz_transform_vector: Apply a transformation to a vector.
  466. transform: Transformation matrix to apply. See fz_concat,
  467. fz_scale and fz_rotate for how to create a matrix. Any
  468. translation will be ignored.
  469. vector: Pointer to vector to update.
  470. Does not throw exceptions.
  471. */
  472. fz_point *fz_transform_vector(fz_point *restrict vector, const fz_matrix *restrict transform);
  473. /*
  474. fz_transform_rect: Apply a transform to a rectangle.
  475. After the four corner points of the axis-aligned rectangle
  476. have been transformed it may not longer be axis-aligned. So a
  477. new axis-aligned rectangle is created covering at least the
  478. area of the transformed rectangle.
  479. transform: Transformation matrix to apply. See fz_concat,
  480. fz_scale and fz_rotate for how to create a matrix.
  481. rect: Rectangle to be transformed. The two special cases
  482. fz_empty_rect and fz_infinite_rect, may be used but are
  483. returned unchanged as expected.
  484. Does not throw exceptions.
  485. */
  486. fz_rect *fz_transform_rect(fz_rect *restrict rect, const fz_matrix *restrict transform);
  487. /*
  488. fz_normalize_vector: Normalize a vector to length one.
  489. */
  490. void fz_normalize_vector(fz_point *p);
  491. void fz_gridfit_matrix(int as_tiled, fz_matrix *m);
  492. float fz_matrix_max_expansion(const fz_matrix *m);
  493. #endif