Attempt to implement faster comparison algorithm
This commit is contained in:
parent
6cfb8d93a2
commit
a04e58c7ca
3 changed files with 98 additions and 13 deletions
40
compare.glsl
40
compare.glsl
|
|
@ -1,13 +1,48 @@
|
||||||
// Compute the pixelwise diff between two textures.
|
// Compute the pixelwise diff between two textures, one row at a time.
|
||||||
// Computes the diff for each color chanel separately (r, g, b).
|
|
||||||
// Assumes both images are not transparent.
|
// Assumes both images are not transparent.
|
||||||
|
// Warning: Only works on rows <= ((2**8)**4)/255 = 16843009
|
||||||
|
|
||||||
// try isampler2D, ivec4 etc.? Probably not
|
// try isampler2D, ivec4 etc.? Probably not
|
||||||
uniform vec2 size;
|
uniform vec2 size;
|
||||||
|
uniform bool horizontal;
|
||||||
// uniform vec2 offs;
|
// uniform vec2 offs;
|
||||||
uniform sampler2D base;
|
uniform sampler2D base;
|
||||||
uniform sampler2D curr;
|
uniform sampler2D curr;
|
||||||
|
|
||||||
|
void main(void)
|
||||||
|
{
|
||||||
|
// calculate total diff of row in either direction
|
||||||
|
float total = 0.0;
|
||||||
|
|
||||||
|
if (horizontal) {
|
||||||
|
float posy = gl_FragCoord.y/size.y;
|
||||||
|
for (float x=0.0; x<size.x; x+=1.0) {
|
||||||
|
vec4 color = texture2D(curr, vec2(x, posy));
|
||||||
|
vec4 basecolor = texture2D(base, vec2(x, 1.0-posy));
|
||||||
|
color.rgb = abs(color.rgb - basecolor.rgb);
|
||||||
|
total = total + color.r + color.g + color.b;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
float posx = gl_FragCoord.x/size.x;
|
||||||
|
for (float y=0.0; y<size.y; y+=1.0) {
|
||||||
|
vec4 color = texture2D(curr, vec2(posx, y));
|
||||||
|
vec4 basecolor = texture2D(base, vec2(posx, 1.0-y));
|
||||||
|
color.rgb = abs(color.rgb - basecolor.rgb);
|
||||||
|
total = total + color.r + color.g + color.b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// encode to rgba channels in current pixel
|
||||||
|
vec4 color;
|
||||||
|
color.r = mod(total, 256.0/255.0); total = (total - color.r) / (256.0/255.0);
|
||||||
|
color.g = mod(total, 256.0/255.0); total = (total - color.g) / (256.0/255.0);
|
||||||
|
color.b = mod(total, 256.0/255.0); total = (total - color.b) / (256.0/255.0);
|
||||||
|
color.a = mod(total, 256.0/255.0);
|
||||||
|
|
||||||
|
gl_FragColor = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
void main(void)
|
void main(void)
|
||||||
{
|
{
|
||||||
vec2 pos = gl_FragCoord.xy/size;
|
vec2 pos = gl_FragCoord.xy/size;
|
||||||
|
|
@ -17,3 +52,4 @@ void main(void)
|
||||||
color.rgb = abs(color.rgb - basecolor.rgb);
|
color.rgb = abs(color.rgb - basecolor.rgb);
|
||||||
gl_FragColor = color;
|
gl_FragColor = color;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
|
||||||
|
|
@ -4,18 +4,33 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Fitness::Fitness(sf::Texture target, float scale)
|
Fitness::Fitness(sf::Texture target, float scale) :
|
||||||
|
dummy(sf::TriangleFan, 4)
|
||||||
{
|
{
|
||||||
this->target = target;
|
this->target = target;
|
||||||
|
|
||||||
sf::Vector2u targetSize = this->target.getSize();
|
sf::Vector2u targetSize = this->target.getSize();
|
||||||
|
|
||||||
this->tex.create(targetSize.x, targetSize.y);
|
this->tex.create(targetSize.x, targetSize.y);
|
||||||
this->sprite.setTexture(this->tex.getTexture());
|
|
||||||
this->comp.create(targetSize.x*scale, targetSize.y*scale);
|
// this->view.reset(sf::FloatRect(0, 0, targetSize.x, targetSize.y));
|
||||||
|
// this->view.setViewport(sf::FloatRect(0, 0, 1, 1));
|
||||||
|
|
||||||
|
this->horizontal = targetSize.x > targetSize.y;
|
||||||
|
if (this->horizontal) {
|
||||||
|
targetSize.x = 1;
|
||||||
|
} else {
|
||||||
|
targetSize.y = 1;
|
||||||
|
// this->comp.create(targetSize.x*scale, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
this->comp.create(targetSize.x, targetSize.y);
|
||||||
// this->comp.setSmooth(true);
|
// this->comp.setSmooth(true);
|
||||||
|
|
||||||
this->view.reset(sf::FloatRect(0, 0, targetSize.x, targetSize.y));
|
this->dummy[0] = sf::Vertex(sf::Vector2f(0, 0), sf::Color::Red );
|
||||||
this->view.setViewport(sf::FloatRect(0, 0, 1, 1));
|
this->dummy[1] = sf::Vertex(sf::Vector2f(targetSize.x, 0), sf::Color::Yellow);
|
||||||
|
this->dummy[2] = sf::Vertex(sf::Vector2f(targetSize), sf::Color::Green );
|
||||||
|
this->dummy[3] = sf::Vertex(sf::Vector2f(0, targetSize.y), sf::Color::Blue );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -26,6 +41,7 @@ bool Fitness::loadShader(std::string filename)
|
||||||
this->compshdr.setUniform("base", this->target);
|
this->compshdr.setUniform("base", this->target);
|
||||||
this->compshdr.setUniform("curr", this->tex.getTexture());
|
this->compshdr.setUniform("curr", this->tex.getTexture());
|
||||||
this->compshdr.setUniform("size", sf::Vector2f(this->comp.getSize()));
|
this->compshdr.setUniform("size", sf::Vector2f(this->comp.getSize()));
|
||||||
|
this->compshdr.setUniform("horizontal", this->horizontal);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -41,8 +57,9 @@ unsigned long long Fitness::of(Chromosome chr)
|
||||||
this->tex.display();
|
this->tex.display();
|
||||||
|
|
||||||
this->comp.clear(sf::Color::Blue);
|
this->comp.clear(sf::Color::Blue);
|
||||||
this->comp.setView(this->view);
|
// this->comp.setView(this->view);
|
||||||
this->comp.draw(this->sprite, &this->compshdr);
|
this->comp.draw(this->dummy, &this->compshdr);
|
||||||
|
// this->comp.draw(this->sprite, &this->compshdr);
|
||||||
// this->comp.draw(this->sprite);
|
// this->comp.draw(this->sprite);
|
||||||
this->comp.display();
|
this->comp.display();
|
||||||
|
|
||||||
|
|
@ -50,11 +67,40 @@ unsigned long long Fitness::of(Chromosome chr)
|
||||||
sf::Image image = this->comp.getTexture().copyToImage();
|
sf::Image image = this->comp.getTexture().copyToImage();
|
||||||
sf::Vector2u size = image.getSize();
|
sf::Vector2u size = image.getSize();
|
||||||
unsigned long long fitness = 0;
|
unsigned long long fitness = 0;
|
||||||
for (unsigned int x=0; x<size.x; ++x) {
|
// for (unsigned int x=0; x<size.x; ++x) {
|
||||||
|
// for (unsigned int y=0; y<size.y; ++y) {
|
||||||
|
// sf::Color color = image.getPixel(x, y);
|
||||||
|
// fitness += color.r + color.g + color.b;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (this->horizontal) {
|
||||||
for (unsigned int y=0; y<size.y; ++y) {
|
for (unsigned int y=0; y<size.y; ++y) {
|
||||||
sf::Color color = image.getPixel(x, y);
|
sf::Color col = image.getPixel(0, y);
|
||||||
fitness += color.r + color.g + color.b;
|
fitness += col.r; // 2^(8*0)
|
||||||
|
fitness += col.g*256; // 2^(8*1)
|
||||||
|
fitness += col.b*65536; // 2^(8*2)
|
||||||
|
fitness += col.a*16777216; // 2^(8*3)
|
||||||
|
// col = image.getPixel(1, y);
|
||||||
|
// fitness += col.r*4294967296; // 2^(8*4)
|
||||||
|
// fitness += col.g*1099511627776; // 2^(8*5)
|
||||||
|
// fitness += col.b*281474976710656; // 2^(8*6)
|
||||||
|
// fitness += col.a*72057594037927936; // 2^(8*7)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (unsigned int x=0; x<size.x; ++x) {
|
||||||
|
sf::Color col = image.getPixel(x, 0);
|
||||||
|
fitness += col.r; // 2^(8*0)
|
||||||
|
fitness += col.g*256; // 2^(8*1)
|
||||||
|
fitness += col.b*65536; // 2^(8*2)
|
||||||
|
fitness += col.a*16777216; // 2^(8*3)
|
||||||
|
// col = image.getPixel(x, 1);
|
||||||
|
// fitness += col.r*4294967296; // 2^(8*4)
|
||||||
|
// fitness += col.g*1099511627776; // 2^(8*5)
|
||||||
|
// fitness += col.b*281474976710656; // 2^(8*6)
|
||||||
|
// fitness += col.a*72057594037927936; // 2^(8*7)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return fitness;
|
return fitness;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,11 @@ public:
|
||||||
sf::RenderTexture tex; // big RenderWindow containg the Chromosome to be evaluated
|
sf::RenderTexture tex; // big RenderWindow containg the Chromosome to be evaluated
|
||||||
sf::RenderTexture comp; // smaller RenderWindow which is downloaded as Image
|
sf::RenderTexture comp; // smaller RenderWindow which is downloaded as Image
|
||||||
|
|
||||||
|
sf::VertexArray dummy;
|
||||||
|
bool horizontal;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
sf::Sprite sprite; // sprite to render tex to comp
|
// sf::Sprite sprite; // sprite to render tex to comp
|
||||||
sf::Shader compshdr; // shader to perform pixel-wise image diff with
|
sf::Shader compshdr; // shader to perform pixel-wise image diff with
|
||||||
sf::View view; // reduces tex to comp size while drawing
|
sf::View view; // reduces tex to comp size while drawing
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue