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  {
80  slotAddNoise(o_it->id(), maxNoise);
81  }
82 
83 }
84 
85 void
86 NoisePlugin::
87 slotAddNoise(int _objectId, double _maxNoise)
88 {
89 
90  if (TriMesh* mesh = PluginFunctions::triMesh(_objectId))
91  {
92  slotAddNoise( mesh, _maxNoise );
93  emit updatedObject(_objectId, UPDATE_GEOMETRY);
94  emit createBackup( _objectId, "Noise", UPDATE_GEOMETRY);
95  }
96  else if (PolyMesh* mesh = PluginFunctions::polyMesh(_objectId))
97  {
98  slotAddNoise( mesh, _maxNoise );
99  emit updatedObject(_objectId, UPDATE_GEOMETRY);
100  emit createBackup( _objectId, "Noise", UPDATE_GEOMETRY);
101  }
102  else if (SplatCloud* cloud = PluginFunctions::splatCloud(_objectId))
103  {
104  slotAddNoise( cloud, _maxNoise );
105  emit updatedObject(_objectId, UPDATE_GEOMETRY);
106  emit createBackup( _objectId, "Noise", UPDATE_GEOMETRY);
107  }
108 }
109 
110 
111 
112 template< class MeshT >
113 void
114 NoisePlugin::
115 slotAddNoise( MeshT* _mesh, double _maxNoise )
116 {
117  if ( _mesh == nullptr )
118  {
119  emit log(LOGERR, "Unable to get mesh for object" );
120  return;
121  }
122 
123  if (!_mesh->has_face_normals())
124  {
125  _mesh->request_face_normals();
126  _mesh->update_face_normals();
127  }
128 
129  if (!_mesh->has_vertex_normals())
130  {
131  _mesh->request_vertex_normals();
132  _mesh->update_vertex_normals();
133  }
134 
135  for ( auto vh : _mesh->vertices())
136  {
137  double randomNumber = ( double(rand()) / double(RAND_MAX) * 2.0 - 1.0) * _maxNoise;
138  _mesh->point(vh) = _mesh->point(vh) + _mesh->normal(vh) * randomNumber;
139  }
140 
141  _mesh->update_normals();
142 
143 }
144 
145 
146 void
147 NoisePlugin::
148 slotAddNoise( SplatCloud* _splat_cloud, double _maxNoise )
149 {
150  if ( ! _splat_cloud->hasPositions() )
151  {
152  emit log(LOGERR, "Error: Splat cloud without positions!" );
153  return;
154  }
155 
156  if ( ! _splat_cloud->hasNormals() )
157  {
158  emit log(LOGINFO, "No normals available, adding noise in all directions" );
159 
160  for ( unsigned int i = 0 ; i < _splat_cloud->numSplats() ; ++i)
161  {
162  ACG::Vec3f direction(10.0,10.0,10.0);
163 
164  while (direction.sqrnorm() > 1.0)
165  {
166  direction[0] = (((double)rand()) / ((double)RAND_MAX) - 0.5) * 2.0;
167  direction[1] = (((double)rand()) / ((double)RAND_MAX) - 0.5) * 2.0;
168  direction[2] = (((double)rand()) / ((double)RAND_MAX) - 0.5) * 2.0;
169  }
170 
171  direction.normalize();
172 
173  double randomNumber = ( double(rand()) / double(RAND_MAX) * 2.0 - 1.0) * _maxNoise;
174 
175  _splat_cloud->positions(i) = _splat_cloud->positions(i) + direction * randomNumber ;
176  }
177  }
178  else
179  {
180  for ( unsigned int i = 0 ; i < _splat_cloud->numSplats() ; ++i)
181  {
182  double randomNumber = ( double(rand()) / double(RAND_MAX) * 2.0 - 1.0) * _maxNoise;
183  _splat_cloud->positions(i) = _splat_cloud->positions(i) + _splat_cloud->normals(i) * randomNumber ;
184  }
185  }
186 }
187 
188 
Position & positions(int _idx)
Get a reference of the predefined property&#39;s value.
Definition: SplatCloud.hh:631
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
const UpdateType UPDATE_GEOMETRY(UpdateTypeSet(4))
Geometry updated.
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.
ObjectRange objects(IteratorRestriction _restriction, DataType _dataType)
Iterable object range.
noiseToolbarWidget * tool_
Widget for Toolbox.
Definition: NoisePlugin.hh:93