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
is_within(double a,double thresh,double b)25 inline bool is_within(double a, double thresh, double b)
26 {
27     return fabs(a - b) < thresh;
28 }
29 
rot_matrices_equal(const double * const mat0,const double * const mat1)30 inline bool rot_matrices_equal(const double *const mat0, const double *const mat1)
31 {
32     return is_within(mat0[0], .1, mat1[0]) && is_within(mat0[1], .1, mat1[1]) && is_within(mat0[2], .1, mat1[2]) && is_within(mat0[3], .1, 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
isMatRotOrSkew(const double * const mat)38 inline bool isMatRotOrSkew(const double *const mat)
39 {
40     return mat[0] < 0 || !is_within(mat[1], .1, 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.
normalizeRotMat(double * mat)46 inline void normalizeRotMat(double *mat)
47 {
48     double scale = fabs(mat[0] + mat[1]);
49     if (!scale)
50         return;
51     for (int i = 0; i < 4; i++)
52         mat[i] /= scale;
53 }
54 
55 #endif /* HTMLUTILS_H_ */
56