Commit 2889f038 authored by Isaak Lim's avatar Isaak Lim
Browse files

added the option for various file writers to specify the precision when writing to an ascii file

#refs 1157

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@15706 383ad7c9-94d9-4d36-a494-682f7c89f535
parent b6c8b125
......@@ -46,9 +46,12 @@
#ifdef ENABLE_BSPLINECURVE_SUPPORT
bool FileOBJPlugin::writeCurve(std::ostream& _out, QString _filename, BSplineCurve* _curve )
{
if ( !OpenFlipper::Options::savingSettings() && saveOptions_ != 0) {
_out.precision(savePrecision_->value());
}
_out << "# " << _filename.toStdString() << "\n";
// save control points (coordinates)
for (uint i = 0; i < _curve->n_control_points(); ++i){
ACG::Vec3d cp = _curve->get_control_point(i);
......@@ -80,6 +83,9 @@ bool FileOBJPlugin::writeCurve(std::ostream& _out, QString _filename, BSplineCur
#ifdef ENABLE_BSPLINESURFACE_SUPPORT
bool FileOBJPlugin::writeSurface(std::ostream& _out, QString _filename, BSplineSurface* _surface ){
if ( !OpenFlipper::Options::savingSettings() && saveOptions_ != 0) {
_out.precision(savePrecision_->value());
}
_out << "# " << _filename.toStdString() << "\n";
......
......@@ -77,6 +77,8 @@ FileOBJPlugin::FileOBJPlugin()
saveTextures_(0),
saveCopyTextures_(0),
saveCreateTexFolder_(0),
savePrecisionLabel_(0),
savePrecision_(0),
saveDefaultButton_(0),
triMeshHandling_(0),
loadVertexColor_(0),
......@@ -2095,6 +2097,15 @@ QWidget* FileOBJPlugin::saveOptionsWidget(QString /*_currentFilter*/) {
saveCreateTexFolder_ = new QCheckBox("Create Textures Folder");
layout->addWidget(saveCreateTexFolder_);
savePrecisionLabel_ = new QLabel("Writer Precision");
layout->addWidget(savePrecisionLabel_);
savePrecision_ = new QSpinBox();
savePrecision_->setMinimum(1);
savePrecision_->setMaximum(12);
savePrecision_->setValue(6);
layout->addWidget(savePrecision_);
saveDefaultButton_ = new QPushButton("Make Default");
layout->addWidget(saveDefaultButton_);
......
......@@ -100,14 +100,14 @@ class FileOBJPlugin : public QObject, BaseInterface, FileInterface, LoadSaveInte
void updateView();
void deleteObject( int _id );
// StatusbarInterface
void showStatusMessage(QString _message, int _timeout = 0);
void setStatus( ApplicationStatus::applicationStatus _status);
//RPCInterface
void pluginExists( QString _pluginName , bool& _exists );
//TextureInterface
void setTextureMode(QString _textureName, QString _mode, int _id );
void switchTexture( QString _textureName, int _id );
......@@ -118,7 +118,7 @@ class FileOBJPlugin : public QObject, BaseInterface, FileInterface, LoadSaveInte
void textureName (int, int, QString &);
void getSubTextures (int, QString, QStringList &);
void textureIndexPropertyName(int, QString&);
private slots:
void fileOpened( int /*_id*/ ){};
......@@ -126,17 +126,17 @@ class FileOBJPlugin : public QObject, BaseInterface, FileInterface, LoadSaveInte
void noguiSupported( ) {} ;
void initializePlugin();
/// Slot called when user wants to save the given Load options as default
void slotLoadDefault();
/// Slot called when user wants to save the given Save options as default
void slotSaveDefault();
void slotHandleCheckBoxes(bool _checked);
public :
FileOBJPlugin();
~FileOBJPlugin() {};
......@@ -159,16 +159,16 @@ class FileOBJPlugin : public QObject, BaseInterface, FileInterface, LoadSaveInte
/// load object and force type
int loadObject(QString _filename, DataType _type);
bool saveObject(int _id, QString _filename);
QString version() { return QString("1.0"); };
QString version() { return QString("1.0"); };
private:
/// Reader functions
void checkTypes(QString _filename, OBJImporter& _importer, QStringList& _includes);
bool readMaterial(QString _filename, OBJImporter& _importer);
void readOBJFile(QString _filename, OBJImporter& _importer);
void createAllGroupObjects(OBJImporter& _importer);
......@@ -176,10 +176,10 @@ class FileOBJPlugin : public QObject, BaseInterface, FileInterface, LoadSaveInte
template <class MeshT>
void backupTextureCoordinates(MeshT& _mesh);
void addTextures(OBJImporter& _importer, int _objectID );
/// Convert non-valid filenames (e.g. of groups that end with .jpg) to valid .objs.
void convertToOBJName(QString& _name);
private :
/// List that contains the material properties
......@@ -187,14 +187,14 @@ class FileOBJPlugin : public QObject, BaseInterface, FileInterface, LoadSaveInte
template< class MeshT >
Material& getMaterial(MeshT& _mesh, const OpenMesh::FaceHandle& _fh, int _objId);
///writer functions
template< class MeshT >
bool writeMaterial(QString _filename, MeshT& _mesh, int _objId );
template< class MeshT >
bool writeMesh(std::ostream& _out, QString _filename, MeshT& _mesh, int _objId );
bool writeMesh(std::ostream& _out, QString _filename, MeshT& _mesh, int _objId);
#ifdef ENABLE_BSPLINECURVE_SUPPORT
bool writeCurve(std::ostream& _out, QString _filename, BSplineCurve* _curve );
#endif
......@@ -202,14 +202,14 @@ class FileOBJPlugin : public QObject, BaseInterface, FileInterface, LoadSaveInte
#ifdef ENABLE_BSPLINESURFACE_SUPPORT
bool writeSurface(std::ostream& _out, QString _filename, BSplineSurface* _surface );
#endif
private:
//Option Widgets
QWidget* loadOptions_;
QWidget* saveOptions_;
QCheckBox* saveBinary_;
QCheckBox* saveVertexColor_;
QCheckBox* saveFaceColor_;
......@@ -219,8 +219,10 @@ class FileOBJPlugin : public QObject, BaseInterface, FileInterface, LoadSaveInte
QCheckBox* saveTextures_;
QCheckBox* saveCopyTextures_;
QCheckBox* saveCreateTexFolder_;
QLabel* savePrecisionLabel_;
QSpinBox* savePrecision_;
QPushButton* saveDefaultButton_;
QComboBox* triMeshHandling_;
QCheckBox* loadVertexColor_;
......@@ -230,10 +232,10 @@ class FileOBJPlugin : public QObject, BaseInterface, FileInterface, LoadSaveInte
QCheckBox* loadTexCoords_;
QCheckBox* loadTextures_;
QPushButton* loadDefaultButton_;
bool forceTriangleMesh_;
bool forcePolyMesh_;
QString textureIndexPropertyName_;
bool textureIndexPropFetched_;
std::map<int,QString> texIndexFileMap_;
......
......@@ -67,21 +67,21 @@ bool FileOBJPlugin::writeMaterial(QString _filename, MeshT& _mesh, int _objId )
optionCreateTexFolder = saveCreateTexFolder_->isChecked();
}
// \TODO Fetch options from ini states if dialog box is not available
std::fstream matStream( _filename.toStdString().c_str(), std::ios_base::out );
if ( !matStream ){
emit log(LOGERR, tr("writeMaterial : cannot not open file %1").arg(_filename) );
return false;
return false;
}
// \TODO Implement setting of all colors (diffuse, ambient and specular)
// There's only diffuse colors so far
OpenMesh::Vec4f c;
materials_.clear();
//iterate over faces
typename MeshT::FaceIter f_it;
typename MeshT::FaceIter f_end = _mesh.faces_end();
......@@ -102,7 +102,7 @@ bool FileOBJPlugin::writeMaterial(QString _filename, MeshT& _mesh, int _objId )
matStream << "Tr " << mat.Tr() << std::endl;
}
matStream << "illum 1" << std::endl;
// Write out texture info
if(optionTextures && mat.has_Texture()) {
if(optionCopyTextures) {
......@@ -121,7 +121,7 @@ bool FileOBJPlugin::writeMaterial(QString _filename, MeshT& _mesh, int _objId )
matStream << "map_Kd " << mat.map_Kd() << std::endl;
}
}
matStream << std::endl;
}
......@@ -140,15 +140,15 @@ Material& FileOBJPlugin::getMaterial(MeshT& _mesh, const OpenMesh::FaceHandle& _
if ( !OpenFlipper::Options::savingSettings() && saveOptions_ != 0)
optionColorAlpha = saveAlpha_->isChecked();
// \TODO Fetch options from ini states if dialog box is not available
OpenMesh::Vec4f c = _mesh.color( _fh );
// First off, try to fetch texture index of current face/object...
if(!textureIndexPropFetched_) {
emit textureIndexPropertyName(_objId, textureIndexPropertyName_);
textureIndexPropFetched_ = true;
}
int texIndex = -1;
OpenMesh::FPropHandleT< int > texture_index_property;
if ( _mesh.get_property_handle(texture_index_property, textureIndexPropertyName_.toStdString()) ) {
......@@ -162,15 +162,15 @@ Material& FileOBJPlugin::getMaterial(MeshT& _mesh, const OpenMesh::FaceHandle& _
emit getCurrentTexture(_objId, texName);
emit textureIndex(texName, _objId, texIndex);
}
QString filename;
bool hasTexture = false;
if(texIndex != -1) {
// Search for texture index in local map
std::map<int,QString>::iterator it = texIndexFileMap_.find(texIndex);
if(it != texIndexFileMap_.end()) {
// We already know this file
filename = (*it).second;
......@@ -179,7 +179,7 @@ Material& FileOBJPlugin::getMaterial(MeshT& _mesh, const OpenMesh::FaceHandle& _
// A new texture file has been found
QString texName;
emit textureName(_objId, texIndex, texName);
if(texName != "NOT_FOUND") {
emit textureFilename( _objId, texName, filename );
// => Add to local map
......@@ -188,9 +188,9 @@ Material& FileOBJPlugin::getMaterial(MeshT& _mesh, const OpenMesh::FaceHandle& _
}
}
}
for (MaterialList::iterator it = materials_.begin(); it != materials_.end(); ++it) {
// No texture has been found
if(!hasTexture) {
// ... just look for diffuse color in materials list
......@@ -217,7 +217,7 @@ Material& FileOBJPlugin::getMaterial(MeshT& _mesh, const OpenMesh::FaceHandle& _
// Set texture info
if(hasTexture)
mat.set_map_Kd(filename.toStdString(), texIndex);
materials_.insert(std::pair<std::string, Material>("Material" + mat.material_number(), mat));
MaterialList::iterator it = materials_.end();
it--;
......@@ -230,8 +230,8 @@ Material& FileOBJPlugin::getMaterial(MeshT& _mesh, const OpenMesh::FaceHandle& _
//-----------------------------------------------------------------------------------------------------
template< class MeshT >
bool FileOBJPlugin::writeMesh(std::ostream& _out, QString _filename, MeshT& _mesh, int _objId ){
bool FileOBJPlugin::writeMesh(std::ostream& _out, QString _filename, MeshT& _mesh, int _objId){
unsigned int i, nV, idx;
Vec3f v, n;
Vec2f t(0.0f,0.0f);
......@@ -247,9 +247,9 @@ bool FileOBJPlugin::writeMesh(std::ostream& _out, QString _filename, MeshT& _mes
bool optionTextures = false;
bool optionCopyTextures = false;
bool optionCreateTexFolder = false;
QFileInfo fi(_filename);
// check options
if ( !OpenFlipper::Options::savingSettings() && saveOptions_ != 0) {
optionFaceColors = saveFaceColor_->isChecked();
......@@ -259,12 +259,13 @@ bool FileOBJPlugin::writeMesh(std::ostream& _out, QString _filename, MeshT& _mes
optionCopyTextures = saveCopyTextures_->isChecked();
optionCreateTexFolder = saveCreateTexFolder_->isChecked();
optionColorAlpha = saveAlpha_->isChecked();
}
_out.precision(savePrecision_->value());
};
// \TODO Fetch options from ini states if dialog box is not available
//create material file if needed
if ( optionFaceColors || optionTextures ){
QString matFile = fi.absolutePath() + QDir::separator() + fi.baseName() + ".mtl";
useMaterial = writeMaterial(matFile, _mesh, _objId);
......@@ -282,7 +283,7 @@ bool FileOBJPlugin::writeMesh(std::ostream& _out, QString _filename, MeshT& _mes
// they can easily be referenced for face definitions
// later on
std::map<typename MeshT::VertexHandle, int> vtMapV;
int cf = 1;
// vertex data (point, normals, texcoords)
for (i=0, nV=_mesh.n_vertices(); i<nV; ++i)
......@@ -290,7 +291,7 @@ bool FileOBJPlugin::writeMesh(std::ostream& _out, QString _filename, MeshT& _mes
vh = typename MeshT::VertexHandle(i);
v = _mesh.point(vh);
n = _mesh.normal(vh);
if ( _mesh.has_vertex_texcoords2D() && !_mesh.has_halfedge_texcoords2D() )
t = _mesh.texcoord2D(vh);
......@@ -308,16 +309,16 @@ bool FileOBJPlugin::writeMesh(std::ostream& _out, QString _filename, MeshT& _mes
cf++;
}
}
typename MeshT::FaceVertexIter fv_it;
typename MeshT::FaceHalfedgeIter fh_it;
typename MeshT::FaceIter f_it;
// Store indices of vertex coordinate (in obj-file)
// in map such that the corresponding halfedge
// can easily be found later on
std::map<typename MeshT::HalfedgeHandle, int> vtMap;
// If mesh has halfedge tex coords, write them out instead of vertex texcoords
if(_mesh.has_halfedge_texcoords2D()) {
int count = 1;
......@@ -332,11 +333,11 @@ bool FileOBJPlugin::writeMesh(std::ostream& _out, QString _filename, MeshT& _mes
}
Material lastMat;
for (f_it = _mesh.faces_begin(); f_it != _mesh.faces_end(); ++f_it){
if (useMaterial && optionFaceColors) {
Material& material = getMaterial(_mesh, f_it.handle(), _objId);
// If we are ina a new material block, specify in the file which material to use
......@@ -350,7 +351,7 @@ bool FileOBJPlugin::writeMesh(std::ostream& _out, QString _filename, MeshT& _mes
// Write out face information
for(fh_it=_mesh.fh_iter(f_it.handle()); fh_it; ++fh_it) {
// Write vertex index
idx = _mesh.to_vertex_handle(fh_it.handle()).idx() + 1;
_out << " " << idx;
......@@ -378,10 +379,10 @@ bool FileOBJPlugin::writeMesh(std::ostream& _out, QString _filename, MeshT& _mes
if ( optionVertexNormals )
_out << idx;
}
_out << std::endl;
}
// Copy texture files (if demanded)
if(optionCopyTextures) {
// Only test existence of folder once
......@@ -389,12 +390,12 @@ bool FileOBJPlugin::writeMesh(std::ostream& _out, QString _filename, MeshT& _mes
bool testedOnce = false;
for(MaterialList::iterator it = materials_.begin(); it != materials_.end(); ++it) {
Material& mat = (*it).second;
if(!mat.has_Texture()) continue;
QImage img(mat.map_Kd().c_str());
QFileInfo img_f(mat.map_Kd().c_str());
if(img.isNull()) {
// Something happened wrong
emit log(LOGERR, tr("An error occurred when trying to copy a texture file."));
......@@ -411,7 +412,7 @@ bool FileOBJPlugin::writeMesh(std::ostream& _out, QString _filename, MeshT& _mes
img.save(fi.absolutePath() + QDir::separator() + fi.baseName() + "_textures" + QDir::separator() + img_f.fileName());
testedOnce = true;
}
} else {
img.save(fi.absolutePath() + QDir::separator() + img_f.fileName());
}
......@@ -424,8 +425,8 @@ bool FileOBJPlugin::writeMesh(std::ostream& _out, QString _filename, MeshT& _mes
textureIndexPropFetched_ = false;
return true;
}
}
This diff is collapsed.
......@@ -84,7 +84,7 @@ class FileOFFPlugin : public QObject, BaseInterface, FileInterface, LoadSaveInte
void updateView();
void deleteObject( int _id );
// StatusbarInterface
void showStatusMessage(QString _message, int _timeout = 0);
void setStatus( ApplicationStatus::applicationStatus _status);
......@@ -96,16 +96,16 @@ class FileOFFPlugin : public QObject, BaseInterface, FileInterface, LoadSaveInte
void noguiSupported( ) {} ;
void initializePlugin();
/// Slot called when user wants to save the given Load options as default
void slotLoadDefault();
/// Slot called when user wants to save the given Save options as default
void slotSaveDefault();
public :
FileOFFPlugin();
~FileOFFPlugin() {};
......@@ -128,77 +128,77 @@ class FileOFFPlugin : public QObject, BaseInterface, FileInterface, LoadSaveInte
/// Loads Object with given datatype
int loadObject(QString _filename, DataType _type);
bool saveObject(int _id, QString _filename);
QString version() { return QString("1.1"); };
private:
/// Before Parsing the actual file, read all features supported
bool readFileOptions(QString _filename, OFFImporter& _importer);
/// Read OFF file and parse it
bool readOFFFile(QString _filename, OFFImporter& _importer);
/// Parse ascii OFF file
bool parseASCII(std::istream& _in, OFFImporter& _importer, DataType _type, QString& _objectName);
/// Parse binary OFF file
bool parseBinary(std::istream& _in, OFFImporter& _importer, DataType _type, QString& _objectName);
/// Get color type
int getColorType(std::string& _line, bool _texCoordsAvailable);
/// Update user options depending on which options have been selected
/// on the load dialog
void updateUserOptions();
/// Test if there are face color components (_nV is the initial face valence)
bool extendedFaceColorTest(std::istream& _in, uint _nV, uint _nF, int _nB) const;
// Binary reader and writer helpers
void readValue(std::istream& _in, float& _value) const {
float tmp;
OpenMesh::IO::restore( _in , tmp, false ); //assuming LSB byte order
_value = tmp;
}
void readValue(std::istream& _in, int& _value) const {
OpenMesh::IO::int32_t tmp;
OpenMesh::IO::restore( _in , tmp, false ); //assuming LSB byte order
_value = tmp;
}
void readValue(std::istream& _in, unsigned int& _value) const {
OpenMesh::IO::uint32_t tmp;
OpenMesh::IO::restore( _in , tmp, false ); //assuming LSB byte order
_value = tmp;
}
void writeValue(std::ostream& _out, int value) const {
OpenMesh::IO::uint32_t tmp = value;
OpenMesh::IO::store(_out, tmp, false);
}
void writeValue(std::ostream& _out, unsigned int value) const {
OpenMesh::IO::uint32_t tmp = value;
OpenMesh::IO::store(_out, tmp, false);
}
void writeValue(std::ostream& _out, float value) const {
float tmp = value;
OpenMesh::IO::store(_out, tmp, false);
}
void trimString( std::string& _string);
/** \brief Function to retrieve next line
*
* @param ifs The input stream we operate on
......@@ -210,19 +210,19 @@ class FileOFFPlugin : public QObject, BaseInterface, FileInterface, LoadSaveInte
/// Check for degenerate faces before adding them
bool checkDegenerateFace(const std::vector<VertexHandle>& _v);
/// Writer function
template< class MeshT >
bool writeMesh(std::ostream& _out, MeshT& _mesh );
/// Write binary mesh data to file
template< class MeshT >
bool writeBinaryData(std::ostream& _out, MeshT& _mesh );
/// Write ASCII mesh data to file
template< class MeshT >
bool writeASCIIData(std::ostream& _out, MeshT& _mesh );
/// backup per vertex/face texture coordinates
template <class MeshT>
void backupTextureCoordinates(MeshT& _mesh);
......@@ -230,15 +230,17 @@ class FileOFFPlugin : public QObject, BaseInterface, FileInterface, LoadSaveInte
//Option Widgets
QWidget* loadOptions_;
QWidget* saveOptions_;
QCheckBox* saveBinary_;
QCheckBox* saveVertexColor_;
QCheckBox* saveFaceColor_;
QCheckBox* saveAlpha_;
QCheckBox* saveNormals_;
QCheckBox* saveTexCoords_;
QLabel* savePrecisionLabel_;
QSpinBox* savePrecision_;
QPushButton* saveDefaultButton_;
QComboBox* triMeshHandling_;
QCheckBox* loadVertexColor_;
......@@ -248,10 +250,10 @@ class FileOFFPlugin : public QObject, BaseInterface, FileInterface, LoadSaveInte
QCheckBox* loadTexCoords_;
QCheckBox* loadCheckManifold_;
QPushButton* loadDefaultButton_;
unsigned int userReadOptions_;
unsigned int userWriteOptions_;