-
Notifications
You must be signed in to change notification settings - Fork 0
/
trig.h
200 lines (158 loc) · 4.37 KB
/
trig.h
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright (c) 2007 Alexander Strange <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef __trig_h
#define __trig_h
#include "atrace.h"
#include <math.h>
#include <stdio.h>
#ifdef M_PI
#define PI M_PI
#else
#define PI 3.14159265358979323846
#endif
#define TWOPI 6.28318530717958647692
#define LOWPRECISION
#ifdef LOWPRECISION
#ifdef __SSSE3__
#define SSEVEC
#include <pmmintrin.h>
static inline __m128 sse_1000() {return (__m128)_mm_set_epi32(-1,0,0,0);}
static inline __m128 zero_w_sse(__m128 s) {return _mm_andnot_ps(sse_1000(), s);}
#endif
#define EPSILON ((world_distance)(1./4096.))
typedef float world_distance;
typedef world_distance angle;
typedef float real;
#define pow powf
#define exp expf
#define sqrt sqrtf
#define fabs fabsf
#define fmod fmodf
#else
#define EPSILON ((world_distance)(1./1099511627776.))
typedef double world_distance;
typedef world_distance angle;
typedef double real;
#endif
typedef float f_real;
template<typename T, typename T2> T dmin(T a, T2 b) {return (a <= b) ? a : b;}
template<typename T, typename T2> T dmax(T a, T2 b) {return (a >= b) ? a : b;}
static inline bool close(world_distance a, world_distance b) {return fabs(a-b) <= EPSILON;}
static inline bool above(world_distance a, world_distance b) {return (a - b) >= EPSILON;}
template <typename T> void swap(T &a, T &b)
{
T c = a;
a = b;
b = c;
}
static size_t wrap(ssize_t i, ssize_t off, ssize_t m, bool modulo)
{
i += off;
if (modulo) {
i = (i < 0) ? (i + m) : ((i >= m) ? (i - m) : i);
} else i = dmin(dmax(i, 0), m-1);
return i;
}
extern real srgbToL[256];
static inline real colorToL(uint8_t vi)
{
return srgbToL[vi];
}
static inline uint8_t colorFromL(f_real v)
{
const f_real a = .055f;
f_real v_srgb = (v > .0031308f) ? ((a + 1.f) * powf(v, 1.f/2.4f) - a) : (v * 12.92f);
return dmin(dmax((int)lrintf(v_srgb * 255.f), 0), 255);
}
static inline uint8_t dithered_fromL(f_real v, f_real *error)
{
f_real er = *error, v_er = v+er;
uint8_t o = colorFromL(v_er);
f_real v2 = colorToL(o);
*error = v_er-v2;
return o;
}
template <typename T, int N> struct vectorX
{
};
#define N 3
#include "vector_template.h"
#define N 4
#include "vector_template.h"
typedef vectorX<world_distance, 3> vector3;
typedef vectorX<world_distance, 4> vector4;
typedef vectorX<real, 3> color3;
typedef vectorX<real, 4> color4;
typedef color3 color;
typedef vector3 point3;
typedef vectorX<f_real, 3> f_pixel;
typedef vectorX<uint8_t, 3> pixel8;
static inline color4 over(color4 above, color4 below)
{
return above + below*(1.-above.a);
}
static inline color4 c3to4(color c)
{
#ifdef SSEVEC
color4 res(c.s);
res.a=1;
return res;
#else
return color4(c.r,c.g,c.b);
#endif
}
static inline color c4to3(color4 c)
{
#ifdef SSEVEC
return color(c.s);
#else
return color(c.r,c.g,c.b);
#endif
}
static inline color4 to_premultiplied(color c, real a)
{
color4 res = c3to4(c);
return res * a;
}
static inline color4 to_premultiplied(color4 cs) {return to_premultiplied(c4to3(cs), cs.a);}
static inline color from_premultiplied(color4 c, real *a = NULL)
{
real resa = c.a;
if (a) *a = resa;
return c4to3(c * resa);
}
static inline color strip_alpha(color4 c)
{
return c4to3(c*c.a);
}
struct ray
{
point3 origin;
vector3 dir;
ray(const point3 &o, const vector3 &d, bool should_normalize = true) __attribute__((always_inline)) : origin(o), dir(should_normalize ? normalize(d) : d) {}
point3 pointAt(world_distance dist) const {
return dir * dist + origin;
}
ray rayAt(world_distance dist) const {
return ray(pointAt(dist), dir, false);
}
};
static inline ray ray_from_to(const point3 &origin, const point3 &destination)
{
vector3 from_to = destination - origin;
return ray(origin, from_to);
}
#endif