ColorStack.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #include "ColorStack.hh"
00056 #include <iostream>
00057
00058
00059
00060
00061
00062 namespace ACG {
00063
00064
00065
00066
00067 ColorStack::ColorStack () :
00068 initialized_(false),
00069 root_ (0),
00070 currentNode_ (0),
00071 error_(false)
00072 {
00073 }
00074
00075
00076
00077 ColorStack::~ColorStack ()
00078 {
00079 if (root_)
00080 delete root_;
00081 }
00082
00083
00084
00085 void ColorStack::initialize ()
00086 {
00087 if (initialized_)
00088 {
00089 delete root_;
00090 }
00091 error_ = false;
00092 translator_.initialize ();
00093 root_ = currentNode_ = new ColorStack::Node (0, 0, &translator_);
00094 initialized_ = true;
00095 }
00096
00097
00098
00099 bool ColorStack::setMaximumIndex (unsigned int _idx)
00100 {
00101 if (initialized_)
00102 {
00103 bool rv = currentNode_->setMaximumIndex (_idx);
00104 if (!rv)
00105 error_ = true;
00106 return rv;
00107 }
00108 return false;
00109 }
00110
00111
00112
00113 void ColorStack::setIndex (unsigned int _idx)
00114 {
00115 if (initialized_)
00116 {
00117 if (!currentNode_->setIndex (_idx))
00118 error_ = true;
00119 }
00120 }
00121
00122
00123
00124 Vec4uc ColorStack::getIndexColor (unsigned int _idx)
00125 {
00126 if (initialized_)
00127 {
00128 Vec4uc rv;
00129 if (!currentNode_->getIndexColor (_idx, rv))
00130 error_ = true;
00131 else
00132 return rv;
00133 }
00134
00135 return Vec4uc (0, 0, 0, 0);
00136 }
00137
00138
00139
00140 void ColorStack::pushIndex (unsigned int _idx)
00141 {
00142 if (initialized_)
00143 currentNode_ = currentNode_->pushIndex (_idx);
00144 }
00145
00146
00147
00148 void ColorStack::popIndex ()
00149 {
00150 if (initialized_)
00151 currentNode_ = currentNode_->popIndex ();
00152 }
00153
00154
00155
00156 std::vector<unsigned int> ColorStack::colorToStack (Vec4uc _rgba) const
00157 {
00158 std::vector<unsigned int> rv(0);
00159 if (initialized_ && !error_)
00160 {
00161 unsigned int idx = translator_.color2index (_rgba);
00162 if (idx >= root_->startIndex () && idx < root_->endIndex ())
00163 root_->colorToStack (rv, idx);
00164 }
00165 return rv;
00166 }
00167
00168
00169
00170 unsigned int ColorStack::freeIndicies() const
00171 {
00172 if (initialized_)
00173 {
00174 return translator_.max_index () - currentNode_->endIndex ();
00175 }
00176 else
00177 return 0;
00178 }
00179
00180
00181
00182 unsigned int ColorStack::currentIndex () const
00183 {
00184 if (initialized_)
00185 {
00186 return currentNode_->colorIndex ();
00187 }
00188 return 0;
00189 }
00190
00191
00192
00193
00194 ColorStack::Node::Node (unsigned int _idx, Node *_parent, ColorTranslator *_ct) :
00195 parent_(_parent),
00196 index_(_idx),
00197 translator_(_ct),
00198 colorStartIdx_(0),
00199 colorEndIdx_(0)
00200 {
00201 if (parent_)
00202 startIdx_ = endIdx_ = parent_->endIndex ();
00203 else
00204 startIdx_ = endIdx_ = 1;
00205 }
00206
00207
00208
00209 ColorStack::Node::~Node ()
00210 {
00211 for (std::vector<Node *>::iterator it = nodes_.begin (); it != nodes_.end(); ++it)
00212 delete (*it);
00213 }
00214
00215
00216
00217 bool ColorStack::Node::setMaximumIndex (unsigned int _idx)
00218 {
00219 if (_idx == 0)
00220 _idx = 1;
00221
00222 if (colorStartIdx_ == 0 && translator_->max_index () > endIdx_ + _idx)
00223 {
00224 colorStartIdx_ = endIdx_;
00225 endIdx_ = colorEndIdx_ = colorStartIdx_ + _idx;
00226 return true;
00227 }
00228 return false;
00229 }
00230
00231
00232
00233 bool ColorStack::Node::setIndex (unsigned int _idx) const
00234 {
00235 if (colorStartIdx_ && colorStartIdx_ + _idx < colorEndIdx_)
00236 {
00237 glColor(translator_->index2color(colorStartIdx_ + _idx));
00238 return true;
00239 }
00240 return false;
00241 }
00242
00243
00244
00245 bool ColorStack::Node::getIndexColor (unsigned int _idx, Vec4uc &_rgba) const
00246 {
00247 if (colorStartIdx_ && colorStartIdx_ + _idx < colorEndIdx_)
00248 {
00249 _rgba = translator_->index2color(colorStartIdx_ + _idx);
00250 return true;
00251 }
00252 return false;
00253 }
00254
00255
00256
00257 ColorStack::Node * ColorStack::Node::pushIndex (unsigned int _idx)
00258 {
00259 ColorStack::Node *n = new ColorStack::Node (_idx, this, translator_);
00260 nodes_.push_back (n);
00261 return n;
00262 }
00263
00264
00265
00266 ColorStack::Node * ColorStack::Node::popIndex ()
00267 {
00268 parent_->endIdx_ = endIdx_;
00269 return parent_;
00270 }
00271
00272
00273
00274 void ColorStack::Node::colorToStack (std::vector<unsigned int> &_stack, unsigned int _index)
00275 {
00276 if (_index >= colorStartIdx_ && _index < colorEndIdx_)
00277 {
00278 _stack.push_back (_index - colorStartIdx_);
00279 }
00280 else
00281 {
00282 for (std::vector<Node *>::iterator it = nodes_.begin (); it != nodes_.end(); ++it)
00283 {
00284 ColorStack::Node *n = (*it);
00285 if (_index >= n->startIndex () && _index < n->endIndex ())
00286 n->colorToStack (_stack, _index);
00287 }
00288 }
00289 _stack.push_back (index_);
00290 }
00291
00292
00293 }
00294
00295