Code

74a627ae7c2a1cc3ee4f6c3927aa0c3ab506afaa
[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 #define nr_new(t,n) ((t *) malloc ((n) * sizeof (t)))
21 #define nr_free free
22 #define nr_renew(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
24 #ifndef TRUE
25 #define TRUE (!0)
26 #endif
27 #ifndef FALSE
28 #define FALSE 0
29 #endif
30 #ifndef MAX
31 #define MAX(a,b) (((a) < (b)) ? (b) : (a))
32 #endif
33 #ifndef MIN
34 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
35 #endif
37 #ifndef CLAMP
38 /** Returns v bounded to within [a, b].  If v is NaN then returns a. 
39  *
40  *  \pre \a a \<= \a b.
41  */
42 # define CLAMP(v,a,b)   \
43         (assert (a <= b),       \
44          ((v) >= (a))   \
45          ? (((v) > (b)) \
46             ? (b)       \
47             : (v))      \
48          : (a))
49 #endif
51 #define NR_DF_TEST_CLOSE(a,b,e) (fabs ((a) - (b)) <= (e))
53 // Todo: move these into nr-matrix.h
54 #define NR_MATRIX_DF_TEST_TRANSFORM_CLOSE(a,b,e) (NR_DF_TEST_CLOSE ((*(a))[0], (*(b))[0], e) && \
55                                         NR_DF_TEST_CLOSE ((*(a))[1], (*(b))[1], e) && \
56                                         NR_DF_TEST_CLOSE ((*(a))[2], (*(b))[2], e) && \
57                                         NR_DF_TEST_CLOSE ((*(a))[3], (*(b))[3], e))
58 #define NR_MATRIX_DF_TEST_TRANSLATE_CLOSE(a,b,e) (NR_DF_TEST_CLOSE ((*(a))[4], (*(b))[4], e) && \
59                                         NR_DF_TEST_CLOSE ((*(a))[5], (*(b))[5], e))
60 #define NR_MATRIX_DF_TEST_CLOSE(a,b,e) (NR_MATRIX_DF_TEST_TRANSLATE_CLOSE (a, b, e) && \
61                                         NR_MATRIX_DF_TEST_TRANSFORM_CLOSE (a, b, e))
63 #define NR_RECT_DFLS_TEST_EMPTY(a) (((a)->x0 >= (a)->x1) || ((a)->y0 >= (a)->y1))
64 #define NR_RECT_DFLS_TEST_INTERSECT(a,b) (((a)->x0 < (b)->x1) && ((a)->x1 > (b)->x0) && ((a)->y0 < (b)->y1) && ((a)->y1 > (b)->y0))
65 #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))
66 #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))
68 #define NR_MATRIX_D_TO_DOUBLE(m) ((m)->c)
69 #define NR_MATRIX_D_FROM_DOUBLE(d) ((NRMatrix *) &(d)[0])
71 #endif