OpenSubdiv
Loading...
Searching...
No Matches
patchParam.h
Go to the documentation of this file.
1//
2// Copyright 2013 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://opensubdiv.org/license.
6//
7
8#ifndef OPENSUBDIV3_FAR_PATCH_PARAM_H
9#define OPENSUBDIV3_FAR_PATCH_PARAM_H
10
11#include "../version.h"
12
13#include "../far/types.h"
14
15namespace OpenSubdiv {
16namespace OPENSUBDIV_VERSION {
17
18namespace Far {
19
84
151 void Set(Index faceid, short u, short v,
152 unsigned short depth, bool nonquad,
153 unsigned short boundary, unsigned short transition,
154 bool regular = false);
155
157 void Clear() { field0 = field1 = 0; }
158
160 Index GetFaceId() const { return Index(unpack(field0,28,0)); }
161
164 unsigned short GetU() const { return (unsigned short)unpack(field1,10,22); }
165
168 unsigned short GetV() const { return (unsigned short)unpack(field1,10,12); }
169
171 unsigned short GetTransition() const { return (unsigned short)unpack(field0,4,28); }
172
174 unsigned short GetBoundary() const { return (unsigned short)unpack(field1,5,7); }
175
177 bool NonQuadRoot() const { return (unpack(field1,1,4) != 0); }
178
180 unsigned short GetDepth() const { return (unsigned short)unpack(field1,4,0); }
181
183 float GetParamFraction() const;
184
191 template <typename REAL>
192 void Normalize( REAL & u, REAL & v ) const;
193 template <typename REAL>
194 void NormalizeTriangle( REAL & u, REAL & v ) const;
195
202 template <typename REAL>
203 void Unnormalize( REAL & u, REAL & v ) const;
204 template <typename REAL>
205 void UnnormalizeTriangle( REAL & u, REAL & v ) const;
206
208 bool IsTriangleRotated() const;
209
211 bool IsRegular() const { return (unpack(field1,1,5) != 0); }
212
213 unsigned int field0:32;
214 unsigned int field1:32;
215
216private:
217 unsigned int pack(unsigned int value, int width, int offset) const {
218 return (unsigned int)((value & ((1<<width)-1)) << offset);
219 }
220
221 unsigned int unpack(unsigned int value, int width, int offset) const {
222 return (unsigned int)((value >> offset) & ((1<<width)-1));
223 }
224};
225
226typedef std::vector<PatchParam> PatchParamTable;
227
230
231inline void
232PatchParam::Set(Index faceid, short u, short v,
233 unsigned short depth, bool nonquad,
234 unsigned short boundary, unsigned short transition,
235 bool regular) {
236 field0 = pack(faceid, 28, 0) |
237 pack(transition, 4, 28);
238
239 field1 = pack(u, 10, 22) |
240 pack(v, 10, 12) |
241 pack(boundary, 5, 7) |
242 pack(regular, 1, 5) |
243 pack(nonquad, 1, 4) |
244 pack(depth, 4, 0);
245}
246
247inline float
249 return 1.0f / (float)(1 << (GetDepth() - NonQuadRoot()));
250}
251
252template <typename REAL>
253inline void
254PatchParam::Normalize( REAL & u, REAL & v ) const {
255
256 REAL fracInv = (REAL)(1.0f / GetParamFraction());
257
258 u = u * fracInv - (REAL)GetU();
259 v = v * fracInv - (REAL)GetV();
260}
261
262template <typename REAL>
263inline void
264PatchParam::Unnormalize( REAL & u, REAL & v ) const {
265
266 REAL frac = (REAL)GetParamFraction();
267
268 u = (u + (REAL)GetU()) * frac;
269 v = (v + (REAL)GetV()) * frac;
270}
271
272inline bool
274
275 return (GetU() + GetV()) >= (1 << GetDepth());
276}
277
278template <typename REAL>
279inline void
280PatchParam::NormalizeTriangle( REAL & u, REAL & v ) const {
281
282 if (IsTriangleRotated()) {
283 REAL fracInv = (REAL)(1.0f / GetParamFraction());
284
285 int depthFactor = 1 << GetDepth();
286 u = (REAL)(depthFactor - GetU()) - (u * fracInv);
287 v = (REAL)(depthFactor - GetV()) - (v * fracInv);
288 } else {
289 Normalize(u, v);
290 }
291}
292
293template <typename REAL>
294inline void
295PatchParam::UnnormalizeTriangle( REAL & u, REAL & v ) const {
296
297 if (IsTriangleRotated()) {
298 REAL frac = GetParamFraction();
299
300 int depthFactor = 1 << GetDepth();
301 u = ((REAL)(depthFactor - GetU()) - u) * frac;
302 v = ((REAL)(depthFactor - GetV()) - v) * frac;
303 } else {
304 Unnormalize(u, v);
305 }
306}
307
308} // end namespace Far
309
310} // end namespace OPENSUBDIV_VERSION
311using namespace OPENSUBDIV_VERSION;
312
313} // end namespace OpenSubdiv
314
315#endif /* OPENSUBDIV3_FAR_PATCH_PARAM */
Vtr::ConstArray< PatchParam > ConstPatchParamArray
Definition patchParam.h:229
std::vector< PatchParam > PatchParamTable
Definition patchParam.h:226
Vtr::Array< PatchParam > PatchParamArray
Definition patchParam.h:228
bool IsTriangleRotated() const
Returns if a triangular patch is parametrically rotated 180 degrees.
Definition patchParam.h:273
bool IsRegular() const
Returns whether the patch is regular.
Definition patchParam.h:211
void NormalizeTriangle(REAL &u, REAL &v) const
Definition patchParam.h:280
unsigned short GetBoundary() const
Returns the boundary edge encoding for the patch.
Definition patchParam.h:174
unsigned short GetTransition() const
Returns the transition edge encoding for the patch.
Definition patchParam.h:171
float GetParamFraction() const
Returns the fraction of unit parametric space covered by this face.
Definition patchParam.h:248
unsigned short GetU() const
Returns the log2 value of the u parameter at the first corner of the patch.
Definition patchParam.h:164
Index GetFaceId() const
Returns the faceid.
Definition patchParam.h:160
unsigned short GetDepth() const
Returns the level of subdivision of the patch.
Definition patchParam.h:180
bool NonQuadRoot() const
True if the parent base face is a non-quad.
Definition patchParam.h:177
void Set(Index faceid, short u, short v, unsigned short depth, bool nonquad, unsigned short boundary, unsigned short transition, bool regular=false)
Sets the values of the bit fields.
Definition patchParam.h:232
void Unnormalize(REAL &u, REAL &v) const
A (u,v) pair in a normalized parametric space is mapped back into the fraction of parametric space co...
Definition patchParam.h:264
void UnnormalizeTriangle(REAL &u, REAL &v) const
Definition patchParam.h:295
unsigned short GetV() const
Returns the log2 value of the v parameter at the first corner of the patch.
Definition patchParam.h:168
void Normalize(REAL &u, REAL &v) const
A (u,v) pair in the fraction of parametric space covered by this face is mapped into a normalized par...
Definition patchParam.h:254