Commit dad2e0c4 authored by Jan Möbius's avatar Jan Möbius
Browse files

Changed color translator to use size_t

parent 0ae7ab82
Pipeline #3277 canceled with stage
in 36 minutes and 40 seconds
......@@ -103,7 +103,7 @@ void ColorStack::initialize ()
//----------------------------------------------------------------------------
bool ColorStack::setMaximumIndex (unsigned int _idx)
bool ColorStack::setMaximumIndex (size_t _idx)
{
if (initialized_)
{
......@@ -117,7 +117,7 @@ bool ColorStack::setMaximumIndex (unsigned int _idx)
//----------------------------------------------------------------------------
void ColorStack::setIndex (unsigned int _idx)
void ColorStack::setIndex (size_t _idx)
{
if (initialized_)
{
......@@ -128,7 +128,7 @@ void ColorStack::setIndex (unsigned int _idx)
//----------------------------------------------------------------------------
Vec4uc ColorStack::getIndexColor (unsigned int _idx)
Vec4uc ColorStack::getIndexColor (size_t _idx)
{
if (initialized_)
{
......@@ -144,7 +144,7 @@ Vec4uc ColorStack::getIndexColor (unsigned int _idx)
//----------------------------------------------------------------------------
void ColorStack::pushIndex (unsigned int _idx)
void ColorStack::pushIndex (size_t _idx)
{
if (initialized_)
currentNode_ = currentNode_->pushIndex (_idx);
......@@ -160,12 +160,12 @@ void ColorStack::popIndex ()
//----------------------------------------------------------------------------
std::vector<unsigned int> ColorStack::colorToStack (Vec4uc _rgba) const
std::vector<size_t> ColorStack::colorToStack (Vec4uc _rgba) const
{
std::vector<unsigned int> rv(0);
std::vector<size_t> rv(0);
if (initialized_ && !error_)
{
unsigned int idx = translator_.color2index (_rgba);
const size_t idx = translator_.color2index (_rgba);
if (idx >= root_->startIndex () && idx < root_->endIndex ())
root_->colorToStack (rv, idx);
}
......@@ -174,7 +174,7 @@ std::vector<unsigned int> ColorStack::colorToStack (Vec4uc _rgba) const
//----------------------------------------------------------------------------
unsigned int ColorStack::freeIndicies() const
size_t ColorStack::freeIndicies() const
{
if (initialized_)
{
......@@ -186,7 +186,7 @@ unsigned int ColorStack::freeIndicies() const
//----------------------------------------------------------------------------
unsigned int ColorStack::currentIndex () const
size_t ColorStack::currentIndex () const
{
if (initialized_)
{
......@@ -198,7 +198,7 @@ unsigned int ColorStack::currentIndex () const
//----------------------------------------------------------------------------
ColorStack::Node::Node (unsigned int _idx, Node *_parent, ColorTranslator *_ct) :
ColorStack::Node::Node (size_t _idx, Node *_parent, ColorTranslator *_ct) :
parent_(_parent),
index_(_idx),
translator_(_ct),
......@@ -221,7 +221,7 @@ ColorStack::Node::~Node ()
//----------------------------------------------------------------------------
bool ColorStack::Node::setMaximumIndex (unsigned int _idx)
bool ColorStack::Node::setMaximumIndex (size_t _idx)
{
if (_idx == 0)
_idx = 1;
......@@ -237,7 +237,7 @@ bool ColorStack::Node::setMaximumIndex (unsigned int _idx)
//----------------------------------------------------------------------------
bool ColorStack::Node::setIndex (unsigned int _idx) const
bool ColorStack::Node::setIndex (size_t _idx) const
{
if (colorStartIdx_ && colorStartIdx_ + _idx < colorEndIdx_)
{
......@@ -249,7 +249,7 @@ bool ColorStack::Node::setIndex (unsigned int _idx) const
//----------------------------------------------------------------------------
bool ColorStack::Node::getIndexColor (unsigned int _idx, Vec4uc &_rgba) const
bool ColorStack::Node::getIndexColor (size_t _idx, Vec4uc &_rgba) const
{
if (colorStartIdx_ && colorStartIdx_ + _idx < colorEndIdx_)
{
......@@ -261,7 +261,7 @@ bool ColorStack::Node::getIndexColor (unsigned int _idx, Vec4uc &_rgba) const
//----------------------------------------------------------------------------
ColorStack::Node * ColorStack::Node::pushIndex (unsigned int _idx)
ColorStack::Node * ColorStack::Node::pushIndex (size_t _idx)
{
ColorStack::Node *n = new ColorStack::Node (_idx, this, translator_);
nodes_.push_back (n);
......@@ -278,7 +278,7 @@ ColorStack::Node * ColorStack::Node::popIndex ()
//----------------------------------------------------------------------------
void ColorStack::Node::colorToStack (std::vector<unsigned int> &_stack, unsigned int _index)
void ColorStack::Node::colorToStack (std::vector<size_t> &_stack, size_t _index)
{
if (_index >= colorStartIdx_ && _index < colorEndIdx_)
{
......
......@@ -95,31 +95,31 @@ public:
bool initialized() const { return initialized_; }
/// sets the maximum index number used in current node
bool setMaximumIndex (unsigned int _idx);
bool setMaximumIndex (size_t _idx);
/// sets the current color the given index (like glLoadName)
void setIndex (unsigned int _idx);
void setIndex (size_t _idx);
/// gets the color instead of setting it directly
Vec4uc getIndexColor (unsigned int _idx);
Vec4uc getIndexColor (size_t _idx);
/// creates a new node the stack (like glPushName)
void pushIndex (unsigned int _idx);
void pushIndex (size_t _idx);
/// pops the current node from the stack (like glPopName)
void popIndex ();
/// converts the given color to index values on the stack
std::vector<unsigned int> colorToStack (Vec4uc _rgba) const;
std::vector<size_t> colorToStack (Vec4uc _rgba) const;
/// returns maximal available index count
unsigned int freeIndicies () const;
size_t freeIndicies () const;
/// Did an error occur during picking
bool error () const { return error_ && initialized_; };
/// returns the current color index
unsigned int currentIndex () const;
size_t currentIndex () const;
private:
......@@ -127,40 +127,40 @@ private:
class Node {
public:
Node (unsigned int _idx, Node *_parent, ColorTranslator *_ct);
Node (size_t _idx, Node *_parent, ColorTranslator *_ct);
~Node ();
/// sets the maximum index number used in current node
bool setMaximumIndex (unsigned int _idx);
bool setMaximumIndex (size_t _idx);
/// sets the current color the given index (like glLoadName)
bool setIndex (unsigned int _idx) const;
bool setIndex (size_t _idx) const;
/// gets the color instead of setting it directly
bool getIndexColor (unsigned int _idx, Vec4uc &_rgba) const;
bool getIndexColor (size_t _idx, Vec4uc &_rgba) const;
/// creates a new node the stack (like glPushName)
Node * pushIndex (unsigned int _idx);
Node * pushIndex (size_t _idx);
/// pops the current node from the stack (like glPopName)
Node * popIndex ();
void colorToStack (std::vector<unsigned int> &_stack, unsigned int _index);
void colorToStack (std::vector<size_t> &_stack, size_t size_t);
unsigned int startIndex () { return startIdx_; };
unsigned int endIndex () { return endIdx_; };
unsigned int colorIndex () { return colorStartIdx_; };
size_t startIndex () const { return startIdx_; };
size_t endIndex () const { return endIdx_; };
size_t colorIndex () const { return colorStartIdx_; };
private:
Node *parent_;
unsigned int index_;
size_t index_;
ColorTranslator *translator_;
std::vector<Node *> nodes_;
unsigned int startIdx_;
unsigned int endIdx_;
unsigned int colorStartIdx_;
unsigned int colorEndIdx_;
size_t startIdx_;
size_t endIdx_;
size_t colorStartIdx_;
size_t colorEndIdx_;
};
......
......@@ -61,6 +61,7 @@
#include "ColorTranslator.hh"
#include <iostream>
#include <limits>
//== NAMESPACES ===============================================================
......@@ -81,21 +82,25 @@ initialize()
glGetIntegerv( GL_BLUE_BITS, &blue_bits_ );
glGetIntegerv( GL_ALPHA_BITS, &alpha_bits_ );
// We currently only support up to 8 bits per channel (
if (red_bits_ > 8) red_bits_ = 8;
if (green_bits_ > 8) green_bits_ = 8;
if (blue_bits_ > 8) blue_bits_ = 8;
if (alpha_bits_ > 8) alpha_bits_ = 8;
// Compute the mask to extract the component
red_mask_ = ((1 << red_bits_) - 1);
green_mask_ = ((1 << green_bits_) - 1);
blue_mask_ = ((1 << blue_bits_) - 1);
alpha_mask_ = ((1 << alpha_bits_) - 1);
// Shift required to move the component to the lowest bits
red_shift_ = 8 - red_bits_;
green_shift_ = 8 - green_bits_;
blue_shift_ = 8 - blue_bits_;
alpha_shift_ = 8 - alpha_bits_;
red_round_ = 1 << (red_shift_ - 1);
green_round_ = 1 << (green_shift_ - 1);
blue_round_ = 1 << (blue_shift_ - 1);
......@@ -110,12 +115,19 @@ initialize()
Vec4uc
ColorTranslator::
index2color(unsigned int _idx) const
index2color(const size_t _idx) const
{
assert(initialized());
unsigned char r, g, b, a;
unsigned int idx(_idx+1);
// Make sure that the number fits
if ( _idx > std::numeric_limits<unsigned int>::max() ) {
std::cerr << "Can't convert index " << _idx << " to RGBA. Number too large for unsigned int \n";
return Vec4uc(0, 0, 0, 0);
}
unsigned int idx = ( static_cast<unsigned int>(_idx) + 1);
b = ((idx & blue_mask_) << blue_shift_) | blue_round_;
idx >>= blue_bits_;
g = ((idx & green_mask_) << green_shift_) | green_round_;
......@@ -139,13 +151,18 @@ index2color(unsigned int _idx) const
//-----------------------------------------------------------------------------
int
size_t
ColorTranslator::
color2index(Vec4uc _rgba) const
color2index(const Vec4uc _rgba) const
{
assert(initialized());
// Work internally with#include <iostream> unsigned int for now
unsigned int result;
// Combine the single unsigned chars according to masks
result = _rgba[3] >> alpha_shift_;
result <<= red_bits_;
result = _rgba[0] >> red_shift_;
......@@ -154,14 +171,15 @@ color2index(Vec4uc _rgba) const
result <<= blue_bits_;
result |= _rgba[2] >> blue_shift_;
return (result-1);
// Return size_t Here
return ( static_cast<size_t>(result-1) );
}
//-----------------------------------------------------------------------------
unsigned int
size_t
ColorTranslator::max_index() const
{
assert(initialized());
......
......@@ -95,20 +95,23 @@ public:
~ColorTranslator() {};
/// init (takes current GL context)
/// init (takes current GL context to get the component sizes)
/// Can't use constructor as we might not have a context at this point.
void initialize();
/// has it been initialized?
bool initialized() const { return initialized_; }
/// index -> color (one buffer)
Vec4uc index2color(unsigned int _idx) const;
Vec4uc index2color(const size_t _idx) const;
/// color -> index (one buffer)
int color2index(Vec4uc _rgba) const;
size_t color2index(const Vec4uc _rgba) const;
/// returns maximal convertable index
unsigned int max_index() const;
/// returns maximal convertible index
size_t max_index() const;
private:
......
......@@ -1044,7 +1044,7 @@ void GLState::pick_init (bool _color)
//-----------------------------------------------------------------------------
bool GLState::pick_set_maximum (unsigned int _idx)
bool GLState::pick_set_maximum (size_t _idx)
{
bool rv = colorStack_.setMaximumIndex (_idx);
if (colorPicking_)
......@@ -1054,14 +1054,14 @@ bool GLState::pick_set_maximum (unsigned int _idx)
//-----------------------------------------------------------------------------
void GLState::pick_set_name (unsigned int _idx)
void GLState::pick_set_name (size_t _idx)
{
colorStack_.setIndex (_idx);
}
//-----------------------------------------------------------------------------
Vec4uc GLState::pick_get_name_color (unsigned int _idx)
Vec4uc GLState::pick_get_name_color (size_t _idx)
{
if (colorPicking_)
return colorStack_.getIndexColor (_idx);
......@@ -1070,7 +1070,7 @@ Vec4uc GLState::pick_get_name_color (unsigned int _idx)
//-----------------------------------------------------------------------------
void GLState::pick_push_name (unsigned int _idx)
void GLState::pick_push_name (size_t _idx)
{
colorStack_.pushIndex (_idx);
}
......@@ -1084,16 +1084,16 @@ void GLState::pick_pop_name ()
//-----------------------------------------------------------------------------
std::vector<unsigned int> GLState::pick_color_to_stack (Vec4uc _rgba) const
std::vector<size_t> GLState::pick_color_to_stack (Vec4uc _rgba) const
{
if (colorPicking_ && colorStack_.initialized ())
return colorStack_.colorToStack (_rgba);
return std::vector<unsigned int> ();
return std::vector<size_t> ();
}
//-----------------------------------------------------------------------------
unsigned int GLState::pick_free_indicies () const
size_t GLState::pick_free_indicies () const
{
if (colorPicking_ && colorStack_.initialized ())
return colorStack_.freeIndicies ();
......@@ -1111,7 +1111,7 @@ bool GLState::pick_error () const
//-----------------------------------------------------------------------------
unsigned int GLState::pick_current_index () const
size_t GLState::pick_current_index () const
{
if (colorPicking_)
return colorStack_.currentIndex ();
......
......@@ -1095,7 +1095,7 @@ public:
* If you create picking for your nodes, the values set with pick_set_name have to be between zero and
* this value.
*/
bool pick_set_maximum (unsigned int _idx);
bool pick_set_maximum (size_t _idx);
/** \brief sets the current name/color (like glLoadName(_idx))
*
......@@ -1107,25 +1107,25 @@ public:
* If you perform the picking and click on the second component, the returned index will be 1 as set in
* the node.
*/
void pick_set_name (unsigned int _idx);
void pick_set_name (size_t _idx);
/// returns the color that will be used for index _idx during color picking if this index will be set
/// with pick_set_name. This can be used to generate color arrays instead of using direct gl calls
/// for each primitive
Vec4uc pick_get_name_color (unsigned int _idx);
Vec4uc pick_get_name_color (size_t _idx);
/// creates a new name the stack (like glPushName())
void pick_push_name (unsigned int _idx);
void pick_push_name (size_t _idx);
/// pops the current name from the stack (like glPopName())
void pick_pop_name ();
/// converts the given color to index values on the stack (only used in color picking)
/// This can be compared to the results of the selection buffer results
std::vector<unsigned int> pick_color_to_stack (Vec4uc _rgba) const;
std::vector<size_t> pick_color_to_stack (Vec4uc _rgba) const;
/// returns the number of still available colors during color picking
unsigned int pick_free_indicies () const;
size_t pick_free_indicies () const;
/// Did an error occur during picking (only used in color picking)
/// Mostly to small color depth or wrong handling of pick_set_maximum
......@@ -1140,7 +1140,7 @@ public:
* of the object), not the name of the primitives/components in the object!
*
*/
unsigned int pick_current_index () const;
size_t pick_current_index () const;
/// Is color picking active?
bool color_picking () const;
......
......@@ -270,7 +270,7 @@ int glViewer::pickColor( ACG::SceneGraph::PickTarget _pickTarget,
rgba[2] = pixels[hit][2];
rgba[3] = pixels[hit][3];
std::vector<unsigned int> rv = properties_.glState().pick_color_to_stack (rgba);
std::vector<size_t> rv = properties_.glState().pick_color_to_stack (rgba);
// something wrong with the color stack ?
if (rv.size () < 2)
......@@ -394,7 +394,7 @@ int glViewer::pickFromCache( ACG::SceneGraph::PickTarget _pickTarget,
rgba[2] = pixels[hit][2];
rgba[3] = pixels[hit][3];
std::vector<unsigned int> rv = properties_.glState().pick_color_to_stack (rgba);
std::vector<size_t> rv = properties_.glState().pick_color_to_stack (rgba);
// something wrong with the color stack ?
if (rv.size () < 2)
......@@ -561,7 +561,7 @@ bool glViewer::pick_region( ACG::SceneGraph::PickTarget _pickTarg
rgba[2] = buffer[bPos + 2];
rgba[3] = buffer[bPos + 3];
std::vector<unsigned int> rv = properties_.glState().pick_color_to_stack (rgba);
std::vector<size_t> rv = properties_.glState().pick_color_to_stack (rgba);
if (rv.size () < 2)
continue;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment