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