Jump to content

  • Log In with Google      Sign In   
  • Create Account


taby

Member Since 10 Feb 2005
Offline Last Active Sep 06 2014 01:16 PM

Topics I've Started

Four pillars of object oriented programming

09 May 2014 - 10:32 AM

What are the four pillars of object oriented programming, with regards to C++? I've googled and googled this subject, and there are different answers depending on which site you visit. What is your impression as to what the four pillars are? I've given my impression below, in the form of code snippets. Are these snippets the correct way to think of the four pillars?

 

Simple class / object:

#include <iostream>
using std::cout;
using std::endl;


class A
{
public:
    A(void)
    {
        value1 = 123;
    }

    int value1;
};


int main(void)
{
    A a;
    cout << a.value1 << endl;

    return 0;
}

Pillar 1 encapsulation:

#include <iostream>
using std::cout;
using std::endl;


class A
{
public:
    A(void)
    {
        value1 = 123;
    }

// Encapsulating the member variable by making it protected from code
// that exists outside of the class    
protected:
    int value1;

public:
    int getvalue1(void)
    {
        return value1;
    }

    void setvalue1(const int value)
    {
        value1 = value;
    }
};


int main(void)
{
    A a;
    cout << a.getvalue1() << endl;

    return 0;
}

Pillar 2 inheritence:

#include <iostream>

using std::cout;
using std::endl;


// Base class
class A
{
public:
    A(void)
    {
        value1 = 123;
    }

protected:
    int value1;

public:
    int getvalue1(void)
    {
        return value1;
    }

    void setvalue1(const int value)
    {
        value1 = value;
    }
};


// Inheriting class
class B: public A
{
public:
    B(void)
    {
        value2 = 456;
    }

protected:
    int value2;

public:
    int getvalue2(void)
    {
        return value2;
    }

    void setvalue2(const int value)
    {
        value2 = value;
    }
};


int main(void)
{
    B b;
    cout << b.getvalue1() << ' ' << b.getvalue2() << endl;

    return 0;
}

Pillar 3 abstraction (abstract class):

#include <iostream>
using std::cout;
using std::endl;


// Abstract base class (cannot be instantiated due to pure virtual member function)
class A
{
public:
    virtual void talk(void) = 0;
};

// Inheriting class must implement pure virtual function talk()
class B: public A 
{
public:
    void talk(void)
    {
        cout << "Class B" << endl;
    }
};


int main(void)
{
    B b;
    b.talk();

    return 0;
}

Pillar 4 polymorphism:

#include <iostream>
using std::cout;
using std::endl;


// Abstract base class (cannot be instantiated due to pure virtual member function)
class A
{
public:
    virtual void talk(void) = 0;
};

// Inheriting class must implement pure virtual function
class B: public A 
{
public:
    void talk(void)
    {
        cout << "Class B" << endl;
    }
};

// Inheriting class must implement pure virtual function
class C: public A 
{
public:
    void talk(void)
    {
        cout << "Class C" << endl;
    }
};


int main(void)
{
    B b;
    b.talk();

    C c;
    c.talk();

    // Also see general function overloading as an example of polymorphism
    // ie:
    // int add(int a, int b) { return a + b }
    // float add(float a, float b) { return a + b }
    //
    // Also see templates
    // ie:
    // STL containers

    return 0;
}

A Song of Ice and Fire -- per character chapter lists

09 January 2014 - 04:40 PM

The A Song of Ice and Fire by GRR Martin chapter data were obtained from Apple iBooks version.

 

The following per character chapter lists allow one to read a particular character's story from start to end (for instance, Arya, Jaime, etc).

The per character chapter lists were obtained from custom software (https://sites.google.com/site/asoiafchapters/asoiaf_per_character_chapter_list.zip).

The per character chapter lists contain spoilers, and aren't meant for use until after the A Song of Ice and Fire series has been read through at least once.

The per character chapter lists are in the public domain (free to use, modify and distribute).
 


==Per Character Chapter Lists==

Aeron in The Prophet:
A Feast for Crows Ch 1.

Aeron in The Drowned Man:
A Feast for Crows Ch 19.

Alayne:
A Feast for Crows Ch 23, Ch 41.

Areo in The Captain of Guards:
A Feast for Crows Ch 2.

Areo as The Watcher:
A Dance with Dragons Ch 38.

Arianne in The Queenmaker:
A Feast for Crows Ch 21.

Arianne in The Princess in the Tower:
A Feast for Crows Ch 40.

Arya:
A Game of Thrones Ch 7, Ch 22, Ch 32, Ch 50, Ch 65,
A Clash of Kings Ch 1, Ch 5, Ch 9, Ch 14, Ch 19, Ch 26, Ch 30, Ch 38, Ch 47, Ch 64,
A Storm of Swords Ch 3, Ch 13, Ch 17, Ch 22, Ch 29, Ch 34, Ch 39, Ch 43, Ch 47, Ch 50, Ch 52, Ch 65, Ch 74,
A Feast for Crows Ch 6, Ch 22.

Arya in Cat of the Canals:
A Feast for Crows Ch 34.

Arya in The Blind Girl:
A Dance with Dragons Ch 45.

Arya in The Ugly Little Girl:
A Dance with Dragons Ch 64.

Arys in The Soiled Knight:
A Feast for Crows Ch 13.

Asha in The Kraken's Daughter:
A Feast for Crows Ch 11.

Asha in The Wayward Bride:
A Dance with Dragons Ch 26.

Asha in The King's Prize:
A Dance with Dragons Ch 42.

Bran:
A Game of Thrones Ch 1, Ch 8, Ch 17, Ch 24, Ch 37, Ch 53, Ch 66,
A Clash of Kings Ch 4, Ch 16, Ch 21, Ch 28, Ch 35, Ch 46, Ch 69,
A Storm of Swords Ch 9, Ch 24, Ch 40, Ch 56,
A Dance with Dragons Ch 4, Ch 13, Ch 34.

Brienne:
A Feast for Crows Ch 4, Ch 9, Ch 14, Ch 20, Ch 25, Ch 31, Ch 37, Ch 42.

Catelyn:
A Game of Thrones Ch 2, Ch 6, Ch 14, Ch 18, Ch 28, Ch 34, Ch 40, Ch 55, Ch 59, Ch 63, Ch 71,
A Clash of Kings Ch 7, Ch 22, Ch 31, Ch 33, Ch 39, Ch 45, Ch 55,
A Storm of Swords Ch 2, Ch 14, Ch 20, Ch 35, Ch 45, Ch 49, Ch 51, Epilogue.

Cersei:
A Feast for Crows Ch 3, Ch 7, Ch 12, Ch 17, Ch 24, Ch 28, Ch 32, Ch 36, Ch 39, Ch 43,
A Dance with Dragons Ch 54, Ch 65.

Daenerys:
A Game of Thrones Ch 3, Ch 11, Ch 23, Ch 36, Ch 46, Ch 54, Ch 61, Ch 64, Ch 68, Ch 72,
A Clash of Kings Ch 12, Ch 27, Ch 40, Ch 48, Ch 63,
A Storm of Swords Ch 8, Ch 23, Ch 27, Ch 42, Ch 57, Ch 71,
A Dance with Dragons Ch 2, Ch 11, Ch 16, Ch 23, Ch 30, Ch 36, Ch 43, Ch 50, Ch 52, Ch 71.

Davos:
A Clash of Kings Ch 10, Ch 42, Ch 58,
A Storm of Swords Ch 5, Ch 10, Ch 25, Ch 36, Ch 54, Ch 63,
A Dance with Dragons Ch 9, Ch 15, Ch 19, Ch 29.

Eddard:
A Game of Thrones Ch 4, Ch 12, Ch 16, Ch 20, Ch 25, Ch 27, Ch 30, Ch 33, Ch 35, Ch 39, Ch 43, Ch 45, Ch 47, Ch 49, Ch 58.

Griff in The Lost Lord:
A Dance with Dragons Ch 24.

Jaime:
A Storm of Swords Ch 1, Ch 11, Ch 21, Ch 31, Ch 37, Ch 44, Ch 62, Ch 67, Ch 72,
A Feast for Crows Ch 8, Ch 16, Ch 27, Ch 30, Ch 33, Ch 38, Ch 44,
A Dance with Dragons Ch 48.

Jaime and Brienne combined:
A Storm of Swords Ch 1, Ch 11, Ch 21, Ch 31, Ch 37, Ch 44, Ch 62, Ch 67, Ch 72,
A Feast for Crows Ch 4, Ch 8, Ch 9, Ch 14, Ch 16, Ch 20, Ch 25, Ch 27, Ch 30, Ch 31, Ch 33, Ch 37, Ch 38, Ch 42 Ch 44,
A Dance with Dragons Ch 48.

Jon:
A Game of Thrones Ch 5, Ch 10, Ch 19, Ch 26, Ch 41, Ch 48, Ch 52, Ch 60, Ch 70,
A Clash of Kings Ch 6, Ch 13, Ch 23, Ch 34, Ch 43, Ch 51, Ch 53, Ch 68,
A Storm of Swords Ch 7, Ch 15, Ch 26, Ch 30, Ch 41, Ch 48, Ch 55, Ch 64, Ch 69, Ch 73, Ch 76, Ch 79,
A Dance with Dragons Ch 3, Ch 7, Ch 10, Ch 17, Ch 21, Ch 28, Ch 35, Ch 39, Ch 44, Ch 49, Ch 53, Ch 58, Ch 69.

Jon Connington in The Griffin Reborn:
A Dance with Dragons Ch 61.

Meanwhile, back on the Wall...:
A Feast for Crows Ch 46.

Melisandre:
A Clash of Kings Prologue,
A Dance with Dragons Ch 31.

Others:
A Game of Thrones Prologue,
A Storm of Swords Prologue,
A Dance with Dragons Prologue.

Pate:
A Feast for Crows Prologue.

Quentyn in The Merchant's Man:
A Dance with Dragons Ch 6.

Quentyn in The Windblown:
A Dance with Dragons Ch 25.

Quentyn in The Spurned Suitor:
A Dance with Dragons Ch 60.

Quentyn in The Dragontamer:
A Dance with Dragons Ch 68.

Samwell:
A Storm of Swords Ch 18, Ch 33, Ch 46, Ch 75, Ch 78,
A Feast for Crows Ch 5, Ch 15, Ch 26, Ch 35, Ch 45.

Sansa:
A Game of Thrones Ch 15, Ch 29, Ch 44, Ch 51, Ch 57, Ch 67,
A Clash of Kings Ch 2, Ch 18, Ch 32, Ch 52, Ch 57, Ch 60, Ch 62, Ch 65,
A Storm of Swords Ch 6, Ch 16, Ch 28, Ch 59, Ch 61, Ch 68, Ch 80,
A Feast for Crows Ch 10.

Barristan in The Queensguard:
A Dance with Dragons Ch 55.

Barristan in The Discarded Knight:
A Dance with Dragons Ch 59.

Barristan in The Kingbreaker:
A Dance with Dragons Ch 67.

Barristan in The Queen's Hand:
A Dance with Dragons Ch 70.

Theon:
A Clash of Kings Ch 11, Ch 24, Ch 37, Ch 50, Ch 56, Ch 66,
A Dance with Dragons Ch 51.

Theon as Reek:
A Dance with Dragons Ch 12, Ch 20, Ch 32.

Theon in The Prince of Winterfell:
A Dance with Dragons Ch 37.

Theon in The Turncloak:
A Dance with Dragons Ch 41.

Theon in A Ghost in Winterfell:
A Dance with Dragons Ch 46.

Theon in The Sacrifice:
A Dance with Dragons Ch 62.

Tyrion:
A Game of Thrones Ch 9, Ch 13, Ch 21, Ch 31, Ch 38, Ch 42, Ch 56, Ch 62, Ch 69,
A Clash of Kings Ch 3, Ch 8, Ch 15, Ch 17, Ch 20, Ch 25, Ch 29, Ch 36, Ch 41, Ch 44, Ch 49, Ch 54, Ch 59, Ch 61, Ch 67,
A Storm of Swords Ch 4, Ch 12, Ch 19, Ch 32, Ch 38, Ch 53, Ch 58, Ch 60, Ch 66, Ch 70, Ch 77,
A Dance with Dragons Ch 1, Ch 5, Ch 8, Ch 14, Ch 18, Ch 22, Ch 27, Ch 33, Ch 40, Ch 47, Ch 57, Ch 66.

Varys:
A Dance with Dragons Epilogue.

Victarion:
A Dance with Dragons Ch 63.

Victarion in The Iron Captain:
A Feast for Crows Ch 18.

Victarion in The Reaver:
A Feast for Crows Ch 29.

Victarion in The Iron Suitor:
A Dance with Dragons Ch 56.


AMD 6310 GLSL/FBO texture copy issue

01 May 2013 - 04:08 PM

I'm using OpenGL 2.0 and an FBO to copy some data from an RGBA texture to an RGB texture, and I ran into an issue where sometimes it "corrupts" the first few lowest order bits of some of the pixel components during the copy.

 

The texture copy is broken up into several steps, and I'm resizing the FBO.

 

At first I thought perhaps it was a problem related to the way that I was resizing the FBO, or with the way the the texture is being sampled, but the problem doesn't always occur, and when it does, it never occurs for every pixel copied nor does it ever occur for all of the components of each problematic pixel. In other words, it seems nearly random, except that it is indeed deterministic insomuch that the same error(s) occur if the same input float values are used during each run of the program.

 

Also, the problem never, ever occurs if I always use an FBO size of 1x1 (which is kind of misleading to know, because it made me think that it was a sampling issue, but, again, that is probably not the case since not every component of every problematic pixel is "corrupted"). Unfortunately, using an FBO size of 1x1 is absolutely useless in the real world where I'm going to be copying a texture containing anything more than a few pixels.

 

The problem happens on Windows 7 and Ubuntu, and the problem happens when I use MSVC++ or g++'s std rand() or Mersenne Twister to generate the input texture values (not that how I generate the values should matter, since copy operations are by definition independent of how the data to be copied was generated beforehand) .

 

I wrote a test program (see code below) where nothing changes between runs of the program other than the input texture values.

 

Does anyone have an AMD 6310 (or any other kind of hardware, really) that they can run this test program on? You'll have to run it a few times, as sometimes it produces an error, and sometimes it does not. I'm just curious if it ever produces the error on your hardware. I just can't spot the pattern, and my naive thinking is that this should either work all of the time, or never -- not so sporadically.

 

I'm also totally wiling to accept that it might, in the end, have something to do with the way that I'm using OpenGL to do the copy. This would be relieving actually, since it would mean that there's an easy and reliable solution. I hope this is the case.

 

I probably have some extraneous calls to glTexParameteri in there somewhere, but I was trying the "better safe than sorry" method while working on this test program.

 

In any case, the problem results in some of the pixel components having error that's on the order of like ~1e-8. Yes, that's a very small error, but it's totally unacceptable for what I'm doing.

 

#include <iostream>
using std::cout;
using std::endl;

#include <iomanip>
using std::ios_base;
using std::setprecision;
using std::fixed;

#include <vector>
using std::vector;

#include <string>
using std::string;

#include <utility>
using std::pair;

#include <cmath>
#include <cstdlib> // MSVC++ chokes if you don't include this before glut.h
#include <ctime>

#include <GL/glew.h>
#include <GL/glut.h>

// Automatically link in the GLUT and GLEW libraries if compiling on MSVC++
#ifdef _MSC_VER
    #pragma comment(lib, "glew32")
    #pragma comment(lib, "glut32")
#endif



float dist(float a, float b);
bool initialize_fragment_shader(const string &fragment_shader_code, GLint &shader, string &error);
void get_chunk_sizes(const size_t num_pixels, vector< pair<size_t, size_t> > &chunk_sizes, const bool force_1x1_chunks, const bool force_square_chunks = false);
string float_bits_string(const float f);

int main(int argc, char **argv)
{
    // This program uses an FBO and a fragment shader to copy RGBA pixels from an input array into an RGB output array.
    // It breaks up the entire pixel copy process into many smaller chunks of a varying number of pixels per chunk.
    // See line 165 to change the number of pixels in the array (right now it's hard-coded to be 7 pixels total).

    // If the chunk sizes are forced to be 1x1 pixels, then there are never any problems with the copy.
    // See line 186 to change whether the chunks are forced to be 1x1 pixels or not (right not they are not being forced as such).
    //
    // If the chunk sizes are not forced to be 1x1 pixels, then almost all of the time (but not quite always)
    // there is a small problem with at least one component of one of the pixels during the copy:
    //
    // The copy is off by a small, non-zero value of practically constant magnitude (on the order of ~1e-8).
    //
    // Since the values of the pixel components are the only thing that change between runs of the program,
    // the problem seems to be entirely dependent on the values of the pixel components themselves. This is totally
    // unexpected -- it should always work or always fail to the same degree, regardless of the pixel component values.
    // While looking at the bit patterns of the problematic pixel component values, it seems that it is always only the
    // first three to five lowest order bits that do not get copied successfully.
    //
    // Note that if the values of the pixel components do not change between runs, then the same errors occur,
    // and so the problem seems to be entirely deterministic. Right now the components are set via PRNG, and are done in a way
    // so that all of the bits of precision are used (see lines 173 - 176).
    // See line 86 to alter the PRNG seed.



// 1) Initialize pseudo-random number generator.
    srand(time(0)); // srand(0);



// 2) Initialize OpenGL and related objects.
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    GLint glut_window_handle = glutCreateWindow("");

    if(! ( GLEW_OK == glewInit() &&
           GLEW_VERSION_2_0 &&
           GLEW_ARB_framebuffer_object &&
           GLEW_ARB_texture_rectangle ) )
    {
        return -1;
    }

    GLint shader_handle = 0;
    GLuint fbo_handle = 0;
    GLuint tex_fbo_handle = 0;
    GLuint tex_in_handle = 0;
    GLuint tex_out_handle = 0;
    const GLint tex_in_internal_format = GL_RGBA32F_ARB;
    const GLint tex_in_format = GL_RGBA;
    const GLint tex_out_internal_format = GL_RGB32F_ARB;
    const GLint tex_out_format = GL_RGB;
    const GLint var_type = GL_FLOAT;

    string code;
    code += "#version 110\n";
    code += "uniform sampler2D input_tex;\n";
    code += "void main(void)\n";
    code += "{\n";
    code += "    vec4 p = texture2D(input_tex, gl_TexCoord[0].st);\n";
    code += "    gl_FragData[0].rgb = vec3(p.xyz);\n";
    code += "}\n";

    string error;

    if(false == initialize_fragment_shader(code, shader_handle, error))
    {
        cout << error << endl;
        return -2;
    }

    glGenTextures(1, &tex_in_handle);
    glBindTexture(GL_TEXTURE_2D, tex_in_handle);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glGenTextures(1, &tex_out_handle);
    glBindTexture(GL_TEXTURE_2D, tex_out_handle);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glGenFramebuffersEXT(1, &fbo_handle);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo_handle);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glGenTextures(1, &tex_fbo_handle);
    glBindTexture(GL_TEXTURE_2D, tex_fbo_handle);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glUseProgram(shader_handle);
    glUniform1i(glGetUniformLocation(shader_handle, "input_tex"), 0); // Use texture 0.



// 3) Set up input -- an array of RGBA float pixels with pseudorandom values.
    size_t num_pixels = 7; // = rand() % 50 + 1;
    size_t num_input_channels = 4;
    vector<float> input(num_pixels*num_input_channels, 0);

    for(size_t i = 0; i < num_pixels; i++)
    {
        size_t input_index = i*num_input_channels;
        
        input[input_index + 0] = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
        input[input_index + 1] = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
        input[input_index + 2] = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
        input[input_index + 3] = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
    }



// 4) Break up processing of input into chunks.
    vector< pair<size_t, size_t> > chunks;



#ifdef FORCE_1x1_CHUNKS
    get_chunk_sizes(num_pixels, chunks, true, true);
#else
    get_chunk_sizes(num_pixels, chunks, false, true);
#endif



    size_t num_pixels_remaining = num_pixels;

    size_t num_output_channels = 3;
    vector<float> output(num_pixels*num_output_channels, 0);

    for(size_t i = 0; i < chunks.size(); i++)
    {
        cout << "Pixels remaining: " << num_pixels_remaining << ", processing chunk size: " << chunks[i].first << " x " << chunks[i].second << " = " << chunks[i].first*chunks[i].second << endl;

        const size_t tex_size_x = chunks[i].first;
        const size_t tex_size_y = chunks[i].second;
        const size_t index = num_pixels - num_pixels_remaining;
        const size_t input_index = index*num_input_channels;
        const size_t output_index = index*num_output_channels;

        // Set the FBO size to match the current chunk size.
        glBindTexture(GL_TEXTURE_2D, tex_fbo_handle);
        glTexImage2D(GL_TEXTURE_2D, 0, tex_out_internal_format, tex_size_x, tex_size_y, 0, tex_out_format, var_type, 0);
        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex_fbo_handle, 0);

        // Write to GPU memory.
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, tex_in_handle);
        glTexImage2D(GL_TEXTURE_2D, 0, tex_in_internal_format, tex_size_x, tex_size_y, 0, tex_in_format, var_type, &input[input_index]);

        // Calculate by "drawing".
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 1, 0, 1, 0, 1);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glViewport(0, 0, tex_size_x, tex_size_y);

        glBegin(GL_QUADS);
            glTexCoord2f(0, 1);    glVertex2f(0, 1);
            glTexCoord2f(0, 0);    glVertex2f(0, 0);
            glTexCoord2f(1, 0); glVertex2f(1, 0);
            glTexCoord2f(1, 1);    glVertex2f(1, 1);
        glEnd();

        // Read from GPU memory.
        glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
        glReadPixels(0, 0, tex_size_x, tex_size_y, tex_out_format, var_type, &output[output_index]);

        num_pixels_remaining -= tex_size_x*tex_size_y;
    }



// 5) Analyze largest distance between input and output -- it should be zero, but it is not zero
//    if the chunk sizes are not forced to be 1x1.
    float largest_dist = 0;
    cout << setprecision(18);

    cout << endl << "Comparing input and output: " << endl;

    for(size_t i = 0; i < num_pixels; i++)
    {
        size_t input_index = i*num_input_channels;
        size_t output_index = i*num_output_channels;

        float dist0 = dist(input[input_index + 0], output[output_index + 0]);
        float dist1 = dist(input[input_index + 1], output[output_index + 1]);
        float dist2 = dist(input[input_index + 2], output[output_index + 2]);

        if(dist0 > largest_dist)
            largest_dist = dist0;

        if(dist1 > largest_dist)
            largest_dist = dist1;

        if(dist2 > largest_dist)
            largest_dist = dist2;

        if(dist0 != 0)
        {
            cout << endl;
            cout << "**** Copy error at pixel " << i + 1  << " first component" << endl;
            cout << "\tInput:  " << input[input_index + 0] << '\n' << "\tOutput: " << output[output_index + 0] << endl;
            cout << "\tInput (as bits):  " << float_bits_string(input[input_index + 0]) << '\n' << "\tOutput (as bits): " << float_bits_string(output[output_index + 0]) << endl;
            cout << endl;
        }
        else
        {
            cout << "OK at pixel " << i + 1  << " first component" << endl;
//            cout << "\tInput:  " << input[input_index + 0] << '\n' << "\tOutput: " << output[output_index + 0] << endl;
        }

        if(dist1 != 0)
        {
            cout << endl;
            cout << "**** Copy error at pixel " << i + 1 << " second component" << endl;
            cout << "\tInput:  " << input[input_index + 1] << '\n' << "\tOutput: " << output[output_index + 1] << endl;
            cout << "\tInput (as bits):  " << float_bits_string(input[input_index + 1]) << '\n' << "\tOutput (as bits): " << float_bits_string(output[output_index + 1]) << endl;
            cout << endl;
        }
        else
        {
            cout << "OK at pixel " << i + 1 << " second component" << endl;
//            cout << "\tInput:  " << input[input_index + 1] << '\n' << "\tOutput: " << output[output_index + 1] << endl;
        }

        if(dist2 != 0)
        {
            cout << endl;
            cout << "**** Copy error at pixel " << i + 1 << " third component" << endl;
            cout << "\tInput:  " << input[input_index + 2] << '\n' << "\tOutput: " << output[output_index + 2] << endl;
            cout << "\tInput (as bits):  " << float_bits_string(input[input_index + 2]) << '\n' << "\tOutput (as bits): " << float_bits_string(output[output_index + 2]) << endl;
            cout << endl;
        }
        else
        {
            cout << "OK at pixel " << i + 1 << " third component" << endl;
//            cout << "\tInput:  " << input[input_index + 2] << '\n' << "\tOutput: " << output[output_index + 2] << endl;
        }

    }

    if(0 != largest_dist)
        cout << "\nLargest copy error: " << largest_dist << endl;
    else
        cout << "\nNo copy errors." << endl;



// 6) Cleanup OpenGL and related objects.
    glDeleteTextures(1, &tex_in_handle);
    glDeleteTextures(1, &tex_out_handle);
    glDeleteTextures(1, &tex_fbo_handle);
    glDeleteFramebuffersEXT(1, &fbo_handle);
    glUseProgram(0);
    glDeleteProgram(shader_handle);
    glutDestroyWindow(glut_window_handle);

    return 0;
}

float dist(float a, float b)
{
    return fabsf(b - a);
}

bool initialize_fragment_shader(const string &fragment_shader_code, GLint &shader, string &error)
{
    error = "";

    // Compile shader.
    const char *cch = 0;
    GLint status = GL_FALSE;
    GLint frag = glCreateShader(GL_FRAGMENT_SHADER);

    glShaderSource(frag, 1, &(cch = fragment_shader_code.c_str()), 0);
    glCompileShader(frag);
    glGetShaderiv(frag, GL_COMPILE_STATUS, &status);

    if(GL_FALSE == status)
    {
        error = "Fragment shader compile error.\n";
        vector<GLchar> buf(4096, '\0');
        glGetShaderInfoLog(frag, 4095, 0, &buf[0]);

        for(size_t i = 0; i < buf.size(); i++)
            if(0 != buf[i])
                error += buf[i];

        error += '\n';

        return false;
    }

    // Link to get final shader.
    shader = glCreateProgram();
    glAttachShader(shader, frag);
    glLinkProgram(shader);
    glGetProgramiv(shader, GL_LINK_STATUS, &status);

    if(GL_FALSE == status)
    {
        error = "Program link error.\n";
        vector<GLchar> buf(4096, '\0');
        glGetShaderInfoLog(shader, 4095, 0, &buf[0]);

        for(size_t i = 0; i < buf.size(); i++)
            if(0 != buf[i])
                error += buf[i];

        error += '\n';

        glDetachShader(shader, frag);
        glDeleteShader(frag);
        return false;
    }

    // Cleanup.
    glDetachShader(shader, frag);
    glDeleteShader(frag);

    return true;
}

void get_chunk_sizes(const size_t num_pixels, vector< pair<size_t, size_t> > &chunk_sizes, const bool force_1x1_chunks, const bool force_square_chunks)
{
    chunk_sizes.clear();

    size_t num_pixels_remaining = num_pixels;
    GLint max_tex_size = 0;

    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_tex_size);

    size_t curr_tex_x = max_tex_size;
    size_t curr_tex_y = max_tex_size;

    if(true == force_1x1_chunks)
        curr_tex_x = curr_tex_y = 1;

    while(0 < num_pixels_remaining)
    {
        if(num_pixels_remaining < curr_tex_x*curr_tex_y)
        {
            if(true == force_square_chunks)
            {
                curr_tex_x /= 2;
                curr_tex_y /= 2;
            }
            else
            {
                if(curr_tex_x == curr_tex_y)
                    curr_tex_y /= 2;
                else
                    curr_tex_x /= 2;
            }
        }
        else
        {
            pair<size_t, size_t> p(curr_tex_x, curr_tex_y);
            chunk_sizes.push_back(p);
            num_pixels_remaining -= curr_tex_x*curr_tex_y;
        }
    }
}

string float_bits_string(const float f)
{
    long unsigned int bit_mask = 1;
    long unsigned int intval = *(long unsigned int*)&f;

    string bits;

    for(size_t i = 0; i < 32; i++, bit_mask <<= 1)
    {
        if(intval & bit_mask)
            bits += '1';
        else
            bits += '0';
    }

    bits = string(bits.rbegin(), bits.rend());

    return bits;
}
 

A C++ code to smooth (and fix cracks in) meshes generated by the standard, original Mar...

22 April 2013 - 03:00 PM

In the past, some people have asked for code or help with applying a smoothing algorithm to the meshes they get from the Marching Cubes algorithm (see: 'Polygonising a scalar field' by P. Bourke).

 

I've attached some sample C++ code to perform Taubin smoothing (see: 'Geometric Signal Processing on Polygonal Meshes' by G. Taubin and 'Implicit Fairing of Irregular Meshes using Diffusion and Curvature Flow' by M. Desbrun, et al.) on a Stereo Lithography (STL) file. The code assumes that the mesh is closed -- Attached File  mc_smooth2.zip   8.27KB   181 downloads

 

The code also eliminates the cracks in the mesh that are unfailingly generated by the standard, original Marching Cubes algorithm (Google for 'marching cubes cracks' to see that it is a common problem).

 

The cracks must be eliminated before smoothing occurs, otherwise the cracks will become holes, and the mesh will be ruined.

 

Attached is a sample mesh (including cracks) -- Attached File  fractal.zip   826.28KB   97 downloads

 

Here is an image of the sample mesh as generated by Marching Cubes, rendered using flat shading -- very blocky:

Attached File  1_raw_flat_shaded.png   76.92KB   43 downloads

 

Here is an image of the cracks highlighted in red, rendered using flat shading:

Attached File  3_raw_cracks.png   119.73KB   38 downloads

 

Here is an image of the sample mesh after 10 iterations of Taubin smoothing, rendered using flat shading -- the cracks have now become holes, thanks to the smoothing. This is bad:

Attached File  4_taubin_smoothed_holes.png   122.38KB   38 downloads

 

Here is an image of the vertex pairs (highlighted in blue, and connected by green lines) that should have been merged -- in order to fix the cracks -- before the smoothing was applied, rendered using flat shading:

Attached File  5_taubin_smoothed_merge_vertices.png   113.17KB   37 downloads

 

Here is an image of the crackless mesh after 10 iterations of Taubin smoothing, rendered using flat shading -- no holes, looks less blocky:

Attached File  6_taubin_smoothed_flat_shaded.png   71.34KB   35 downloads

 

Here is an image of the crackless mesh after 10 iterations of Taubin smoothing, rendered using Gouraud shading -- no holes, looks much less blocky:

Attached File  7_taubin_smoothed_gouraud_shaded.png   99.43KB   34 downloads

 

Good luck.

 

P.S. The sample mesh is an isosurface of the escape value (where isovalue = 4.0) for the quaternion Julia set generated by Z' = Z^2 + C where C = {0.3, 0.5, 0.4, 0.2} and the maximum number of iterations is 8. (see: 'Quaternion Julia Fractals' by P. Bourke). The code to generate this mesh can be found at: http://code.google.com/p/qjs-isosurface/downloads/detail?name=qjs-isosurface-v2.0.2.1.zip


FQXi Large Grant Program -- Physics of Information, An International Request for Proposals

16 October 2012 - 04:36 PM

The Foundational Questions Institute is calling for grant proposals related to the topic of "Physics of Information".

Main link:
http://fqxi.org/grants/large/initial

PDF intro:
http://fqxi.org/tools/download/__details/2013-Request-for-Proposals.pdf

"FQXi requests proposals for rigorous research on the Physics of Information both in physics and also in
related fields including cosmology, astrophysics, mathematics, computer science, complex systems,
biology, and neuroscience. Funded research will address this gap between research and technological
progress on information science on one side, and active study of the true physical nature of information
on the other. It will also, by bringing together the community in a focused and intense effort, seek to
help develop a common understanding of the different types of information and the roles they play.
Most importantly, the research supported by this program should have significant implications for our
understanding of the major questions across many scientific disciplines, and address the deep or
‘ultimate’ nature of reality."

"This RFP is intended to fill a gap, not a shortfall, in conventional funding. We
wish to enable research that, because of its speculative, non-mainstream, or high-risk nature,
would otherwise go unperformed due to lack of available monies. Thus, although there will be
inevitable overlaps, an otherwise scientifically rigorous proposal that is a good candidate for an
FQXi grant will generally not be a good candidate for funding by the NSF, DOE, etc. – and vice
versa."

There are a ton of people here much better than I will ever be at physics, math and comp sci. Maybe you could use the grant money to buy yourself some books, take some classes, do some thinking. Just a thought.

PARTNERS