https://github.com/PixarAnimationStudios/OpenSubdiv/blob/release/tutorials/far/tutorial_5_3/far_tutorial_5_3.cpp
using namespace OpenSubdiv;
using Far::Index;
namespace {
struct Pos {
Pos() { }
Pos(float x, float y, float z) { p[0] = x, p[1] = y, p[2] = z; }
Pos operator+(Pos const & op) const {
return Pos(p[0] + op.p[0], p[1] + op.p[1], p[2] + op.p[2]);
}
void Clear( void * =0 ) { p[0] = p[1] = p[2] = 0.0f; }
void AddWithWeight(Pos const & src, float weight) {
p[0] += weight * src.p[0];
p[1] += weight * src.p[1];
p[2] += weight * src.p[2];
}
float p[3];
};
typedef std::vector<Pos> PosVector;
struct Tri {
Tri() { }
Tri(int a, int b, int c) { v[0] = a, v[1] = b, v[2] = c; }
int v[3];
};
typedef std::vector<Tri> TriVector;
void
createCube(std::vector<int> & vertsPerFace,
std::vector<Index> & faceVertsPerFace,
std::vector<Pos> & positionsPerVert) {
static float const cubePositions[8][3] = { { -0.5f, -0.5f, -0.5f },
{ -0.5f, 0.5f, -0.5f },
{ -0.5f, 0.5f, 0.5f },
{ -0.5f, -0.5f, 0.5f },
{ 0.5f, -0.5f, -0.5f },
{ 0.5f, 0.5f, -0.5f },
{ 0.5f, 0.5f, 0.5f },
{ 0.5f, -0.5f, 0.5f } };
static int const cubeFaceVerts[6][4] = { { 0, 3, 2, 1 },
{ 4, 5, 6, 7 },
{ 0, 4, 7, 3 },
{ 1, 2, 6, 5 },
{ 0, 1, 5, 4 },
{ 3, 7, 6, 2 } };
vertsPerFace.resize(6);
faceVertsPerFace.resize(24);
for (int i = 0; i < 6; ++i) {
vertsPerFace[i] = 4;
for (int j = 0; j < 4; ++j) {
faceVertsPerFace[i*4+j] = cubeFaceVerts[i][j];
}
}
positionsPerVert.resize(8);
for (int i = 0; i < 8; ++i) {
float const * p = cubePositions[i];
positionsPerVert[i] = Pos(p[0], p[1], p[2]);
}
}
Far::TopologyRefiner *
createTopologyRefinerDefault(PosVector & posVector) {
std::vector<int> topVertsPerFace;
std::vector<Index> topFaceVerts;
createCube(topVertsPerFace, topFaceVerts, posVector);
typedef Far::TopologyDescriptor Descriptor;
Sdc::SchemeType type = OpenSubdiv::Sdc::SCHEME_CATMARK;
Sdc::Options options;
options.SetVtxBoundaryInterpolation(
Sdc::Options::VTX_BOUNDARY_EDGE_AND_CORNER);
Descriptor desc;
desc.numVertices = (int) posVector.size();
desc.numFaces = (int) topVertsPerFace.size();
desc.numVertsPerFace = &topVertsPerFace[0];
desc.vertIndicesPerFace = &topFaceVerts[0];
Far::TopologyRefiner * refiner =
Far::TopologyRefinerFactory<Descriptor>::Create(desc,
Far::TopologyRefinerFactory<Descriptor>::Options(type,options));
assert(refiner);
return refiner;
}
Far::TopologyRefiner *
createTopologyRefinerFromObj(std::string const & objFileName,
Sdc::SchemeType schemeType,
PosVector & posVector) {
const char * filename = objFileName.c_str();
const Shape * shape = 0;
std::ifstream ifs(filename);
if (ifs) {
std::stringstream ss;
ss << ifs.rdbuf();
ifs.close();
std::string shapeString = ss.str();
shape = Shape::parseObj(shapeString.c_str(),
ConvertSdcTypeToShapeScheme(schemeType), false);
if (shape == 0) {
fprintf(stderr,
"Error: Cannot create Shape from .obj file '%s'\n",
filename);
return 0;
}
} else {
fprintf(stderr, "Error: Cannot open .obj file '%s'\n", filename);
return 0;
}
Sdc::SchemeType sdcType = GetSdcType(*shape);
Sdc::Options sdcOptions = GetSdcOptions(*shape);
Far::TopologyRefiner * refiner =
Far::TopologyRefinerFactory<Shape>::Create(*shape,
Far::TopologyRefinerFactory<Shape>::Options(
sdcType, sdcOptions));
if (refiner == 0) {
fprintf(stderr, "Error: Unable to construct TopologyRefiner "
"from .obj file '%s'\n", filename);
return 0;
}
int numVertices = refiner->GetNumVerticesTotal();
posVector.resize(numVertices);
std::memcpy(&posVector[0].p[0], &shape->verts[0],
numVertices * 3 * sizeof(float));
delete shape;
return refiner;
}
int writeToObj(
Far::TopologyLevel const & baseLevel,
std::vector<Pos> const & vertexPositions,
int nextObjVertexIndex) {
for (size_t i = 0; i < vertexPositions.size(); ++i) {
float const * p = vertexPositions[i].p;
printf("v %f %f %f\n", p[0], p[1], p[2]);
}
for (int i = 0; i < baseLevel.GetNumFaces(); ++i) {
int faceSize = baseLevel.GetFaceVertices(i).size();
int vCenter = nextObjVertexIndex + 1;
int vCorner = vCenter + 1;
for (int k = 0; k < faceSize; ++k) {
printf("f %d %d %d\n",
vCenter, vCorner + k, vCorner + ((k + 1) % faceSize));
}
nextObjVertexIndex += faceSize + 1;
}
return nextObjVertexIndex;
}
}
class Args {
public:
std::string inputObjFile;
Sdc::SchemeType schemeType;
int maxPatchDepth;
int numPoses;
Pos poseOffset;
bool deriv1Flag;
bool noPatchesFlag;
bool noOutputFlag;
public:
Args(int argc, char ** argv) :
inputObjFile(),
schemeType(Sdc::SCHEME_CATMARK),
maxPatchDepth(3),
numPoses(0),
poseOffset(1.0f, 0.0f, 0.0f),
deriv1Flag(false),
noPatchesFlag(false),
noOutputFlag(false) {
ArgOptions args;
args.Parse(argc, argv);
maxPatchDepth = args.GetLevel();
schemeType = ConvertShapeSchemeToSdcType(args.GetDefaultScheme());
const std::vector<const char *> objFiles = args.GetObjFiles();
if (!objFiles.empty()) {
for (size_t i = 1; i < objFiles.size(); ++i) {
fprintf(stderr,
"Warning: .obj file '%s' ignored\n", objFiles[i]);
}
inputObjFile = std::string(objFiles[0]);
}
const std::vector<const char *> &rargs = args.GetRemainingArgs();
for (size_t i = 0; i < rargs.size(); ++i) {
if (!strcmp(rargs[i], "-d1")) {
deriv1Flag = true;
} else if (!strcmp(rargs[i], "-nopatches")) {
noPatchesFlag = true;
} else if (!strcmp(rargs[i], "-poses")) {
if (++i < rargs.size()) numPoses = atoi(rargs[i]);
} else if (!strcmp(rargs[i], "-offset")) {
if (++i < rargs.size()) poseOffset.p[0] = (float)atof(rargs[i]);
if (++i < rargs.size()) poseOffset.p[1] = (float)atof(rargs[i]);
if (++i < rargs.size()) poseOffset.p[2] = (float)atof(rargs[i]);
} else if (!strcmp(rargs[i], "-nooutput")) {
noOutputFlag = true;
} else {
fprintf(stderr, "Warning: Argument '%s' ignored\n", rargs[i]);
}
}
}
private:
Args() { }
};
typedef Far::LimitStencilTableFactory::LocationArray LocationArray;
int assembleLimitPointLocations(Far::TopologyRefiner const & refiner,
std::vector<LocationArray> & locations) {
static float const quadSCoords[5] = { 0.5f, 0.05f, 0.95f, 0.95f, 0.05f };
static float const quadTCoords[5] = { 0.5f, 0.05f, 0.05f, 0.95f, 0.95f };
static float const triSCoords[4] = { 0.33f, 0.05f, 0.95f, 0.05f };
static float const triTCoords[4] = { 0.33f, 0.05f, 0.00f, 0.95f };
static float const irregSCoords[2] = { 1.0f, 0.05f };
static float const irregTCoords[2] = { 1.0f, 0.05f };
Far::TopologyLevel const & baseLevel = refiner.GetLevel(0);
Far::PtexIndices basePtexIndices(refiner);
int regFaceSize = Sdc::SchemeTypeTraits::GetRegularFaceSize(
refiner.GetSchemeType());
locations.clear();
int numLimitPoints = 0;
for (int i = 0; i < baseLevel.GetNumFaces(); ++i) {
int baseFaceSize = baseLevel.GetFaceVertices(i).size();
int basePtexId = basePtexIndices.GetFaceId(i);
bool faceIsRegular = (baseFaceSize == regFaceSize);
if (faceIsRegular) {
LocationArray loc;
loc.ptexIdx = basePtexId;
loc.numLocations = baseFaceSize + 1;
if (baseFaceSize == 4) {
loc.s = quadSCoords;
loc.t = quadTCoords;
} else {
loc.s = triSCoords;
loc.t = triTCoords;
}
locations.push_back(loc);
} else {
LocationArray loc;
loc.numLocations = 1;
for (int j = 0; j <= baseFaceSize; ++j) {
bool isPerimeter = (j > 0);
loc.ptexIdx = basePtexId + (isPerimeter ? (j-1) : 0);
loc.s = &irregSCoords[isPerimeter];
loc.t = &irregTCoords[isPerimeter];
locations.push_back(loc);
}
}
numLimitPoints += baseFaceSize + 1;
}
return numLimitPoints;
}
int
main(int argc, char **argv) {
Args args(argc, argv);
std::vector<Pos> basePositions;
Far::TopologyRefiner * refinerPtr = args.inputObjFile.empty() ?
createTopologyRefinerDefault(basePositions) :
createTopologyRefinerFromObj(args.inputObjFile, args.schemeType,
basePositions);
assert(refinerPtr);
Far::TopologyRefiner & refiner = *refinerPtr;
Far::TopologyLevel const & baseLevel = refiner.GetLevel(0);
Far::PatchTable * patchTablePtr = 0;
if (args.noPatchesFlag) {
refiner.RefineAdaptive(
Far::TopologyRefiner::AdaptiveOptions(args.maxPatchDepth));
} else {
Far::PatchTableFactory::Options patchOptions(args.maxPatchDepth);
patchOptions.useInfSharpPatch = true;
patchOptions.generateLegacySharpCornerPatches = false;
patchOptions.generateVaryingTables = false;
patchOptions.generateFVarTables = false;
patchOptions.endCapType =
Far::PatchTableFactory::Options::ENDCAP_GREGORY_BASIS;
refiner.RefineAdaptive(patchOptions.GetRefineAdaptiveOptions());
patchTablePtr = Far::PatchTableFactory::Create(refiner, patchOptions);
assert(patchTablePtr);
}
std::vector<LocationArray> locations;
int numLimitPoints = assembleLimitPointLocations(refiner, locations);
Far::LimitStencilTableFactory::Options limitOptions;
limitOptions.generate1stDerivatives = args.deriv1Flag;
Far::LimitStencilTable const * limitStencilTablePtr =
Far::LimitStencilTableFactory::Create(refiner, locations,
0, patchTablePtr, limitOptions);
assert(limitStencilTablePtr);
Far::LimitStencilTable const & limitStencilTable = *limitStencilTablePtr;
std::vector<Pos> limitPositions(numLimitPoints);
limitStencilTable.UpdateValues(basePositions, limitPositions);
limitStencilTable.UpdateValues(basePositions, limitPositions,
0, numLimitPoints / 2);
limitStencilTable.UpdateValues(basePositions, limitPositions,
(numLimitPoints / 2) + 1, numLimitPoints);
int objVertCount = 0;
if (!args.noOutputFlag) {
printf("g base_mesh\n");
objVertCount = writeToObj(baseLevel, limitPositions, objVertCount);
}
std::vector<Pos> posePositions(basePositions);
std::vector<Pos> limitDu(args.deriv1Flag ? numLimitPoints : 0);
std::vector<Pos> limitDv(args.deriv1Flag ? numLimitPoints : 0);
for (int i = 0; i < args.numPoses; ++i) {
for (size_t j = 0; j < basePositions.size(); ++j) {
posePositions[j] = posePositions[j] + args.poseOffset;
}
limitStencilTable.UpdateValues(posePositions, limitPositions);
if (args.deriv1Flag) {
limitStencilTable.UpdateDerivs(posePositions, limitDu, limitDv);
}
if (!args.noOutputFlag) {
printf("\ng pose_%d\n", i);
objVertCount = writeToObj(baseLevel, limitPositions, objVertCount);
}
}
delete refinerPtr;
delete patchTablePtr;
delete limitStencilTablePtr;
return EXIT_SUCCESS;
}