HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GT_FaceSet.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_FaceSet.h (GT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __GT_FaceSet__
12 #define __GT_FaceSet__
13 
14 #include "GT_API.h"
15 #include <UT/UT_IntrusivePtr.h>
16 #include <UT/UT_Lock.h>
17 #include <UT/UT_Set.h>
18 #include "GT_Types.h"
19 #include "GT_Handles.h"
20 #include "GT_DANumeric.h"
21 
22 class UT_JSONWriter;
23 class GT_FaceSet;
24 
26 
27 /// Class to store "face sets" on GT mesh primitives
29  : public UT_IntrusiveRefCounter<GT_FaceSet>
30  , public UT_NonCopyable
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)
43  : myOffsets()
44  , myDirty(false)
45  {}
47  : myOffsets(src.myOffsets)
48  , myDirty(src.myDirty)
49  {
50  }
51  ~SetType() {}
52 
54  {
55  return sizeof(*this) + myOffsets.getMemoryUsage();
56  }
57 
59  {
60  if (myOffsets.entries() && myOffsets.last() >= offset)
61  {
62  if (myOffsets.last() == offset)
63  return; // No duplicates, so we can just return
64  myDirty = true; // Flag as un-ordered
65  }
66  myOffsets.append(offset);
67  }
68  template <typename T>
69  void multipleInsert(const T *list, exint size)
70  {
71  if (size < 2)
72  {
73  if (size == 1)
74  insert(list[0]); // Don't automatically mark as dirty
75  return;
76  }
77  // Instead of keeping track of whether the inserts are ordered or
78  // not, just mark the list as dirty.
79  myDirty = true;
80  exint start = myOffsets.entries();
81  exint end = start + size;
82  myOffsets.setCapacity(end);
83  myOffsets.entries(end);
84  for (exint i = start; i < end; ++i, ++list)
85  myOffsets(i) = list[0];
86  }
87  exint size() const { return myOffsets.entries(); }
88  int count(GT_Offset idx) const
89  {
90  return findOffset(idx) >= 0 ? 1 : 0;
91  }
92  void erase(GT_Offset item)
93  {
94  exint idx = findOffset(item);
95  if (idx >= 0)
96  myOffsets.removeIndex(idx);
97  }
100  {
101  // If we begin iteration, and another thread does a find, we need
102  // to be sorted.
103  ensureSorted();
104  return myOffsets.begin();
105  }
106  const_iterator end() const { return myOffsets.end(); }
107 
108  // Methods not on std::set
109  template <typename T> void
110  fillOrderedArray(T *list) const
111  {
112  ensureSorted();
113  for (exint i = 0; i < myOffsets.entries(); ++i)
114  list[i] = myOffsets(i);
115  }
116 
117  private:
118  inline void ensureSorted() const
119  {
120  if (myDirty)
121  SYSconst_cast(this)->sortOffsets();
122  }
123  void sortOffsets()
124  {
125  // TODO: Fence this?
126  UT_AutoLock lock(myLock);
127  myOffsets.sort();
128  myOffsets.sortedRemoveDuplicates();
129  myDirty = false;
130  }
131  exint findOffset(GT_Offset item) const
132  {
133  ensureSorted();
134  return myOffsets.uniqueSortedFind(item, std::less<exint>());
135  }
136  ArrayType myOffsets;
137  UT_Lock myLock;
138  bool myDirty;
139  };
140 
141  GT_FaceSet();
142  GT_FaceSet(const GT_DataArrayHandle &members);
143  ~GT_FaceSet();
144 
145  /// @{
146  /// Number of faces in the face set
147  GT_Size entries() const { return myMembers.size(); }
148  GT_Size size() const { return myMembers.size(); }
149  exint getMemoryUsage() const;
150  /// @}
151 
152  /// Test whether a face is in the set
153  bool contains(GT_Offset face_index) const
154  { return myMembers.count(face_index) > 0; }
155 
156  /// Add a face to the set
157  void addFace(GT_Offset face_index);
158 
159  /// @{
160  /// Add a list of faces
161  void addFaces(const int32 *indices, exint count);
162  void addFaces(const int64 *indices, exint count);
163  /// @}
164 
165  /// Remove a face from the set
166  void removeFace(GT_Offset face_index);
167 
168  /// Check whether any of the faces in this set are in the list of faces to
169  /// be deleted.
170  bool anyIntersect(const UT_Set<int> &faces) const;
171 
172  /// Delete polygons from the face set. The second argument is an array
173  /// mapping the old face numbers to the new face numbers. For example,
174  /// deleting polygon 2 from a set of [0,1,2,3] would expect a remapping
175  /// array of [0,1,x,2] (the value of 2 doesn't actually matter)
176  GT_FaceSetPtr deleteFaces(const UT_Set<int> &faces,
177  const GT_Int32Array &mapping) const;
178 
179  /// Extract the member list as a sorted array of integers
180  GT_DataArrayHandle extractMembers() const;
181 
182  /// Save sets to a JSON stream
183  bool save(UT_JSONWriter &w) const;
184 
185  SetType::const_iterator begin() const { return myMembers.begin(); }
186  SetType::const_iterator end() const { return myMembers.end(); }
187 
188 private:
189  SetType myMembers;
190  bool my64bit;
191 };
192 
193 #endif
const_iterator begin() const
Definition: GT_FaceSet.h:99
GLsizei GLenum const void * indices
Definition: glcorearb.h:406
int int32
Definition: SYS_Types.h:39
GLuint start
Definition: glcorearb.h:475
SYS_FORCE_INLINE T * SYSconst_cast(const T *foo)
Definition: SYS_Types.h:136
bool contains(GT_Offset face_index) const
Test whether a face is in the set.
Definition: GT_FaceSet.h:153
#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
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_FaceSet.h:53
exint size() const
Definition: GT_FaceSet.h:87
GT_Size size() const
Definition: GT_FaceSet.h:148
void insert(GT_Offset offset)
Definition: GT_FaceSet.h:58
GLintptr offset
Definition: glcorearb.h:665
GLuint GLuint end
Definition: glcorearb.h:475
Wrapper around hboost::intrusive_ptr.
int64 GT_Offset
Definition: GT_Types.h:129
long long int64
Definition: SYS_Types.h:116
int count(GT_Offset idx) const
Definition: GT_FaceSet.h:88
void fillOrderedArray(T *list) const
Definition: GT_FaceSet.h:110
ArrayType::const_iterator const_iterator
Definition: GT_FaceSet.h:98
SetType::const_iterator begin() const
Definition: GT_FaceSet.h:185
int64 GT_Size
Definition: GT_Types.h:128
GLsizeiptr size
Definition: glcorearb.h:664
SetType::const_iterator end() const
Definition: GT_FaceSet.h:186
base_iterator< const GT_Offset, true > const_iterator
Definition: UT_Array.h:979
void multipleInsert(const T *list, exint size)
Definition: GT_FaceSet.h:69
SetType(const SetType &src)
Definition: GT_FaceSet.h:46
An array of numeric values (int32, int64, fpreal16, fpreal32, fpreal64)
Definition: GT_DANumeric.h:23
GT_Size entries() const
Definition: GT_FaceSet.h:147
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
const_iterator end() const
Definition: GT_FaceSet.h:106
Class to store "face sets" on GT mesh primitives.
Definition: GT_FaceSet.h:28
GLint GLsizei count
Definition: glcorearb.h:405
void erase(GT_Offset item)
Definition: GT_FaceSet.h:92
GLenum src
Definition: glcorearb.h:1793