export point3f/d vertex attribute works ply files

This commit is contained in:
alemuntoni 2021-04-02 12:09:52 +02:00
parent 4001b5aa34
commit 6509139fc6
4 changed files with 875 additions and 777 deletions

View File

@ -34,7 +34,7 @@
#include<wrap/io_trimesh/io_ply.h>
#include<wrap/io_trimesh/precision.h>
#include<vcg/container/simple_temporary_data.h>
#include <vcg/complex/base.h>
#include <stdio.h>
@ -118,9 +118,8 @@ namespace vcg {
fprintf(fpout,
"ply\n"
"format %s 1.0\n"
"comment VCGLIB generated\n"
,h
);
"comment VCGLIB generated\n" ,
h);
if (((pi.mask & Mask::IOM_WEDGTEXCOORD) != 0) || ((pi.mask & Mask::IOM_VERTTEXCOORD) != 0))
{
@ -178,8 +177,7 @@ namespace vcg {
if( HasPerVertexFlags(m) &&( pi.mask & Mask::IOM_VERTFLAGS) )
{
fprintf(fpout,
"property int flags\n"
);
"property int flags\n");
}
if( HasPerVertexColor(m) && (pi.mask & Mask::IOM_VERTCOLOR) )
@ -188,8 +186,7 @@ namespace vcg {
"property uchar red\n"
"property uchar green\n"
"property uchar blue\n"
"property uchar alpha\n"
);
"property uchar alpha\n");
}
if( HasPerVertexQuality(m) && (pi.mask & Mask::IOM_VERTQUALITY) )
@ -207,31 +204,42 @@ namespace vcg {
{
fprintf(fpout,
"property float texture_u\n"
"property float texture_v\n"
);
"property float texture_v\n");
}
for(size_t i=0;i<pi.VertDescriptorVec.size();i++){
if (!pi.VertDescriptorVec[i].islist) {
fprintf(
fpout,
"property %s %s\n",
pi.VertDescriptorVec[i].stotypename(),
pi.VertDescriptorVec[i].propname.c_str());
}
else {
fprintf(
fpout,
"property list %s %s %s\n",
pi.VertDescriptorVec[i].stotype2name(),
pi.VertDescriptorVec[i].stotypename(),
pi.VertDescriptorVec[i].propname.c_str());
}
}
for(size_t i=0;i<pi.VertDescriptorVec.size();i++)
fprintf(fpout,"property %s %s\n",pi.VertDescriptorVec[i].stotypename(),pi.VertDescriptorVec[i].propname.c_str());
fprintf(fpout,
"element face %d\n"
"property list uchar int vertex_indices\n"
,m.fn
);
"property list uchar int vertex_indices\n",
m.fn );
if(HasPerFaceFlags(m) && (pi.mask & Mask::IOM_FACEFLAGS) )
{
fprintf(fpout,
"property int flags\n"
);
"property int flags\n");
}
if( (HasPerWedgeTexCoord(m) && pi.mask & Mask::IOM_WEDGTEXCOORD ) ||
(HasPerVertexTexCoord(m) && (!HasPerWedgeTexCoord(m)) && pi.mask & Mask::IOM_WEDGTEXCOORD ) ) // Note that you can save VT as WT if you really really want it...
{
fprintf(fpout,
"property list uchar float texcoord\n"
);
"property list uchar float texcoord\n" );
}
// The texture index information has to be saved for each face (if necessary) both for PerVert and PerWedg
if( saveTexIndexFlag &&
@ -242,25 +250,24 @@ namespace vcg {
)
{
fprintf(fpout,
"property int texnumber\n"
);
"property int texnumber\n");
}
if( HasPerFaceColor(m) && (pi.mask & Mask::IOM_FACECOLOR) )
{
fprintf(fpout,
fprintf(
fpout,
"property uchar red\n"
"property uchar green\n"
"property uchar blue\n"
"property uchar alpha\n"
);
"property uchar alpha\n");
}
if ( HasPerWedgeColor(m) && (pi.mask & Mask::IOM_WEDGCOLOR) )
{
fprintf(fpout,
"property list uchar float color\n"
);
fprintf(
fpout,
"property list uchar float color\n");
}
if (HasPerFaceNormal(m) && (pi.mask & Mask::IOM_FACENORMAL))
@ -281,12 +288,9 @@ namespace vcg {
fprintf(fpout,"property %s %s\n",pi.FaceDescriptorVec[i].stotypename(),pi.FaceDescriptorVec[i].propname.c_str());
// Saving of edges is enabled if requested
if( m.en>0 && (pi.mask & Mask::IOM_EDGEINDEX) )
fprintf(fpout,
"element edge %d\n"
"property int vertex1\n"
"property int vertex2\n"
,m.en
);
fprintf(
fpout,
"element edge %d\n" "property int vertex1\n""property int vertex2\n",m.en);
fprintf(fpout, "end_header\n" );
// Salvataggio camera
@ -366,12 +370,15 @@ namespace vcg {
std::vector<typename SaveMeshType:: template ConstPerVertexAttributeHandle<short > > thsv(pi.VertDescriptorVec.size());
std::vector<typename SaveMeshType:: template ConstPerVertexAttributeHandle<char > > thcv(pi.VertDescriptorVec.size());
std::vector<typename SaveMeshType:: template ConstPerVertexAttributeHandle<unsigned char> > thuv(pi.VertDescriptorVec.size());
std::vector<typename SaveMeshType:: template ConstPerVertexAttributeHandle<vcg::Point3f> > thp3fv(pi.VertDescriptorVec.size());
std::vector<typename SaveMeshType:: template ConstPerVertexAttributeHandle<vcg::Point3d> > thp3dv(pi.VertDescriptorVec.size());
for(size_t i=0;i<pi.VertDescriptorVec.size();i++)
{
if(!pi.VertAttrNameVec.empty() && !pi.VertAttrNameVec[i].empty())
{ // trying to use named attribute to retrieve the value to store
assert(vcg::tri::HasPerVertexAttribute(m,pi.VertAttrNameVec[i]));
if (!pi.VertDescriptorVec[i].islist){
switch (pi.VertDescriptorVec[i].stotype1)
{
case ply::T_FLOAT : thfv[i] = vcg::tri::Allocator<SaveMeshType>::template FindPerVertexAttribute<float>(m,pi.VertAttrNameVec[i]); break;
@ -383,6 +390,15 @@ namespace vcg {
default : assert(0);
}
}
else {
switch (pi.VertDescriptorVec[i].stotype1)
{
case ply::T_FLOAT : thp3fv[i] = vcg::tri::Allocator<SaveMeshType>::template FindPerVertexAttribute<vcg::Point3f>(m,pi.VertAttrNameVec[i]); break;
case ply::T_DOUBLE : thp3dv[i] = vcg::tri::Allocator<SaveMeshType>::template FindPerVertexAttribute<vcg::Point3d>(m,pi.VertAttrNameVec[i]); break;
default : assert(0);
}
}
}
}
std::vector<typename SaveMeshType:: template ConstPerFaceAttributeHandle<float > > thff(pi.FaceDescriptorVec.size());
std::vector<typename SaveMeshType:: template ConstPerFaceAttributeHandle<double> > thdf(pi.FaceDescriptorVec.size());
@ -409,9 +425,6 @@ namespace vcg {
}
}
for(j=0,vi=m.vert.begin();vi!=m.vert.end();++vi){
vp=&(*vi);
indices[vi] = j;
@ -464,6 +477,7 @@ namespace vcg {
if(!pi.VertAttrNameVec.empty() && !pi.VertAttrNameVec[i].empty())
{ // trying to use named attribute to retrieve the value to store
assert(vcg::tri::HasPerVertexAttribute(m,pi.VertAttrNameVec[i]));
if (!pi.VertDescriptorVec[i].islist){
switch (pi.VertDescriptorVec[i].stotype1)
{
case ply::T_FLOAT : tf=thfv[i][vp]; fwrite(&tf, sizeof(float),1,fpout); break;
@ -475,6 +489,28 @@ namespace vcg {
default : assert(0);
}
}
else { //it is a Poin3f or a Point3d attribute. Saving it as a list
static const unsigned int psize = 3;
switch (pi.VertDescriptorVec[i].stotype1)
{
case ply::T_FLOAT :
fwrite(&psize, sizeof(unsigned int), 1,fpout);
fwrite(&thp3fv[i][vp][0], sizeof(float), 1,fpout);
fwrite(&thp3fv[i][vp][1], sizeof(float), 1,fpout);
fwrite(&thp3fv[i][vp][2], sizeof(float), 1,fpout);
break;
//fprintf(fpout,"%d %f %f %f", 3, thp3fv[i][vp][0], thp3fv[i][vp][1], thp3fv[i][vp][2]); break;
case ply::T_DOUBLE :
fwrite(&psize, sizeof(unsigned int), 1,fpout);
fwrite(&thp3dv[i][vp][0], sizeof(double), 1,fpout);
fwrite(&thp3dv[i][vp][1], sizeof(double), 1,fpout);
fwrite(&thp3dv[i][vp][2], sizeof(double), 1,fpout);
break;
//fprintf(fpout,"%d %lf %lf %lf", 3, thp3dv[i][vp][0], thp3dv[i][vp][1], thp3dv[i][vp][2]); break;
default : assert(0);
}
}
}
else
{
switch (pi.VertDescriptorVec[i].stotype1)
@ -518,6 +554,7 @@ namespace vcg {
if(!pi.VertAttrNameVec.empty() && !pi.VertAttrNameVec[i].empty())
{ // trying to use named attribute to retrieve the value to store
assert(vcg::tri::HasPerVertexAttribute(m,pi.VertAttrNameVec[i]));
if (!pi.VertDescriptorVec[i].islist){
switch (pi.VertDescriptorVec[i].stotype1)
{
case ply::T_FLOAT : tf=thfv[i][vp]; fprintf(fpout,"%f ",tf); break;
@ -529,12 +566,21 @@ namespace vcg {
default : assert(0);
}
}
else { //it is a Poin3f or a Point3d attribute. Saving it as a list
switch (pi.VertDescriptorVec[i].stotype1)
{
case ply::T_FLOAT : fprintf(fpout,"%d %f %f %f", 3, thp3fv[i][vp][0], thp3fv[i][vp][1], thp3fv[i][vp][2]); break;
case ply::T_DOUBLE : fprintf(fpout,"%d %lf %lf %lf", 3, thp3dv[i][vp][0], thp3dv[i][vp][1], thp3dv[i][vp][2]); break;
default : assert(0);
}
}
}
else
{
switch (pi.VertDescriptorVec[i].memtype1)
{
case ply::T_FLOAT : tf=*( (float *) (((char *)vp)+pi.VertDescriptorVec[i].offset1)); fprintf(fpout,"%f ",tf); break;
case ply::T_DOUBLE : td=*( (double *) (((char *)vp)+pi.VertDescriptorVec[i].offset1)); fprintf(fpout,"%f ",tf); break;
case ply::T_DOUBLE : td=*( (double *) (((char *)vp)+pi.VertDescriptorVec[i].offset1)); fprintf(fpout,"%lf ",tf); break;
case ply::T_INT : ti=*( (int *) (((char *)vp)+pi.VertDescriptorVec[i].offset1)); fprintf(fpout,"%i ",ti); break;
case ply::T_SHORT : ti=*( (short *) (((char *)vp)+pi.VertDescriptorVec[i].offset1)); fprintf(fpout,"%i ",ti); break;
case ply::T_CHAR : ti=*( (char *) (((char *)vp)+pi.VertDescriptorVec[i].offset1)); fprintf(fpout,"%i ",ti); break;

View File

@ -100,22 +100,66 @@ public:
addPerElemScalarAttribute(elemType, vcg::ply::T_DOUBLE, attrName, propName);
}
void addPerVertexScalarAttribute(const std::string& attrName, vcg::ply::PlyTypes attrType, std::string propName="") {
void addPerVertexScalarAttribute(const std::string& attrName, vcg::ply::PlyTypes attrType, std::string propName="")
{
addPerElemScalarAttribute(0,attrType, attrName,propName);
}
void AddPerVertexFloatAttribute(const std::string& attrName, std::string propName="") {
void AddPerVertexFloatAttribute(const std::string& attrName, std::string propName="")
{
AddPerElemFloatAttribute(0,attrName,propName);
}
void addPerFaceScalarAttribute(const std::string& attrName, vcg::ply::PlyTypes attrType, std::string propName="") {
void addPerFaceScalarAttribute(const std::string& attrName, vcg::ply::PlyTypes attrType, std::string propName="")
{
addPerElemScalarAttribute(1,attrType, attrName,propName);
}
void AddPerFaceFloatAttribute(const std::string& attrName, std::string propName="") {
void AddPerFaceFloatAttribute(const std::string& attrName, std::string propName="")
{
AddPerElemFloatAttribute(1,attrName,propName);
}
void addPerElemPointAttribute(int elemType, vcg::ply::PlyTypes propertyType, const std::string& attrName, std::string propName="")
{
if(propName=="")
propName=attrName;
PropDescriptor p;
p.propname=propName;
p.stotype1 = propertyType;
p.memtype1 = propertyType;
p.islist = true;
p.stotype2 = vcg::ply::PlyTypes::T_UINT;
p.stotype2 = vcg::ply::PlyTypes::T_UINT;
if (elemType == 0){ //vertex
VertAttrNameVec.push_back(attrName);
p.elemname="vertex";
VertDescriptorVec.push_back(p);
}
else if (elemType == 1){ //face
FaceAttrNameVec.push_back(attrName);
p.elemname="face";
FaceDescriptorVec.push_back(p);
}
}
void addPerVertexPoint3mAttribute(const std::string& attrName, vcg::ply::PlyTypes attrType, std::string propName="")
{
addPerElemPointAttribute(0,attrType, attrName,propName);
}
void addPerVertexPoint3fAttribute(const std::string& attrName, std::string propName="")
{
addPerElemPointAttribute(0,vcg::ply::PlyTypes::T_FLOAT, attrName,propName);
}
void addPerVertexPoint3dAttribute(const std::string& attrName, std::string propName="")
{
addPerElemPointAttribute(0,vcg::ply::PlyTypes::T_DOUBLE, attrName,propName);
}
/* Note that saving a per vertex point3 attribute is a mess.
* Actually require to allocate 3 float attribute and save them. And they are never deallocated... */
template<class MeshType>

View File

@ -143,8 +143,12 @@ static int TypeSize[] = {
size_t PropDescriptor::memtypesize() const {return TypeSize[memtype1];}
size_t PropDescriptor::stotypesize() const {return TypeSize[stotype1];}
size_t PropDescriptor::memtype2size() const {return TypeSize[memtype2];}
size_t PropDescriptor::stotype2size() const {return TypeSize[stotype2];}
const char *PropDescriptor::memtypename() const {return PlyFile::typenames[memtype1];}
const char *PropDescriptor::stotypename() const {return PlyFile::typenames[stotype1];}
const char *PropDescriptor::memtype2name() const {return PlyFile::typenames[memtype2];}
const char *PropDescriptor::stotype2name() const {return PlyFile::typenames[stotype2];}
static char CrossType[9][9]=
{

View File

@ -133,6 +133,10 @@ public:
size_t memtypesize() const; // per sapere quanto e'grande un dato descrittore in memoria
const char* memtypename() const;
const char* stotypename() const;
size_t stotype2size() const; // per sapere quanto e'grande un dato descrittore sul file
size_t memtype2size() const; // per sapere quanto e'grande un dato descrittore in memoria
const char* memtype2name() const;
const char* stotype2name() const;
};
// Reading Callback (used to copy a data prop)