PDA

View Full Version : QVectos to multidimensional array



zorro68
15th March 2007, 10:00
Hi, I'm trying to program a surface with opengl evaluators or glunurbs, but this two functions needs a multidimensional array (three dimension) for points. I have my points in three Qvector (one for x, one for y and one for z). When I trying to convert this qvectors to a array i have an error, but i dont know what. The compiler says ok all, but when i run the program, the program exit with no warning. This is the code:


GLuint GLWidget::makeSurf(const GLfloat *reflectance,QVector<float> x,QVector<float> y,QVector<float> z)
{
float ***points;
int dimx=x.size();
int dimy=y.size();

points= new float **[dimx];
for (int i=0;i<dimx;i++){
points[i]=new float *[dimy];
for (int j=0;j<dimy;j++){
points[i][j]=new float[3];
}
}
int cont=0;
for (int ix=0; ix<dimx; ix++){
for (int iy=0; iy<dimy; iy++){
cont++;
points[ix][iy][0]=x[cont];
points[ix][iy][1]=y[cont];
points[ix][iy][2]=z[cont];
}
}


or this way:

points = new float **[dimx];
for(int i=0;i<dimx;i++) {
*(points+i) = new float *[dimy];
for(int j=0;j<dimy;j++){
*(*(points+i)+j) = new float[3];
}
}
int cont=0;
for(int i=0;i<dimx;i++) {
for(int j=0;j<dimy;j++) {
*(*(*(points+i)+j)+0) = x[cont];
*(*(*(points+i)+j)+1) = y[cont];
*(*(*(points+i)+j)+2) = z[cont];
cont++;
}
}
..........
}


Both samples crash. I have check dimension of qvectors and are ok. But exit the programm with no warning.
Need help please.
Thanks

high_flyer
15th March 2007, 10:08
It is definetly an idex problem.
I suggest you do a core dump, or - instert qDebug() along the code, and play with it untill you get to the line that is crashing, then add a debug information befor that line that will dump all the index values.

zorro68
15th March 2007, 11:05
i can see now whats the problem. If i have for example 6 points (6 for x, 6 for y and 6 for z) i cannot programm a double for loop to define the points of the surface, because this code are trying to save 6*6 points, and i only have 6 points (with 3 coordenates each point).

But, can anybody tell me how to make a surface (with opengl or glunurbs) if a have a collection of coordenates (x,y,z)?

high_flyer
15th March 2007, 11:09
If i have for example 6 points (6 for x, 6 for y and 6 for z)
But isn'T a point made of x,y and z?
like: point1(x,y,z) point2(x,y,z)?

zorro68
15th March 2007, 11:32
These are the points:

( x[0] , y[0] , z[0] )
( x[1] , y[1] , z[1] )
( x[2] , y[2] , z[2] )
...
( x[n] , y[n] , z[n] )

But i save all x in one qvector, all y in other qvector and all z in other qvector.

high_flyer
15th March 2007, 11:53
so what do you mean by :

If i have for example 6 points (6 for x, 6 for y and 6 for z)?

zorro68
15th March 2007, 12:10
I have 6 points (for example) but i dont have dimx=6 * dimy=6 points, and when i use this code:



int cont=0;
for (int ix=0; ix<dimx; ix++){
for (int iy=0; iy<dimy; iy++){
cont++;
points[ix][iy][0]=x[cont];
points[ix][iy][1]=y[cont];
points[ix][iy][2]=z[cont];
}
}


i'm trying to alocate 6x6 points, and this is the error, cont=0,1,.....,36, and x[0,..,6] y[0,...,6] and z[0,...,6].
The definition of the pointer (***points) is ok, but the initialization of the pointer is bad.
This code works if i make only a for loop like this:



for (int i=0; i<dimx; i++){
points[i][i][0]=x[i];
points[i][i][1]=y[i];
points[i][i][2]=z[i];
}


But this is not what i want for making a surface, because i have a lots of points in the array with no initialization.

So, i make this question: how construct a surface if i have n points (x,y,z) with evaluators (opengl) or nurbs (glut), or another way...?

high_flyer
15th March 2007, 12:43
I don't see any problem with the array initialization.
Your array has dimx*dimy*3 dimensions.
So if your x vector is size 6, and your y vector is size 6 then your array is
6*6*3.
Where any combination of: points[0-5][0-5][0-2] is valid.
The only problem (now that I looked a bit more carefully) is this the count.
But I need to think it over.
I am confuses.
Do you have 6 (for example) given poitns which have x,y,z values, OR - you have 6 x value, 6 y values and 6 z values that you want to asign to any possible combination of the tree vectors?

zorro68
15th March 2007, 13:51
No, I have 6 (for examples) points, only 6 point whoose x values are save in a qvector, y coordinates in a qvector and z coordinates in a qvector. But i dont want to the combination of these values. The points are:

( x[0] , y[0] , z[0] )
( x[1] , y[1] , z[1] )
( x[2] , y[2] , z[2] )
( x[3] , y[3] , z[3] )
( x[4] , y[4] , z[4] )
( x[5] , y[5] , z[5] )

but not the combination of theese values. For example, this is not a valid point for me: ( x[1] , y[2] , z[3] ).

Sorry for my bad english.

high_flyer
15th March 2007, 14:04
it that case you dont need a multi dimensional array the way you did it.
You need one dimensional array of points, where each point has x,y,z values.
I still don't know how you want to use this data.
Whats wrong with having the coordinated in 3 vectors?
What are you trying to acheave by having all the infomration in one entity?
Is doing:


valx = x[i];
valy = y[i];
valz = z[i];


not good?

If you must have each point as entity you can do:


typedef struct {
float x;
float y;
float z;
} point;

QVector<point> vpoints;
point temp;
for(int i=0; i<x.size(); ++i)
{
temp.x = x[i];
temp.y = y[i];
temp.z = z[i];
vpoints.push_back(temp);
}

zorro68
15th March 2007, 17:32
Yes, but i need a multidimensional array because opengl functions to evaluate surfaces (glMap2f, GlEvalMesh2,...) needs points in that way, like multidimensional array, and glut functions for NURBS surfaces need it too. Thats the reason why i need multidimensional array.
If you know other way to do this (paint in opengl a surface with n points), please tell me how?

Thanks

high_flyer
15th March 2007, 17:56
Yes, but you are mixing and confusing things.
You have QVector.size() points.
So the index of the x,y and z vectors is not their location in the 3D world, but their joint values are, yet you are using the vector index as a position:

points[ix][iy][0]=x[cont];
what (maybe) you are looking for is:


for(int i=0: i<x.size(); i++)
points[x[i]][y[i]][z[i]] = somevalue;

but what 'somevalue' is, I don't know, its not visible in your code, maybe reflectance[i]?

zorro68
15th March 2007, 19:13
So the index of the x,y and z vectors is not their location in the 3D world, but their joint values are, yet you are using the vector index as a position:
Yes, you are right about confusing.:o


but what 'somevalue' is, I don't know, its not visible in your code, maybe reflectance[i]?
No, in qvector x, y and z are the coordinates in a real 3D space of the points of the surface.

high_flyer
15th March 2007, 19:48
No, in qvector x, y and z are the coordinates in a real 3D space of the points of the surface.
Which is exactly what my last suggestion does.
The 3D array should hold some point values, right?
The vectors hold the x,y,z position and.... where is the value?

zorro68
16th March 2007, 10:02
With a fortran programm i generate a grid of points who contents x,y,z,value then i select wich points has a value between l1 and l2 ( l1=value-delta, l2=value+delta), this made me a points collection with the same value of the function (this is a contour). I want to print in open gl this contour (points(x,y,z) with the same value of the function) and construct a surface with theese points. Im trying this (x,y,z) be my control points to construct the surface. I know i can joint this points with lines, triangles,... but this is not a smooth surface, so im trying to use opengl evaluators or glutnurbs where my points are the control points of the evaluator or nurbs.

I hope you understand me.

high_flyer
16th March 2007, 10:53
I hope you understand me.
Well, yes and no...

Ok, so you have the values for each point calculated by some external function.
Good.
So where is the problem?
You can use the code I suggested (with your original 3D array initialization)


for(int i=0: i<x.size(); i++)
points[x[i]][y[i]][z[i]] = somevalue;


Where 'somevalue' is the value you calculated with your fortran program.
And you can feed this 3D array then to opengl.

zorro68
16th March 2007, 11:15
After all, thanks a lot for your patient and for your suggestions.

I cant see why this code works because x[i], y[i] and z[i] are floating values. And I dont know why theese points are the control points of my surface in opengl.

Nevertheless, im going to try this....

As I thought, compiler says me that x[i]... are not integral values. And no compile the program.

high_flyer
16th March 2007, 11:35
I cant see why this code works because x[i], y[i] and z[i] are floating values.
You are right, I forgot these are floats.
But then something does not add up with the information you are giving.
What kind of a multi dimenssional array does openGL expects?
What kind of infomation the array should have?
The only way I see is to map the vector values to ints - otherwise you can't use them as indexes in an array.

minimoog
16th March 2007, 11:57
To help this discussion...

This is what OpenGL expects. gluNurbsSurface. (http://www.opengl.org/sdk/docs/man/xhtml/gluNurbsSurface.xml)

zorro68
16th March 2007, 11:59
Im trying to do something like this with my points:

http://pgrafica.webideas4all.com/evaluators.html

but in this way i need n*m control points, and i have not this n*m control points. I have lots of points in a 3Dspace, and i need a surface that contains all theese points.
Im thinking of generate the n*m points with my points and interpolate (splines,bsplines,....or any suggestion) the other points. If i have the n*m control points i can construct the surface with evaluators.

high_flyer
16th March 2007, 12:21
I have no idea what nurbs are, since my opengl skills are limited.
I was trying to help you sort the problem with the multidimensional array.
I can't help you about specific openGL issues, nor is this the place for that.
If the problem is opengl specific, you might want to try to post it on an opengl forum.

zorro68
16th March 2007, 14:45
Thanks for all high_flyer