1 | // |
2 | // HtmlUtils.h |
3 | // |
4 | // Created on: Jun 8, 2011 |
5 | // Author: Joshua Richardson <jric@chegg.com> |
6 | // Copyright 2011 |
7 | // |
8 | // All changes made under the Poppler project to this file are licensed |
9 | // under GPL version 2 or later |
10 | // |
11 | // Copyright (C) 2011 Joshua Richardson <jric@chegg.com> |
12 | // |
13 | // To see a description of the changes please see the Changelog file that |
14 | // came with your tarball or type make ChangeLog if you are building from git |
15 | // |
16 | //======================================================================== |
17 | |
18 | #ifndef HTMLUTILS_H_ |
19 | #define HTMLUTILS_H_ |
20 | |
21 | #include <cmath> // fabs |
22 | |
23 | // Returns true iff the difference between a and b is less than the threshold |
24 | // We always use fuzzy math when comparing decimal numbers due to imprecision |
25 | inline bool is_within(double a, double thresh, double b) |
26 | { |
27 | return fabs(x: a - b) < thresh; |
28 | } |
29 | |
30 | inline bool rot_matrices_equal(const double *const mat0, const double *const mat1) |
31 | { |
32 | return is_within(a: mat0[0], thresh: .1, b: mat1[0]) && is_within(a: mat0[1], thresh: .1, b: mat1[1]) && is_within(a: mat0[2], thresh: .1, b: mat1[2]) && is_within(a: mat0[3], thresh: .1, b: mat1[3]); |
33 | } |
34 | |
35 | // rotation is (cos q, sin q, -sin q, cos q, 0, 0) |
36 | // sin q is zero iff there is no rotation, or 180 deg. rotation; |
37 | // for 180 rotation, cos q will be negative |
38 | inline bool isMatRotOrSkew(const double *const mat) |
39 | { |
40 | return mat[0] < 0 || !is_within(a: mat[1], thresh: .1, b: 0); |
41 | } |
42 | |
43 | // Alters the matrix so that it does not scale a vector's x component; |
44 | // If the matrix does not skew, then that will also normalize the y |
45 | // component, keeping any rotation, but removing scaling. |
46 | inline void normalizeRotMat(double *mat) |
47 | { |
48 | double scale = fabs(x: mat[0] + mat[1]); |
49 | if (!scale) { |
50 | return; |
51 | } |
52 | for (int i = 0; i < 4; i++) { |
53 | mat[i] /= scale; |
54 | } |
55 | } |
56 | |
57 | #endif /* HTMLUTILS_H_ */ |
58 | |