MeshConvert.cc 6.94 KB
Newer Older
Martin Schultz's avatar
Martin Schultz committed
1 2 3 4 5 6 7 8 9 10 11 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
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
\*===========================================================================*/



#include "MeshConvert.hh"
#include <OpenFlipper/common/GlobalOptions.hh>
#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
#include <ObjectTypes/PolyMesh/PolyMesh.hh>
#include <ObjectTypes/TriangleMesh/TriangleMesh.hh>
#include <ObjectTypes/PolyMesh/PluginFunctionsPolyMesh.hh>
#include <OpenFlipper/libs_required/OpenMesh/src/OpenMesh/Core/Mesh/Casts.hh>


void MeshConvertPlugin::initializePlugin()
{

}


void MeshConvertPlugin::pluginsInitialized()
{

  // Create your toolbar
Jan Möbius's avatar
Jan Möbius committed
71
  toolbar = new QToolBar(tr("Mesh conversion"));
Martin Schultz's avatar
Martin Schultz committed
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

  grp = new QActionGroup(toolbar);

  // Create an action for the toolbar
  bidirectionalConversion = new QAction(tr("&Convert Meshes"), grp);
  polyConversion = new QAction(tr("&Convert to PolyMesh"), grp);
  triConversion = new QAction(tr("&Convert to TriMesh"), grp);

  // Create an icon which is shown for the action
  bidirectionalConversion->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"Mesh-Convert.png"));
  polyConversion->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"Mesh-Convert-Poly.png"));
  triConversion->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"Mesh-Convert-Tri.png"));

  // Add the action to the toolbar
  toolbar->addAction(bidirectionalConversion);
  toolbar->addAction(polyConversion);
  toolbar->addAction(triConversion);



  connect( grp, SIGNAL( triggered(QAction*) ), this, SLOT(convert(QAction*)) );

  // Integrate the new toolbar into OpenFlipper
  emit addToolbar( toolbar );

97 98 99 100 101
  //populate scripting function
  emit setSlotDescription("convert(int,bool)", "Convert a mesh to PolyMesh or to TriMesh. returns the ID of the new mesh or -1 in case of error. The old mesh remains unchanged.",
                          QString("object_id,toTriMesh").split(","),
                          QString(" id of an object to convert, flag to convert to a TriMesh, if not set creates a new PolyMesh").split(","));

Martin Schultz's avatar
Martin Schultz committed
102 103
}

104 105 106 107 108 109
MeshConvertPlugin::MeshConvertPlugin() :
  toolbar(nullptr),
  grp(nullptr),
  bidirectionalConversion(nullptr),
  polyConversion(nullptr),
  triConversion(nullptr)
Martin Schultz's avatar
Martin Schultz committed
110 111 112 113 114 115 116 117 118
{

}

MeshConvertPlugin::~MeshConvertPlugin()
{

}

119
int MeshConvertPlugin::convert(int _id, bool _toTriMesh)
Martin Schultz's avatar
Martin Schultz committed
120
{
121
  int newID = -1;
Martin Schultz's avatar
Martin Schultz committed
122 123
  PolyMesh* p;
  TriMesh* t;
124
  if(_toTriMesh && PluginFunctions::getMesh(_id,p))
Martin Schultz's avatar
Martin Schultz committed
125
  {
126 127 128 129 130 131 132 133 134 135 136
    TriMesh  converted = static_cast<TriMesh>(*p);
    emit addEmptyObject(DATA_TRIANGLE_MESH, newID);
    if(PluginFunctions::getMesh(newID,t))
    {
      *t = converted;
      emit updatedObject(newID);
    }
  }
  else
  {
    if( PluginFunctions::getMesh(_id,t))
Martin Schultz's avatar
Martin Schultz committed
137 138 139 140 141 142 143 144 145
    {
      PolyMesh  converted = static_cast<PolyMesh>(*t);
      emit addEmptyObject(DATA_POLY_MESH, newID);
      if(PluginFunctions::getMesh(newID,p))
      {
        *p = converted;
        emit updatedObject(newID);
      }
    }
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  }
  return newID;
}

void MeshConvertPlugin::convert(QAction* _action)
{
  std::vector<int> _ids;
  if(! PluginFunctions::getTargetIdentifiers( _ids  ))
    return;
  for(std::vector<int>::iterator id = _ids.begin(); id != _ids.end(); ++id)
  {
    if((_action == bidirectionalConversion || _action == polyConversion))
    {
      convert(*id,false);
    }
    if((_action == bidirectionalConversion || _action == triConversion))
Martin Schultz's avatar
Martin Schultz committed
162
    {
163
      convert(*id,true);
Martin Schultz's avatar
Martin Schultz committed
164 165 166 167 168 169 170 171 172
    }
  }
}


#if QT_VERSION < 0x050000
Q_EXPORT_PLUGIN2( meshconvertplugin , MeshConvertPlugin );
#endif