Archive

Archive for the ‘C++’ Category

stl Sort in C++; simple code for sorting the array of structures

February 18, 2009 mohsenam Leave a comment

Simple Code that shows how to use the C++ STL function sort()


<blockquote>
#include <iostream>
#include <algorithm>

using namespace std;

struct Numb{
double num;
int pos1;

bool operator &lt;(const Numb &amp;n1){
return num &lt; n1.num;
};
};
////////////////////////////////
void main(void){

Numb numb[10];

for(int i=0; i &lt; 10; i++){
numb[i].num = rand();
numb[i].pos1 = i;
}
cout &lt;&lt; endl &lt;&lt; "---------" &lt;&lt;endl;
for(int i=0; i &lt; 10; i++){
cout &lt;&lt; " " &lt;&lt; "(" &lt;&lt; numb[i].pos1 &lt;&lt; ") " &lt;&lt; numb[i].num;
}
cout &lt;&lt; endl &lt;&lt; "---------" &lt;&lt;endl;

std::sort( numb, numb+10);
cout &lt;&lt; endl &lt;&lt; "---------" &lt;&lt;endl;
for(int i=0; i &lt; 10; i++){
cout &lt;&lt; " " &lt;&lt; "(" &lt;&lt; numb[i].pos1 &lt;&lt; ") " &lt;&lt; numb[i].num;
}

cout &lt;&lt; endl &lt;&lt; "---------" &lt;&lt;endl;

}
Categories: STL, code Tags: , , , ,

converting simple STL list to circular list

February 11, 2009 mohsenam Leave a comment

/////////////////////////////////////////////////////////
//converting simple STL list to circular list
/////////////////////////////////////////////////////////
list<int>::iterator nextIter(list<int>::iterator i, list<int>::iterator beg,  list<int>::iterator end){
i++;
if(i == end){i = beg;}
return i;
}
/////////////////////////////////////////////////////////
list<int>::iterator prevIter(list<int>::iterator i, list<int>::iterator beg,  list<int>::iterator end){
if(i == beg){i = end; i–;}
else{i–;}
return i;
}
/////////////////////////////////////////////////////////
void printL(list<int> &L){
cout << endl;
list<int>::iterator i;
for(i=L.begin(); i != L.end(); ++i) cout << *i << ” “;
cout << endl;
}

Categories: C++, STL Tags:

Simple STL code in C++

February 11, 2009 mohsenam Leave a comment

Just a general simple program for the Standard Template Library (STL) List

#include <list>
#include <iostream>
using namespace std;
////////////////////////////////
class codeWord{
public:
double vl[3];
double minI;

codeWord(){
minI = 0;
vl[0]=vl[1]=vl[2] = 0;
};
};
////////////////////////////////
void printList(std::list<codeWord *> &ml){
cout <<endl;

codeWord *ptr;
for(std::list<codeWord *>::iterator list_iter = ml.begin(); list_iter != ml.end(); list_iter++)    {
ptr = *list_iter;
std::cout<<” ” << ptr->minI<<endl;
}

cout <<endl;

};
////////////////////////////////
void main(void){

std::list<codeWord *> mlist;
codeWord a;
codeWord b;

a.minI = 10;
b.minI = 5;

mlist.push_front(&a);
mlist.push_front(&b);

printList(mlist);

};

Categories: STL Tags: ,

Writing PPM image file in C++

November 20, 2008 mohsenam Leave a comment

Following is the C++ code for writting the ppm image file

strtemp has file name,

fData is the pointer to the image data.

sprintf(strtemp,”%04d.ppm”,imageNo);
ofstream output(strtemp, ios::binary|ios::out);
if(!output){
cout << ” unable to open the output file “<< strtemp << endl;
}
else{
output << “P6″<< endl <<”# foreground “<<endl;
output << itoa(IWidth, strtemp, 10);
output << ” “;
output << itoa(IHeight, strtemp, 10);
output << endl;
output << itoa(255, strtemp, 10) << endl;
output.write( (char *)fData, IHeight*IWidth*3);
output.close();

};//end of else

Categories: C++, File-Handling, ImageProcessing Tags:

OpenCv IplImage how to clear or initialize the image to scalar

November 17, 2008 mohsenam Leave a comment

just use the cvSet

e.g if img is the IplImage object pointer with 3 channels.

cvSet(img, cvScalar(0,0,0));

will set each and every pixel to zero.

Becarefull with the  channels and cvScalar used.

GLSL: Accessing one row of the texture

//Fragment Shader Program to access texture and just copying one row to other

uniform sampler2D textureX;
uniform float        texRows;
uniform float        texCols;

void main(void) {
//cols                rows
vec4 x            = texture2D(textureX, vec2(gl_TexCoord[0].s, gl_TexCoord[0].t+1.0/texRows));
gl_FragColor    = x;

}

Categories: C++, GLSL, Opengl, Texture Tags: ,

GLSL Texture

Coordinates of the gl_TexCoord[0]

gl_TexCoord[0].t —— rows

gl_TexCoord[0].s ——cols

vec4 x = texture2D(textureX, vec2(Columns, rows));

gl_FragColor = x;

Categories: C++, GLSL, Opengl Tags: ,

Assignment 3; Textures Very Imp Links

April 27, 2008 mohsenam Leave a comment

Use glEnable(GL_TEXTURE_2D);

GPU Programming Course http://www.evl.uic.edu/aej/594/

For the Multi Texturing refer to 

http://www.clockworkcoders.com/oglsl/tutorial8.htm

glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_2D, texture1);
glEnable(GL_TEXTURE_2D);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
glTexEnvf (GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_REPLACE);

glActiveTextureARB(GL_TEXTURE1_ARB);
glBindTexture(GL_TEXTURE_2D, texture2);
glEnable(GL_TEXTURE_2D);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
glTexEnvf (GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_INCR);
http://www.evl.uic.edu/aej/594/lecture04.html
http://www.research.rutgers.edu/~sueda/428Fall2005/proj3.html

Texture Rectangle
http://www.opengl.org/registry/specs/ARB/texture_rectangle.txt

The Dominick Page
http://www.mathematik.uni-dortmund.de/~goeddeke/gpgpu/tutorial.html
http://www.mathematik.uni-dortmund.de/~goeddeke/gpgpu/saxpy_glsl2.cpp
http://code.google.com/p/matpix/source/browse/trunk/src/helloGPGPU.cpp
Categories: C++, GLSL, Opengl Tags: ,

Settingup Texture to read the Frame Buffer

April 26, 2008 mohsenam Leave a comment

<good link http://www.mathematik.tu-dortmund.de/~goeddeke/gpgpu/saxpy_glsl.cpp>

These are basic steps to take

For each texture do the following things

1) Generate the texture 

glGenTextures(1, texId);

2) Then Bind the texture to what target you want 

glBindTexture( GL_TEXTURE_RECTANGLE_ARB, tex);

3) Setup the properties to avoid the wraping on edges, min-mapping,

glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
//clamp so that it does not excede where you do want to write
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP);

4) Use the  glTexImage2D to setup the properties like for format, type, height and width of the texture

 

Categories: C++, GLSL, Opengl Tags: , ,

Gaussian Masks

April 8, 2008 mohsenam 1 comment

Gaussian Masks (2D Normal Distribution)

Just Placing Masks so that one does not have to generate again when wanting to use quickly.

For the Matlab Code  you can go to http://whatevericode.wordpress.com/2008/03/24/genrating-gaussian-mask/

Standard Deviation 1

0.0030    0.0133    0.0219    0.0133    0.0030
0.0133    0.0596    0.0983    0.0596    0.0133
0.0219    0.0983    0.1621    0.0983    0.0219
0.0133    0.0596    0.0983    0.0596    0.0133
0.0030    0.0133    0.0219    0.0133    0.0030

Standard Deviation 0.5

0.0113    0.0838    0.0113
0.0838    0.6193    0.0838
0.0113    0.0838    0.0113

Standard Deviation 0.25

0.0000    0.0003    0.0000
0.0003    0.9987    0.0003
0.0000    0.0003    0.0000