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 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 OPENSUBDIV3_FAR_PATCH_PARAM_H
26#define OPENSUBDIV3_FAR_PATCH_PARAM_H
27
28#include "../version.h"
29
30#include "../far/types.h"
31
32namespace OpenSubdiv {
33namespace OPENSUBDIV_VERSION {
34
35namespace Far {
36
101
152 struct PatchParam {
168 void Set(Index faceid, short u, short v,
169 unsigned short depth, bool nonquad,
170 unsigned short boundary, unsigned short transition,
171 bool regular = false);
172
174 void Clear() { field0 = field1 = 0; }
175
177 Index GetFaceId() const { return Index(unpack(field0,28,0)); }
178
181 unsigned short GetU() const { return (unsigned short)unpack(field1,10,22); }
182
185 unsigned short GetV() const { return (unsigned short)unpack(field1,10,12); }
186
188 unsigned short GetTransition() const { return (unsigned short)unpack(field0,4,28); }
189
191 unsigned short GetBoundary() const { return (unsigned short)unpack(field1,5,7); }
192
194 bool NonQuadRoot() const { return (unpack(field1,1,4) != 0); }
195
197 unsigned short GetDepth() const { return (unsigned short)unpack(field1,4,0); }
198
200 float GetParamFraction() const;
201
208 template <typename REAL>
209 void Normalize( REAL & u, REAL & v ) const;
210 template <typename REAL>
211 void NormalizeTriangle( REAL & u, REAL & v ) const;
212
219 template <typename REAL>
220 void Unnormalize( REAL & u, REAL & v ) const;
221 template <typename REAL>
222 void UnnormalizeTriangle( REAL & u, REAL & v ) const;
223
225 bool IsTriangleRotated() const;
226
228 bool IsRegular() const { return (unpack(field1,1,5) != 0); }
229
230 unsigned int field0:32;
231 unsigned int field1:32;
232
233private:
234 unsigned int pack(unsigned int value, int width, int offset) const {
235 return (unsigned int)((value & ((1<<width)-1)) << offset);
236 }
237
238 unsigned int unpack(unsigned int value, int width, int offset) const {
239 return (unsigned int)((value >> offset) & ((1<<width)-1));
240 }
241};
242
243 typedef std::vector<PatchParam> PatchParamTable;
244
245 typedef Vtr::Array<PatchParam> PatchParamArray;
246 typedef Vtr::ConstArray<PatchParam> ConstPatchParamArray;
247
248inline void
249PatchParam::Set(Index faceid, short u, short v,
250 unsigned short depth, bool nonquad,
251 unsigned short boundary, unsigned short transition,
252 bool regular) {
253 field0 = pack(faceid, 28, 0) |
254 pack(transition, 4, 28);
255
256 field1 = pack(u, 10, 22) |
257 pack(v, 10, 12) |
258 pack(boundary, 5, 7) |
259 pack(regular, 1, 5) |
260 pack(nonquad, 1, 4) |
261 pack(depth, 4, 0);
262}
263
264inline float
265PatchParam::GetParamFraction( ) const {
266 return 1.0f / (float)(1 << (GetDepth() - NonQuadRoot()));
267}
268
269template <typename REAL>
270inline void
271PatchParam::Normalize( REAL & u, REAL & v ) const {
272
273 REAL fracInv = (REAL)(1.0f / GetParamFraction());
274
275 u = u * fracInv - (REAL)GetU();
276 v = v * fracInv - (REAL)GetV();
277}
278
279template <typename REAL>
280inline void
281PatchParam::Unnormalize( REAL & u, REAL & v ) const {
282
283 REAL frac = (REAL)GetParamFraction();
284
285 u = (u + (REAL)GetU()) * frac;
286 v = (v + (REAL)GetV()) * frac;
287}
288
289inline bool
290PatchParam::IsTriangleRotated() const {
291
292 return (GetU() + GetV()) >= (1 << GetDepth());
293}
294
295template <typename REAL>
296inline void
297PatchParam::NormalizeTriangle( REAL & u, REAL & v ) const {
298
299 if (IsTriangleRotated()) {
300 REAL fracInv = (REAL)(1.0f / GetParamFraction());
301
302 int depthFactor = 1 << GetDepth();
303 u = (REAL)(depthFactor - GetU()) - (u * fracInv);
304 v = (REAL)(depthFactor - GetV()) - (v * fracInv);
305 } else {
306 Normalize(u, v);
307 }
308}
309
310template <typename REAL>
311inline void
312PatchParam::UnnormalizeTriangle( REAL & u, REAL & v ) const {
313
314 if (IsTriangleRotated()) {
315 REAL frac = GetParamFraction();
316
317 int depthFactor = 1 << GetDepth();
318 u = ((REAL)(depthFactor - GetU()) - u) * frac;
319 v = ((REAL)(depthFactor - GetV()) - v) * frac;
320 } else {
321 Unnormalize(u, v);
322 }
323}
324
325} // end namespace Far
326
327} // end namespace OPENSUBDIV_VERSION
328using namespace OPENSUBDIV_VERSION;
329
330} // end namespace OpenSubdiv
331
332#endif /* OPENSUBDIV3_FAR_PATCH_PARAM */
OpenSubdiv
Definition limits.h:32
OpenSubdiv::OPENSUBDIV_VERSION::Far::ConstPatchParamArray
Vtr::ConstArray< PatchParam > ConstPatchParamArray
Definition patchParam.h:246
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParamTable
std::vector< PatchParam > PatchParamTable
Definition patchParam.h:243
OpenSubdiv::OPENSUBDIV_VERSION::Far::Index
Vtr::Index Index
Definition types.h:41
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParamArray
Vtr::Array< PatchParam > PatchParamArray
Definition patchParam.h:245
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam
Patch parameterization.
Definition patchParam.h:152
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::IsTriangleRotated
bool IsTriangleRotated() const
Returns if a triangular patch is parametrically rotated 180 degrees.
Definition patchParam.h:290
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::IsRegular
bool IsRegular() const
Returns whether the patch is regular.
Definition patchParam.h:228
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::NormalizeTriangle
void NormalizeTriangle(REAL &u, REAL &v) const
Definition patchParam.h:297
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::GetBoundary
unsigned short GetBoundary() const
Returns the boundary edge encoding for the patch.
Definition patchParam.h:191
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::GetTransition
unsigned short GetTransition() const
Returns the transition edge encoding for the patch.
Definition patchParam.h:188
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::GetParamFraction
float GetParamFraction() const
Returns the fraction of unit parametric space covered by this face.
Definition patchParam.h:265
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:181
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::GetFaceId
Index GetFaceId() const
Returns the faceid.
Definition patchParam.h:177
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::GetDepth
unsigned short GetDepth() const
Returns the level of subdivision of the patch.
Definition patchParam.h:197
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::NonQuadRoot
bool NonQuadRoot() const
True if the parent base face is a non-quad.
Definition patchParam.h:194
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:249
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::Clear
void Clear()
Resets everything to 0.
Definition patchParam.h:174
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::field1
unsigned int field1
Definition patchParam.h:231
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:281
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::UnnormalizeTriangle
void UnnormalizeTriangle(REAL &u, REAL &v) const
Definition patchParam.h:312
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:185
OpenSubdiv::OPENSUBDIV_VERSION::Far::PatchParam::field0
unsigned int field0
Definition patchParam.h:230
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:271
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::ConstArray
Definition array.h:53
OpenSubdiv::OPENSUBDIV_VERSION::Vtr::Array
Definition array.h:105
types.h