HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GT_CountArray.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: GT_CountArray.h ( GT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __GT_CountArray__
12 #define __GT_CountArray__
13 
14 #include "GT_API.h"
15 #include "GT_DataArray.h"
16 #include "GT_Handles.h"
17 
18 class UT_JSONWriter;
19 
20 /// A count/offset array
21 ///
22 /// In a polygon mesh, there is a list of faces. Each face has a number of
23 /// vertices and a list of vertices. Thus, for each polygon, there's an offset
24 /// into the vertex array and a count of the number of vertices to extract.
25 /// This array manages those offsets/counts efficiently
26 /// For example, if there are counts [3, 4, 2], the corresponding offset array
27 /// would be [0, 3, 7]. Thus: @code
28 /// counts.getOffset(0) == 0;
29 /// counts.getOffset(1) == 3;
30 /// counts.getOffset(2) == 7;
31 /// counts.getCount(0) == 3;
32 /// counts.getCount(1) == 4;
33 /// counts.getCount(2) == 2;
34 /// @endcode
36 {
37 public:
39  : myOffsets()
40  , myMaxCount(0)
41  , myMinCount(SYS_INT32_MAX)
42  {
43  }
45  GT_Size mincount=0, GT_Size maxcount=0)
46  : myOffsets()
47  {
48  init(counts, mincount, maxcount);
49  }
50 
51  /// Clone from the source count array, but swapping out the offset array
52  /// with the given one.
54  const GT_DataArrayHandle &raw_offsets,
55  GT_Size mincount,
56  GT_Size maxcount)
57  : myOffsets(raw_offsets)
58  , myMinCount(mincount)
59  , myMaxCount(maxcount)
60  {
61  UT_ASSERT(raw_offsets->entries() == counts.rawOffsets()->entries());
62  }
63 
64  /// Return memory used
65  exint getMemoryUsage() const;
66  /// harden the data array
67  void harden();
68 
69  /// Clear the count array
70  void clear()
71  {
72  myOffsets = GT_DataArrayHandle();
73  myMaxCount = 0;
74  }
75 
76  /// Return the storage type
78  {
79  return myOffsets ? myOffsets->getStorage() : GT_STORE_INT32;
80  }
81 
82  /// Initialize given an array of counts
83  void init(const GT_DataArrayHandle &counts,
84  GT_Size mincount=0, GT_Size maxcount=0);
85  /// Initialize when you know that there are @c entries that are all the
86  /// same @c repeated_value.
87  void init(exint entries, exint repeated_value);
88 
89  GT_Size entries() const { return myOffsets ? myOffsets->entries() : 0; }
91  {
92  return idx ? myOffsets->getI64(idx-1) : 0;
93  }
94  GT_Size getCount(exint idx) const
95  {
96  GT_Size size = myOffsets->getI64(idx);
97  if (idx)
98  size -= myOffsets->getI64(idx-1);
99  return size;
100  }
101  // More efficient version to get both the offset and size. This only
102  // requires two lookups into the offset array (instead of three).
103  std::pair<GT_Size,GT_Size> getOffsetCount(exint idx) const
104  {
105  const GT_Size offset = idx ? myOffsets->getI64(idx-1) : 0;
106  const GT_Size size = myOffsets->getI64(idx) - offset;
107  return std::make_pair(offset, size);
108  }
109 
110  /// Test equality
111  bool isEqual(const GT_CountArray &other) const
112  {
113  if (myMinCount != other.myMinCount
114  || myMaxCount != other.myMaxCount)
115  {
116  return false;
117  }
118  if (!myOffsets || !other.myOffsets)
119  return !myOffsets && !other.myOffsets;
120  return myOffsets->isEqual(*other.myOffsets);
121  }
122  bool operator==(const GT_CountArray &other) const
123  { return isEqual(other); }
124  bool operator!=(const GT_CountArray &other) const
125  { return !isEqual(other); }
126 
127  /// Return the minimum value that @c getCount() can return
128  GT_Size getMinCount() const { return myMinCount; }
129  /// Return the maximum value that @c getCount() can return
130  GT_Size getMaxCount() const { return myMaxCount; }
131 
132  /// Return the sum of all the counts
133  GT_Size sumCounts() const { return getOffset(entries()); }
134 
135  const GT_DataArrayHandle &rawOffsets() const { return myOffsets; }
136 
137  GT_DataArrayHandle extractCounts() const;
138  GT_DataArrayHandle extractOffsets() const;
139 
140  /// Creates a list of {0,0,0, 1,1,1,1,1, 2,2} from {3,5,2}
141  GT_DataArrayHandle buildRepeatList() const;
142 
143  /// Saves as an array of the counts (not the offsets). i.e.
144  /// [ getCount(0), getCount(1), ... getCount(n) ]
145  bool save(UT_JSONWriter &w) const;
146 private:
147  GT_DataArrayHandle myOffsets;
148  GT_Size myMinCount;
149  GT_Size myMaxCount;
150 };
151 
152 #endif
GT_Storage
Definition: GT_Types.h:19
bool isEqual(const GT_CountArray &other) const
Test equality.
GT_Offset getOffset(exint idx) const
Definition: GT_CountArray.h:90
GT_CountArray(const GT_DataArrayHandle &counts, GT_Size mincount=0, GT_Size maxcount=0)
Definition: GT_CountArray.h:44
#define SYS_INT32_MAX
Definition: SYS_Types.h:171
const GT_DataArrayHandle & rawOffsets() const
#define GT_API
Definition: GT_API.h:13
int64 exint
Definition: SYS_Types.h:125
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:37
std::pair< GT_Size, GT_Size > getOffsetCount(exint idx) const
bool operator==(const GT_CountArray &other) const
GT_Storage getStorage() const
Return the storage type.
Definition: GT_CountArray.h:77
GLintptr offset
Definition: glcorearb.h:665
UT_IntrusivePtr< GT_DataArray > GT_DataArrayHandle
Definition: GT_DataArray.h:32
GT_Size getMinCount() const
Return the minimum value that getCount() can return.
int64 GT_Offset
Definition: GT_Types.h:129
GT_Size getMaxCount() const
Return the maximum value that getCount() can return.
int64 GT_Size
Definition: GT_Types.h:128
GLsizeiptr size
Definition: glcorearb.h:664
GT_CountArray(const GT_CountArray &counts, const GT_DataArrayHandle &raw_offsets, GT_Size mincount, GT_Size maxcount)
Definition: GT_CountArray.h:53
void clear()
Clear the count array.
Definition: GT_CountArray.h:70
GT_Size entries() const
Definition: GT_CountArray.h:89
bool operator!=(const GT_CountArray &other) const
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
GT_Size getCount(exint idx) const
Definition: GT_CountArray.h:94
GT_Size sumCounts() const
Return the sum of all the counts.