Clean up fitness related parts
This commit is contained in:
parent
442a5f8434
commit
ac8354c686
3 changed files with 18 additions and 80 deletions
35
compare.glsl
35
compare.glsl
|
|
@ -1,11 +1,10 @@
|
||||||
// Compute the pixelwise diff between two textures, one row at a time.
|
// Compute the pixelwise diff between two textures, one row at a time.
|
||||||
// Assumes both images are not transparent.
|
// Assumes both images are not transparent.
|
||||||
// Warning: Only works on rows <= ((2**8)**4)/255 = 16843009
|
// Warning: Only works on rows <= 65793
|
||||||
|
// Formula for max pixels per row: (256^3)/255
|
||||||
|
|
||||||
// try isampler2D, ivec4 etc.? Probably not
|
|
||||||
uniform vec2 size;
|
uniform vec2 size;
|
||||||
uniform bool horizontal;
|
uniform bool horizontal;
|
||||||
// uniform vec2 offs;
|
|
||||||
uniform sampler2D base;
|
uniform sampler2D base;
|
||||||
uniform sampler2D curr;
|
uniform sampler2D curr;
|
||||||
|
|
||||||
|
|
@ -34,38 +33,10 @@ void main(void)
|
||||||
|
|
||||||
// encode to rgba channels in current pixel
|
// encode to rgba channels in current pixel
|
||||||
vec4 col = vec4(0.0, 0.0, 0.0, 1.0);
|
vec4 col = vec4(0.0, 0.0, 0.0, 1.0);
|
||||||
// total = total/255.0;
|
|
||||||
col.r = floor(mod(total*255.0, 256.0))/255.0;
|
col.r = floor(mod(total*255.0, 256.0))/255.0;
|
||||||
col.g = floor(mod(total*255.0/256.0, 256.0))/255.0;
|
col.g = floor(mod(total*255.0/256.0, 256.0))/255.0;
|
||||||
col.b = floor(mod(total*255.0/65536.0, 256.0))/255.0;
|
col.b = floor(mod(total*255.0/65536.0, 256.0))/255.0;
|
||||||
// color.g = 0.0;
|
|
||||||
// color.b = 0.0;
|
|
||||||
// color.g = mod(total, 256.0/255.0);
|
|
||||||
// color.r = float(mod(total, 256))/255.0;
|
|
||||||
// total = total/256;
|
|
||||||
// color.r = 1.0/255.0;
|
|
||||||
// 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);
|
|
||||||
// color.a = 1.0;
|
|
||||||
|
|
||||||
// color.r = 0;
|
|
||||||
|
|
||||||
gl_FragColor = col;
|
gl_FragColor = col;
|
||||||
// color.a = mod(total, 256.0/255.0);
|
|
||||||
|
|
||||||
// gl_FragColor = vec4(0.0, 1.0, 1.0, 1.0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
void main(void)
|
|
||||||
{
|
|
||||||
vec2 pos = gl_FragCoord.xy/size;
|
|
||||||
vec4 color = texture2D(curr, pos);
|
|
||||||
pos.y = 1.0 - pos.y;
|
|
||||||
vec4 basecolor = texture2D(base, pos);
|
|
||||||
color.rgb = abs(color.rgb - basecolor.rgb);
|
|
||||||
gl_FragColor = color;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
|
||||||
|
|
@ -2,28 +2,20 @@
|
||||||
|
|
||||||
#include <SFML/System.hpp>
|
#include <SFML/System.hpp>
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
Fitness::Fitness(sf::Texture target, float scale) :
|
|
||||||
|
Fitness::Fitness(sf::Texture target) :
|
||||||
dummy(sf::TriangleFan, 4)
|
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->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;
|
this->horizontal = targetSize.x >= targetSize.y;
|
||||||
// this->horizontal = true;
|
|
||||||
std::cout << "Horizontal mode: " << horizontal << " " << targetSize.x << "." << targetSize.y << std::endl;
|
|
||||||
if (this->horizontal) {
|
if (this->horizontal) {
|
||||||
targetSize.x = 1;
|
targetSize.x = 1;
|
||||||
} else {
|
} else {
|
||||||
targetSize.y = 1;
|
targetSize.y = 1;
|
||||||
// this->comp.create(targetSize.x*scale, 2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this->comp.create(targetSize.x, targetSize.y);
|
this->comp.create(targetSize.x, targetSize.y);
|
||||||
|
|
@ -51,59 +43,36 @@ bool Fitness::loadShader(std::string filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
unsigned long long Fitness::of(Chromosome chr)
|
unsigned long long Fitness::of(const Chromosome& chr)
|
||||||
{
|
{
|
||||||
// first, render a reduced, shaderized version to RenderTextures
|
// first, render chr to a texture, so we can just compare two textures
|
||||||
this->tex.clear();
|
this->tex.clear();
|
||||||
this->tex.draw(chr);
|
this->tex.draw(chr);
|
||||||
this->tex.display();
|
this->tex.display();
|
||||||
|
|
||||||
|
// compare the two textures using the SUPER ADVANCED shader
|
||||||
this->comp.clear(sf::Color::Blue);
|
this->comp.clear(sf::Color::Blue);
|
||||||
// this->comp.setView(this->view);
|
|
||||||
this->comp.draw(this->dummy, &this->compshdr);
|
this->comp.draw(this->dummy, &this->compshdr);
|
||||||
// this->comp.draw(this->sprite, &this->compshdr);
|
|
||||||
// this->comp.draw(this->sprite);
|
|
||||||
this->comp.display();
|
this->comp.display();
|
||||||
|
|
||||||
// then, download the result as an image and add the pixels
|
// then, download the result as an image and extract the result
|
||||||
sf::Image image = this->comp.getTexture().copyToImage();
|
sf::Image image = this->comp.getTexture().copyToImage();
|
||||||
sf::Vector2u size = image.getSize();
|
sf::Vector2u size = image.getSize();
|
||||||
// std::cout << "Image size: " << size.x << " " << size.y << std::endl;
|
|
||||||
unsigned long long fitness = 0;
|
|
||||||
// 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;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
unsigned long long fitness = 0;
|
||||||
if (this->horizontal) {
|
if (this->horizontal) {
|
||||||
for (unsigned int y=0; y<size.y; ++y) {
|
for (unsigned int y=0; y<size.y; ++y) {
|
||||||
sf::Color col = image.getPixel(0, y);
|
sf::Color col = image.getPixel(0, y);
|
||||||
// std::cout<<(int)col.r<<"|"<<(int)col.g<<"|"<<(int)col.b<<"|"<<(int)col.a<<std::endl;
|
fitness += (unsigned long long)col.r; // 2^(8*0) - first byte
|
||||||
fitness += ((unsigned long long)col.r); // 2^(8*0)
|
fitness += (unsigned long long)col.g*256; // 2^(8*1) - second byte
|
||||||
fitness += ((unsigned long long)col.g)*256; // 2^(8*1)
|
fitness += (unsigned long long)col.b*65536; // 2^(8*2) - third byte
|
||||||
fitness += ((unsigned long long)col.b)*65536; // 2^(8*2)
|
|
||||||
// fitness += (unsigned long long)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 {
|
} else {
|
||||||
for (unsigned int x=0; x<size.x; ++x) {
|
for (unsigned int x=0; x<size.x; ++x) {
|
||||||
sf::Color col = image.getPixel(x, 0);
|
sf::Color col = image.getPixel(x, 0);
|
||||||
// std::cout<<(int)col.r<<"|"<<(int)col.g<<"|"<<(int)col.b<<"|"<<(int)col.a<<std::endl;
|
fitness += (unsigned long long)col.r; // 2^(8*0) - first byte
|
||||||
fitness += (unsigned long long)col.r; // 2^(8*0)
|
fitness += (unsigned long long)col.g*256; // 2^(8*1) - second byte
|
||||||
fitness += ((unsigned long long)col.g)*256; // 2^(8*1)
|
fitness += (unsigned long long)col.b*65536; // 2^(8*2) - third byte
|
||||||
fitness += ((unsigned long long)col.b)*65536; // 2^(8*2)
|
|
||||||
// fitness += ((unsigned long int)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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,10 +8,10 @@
|
||||||
class Fitness
|
class Fitness
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Fitness(sf::Texture target, float scale=1);
|
Fitness(sf::Texture target);
|
||||||
bool loadShader(std::string filename);
|
bool loadShader(std::string filename);
|
||||||
|
|
||||||
unsigned long long of(Chromosome chr);
|
unsigned long long of(const Chromosome& chr);
|
||||||
|
|
||||||
sf::Texture target; // base image to compare against
|
sf::Texture target; // base image to compare against
|
||||||
sf::RenderTexture tex; // big RenderWindow containg the Chromosome to be evaluated
|
sf::RenderTexture tex; // big RenderWindow containg the Chromosome to be evaluated
|
||||||
|
|
@ -21,7 +21,5 @@ public:
|
||||||
bool horizontal;
|
bool horizontal;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// 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
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue