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