nodeRelation.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Filename: nodeRelation.h
  2. // Created by: drose (26Oct98)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. #ifndef NODERELATION_H
  6. #define NODERELATION_H
  7. #include <pandabase.h>
  8. #include "nodeTransitions.h"
  9. #include "nodeTransitionCache.h"
  10. #include "boundedObject.h"
  11. #include "pt_Node.h"
  12. #include <typedWriteableReferenceCount.h>
  13. #include <bamWriter.h>
  14. #include <bamReader.h>
  15. #include <pointerTo.h>
  16. #include <updateSeq.h>
  17. #include <factory.h>
  18. #include <set>
  19. #include <map>
  20. #include <stdlib.h>
  21. class Node;
  22. // This function keeps a monotonically incrementing sequence number
  23. // for each change made to the graph, for the purpose of invalidating
  24. // wrt cache values. A different sequence number is kept for each
  25. // type of graph, hence the parameter.
  26. extern EXPCL_PANDA UpdateSeq &last_graph_update(TypeHandle graph_type);
  27. EXPCL_PANDA INLINE_GRAPH void remove_arc(NodeRelation *arc);
  28. ///////////////////////////////////////////////////////////////////
  29. // Class : NodeRelation
  30. // Description : The base class for all scene graph arcs. This is the
  31. // glue between Nodes that defines the scene graph.
  32. // There are no arcs of type NodeRelation per se, but
  33. // there may be any number of types of arcs that inherit
  34. // from NodeRelation.
  35. //
  36. // All arcs are directed binary relations, from a parent
  37. // node to a child node, and may include any number of
  38. // NodeTransitions which affect the attributes of the
  39. // child node and later descendants.
  40. ////////////////////////////////////////////////////////////////////
  41. class EXPCL_PANDA NodeRelation : public TypedWriteableReferenceCount, public BoundedObject {
  42. public:
  43. NodeRelation(Node *from, Node *to, int sort, TypeHandle graph_type);
  44. protected:
  45. // Normally, this should only be used from derived classes for
  46. // passing to the factory. Don't attempt to create an unattached
  47. // arc directly.
  48. NodeRelation(TypeHandle graph_type);
  49. private:
  50. // It is an error to attempt to copy an arc.
  51. NodeRelation(const NodeRelation &copy);
  52. void operator = (const NodeRelation &copy);
  53. public:
  54. // The destructor needs to be virtual so that we can delete the
  55. // NodeRelations of various types when the Node they're attached to
  56. // destructs. However, a NodeRelation should not generally be
  57. // deleted directly, because it is reference-counted in the tree;
  58. // instead, you should call remove_arc().
  59. virtual ~NodeRelation();
  60. PUBLISHED:
  61. void output(ostream &out) const;
  62. INLINE_GRAPH void output_transitions(ostream &out) const;
  63. INLINE_GRAPH void write_transitions(ostream &out, int indent = 0) const;
  64. INLINE_GRAPH Node *get_parent() const;
  65. INLINE_GRAPH Node *get_child() const;
  66. INLINE_GRAPH int get_sort() const;
  67. INLINE_GRAPH TypeHandle get_graph_type() const;
  68. void ref_parent();
  69. void unref_parent();
  70. INLINE_GRAPH void change_parent(Node *parent);
  71. INLINE_GRAPH void change_parent(Node *parent, int sort);
  72. INLINE_GRAPH void change_child(Node *child);
  73. INLINE_GRAPH void change_parent_and_child(Node *parent, Node *child);
  74. INLINE_GRAPH void set_sort(int sort);
  75. INLINE_GRAPH void set_graph_type(TypeHandle graph_type);
  76. PT(NodeTransition) set_transition(TypeHandle handle, NodeTransition *trans); /*DONT INLINE_GRAPH THIS*/
  77. INLINE_GRAPH PT(NodeTransition) set_transition(NodeTransition *trans);
  78. INLINE_GRAPH PT(NodeTransition) set_transition(NodeTransition *trans, int priority);
  79. PT(NodeTransition) clear_transition(TypeHandle handle); /*DONT INLINE_GRAPH THIS*/
  80. INLINE_GRAPH bool has_transition(TypeHandle handle) const;
  81. INLINE_GRAPH bool has_any_transition() const;
  82. INLINE_GRAPH NodeTransition *get_transition(TypeHandle handle) const;
  83. void copy_transitions_from(const NodeRelation *arc);
  84. void compose_transitions_from(const NodeRelation *arc);
  85. void copy_transitions_from(const NodeTransitions &trans);
  86. void compose_transitions_from(const NodeTransitions &trans);
  87. void adjust_all_priorities(int adjustment);
  88. INLINE_GRAPH int compare_transitions_to(const NodeRelation *arc) const;
  89. INLINE_GRAPH UpdateSeq get_last_update() const;
  90. public:
  91. bool sub_render_trans(const AllAttributesWrapper &attrib,
  92. AllTransitionsWrapper &trans,
  93. RenderTraverser *trav);
  94. bool has_sub_render_trans() const;
  95. int get_num_sub_render_trans() const;
  96. public:
  97. // Factory stuff: to create a new NodeRelation based on its
  98. // TypeHandle.
  99. INLINE_GRAPH static NodeRelation *
  100. create_typed_arc(TypeHandle type, Node *parent, Node *child, int sort = 0);
  101. // This is just to be called at initialization time; don't try to
  102. // call this directly.
  103. INLINE_GRAPH static void register_with_factory();
  104. protected:
  105. INLINE_GRAPH static Factory<NodeRelation> &get_factory();
  106. private:
  107. static NodeRelation *make_arc(const FactoryParams &params);
  108. static Factory<NodeRelation> *_factory;
  109. protected:
  110. void attach();
  111. PT(NodeRelation) detach();
  112. PT(NodeRelation) detach_below();
  113. private:
  114. // We reference-count the child pointer, but not the parent pointer,
  115. // to avoid circular reference counting.
  116. Node *_parent;
  117. PT_Node _child;
  118. int _sort;
  119. TypeHandle _graph_type;
  120. bool _attached;
  121. int _parent_ref;
  122. private:
  123. // This is the set of transitions assigned to the arc. You should
  124. // never access this directly; use set_transition() and friends
  125. // instead.
  126. NodeTransitions _transitions;
  127. // This stores the known net transitions from the _top_subtree node,
  128. // which is either NULL to indicate the root of the graph, or a
  129. // pointer to the nearest descendent with multiple parents. You
  130. // should never even attempt to access it directly; it exists only
  131. // to support caching in wrt().
  132. PT(NodeTransitionCache) _net_transitions;
  133. Node *_top_subtree;
  134. // This is updated with the current update sequence whenever we
  135. // verify that *all* the transitions to this arc are accurately
  136. // reflected in the above set.
  137. UpdateSeq _all_verified;
  138. // This is updated with the current update sequence each time the
  139. // arc is changed (for instance, to change its state or to reparent
  140. // it or something). It exists to support caching in the cull
  141. // traversal.
  142. UpdateSeq _last_update;
  143. public:
  144. static void register_with_read_factory(void);
  145. virtual void write_datagram(BamWriter* manager, Datagram &me);
  146. virtual int complete_pointers(vector_typedWriteable &plist,
  147. BamReader *manager);
  148. static TypedWriteable *make_NodeRelation(const FactoryParams &params);
  149. protected:
  150. void fillin(DatagramIterator& scan, BamReader* manager);
  151. private:
  152. //This value is only used for the process of re-construction
  153. //from a binary source. DO NOT ACCESS. The value is only
  154. //guaranteed to be accurate during that process
  155. int _num_transitions;
  156. public:
  157. virtual void changed_transition(TypeHandle transition_type);
  158. protected:
  159. virtual void propagate_stale_bound();
  160. virtual void recompute_bound();
  161. PUBLISHED:
  162. INLINE_GRAPH static TypeHandle get_class_type();
  163. INLINE_GRAPH static TypeHandle get_stashed_type();
  164. public:
  165. static void init_type();
  166. virtual TypeHandle get_type() const;
  167. virtual TypeHandle force_init_type();
  168. private:
  169. static TypeHandle _type_handle;
  170. static TypeHandle _stashed_type_handle;
  171. // Should not include storage class specifiers in friend declaration.
  172. friend EXPCL_PANDA void remove_arc(NodeRelation *arc);
  173. friend class Node;
  174. friend class NodeTransitionWrapper;
  175. friend class AllTransitionsWrapper;
  176. friend class GraphPriorityAdjuster;
  177. };
  178. EXPCL_PANDA INLINE_GRAPH ostream &
  179. operator << (ostream &out, const NodeRelation &arc);
  180. typedef vector< PT(NodeRelation) > DownRelationPointers;
  181. typedef vector<NodeRelation *> UpRelationPointers;
  182. #include "nodeRelation.T"
  183. #ifndef DONT_INLINE_GRAPH
  184. #include "nodeRelation.I"
  185. #endif
  186. // We include node.h here at the end, so we'll know that nodes are of
  187. // type ReferenceCount and will be able to compile anything that uses
  188. // a NodeRelation. We have to include it here at the end instead of
  189. // the beginning because node.h also includes nodeRelation.h.
  190. // This comment tells ppremake that we know this is a circular
  191. // #include reference, and please don't bother us about it. The line
  192. // must be exactly as shown.
  193. /* okcircular */
  194. #include "node.h"
  195. #endif