summaryrefslogtreecommitdiff
path: root/math.h
blob: 48519b46d0f3b64efb32b0615d2029fabaa39bf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
    Copyright (C) 1992-2009 Spotworks LLC
    Copyright (C) 2015 pucket contributors

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <math.h>
#include <assert.h>
#include <stdlib.h>

#include "build/config.h"
#include "vector.h"

#ifdef HAVE_AMDLIBM
#define REPLACE_WITH_AMDLIBM
#include <amdlibm.h>
#undef nearbyint
#undef floor
#endif

#define clamp(a,min,max) (a > max ? max : (a < min ? min : a))
#define EPS (1e-10)

/*	Apply affine coordinate transformation
 */
inline double2 apply_affine (const double2 in, const double2 matrix[3]) {
	return matrix[0] * in[0] + matrix[1] * in[1] + matrix[2];
}

/*	Create affine rotation matrix, angle in degree
 */
inline void rotate (const double angle, double2 matrix[3]) {
	double s, c;
	sincos (angle * 2.0 * M_PI / 360.0, &s, &c);
	matrix[0] = (double2) { c, s };
	matrix[1] = (double2) { -s, c };
	matrix[2] = (double2) { 0.0, 0.0 };
}

/*	Create affine translation matrix
 */
inline void translate (const double2 xy, double2 matrix[3]) {
	matrix[0] = (double2) { 1.0, 0.0 };
	matrix[1] = (double2) { 0.0, 1.0 };
	matrix[2] = xy;
}

/*	Create affine scaling matrix
 */
inline void scale (const double2 xy, double2 matrix[3]) {
	matrix[0] = (double2) { xy[0], 0.0 };
	matrix[1] = (double2) { 0.0, xy[1] };
	matrix[2] = (double2) { 0.0, 0.0 };
}

/*	Multiply two affine matrices a, b and store the result in c.
 *
 *	The last row of each matrix is assumed to be 0, 0, 1.
 */
inline void matrixmul (const double2 a[3], const double2 b[3], double2 c[3]) {
	c[0] = a[0] * b[0][0] + a[1] * b[0][1];
	c[1] = a[0] * b[1][0] + a[1] * b[1][1];
	c[2] = a[0] * b[2][0] + a[1] * b[2][1] + a[2];
}

/*	Affine matrix that transforms rect from (x1, y1, x2, y2) into rect to
 */
inline void translate_rect (const double4 from, const double4 to,
		double2 matrix[3]) {
	const double2 from_edge = (double2) { from[0], from[1] },
			to_edge = (double2) { to[0], to[1] };
	/* first align one of A and B’s edges */
	double2 translate_edge[3];
	translate (to_edge - from_edge, translate_edge);
	/* then scale it up or down */
	double2 scale_rect[3];
	scale ((double2) { (to[2] - to[0])/(from[2] - from[0]),
			(to[3] - to[1])/(from[3] - from[1])}, scale_rect);
	/* the result is scale*translate (i.e. translate first) */
	matrixmul (scale_rect, translate_edge, matrix);
}

/* Create rotation around center. Note that matrix multiplication is
 * right-associative, thus A*B*C == A*(B*C) */
inline void rotate_center (const double2 center, const double angle, double2 out[3]) {
	double2 rot[3], trans_a[3], trans_b[3], tmp[3];
	translate (-1.0 * center, trans_a);
	rotate (angle, rot);
	translate (center, trans_b);
	matrixmul (rot, trans_a, tmp);
	matrixmul (trans_b, tmp, out);
}

inline double sum(const double2 in) {
	return in[0] + in[1];
}

inline void normalize (double * const a, const size_t n) {
	double sum = 0.0;
	for (unsigned int j = 0; j < n; j++) {
		sum += a[j];
	}
	assert (sum > 0.0);

	for (unsigned int j = 0; j < n; j++) {
		a[j] /= sum;
	}
}

#define max(a,b) ((a) > (b) ? (a) : (b))

/*	Vector wrapping function, could be replaced by true vector functions later
 */

inline double4 clamp_d4 (const double4 in, const double min, const double max) {
	return (double4) {
			clamp (in[0], min, max),
			clamp (in[1], min, max),
			clamp (in[2], min, max),
			clamp (in[3], min, max),
			};
}

inline double4 pow_d4 (const double4 in, double exp) {
	return (double4) {
			pow (in[0], exp),
			pow (in[1], exp),
			pow (in[2], exp),
			pow (in[3], exp),
			};
}

inline double4 nearbyint_d4 (const double4 in) {
	return (double4) {
			nearbyint (in[0]),
			nearbyint (in[1]),
			nearbyint (in[2]),
			nearbyint (in[3]),
			};
}

inline double2 sin_d2 (const double2 in) {
	return (double2) { sin (in[0]), sin (in[1]) };
}

inline double2 cos_d2 (const double2 in) {
	return (double2) { cos (in[0]), cos (in[1]) };
}

inline double2 swap_d2 (const double2 in) {
	return (double2) { in[1], in[0] };
}