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