2012-06-08 18:18:20 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// Copyright 2013 Pixar
|
2012-06-08 18:18:20 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// 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:
|
2012-06-08 18:18:20 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// 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.
|
2012-06-08 18:18:20 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// You may obtain a copy of the Apache License at
|
2012-06-08 18:18:20 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2013-07-18 21:19:50 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// 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.
|
2012-06-08 18:18:20 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
|
2012-06-08 18:18:20 +00:00
|
|
|
#ifndef SHAPE_UTILS_H
|
|
|
|
#define SHAPE_UTILS_H
|
|
|
|
|
2013-11-22 00:05:31 +00:00
|
|
|
#ifndef HBR_ADAPTIVE
|
|
|
|
#define HBR_ADAPTIVE
|
|
|
|
#endif
|
|
|
|
|
2012-09-18 01:41:48 +00:00
|
|
|
#include <hbr/mesh.h>
|
|
|
|
#include <hbr/bilinear.h>
|
|
|
|
#include <hbr/loop.h>
|
|
|
|
#include <hbr/catmark.h>
|
2012-08-04 02:51:27 +00:00
|
|
|
#include <hbr/vertexEdit.h>
|
|
|
|
#include <hbr/cornerEdit.h>
|
2013-03-02 02:27:19 +00:00
|
|
|
#include <hbr/holeEdit.h>
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-06-08 18:18:20 +00:00
|
|
|
#include <stdio.h>
|
2012-06-09 21:23:49 +00:00
|
|
|
#include <string.h>
|
2012-06-08 18:18:20 +00:00
|
|
|
|
|
|
|
#include <list>
|
2012-06-09 21:23:49 +00:00
|
|
|
#include <string>
|
2012-08-17 23:58:01 +00:00
|
|
|
#include <sstream>
|
2012-06-09 21:23:49 +00:00
|
|
|
#include <vector>
|
2012-06-08 18:18:20 +00:00
|
|
|
|
2012-08-04 02:51:27 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2012-06-08 18:18:20 +00:00
|
|
|
static char const * sgets( char * s, int size, char ** stream ) {
|
2012-08-04 02:51:27 +00:00
|
|
|
for (int i=0; i<size; ++i) {
|
2012-06-08 18:18:20 +00:00
|
|
|
if ( (*stream)[i]=='\n' or (*stream)[i]=='\0') {
|
|
|
|
|
|
|
|
memcpy(s, *stream, i);
|
|
|
|
s[i]='\0';
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-06-08 18:18:20 +00:00
|
|
|
if ((*stream)[i]=='\0')
|
|
|
|
return 0;
|
|
|
|
else {
|
|
|
|
(*stream) += i+1;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-17 23:58:01 +00:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
enum Scheme {
|
|
|
|
kBilinear,
|
|
|
|
kCatmark,
|
|
|
|
kLoop
|
|
|
|
};
|
|
|
|
|
2012-08-04 02:51:27 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2012-06-08 18:18:20 +00:00
|
|
|
|
|
|
|
struct shape {
|
|
|
|
|
|
|
|
struct tag {
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-06-08 18:18:20 +00:00
|
|
|
static tag * parseTag( char const * stream );
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-08-17 23:58:01 +00:00
|
|
|
std::string genTag() const;
|
2012-08-04 02:51:27 +00:00
|
|
|
|
|
|
|
std::string name;
|
2012-06-08 18:18:20 +00:00
|
|
|
std::vector<int> intargs;
|
|
|
|
std::vector<float> floatargs;
|
|
|
|
std::vector<std::string> stringargs;
|
|
|
|
};
|
|
|
|
|
|
|
|
static shape * parseShape(char const * shapestr, int axis=1);
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-08-17 23:58:01 +00:00
|
|
|
std::string genShape(char const * name) const;
|
2012-12-11 01:15:13 +00:00
|
|
|
|
|
|
|
std::string genObj(char const * name) const;
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-08-17 23:58:01 +00:00
|
|
|
std::string genRIB() const;
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-06-08 18:18:20 +00:00
|
|
|
~shape();
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-06-08 18:18:20 +00:00
|
|
|
int getNverts() const { return (int)verts.size()/3; }
|
|
|
|
|
|
|
|
int getNfaces() const { return (int)nvertsPerFace.size(); }
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2013-06-07 17:40:11 +00:00
|
|
|
bool hasUV() const { return not (uvs.empty() or faceuvs.empty()); }
|
2012-06-11 02:59:04 +00:00
|
|
|
|
2012-06-08 18:18:20 +00:00
|
|
|
std::vector<float> verts;
|
|
|
|
std::vector<float> uvs;
|
2012-12-11 01:15:13 +00:00
|
|
|
std::vector<float> normals;
|
2012-08-04 02:51:27 +00:00
|
|
|
std::vector<int> nvertsPerFace;
|
|
|
|
std::vector<int> faceverts;
|
2012-06-08 18:18:20 +00:00
|
|
|
std::vector<int> faceuvs;
|
2012-12-11 01:15:13 +00:00
|
|
|
std::vector<int> facenormals;
|
2012-06-08 18:18:20 +00:00
|
|
|
std::vector<tag *> tags;
|
2012-08-17 23:58:01 +00:00
|
|
|
Scheme scheme;
|
2012-08-04 02:51:27 +00:00
|
|
|
};
|
2012-06-08 18:18:20 +00:00
|
|
|
|
2012-08-04 02:51:27 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2012-06-08 18:18:20 +00:00
|
|
|
shape::~shape() {
|
|
|
|
for (int i=0; i<(int)tags.size(); ++i)
|
|
|
|
delete tags[i];
|
|
|
|
}
|
|
|
|
|
2012-08-04 02:51:27 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2012-06-08 18:18:20 +00:00
|
|
|
shape::tag * shape::tag::parseTag(char const * line) {
|
|
|
|
tag * t = 0;
|
2012-08-04 02:51:27 +00:00
|
|
|
|
|
|
|
const char* cp = &line[2];
|
2012-06-08 18:18:20 +00:00
|
|
|
|
2012-06-13 15:30:01 +00:00
|
|
|
char name[50];
|
2012-06-08 18:18:20 +00:00
|
|
|
while (*cp == ' ') cp++;
|
2012-08-04 02:51:27 +00:00
|
|
|
if (sscanf(cp, "%s", name )!=1) return t;
|
|
|
|
while (*cp && *cp != ' ') cp++;
|
2012-06-08 18:18:20 +00:00
|
|
|
|
|
|
|
int nints=0, nfloats=0, nstrings=0;
|
|
|
|
while (*cp == ' ') cp++;
|
|
|
|
if (sscanf(cp, "%d/%d/%d", &nints, &nfloats, &nstrings)!=3) return t;
|
2012-08-04 02:51:27 +00:00
|
|
|
while (*cp && *cp != ' ') cp++;
|
2012-06-08 18:18:20 +00:00
|
|
|
|
|
|
|
std::vector<int> intargs;
|
|
|
|
for (int i=0; i<nints; ++i) {
|
2012-06-15 01:47:57 +00:00
|
|
|
int val;
|
|
|
|
while (*cp == ' ') cp++;
|
|
|
|
if (sscanf(cp, "%d", &val)!=1) return t;
|
|
|
|
intargs.push_back(val);
|
2012-08-04 02:51:27 +00:00
|
|
|
while (*cp && *cp != ' ') cp++;
|
2012-06-08 18:18:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<float> floatargs;
|
|
|
|
for (int i=0; i<nfloats; ++i) {
|
2012-06-15 01:47:57 +00:00
|
|
|
float val;
|
|
|
|
while (*cp == ' ') cp++;
|
|
|
|
if (sscanf(cp, "%f", &val)!=1) return t;
|
|
|
|
floatargs.push_back(val);
|
2012-08-04 02:51:27 +00:00
|
|
|
while (*cp && *cp != ' ') cp++;
|
2012-06-08 18:18:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> stringargs;
|
|
|
|
for (int i=0; i<nstrings; ++i) {
|
2012-08-04 02:51:27 +00:00
|
|
|
char val[512];
|
2012-06-15 01:47:57 +00:00
|
|
|
while (*cp == ' ') cp++;
|
2012-08-04 02:51:27 +00:00
|
|
|
if (sscanf(cp, "%s", val)!=1) return t;
|
2012-06-15 01:47:57 +00:00
|
|
|
stringargs.push_back(val);
|
2012-08-04 02:51:27 +00:00
|
|
|
while (*cp && *cp != ' ') cp++;
|
2012-06-08 18:18:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t = new shape::tag;
|
|
|
|
t->name = name;
|
|
|
|
t->intargs = intargs;
|
|
|
|
t->floatargs = floatargs;
|
|
|
|
t->stringargs = stringargs;
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-06-08 18:18:20 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2012-08-17 23:58:01 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
std::string shape::tag::genTag() const {
|
|
|
|
std::stringstream t;
|
|
|
|
|
|
|
|
t<<"\"t \""<<name<<"\" ";
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-08-17 23:58:01 +00:00
|
|
|
t<<intargs.size()<<"/"<<floatargs.size()<<"/"<<stringargs.size()<<" ";
|
|
|
|
|
|
|
|
std::copy(intargs.begin(), intargs.end(), std::ostream_iterator<int>(t));
|
|
|
|
t<<" ";
|
|
|
|
|
|
|
|
std::copy(floatargs.begin(), floatargs.end(), std::ostream_iterator<float>(t));
|
|
|
|
t<<" ";
|
|
|
|
|
|
|
|
std::copy(stringargs.begin(), stringargs.end(), std::ostream_iterator<std::string>(t));
|
|
|
|
t<<"\\n\"\n";
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-08-17 23:58:01 +00:00
|
|
|
return t.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
std::string shape::genShape(char const * name) const {
|
|
|
|
std::stringstream sh;
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-08-17 23:58:01 +00:00
|
|
|
sh<<"static char const * "<<name<<" = \n";
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-08-17 23:58:01 +00:00
|
|
|
for (int i=0; i<(int)verts.size(); i+=3)
|
|
|
|
sh << "\"v " << verts[i] << " " << verts[i+1] << " " << verts[i+2] <<"\\n\"\n";
|
|
|
|
|
|
|
|
for (int i=0; i<(int)uvs.size(); i+=2)
|
|
|
|
sh << "\"vt " << uvs[i] << " " << uvs[i+1] << "\\n\"\n";
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
for (int i=0; i<(int)normals.size(); i+=3)
|
|
|
|
sh << "\"vn " << normals[i] << " " << normals[i+1] << " " << normals[i+2] <<"\\n\"\n";
|
|
|
|
|
2012-08-17 23:58:01 +00:00
|
|
|
sh << "\"s off\\n\"\n";
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
for (int i=0, idx=0; i<(int)nvertsPerFace.size();++i) {
|
2012-08-17 23:58:01 +00:00
|
|
|
sh << "\"f ";
|
2012-12-11 01:15:13 +00:00
|
|
|
for (int j=0; j<nvertsPerFace[i];++j) {
|
|
|
|
int vert = faceverts[idx+j]+1,
|
|
|
|
uv = (int)faceuvs.size()>0 ? faceuvs[idx+j]+1 : vert,
|
|
|
|
normal = (int)facenormals.size()>0 ? facenormals[idx+j]+1 : vert;
|
|
|
|
sh << vert << "/" << uv << "/" << normal << " ";
|
2012-08-17 23:58:01 +00:00
|
|
|
}
|
|
|
|
sh << "\\n\"\n";
|
2012-12-11 01:15:13 +00:00
|
|
|
idx+=nvertsPerFace[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=0; i<(int)tags.size(); ++i)
|
|
|
|
sh << tags[i]->genTag();
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
return sh.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
std::string shape::genObj(char const * name) const {
|
|
|
|
std::stringstream sh;
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
sh<<"# This file uses centimeters as units for non-parametric coordinates.\n\n";
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
for (int i=0; i<(int)verts.size(); i+=3)
|
|
|
|
sh << "v " << verts[i] << " " << verts[i+1] << " " << verts[i+2] <<"\n";
|
|
|
|
|
|
|
|
for (int i=0; i<(int)uvs.size(); i+=2)
|
|
|
|
sh << "vt " << uvs[i] << " " << uvs[i+1] << "\n";
|
|
|
|
|
|
|
|
for (int i=0; i<(int)normals.size(); i+=3)
|
|
|
|
sh << "vn " << normals[i] << " " << normals[i+1] << " " << normals[i+2] <<"\n";
|
|
|
|
|
|
|
|
for (int i=0, idx=0; i<(int)nvertsPerFace.size();++i) {
|
|
|
|
sh << "f ";
|
|
|
|
for (int j=0; j<nvertsPerFace[i];++j) {
|
|
|
|
int vert = faceverts[idx+j]+1,
|
|
|
|
uv = (int)faceuvs.size()>0 ? faceuvs[idx+j]+1 : vert,
|
|
|
|
normal = (int)facenormals.size()>0 ? facenormals[idx+j]+1 : vert;
|
|
|
|
sh << vert << "/" << uv << "/" << normal << " ";
|
|
|
|
}
|
|
|
|
sh << "\n";
|
|
|
|
idx+=nvertsPerFace[i];
|
2012-08-17 23:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=0; i<(int)tags.size(); ++i)
|
|
|
|
sh << tags[i]->genTag();
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-08-17 23:58:01 +00:00
|
|
|
return sh.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
std::string shape::genRIB() const {
|
|
|
|
std::stringstream rib;
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-08-17 23:58:01 +00:00
|
|
|
rib << "HierarchicalSubdivisionMesh \"catmull-clark\" ";
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-08-17 23:58:01 +00:00
|
|
|
rib << "[";
|
|
|
|
std::copy(nvertsPerFace.begin(), nvertsPerFace.end(), std::ostream_iterator<int>(rib));
|
|
|
|
rib << "] ";
|
|
|
|
|
|
|
|
rib << "[";
|
|
|
|
std::copy(faceverts.begin(), faceverts.end(), std::ostream_iterator<int>(rib));
|
|
|
|
rib << "] ";
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-08-17 23:58:01 +00:00
|
|
|
std::stringstream names, nargs, intargs, floatargs, strargs;
|
|
|
|
for (int i=0; i<(int)tags.size();) {
|
|
|
|
tag * t = tags[i];
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-08-17 23:58:01 +00:00
|
|
|
names << t->name;
|
|
|
|
|
|
|
|
nargs << t->intargs.size() << " " << t->floatargs.size() << " " << t->stringargs.size();
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-08-17 23:58:01 +00:00
|
|
|
std::copy(t->intargs.begin(), t->intargs.end(), std::ostream_iterator<int>(intargs));
|
|
|
|
|
|
|
|
std::copy(t->floatargs.begin(), t->floatargs.end(), std::ostream_iterator<float>(floatargs));
|
|
|
|
|
|
|
|
std::copy(t->stringargs.begin(), t->stringargs.end(), std::ostream_iterator<std::string>(strargs));
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-08-17 23:58:01 +00:00
|
|
|
if (++i<(int)tags.size()) {
|
|
|
|
names << " ";
|
|
|
|
nargs << " ";
|
|
|
|
intargs << " ";
|
|
|
|
floatargs << " ";
|
|
|
|
strargs << " ";
|
|
|
|
}
|
|
|
|
}
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2014-04-25 17:49:59 +00:00
|
|
|
rib << "["<<names.str()<<"] " << "["<<nargs.str()<<"] " << "["<<intargs.str()<<"] " << "["<<floatargs.str()<<"] " << "["<<strargs.str()<<"] ";
|
2012-08-17 23:58:01 +00:00
|
|
|
|
|
|
|
rib << "\"P\" [";
|
|
|
|
std::copy(verts.begin(), verts.end(), std::ostream_iterator<float>(rib));
|
|
|
|
rib << "] ";
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-08-17 23:58:01 +00:00
|
|
|
return rib.str();
|
|
|
|
}
|
2012-06-08 18:18:20 +00:00
|
|
|
|
2012-08-04 02:51:27 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2012-06-08 18:18:20 +00:00
|
|
|
shape * shape::parseShape(char const * shapestr, int axis ) {
|
|
|
|
|
|
|
|
shape * s = new shape;
|
|
|
|
|
|
|
|
char * str=const_cast<char *>(shapestr), line[256];
|
|
|
|
bool done = false;
|
2012-08-04 02:51:27 +00:00
|
|
|
while( not done )
|
2012-06-08 18:18:20 +00:00
|
|
|
{ done = sgets(line, sizeof(line), &str)==0;
|
2012-08-04 02:51:27 +00:00
|
|
|
char* end = &line[strlen(line)-1];
|
|
|
|
if (*end == '\n') *end = '\0'; // strip trailing nl
|
|
|
|
float x, y, z, u, v;
|
|
|
|
switch (line[0]) {
|
|
|
|
case 'v': switch (line[1])
|
2012-08-17 23:58:01 +00:00
|
|
|
{ case ' ': if(sscanf(line, "v %f %f %f", &x, &y, &z) == 3) {
|
2012-08-04 02:51:27 +00:00
|
|
|
s->verts.push_back(x);
|
2012-08-17 23:58:01 +00:00
|
|
|
switch( axis ) {
|
|
|
|
case 0 : s->verts.push_back(-z);
|
|
|
|
s->verts.push_back(y); break;
|
|
|
|
case 1 : s->verts.push_back(y);
|
|
|
|
s->verts.push_back(z); break;
|
2013-10-28 17:40:24 +00:00
|
|
|
}
|
2012-08-04 02:51:27 +00:00
|
|
|
} break;
|
|
|
|
case 't': if(sscanf(line, "vt %f %f", &u, &v) == 2) {
|
|
|
|
s->uvs.push_back(u);
|
|
|
|
s->uvs.push_back(v);
|
2012-06-08 18:18:20 +00:00
|
|
|
} break;
|
2012-12-11 01:15:13 +00:00
|
|
|
case 'n' : if(sscanf(line, "vn %f %f %f", &x, &y, &z) == 3) {
|
|
|
|
s->normals.push_back(x);
|
|
|
|
s->normals.push_back(y);
|
|
|
|
s->normals.push_back(z);
|
|
|
|
}
|
|
|
|
break; // skip normals for now
|
2012-06-08 18:18:20 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'f': if(line[1] == ' ') {
|
2012-08-04 02:51:27 +00:00
|
|
|
int vi, ti, ni;
|
|
|
|
const char* cp = &line[2];
|
|
|
|
while (*cp == ' ') cp++;
|
|
|
|
int nverts = 0, nitems=0;
|
|
|
|
while( (nitems=sscanf(cp, "%d/%d/%d", &vi, &ti, &ni))>0) {
|
2012-12-11 01:15:13 +00:00
|
|
|
nverts++;
|
2012-08-04 02:51:27 +00:00
|
|
|
s->faceverts.push_back(vi-1);
|
|
|
|
if(nitems >= 1) s->faceuvs.push_back(ti-1);
|
2012-12-11 01:15:13 +00:00
|
|
|
if(nitems >= 2) s->facenormals.push_back(ni-1);
|
2012-08-04 02:51:27 +00:00
|
|
|
while (*cp && *cp != ' ') cp++;
|
|
|
|
while (*cp == ' ') cp++;
|
|
|
|
}
|
|
|
|
s->nvertsPerFace.push_back(nverts);
|
2012-06-08 18:18:20 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 't' : if(line[1] == ' ') {
|
|
|
|
shape::tag * t = tag::parseTag( line );
|
|
|
|
if (t)
|
2012-06-15 01:47:57 +00:00
|
|
|
s->tags.push_back(t);
|
|
|
|
} break;
|
2012-06-08 18:18:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2012-08-04 02:51:27 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
template <class T>
|
2012-06-15 01:47:57 +00:00
|
|
|
void applyTags( OpenSubdiv::HbrMesh<T> * mesh, shape const * sh ) {
|
2012-06-08 18:18:20 +00:00
|
|
|
|
|
|
|
for (int i=0; i<(int)sh->tags.size(); ++i) {
|
|
|
|
shape::tag * t = sh->tags[i];
|
|
|
|
|
|
|
|
if (t->name=="crease") {
|
2012-08-04 02:51:27 +00:00
|
|
|
for (int j=0; j<(int)t->intargs.size()-1; j += 2) {
|
2012-06-15 01:47:57 +00:00
|
|
|
OpenSubdiv::HbrVertex<T> * v = mesh->GetVertex( t->intargs[j] ),
|
|
|
|
* w = mesh->GetVertex( t->intargs[j+1] );
|
|
|
|
OpenSubdiv::HbrHalfedge<T> * e = 0;
|
2012-08-04 02:51:27 +00:00
|
|
|
if( v && w ) {
|
2013-02-10 02:54:04 +00:00
|
|
|
if((e = v->GetEdge(w)) == 0)
|
|
|
|
e = w->GetEdge(v);
|
2012-06-08 18:18:20 +00:00
|
|
|
if(e) {
|
|
|
|
int nfloat = (int) t->floatargs.size();
|
|
|
|
e->SetSharpness( std::max(0.0f, ((nfloat > 1) ? t->floatargs[j] : t->floatargs[0])) );
|
|
|
|
} else
|
|
|
|
printf("cannot find edge for crease tag (%d,%d)\n", t->intargs[j], t->intargs[j+1] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (t->name=="corner") {
|
|
|
|
for (int j=0; j<(int)t->intargs.size(); ++j) {
|
2012-06-15 01:47:57 +00:00
|
|
|
OpenSubdiv::HbrVertex<T> * v = mesh->GetVertex( t->intargs[j] );
|
2012-06-08 18:18:20 +00:00
|
|
|
if(v) {
|
|
|
|
int nfloat = (int) t->floatargs.size();
|
|
|
|
v->SetSharpness( std::max(0.0f, ((nfloat > 1) ? t->floatargs[j] : t->floatargs[0])) );
|
|
|
|
} else
|
|
|
|
printf("cannot find vertex for corner tag (%d)\n", t->intargs[j] );
|
|
|
|
}
|
|
|
|
} else if (t->name=="hole") {
|
|
|
|
for (int j=0; j<(int)t->intargs.size(); ++j) {
|
2012-06-15 01:47:57 +00:00
|
|
|
OpenSubdiv::HbrFace<T> * f = mesh->GetFace( t->intargs[j] );
|
2012-06-08 18:18:20 +00:00
|
|
|
if(f) {
|
|
|
|
f->SetHole();
|
|
|
|
} else
|
|
|
|
printf("cannot find face for hole tag (%d)\n", t->intargs[j] );
|
|
|
|
}
|
|
|
|
} else if (t->name=="interpolateboundary") {
|
2012-06-15 01:47:57 +00:00
|
|
|
if ((int)t->intargs.size()!=1) {
|
2012-06-08 18:18:20 +00:00
|
|
|
printf("expecting 1 integer for \"interpolateboundary\" tag n. %d\n", i);
|
|
|
|
continue;
|
|
|
|
}
|
2012-08-04 02:51:27 +00:00
|
|
|
switch( t->intargs[0] ) {
|
2012-06-15 01:47:57 +00:00
|
|
|
case 0 : mesh->SetInterpolateBoundaryMethod(OpenSubdiv::HbrMesh<T>::k_InterpolateBoundaryNone); break;
|
|
|
|
case 1 : mesh->SetInterpolateBoundaryMethod(OpenSubdiv::HbrMesh<T>::k_InterpolateBoundaryEdgeAndCorner); break;
|
|
|
|
case 2 : mesh->SetInterpolateBoundaryMethod(OpenSubdiv::HbrMesh<T>::k_InterpolateBoundaryEdgeOnly); break;
|
2013-10-10 17:37:39 +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] ) {
|
|
|
|
case 0 : mesh->SetFVarInterpolateBoundaryMethod(OpenSubdiv::HbrMesh<T>::k_InterpolateBoundaryNone); break;
|
|
|
|
case 1 : mesh->SetFVarInterpolateBoundaryMethod(OpenSubdiv::HbrMesh<T>::k_InterpolateBoundaryEdgeAndCorner); break;
|
|
|
|
case 2 : mesh->SetFVarInterpolateBoundaryMethod(OpenSubdiv::HbrMesh<T>::k_InterpolateBoundaryEdgeOnly); break;
|
|
|
|
case 3 : mesh->SetFVarInterpolateBoundaryMethod(OpenSubdiv::HbrMesh<T>::k_InterpolateBoundaryAlwaysSharp); break;
|
|
|
|
default: printf("unknown facevarying interpolate boundary : %d\n", t->intargs[0] ); break;
|
2012-06-15 01:47:57 +00:00
|
|
|
}
|
2012-06-08 18:18:20 +00:00
|
|
|
} else if (t->name=="facevaryingpropagatecorners") {
|
2012-06-15 01:47:57 +00:00
|
|
|
if ((int)t->intargs.size()==1)
|
|
|
|
mesh->SetFVarPropagateCorners( t->intargs[0] != 0 );
|
|
|
|
else
|
|
|
|
printf( "expecting single int argument for \"facevaryingpropagatecorners\"\n" );
|
2013-11-01 18:38:20 +00:00
|
|
|
} else if (t->name=="smoothtriangles") {
|
2013-11-22 00:05:31 +00:00
|
|
|
|
2013-11-01 18:38:20 +00:00
|
|
|
OpenSubdiv::HbrCatmarkSubdivision<T> * scheme =
|
|
|
|
dynamic_cast<OpenSubdiv::HbrCatmarkSubdivision<T> *>( mesh->GetSubdivision() );
|
|
|
|
|
|
|
|
if (not scheme) {
|
2013-11-08 01:06:55 +00:00
|
|
|
printf("the \"smoothtriangles\" tag can only be applied to Catmark meshes\n");
|
2013-11-01 18:38:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((int)t->intargs.size()==0) {
|
2013-11-08 01:06:55 +00:00
|
|
|
printf("the \"smoothtriangles\" tag expects an int argument\n");
|
2013-11-01 18:38:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( t->intargs[0]==1 )
|
|
|
|
scheme->SetTriangleSubdivisionMethod(
|
|
|
|
OpenSubdiv::HbrCatmarkSubdivision<T>::k_Old);
|
|
|
|
else if( t->intargs[0]==2 )
|
|
|
|
scheme->SetTriangleSubdivisionMethod(
|
|
|
|
OpenSubdiv::HbrCatmarkSubdivision<T>::k_New);
|
|
|
|
else
|
2013-11-08 01:06:55 +00:00
|
|
|
printf("the \"smoothtriangles\" tag only accepts 1 or 2 as value (%d)\n", t->intargs[0]);
|
2013-11-01 18:38:20 +00:00
|
|
|
|
2012-06-08 18:18:20 +00:00
|
|
|
} else if (t->name=="creasemethod") {
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2013-11-08 01:06:55 +00:00
|
|
|
OpenSubdiv::HbrSubdivision<T> * scheme = mesh->GetSubdivision();
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2013-11-08 01:06:55 +00:00
|
|
|
assert(scheme);
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-06-15 01:47:57 +00:00
|
|
|
if ((int)t->stringargs.size()==0) {
|
|
|
|
printf("the \"creasemethod\" tag expects a string argument\n");
|
|
|
|
continue;
|
|
|
|
}
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-06-08 18:18:20 +00:00
|
|
|
if( t->stringargs[0]=="normal" )
|
2013-11-01 18:38:20 +00:00
|
|
|
scheme->SetCreaseSubdivisionMethod(
|
|
|
|
OpenSubdiv::HbrSubdivision<T>::k_CreaseNormal);
|
2012-06-08 18:18:20 +00:00
|
|
|
else if( t->stringargs[0]=="chaikin" )
|
2013-11-01 18:38:20 +00:00
|
|
|
scheme->SetCreaseSubdivisionMethod(
|
|
|
|
OpenSubdiv::HbrSubdivision<T>::k_CreaseChaikin);
|
2012-06-15 01:47:57 +00:00
|
|
|
else
|
|
|
|
printf("the \"creasemethod\" tag only accepts \"normal\" or \"chaikin\" as value (%s)\n", t->stringargs[0].c_str());
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-06-08 18:18:20 +00:00
|
|
|
} else if (t->name=="vertexedit" or t->name=="edgeedit") {
|
2012-08-04 02:51:27 +00:00
|
|
|
int nops = 0;
|
|
|
|
int floatstride = 0;
|
|
|
|
int maxfloatwidth = 0;
|
|
|
|
std::vector<typename OpenSubdiv::HbrHierarchicalEdit<T>::Operation > ops;
|
|
|
|
std::vector<std::string> opnames;
|
|
|
|
std::vector<std::string> varnames;
|
|
|
|
std::vector<typename OpenSubdiv::HbrHierarchicalEdit<T>::Operation > opmodifiers;
|
|
|
|
std::vector<int> floatwidths;
|
|
|
|
std::vector<bool> isP;
|
|
|
|
std::vector<int> vvindex;
|
|
|
|
|
|
|
|
for (int j=0; j<(int)t->stringargs.size(); j+=3) {
|
|
|
|
const std::string & opname = t->stringargs[j+2];
|
|
|
|
const std::string & opmodifiername = t->stringargs[j];
|
|
|
|
const std::string & varname = t->stringargs[j+1];
|
|
|
|
|
|
|
|
typename OpenSubdiv::HbrHierarchicalEdit<T>::Operation opmodifier = OpenSubdiv::HbrVertexEdit<T>::Set;
|
|
|
|
if (opmodifiername == "set") {
|
|
|
|
opmodifier = OpenSubdiv::HbrHierarchicalEdit<T>::Set;
|
|
|
|
} else if (opmodifiername == "add") {
|
|
|
|
opmodifier = OpenSubdiv::HbrHierarchicalEdit<T>::Add;
|
|
|
|
} else if (opmodifiername == "subtract") {
|
|
|
|
opmodifier = OpenSubdiv::HbrHierarchicalEdit<T>::Subtract;
|
|
|
|
} else {
|
|
|
|
printf("invalid modifier %s\n", opmodifiername.c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
if ((t->name=="vertexedit" && opname=="value") || opname=="sharpness") {
|
2012-08-04 02:51:27 +00:00
|
|
|
nops++;
|
|
|
|
|
|
|
|
// only varname="P" is supported here for now.
|
|
|
|
if (varname != "P") continue;
|
|
|
|
|
|
|
|
vvindex.push_back(0);
|
|
|
|
isP.push_back(true);
|
|
|
|
opnames.push_back(opname);
|
|
|
|
opmodifiers.push_back(opmodifier);
|
|
|
|
varnames.push_back(varname);
|
|
|
|
|
|
|
|
if (opname=="sharpness") {
|
|
|
|
floatwidths.push_back(1);
|
|
|
|
floatstride += 1;
|
|
|
|
} else {
|
|
|
|
// assuming width of P == 3. should be replaced with 'P 0 3' like declaration
|
|
|
|
int numElements = 3;
|
|
|
|
maxfloatwidth = std::max(maxfloatwidth, numElements);
|
|
|
|
floatwidths.push_back(numElements);
|
|
|
|
floatstride += numElements;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf("%s tag specifies invalid operation '%s %s' on Subdivmesh\n", t->name.c_str(), opmodifiername.c_str(), opname.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float *xformed = (float*)alloca(maxfloatwidth * sizeof(float));
|
|
|
|
|
|
|
|
int floatoffset = 0;
|
|
|
|
for(int j=0; j<nops; ++j) {
|
|
|
|
int floatidx = floatoffset;
|
|
|
|
for (int k=0; k < (int)t->intargs.size();) {
|
|
|
|
int pathlength = t->intargs[k];
|
|
|
|
|
|
|
|
int faceid = t->intargs[k+1];
|
|
|
|
int vertexid = t->intargs[k+pathlength];
|
|
|
|
int nsubfaces = pathlength - 2;
|
|
|
|
int *subfaces = &t->intargs[k+2];
|
|
|
|
OpenSubdiv::HbrFace<T> * f = mesh->GetFace(faceid);
|
|
|
|
if (!f) {
|
|
|
|
printf("Invalid face %d specified for %s tag on SubdivisionMesh.\n", faceid, t->name.c_str());
|
|
|
|
goto nexttag;
|
|
|
|
}
|
|
|
|
// Found the face. Do some preliminary error checking to make sure the path is
|
|
|
|
// correct. First value in path depends on the number of vertices of the face
|
|
|
|
// which we have in hand
|
|
|
|
if (nsubfaces && (subfaces[0] < 0 || subfaces[0] >= f->GetNumVertices()) ) {
|
|
|
|
printf("Invalid path component %d in %s tag on SubdivisionMesh.\n", subfaces[0], t->name.c_str());
|
|
|
|
goto nexttag;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All subsequent values must be less than 4 (FIXME or 3 in the loop case?)
|
|
|
|
for (int l=1; l<nsubfaces; ++l) {
|
|
|
|
if (subfaces[l] < 0 || subfaces[l] > 3) {
|
|
|
|
printf("Invalid path component %d in %s tag on SubdivisionMesh.\n", subfaces[0], t->name.c_str());
|
|
|
|
goto nexttag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (vertexid < 0 || vertexid > 3) {
|
|
|
|
printf("Invalid path component (vertexid) %d in %s tag on SubdivisionMesh.\n", vertexid, t->name.c_str());
|
|
|
|
goto nexttag;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transform all the float values associated with the tag if needed
|
|
|
|
if(opnames[j] != "sharpness") {
|
|
|
|
for(int l=0; l<floatwidths[j]; ++l) {
|
|
|
|
xformed[l] = t->floatargs[l + floatidx];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Edits of facevarying data are a different hierarchical edit type altogether
|
|
|
|
OpenSubdiv::HbrVertexEdit<T> * edit = new OpenSubdiv::HbrVertexEdit<T>(faceid, nsubfaces, subfaces,
|
|
|
|
vertexid, vvindex[j], floatwidths[j],
|
|
|
|
isP[j], opmodifiers[j], xformed);
|
|
|
|
mesh->AddHierarchicalEdit(edit);
|
|
|
|
} else {
|
|
|
|
if (t->name == "vertexedit") {
|
|
|
|
OpenSubdiv::HbrCornerEdit<T> * edit = new OpenSubdiv::HbrCornerEdit<T>(faceid, nsubfaces, subfaces,
|
|
|
|
vertexid, opmodifiers[j], t->floatargs[floatidx]);
|
|
|
|
mesh->AddHierarchicalEdit(edit);
|
|
|
|
} else {
|
|
|
|
OpenSubdiv::HbrCreaseEdit<T> * edit = new OpenSubdiv::HbrCreaseEdit<T>(faceid, nsubfaces, subfaces,
|
|
|
|
vertexid, opmodifiers[j], t->floatargs[floatidx]);
|
|
|
|
mesh->AddHierarchicalEdit(edit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance to next path
|
|
|
|
k += pathlength + 1;
|
|
|
|
|
|
|
|
// Advance to start of float data
|
|
|
|
floatidx += floatstride;
|
|
|
|
} // End of integer processing loop
|
|
|
|
|
|
|
|
// Next subop
|
|
|
|
floatoffset += floatwidths[j];
|
|
|
|
|
|
|
|
} // End of subop processing loop
|
|
|
|
} else if (t->name=="faceedit") {
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2013-03-02 02:27:19 +00:00
|
|
|
int nint = (int)t->intargs.size();
|
|
|
|
for (int k=0; k<nint; ) {
|
|
|
|
int pathlength = t->intargs[k];
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2013-03-02 02:27:19 +00:00
|
|
|
if (k+pathlength>=nint) {
|
|
|
|
printf("Invalid path length for %s tag on SubdivisionMesh", t->name.c_str());
|
|
|
|
goto nexttag;
|
|
|
|
}
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2013-03-02 02:27:19 +00:00
|
|
|
int faceid = t->intargs[k+1];
|
|
|
|
int nsubfaces = pathlength - 1;
|
|
|
|
int *subfaces = &t->intargs[k+2];
|
|
|
|
OpenSubdiv::HbrFace<T> * f = mesh->GetFace(faceid);
|
|
|
|
if (!f) {
|
|
|
|
printf("Invalid face %d specified for %s tag on SubdivisionMesh.\n", faceid, t->name.c_str());
|
|
|
|
goto nexttag;
|
|
|
|
}
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2013-03-02 02:27:19 +00:00
|
|
|
// Found the face. Do some preliminary error checking to make sure the path is
|
|
|
|
// correct. First value in path depends on the number of vertices of the face
|
|
|
|
// which we have in hand
|
|
|
|
if (nsubfaces && (subfaces[0] < 0 || subfaces[0] >= f->GetNumVertices()) ) {
|
|
|
|
printf("Invalid path component %d in %s tag on SubdivisionMesh.\n", subfaces[0], t->name.c_str());
|
|
|
|
goto nexttag;
|
|
|
|
}
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2013-03-02 02:27:19 +00:00
|
|
|
// All subsequent values must be less than 4 (FIXME or 3 in the loop case?)
|
|
|
|
for (int l=1; l<nsubfaces; ++l) {
|
|
|
|
if (subfaces[l] < 0 || subfaces[l] > 3) {
|
|
|
|
printf("Invalid path component %d in %s tag on SubdivisionMesh.\n", subfaces[0], t->name.c_str());
|
|
|
|
goto nexttag;
|
|
|
|
}
|
|
|
|
}
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2013-03-02 02:27:19 +00:00
|
|
|
// Now loop over string ops
|
|
|
|
int nstring = (int)t->stringargs.size();
|
|
|
|
for (int l = 0; l < nstring; ) {
|
|
|
|
if ( t->stringargs[l] == "hole" ) {
|
|
|
|
// Construct the edit
|
|
|
|
OpenSubdiv::HbrHoleEdit<T> * edit = new OpenSubdiv::HbrHoleEdit<T>(faceid, nsubfaces, subfaces);
|
|
|
|
mesh->AddHierarchicalEdit(edit);
|
|
|
|
++l;
|
|
|
|
} else if ( t->stringargs[l] == "attributes" ) {
|
|
|
|
// see NgpSubdivMesh.cpp:4341
|
|
|
|
printf("\"attributes\" face tag not supported yet.\n");
|
|
|
|
goto nexttag;
|
|
|
|
} else if ( t->stringargs[l] == "set" || t->stringargs[l] == "add" ) {
|
|
|
|
// see NgpSubdivMesh.cpp:4341
|
|
|
|
printf("\"set\" and \"add\" face tag not supported yet.\n");
|
|
|
|
goto nexttag;
|
|
|
|
} else {
|
|
|
|
printf("Faceedit tag specifies invalid operation '%s' on Subdivmesh.\n", t->stringargs[l].c_str());
|
|
|
|
goto nexttag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Advance to next path
|
|
|
|
k += pathlength + 1;
|
|
|
|
} // end face path loop
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-06-08 18:18:20 +00:00
|
|
|
} else {
|
|
|
|
printf("Unknown tag : \"%s\" - skipping\n", t->name.c_str());
|
2012-08-04 02:51:27 +00:00
|
|
|
}
|
|
|
|
nexttag: ;
|
2012-06-08 18:18:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2013-10-28 17:40:24 +00:00
|
|
|
template <class T> std::string
|
2012-12-11 01:15:13 +00:00
|
|
|
hbrToObj( OpenSubdiv::HbrMesh<T> * mesh ) {
|
|
|
|
|
|
|
|
std::stringstream sh;
|
|
|
|
|
|
|
|
sh<<"# This file uses centimeters as units for non-parametric coordinates.\n\n";
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
int nv = mesh->GetNumVertices();
|
|
|
|
for (int i=0; i<nv; ++i) {
|
|
|
|
const float * pos = mesh->GetVertex(i)->GetData().GetPos();
|
|
|
|
sh << "v " << pos[0] << " " << pos[1] << " " << pos[2] <<"\n";
|
|
|
|
}
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
int nf = mesh->GetNumFaces();
|
|
|
|
for (int i=0; i<nf; ++i) {
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
sh << "f ";
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
OpenSubdiv::HbrFace<T> * f = mesh->GetFace(i);
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
for (int j=0; j<f->GetNumVertices(); ++j) {
|
|
|
|
int vert = f->GetVertex(j)->GetID()+1;
|
|
|
|
sh << vert << "/" << vert << "/" << vert << " ";
|
|
|
|
}
|
|
|
|
sh << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
sh << "\n";
|
|
|
|
|
|
|
|
return sh.str();
|
|
|
|
}
|
|
|
|
|
2012-06-09 21:23:49 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2012-06-15 01:47:57 +00:00
|
|
|
template <class T> OpenSubdiv::HbrMesh<T> *
|
2013-06-07 01:07:46 +00:00
|
|
|
createMesh( Scheme scheme=kCatmark, int fvarwidth=0) {
|
2012-06-08 18:18:20 +00:00
|
|
|
|
2012-06-15 01:47:57 +00:00
|
|
|
OpenSubdiv::HbrMesh<T> * mesh = 0;
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-06-15 01:47:57 +00:00
|
|
|
static OpenSubdiv::HbrBilinearSubdivision<T> _bilinear;
|
|
|
|
static OpenSubdiv::HbrLoopSubdivision<T> _loop;
|
|
|
|
static OpenSubdiv::HbrCatmarkSubdivision<T> _catmark;
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2013-10-10 17:37:39 +00:00
|
|
|
static int indices[2] = { 0, 1 },
|
|
|
|
widths[2] = { 1, 1 };
|
2013-06-07 01:07:46 +00:00
|
|
|
|
2013-10-10 17:37:39 +00:00
|
|
|
int const fvarcount = fvarwidth > 0 ? 2 : 0,
|
2013-06-07 01:07:46 +00:00
|
|
|
* fvarindices = fvarwidth > 0 ? indices : NULL,
|
|
|
|
* fvarwidths = fvarwidth > 0 ? widths : NULL;
|
|
|
|
|
|
|
|
|
2012-06-08 18:18:20 +00:00
|
|
|
switch (scheme) {
|
2013-10-28 17:40:24 +00:00
|
|
|
case kBilinear : mesh = new OpenSubdiv::HbrMesh<T>( &_bilinear,
|
2013-06-07 01:07:46 +00:00
|
|
|
fvarcount,
|
|
|
|
fvarindices,
|
|
|
|
fvarwidths,
|
|
|
|
fvarwidth ); break;
|
|
|
|
|
|
|
|
case kLoop : mesh = new OpenSubdiv::HbrMesh<T>( &_loop,
|
|
|
|
fvarcount,
|
|
|
|
fvarindices,
|
|
|
|
fvarwidths,
|
|
|
|
fvarwidth ); break;
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2013-06-07 01:07:46 +00:00
|
|
|
case kCatmark : mesh = new OpenSubdiv::HbrMesh<T>( &_catmark,
|
|
|
|
fvarcount,
|
|
|
|
fvarindices,
|
|
|
|
fvarwidths,
|
|
|
|
fvarwidth ); break;
|
2012-06-08 18:18:20 +00:00
|
|
|
}
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-06-11 02:59:04 +00:00
|
|
|
return mesh;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
template <class T> void
|
2012-06-20 00:15:07 +00:00
|
|
|
createVertices( shape const * sh, OpenSubdiv::HbrMesh<T> * mesh, std::vector<float> * verts ) {
|
2012-06-19 01:20:34 +00:00
|
|
|
|
|
|
|
T v;
|
|
|
|
for(int i=0;i<sh->getNverts(); i++ ) {
|
|
|
|
v.SetPosition( sh->verts[i*3], sh->verts[i*3+1], sh->verts[i*3+2] );
|
|
|
|
mesh->NewVertex( i, v );
|
|
|
|
}
|
2012-06-11 02:59:04 +00:00
|
|
|
}
|
2012-06-08 18:18:20 +00:00
|
|
|
|
2012-06-11 02:59:04 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
template <class T> void
|
2012-06-15 01:47:57 +00:00
|
|
|
createVertices( shape const * sh, OpenSubdiv::HbrMesh<T> * mesh, std::vector<float> & verts ) {
|
2012-06-11 02:59:04 +00:00
|
|
|
|
|
|
|
T v;
|
2012-06-20 00:15:07 +00:00
|
|
|
for(int i=0;i<sh->getNverts(); i++ )
|
2012-06-11 02:59:04 +00:00
|
|
|
mesh->NewVertex( i, v );
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
2012-06-20 00:15:07 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
template <class T> void
|
|
|
|
copyVertexPositions( shape const * sh, OpenSubdiv::HbrMesh<T> * mesh, std::vector<float> & verts ) {
|
|
|
|
|
|
|
|
int nverts = mesh->GetNumVertices();
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
verts.resize( nverts * 3 );
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
std::copy(sh->verts.begin(), sh->verts.end(), verts.begin());
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2013-11-22 19:27:01 +00:00
|
|
|
// Sometimes Hbr dupes some vertices during Mesh::Finish() and our example
|
|
|
|
// code uses those vertices to draw coarse control cages and such
|
2013-11-22 00:05:31 +00:00
|
|
|
std::vector<std::pair<int, int> > const splits = mesh->GetSplitVertices();
|
|
|
|
for (int i=0; i<(int)splits.size(); ++i) {
|
|
|
|
memcpy(&verts[splits[i].first*3], &sh->verts[splits[i].second*3], 3*sizeof(float));
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
2012-06-11 02:59:04 +00:00
|
|
|
}
|
2012-06-08 18:18:20 +00:00
|
|
|
|
2012-06-11 02:59:04 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
template <class T> void
|
2012-06-15 01:47:57 +00:00
|
|
|
createTopology( shape const * sh, OpenSubdiv::HbrMesh<T> * mesh, Scheme scheme) {
|
2012-06-08 18:18:20 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
const int * fv=&(sh->faceverts[0]);
|
|
|
|
for(int f=0, ptxidx=0;f<sh->getNfaces(); f++ ) {
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
int nv = sh->nvertsPerFace[f];
|
2012-06-08 18:18:20 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
if ((scheme==kLoop) and (nv!=3)) {
|
2013-07-08 01:18:06 +00:00
|
|
|
printf("Trying to create a Loop subd with non-triangle face\n");
|
2012-12-11 01:15:13 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2012-06-11 02:59:04 +00:00
|
|
|
|
2013-10-28 17:40:24 +00:00
|
|
|
bool valid = true;
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
for(int j=0;j<nv;j++) {
|
|
|
|
OpenSubdiv::HbrVertex<T> * origin = mesh->GetVertex( fv[j] );
|
2013-07-08 01:18:06 +00:00
|
|
|
OpenSubdiv::HbrVertex<T> * destination = mesh->GetVertex( fv[(j+1)%nv] );
|
2012-12-11 01:15:13 +00:00
|
|
|
OpenSubdiv::HbrHalfedge<T> * opposite = destination->GetEdge(origin);
|
2012-06-11 02:59:04 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
if(origin==NULL || destination==NULL) {
|
|
|
|
printf(" An edge was specified that connected a nonexistent vertex\n");
|
2013-10-28 17:40:24 +00:00
|
|
|
valid=false;
|
|
|
|
break;
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
2012-06-11 02:59:04 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
if(origin == destination) {
|
|
|
|
printf(" An edge was specified that connected a vertex to itself\n");
|
2013-10-28 17:40:24 +00:00
|
|
|
valid=false;
|
|
|
|
break;
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
2012-06-11 02:59:04 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
if(opposite && opposite->GetOpposite() ) {
|
|
|
|
printf(" A non-manifold edge incident to more than 2 faces was found\n");
|
2013-10-28 17:40:24 +00:00
|
|
|
valid=false;
|
|
|
|
break;
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
2012-06-11 02:59:04 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
if(origin->GetEdge(destination)) {
|
|
|
|
printf(" An edge connecting two vertices was specified more than once."
|
|
|
|
" It's likely that an incident face was flipped\n");
|
2013-10-28 17:40:24 +00:00
|
|
|
valid=false;
|
|
|
|
break;
|
2012-12-11 01:15:13 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-11 02:59:04 +00:00
|
|
|
|
2013-10-28 17:40:24 +00:00
|
|
|
if (valid) {
|
2012-06-11 02:59:04 +00:00
|
|
|
|
2013-10-28 17:40:24 +00:00
|
|
|
OpenSubdiv::HbrFace<T> * face = mesh->NewFace(nv, (int *)fv, 0);
|
2012-06-11 02:59:04 +00:00
|
|
|
|
2013-10-28 17:40:24 +00:00
|
|
|
face->SetPtexIndex(ptxidx);
|
|
|
|
|
|
|
|
if ( (scheme==kCatmark or scheme==kBilinear) and nv != 4 ) {
|
|
|
|
ptxidx+=nv;
|
|
|
|
} else {
|
|
|
|
ptxidx++;
|
|
|
|
}
|
|
|
|
}
|
2012-06-11 02:59:04 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
fv+=nv;
|
|
|
|
}
|
2012-06-11 02:59:04 +00:00
|
|
|
|
2013-11-08 01:06:55 +00:00
|
|
|
mesh->SetInterpolateBoundaryMethod(
|
|
|
|
OpenSubdiv::HbrMesh<T>::k_InterpolateBoundaryEdgeOnly);
|
|
|
|
|
|
|
|
mesh->GetSubdivision()->SetCreaseSubdivisionMethod(
|
|
|
|
OpenSubdiv::HbrSubdivision<T>::k_CreaseNormal);
|
2013-11-22 00:05:31 +00:00
|
|
|
|
2013-11-08 01:06:55 +00:00
|
|
|
if (OpenSubdiv::HbrCatmarkSubdivision<T> * scheme =
|
|
|
|
dynamic_cast<OpenSubdiv::HbrCatmarkSubdivision<T> *>(mesh->GetSubdivision())) {
|
2013-11-22 00:05:31 +00:00
|
|
|
|
2013-11-08 01:06:55 +00:00
|
|
|
scheme->SetTriangleSubdivisionMethod(
|
|
|
|
OpenSubdiv::HbrCatmarkSubdivision<T>::k_Normal);
|
|
|
|
}
|
2012-06-16 02:13:29 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
applyTags<T>( mesh, sh );
|
2012-06-11 02:59:04 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
mesh->Finish();
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2013-06-13 23:22:59 +00:00
|
|
|
// check for disconnected vertices
|
|
|
|
if (mesh->GetNumDisconnectedVertices()) {
|
|
|
|
printf("The specified subdivmesh contains disconnected surface components.\n");
|
|
|
|
}
|
2012-06-11 02:59:04 +00:00
|
|
|
}
|
|
|
|
|
2013-06-07 01:07:46 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
template <class T> void
|
|
|
|
createFaceVaryingUV( shape const * sh, OpenSubdiv::HbrMesh<T> * mesh) {
|
|
|
|
|
2013-06-07 17:40:11 +00:00
|
|
|
if (not sh->hasUV())
|
2013-06-07 01:07:46 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (int i=0, idx=0; i<sh->getNfaces(); ++i ) {
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2013-06-07 01:07:46 +00:00
|
|
|
OpenSubdiv::HbrFace<T> * f = mesh->GetFace(i);
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2013-06-07 01:07:46 +00:00
|
|
|
int nv = sh->nvertsPerFace[i];
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2013-06-07 01:07:46 +00:00
|
|
|
OpenSubdiv::HbrHalfedge<T> * e = f->GetFirstEdge();
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2013-06-07 01:07:46 +00:00
|
|
|
for (int j=0; j<nv; ++j, e=e->GetNext()) {
|
|
|
|
|
|
|
|
OpenSubdiv::HbrFVarData<T> & fvt = e->GetOrgVertex()->GetFVarData(f);
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2013-06-07 21:47:41 +00:00
|
|
|
float const * fvdata = &sh->uvs[ sh->faceuvs[idx++]*2 ];
|
|
|
|
|
2013-06-07 01:07:46 +00:00
|
|
|
if (not fvt.IsInitialized()) {
|
|
|
|
fvt.SetAllData(2, fvdata);
|
|
|
|
} else if (not fvt.CompareAll(2, fvdata)) {
|
|
|
|
OpenSubdiv::HbrFVarData<T> & nfvt = e->GetOrgVertex()->NewFVarData(f);
|
|
|
|
nfvt.SetAllData(2, fvdata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-11 02:59:04 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2012-06-15 01:47:57 +00:00
|
|
|
template <class T> OpenSubdiv::HbrMesh<T> *
|
2013-06-07 01:07:46 +00:00
|
|
|
simpleHbr(char const * shapestr, Scheme scheme, std::vector<float> * verts=0, bool fvar=false) {
|
2012-06-11 02:59:04 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
shape * sh = shape::parseShape( shapestr );
|
2012-06-11 02:59:04 +00:00
|
|
|
|
2013-06-07 17:40:11 +00:00
|
|
|
int fvarwidth = fvar and sh->hasUV() ? 2 : 0;
|
2013-06-07 01:07:46 +00:00
|
|
|
|
|
|
|
OpenSubdiv::HbrMesh<T> * mesh = createMesh<T>(scheme, fvarwidth);
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
createVertices<T>(sh, mesh, verts);
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
createTopology<T>(sh, mesh, scheme);
|
2013-10-28 17:40:24 +00:00
|
|
|
|
2013-06-07 01:07:46 +00:00
|
|
|
if (fvar)
|
|
|
|
createFaceVaryingUV<T>(sh, mesh);
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2013-06-07 01:07:46 +00:00
|
|
|
if (verts)
|
2012-12-11 01:15:13 +00:00
|
|
|
copyVertexPositions<T>(sh,mesh,*verts);
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
delete sh;
|
|
|
|
|
|
|
|
return mesh;
|
2012-06-08 18:18:20 +00:00
|
|
|
}
|
|
|
|
|
2012-06-11 02:59:04 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2012-06-15 01:47:57 +00:00
|
|
|
template <class T> OpenSubdiv::HbrMesh<T> *
|
2013-06-07 01:07:46 +00:00
|
|
|
simpleHbr(char const * shapestr, Scheme scheme, std::vector<float> & verts, bool fvar=false) {
|
2012-06-11 02:59:04 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
shape * sh = shape::parseShape( shapestr );
|
2012-06-11 02:59:04 +00:00
|
|
|
|
2013-06-07 17:40:11 +00:00
|
|
|
int fvarwidth = fvar and sh->hasUV() ? 2 : 0;
|
2013-06-07 01:07:46 +00:00
|
|
|
|
|
|
|
OpenSubdiv::HbrMesh<T> * mesh = createMesh<T>(scheme, fvarwidth);
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
createVertices<T>(sh, mesh, verts);
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
createTopology<T>(sh, mesh, scheme);
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2013-06-07 01:07:46 +00:00
|
|
|
if (fvar)
|
|
|
|
createFaceVaryingUV<T>(sh, mesh);
|
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
copyVertexPositions<T>(sh,mesh,verts);
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-12-11 01:15:13 +00:00
|
|
|
delete sh;
|
|
|
|
|
|
|
|
return mesh;
|
2012-06-11 02:59:04 +00:00
|
|
|
}
|
2012-06-08 18:18:20 +00:00
|
|
|
|
|
|
|
#endif /* SHAPE_UTILS_H */
|