HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CL_Clip.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: CL_Clip.h ( Clip Library, C++)
7  *
8  * COMMENTS:
9  * A collection of raw data tracks
10  */
11 
12 #ifndef __CL_Clip__
13 #define __CL_Clip__
14 
15 #include "CL_API.h"
16 #include "CL_Track.h"
17 
18 #include <UT/UT_IntArray.h>
19 #include <UT/UT_NameManager.h>
20 #include <UT/UT_SharedPtr.h>
21 #include <UT/UT_String.h>
22 #include <UT/UT_StringSet.h>
23 #include <UT/UT_StringHolder.h>
24 #include <UT/UT_ValArray.h>
25 #include <UT/UT_XformOrder.h>
26 
27 #include <SYS/SYS_Math.h>
28 #include <SYS/SYS_Types.h>
29 
30 #undef Bool
31 
32 class UT_IStream;
33 class UT_OStream;
34 class UT_InfoTree;
35 class UT_WorkBuffer;
36 class CL_ClipPrivateIO;
37 class UT_VariantMap;
38 
39 typedef void (*CL_InfoCallback)(UT_String &result, void *data, int idx);
41 
42 class CL_API CL_Clip {
43 
44 public:
45 
46  CL_Clip(int size);
47  CL_Clip(const CL_Clip &from);
48  ~CL_Clip();
49 
50  CL_Clip &operator= (const CL_Clip &v);
51 
52  // a very efficient copy constructor which checks existing
53  // data to minimize memory calls
54 
55  void copyContents(const CL_Clip &v, int data, int slerps);
56 
57 
58  // these methods when used with caution can increase efficiency
59  // when adding tracks
60 
62  { return myCheckDuplicateNames; }
63 
65  { myCheckDuplicateNames = val; }
66 
67  //
68  // access/query methods
69  //
70 
71  void clearAndDestroy();
72 
74  { myStart = start; }
75 
76  fpreal getStart() const
77  { return myStart; }
78 
79  fpreal getEnd() const
80  { return myStart + myTrackLength - 1; }
81 
82  int getTrackLength() const
83  { return myTrackLength; }
84 
85  void setTrackLength(int length, int clear_extra=0);
86 
87  void setSampleRate(fpreal rate)
88  { mySampleRate = SYSmax(rate, 1.0e-4); }
89 
91  { return mySampleRate; }
92 
93  int getTrackNames(UT_ValArray<const char *> &list) const;
94  int getTracks(CL_TrackListC &list,
95  const char *pattern = 0) const;
96  int getTracks(UT_IntArray &list, const char *pat) const;
97 
98 
99  int isTimeDependent() const;
100 
101  //
102  // quaternion (slerp) methods
103  // each track can belong to at most 1 slerp triplet
104  //
105 
106  int numSlerps() const;
107 
108  int copySlerpAttributes(const CL_Clip &v,
109  int by_name = 0);
110 
111  int resetSlerps();
112 
113  int findSlerp(const CL_Track *track, int &idx) const;
114 
115  int getSlerp(int slerp_idx,
116  int &x_idx,
117  int &y_idx,
118  int &z_idx) const;
119 
120  int getSlerp(const CL_Track *track,
121  int &x_idx,
122  int &y_idx,
123  int &z_idx) const;
124 
125  void setSlerpCapacity(int capacity)
126  {
127  mySXTracks.setCapacity(capacity);
128  mySYTracks.setCapacity(capacity);
129  mySZTracks.setCapacity(capacity);
130  }
131 
132  int setSlerp(int x_idx, int y_idx, int z_idx);
133 
134  int slerpBlend(int slerp_idx,
135  const fpreal *xvalues,
136  const fpreal *yvalues,
137  const fpreal *zvalues,
138  fpreal start, int len, fpreal base,
139  const CL_Track *effect_track,
140  const UT_XformOrder &order);
141 
142  int slerpBlend(int slerp_idx,
143  const fpreal *xvalues,
144  const fpreal *yvalues,
145  const fpreal *zvalues,
146  fpreal start, int len, fpreal base,
147  const CL_SubRange *effect,
148  const UT_XformOrder &order);
149 
150  int validateSlerps();
151 
153  { mySRotOrder = o; }
154 
156  { return mySRotOrder; }
157 
158  //
159  // track access/creation
160  // returns index if name was added, else already exists and returns -1
161  //
162 
163  void removeTrack(int idx);
164 
165  void setTrackCapacity(int capacity)
166  { myTracks.setCapacity(capacity); }
167 
168  CL_Track *addTrack(const UT_StringHolder &name);
169  CL_Track *dupTrackInfo(const CL_Track *,int data_too = 0,
170  const UT_StringHolder &name
172 
173  int getNumTracks() const
174  { return myTracks.entries(); }
175 
177  { return (index >= 0 && index < myTracks.entries()) ?
178  myTracks(index) : 0; }
179 
180  const CL_Track *getTrack(int index) const
181  { return (index >= 0 && index < myTracks.entries()) ?
182  myTracks(index) : 0; }
183 
184  CL_Track *getTrack(const UT_StringRef &name, int *index = 0);
185  const CL_Track *getTrack(const UT_StringRef &name, int *index = 0) const;
186 
187 
188  //
189  // track evaluation
190  //
191 
192 
193  // These three evaluat methods should only be used when appropriate,
194  // it is much more efficient to use the range evaluations
195 
197  { return track->evaluateSingle(index - myStart); }
198 
200  { return evaluateSingle(track, getIndex(t)); }
201 
203  fpreal f, fpreal fps) const
204  { return evaluateSingle(track, getIndexF(f, fps)); }
205 
206 
207 
208  void evaluate(const CL_Track *track,
209  fpreal start_index, fpreal stop_index,
210  fpreal *data, int size) const;
211 
212  int evaluateSlerp(int slerp_idx,
213  fpreal start_index, fpreal stop_index,
214  fpreal *xdata, fpreal *ydata, fpreal *zdata,
215  int size) const;
216 
217  void evaluateTime(const CL_Track *track,
218  fpreal start_time, fpreal stop_time,
219  fpreal *data, int size) const
220  { evaluate(track, getIndex(start_time),
221  getIndex(stop_time), data, size); }
222 
223  void resampleTime(const CL_Track *track,
224  fpreal start_time, fpreal stop_time,
225  fpreal *data, int size) const;
226 
227  // Find the max/min height of the track.
228  // If track is nil, find the min max of every track
229 
230  void getHeight(const CL_Track *track,
231  fpreal start_index, fpreal stop_index,
232  int nsamples, fpreal *imin, fpreal *imax) const;
233 
234  // Find the min max of the graph over *each* step interval
235  // data should be twice as long as size
236 
237  void evaluateHeightRange(const CL_Track *track,
238  fpreal start_index, fpreal stop_index,
239  fpreal *data, int size) const;
240 
241 
242  // This method extends the clip so that it contains a local index
243  // at time t, and returns the index. The clip may be extended
244  // or cycled to accomodate the change. If delta is given, it
245  // returns the index delta of the clips start times.
246  // Note all indices are truncated to ints
247 
248  int extendTime(fpreal t, int *delta);
249 
250 
251  //
252  // convenience methods
253  //
254 
256  { return getTime(myStart); }
257 
259  { return getTime(myStart + myTrackLength - 1); }
260 
261  void dupAttributes(const CL_Clip &);
262 
263  //
264  // unit conversion methods
265  //
266 
268  { return time*mySampleRate; }
269 
271  { return index / mySampleRate; }
272 
274  { return index*fps/mySampleRate + 1;}
275 
276  fpreal getIndexF(fpreal frame, fpreal fps) const
277  { return getTimeF(frame, fps)*mySampleRate; }
278 
279  static fpreal getTimeF(fpreal frame, fpreal fps)
280  { return (frame - 1) / fps; }
281 
283  { return time*fps + 1; }
284 
285 
286  //
287  // Data operations
288  //
289 
290  void trim(const CL_Clip *clip, fpreal leftpos,
291  fpreal rightpos, int discard);
292 
293  // shorten the tracklength to newlen, if add is 1 then the
294  // remaining samples are added to the beginning
295  // Returns 1 if newlen is smaller than the beginning length
296 
297  int crop(int newlen, int add);
298 
299 
300  //
301  // I/O methods
302  // Note: 0 is success, -ve is fail
303  //
304 
305  int save(UT_OStream &os,
306  const CL_ClipPrivateIO *priv = 0,
307  bool use_blosc_compresssion = false) const;
308  int save(UT_OStream &os, UT_StringArray& parms,
309  const CL_ClipPrivateIO *priv = 0,
310  bool use_blosc_compresssion = false) const;
311  int save(const char *nm) const;
312 
313  // Returns true on success and false otherwise.
314  bool load(UT_IStream &is, const char *path=0);
315 
316  int load(const char *nm);
317 
318  int64 getMemoryUsage(bool inclusive) const;
319  int info(UT_OStream &os, fpreal fps, fpreal t,
320  bool limit, CL_InfoCallback cb,
321  void *cbdata) const;
322  int info(UT_InfoTree &tree, fpreal fps, fpreal t,
323  CL_InfoCallback cb, void *cbdata) const;
324 
325  static fpreal fixedDecimal(fpreal num, int digits = 2);
326  static void printNum(UT_OStream &os,fpreal num);
327 
328  //
329  // This method will reorder our tracks given a new list
330  // Should be used with caution.
331 
332  void reorder(CL_TrackList &list);
333 
334  // Given 3 track indices compute the sets of rotations that will
335  // eliminate any discontinuites introduced by static cracking.
336  // Negative indices are considered constant 0 tracks.
337  // Optionally supply a desired beginning rotation
338 
339  void crackSmooth(int xidx, int yidx, int zidz,
340  const UT_XformOrder &order,
341  int use_hint,
342  fpreal hx, fpreal hy, fpreal hz);
343 
344  // This method will return true if the clips lie over the same
345  // range (ie, sample-to-sample correspondence
346 
347  int isSameRange(const CL_Clip &v) const;
348 
349  // These methods can be used to increase performance by reducing
350  // memory management
351 
352  void stashAll();
353  void destroyStashed();
354 
355  // Generate a new unique name based on an existing name.
356  // oldname is the string to test and the method assumes the name is already being used.
357  // This means the method will always generate a new name even if oldname was valid unique name already.
358  // newname workbuffer is used to generate and return a new name.
359  void newName(const UT_StringRef &oldname, UT_WorkBuffer& newname) const;
360 
361  void setDefaults();
362 
363  // Called by CL_Track to update the name hash table when loaded.
364  void trackRenamed(CL_Track *track,
365  const UT_StringRef &oldname);
366 
367 
368  // Attributes
369  int setAttribute( const UT_StringHolder &name, const UT_StringHolder &v, int track=-1, int sample=-1 );
370  int setAttribute( const UT_StringHolder &name, const fpreal32 &v, int track=-1, int sample=-1 );
371  int setAttribute( const UT_StringHolder &name, const fpreal64 &v, int track=-1, int sample=-1 );
372  int setAttribute( const UT_StringHolder &name, const int &v, int track=-1, int sample=-1 );
373  int setAttribute( const UT_StringHolder &name, const int64 &v, int track=-1, int sample=-1 );
374  int setAttribute( const UT_StringHolder &name, const UT_Vector2 &v, int track=-1, int sample=-1 );
375  int setAttribute( const UT_StringHolder &name, const UT_Vector2D &v, int track=-1, int sample=-1 );
376  int setAttribute( const UT_StringHolder &name, const UT_Vector3 &v, int track=-1, int sample=-1 );
377  int setAttribute( const UT_StringHolder &name, const UT_Vector3D &v, int track=-1, int sample=-1 );
378  int setAttribute( const UT_StringHolder &name, const UT_Vector4 &v, int track=-1, int sample=-1 );
379  int setAttribute( const UT_StringHolder &name, const UT_Vector4D &v, int track=-1, int sample=-1 );
380  int setAttribute( const UT_StringHolder &name, const UT_Matrix2 &v, int track=-1, int sample=-1 );
381  int setAttribute( const UT_StringHolder &name, const UT_Matrix2D &v, int track=-1, int sample=-1 );
382  int setAttribute( const UT_StringHolder &name, const UT_Matrix3 &v, int track=-1, int sample=-1 );
383  int setAttribute( const UT_StringHolder &name, const UT_Matrix3D &v, int track=-1, int sample=-1 );
384  int setAttribute( const UT_StringHolder &name, const UT_Matrix4 &v, int track=-1, int sample=-1 );
385  int setAttribute( const UT_StringHolder &name, const UT_Matrix4D &v, int track=-1, int sample=-1 );
386 
387  int getAttribute( const UT_StringHolder &name, UT_StringHolder &v, int track=-1, int sample=-1 ) const;
388  int getAttribute( const UT_StringHolder &name, fpreal32 &v, int track=-1, int sample=-1 ) const;
389  int getAttribute( const UT_StringHolder &name, fpreal64 &v, int track=-1, int sample=-1 ) const;
390  int getAttribute( const UT_StringHolder &name, int &v, int track=-1, int sample=-1 ) const;
391  int getAttribute( const UT_StringHolder &name, int64 &v, int track=-1, int sample=-1 ) const;
392  int getAttribute( const UT_StringHolder &name, UT_Vector2 &v, int track=-1, int sample=-1 ) const;
393  int getAttribute( const UT_StringHolder &name, UT_Vector2D &v, int track=-1, int sample=-1 ) const;
394  int getAttribute( const UT_StringHolder &name, UT_Vector3 &v, int track=-1, int sample=-1 ) const;
395  int getAttribute( const UT_StringHolder &name, UT_Vector3D &v, int track=-1, int sample=-1 ) const;
396  int getAttribute( const UT_StringHolder &name, UT_Vector4 &v, int track=-1, int sample=-1 ) const;
397  int getAttribute( const UT_StringHolder &name, UT_Vector4D &v, int track=-1, int sample=-1 ) const;
398  int getAttribute( const UT_StringHolder &name, UT_Matrix2 &v, int track=-1, int sample=-1 ) const;
399  int getAttribute( const UT_StringHolder &name, UT_Matrix2D &v, int track=-1, int sample=-1 ) const;
400  int getAttribute( const UT_StringHolder &name, UT_Matrix3 &v, int track=-1, int sample=-1 ) const;
401  int getAttribute( const UT_StringHolder &name, UT_Matrix3D &v, int track=-1, int sample=-1 ) const;
402  int getAttribute( const UT_StringHolder &name, UT_Matrix4 &v, int track=-1, int sample=-1 ) const;
403  int getAttribute( const UT_StringHolder &name, UT_Matrix4D &v, int track=-1, int sample=-1 ) const;
404 
405  const CL_ClipAttributes *getAttributes() const { return myAttributes; }
406  void setAttributes( const CL_ClipAttributes *attrs );
407  void updateAttributes( const UT_StringSet &keys, const CL_ClipAttributes *attrs );
408 
409  // Removes a single attribute instance in the attribute map.
410  int removeAttribute(const UT_StringHolder &name, int track=-1, int sample=-1);
411  static int removeAttribute(CL_ClipAttributes *&attributes, const UT_StringHolder &name, int track=-1, int sample=-1);
412 
413  // Removes a list of attributes.
414  // The string should contain space separated pairs of "attrclass:attrname"
415  // where attrclass is one of "auto clip channel channelsample sample"
416  int removeAttributes(const UT_StringHolder &names);
417  static int removeAttributes(CL_ClipAttributes *&attributes, const UT_StringHolder &names);
418 
419  // Lists the attribute names from attrclass
420  int getAttributeNames( const UT_StringHolder &attrclass, UT_StringArray &names ) const;
421 
422  // Lists the attribute names from attrclass
423  static int getAttributeNames(const CL_ClipAttributes *attributes, const UT_StringHolder &attrclass, UT_StringArray &names );
424  static inline bool
425  buildKey( const UT_StringHolder &name, int track, int sample,
426  UT_WorkBuffer &key );
427 
428  bool isUsingCurrentFrame() const
429  { return myIsUsingCurrentFrame; }
431  { myIsUsingCurrentFrame = b; myIsConstantSet = false; }
432 
433  bool isConstant() const
434  {
435  if (myIsUsingCurrentFrame)
436  return false;
437  if (!myIsConstantSet)
438  const_cast<CL_Clip*>(this)->computeIsConstant();
439  return myIsConstant;
440  }
442  { myIsConstantSet = false; }
443 
444 private:
445  CL_Clip(); //prevent default ctor
446 
447  // Attributes
448  template< class T >
449  int setAttributeT( const UT_StringHolder &name, const T &v, int track=-1, int sample=-1 );
450 
451  template< class T >
452  int getAttributeT( const UT_StringHolder &name, T &v, int track=-1, int sample=-1 ) const;
453 
454  void clearTrackNames(bool set_to_null=true);
455 
456  void computeIsConstant();
457 
458 private:
459 
460  fpreal mySampleRate;
461  fpreal myStart;
462 
463  int myTrackLength;
464  CL_TrackList myTracks;
465  CL_TrackList myStashedTracks;
466 
467  UT_IntArray mySXTracks; // Quaternion tracks
468  UT_IntArray mySYTracks;
469  UT_IntArray mySZTracks;
470 
471  UT_XformOrder::xyzOrder mySRotOrder;
472  CL_ClipAttributes *myAttributes;
473 
474  UT_NameManager<CL_Track*> myTrackNames;
475 
476  int myStashedTracksNext;
477 
478  bool myCheckDuplicateNames;
479  bool myIsConstantSet;
480 
481  unsigned myDisableRemovingTrackNames:1,
482  myIsUsingCurrentFrame:1,
483  myIsConstant:1;
484 
485  class ClearTrackNamesTask;
486  friend class ClearTrackNamesTask;
487 
488 };
489 
493 
494 ///////////////////////////////////////////////////////////////////////////////
495 //
496 // Inline implementation
497 //
498 
499 inline CL_Track *
501 {
502  CL_Track *track = myTrackNames.findSymbol(name);
503  if (index)
504  *index = track ? track->getIndex() : -1;
505  return track;
506 }
507 
508 inline const CL_Track *
510 {
511  const CL_Track *track = myTrackNames.findSymbol(name);
512  if (index)
513  *index = track ? track->getIndex() : -1;
514  return track;
515 }
516 
517 inline bool
518 CL_Clip::buildKey(const UT_StringHolder &name, int track, int sample,
519  UT_WorkBuffer &key)
520 {
521  if (!name.isstring())
522  return false;
523 
524  if (track<0 && sample<0)
525  key.sprintf("%s -1 -1", name.buffer());
526  else if(track <0 && sample>=0)
527  key.sprintf("%s -1 %d", name.buffer(), sample);
528  else if(track >=0 && sample<0)
529  key.sprintf("%s %d -1", name.buffer(), track);
530  else
531  key.sprintf("%s %d %d", name.buffer(), track, sample );
532  return true;
533 }
534 
535 #endif // __CL_Clip__
#define SYSmax(a, b)
Definition: SYS_Math.h:1538
fpreal evaluateSingleFrame(const CL_Track *track, fpreal f, fpreal fps) const
Definition: CL_Clip.h:202
fpreal getStart() const
Definition: CL_Clip.h:76
static fpreal getTimeF(fpreal frame, fpreal fps)
Definition: CL_Clip.h:279
void setTrackCapacity(int capacity)
Definition: CL_Clip.h:165
void
Definition: png.h:1083
GLboolean * data
Definition: glcorearb.h:131
GT_API const UT_StringHolder time
const GLdouble * v
Definition: glcorearb.h:837
UT_XformOrder::xyzOrder getSlerpOrder() const
Definition: CL_Clip.h:155
Transformation order of scales, rotates, and translates.
Definition: UT_XformOrder.h:23
GLuint start
Definition: glcorearb.h:475
void(* CL_InfoCallback)(UT_String &result, void *data, int idx)
Definition: CL_Clip.h:39
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
const CL_ClipAttributes * getAttributes() const
Definition: CL_Clip.h:405
CL_Track * getTrack(int index)
Definition: CL_Clip.h:176
fpreal getSampleRate() const
Definition: CL_Clip.h:90
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
void unsetIsConstant()
Definition: CL_Clip.h:441
UT_SharedPtr< CL_Clip > CL_ClipPtr
Definition: CL_Clip.h:492
**But if you need a result
Definition: thread.h:613
bool isUsingCurrentFrame() const
Definition: CL_Clip.h:428
float fpreal32
Definition: SYS_Types.h:200
int getIndex() const
void setIsUsingCurrentFrame(bool b)
Definition: CL_Clip.h:430
fpreal getStartTime() const
Definition: CL_Clip.h:255
void evaluateTime(const CL_Track *track, fpreal start_time, fpreal stop_time, fpreal *data, int size) const
Definition: CL_Clip.h:217
double fpreal64
Definition: SYS_Types.h:201
#define CL_API
Definition: CL_API.h:10
GLfloat f
Definition: glcorearb.h:1926
SYS_FORCE_INLINE const char * buffer() const
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
bool isConstant() const
Definition: CL_Clip.h:433
UT_VariantMap CL_ClipAttributes
Definition: CL_Clip.h:40
int getCheckDuplicateNames() const
Definition: CL_Clip.h:61
DATA_TYPE findSymbol(const UT_StringRef &symbol) const
static const UT_StringHolder theEmptyString
fpreal evaluateSingle(const CL_Track *track, fpreal index) const
Definition: CL_Clip.h:196
GLdouble GLdouble GLint GLint order
Definition: glad.h:2676
long long int64
Definition: SYS_Types.h:116
fpreal getFrame(fpreal index, fpreal fps) const
Definition: CL_Clip.h:273
void trim(std::string &s)
Definition: Name.h:83
GLuint const GLchar * name
Definition: glcorearb.h:786
GLushort pattern
Definition: glad.h:2583
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
void setStart(fpreal start)
Definition: CL_Clip.h:73
GLdouble t
Definition: glad.h:2397
int sprintf(const char *fmt,...) SYS_PRINTF_CHECK_ATTRIBUTE(2
GLsizeiptr size
Definition: glcorearb.h:664
void setSlerpCapacity(int capacity)
Definition: CL_Clip.h:125
void setSampleRate(fpreal rate)
Definition: CL_Clip.h:87
void setCheckDuplicateNames(int val)
Definition: CL_Clip.h:64
fpreal64 fpreal
Definition: SYS_Types.h:277
GLuint index
Definition: glcorearb.h:786
fpreal evaluateSingleTime(const CL_Track *track, fpreal t) const
Definition: CL_Clip.h:199
static bool buildKey(const UT_StringHolder &name, int track, int sample, UT_WorkBuffer &key)
Definition: CL_Clip.h:518
void setSlerpOrder(UT_XformOrder::xyzOrder o)
Definition: CL_Clip.h:152
GLuint GLfloat * val
Definition: glcorearb.h:1608
int getTrackLength() const
Definition: CL_Clip.h:82
fpreal getIndex(fpreal time) const
Definition: CL_Clip.h:267
static fpreal getFrameT(fpreal time, fpreal fps)
Definition: CL_Clip.h:282
fpreal getEndTime() const
Definition: CL_Clip.h:258
UT_ValArray< const CL_Clip * > CL_ClipListC
Definition: CL_Clip.h:491
UT_ValArray< CL_Clip * > CL_ClipList
Definition: CL_Clip.h:490
ImageBuf OIIO_API add(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
fpreal getIndexF(fpreal frame, fpreal fps) const
Definition: CL_Clip.h:276
ImageBuf OIIO_API crop(const ImageBuf &src, ROI roi={}, int nthreads=0)
const CL_Track * getTrack(int index) const
Definition: CL_Clip.h:180
int getNumTracks() const
Definition: CL_Clip.h:173
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T clip(const T &p, const Box< T > &box) IMATH_NOEXCEPT
Definition: ImathBoxAlgo.h:29
fpreal getTime(fpreal index) const
Definition: CL_Clip.h:270
fpreal getEnd() const
Definition: CL_Clip.h:79
SYS_FORCE_INLINE bool isstring() const
Definition: format.h:895