OpenSubdiv: opensubdiv/far/patchParam.h Source File

| 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
134
135 struct PatchParam {
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
226 typedef std::vector<PatchParam> PatchParamTable;
227
228 typedef Vtr::Array<PatchParam> PatchParamArray;
229 typedef Vtr::ConstArray<PatchParam> ConstPatchParamArray;
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
248PatchParam::GetParamFraction( ) const {
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
273PatchParam::IsTriangleRotated() const {
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 */
OpenSubdiv
Definition limits.h:15
OpenSubdiv::OPENSUBDIV_VERSION
Definition limits.h:16
OpenSubdiv::OPENSUBDIV_VERSION::Far
Definition refinerSurfaceFactory.h:19
OpenSubdiv::OPENSUBDIV_VERSION::Far::ConstPatchParamArray
Vtr::ConstArray< PatchParam > ConstPatchParamArray
Definition patchParam.h:229
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParamTable
std::vector< PatchParam > PatchParamTable
Definition patchParam.h:226
OpenSubdiv::OPENSUBDIV_VERSION::Far::Index
Vtr::Index Index
Definition types.h:24
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParamArray
Vtr::Array< PatchParam > PatchParamArray
Definition patchParam.h:228
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam
Patch parameterization.
Definition patchParam.h:135
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::IsTriangleRotated
bool IsTriangleRotated() const
Returns if a triangular patch is parametrically rotated 180 degrees.
Definition patchParam.h:273
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::IsRegular
bool IsRegular() const
Returns whether the patch is regular.
Definition patchParam.h:211
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::NormalizeTriangle
void NormalizeTriangle(REAL &u, REAL &v) const
Definition patchParam.h:280
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::GetBoundary
unsigned short GetBoundary() const
Returns the boundary edge encoding for the patch.
Definition patchParam.h:174
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::GetTransition
unsigned short GetTransition() const
Returns the transition edge encoding for the patch.
Definition patchParam.h:171
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::GetParamFraction
float GetParamFraction() const
Returns the fraction of unit parametric space covered by this face.
Definition patchParam.h:248
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::GetU
unsigned short GetU() const
Returns the log2 value of the u parameter at the first corner of the patch.
Definition patchParam.h:164
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::GetFaceId
Index GetFaceId() const
Returns the faceid.
Definition patchParam.h:160
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::GetDepth
unsigned short GetDepth() const
Returns the level of subdivision of the patch.
Definition patchParam.h:180
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::NonQuadRoot
bool NonQuadRoot() const
True if the parent base face is a non-quad.
Definition patchParam.h:177
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::Set
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
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::Clear
void Clear()
Resets everything to 0.
Definition patchParam.h:157
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::field1
unsigned int field1
Definition patchParam.h:214
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::Unnormalize
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
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::UnnormalizeTriangle
void UnnormalizeTriangle(REAL &u, REAL &v) const
Definition patchParam.h:295
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::GetV
unsigned short GetV() const
Returns the log2 value of the v parameter at the first corner of the patch.
Definition patchParam.h:168
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::field0
unsigned int field0
Definition patchParam.h:213
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::Normalize
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
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray
Definition array.h:36
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array
Definition array.h:88
types.h