Code

0ccad30c4a48b5d0846fba6dcc0ccaae4335e9c4
[inkscape.git] / src / libnr / nr-macros.h
1 #ifndef __NR_MACROS_H__
2 #define __NR_MACROS_H__
4 /*
5  * Pixel buffer rendering library
6  *
7  * Authors:
8  *   Lauris Kaplinski <lauris@kaplinski.com>
9  *
10  * This code is in public domain
11  */
13 #include <math.h>
15 #if HAVE_STDLIB_H
16 #include <stdlib.h>
17 #endif
18 #include <assert.h>
20 #ifndef TRUE
21 #define TRUE (!0)
22 #endif
23 #ifndef FALSE
24 #define FALSE 0
25 #endif
26 #ifndef MAX
27 #define MAX(a,b) (((a) < (b)) ? (b) : (a))
28 #endif
29 #ifndef MIN
30 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
31 #endif
33 #ifndef CLAMP
34 /** Returns v bounded to within [a, b].  If v is NaN then returns a. 
35  *
36  *  \pre \a a \<= \a b.
37  */
38 # define CLAMP(v,a,b)   \
39         (assert (a <= b),       \
40          ((v) >= (a))   \
41          ? (((v) > (b)) \
42             ? (b)       \
43             : (v))      \
44          : (a))
45 #endif
47 #define NR_DF_TEST_CLOSE(a,b,e) (fabs ((a) - (b)) <= (e))
49 // Todo: move these into nr-matrix.h
50 #define NR_MATRIX_DF_TEST_TRANSFORM_CLOSE(a,b,e) (NR_DF_TEST_CLOSE ((*(a))[0], (*(b))[0], e) && \
51                                         NR_DF_TEST_CLOSE ((*(a))[1], (*(b))[1], e) && \
52                                         NR_DF_TEST_CLOSE ((*(a))[2], (*(b))[2], e) && \
53                                         NR_DF_TEST_CLOSE ((*(a))[3], (*(b))[3], e))
54 #define NR_MATRIX_DF_TEST_TRANSLATE_CLOSE(a,b,e) (NR_DF_TEST_CLOSE ((*(a))[4], (*(b))[4], e) && \
55                                         NR_DF_TEST_CLOSE ((*(a))[5], (*(b))[5], e))
56 #define NR_MATRIX_DF_TEST_CLOSE(a,b,e) (NR_MATRIX_DF_TEST_TRANSLATE_CLOSE (a, b, e) && \
57                                         NR_MATRIX_DF_TEST_TRANSFORM_CLOSE (a, b, e))
59 #define NR_RECT_DFLS_TEST_EMPTY(a) (((a)->x0 >= (a)->x1) || ((a)->y0 >= (a)->y1))
60 #define NR_RECT_DFLS_TEST_INTERSECT(a,b) (((a)->x0 < (b)->x1) && ((a)->x1 > (b)->x0) && ((a)->y0 < (b)->y1) && ((a)->y1 > (b)->y0))
61 #define NR_RECT_DF_POINT_DF_TEST_INSIDE(r,p) (((p)->x >= (r)->x0) && ((p)->x < (r)->x1) && ((p)->y >= (r)->y0) && ((p)->y < (r)->y1))
62 #define NR_RECT_LS_POINT_LS_TEST_INSIDE(r,p) (((p)->x >= (r)->x0) && ((p)->x < (r)->x1) && ((p)->y >= (r)->y0) && ((p)->y < (r)->y1))
63 #define NR_RECT_LS_TEST_INSIDE(r,x,y) ((x >= (r)->x0) && (x < (r)->x1) && (y >= (r)->y0) && (y < (r)->y1))
65 #define NR_MATRIX_D_TO_DOUBLE(m) ((m)->c)
66 #define NR_MATRIX_D_FROM_DOUBLE(d) ((NRMatrix *) &(d)[0])
68 #endif