-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathabnormal.cpp
162 lines (121 loc) · 2.84 KB
/
abnormal.cpp
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
#include <assert.h>
#include <iostream>
#include "abnormal.h"
using namespace std;
float error(const Vec3f& v0, const Vec3f& v1)
{
float cos_theta = v0*v1;
return acos(cos_theta) * (180.0f / M_PI);
}
void explicit_test(const Vec3f& v)
{
Norm in = v;
Vec3f out = in;
cout << v << " -> " << out << ",\n\terror = "
<< error(v, out) << " degrees." << endl;
}
void test_abnormals()
{
Norm::generate_table();
explicit_test( Vec3f(1.0f, 0.0f, 0.0f) );
explicit_test( Vec3f(0.0f, 1.0f, 0.0f) );
explicit_test( Vec3f(0.0f, 0.0f, 1.0f) );
const int N = 8;
double error_sum = 0;
for (int i=0; i < N; i++) {
float x = (rand() / (double)RAND_MAX) * 0.5f - 0.25f;
float y = (rand() / (double)RAND_MAX) * 0.5f - 0.25f;
float z = (rand() / (double)RAND_MAX) * 0.5f - 0.25f;
Vec3f v( x,y,z );
v /= norm(v);
Norm in = v;
Vec3f out = in;
explicit_test(v);
error_sum += error(v,out);
}
error_sum /= (double)N;
cout << "(" << BIT_DIM*2 << " bits per normal.)" << endl;
cout << "Average error over " << N << " random normals = "
<< error_sum << " degrees." << endl;
}
const float bit_scale = (float)(BIT_DIM-1);
Vec3f Norm::vec_table[BIT_DIM][BIT_DIM];
void Norm::generate_table()
{
const float bit_inv = 1.0f / bit_scale;
std::cout << "Generating norm table..." << std::endl;
for (int x=0; x < BIT_DIM; x++) {
for (int y=0; y < BIT_DIM; y++) {
if (x == 13 && y == 0) {
bool boob = true;
}
if (x == 6 && y == 2) {
bool boob = true;
}
float fx = (float)x * bit_inv;
float fy = (float)y * bit_inv;
float z_sqr = 1.0f - fx*fx - fy*fy;
float fz = 0.0f;
if (z_sqr >= 0.0f) {
fz = sqrt(z_sqr);
}
vec_table[x][y] = Vec3f(fx, fy, fz);
if (z_sqr < 0.0f) {
vec_table[x][y] /= norm(vec_table[x][y]);
}
}
}
}
Norm::Norm()
{
self.index_x = self.index_y = 0;
}
Norm::Norm(const Vec3f& v)
{
operator=(v);
}
void Norm::operator=(const Vec3f& v)
{
assert(norm2(v) <= 1.001f);
float n2 = norm2(v);
float x, y;
if (v[0] >= 0.0f) {
self.x = 1;
x = v[0];
} else {
self.x = 0;
x = -v[0];
}
if (v[1] >= 0.0f) {
self.y = 1;
y = v[1];
} else {
self.y = 0;
y = -v[1];
}
self.z = (v[2] >= 0.0f);
self.index_x = (index_type)(x*bit_scale+0.5f);
self.index_y = (index_type)(y*bit_scale+0.5f);
assert(self.index_x >= 0 && self.index_x < BIT_DIM);
assert(self.index_y >= 0 && self.index_y < BIT_DIM);
}
Norm::operator Vec3f()
{
Vec3f v = vec_table[self.index_x][self.index_y];
if (!self.x) {
v[0] = -v[0];
}
if (!self.y) {
v[1] = -v[1];
}
if (!self.z) {
v[2] = -v[2];
}
return v;
}
void Norm::operator+=(const Vec3f& v)
{
Vec3f new_v = this->operator Vec3f();
new_v += v;
this->operator=(new_v);
}