LaplacePlugin.cc 8.79 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2
3
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
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
 *           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.              *
Jan Möbius's avatar
Jan Möbius committed
39
*                                                                            *
40
41
42
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
43
44
45
46
47
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
48
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
49

Matthias Möller's avatar
Matthias Möller committed
50

Jan Möbius's avatar
 
Jan Möbius committed
51
52
53
#include "LaplacePlugin.hh"

#include "OpenFlipper/BasePlugin/PluginFunctions.hh"
Jan Möbius's avatar
 
Jan Möbius committed
54
55
#include <ObjectTypes/PolyMesh/PolyMesh.hh>
#include <ObjectTypes/TriangleMesh/TriangleMesh.hh>
Jan Möbius's avatar
 
Jan Möbius committed
56
57
58
59

#ifdef USE_OPENMP
#endif

60
#if QT_VERSION >= 0x050000
61
62
63
64
#else
#include <QtGui>
#endif

Jan Möbius's avatar
 
Jan Möbius committed
65
66
#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
67
68
69
70
71

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
72

Jan Möbius's avatar
 
Jan Möbius committed
73
74
75
76
77
78
79
80
81
  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
82

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

   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
99

Jan Möbius's avatar
 
Jan Möbius committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
   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
117
118

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

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


Jan Möbius's avatar
 
Jan Möbius committed
125
126
127
128
  QTime time;
  time.start();
  std::vector< typename MeshT::VertexHandle > handles;
  handles.reserve(_mesh->n_vertices());
Jan Möbius's avatar
 
Jan Möbius committed
129
  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
130
    handles.push_back( *v_it );
Jan Möbius's avatar
 
Jan Möbius committed
131

132
  #ifdef USE_OPENMP
Jan Möbius's avatar
 
Jan Möbius committed
133
134
135
136
    #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
137

Jan Möbius's avatar
 
Jan Möbius committed
138
    ACG::Vec3d laplace(0.0,0.0,0.0);
Jan Möbius's avatar
Jan Möbius committed
139
140
    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
141

Jan Möbius's avatar
 
Jan Möbius committed
142
143
144
145
    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
146
147

//   #ifdef USE_OPENMP
Jan Möbius's avatar
 
Jan Möbius committed
148
149
150
151
//     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
152

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

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

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

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

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

Jan Möbius's avatar
 
Jan Möbius committed
170
171
172
173
  QTime time;
  time.start();
  std::vector< typename MeshT::VertexHandle > handles;
  handles.reserve(_mesh->n_vertices());
Jan Möbius's avatar
 
Jan Möbius committed
174
  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
175
    handles.push_back( *v_it );
Jan Möbius's avatar
 
Jan Möbius committed
176

177
  #ifdef USE_OPENMP
Jan Möbius's avatar
 
Jan Möbius committed
178
179
180
181
    #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
182

Jan Möbius's avatar
 
Jan Möbius committed
183
    ACG::Vec3d laplace(0.0,0.0,0.0);
Jan Möbius's avatar
Jan Möbius committed
184
185
    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
186
187
188
    laplace = 1.0 /(double)_mesh->valence(handle) * laplace;
    _mesh->property(laplace_squared,handle) = laplace.norm();
  }
Jan Möbius's avatar
 
Jan Möbius committed
189
190

//   #ifdef USE_OPENMP
Jan Möbius's avatar
 
Jan Möbius committed
191
192
193
194
195
196
//     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
197
198
199
200
201
#if QT_VERSION < 0x050000
  Q_EXPORT_PLUGIN2( laplacelengthplugin , LaplaceLengthPlugin );
#endif


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