Added support for the color support in STL files (both the SolidView and the Magics 'standard').

This commit is contained in:
Paolo Cignoni 2012-11-27 07:05:38 +00:00
parent 049fae96c0
commit 2cbd4b7964
3 changed files with 148 additions and 29 deletions

View File

@ -226,7 +226,20 @@ inline static Color4 ColorRamp(const float &minf,const float &maxf ,float v )
rc.SetColorRamp(minf,maxf,v); rc.SetColorRamp(minf,maxf,v);
return rc; return rc;
} }
};
inline static unsigned short ToUnsignedB5G5R5(Color4 &) { return 0;}
inline static unsigned short ToUnsignedR5G5B5(Color4 &) { return 0;}
inline static Color4 FromUnsignedB5G5R5(unsigned short)
{
return Color4(Color4::White);
}
inline static Color4 FromUnsignedR5G5B5(unsigned short)
{
return Color4(Color4::White);
}
}; /// END CLASS ///////////////////
template <> template <> template <> template <>
inline void Color4<float>::Import(const Color4<unsigned char> &b) inline void Color4<float>::Import(const Color4<unsigned char> &b)
@ -312,6 +325,48 @@ typedef Color4<unsigned char> Color4b;
typedef Color4<float> Color4f; typedef Color4<float> Color4f;
typedef Color4<double> Color4d; typedef Color4<double> Color4d;
template<>
inline unsigned short Color4<unsigned char>::ToUnsignedB5G5R5(Color4<unsigned char> &cc)
{
unsigned short r = cc[0]/8;
unsigned short g = cc[1]/8;
unsigned short b = cc[2]/8;
unsigned short res = b + g*32 + r*1024;
return res;
}
template<>
inline unsigned short Color4<unsigned char>::ToUnsignedR5G5B5(Color4<unsigned char> &cc)
{
unsigned short r = cc[0]/8;
unsigned short g = cc[1]/8;
unsigned short b = cc[2]/8;
unsigned short res = r + g*32 + b*1024;
return res;
}
template<>
inline Color4<unsigned char> Color4<unsigned char>::FromUnsignedR5G5B5(unsigned short val)
{
unsigned short r = val % 32 *8;
unsigned short g = ((val/32)%32)*8;
unsigned short b = ((val/1024)%32)*8;
Color4b cc((unsigned char)r,(unsigned char)g,(unsigned char)b,(unsigned char)255);
return cc;
}
template<>
inline Color4<unsigned char> Color4<unsigned char>::FromUnsignedB5G5R5(unsigned short val)
{
unsigned short b = val % 32 *8;
unsigned short g = ((val/32)%32)*8;
unsigned short r = ((val/1024)%32)*8;
Color4b cc((unsigned char)r,(unsigned char)g,(unsigned char)b,(unsigned char)255);
return cc;
}
/*@}*/ /*@}*/

View File

@ -75,12 +75,12 @@ public:
typedef typename SaveMeshType::FaceType FaceType; typedef typename SaveMeshType::FaceType FaceType;
typedef unsigned short CallBackSTLFaceAttribute(const SaveMeshType &m, const FaceType &f); typedef unsigned short CallBackSTLFaceAttribute(const SaveMeshType &m, const FaceType &f);
static int Save(SaveMeshType &m, const char * filename, const int &/*mask*/, CallBackPos *) static int Save(SaveMeshType &m, const char * filename, const int &mask, CallBackPos *)
{ {
return Save(m,filename,true); return Save(m,filename,true,mask);
} }
static int Save(SaveMeshType &m, const char * filename , bool binary =true, const char *objectname=0, CallBackSTLFaceAttribute *faceAttributeCallback = NULL) static int Save(SaveMeshType &m, const char * filename , bool binary =true, int mask=0, const char *objectname=0, bool magicsMode=0)
{ {
typedef typename SaveMeshType::FaceIterator FaceIterator; typedef typename SaveMeshType::FaceIterator FaceIterator;
FILE *fp; FILE *fp;
@ -94,14 +94,23 @@ static int Save(SaveMeshType &m, const char * filename , bool binary =true, cons
// Write Header // Write Header
char header[128]="VCG "; char header[128]="VCG ";
if(objectname) strncpy(header,objectname,80); if(objectname) strncpy(header,objectname,80);
if(magicsMode)
{
strncpy(header,"COLOR=XXX MATERIAL=AAA BBB CCC ",80);
for(int i=0;i<3;++i)
{
header[0x06+i]=0x7f;
header[0x13+i]=0x7f;
header[0x17+i]=0x7f;
header[0x1b+i]=0x7f;
}
}
fwrite(header,80,1,fp); fwrite(header,80,1,fp);
// write number of facets // write number of facets
fwrite(&m.fn,1,sizeof(int),fp); fwrite(&m.fn,1,sizeof(int),fp);
Point3f p; Point3f p;
unsigned short attributes=0; unsigned short attributes=0;
for(FaceIterator fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() )
FaceIterator fi;
for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() )
{ {
// For each triangle write the normal, the three coords and a short set to zero // For each triangle write the normal, the three coords and a short set to zero
p.Import(vcg::NormalizedNormal(*fi)); p.Import(vcg::NormalizedNormal(*fi));
@ -111,8 +120,10 @@ static int Save(SaveMeshType &m, const char * filename , bool binary =true, cons
p.Import((*fi).V(k)->P()); p.Import((*fi).V(k)->P());
fwrite(p.V(),3,sizeof(float),fp); fwrite(p.V(),3,sizeof(float),fp);
} }
if (faceAttributeCallback != NULL) { if ((mask & Mask::IOM_FACECOLOR) && tri::HasPerFaceColor(m))
attributes = (*faceAttributeCallback)(m, *fi); {
if(magicsMode) attributes = 32768 | vcg::Color4b::ToUnsignedR5G5B5(fi->C());
else attributes = 32768 | vcg::Color4b::ToUnsignedB5G5R5(fi->C());
} }
fwrite(&attributes,1,sizeof(short),fp); fwrite(&attributes,1,sizeof(short),fp);
} }
@ -164,6 +175,7 @@ static int GetExportMaskCapability()
int capability = 0; int capability = 0;
capability |= vcg::tri::io::Mask::IOM_VERTCOORD; capability |= vcg::tri::io::Mask::IOM_VERTCOORD;
capability |= vcg::tri::io::Mask::IOM_FACEINDEX; capability |= vcg::tri::io::Mask::IOM_FACEINDEX;
capability |= vcg::tri::io::Mask::IOM_FACECOLOR;
return capability; return capability;
} }

View File

@ -25,6 +25,7 @@
#define __VCGLIB_IMPORT_STL #define __VCGLIB_IMPORT_STL
#include <stdio.h> #include <stdio.h>
#include <wrap/callback.h> #include <wrap/callback.h>
#include <vcg/space/color4.h>
namespace vcg { namespace vcg {
namespace tri { namespace tri {
@ -79,28 +80,59 @@ static const char *ErrorMsg(int error)
else return stl_error_msg[error]; else return stl_error_msg[error];
}; };
static bool LoadMask(const char * /*filename*/, int &mask) static bool LoadMask(const char * filename, int &mask)
{ {
bool magicMode;
mask = Mask::IOM_VERTCOORD | Mask::IOM_FACEINDEX; mask = Mask::IOM_VERTCOORD | Mask::IOM_FACEINDEX;
if(IsSTLColored(filename,magicMode))
mask |= Mask::IOM_FACECOLOR;
return true; return true;
} }
static int Open(OpenMeshType &mesh, const char *filename, int &loadmask, CallBackPos *cb=0) /* Try to guess if a stl has color
*
* rules:
* - It has to be binary
* - The per face attribute should be not zero
*
*/
static bool IsSTLColored(const char * filename, bool &magicsMode)
{ {
loadmask = Mask::IOM_VERTCOORD | Mask::IOM_FACEINDEX; if(IsSTLBinary(filename)==false) return false;
return Open(mesh,filename,cb); FILE *fp = fopen(filename, "r");
char buf[STL_LABEL_SIZE+1];
fread(buf,sizeof(char),STL_LABEL_SIZE,fp);
std::string strInput(buf);
size_t cInd = strInput.rfind("COLOR=");
size_t mInd = strInput.rfind("MATERIAL=");
if(cInd!=std::string::npos && mInd!=std::string::npos)
magicsMode = true;
else
magicsMode = false;
int facenum;
fread(&facenum, sizeof(int), 1, fp);
for(int i=0;i<std::min(facenum,1000);++i)
{
unsigned short attr;
Point3f norm;
Point3f tri[3];
fread(&norm,sizeof(Point3f),1,fp);
fread(&tri,sizeof(Point3f),3,fp);
fread(&attr,sizeof(unsigned short),1,fp);
if(attr!=0)
{
if(Color4b::FromUnsignedR5G5B5(attr) != Color4b(Color4b::White)) return true;
}
}
return false;
} }
static int Open( OpenMeshType &m, const char * filename, CallBackPos *cb=0) static bool IsSTLBinary(const char * filename)
{ {
FILE *fp;
bool binary=false; bool binary=false;
fp = fopen(filename, "r"); FILE *fp = fopen(filename, "r");
if(fp == NULL)
{
return E_CANTOPEN;
}
/* Find size of file */ /* Find size of file */
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
int file_size = ftell(fp); int file_size = ftell(fp);
@ -115,18 +147,29 @@ static int Open( OpenMeshType &m, const char * filename, CallBackPos *cb=0)
for(unsigned int i = 0; i < sizeof(tmpbuf); i++) for(unsigned int i = 0; i < sizeof(tmpbuf); i++)
{ {
if(tmpbuf[i] > 127) if(tmpbuf[i] > 127)
{ {
binary=true; binary=true;
break; break;
} }
} }
// Now we know if the stl file is ascii or binary. // Now we know if the stl file is ascii or binary.
fclose(fp); fclose(fp);
if(binary) return OpenBinary(m,filename,cb); return binary;
}
static int Open( OpenMeshType &m, const char * filename, int &loadMask, CallBackPos *cb=0)
{
FILE *fp = fopen(filename, "r");
if(fp == NULL)
return E_CANTOPEN;
fclose(fp);
loadMask |= Mask::IOM_VERTCOORD | Mask::IOM_FACEINDEX;
if(IsSTLBinary(filename)) return OpenBinary(m,filename,loadMask,cb);
else return OpenAscii(m,filename,cb); else return OpenAscii(m,filename,cb);
} }
static int OpenBinary( OpenMeshType &m, const char * filename, CallBackPos *cb=0) static int OpenBinary( OpenMeshType &m, const char * filename, int &loadMask, CallBackPos *cb=0)
{ {
FILE *fp; FILE *fp;
fp = fopen(filename, "rb"); fp = fopen(filename, "rb");
@ -135,6 +178,10 @@ static int OpenBinary( OpenMeshType &m, const char * filename, CallBackPos *cb=0
return E_CANTOPEN; return E_CANTOPEN;
} }
bool magicsMode;
if(!IsSTLColored(filename,magicsMode))
loadMask = loadMask & (~Mask::IOM_FACECOLOR);
int facenum; int facenum;
fseek(fp, STL_LABEL_SIZE, SEEK_SET); fseek(fp, STL_LABEL_SIZE, SEEK_SET);
fread(&facenum, sizeof(int), 1, fp); fread(&facenum, sizeof(int), 1, fp);
@ -145,12 +192,17 @@ static int OpenBinary( OpenMeshType &m, const char * filename, CallBackPos *cb=0
// For each triangle read the normal, the three coords and a short set to zero // For each triangle read the normal, the three coords and a short set to zero
for(int i=0;i<facenum;++i) for(int i=0;i<facenum;++i)
{ {
short attr; unsigned short attr;
Point3f norm; Point3f norm;
Point3f tri[3]; Point3f tri[3];
fread(&norm,sizeof(Point3f),1,fp); fread(&norm,sizeof(Point3f),1,fp);
fread(&tri,sizeof(Point3f),3,fp); fread(&tri,sizeof(Point3f),3,fp);
fread(&attr,sizeof(short),1,fp); fread(&attr,sizeof(unsigned short),1,fp);
if(tri::HasPerFaceColor(m) && (loadMask & Mask::IOM_FACECOLOR) )
{
if(magicsMode) (*fi).C()= Color4b::FromUnsignedR5G5B5(attr);
else (*fi).C()= Color4b::FromUnsignedB5G5R5(attr);
}
for(int k=0;k<3;++k) for(int k=0;k<3;++k)
{ {
(*vi).P().Import(tri[k]); (*vi).P().Import(tri[k]);