diff --git a/src/fgt/edit_quality/transferfunction.cpp b/src/fgt/edit_quality/transferfunction.cpp index 8cc920a6d..026a774b1 100644 --- a/src/fgt/edit_quality/transferfunction.cpp +++ b/src/fgt/edit_quality/transferfunction.cpp @@ -16,32 +16,104 @@ void TfChannel::setType(TF_CHANNELS type) TF_CHANNELS TfChannel::getType() { return _type; } -TF_KEY* TfChannel::addKey(float x, float y_up, float y_bot) -{ - TF_KEY key(x, y_up, y_bot); - return this->addKey(key); -} - +//adds to the keys list new_key +//returns a pointer to the key just added TF_KEY* TfChannel::addKey(TF_KEY &new_key) { + //inserting at the head of the list KEYS.insert(KEYS.begin(), new_key); + return &(*KEYS.begin()); } -TF_KEY* TfChannel::removeKey(float x) +//adds to the keys list a new keys with fields passed to the method +//returns a pointer to the key just added +TF_KEY* TfChannel::addKey(float x, float y_up, float y_bot) +{ + //building key + TF_KEY key(x, y_up, y_bot); + + //adding it to list + return this->addKey(key); +} + +//removes from keys list to_remove_key +//returns the x value of the removed key or -1 if key was not found +float TfChannel::removeKey(TF_KEY& to_remove_key) +{ + //not found by default + float result = -1.0f; + + //searching key + KEY_LISTiterator it = find(KEYS.begin(), KEYS.end(), to_remove_key); + + //if found, deleting it from list + if ( it != KEYS.end() ) + { + result = (*it).x; + KEYS.erase(it); + } + + return result; +} + +//removes from keys list the key whose x value is x_val +//returns the x value of the removed key or -1 if key was not found +float TfChannel::removeKey(float x_val) { //searching key with proper x - TF_KEY to_find_key(x); - KEY_LISTiterator it = find(KEYS.begin(), KEYS.end(), 5); - KEYS.erase(it); - return 0; + TF_KEY to_find_key(x_val); + + return this->removeKey(to_find_key); } +//merges two keys together by copying opportunely y values of the keys in just one key +//returns a pointer to the "merged key" +TF_KEY *TfChannel::mergeKeys(TF_KEY key1, TF_KEY key2) +{ + KEY_LISTiterator it1 = std::find(KEYS.begin(), KEYS.end(), key1); + KEY_LISTiterator it2 = find(KEYS.begin(), KEYS.end(), key2); + + //key1 or key2 not found! + assert(( it1 == KEYS.end() ) || ( it2 == KEYS.end() )); + + //at least one of two y_lower should be 0 + if ( ( (*it1).y_lower * key2.y_lower) == 0 ) + if ( (*it1).y_lower == 0 ) + (*it1).y_lower = key2.y_lower; + + //at least one of two y_upper should be 0 + if ( ( (*it1).y_upper * key2.y_upper) == 0 ) + if ( (*it1).y_upper == 0 ) + (*it1).y_upper = key2.y_upper; + + //merge done, deleting key2 + KEYS.erase(it2); + + return &(*it1); +} + +//merges two keys together by copying opportunely y values of the keys in just one key +//returns a pointer to the "merged key" TF_KEY* TfChannel::mergeKeys(float x1, float x2) { - return 0; + TF_KEY key1(x1); + TF_KEY key2(x2); + + //calling merge keys on TF_KEY objects + return this->mergeKeys(key1, key2); } +float TfChannel::getChannelValuef(float x_position) +{ + return x_position; +} + + +UINT8 TfChannel::getChannelValueb(float x_position) +{ + return (UINT8)(this->getChannelValuef(x_position) * 255.0f); +} @@ -58,9 +130,28 @@ TransferFunction::TransferFunction(void) _channels[i].setType((TF_CHANNELS)i); _channels_order[i] = i; } + + //resetting color band value memset(_color_band,0,sizeof(_color_band)); } TransferFunction::~TransferFunction(void) { } + +//returns a relative-absolute x value conversion rounded to closer integer value +int TransferFunction::relative2AbsoluteVal(float relative_val, float max_val) +{ return (int)((relative_val * max_val)+0.5f); } + +//returns an absolute-relative x value conversion +float TransferFunction::absolute2RelativeVal(int absolute_val, float max_val) +{ return (float)absolute_val / max_val; } + + +void TransferFunction::buildColorBand() +{ + for (int i=0; iabsolute2RelativeVal(i)), + _channels[GREEN_CHANNEL].getChannelValueb(this->absolute2RelativeVal(i)), + _channels[BLUE_CHANNEL].getChannelValueb(this->absolute2RelativeVal(i)) ); +} \ No newline at end of file diff --git a/src/fgt/edit_quality/transferfunction.h b/src/fgt/edit_quality/transferfunction.h index 65c037eff..9ecca5550 100644 --- a/src/fgt/edit_quality/transferfunction.h +++ b/src/fgt/edit_quality/transferfunction.h @@ -24,11 +24,15 @@ #define _TRANSFER_FUNCTION_H_ #include +#include #include #include #include +#include "const_types.h" + using namespace std; +using namespace vcg; @@ -58,9 +62,9 @@ typedef vector::iterator KEY_LISTiterator; //list of channels enum TF_CHANNELS { - RED = 0, - GREEN, - BLUE, + RED_CHANNEL = 0, + GREEN_CHANNEL, + BLUE_CHANNEL, NUMBER_OF_CHANNELS }; @@ -82,15 +86,18 @@ public: void setType(TF_CHANNELS); TF_CHANNELS getType(); TF_KEY *addKey(float x, float y_up, float y_bot); - TF_KEY *addKey(TF_KEY &new_key); - TF_KEY *removeKey(float x); + TF_KEY *addKey(TF_KEY& new_key); + float removeKey(float x); + float removeKey(TF_KEY& to_remove_key); TF_KEY *mergeKeys(float x1, float x2); + TF_KEY *mergeKeys(TF_KEY key1, TF_KEY key2); + + float getChannelValuef(float x_position); + UINT8 getChannelValueb(float x_position); }; -#define COLOR_BAND_SIZE 1024 - //Representation of a transfer function as a triple of vectors of Keys, //one for each color (RGB) @@ -99,13 +106,16 @@ class TransferFunction private: TfChannel _channels[NUMBER_OF_CHANNELS]; //set of channels int _channels_order[NUMBER_OF_CHANNELS]; //array used to carry out virtual pivoting indexing - int _color_band[COLOR_BAND_SIZE]; + Color4f _color_band[COLOR_BAND_SIZE]; /*rendere color band una classe a se stante??*/ + + int relative2AbsoluteVal(float relative_val, float max_val=COLOR_BAND_SIZE); + float absolute2RelativeVal(int absolute_val, float max_val=COLOR_BAND_SIZE); public: TransferFunction(void); ~TransferFunction(void); - void makeColorBand(); + void buildColorBand(); }; #endif \ No newline at end of file