Developer Documentation
NoisePlugin.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 #include "NoisePlugin.hh"
44 
45 #include <iostream>
46 
48 
49 //-----------------------------------------------------------------------------
50 
51 void
52 NoisePlugin::
53 initializePlugin()
54 {
55  if ( OpenFlipper::Options::gui() ) {
56  tool_ = new noiseToolbarWidget();
57  QSize size(300, 300);
58  tool_->resize(size);
59 
60  connect(tool_->addNoise, SIGNAL(clicked()), this, SLOT(slotAddNoise()));
61 
62  QIcon* toolIcon = new QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"NoiseIcon.png");
63 
64  emit addToolbox( tr("Noise") , tool_, toolIcon );
65  }
66 }
67 
68 
69 //-----------------------------------------------------------------------------
70 
71 void
72 NoisePlugin::
73 slotAddNoise()
74 {
75 
76  const double maxNoise = tool_->maxDistance->value();
77 
79  o_it != PluginFunctions::objectsEnd(); ++o_it)
80  {
81  slotAddNoise(o_it->id(), maxNoise);
82  }
83 
84 }
85 
86 void
87 NoisePlugin::
88 slotAddNoise(int _objectId, double _maxNoise)
89 {
90 
91  if (TriMesh* mesh = PluginFunctions::triMesh(_objectId))
92  {
93  slotAddNoise( mesh, _maxNoise );
94  emit updatedObject(_objectId, UPDATE_GEOMETRY);
95  emit createBackup( _objectId, "Noise", UPDATE_GEOMETRY);
96  }
97  else if (PolyMesh* mesh = PluginFunctions::polyMesh(_objectId))
98  {
99  slotAddNoise( mesh, _maxNoise );
100  emit updatedObject(_objectId, UPDATE_GEOMETRY);
101  emit createBackup( _objectId, "Noise", UPDATE_GEOMETRY);
102  }
103  else if (SplatCloud* cloud = PluginFunctions::splatCloud(_objectId))
104  {
105  slotAddNoise( cloud, _maxNoise );
106  emit updatedObject(_objectId, UPDATE_GEOMETRY);
107  emit createBackup( _objectId, "Noise", UPDATE_GEOMETRY);
108  }
109 }
110 
111 
112 
113 template< class MeshT >
114 void
115 NoisePlugin::
116 slotAddNoise( MeshT* _mesh, double _maxNoise )
117 {
118  if ( _mesh == nullptr )
119  {
120  emit log(LOGERR, "Unable to get mesh for object" );
121  return;
122  }
123 
124  if (!_mesh->has_face_normals())
125  {
126  _mesh->request_face_normals();
127  _mesh->update_face_normals();
128  }
129 
130  if (!_mesh->has_vertex_normals())
131  {
132  _mesh->request_vertex_normals();
133  _mesh->update_vertex_normals();
134  }
135 
136  for ( auto vh : _mesh->vertices())
137  {
138  double randomNumber = ( double(rand()) / double(RAND_MAX) * 2.0 - 1.0) * _maxNoise;
139  _mesh->point(vh) = _mesh->point(vh) + _mesh->normal(vh) * randomNumber;
140  }
141 
142  _mesh->update_normals();
143 
144 }
145 
146 
147 void
148 NoisePlugin::
149 slotAddNoise( SplatCloud* _splat_cloud, double _maxNoise )
150 {
151  if ( ! _splat_cloud->hasPositions() )
152  {
153  emit log(LOGERR, "Error: Splat cloud without positions!" );
154  return;
155  }
156 
157  if ( ! _splat_cloud->hasNormals() )
158  {
159  emit log(LOGINFO, "No normals available, adding noise in all directions" );
160 
161  for ( unsigned int i = 0 ; i < _splat_cloud->numSplats() ; ++i)
162  {
163  ACG::Vec3f direction(10.0,10.0,10.0);
164 
165  while (direction.sqrnorm() > 1.0)
166  {
167  direction[0] = (((double)rand()) / ((double)RAND_MAX) - 0.5) * 2.0;
168  direction[1] = (((double)rand()) / ((double)RAND_MAX) - 0.5) * 2.0;
169  direction[2] = (((double)rand()) / ((double)RAND_MAX) - 0.5) * 2.0;
170  }
171 
172  direction.normalize();
173 
174  double randomNumber = ( double(rand()) / double(RAND_MAX) * 2.0 - 1.0) * _maxNoise;
175 
176  _splat_cloud->positions(i) = _splat_cloud->positions(i) + direction * randomNumber ;
177  }
178  }
179  else
180  {
181  for ( unsigned int i = 0 ; i < _splat_cloud->numSplats() ; ++i)
182  {
183  double randomNumber = ( double(rand()) / double(RAND_MAX) * 2.0 - 1.0) * _maxNoise;
184  _splat_cloud->positions(i) = _splat_cloud->positions(i) + _splat_cloud->normals(i) * randomNumber ;
185  }
186  }
187 }
188 
189 
Position & positions(int _idx)
Get a reference of the predefined property&#39;s value.
Definition: SplatCloud.hh:631
const UpdateType UPDATE_GEOMETRY(UpdateTypeSet(1)<< 2)
Geometry updated.
unsigned int numSplats() const
Get the number of splats.
Definition: SplatCloud.hh:179
PolyMesh * polyMesh(BaseObjectData *_object)
Get a poly mesh from an object.
TriMesh * triMesh(BaseObjectData *_object)
Get a triangle mesh from an object.
bool hasPositions() const
Return the availability of the predefined property.
Definition: SplatCloud.hh:607
DLLEXPORT ObjectIterator objectsEnd()
Return Iterator to Object End.
Normal & normals(int _idx)
Get a reference of the predefined property&#39;s value.
Definition: SplatCloud.hh:635
SplatCloud * splatCloud(BaseObjectData *_object)
Get a SplatCloud from an object.
bool hasNormals() const
Return the availability of the predefined property.
Definition: SplatCloud.hh:609
const QStringList TARGET_OBJECTS("target")
Iterable object range.
noiseToolbarWidget * tool_
Widget for Toolbox.
Definition: NoisePlugin.hh:93