HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GT_ElementSet.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_ElementSet.h (GT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __GT_ElementSet__
12 #define __GT_ElementSet__
13 
14 #include "GT_API.h"
15 
16 #include "GT_DANumeric.h"
17 #include "GT_Handles.h"
18 #include "GT_Types.h"
19 #include <UT/UT_IntrusivePtr.h>
20 #include <UT/UT_Lock.h>
21 #include <UT/UT_Set.h>
22 #include <SYS/SYS_Deprecated.h>
23 
24 class UT_JSONWriter;
25 class GT_ElementSet;
26 
28 
29 /// A set of element indices in a GT_Primitive, e.g. points / faces / tets
30 class GT_API GT_ElementSet : public UT_IntrusiveRefCounter<GT_ElementSet>
31 {
32 public:
33  class SetType
34  {
35  public:
36  // Thread-safety semantics
37  // Only one thread can write to the set at a time
38  // No other threads can read during creation
39  // After creation, multiple threads should be able to access (search,
40  // iterate)
42 
43  SetType() = default;
44 
46  {
47  return sizeof(*this) + myOffsets.getMemoryUsage();
48  }
49 
51  {
52  if (myOffsets.entries() && myOffsets.last() >= offset)
53  {
54  if (myOffsets.last() == offset)
55  return; // No duplicates, so we can just return
56  myDirty = true; // Flag as un-ordered
57  }
58  myOffsets.append(offset);
59  }
60  template <typename T>
61  void multipleInsert(const T *list, exint size)
62  {
63  if (size < 2)
64  {
65  if (size == 1)
66  insert(list[0]); // Don't automatically mark as dirty
67  return;
68  }
69  // Instead of keeping track of whether the inserts are ordered or
70  // not, just mark the list as dirty.
71  myDirty = true;
72  exint start = myOffsets.entries();
73  exint end = start + size;
74  myOffsets.setCapacity(end);
75  myOffsets.entries(end);
76  for (exint i = start; i < end; ++i, ++list)
77  myOffsets(i) = list[0];
78  }
79  exint size() const { return myOffsets.entries(); }
80  int count(GT_Offset idx) const
81  {
82  return findOffset(idx) >= 0 ? 1 : 0;
83  }
84  void erase(GT_Offset item)
85  {
86  exint idx = findOffset(item);
87  if (idx >= 0)
88  myOffsets.removeIndex(idx);
89  }
92  {
93  // If we begin iteration, and another thread does a find, we need
94  // to be sorted.
95  ensureSorted();
96  return myOffsets.begin();
97  }
98  const_iterator end() const { return myOffsets.end(); }
99 
100  // Methods not on std::set
101  template <typename T> void
102  fillOrderedArray(T *list) const
103  {
104  ensureSorted();
105  for (exint i = 0; i < myOffsets.entries(); ++i)
106  list[i] = myOffsets(i);
107  }
108 
109  private:
110  inline void ensureSorted() const
111  {
112  if (myDirty)
113  SYSconst_cast(this)->sortOffsets();
114  }
115  void sortOffsets()
116  {
117  // TODO: Fence this?
118  UT_AutoLock lock(myLock);
119  myOffsets.sort();
120  myOffsets.sortedRemoveDuplicates();
121  myDirty = false;
122  }
123  exint findOffset(GT_Offset item) const
124  {
125  ensureSorted();
126  return myOffsets.uniqueSortedFind(item, std::less<exint>());
127  }
128  ArrayType myOffsets;
129  UT_Lock myLock;
130  bool myDirty = false;
131  };
132 
133  GT_ElementSet() = default;
134  GT_ElementSet(const GT_DataArrayHandle &members);
135  ~GT_ElementSet();
136 
138 
139  /// @{
140  /// Number of elements in the set
141  GT_Size entries() const { return myMembers.size(); }
142  GT_Size size() const { return myMembers.size(); }
143  exint getMemoryUsage() const;
144  /// @}
145 
146  /// Test whether an element is in the set
147  bool contains(GT_Offset element) const
148  { return myMembers.count(element) > 0; }
149 
150  /// Add an element to the set
151  void addElement(GT_Offset element);
152 
153  /// @{
154  /// Add a list of elements
155  void addElements(const int32 *indices, exint count);
156  void addElements(const int64 *indices, exint count);
157  /// @}
158 
159  /// Remove an element from the set
160  void removeElement(GT_Offset element);
161 
162  /// Extract the member list as a sorted array of integers
163  GT_DataArrayHandle extractMembers() const;
164 
165  /// Save sets to a JSON stream
166  bool save(UT_JSONWriter &w) const;
167 
168  SetType::const_iterator begin() const { return myMembers.begin(); }
169  SetType::const_iterator end() const { return myMembers.end(); }
170 
171  /// Check whether any of the elements in this set are in the list of
172  /// elements to be deleted.
173  bool anyIntersect(const UT_Set<int> &elements) const;
174 
175  /// Delete elements from the set. The second argument is an array
176  /// mapping the old element numbers to the new element numbers. For example,
177  /// deleting polygon 2 from a set of [0,1,2,3] would expect a remapping
178  /// array of [0,1,x,2] (the value of 2 doesn't actually matter)
179  GT_ElementSetPtr deleteElements(const UT_Set<int> &elements,
180  const GT_Int32Array &mapping) const;
181 
182  // Old Methods for backwards compatibility with GT_FaceSet.
183 
184  /// Add a face to the set
185  SYS_DEPRECATED_REPLACE(20.5, "addElement")
186  void addFace(GT_Offset face_index)
187  { addElement(face_index); }
188 
189  /// @{
190  /// Add a list of faces
191  SYS_DEPRECATED_REPLACE(20.5, "addElements")
192  void addFaces(const int32 *indices, exint count)
193  { addElements(indices, count); }
194  SYS_DEPRECATED_REPLACE(20.5, "addElements")
195  void addFaces(const int64 *indices, exint count)
196  { addElements(indices, count); }
197  /// @}
198 
199  /// Remove a face from the set
200  SYS_DEPRECATED_REPLACE(20.5, "removeElement")
201  void removeFace(GT_Offset face_index)
202  { removeElement(face_index); }
203 
204  /// Delete polygons from the face set. The second argument is an array
205  /// mapping the old face numbers to the new face numbers. For example,
206  /// deleting polygon 2 from a set of [0,1,2,3] would expect a remapping
207  /// array of [0,1,x,2] (the value of 2 doesn't actually matter)
208  SYS_DEPRECATED_REPLACE(20.5, "deleteElements")
209  GT_ElementSetPtr deleteFaces(const UT_Set<int> &faces,
210  const GT_Int32Array &mapping) const
211  { return deleteElements(faces, mapping); }
212 
213 protected:
215  bool my64bit = false;
216 };
217 
218 #endif
GLsizei GLenum const void * indices
Definition: glcorearb.h:406
int int32
Definition: SYS_Types.h:39
SetType myMembers
void
Definition: png.h:1083
SetType::const_iterator begin() const
GLuint start
Definition: glcorearb.h:475
void fillOrderedArray(T *list) const
SYS_FORCE_INLINE T * SYSconst_cast(const T *foo)
Definition: SYS_Types.h:136
const_iterator end() const
Definition: GT_ElementSet.h:98
#define GT_API
Definition: GT_API.h:13
int64 exint
Definition: SYS_Types.h:125
A set of element indices in a GT_Primitive, e.g. points / faces / tets.
Definition: GT_ElementSet.h:30
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:37
A reference counter base class for use with UT_IntrusivePtr.
OIIO_FORCEINLINE vbool4 insert(const vbool4 &a, bool val)
Helper: substitute val for a[i].
Definition: simd.h:3436
exint getMemoryUsage() const
Definition: GT_ElementSet.h:45
GT_Size entries() const
int count(GT_Offset idx) const
Definition: GT_ElementSet.h:80
#define SYS_DEPRECATED_REPLACE(__V__, __R__)
GLintptr offset
Definition: glcorearb.h:665
GT_Size size() const
GLuint GLuint end
Definition: glcorearb.h:475
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
Wrapper around hboost::intrusive_ptr.
int64 GT_Offset
Definition: GT_Types.h:129
long long int64
Definition: SYS_Types.h:116
void multipleInsert(const T *list, exint size)
Definition: GT_ElementSet.h:61
ArrayType::const_iterator const_iterator
Definition: GT_ElementSet.h:90
void insert(GT_Offset offset)
Definition: GT_ElementSet.h:50
uint8_t ArrayType
Definition: NanoVDB.h:5566
exint size() const
Definition: GT_ElementSet.h:79
const_iterator begin() const
Definition: GT_ElementSet.h:91
int64 GT_Size
Definition: GT_Types.h:128
GLsizeiptr size
Definition: glcorearb.h:664
SetType::const_iterator end() const
base_iterator< const GT_Offset, true > const_iterator
Definition: UT_Array.h:999
bool contains(GT_Offset element) const
Test whether an element is in the set.
An array of numeric values (int32, int64, fpreal16, fpreal32, fpreal64)
Definition: GT_DANumeric.h:23
void erase(GT_Offset item)
Definition: GT_ElementSet.h:84
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
GLint GLsizei count
Definition: glcorearb.h:405