OpenSubdiv/opensubdiv/hbr/fvarData.h

224 lines
7.4 KiB
C
Raw Normal View History

2012-06-08 18:18:20 +00:00
//
// Copyright (C) Pixar. All rights reserved.
//
// This license governs use of the accompanying software. If you
// use the software, you accept this license. If you do not accept
// the license, do not use the software.
//
// 1. Definitions
// The terms "reproduce," "reproduction," "derivative works," and
// "distribution" have the same meaning here as under U.S.
// copyright law. A "contribution" is the original software, or
// any additions or changes to the software.
// A "contributor" is any person or entity that distributes its
// contribution under this license.
// "Licensed patents" are a contributor's patent claims that read
// directly on its contribution.
//
// 2. Grant of Rights
// (A) Copyright Grant- Subject to the terms of this license,
// including the license conditions and limitations in section 3,
// each contributor grants you a non-exclusive, worldwide,
// royalty-free copyright license to reproduce its contribution,
// prepare derivative works of its contribution, and distribute
// its contribution or any derivative works that you create.
// (B) Patent Grant- Subject to the terms of this license,
// including the license conditions and limitations in section 3,
// each contributor grants you a non-exclusive, worldwide,
// royalty-free license under its licensed patents to make, have
// made, use, sell, offer for sale, import, and/or otherwise
// dispose of its contribution in the software or derivative works
// of the contribution in the software.
//
// 3. Conditions and Limitations
// (A) No Trademark License- This license does not grant you
// rights to use any contributor's name, logo, or trademarks.
// (B) If you bring a patent claim against any contributor over
// patents that you claim are infringed by the software, your
// patent license from such contributor to the software ends
// automatically.
// (C) If you distribute any portion of the software, you must
// retain all copyright, patent, trademark, and attribution
// notices that are present in the software.
// (D) If you distribute any portion of the software in source
// code form, you may do so only under this license by including a
// complete copy of this license with your distribution. If you
// distribute any portion of the software in compiled or object
// code form, you may only do so under a license that complies
// with this license.
// (E) The software is licensed "as-is." You bear the risk of
// using it. The contributors give no express warranties,
// guarantees or conditions. You may have additional consumer
// rights under your local laws which this license cannot change.
// To the extent permitted under your local laws, the contributors
// exclude the implied warranties of merchantability, fitness for
// a particular purpose and non-infringement.
//
#ifndef HBRFVARDATA_H
#define HBRFVARDATA_H
#include <cstring>
2012-06-08 18:18:20 +00:00
#include <cmath>
#include "../version.h"
namespace OpenSubdiv {
namespace OPENSUBDIV_VERSION {
2012-06-08 18:18:20 +00:00
template <class T> class HbrFVarEdit;
template <class T> class HbrFace;
// This class implements a "face varying vector item". Really it's
// just a smart wrapper around face varying data (itself just a bunch
// of floats) stored on each vertex.
template <class T> class HbrFVarData {
public:
HbrFVarData(float *dataptr)
: initialized(false), face(0), data(dataptr) {
2012-06-08 18:18:20 +00:00
}
~HbrFVarData() {
Uninitialize();
}
// Sets the face pointer
void SetFace(const HbrFace<T> *_face) {
face = _face;
}
// Returns the face pointer
const HbrFace<T> * GetFace() const {
return face;
}
2012-06-08 18:18:20 +00:00
// Clears the initialized flag
void Uninitialize() {
initialized = false;
face = 0;
}
// Returns initialized flag
bool IsInitialized() const {
return initialized;
}
// Sets initialized flag
void SetInitialized() {
initialized = true;
}
2012-06-08 18:18:20 +00:00
// Return the data from the NgpFVVector
float* GetData(int item) const { return &data[item]; }
// Clears the indicates value of this item
void Clear(int startindex, int width) {
memset(data + startindex, 0, width * sizeof(float));
}
// Clears all values of this item
2012-06-08 18:18:20 +00:00
void ClearAll(int width) {
initialized = true;
memset(data, 0, width * sizeof(float));
}
// Set values of the indicated item (with the indicated weighing)
// on this item
void SetWithWeight(const HbrFVarData& fvvi, int startindex, int width, float weight) {
float *dst = data + startindex, *src = fvvi.data + startindex;
for (int i = 0; i < width; ++i) {
*dst++ = weight * *src++;
}
2012-06-08 18:18:20 +00:00
}
2012-06-08 18:18:20 +00:00
// Add values of the indicated item (with the indicated weighing)
// to this item
void AddWithWeight(const HbrFVarData& fvvi, int startindex, int width, float weight) {
float *dst = data + startindex, *src = fvvi.data + startindex;
for (int i = 0; i < width; ++i) {
*dst++ += weight * *src++;
}
2012-06-08 18:18:20 +00:00
}
// Add all values of the indicated item (with the indicated
// weighing) to this item
void AddWithWeightAll(const HbrFVarData& fvvi, int width, float weight) {
float *dst = data, *src = fvvi.data;
for (int i = 0; i < width; ++i) {
*dst++ += weight * *src++;
}
2012-06-08 18:18:20 +00:00
}
// Compare all values item against a float buffer. Returns true
// if all values match
bool CompareAll(int width, const float *values, float tolerance=0.0f) const {
if (!initialized) return false;
for (int i = 0; i < width; ++i) {
if (fabsf(values[i] - data[i]) > tolerance) return false;
}
return true;
}
// Initializes data
void SetAllData(int width, const float *values) {
initialized = true;
memcpy(data, values, width * sizeof(float));
}
2012-06-08 18:18:20 +00:00
// Compare this item against another item with tolerance. Returns
// true if it compares identical
bool Compare(const HbrFVarData& fvvi, int startindex, int width, float tolerance=0.0f) const {
for (int i = 0; i < width; ++i) {
if (fabsf(data[startindex + i] - fvvi.data[startindex + i]) > tolerance) return false;
}
return true;
}
// Modify the data of the item with an edit
void ApplyFVarEdit(const HbrFVarEdit<T>& edit);
2012-06-08 18:18:20 +00:00
private:
bool initialized;
const HbrFace<T> *face;
float* const data;
};
} // end namespace OPENSUBDIV_VERSION
using namespace OPENSUBDIV_VERSION;
} // end namespace OpenSubdiv
2012-06-08 18:18:20 +00:00
#include "../hbr/fvarEdit.h"
namespace OpenSubdiv {
namespace OPENSUBDIV_VERSION {
2012-06-08 18:18:20 +00:00
template <class T>
void
HbrFVarData<T>::ApplyFVarEdit(const HbrFVarEdit<T>& edit) {
float *dst = data + edit.GetIndex() + edit.GetOffset();
const float *src = edit.GetEdit();
for (int i = 0; i < edit.GetWidth(); ++i) {
switch(edit.GetOperation()) {
case HbrVertexEdit<T>::Set:
*dst++ = *src++;
break;
case HbrVertexEdit<T>::Add:
*dst++ += *src++;
break;
case HbrVertexEdit<T>::Subtract:
*dst++ -= *src++;
}
}
2012-06-08 18:18:20 +00:00
initialized = true;
}
} // end namespace OPENSUBDIV_VERSION
using namespace OPENSUBDIV_VERSION;
} // end namespace OpenSubdiv
2012-06-08 18:18:20 +00:00
#endif /* HBRFVARDATA_H */