SCM Repository
Annotation of /branches/charisee/src/include/Diderot/inline-vec2.h
Parent Directory
|
Revision Log
Revision 2604 - (view) (download) (as text)
1 : | jhr | 1115 | /*! \file inline-vec2.h |
2 : | * | ||
3 : | * \author John Reppy | ||
4 : | * | ||
5 : | * \brief inline functions for 2D vectors | ||
6 : | */ | ||
7 : | |||
8 : | /* | ||
9 : | * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu) | ||
10 : | * All rights reserved. | ||
11 : | */ | ||
12 : | |||
13 : | #ifndef _DIDEROT_INLINE_VEC2_H_ | ||
14 : | #define _DIDEROT_INLINE_VEC2_H_ | ||
15 : | |||
16 : | #ifndef _DIDEROT_TYPES_H_ | ||
17 : | #include "Diderot/types.h" | ||
18 : | #endif | ||
19 : | |||
20 : | jhr | 1640 | STATIC_INLINE Diderot_vec2_t vec2 (Diderot_real_t a, Diderot_real_t b) |
21 : | jhr | 1115 | { |
22 : | #ifdef VEC2_OK | ||
23 : | jhr | 1640 | return __extension__ (Diderot_vec2_t){ a, b }; |
24 : | jhr | 1115 | #else |
25 : | jhr | 1640 | return __extension__ (Diderot_vec2_t){ a, b, 0.0, 0.0 }; |
26 : | jhr | 1115 | #endif |
27 : | } | ||
28 : | |||
29 : | jhr | 2356 | STATIC_INLINE Diderot_ivec2_t vec2i (Diderot_int_t x, Diderot_int_t y) |
30 : | { | ||
31 : | #ifdef VEC2_OK | ||
32 : | return __extension__ (Diderot_ivec2_t){x,y}; | ||
33 : | #else | ||
34 : | return __extension__ (Diderot_ivec2_t){x,y, 0, 0 }; | ||
35 : | #endif | ||
36 : | } | ||
37 : | |||
38 : | jhr | 1640 | STATIC_INLINE Diderot_ivec2_t vec2rtoi (Diderot_vec2_t v) |
39 : | jhr | 1115 | { |
40 : | jhr | 1640 | Diderot_union2_t u; |
41 : | jhr | 1115 | u.v = v; |
42 : | #ifdef VEC2_OK | ||
43 : | jhr | 1640 | return __extension__ (Diderot_ivec2_t){ (Diderot_int_t)u.r[0], (Diderot_int_t)u.r[1] }; |
44 : | jhr | 1115 | #else |
45 : | jhr | 1640 | return __extension__ (Diderot_ivec2_t){ (Diderot_int_t)u.r[0], (Diderot_int_t)u.r[1], 0, 0 }; |
46 : | jhr | 1115 | #endif |
47 : | } | ||
48 : | |||
49 : | jhr | 1640 | STATIC_INLINE Diderot_vec2_t vec2itor (Diderot_ivec2_t v) |
50 : | jhr | 1115 | { |
51 : | union2i_t u; | ||
52 : | u.v = v; | ||
53 : | #ifdef VEC2_OK | ||
54 : | jhr | 1640 | return __extension__ (Diderot_vec2_t){ (Diderot_real_t)u.i[0], (Diderot_real_t)u.i[1] }; |
55 : | jhr | 1115 | #else |
56 : | jhr | 1640 | return __extension__ (Diderot_vec2_t){ (Diderot_real_t)u.i[0], (Diderot_real_t)u.i[1], (Diderot_real_t)u.i[2], 0 }; |
57 : | jhr | 1115 | #endif |
58 : | } | ||
59 : | |||
60 : | jhr | 1640 | STATIC_INLINE Diderot_vec2_t scale2 (Diderot_real_t s, Diderot_vec2_t v) |
61 : | jhr | 1115 | { |
62 : | jhr | 1640 | return vec2(s, s) * v; |
63 : | jhr | 1115 | } |
64 : | |||
65 : | jhr | 1640 | STATIC_INLINE Diderot_vec2_t clamp2 (Diderot_vec2_t lo, Diderot_vec2_t hi, Diderot_vec2_t v) |
66 : | jhr | 1295 | { |
67 : | // FIXME: there is probably a vectorized way to compute this | ||
68 : | jhr | 1640 | Diderot_union2_t a, b, c; |
69 : | jhr | 1295 | a.v = lo; b.v = hi; c.v = v; |
70 : | jhr | 1640 | return vec2( |
71 : | clamp(a.r[0], b.r[0], c.r[0]), | ||
72 : | clamp(a.r[1], b.r[1], c.r[1])); | ||
73 : | jhr | 1295 | } |
74 : | |||
75 : | jhr | 1640 | STATIC_INLINE Diderot_vec2_t lerp2 (Diderot_vec2_t a, Diderot_vec2_t b, Diderot_real_t t) |
76 : | jhr | 1115 | { |
77 : | jhr | 1640 | return a + scale2(t, b - a); |
78 : | jhr | 1115 | } |
79 : | |||
80 : | jhr | 1640 | STATIC_INLINE Diderot_vec2_t floor2 (Diderot_vec2_t v) |
81 : | jhr | 1115 | { |
82 : | jhr | 1640 | Diderot_union2_t u; |
83 : | jhr | 1115 | u.v = v; |
84 : | jhr | 1640 | return vec2(FLOOR(u.r[0]), FLOOR(u.r[1])); |
85 : | jhr | 1115 | } |
86 : | |||
87 : | jhr | 1640 | STATIC_INLINE Diderot_ivec2_t truncToInt2 (Diderot_vec2_t v) |
88 : | jhr | 1115 | { |
89 : | jhr | 1640 | Diderot_union2_t t; |
90 : | jhr | 1115 | t.v = v; |
91 : | #ifdef VEC2_OK | ||
92 : | jhr | 1640 | return __extension__ (Diderot_ivec2_t){ |
93 : | (Diderot_int_t)TRUNC(t.r[0]), | ||
94 : | (Diderot_int_t)TRUNC(t.r[1]) }; | ||
95 : | jhr | 1115 | #else |
96 : | jhr | 1640 | return __extension__ (Diderot_ivec2_t){ |
97 : | (Diderot_int_t)TRUNC(t.r[0]), | ||
98 : | (Diderot_int_t)TRUNC(t.r[1]), | ||
99 : | jhr | 1370 | 0, |
100 : | 0 }; | ||
101 : | jhr | 1115 | #endif |
102 : | } | ||
103 : | |||
104 : | jhr | 1640 | STATIC_INLINE Diderot_real_t dot2 (Diderot_vec2_t u, Diderot_vec2_t v) |
105 : | jhr | 1115 | { |
106 : | jhr | 1640 | Diderot_union2_t uv = __extension__ (Diderot_union2_t)(u*v); |
107 : | jhr | 1115 | return uv.r[0] + uv.r[1]; |
108 : | } | ||
109 : | |||
110 : | jhr | 1640 | STATIC_INLINE Diderot_real_t length2 (Diderot_vec2_t v) |
111 : | jhr | 1115 | { |
112 : | jhr | 1640 | return SQRT(dot2(v, v)); |
113 : | jhr | 1115 | } |
114 : | |||
115 : | jhr | 1640 | STATIC_INLINE Diderot_vec2_t normalize2 (Diderot_vec2_t v) |
116 : | jhr | 1115 | { |
117 : | jhr | 1640 | return scale2(1.0 / length2(v), v); |
118 : | jhr | 1115 | } |
119 : | |||
120 : | #endif /* !_DIDEROT_INLINE_VEC2_H_ */ |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |