- bugfix and additional code (for now) to track duplicates and problems in color picking. We now use will's implementation as it supports 64K colors.

This commit is contained in:
SoftCoder 2013-11-30 17:20:30 -08:00
parent b36244fcbd
commit f7bc5e8f92
3 changed files with 46 additions and 33 deletions

View File

@ -261,10 +261,12 @@ class BaseColorPickEntity {
public:
BaseColorPickEntity();
virtual ~BaseColorPickEntity() {}
virtual ~BaseColorPickEntity() {
recycleUniqueColor();
}
//static const int COLOR_COMPONENTS = 4;
static const int COLOR_COMPONENTS = 3;
//static const int COLOR_COMPONENTS = 3;
static const int COLOR_COMPONENTS = 4;
static void init(int bufferSize);
static void beginPicking();
static void endPicking();
@ -284,17 +286,14 @@ public:
unsigned char * getUniqueColorID() { return &uniqueColorID[0]; }
bool get_next_assign_color(unsigned char *assign_to);
protected:
void recycleUniqueColor();
private:
unsigned char uniqueColorID[COLOR_COMPONENTS];
static unsigned char nextColorID[COLOR_COMPONENTS];
static int nextColorRGB;
static const int k, p;
static Mutex mutexNextColorID;
static unsigned int nextColorRGB;
static const unsigned int k, p;
//static Mutex mutexNextColorID;
static bool using_loop_method;
@ -309,6 +308,8 @@ private:
void assign_color_using_prime(unsigned char *assign_to);
void assign_color_using_loop(unsigned char *assign_to);
void recycleUniqueColor();
};
}}//end namespace

View File

@ -1826,11 +1826,14 @@ PixelBufferWrapper::~PixelBufferWrapper() {
cleanup();
}
const int BaseColorPickEntity::p = 64007;
const int BaseColorPickEntity::k = 43067;
int BaseColorPickEntity::nextColorRGB = BaseColorPickEntity::k;
Mutex BaseColorPickEntity::mutexNextColorID;
unsigned char BaseColorPickEntity::nextColorID[COLOR_COMPONENTS] = { 1, 1, 1 };
// ---------------------------------------------------------------------------
const unsigned int BaseColorPickEntity::p = 64007;
const unsigned int BaseColorPickEntity::k = 43067;
unsigned int BaseColorPickEntity::nextColorRGB = BaseColorPickEntity::k;
//Mutex BaseColorPickEntity::mutexNextColorID;
unsigned char BaseColorPickEntity::nextColorID[COLOR_COMPONENTS] = { 1, 1, 1, 0 };
auto_ptr<PixelBufferWrapper> BaseColorPickEntity::pbo;
map<string,bool> BaseColorPickEntity::usedColorIDList;
@ -1838,14 +1841,18 @@ bool BaseColorPickEntity::trackColorUse = true;
vector<vector<unsigned char> > BaseColorPickEntity::nextColorIDReuseList;
bool BaseColorPickEntity::using_loop_method = true;
bool BaseColorPickEntity::using_loop_method = false;
BaseColorPickEntity::BaseColorPickEntity() {
assign_color();
uniqueColorID[0] = 0;
uniqueColorID[1] = 0;
uniqueColorID[2] = 0;
uniqueColorID[3] = 0;
assign_color();
}
bool BaseColorPickEntity::get_next_assign_color(unsigned char *assign_to) {
MutexSafeWrapper safeMutex(&mutexNextColorID);
//MutexSafeWrapper safeMutex(&mutexNextColorID);
if(assign_to == NULL) {
throw megaglest_runtime_error("assign_to == NULL");
@ -1865,6 +1872,8 @@ bool BaseColorPickEntity::get_next_assign_color(unsigned char *assign_to) {
if(usedColorIDList.find(color_key) == usedColorIDList.end()) {
usedColorIDList[color_key] = true;
//printf("Color added to used list [%s] usedColorIDList = %d nextColorIDReuseList = %d!\n",color_key.c_str(),(int)usedColorIDList.size(),(int)nextColorIDReuseList.size());
}
else {
isDuplicate = true;
@ -1884,11 +1893,7 @@ void BaseColorPickEntity::assign_color_using_prime(unsigned char *assign_to) {
// nextColorID is a 16-bit (hi)colour (for players with 16-bit display depths)
// we expand it to true-color for use with OpenGL
// const int
// r = (nextColorRGB >> 11) & ((1<<6)-1),
// b = (nextColorRGB >> 5) & ((1<<7)-1),
// g = nextColorRGB & ((1<<6)-1);
const int
const unsigned int
r = (nextColorRGB >> 11) & ((1<<5)-1),
g = (nextColorRGB >> 5) & ((1<<6)-1),
b = nextColorRGB & ((1<<5)-1);
@ -1900,6 +1905,8 @@ void BaseColorPickEntity::assign_color_using_prime(unsigned char *assign_to) {
void BaseColorPickEntity::assign_color_using_loop(unsigned char *assign_to) {
if(nextColorIDReuseList.empty() == false) {
//printf("Color being reused [%u.%u.%u] usedColorIDList = %d nextColorIDReuseList = %d!\n",nextColorIDReuseList.back()[0],nextColorIDReuseList.back()[1],nextColorIDReuseList.back()[2],(int)usedColorIDList.size(),(int)nextColorIDReuseList.size());
assign_to[0] = nextColorIDReuseList.back()[0];
assign_to[1] = nextColorIDReuseList.back()[1];
assign_to[2] = nextColorIDReuseList.back()[2];
@ -1908,7 +1915,8 @@ void BaseColorPickEntity::assign_color_using_loop(unsigned char *assign_to) {
string color_key = getColorDescription();
if(usedColorIDList.find(color_key) == usedColorIDList.end()) {
usedColorIDList[color_key] = true;
//usedColorIDList[color_key] = true;
//printf("Color added to used list [%s] usedColorIDList = %d nextColorIDReuseList = %d!\n",color_key.c_str(),(int)usedColorIDList.size(),(int)nextColorIDReuseList.size());
}
else {
printf("Line ref: %d *WARNING* color [%s] ALREADY FOUND in history list!\n",__LINE__,color_key.c_str());
@ -1937,11 +1945,11 @@ void BaseColorPickEntity::assign_color_using_loop(unsigned char *assign_to) {
}
else {
//printf("Color rolled over on 3rd level!\n");
printf("Color rolled over on 3rd level usedColorIDList = %d!\n",(int)usedColorIDList.size());
nextColorID[0] = 1;
nextColorID[1] = 1;
nextColorID[2] = 1;
nextColorID[0] = 1;
nextColorID[1] = 1;
nextColorID[2] = 1;
// nextColorID[2] = 1;
@ -1960,7 +1968,7 @@ void BaseColorPickEntity::assign_color_using_loop(unsigned char *assign_to) {
}
void BaseColorPickEntity::recycleUniqueColor() {
MutexSafeWrapper safeMutex(&mutexNextColorID);
//MutexSafeWrapper safeMutex(&mutexNextColorID);
vector<unsigned char> reUseColor;
reUseColor.push_back(uniqueColorID[0]);
@ -1968,10 +1976,14 @@ void BaseColorPickEntity::recycleUniqueColor() {
reUseColor.push_back(uniqueColorID[2]);
nextColorIDReuseList.push_back(reUseColor);
if(usedColorIDList.size() > 0) {
//printf("RECYCLE Color [%u.%u.%u] usedColorIDList = %d nextColorIDReuseList = %d!\n",reUseColor[0],reUseColor[1],reUseColor[2],(int)usedColorIDList.size(),(int)nextColorIDReuseList.size());
if(usedColorIDList.empty() == false) {
string color_key = getColorDescription();
if(usedColorIDList.find(color_key) != usedColorIDList.end()) {
usedColorIDList.erase(color_key);
//printf("REMOVING used Color [%s] usedColorIDList = %d nextColorIDReuseList = %d!\n",color_key.c_str(),(int)usedColorIDList.size(),(int)nextColorIDReuseList.size());
}
else {
printf("Line ref: %d *WARNING* color [%s] used count: %d NOT FOUND in history list!\n",__LINE__,color_key.c_str(),(int)usedColorIDList.size());
@ -1980,7 +1992,7 @@ void BaseColorPickEntity::recycleUniqueColor() {
}
void BaseColorPickEntity::resetUniqueColors() {
MutexSafeWrapper safeMutex(&mutexNextColorID);
//MutexSafeWrapper safeMutex(&mutexNextColorID);
BaseColorPickEntity::nextColorRGB = BaseColorPickEntity::k;

View File

@ -80,7 +80,7 @@ public:
TestBaseColorPickEntity colorPicker;
// This is the max color count this algorithm supports
const int MAX_SUPPORTED_COLORS_USING_THIS_METHOD = 472;
const int MAX_SUPPORTED_COLORS_USING_THIS_METHOD = 64005;
for(unsigned int i = 0; i < MAX_SUPPORTED_COLORS_USING_THIS_METHOD; ++i) {
bool duplicate = colorPicker.get_next_assign_color(colorPicker.getUniqueColorID());
CPPUNIT_ASSERT_EQUAL( false,duplicate );
@ -90,7 +90,7 @@ public:
TestBaseColorPickEntity colorPicker2;
// This is a test to prove when the algorithm fails
const int MAX_SUPPORTED_COLORS_USING_THIS_METHOD_FAIL = 473;
const int MAX_SUPPORTED_COLORS_USING_THIS_METHOD_FAIL = 64006;
for(unsigned int i = 0; i < MAX_SUPPORTED_COLORS_USING_THIS_METHOD_FAIL; ++i) {
bool duplicate = colorPicker2.get_next_assign_color(colorPicker2.getUniqueColorID());
CPPUNIT_ASSERT_EQUAL( (i+1 >= MAX_SUPPORTED_COLORS_USING_THIS_METHOD_FAIL),duplicate );