LaplacePlugin.cc 8.2 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
\*===========================================================================*/

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

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

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

Jan Möbius's avatar
 
Jan Möbius committed
45
46
47
#include "LaplacePlugin.hh"

#include "OpenFlipper/BasePlugin/PluginFunctions.hh"
Jan Möbius's avatar
   
Jan Möbius committed
48
49
#include <ObjectTypes/PolyMesh/PolyMesh.hh>
#include <ObjectTypes/TriangleMesh/TriangleMesh.hh>
Jan Möbius's avatar
 
Jan Möbius committed
50

Jan's avatar
Jan committed
51
52
#include <QElapsedTimer>

Jan Möbius's avatar
 
Jan Möbius committed
53
54
55
#ifdef USE_OPENMP
#endif

56

Jan Möbius's avatar
   
Jan Möbius committed
57
58
#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
59
60
61
62
63

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
64

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

Jan Möbius's avatar
 
Jan Möbius committed
75
   BaseObjectData* object;
Jan Möbius's avatar
   
Jan Möbius committed
76
   if (! PluginFunctions::getObject(  _identifier , object ) ) {
Jan Möbius's avatar
   
Jan Möbius committed
77
78
      return;
   }
Jan Möbius's avatar
 
Jan Möbius committed
79
80
81
82
83
84
85
86
87
88
89
90

   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
91

Jan Möbius's avatar
 
Jan Möbius committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
   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
109
110

  if(!_mesh->get_property_handle( laplace_vector_property , "Laplace Vector" ))
Jan Möbius's avatar
 
Jan Möbius committed
111
    _mesh->add_property( laplace_vector_property, "Laplace Vector" );
Jan Möbius's avatar
   
Jan Möbius committed
112
113

  if(!_mesh->get_property_handle( laplace_length_property , UNIFORM_LAPLACE_NAME ))
Jan Möbius's avatar
 
Jan Möbius committed
114
    _mesh->add_property( laplace_length_property, UNIFORM_LAPLACE_NAME );
Jan Möbius's avatar
   
Jan Möbius committed
115
116


Jan's avatar
Jan committed
117
  QElapsedTimer time;
Jan Möbius's avatar
 
Jan Möbius committed
118
119
120
  time.start();
  std::vector< typename MeshT::VertexHandle > handles;
  handles.reserve(_mesh->n_vertices());
Jan Möbius's avatar
   
Jan Möbius committed
121
  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
122
    handles.push_back( *v_it );
Jan Möbius's avatar
   
Jan Möbius committed
123

124
  #ifdef USE_OPENMP
Jan Möbius's avatar
 
Jan Möbius committed
125
126
127
128
    #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
129

Jan Möbius's avatar
 
Jan Möbius committed
130
    ACG::Vec3d laplace(0.0,0.0,0.0);
Jan Möbius's avatar
Jan Möbius committed
131
132
    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
133

Jan Möbius's avatar
 
Jan Möbius committed
134
135
136
137
    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
138
139

//   #ifdef USE_OPENMP
Jan Möbius's avatar
 
Jan Möbius committed
140
141
142
143
//     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
144

Jan Möbius's avatar
 
Jan Möbius committed
145
146
147
148
}

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

Jan Möbius's avatar
 
Jan Möbius committed
151
152
  OpenMesh::VPropHandleT< ACG::Vec3d > laplace_property;
  OpenMesh::VPropHandleT< double > laplace_squared;
Jan Möbius's avatar
   
Jan Möbius committed
153

Jan Möbius's avatar
   
Jan Möbius committed
154
  if(!_mesh->get_property_handle( laplace_property , "Laplace Vector" )) {
Jan Möbius's avatar
 
Jan Möbius committed
155
156
157
    std::cerr << "LaplaceLengthPlugin : Unable to get Laplace Vector property" << std::endl;
    return;
  }
Jan Möbius's avatar
   
Jan Möbius committed
158
159

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

Jan's avatar
Jan committed
162
  QElapsedTimer time;
Jan Möbius's avatar
 
Jan Möbius committed
163
164
165
  time.start();
  std::vector< typename MeshT::VertexHandle > handles;
  handles.reserve(_mesh->n_vertices());
Jan Möbius's avatar
   
Jan Möbius committed
166
  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
167
    handles.push_back( *v_it );
Jan Möbius's avatar
   
Jan Möbius committed
168

169
  #ifdef USE_OPENMP
Jan Möbius's avatar
 
Jan Möbius committed
170
171
172
173
    #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
174

Jan Möbius's avatar
 
Jan Möbius committed
175
    ACG::Vec3d laplace(0.0,0.0,0.0);
Jan Möbius's avatar
Jan Möbius committed
176
177
    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
178
179
180
    laplace = 1.0 /(double)_mesh->valence(handle) * laplace;
    _mesh->property(laplace_squared,handle) = laplace.norm();
  }
Jan Möbius's avatar
   
Jan Möbius committed
181
182

//   #ifdef USE_OPENMP
Jan Möbius's avatar
 
Jan Möbius committed
183
184
185
186
187
188
//     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
189
190


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