HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GP_FrameHierarchy.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: Domain pasting Library (C++)
7  *
8  * COMMENTS: Specialized frame collection. Based on a tree structure.
9  *
10  */
11 
12 #ifndef __GP_FrameHierarchy_h__
13 #define __GP_FrameHierarchy_h__
14 
15 #include "GP_API.h"
16 #include <iosfwd>
17 #include "GP_CorrectionFrame.h"
18 
19 class UT_IStream;
20 class GA_SaveMap;
21 class GA_LoadMap;
22 class GP_Domain;
23 class GP_DomainDAG;
24 class GP_NodeTree;
25 
27 {
28 public:
30  {
31  dirty = 0;
32  modified = 0;
33  }
34  unsigned dirty :1, // xform changed
35  modified :1; // visited by update method
36 };
37 
39 {
40  public:
41  // Class c-tors and d-tor.
42  GP_FrameHierarchy(void);
43  GP_FrameHierarchy(const GP_XformHandle &rootxform);
44  GP_FrameHierarchy(const GP_FrameHierarchy &h); // shallow!
45  virtual ~GP_FrameHierarchy(void);
46 
47  // Insert the root node, which will replace the existing root. Return 0 if
48  // successful and -1 otherwise.
49  int insertRoot(GP_CorrectionFrame &root);
50 
51  // Insert child into this hierarchy and return 0 if OK, -1 if failure.
52  // The child must not belong to another hierarchy at this point. Checking
53  // for duplicates is OFF by default.
54  int insert(GP_FrameHierarchy &child, int check = 0,
55  int wcorrection = 1);
56 
57  // Remove child, update the hierarchy the child's world xform. The child
58  // becomes stand-alone. The method returns the child if OK and 0 otherwise.
59  GP_FrameHierarchy *remove(GP_FrameHierarchy &child);
60 
61  // Same as removal, only the child's anchor is not updated. Useful for
62  // imminent re-attachment.
63  GP_FrameHierarchy *detach(GP_FrameHierarchy &child);
64 
65  // Remove child w/o updating the child's state -- just cut him off.
66  // The method returns the child if OK and 0 otherwise.
67  GP_FrameHierarchy *discard(GP_FrameHierarchy &child);
68 
69  // Isolate ourselves by removing ourselves from the tree of hierarchies
70  // and letting our parent and all of our children know. We also reset
71  // myDAG to zero.
72  void isolate(void);
73 
74  // Move child to the given hierarchy, making all the necessary updates.
75  // Return 0 if OK and -1 otherwise. The child should be ours initially.
77  {
78  return detach(child) ? to.insert(child) : -1;
79  }
80 
81  // Update this collection starting at the given frame or at the root
82  // if the frame is nil. We assume the frame is in the collection.
83  void update(const GP_Frame * = 0)
84  {
85  if (myAnchor->myFlags.dirty)
86  myAnchor->traverseUpdateWorld(0);
87  }
88 
89  // Hierarchy traversal method (depth-first):
90  void apply(void (f)(GP_FrameHierarchy*,void*), void *data);
91 
92  // I/O functions returning 0 if OK and -1 otherwise. They operate ONLY
93  // on the root.
94  int save(std::ostream &os, int binary = 0) const;
95  bool load(UT_IStream &is);
96 
97  bool save(UT_JSONWriter &w, const GA_SaveMap &map) const;
98  bool load(UT_JSONParser &p, const GA_LoadMap &map);
99 
100  // Copy just our root from the source's root:
101  void copyFrom(const GP_FrameHierarchy &src);
102 
103  // Copy the links of the frames stored in destnodes. Returns a pointer to
104  // the root frame in destnodes.
105  GP_FrameHierarchy *copyLinks(GP_NodeTree &destnodes) const;
106 
107  // Search the whole hierarchy (not just immediate children), or just the
108  // children:
109  int contains(const GP_FrameHierarchy &descendent) const;
110  int hasChild(const GP_FrameHierarchy &child ) const
111  {
112  int f = 0;
113  GP_FrameHierarchy *h = myChildren;
114 
115  while (h && !(f = (h==&child))) h = h->mySibling;
116  return f;
117  }
118 
119  // Find out if we have any children or a parent:
120  int hasParent () const { return myParent ? 1 : 0; }
121  int hasChildren() const { return myChildren ? 1 : 0; }
122 
123  // Access some of the internal hierarchies we know about:
124  GP_FrameHierarchy *anchor (void) const { return myAnchor; }
125  GP_FrameHierarchy *parent (void) const { return myParent; }
126  GP_FrameHierarchy *sibling (void) const { return mySibling; }
127  GP_FrameHierarchy *children(void) const { return myChildren; }
128 
129  // Const reference to the root.
130  const GP_CorrectionFrame &root() const { return myRoot; }
131 
132  // Query or set the "modified" flag. If true, it means that either us
133  // or our ancestors had changed at one point in time. It does not mean
134  // we're still dirty. Use the dirty() method for that.
135  int modified(void) const { return myFlags.modified; }
136  void modified(int m) { myFlags.modified = m; }
137 
138  // Query or set the domain information stored in this node:
139  GP_Domain *domain(void) const { return myDomain; }
140  void domain(GP_Domain *d) { myDomain = d; }
141 
142  // Query or set the domain DAG information stored in this node.
143  // This is to be used only by the anchor!
144  GP_DomainDAG *dag(void) const { return myDAG; }
145  void dag(GP_DomainDAG *d) { myDAG = d; }
146 
147  // Some frame's transformation has changed. Let us know.
148  int isDirty(void) const { return myFlags.dirty; }
149  void dirty(void)
150  {
151  myFlags.dirty = 1;
152  myAnchor->myFlags.dirty = 1; // very important!
153  }
154 
155 
156 protected:
157 private:
158  GP_CorrectionFrame myRoot; // root node. kids are hierarchies
159 
160  GP_FrameHierarchy *myAnchor; // root-most hierar. we're in. never 0.
161  GP_FrameHierarchy *myParent; // parent "node"
162  GP_FrameHierarchy *mySibling; // sibling "node"
163  GP_FrameHierarchy *myChildren; // hierarchy of children
164 
165  GP_Domain *myDomain; // domain associated with frame
166  GP_DomainDAG *myDAG; // this ptr is set only for the ANCHOR!
167 
168  GP_FrameHierarchyFlags myFlags;
169 
170 
171  // Special internal c-tor.
172  GP_FrameHierarchy(const GP_FrameHierarchy &hierarchy,
173  const GP_CorrectionFrame &root);
174 
175  // Traverse the hierarchy from us to all the descendents and update
176  // everyone's world transforms, then clear everyone's dirty flags.
177  void traverseUpdateWorld(int history = 0);
178 
179  // Link an immediate child to us. Really meant ot be used internally.
180  void attach(GP_FrameHierarchy &child)
181  {
182  child.myParent = this;
183  child.mySibling = myChildren;
184  myChildren = &child;
185  }
186 
187  // Set the anchor to us and all our descendents:
188  void anchor(GP_FrameHierarchy *a)
189  {
190  myAnchor = a;
191  GP_FrameHierarchy *h = myChildren;
192  while (h)
193  {
194  h->anchor(a);
195  h = h->mySibling;
196  }
197  }
198 
199  // Tell a frame if it's in this list or not:
200  void link (GP_Frame &frame) { frame.hierarchy(this); }
201  void unlink(GP_Frame &frame) { frame.hierarchy( 0 ); }
202 };
203 
204 #endif
#define GP_API
Definition: GP_API.h:10
Used to pass options and map offset values during saving.
Definition: GA_SaveMap.h:48
const GLuint GLenum const void * binary
Definition: glcorearb.h:1923
GP_FrameHierarchy * anchor(void) const
const GP_CorrectionFrame & root() const
GP_FrameHierarchy * children(void) const
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:72
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:32
GP_DomainDAG * dag(void) const
int hasChildren() const
GP_FrameHierarchy * sibling(void) const
int hasParent() const
GLfloat f
Definition: glcorearb.h:1925
void update(const GP_Frame *=0)
void domain(GP_Domain *d)
void dag(GP_DomainDAG *d)
int hasChild(const GP_FrameHierarchy &child) const
int move(GP_FrameHierarchy &child, GP_FrameHierarchy &to)
Options during loading.
Definition: GA_LoadMap.h:42
GLboolean * data
Definition: glcorearb.h:130
int isDirty(void) const
int modified(void) const
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2001
GP_FrameHierarchy * parent(void) const
int insert(GP_FrameHierarchy &child, int check=0, int wcorrection=1)
GP_FrameHierarchy * hierarchy(void) const
Definition: GP_Frame.h:79
GP_Domain * domain(void) const
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
GLenum src
Definition: glcorearb.h:1792