LaplacePlugin.cc 5.2 KB
Newer Older
Jan Möbius's avatar
   
Jan Möbius committed
1

Jan Möbius's avatar
 
Jan Möbius committed
2
3
4
5
6
#include <QtGui>

#include "LaplacePlugin.hh"

#include <iostream>
Jan Möbius's avatar
   
Jan Möbius committed
7
#include <ACG/GL/GLState.hh>
Jan Möbius's avatar
 
Jan Möbius committed
8
9

#include "OpenFlipper/BasePlugin/PluginFunctions.hh"
Jan Möbius's avatar
   
Jan Möbius committed
10
11
#include <ObjectTypes/PolyMesh/PolyMesh.hh>
#include <ObjectTypes/TriangleMesh/TriangleMesh.hh>
Jan Möbius's avatar
 
Jan Möbius committed
12
13
14
15
16

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

Jan Möbius's avatar
   
Jan Möbius committed
17
18
#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
19
20
21
22
23

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
24

Jan Möbius's avatar
 
Jan Möbius committed
25
26
27
28
29
30
31
32
33
  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
34

Jan Möbius's avatar
 
Jan Möbius committed
35
36
   BaseObjectData* object;
   if (! PluginFunctions::get_object(  _identifier , object ) ) {
Jan Möbius's avatar
   
Jan Möbius committed
37
38
      return;
   }
Jan Möbius's avatar
 
Jan Möbius committed
39
40
41
42
43
44
45
46
47
48
49
50

   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
51

Jan Möbius's avatar
 
Jan Möbius committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
   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
69
70

  if(!_mesh->get_property_handle( laplace_vector_property , "Laplace Vector" ))
Jan Möbius's avatar
 
Jan Möbius committed
71
    _mesh->add_property( laplace_vector_property, "Laplace Vector" );
Jan Möbius's avatar
   
Jan Möbius committed
72
73

  if(!_mesh->get_property_handle( laplace_length_property , UNIFORM_LAPLACE_NAME ))
Jan Möbius's avatar
 
Jan Möbius committed
74
    _mesh->add_property( laplace_length_property, UNIFORM_LAPLACE_NAME );
Jan Möbius's avatar
   
Jan Möbius committed
75
76


Jan Möbius's avatar
 
Jan Möbius committed
77
78
79
80
  QTime time;
  time.start();
  std::vector< typename MeshT::VertexHandle > handles;
  handles.reserve(_mesh->n_vertices());
Jan Möbius's avatar
   
Jan Möbius committed
81
82
83
  for ( typename MeshT::VertexIter v_it = _mesh->vertices_begin() ; v_it != _mesh->vertices_end(); ++v_it)
    handles.push_back( v_it.handle() );

Jan Möbius's avatar
 
Jan Möbius committed
84
85
86
87
88
  #ifdef USE_OPENMP
    #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
89

Jan Möbius's avatar
 
Jan Möbius committed
90
    ACG::Vec3d laplace(0.0,0.0,0.0);
Jan Möbius's avatar
   
Jan Möbius committed
91
    for ( typename MeshT::VertexVertexIter vv_it(*_mesh , handle) ; vv_it ; ++vv_it )
Jan Möbius's avatar
 
Jan Möbius committed
92
        laplace += _mesh->point(vv_it) - _mesh->point(handle);
Jan Möbius's avatar
   
Jan Möbius committed
93

Jan Möbius's avatar
 
Jan Möbius committed
94
95
96
97
    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
98
99

//   #ifdef USE_OPENMP
Jan Möbius's avatar
 
Jan Möbius committed
100
101
102
103
//     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
104

Jan Möbius's avatar
 
Jan Möbius committed
105
106
107
108
}

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

Jan Möbius's avatar
 
Jan Möbius committed
111
112
  OpenMesh::VPropHandleT< ACG::Vec3d > laplace_property;
  OpenMesh::VPropHandleT< double > laplace_squared;
Jan Möbius's avatar
   
Jan Möbius committed
113

Jan Möbius's avatar
   
Jan Möbius committed
114
  if(!_mesh->get_property_handle( laplace_property , "Laplace Vector" )) {
Jan Möbius's avatar
 
Jan Möbius committed
115
116
117
    std::cerr << "LaplaceLengthPlugin : Unable to get Laplace Vector property" << std::endl;
    return;
  }
Jan Möbius's avatar
   
Jan Möbius committed
118
119

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

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

Jan Möbius's avatar
 
Jan Möbius committed
129
130
131
132
133
  #ifdef USE_OPENMP
    #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
134

Jan Möbius's avatar
 
Jan Möbius committed
135
    ACG::Vec3d laplace(0.0,0.0,0.0);
Jan Möbius's avatar
   
Jan Möbius committed
136
    for ( typename MeshT::VertexVertexIter vv_it(*_mesh , handle) ; vv_it ; ++vv_it )
Jan Möbius's avatar
 
Jan Möbius committed
137
138
139
140
        laplace += _mesh->property(laplace_property,vv_it) - _mesh->property(laplace_property,handle);
    laplace = 1.0 /(double)_mesh->valence(handle) * laplace;
    _mesh->property(laplace_squared,handle) = laplace.norm();
  }
Jan Möbius's avatar
   
Jan Möbius committed
141
142

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

Q_EXPORT_PLUGIN2( laplacelengthplugin , LaplaceLengthPlugin );