UpdateType.cc 11.2 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
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
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39
40
41
*                                                                            *
\*===========================================================================*/

42
43
44
45
46
47
// Workaround compiler bug in VS2017 and earlier with colliding Qt operators and bitset
#ifdef _MSC_VER
  #if (_MSC_VER <= 1916)
    #define  QT_NO_FLOAT16_OPERATORS
  #endif
#endif
Jan Möbius's avatar
Jan Möbius committed
48

Dirk Wilden's avatar
Dirk Wilden committed
49
#include "TypesInternal.hh"
Martin Schultz's avatar
Martin Schultz committed
50
#include "UpdateType.hh"
Dirk Wilden's avatar
Dirk Wilden committed
51
52
53
54
55
56
57
58
59
60
#include <QCoreApplication>
#include <OpenFlipper/common/GlobalOptions.hh>


/** This map maps an updateType id to an typeName
 */
static std::map< UpdateType, QString > updateTypeToString;

/** This map maps an updateType name to its id in the types vector
 */
61
static std::map< QString , size_t > stringToUpdateTypeInfo;
Dirk Wilden's avatar
Dirk Wilden committed
62
63
64

/** This map maps an updateType id to its id in the types vector
 */
65
static std::map< UpdateType , size_t > updateTypeToTypeInfo;
Dirk Wilden's avatar
Dirk Wilden committed
66

Jan Möbius's avatar
Jan Möbius committed
67
68
69
70
/** This field defines the start id for custom updatetypes.
*/
static UpdateType firstFreeID_(UPDATE_UNUSED);

71
72
73
74
75
76
UpdateType::UpdateType()
:type_(0)
{

}

Jan Möbius's avatar
Jan Möbius committed
77

Jan Möbius's avatar
Jan Möbius committed
78
UpdateType::UpdateType(const UpdateTypeSet& _set)
Jan Möbius's avatar
Jan Möbius committed
79
80
81
82
83
84
85
86
87
: type_(_set)
{
  
}

bool UpdateType::operator==(const UpdateType& _type) const {
  return ((type_ & _type.type_).any());
};

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
UpdateType 
UpdateType::operator|(const UpdateType& _type) const
{
  return (type_ | _type.type_);
}


UpdateType& 
UpdateType::operator|=(const UpdateType& _type)
{
  type_ |= _type.type_;

  return(*this);
}


Jan Möbius's avatar
Jan Möbius committed
104
/// Check if this update contains the given UpdateType
105
bool UpdateType::contains( const UpdateType& _type ) const {
106
107
108
109
  
  if ( type_ == UPDATE_ALL.type_ )
    return true;
  
Jan Möbius's avatar
Jan Möbius committed
110
111
  // Catch the specialization of updates
  if ( _type == UPDATE_SELECTION ) {
112
    if ( type_ == UPDATE_SELECTION_VERTICES.type_ || type_ == UPDATE_SELECTION_EDGES.type_ ||  type_ == UPDATE_SELECTION_HALFEDGES.type_ || type_ == UPDATE_SELECTION_FACES.type_ || type_ == UPDATE_SELECTION_KNOTS.type_ )
Jan Möbius's avatar
Jan Möbius committed
113
114
115
116
117
118
119
120
121
122
123
      return true;
  }  
  
  return ((type_ & _type.type_).any());
}

UpdateType& UpdateType::operator++() {
  if ( type_.count() != 1 ) {
    std::cerr << "Operator ++ for UpdateType which is not atomic!!" << std::endl;
  }
  
124
  type_ <<= 1;
Jan Möbius's avatar
Jan Möbius committed
125
126
127
128
129
130
131
132
  
  return (*this);
}

bool UpdateType::operator<( const UpdateType& _i ) const {
  return (type_.to_ulong() < _i.type_.to_ulong()); 
}

Dirk Wilden's avatar
Dirk Wilden committed
133
134
135
136
class UpdateTypeInfo {

  public:

Jan Möbius's avatar
Jan Möbius committed
137
  UpdateTypeInfo(const UpdateType& _type, QString _name, bool _needsScenegraphReset ) :
Dirk Wilden's avatar
Dirk Wilden committed
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
    type(_type),
    name(_name),
    resetNeeded(_needsScenegraphReset)
  {
  }

  /// The id of the UpdateType
  UpdateType type;

  /// The name of the UpdateType
  QString  name;

  /// is a sceneGraph reset needed for this update
  bool resetNeeded;
};

static std::vector< UpdateTypeInfo > updateTypes;


// functions -------------------------------------------------


void initializeUpdateTypes() {

  stringToUpdateTypeInfo["All"]      = updateTypes.size();
  updateTypeToTypeInfo[UPDATE_ALL]   = updateTypes.size();
  updateTypes.push_back( UpdateTypeInfo(UPDATE_ALL, "All", true) );

  stringToUpdateTypeInfo["Visibility"]      = updateTypes.size();
  updateTypeToTypeInfo[UPDATE_VISIBILITY]   = updateTypes.size();
  updateTypes.push_back( UpdateTypeInfo(UPDATE_VISIBILITY, "Visibility", true) );
  
  stringToUpdateTypeInfo["Geometry"]      = updateTypes.size();
  updateTypeToTypeInfo[UPDATE_GEOMETRY]   = updateTypes.size();
  updateTypes.push_back( UpdateTypeInfo(UPDATE_GEOMETRY, "Geometry", true) );
  
  stringToUpdateTypeInfo["Topology"]      = updateTypes.size();
  updateTypeToTypeInfo[UPDATE_TOPOLOGY]   = updateTypes.size();
  updateTypes.push_back( UpdateTypeInfo(UPDATE_TOPOLOGY, "Topology", true) );
  
  stringToUpdateTypeInfo["Selection"]      = updateTypes.size();
  updateTypeToTypeInfo[UPDATE_SELECTION]   = updateTypes.size();
  updateTypes.push_back( UpdateTypeInfo(UPDATE_SELECTION, "Selection", true) );
Jan Möbius's avatar
Jan Möbius committed
181
182
183
184
185
186
187
188
189
  
  stringToUpdateTypeInfo["VertexSelection"]      = updateTypes.size();
  updateTypeToTypeInfo[UPDATE_SELECTION_VERTICES]   = updateTypes.size();
  updateTypes.push_back( UpdateTypeInfo(UPDATE_SELECTION_VERTICES, "VertexSelection", true) );
  
  stringToUpdateTypeInfo["EdgeSelection"]      = updateTypes.size();
  updateTypeToTypeInfo[UPDATE_SELECTION_EDGES]   = updateTypes.size();
  updateTypes.push_back( UpdateTypeInfo(UPDATE_SELECTION_EDGES, "EdgeSelection", true) );
  
190
191
192
193
  stringToUpdateTypeInfo["HalfedgeSelection"]    = updateTypes.size();
  updateTypeToTypeInfo[UPDATE_SELECTION_HALFEDGES]   = updateTypes.size();
  updateTypes.push_back( UpdateTypeInfo(UPDATE_SELECTION_HALFEDGES, "HalfedgeSelection", true) );

Jan Möbius's avatar
Jan Möbius committed
194
195
196
  stringToUpdateTypeInfo["FaceSelection"]      = updateTypes.size();
  updateTypeToTypeInfo[UPDATE_SELECTION_FACES]   = updateTypes.size();
  updateTypes.push_back( UpdateTypeInfo(UPDATE_SELECTION_FACES, "FaceSelection", true) );
197
198
199
200
  
  stringToUpdateTypeInfo["KnotSelection"]      = updateTypes.size();
  updateTypeToTypeInfo[UPDATE_SELECTION_KNOTS] = updateTypes.size();
  updateTypes.push_back( UpdateTypeInfo(UPDATE_SELECTION_KNOTS, "KnotSelection", true) );
Dirk Wilden's avatar
Dirk Wilden committed
201

202
203
204
  stringToUpdateTypeInfo["Color"]      = updateTypes.size();
  updateTypeToTypeInfo[UPDATE_COLOR]   = updateTypes.size();
  updateTypes.push_back( UpdateTypeInfo(UPDATE_COLOR, "Color", true) );
Dirk Wilden's avatar
Dirk Wilden committed
205
  
206
207
208
209
210
211
212
213
  stringToUpdateTypeInfo["Texture"]      = updateTypes.size();
  updateTypeToTypeInfo[UPDATE_TEXTURE]   = updateTypes.size();
  updateTypes.push_back( UpdateTypeInfo(UPDATE_TEXTURE, "Texture", true) );

  stringToUpdateTypeInfo["State"]      = updateTypes.size();
  updateTypeToTypeInfo[UPDATE_STATE]   = updateTypes.size();
  updateTypes.push_back( UpdateTypeInfo(UPDATE_STATE, "State", true) );

Jan Möbius's avatar
Jan Möbius committed
214
215
216
217
218
219
220
  updateTypeToString[UPDATE_ALL]                = "All";
  updateTypeToString[UPDATE_VISIBILITY]         = "Visibility";
  updateTypeToString[UPDATE_GEOMETRY]           = "Geometry";
  updateTypeToString[UPDATE_TOPOLOGY]           = "Topology";
  updateTypeToString[UPDATE_SELECTION]          = "Selection";
  updateTypeToString[UPDATE_SELECTION_VERTICES] = "VertexSelection";
  updateTypeToString[UPDATE_SELECTION_EDGES]    = "EdgeSelection";
221
  updateTypeToString[UPDATE_SELECTION_HALFEDGES]= "HalfedgeSelection";
Jan Möbius's avatar
Jan Möbius committed
222
  updateTypeToString[UPDATE_SELECTION_FACES]    = "FaceSelection";
223
  updateTypeToString[UPDATE_SELECTION_KNOTS]    = "KnotSelection";
224
  updateTypeToString[UPDATE_COLOR]              = "Color";
225
226
  updateTypeToString[UPDATE_TEXTURE]            = "Texture";
  updateTypeToString[UPDATE_STATE]              = "State";
Dirk Wilden's avatar
Dirk Wilden committed
227
228
229
230
231
232
}

/// Adds a updateType and returns the id for the new type
UpdateType addUpdateType(QString _name, bool _resetNeeded) {

  //first check if it's already available
233
  std::map<QString, size_t>::iterator index = stringToUpdateTypeInfo.find( _name );
Dirk Wilden's avatar
Dirk Wilden committed
234
235
236
237
238

  if ( index != stringToUpdateTypeInfo.end() )
    return updateTypes[ index->second ].type;
  else {
  
Jan Möbius's avatar
Jan Möbius committed
239
    UpdateType type = firstFreeID_;
Dirk Wilden's avatar
Dirk Wilden committed
240
241
242
243
244
245
246

    stringToUpdateTypeInfo[ _name ] = updateTypes.size();
    updateTypeToTypeInfo[ type ] = updateTypes.size();
    updateTypes.push_back( UpdateTypeInfo(type, _name, _resetNeeded ) );

    updateTypeToString[type] = _name;

Jan Möbius's avatar
Jan Möbius committed
247
    ++firstFreeID_;
Dirk Wilden's avatar
Dirk Wilden committed
248
249
250
251
252
253
254
    return( type );
  }
}

/// Get the id of a type with given name
UpdateType updateType(QString _name) {

255
  std::map<QString, size_t>::iterator index = stringToUpdateTypeInfo.find( _name );
Dirk Wilden's avatar
Dirk Wilden committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275

  if ( index != stringToUpdateTypeInfo.end() )
    return updateTypes[ index->second ].type;
  else {
    #ifdef DEBUG
      std::cerr << "Unknown UpdateType with name " << _name.toStdString() << std::endl;
    #endif
    return UPDATE_ALL;
  }
}

/// Get the name of a type with given id
QString updateTypeName(UpdateType _id) {

  std::map<UpdateType, QString>::iterator name = updateTypeToString.find(_id);

  if ( name != updateTypeToString.end() )
    return name->second;
  else {
    #ifdef DEBUG
Jan Möbius's avatar
Jan Möbius committed
276
    std::cerr << "Unable to retrieve updateTypeName" << std::endl;
Dirk Wilden's avatar
Dirk Wilden committed
277
    #endif
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295

    QString type = "";

    for ( size_t i = 0; i < updateTypeCount(); ++i ) {
      if ( _id.contains(UpdateType(UpdateTypeSet(1) << i)) ) {
        std::map<UpdateType, QString>::iterator partName = updateTypeToString.find(UpdateType(UpdateTypeSet(1) << i));
        if ( partName != updateTypeToString.end() ) {
            type += partName->second + ";";
        } else {
          type += "Unknown;";
        }
      }
    }

    // Remove the last ;
    type.left(type.length()-1);

    return type;
Dirk Wilden's avatar
Dirk Wilden committed
296
297
298
299
  }
}

/// Return the number of registered types
300
size_t updateTypeCount() {
Dirk Wilden's avatar
Dirk Wilden committed
301
  return updateTypes.size();
302
}