SubdividerPlugin.cc 10.6 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2014 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
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
*                           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$                     *
*                                                                            *
\*===========================================================================*/

Jan Möbius's avatar
 
Jan Möbius committed
43

Matthias Möller's avatar
Matthias Möller committed
44
45
46
47
48
49
#if QT_VERSION >= 0x050000 
  #include <QtWidgets>
#else
  #include <QtGui>
#endif

Jan Möbius's avatar
 
Jan Möbius committed
50
51
52
53
54
55
56
57
58
59
60

#include "SubdividerPlugin.hh"

#include <iostream>
#include <algorithm>

#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
#include <OpenFlipper/common/GlobalOptions.hh>
#include <OpenFlipper/INIFile/INIFile.hh>
#include <ACG/Scenegraph/PointNode.hh>

61
62
63
64
#include <OpenMesh/Tools/Subdivider/Uniform/LoopT.hh>
#include <OpenMesh/Tools/Subdivider/Uniform/Sqrt3T.hh>
#include <OpenMesh/Tools/Subdivider/Uniform/Sqrt3InterpolatingSubdividerLabsikGreinerT.hh>
#include <OpenMesh/Tools/Subdivider/Uniform/ModifiedButterFlyT.hh>
65
#include <OpenMesh/Tools/Subdivider/Uniform/LongestEdgeT.hh>
66

67
68
#include <OpenMesh/Tools/Subdivider/Uniform/CatmullClarkT.hh>

Jan Möbius's avatar
Jan Möbius committed
69
70
71
72
73
74
75
76
77

SubdividerPlugin::SubdividerPlugin() :
        tool_(0),
        toolIcon_(0)
{

}


Jan Möbius's avatar
 
Jan Möbius committed
78
79
80
81
82
83
84
void SubdividerPlugin::initializePlugin() 
{
  if ( OpenFlipper::Options::gui() ) {
    tool_ = new subdividerToolbarWidget();
    QSize size(300, 300);
    tool_->resize(size);
    
85
    connect(tool_->subdivide_uniform_toolButton,    SIGNAL( clicked() ),  this, SLOT( slotSubdivideUniform() ) );
86
87
88
    connect(tool_->simpleButton,                    SIGNAL( clicked() ),  this, SLOT( slotSimpleSubdivide() ) );


Jan Möbius's avatar
 
Jan Möbius committed
89
    
Dirk Wilden's avatar
Dirk Wilden committed
90
91
    toolIcon_ = new QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"subdivider.png");
    emit addToolbox( tr("Subdivider") , tool_, toolIcon_ );
Jan Möbius's avatar
 
Jan Möbius committed
92
93
94
95
96
  }
}

void SubdividerPlugin::pluginsInitialized() 
{
97
98
  emit setSlotDescription("subdivide(int,QString,int)", "Smooth a triangular mesh",
                          QString("object_id,algorithm,iterations").split(","),
99
                          QString("id of an object, algorithm to use (linear | loop | sqrt3 | interpolating_sqrt3 | modifiedButterfly | catmullClark ), number of iterations").split(","));
Jan Möbius's avatar
 
Jan Möbius committed
100
101
102
103
}

//-----------------------------------------------------------------------------

104
void SubdividerPlugin::slotSubdivideUniform()
Jan Möbius's avatar
 
Jan Möbius committed
105
106
107
108
109
110
111
112
{
  std::vector< int > ids;
  if ( PluginFunctions::getTargetIdentifiers( ids ) )
  {
    for (unsigned int i = 0; i < ids.size(); ++i)
    {
      if(tool_->loop_radioButton->isChecked())
      {
113
        subdivide(ids[i],"loop",tool_->subdivision_steps_spinBox->value());
Jan Möbius's avatar
 
Jan Möbius committed
114
      }
115
      else if ( tool_->sqrt3_radioButton->isChecked() )
Jan Möbius's avatar
 
Jan Möbius committed
116
      {
117
        subdivide(ids[i],"sqrt3",tool_->subdivision_steps_spinBox->value());
Jan Möbius's avatar
 
Jan Möbius committed
118
      }
119
120
      else if ( tool_->LabsikGreiner_radioButton->isChecked()  )
      {
Jan Möbius's avatar
Jan Möbius committed
121
        subdivide(ids[i],"interpolating_sqrt3",tool_->subdivision_steps_spinBox->value());
122
123
124
      }
      else if ( tool_->modifiedButterfly_radioButton->isChecked()  )
      {
125
        subdivide(ids[i],"modifiedButterfly",tool_->subdivision_steps_spinBox->value());
126
      }
127
128
129
130
      else if ( tool_->catmullClark_radioButton->isChecked()  )
      {
        subdivide(ids[i],"catmullClark",tool_->subdivision_steps_spinBox->value());
      }
131
132
133
134
    }
  }
  emit updateView();
}
135

136
//-----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
137

138
139
140
141
142
143
144
145
146
147
148
149
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
void SubdividerPlugin::slotSimpleSubdivide()
{
  std::vector< int > ids;
  if ( PluginFunctions::getTargetIdentifiers( ids ) )
  {
    for (unsigned int i = 0; i < ids.size(); ++i)
    {
      if(tool_->longestEdgeSplit->isChecked())
      {
        simpleSubdivide(ids[i],"longest",tool_->subdivision_steps_spinBox->value(), tool_->maximalEdgeLength->value());
      }
    }
  }
  emit updateView();
}

//-----------------------------------------------------------------------------

void SubdividerPlugin::simpleSubdivide(int _objectId, QString _algorithm , int _steps, double _parameter) {

  BaseObjectData* object;
  if (!test_trimesh_object(_objectId, object))
    return;

  TriMesh* mesh = PluginFunctions::triMesh(object);

  if (_algorithm.contains("longest", Qt::CaseInsensitive)) {
    OpenMesh::Subdivider::Uniform::LongestEdgeT<TriMesh, double> subdivider;

    subdivider.attach(*mesh);
    subdivider.set_max_edge_length(_parameter);
    subdivider(*mesh, _steps, tool_->updatePoints->isChecked());
    subdivider.detach();
  }

  mesh->garbage_collection();

  mesh->update_face_normals();
  mesh->update_vertex_normals();
  TriMeshObject* tmo = PluginFunctions::triMeshObject(object);
  tmo->update();

  // Create backup
  emit createBackup(object->id(), "Subdivider", UPDATE_TOPOLOGY);

  // Geometry and topology changed!
  emit updatedObject(object->id(), UPDATE_TOPOLOGY);
}

//-----------------------------------------------------------------------------

189
190
191
void SubdividerPlugin::subdivide(int _objectId, QString _algorithm , int _steps) {

  BaseObjectData* object;
192
193
194
  PluginFunctions::getObject(_objectId,object);
  if(!object) {
    emit log(LOGERR,"Unable to get Object in SubdividerPlugin::subdivide");
195
    return;
196
  }
197

198
199
  TriMesh*  mesh = PluginFunctions::triMesh(object);
  PolyMesh* polyMesh = PluginFunctions::polyMesh(object);
200

201

202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
  if ( mesh ) {
    if(_algorithm.contains("loop",Qt::CaseInsensitive))
    {
      OpenMesh::Subdivider::Uniform::LoopT<TriMesh,double> subdivider;

      subdivider.attach(*mesh);
      subdivider(*mesh,_steps,tool_->updatePoints->isChecked());
      subdivider.detach();
    }
    else if ( _algorithm.contains("sqrt3",Qt::CaseInsensitive) )
    {
      OpenMesh::Subdivider::Uniform::Sqrt3T<TriMesh,double> subdivider;

      subdivider.attach(*mesh);
      subdivider(_steps,tool_->updatePoints->isChecked());
      subdivider.detach();
    }
    else if ( _algorithm.contains("interpolating_sqrt(3)",Qt::CaseInsensitive)  )
    {
      OpenMesh::Subdivider::Uniform::InterpolatingSqrt3LGT<TriMesh,double> subdivider;

      subdivider.attach(*mesh);
      subdivider(_steps,tool_->updatePoints->isChecked());
      subdivider.detach();
    }
    else if ( _algorithm.contains("modifiedButterfly",Qt::CaseInsensitive)  )
    {
      OpenMesh::Subdivider::Uniform::ModifiedButterflyT<TriMesh,double> subdivider;

      subdivider.attach(*mesh);
      subdivider(_steps,tool_->updatePoints->isChecked());
      subdivider.detach();
    } else {
      emit log(LOGERR,"Unsupported or unknown subdivider for triangular meshes: " + _algorithm);
    }

    mesh->garbage_collection();

    mesh->update_face_normals();
    mesh->update_vertex_normals();
    //TriMeshObject* tmo = PluginFunctions::triMeshObject(object);
    //tmo->update();
244
245
  }

246
  if ( polyMesh ) {
247

248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
    if ( _algorithm.contains("catmullClark",Qt::CaseInsensitive)  )
    {

      PolyMesh* polyMesh = PluginFunctions::polyMesh(object);

      if ( ! polyMesh) {
        emit log(LOGERR,"Error: Catmull Clark only works on Poly Meshes!");
        return;
      }

      OpenMesh::Subdivider::Uniform::CatmullClarkT<PolyMesh> subdivider;

      subdivider.attach(*polyMesh);
      subdivider(_steps,tool_->updatePoints->isChecked());
      subdivider.detach();

      polyMesh->garbage_collection();

      polyMesh->update_face_normals();
      polyMesh->update_vertex_normals();
      //PolyMeshObject* pmo = PluginFunctions::polyMeshObject(object);
      //pmo->update();

    } else {
      emit log(LOGERR,"Unsupported or unknown subdivider for polygonal meshes: " + _algorithm);
    }

  }
276
277

  // Create backup
Dirk Wilden's avatar
Dirk Wilden committed
278
  emit createBackup(object->id(), "Subdivider", UPDATE_TOPOLOGY);
279
280
  
  // Geometry and topology changed!
Dirk Wilden's avatar
Dirk Wilden committed
281
  emit updatedObject(object->id(), UPDATE_TOPOLOGY);
282

Jan Möbius's avatar
 
Jan Möbius committed
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
}


//-----------------------------------------------------------------------------


bool SubdividerPlugin::test_trimesh_object(int _identifier, BaseObjectData*& _object)
{
  if ( _identifier == -1)
    return false;

  if (! PluginFunctions::getObject(_identifier,_object) ) 
    return false;  

  if (!_object->dataType(DATA_TRIANGLE_MESH) )
    return false;
  return true;
}

Matthias Möller's avatar
Matthias Möller committed
302
303
304
305
306
#if QT_VERSION < 0x050000
  Q_EXPORT_PLUGIN2( subdividerplugin , SubdividerPlugin );
#endif