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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
|
/**
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* Contributor(s): Campbell Barton <ideasman42@gmail.com>
*
* ***** END GPL LICENSE BLOCK *****
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "MEM_guardedalloc.h"
#include "DNA_ID.h"
#include "DNA_cloth_types.h"
#include "DNA_modifier_types.h"
#include "DNA_object_types.h"
#include "DNA_object_force.h"
#include "DNA_particle_types.h"
#include "DNA_scene_types.h"
#include "BLI_blenlib.h"
#include "BKE_cloth.h"
#include "BKE_depsgraph.h"
#include "BKE_global.h"
#include "BKE_library.h"
#include "BKE_main.h"
#include "BKE_modifier.h"
#include "BKE_object.h"
#include "BKE_particle.h"
#include "BKE_pointcache.h"
#include "BKE_scene.h"
#include "BKE_softbody.h"
#include "BKE_utildefines.h"
#include "BLI_blenlib.h"
/* needed for directory lookup */
#ifndef WIN32
#include <dirent.h>
#else
#include "BLI_winstuff.h"
#endif
/* untitled blend's need getpid for a unique name */
#ifdef WIN32
#include <process.h>
#else
#include <unistd.h>
#endif
#ifdef _WIN32
#ifndef snprintf
#define snprintf _snprintf
#endif
#endif
/* Creating ID's */
void BKE_ptcache_id_from_softbody(PTCacheID *pid, Object *ob, SoftBody *sb)
{
ParticleSystemModifierData *psmd;
ModifierData *md;
int a;
memset(pid, 0, sizeof(PTCacheID));
pid->ob= ob;
pid->data= sb;
pid->type= PTCACHE_TYPE_SOFTBODY;
pid->cache= sb->pointcache;
if(sb->particles) {
psmd= psys_get_modifier(ob, sb->particles);
pid->stack_index= modifiers_indexInObject(ob, (ModifierData*)psmd);
}
else {
for(a=0, md=ob->modifiers.first; md; md=md->next, a++) {
if(md->type == eModifierType_Softbody) {
pid->stack_index = a;
break;
}
}
}
}
void BKE_ptcache_id_from_particles(PTCacheID *pid, Object *ob, ParticleSystem *psys)
{
ParticleSystemModifierData *psmd= psys_get_modifier(ob, psys);
memset(pid, 0, sizeof(PTCacheID));
pid->ob= ob;
pid->data= psys;
pid->type= PTCACHE_TYPE_PARTICLES;
pid->stack_index= modifiers_indexInObject(ob, (ModifierData *)psmd);
pid->cache= psys->pointcache;
}
void BKE_ptcache_id_from_cloth(PTCacheID *pid, Object *ob, ClothModifierData *clmd)
{
memset(pid, 0, sizeof(PTCacheID));
pid->ob= ob;
pid->data= clmd;
pid->type= PTCACHE_TYPE_CLOTH;
pid->stack_index= modifiers_indexInObject(ob, (ModifierData *)clmd);
pid->cache= clmd->point_cache;
}
void BKE_ptcache_ids_from_object(ListBase *lb, Object *ob)
{
PTCacheID *pid;
ParticleSystem *psys;
ModifierData *md;
lb->first= lb->last= NULL;
if(ob->soft) {
pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
BKE_ptcache_id_from_softbody(pid, ob, ob->soft);
BLI_addtail(lb, pid);
}
for(psys=ob->particlesystem.first; psys; psys=psys->next) {
pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
BKE_ptcache_id_from_particles(pid, ob, psys);
BLI_addtail(lb, pid);
if(psys->soft) {
pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
BKE_ptcache_id_from_softbody(pid, ob, psys->soft);
BLI_addtail(lb, pid);
}
}
for(md=ob->modifiers.first; md; md=md->next) {
if(md->type == eModifierType_Cloth) {
pid= MEM_callocN(sizeof(PTCacheID), "PTCacheID");
BKE_ptcache_id_from_cloth(pid, ob, (ClothModifierData*)md);
BLI_addtail(lb, pid);
}
}
}
/* Takes an Object ID and returns a unique name
- id: object id
- cfra: frame for the cache, can be negative
- stack_index: index in the modifier stack. we can have cache for more then one stack_index
*/
#define MAX_PTCACHE_PATH FILE_MAX
#define MAX_PTCACHE_FILE ((FILE_MAXDIR+FILE_MAXFILE)*2)
static int ptcache_path(PTCacheID *pid, char *filename)
{
Library *lib;
int i;
lib= (pid)? pid->ob->id.lib: NULL;
if (G.relbase_valid || lib) {
char file[MAX_PTCACHE_PATH]; /* we dont want the dir, only the file */
char *blendfilename;
blendfilename= (lib)? lib->filename: G.sce;
BLI_split_dirfile_basic(blendfilename, NULL, file);
i = strlen(file);
/* remove .blend */
if (i > 6)
file[i-6] = '\0';
snprintf(filename, MAX_PTCACHE_PATH, "//"PTCACHE_PATH"%s", file); /* add blend file name to pointcache dir */
BLI_convertstringcode(filename, blendfilename);
BLI_add_slash(filename);
return strlen(filename);
}
/* use the temp path. this is weak but better then not using point cache at all */
/* btempdir is assumed to exist and ALWAYS has a trailing slash */
snprintf(filename, MAX_PTCACHE_PATH, "%s"PTCACHE_PATH"%d", btempdir, abs(getpid()));
BLI_add_slash(filename);
return strlen(filename);
}
static int BKE_ptcache_id_filename(PTCacheID *pid, char *filename, int cfra, short do_path, short do_ext)
{
int len=0;
char *idname;
char *newname;
filename[0] = '\0';
newname = filename;
if (!G.relbase_valid) return 0; /* save blend file before using disk pointcache */
/* start with temp dir */
if (do_path) {
len = ptcache_path(pid, filename);
newname += len;
}
if(strcmp(pid->cache->name, "")==0) {
idname = (pid->ob->id.name+2);
/* convert chars to hex so they are always a valid filename */
while('\0' != *idname) {
snprintf(newname, MAX_PTCACHE_FILE, "%02X", (char)(*idname++));
newname+=2;
len += 2;
}
}
else {
int temp = strlen(pid->cache->name);
strcpy(newname, pid->cache->name);
newname+=temp;
len += temp;
}
if (do_ext) {
snprintf(newname, MAX_PTCACHE_FILE, "_%06d_%02d"PTCACHE_EXT, cfra, pid->stack_index); /* always 6 chars */
len += 16;
}
return len; /* make sure the above string is always 16 chars */
}
/* youll need to close yourself after! */
PTCacheFile *BKE_ptcache_file_open(PTCacheID *pid, int mode, int cfra)
{
PTCacheFile *pf;
FILE *fp = NULL;
char filename[(FILE_MAXDIR+FILE_MAXFILE)*2];
/* don't allow writing for linked objects */
if(pid->ob->id.lib && mode == PTCACHE_FILE_WRITE)
return NULL;
if (!G.relbase_valid) return NULL; /* save blend file before using disk pointcache */
BKE_ptcache_id_filename(pid, filename, cfra, 1, 1);
if (mode==PTCACHE_FILE_READ) {
if (!BLI_exists(filename)) {
return NULL;
}
fp = fopen(filename, "rb");
} else if (mode==PTCACHE_FILE_WRITE) {
BLI_make_existing_file(filename); /* will create the dir if needs be, same as //textures is created */
fp = fopen(filename, "wb");
}
if (!fp)
return NULL;
pf= MEM_mallocN(sizeof(PTCacheFile), "PTCacheFile");
pf->fp= fp;
return pf;
}
void BKE_ptcache_file_close(PTCacheFile *pf)
{
fclose(pf->fp);
MEM_freeN(pf);
}
int BKE_ptcache_file_read_floats(PTCacheFile *pf, float *f, int tot)
{
return (fread(f, sizeof(float), tot, pf->fp) == tot);
}
int BKE_ptcache_file_write_floats(PTCacheFile *pf, float *f, int tot)
{
return (fwrite(f, sizeof(float), tot, pf->fp) == tot);
}
static int ptcache_pid_elemsize(PTCacheID *pid)
{
if(pid->type==PTCACHE_TYPE_SOFTBODY)
return 0; // TODO
else if(pid->type==PTCACHE_TYPE_PARTICLES)
return sizeof(ParticleKey);
else if(pid->type==PTCACHE_TYPE_CLOTH)
return 9 * sizeof(float);
return 0;
}
static int ptcache_pid_totelem(PTCacheID *pid)
{
if(pid->type==PTCACHE_TYPE_SOFTBODY)
return 0; // TODO
else if(pid->type==PTCACHE_TYPE_PARTICLES) {
ParticleSystem *psys = pid->data;
return psys->totpart;
}
else if(pid->type==PTCACHE_TYPE_CLOTH)
return 0; // TODO
return 0;
}
void BKE_ptcache_update_info(PTCacheID *pid)
{
PointCache *cache = pid->cache;
int totframes = 0;
char mem_info[64];
if(cache->flag & PTCACHE_DISK_CACHE) {
int cfra = cache->startframe;
for(; cfra<=cache->endframe; cfra++) {
if(BKE_ptcache_id_exist(pid, cfra))
totframes++;
}
sprintf(mem_info, "%i frames on disk", totframes);
}
else {
PTCacheMem *pm = cache->mem_cache.first;
float framesize = 0.0f, bytes = 0.0f;
int mb;
if(pm)
framesize = (float)ptcache_pid_elemsize(pid) * (float)pm->totpoint;
for(; pm; pm=pm->next)
totframes++;
bytes = totframes * framesize;
mb = (bytes > 1024.0f * 1024.0f);
sprintf(mem_info, "%i frames in memory (%.1f %s)",
totframes,
bytes / (mb ? 1024.0f * 1024.0f : 1024.0f),
mb ? "Mb" : "kb");
}
if(cache->flag & PTCACHE_OUTDATED) {
sprintf(cache->info, "%s, cache is outdated!", mem_info);
}
else if(cache->flag & PTCACHE_FRAMES_SKIPPED) {
sprintf(cache->info, "%s, not exact since frame %i.", mem_info, cache->last_exact);
}
else
sprintf(cache->info, "%s.", mem_info);
}
/* reads cache from disk or memory */
/* possible to get old or interpolated result */
int BKE_ptcache_read_cache(PTCacheReader *reader)
{
PTCacheID *pid = reader->pid;
PTCacheFile *pf=NULL, *pf2=NULL;
PTCacheMem *pm=NULL, *pm2=NULL;
int totelem = reader->totelem;
float cfra = reader->cfra;
int cfrai = (int)cfra;
int elemsize = ptcache_pid_elemsize(pid);
int i, incr = elemsize / sizeof(float);
float frs_sec = reader->scene->r.frs_sec;
int cfra1=0, cfra2;
int ret = 0;
if(totelem == 0)
return 0;
/* first check if we have the actual frame cached */
if(cfra == (float)cfrai) {
if(pid->cache->flag & PTCACHE_DISK_CACHE) {
pf= BKE_ptcache_file_open(pid, PTCACHE_FILE_READ, cfrai);
}
else {
pm = pid->cache->mem_cache.first;
for(; pm; pm=pm->next) {
if(pm->frame == cfrai)
break;
}
}
}
/* if found, use exact frame */
if(pf || pm) {
float *data;
if(pm)
data = pm->data;
else
data = MEM_callocN(elemsize, "pointcache read data");
for(i=0; i<totelem; i++) {
if(pf) {
if(!BKE_ptcache_file_read_floats(pf, data, incr)) {
BKE_ptcache_file_close(pf);
MEM_freeN(data);
return 0;
}
reader->set_elem(i, reader->calldata, data);
}
else {
reader->set_elem(i, reader->calldata, data);
data += incr;
}
}
if(pf) {
BKE_ptcache_file_close(pf);
MEM_freeN(data);
}
ret = PTCACHE_READ_EXACT;
}
if(ret)
;
/* no exact cache frame found so try to find cached frames around cfra */
else if(pid->cache->flag & PTCACHE_DISK_CACHE) {
pf=NULL;
while(cfrai > pid->cache->startframe && !pf) {
cfrai--;
pf= BKE_ptcache_file_open(pid, PTCACHE_FILE_READ, cfrai);
cfra1 = cfrai;
}
if(reader->old_frame)
*(reader->old_frame) = cfrai;
cfrai = (int)cfra;
while(cfrai < pid->cache->endframe && !pf2) {
cfrai++;
pf2= BKE_ptcache_file_open(pid, PTCACHE_FILE_READ, cfrai);
cfra2 = cfrai;
}
}
else if(pid->cache->mem_cache.first){
pm = pid->cache->mem_cache.first;
while(pm->next && pm->next->frame < cfra)
pm= pm->next;
if(pm) {
if(reader->old_frame)
*(reader->old_frame) = pm->frame;
cfra1 = pm->frame;
}
pm2 = pid->cache->mem_cache.last;
if(pm2 && pm2->frame < cfra)
pm2 = NULL;
else {
while(pm2->prev && pm2->prev->frame > cfra)
pm2= pm2->prev;
if(pm2)
cfra2 = pm2->frame;
}
}
if(ret)
;
else if((pf && pf2) || (pm && pm2)) {
/* interpolate from nearest frames if cache isn't outdated */
float *data1, *data2;
if(pm) {
data1 = pm->data;
data2 = pm2->data;
}
else {
data1 = MEM_callocN(elemsize, "pointcache read data1");
data2 = MEM_callocN(elemsize, "pointcache read data2");
}
for(i=0; i<totelem; i++) {
if(pf && pf2) {
if(!BKE_ptcache_file_read_floats(pf, data1, incr)) {
BKE_ptcache_file_close(pf);
BKE_ptcache_file_close(pf2);
MEM_freeN(data1);
MEM_freeN(data2);
return 0;
}
if(!BKE_ptcache_file_read_floats(pf2, data2, incr)) {
BKE_ptcache_file_close(pf);
BKE_ptcache_file_close(pf2);
MEM_freeN(data1);
MEM_freeN(data2);
return 0;
}
reader->interpolate_elem(i, reader->calldata, frs_sec, cfra, (float)cfra1, (float)cfra2, data1, data2);
}
else {
reader->interpolate_elem(i, reader->calldata, frs_sec, cfra, (float)cfra1, (float)cfra2, data1, data2);
data1 += incr;
data2 += incr;
}
}
if(pf) {
BKE_ptcache_file_close(pf);
BKE_ptcache_file_close(pf2);
MEM_freeN(data1);
MEM_freeN(data2);
}
ret = PTCACHE_READ_INTERPOLATED;
}
else if(pf || pm) {
/* use last valid cache frame */
float *data;
/* don't read cache if allready simulated past cached frame */
if(cfra1 && cfra1 <= pid->cache->simframe) {
if(pf)
BKE_ptcache_file_close(pf);
if(pf2)
BKE_ptcache_file_close(pf2);
return 0;
}
if(pm)
data = pm->data;
else
data = MEM_callocN(elemsize, "pointcache read data");
for(i=0; i<totelem; i++) {
if(pf) {
if(!BKE_ptcache_file_read_floats(pf, data, incr)) {
BKE_ptcache_file_close(pf);
if(pf2)
BKE_ptcache_file_close(pf2);
return 0;
}
reader->set_elem(i, reader->calldata, data);
}
else {
reader->set_elem(i, reader->calldata, data);
data += incr;
}
}
if(pf) {
BKE_ptcache_file_close(pf);
MEM_freeN(data);
}
if(pf2)
BKE_ptcache_file_close(pf2);
ret = PTCACHE_READ_OLD;
}
if(pf)
BKE_ptcache_file_close(pf);
if(pf2)
BKE_ptcache_file_close(pf2);
if((pid->cache->flag & PTCACHE_QUICK_CACHE)==0) {
/* clear invalid cache frames so that better stuff can be simulated */
if(pid->cache->flag & PTCACHE_OUTDATED) {
BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, cfra);
}
else if(pid->cache->flag & PTCACHE_FRAMES_SKIPPED) {
if(cfra <= pid->cache->last_exact)
pid->cache->flag &= ~PTCACHE_FRAMES_SKIPPED;
BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, MAX2(cfra,pid->cache->last_exact));
}
}
return ret;
}
/* writes cache to disk or memory */
int BKE_ptcache_write_cache(PTCacheWriter *writer)
{
PointCache *cache = writer->pid->cache;
PTCacheFile *pf= NULL;
int elemsize = ptcache_pid_elemsize(writer->pid);
int i, incr = elemsize / sizeof(float);
int add = 0, overwrite = 0, ocfra;
float temp[14];
if(writer->totelem == 0 || writer->cfra <= 0)
return 0;
if(cache->flag & PTCACHE_DISK_CACHE) {
/* allways start from scratch on the first frame */
if(writer->cfra == cache->startframe) {
BKE_ptcache_id_clear(writer->pid, PTCACHE_CLEAR_ALL, writer->cfra);
cache->flag &= ~PTCACHE_REDO_NEEDED;
add = 1;
}
else {
int cfra = cache->endframe;
/* find last cached frame */
while(cfra > cache->startframe && !BKE_ptcache_id_exist(writer->pid, cfra))
cfra--;
/* find second last cached frame */
ocfra = cfra-1;
while(ocfra > cache->startframe && !BKE_ptcache_id_exist(writer->pid, ocfra))
ocfra--;
if(writer->cfra > cfra) {
if(ocfra >= cache->startframe && cfra - ocfra < cache->step)
overwrite = 1;
else
add = 1;
}
}
if(add || overwrite) {
if(overwrite)
BKE_ptcache_id_clear(writer->pid, PTCACHE_CLEAR_FRAME, ocfra);
pf = BKE_ptcache_file_open(writer->pid, PTCACHE_FILE_WRITE, writer->cfra);
if(!pf)
return 0;
for(i=0; i<writer->totelem; i++) {
writer->set_elem(i, writer->calldata, &temp);
BKE_ptcache_file_write_floats(pf, &temp, incr);
}
}
}
else {
PTCacheMem *pm;
PTCacheMem *pm2;
float *pmdata;
pm2 = cache->mem_cache.first;
/* allways start from scratch on the first frame */
if(writer->cfra == cache->startframe) {
BKE_ptcache_id_clear(writer->pid, PTCACHE_CLEAR_ALL, writer->cfra);
cache->flag &= ~PTCACHE_REDO_NEEDED;
add = 1;
}
else {
pm2 = cache->mem_cache.last;
if(pm2 && writer->cfra > pm2->frame) {
if(pm2 && pm2->prev && pm2->frame - pm2->prev->frame < cache->step)
overwrite = 1;
else
add = 1;
}
}
if(overwrite) {
pm = cache->mem_cache.last;
pmdata = pm->data;
for(i=0; i<writer->totelem; i++, pmdata+=incr) {
writer->set_elem(i, writer->calldata, &temp);
memcpy(pmdata, &temp, elemsize);
}
pm->frame = writer->cfra;
}
else if(add) {
pm = MEM_callocN(sizeof(PTCacheMem), "Pointcache mem");
pm->data = MEM_callocN(elemsize * writer->totelem, "Pointcache mem data");
pmdata = pm->data;
for(i=0; i<writer->totelem; i++, pmdata+=incr) {
writer->set_elem(i, writer->calldata, &temp);
memcpy(pmdata, &temp, elemsize);
}
pm->frame = writer->cfra;
pm->totpoint = writer->totelem;
BLI_addtail(&cache->mem_cache, pm);
}
}
if(add || overwrite) {
if(writer->cfra - cache->last_exact == 1
|| writer->cfra == cache->startframe) {
cache->last_exact = writer->cfra;
cache->flag &= ~PTCACHE_FRAMES_SKIPPED;
}
else
cache->flag |= PTCACHE_FRAMES_SKIPPED;
}
if(pf)
BKE_ptcache_file_close(pf);
BKE_ptcache_update_info(writer->pid);
return 1;
}
/* youll need to close yourself after!
* mode - PTCACHE_CLEAR_ALL,
*/
void BKE_ptcache_id_clear(PTCacheID *pid, int mode, int cfra)
{
int len; /* store the length of the string */
/* mode is same as fopen's modes */
DIR *dir;
struct dirent *de;
char path[MAX_PTCACHE_PATH];
char filename[MAX_PTCACHE_FILE];
char path_full[MAX_PTCACHE_FILE];
char ext[MAX_PTCACHE_PATH];
if(!pid->cache)
return;
/* don't allow clearing for linked objects */
if(pid->ob->id.lib)
return;
/*if (!G.relbase_valid) return; *//* save blend file before using pointcache */
/* clear all files in the temp dir with the prefix of the ID and the ".bphys" suffix */
switch (mode) {
case PTCACHE_CLEAR_ALL:
case PTCACHE_CLEAR_BEFORE:
case PTCACHE_CLEAR_AFTER:
if(pid->cache->flag & PTCACHE_DISK_CACHE) {
ptcache_path(pid, path);
len = BKE_ptcache_id_filename(pid, filename, cfra, 0, 0); /* no path */
dir = opendir(path);
if (dir==NULL)
return;
snprintf(ext, sizeof(ext), "_%02d"PTCACHE_EXT, pid->stack_index);
while ((de = readdir(dir)) != NULL) {
if (strstr(de->d_name, ext)) { /* do we have the right extension?*/
if (strncmp(filename, de->d_name, len ) == 0) { /* do we have the right prefix */
if (mode == PTCACHE_CLEAR_ALL) {
pid->cache->last_exact = 0;
BLI_join_dirfile(path_full, path, de->d_name);
BLI_delete(path_full, 0, 0);
} else {
/* read the number of the file */
int frame, len2 = strlen(de->d_name);
char num[7];
if (len2 > 15) { /* could crash if trying to copy a string out of this range*/
BLI_strncpy(num, de->d_name + (strlen(de->d_name) - 15), sizeof(num));
frame = atoi(num);
if((mode==PTCACHE_CLEAR_BEFORE && frame < cfra) ||
(mode==PTCACHE_CLEAR_AFTER && frame > cfra) ) {
BLI_join_dirfile(path_full, path, de->d_name);
BLI_delete(path_full, 0, 0);
}
}
}
}
}
}
closedir(dir);
}
else {
PTCacheMem *pm= pid->cache->mem_cache.first;
PTCacheMem *link= NULL;
if(mode == PTCACHE_CLEAR_ALL) {
pid->cache->last_exact = 0;
for(; pm; pm=pm->next)
MEM_freeN(pm->data);
BLI_freelistN(&pid->cache->mem_cache);
} else {
while(pm) {
if((mode==PTCACHE_CLEAR_BEFORE && pm->frame < cfra) ||
(mode==PTCACHE_CLEAR_AFTER && pm->frame > cfra) ) {
link = pm;
pm = pm->next;
MEM_freeN(link->data);
BLI_freelinkN(&pid->cache->mem_cache, link);
}
else
pm = pm->next;
}
}
}
break;
case PTCACHE_CLEAR_FRAME:
if(pid->cache->flag & PTCACHE_DISK_CACHE) {
if(BKE_ptcache_id_exist(pid, cfra)) {
BKE_ptcache_id_filename(pid, filename, cfra, 1, 1); /* no path */
BLI_delete(filename, 0, 0);
}
}
else {
PTCacheMem *pm = pid->cache->mem_cache.first;
for(; pm; pm=pm->next) {
if(pm->frame == cfra) {
MEM_freeN(pm->data);
BLI_freelinkN(&pid->cache->mem_cache, pm);
break;
}
}
}
break;
}
BKE_ptcache_update_info(pid);
}
int BKE_ptcache_id_exist(PTCacheID *pid, int cfra)
{
if(!pid->cache)
return 0;
if(pid->cache->flag & PTCACHE_DISK_CACHE) {
char filename[MAX_PTCACHE_FILE];
BKE_ptcache_id_filename(pid, filename, cfra, 1, 1);
return BLI_exists(filename);
}
else {
PTCacheMem *pm = pid->cache->mem_cache.first;
for(; pm; pm=pm->next) {
if(pm->frame==cfra)
return 1;
}
return 0;
}
}
void BKE_ptcache_id_time(PTCacheID *pid, Scene *scene, float cfra, int *startframe, int *endframe, float *timescale)
{
Object *ob;
PointCache *cache;
float offset, time, nexttime;
/* TODO: this has to be sorter out once bsystem_time gets redone, */
/* now caches can handle interpolating etc. too - jahka */
/* time handling for point cache:
* - simulation time is scaled by result of bsystem_time
* - for offsetting time only time offset is taken into account, since
* that's always the same and can't be animated. a timeoffset which
* varies over time is not simpe to support.
* - field and motion blur offsets are currently ignored, proper solution
* is probably to interpolate results from two frames for that ..
*/
ob= pid->ob;
cache= pid->cache;
if(timescale) {
time= bsystem_time(scene, ob, cfra, 0.0f);
nexttime= bsystem_time(scene, ob, cfra+1.0f, 0.0f);
*timescale= MAX2(nexttime - time, 0.0f);
}
if(startframe && endframe) {
*startframe= cache->startframe;
*endframe= cache->endframe;
// XXX ipoflag is depreceated - old animation system stuff
if (/*(ob->ipoflag & OB_OFFS_PARENT) &&*/ (ob->partype & PARSLOW)==0) {
offset= give_timeoffset(ob);
*startframe += (int)(offset+0.5f);
*endframe += (int)(offset+0.5f);
}
}
}
int BKE_ptcache_id_reset(Scene *scene, PTCacheID *pid, int mode)
{
PointCache *cache;
int reset, clear, after;
if(!pid->cache)
return 0;
cache= pid->cache;
reset= 0;
clear= 0;
after= 0;
if(mode == PTCACHE_RESET_DEPSGRAPH) {
if(!(cache->flag & PTCACHE_BAKED) && !BKE_ptcache_get_continue_physics()) {
if(cache->flag & PTCACHE_QUICK_CACHE)
clear= 1;
after= 1;
}
cache->flag |= PTCACHE_OUTDATED;
}
else if(mode == PTCACHE_RESET_BAKED) {
if(!BKE_ptcache_get_continue_physics()) {
reset= 1;
clear= 1;
}
else
cache->flag |= PTCACHE_OUTDATED;
}
else if(mode == PTCACHE_RESET_OUTDATED) {
reset = 1;
if(cache->flag & PTCACHE_OUTDATED)
if(!(cache->flag & PTCACHE_BAKED))
clear= 1;
}
if(reset) {
cache->flag &= ~(PTCACHE_REDO_NEEDED|PTCACHE_SIMULATION_VALID);
cache->simframe= 0;
cache->last_exact= 0;
if(pid->type == PTCACHE_TYPE_CLOTH)
cloth_free_modifier(pid->ob, pid->data);
else if(pid->type == PTCACHE_TYPE_SOFTBODY)
sbFreeSimulation(pid->data);
else if(pid->type == PTCACHE_TYPE_PARTICLES)
psys_reset(pid->data, PSYS_RESET_DEPSGRAPH);
}
if(clear)
BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
else if(after)
BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_AFTER, CFRA);
return (reset || clear || after);
}
int BKE_ptcache_object_reset(Scene *scene, Object *ob, int mode)
{
PTCacheID pid;
ParticleSystem *psys;
ModifierData *md;
int reset, skip;
reset= 0;
skip= 0;
if(ob->soft) {
BKE_ptcache_id_from_softbody(&pid, ob, ob->soft);
reset |= BKE_ptcache_id_reset(scene, &pid, mode);
}
for(psys=ob->particlesystem.first; psys; psys=psys->next) {
/* Baked softbody hair has to be checked first, because we don't want to reset */
/* particles or softbody in that case -jahka */
if(psys->soft) {
BKE_ptcache_id_from_softbody(&pid, ob, psys->soft);
if(mode == PSYS_RESET_ALL || !(psys->part->type == PART_HAIR && (pid.cache->flag & PTCACHE_BAKED)))
reset |= BKE_ptcache_id_reset(scene, &pid, mode);
else
skip = 1;
}
else if(psys->recalc & PSYS_RECALC_REDO || psys->recalc & PSYS_RECALC_CHILD)
skip = 1;
if(skip == 0) {
BKE_ptcache_id_from_particles(&pid, ob, psys);
reset |= BKE_ptcache_id_reset(scene, &pid, mode);
}
}
for(md=ob->modifiers.first; md; md=md->next) {
if(md->type == eModifierType_Cloth) {
BKE_ptcache_id_from_cloth(&pid, ob, (ClothModifierData*)md);
reset |= BKE_ptcache_id_reset(scene, &pid, mode);
}
}
return reset;
}
/* Use this when quitting blender, with unsaved files */
void BKE_ptcache_remove(void)
{
char path[MAX_PTCACHE_PATH];
char path_full[MAX_PTCACHE_PATH];
int rmdir = 1;
ptcache_path(NULL, path);
if (BLI_exist(path)) {
/* The pointcache dir exists? - remove all pointcache */
DIR *dir;
struct dirent *de;
dir = opendir(path);
if (dir==NULL)
return;
while ((de = readdir(dir)) != NULL) {
if( strcmp(de->d_name, ".")==0 || strcmp(de->d_name, "..")==0) {
/* do nothing */
} else if (strstr(de->d_name, PTCACHE_EXT)) { /* do we have the right extension?*/
BLI_join_dirfile(path_full, path, de->d_name);
BLI_delete(path_full, 0, 0);
} else {
rmdir = 0; /* unknown file, dont remove the dir */
}
}
closedir(dir);
} else {
rmdir = 0; /* path dosnt exist */
}
if (rmdir) {
BLI_delete(path, 1, 0);
}
}
/* Continuous Interaction */
static int CONTINUE_PHYSICS = 0;
void BKE_ptcache_set_continue_physics(Scene *scene, int enable)
{
Object *ob;
if(CONTINUE_PHYSICS != enable) {
CONTINUE_PHYSICS = enable;
if(CONTINUE_PHYSICS == 0) {
for(ob=G.main->object.first; ob; ob=ob->id.next)
if(BKE_ptcache_object_reset(scene, ob, PTCACHE_RESET_OUTDATED))
DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
}
}
}
int BKE_ptcache_get_continue_physics()
{
return CONTINUE_PHYSICS;
}
/* Point Cache */
PointCache *BKE_ptcache_add()
{
PointCache *cache;
cache= MEM_callocN(sizeof(PointCache), "PointCache");
cache->startframe= 1;
cache->endframe= 250;
cache->step= 10;
return cache;
}
void BKE_ptcache_free(PointCache *cache)
{
PTCacheMem *pm = cache->mem_cache.first;
if(pm) {
for(; pm; pm=pm->next)
MEM_freeN(pm->data);
BLI_freelistN(&cache->mem_cache);
}
MEM_freeN(cache);
}
PointCache *BKE_ptcache_copy(PointCache *cache)
{
PointCache *ncache;
ncache= MEM_dupallocN(cache);
ncache->flag= 0;
ncache->simframe= 0;
return ncache;
}
/* Baking */
static int count_quick_cache(Scene *scene, int *quick_step)
{
Base *base = scene->base.first;
PTCacheID *pid;
ListBase pidlist;
int autocache_count= 0;
for(base = scene->base.first; base; base = base->next) {
if(base->object) {
BKE_ptcache_ids_from_object(&pidlist, base->object);
for(pid=pidlist.first; pid; pid=pid->next) {
if((pid->cache->flag & PTCACHE_BAKED)
|| (pid->cache->flag & PTCACHE_QUICK_CACHE)==0)
continue;
if(pid->cache->flag & PTCACHE_OUTDATED || (pid->cache->flag & PTCACHE_SIMULATION_VALID)==0) {
if(!autocache_count)
*quick_step = pid->cache->step;
else
*quick_step = MIN2(*quick_step, pid->cache->step);
autocache_count++;
}
}
BLI_freelistN(&pidlist);
}
}
return autocache_count;
}
void BKE_ptcache_quick_cache_all(Scene *scene)
{
PTCacheBaker baker;
baker.bake=0;
baker.break_data=NULL;
baker.break_test=NULL;
baker.pid=NULL;
baker.progressbar=NULL;
baker.progresscontext=NULL;
baker.render=0;
baker.scene=scene;
if(count_quick_cache(scene, &baker.quick_step))
BKE_ptcache_make_cache(&baker);
}
/* if bake is not given run simulations to current frame */
void BKE_ptcache_make_cache(PTCacheBaker* baker)
{
Scene *scene = baker->scene;
Base *base;
ListBase pidlist;
PTCacheID *pid = baker->pid;
PointCache *cache;
float frameleno = scene->r.framelen;
int cfrao = CFRA;
int startframe = MAXFRAME;
int endframe = CFRA;
int bake = baker->bake;
int render = baker->render;
int step = baker->quick_step;
G.afbreek = 0;
/* set caches to baking mode and figure out start frame */
if(pid) {
/* cache/bake a single object */
cache = pid->cache;
if((cache->flag & PTCACHE_BAKED)==0) {
if(pid->type==PTCACHE_TYPE_PARTICLES)
psys_get_pointcache_start_end(scene, pid->data, &cache->startframe, &cache->endframe);
if(bake || cache->flag & PTCACHE_REDO_NEEDED)
BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
startframe = MAX2(cache->last_exact, cache->startframe);
if(bake) {
endframe = cache->endframe;
cache->flag |= PTCACHE_BAKING;
}
else {
endframe = MIN2(endframe, cache->endframe);
}
cache->flag &= ~PTCACHE_BAKED;
}
}
else for(base=scene->base.first; base; base= base->next) {
/* cache/bake everything in the scene */
BKE_ptcache_ids_from_object(&pidlist, base->object);
for(pid=pidlist.first; pid; pid=pid->next) {
cache = pid->cache;
if((cache->flag & PTCACHE_BAKED)==0) {
if(pid->type==PTCACHE_TYPE_PARTICLES)
psys_get_pointcache_start_end(scene, pid->data, &cache->startframe, &cache->endframe);
if((cache->flag & PTCACHE_REDO_NEEDED || (cache->flag & PTCACHE_SIMULATION_VALID)==0)
&& ((cache->flag & PTCACHE_QUICK_CACHE)==0 || render || bake))
BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
startframe = MIN2(startframe, cache->startframe);
if(bake || render) {
cache->flag |= PTCACHE_BAKING;
if(bake)
endframe = MAX2(endframe, cache->endframe);
}
cache->flag &= ~PTCACHE_BAKED;
}
}
BLI_freelistN(&pidlist);
}
CFRA= startframe;
scene->r.framelen = 1.0;
for(; CFRA <= endframe; CFRA+=step) {
float prog;
if(bake)
prog = (int)(100.0 * (float)(CFRA - startframe)/(float)(endframe-startframe));
else
prog = CFRA;
/* NOTE: baking should not redraw whole ui as this slows things down */
if(baker->progressbar)
baker->progressbar(baker->progresscontext, prog);
scene_update_for_newframe(scene, scene->lay);
/* NOTE: breaking baking should leave calculated frames in cache, not clear it */
if(baker->break_test && baker->break_test(baker->break_data))
break;
}
/* clear baking flag */
if(pid) {
cache->flag &= ~(PTCACHE_BAKING|PTCACHE_REDO_NEEDED);
cache->flag |= PTCACHE_SIMULATION_VALID;
if(bake)
cache->flag |= PTCACHE_BAKED;
}
else for(base=scene->base.first; base; base= base->next) {
BKE_ptcache_ids_from_object(&pidlist, base->object);
for(pid=pidlist.first; pid; pid=pid->next) {
cache = pid->cache;
if(step > 1)
cache->flag &= ~(PTCACHE_BAKING|PTCACHE_OUTDATED);
else
cache->flag &= ~(PTCACHE_BAKING|PTCACHE_REDO_NEEDED);
cache->flag |= PTCACHE_SIMULATION_VALID;
if(bake)
cache->flag |= PTCACHE_BAKED;
}
BLI_freelistN(&pidlist);
}
scene->r.framelen = frameleno;
CFRA = cfrao;
scene_update_for_newframe(scene, scene->lay);
/* TODO: call redraw all windows somehow */
}
void BKE_ptcache_toggle_disk_cache(PTCacheID *pid) {
PointCache *cache = pid->cache;
PTCacheFile *pf;
PTCacheMem *pm;
int totelem=0;
int float_count=0;
int tot;
int last_exact = cache->last_exact;
if (!G.relbase_valid){
cache->flag &= ~PTCACHE_DISK_CACHE;
return;
}
totelem = ptcache_pid_totelem(pid);
float_count = ptcache_pid_elemsize(pid) / sizeof(float);
if(totelem==0 || float_count==0)
return;
tot = totelem*float_count;
/* MEM -> DISK */
if(cache->flag & PTCACHE_DISK_CACHE) {
pm = cache->mem_cache.first;
BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
for(; pm; pm=pm->next) {
pf = BKE_ptcache_file_open(pid, PTCACHE_FILE_WRITE, pm->frame);
if(pf) {
if(fwrite(pm->data, sizeof(float), tot, pf->fp) != tot) {
printf("Error writing to disk cache\n");
cache->flag &= ~PTCACHE_DISK_CACHE;
BKE_ptcache_file_close(pf);
return;
}
BKE_ptcache_file_close(pf);
}
else
printf("Error creating disk cache file\n");
}
cache->flag &= ~PTCACHE_DISK_CACHE;
BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
cache->flag |= PTCACHE_DISK_CACHE;
}
/* DISK -> MEM */
else {
int cfra;
int sfra = cache->startframe;
int efra = cache->endframe;
BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
for(cfra=sfra; cfra <= efra; cfra++) {
pf = BKE_ptcache_file_open(pid, PTCACHE_FILE_READ, cfra);
if(pf) {
pm = MEM_callocN(sizeof(PTCacheMem), "Pointcache mem");
pm->data = MEM_callocN(sizeof(float)*tot, "Pointcache mem data");
if(fread(pm->data, sizeof(float), tot, pf->fp)!= tot) {
printf("Error reading from disk cache\n");
cache->flag |= PTCACHE_DISK_CACHE;
MEM_freeN(pm->data);
MEM_freeN(pm);
BKE_ptcache_file_close(pf);
return;
}
pm->frame = cfra;
pm->totpoint = totelem;
BLI_addtail(&pid->cache->mem_cache, pm);
BKE_ptcache_file_close(pf);
}
}
cache->flag |= PTCACHE_DISK_CACHE;
BKE_ptcache_id_clear(pid, PTCACHE_CLEAR_ALL, 0);
cache->flag &= ~PTCACHE_DISK_CACHE;
}
cache->last_exact = last_exact;
BKE_ptcache_update_info(pid);
}
|