HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GP_Xform.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: General domain transformation class.
9  * GP_Xform and GP_XformHandle
10  *
11  */
12 
13 #ifndef __GP_Xform_h__
14 #define __GP_Xform_h__
15 
16 #include "GP_API.h"
17 #include <UT/UT_Vector2.h>
18 #include <UT/UT_RefMatrix.h>
19 #include <UT/UT_BoundingRect.h>
20 #include <UT/UT_JSONParser.h>
21 #include <iosfwd>
22 
25 
26 
27 #define GP_LINEAR_XFORM 0x00001
28 #define GP_BILINEAR_XFORM 0x00002
29 
30 #define GP_LINEAR_XFORM_N "Linear"
31 #define GP_BILINEAR_XFORM_N "Bilinear"
32 
33 #define GP_LINEAR_XFORM_JSON_TOKEN "linear"
34 #define GP_BILINEAR_XFORM_JSON_TOKEN "bilinear"
35 
36 
38 {
39 public:
40  // Class c-tor and d-tor.
41  GP_Xform(void) { myRefCount = 0; }
42  virtual ~GP_Xform(void);
43 
44  // Apply this transformation to the data forward or backward:
45  // Return 0 if OK and -1 if error (eg. can't invert).
46  virtual int forward (GP_Point &p) const = 0;
47  virtual int forward (GP_PointMatrix &m) const = 0;
48  virtual int backward(GP_Point &p) const = 0;
49  virtual int backward(GP_PointMatrix &m) const = 0;
50 
51  // Set the transformation to identity:
52  virtual void reset(void) = 0;
53 
54  // Add a translation by delta to this transform:
55  virtual void translate(float dx, float dy) = 0;
56 
57  // Compose us with the given transformation. Return 0 if we cannot compress
58  // the composition. Otherwise return a pointer to the transformation, which
59  // will be us or a new xform of a different type.
60  virtual GP_Xform *compose(const GP_Xform &xform,
61  const UT_BoundingRect *brect = 0) = 0;
62  virtual GP_Xform *composeInverse(const GP_Xform &xform,
63  const UT_BoundingRect *brect = 0) = 0;
64 
65  // Copy your data from the other guy if both xforms have the same type.
66  // Return 0 if same type, else -1.
67  virtual int copyFrom(const GP_Xform &xform) = 0;
68 
69  // Return an inverse transformation. The first method allocates
70  // space for the object and return 0 if it fails somewhere. The second
71  // method returns the inverse in the reference provided as long as it's
72  // the appropriate type, and returns 0. If the xform is not the right type,
73  // or the inverse can't be computed, it returns -1.
74  virtual GP_Xform *inverse(void) = 0;
75  virtual int inverse(GP_Xform &) = 0;
76 
77  // Each derived class has its own name and id:
78  virtual const char *getName(void) const = 0;
79  virtual unsigned int getType(void) const = 0;
80  virtual const char *getJSONToken(void) const = 0;
81 
82  // I/O functions returning 0 if OK and -1 otherwise.
83  virtual int save(std::ostream &os, int binary = 0) const = 0;
84  virtual bool load(UT_IStream &is) = 0;
85 
86  virtual bool save(UT_JSONWriter &w) const = 0;
87  virtual bool load(UT_JSONParser &p, UT_JSONParser::traverser &it)=0;
88 
89  // Bump the reference count up or down:
90  int addReference(void) { return ++myRefCount; }
91  int delReference(void) { return --myRefCount; }
92 
93  // A new entity of the exact type and contents as ourselves:
94  GP_Xform *copy(void) const { return newSpecies(*this); }
95 
96  // Generate a new xform of the species specified in the name or type.
97  // The method that takes another xform also copies its contents.
98  static GP_Xform *newSpecies(const char *name);
99  static GP_Xform *newSpecies(unsigned int type);
100  static GP_Xform *newSpecies(const GP_Xform &xform);
101  static GP_Xform *newSpeciesJSON(const char *name);
102 
103  // Read an xform from the input stream:
104  static GP_Xform *read(UT_IStream &is);
105  static GP_Xform *read(UT_JSONParser &p);
106 
107  // Save an identity transformation:
108  static int saveIdentity(std::ostream &os, int binary = 0);
109  static bool saveIdentity(UT_JSONWriter &w);
110 
111 
112 protected:
113  // Save the header part (name or type):
114  int saveHeader(std::ostream &os, int binary = 0) const;
115  // Save the JSON type field
116  bool saveType(UT_JSONWriter &w) const;
117 
118 private:
119  int myRefCount; // reference counter for handle
120 };
121 
122 
123 
125 {
126 public:
127  // C-tors and d-tor.
128  GP_XformHandle(GP_Xform *xform = 0) : myXform(xform)
129  {
130  if (myXform)
131  myXform->addReference();
132  }
133  GP_XformHandle(const GP_XformHandle &handle) : myXform(handle.myXform)
134  {
135  if (myXform)
136  myXform->addReference();
137  }
139  {
140  if (myXform && !myXform->delReference())
141  delete myXform;
142  }
143 
144  // Assign the contents of another handle to us:
146  {
147  if (this != &h)
148  {
149  if (myXform && !myXform->delReference())
150  delete myXform;
151  myXform = h.myXform;
152  if (myXform)
153  myXform->addReference();
154  }
155  return *this;
156  }
157 
158  // Assign a free floating xform to us. This does a deep copy of xform
159  // as long as xform is not identical to myXform, so neither is xform
160  // affected, nor do we rely on it to stay alive:
162  {
163  if (myXform != &xform)
164  {
165  // First try to copy if same type:
166  if (!myXform || myXform->copyFrom(xform))
167  {
168  if (myXform && !myXform->delReference())
169  delete myXform;
170 
171  myXform = xform.copy();
172  myXform->addReference();
173  }
174  else // myXform exists and copyFrom failed
175  {
176  if (!myXform->delReference())
177  delete myXform;
178  myXform = xform.copy();
179  myXform->addReference();
180  }
181  }
182  return *this;
183  }
184 
185  // I/O functions returning 0 if OK and -1 otherwise.
186  int save(std::ostream &os, int binary = 0) const
187  {
188  return myXform ? myXform->save(os,binary)
190  }
191 
192  bool load(UT_IStream &is)
193  {
194  if (myXform && !myXform->delReference())
195  delete myXform;
196  myXform = GP_Xform::read(is);
197  if (myXform)
198  myXform->addReference();
199  return myXform != NULL;
200  }
201 
202  bool save(UT_JSONWriter &w) const
203  {
204  return myXform ? myXform->save(w)
206  }
207 
209  {
210  if (myXform && !myXform->delReference())
211  delete myXform;
212  myXform = GP_Xform::read(p);
213  if (myXform)
214  myXform->addReference();
215  return myXform != NULL;
216  }
217 
218  // Compose my xform with another handle's. We don't do this directly
219  // through the GP_Xform because the pointers might change.
220  void compose(const GP_XformHandle &handle,
221  const UT_BoundingRect *brect = 0)
222  {
223  if (handle.myXform)
224  {
225  if (myXform)
226  {
227  GP_Xform *newxf;
228  newxf = myXform->compose(*handle.myXform,
229  brect);
230 
231  if (newxf != myXform)
232  {
233  if (!myXform->delReference())
234  delete myXform;
235  myXform = newxf;
236  myXform->addReference();
237  }
238  }
239  else // we don't have an xform
240  {
241  myXform = handle.myXform->copy();
242  myXform->addReference();
243  }
244  }
245  } // end compose()
246 
247  void composeInverse(const GP_XformHandle &handle,
248  const UT_BoundingRect *brect = 0)
249  {
250  if (handle.myXform)
251  {
252  if (myXform)
253  {
254  GP_Xform *newxf;
255  newxf = myXform->composeInverse(
256  *handle.myXform, brect);
257 
258  if (newxf != myXform)
259  {
260  if (!myXform->delReference())
261  delete myXform;
262  myXform = newxf;
263  myXform->addReference();
264  }
265  }
266  else // we don't have an xform
267  {
268  myXform = handle.myXform->copy();
269  myXform->addReference();
270  }
271  }
272  } // end compose()
273 
274  // Grab the contents, i.e. the transformation:
275  GP_Xform *operator->(void) const { return myXform; }
276  GP_Xform *operator* (void) const { return myXform; }
277 
278  // Check if this handle contains a transformation:
279  int isDefined(void) const { return myXform != 0; }
280 
281 
282 private:
283  GP_Xform *myXform;
284 };
285 
286 #endif
Color4< T > operator*(S a, const Color4< T > &v)
Definition: ImathColor.h:728
#define GP_API
Definition: GP_API.h:10
GP_XformHandle & operator=(const GP_XformHandle &h)
Definition: GP_Xform.h:145
const GLuint GLenum const void * binary
Definition: glcorearb.h:1923
GP_Xform * operator->(void) const
Definition: GP_Xform.h:275
GA_API const char * getJSONToken(JDTupleToken tokenID)
void composeInverse(const GP_XformHandle &handle, const UT_BoundingRect *brect=0)
Definition: GP_Xform.h:247
void compose(const GP_XformHandle &handle, const UT_BoundingRect *brect=0)
Definition: GP_Xform.h:220
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:75
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:32
static GP_Xform * read(UT_IStream &is)
void read(T &in, bool &v)
Definition: ImfXdr.h:611
int addReference(void)
Definition: GP_Xform.h:90
bool save(UT_JSONWriter &w) const
Definition: GP_Xform.h:202
static int saveIdentity(std::ostream &os, int binary=0)
Traverse an array object in the parser.
int isDefined(void) const
Definition: GP_Xform.h:279
GLuint const GLchar * name
Definition: glcorearb.h:785
GP_XformHandle & operator=(const GP_Xform &xform)
Definition: GP_Xform.h:161
bool load(UT_JSONParser &p)
Definition: GP_Xform.h:208
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2001
~GP_XformHandle(void)
Definition: GP_Xform.h:138
UT_RefMatrix< GP_Point > GP_PointMatrix
Definition: GP_Xform.h:24
virtual GP_Xform * compose(const GP_Xform &xform, const UT_BoundingRect *brect=0)=0
GP_XformHandle(const GP_XformHandle &handle)
Definition: GP_Xform.h:133
UT_Vector2 GP_Point
Definition: GP_Xform.h:23
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
int delReference(void)
Definition: GP_Xform.h:91
bool load(UT_IStream &is)
Definition: GP_Xform.h:192
GP_XformHandle(GP_Xform *xform=0)
Definition: GP_Xform.h:128
GP_Xform * copy(void) const
Definition: GP_Xform.h:94
GP_Xform(void)
Definition: GP_Xform.h:41
virtual GP_Xform * composeInverse(const GP_Xform &xform, const UT_BoundingRect *brect=0)=0
int save(std::ostream &os, int binary=0) const
Definition: GP_Xform.h:186