Camera tracking: libmv distortion API now also uses camera intrinsics
[blender.git] / source / blender / render / intern / source / voxeldata.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): Raul Fernandez Hernandez (Farsthary), Matt Ebb.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/render/intern/source/voxeldata.c
29  *  \ingroup render
30  */
31
32
33 #include <math.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_math.h"
40 #include "BLI_blenlib.h"
41 #include "BLI_voxel.h"
42 #include "BLI_utildefines.h"
43
44 #include "IMB_imbuf.h"
45 #include "IMB_imbuf_types.h"
46
47 #include "BKE_global.h"
48 #include "BKE_image.h"
49 #include "BKE_main.h"
50 #include "BKE_modifier.h"
51
52 #include "smoke_API.h"
53
54 #include "DNA_texture_types.h"
55 #include "DNA_object_force.h"
56 #include "DNA_object_types.h"
57 #include "DNA_modifier_types.h"
58 #include "DNA_smoke_types.h"
59
60
61 #include "render_types.h"
62 #include "renderdatabase.h"
63 #include "texture.h"
64 #include "voxeldata.h"
65
66 static int is_vd_res_ok(VoxelData *vd)
67 {
68         /* arbitrary large value so corrupt headers don't break */
69         const int min = 1, max = 100000;
70         return (vd->resol[0] >= min && vd->resol[0] <= max) &&
71                (vd->resol[1] >= min && vd->resol[1] <= max) &&
72                (vd->resol[2] >= min && vd->resol[2] <= max);
73 }
74
75 /* use size_t because the result may exceed INT_MAX */
76 static size_t vd_resol_size(VoxelData *vd)
77 {
78         return (size_t)vd->resol[0] * (size_t)vd->resol[1] * (size_t)vd->resol[2];
79 }
80
81 static int load_frame_blendervoxel(VoxelData *vd, FILE *fp, int frame)
82 {       
83         const size_t size = vd_resol_size(vd);
84         size_t offset = sizeof(VoxelDataHeader);
85         
86         if (is_vd_res_ok(vd) == FALSE)
87                 return 0;
88
89         vd->dataset = MEM_mapallocN(sizeof(float) * size, "voxel dataset");
90         if (vd->dataset == NULL) return 0;
91
92         if (fseek(fp, frame * size * sizeof(float) + offset, 0) == -1)
93                 return 0;
94         if (fread(vd->dataset, sizeof(float), size, fp) != size)
95                 return 0;
96         
97         vd->cachedframe = frame;
98         vd->ok = 1;
99         return 1;
100 }
101
102 static int load_frame_raw8(VoxelData *vd, FILE *fp, int frame)
103 {
104         const size_t size = vd_resol_size(vd);
105         char *data_c;
106         int i;
107
108         if (is_vd_res_ok(vd) == FALSE)
109                 return 0;
110
111         vd->dataset = MEM_mapallocN(sizeof(float) * size, "voxel dataset");
112         if (vd->dataset == NULL) return 0;
113         data_c = (char *)MEM_mallocN(sizeof(char) * size, "temporary voxel file reading storage");
114         if (data_c == NULL) {
115                 MEM_freeN(vd->dataset);
116                 vd->dataset = NULL;
117                 return 0;
118         }
119
120         if (fseek(fp, (frame - 1) * size * sizeof(char), 0) == -1) {
121                 MEM_freeN(data_c);
122                 MEM_freeN(vd->dataset);
123                 vd->dataset = NULL;
124                 return 0;
125         }
126         if (fread(data_c, sizeof(char), size, fp) != size) {
127                 MEM_freeN(data_c);
128                 MEM_freeN(vd->dataset);
129                 vd->dataset = NULL;
130                 return 0;
131         }
132         
133         for (i = 0; i < size; i++) {
134                 vd->dataset[i] = (float)data_c[i] / 255.f;
135         }
136         MEM_freeN(data_c);
137         
138         vd->cachedframe = frame;
139         vd->ok = 1;
140         return 1;
141 }
142
143 static void load_frame_image_sequence(VoxelData *vd, Tex *tex)
144 {
145         ImBuf *ibuf;
146         Image *ima = tex->ima;
147         ImageUser *tiuser = &tex->iuser;
148         ImageUser iuser = *(tiuser);
149         int x = 0, y = 0, z = 0;
150         float *rf;
151
152         if (!ima || !tiuser) return;
153         if (iuser.frames == 0) return;
154         
155         ima->source = IMA_SRC_SEQUENCE;
156         iuser.framenr = 1 + iuser.offset;
157
158         /* find the first valid ibuf and use it to initialize the resolution of the data set */
159         /* need to do this in advance so we know how much memory to allocate */
160         ibuf = BKE_image_acquire_ibuf(ima, &iuser, NULL);
161         while (!ibuf && (iuser.framenr < iuser.frames)) {
162                 iuser.framenr++;
163                 ibuf = BKE_image_acquire_ibuf(ima, &iuser, NULL);
164         }
165         if (!ibuf) return;
166         if (!ibuf->rect_float) IMB_float_from_rect(ibuf);
167         
168         vd->flag |= TEX_VD_STILL;
169         vd->resol[0] = ibuf->x;
170         vd->resol[1] = ibuf->y;
171         vd->resol[2] = iuser.frames;
172         vd->dataset = MEM_mapallocN(sizeof(float) * vd_resol_size(vd), "voxel dataset");
173         
174         for (z = 0; z < iuser.frames; z++) {
175                 /* get a new ibuf for each frame */
176                 if (z > 0) {
177                         iuser.framenr++;
178                         BKE_image_release_ibuf(ima, ibuf, NULL);
179                         ibuf = BKE_image_acquire_ibuf(ima, &iuser, NULL);
180                         if (!ibuf) break;
181                         if (!ibuf->rect_float) IMB_float_from_rect(ibuf);
182                 }
183                 rf = ibuf->rect_float;
184                 
185                 for (y = 0; y < ibuf->y; y++) {
186                         for (x = 0; x < ibuf->x; x++) {
187                                 /* currently averaged to monchrome */
188                                 vd->dataset[BLI_VOXEL_INDEX(x, y, z, vd->resol)] = (rf[0] + rf[1] + rf[2]) / 3.0f;
189                                 rf += 4;
190                         }
191                 }
192                 
193                 BKE_image_free_anim_ibufs(ima, iuser.framenr);
194         }
195
196         BKE_image_release_ibuf(ima, ibuf, NULL);
197
198         vd->ok = 1;
199         return;
200 }
201
202 static int read_voxeldata_header(FILE *fp, struct VoxelData *vd)
203 {
204         VoxelDataHeader *h = (VoxelDataHeader *)MEM_mallocN(sizeof(VoxelDataHeader), "voxel data header");
205         
206         rewind(fp);
207         if (fread(h, sizeof(VoxelDataHeader), 1, fp) != 1) {
208                 MEM_freeN(h);
209                 return 0;
210         }
211         
212         vd->resol[0] = h->resolX;
213         vd->resol[1] = h->resolY;
214         vd->resol[2] = h->resolZ;
215
216         MEM_freeN(h);
217         return 1;
218 }
219
220 static void init_frame_smoke(VoxelData *vd, float cfra)
221 {
222 #ifdef WITH_SMOKE
223         Object *ob;
224         ModifierData *md;
225         
226         vd->dataset = NULL;
227         if (vd->object == NULL) return;
228         ob = vd->object;
229         
230         /* draw code for smoke */
231         if ((md = (ModifierData *)modifiers_findByType(ob, eModifierType_Smoke))) {
232                 SmokeModifierData *smd = (SmokeModifierData *)md;
233                 SmokeDomainSettings *sds = smd->domain;
234                 
235                 if (sds && sds->fluid) {
236                         if (cfra < sds->point_cache[0]->startframe)
237                                 ;  /* don't show smoke before simulation starts, this could be made an option in the future */
238                         else if (vd->smoked_type == TEX_VD_SMOKEHEAT) {
239                                 size_t totRes;
240                                 size_t i;
241                                 float *heat;
242
243                                 if (!smoke_has_heat(sds->fluid)) return;
244
245                                 copy_v3_v3_int(vd->resol, sds->res);
246                                 totRes = vd_resol_size(vd);
247                                 vd->dataset = MEM_mapallocN(sizeof(float) * (totRes), "smoke data");
248                                 /* get heat data */
249                                 heat = smoke_get_heat(sds->fluid);
250
251                                 /* scale heat values from -2.0-2.0 to 0.0-1.0 */
252                                 for (i = 0; i < totRes; i++) {
253                                         vd->dataset[i] = (heat[i] + 2.0f) / 4.0f;
254                                 }
255                         }
256                         else if (vd->smoked_type == TEX_VD_SMOKEVEL) {
257                                 size_t totRes;
258                                 size_t i;
259                                 float *xvel, *yvel, *zvel;
260
261                                 copy_v3_v3_int(vd->resol, sds->res);
262                                 totRes = vd_resol_size(vd);
263                                 vd->dataset = MEM_mapallocN(sizeof(float) * (totRes), "smoke data");
264                                 /* get velocity data */
265                                 xvel = smoke_get_velocity_x(sds->fluid);
266                                 yvel = smoke_get_velocity_y(sds->fluid);
267                                 zvel = smoke_get_velocity_z(sds->fluid);
268
269                                 /* map velocities between 0 and 0.3f */
270                                 for (i = 0; i < totRes; i++) {
271                                         vd->dataset[i] = sqrtf(xvel[i] * xvel[i] + yvel[i] * yvel[i] + zvel[i] * zvel[i]) * 3.0f;
272                                 }
273
274                         }
275                         else if (vd->smoked_type == TEX_VD_SMOKEFLAME) {
276                                 size_t totRes;
277                                 float *flame;
278
279                                 if (sds->flags & MOD_SMOKE_HIGHRES) {
280                                         if (!smoke_turbulence_has_fuel(sds->wt)) return;
281                                         smoke_turbulence_get_res(sds->wt, vd->resol);
282                                         flame = smoke_turbulence_get_flame(sds->wt);
283                                 }
284                                 else {
285                                         if (!smoke_has_fuel(sds->fluid)) return;
286                                         copy_v3_v3_int(vd->resol, sds->res);
287                                         flame = smoke_get_flame(sds->fluid);
288                                 }
289
290                                 /* always store copy, as smoke internal data can change */
291                                 totRes = vd_resol_size(vd);
292                                 vd->dataset = MEM_mapallocN(sizeof(float)*(totRes), "smoke data");
293                                 memcpy(vd->dataset, flame, sizeof(float)*totRes);
294                         }
295                         else {
296                                 size_t totCells;
297                                 int depth = 4;
298                                 vd->data_type = TEX_VD_RGBA_PREMUL;
299
300                                 /* data resolution */
301                                 if (sds->flags & MOD_SMOKE_HIGHRES) {
302                                         smoke_turbulence_get_res(sds->wt, vd->resol);
303                                 }
304                                 else {
305                                         copy_v3_v3_int(vd->resol, sds->res);
306                                 }
307
308                                 /* TODO: is_vd_res_ok(rvd) doesnt check this resolution */
309                                 totCells = vd_resol_size(vd) * depth;
310                                 /* always store copy, as smoke internal data can change */
311                                 vd->dataset = MEM_mapallocN(sizeof(float) * totCells, "smoke data");
312
313                                 if (sds->flags & MOD_SMOKE_HIGHRES) {
314                                         if (smoke_turbulence_has_colors(sds->wt)) {
315                                                 smoke_turbulence_get_rgba(sds->wt, vd->dataset, 1);
316                                         }
317                                         else {
318                                                 smoke_turbulence_get_rgba_from_density(sds->wt, sds->active_color, vd->dataset, 1);
319                                         }
320                                 }
321                                 else {
322                                         if (smoke_has_colors(sds->fluid)) {
323                                                 smoke_get_rgba(sds->fluid, vd->dataset, 1);
324                                         }
325                                         else {
326                                                 smoke_get_rgba_from_density(sds->fluid, sds->active_color, vd->dataset, 1);
327                                         }
328                                 }
329                         }  /* end of fluid condition */
330                 }
331         }
332         
333         vd->ok = 1;
334
335 #else // WITH_SMOKE
336         (void)vd;
337         (void)cfra;
338
339         vd->dataset = NULL;
340 #endif
341 }
342
343 void cache_voxeldata(Tex *tex, int scene_frame)
344 {       
345         VoxelData *vd = tex->vd;
346         FILE *fp;
347         int curframe;
348         char path[sizeof(vd->source_path)];
349         
350         /* only re-cache if dataset needs updating */
351         if ((vd->flag & TEX_VD_STILL) || (vd->cachedframe == scene_frame))
352                 if (vd->ok) return;
353         
354         /* clear out old cache, ready for new */
355         if (vd->dataset) {
356                 MEM_freeN(vd->dataset);
357                 vd->dataset = NULL;
358         }
359         /* reset data_type */
360         vd->data_type = TEX_VD_INTENSITY;
361
362         if (vd->flag & TEX_VD_STILL)
363                 curframe = vd->still_frame;
364         else
365                 curframe = scene_frame;
366         
367         BLI_strncpy(path, vd->source_path, sizeof(path));
368         
369         switch (vd->file_format) {
370                 case TEX_VD_IMAGE_SEQUENCE:
371                         load_frame_image_sequence(vd, tex);
372                         return;
373                 case TEX_VD_SMOKE:
374                         init_frame_smoke(vd, scene_frame);
375                         return;
376                 case TEX_VD_BLENDERVOXEL:
377                         BLI_path_abs(path, G.main->name);
378                         if (!BLI_exists(path)) return;
379                         fp = BLI_fopen(path, "rb");
380                         if (!fp) return;
381                         
382                         if (read_voxeldata_header(fp, vd))
383                                 load_frame_blendervoxel(vd, fp, curframe - 1);
384
385                         fclose(fp);
386                         return;
387                 case TEX_VD_RAW_8BIT:
388                         BLI_path_abs(path, G.main->name);
389                         if (!BLI_exists(path)) return;
390                         fp = BLI_fopen(path, "rb");
391                         if (!fp) return;
392                         
393                         load_frame_raw8(vd, fp, curframe);
394                         fclose(fp);
395                         return;
396         }
397 }
398
399 void make_voxeldata(struct Render *re)
400 {
401         Tex *tex;
402         
403         re->i.infostr = "Loading voxel datasets";
404         re->stats_draw(re->sdh, &re->i);
405         
406         /* XXX: should be doing only textures used in this render */
407         for (tex = re->main->tex.first; tex; tex = tex->id.next) {
408                 if (tex->id.us && tex->type == TEX_VOXELDATA) {
409                         cache_voxeldata(tex, re->r.cfra);
410                 }
411         }
412         
413         re->i.infostr = NULL;
414         re->stats_draw(re->sdh, &re->i);
415         
416 }
417
418 int voxeldatatex(struct Tex *tex, const float texvec[3], struct TexResult *texres)
419 {        
420         VoxelData *vd = tex->vd;
421         float co[3], offset[3] = {0.5, 0.5, 0.5}, a;
422         int retval = (vd->data_type == TEX_VD_RGBA_PREMUL) ? TEX_RGB : TEX_INT;
423         int depth = (vd->data_type == TEX_VD_RGBA_PREMUL) ? 4 : 1;
424         int ch;
425
426         if (vd->dataset == NULL) {
427                 texres->tin = 0.0f;
428                 return 0;
429         }
430         
431         /* scale lookup from 0.0-1.0 (original location) to -1.0, 1.0, consistent with image texture tex coords */
432         /* in implementation this works backwards, bringing sample locations from -1.0, 1.0
433          * to the range 0.0, 1.0, before looking up in the voxel structure. */
434         copy_v3_v3(co, texvec);
435         mul_v3_fl(co, 0.5f);
436         add_v3_v3(co, offset);
437
438         /* co is now in the range 0.0, 1.0 */
439         switch (vd->extend) {
440                 case TEX_CLIP:
441                 {
442                         if ((co[0] < 0.f || co[0] > 1.f) || (co[1] < 0.f || co[1] > 1.f) || (co[2] < 0.f || co[2] > 1.f)) {
443                                 texres->tin = 0.f;
444                                 return retval;
445                         }
446                         break;
447                 }
448                 case TEX_REPEAT:
449                 {
450                         co[0] = co[0] - floorf(co[0]);
451                         co[1] = co[1] - floorf(co[1]);
452                         co[2] = co[2] - floorf(co[2]);
453                         break;
454                 }
455                 case TEX_EXTEND:
456                 {
457                         CLAMP(co[0], 0.f, 1.f);
458                         CLAMP(co[1], 0.f, 1.f);
459                         CLAMP(co[2], 0.f, 1.f);
460                         break;
461                 }
462         }
463
464         for (ch = 0; ch < depth; ch++) {
465                 float *dataset = vd->dataset + ch*vd->resol[0]*vd->resol[1]*vd->resol[2];
466                 float *result = &texres->tin;
467
468                 if (vd->data_type == TEX_VD_RGBA_PREMUL) {
469                         switch (ch) {
470                                 case 0:
471                                         result = &texres->tr;
472                                         break;
473                                 case 1:
474                                         result = &texres->tg;
475                                         break;
476                                 case 2:
477                                         result = &texres->tb;
478                                         break;
479                         }
480                 }
481
482                 switch (vd->interp_type) {
483                         case TEX_VD_NEARESTNEIGHBOR:
484                                 *result = BLI_voxel_sample_nearest(dataset, vd->resol, co);
485                                 break;  
486                         case TEX_VD_LINEAR:
487                                 *result = BLI_voxel_sample_trilinear(dataset, vd->resol, co);
488                                 break;
489                         case TEX_VD_QUADRATIC:
490                                 *result = BLI_voxel_sample_triquadratic(dataset, vd->resol, co);
491                                 break;
492                         case TEX_VD_TRICUBIC_CATROM:
493                         case TEX_VD_TRICUBIC_BSPLINE:
494                                 *result = BLI_voxel_sample_tricubic(dataset, vd->resol, co, (vd->interp_type == TEX_VD_TRICUBIC_BSPLINE));
495                                 break;
496                 }
497         }
498
499         a = texres->tin;
500         texres->tin *= vd->int_multiplier;
501         BRICONT;
502         
503         if (vd->data_type == TEX_VD_RGBA_PREMUL) {
504                 /* unmultiply */
505                 if (a>0.001f) {
506                         texres->tr /= a;
507                         texres->tg /= a;
508                         texres->tb /= a;
509                 }
510                 texres->talpha = 1;
511         }
512         else {
513                 texres->tr = texres->tin;
514                 texres->tg = texres->tin;
515                 texres->tb = texres->tin;
516         }
517
518         texres->ta = texres->tin;
519         BRICONTRGB;
520         
521         return retval;
522 }