SplatCloudObjectSelectionPlugin2.cc 9.97 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *      Copyright (C) 2001-2010 by Computer Graphics Group, RWTH Aachen      *
 *                           www.openflipper.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenFlipper.                                        *
 *                                                                           *
 *  OpenFlipper is free software: you can redistribute it and/or modify      *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenFlipper is distributed in the hope that it will be useful,           *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenFlipper. If not,                                  *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                      *
 *   $Author$                                                       *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/

//================================================================
//
//  CLASS SplatCloudObjectSelectionPlugin - IMPLEMENTATION (continued)
//
//================================================================


//== INCLUDES ====================================================


#include "SplatCloudObjectSelectionPlugin.hh"

#include "SplatCloudSelection.hh"

#include <set>


//== IMPLEMENTATION ==============================================


/** \brief Delete all selected elements of a SplatCloud
 *
 * @param _splatCloud a SplatCloud
 * @param _primitiveType bitmask of active primitive types
 * @return returns true if the SplatCloud was changed (i.e there was something selected that was deleted)
 */
bool SplatCloudObjectSelectionPlugin::splatCloudDeleteSelection( SplatCloud *_splatCloud, PrimitiveType _primitiveType )
{
  if( (_primitiveType & vertexType_) == 0 )
    return false; // done

  if( _splatCloud == 0 )
    return false; // error

  unsigned int numDeleted = _splatCloud->eraseSplatsByIndex( SplatCloudSelection::getVertexSelection( _splatCloud ) );

  return (numDeleted != 0);
}


//----------------------------------------------------------------


/** \brief Toggle the selection state of SplatCloud primitives
 *
 * @param _splatCloud a SplatCloud
 * @param _index index of vertex that was picked
 * @param _hit_point point that was picked
 * @param _primitiveType primitive types to be selected
 */
void SplatCloudObjectSelectionPlugin::splatCloudToggleSelection( SplatCloud *_splatCloud, uint _index, ACG::Vec3d &_hit_point, PrimitiveType _primitiveType )
{
  if( (_primitiveType & vertexType_) == 0 )
    return; // done

  if( _splatCloud == 0 )
    return; // error

  if( /*(_index < 0) ||*/ (_index >= _splatCloud->numSplats()) )
    return; // error

  if( !_splatCloud->hasSelections() )
  {
    if( !_splatCloud->requestSelections() )
      return; // error

    unsigned int i, num = _splatCloud->numSplats();
    for( i=0; i<num; ++i )
      _splatCloud->selections( i ) = false;
  }

  _splatCloud->selections( _index ) = !_splatCloud->selections( _index );

  if( _splatCloud->selections( _index ) )
    emit scriptInfo( "selectVertices(ObjectId , [" + QString::number( _index ) + "])" );
  else
    emit scriptInfo( "unselectVertices(ObjectId , [" + QString::number( _index ) + "])" );
}


//----------------------------------------------------------------


/** \brief paint selection with a sphere
 *
 * @param _splatCloud a SplatCloud
 * @param _index index of vertex that was picked
 * @param _hit_point point that was picked
 * @param _radius selection sphere radius
 * @param _primitiveType primitive types to be selected
 * @param _deselection true, if entities should be deselected
 */
void SplatCloudObjectSelectionPlugin::splatCloudSphereSelection( SplatCloud *_splatCloud, uint _index, ACG::Vec3d &_hit_point, double _radius, PrimitiveType _primitiveType, bool _deselection )
{
  if( (_primitiveType & vertexType_) == 0 )
    return; // done

  if( _splatCloud == 0 )
    return; // error

  if( /*(_index < 0) ||*/ (_index >= _splatCloud->numSplats()) )
    return; // error

  if( !_splatCloud->hasPositions() )
    return; // error

  if( !_splatCloud->hasSelections() )
  {
    if( _deselection )
      return; // done

    if( !_splatCloud->requestSelections() )
      return; // error

    unsigned int i, num = _splatCloud->numSplats();
    for( i=0; i<num; ++i )
      _splatCloud->selections( i ) = false;
  }

  if( _radius < 0.0 )
    return; // done

  double sqrRadius = _radius * _radius;

  SplatCloud::Selection selection = !_deselection;

  unsigned int i, num = _splatCloud->numSplats();
  for( i=0; i<num; ++i )
  {
    const SplatCloud::Position &pos = _splatCloud->positions( i );

    double dx = pos[0] - _hit_point[0];
    double dy = pos[1] - _hit_point[1];
    double dz = pos[2] - _hit_point[2];

    double sqrDist = dx*dx + dy*dy + dz*dz;

    if( sqrDist <= sqrRadius )
      _splatCloud->selections( i ) = selection;
  }
}


//----------------------------------------------------------------


/** \brief Select all primitves that are projected to the given region
 *
 * @param _splatCloud a SplatCloud
 * @param _state current gl state
 * @param _region region
 * @param _primitiveType primitive types to be selected
 * @param _deselection true, if entities should be deselected
 * @return true, if something was selected
 */
bool SplatCloudObjectSelectionPlugin::splatCloudVolumeSelection( SplatCloud *_splatCloud, ACG::GLState &_state, QRegion *_region, PrimitiveType _primitiveType, bool _deselection )
{
  if( (_primitiveType & vertexType_) == 0 )
    return false; // done

  if( _splatCloud == 0 )
    return false; // error

  if( !_splatCloud->hasPositions() )
    return false; // error

  bool modify = true;

  if( !_splatCloud->hasSelections() )
  {
    if( _deselection )
    {
      modify = false;
    }
    else
    {
      if( !_splatCloud->requestSelections() )
        return false; // error

      unsigned int i, num = _splatCloud->numSplats();
      for( i=0; i<num; ++i )
        _splatCloud->selections( i ) = false;
    }
  }

  bool rv = false;

  SplatCloud::Selection selection = !_deselection;

232
233
234
235
  int i, num = static_cast<int>(_splatCloud->numSplats());
#ifdef USE_OPENMP
    #pragma omp parallel for
#endif
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
  for( i=0; i<num; ++i )
  {
    const SplatCloud::Position &pos = _splatCloud->positions( i );

    ACG::Vec3d proj = _state.project( ACG::Vec3d( pos[0], pos[1], pos[2] ) );

    if( _region->contains( QPoint( (int) proj[0], _state.context_height() - (int) proj[1] ) ) )
    {
      if( modify )
        _splatCloud->selections( i ) = selection;

      rv = true;
    }
  }

  return rv;
}


//----------------------------------------------------------------


/** \brief Colorize a selection
 *
 * @param _splatCloud a SplatCloud
 * @param _primitiveTypes selection types
 * @param _r red   rgba color-component
 * @param _g green rgba color-component
 * @param _b blue  rgba color-component
 * @param _a blue  rgba color-component
 */
void SplatCloudObjectSelectionPlugin::splatCloudColorizeSelection( SplatCloud *_splatCloud, PrimitiveType _primitiveTypes, int _r, int _g, int _b, int _a )
{
  if( (_primitiveTypes & vertexType_) == 0 )
    return; // done

  if( _splatCloud == 0 )
    return; // error

  if( !_splatCloud->hasColors() )
  {
    if( !_splatCloud->requestColors() )
      return; // error

    SplatCloud::Color black( 0, 0, 0 );

    unsigned int i, num = _splatCloud->numSplats();
    for( i=0; i<num; ++i )
      _splatCloud->colors( i ) = black;
  }

  if( !_splatCloud->hasSelections() )
    return; // done

  unsigned char r = (_r < 0) ? 0 : (_r > 255) ? 255 : (unsigned char) _r;
  unsigned char g = (_g < 0) ? 0 : (_g > 255) ? 255 : (unsigned char) _g;
  unsigned char b = (_b < 0) ? 0 : (_b > 255) ? 255 : (unsigned char) _b;

  SplatCloud::Color color( r, g, b ); // drop alpha

  unsigned int i, num = _splatCloud->numSplats();
  for( i=0; i<num; ++i )
  {
    if( _splatCloud->selections( i ) )
      _splatCloud->colors( i ) = color;
  }
}