Commit f5e407ca authored by Jan Möbius's avatar Jan Möbius

Plugin Collections for file, type and postprocessor plugins



git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@15803 383ad7c9-94d9-4d36-a494-682f7c89f535
parents
/*===========================================================================*\
* *
* OpenFlipper *
* Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
* www.openflipper.org *
* *
*--------------------------------------------------------------------------- *
* This file is part of OpenFlipper. *
* *
* OpenFlipper is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version with the *
* following exceptions: *
* *
* If other files instantiate templates or use macros *
* or inline functions from this file, or you compile this file and *
* link it with other files to produce an executable, this file does *
* not by itself cause the resulting executable to be covered by the *
* GNU Lesser General Public License. This exception does not however *
* invalidate any other reasons why the executable file might be *
* covered by the GNU Lesser General Public License. *
* *
* OpenFlipper is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU LesserGeneral Public *
* License along with OpenFlipper. If not, *
* see <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
/*===========================================================================*\
* *
* $Revision$ *
* $LastChangedBy$ *
* $Date$ *
* *
\*===========================================================================*/
#include "FileOBJ.hh"
#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);
_out << "v " << cp[0] << " " << cp[1] << " " << cp[2] << "\n";
}
_out << "cstype bspline\n";
_out << "deg " << _curve->degree() << "\n";
_out << "g " << _filename.toStdString() << "\n";
// save control polygon
_out << "curv " << _curve->get_knot(0) << " " << _curve->get_knot(_curve->n_knots()-1) << " ";
// save control point indices
for (unsigned int i = 0; i < _curve->n_control_points(); ++i)
_out << i+1 << " "; // obj enumerates the cps starting with 1
_out << "\n";
_out << "parm u ";
// save knotvector
for (unsigned int i = 0; i < _curve->n_knots(); ++i)
_out << _curve->get_knot(i) << " ";
_out << "\n";
_out << "end";
return true;
}
#endif
#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";
// save control net (coordinates)
unsigned int num_cp_m = _surface->n_control_points_m();
unsigned int num_cp_n = _surface->n_control_points_n();
for (unsigned int i = 0; i < num_cp_m; ++i)
{
for (unsigned int j = 0; j < num_cp_n; ++j)
{
ACG::Vec3d cp = (*_surface)(i,j);
_out << "v " << cp[0] << " " << cp[1] << " " << cp[2] << "\n";
}
}
_out << "cstype bspline\n";
_out << "deg " << _surface->degree_m() << " " << _surface->degree_n() << "\n";
_out << "g " << _filename.toStdString() << "\n";
// save control polygon
_out << "surf " << _surface->get_knot_m(0) << " " << _surface->get_knot_m(_surface->n_knots_m()-1) << " "
<< _surface->get_knot_n(0) << " " << _surface->get_knot_n(_surface->n_knots_n()-1) << " ";
// save control point indices
for (unsigned int j = 0; j < num_cp_n; ++j)
for (unsigned int i = 0; i < num_cp_m; ++i)
_out << (i*num_cp_n) + j+1 << " "; // obj enumerates the cps starting with 1
_out << "\n";
_out << "parm u ";
// save knotvector in m direction
for (unsigned int i = 0; i < _surface->n_knots_m(); ++i)
_out << _surface->get_knot_m(i) << " ";
_out << "\n";
_out << "parm v ";
// save knotvector in n direction
for (unsigned int i = 0; i < _surface->n_knots_n(); ++i)
_out << _surface->get_knot_n(i) << " ";
_out << "\n";
_out << "end";
return true;
}
#endif
include (plugin)
if (EXISTS ${CMAKE_SOURCE_DIR}/ObjectTypes/BSplineCurve)
add_definitions (-DENABLE_BSPLINECURVE_SUPPORT)
endif ()
if (EXISTS ${CMAKE_SOURCE_DIR}/ObjectTypes/BSplineSurface)
add_definitions (-DENABLE_BSPLINESURFACE_SUPPORT)
endif ()
openflipper_plugin ()
This diff is collapsed.
/*===========================================================================*\
* *
* OpenFlipper *
* Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
* www.openflipper.org *
* *
*--------------------------------------------------------------------------- *
* This file is part of OpenFlipper. *
* *
* OpenFlipper is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version with the *
* following exceptions: *
* *
* If other files instantiate templates or use macros *
* or inline functions from this file, or you compile this file and *
* link it with other files to produce an executable, this file does *
* not by itself cause the resulting executable to be covered by the *
* GNU Lesser General Public License. This exception does not however *
* invalidate any other reasons why the executable file might be *
* covered by the GNU Lesser General Public License. *
* *
* OpenFlipper is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU LesserGeneral Public *
* License along with OpenFlipper. If not, *
* see <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
/*===========================================================================*\
* *
* $Revision$ *
* $LastChangedBy$ *
* $Date$ *
* *
\*===========================================================================*/
#ifndef FILEOBJPLUGIN_HH
#define FILEOBJPLUGIN_HH
#include <QObject>
#include <OpenFlipper/common/Types.hh>
#include <OpenFlipper/BasePlugin/BaseInterface.hh>
#include <OpenFlipper/BasePlugin/FileInterface.hh>
#include <OpenFlipper/BasePlugin/LoadSaveInterface.hh>
#include <OpenFlipper/BasePlugin/LoggingInterface.hh>
#include <OpenFlipper/BasePlugin/ScriptInterface.hh>
#include <OpenFlipper/BasePlugin/TypeInterface.hh>
#include <OpenFlipper/BasePlugin/RPCInterface.hh>
#include <OpenFlipper/BasePlugin/StatusbarInterface.hh>
#include <OpenFlipper/BasePlugin/TextureInterface.hh>
#include <ObjectTypes/PolyMesh/PolyMesh.hh>
#include <ObjectTypes/TriangleMesh/TriangleMesh.hh>
#ifdef ENABLE_BSPLINECURVE_SUPPORT
#include <ObjectTypes/BSplineCurve/BSplineCurve.hh>
#endif
#ifdef ENABLE_BSPLINESURFACE_SUPPORT
#include <ObjectTypes/BSplineSurface/BSplineSurface.hh>
#endif
#include "OBJImporter.hh"
#include "Material.hh"
enum ReaderMode
{
NONE = 0,
CURVE = 1,
SURFACE = 1 << 1
};
class FileOBJPlugin : public QObject, BaseInterface, FileInterface, LoadSaveInterface,
LoggingInterface, ScriptInterface, StatusbarInterface, RPCInterface, TextureInterface
{
Q_OBJECT
Q_INTERFACES(FileInterface)
Q_INTERFACES(LoadSaveInterface)
Q_INTERFACES(LoggingInterface)
Q_INTERFACES(BaseInterface)
Q_INTERFACES(ScriptInterface)
Q_INTERFACES(StatusbarInterface)
Q_INTERFACES(RPCInterface)
Q_INTERFACES(TextureInterface)
signals:
void openedFile( int _id );
void addEmptyObject( DataType _type, int& _id);
void load(QString _filename, DataType _type, int& _id);
void save(int _id , QString _filename );
void log(Logtype _type, QString _message);
void log(QString _message);
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 );
void addMultiTexture( QString _textureGroup, QString _name, QString _filename, int _id, int& _textureId);
void textureFilename( int /*_id*/, QString /*_textureName*/, QString& /*_textureFilename*/ );
void textureIndex(QString _name,int _id, int& _index);
void getCurrentTexture(int /*_id*/, QString& /*_name*/);
void textureName (int, int, QString &);
void getSubTextures (int, QString, QStringList &);
void textureIndexPropertyName(int, QString&);
private slots:
void fileOpened( int /*_id*/ ){};
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() {};
QString name() { return (QString("FileOBJ")); };
QString description( ) { return (QString(tr("Load/Save OBJ-Files"))); };
DataType supportedType();
QString getSaveFilters();
QString getLoadFilters();
QWidget* saveOptionsWidget(QString /*_currentFilter*/);
QWidget* loadOptionsWidget(QString /*_currentFilter*/);
public slots:
/// Loads Object and converts it to a triangle mesh if possible
int loadObject(QString _filename);
/// load object and force type
int loadObject(QString _filename, DataType _type);
bool saveObject(int _id, QString _filename);
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);
//void addNewObject(OBJImporter& _importer, QString _name );
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
MaterialList materials_;
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);
#ifdef ENABLE_BSPLINECURVE_SUPPORT
bool writeCurve(std::ostream& _out, QString _filename, BSplineCurve* _curve );
#endif
#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_;
QCheckBox* saveAlpha_;
QCheckBox* saveNormals_;
QCheckBox* saveTexCoords_;
QCheckBox* saveTextures_;
QCheckBox* saveCopyTextures_;
QCheckBox* saveCreateTexFolder_;
QLabel* savePrecisionLabel_;
QSpinBox* savePrecision_;
QPushButton* saveDefaultButton_;
QComboBox* triMeshHandling_;
QCheckBox* loadVertexColor_;
QCheckBox* loadFaceColor_;
QCheckBox* loadAlpha_;
QCheckBox* loadNormals_;
QCheckBox* loadTexCoords_;
QCheckBox* loadTextures_;
QPushButton* loadDefaultButton_;
bool forceTriangleMesh_;
bool forcePolyMesh_;
QString textureIndexPropertyName_;
bool textureIndexPropFetched_;
std::map<int,QString> texIndexFileMap_;
};
#if defined(INCLUDE_TEMPLATES) && !defined(FILEOBJPLUGIN_C)
#define FILEOBJPLUGIN_TEMPLATES
#include "FileOBJT.cc"
#endif
#endif //FILEOBJPLUGIN_HH
/*===========================================================================*\
* *
* OpenFlipper *
* Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
* www.openflipper.org *
* *
*--------------------------------------------------------------------------- *
* This file is part of OpenFlipper. *
* *
* OpenFlipper is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version with the *
* following exceptions: *
* *
* If other files instantiate templates or use macros *
* or inline functions from this file, or you compile this file and *
* link it with other files to produce an executable, this file does *
* not by itself cause the resulting executable to be covered by the *
* GNU Lesser General Public License. This exception does not however *
* invalidate any other reasons why the executable file might be *
* covered by the GNU Lesser General Public License. *
* *
* OpenFlipper is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU LesserGeneral Public *
* License along with OpenFlipper. If not, *
* see <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
/*===========================================================================*\
* *
* $Revision$ *
* $LastChangedBy$ *
* $Date$ *
* *
\*===========================================================================*/
#define FILEOBJPLUGIN_C
#include "FileOBJ.hh"
#include <OpenMesh/Core/Utils/color_cast.hh>
#include <OpenMesh/Core/Geometry/VectorT.hh>
//-----------------------------------------------------------------------------------------------------
template< class MeshT >
bool FileOBJPlugin::writeMaterial(QString _filename, MeshT& _mesh, int _objId )
{
bool optionColorAlpha = false;
bool optionTextures = false;
bool optionCopyTextures = false;
bool optionCreateTexFolder = false;
// check options
if ( !OpenFlipper::Options::savingSettings() && saveOptions_ != 0) {
optionColorAlpha = saveAlpha_->isChecked();
optionTextures = saveTextures_->isChecked();
optionCopyTextures = saveCopyTextures_->isChecked();
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;
}
// \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();
// Prepare materials ( getMaterial handles a list that is set up by this call)
for (f_it = _mesh.faces_begin(); f_it != f_end; ++f_it){
getMaterial(_mesh, f_it.handle(), _objId);
}
//write the materials
for(MaterialList::iterator it = materials_.begin(); it != materials_.end(); ++it) {
Material& mat = (*it).second;
matStream << "newmtl " << mat << std::endl;
matStream << "Ka 0.5000 0.5000 0.5000" << std::endl;
ACG::Vec3f c = mat.Kd();
matStream << "Kd " << c[0] << " " << c[1] << " " << c[2] << std::endl;
if(optionColorAlpha) {
matStream << "Tr " << mat.Tr() << std::endl;
}
matStream << "illum 1" << std::endl;
// Write out texture info
if(optionTextures && mat.has_Texture()) {
if(optionCopyTextures) {
// Use file path in target folder (relative)
QFileInfo file(mat.map_Kd().c_str());
if(optionCreateTexFolder) {
QFileInfo materialFilename(_filename);
matStream << "map_Kd " << materialFilename.baseName().toStdString() << "_textures" << QDir::separator().toAscii()
<< file.fileName().toStdString() << std::endl;
} else {
matStream << "map_Kd " << file.fileName().toStdString() << std::endl;
}
} else {
// Use original file path
matStream << "map_Kd " << mat.map_Kd() << std::endl;
}
}
matStream << std::endl;
}
matStream.close();
return true;
}
//-----------------------------------------------------------------------------------------------------
template< class MeshT >
Material& FileOBJPlugin::getMaterial(MeshT& _mesh, const OpenMesh::FaceHandle& _fh, int _objId)
{
// check options
bool optionColorAlpha = false;
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()) ) {
texIndex = _mesh.property(texture_index_property, _fh);
} else if ( _mesh.get_property_handle(texture_index_property, "f:textureindex") ) {
texIndex = _mesh.property(texture_index_property, _fh);
} else if(_mesh.has_face_texture_index()) {
texIndex = _mesh.texture_index(_fh);
} else {
QString texName;
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;
hasTexture = true;
} else {
// 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
texIndexFileMap_.insert(std::pair<int,QString>(texIndex, filename));
hasTexture = true;
}
}
}
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
if(((*it).second).Kd() == ACG::Vec3f(c[0], c[1], c[2]) &&
((optionColorAlpha && ((*it).second).Tr() == c[3]) || !optionColorAlpha))
return (*it).second;
} else {
// Texture has been found, look for both, matching texture and color
QString mKd(((*it).second).map_Kd().c_str());
if((((*it).second).Kd() == ACG::Vec3f(c[0], c[1], c[2]) &&
((optionColorAlpha && ((*it).second).Tr() == c[3]) || !optionColorAlpha)) &&
(filename == mKd && ((*it).second).map_Kd_index() == texIndex))
return (*it).second;
}
}
// If not found, add new material(s)
Material mat;
// Set diffuse color
mat.set_Kd(c[0], c[1], c[2]);
// Add transparency if available
if(optionColorAlpha) mat.set_Tr(c[3]);
mat.material_number(materials_.size());
// 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--;
return (*it).second;
}
//-----------------------------------------------------------------------------------------------------
template< class MeshT >
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);
typename MeshT::VertexHandle vh;
std::vector<typename MeshT::VertexHandle> vhandles;
bool useMaterial = false;
OpenMesh::Vec4f c;