All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fvarData.h
Go to the documentation of this file.
1 //
2 // Copyright 2013 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 
25 #ifndef HBRFVARDATA_H
26 #define HBRFVARDATA_H
27 
28 #include <cstring>
29 #include <cmath>
30 
31 #include "../version.h"
32 
33 namespace OpenSubdiv {
34 namespace OPENSUBDIV_VERSION {
35 
36 template <class T> class HbrFVarEdit;
37 template <class T> class HbrFace;
38 template <class T> class HbrVertex;
39 
40 // This class implements a "face varying vector item". Really it's
41 // just a smart wrapper around face varying data (itself just a bunch
42 // of floats) stored on each vertex.
43 template <class T> class HbrFVarData {
44 
45 private:
46  HbrFVarData()
47  : faceid(0), initialized(0) {
48  }
49 
50  ~HbrFVarData() {
51  Uninitialize();
52  }
53 
54  HbrFVarData(const HbrFVarData &/* data */) {}
55 
56 public:
57 
58  // Sets the face id
59  void SetFaceID(int id) {
60  faceid = id;
61  }
62 
63  // Returns the id of the face to which this data is bound
64  int GetFaceID() const {
65  return faceid;
66  }
67 
68  // Clears the initialized flag
69  void Uninitialize() {
70  initialized = 0;
71  faceid = 0;
72  }
73 
74  // Returns initialized flag
75  bool IsInitialized() const {
76  return initialized;
77  }
78 
79  // Sets initialized flag
80  void SetInitialized() {
81  initialized = 1;
82  }
83 
84  // Return the data from the NgpFVVector
85  float* GetData(int item) { return data + item; }
86 
87  // Clears the indicates value of this item
88  void Clear(int startindex, int width) {
89  memset(data + startindex, 0, width * sizeof(float));
90  }
91 
92  // Clears all values of this item
93  void ClearAll(int width) {
94  initialized = 1;
95  memset(data, 0, width * sizeof(float));
96  }
97 
98  // Set values of the indicated item (with the indicated weighing)
99  // on this item
100  void SetWithWeight(const HbrFVarData& fvvi, int startindex, int width, float weight) {
101  float *dst = data + startindex;
102  const float *src = fvvi.data + startindex;
103  for (int i = 0; i < width; ++i) {
104  *dst++ = weight * *src++;
105  }
106  }
107 
108  // Add values of the indicated item (with the indicated weighing)
109  // to this item
110  void AddWithWeight(const HbrFVarData& fvvi, int startindex, int width, float weight) {
111  float *dst = data + startindex;
112  const float *src = fvvi.data + startindex;
113  for (int i = 0; i < width; ++i) {
114  *dst++ += weight * *src++;
115  }
116  }
117 
118  // Add all values of the indicated item (with the indicated
119  // weighing) to this item
120  void AddWithWeightAll(const HbrFVarData& fvvi, int width, float weight) {
121  float *dst = data;
122  const float *src = fvvi.data;
123  for (int i = 0; i < width; ++i) {
124  *dst++ += weight * *src++;
125  }
126  }
127 
128  // Compare all values item against a float buffer. Returns true
129  // if all values match
130  bool CompareAll(int width, const float *values, float tolerance=0.0f) const {
131  if (!initialized) return false;
132  for (int i = 0; i < width; ++i) {
133  if (fabsf(values[i] - data[i]) > tolerance) return false;
134  }
135  return true;
136  }
137 
138  // Initializes data
139  void SetAllData(int width, const float *values) {
140  initialized = 1;
141  memcpy(data, values, width * sizeof(float));
142  }
143 
144  // Compare this item against another item with tolerance. Returns
145  // true if it compares identical
146  bool Compare(const HbrFVarData& fvvi, int startindex, int width, float tolerance=0.0f) const {
147  for (int i = 0; i < width; ++i) {
148  if (fabsf(data[startindex + i] - fvvi.data[startindex + i]) > tolerance) return false;
149  }
150  return true;
151  }
152 
153  // Modify the data of the item with an edit
154  void ApplyFVarEdit(const HbrFVarEdit<T>& edit);
155 
156  friend class HbrVertex<T>;
157 
158 private:
159  unsigned int faceid:31;
160  unsigned int initialized:1;
161  float data[1];
162 };
163 
164 } // end namespace OPENSUBDIV_VERSION
165 using namespace OPENSUBDIV_VERSION;
166 
167 } // end namespace OpenSubdiv
168 
169 #include "../hbr/fvarEdit.h"
170 
171 namespace OpenSubdiv {
172 namespace OPENSUBDIV_VERSION {
173 
174 template <class T>
175 void
177  float *dst = data + edit.GetIndex() + edit.GetOffset();
178  const float *src = edit.GetEdit();
179  for (int i = 0; i < edit.GetWidth(); ++i) {
180  switch(edit.GetOperation()) {
182  *dst++ = *src++;
183  break;
185  *dst++ += *src++;
186  break;
188  *dst++ -= *src++;
189  }
190  }
191  initialized = 1;
192  }
193 
194 
195 } // end namespace OPENSUBDIV_VERSION
196 using namespace OPENSUBDIV_VERSION;
197 
198 } // end namespace OpenSubdiv
199 
200 #endif /* HBRFVARDATA_H */
201 
void ApplyFVarEdit(const HbrFVarEdit< T > &edit)
Definition: fvarData.h:176
void AddWithWeightAll(const HbrFVarData &fvvi, int width, float weight)
Definition: fvarData.h:120
void SetWithWeight(const HbrFVarData &fvvi, int startindex, int width, float weight)
Definition: fvarData.h:100
HbrHierarchicalEdit< T >::Operation GetOperation() const
Definition: fvarEdit.h:86
void AddWithWeight(const HbrFVarData &fvvi, int startindex, int width, float weight)
Definition: fvarData.h:110
bool Compare(const HbrFVarData &fvvi, int startindex, int width, float tolerance=0.0f) const
Definition: fvarData.h:146
void SetAllData(int width, const float *values)
Definition: fvarData.h:139
void Clear(int startindex, int width)
Definition: fvarData.h:88
bool CompareAll(int width, const float *values, float tolerance=0.0f) const
Definition: fvarData.h:130