3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * Contributor(s): Campbell Barton <ideasman42@gmail.com>
24 * ***** END GPL LICENSE BLOCK *****
31 #include <sys/types.h>
33 #include "MEM_guardedalloc.h"
36 #include "DNA_cloth_types.h"
37 #include "DNA_modifier_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_object_force.h"
40 #include "DNA_particle_types.h"
41 #include "DNA_scene_types.h"
43 #include "BLI_blenlib.h"
45 #include "BKE_cloth.h"
46 #include "BKE_depsgraph.h"
47 #include "BKE_global.h"
48 #include "BKE_library.h"
50 #include "BKE_modifier.h"
51 #include "BKE_object.h"
52 #include "BKE_particle.h"
53 #include "BKE_pointcache.h"
54 #include "BKE_softbody.h"
55 #include "BKE_utildefines.h"
59 /* needed for directory lookup */
63 #include "BLI_winstuff.h"
66 /* untitled blend's need getpid for a unique name */
75 void BKE_ptcache_id_from_softbody(PTCacheID *pid, Object *ob, SoftBody *sb)
77 ParticleSystemModifierData *psmd;
81 memset(pid, 0, sizeof(PTCacheID));
85 pid->type= PTCACHE_TYPE_SOFTBODY;
86 pid->cache= sb->pointcache;
89 psmd= psys_get_modifier(ob, sb->particles);
90 pid->stack_index= modifiers_indexInObject(ob, (ModifierData*)psmd);
93 for(a=0, md=ob->modifiers.first; md; md=md->next, a++) {
94 if(md->type == eModifierType_Softbody) {
102 void BKE_ptcache_id_from_particles(PTCacheID *pid, Object *ob, ParticleSystem *psys)
104 ParticleSystemModifierData *psmd= psys_get_modifier(ob, psys);
106 memset(pid, 0, sizeof(PTCacheID));
110 pid->type= PTCACHE_TYPE_PARTICLES;
111 pid->stack_index= modifiers_indexInObject(ob, (ModifierData *)psmd);
112 pid->cache= psys->pointcache;
115 void BKE_ptcache_id_from_cloth(PTCacheID *pid, Object *ob, ClothModifierData *clmd)
117 memset(pid, 0, sizeof(PTCacheID));
121 pid->type= PTCACHE_TYPE_CLOTH;
122 pid->stack_index= modifiers_indexInObject(ob, (ModifierData *)clmd);
123 pid->cache= clmd->point_cache;
126 void BKE_ptcache_ids_from_object(ListBase *lb, Object *ob)
129 ParticleSystem *psys;
132 lb->first= lb->last= NULL;
135 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
136 BKE_ptcache_id_from_softbody(pid, ob, ob->soft);
137 BLI_addtail(lb, pid);
140 for(psys=ob->particlesystem.first; psys; psys=psys->next) {
141 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
142 BKE_ptcache_id_from_particles(pid, ob, psys);
143 BLI_addtail(lb, pid);
146 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
147 BKE_ptcache_id_from_softbody(pid, ob, psys->soft);
148 BLI_addtail(lb, pid);
152 for(md=ob->modifiers.first; md; md=md->next) {
153 if(md->type == eModifierType_Cloth) {
154 pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
155 BKE_ptcache_id_from_cloth(pid, ob, (ClothModifierData*)md);
156 BLI_addtail(lb, pid);
161 /* Takes an Object ID and returns a unique name
163 - cfra: frame for the cache, can be negative
164 - stack_index: index in the modifier stack. we can have cache for more then one stack_index
167 static int ptcache_path(PTCacheID *pid, char *filename)
172 lib= (pid)? pid->ob->id.lib: NULL;
174 if (G.relbase_valid || lib) {
175 char dir[FILE_MAX], file[FILE_MAX]; /* we dont want the dir, only the file */
178 blendfilename= (lib)? lib->filename: G.sce;
180 BLI_split_dirfile_basic(blendfilename, NULL, file);
187 sprintf(filename, "//"PTCACHE_PATH"%s", file); /* add blend file name to pointcache dir */
188 BLI_add_slash(filename);
189 BLI_convertstringcode(filename, blendfilename, 0);
190 return strlen(filename);
193 /* use the temp path. this is weak but better then not using point cache at all */
194 /* btempdir is assumed to exist and ALWAYS has a trailing slash */
195 sprintf(filename, "%s"PTCACHE_PATH"%d", btempdir, abs(getpid()));
196 BLI_add_slash(filename);
197 return strlen(filename);
200 static int BKE_ptcache_id_filename(PTCacheID *pid, char *filename, int cfra, short do_path, short do_ext)
208 /*if (!G.relbase_valid) return 0; *//* save blend file before using pointcache */
210 /* start with temp dir */
212 len = ptcache_path(pid, filename);
215 idname = (pid->ob->id.name+2);
216 /* convert chars to hex so they are always a valid filename */
217 while('\0' != *idname) {
218 sprintf(newname, "%02X", (char)(*idname++));
224 sprintf(newname, "_%06d_%02d"PTCACHE_EXT, cfra, pid->stack_index); /* always 6 chars */
228 return len; /* make sure the above string is always 16 chars */
231 /* youll need to close yourself after! */
232 PTCacheFile *BKE_ptcache_file_open(PTCacheID *pid, int mode, int cfra)
236 char filename[(FILE_MAXDIR+FILE_MAXFILE)*2];
238 /* don't allow writing for linked objects */
239 if(pid->ob->id.lib && mode == PTCACHE_FILE_WRITE)
242 /*if (!G.relbase_valid) return NULL; *//* save blend file before using pointcache */
244 BKE_ptcache_id_filename(pid, filename, cfra, 1, 1);
246 if (mode==PTCACHE_FILE_READ) {
247 if (!BLI_exists(filename)) {
250 fp = fopen(filename, "rb");
251 } else if (mode==PTCACHE_FILE_WRITE) {
252 BLI_make_existing_file(filename); /* will create the dir if needs be, same as //textures is created */
253 fp = fopen(filename, "wb");
259 pf= MEM_mallocN(sizeof(PTCacheFile), "PTCacheFile");
265 void BKE_ptcache_file_close(PTCacheFile *pf)
271 int BKE_ptcache_file_read_floats(PTCacheFile *pf, float *f, int tot)
273 return (fread(f, sizeof(float), tot, pf->fp) == tot);
276 int BKE_ptcache_file_write_floats(PTCacheFile *pf, float *f, int tot)
278 return (fwrite(f, sizeof(float), tot, pf->fp) == tot);
281 /* youll need to close yourself after!
282 * mode - PTCACHE_CLEAR_ALL,
286 void BKE_ptcache_id_clear(PTCacheID *pid, int mode, int cfra)
288 int len; /* store the length of the string */
290 /* mode is same as fopen's modes */
294 char filename[(FILE_MAXDIR+FILE_MAXFILE)*2];
295 char path_full[(FILE_MAXDIR+FILE_MAXFILE)*2];
300 /* don't allow clearing for linked objects */
304 /*if (!G.relbase_valid) return; *//* save blend file before using pointcache */
306 /* clear all files in the temp dir with the prefix of the ID and the ".bphys" suffix */
308 case PTCACHE_CLEAR_ALL:
309 case PTCACHE_CLEAR_BEFORE:
310 case PTCACHE_CLEAR_AFTER:
311 ptcache_path(pid, path);
313 len = BKE_ptcache_id_filename(pid, filename, cfra, 0, 0); /* no path */
319 while ((de = readdir(dir)) != NULL) {
320 if (strstr(de->d_name, PTCACHE_EXT)) { /* do we have the right extension?*/
321 if (strncmp(filename, de->d_name, len ) == 0) { /* do we have the right prefix */
322 if (mode == PTCACHE_CLEAR_ALL) {
323 BLI_join_dirfile(path_full, path, de->d_name);
324 BLI_delete(path_full, 0, 0);
326 /* read the number of the file */
327 int frame, len2 = strlen(de->d_name);
329 if (len2 > 15) { /* could crash if trying to copy a string out of this range*/
330 strncpy(num, de->d_name + (strlen(de->d_name) - 15), 6);
333 if((mode==PTCACHE_CLEAR_BEFORE && frame < cfra) ||
334 (mode==PTCACHE_CLEAR_AFTER && frame > cfra) ) {
336 BLI_join_dirfile(path_full, path, de->d_name);
337 BLI_delete(path_full, 0, 0);
347 case PTCACHE_CLEAR_FRAME:
348 len = BKE_ptcache_id_filename(pid, filename, cfra, 1, 1); /* no path */
349 BLI_delete(filename, 0, 0);
354 int BKE_ptcache_id_exist(PTCacheID *pid, int cfra)
356 char filename[(FILE_MAXDIR+FILE_MAXFILE)*2];
361 BKE_ptcache_id_filename(pid, filename, cfra, 1, 1);
363 return BLI_exists(filename);
366 void BKE_ptcache_id_time(PTCacheID *pid, float cfra, int *startframe, int *endframe, float *timescale)
370 float offset, time, nexttime;
372 /* time handling for point cache:
373 * - simulation time is scaled by result of bsystem_time
374 * - for offsetting time only time offset is taken into account, since
375 * that's always the same and can't be animated. a timeoffset which
376 * varies over time is not simpe to support.
377 * - field and motion blur offsets are currently ignored, proper solution
378 * is probably to interpolate results from two frames for that ..
385 time= bsystem_time(ob, cfra, 0.0f);
386 nexttime= bsystem_time(ob, cfra+1.0f, 0.0f);
388 *timescale= MAX2(nexttime - time, 0.0f);
391 if(startframe && endframe) {
392 *startframe= cache->startframe;
393 *endframe= cache->endframe;
395 if ((ob->ipoflag & OB_OFFS_PARENT) && (ob->partype & PARSLOW)==0) {
396 offset= give_timeoffset(ob);
398 *startframe += (int)(offset+0.5f);
399 *endframe += (int)(offset+0.5f);
404 int BKE_ptcache_id_reset(PTCacheID *pid, int mode)
416 if(mode == PTCACHE_RESET_DEPSGRAPH) {
417 if(!(cache->flag & PTCACHE_BAKED) && !BKE_ptcache_get_continue_physics()) {
422 cache->flag |= PTCACHE_OUTDATED;
424 else if(mode == PTCACHE_RESET_BAKED) {
425 if(!BKE_ptcache_get_continue_physics()) {
430 cache->flag |= PTCACHE_OUTDATED;
432 else if(mode == PTCACHE_RESET_OUTDATED) {
435 if(cache->flag & PTCACHE_OUTDATED)
436 if(!(cache->flag & PTCACHE_BAKED))
441 cache->flag &= ~(PTCACHE_OUTDATED|PTCACHE_SIMULATION_VALID);
444 if(pid->type == PTCACHE_TYPE_CLOTH)
445 cloth_free_modifier(pid->ob, pid->data);
446 else if(pid->type == PTCACHE_TYPE_SOFTBODY)
447 sbFreeSimulation(pid->data);
448 else if(pid->type == PTCACHE_TYPE_PARTICLES)
449 psys_reset(pid->data, PSYS_RESET_DEPSGRAPH);
452 BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
454 return (reset || clear);
457 int BKE_ptcache_object_reset(Object *ob, int mode)
460 ParticleSystem *psys;
467 BKE_ptcache_id_from_softbody(&pid, ob, ob->soft);
468 reset |= BKE_ptcache_id_reset(&pid, mode);
471 for(psys=ob->particlesystem.first; psys; psys=psys->next) {
472 BKE_ptcache_id_from_particles(&pid, ob, psys);
473 reset |= BKE_ptcache_id_reset(&pid, mode);
476 BKE_ptcache_id_from_softbody(&pid, ob, psys->soft);
477 reset |= BKE_ptcache_id_reset(&pid, mode);
481 for(md=ob->modifiers.first; md; md=md->next) {
482 if(md->type == eModifierType_Cloth) {
483 BKE_ptcache_id_from_cloth(&pid, ob, (ClothModifierData*)md);
484 reset |= BKE_ptcache_id_reset(&pid, mode);
491 /* Use this when quitting blender, with unsaved files */
492 void BKE_ptcache_remove(void)
495 char path_full[FILE_MAX];
498 ptcache_path(NULL, path);
500 if (BLI_exist(path)) {
501 /* The pointcache dir exists? - remove all pointcache */
510 while ((de = readdir(dir)) != NULL) {
511 if( strcmp(de->d_name, ".")==0 || strcmp(de->d_name, "..")==0) {
513 } else if (strstr(de->d_name, PTCACHE_EXT)) { /* do we have the right extension?*/
514 BLI_join_dirfile(path_full, path, de->d_name);
515 BLI_delete(path_full, 0, 0);
517 rmdir = 0; /* unknown file, dont remove the dir */
523 rmdir = 0; /* path dosnt exist */
527 BLI_delete(path, 1, 0);
531 /* Continuous Interaction */
533 static int CONTINUE_PHYSICS = 0;
535 void BKE_ptcache_set_continue_physics(int enable)
539 if(CONTINUE_PHYSICS != enable) {
540 CONTINUE_PHYSICS = enable;
542 if(CONTINUE_PHYSICS == 0) {
543 for(ob=G.main->object.first; ob; ob=ob->id.next)
544 if(BKE_ptcache_object_reset(ob, PTCACHE_RESET_OUTDATED))
545 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
550 int BKE_ptcache_get_continue_physics()
552 return CONTINUE_PHYSICS;
557 PointCache *BKE_ptcache_add()
561 cache= MEM_callocN(sizeof(PointCache), "PointCache");
562 cache->startframe= 1;
563 cache->endframe= 250;
568 void BKE_ptcache_free(PointCache *cache)
573 PointCache *BKE_ptcache_copy(PointCache *cache)
577 ncache= MEM_dupallocN(cache);