Commit 620f9a6d authored by Mike Kremer's avatar Mike Kremer
Browse files

Made obj-reader correctly parse groups.

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@11762 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 534012bd
......@@ -279,103 +279,107 @@ bool FileOBJPlugin::readMaterial(QString _filename, OBJImporter& _importer)
//-----------------------------------------------------------------------------
void FileOBJPlugin::createAllGroupObjects(OBJImporter& _importer) {
// Two cases: Either groups exist or they don't
unsigned int i = 0;
if(_importer.numGroups() > 1) {
// We do have groups, so neglect
// default mesh in index 0
i = 1;
}
void FileOBJPlugin::addNewObject( OBJImporter& _importer, QString _name )
{
convertToOBJName(_name);
//if no additional object is needed return
if ( _importer.currentObject()+1 >= (int)_importer.objectOptions().size() )
return;
if ( _importer.isTriangleMesh( _importer.currentObject()+1 ) ){
// add a triangle mesh
int id = -1;
emit addEmptyObject(DATA_TRIANGLE_MESH, id);
BaseObjectData* object(0);
if(PluginFunctions::getObject( id, object)){
_importer.addObject( object );
for(; i < _importer.numGroups(); ++i) {
object->setPath( _importer.path() );
std::cerr << "Adding object " << _name.toStdString() << std::endl;
object->setName( _name );
}
} else if ( _importer.isPolyMesh( _importer.currentObject()+1 ) ){
int id = -1;
emit addEmptyObject(DATA_POLY_MESH, id);
BaseObjectData* object(0);
if(PluginFunctions::getObject( id, object)){
_importer.addObject( object );
// Get group name
QString name = _importer.groupName(i);
convertToOBJName(name);
if ( _importer.isTriangleMesh( i ) ){
// add a triangle mesh
int id = -1;
emit addEmptyObject(DATA_TRIANGLE_MESH, id);
BaseObjectData* object(0);
if (PluginFunctions::getObject(id, object)) {
_importer.setObject(object, i);
object->setPath(_importer.path());
object->setName(name);
}
} else if (_importer.isPolyMesh( i )) {
int id = -1;
emit addEmptyObject(DATA_POLY_MESH, id);
BaseObjectData* object(0);
if (PluginFunctions::getObject(id, object)) {
_importer.setObject(object, i);
object->setPath(_importer.path());
object->setName(name);
}
}
object->setPath( _importer.path() );
object->setName( _name );
}
}
#ifdef ENABLE_BSPLINECURVE_SUPPORT
else if ( _importer.isCurve( _importer.currentObject()+1 ) ){
int id = -1;
emit addEmptyObject(DATA_BSPLINE_CURVE, id);
BaseObjectData* object(0);
if(PluginFunctions::getObject( id, object)){
_importer.addObject( object );
object->setPath( _importer.path() );
object->setName( _name );
}
}
else if (_importer.isCurve( i )) {
int id = -1;
emit addEmptyObject(DATA_BSPLINE_CURVE, id);
BaseObjectData* object(0);
if (PluginFunctions::getObject(id, object)) {
_importer.setObject(object, i);
object->setPath(_importer.path());
object->setName(name);
}
}
#endif
#ifdef ENABLE_BSPLINESURFACE_SUPPORT
else if ( _importer.isSurface( _importer.currentObject()+1 ) ){
int id = -1;
emit addEmptyObject(DATA_BSPLINE_SURFACE, id);
BaseObjectData* object(0);
if(PluginFunctions::getObject( id, object)){
_importer.addObject( object );
object->setPath( _importer.path() );
object->setName( _name );
}
}
else if (_importer.isSurface( i )) {
int id = -1;
emit addEmptyObject(DATA_BSPLINE_SURFACE, id);
BaseObjectData* object(0);
if (PluginFunctions::getObject(id, object)) {
_importer.setObject(object, i);
object->setPath(_importer.path());
object->setName(name);
}
}
#endif
//force gui settings
if ( OpenFlipper::Options::gui() && loadOptions_ != 0 ){
if ( !loadFaceColor_->isChecked() )
_importer.objectOptions()[ _importer.currentObject() ] |= OBJImporter::FORCE_NOCOLOR;
if ( !loadNormals_->isChecked() )
_importer.objectOptions()[ _importer.currentObject() ] |= OBJImporter::FORCE_NONORMALS;
if ( !loadTexCoords_->isChecked() || !loadTextures_->isChecked())
_importer.objectOptions()[ _importer.currentObject() ] |= OBJImporter::FORCE_NOTEXTURES;
}
//force gui settings
if (OpenFlipper::Options::gui() && loadOptions_ != 0) {
if (!loadFaceColor_->isChecked())
_importer.objectOptions()[ i ] |= OBJImporter::FORCE_NOCOLOR;
if (!loadNormals_->isChecked())
_importer.objectOptions()[ i ] |= OBJImporter::FORCE_NONORMALS;
if (!loadTexCoords_->isChecked() || !loadTextures_->isChecked())
_importer.objectOptions()[ i ] |= OBJImporter::FORCE_NOTEXTURES;
}
}
}
void FileOBJPlugin::convertToOBJName(QString& _name) {
......@@ -560,6 +564,12 @@ void FileOBJPlugin::readOBJFile(QString _filename, OBJImporter& _importer)
_importer.setPath( path );
// Set filename for default mesh
_importer.setGroupName(0, currentFileName);
// Now add all meshes for every group (if exists)
createAllGroupObjects(_importer);
while( input && !input.eof() )
{
std::getline(input,line);
......@@ -617,9 +627,9 @@ void FileOBJPlugin::readOBJFile(QString _filename, OBJImporter& _importer)
if ( mat.has_Texture() ){
//add object if not already there
if (_importer.currentObject() == -1) {
addNewObject(_importer, currentFileName );
}
// if (_importer.currentObject() == -1) {
// addNewObject(_importer, currentFileName );
// }
_importer.useMaterial( matname );
}
......@@ -680,17 +690,25 @@ void FileOBJPlugin::readOBJFile(QString _filename, OBJImporter& _importer)
else if (mode == NONE && keyWrd == "g"){
std::string groupName;
std::getline(stream,groupName);
std::getline(stream, groupName);
if ( faceCount > 0 )
addNewObject( _importer, QString(groupName.c_str()) );
else {
// if ( faceCount > 0 )
// addNewObject( _importer, QString(groupName.c_str()) );
//else {
if(faceCount == 0) {
currentFileName = QString(groupName.c_str());
}
//since obj-groups are used, all new objects will be grouped together in OpenFlipper
if ( _importer.objectOptions().size() > 1 )
_importer.setGroup( QFileInfo(_filename).fileName() );
// if ( _importer.objectOptions().size() > 1 )
// _importer.setGroup( QFileInfo(_filename).fileName() );
int id = _importer.groupId(groupName.c_str());
if(id == -1) {
std::cerr << "Error: Group has not been added before!" << std::endl;
return;
}
_importer.setCurrentGroup(id);
faceCount = 0;
}
......@@ -706,8 +724,8 @@ void FileOBJPlugin::readOBJFile(QString _filename, OBJImporter& _importer)
face_texcoords.clear();
//add object if not already there
if (_importer.currentObject() == -1)
addNewObject(_importer, currentFileName );
// if (_importer.currentObject() == -1)
// addNewObject(_importer, currentFileName );
// read full line after detecting a face
std::string faceLine;
......@@ -878,8 +896,8 @@ void FileOBJPlugin::readOBJFile(QString _filename, OBJImporter& _importer)
mode = CURVE;
if ( keyWrd == "curv" )
addNewObject(_importer, currentFileName );
// if ( keyWrd == "curv" )
// addNewObject(_importer, currentFileName );
//get curve control points
std::string curveLine;
......@@ -997,8 +1015,8 @@ void FileOBJPlugin::readOBJFile(QString _filename, OBJImporter& _importer)
mode = SURFACE;
if ( keyWrd == "surf" )
addNewObject(_importer, currentFileName );
// if ( keyWrd == "surf" )
// addNewObject(_importer, currentFileName );
//get surface control points
std::string surfLine;
......@@ -1166,16 +1184,23 @@ void FileOBJPlugin::checkTypes(QString _filename, OBJImporter& _importer, QStrin
// group
else if (mode == NONE && keyWrd == "g"){
if ( faceCount > 0 ){
//if ( faceCount > 0 ){
//give options to importer and reinitialize
//for next object
std::string grpName;
stream >> grpName;
if ( options & OBJImporter::TRIMESH ) TriMeshCount++;
if ( options & OBJImporter::POLYMESH ) PolyMeshCount++;
_importer.addObjectOptions( options );
int id = _importer.addGroup(grpName.c_str());
_importer.setCurrentGroup(id);
_importer.setObjectOptions( options );
options = OBJImporter::TRIMESH;
faceCount = 0;
}
//}
}
// face
else if (mode == NONE && keyWrd == "f"){
......@@ -1258,7 +1283,7 @@ void FileOBJPlugin::checkTypes(QString _filename, OBJImporter& _importer, QStrin
if ( options & OBJImporter::TRIMESH ) TriMeshCount++;
if ( options & OBJImporter::POLYMESH ) PolyMeshCount++;
_importer.addObjectOptions( options );
_importer.setObjectOptions( options );
}
options = OBJImporter::CURVE;
......@@ -1303,7 +1328,7 @@ void FileOBJPlugin::checkTypes(QString _filename, OBJImporter& _importer, QStrin
mode = NONE;
_importer.addObjectOptions( options );
_importer.setObjectOptions( options );
options = OBJImporter::TRIMESH;
faceCount = 0;
}
......@@ -1324,7 +1349,7 @@ void FileOBJPlugin::checkTypes(QString _filename, OBJImporter& _importer, QStrin
if ( options & OBJImporter::TRIMESH ) TriMeshCount++;
if ( options & OBJImporter::POLYMESH ) PolyMeshCount++;
_importer.addObjectOptions( options );
_importer.setObjectOptions( options );
}
options = OBJImporter::SURFACE;
......@@ -1361,7 +1386,7 @@ void FileOBJPlugin::checkTypes(QString _filename, OBJImporter& _importer, QStrin
mode = NONE;
_importer.addObjectOptions( options );
_importer.setObjectOptions( options );
options = OBJImporter::TRIMESH;
faceCount = 0;
}
......@@ -1372,7 +1397,7 @@ void FileOBJPlugin::checkTypes(QString _filename, OBJImporter& _importer, QStrin
if (faceCount > 0){
if ( options & OBJImporter::TRIMESH ) TriMeshCount++;
if ( options & OBJImporter::POLYMESH ) PolyMeshCount++;
_importer.addObjectOptions( options );
_importer.setObjectOptions( options );
}
if (TriMeshCount == 0 && PolyMeshCount == 0)
......@@ -1453,7 +1478,7 @@ int FileOBJPlugin::loadObject(QString _filename) {
//add a group if we have includes
if (includes.size() > 0)
importer.setGroup( QFileInfo(_filename).fileName() );
importer.addGroup( QFileInfo(_filename).fileName() );
//check if something was found
if ( importer.objectOptions().size() == 0 && objIDs.size() == 0 ){
......@@ -1473,28 +1498,34 @@ int FileOBJPlugin::loadObject(QString _filename) {
int returnID = -1;
//perhaps add group
if ( importer.group() != "" ){
if ( importer.numGroups() > 1 ){
bool dataControlExists = false;
pluginExists( "datacontrol", dataControlExists );
if ( dataControlExists ){
for(uint i=0; i < importer.objectCount(); i++)
objIDs.push_back( importer.object(i)->id() );
returnID = RPC::callFunctionValue<int>("datacontrol","groupObjects", objIDs, importer.group());
for(uint i=1; i < importer.objectCount(); i++) {
BaseObject* obj = importer.object(i);
if(obj) {
objIDs.push_back( obj->id() );
} else {
std::cerr << "Object is NULL!" << std::endl;
}
}
returnID = RPC::callFunctionValue<int>("datacontrol","groupObjects", objIDs, importer.groupName(0));
}
}
//check all new objects
for(uint i=0; i < importer.objectCount(); i++){
BaseObject* object = importer.object(i);
if(object == NULL) continue;
//remember the id of the first opened object
if ( i == 0 && importer.group() == "" )
if ( returnID == -1)
returnID = object->id();
//handle new PolyMeshes
......
......@@ -170,7 +170,8 @@ class FileOBJPlugin : public QObject, BaseInterface, FileInterface, LoadSaveInte
bool readMaterial(QString _filename, OBJImporter& _importer);
void readOBJFile(QString _filename, OBJImporter& _importer);
void addNewObject(OBJImporter& _importer, QString _name );
void createAllGroupObjects(OBJImporter& _importer);
//void addNewObject(OBJImporter& _importer, QString _name );
void addTextures(OBJImporter& _importer, int _objectID );
/// Convert non-valid filenames (e.g. of groups that end with .jpg) to valid .objs.
......
......@@ -120,81 +120,50 @@ int OBJImporter::degreeV(){
//-----------------------------------------------------------------------------
/// add a mesh
void OBJImporter::addObject( BaseObject* _object ){
void OBJImporter::setObject(BaseObject* _object, int _groupId) {
PolyMeshObject* polyMeshObj = dynamic_cast< PolyMeshObject* > (_object);
TriMeshObject* triMeshObj = dynamic_cast< TriMeshObject* > (_object);
#ifdef ENABLE_BSPLINECURVE_SUPPORT
BSplineCurveObject* curveObject = dynamic_cast< BSplineCurveObject* > (_object);
#endif
#ifdef ENABLE_BSPLINESURFACE_SUPPORT
BSplineSurfaceObject* surfaceObject = dynamic_cast< BSplineSurfaceObject* > (_object);
#endif
if ( polyMeshObj ){
while ((unsigned int)_groupId >= objects_.size()) {
objects_.push_back(NULL);
}
polyMeshes_.push_back( polyMeshObj->mesh() );
objects_.push_back( _object );
if (objects_[_groupId] != NULL)
return;
addUsedVertices();
} else if ( triMeshObj ){
triMeshes_.push_back( triMeshObj->mesh() );
objects_.push_back( _object );
addUsedVertices();
}
if (_object != NULL) {
#ifdef ENABLE_BSPLINECURVE_SUPPORT
else if ( curveObject ){
curves_.push_back( curveObject->splineCurve() );
objects_.push_back( _object );
}
#endif
objects_[_groupId] = _object;
#ifdef ENABLE_BSPLINESURFACE_SUPPORT
else if ( surfaceObject ){
surfaces_.push_back( surfaceObject->splineSurface() );
objects_.push_back( _object );
}
#endif
if(PluginFunctions::polyMesh(_object->id()) != NULL || PluginFunctions::triMesh(_object->id()) != NULL) {
addUsedVertices(_groupId);
}
else {
std::cerr << "Error: Cannot add object. Type is unknown!" << std::endl;
}
} else {
std::cerr << "Error: Cannot add object. Type is unknown!" << std::endl;
}
}
//-----------------------------------------------------------------------------
/// get id of the active object
int OBJImporter::currentObject(){
return objects_.size()-1;
return currentGroup_;
}
//-----------------------------------------------------------------------------
/// get the active polyMesh
PolyMesh* OBJImporter::currentPolyMesh(){
if (polyMeshes_.size() == 0)
return 0;
else
return polyMeshes_.back();
return PluginFunctions::polyMesh(object(currentObject())->id());
}
//-----------------------------------------------------------------------------
/// get the active triMesh
TriMesh* OBJImporter::currentTriMesh(){
if (triMeshes_.size() == 0)
return 0;
else
return triMeshes_.back();
return PluginFunctions::triMesh(object(currentObject())->id());
}
//-----------------------------------------------------------------------------
......@@ -202,10 +171,8 @@ TriMesh* OBJImporter::currentTriMesh(){
#ifdef ENABLE_BSPLINECURVE_SUPPORT
BSplineCurve* OBJImporter::currentCurve(){
if (curves_.size() == 0)
return 0;
else
return curves_.back();
return PluginFunctions::splineCurve(PluginFunctions::baseObjectData(object(currentObject())));
}
#endif
......@@ -215,10 +182,8 @@ BSplineCurve* OBJImporter::currentCurve(){
#ifdef ENABLE_BSPLINESURFACE_SUPPORT
BSplineSurface* OBJImporter::currentSurface(){
if (surfaces_.size() == 0)
return 0;
else
return surfaces_.back();
return PluginFunctions::splineSurface(PluginFunctions::baseObjectData(object(currentObject())));
}
#endif
......@@ -226,53 +191,52 @@ BSplineSurface* OBJImporter::currentSurface(){
//-----------------------------------------------------------------------------
/// add all vertices that are used to the mesh (in correct order)
void OBJImporter::addUsedVertices(){
void OBJImporter::addUsedVertices(int _groupId) {
if ( isTriangleMesh( currentObject() ) ){
BaseObject* obj = objects_[_groupId];
//handle triangle meshes
if ( !currentTriMesh() ) return;
//add all vertices to the mesh
std::set<VertexHandle>::iterator it;
if (isTriangleMesh(_groupId)) {
// Clear the map of already existing vertices
vertexMapTri_.clear();
//handle triangle meshes
TriMesh* curMesh = PluginFunctions::triMesh(obj->id());
if (curMesh == NULL)
return;
for ( it=usedVertices_[ currentObject() ].begin() ; it != usedVertices_[ currentObject() ].end(); it++ ){
// add all vertices to the mesh
if ( *it >= (int)vertices_.size() ){
std::cerr << "Error: Vertex ID too large" << std::endl;
continue;
}
for (std::set<VertexHandle>::iterator it = usedVertices_[_groupId].begin(); it != usedVertices_[_groupId].end(); it++) {
if ( vertexMapTri_.find( *it ) == vertexMapTri_.end() )
vertexMapTri_[ *it ] = currentTriMesh()->add_vertex( (TriMesh::Point) vertices_[*it] );
}
} else if ( isPolyMesh( currentObject() ) ){
if (*it >= (int) vertices_.size()) {
std::cerr << "Error: Vertex ID too large" << std::endl;
continue;
}
//handle poly meshes
if ( !currentPolyMesh() ) return;
if (vertexMapTri_[_groupId].find(*it) == vertexMapTri_[_groupId].end()) {
(vertexMapTri_[_groupId])[*it] = curMesh->add_vertex((TriMesh::Point) vertices_[*it]);
}
}
//add all vertices to the mesh
std::set<VertexHandle>::iterator it;
} else if (isPolyMesh(_groupId)) {
// Clear the map of already existing vertices
vertexMapPoly_.clear();
//handle triangle meshes
PolyMesh* curMesh = PluginFunctions::polyMesh(obj->id());
if (curMesh == NULL)
return;
for ( it=usedVertices_[ currentObject() ].begin() ; it != usedVertices_[ currentObject() ].end(); it++ ){
if ( *it >= (int)vertices_.size() ){
std::cerr << "Error: Vertex ID too large" << std::endl;
return;
}
//add all vertices to the mesh
if ( vertexMapPoly_.find( *it ) == vertexMapPoly_.end() )
vertexMapPoly_[ *it ] = currentPolyMesh()->add_vertex( (PolyMesh::Point) vertices_[*it] );
for (std::set<VertexHandle>::iterator it = usedVertices_[