Keyed physics refresh:
[blender-staging.git] / source / blender / blenkernel / intern / depsgraph.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2004 Blender Foundation.
21  * All rights reserved.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27  
28 #include <stdio.h>
29 #include <string.h>
30 #include <math.h>
31
32 #ifdef _WIN32
33 #include "BLI_winstuff.h"
34 #endif
35
36 #include "BLI_blenlib.h"
37 #include "BLI_arithb.h"
38
39 #include "DNA_anim_types.h"
40 #include "DNA_action_types.h"
41 #include "DNA_armature_types.h"
42 #include "DNA_curve_types.h"
43 #include "DNA_camera_types.h"
44 #include "DNA_ID.h"
45 #include "DNA_effect_types.h"
46 #include "DNA_group_types.h"
47 #include "DNA_lattice_types.h"
48 #include "DNA_lamp_types.h"
49 #include "DNA_key_types.h"
50 #include "DNA_mesh_types.h"
51 #include "DNA_modifier_types.h"
52 #include "DNA_nla_types.h"
53 #include "DNA_object_types.h"
54 #include "DNA_object_force.h"
55 #include "DNA_object_fluidsim.h"
56 #include "DNA_outliner_types.h"
57 #include "DNA_particle_types.h"
58 #include "DNA_scene_types.h"
59 #include "DNA_screen_types.h"
60 #include "DNA_space_types.h"
61 #include "DNA_view2d_types.h"
62 #include "DNA_view3d_types.h"
63
64 #include "BLI_ghash.h"
65
66 #include "BKE_action.h"
67 #include "BKE_effect.h"
68 #include "BKE_global.h"
69 #include "BKE_group.h"
70 #include "BKE_key.h"
71 #include "BKE_main.h"
72 #include "BKE_mball.h"
73 #include "BKE_modifier.h"
74 #include "BKE_object.h"
75 #include "BKE_particle.h"
76 #include "BKE_pointcache.h"
77 #include "BKE_utildefines.h"
78 #include "BKE_scene.h"
79
80 #include "MEM_guardedalloc.h"
81
82 #ifndef DISABLE_PYTHON
83 #include "BPY_extern.h"
84 #endif
85
86  #include "depsgraph_private.h"
87  
88 /* Queue and stack operations for dag traversal 
89  *
90  * the queue store a list of freenodes to avoid successives alloc/dealloc
91  */
92
93 DagNodeQueue * queue_create (int slots) 
94 {
95         DagNodeQueue * queue;
96         DagNodeQueueElem * elem;
97         int i;
98         
99         queue = MEM_mallocN(sizeof(DagNodeQueue),"DAG queue");
100         queue->freenodes = MEM_mallocN(sizeof(DagNodeQueue),"DAG queue");
101         queue->count = 0;
102         queue->maxlevel = 0;
103         queue->first = queue->last = NULL;
104         elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem3");
105         elem->node = NULL;
106         elem->next = NULL;
107         queue->freenodes->first = queue->freenodes->last = elem;
108         
109         for (i = 1; i <slots;i++) {
110                 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem4");
111                 elem->node = NULL;
112                 elem->next = NULL;
113                 queue->freenodes->last->next = elem;
114                 queue->freenodes->last = elem;
115         }
116         queue->freenodes->count = slots;
117         return queue;
118 }
119
120 void queue_raz(DagNodeQueue *queue)
121 {
122         DagNodeQueueElem * elem;
123         
124         elem = queue->first;
125         if (queue->freenodes->last)
126                 queue->freenodes->last->next = elem;
127         else
128                 queue->freenodes->first = queue->freenodes->last = elem;
129         
130         elem->node = NULL;
131         queue->freenodes->count++;
132         while (elem->next) {
133                 elem = elem->next;
134                 elem->node = NULL;
135                 queue->freenodes->count++;
136         }
137         queue->freenodes->last = elem;
138         queue->count = 0;
139 }
140
141 void queue_delete(DagNodeQueue *queue)
142 {
143         DagNodeQueueElem * elem;
144         DagNodeQueueElem * temp;
145         
146         elem = queue->first;
147         while (elem) {
148                 temp = elem;
149                 elem = elem->next;
150                 MEM_freeN(temp);
151         }
152         
153         elem = queue->freenodes->first;
154         while (elem) {
155                 temp = elem;
156                 elem = elem->next;
157                 MEM_freeN(temp);
158         }
159         
160         MEM_freeN(queue->freenodes);                    
161         MEM_freeN(queue);                       
162 }
163
164 /* insert in queue, remove in front */
165 void push_queue(DagNodeQueue *queue, DagNode *node)
166 {
167         DagNodeQueueElem * elem;
168         int i;
169
170         if (node == NULL) {
171                 fprintf(stderr,"pushing null node \n");
172                 return;
173         }
174         /*fprintf(stderr,"BFS push : %s %d\n",((ID *) node->ob)->name, queue->count);*/
175
176         elem = queue->freenodes->first;
177         if (elem != NULL) {
178                 queue->freenodes->first = elem->next;
179                 if ( queue->freenodes->last == elem) {
180                         queue->freenodes->last = NULL;
181                         queue->freenodes->first = NULL;
182                 }
183                 queue->freenodes->count--;
184         } else { /* alllocating more */         
185                 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem1");
186                 elem->node = NULL;
187                 elem->next = NULL;
188                 queue->freenodes->first = queue->freenodes->last = elem;
189
190                 for (i = 1; i <DAGQUEUEALLOC;i++) {
191                         elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem2");
192                         elem->node = NULL;
193                         elem->next = NULL;
194                         queue->freenodes->last->next = elem;
195                         queue->freenodes->last = elem;
196                 }
197                 queue->freenodes->count = DAGQUEUEALLOC;
198                         
199                 elem = queue->freenodes->first; 
200                 queue->freenodes->first = elem->next;   
201         }
202         elem->next = NULL;
203         elem->node = node;
204         if (queue->last != NULL)
205                 queue->last->next = elem;
206         queue->last = elem;
207         if (queue->first == NULL) {
208                 queue->first = elem;
209         }
210         queue->count++;
211 }
212
213
214 /* insert in front, remove in front */
215 void push_stack(DagNodeQueue *queue, DagNode *node)
216 {
217         DagNodeQueueElem * elem;
218         int i;
219
220         elem = queue->freenodes->first; 
221         if (elem != NULL) {
222                 queue->freenodes->first = elem->next;
223                 if ( queue->freenodes->last == elem) {
224                         queue->freenodes->last = NULL;
225                         queue->freenodes->first = NULL;
226                 }
227                 queue->freenodes->count--;
228         } else { /* alllocating more */
229                 elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem1");
230                 elem->node = NULL;
231                 elem->next = NULL;
232                 queue->freenodes->first = queue->freenodes->last = elem;
233
234                 for (i = 1; i <DAGQUEUEALLOC;i++) {
235                         elem = MEM_mallocN(sizeof(DagNodeQueueElem),"DAG queue elem2");
236                         elem->node = NULL;
237                         elem->next = NULL;
238                         queue->freenodes->last->next = elem;
239                         queue->freenodes->last = elem;
240                 }
241                 queue->freenodes->count = DAGQUEUEALLOC;
242                         
243                 elem = queue->freenodes->first; 
244                 queue->freenodes->first = elem->next;   
245         }
246         elem->next = queue->first;
247         elem->node = node;
248         queue->first = elem;
249         if (queue->last == NULL)
250                 queue->last = elem;
251         queue->count++;
252 }
253
254
255 DagNode * pop_queue(DagNodeQueue *queue)
256 {
257         DagNodeQueueElem * elem;
258         DagNode *node;
259
260         elem = queue->first;
261         if (elem) {
262                 queue->first = elem->next;
263                 if (queue->last == elem) {
264                         queue->last=NULL;
265                         queue->first=NULL;
266                 }
267                 queue->count--;
268                 if (queue->freenodes->last)
269                         queue->freenodes->last->next=elem;
270                 queue->freenodes->last=elem;
271                 if (queue->freenodes->first == NULL)
272                         queue->freenodes->first=elem;
273                 node = elem->node;
274                 elem->node = NULL;
275                 elem->next = NULL;
276                 queue->freenodes->count++;
277                 return node;
278         } else {
279                 fprintf(stderr,"return null \n");
280                 return NULL;
281         }
282 }
283
284 void    *pop_ob_queue(struct DagNodeQueue *queue) {
285         return(pop_queue(queue)->ob);
286 }
287
288 DagNode * get_top_node_queue(DagNodeQueue *queue) 
289 {
290         return queue->first->node;
291 }
292
293 int             queue_count(struct DagNodeQueue *queue){
294         return queue->count;
295 }
296
297
298 DagForest * dag_init()
299 {
300         DagForest *forest;
301         /* use callocN to init all zero */
302         forest = MEM_callocN(sizeof(DagForest),"DAG root");
303         return forest;
304 }
305
306 /* isdata = object data... */
307 // XXX this needs to be extended to be more flexible (so that not only objects are evaluated via depsgraph)...
308 static void dag_add_driver_relation(AnimData *adt, DagForest *dag, DagNode *node, int isdata)
309 {
310         FCurve *fcu;
311         DagNode *node1;
312         
313         for (fcu= adt->drivers.first; fcu; fcu= fcu->next) {
314                 ChannelDriver *driver= fcu->driver;
315                 DriverTarget *dtar;
316                 
317                 /* loop over targets, adding relationships as appropriate */
318                 for (dtar= driver->targets.first; dtar; dtar= dtar->next) {
319                         if (dtar->id) {
320                                 if (GS(dtar->id->name)==ID_OB) {
321                                         Object *ob= (Object *)dtar->id;
322                                         
323                                         /* normal channel-drives-channel */
324                                         node1 = dag_get_node(dag, dtar->id);
325                                         
326                                         /* check if bone... */
327                                         if ((ob->type==OB_ARMATURE) && dtar->rna_path && strstr(dtar->rna_path, "pose.pose_channels["))
328                                                 dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB, "Driver");
329                                         /* check if ob data */
330                                         else if (dtar->rna_path && strstr(dtar->rna_path, "data."))
331                                                 dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB, "Driver");
332                                         /* normal */
333                                         else
334                                                 dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB, "Driver");
335                                 }
336                         }
337                 }
338         }
339 }
340
341 static void dag_add_collision_field_relation(DagForest *dag, Scene *scene, Object *ob, DagNode *node)
342 {
343         Base *base;
344         DagNode *node2;
345
346         // would be nice to have a list of colliders here
347         // so for now walk all objects in scene check 'same layer rule'
348         for(base = scene->base.first; base; base= base->next) {
349                 if((base->lay & ob->lay) && base->object->pd) {
350                         Object *ob1= base->object;
351                         if((ob1->pd->deflect || ob1->pd->forcefield) && (ob1 != ob))  {
352                                 node2 = dag_get_node(dag, ob1);                                 
353                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Field Collision");
354                         }
355                 }
356         }
357 }
358
359 static void build_dag_object(DagForest *dag, DagNode *scenenode, Scene *scene, Object *ob, int mask)
360 {
361         bConstraint *con;
362         DagNode * node;
363         DagNode * node2;
364         DagNode * node3;
365         Key *key;
366         ParticleSystem *psys;
367         int addtoroot= 1;
368         
369         node = dag_get_node(dag, ob);
370         
371         if ((ob->data) && (mask&DAG_RL_DATA)) {
372                 node2 = dag_get_node(dag,ob->data);
373                 dag_add_relation(dag,node,node2,DAG_RL_DATA, "Object-Data Relation");
374                 node2->first_ancestor = ob;
375                 node2->ancestor_count += 1;
376         }
377         
378         if (ob->type == OB_ARMATURE) {
379                 if (ob->pose){
380                         bPoseChannel *pchan;
381                         bConstraint *con;
382                         
383                         for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
384                                 for (con = pchan->constraints.first; con; con=con->next) {
385                                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
386                                         ListBase targets = {NULL, NULL};
387                                         bConstraintTarget *ct;
388                                         
389                                         if (cti && cti->get_constraint_targets) {
390                                                 cti->get_constraint_targets(con, &targets);
391                                                 
392                                                 for (ct= targets.first; ct; ct= ct->next) {
393                                                         if (ct->tar && ct->tar != ob) {
394                                                                 // fprintf(stderr,"armature %s target :%s \n", ob->id.name, target->id.name);
395                                                                 node3 = dag_get_node(dag, ct->tar);
396                                                                 
397                                                                 if (ct->subtarget[0])
398                                                                         dag_add_relation(dag,node3,node, DAG_RL_OB_DATA|DAG_RL_DATA_DATA, cti->name);
399                                                                 else if(ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))   
400                                                                         dag_add_relation(dag,node3,node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, cti->name);
401                                                                 else
402                                                                         dag_add_relation(dag,node3,node, DAG_RL_OB_DATA, cti->name);
403                                                         }
404                                                 }
405                                                 
406                                                 if (cti->flush_constraint_targets)
407                                                         cti->flush_constraint_targets(con, &targets, 1);
408                                         }
409                                         
410                                 }
411                         }
412                 }
413         }
414         
415         /* driver dependencies, nla modifiers */
416 #if 0 // XXX old animation system
417         if(ob->nlastrips.first) {
418                 bActionStrip *strip;
419                 bActionChannel *chan;
420                 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
421                         if(strip->modifiers.first) {
422                                 bActionModifier *amod;
423                                 for(amod= strip->modifiers.first; amod; amod= amod->next) {
424                                         if(amod->ob) {
425                                                 node2 = dag_get_node(dag, amod->ob);
426                                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "NLA Strip Modifier");
427                                         }
428                                 }
429                         }
430                 }
431         }
432 #endif // XXX old animation system
433         if (ob->adt)
434                 dag_add_driver_relation(ob->adt, dag, node, (ob->type == OB_ARMATURE)); // XXX isdata arg here doesn't give an accurate picture of situation
435                 
436         key= ob_get_key(ob);
437         if (key && key->adt)
438                 dag_add_driver_relation(key->adt, dag, node, 1);
439
440         if (ob->modifiers.first) {
441                 ModifierData *md;
442                 
443                 for(md=ob->modifiers.first; md; md=md->next) {
444                         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
445                         
446                         if (mti->updateDepgraph) mti->updateDepgraph(md, dag, scene, ob, node);
447                 }
448         }
449         if (ob->parent) {
450                 node2 = dag_get_node(dag,ob->parent);
451                 
452                 switch(ob->partype) {
453                         case PARSKEL:
454                                 dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Parent");
455                                 break;
456                         case PARVERT1: case PARVERT3: case PARBONE:
457                                 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB, "Vertex Parent");
458                                 break;
459                         default:
460                                 if(ob->parent->type==OB_LATTICE) 
461                                         dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Lattice Parent");
462                                 else if(ob->parent->type==OB_CURVE) {
463                                         Curve *cu= ob->parent->data;
464                                         if(cu->flag & CU_PATH) 
465                                                 dag_add_relation(dag,node2,node,DAG_RL_DATA_OB|DAG_RL_OB_OB, "Curve Parent");
466                                         else
467                                                 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Curve Parent");
468                                 }
469                                 else
470                                         dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Parent");
471                 }
472                 /* exception case: parent is duplivert */
473                 if(ob->type==OB_MBALL && (ob->parent->transflag & OB_DUPLIVERTS)) {
474                         dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Duplivert");
475                 }
476                 
477                 addtoroot = 0;
478         }
479         if (ob->track) {
480                 node2 = dag_get_node(dag,ob->track);
481                 dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Track To");
482                 addtoroot = 0;
483         }
484         if (ob->proxy) {
485                 node2 = dag_get_node(dag, ob->proxy);
486                 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA|DAG_RL_OB_OB, "Proxy");
487                 /* inverted relation, so addtoroot shouldn't be set to zero */
488         }
489         
490
491         if (ob->type==OB_CAMERA) {
492                 Camera *cam = (Camera *)ob->data;
493                 if (cam->adt)
494                         dag_add_driver_relation(cam->adt, dag, node, 1);
495                 if (cam->dof_ob) {
496                         node2 = dag_get_node(dag, cam->dof_ob);
497                         dag_add_relation(dag,node2,node,DAG_RL_OB_OB, "Camera DoF");
498                 }
499         }
500         if (ob->type==OB_LAMP) {
501                 Lamp *la = (Lamp *)ob->data;
502                 if (la->adt)
503                         dag_add_driver_relation(la->adt, dag, node, 1);
504         }
505         
506         if (ob->transflag & OB_DUPLI) {
507                 if((ob->transflag & OB_DUPLIGROUP) && ob->dup_group) {
508                         GroupObject *go;
509                         for(go= ob->dup_group->gobject.first; go; go= go->next) {
510                                 if(go->ob) {
511                                         node2 = dag_get_node(dag, go->ob);
512                                         /* node2 changes node1, this keeps animations updated in groups?? not logical? */
513                                         dag_add_relation(dag, node2, node, DAG_RL_OB_OB, "Dupligroup");
514                                 }
515                         }
516                 }
517         }
518     
519         /* softbody collision  */
520         if((ob->type==OB_MESH) || (ob->type==OB_CURVE) || (ob->type==OB_LATTICE))
521                 if(modifiers_isSoftbodyEnabled(ob) || modifiers_isClothEnabled(ob))
522                         dag_add_collision_field_relation(dag, scene, ob, node);
523                 
524         if (ob->type==OB_MBALL) {
525                 Object *mom= find_basis_mball(scene, ob);
526                 if(mom!=ob) {
527                         node2 = dag_get_node(dag, mom);
528                         dag_add_relation(dag,node,node2,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Metaball");  // mom depends on children!
529                 }
530         }
531         else if (ob->type==OB_CURVE) {
532                 Curve *cu= ob->data;
533                 if(cu->bevobj) {
534                         node2 = dag_get_node(dag, cu->bevobj);
535                         dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Curve Bevel");
536                 }
537                 if(cu->taperobj) {
538                         node2 = dag_get_node(dag, cu->taperobj);
539                         dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Curve Taper");
540                 }
541                 if (cu->adt)
542                         dag_add_driver_relation(cu->adt, dag, node, 1);
543         }
544         else if(ob->type==OB_FONT) {
545                 Curve *cu= ob->data;
546                 if(cu->textoncurve) {
547                         node2 = dag_get_node(dag, cu->textoncurve);
548                         dag_add_relation(dag,node2,node,DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Texture On Curve");
549                 }
550         }
551         
552         psys= ob->particlesystem.first;
553         if(psys) {
554                 ParticleEffectorCache *nec;
555                 GroupObject *go;
556
557                 for(; psys; psys=psys->next) {
558                         ParticleSettings *part= psys->part;
559
560                         dag_add_relation(dag, node, node, DAG_RL_OB_DATA, "Particle-Object Relation");
561
562                         if(!psys_check_enabled(ob, psys))
563                                 continue;
564
565                         if(part->phystype==PART_PHYS_KEYED) {
566                                 KeyedParticleTarget *kpt = psys->keyed_targets.first;
567
568                                 for(; kpt; kpt=kpt->next) {
569                                         if(kpt->ob && BLI_findlink(&kpt->ob->particlesystem, kpt->psys-1)) {
570                                                 node2 = dag_get_node(dag, kpt->ob);
571                                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Particle Keyed Physics");
572                                         }
573                                         else
574                                                 break;
575                            }
576                         }
577
578                         if(part->ren_as == PART_DRAW_OB && part->dup_ob) {
579                                 node2 = dag_get_node(dag, part->dup_ob);
580                                 dag_add_relation(dag, node, node2, DAG_RL_OB_OB, "Particle Object Visualisation");
581                                 if(part->dup_ob->type == OB_MBALL)
582                                         dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA, "Particle Object Visualisation");
583                         }
584
585                         if(part->ren_as == PART_DRAW_GR && part->dup_group) {
586                                 for(go=part->dup_group->gobject.first; go; go=go->next) {
587                                         node2 = dag_get_node(dag, go->ob);
588                                         dag_add_relation(dag, node, node2, DAG_RL_OB_OB, "Particle Group Visualisation");
589                                 }
590                         }
591
592                         if(psys->effectors.first)
593                                 psys_end_effectors(psys);
594                         psys_init_effectors(scene, ob, psys->part->eff_group, psys);
595
596                         if(psys->effectors.first) {
597                                 for(nec= psys->effectors.first; nec; nec= nec->next) {
598                                         Object *ob1= nec->ob;
599
600                                         if(nec->type & PSYS_EC_EFFECTOR) {
601                                                 node2 = dag_get_node(dag, ob1);
602                                                 if(ob1->pd->forcefield==PFIELD_GUIDE)
603                                                         dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Particle Field");
604                                                 else
605                                                         dag_add_relation(dag, node2, node, DAG_RL_OB_DATA, "Particle Field");
606                                         }
607                                         else if(nec->type & PSYS_EC_DEFLECT) {
608                                                 node2 = dag_get_node(dag, ob1);
609                                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Particle Collision");
610                                         }
611                                         else if(nec->type & PSYS_EC_PARTICLE) {
612                                                 node2 = dag_get_node(dag, ob1);
613                                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA, "Particle Field");
614                                         }
615                                         
616                                         if(nec->type & PSYS_EC_REACTOR) {
617                                                 node2 = dag_get_node(dag, ob1);
618                                                 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA, "Particle Reactor");
619                                         }
620                                 }
621                         }
622                 }
623         }
624         
625         for (con = ob->constraints.first; con; con=con->next) {
626                 bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
627                 ListBase targets = {NULL, NULL};
628                 bConstraintTarget *ct;
629                 
630                 if (cti && cti->get_constraint_targets) {
631                         cti->get_constraint_targets(con, &targets);
632                         
633                         for (ct= targets.first; ct; ct= ct->next) {
634                                 Object *obt;
635                                 
636                                 if (ct->tar)
637                                         obt= ct->tar;
638                                 else
639                                         continue;
640                                 
641                                 node2 = dag_get_node(dag, obt);
642                                 if (ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO))
643                                         dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
644                                 else {
645                                         if (ELEM3(obt->type, OB_ARMATURE, OB_MESH, OB_LATTICE) && (ct->subtarget[0]))
646                                                 dag_add_relation(dag, node2, node, DAG_RL_DATA_OB|DAG_RL_OB_OB, cti->name);
647                                         else
648                                                 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, cti->name);
649                                 }
650                                 addtoroot = 0;
651                         }
652                         
653                         if (cti->flush_constraint_targets)
654                                 cti->flush_constraint_targets(con, &targets, 1);
655                 }
656         }
657
658         if (addtoroot == 1 )
659                 dag_add_relation(dag,scenenode,node,DAG_RL_SCENE, "Scene Relation");
660 }
661
662 struct DagForest *build_dag(struct Scene *sce, short mask) 
663 {
664         Base *base;
665         Object *ob;
666         Group *group;
667         GroupObject *go;
668         DagNode *node;
669         DagNode *scenenode;
670         DagForest *dag;
671         DagAdjList *itA;
672
673         dag = sce->theDag;
674         sce->dagisvalid=1;
675         if ( dag)
676                 free_forest( dag ); 
677         else {
678                 dag = dag_init();
679                 sce->theDag = dag;
680         }
681         
682         /* add base node for scene. scene is always the first node in DAG */
683         scenenode = dag_add_node(dag, sce);     
684         
685         /* add current scene objects */
686         for(base = sce->base.first; base; base= base->next) {
687                 ob= base->object;
688                 
689                 build_dag_object(dag, scenenode, sce, ob, mask);
690                 if(ob->proxy)
691                         build_dag_object(dag, scenenode, sce, ob->proxy, mask);
692                 
693                 /* handled in next loop */
694                 if(ob->dup_group) 
695                         ob->dup_group->id.flag |= LIB_DOIT;
696         }
697         
698         /* add groups used in current scene objects */
699         for(group= G.main->group.first; group; group= group->id.next) {
700                 if(group->id.flag & LIB_DOIT) {
701                         for(go= group->gobject.first; go; go= go->next) {
702                                 build_dag_object(dag, scenenode, sce, go->ob, mask);
703                         }
704                         group->id.flag &= ~LIB_DOIT;
705                 }
706         }
707         
708         /* Now all relations were built, but we need to solve 1 exceptional case;
709            When objects have multiple "parents" (for example parent + constraint working on same object)
710            the relation type has to be synced. One of the parents can change, and should give same event to child */
711         
712         /* nodes were callocced, so we can use node->color for temporal storage */
713         for(node = sce->theDag->DagNode.first; node; node= node->next) {
714                 if(node->type==ID_OB) {
715                         for(itA = node->child; itA; itA= itA->next) {
716                                 if(itA->node->type==ID_OB) {
717                                         itA->node->color |= itA->type;
718                                 }
719                         }
720                 }
721         }
722         /* now set relations equal, so that when only one parent changes, the correct recalcs are found */
723         for(node = sce->theDag->DagNode.first; node; node= node->next) {
724                 if(node->type==ID_OB) {
725                         for(itA = node->child; itA; itA= itA->next) {
726                                 if(itA->node->type==ID_OB) {
727                                         itA->type |= itA->node->color;
728                                 }
729                         }
730                 }
731         }
732         
733         // cycle detection and solving
734         // solve_cycles(dag);   
735         
736         return dag;
737 }
738
739
740 void free_forest(DagForest *Dag) 
741 {  /* remove all nodes and deps */
742         DagNode *tempN;
743         DagAdjList *tempA;      
744         DagAdjList *itA;
745         DagNode *itN = Dag->DagNode.first;
746         
747         while (itN) {
748                 itA = itN->child;       
749                 while (itA) {
750                         tempA = itA;
751                         itA = itA->next;
752                         MEM_freeN(tempA);                       
753                 }
754                 
755                 itA = itN->parent;      
756                 while (itA) {
757                         tempA = itA;
758                         itA = itA->next;
759                         MEM_freeN(tempA);                       
760                 }
761                 
762                 tempN = itN;
763                 itN = itN->next;
764                 MEM_freeN(tempN);
765         }
766
767         BLI_ghash_free(Dag->nodeHash, NULL, NULL);
768         Dag->nodeHash= NULL;
769         Dag->DagNode.first = NULL;
770         Dag->DagNode.last = NULL;
771         Dag->numNodes = 0;
772
773 }
774
775 DagNode * dag_find_node (DagForest *forest,void * fob)
776 {
777         if(forest->nodeHash)
778                 return BLI_ghash_lookup(forest->nodeHash, fob);
779
780         return NULL;
781 }
782
783 static int ugly_hack_sorry= 1;  // prevent type check
784
785 /* no checking of existance, use dag_find_node first or dag_get_node */
786 DagNode * dag_add_node (DagForest *forest, void * fob)
787 {
788         DagNode *node;
789                 
790         node = MEM_callocN(sizeof(DagNode),"DAG node");
791         if (node) {
792                 node->ob = fob;
793                 node->color = DAG_WHITE;
794
795                 if(ugly_hack_sorry) node->type = GS(((ID *) fob)->name);        // sorry, done for pose sorting
796                 if (forest->numNodes) {
797                         ((DagNode *) forest->DagNode.last)->next = node;
798                         forest->DagNode.last = node;
799                         forest->numNodes++;
800                 } else {
801                         forest->DagNode.last = node;
802                         forest->DagNode.first = node;
803                         forest->numNodes = 1;
804                 }
805
806                 if(!forest->nodeHash)
807                         forest->nodeHash= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
808                 BLI_ghash_insert(forest->nodeHash, fob, node);
809         }
810
811         return node;
812 }
813
814 DagNode * dag_get_node (DagForest *forest,void * fob)
815 {
816         DagNode *node;
817         
818         node = dag_find_node (forest, fob);     
819         if (!node) 
820                 node = dag_add_node(forest, fob);
821         return node;
822 }
823
824
825
826 DagNode * dag_get_sub_node (DagForest *forest,void * fob)
827 {
828         DagNode *node;
829         DagAdjList *mainchild, *prev=NULL;
830         
831         mainchild = ((DagNode *) forest->DagNode.first)->child;
832         /* remove from first node (scene) adj list if present */
833         while (mainchild) {
834                 if (mainchild->node == fob) {
835                         if (prev) {
836                                 prev->next = mainchild->next;
837                                 MEM_freeN(mainchild);
838                                 break;
839                         } else {
840                                 ((DagNode *) forest->DagNode.first)->child = mainchild->next;
841                                 MEM_freeN(mainchild);
842                                 break;
843                         }
844                 }
845                 prev = mainchild;
846                 mainchild = mainchild->next;
847         }
848         node = dag_find_node (forest, fob);     
849         if (!node) 
850                 node = dag_add_node(forest, fob);
851         return node;
852 }
853
854 static void dag_add_parent_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name) 
855 {
856         DagAdjList *itA = fob2->parent;
857         
858         while (itA) { /* search if relation exist already */
859                 if (itA->node == fob1) {
860                         itA->type |= rel;
861                         itA->count += 1;
862                         return;
863                 }
864                 itA = itA->next;
865         }
866         /* create new relation and insert at head. MALLOC alert! */
867         itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
868         itA->node = fob1;
869         itA->type = rel;
870         itA->count = 1;
871         itA->next = fob2->parent;
872         itA->name = name;
873         fob2->parent = itA;
874 }
875
876 void dag_add_relation(DagForest *forest, DagNode *fob1, DagNode *fob2, short rel, char *name) 
877 {
878         DagAdjList *itA = fob1->child;
879         
880         /* parent relation is for cycle checking */
881         dag_add_parent_relation(forest, fob1, fob2, rel, name);
882
883         while (itA) { /* search if relation exist already */
884                 if (itA->node == fob2) {
885                         itA->type |= rel;
886                         itA->count += 1;
887                         return;
888                 }
889                 itA = itA->next;
890         }
891         /* create new relation and insert at head. MALLOC alert! */
892         itA = MEM_mallocN(sizeof(DagAdjList),"DAG adj list");
893         itA->node = fob2;
894         itA->type = rel;
895         itA->count = 1;
896         itA->next = fob1->child;
897         itA->name = name;
898         fob1->child = itA;
899 }
900
901 static char *dag_node_name(DagNode *node)
902 {
903         if(node->ob == NULL)
904                 return "null";
905         else if(ugly_hack_sorry)
906                 return ((ID*)(node->ob))->name+2;
907         else
908                 return ((bPoseChannel*)(node->ob))->name;
909 }
910
911 #if 0
912 static void dag_node_print_dependencies(DagNode *node)
913 {
914         DagAdjList *itA;
915
916         printf("%s depends on:\n", dag_node_name(node));
917
918         for(itA= node->parent; itA; itA= itA->next)
919                 printf("  %s through %s\n", dag_node_name(itA->node), itA->name);
920         printf("\n");
921 }
922 #endif
923
924 static int dag_node_print_dependency_recurs(DagNode *node, DagNode *endnode)
925 {
926         DagAdjList *itA;
927
928         if(node->color == DAG_BLACK)
929                 return 0;
930
931         node->color= DAG_BLACK;
932
933         if(node == endnode)
934                 return 1;
935
936         for(itA= node->parent; itA; itA= itA->next) {
937                 if(dag_node_print_dependency_recurs(itA->node, endnode)) {
938                         printf("  %s depends on %s through %s.\n", dag_node_name(node), dag_node_name(itA->node), itA->name);
939                         return 1;
940                 }
941         }
942
943         return 0;
944 }
945
946 static void dag_node_print_dependency_cycle(DagForest *dag, DagNode *startnode, DagNode *endnode, char *name)
947 {
948         DagNode *node;
949
950     for(node = dag->DagNode.first; node; node= node->next)
951                 node->color= DAG_WHITE;
952
953         printf("  %s depends on %s through %s.\n", dag_node_name(endnode), dag_node_name(startnode), name);
954         dag_node_print_dependency_recurs(startnode, endnode);
955         printf("\n");
956 }
957
958 static int dag_node_recurs_level(DagNode *node, int level)
959 {
960         DagAdjList *itA;
961         int newlevel;
962
963         node->color= DAG_BLACK; /* done */
964         newlevel= ++level;
965         
966         for(itA= node->parent; itA; itA= itA->next) {
967                 if(itA->node->color==DAG_WHITE) {
968                         itA->node->ancestor_count= dag_node_recurs_level(itA->node, level);
969                         newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
970                 }
971                 else
972                         newlevel= MAX2(newlevel, level+itA->node->ancestor_count);
973         }
974         
975         return newlevel;
976 }
977
978 static void dag_check_cycle(DagForest *dag)
979 {
980         DagNode *node;
981         DagAdjList *itA;
982
983         /* tag nodes unchecked */
984         for(node = dag->DagNode.first; node; node= node->next)
985                 node->color= DAG_WHITE;
986         
987         for(node = dag->DagNode.first; node; node= node->next) {
988                 if(node->color==DAG_WHITE) {
989                         node->ancestor_count= dag_node_recurs_level(node, 0);
990                 }
991         }
992         
993         /* check relations, and print errors */
994         for(node = dag->DagNode.first; node; node= node->next) {
995                 for(itA= node->parent; itA; itA= itA->next) {
996                         if(itA->node->ancestor_count > node->ancestor_count) {
997                                 if(node->ob && itA->node->ob) {
998                                         printf("Dependency cycle detected:\n");
999                                         dag_node_print_dependency_cycle(dag, itA->node, node, itA->name);
1000                                 }
1001                         }
1002                 }
1003         }
1004
1005         /* parent relations are only needed for cycle checking, so free now */
1006         for(node = dag->DagNode.first; node; node= node->next) {
1007                 while (node->parent) {
1008                         itA = node->parent->next;
1009                         MEM_freeN(node->parent);                        
1010                         node->parent = itA;
1011                 }
1012         }
1013 }
1014
1015 /*
1016  * MainDAG is the DAG of all objects in current scene
1017  * used only for drawing there is one also in each scene
1018  */
1019 static DagForest * MainDag = NULL;
1020
1021 DagForest *getMainDag(void)
1022 {
1023         return MainDag;
1024 }
1025
1026
1027 void setMainDag(DagForest *dag)
1028 {
1029         MainDag = dag;
1030 }
1031
1032
1033 /*
1034  * note for BFS/DFS
1035  * in theory we should sweep the whole array
1036  * but in our case the first node is the scene
1037  * and is linked to every other object
1038  *
1039  * for general case we will need to add outer loop
1040  */
1041
1042 /*
1043  * ToDo : change pos kludge
1044  */
1045
1046 /* adjust levels for drawing in oops space */
1047 void graph_bfs(void)
1048 {
1049         DagNode *node;
1050         DagNodeQueue *nqueue;
1051         int pos[50];
1052         int i;
1053         DagAdjList *itA;
1054         int minheight;
1055         
1056         /* fprintf(stderr,"starting BFS \n ------------\n"); */ 
1057         nqueue = queue_create(DAGQUEUEALLOC);
1058         for ( i=0; i<50; i++)
1059                 pos[i] = 0;
1060         
1061         /* Init
1062          * dagnode.first is alway the root (scene) 
1063          */
1064         node = MainDag->DagNode.first;
1065         while(node) {
1066                 node->color = DAG_WHITE;
1067                 node->BFS_dist = 9999;
1068                 node->k = 0;
1069                 node = node->next;
1070         }
1071         
1072         node = MainDag->DagNode.first;
1073         if (node->color == DAG_WHITE) {
1074                 node->color = DAG_GRAY;
1075                 node->BFS_dist = 1;
1076                 push_queue(nqueue,node);  
1077                 while(nqueue->count) {
1078                         node = pop_queue(nqueue);
1079                         
1080                         minheight = pos[node->BFS_dist];
1081                         itA = node->child;
1082                         while(itA != NULL) {
1083                                 if((itA->node->color == DAG_WHITE) ) {
1084                                         itA->node->color = DAG_GRAY;
1085                                         itA->node->BFS_dist = node->BFS_dist + 1;
1086                                         itA->node->k = (float) minheight;
1087                                         push_queue(nqueue,itA->node);
1088                                 }
1089                                 
1090                                  else {
1091                                         fprintf(stderr,"bfs not dag tree edge color :%i \n",itA->node->color);
1092                                 }
1093                                  
1094                                 
1095                                 itA = itA->next;
1096                         }
1097                         if (pos[node->BFS_dist] > node->k ) {
1098                                 pos[node->BFS_dist] += 1;                               
1099                                 node->k = (float) pos[node->BFS_dist];
1100                         } else {
1101                                 pos[node->BFS_dist] = (int) node->k +1;
1102                         }
1103                         set_node_xy(node, node->BFS_dist*DEPSX*2, pos[node->BFS_dist]*DEPSY*2);
1104                         node->color = DAG_BLACK;
1105                         /*
1106                          fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);   
1107                          */
1108                 }
1109         }
1110         queue_delete(nqueue);
1111 }
1112
1113 int pre_and_post_BFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1114         DagNode *node;
1115         
1116         node = dag->DagNode.first;
1117         return pre_and_post_source_BFS(dag, mask,  node,  pre_func,  post_func, data);
1118 }
1119
1120
1121 int pre_and_post_source_BFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1122 {
1123         DagNode *node;
1124         DagNodeQueue *nqueue;
1125         DagAdjList *itA;
1126         int     retval = 0;
1127         /* fprintf(stderr,"starting BFS \n ------------\n"); */ 
1128         
1129         /* Init
1130                 * dagnode.first is alway the root (scene) 
1131                 */
1132         node = dag->DagNode.first;
1133         nqueue = queue_create(DAGQUEUEALLOC);
1134         while(node) {
1135                 node->color = DAG_WHITE;
1136                 node->BFS_dist = 9999;
1137                 node = node->next;
1138         }
1139         
1140         node = source;
1141         if (node->color == DAG_WHITE) {
1142                 node->color = DAG_GRAY;
1143                 node->BFS_dist = 1;
1144                 pre_func(node->ob,data);
1145                 
1146                 while(nqueue->count) {
1147                         node = pop_queue(nqueue);
1148                         
1149                         itA = node->child;
1150                         while(itA != NULL) {
1151                                 if((itA->node->color == DAG_WHITE) && (itA->type & mask)) {
1152                                         itA->node->color = DAG_GRAY;
1153                                         itA->node->BFS_dist = node->BFS_dist + 1;
1154                                         push_queue(nqueue,itA->node);
1155                                         pre_func(node->ob,data);
1156                                 }
1157                                 
1158                                 else { // back or cross edge
1159                                         retval = 1;
1160                                 }
1161                                 itA = itA->next;
1162                         }
1163                         post_func(node->ob,data);
1164                         node->color = DAG_BLACK;
1165                         /*
1166                          fprintf(stderr,"BFS node : %20s %i %5.0f %5.0f\n",((ID *) node->ob)->name,node->BFS_dist, node->x, node->y);   
1167                          */
1168                 }
1169         }
1170         queue_delete(nqueue);
1171         return retval;
1172 }
1173
1174 /* non recursive version of DFS, return queue -- outer loop present to catch odd cases (first level cycles)*/
1175 DagNodeQueue * graph_dfs(void)
1176 {
1177         DagNode *node;
1178         DagNodeQueue *nqueue;
1179         DagNodeQueue *retqueue;
1180         int pos[50];
1181         int i;
1182         DagAdjList *itA;
1183         int time;
1184         int skip = 0;
1185         int minheight;
1186         int maxpos=0;
1187         int     is_cycle = 0;
1188         /*
1189          *fprintf(stderr,"starting DFS \n ------------\n");
1190          */     
1191         nqueue = queue_create(DAGQUEUEALLOC);
1192         retqueue = queue_create(MainDag->numNodes);
1193         for ( i=0; i<50; i++)
1194                 pos[i] = 0;
1195         
1196         /* Init
1197          * dagnode.first is alway the root (scene) 
1198          */
1199         node = MainDag->DagNode.first;
1200         while(node) {
1201                 node->color = DAG_WHITE;
1202                 node->DFS_dist = 9999;
1203                 node->DFS_dvtm = node->DFS_fntm = 9999;
1204                 node->k = 0;
1205                 node =  node->next;
1206         }
1207         
1208         time = 1;
1209         
1210         node = MainDag->DagNode.first;
1211
1212         do {
1213         if (node->color == DAG_WHITE) {
1214                 node->color = DAG_GRAY;
1215                 node->DFS_dist = 1;
1216                 node->DFS_dvtm = time;
1217                 time++;
1218                 push_stack(nqueue,node);  
1219                         
1220                 while(nqueue->count) {
1221                         //graph_print_queue(nqueue);
1222
1223                          skip = 0;
1224                         node = get_top_node_queue(nqueue);
1225                         
1226                         minheight = pos[node->DFS_dist];
1227
1228                         itA = node->child;
1229                         while(itA != NULL) {
1230                                 if((itA->node->color == DAG_WHITE) ) {
1231                                         itA->node->DFS_dvtm = time;
1232                                         itA->node->color = DAG_GRAY;
1233
1234                                         time++;
1235                                         itA->node->DFS_dist = node->DFS_dist + 1;
1236                                         itA->node->k = (float) minheight;
1237                                         push_stack(nqueue,itA->node);
1238                                         skip = 1;
1239                                         break;
1240                                 } else { 
1241                                         if (itA->node->color == DAG_GRAY) { // back edge
1242                                                 fprintf(stderr,"dfs back edge :%15s %15s \n",((ID *) node->ob)->name, ((ID *) itA->node->ob)->name);
1243                                                 is_cycle = 1;
1244                                         } else if (itA->node->color == DAG_BLACK) {
1245                                                 ;
1246                                                 /* already processed node but we may want later to change distance either to shorter to longer.
1247                                                  * DFS_dist is the first encounter  
1248                                                 */
1249                                                 /*if (node->DFS_dist >= itA->node->DFS_dist)
1250                                                         itA->node->DFS_dist = node->DFS_dist + 1;
1251                                                         
1252                                                         fprintf(stderr,"dfs forward or cross edge :%15s %i-%i %15s %i-%i \n",
1253                                                                 ((ID *) node->ob)->name,
1254                                                                 node->DFS_dvtm, 
1255                                                                 node->DFS_fntm, 
1256                                                                 ((ID *) itA->node->ob)->name, 
1257                                                                 itA->node->DFS_dvtm,
1258                                                                 itA->node->DFS_fntm);
1259                                         */
1260                                         } else 
1261                                                 fprintf(stderr,"dfs unknown edge \n");
1262                                 }
1263                                 itA = itA->next;
1264                         }                       
1265
1266                         if (!skip) {
1267                                 node = pop_queue(nqueue);
1268                                 node->color = DAG_BLACK;
1269
1270                                 node->DFS_fntm = time;
1271                                 time++;
1272                                 if (node->DFS_dist > maxpos)
1273                                         maxpos = node->DFS_dist;
1274                                 if (pos[node->DFS_dist] > node->k ) {
1275                                         pos[node->DFS_dist] += 1;                               
1276                                         node->k = (float) pos[node->DFS_dist];
1277                                 } else {
1278                                         pos[node->DFS_dist] = (int) node->k +1;
1279                                 }
1280                                 set_node_xy(node, node->DFS_dist*DEPSX*2, pos[node->DFS_dist]*DEPSY*2);
1281                                 
1282                                 /*
1283                                  fprintf(stderr,"DFS node : %20s %i %i %i %i\n",((ID *) node->ob)->name,node->BFS_dist, node->DFS_dist, node->DFS_dvtm, node->DFS_fntm );       
1284                                 */
1285                                  push_stack(retqueue,node);
1286                                 
1287                         }
1288                 }
1289         }
1290                 node = node->next;
1291         } while (node);
1292 //        fprintf(stderr,"i size : %i \n", maxpos);
1293           
1294         queue_delete(nqueue);
1295           return(retqueue);
1296 }
1297
1298 /* unused */
1299 int pre_and_post_DFS(DagForest *dag, short mask, graph_action_func pre_func, graph_action_func post_func, void **data) {
1300         DagNode *node;
1301
1302         node = dag->DagNode.first;
1303         return pre_and_post_source_DFS(dag, mask,  node,  pre_func,  post_func, data);
1304 }
1305
1306 int pre_and_post_source_DFS(DagForest *dag, short mask, DagNode *source, graph_action_func pre_func, graph_action_func post_func, void **data)
1307 {
1308         DagNode *node;
1309         DagNodeQueue *nqueue;
1310         DagAdjList *itA;
1311         int time;
1312         int skip = 0;
1313         int retval = 0;
1314         /*
1315          *fprintf(stderr,"starting DFS \n ------------\n");
1316          */     
1317         nqueue = queue_create(DAGQUEUEALLOC);
1318         
1319         /* Init
1320                 * dagnode.first is alway the root (scene) 
1321                 */
1322         node = dag->DagNode.first;
1323         while(node) {
1324                 node->color = DAG_WHITE;
1325                 node->DFS_dist = 9999;
1326                 node->DFS_dvtm = node->DFS_fntm = 9999;
1327                 node->k = 0;
1328                 node =  node->next;
1329         }
1330         
1331         time = 1;
1332         
1333         node = source;
1334         do {
1335                 if (node->color == DAG_WHITE) {
1336                         node->color = DAG_GRAY;
1337                         node->DFS_dist = 1;
1338                         node->DFS_dvtm = time;
1339                         time++;
1340                         push_stack(nqueue,node);  
1341                         pre_func(node->ob,data);
1342
1343                         while(nqueue->count) {
1344                                 skip = 0;
1345                                 node = get_top_node_queue(nqueue);
1346                                                                 
1347                                 itA = node->child;
1348                                 while(itA != NULL) {
1349                                         if((itA->node->color == DAG_WHITE) && (itA->type & mask) ) {
1350                                                 itA->node->DFS_dvtm = time;
1351                                                 itA->node->color = DAG_GRAY;
1352                                                 
1353                                                 time++;
1354                                                 itA->node->DFS_dist = node->DFS_dist + 1;
1355                                                 push_stack(nqueue,itA->node);
1356                                                 pre_func(node->ob,data);
1357
1358                                                 skip = 1;
1359                                                 break;
1360                                         } else {
1361                                                 if (itA->node->color == DAG_GRAY) {// back edge
1362                                                         retval = 1;
1363                                                 }
1364 //                                              else if (itA->node->color == DAG_BLACK) { // cross or forward
1365 //                                                      ;
1366                                         }
1367                                         itA = itA->next;
1368                                 }                       
1369                                 
1370                                 if (!skip) {
1371                                         node = pop_queue(nqueue);
1372                                         node->color = DAG_BLACK;
1373                                         
1374                                         node->DFS_fntm = time;
1375                                         time++;
1376                                         post_func(node->ob,data);
1377                                 }
1378                         }
1379                 }
1380                 node = node->next;
1381         } while (node);
1382         queue_delete(nqueue);
1383         return(retval);
1384 }
1385
1386
1387 // used to get the obs owning a datablock
1388 struct DagNodeQueue *get_obparents(struct DagForest     *dag, void *ob) 
1389 {
1390         DagNode * node, *node1;
1391         DagNodeQueue *nqueue;
1392         DagAdjList *itA;
1393
1394         node = dag_find_node(dag,ob);
1395         if(node==NULL) {
1396                 return NULL;
1397         }
1398         else if (node->ancestor_count == 1) { // simple case
1399                 nqueue = queue_create(1);
1400                 push_queue(nqueue,node);
1401         } else {        // need to go over the whole dag for adj list
1402                 nqueue = queue_create(node->ancestor_count);
1403                 
1404                 node1 = dag->DagNode.first;
1405                 do {
1406                         if (node1->DFS_fntm > node->DFS_fntm) { // a parent is finished after child. must check adj list
1407                                 itA = node->child;
1408                                 while(itA != NULL) {
1409                                         if ((itA->node == node) && (itA->type == DAG_RL_DATA)) {
1410                                                 push_queue(nqueue,node);
1411                                         }
1412                                         itA = itA->next;
1413                                 }
1414                         }
1415                         node1 = node1->next;
1416                 } while (node1);
1417         }
1418         return nqueue;
1419 }
1420
1421 struct DagNodeQueue *get_first_ancestors(struct DagForest       *dag, void *ob)
1422 {
1423         DagNode * node, *node1;
1424         DagNodeQueue *nqueue;
1425         DagAdjList *itA;
1426         
1427         node = dag_find_node(dag,ob);
1428         
1429         // need to go over the whole dag for adj list
1430         nqueue = queue_create(node->ancestor_count);
1431         
1432         node1 = dag->DagNode.first;
1433         do {
1434                 if (node1->DFS_fntm > node->DFS_fntm) { 
1435                         itA = node->child;
1436                         while(itA != NULL) {
1437                                 if (itA->node == node) {
1438                                         push_queue(nqueue,node);
1439                                 }
1440                                 itA = itA->next;
1441                         }
1442                 }
1443                 node1 = node1->next;
1444         } while (node1);
1445         
1446         return nqueue;  
1447 }
1448
1449 // standard DFS list
1450 struct DagNodeQueue *get_all_childs(struct DagForest    *dag, void *ob)
1451 {
1452         DagNode *node;
1453         DagNodeQueue *nqueue;
1454         DagNodeQueue *retqueue;
1455         DagAdjList *itA;
1456         int time;
1457         int skip = 0;
1458
1459         nqueue = queue_create(DAGQUEUEALLOC);
1460         retqueue = queue_create(dag->numNodes); // was MainDag... why? (ton)
1461         
1462         node = dag->DagNode.first;
1463         while(node) {
1464                 node->color = DAG_WHITE;
1465                 node =  node->next;
1466         }
1467         
1468         time = 1;
1469         
1470         node = dag_find_node(dag, ob);   // could be done in loop above (ton)
1471         if(node) { // can be null for newly added objects
1472                 
1473                 node->color = DAG_GRAY;
1474                 time++;
1475                 push_stack(nqueue,node);  
1476                 
1477                 while(nqueue->count) {
1478                         
1479                         skip = 0;
1480                         node = get_top_node_queue(nqueue);
1481                                         
1482                         itA = node->child;
1483                         while(itA != NULL) {
1484                                 if((itA->node->color == DAG_WHITE) ) {
1485                                         itA->node->DFS_dvtm = time;
1486                                         itA->node->color = DAG_GRAY;
1487                                         
1488                                         time++;
1489                                         push_stack(nqueue,itA->node);
1490                                         skip = 1;
1491                                         break;
1492                                 } 
1493                                 itA = itA->next;
1494                         }                       
1495                         
1496                         if (!skip) {
1497                                 node = pop_queue(nqueue);
1498                                 node->color = DAG_BLACK;
1499                                 
1500                                 time++;
1501                                 push_stack(retqueue,node);                      
1502                         }
1503                 }
1504         }
1505         queue_delete(nqueue);
1506         return(retqueue);
1507 }
1508
1509 /* unused */
1510 short   are_obs_related(struct DagForest        *dag, void *ob1, void *ob2) {
1511         DagNode * node;
1512         DagAdjList *itA;
1513         
1514         node = dag_find_node(dag, ob1);
1515         
1516         itA = node->child;
1517         while(itA != NULL) {
1518                 if((itA->node->ob == ob2) ) {
1519                         return itA->node->type;
1520                 } 
1521                 itA = itA->next;
1522         }
1523         return DAG_NO_RELATION;
1524 }
1525
1526 int     is_acyclic( DagForest   *dag) {
1527         return dag->is_acyclic;
1528 }
1529
1530 void set_node_xy(DagNode *node, float x, float y)
1531 {
1532         node->x = x;
1533         node->y = y;
1534 }
1535
1536
1537 /* debug test functions */
1538
1539 void graph_print_queue(DagNodeQueue *nqueue)
1540 {       
1541         DagNodeQueueElem *queueElem;
1542         
1543         queueElem = nqueue->first;
1544         while(queueElem) {
1545                 fprintf(stderr,"** %s %i %i-%i ",((ID *) queueElem->node->ob)->name,queueElem->node->color,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1546                 queueElem = queueElem->next;            
1547         }
1548         fprintf(stderr,"\n");
1549 }
1550
1551 void graph_print_queue_dist(DagNodeQueue *nqueue)
1552 {       
1553         DagNodeQueueElem *queueElem;
1554         int max, count;
1555         
1556         queueElem = nqueue->first;
1557         max = queueElem->node->DFS_fntm;
1558         count = 0;
1559         while(queueElem) {
1560                 fprintf(stderr,"** %25s %2.2i-%2.2i ",((ID *) queueElem->node->ob)->name,queueElem->node->DFS_dvtm,queueElem->node->DFS_fntm);
1561                 while (count < queueElem->node->DFS_dvtm-1) { fputc(' ',stderr); count++;}
1562                 fputc('|',stderr); 
1563                 while (count < queueElem->node->DFS_fntm-2) { fputc('-',stderr); count++;}
1564                 fputc('|',stderr);
1565                 fputc('\n',stderr);
1566                 count = 0;
1567                 queueElem = queueElem->next;            
1568         }
1569         fprintf(stderr,"\n");
1570 }
1571
1572 void graph_print_adj_list(void)
1573 {
1574         DagNode *node;
1575         DagAdjList *itA;
1576         
1577         node = (getMainDag())->DagNode.first;
1578         while(node) {
1579                 fprintf(stderr,"node : %s col: %i",((ID *) node->ob)->name, node->color);               
1580                 itA = node->child;
1581                 while (itA) {
1582                         fprintf(stderr,"-- %s ",((ID *) itA->node->ob)->name);
1583                         
1584                         itA = itA->next;
1585                 }
1586                 fprintf(stderr,"\n");
1587                 node = node->next;
1588         }
1589 }
1590
1591 /* ************************ API *********************** */
1592
1593 /* groups with objects in this scene need to be put in the right order as well */
1594 static void scene_sort_groups(Scene *sce)
1595 {
1596         Base *base;
1597         Group *group;
1598         GroupObject *go;
1599         Object *ob;
1600         
1601         /* test; are group objects all in this scene? */
1602         for(ob= G.main->object.first; ob; ob= ob->id.next) {
1603                 ob->id.flag &= ~LIB_DOIT;
1604                 ob->id.newid= NULL;     /* newid abuse for GroupObject */
1605         }
1606         for(base = sce->base.first; base; base= base->next)
1607                 base->object->id.flag |= LIB_DOIT;
1608         
1609         for(group= G.main->group.first; group; group= group->id.next) {
1610                 for(go= group->gobject.first; go; go= go->next) {
1611                         if((go->ob->id.flag & LIB_DOIT)==0)
1612                                 break;
1613                 }
1614                 /* this group is entirely in this scene */
1615                 if(go==NULL) {
1616                         ListBase listb= {NULL, NULL};
1617                         
1618                         for(go= group->gobject.first; go; go= go->next)
1619                                 go->ob->id.newid= (ID *)go;
1620                         
1621                         /* in order of sorted bases we reinsert group objects */
1622                         for(base = sce->base.first; base; base= base->next) {
1623                                 
1624                                 if(base->object->id.newid) {
1625                                         go= (GroupObject *)base->object->id.newid;
1626                                         base->object->id.newid= NULL;
1627                                         BLI_remlink( &group->gobject, go);
1628                                         BLI_addtail( &listb, go);
1629                                 }
1630                         }
1631                         /* copy the newly sorted listbase */
1632                         group->gobject= listb;
1633                 }
1634         }
1635 }
1636
1637 /* sort the base list on dependency order */
1638 void DAG_scene_sort(struct Scene *sce)
1639 {
1640         DagNode *node;
1641         DagNodeQueue *nqueue;
1642         DagAdjList *itA;
1643         int time;
1644         int skip = 0;
1645         ListBase tempbase;
1646         Base *base;
1647         
1648         tempbase.first= tempbase.last= NULL;
1649         
1650         build_dag(sce, DAG_RL_ALL_BUT_DATA);
1651         
1652         dag_check_cycle(sce->theDag);
1653
1654         nqueue = queue_create(DAGQUEUEALLOC);
1655         
1656         for(node = sce->theDag->DagNode.first; node; node= node->next) {
1657                 node->color = DAG_WHITE;
1658         }
1659         
1660         time = 1;
1661         
1662         node = sce->theDag->DagNode.first;
1663         
1664         node->color = DAG_GRAY;
1665         time++;
1666         push_stack(nqueue,node);  
1667         
1668         while(nqueue->count) {
1669                 
1670                 skip = 0;
1671                 node = get_top_node_queue(nqueue);
1672                 
1673                 itA = node->child;
1674                 while(itA != NULL) {
1675                         if((itA->node->color == DAG_WHITE) ) {
1676                                 itA->node->DFS_dvtm = time;
1677                                 itA->node->color = DAG_GRAY;
1678                                 
1679                                 time++;
1680                                 push_stack(nqueue,itA->node);
1681                                 skip = 1;
1682                                 break;
1683                         } 
1684                         itA = itA->next;
1685                 }                       
1686                 
1687                 if (!skip) {
1688                         if (node) {
1689                                 node = pop_queue(nqueue);
1690                                 if (node->ob == sce)    // we are done
1691                                         break ;
1692                                 node->color = DAG_BLACK;
1693                                 
1694                                 time++;
1695                                 base = sce->base.first;
1696                                 while (base && base->object != node->ob)
1697                                         base = base->next;
1698                                 if(base) {
1699                                         BLI_remlink(&sce->base,base);
1700                                         BLI_addhead(&tempbase,base);
1701                                 }
1702                         }       
1703                 }
1704         }
1705         
1706         // temporal correction for circular dependancies
1707         base = sce->base.first;
1708         while (base) {
1709                 BLI_remlink(&sce->base,base);
1710                 BLI_addhead(&tempbase,base);
1711                 //if(G.f & G_DEBUG) 
1712                         printf("cyclic %s\n", base->object->id.name);
1713                 base = sce->base.first;
1714         }
1715         
1716         sce->base = tempbase;
1717         queue_delete(nqueue);
1718         
1719         /* all groups with objects in this scene gets resorted too */
1720         scene_sort_groups(sce);
1721         
1722         if(G.f & G_DEBUG) {
1723                 printf("\nordered\n");
1724                 for(base = sce->base.first; base; base= base->next) {
1725                         printf(" %s\n", base->object->id.name);
1726                 }
1727         }
1728         /* temporal...? */
1729         sce->recalc |= SCE_PRV_CHANGED; /* test for 3d preview */
1730 }
1731
1732 /* node was checked to have lasttime != curtime and is if type ID_OB */
1733 static void flush_update_node(DagNode *node, unsigned int layer, int curtime)
1734 {
1735         DagAdjList *itA;
1736         Object *ob, *obc;
1737         int oldflag, changed=0;
1738         unsigned int all_layer;
1739         
1740         node->lasttime= curtime;
1741         
1742         ob= node->ob;
1743         if(ob && (ob->recalc & OB_RECALC)) {
1744                 all_layer= ob->lay;
1745
1746                 /* got an object node that changes, now check relations */
1747                 for(itA = node->child; itA; itA= itA->next) {
1748                         all_layer |= itA->lay;
1749                         /* the relationship is visible */
1750                         if((itA->lay & layer)) { // XXX || (itA->node->ob == obedit)
1751                                 if(itA->node->type==ID_OB) {
1752                                         obc= itA->node->ob;
1753                                         oldflag= obc->recalc;
1754                                         
1755                                         /* got a ob->obc relation, now check if flag needs flush */
1756                                         if(ob->recalc & OB_RECALC_OB) {
1757                                                 if(itA->type & DAG_RL_OB_OB) {
1758                                                         //printf("ob %s changes ob %s\n", ob->id.name, obc->id.name);
1759                                                         obc->recalc |= OB_RECALC_OB;
1760                                                 }
1761                                                 if(itA->type & DAG_RL_OB_DATA) {
1762                                                         //printf("ob %s changes obdata %s\n", ob->id.name, obc->id.name);
1763                                                         obc->recalc |= OB_RECALC_DATA;
1764                                                 }
1765                                         }
1766                                         if(ob->recalc & OB_RECALC_DATA) {
1767                                                 if(itA->type & DAG_RL_DATA_OB) {
1768                                                         //printf("obdata %s changes ob %s\n", ob->id.name, obc->id.name);
1769                                                         obc->recalc |= OB_RECALC_OB;
1770                                                 }
1771                                                 if(itA->type & DAG_RL_DATA_DATA) {
1772                                                         //printf("obdata %s changes obdata %s\n", ob->id.name, obc->id.name);
1773                                                         obc->recalc |= OB_RECALC_DATA;
1774                                                 }
1775                                         }
1776                                         if(oldflag!=obc->recalc) changed= 1;
1777                                 }
1778                         }
1779                 }
1780                 /* even nicer, we can clear recalc flags...  */
1781                 if((all_layer & layer)==0) { // XXX && (ob != obedit)) {
1782                         /* but existing displaylists or derivedmesh should be freed */
1783                         if(ob->recalc & OB_RECALC_DATA)
1784                                 object_free_display(ob);
1785                         
1786                         ob->recalc &= ~OB_RECALC;
1787                 }
1788         }
1789         
1790         /* check case where child changes and parent forcing obdata to change */
1791         /* should be done regardless if this ob has recalc set */
1792         /* could merge this in with loop above...? (ton) */
1793         for(itA = node->child; itA; itA= itA->next) {
1794                 /* the relationship is visible */
1795                 if((itA->lay & layer)) {                // XXX  || (itA->node->ob == obedit)
1796                         if(itA->node->type==ID_OB) {
1797                                 obc= itA->node->ob;
1798                                 /* child moves */
1799                                 if((obc->recalc & OB_RECALC)==OB_RECALC_OB) {
1800                                         /* parent has deforming info */
1801                                         if(itA->type & (DAG_RL_OB_DATA|DAG_RL_DATA_DATA)) {
1802                                                 // printf("parent %s changes ob %s\n", ob->id.name, obc->id.name);
1803                                                 obc->recalc |= OB_RECALC_DATA;
1804                                         }
1805                                 }
1806                         }
1807                 }
1808         }
1809         
1810         /* we only go deeper if node not checked or something changed  */
1811         for(itA = node->child; itA; itA= itA->next) {
1812                 if(changed || itA->node->lasttime!=curtime) 
1813                         flush_update_node(itA->node, layer, curtime);
1814         }
1815         
1816 }
1817
1818 /* node was checked to have lasttime != curtime , and is of type ID_OB */
1819 static unsigned int flush_layer_node(Scene *sce, DagNode *node, int curtime)
1820 {
1821         DagAdjList *itA;
1822         
1823         node->lasttime= curtime;
1824         node->lay= node->scelay;
1825         
1826         for(itA = node->child; itA; itA= itA->next) {
1827                 if(itA->node->type==ID_OB) {
1828                         if(itA->node->lasttime!=curtime) {
1829                                 itA->lay= flush_layer_node(sce, itA->node, curtime);  // lay is only set once for each relation
1830                         }
1831                         else itA->lay= itA->node->lay;
1832                         
1833                         node->lay |= itA->lay;
1834                 }
1835         }
1836
1837         return node->lay;
1838 }
1839
1840 /* node was checked to have lasttime != curtime , and is of type ID_OB */
1841 static void flush_pointcache_reset(Scene *scene, DagNode *node, int curtime, int reset)
1842 {
1843         DagAdjList *itA;
1844         Object *ob;
1845         
1846         node->lasttime= curtime;
1847         
1848         for(itA = node->child; itA; itA= itA->next) {
1849                 if(itA->node->type==ID_OB) {
1850                         if(itA->node->lasttime!=curtime) {
1851                                 ob= (Object*)(node->ob);
1852
1853                                 if(reset || (ob->recalc & OB_RECALC)) {
1854                                         if(BKE_ptcache_object_reset(scene, ob, PTCACHE_RESET_DEPSGRAPH))
1855                                                 ob->recalc |= OB_RECALC_DATA;
1856
1857                                         flush_pointcache_reset(scene, itA->node, curtime, 1);
1858                                 }
1859                                 else
1860                                         flush_pointcache_reset(scene, itA->node, curtime, 0);
1861                         }
1862                 }
1863         }
1864 }
1865
1866 /* flushes all recalc flags in objects down the dependency tree */
1867 void DAG_scene_flush_update(Scene *sce, unsigned int lay, int time)
1868 {
1869         DagNode *firstnode, *node;
1870         DagAdjList *itA;
1871         Object *ob;
1872         Base *base;
1873         int lasttime;
1874         
1875         if(sce->theDag==NULL) {
1876                 printf("DAG zero... not allowed to happen!\n");
1877                 DAG_scene_sort(sce);
1878         }
1879         
1880         firstnode= sce->theDag->DagNode.first;  // always scene node
1881
1882         for(itA = firstnode->child; itA; itA= itA->next)
1883                 itA->lay= 0;
1884         
1885         /* first we flush the layer flags */
1886         sce->theDag->time++;    // so we know which nodes were accessed
1887         lasttime= sce->theDag->time;
1888
1889
1890         for(base= sce->base.first; base; base= base->next) {
1891                 node= dag_get_node(sce->theDag, base->object);
1892                 if(node)
1893                         node->scelay= base->object->lay;
1894                 else
1895                         node->scelay= 0;
1896         }
1897
1898         for(itA = firstnode->child; itA; itA= itA->next)
1899                 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB) 
1900                         flush_layer_node(sce, itA->node, lasttime);
1901         
1902         /* then we use the relationships + layer info to flush update events */
1903         sce->theDag->time++;    // so we know which nodes were accessed
1904         lasttime= sce->theDag->time;
1905         for(itA = firstnode->child; itA; itA= itA->next)
1906                 if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)
1907                         flush_update_node(itA->node, lay, lasttime);
1908
1909         /* if update is not due to time change, do pointcache clears */
1910         if(!time) {
1911                 sce->theDag->time++;    // so we know which nodes were accessed
1912                 lasttime= sce->theDag->time;
1913                 for(itA = firstnode->child; itA; itA= itA->next) {
1914                         if(itA->node->lasttime!=lasttime && itA->node->type==ID_OB)  {
1915                                 ob= (Object*)(itA->node->ob);
1916
1917                                 if(ob->recalc & OB_RECALC) {
1918                                         if(BKE_ptcache_object_reset(sce, ob, PTCACHE_RESET_DEPSGRAPH))
1919                                                 ob->recalc |= OB_RECALC_DATA;
1920
1921                                         flush_pointcache_reset(sce, itA->node, lasttime, 1);
1922                                 }
1923                                 else
1924                                         flush_pointcache_reset(sce, itA->node, lasttime, 0);
1925                         }
1926                 }
1927         }
1928 }
1929
1930 static int object_modifiers_use_time(Object *ob)
1931 {
1932         ModifierData *md;
1933
1934         for (md=ob->modifiers.first; md; md=md->next)
1935                 if (modifier_dependsOnTime(md))
1936                         return 1;
1937
1938         return 0;
1939 }
1940
1941 static short animdata_use_time(AnimData *adt)
1942 {
1943         NlaTrack *nlt;
1944         
1945         if(adt==NULL) return 0;
1946         
1947         /* check action - only if assigned, and it has anim curves */
1948         if (adt->action && adt->action->curves.first)
1949                 return 1;
1950         
1951         /* check NLA tracks + strips */
1952         for (nlt= adt->nla_tracks.first; nlt; nlt= nlt->next) {
1953                 if (nlt->strips.first)
1954                         return 1;
1955         }
1956         
1957         return 0;
1958 }
1959
1960 static void dag_object_time_update_flags(Object *ob)
1961 {
1962         if(ob->constraints.first) {
1963                 bConstraint *con;
1964                 for (con = ob->constraints.first; con; con=con->next) {
1965                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
1966                         ListBase targets = {NULL, NULL};
1967                         bConstraintTarget *ct;
1968                         
1969                         if (cti && cti->get_constraint_targets) {
1970                                 cti->get_constraint_targets(con, &targets);
1971                                 
1972                                 for (ct= targets.first; ct; ct= ct->next) {
1973                                         if (ct->tar) {
1974                                                 ob->recalc |= OB_RECALC_OB;
1975                                                 break;
1976                                         }
1977                                 }
1978                                 
1979                                 if (cti->flush_constraint_targets)
1980                                         cti->flush_constraint_targets(con, &targets, 1);
1981                         }
1982                 }
1983         }
1984         
1985         if(ob->scriptlink.totscript) ob->recalc |= OB_RECALC_OB;
1986         
1987         if(ob->parent) {
1988                 /* motion path or bone child */
1989                 if(ob->parent->type==OB_CURVE || ob->parent->type==OB_ARMATURE) ob->recalc |= OB_RECALC_OB;
1990         }
1991         
1992 #if 0 // XXX old animation system
1993         if(ob->nlastrips.first) {
1994                 if(ob->dup_group) {
1995                         bActionStrip *strip;
1996                         /* this case is for groups with nla, whilst nla target has no action or nla */
1997                         for(strip= ob->nlastrips.first; strip; strip= strip->next) {
1998                                 if(strip->object)
1999                                         strip->object->recalc |= OB_RECALC;
2000                         }
2001                 }
2002         }
2003 #endif // XXX old animation system
2004         
2005         if(animdata_use_time(ob->adt)) {
2006                 ob->recalc |= OB_RECALC;
2007                 ob->adt->recalc |= ADT_RECALC_ANIM;
2008         }
2009         
2010         if((ob->adt) && (ob->type==OB_ARMATURE)) ob->recalc |= OB_RECALC_DATA;
2011         
2012         if(object_modifiers_use_time(ob)) ob->recalc |= OB_RECALC_DATA;
2013         if((ob->pose) && (ob->pose->flag & POSE_CONSTRAINTS_TIMEDEPEND)) ob->recalc |= OB_RECALC_DATA;
2014         
2015         {
2016                 Mesh *me;
2017                 Curve *cu;
2018                 Lattice *lt;
2019                 
2020                 switch(ob->type) {
2021                         case OB_MESH:
2022                                 me= ob->data;
2023                                 if(me->key) {
2024                                         if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2025                                                 ob->recalc |= OB_RECALC_DATA;
2026                                                 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2027                                         }
2028                                 }
2029                                 if(ob->particlesystem.first)
2030                                         ob->recalc |= OB_RECALC_DATA;
2031                                 break;
2032                         case OB_CURVE:
2033                         case OB_SURF:
2034                                 cu= ob->data;
2035                                 if(cu->key) {
2036                                         if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2037                                                 ob->recalc |= OB_RECALC_DATA;
2038                                                 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2039                                         }
2040                                 }
2041                                 break;
2042                         case OB_FONT:
2043                                 cu= ob->data;
2044                                 if(cu->nurb.first==NULL && cu->str && cu->vfont)
2045                                         ob->recalc |= OB_RECALC_DATA;
2046                                 break;
2047                         case OB_LATTICE:
2048                                 lt= ob->data;
2049                                 if(lt->key) {
2050                                         if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
2051                                                 ob->recalc |= OB_RECALC_DATA;
2052                                                 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
2053                                         }
2054                                 }
2055                                         break;
2056                         case OB_MBALL:
2057                                 if(ob->transflag & OB_DUPLI) ob->recalc |= OB_RECALC_DATA;
2058                                 break;
2059                 }
2060
2061                 if(ob->particlesystem.first) {
2062                         ParticleSystem *psys= ob->particlesystem.first;
2063
2064                         for(; psys; psys=psys->next) {
2065                                 if(psys_check_enabled(ob, psys)) {
2066                                         ob->recalc |= OB_RECALC_DATA;
2067                                         break;
2068                                 }
2069                         }
2070                 }
2071         }               
2072 }
2073
2074 /* flag all objects that need recalc, for changes in time for example */
2075 void DAG_scene_update_flags(Scene *scene, unsigned int lay)
2076 {
2077         Base *base;
2078         Object *ob;
2079         Group *group;
2080         GroupObject *go;
2081         Scene *sce;
2082         
2083         /* set ob flags where animated systems are */
2084         for(SETLOOPER(scene, base)) {
2085                 ob= base->object;
2086                 
2087                 /* now if DagNode were part of base, the node->lay could be checked... */
2088                 /* we do all now, since the scene_flush checks layers and clears recalc flags even */
2089                 dag_object_time_update_flags(ob);
2090                 
2091                 /* handled in next loop */
2092                 if(ob->dup_group) 
2093                         ob->dup_group->id.flag |= LIB_DOIT;
2094         }       
2095         
2096         /* we do groups each once */
2097         for(group= G.main->group.first; group; group= group->id.next) {
2098                 if(group->id.flag & LIB_DOIT) {
2099                         for(go= group->gobject.first; go; go= go->next) {
2100                                 dag_object_time_update_flags(go->ob);
2101                         }
2102                 }
2103         }
2104         
2105         for(sce= scene; sce; sce= sce->set)
2106                 DAG_scene_flush_update(sce, lay, 1);
2107         
2108         /* test: set time flag, to disable baked systems to update */
2109         for(SETLOOPER(scene, base)) {
2110                 ob= base->object;
2111                 if(ob->recalc)
2112                         ob->recalc |= OB_RECALC_TIME;
2113         }
2114         
2115         /* hrmf... an exception to look at once, for invisible camera object we do it over */
2116         if(scene->camera)
2117                 dag_object_time_update_flags(scene->camera);
2118         
2119         /* and store the info in groupobject */
2120         for(group= G.main->group.first; group; group= group->id.next) {
2121                 if(group->id.flag & LIB_DOIT) {
2122                         for(go= group->gobject.first; go; go= go->next) {
2123                                 go->recalc= go->ob->recalc;
2124                                 // printf("ob %s recalc %d\n", go->ob->id.name, go->recalc);
2125                         }
2126                         group->id.flag &= ~LIB_DOIT;
2127                 }
2128         }
2129         
2130 }
2131
2132
2133 /* flag this object and all its relations to recalc */
2134 /* if you need to do more objects, tag object yourself and
2135    use DAG_scene_flush_update() in end */
2136 void DAG_object_flush_update(Scene *sce, Object *ob, short flag)
2137 {
2138         
2139         if(ob==NULL || sce->theDag==NULL) return;
2140
2141         ob->recalc |= flag;
2142         BKE_ptcache_object_reset(sce, ob, PTCACHE_RESET_DEPSGRAPH);
2143         
2144         /* all users of this ob->data should be checked */
2145         /* BUT! displists for curves are still only on cu */
2146         if(flag & OB_RECALC_DATA) {
2147                 if(ob->type!=OB_CURVE && ob->type!=OB_SURF) {
2148                         ID *id= ob->data;
2149                         if(id && id->us>1) {
2150                                 /* except when there's a key and shapes are locked */
2151                                 if(ob_get_key(ob) && (ob->shapeflag & (OB_SHAPE_LOCK|OB_SHAPE_TEMPLOCK)));
2152                                 else {
2153                                         Object *obt;
2154                                         for (obt=G.main->object.first; obt; obt= obt->id.next) {
2155                                                 if (obt != ob && obt->data==ob->data) {
2156                                                         obt->recalc |= OB_RECALC_DATA;
2157                                                         BKE_ptcache_object_reset(sce, obt, PTCACHE_RESET_DEPSGRAPH);
2158                                                 }
2159                                         }
2160                                 }
2161                         }
2162                 }
2163         }
2164         
2165 // XXX  if(G.curscreen)
2166 //              DAG_scene_flush_update(sce, dag_screen_view3d_layers(), 0);
2167 //      else
2168                 DAG_scene_flush_update(sce, sce->lay, 0);
2169 }
2170
2171 /* recursively descends tree, each node only checked once */
2172 /* node is checked to be of type object */
2173 static int parent_check_node(DagNode *node, int curtime)
2174 {
2175         DagAdjList *itA;
2176         
2177         node->lasttime= curtime;
2178         
2179         if(node->color==DAG_GRAY)
2180                 return DAG_GRAY;
2181         
2182         for(itA = node->child; itA; itA= itA->next) {
2183                 if(itA->node->type==ID_OB) {
2184                         
2185                         if(itA->node->color==DAG_GRAY)
2186                                 return DAG_GRAY;
2187
2188                         /* descend if not done */
2189                         if(itA->node->lasttime!=curtime) {
2190                                 itA->node->color= parent_check_node(itA->node, curtime);
2191                         
2192                                 if(itA->node->color==DAG_GRAY)
2193                                         return DAG_GRAY;
2194                         }
2195                 }
2196         }
2197         
2198         return DAG_WHITE;
2199 }
2200
2201 /* all nodes that influence this object get tagged, for calculating the exact
2202    position of this object at a given timeframe */
2203 void DAG_object_update_flags(Scene *sce, Object *ob, unsigned int lay)
2204 {
2205         DagNode *node;
2206         DagAdjList *itA;
2207         
2208         /* tag nodes unchecked */
2209         for(node = sce->theDag->DagNode.first; node; node= node->next) 
2210                 node->color = DAG_WHITE;
2211         
2212         node= dag_find_node(sce->theDag, ob);
2213         
2214         /* object not in scene? then handle group exception. needs to be dagged once too */
2215         if(node==NULL) {
2216                 Group *group= NULL;
2217                 while( (group = find_group(ob, group)) ) {
2218                         GroupObject *go;
2219                         /* primitive; tag all... this call helps building groups for particles */
2220                         for(go= group->gobject.first; go; go= go->next)
2221                                 go->ob->recalc= OB_RECALC;
2222                 }
2223         }
2224         else {
2225                 
2226                 node->color = DAG_GRAY;
2227                 
2228                 sce->theDag->time++;
2229                 node= sce->theDag->DagNode.first;
2230                 for(itA = node->child; itA; itA= itA->next) {
2231                         if(itA->node->type==ID_OB && itA->node->lasttime!=sce->theDag->time)
2232                                 itA->node->color= parent_check_node(itA->node, sce->theDag->time);
2233                 }
2234                 
2235                 /* set recalcs and flushes */
2236                 DAG_scene_update_flags(sce, lay);
2237                 
2238                 /* now we clear recalcs, unless color is set */
2239                 for(node = sce->theDag->DagNode.first; node; node= node->next) {
2240                         if(node->type==ID_OB && node->color==DAG_WHITE) {
2241                                 Object *ob= node->ob;
2242                                 ob->recalc= 0;
2243                         }
2244                 }
2245         }
2246 }
2247
2248 /* ******************* DAG FOR ARMATURE POSE ***************** */
2249
2250 /* we assume its an armature with pose */
2251 void DAG_pose_sort(Object *ob)
2252 {
2253         bPose *pose= ob->pose;
2254         bPoseChannel *pchan;
2255         bConstraint *con;
2256         DagNode *node;
2257         DagNode *node2, *node3;
2258         DagNode *rootnode;
2259         DagForest *dag;
2260         DagNodeQueue *nqueue;
2261         DagAdjList *itA;
2262         ListBase tempbase;
2263         int skip = 0;
2264         
2265         dag = dag_init();
2266         ugly_hack_sorry= 0;     // no ID structs
2267
2268         rootnode = dag_add_node(dag, NULL);     // node->ob becomes NULL
2269         
2270         /* we add the hierarchy and the constraints */
2271         for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2272                 int addtoroot = 1;
2273                 
2274                 node = dag_get_node(dag, pchan);
2275                 
2276                 if(pchan->parent) {
2277                         node2 = dag_get_node(dag, pchan->parent);
2278                         dag_add_relation(dag, node2, node, 0, "Parent Relation");
2279                         addtoroot = 0;
2280                 }
2281                 for (con = pchan->constraints.first; con; con=con->next) {
2282                         bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
2283                         ListBase targets = {NULL, NULL};
2284                         bConstraintTarget *ct;
2285                         
2286 #if 0 // XXX old animation system... driver stuff to watch out for
2287                         if(con->ipo) {
2288                                 IpoCurve *icu;
2289                                 for(icu= con->ipo->curve.first; icu; icu= icu->next) {
2290                                         /* icu->driver->ob should actually point to ob->proxy if it
2291                                          * is a proxy, but since it wasn't set correct it older
2292                                          * files comparing with ob->proxy makes it work for those */
2293                                         if(icu->driver && (icu->driver->ob==ob || icu->driver->ob==ob->proxy)) {
2294                                                 bPoseChannel *target= get_pose_channel(ob->pose, icu->driver->name);
2295                                                 if(target) {
2296                                                         node2 = dag_get_node(dag, target);
2297                                                         dag_add_relation(dag, node2, node, 0, "Ipo Driver");
2298                                                         
2299                                                         /* uncommented this line, results in dependencies
2300                                                          * not being added properly for this constraint,
2301                                                          * what is the purpose of this? - brecht */
2302                                                         /*cti= NULL;*/  /* trick to get next loop skipped */
2303                                                 }
2304                                         }
2305                                 }
2306                         }
2307 #endif // XXX old animation system... driver stuff to watch out for
2308                         
2309                         if (cti && cti->get_constraint_targets) {
2310                                 cti->get_constraint_targets(con, &targets);
2311                                 
2312                                 for (ct= targets.first; ct; ct= ct->next) {
2313                                         if (ct->tar==ob && ct->subtarget[0]) {
2314                                                 bPoseChannel *target= get_pose_channel(ob->pose, ct->subtarget);
2315                                                 if (target) {
2316                                                         node2= dag_get_node(dag, target);
2317                                                         dag_add_relation(dag, node2, node, 0, "IK Constraint");
2318
2319                                                         if (con->type==CONSTRAINT_TYPE_KINEMATIC) {
2320                                                                 bKinematicConstraint *data = (bKinematicConstraint *)con->data;
2321                                                                 bPoseChannel *parchan;
2322                                                                 int segcount= 0;
2323                                                                 
2324                                                                 /* exclude tip from chain? */
2325                                                                 if(!(data->flag & CONSTRAINT_IK_TIP))
2326                                                                         parchan= pchan->parent;
2327                                                                 else
2328                                                                         parchan= pchan;
2329                                                                 
2330                                                                 /* Walk to the chain's root */
2331                                                                 while (parchan) {
2332                                                                         node3= dag_get_node(dag, parchan);
2333                                                                         dag_add_relation(dag, node2, node3, 0, "IK Constraint");
2334                                                                         
2335                                                                         segcount++;
2336                                                                         if (segcount==data->rootbone || segcount>255) break; // 255 is weak
2337                                                                         parchan= parchan->parent;
2338                                                                 }
2339                                                         }
2340                                                 }
2341                                         }
2342                                 }
2343                                 
2344                                 if (cti->flush_constraint_targets)
2345                                         cti->flush_constraint_targets(con, &targets, 1);
2346                         }
2347                 }
2348                 if (addtoroot == 1 ) {
2349                         dag_add_relation(dag, rootnode, node, 0, "Root Bone Relation");
2350                 }
2351         }
2352
2353         dag_check_cycle(dag);
2354         
2355         /* now we try to sort... */
2356         tempbase.first= tempbase.last= NULL;
2357
2358         nqueue = queue_create(DAGQUEUEALLOC);
2359         
2360         /* tag nodes unchecked */
2361         for(node = dag->DagNode.first; node; node= node->next) 
2362                 node->color = DAG_WHITE;
2363         
2364         node = dag->DagNode.first;
2365         
2366         node->color = DAG_GRAY;
2367         push_stack(nqueue, node);  
2368         
2369         while(nqueue->count) {
2370                 
2371                 skip = 0;
2372                 node = get_top_node_queue(nqueue);
2373                 
2374                 itA = node->child;
2375                 while(itA != NULL) {
2376                         if((itA->node->color == DAG_WHITE) ) {
2377                                 itA->node->color = DAG_GRAY;
2378                                 push_stack(nqueue,itA->node);
2379                                 skip = 1;
2380                                 break;
2381                         } 
2382                         itA = itA->next;
2383                 }                       
2384                 
2385                 if (!skip) {
2386                         if (node) {
2387                                 node = pop_queue(nqueue);
2388                                 if (node->ob == NULL)   // we are done
2389                                         break ;
2390                                 node->color = DAG_BLACK;
2391                                 
2392                                 /* put node in new list */
2393                                 BLI_remlink(&pose->chanbase, node->ob);
2394                                 BLI_addhead(&tempbase, node->ob);
2395                         }       
2396                 }
2397         }
2398         
2399         // temporal correction for circular dependancies
2400         while(pose->chanbase.first) {
2401                 pchan= pose->chanbase.first;
2402                 BLI_remlink(&pose->chanbase, pchan);
2403                 BLI_addhead(&tempbase, pchan);
2404
2405                 printf("cyclic %s\n", pchan->name);
2406         }
2407         
2408         pose->chanbase = tempbase;
2409         queue_delete(nqueue);
2410         
2411 //      printf("\nordered\n");
2412 //      for(pchan = pose->chanbase.first; pchan; pchan= pchan->next) {
2413 //              printf(" %s\n", pchan->name);
2414 //      }
2415         
2416         free_forest( dag );
2417         MEM_freeN( dag );
2418         
2419         ugly_hack_sorry= 1;
2420 }
2421
2422
2423