#ifndef TXS_SLIDEWRITER_GUARD #define TXS_SLIDEWRITER_GUARD #include "txs_globals.h" #include "TxsMesh.h" #include namespace TXS { class SlideWriter { private: ofstream slfOut; ofstream batOut; char *outDir; TxsMesh *mesh; int level; /* * We need unique names for verts and faces for SLF files. * These functions just take the address and append some letter to them. * So they are unique within this proceses. */ char *vertUseName(char *buf, TxsFace *f, TxsVertex *v) { sprintf(buf, "v%d_%d", (long)f, (long)v); return buf; } char *faceSurfaceName(char *buf, TxsFace *f, TiffImage *t) { sprintf(buf, "s%d", (long)f); return buf; } char *faceName(char *buf, TxsFace *f) { sprintf(buf, "f%d", (long)f); return buf; } // unused char *tiffFilename(char *surfaceNameBuf) { strcat(surfaceNameBuf, ".tif"); return surfaceNameBuf; } /* * the main output functions */ void outputVertex(TxsVertex *v) { list faces = v->getIncidentFaces(); Point p = v->getPoint(); char pointName[64]; // visit each incident face list::iterator fit; for(fit = faces.begin(); fit != faces.end(); ++fit) { TxsFace* f = *fit; // get the UV coord for this vert use UVs uv = f->calcUV(p); // NOTE: SLIDE inteprets v=0 as the texture TOP going down // but we always assume v=0 is the texture bottom going up // so, we must flip and translate uv.v = 1.0 - uv.v; // get name for vert use this->vertUseName(pointName, f, v); this->slfOut << "point " << pointName << " " << " (" << (p[0]) << " " << (p[1]) << " " << (p[2]) << ") " << " texture ( " << uv.u << " " << uv.v << " ) endpoint " << endl; } } /* return true to STOP traversing */ void outputFace(TxsFace *f) { char surfaceName[64]; char faceName[64]; char vertUseName[64]; char filename[64]; // get the texture image to write TiffImage *img = NULL; if(level == -1) { // get finest level image // if there was no level given img = f->getFinestPyLevel(); } else { img = f->getPyramid()->getLevel(level); } // get surface name for texture file this->faceSurfaceName(surfaceName, f, img); // output TiffImage to surface name.tif sprintf(filename, "%s/%s.tif", this->outDir, surfaceName); img->writeToTiff(filename); // output first half of tif->gif convert cmd // but the BATCH doesn't need the outDir sprintf(filename, "%s.tif", surfaceName); this->batOut << "convert " << filename << " -size 200% "/*"-gaussian 2 "*/; // output surface def // surface surf_arrows bitmap "Arrows_Y.GIF" SLF_TEXMAP endsurface // remember to use .gif, isntead of .tif // and don't use the directory sprintf(filename, "%s.gif", surfaceName); this->slfOut << "# FACE" << endl; this->slfOut << "surface " << surfaceName << " " << " bitmap \"" << filename << "\" SLF_TEXMAP " << " endsurface " << endl; // complete convert cmd this->batOut << " " << filename << endl; // now, finally output the face SLF // face name (v0 v1 v2) surface surfaceName endface this->faceName(faceName, f); this->slfOut << "face " << faceName << " ("; // now, output the verts // visit each vert use vector verts = f->getVertices(); vector::iterator vit; for(vit = verts.begin(); vit != verts.end(); ++vit) { TxsVertex* v = *vit; // get name for vert use this->vertUseName(vertUseName, f, v); // do it this->slfOut << vertUseName << " "; } // complete it this->slfOut << " ) surface " << surfaceName << " endface " << endl; } void outputMesh(TxsMesh *mesh) { // output verts list verts = mesh->getVerts(); list::iterator vit; for(vit = verts.begin(); vit != verts.end(); ++vit) { TxsVertex* v = *vit; this->outputVertex(v); } // output faces list faces = mesh->getFaces(); list::iterator fit; for(fit = faces.begin(); fit != faces.end(); ++fit) { TxsFace* f = *fit; this->outputFace(f); } // now put all faces together into the object // object txsObject (f1 f2 f3 ...) this->slfOut << "# OBJECT" << endl; this->slfOut << "object txsObject ( " << endl; char faceName[64]; for(fit = faces.begin(); fit != faces.end(); ++fit) { TxsFace* f = *fit; this->faceName(faceName, f); this->slfOut << " " << faceName << endl; } this->slfOut << ") " << endl << "solid SLF_SOLID" << endl << "shading SLF_FLAT" << endl << "endobject" << endl; // done! // this->slfOut.close(); this->batOut.close(); } public: SlideWriter() { this->outDir = NULL; this->mesh = NULL; } void write(char *outDir, TxsMesh *mesh, int level = -1) { this->outDir = outDir; this->mesh = mesh; this->level = level; char filename[100]; // the SLF file sprintf(filename, "%s/_open_me_in_slide_.slf", outDir); __LOG__("SLF out filename: \"" << filename << "\""); slfOut.open(filename); if(!slfOut.is_open()) { __ERROR__("SlideOutputMeshVisitor: couldn't open the file!"); } // the BAT file sprintf(filename, "%s/convert-TIFs.bat", outDir); __LOG__("BAT out filename: \"" << filename << "\""); batOut.open(filename); if(!batOut.is_open()) { __ERROR__("SlideOutputMeshVisitor: couldn't open the file!"); } this->outputMesh(this->mesh); } }; } #endif /* TXS_SLIDEWRITER_GUARD */