2014-09-05 22:07:46 +00:00
|
|
|
//
|
|
|
|
// Copyright 2013 Pixar
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
|
|
|
// with the following modification; you may not use this file except in
|
|
|
|
// compliance with the Apache License and the following modification to it:
|
|
|
|
// Section 6. Trademarks. is deleted and replaced with:
|
|
|
|
//
|
|
|
|
// 6. Trademarks. This License does not grant permission to use the trade
|
|
|
|
// names, trademarks, service marks, or product names of the Licensor
|
|
|
|
// and its affiliates, except as required to comply with Section 4(c) of
|
|
|
|
// the License and to reproduce the content of the NOTICE file.
|
|
|
|
//
|
|
|
|
// You may obtain a copy of the Apache License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the Apache License with the above modification is
|
|
|
|
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
// KIND, either express or implied. See the Apache License for the specific
|
|
|
|
// language governing permissions and limitations under the Apache License.
|
|
|
|
//
|
|
|
|
|
2015-05-31 05:10:07 +00:00
|
|
|
#ifndef FAR_UTILS_H
|
|
|
|
#define FAR_UTILS_H
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2018-10-09 17:51:31 +00:00
|
|
|
#include "shape_utils.h"
|
|
|
|
|
|
|
|
#include <opensubdiv/far/topologyRefinerFactory.h>
|
|
|
|
#include <opensubdiv/far/primvarRefiner.h>
|
|
|
|
#include <opensubdiv/far/types.h>
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2015-06-01 07:14:19 +00:00
|
|
|
#include <cstdio>
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2018-10-19 20:05:23 +00:00
|
|
|
inline Scheme
|
|
|
|
ConvertSdcTypeToShapeScheme(OpenSubdiv::Sdc::SchemeType sdcScheme) {
|
|
|
|
|
|
|
|
switch (sdcScheme) {
|
|
|
|
case OpenSubdiv::Sdc::SCHEME_BILINEAR: return kBilinear;
|
|
|
|
case OpenSubdiv::Sdc::SCHEME_CATMARK: return kCatmark;
|
|
|
|
case OpenSubdiv::Sdc::SCHEME_LOOP: return kLoop;
|
|
|
|
default: printf("unknown Sdc::SchemeType : %d\n", (int)sdcScheme); break;
|
|
|
|
}
|
|
|
|
return kCatmark;
|
|
|
|
}
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2018-10-19 20:05:23 +00:00
|
|
|
inline OpenSubdiv::Sdc::SchemeType
|
|
|
|
ConvertShapeSchemeToSdcType(Scheme shapeScheme) {
|
2014-09-05 22:07:46 +00:00
|
|
|
|
2018-10-19 20:05:23 +00:00
|
|
|
switch (shapeScheme) {
|
|
|
|
case kBilinear: return OpenSubdiv::Sdc::SCHEME_BILINEAR;
|
|
|
|
case kCatmark: return OpenSubdiv::Sdc::SCHEME_CATMARK;
|
|
|
|
case kLoop: return OpenSubdiv::Sdc::SCHEME_LOOP;
|
|
|
|
default: printf("unknown Shape Scheme : %d\n", (int)shapeScheme); break;
|
2014-09-05 22:07:46 +00:00
|
|
|
}
|
2018-10-19 20:05:23 +00:00
|
|
|
return OpenSubdiv::Sdc::SCHEME_CATMARK;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline OpenSubdiv::Sdc::SchemeType
|
|
|
|
GetSdcType(Shape const & shape) {
|
|
|
|
|
|
|
|
return ConvertShapeSchemeToSdcType(shape.scheme);
|
2014-09-05 22:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline OpenSubdiv::Sdc::Options
|
|
|
|
GetSdcOptions(Shape const & shape) {
|
|
|
|
|
|
|
|
typedef OpenSubdiv::Sdc::Options Options;
|
|
|
|
|
|
|
|
Options result;
|
|
|
|
|
2014-12-12 19:10:17 +00:00
|
|
|
result.SetVtxBoundaryInterpolation(Options::VTX_BOUNDARY_EDGE_ONLY);
|
2014-09-05 22:07:46 +00:00
|
|
|
result.SetCreasingMethod(Options::CREASE_UNIFORM);
|
2014-12-11 17:19:41 +00:00
|
|
|
result.SetTriangleSubdivision(Options::TRI_SUB_CATMARK);
|
2014-09-05 22:07:46 +00:00
|
|
|
|
|
|
|
for (int i=0; i<(int)shape.tags.size(); ++i) {
|
|
|
|
|
|
|
|
Shape::tag * t = shape.tags[i];
|
|
|
|
|
|
|
|
if (t->name=="interpolateboundary") {
|
|
|
|
if ((int)t->intargs.size()!=1) {
|
|
|
|
printf("expecting 1 integer for \"interpolateboundary\" tag n. %d\n", i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch( t->intargs[0] ) {
|
2014-12-12 19:10:17 +00:00
|
|
|
case 0 : result.SetVtxBoundaryInterpolation(Options::VTX_BOUNDARY_NONE); break;
|
|
|
|
case 1 : result.SetVtxBoundaryInterpolation(Options::VTX_BOUNDARY_EDGE_AND_CORNER); break;
|
|
|
|
case 2 : result.SetVtxBoundaryInterpolation(Options::VTX_BOUNDARY_EDGE_ONLY); break;
|
2014-09-05 22:07:46 +00:00
|
|
|
default: printf("unknown interpolate boundary : %d\n", t->intargs[0] ); break;
|
|
|
|
}
|
|
|
|
} else if (t->name=="facevaryinginterpolateboundary") {
|
|
|
|
if ((int)t->intargs.size()!=1) {
|
|
|
|
printf("expecting 1 integer for \"facevaryinginterpolateboundary\" tag n. %d\n", i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch( t->intargs[0] ) {
|
2014-10-10 18:57:43 +00:00
|
|
|
case 0 : result.SetFVarLinearInterpolation(Options::FVAR_LINEAR_NONE); break;
|
|
|
|
case 1 : result.SetFVarLinearInterpolation(Options::FVAR_LINEAR_CORNERS_ONLY); break;
|
|
|
|
case 2 : result.SetFVarLinearInterpolation(Options::FVAR_LINEAR_CORNERS_PLUS1); break;
|
|
|
|
case 3 : result.SetFVarLinearInterpolation(Options::FVAR_LINEAR_CORNERS_PLUS2); break;
|
|
|
|
case 4 : result.SetFVarLinearInterpolation(Options::FVAR_LINEAR_BOUNDARIES); break;
|
|
|
|
case 5 : result.SetFVarLinearInterpolation(Options::FVAR_LINEAR_ALL); break;
|
2014-09-05 22:07:46 +00:00
|
|
|
default: printf("unknown interpolate boundary : %d\n", t->intargs[0] ); break;
|
|
|
|
}
|
|
|
|
} else if (t->name=="facevaryingpropagatecorners") {
|
|
|
|
if ((int)t->intargs.size()==1) {
|
|
|
|
// XXXX no propagate corners in Options
|
|
|
|
assert(0);
|
|
|
|
} else
|
|
|
|
printf( "expecting single int argument for \"facevaryingpropagatecorners\"\n" );
|
|
|
|
} else if (t->name=="creasemethod") {
|
|
|
|
|
|
|
|
if ((int)t->stringargs.size()==0) {
|
|
|
|
printf("the \"creasemethod\" tag expects a string argument\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-12-11 17:19:41 +00:00
|
|
|
if (t->stringargs[0]=="normal") {
|
2014-09-05 22:07:46 +00:00
|
|
|
result.SetCreasingMethod(Options::CREASE_UNIFORM);
|
2014-12-11 17:19:41 +00:00
|
|
|
} else if (t->stringargs[0]=="chaikin") {
|
2014-09-05 22:07:46 +00:00
|
|
|
result.SetCreasingMethod(Options::CREASE_CHAIKIN);
|
2014-12-11 17:19:41 +00:00
|
|
|
} else {
|
2014-09-05 22:07:46 +00:00
|
|
|
printf("the \"creasemethod\" tag only accepts \"normal\" or \"chaikin\" as value (%s)\n", t->stringargs[0].c_str());
|
2014-12-11 17:19:41 +00:00
|
|
|
}
|
|
|
|
} else if (t->name=="smoothtriangles") {
|
|
|
|
|
|
|
|
if (shape.scheme!=kCatmark) {
|
|
|
|
printf("the \"smoothtriangles\" tag can only be applied to Catmark meshes\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (t->stringargs[0]=="catmark") {
|
|
|
|
result.SetTriangleSubdivision(Options::TRI_SUB_CATMARK);
|
|
|
|
} else if (t->stringargs[0]=="smooth") {
|
|
|
|
result.SetTriangleSubdivision(Options::TRI_SUB_SMOOTH);
|
|
|
|
} else {
|
|
|
|
printf("the \"smoothtriangles\" tag only accepts \"catmark\" or \"smooth\" as value (%s)\n", t->stringargs[0].c_str());
|
|
|
|
}
|
2014-09-05 22:07:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-03-28 18:13:53 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2014-09-05 22:07:46 +00:00
|
|
|
void
|
|
|
|
InterpolateFVarData(OpenSubdiv::Far::TopologyRefiner & refiner,
|
|
|
|
Shape const & shape, std::vector<float> & fvarData);
|
2015-03-28 18:13:53 +00:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
OpenSubdiv::Far::TopologyRefiner *
|
2016-04-05 02:49:27 +00:00
|
|
|
InterpolateFarVertexData(Shape const & shape, int maxlevel, std::vector<T> &data) {
|
2015-03-28 18:13:53 +00:00
|
|
|
|
|
|
|
typedef OpenSubdiv::Far::TopologyRefiner FarTopologyRefiner;
|
|
|
|
typedef OpenSubdiv::Far::TopologyRefinerFactory<Shape> FarTopologyRefinerFactory;
|
|
|
|
|
2015-05-31 05:10:07 +00:00
|
|
|
// Far interpolation
|
2015-03-28 18:13:53 +00:00
|
|
|
FarTopologyRefiner * refiner =
|
2016-04-05 02:49:27 +00:00
|
|
|
FarTopologyRefinerFactory::Create(shape,
|
2015-03-28 18:13:53 +00:00
|
|
|
FarTopologyRefinerFactory::Options(
|
2016-04-05 02:49:27 +00:00
|
|
|
GetSdcType(shape), GetSdcOptions(shape)));
|
2015-03-28 18:13:53 +00:00
|
|
|
assert(refiner);
|
|
|
|
|
|
|
|
FarTopologyRefiner::UniformOptions options(maxlevel);
|
|
|
|
options.fullTopologyInLastLevel=true;
|
|
|
|
refiner->RefineUniform(options);
|
|
|
|
|
|
|
|
// populate coarse mesh positions
|
|
|
|
data.resize(refiner->GetNumVerticesTotal());
|
2015-05-22 02:52:25 +00:00
|
|
|
for (int i=0; i<refiner->GetLevel(0).GetNumVertices(); i++) {
|
2016-04-05 02:49:27 +00:00
|
|
|
data[i].SetPosition(shape.verts[i*3+0],
|
|
|
|
shape.verts[i*3+1],
|
|
|
|
shape.verts[i*3+2]);
|
2015-03-28 18:13:53 +00:00
|
|
|
}
|
|
|
|
|
2015-05-30 05:21:50 +00:00
|
|
|
T * srcVerts = &data[0];
|
|
|
|
T * dstVerts = srcVerts + refiner->GetLevel(0).GetNumVertices();
|
|
|
|
OpenSubdiv::Far::PrimvarRefiner primvarRefiner(*refiner);
|
|
|
|
|
|
|
|
for (int i = 1; i <= refiner->GetMaxLevel(); ++i) {
|
|
|
|
primvarRefiner.Interpolate(i, srcVerts, dstVerts);
|
|
|
|
srcVerts = dstVerts;
|
|
|
|
dstVerts += refiner->GetLevel(i).GetNumVertices();
|
|
|
|
}
|
2016-04-05 02:49:27 +00:00
|
|
|
return refiner;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
OpenSubdiv::Far::TopologyRefiner *
|
|
|
|
InterpolateFarVertexData(const char *shapeStr, Scheme scheme, int maxlevel,
|
|
|
|
std::vector<T> &data) {
|
|
|
|
|
|
|
|
Shape const * shape = Shape::parseObj(shapeStr, scheme);
|
|
|
|
|
|
|
|
OpenSubdiv::Far::TopologyRefiner * refiner =
|
|
|
|
InterpolateFarVertexData(*shape, maxlevel, data);
|
2015-03-28 18:13:53 +00:00
|
|
|
|
|
|
|
delete shape;
|
|
|
|
return refiner;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-04 04:21:06 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace OpenSubdiv {
|
|
|
|
namespace OPENSUBDIV_VERSION {
|
|
|
|
|
|
|
|
namespace Far {
|
|
|
|
|
|
|
|
template <>
|
2015-01-09 02:08:30 +00:00
|
|
|
inline bool
|
2014-12-04 04:21:06 +00:00
|
|
|
TopologyRefinerFactory<Shape>::resizeComponentTopology(
|
|
|
|
Far::TopologyRefiner & refiner, Shape const & shape) {
|
|
|
|
|
|
|
|
int nfaces = shape.GetNumFaces(),
|
|
|
|
nverts = shape.GetNumVertices();
|
|
|
|
|
2015-05-29 05:39:45 +00:00
|
|
|
setNumBaseFaces(refiner, nfaces);
|
2014-12-04 04:21:06 +00:00
|
|
|
for (int i=0; i<nfaces; ++i) {
|
|
|
|
|
|
|
|
int nv = shape.nvertsPerFace[i];
|
2015-05-29 05:39:45 +00:00
|
|
|
setNumBaseFaceVertices(refiner, i, nv);
|
2014-12-04 04:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Vertices and vert-faces and vert-edges
|
2015-05-29 05:39:45 +00:00
|
|
|
setNumBaseVertices(refiner, nverts);
|
2015-01-09 02:08:30 +00:00
|
|
|
|
|
|
|
return true;
|
2014-12-04 04:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------
|
|
|
|
template <>
|
2015-01-09 02:08:30 +00:00
|
|
|
inline bool
|
2014-12-04 04:21:06 +00:00
|
|
|
TopologyRefinerFactory<Shape>::assignComponentTopology(
|
|
|
|
Far::TopologyRefiner & refiner, Shape const & shape) {
|
|
|
|
|
|
|
|
{ // Face relations:
|
2015-05-29 05:39:45 +00:00
|
|
|
int nfaces = getNumBaseFaces(refiner);
|
2014-12-04 04:21:06 +00:00
|
|
|
|
|
|
|
for (int i=0, ofs=0; i < nfaces; ++i) {
|
|
|
|
|
2015-05-29 05:39:45 +00:00
|
|
|
Far::IndexArray dstFaceVerts = getBaseFaceVertices(refiner, i);
|
2014-12-04 04:21:06 +00:00
|
|
|
|
2015-04-15 21:32:48 +00:00
|
|
|
if (shape.isLeftHanded) {
|
2015-04-23 19:01:15 +00:00
|
|
|
dstFaceVerts[0] = shape.faceverts[ofs++];
|
|
|
|
for (int j=dstFaceVerts.size()-1; j>0; --j) {
|
2015-04-14 17:03:02 +00:00
|
|
|
dstFaceVerts[j] = shape.faceverts[ofs++];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int j=0; j<dstFaceVerts.size(); ++j) {
|
|
|
|
dstFaceVerts[j] = shape.faceverts[ofs++];
|
|
|
|
}
|
2014-12-04 04:21:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-09 02:08:30 +00:00
|
|
|
return true;
|
2014-12-04 04:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------
|
|
|
|
template <>
|
2015-01-09 02:08:30 +00:00
|
|
|
inline bool
|
2014-12-04 04:21:06 +00:00
|
|
|
TopologyRefinerFactory<Shape>::assignFaceVaryingTopology(
|
|
|
|
Far::TopologyRefiner & refiner, Shape const & shape) {
|
|
|
|
|
2015-08-12 00:11:55 +00:00
|
|
|
// UV layout (we only parse 1 channel)
|
2016-02-19 09:02:07 +00:00
|
|
|
if (! shape.faceuvs.empty()) {
|
2014-12-04 04:21:06 +00:00
|
|
|
|
2015-05-29 05:39:45 +00:00
|
|
|
int nfaces = getNumBaseFaces(refiner),
|
|
|
|
channel = createBaseFVarChannel(refiner, (int)shape.uvs.size()/2 );
|
2014-12-04 04:21:06 +00:00
|
|
|
|
|
|
|
for (int i=0, ofs=0; i < nfaces; ++i) {
|
|
|
|
|
2015-06-01 23:25:44 +00:00
|
|
|
Far::IndexArray dstFaceUVs = getBaseFaceFVarValues(refiner, i, channel);
|
2014-12-04 04:21:06 +00:00
|
|
|
|
2015-04-15 21:32:48 +00:00
|
|
|
if (shape.isLeftHanded) {
|
2015-04-23 19:01:15 +00:00
|
|
|
dstFaceUVs[0] = shape.faceuvs[ofs++];
|
|
|
|
for (int j=dstFaceUVs.size()-1; j > 0; --j) {
|
2015-04-14 17:03:02 +00:00
|
|
|
dstFaceUVs[j] = shape.faceuvs[ofs++];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int j=0; j<dstFaceUVs.size(); ++j) {
|
|
|
|
dstFaceUVs[j] = shape.faceuvs[ofs++];
|
|
|
|
}
|
2014-12-04 04:21:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-09 02:08:30 +00:00
|
|
|
return true;
|
2014-12-04 04:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------
|
|
|
|
template <>
|
2015-01-09 02:08:30 +00:00
|
|
|
inline bool
|
2014-12-04 04:21:06 +00:00
|
|
|
TopologyRefinerFactory<Shape>::assignComponentTags(
|
|
|
|
Far::TopologyRefiner & refiner, Shape const & shape) {
|
|
|
|
|
|
|
|
|
|
|
|
for (int i=0; i<(int)shape.tags.size(); ++i) {
|
|
|
|
|
|
|
|
Shape::tag * t = shape.tags[i];
|
|
|
|
|
|
|
|
if (t->name=="crease") {
|
|
|
|
|
|
|
|
for (int j=0; j<(int)t->intargs.size()-1; j += 2) {
|
|
|
|
|
2015-05-31 05:10:07 +00:00
|
|
|
OpenSubdiv::Far::Index edge = findBaseEdge(refiner, t->intargs[j], t->intargs[j+1]);
|
|
|
|
if (edge==OpenSubdiv::Far::INDEX_INVALID) {
|
2014-12-04 04:21:06 +00:00
|
|
|
printf("cannot find edge for crease tag (%d,%d)\n", t->intargs[j], t->intargs[j+1] );
|
2015-01-09 02:08:30 +00:00
|
|
|
return false;
|
2014-12-04 04:21:06 +00:00
|
|
|
} else {
|
|
|
|
int nfloat = (int) t->floatargs.size();
|
2015-05-29 05:39:45 +00:00
|
|
|
setBaseEdgeSharpness(refiner, edge,
|
2015-01-05 00:33:18 +00:00
|
|
|
std::max(0.0f, ((nfloat > 1) ? t->floatargs[j] : t->floatargs[0])));
|
2014-12-04 04:21:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (t->name=="corner") {
|
|
|
|
|
|
|
|
for (int j=0; j<(int)t->intargs.size(); ++j) {
|
|
|
|
int vertex = t->intargs[j];
|
2016-02-19 09:02:07 +00:00
|
|
|
if (vertex<0 || vertex>=getNumBaseVertices(refiner)) {
|
2014-12-04 04:21:06 +00:00
|
|
|
printf("cannot find vertex for corner tag (%d)\n", vertex );
|
2015-01-09 02:08:30 +00:00
|
|
|
return false;
|
2014-12-04 04:21:06 +00:00
|
|
|
} else {
|
|
|
|
int nfloat = (int) t->floatargs.size();
|
2015-05-29 05:39:45 +00:00
|
|
|
setBaseVertexSharpness(refiner, vertex,
|
2015-01-05 00:33:18 +00:00
|
|
|
std::max(0.0f, ((nfloat > 1) ? t->floatargs[j] : t->floatargs[0])));
|
2014-12-04 04:21:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{ // Hole tags
|
|
|
|
for (int i=0; i<(int)shape.tags.size(); ++i) {
|
|
|
|
Shape::tag * t = shape.tags[i];
|
|
|
|
if (t->name=="hole") {
|
|
|
|
for (int j=0; j<(int)t->intargs.size(); ++j) {
|
2015-05-29 05:39:45 +00:00
|
|
|
setBaseFaceHole(refiner, t->intargs[j], true);
|
2014-12-04 04:21:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-09 02:08:30 +00:00
|
|
|
return true;
|
2014-12-04 04:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline void
|
2014-12-09 01:24:54 +00:00
|
|
|
TopologyRefinerFactory<Shape>::reportInvalidTopology(
|
|
|
|
TopologyRefinerFactory::TopologyError /* errCode */, char const * msg, Shape const & /* shape */) {
|
2014-12-04 04:21:06 +00:00
|
|
|
Warning(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Far
|
|
|
|
|
|
|
|
} // namespace OPENSUBDIV_VERSION
|
|
|
|
} // namespace OpenSubdiv
|
2014-09-05 22:07:46 +00:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2015-05-31 05:10:07 +00:00
|
|
|
#endif /* FAR_UTILS_H */
|