Developer Documentation
LaplacePlugin.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 
51 #include "LaplacePlugin.hh"
52 
56 
57 #ifdef USE_OPENMP
58 #endif
59 
60 #if QT_VERSION >= 0x050000
61 #else
62 #include <QtGui>
63 #endif
64 
65 #define UNIFORM_LAPLACE_NAME "Uniform Laplace Length"
66 #define UNIFORM_LAPLACE_SQUARED_NAME "Uniform Laplace Squared Length"
67 
68 void LaplaceLengthPlugin::pluginsInitialized()
69 {
70  emit addTexture( UNIFORM_LAPLACE_NAME , "laplace_length.png" , 1 );
71  emit setTextureMode(UNIFORM_LAPLACE_NAME,"clamp=true,center=true,repeat=false,clamp_min=-20,clamp_max=20");
72 
73  emit addTexture( UNIFORM_LAPLACE_SQUARED_NAME , "laplace_length.png" , 1 );
74  emit setTextureMode(UNIFORM_LAPLACE_SQUARED_NAME,"clamp=true,center=true,repeat=false,clamp_min=-20,clamp_max=20");
75 }
76 
77 void LaplaceLengthPlugin::slotUpdateTexture( QString _textureName , int _identifier )
78 {
79  if ( (_textureName != UNIFORM_LAPLACE_SQUARED_NAME) && (_textureName != UNIFORM_LAPLACE_NAME ) ) {
80  return;
81  }
82 
83  BaseObjectData* object;
84  if (! PluginFunctions::getObject( _identifier , object ) ) {
85  return;
86  }
87 
88  if ( object->dataType( DATA_TRIANGLE_MESH ) ) {
89  TriMesh* mesh = PluginFunctions::triMesh(object);
90  if ( _textureName == UNIFORM_LAPLACE_NAME ) {
91  computeLaplaceLength(mesh);
92  emit updatedTextures(UNIFORM_LAPLACE_NAME,_identifier);
93  }
94  if ( _textureName == UNIFORM_LAPLACE_SQUARED_NAME ) {
95  computeLaplaceSquaredLength(mesh);
96  emit updatedTextures(UNIFORM_LAPLACE_SQUARED_NAME,_identifier);
97  }
98  }
99 
100  if ( object->dataType( DATA_POLY_MESH ) ) {
101  PolyMesh* mesh = PluginFunctions::polyMesh(object);
102  if ( _textureName == UNIFORM_LAPLACE_NAME ) {
103  computeLaplaceLength(mesh);
104  emit updatedTextures(UNIFORM_LAPLACE_NAME,_identifier);
105  }
106  if ( _textureName == UNIFORM_LAPLACE_SQUARED_NAME ) {
107  computeLaplaceSquaredLength(mesh);
108  emit updatedTextures(UNIFORM_LAPLACE_SQUARED_NAME,_identifier);
109  }
110  }
111 }
112 
113 template< typename MeshT >
114 void LaplaceLengthPlugin::computeLaplaceLength(MeshT* _mesh) {
115  OpenMesh::VPropHandleT< ACG::Vec3d > laplace_vector_property;
116  OpenMesh::VPropHandleT< double > laplace_length_property;
117 
118  if(!_mesh->get_property_handle( laplace_vector_property , "Laplace Vector" ))
119  _mesh->add_property( laplace_vector_property, "Laplace Vector" );
120 
121  if(!_mesh->get_property_handle( laplace_length_property , UNIFORM_LAPLACE_NAME ))
122  _mesh->add_property( laplace_length_property, UNIFORM_LAPLACE_NAME );
123 
124 
125  QTime time;
126  time.start();
127  std::vector< typename MeshT::VertexHandle > handles;
128  handles.reserve(_mesh->n_vertices());
129  for ( typename MeshT::VertexIter v_it = _mesh->vertices_begin() ; v_it != _mesh->vertices_end(); ++v_it)
130  handles.push_back( *v_it );
131 
132  #ifdef USE_OPENMP
133  #pragma omp parallel for
134  #endif
135  for ( int i = 0 ; i < (int)handles.size(); ++i ) {
136  const typename MeshT::VertexHandle handle = handles[i];
137 
138  ACG::Vec3d laplace(0.0,0.0,0.0);
139  for ( typename MeshT::VertexVertexIter vv_it(*_mesh , handle) ; vv_it.is_valid() ; ++vv_it )
140  laplace += _mesh->point(*vv_it) - _mesh->point(handle);
141 
142  laplace = 1.0 /(double)_mesh->valence(handle) * laplace;
143  _mesh->property(laplace_vector_property,handle) = laplace;
144  _mesh->property(laplace_length_property,handle) = laplace.norm();
145  }
146 
147 // #ifdef USE_OPENMP
148 // std::cerr << "Laplace parallel took : " << time.elapsed() << std::endl;
149 // #else
150 // std::cerr << "Laplace sequential took : " << time.elapsed() << std::endl;
151 // #endif
152 
153 }
154 
155 template< typename MeshT >
156 void LaplaceLengthPlugin::computeLaplaceSquaredLength(MeshT* _mesh) {
157  computeLaplaceLength(_mesh);
158 
159  OpenMesh::VPropHandleT< ACG::Vec3d > laplace_property;
160  OpenMesh::VPropHandleT< double > laplace_squared;
161 
162  if(!_mesh->get_property_handle( laplace_property , "Laplace Vector" )) {
163  std::cerr << "LaplaceLengthPlugin : Unable to get Laplace Vector property" << std::endl;
164  return;
165  }
166 
167  if(!_mesh->get_property_handle( laplace_squared , UNIFORM_LAPLACE_SQUARED_NAME ))
168  _mesh->add_property( laplace_squared, UNIFORM_LAPLACE_SQUARED_NAME );
169 
170  QTime time;
171  time.start();
172  std::vector< typename MeshT::VertexHandle > handles;
173  handles.reserve(_mesh->n_vertices());
174  for ( typename MeshT::VertexIter v_it = _mesh->vertices_begin() ; v_it != _mesh->vertices_end(); ++v_it)
175  handles.push_back( *v_it );
176 
177  #ifdef USE_OPENMP
178  #pragma omp parallel for
179  #endif
180  for ( int i = 0 ; i < (int)handles.size(); ++i ) {
181  const typename MeshT::VertexHandle handle = handles[i];
182 
183  ACG::Vec3d laplace(0.0,0.0,0.0);
184  for ( typename MeshT::VertexVertexIter vv_it(*_mesh , handle) ; vv_it.is_valid() ; ++vv_it )
185  laplace += _mesh->property(laplace_property,*vv_it) - _mesh->property(laplace_property,handle);
186  laplace = 1.0 /(double)_mesh->valence(handle) * laplace;
187  _mesh->property(laplace_squared,handle) = laplace.norm();
188  }
189 
190 // #ifdef USE_OPENMP
191 // std::cerr << "Laplace Squared parallel took : " << time.elapsed() << std::endl;
192 // #else
193 // std::cerr << "Laplace Squared sequential took : " << time.elapsed() << std::endl;
194 // #endif
195 }
196 
197 #if QT_VERSION < 0x050000
198  Q_EXPORT_PLUGIN2( laplacelengthplugin , LaplaceLengthPlugin );
199 #endif
200 
201 
202 
bool getObject(int _identifier, BSplineCurveObject *&_object)
PolyMesh * polyMesh(BaseObjectData *_object)
Get a poly mesh from an object.
bool dataType(DataType _type) const
Definition: BaseObject.cc:232
#define DATA_POLY_MESH
Definition: PolyMesh.hh:65
#define DATA_TRIANGLE_MESH
Definition: TriangleMesh.hh:66
TriMesh * triMesh(BaseObjectData *_object)
Get a triangle mesh from an object.