Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ColorStack.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  * *
44  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 
52 //=============================================================================
53 //
54 // CLASS ColorStack - IMPLEMENTATION
55 //
56 //=============================================================================
57 
58 
59 //== INCLUDES =================================================================
60 
61 
62 #include "ColorStack.hh"
63 #include <iostream>
64 
65 
66 //== NAMESPACES ===============================================================
67 
68 
69 namespace ACG {
70 
71 
72 //== IMPLEMENTATION ==========================================================
73 
75  initialized_(false),
76  root_ (0),
77  currentNode_ (0),
78  error_(false)
79 {
80 }
81 
82 //----------------------------------------------------------------------------
83 
85 {
86  if (root_)
87  delete root_;
88 }
89 
90 //----------------------------------------------------------------------------
91 
93 {
94  if (initialized_)
95  {
96  delete root_;
97  }
98  error_ = false;
99  translator_.initialize ();
100  root_ = currentNode_ = new ColorStack::Node (0, 0, &translator_);
101  initialized_ = true;
102 }
103 
104 //----------------------------------------------------------------------------
105 
106 bool ColorStack::setMaximumIndex (unsigned int _idx)
107 {
108  if (initialized_)
109  {
110  bool rv = currentNode_->setMaximumIndex (_idx);
111  if (!rv)
112  error_ = true;
113  return rv;
114  }
115  return false;
116 }
117 
118 //----------------------------------------------------------------------------
119 
120 void ColorStack::setIndex (unsigned int _idx)
121 {
122  if (initialized_)
123  {
124  if (!currentNode_->setIndex (_idx))
125  error_ = true;
126  }
127 }
128 
129 //----------------------------------------------------------------------------
130 
132 {
133  if (initialized_)
134  {
135  Vec4uc rv;
136  if (!currentNode_->getIndexColor (_idx, rv))
137  error_ = true;
138  else
139  return rv;
140  }
141 
142  return Vec4uc (0, 0, 0, 0);
143 }
144 
145 //----------------------------------------------------------------------------
146 
147 void ColorStack::pushIndex (unsigned int _idx)
148 {
149  if (initialized_)
150  currentNode_ = currentNode_->pushIndex (_idx);
151 }
152 
153 //----------------------------------------------------------------------------
154 
156 {
157  if (initialized_)
158  currentNode_ = currentNode_->popIndex ();
159 }
160 
161 //----------------------------------------------------------------------------
162 
163 std::vector<unsigned int> ColorStack::colorToStack (Vec4uc _rgba) const
164 {
165  std::vector<unsigned int> rv(0);
166  if (initialized_ && !error_)
167  {
168  unsigned int idx = translator_.color2index (_rgba);
169  if (idx >= root_->startIndex () && idx < root_->endIndex ())
170  root_->colorToStack (rv, idx);
171  }
172  return rv;
173 }
174 
175 //----------------------------------------------------------------------------
176 
177 unsigned int ColorStack::freeIndicies() const
178 {
179  if (initialized_)
180  {
181  return translator_.max_index () - currentNode_->endIndex ();
182  }
183  else
184  return 0;
185 }
186 
187 //----------------------------------------------------------------------------
188 
189 unsigned int ColorStack::currentIndex () const
190 {
191  if (initialized_)
192  {
193  return currentNode_->colorIndex ();
194  }
195  return 0;
196 }
197 
198 
199 //----------------------------------------------------------------------------
200 
201 ColorStack::Node::Node (unsigned int _idx, Node *_parent, ColorTranslator *_ct) :
202  parent_(_parent),
203  index_(_idx),
204  translator_(_ct),
205  colorStartIdx_(0),
206  colorEndIdx_(0)
207 {
208  if (parent_)
209  startIdx_ = endIdx_ = parent_->endIndex ();
210  else
211  startIdx_ = endIdx_ = 1;
212 }
213 
214 //----------------------------------------------------------------------------
215 
216 ColorStack::Node::~Node ()
217 {
218  for (std::vector<Node *>::iterator it = nodes_.begin (); it != nodes_.end(); ++it)
219  delete (*it);
220 }
221 
222 //----------------------------------------------------------------------------
223 
224 bool ColorStack::Node::setMaximumIndex (unsigned int _idx)
225 {
226  if (_idx == 0)
227  _idx = 1;
228 
229  if (colorStartIdx_ == 0 && translator_->max_index () > endIdx_ + _idx)
230  {
231  colorStartIdx_ = endIdx_;
232  endIdx_ = colorEndIdx_ = colorStartIdx_ + _idx;
233  return true;
234  }
235  return false;
236 }
237 
238 //----------------------------------------------------------------------------
239 
240 bool ColorStack::Node::setIndex (unsigned int _idx) const
241 {
242  if (colorStartIdx_ && colorStartIdx_ + _idx < colorEndIdx_)
243  {
244  glColor(translator_->index2color(colorStartIdx_ + _idx));
245  return true;
246  }
247  return false;
248 }
249 
250 //----------------------------------------------------------------------------
251 
252 bool ColorStack::Node::getIndexColor (unsigned int _idx, Vec4uc &_rgba) const
253 {
254  if (colorStartIdx_ && colorStartIdx_ + _idx < colorEndIdx_)
255  {
256  _rgba = translator_->index2color(colorStartIdx_ + _idx);
257  return true;
258  }
259  return false;
260 }
261 
262 //----------------------------------------------------------------------------
263 
265 {
266  ColorStack::Node *n = new ColorStack::Node (_idx, this, translator_);
267  nodes_.push_back (n);
268  return n;
269 }
270 
271 //----------------------------------------------------------------------------
272 
274 {
275  parent_->endIdx_ = endIdx_;
276  return parent_;
277 }
278 
279 //----------------------------------------------------------------------------
280 
281 void ColorStack::Node::colorToStack (std::vector<unsigned int> &_stack, unsigned int _index)
282 {
283  if (_index >= colorStartIdx_ && _index < colorEndIdx_)
284  {
285  _stack.push_back (_index - colorStartIdx_);
286  }
287  else
288  {
289  for (std::vector<Node *>::iterator it = nodes_.begin (); it != nodes_.end(); ++it)
290  {
291  ColorStack::Node *n = (*it);
292  if (_index >= n->startIndex () && _index < n->endIndex ())
293  n->colorToStack (_stack, _index);
294  }
295  }
296  _stack.push_back (index_);
297 }
298 
299 //=============================================================================
300 } // namespace ACG
301 //=============================================================================
302 
bool setMaximumIndex(unsigned int _idx)
sets the maximum index number used in current node
Definition: ColorStack.cc:224
void initialize()
init (takes current GL context)
void popIndex()
pops the current node from the stack (like glPopName)
Definition: ColorStack.cc:155
void pushIndex(unsigned int _idx)
creates a new node the stack (like glPushName)
Definition: ColorStack.cc:147
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
bool getIndexColor(unsigned int _idx, Vec4uc &_rgba) const
gets the color instead of setting it directly
Definition: ColorStack.cc:252
std::vector< unsigned int > colorToStack(Vec4uc _rgba) const
converts the given color to index values on the stack
Definition: ColorStack.cc:163
int color2index(Vec4uc _rgba) const
color -> index (one buffer)
bool setIndex(unsigned int _idx) const
sets the current color the given index (like glLoadName)
Definition: ColorStack.cc:240
Node * popIndex()
pops the current node from the stack (like glPopName)
Definition: ColorStack.cc:273
void glColor(const Vec3f &_v)
Wrapper: glColor for Vec3f.
Definition: gl.hh:146
Vec4uc getIndexColor(unsigned int _idx)
gets the color instead of setting it directly
Definition: ColorStack.cc:131
~ColorStack()
Destructor.
Definition: ColorStack.cc:84
Vec4uc index2color(unsigned int _idx) const
index -> color (one buffer)
unsigned int max_index() const
returns maximal convertable index
unsigned int freeIndicies() const
returns maximal available index count
Definition: ColorStack.cc:177
VectorT< unsigned char, 4 > Vec4uc
Definition: VectorT.hh:134
Node * pushIndex(unsigned int _idx)
creates a new node the stack (like glPushName)
Definition: ColorStack.cc:264
bool setMaximumIndex(unsigned int _idx)
sets the maximum index number used in current node
Definition: ColorStack.cc:106
ColorStack()
Default constructor.
Definition: ColorStack.cc:74
void initialize()
init (takes current GL context/ like glInitNames (); glPushName (0))
Definition: ColorStack.cc:92
void setIndex(unsigned int _idx)
sets the current color the given index (like glLoadName)
Definition: ColorStack.cc:120
unsigned int currentIndex() const
returns the current color index
Definition: ColorStack.cc:189