LaplacePlugin.cc 8.34 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
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
*                           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/>.                                       *
*                                                                            *
33
34
35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36
37
38
39
40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
41
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
42

Matthias Möller's avatar
Matthias Möller committed
43
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

#include "LaplacePlugin.hh"

#include <iostream>
Jan Möbius's avatar
 
Jan Möbius committed
54
#include <ACG/GL/GLState.hh>
Jan Möbius's avatar
 
Jan Möbius committed
55
56

#include "OpenFlipper/BasePlugin/PluginFunctions.hh"
Jan Möbius's avatar
 
Jan Möbius committed
57
58
#include <ObjectTypes/PolyMesh/PolyMesh.hh>
#include <ObjectTypes/TriangleMesh/TriangleMesh.hh>
Jan Möbius's avatar
 
Jan Möbius committed
59
60
61
62
63

#ifdef USE_OPENMP
#include <omp.h>
#endif

Jan Möbius's avatar
 
Jan Möbius committed
64
65
#define UNIFORM_LAPLACE_NAME "Uniform Laplace Length"
#define UNIFORM_LAPLACE_SQUARED_NAME "Uniform Laplace Squared Length"
Jan Möbius's avatar
 
Jan Möbius committed
66
67
68
69
70

void LaplaceLengthPlugin::pluginsInitialized()
{
  emit addTexture( UNIFORM_LAPLACE_NAME , "laplace_length.png" , 1 );
  emit setTextureMode(UNIFORM_LAPLACE_NAME,"clamp=true,center=true,repeat=false,clamp_min=-20,clamp_max=20");
Jan Möbius's avatar
 
Jan Möbius committed
71

Jan Möbius's avatar
 
Jan Möbius committed
72
73
74
75
76
77
78
79
80
  emit addTexture( UNIFORM_LAPLACE_SQUARED_NAME , "laplace_length.png" , 1 );
  emit setTextureMode(UNIFORM_LAPLACE_SQUARED_NAME,"clamp=true,center=true,repeat=false,clamp_min=-20,clamp_max=20");
}

void LaplaceLengthPlugin::slotUpdateTexture( QString _textureName , int _identifier )
{
   if ( (_textureName != UNIFORM_LAPLACE_SQUARED_NAME) && (_textureName != UNIFORM_LAPLACE_NAME ) ) {
      return;
   }
Jan Möbius's avatar
 
Jan Möbius committed
81

Jan Möbius's avatar
 
Jan Möbius committed
82
   BaseObjectData* object;
Jan Möbius's avatar
 
Jan Möbius committed
83
   if (! PluginFunctions::getObject(  _identifier , object ) ) {
Jan Möbius's avatar
 
Jan Möbius committed
84
85
      return;
   }
Jan Möbius's avatar
 
Jan Möbius committed
86
87
88
89
90
91
92
93
94
95
96
97

   if ( object->dataType( DATA_TRIANGLE_MESH ) ) {
      TriMesh* mesh = PluginFunctions::triMesh(object);
      if ( _textureName == UNIFORM_LAPLACE_NAME ) {
         computeLaplaceLength(mesh);
         emit updatedTextures(UNIFORM_LAPLACE_NAME,_identifier);
      }
      if ( _textureName == UNIFORM_LAPLACE_SQUARED_NAME ) {
         computeLaplaceSquaredLength(mesh);
         emit updatedTextures(UNIFORM_LAPLACE_SQUARED_NAME,_identifier);
      }
   }
Jan Möbius's avatar
 
Jan Möbius committed
98

Jan Möbius's avatar
 
Jan Möbius committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
   if ( object->dataType( DATA_POLY_MESH ) ) {
      PolyMesh* mesh = PluginFunctions::polyMesh(object);
      if ( _textureName == UNIFORM_LAPLACE_NAME ) {
         computeLaplaceLength(mesh);
         emit updatedTextures(UNIFORM_LAPLACE_NAME,_identifier);
      }
      if ( _textureName == UNIFORM_LAPLACE_SQUARED_NAME ) {
         computeLaplaceSquaredLength(mesh);
         emit updatedTextures(UNIFORM_LAPLACE_SQUARED_NAME,_identifier);
      }
   }
}

template< typename MeshT >
void LaplaceLengthPlugin::computeLaplaceLength(MeshT* _mesh) {
  OpenMesh::VPropHandleT< ACG::Vec3d > laplace_vector_property;
  OpenMesh::VPropHandleT< double > laplace_length_property;
Jan Möbius's avatar
 
Jan Möbius committed
116
117

  if(!_mesh->get_property_handle( laplace_vector_property , "Laplace Vector" ))
Jan Möbius's avatar
 
Jan Möbius committed
118
    _mesh->add_property( laplace_vector_property, "Laplace Vector" );
Jan Möbius's avatar
 
Jan Möbius committed
119
120

  if(!_mesh->get_property_handle( laplace_length_property , UNIFORM_LAPLACE_NAME ))
Jan Möbius's avatar
 
Jan Möbius committed
121
    _mesh->add_property( laplace_length_property, UNIFORM_LAPLACE_NAME );
Jan Möbius's avatar
 
Jan Möbius committed
122
123


Jan Möbius's avatar
 
Jan Möbius committed
124
125
126
127
  QTime time;
  time.start();
  std::vector< typename MeshT::VertexHandle > handles;
  handles.reserve(_mesh->n_vertices());
Jan Möbius's avatar
 
Jan Möbius committed
128
  for ( typename MeshT::VertexIter v_it = _mesh->vertices_begin() ; v_it != _mesh->vertices_end(); ++v_it)
Jan Möbius's avatar
Jan Möbius committed
129
    handles.push_back( *v_it );
Jan Möbius's avatar
 
Jan Möbius committed
130

131
  #ifdef USE_OPENMP
Jan Möbius's avatar
 
Jan Möbius committed
132
133
134
135
    #pragma omp parallel for
  #endif
  for ( int i = 0 ; i < (int)handles.size(); ++i ) {
    const typename MeshT::VertexHandle handle = handles[i];
Jan Möbius's avatar
 
Jan Möbius committed
136

Jan Möbius's avatar
 
Jan Möbius committed
137
    ACG::Vec3d laplace(0.0,0.0,0.0);
Jan Möbius's avatar
Jan Möbius committed
138
139
    for ( typename MeshT::VertexVertexIter vv_it(*_mesh , handle) ; vv_it.is_valid() ; ++vv_it )
        laplace += _mesh->point(*vv_it) - _mesh->point(handle);
Jan Möbius's avatar
 
Jan Möbius committed
140

Jan Möbius's avatar
 
Jan Möbius committed
141
142
143
144
    laplace = 1.0 /(double)_mesh->valence(handle) * laplace;
    _mesh->property(laplace_vector_property,handle) = laplace;
    _mesh->property(laplace_length_property,handle) = laplace.norm();
  }
Jan Möbius's avatar
 
Jan Möbius committed
145
146

//   #ifdef USE_OPENMP
Jan Möbius's avatar
 
Jan Möbius committed
147
148
149
150
//     std::cerr << "Laplace parallel took : " << time.elapsed() << std::endl;
//   #else
//     std::cerr << "Laplace sequential took : " << time.elapsed() << std::endl;
//   #endif
Jan Möbius's avatar
 
Jan Möbius committed
151

Jan Möbius's avatar
 
Jan Möbius committed
152
153
154
155
}

template< typename MeshT >
void LaplaceLengthPlugin::computeLaplaceSquaredLength(MeshT* _mesh) {
Jan Möbius's avatar
 
Jan Möbius committed
156
157
  computeLaplaceLength(_mesh);

Jan Möbius's avatar
 
Jan Möbius committed
158
159
  OpenMesh::VPropHandleT< ACG::Vec3d > laplace_property;
  OpenMesh::VPropHandleT< double > laplace_squared;
Jan Möbius's avatar
 
Jan Möbius committed
160

Jan Möbius's avatar
 
Jan Möbius committed
161
  if(!_mesh->get_property_handle( laplace_property , "Laplace Vector" )) {
Jan Möbius's avatar
 
Jan Möbius committed
162
163
164
    std::cerr << "LaplaceLengthPlugin : Unable to get Laplace Vector property" << std::endl;
    return;
  }
Jan Möbius's avatar
 
Jan Möbius committed
165
166

  if(!_mesh->get_property_handle( laplace_squared , UNIFORM_LAPLACE_SQUARED_NAME ))
Jan Möbius's avatar
 
Jan Möbius committed
167
    _mesh->add_property( laplace_squared, UNIFORM_LAPLACE_SQUARED_NAME );
Jan Möbius's avatar
 
Jan Möbius committed
168

Jan Möbius's avatar
 
Jan Möbius committed
169
170
171
172
  QTime time;
  time.start();
  std::vector< typename MeshT::VertexHandle > handles;
  handles.reserve(_mesh->n_vertices());
Jan Möbius's avatar
 
Jan Möbius committed
173
  for ( typename MeshT::VertexIter v_it = _mesh->vertices_begin() ; v_it != _mesh->vertices_end(); ++v_it)
Jan Möbius's avatar
Jan Möbius committed
174
    handles.push_back( *v_it );
Jan Möbius's avatar
 
Jan Möbius committed
175

176
  #ifdef USE_OPENMP
Jan Möbius's avatar
 
Jan Möbius committed
177
178
179
180
    #pragma omp parallel for
  #endif
  for ( int i = 0 ; i < (int)handles.size(); ++i ) {
    const typename MeshT::VertexHandle handle = handles[i];
Jan Möbius's avatar
 
Jan Möbius committed
181

Jan Möbius's avatar
 
Jan Möbius committed
182
    ACG::Vec3d laplace(0.0,0.0,0.0);
Jan Möbius's avatar
Jan Möbius committed
183
184
    for ( typename MeshT::VertexVertexIter vv_it(*_mesh , handle) ; vv_it.is_valid() ; ++vv_it )
        laplace += _mesh->property(laplace_property,*vv_it) - _mesh->property(laplace_property,handle);
Jan Möbius's avatar
 
Jan Möbius committed
185
186
187
    laplace = 1.0 /(double)_mesh->valence(handle) * laplace;
    _mesh->property(laplace_squared,handle) = laplace.norm();
  }
Jan Möbius's avatar
 
Jan Möbius committed
188
189

//   #ifdef USE_OPENMP
Jan Möbius's avatar
 
Jan Möbius committed
190
191
192
193
194
195
//     std::cerr << "Laplace Squared  parallel took : " << time.elapsed() << std::endl;
//   #else
//     std::cerr << "Laplace Squared sequential took : " << time.elapsed() << std::endl;
//   #endif
}

Matthias Möller's avatar
Matthias Möller committed
196
197
198
199
200
#if QT_VERSION < 0x050000
  Q_EXPORT_PLUGIN2( laplacelengthplugin , LaplaceLengthPlugin );
#endif


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