Developer Documentation
UpdateType.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 #include "TypesInternal.hh"
43 #include "UpdateType.hh"
44 #include <QCoreApplication>
46 
47 
50 static std::map< UpdateType, QString > updateTypeToString;
51 
54 static std::map< QString , size_t > stringToUpdateTypeInfo;
55 
58 static std::map< UpdateType , size_t > updateTypeToTypeInfo;
59 
62 static UpdateType firstFreeID_(UPDATE_UNUSED);
63 
64 
65 
66 UpdateType::UpdateType()
67 :type_(0)
68 {
69 
70 }
71 
72 UpdateType::UpdateType(const UpdateTypeSet& _set)
73 : type_(_set)
74 {
75 
76 }
77 
78 bool UpdateType::operator==(const UpdateType& _type) const {
79  return ( (type_ & _type.type_) != 0);
80 };
81 
83 UpdateType::operator|(const UpdateType& _type) const
84 {
85  return UpdateType(type_|_type.type_);
86 }
87 
88 
89 UpdateType&
90 UpdateType::operator|=(const UpdateType& _type)
91 {
92  type_ |= _type.type_;
93 
94  return(*this);
95 }
96 
97 
99 bool UpdateType::contains( const UpdateType& _type ) const {
100 
101  if ( type_ == UPDATE_ALL.type_ )
102  return true;
103 
104  // Catch the specialization of updates
105  if ( _type == UPDATE_SELECTION ) {
106  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_ )
107  return true;
108  }
109 
110  return ( (type_ & _type.type_) != 0 );
111 }
112 
113 UpdateType& UpdateType::operator++() {
114 
115  UpdateTypeSet check = type_; // count the number of bits set in type_
116  unsigned int count; // c accumulates the total bits set in v
117  for (count = 0; check; count++)
118  {
119  check &= check - 1; // clear the least significant bit set
120  }
121 
122  if ( count != 1 ) {
123  std::cerr << "Operator ++ for UpdateType which is not atomic!!" << std::endl;
124  }
125 
126  type_ <<= 1;
127 
128  return (*this);
129 }
130 
131 bool UpdateType::operator<( const UpdateType& _i ) const {
132  return (type_ < _i.type_);
133 }
134 
136 
137  public:
138 
139  UpdateTypeInfo(const UpdateType& _type, QString _name, bool _needsScenegraphReset ) :
140  type(_type),
141  name(_name),
142  resetNeeded(_needsScenegraphReset)
143  {
144  }
145 
148 
150  QString name;
151 
154 };
155 
156 static std::vector< UpdateTypeInfo > updateTypes;
157 
158 
159 // functions -------------------------------------------------
160 
161 
162 void initializeUpdateTypes() {
163 
164  stringToUpdateTypeInfo["All"] = updateTypes.size();
165  updateTypeToTypeInfo[UPDATE_ALL] = updateTypes.size();
166  updateTypes.push_back( UpdateTypeInfo(UPDATE_ALL, "All", true) );
167 
168  stringToUpdateTypeInfo["Visibility"] = updateTypes.size();
169  updateTypeToTypeInfo[UPDATE_VISIBILITY] = updateTypes.size();
170  updateTypes.push_back( UpdateTypeInfo(UPDATE_VISIBILITY, "Visibility", true) );
171 
172  stringToUpdateTypeInfo["Geometry"] = updateTypes.size();
173  updateTypeToTypeInfo[UPDATE_GEOMETRY] = updateTypes.size();
174  updateTypes.push_back( UpdateTypeInfo(UPDATE_GEOMETRY, "Geometry", true) );
175 
176  stringToUpdateTypeInfo["Topology"] = updateTypes.size();
177  updateTypeToTypeInfo[UPDATE_TOPOLOGY] = updateTypes.size();
178  updateTypes.push_back( UpdateTypeInfo(UPDATE_TOPOLOGY, "Topology", true) );
179 
180  stringToUpdateTypeInfo["Selection"] = updateTypes.size();
181  updateTypeToTypeInfo[UPDATE_SELECTION] = updateTypes.size();
182  updateTypes.push_back( UpdateTypeInfo(UPDATE_SELECTION, "Selection", true) );
183 
184  stringToUpdateTypeInfo["VertexSelection"] = updateTypes.size();
185  updateTypeToTypeInfo[UPDATE_SELECTION_VERTICES] = updateTypes.size();
186  updateTypes.push_back( UpdateTypeInfo(UPDATE_SELECTION_VERTICES, "VertexSelection", true) );
187 
188  stringToUpdateTypeInfo["EdgeSelection"] = updateTypes.size();
189  updateTypeToTypeInfo[UPDATE_SELECTION_EDGES] = updateTypes.size();
190  updateTypes.push_back( UpdateTypeInfo(UPDATE_SELECTION_EDGES, "EdgeSelection", true) );
191 
192  stringToUpdateTypeInfo["HalfedgeSelection"] = updateTypes.size();
193  updateTypeToTypeInfo[UPDATE_SELECTION_HALFEDGES] = updateTypes.size();
194  updateTypes.push_back( UpdateTypeInfo(UPDATE_SELECTION_HALFEDGES, "HalfedgeSelection", true) );
195 
196  stringToUpdateTypeInfo["FaceSelection"] = updateTypes.size();
197  updateTypeToTypeInfo[UPDATE_SELECTION_FACES] = updateTypes.size();
198  updateTypes.push_back( UpdateTypeInfo(UPDATE_SELECTION_FACES, "FaceSelection", true) );
199 
200  stringToUpdateTypeInfo["KnotSelection"] = updateTypes.size();
201  updateTypeToTypeInfo[UPDATE_SELECTION_KNOTS] = updateTypes.size();
202  updateTypes.push_back( UpdateTypeInfo(UPDATE_SELECTION_KNOTS, "KnotSelection", true) );
203 
204  stringToUpdateTypeInfo["Color"] = updateTypes.size();
205  updateTypeToTypeInfo[UPDATE_COLOR] = updateTypes.size();
206  updateTypes.push_back( UpdateTypeInfo(UPDATE_COLOR, "Color", true) );
207 
208  stringToUpdateTypeInfo["Texture"] = updateTypes.size();
209  updateTypeToTypeInfo[UPDATE_TEXTURE] = updateTypes.size();
210  updateTypes.push_back( UpdateTypeInfo(UPDATE_TEXTURE, "Texture", true) );
211 
212  stringToUpdateTypeInfo["State"] = updateTypes.size();
213  updateTypeToTypeInfo[UPDATE_STATE] = updateTypes.size();
214  updateTypes.push_back( UpdateTypeInfo(UPDATE_STATE, "State", true) );
215 
216  updateTypeToString[UPDATE_ALL] = "All";
217  updateTypeToString[UPDATE_VISIBILITY] = "Visibility";
218  updateTypeToString[UPDATE_GEOMETRY] = "Geometry";
219  updateTypeToString[UPDATE_TOPOLOGY] = "Topology";
220  updateTypeToString[UPDATE_SELECTION] = "Selection";
221  updateTypeToString[UPDATE_SELECTION_VERTICES] = "VertexSelection";
222  updateTypeToString[UPDATE_SELECTION_EDGES] = "EdgeSelection";
223  updateTypeToString[UPDATE_SELECTION_HALFEDGES]= "HalfedgeSelection";
224  updateTypeToString[UPDATE_SELECTION_FACES] = "FaceSelection";
225  updateTypeToString[UPDATE_SELECTION_KNOTS] = "KnotSelection";
226  updateTypeToString[UPDATE_COLOR] = "Color";
227  updateTypeToString[UPDATE_TEXTURE] = "Texture";
228  updateTypeToString[UPDATE_STATE] = "State";
229 }
230 
232 UpdateType addUpdateType(QString _name, bool _resetNeeded) {
233 
234  //first check if it's already available
235  std::map<QString, size_t>::iterator index = stringToUpdateTypeInfo.find( _name );
236 
237  if ( index != stringToUpdateTypeInfo.end() )
238  return updateTypes[ index->second ].type;
239  else {
240 
241  UpdateType type = firstFreeID_;
242 
243  stringToUpdateTypeInfo[ _name ] = updateTypes.size();
244  updateTypeToTypeInfo[ type ] = updateTypes.size();
245  updateTypes.push_back( UpdateTypeInfo(type, _name, _resetNeeded ) );
246 
247  updateTypeToString[type] = _name;
248 
249  ++firstFreeID_;
250  return( type );
251  }
252 }
253 
255 UpdateType updateType(QString _name) {
256 
257  std::map<QString, size_t>::iterator index = stringToUpdateTypeInfo.find( _name );
258 
259  if ( index != stringToUpdateTypeInfo.end() )
260  return updateTypes[ index->second ].type;
261  else {
262  #ifdef DEBUG
263  std::cerr << "Unknown UpdateType with name " << _name.toStdString() << std::endl;
264  #endif
265  return UPDATE_ALL;
266  }
267 }
268 
271 
272  std::map<UpdateType, QString>::iterator name = updateTypeToString.find(_id);
273 
274  if ( name != updateTypeToString.end() )
275  return name->second;
276  else {
277  #ifdef DEBUG
278  std::cerr << "Unable to retrieve updateTypeName" << std::endl;
279  #endif
280 
281  QString type = "";
282 
283  for ( size_t i = 0; i < updateTypeCount(); ++i ) {
284  if ( _id.contains(UpdateType(UpdateTypeSet(1) << i)) ) {
285  std::map<UpdateType, QString>::iterator partName = updateTypeToString.find(UpdateType(UpdateTypeSet(1) << i));
286  if ( partName != updateTypeToString.end() ) {
287  type += partName->second + ";";
288  } else {
289  type += "Unknown;";
290  }
291  }
292  }
293 
294  // Remove the last ;
295  type = type.left(type.length()-1);
296 
297  return type;
298  }
299 }
300 
302 size_t updateTypeCount() {
303  return updateTypes.size();
304 }
const UpdateType UPDATE_TOPOLOGY(UpdateTypeSet(8))
Topology updated.
UpdateType addUpdateType(QString _name, bool _resetNeeded)
Adds a updateType and returns the id for the new type.
Definition: UpdateType.cc:232
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
QString updateTypeName(UpdateType _id)
Get the name of a type with given id.
Definition: UpdateType.cc:270
const UpdateType UPDATE_SELECTION(UpdateTypeSet(16))
Selection updated.
QString name
The name of the UpdateType.
Definition: UpdateType.cc:150
const UpdateType UPDATE_TEXTURE(UpdateTypeSet(2048))
Textures have changed.
const UpdateType UPDATE_GEOMETRY(UpdateTypeSet(4))
Geometry updated.
const UpdateType UPDATE_SELECTION_HALFEDGES(UpdateTypeSet(128))
Halfedge selection has changed.
const UpdateType UPDATE_UNUSED(UpdateTypeSet(8192))
marks the last used ID
const UpdateType UPDATE_STATE(UpdateTypeSet(4096))
State has changed.
Update type class.
Definition: UpdateType.hh:59
bool resetNeeded
is a sceneGraph reset needed for this update
Definition: UpdateType.cc:153
bool contains(const UpdateType &_type) const
Check if this update contains the given UpdateType.
Definition: UpdateType.cc:99
const UpdateType UPDATE_SELECTION_KNOTS(UpdateTypeSet(512))
Knot selection has changed.
const UpdateType UPDATE_COLOR(UpdateTypeSet(1024))
Colors have changed.
const UpdateType UPDATE_SELECTION_EDGES(UpdateTypeSet(64))
Edge selection has changed.
const UpdateType UPDATE_VISIBILITY(UpdateTypeSet(2))
This is the update identifier for global Object visibility ( show/hide )
const UpdateType UPDATE_SELECTION_FACES(UpdateTypeSet(256))
Face selection has changed.
bool operator==(const UpdateType &_type) const
Exact compare operator.
Definition: UpdateType.cc:78
size_t updateTypeCount()
Return the number of registered types.
Definition: UpdateType.cc:302
UpdateType type
The id of the UpdateType.
Definition: UpdateType.cc:147
UpdateType updateType(QString _name)
Get the id of a type with given name.
Definition: UpdateType.cc:255
const UpdateType UPDATE_SELECTION_VERTICES(UpdateTypeSet(32))
Vertex selection has changed.