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
|
/*
* 3D utils.
*
* Authors:
* Jean-Rene Reinhard <jr@komite.net>
*
* Copyright (C) 2007 authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include <glib/gmessages.h>
#include "libnr/nr-pixblock.h"
#include "display/nr-3dutils.h"
#include <cmath>
namespace NR {
#define BEGIN 0 // TOP or LEFT
#define MIDDLE 1
#define END 2 // BOTTOM or RIGHT
#define START(v) ((v)==BEGIN? 1 : 0)
#define FINISH(v) ((v)==END? 1 : 2)
signed char K_X[3][3][3][3] = {
//K_X[TOP]
{
//K_X[TOP][LEFT]
{
{ 0, 0, 0},
{ 0, -2, 2},
{ 0, -1, 1}
},
{
{ 0, 0, 0},
{-2, 0, 2},
{-1, 0, 1}
},
{
{ 0, 0, 0},
{-2, 2, 0},
{-1, 1, 0}
}
},
//K_X[MIDDLE]
{
//K_X[MIDDLE][LEFT]
{
{ 0, -1, 1},
{ 0, -2, 2},
{ 0, -1, 1}
},
{
{-1, 0, 1},
{-2, 0, 2},
{-1, 0, 1}
},
{
{-1, 1, 0},
{-2, 2, 0},
{-1, 1, 0}
}
},
//K_X[BOTTOM]
{
//K_X[BOTTOM][LEFT]
{
{ 0, -1, 1},
{ 0, -2, 2},
{ 0, 0, 0}
},
{
{-1, 0, 1},
{-2, 0, 2},
{ 0, 0, 0}
},
{
{-1, 1, 0},
{-2, 2, 0},
{ 0, 0, 0}
}
}
};
//K_Y is obtained by transposing K_X globally and each of its components
gdouble FACTOR_X[3][3] = {
{2./3, 1./3, 2./3},
{1./2, 1./4, 1./2},
{2./3, 1./3, 2./3}
};
//FACTOR_Y is obtained by transposing FACTOR_X
inline
int get_carac(int i, int len, int delta) {
if (i < delta)
return BEGIN;
else if (i > len - 1 - delta)
return END;
else
return MIDDLE;
}
//assumes in is RGBA
//should be made more resistant
void compute_surface_normal(Fvector &N, gdouble ss, NRPixBlock *in, int i, int j, int dx, int dy) {
int w = in->area.x1 - in->area.x0;
int h = in->area.y1 - in->area.y0;
int k, l, alpha_idx, alpha_idx_y;
int x_carac, y_carac;
gdouble alpha;
gdouble accu_x;
gdouble accu_y;
unsigned char *data = NR_PIXBLOCK_PX (in);
g_assert(NR_PIXBLOCK_BPP(in) == 4);
x_carac = get_carac(j, w, dx); //LEFT, MIDDLE or RIGHT
y_carac = get_carac(i, h, dy); //TOP, MIDDLE or BOTTOM
alpha_idx = 4*(i*w + j);
accu_x = 0;
accu_y = 0;
for (k = START(y_carac); k <= FINISH(y_carac); k++) {
alpha_idx_y = alpha_idx + 4*(k-1)*dy*w;
for (l = START(x_carac); l <= FINISH(x_carac); l++) {
alpha = (data + alpha_idx_y + 4*dx*(l-1))[3];
accu_x += K_X[y_carac][x_carac][k][l] * alpha / 255;
accu_y += K_X[x_carac][y_carac][l][k] * alpha / 255;
}
}
N[X_3D] = -1 * ss * FACTOR_X[y_carac][x_carac] * accu_x / dx;
N[Y_3D] = -1 * ss * FACTOR_X[x_carac][y_carac] * accu_y / dy;
N[Z_3D] = 1;
normalize_vector(N);
//std::cout << "(" << N[X_3D] << ", " << N[Y_3D] << ", " << N[Z_3D] << ")" << std::endl;
}
void convert_coord(gdouble &x, gdouble &y, gdouble &z, Geom::Matrix const &trans) {
Point p = Point(x, y);
p *= trans;
x = p[Geom::X];
y = p[Geom::Y];
z *= trans[0];
}
gdouble norm(const Fvector &v) {
return sqrt(v[X_3D]*v[X_3D] + v[Y_3D]*v[Y_3D] + v[Z_3D]*v[Z_3D]);
}
void normalize_vector(Fvector &v) {
gdouble nv = norm(v);
//TODO test nv == 0
for (int j = 0; j < 3; j++) {
v[j] /= nv;
}
}
gdouble scalar_product(const Fvector &a, const Fvector &b) {
return a[X_3D] * b[X_3D] +
a[Y_3D] * b[Y_3D] +
a[Z_3D] * b[Z_3D];
}
void normalized_sum(Fvector &r, const Fvector &a, const Fvector &b) {
r[X_3D] = a[X_3D] + b[X_3D];
r[Y_3D] = a[Y_3D] + b[Y_3D];
r[Z_3D] = a[Z_3D] + b[Z_3D];
normalize_vector(r);
}
}/* namespace NR */
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
|