MergePlugin.cc 6.34 KB
Newer Older
Martin Schultz's avatar
Martin Schultz committed
1

2
//#include <QtGui>
Martin Schultz's avatar
Martin Schultz committed
3 4 5 6 7 8

#include "MergePlugin.hh"

#include "OpenFlipper/BasePlugin/PluginFunctions.hh"
#include <OpenFlipper/BasePlugin/RPCWrappers.hh>

9 10 11
#include <ObjectTypes/PolyMesh/PolyMesh.hh>
#include <ObjectTypes/TriangleMesh/TriangleMesh.hh>

Martin Schultz's avatar
Martin Schultz committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
/**
 * Helper functions
 */
namespace
{
  template< class MeshT >
  void getAllMeshes(std::vector< MeshT* > & meshes, const std::vector< BaseObjectData* > & objects)
  {
    for (uint i=0; i < objects.size(); i++)
    {
      MeshT* t;
      if(PluginFunctions::getMesh(objects[i]->id(),t))
        meshes.push_back( t );
    }
  }

  void getTargets(std::vector< BaseObjectData* > & _objects)
  {
    _objects.clear();
    //read all target objects
    for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS,DataType(DATA_TRIANGLE_MESH | DATA_POLY_MESH)) ;
                                          o_it != PluginFunctions::objectsEnd(); ++o_it)
        _objects.push_back( *o_it );
  }

  template< class MeshT >
  void convertMeshes(const DataType & _type, std::vector< int >& convertedIds, std::vector< MeshT* >& _meshes)
  {
    for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS,_type) ;
                                          o_it != PluginFunctions::objectsEnd(); ++o_it)
    {
        //convert polyMesh to triMesh or vice versa
        convertedIds.push_back(RPC::callFunctionValue<int>("meshconvert","convert",(*o_it)->id(), _type != DATA_TRIANGLE_MESH));
        MeshT* ptr;
        PluginFunctions::getMesh(convertedIds.back(),ptr);
        _meshes.push_back(ptr);
    }
  }
}

void MergePlugin::slotCleanup( DataType _type, bool _deleteSeparateObjects )
{
  //clean up conversion objects
  for(int i : convertedIds)     
     emit deleteObject( i );

  convertedIds.clear();

  if(_deleteSeparateObjects)
  {
    //clean up separated objects
    for (size_t i=0; i < objects.size(); i++)
      emit deleteObject( (objects[i])->id() );

    objects.clear();
  }
  //clean up unused merge target
  if(_type == DATA_TRIANGLE_MESH)
    emit deleteObject( polyMergeID );
  else
    emit deleteObject( triMergeID );
}

DataType MergePlugin::checkType(const std::vector< BaseObjectData* > & objects )
{
  DataType type = (objects[0])->dataType();
  bool askForType = false;
  for (uint i=1; i < objects.size(); i++)
    if ( type != (objects[i])->dataType() ){
      askForType = true;
      break;
    }

  if(askForType)
  {
    QStringList types;
    types.append(dataTypeName(DATA_TRIANGLE_MESH));
    types.append(dataTypeName(DATA_POLY_MESH));
    bool ok;
    QString result = QInputDialog::getItem(nullptr,
                          tr("Select Mesh Type"),
                          tr("Convert meshes to:"),
                          types,
                          1,
                          false,
                          &ok);
    if(ok)
    {
      if( result == dataTypeName(DATA_POLY_MESH))
      {
        type = DATA_POLY_MESH;        
      }
      else
      {
        type = DATA_TRIANGLE_MESH;        
      }
    }
    else
      return DataType();
  }
  return type;
}


116 117 118 119 120 121 122 123
MergePlugin::MergePlugin() :
  tool_(nullptr),
  polyMergeID(0),
  triMergeID(0)
{

}

Martin Schultz's avatar
Martin Schultz committed
124 125 126 127 128 129 130 131 132 133
/// init the Toolbox
void MergePlugin::initializePlugin() {
   tool_ = new MergeToolBox();

   QSize size(300, 300);
   tool_->resize(size);

   connect(tool_->mergeButton, SIGNAL( clicked() ), this, SLOT( mergeObjects() ) );
   tool_->mergeButton->setStatusTip("Merge all target objects into one without changing geometry");
   tool_->mergeButton->setToolTip( tool_->mergeButton->statusTip() );
134
   QIcon* toolIcon_ = new QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"merge.png");
Martin Schultz's avatar
Martin Schultz committed
135 136 137

   connect(this,SIGNAL(cleanup(DataType, bool)),this,SLOT(slotCleanup(DataType, bool)),Qt::QueuedConnection);

138
   emit addToolbox( tr("Merge") , tool_, toolIcon_ );
Martin Schultz's avatar
Martin Schultz committed
139 140 141 142 143 144 145 146 147 148
}

void MergePlugin::pluginsInitialized()
{
  //populate scripting function
  emit setSlotDescription("mergeObjects(const std::vector< BaseObjectData* >,QString,bool)", "Merges multiple meshes into one mesh. returns the ID of the new mesh or -1 in case of error.",
                          QString("objects,mergedName,deleteSeparateObjects").split(","),
                          QString(" vector of BaseObjectData* containing Poly or TriMeshes to be merged, name for the merged object, flag to remove separated objects default is true").split(","));
}

149
int MergePlugin::mergeObjects(const std::vector< BaseObjectData* > & _objects, QString _name, bool _deleteSeparateObjects)
Martin Schultz's avatar
Martin Schultz committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
{
  int result = -1;
  if (_objects.size() < 2)
    return -1; //nothing to do

  objects = _objects;

  //check dataType
  DataType type = checkType(objects);

  //user pushed the cancel button
  if(type != DATA_TRIANGLE_MESH && type != DATA_POLY_MESH)
    return -1;

  convertedIds.clear();
  std::vector< TriMesh* > triMeshes;
  std::vector< PolyMesh* > polyMeshes;
  TriMesh* triMergePtr;
  PolyMesh* polyMergePtr;

  emit addEmptyObject(DATA_POLY_MESH, polyMergeID);
  PluginFunctions::getMesh(polyMergeID, polyMergePtr);
  polyMeshes.push_back(polyMergePtr);
  emit addEmptyObject(DATA_TRIANGLE_MESH, triMergeID);
  PluginFunctions::getMesh(triMergeID,triMergePtr);
  triMeshes.push_back(triMergePtr);

  getAllMeshes(triMeshes,objects);
  getAllMeshes(polyMeshes,objects);

  if ( type == DATA_TRIANGLE_MESH ) {
    // Convert PolyMeshes to TriMeshes
    convertMeshes(DATA_POLY_MESH,convertedIds,triMeshes);

    *triMergePtr = *triMeshes[1];
    mergeMeshes(triMeshes);
    (triMeshes[0])->update_normals();
    BaseObject* bo;
    PluginFunctions::getObject(triMergeID,bo);
    bo->setName(_name);
    result = bo->id();
  } else {
    // Convert TriMeshes to PolyMeshes
    convertMeshes(DATA_TRIANGLE_MESH,convertedIds,polyMeshes);

    *polyMergePtr = *polyMeshes[1];
    mergeMeshes(polyMeshes);
    (polyMeshes[0])->update_normals();
    BaseObject* bo;
    PluginFunctions::getObject(polyMergeID,bo);
    bo->setName(_name);
    result = bo->id();
  }

  emit updatedObject(triMergeID,UPDATE_ALL);
  emit updatedObject(polyMergeID,UPDATE_ALL);

  //clean up after merging (removes OF objects)
  emit cleanup( type, _deleteSeparateObjects );
  return result;
}

/// merge two objects with target flag
void MergePlugin::mergeObjects()
{
  getTargets(objects);
  mergeObjects(objects,tool_->mergedName->text(),tool_->deleteObjects->isChecked());
}


#if QT_VERSION < 0x050000
Q_EXPORT_PLUGIN2( mergePlugin , MergePlugin );
#endif