10eb4e8291265f320c1640406d91c29c0ad9daa6
[blender.git] / source / blender / blenlib / intern / voxel.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Matt Ebb, Raul Fernandez Hernandez (Farsthary).
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenlib/intern/voxel.c
29  *  \ingroup bli
30  */
31
32
33 #include "BLI_voxel.h"
34 #include "BLI_utildefines.h"
35
36
37
38 BLI_INLINE float D(float *data, const int res[3], int x, int y, int z)
39 {
40         CLAMP(x, 0, res[0] - 1);
41         CLAMP(y, 0, res[1] - 1);
42         CLAMP(z, 0, res[2] - 1);
43         return data[BLI_VOXEL_INDEX(x, y, z, res)];
44 }
45
46 /* *** nearest neighbor *** */
47 /* input coordinates must be in bounding box 0.0 - 1.0 */
48 float BLI_voxel_sample_nearest(float *data, const int res[3], const float co[3])
49 {
50         int xi, yi, zi;
51         
52         xi = co[0] * res[0];
53         yi = co[1] * res[1];
54         zi = co[2] * res[2];
55         
56         return D(data, res, xi, yi, zi);
57 }
58
59 // returns highest integer <= x as integer (slightly faster than floor())
60 BLI_INLINE int FLOORI(float x)
61 {
62         const int r = (int)x;
63         return ((x >= 0.f) || (float)r == x) ? r : (r - 1);
64 }
65
66 // clamp function, cannot use the CLAMPIS macro, it sometimes returns unwanted results apparently related to gcc optimization flag -fstrict-overflow which is enabled at -O2
67 // this causes the test (x + 2) < 0 with int x == 2147483647 to return false (x being an integer, x + 2 should wrap around to -2147483647 so the test < 0 should return true, which it doesn't)
68 BLI_INLINE int _clamp(int a, int b, int c)
69 {
70         return (a < b) ? b : ((a > c) ? c : a);
71 }
72
73 float BLI_voxel_sample_trilinear(float *data, const int res[3], const float co[3])
74 {
75         if (data) {
76         
77                 const float xf = co[0] * res[0] - 0.5f;
78                 const float yf = co[1] * res[1] - 0.5f;
79                 const float zf = co[2] * res[2] - 0.5f;
80                 
81                 const int x = FLOORI(xf), y = FLOORI(yf), z = FLOORI(zf);
82         
83                 const int xc[2] = {_clamp(x, 0, res[0] - 1), _clamp(x + 1, 0, res[0] - 1)};
84                 const int yc[2] = {res[0] * _clamp(y, 0, res[1] - 1), res[0] * _clamp(y + 1, 0, res[1] - 1)};
85                 const int zc[2] = {res[0] * res[1] * _clamp(z, 0, res[2] - 1), res[0] * res[1] * _clamp(z + 1, 0, res[2] - 1)};
86         
87                 const float dx = xf - (float)x;
88                 const float dy = yf - (float)y;
89                 const float dz = zf - (float)z;
90                 
91                 const float u[2] = {1.f - dx, dx};
92                 const float v[2] = {1.f - dy, dy};
93                 const float w[2] = {1.f - dz, dz};
94         
95                 return w[0] * (   v[0] * ( u[0] * data[xc[0] + yc[0] + zc[0]] + u[1] * data[xc[1] + yc[0] + zc[0]] )
96                                                 + v[1] * ( u[0] * data[xc[0] + yc[1] + zc[0]] + u[1] * data[xc[1] + yc[1] + zc[0]] ) )
97                          + w[1] * (   v[0] * ( u[0] * data[xc[0] + yc[0] + zc[1]] + u[1] * data[xc[1] + yc[0] + zc[1]] )
98                                                 + v[1] * ( u[0] * data[xc[0] + yc[1] + zc[1]] + u[1] * data[xc[1] + yc[1] + zc[1]] ) );
99         
100         }
101         return 0.f;
102 }
103         
104
105 float BLI_voxel_sample_triquadratic(float *data, const int res[3], const float co[3])
106 {
107         if (data) {
108
109                 const float xf = co[0] * res[0], yf = co[1] * res[1], zf = co[2] * res[2];
110                 const int x = FLOORI(xf), y = FLOORI(yf), z = FLOORI(zf);
111
112                 const int xc[3] = {_clamp(x - 1, 0, res[0] - 1), _clamp(x, 0, res[0] - 1), _clamp(x + 1, 0, res[0] - 1)};
113                 const int yc[3] = {res[0] * _clamp(y - 1, 0, res[1] - 1), res[0] * _clamp(y, 0, res[1] - 1), res[0] * _clamp(y + 1, 0, res[1] - 1)};
114                 const int zc[3] = {res[0] * res[1] * _clamp(z - 1, 0, res[2] - 1), res[0] * res[1] * _clamp(z, 0, res[2] - 1), res[0] * res[1] * _clamp(z + 1, 0, res[2] - 1)};
115
116                 const float dx = xf - (float)x, dy = yf - (float)y, dz = zf - (float)z;
117                 const float u[3] = {dx*(0.5f*dx - 1.f) + 0.5f, dx*(1.f - dx) + 0.5f, 0.5f*dx*dx};
118                 const float v[3] = {dy*(0.5f*dy - 1.f) + 0.5f, dy*(1.f - dy) + 0.5f, 0.5f*dy*dy};
119                 const float w[3] = {dz*(0.5f*dz - 1.f) + 0.5f, dz*(1.f - dz) + 0.5f, 0.5f*dz*dz};
120
121                 return w[0] * (   v[0] * ( u[0] * data[xc[0] + yc[0] + zc[0]] + u[1] * data[xc[1] + yc[0] + zc[0]] + u[2] * data[xc[2] + yc[0] + zc[0]] )
122                                                 + v[1] * ( u[0] * data[xc[0] + yc[1] + zc[0]] + u[1] * data[xc[1] + yc[1] + zc[0]] + u[2] * data[xc[2] + yc[1] + zc[0]] )
123                                                 + v[2] * ( u[0] * data[xc[0] + yc[2] + zc[0]] + u[1] * data[xc[1] + yc[2] + zc[0]] + u[2] * data[xc[2] + yc[2] + zc[0]] ) )
124                          + w[1] * (   v[0] * ( u[0] * data[xc[0] + yc[0] + zc[1]] + u[1] * data[xc[1] + yc[0] + zc[1]] + u[2] * data[xc[2] + yc[0] + zc[1]] )
125                                                 + v[1] * ( u[0] * data[xc[0] + yc[1] + zc[1]] + u[1] * data[xc[1] + yc[1] + zc[1]] + u[2] * data[xc[2] + yc[1] + zc[1]] )
126                                                 + v[2] * ( u[0] * data[xc[0] + yc[2] + zc[1]] + u[1] * data[xc[1] + yc[2] + zc[1]] + u[2] * data[xc[2] + yc[2] + zc[1]] ) )
127                          + w[2] * (   v[0] * ( u[0] * data[xc[0] + yc[0] + zc[2]] + u[1] * data[xc[1] + yc[0] + zc[2]] + u[2] * data[xc[2] + yc[0] + zc[2]] )
128                                                 + v[1] * ( u[0] * data[xc[0] + yc[1] + zc[2]] + u[1] * data[xc[1] + yc[1] + zc[2]] + u[2] * data[xc[2] + yc[1] + zc[2]] )
129                                                 + v[2] * ( u[0] * data[xc[0] + yc[2] + zc[2]] + u[1] * data[xc[1] + yc[2] + zc[2]] + u[2] * data[xc[2] + yc[2] + zc[2]] ) );
130
131         }
132         return 0.f;
133 }
134
135 float BLI_voxel_sample_tricubic(float *data, const int res[3], const float co[3], int bspline)
136 {
137         if (data) {
138
139                 const float xf = co[0] * res[0] - 0.5f, yf = co[1] * res[1] - 0.5f, zf = co[2] * res[2] - 0.5f;
140                 const int x = FLOORI(xf), y = FLOORI(yf), z = FLOORI(zf);
141
142                 const int xc[4] = {_clamp(x - 1, 0, res[0] - 1), _clamp(x, 0, res[0] - 1), _clamp(x + 1, 0, res[0] - 1), _clamp(x + 2, 0, res[0] - 1)};
143                 const int yc[4] = {res[0] * _clamp(y - 1, 0, res[1] - 1), res[0] * _clamp(y, 0, res[1] - 1), res[0] * _clamp(y + 1, 0, res[1] - 1), res[0] * _clamp(y + 2, 0, res[1] - 1)};
144                 const int zc[4] = {res[0] * res[1] * _clamp(z - 1, 0, res[2] - 1), res[0] * res[1] * _clamp(z, 0, res[2] - 1), res[0] * res[1] * _clamp(z + 1, 0, res[2] - 1), res[0] * res[1] * _clamp(z + 2, 0, res[2] - 1)};
145
146                 const float dx = xf - (float)x, dy = yf - (float)y, dz = zf - (float)z;
147
148                 float u[4], v[4], w[4];
149                 if (bspline) {  // B-Spline
150                         u[0] = (((-1.f/6.f)*dx + 0.5f)*dx - 0.5f)*dx + (1.f/6.f);
151                         u[1] =  ((     0.5f*dx - 1.f )*dx       )*dx + (2.f/3.f);
152                         u[2] =  ((    -0.5f*dx + 0.5f)*dx + 0.5f)*dx + (1.f/6.f);
153                         u[3] =   ( 1.f/6.f)*dx*dx*dx;
154                         v[0] = (((-1.f/6.f)*dy + 0.5f)*dy - 0.5f)*dy + (1.f/6.f);
155                         v[1] =  ((     0.5f*dy - 1.f )*dy       )*dy + (2.f/3.f);
156                         v[2] =  ((    -0.5f*dy + 0.5f)*dy + 0.5f)*dy + (1.f/6.f);
157                         v[3] =  ( 1.f/6.f)*dy*dy*dy;
158                         w[0] = (((-1.f/6.f)*dz + 0.5f)*dz - 0.5f)*dz + (1.f/6.f);
159                         w[1] =  ((     0.5f*dz - 1.f )*dz       )*dz + (2.f/3.f);
160                         w[2] =  ((    -0.5f*dz + 0.5f)*dz + 0.5f)*dz + (1.f/6.f);
161                         w[3] =   ( 1.f/6.f)*dz*dz*dz;
162                 }
163                 else {  // Catmull-Rom
164                         u[0] = ((-0.5f*dx + 1.0f)*dx - 0.5f)*dx;
165                         u[1] = (( 1.5f*dx - 2.5f)*dx       )*dx + 1.0f;
166                         u[2] = ((-1.5f*dx + 2.0f)*dx + 0.5f)*dx;
167                         u[3] = (( 0.5f*dx - 0.5f)*dx       )*dx;
168                         v[0] = ((-0.5f*dy + 1.0f)*dy - 0.5f)*dy;
169                         v[1] = (( 1.5f*dy - 2.5f)*dy       )*dy + 1.0f;
170                         v[2] = ((-1.5f*dy + 2.0f)*dy + 0.5f)*dy;
171                         v[3] = (( 0.5f*dy - 0.5f)*dy       )*dy;
172                         w[0] = ((-0.5f*dz + 1.0f)*dz - 0.5f)*dz;
173                         w[1] = (( 1.5f*dz - 2.5f)*dz       )*dz + 1.0f;
174                         w[2] = ((-1.5f*dz + 2.0f)*dz + 0.5f)*dz;
175                         w[3] = (( 0.5f*dz - 0.5f)*dz       )*dz;
176                 }
177
178                 return w[0] * (   v[0] * ( u[0] * data[xc[0] + yc[0] + zc[0]] + u[1] * data[xc[1] + yc[0] + zc[0]] + u[2] * data[xc[2] + yc[0] + zc[0]] + u[3] * data[xc[3] + yc[0] + zc[0]] )
179                                                 + v[1] * ( u[0] * data[xc[0] + yc[1] + zc[0]] + u[1] * data[xc[1] + yc[1] + zc[0]] + u[2] * data[xc[2] + yc[1] + zc[0]] + u[3] * data[xc[3] + yc[1] + zc[0]] )
180                                                 + v[2] * ( u[0] * data[xc[0] + yc[2] + zc[0]] + u[1] * data[xc[1] + yc[2] + zc[0]] + u[2] * data[xc[2] + yc[2] + zc[0]] + u[3] * data[xc[3] + yc[2] + zc[0]] )
181                                                 + v[3] * ( u[0] * data[xc[0] + yc[3] + zc[0]] + u[1] * data[xc[1] + yc[3] + zc[0]] + u[2] * data[xc[2] + yc[3] + zc[0]] + u[3] * data[xc[3] + yc[3] + zc[0]] ) )
182                          + w[1] * (   v[0] * ( u[0] * data[xc[0] + yc[0] + zc[1]] + u[1] * data[xc[1] + yc[0] + zc[1]] + u[2] * data[xc[2] + yc[0] + zc[1]] + u[3] * data[xc[3] + yc[0] + zc[1]] )
183                                                 + v[1] * ( u[0] * data[xc[0] + yc[1] + zc[1]] + u[1] * data[xc[1] + yc[1] + zc[1]] + u[2] * data[xc[2] + yc[1] + zc[1]] + u[3] * data[xc[3] + yc[1] + zc[1]] )
184                                                 + v[2] * ( u[0] * data[xc[0] + yc[2] + zc[1]] + u[1] * data[xc[1] + yc[2] + zc[1]] + u[2] * data[xc[2] + yc[2] + zc[1]] + u[3] * data[xc[3] + yc[2] + zc[1]] )
185                                                 + v[3] * ( u[0] * data[xc[0] + yc[3] + zc[1]] + u[1] * data[xc[1] + yc[3] + zc[1]] + u[2] * data[xc[2] + yc[3] + zc[1]] + u[3] * data[xc[3] + yc[3] + zc[1]] ) )
186                          + w[2] * (   v[0] * ( u[0] * data[xc[0] + yc[0] + zc[2]] + u[1] * data[xc[1] + yc[0] + zc[2]] + u[2] * data[xc[2] + yc[0] + zc[2]] + u[3] * data[xc[3] + yc[0] + zc[2]] )
187                                                 + v[1] * ( u[0] * data[xc[0] + yc[1] + zc[2]] + u[1] * data[xc[1] + yc[1] + zc[2]] + u[2] * data[xc[2] + yc[1] + zc[2]] + u[3] * data[xc[3] + yc[1] + zc[2]] )
188                                                 + v[2] * ( u[0] * data[xc[0] + yc[2] + zc[2]] + u[1] * data[xc[1] + yc[2] + zc[2]] + u[2] * data[xc[2] + yc[2] + zc[2]] + u[3] * data[xc[3] + yc[2] + zc[2]] )
189                                                 + v[3] * ( u[0] * data[xc[0] + yc[3] + zc[2]] + u[1] * data[xc[1] + yc[3] + zc[2]] + u[2] * data[xc[2] + yc[3] + zc[2]] + u[3] * data[xc[3] + yc[3] + zc[2]] ) )
190                          + w[3] * (   v[0] * ( u[0] * data[xc[0] + yc[0] + zc[3]] + u[1] * data[xc[1] + yc[0] + zc[3]] + u[2] * data[xc[2] + yc[0] + zc[3]] + u[3] * data[xc[3] + yc[0] + zc[3]] )
191                                                 + v[1] * ( u[0] * data[xc[0] + yc[1] + zc[3]] + u[1] * data[xc[1] + yc[1] + zc[3]] + u[2] * data[xc[2] + yc[1] + zc[3]] + u[3] * data[xc[3] + yc[1] + zc[3]] )
192                                                 + v[2] * ( u[0] * data[xc[0] + yc[2] + zc[3]] + u[1] * data[xc[1] + yc[2] + zc[3]] + u[2] * data[xc[2] + yc[2] + zc[3]] + u[3] * data[xc[3] + yc[2] + zc[3]] )
193                                                 + v[3] * ( u[0] * data[xc[0] + yc[3] + zc[3]] + u[1] * data[xc[1] + yc[3] + zc[3]] + u[2] * data[xc[2] + yc[3] + zc[3]] + u[3] * data[xc[3] + yc[3] + zc[3]] ) );
194
195         }
196         return 0.f;
197 }