SubdividerPlugin.cc 10.5 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-2011 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
44
45
46
47
48
49
50
51
52
53
54
55

#include <QtGui>

#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>

56
57
58
59
#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>
60
#include <OpenMesh/Tools/Subdivider/Uniform/LongestEdgeT.hh>
61

62
63
#include <OpenMesh/Tools/Subdivider/Uniform/CatmullClarkT.hh>

Jan Möbius's avatar
Jan Möbius committed
64
65
66
67
68
69
70
71
72

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

}


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


Jan Möbius's avatar
 
Jan Möbius committed
84
    
Dirk Wilden's avatar
Dirk Wilden committed
85
86
    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
87
88
89
90
91
  }
}

void SubdividerPlugin::pluginsInitialized() 
{
92
93
  emit setSlotDescription("subdivide(int,QString,int)", "Smooth a triangular mesh",
                          QString("object_id,algorithm,iterations").split(","),
94
                          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
95
96
97
98
}

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

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

131
//-----------------------------------------------------------------------------
Jan Möbius's avatar
 
Jan Möbius committed
132

133
134
135
136
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
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);
}

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

184
185
186
void SubdividerPlugin::subdivide(int _objectId, QString _algorithm , int _steps) {

  BaseObjectData* object;
187
188
189
  PluginFunctions::getObject(_objectId,object);
  if(!object) {
    emit log(LOGERR,"Unable to get Object in SubdividerPlugin::subdivide");
190
    return;
191
  }
192

193
194
  TriMesh*  mesh = PluginFunctions::triMesh(object);
  PolyMesh* polyMesh = PluginFunctions::polyMesh(object);
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
  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();
239
240
  }

241
  if ( polyMesh ) {
242

243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
    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);
    }

  }
271
272

  // Create backup
Dirk Wilden's avatar
Dirk Wilden committed
273
  emit createBackup(object->id(), "Subdivider", UPDATE_TOPOLOGY);
274
275
  
  // Geometry and topology changed!
Dirk Wilden's avatar
Dirk Wilden committed
276
  emit updatedObject(object->id(), UPDATE_TOPOLOGY);
277

Jan Möbius's avatar
 
Jan Möbius committed
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
}


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


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;
}

297
Q_EXPORT_PLUGIN2( subdividerplugin , SubdividerPlugin );