~lib2geom-hackers/lib2geom/trunk

« back to all changes in this revision

Viewing changes to macros.h

  • Committer: njh
  • Date: 2006-05-22 11:50:24 UTC
  • Revision ID: svn-v4:4601daaa-0314-0410-9a8b-c964a3c23b6b:trunk/lib2geom:1
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef __Geom_MACROS_H__
 
2
#define __Geom_MACROS_H__
 
3
 
 
4
/*
 
5
 * Pixel buffer rendering library
 
6
 *
 
7
 * Authors:
 
8
 *   Lauris Kaplinski <lauris@kaplinski.com>
 
9
 *
 
10
 * This code is in public domain
 
11
 */
 
12
 
 
13
#include <math.h>
 
14
 
 
15
#if HAVE_STDLIB_H
 
16
#include <stdlib.h>
 
17
#endif
 
18
#include <assert.h>
 
19
 
 
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)))
 
23
 
 
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
 
36
 
 
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
 
50
 
 
51
#define Geom_DF_TEST_CLOSE(a,b,e) (fabs ((a) - (b)) <= (e))
 
52
 
 
53
// Todo: move these into matrix.h
 
54
#define Geom_MATRIX_DF_TEST_TRANSFORM_CLOSE(a,b,e) (Geom_DF_TEST_CLOSE ((*(a))[0], (*(b))[0], e) && \
 
55
                                        Geom_DF_TEST_CLOSE ((*(a))[1], (*(b))[1], e) && \
 
56
                                        Geom_DF_TEST_CLOSE ((*(a))[2], (*(b))[2], e) && \
 
57
                                        Geom_DF_TEST_CLOSE ((*(a))[3], (*(b))[3], e))
 
58
#define Geom_MATRIX_DF_TEST_TRANSLATE_CLOSE(a,b,e) (Geom_DF_TEST_CLOSE ((*(a))[4], (*(b))[4], e) && \
 
59
                                        Geom_DF_TEST_CLOSE ((*(a))[5], (*(b))[5], e))
 
60
#define Geom_MATRIX_DF_TEST_CLOSE(a,b,e) (Geom_MATRIX_DF_TEST_TRANSLATE_CLOSE (a, b, e) && \
 
61
                                        Geom_MATRIX_DF_TEST_TRANSFORM_CLOSE (a, b, e))
 
62
 
 
63
#define Geom_RECT_DFLS_TEST_EMPTY(a) (((a)->x0 >= (a)->x1) || ((a)->y0 >= (a)->y1))
 
64
#define Geom_RECT_DFLS_TEST_INTERSECT(a,b) (((a)->x0 < (b)->x1) && ((a)->x1 > (b)->x0) && ((a)->y0 < (b)->y1) && ((a)->y1 > (b)->y0))
 
65
#define Geom_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 Geom_RECT_LS_POINT_LS_TEST_INSIDE(r,p) (((p)->x >= (r)->x0) && ((p)->x < (r)->x1) && ((p)->y >= (r)->y0) && ((p)->y < (r)->y1))
 
67
 
 
68
#define Geom_MATRIX_D_TO_DOUBLE(m) ((m)->c)
 
69
#define Geom_MATRIX_D_FROM_DOUBLE(d) ((NRMatrix *) &(d)[0])
 
70
 
 
71
#endif