Help with a 3D Grid\

G

Guardiano del Faro

Hello!!

i'm just a dummie in C/C++ and i need you help for solving my little
problem.

I have to create a 3D grid of points. Each point has the 3 coordinates
and a boolean value. (I made a structure in order to make this).

My task is to scan the grid through the x, y and z planes.

According to you, which is the best way to create this data structure?

I have tried to create a vector of vectors of vectors of point
structure, so to access to each point coordinate i just have to do
like:

Grid[j][k].x

So the i index will give me all the points which are on the x
planes....

but i have now idea about how to build it!!!
Does anybody has any clue?!

Thank you very much

Vito Baldassarre
 
V

Victor Bazarov

Guardiano said:
i'm just a dummie in C/C++ and i need you help for solving my little
problem.

I have to create a 3D grid of points. Each point has the 3 coordinates
and a boolean value. (I made a structure in order to make this).

My task is to scan the grid through the x, y and z planes.

According to you, which is the best way to create this data structure?

I have tried to create a vector of vectors of vectors of point
structure, so to access to each point coordinate i just have to do
like:

Grid[j][k].x

So the i index will give me all the points which are on the x
planes....

but i have now idea about how to build it!!!


How to build what?
Does anybody has any clue?!

What does it mean "to scan the grid"? If points are in arbitrary
positions throughout the XYZ space, none of them is guaranteed to
have any particular x or y or z coordinate (coincident with your
plane). Otherwise, if you have i,j,k _and_ x,y,z of every point,
then you don't have a 3-dimensional space (grid), you have six
dimensions and are working with a subset of it (hyperplane) for
every i or j or k...

You don't seem to have a clear understanding of what's required or
it's the language barrier that is stopping you from explaining it
better. Perhaps if you told us what problem you're solving, it
would be easier to suggest something. As an alternative, consider
posting to 'comp.graphics.algorithms' newsgroup. They deal with
spaces and points every day.

V
 
S

Steven T. Hatton

Guardiano said:
Hello!!

i'm just a dummie in C/C++ and i need you help for solving my little
problem.

I have to create a 3D grid of points. Each point has the 3 coordinates
and a boolean value. (I made a structure in order to make this).

My task is to scan the grid through the x, y and z planes.

According to you, which is the best way to create this data structure?

I have tried to create a vector of vectors of vectors of point
structure, so to access to each point coordinate i just have to do
like:

Grid[j][k].x

So the i index will give me all the points which are on the x
planes....

but i have now idea about how to build it!!!
Does anybody has any clue?!

Thank you very much

Vito Baldassarre


Try using typedef to create an alternative name for the vector of bool, and
then do the same for a vector of that type.
 
B

BobR

Guardiano del Faro wrote in message
Hello!!
i'm just a dummie in C/C++ and i need you help for solving my little
problem.
I have to create a 3D grid of points. Each point has the 3 coordinates
and a boolean value. (I made a structure in order to make this).
My task is to scan the grid through the x, y and z planes.
According to you, which is the best way to create this data structure?
I have tried to create a vector of vectors of vectors of point
structure, so to access to each point coordinate i just have to do
like:
Grid[j][k].x
So the i index will give me all the points which are on the x
planes....
but i have now idea about how to build it!!!
Does anybody has any clue?!
Thank you very much
Vito Baldassarre


// #includes<iostream>, <vector>
// ------------------------------------
void TriVector( std::eek:stream &cout ){
typedef std::vector<std::vector<std::vector<int> > > vec3d;
vec3d vec3D(3, std::vector<std::vector<int> >(3,
std::vector<int>(3, int(7))));

for(size_t x(0); x < vec3D.size(); ++x){
for(size_t y(0); y < vec3D.at(x).size(); ++y){
for(size_t z(0); z < vec3D.at(x).at(y).size(); ++z){
cout<<" vec3D.at("<<x<<").at("<<y<<").at("<<z<<")= "
<<vec3D.at(x).at(y).at(z)<<std::endl;
vec3D.at(x).at(y).at(z) = x+y+z;
cout<<" vec3D.at("<<x<<").at("<<y<<").at("<<z<<")= "
<<vec3D.at(x).at(y).at(z)<<std::endl;
} //for(z)
} //for(y)
cout<<std::endl;
} //for(x)
cout<<std::endl;
return;
} //TriVector(std::eek:stream&)
// ------------------------------------

int main(){
TriVector( std::cout );
return 0;
}
 
D

Daniel T.

"Guardiano del Faro said:
Hello!!

i'm just a dummie in C/C++ and i need you help for solving my little
problem.

I have to create a 3D grid of points. Each point has the 3 coordinates
and a boolean value. (I made a structure in order to make this).

My task is to scan the grid through the x, y and z planes.

According to you, which is the best way to create this data structure?

I have tried to create a vector of vectors of vectors of point
structure, so to access to each point coordinate i just have to do
like:

Grid[j][k].x

So the i index will give me all the points which are on the x
planes....

but i have now idea about how to build it!!!
Does anybody has any clue?!


My suggestion:

class Grid3D {
std::vector<bool> rep;
unsigned h, w, d;
public:
typedef std::vector<bool>::reference reference;
typedef std::vector<bool>::const_reference const_reference;

Grid3D( unsigned x, unsigned y, unsigned z ):
h( x ), w( y ), d( z ), rep( x * y * z )
{ }

reference at( unsigned x, unsigned y, unsigned z ) {
assert( x < h && y < w && z < d );
return rep[ x*w*d + y*d + z ] != 0;
}

const_reference at( unsigned x, unsigned y, unsigned z ) const {
assert( x < h && y < w && z < d );
return rep[ x*w*d + y*d + z ] != 0;
}

// other functions to taste
};

use like this:

int main() {
Grid3D g( 10, 10, 10 );
g.at( 3, 4, 6 ) = true;

if ( g.at( 7, 5, 9 ) ) {
cout << "should not print";
}
}

Caution: I wrote the above with a puppy in my lap, so there may be some
errors. :)
 
G

Guardiano del Faro

Victor Bazarov ha scritto:
Guardiano del Faro wrote:
Hello everybody and thanks for your help!
How to build what?

the data structure!
You don't seem to have a clear understanding of what's required or
it's the language barrier that is stopping you [...]

Ok, i will try to explain it better!

I have a protein, which is a set of spheras (i have a file which
contains the 3d coords of the atoms). So, let suppose the protein is
just a set of point. I know the MIN and the MAX, which are the
left-bottom and the right-up vertex.

I have to build a grind which contain this protein, and after that i
must scan the grid through the x direction, then the y direction, and
then the z direction

if a point of the grid is inside the protein (i already know how to
check it) it has a TRUE value, otherwise it's FALSE.

It's quite a simple idea, the problem is that i wanted to have a
dynamic allocation of the vectors...maybe it can be easier if i
calculate the dimensions of them...

Thanks a lot, i hope it's clear now

Buon Natale, Marry christmas
 
V

Victor Bazarov

Daniel said:
Guardiano del Faro said:
Hello!!

i'm just a dummie in C/C++ and i need you help for solving my little
problem.

I have to create a 3D grid of points. Each point has the 3
coordinates and a boolean value. (I made a structure in order to
make this).

My task is to scan the grid through the x, y and z planes.

According to you, which is the best way to create this data
structure?

I have tried to create a vector of vectors of vectors of point
structure, so to access to each point coordinate i just have to do
like:

Grid[j][k].x

So the i index will give me all the points which are on the x
planes....

but i have now idea about how to build it!!!
Does anybody has any clue?!


My suggestion:

class Grid3D {
std::vector<bool> rep;
unsigned h, w, d;
public:
typedef std::vector<bool>::reference reference;
typedef std::vector<bool>::const_reference const_reference;

Grid3D( unsigned x, unsigned y, unsigned z ):
h( x ), w( y ), d( z ), rep( x * y * z )
{ }

reference at( unsigned x, unsigned y, unsigned z ) {
assert( x < h && y < w && z < d );
return rep[ x*w*d + y*d + z ] != 0;


I am not sure 'reference' will work as the return value here or the
'return' statement should just be

return rep[ x*w*d + y*d + z ];
}

const_reference at( unsigned x, unsigned y, unsigned z ) const {
assert( x < h && y < w && z < d );
return rep[ x*w*d + y*d + z ] != 0;

I'd probably just do

return rep[ x*w*d + y*d + z ];

here as well.
}

// other functions to taste
};

use like this:

int main() {
Grid3D g( 10, 10, 10 );
g.at( 3, 4, 6 ) = true;

if ( g.at( 7, 5, 9 ) ) {
cout << "should not print";
}
}

Caution: I wrote the above with a puppy in my lap, so there may be
some errors. :)

V
 
V

Victor Bazarov

Guardiano said:
Victor Bazarov ha scritto:
Guardiano del Faro wrote:
Hello everybody and thanks for your help!
How to build what?

the data structure!
You don't seem to have a clear understanding of what's required or
it's the language barrier that is stopping you [...]

Ok, i will try to explain it better!

I have a protein, which is a set of spheras (i have a file which
contains the 3d coords of the atoms). So, let suppose the protein is
just a set of point. I know the MIN and the MAX, which are the
left-bottom and the right-up vertex.

I have to build a grind which contain this protein, and after that i
must scan the grid through the x direction, then the y direction, and
then the z direction

if a point of the grid is inside the protein (i already know how to
check it) it has a TRUE value, otherwise it's FALSE.

It's quite a simple idea, the problem is that i wanted to have a
dynamic allocation of the vectors...maybe it can be easier if i
calculate the dimensions of them...

Thanks a lot, i hope it's clear now

Coupling with Daniel T.'s suggestion, scanning through the grid
would be done by nested loops:

unsigned const H = ??;
unsigned const W = ??;
unsigned const D = ??;
double stepx = (xmax-xmin)/(H-1), stepy = (ymax-ymin)/(W-1);
double stepz = (zmax-zmin)/(D-1);
Grid3D inside(H, W, D); // see Daniel's suggestion
for (unsigned x = 0; x < H; ++x) {
for (unsigned y = 0; y < W; ++y) {
for (unsigned z = 0; z < D; ++z) {
// generate the point of the grid
double xP = xmin + x*stepx;
double yP = ymin + y*stepy;
double zP = zmin + z*stepz;
inside.at(x,y,z) = CheckIfInsideProtein(xP, yP, zP);
} } }

If your grid is supposedly regular, you essentially need a structure
that keeps the inside/outside flag (Daniel's Grid3D) and a way to
convert Grid3D into X,Y,Z for checking.

You can, of course, make Grid3D keep the min and max coordinate values
and let it convert indices into coordinates and values.

V
 
D

Daniel T.

Victor Bazarov said:
Daniel said:
Guardiano del Faro said:
Hello!!

i'm just a dummie in C/C++ and i need you help for solving my
little problem.

I have to create a 3D grid of points. Each point has the 3
coordinates and a boolean value. (I made a structure in order to
make this).

My task is to scan the grid through the x, y and z planes.

According to you, which is the best way to create this data
structure?

I have tried to create a vector of vectors of vectors of point
structure, so to access to each point coordinate i just have to
do like:

Grid[j][k].x

So the i index will give me all the points which are on the x
planes....

but i have now idea about how to build it!!! Does anybody has
any clue?!


My suggestion:

class Grid3D {
std::vector<bool> rep;
unsigned h, w, d;
public:
typedef std::vector<bool>::reference reference;
typedef std::vector<bool>::const_reference const_reference;

Grid3D( unsigned x, unsigned y, unsigned z ):
h( x ), w( y ), d( z ), rep( x * y * z )
{ }

reference at( unsigned x, unsigned y, unsigned z ) {
assert( x < h && y < w && z < d );
return rep[ x*w*d + y*d + z ] != 0;


I am not sure 'reference' will work as the return value here or the
'return' statement should just be

return rep[ x*w*d + y*d + z ];


Good catch. I originally was going to hold a vector of chars to avoid
the vector<bool> partial specialization (which would necessitate '!= 0'
bit,) but then I realized that I would need to create a special class to
return a modifiable reference. Since vector<bool> already has that work
done for me, I decided to switch to vector<bool> but forgot to remove
the '!= 0' bit.

Having a puppy in your lap can be very distracting. :)
}

const_reference at( unsigned x, unsigned y, unsigned z ) const {
assert( x < h && y < w && z < d );
return rep[ x*w*d + y*d + z ] != 0;

I'd probably just do

return rep[ x*w*d + y*d + z ];

here as well.

As above...
 
G

Guardiano del Faro

Victor Bazarov ha scritto:

i solved the problem in a different way!! :D
i realized that that grid needs a lot of space (because it's a
1000x1000x1000 points grid!!) and i can have some memory problems, and
the point is that i dont actually need to memorize the grid, but only
the points which have the boolean value 1!! So i scan a "virtual"grid
(3 for cycles) and i store the points that i need!

Much more easier, simple and light!

thank you for you help!

ViTo
 
D

Daniel T.

Guardiano del Faro said:
Victor Bazarov ha scritto:

i solved the problem in a different way!! :D
i realized that that grid needs a lot of space (because it's a
1000x1000x1000 points grid!!) and i can have some memory problems, and
the point is that i dont actually need to memorize the grid, but only
the points which have the boolean value 1!! So i scan a "virtual"grid
(3 for cycles) and i store the points that i need!

Much more easier, simple and light!

thank you for you help!

For something like that, I would use a set:

struct Vector3 {
unsigned x, y, z;
Vector3(): x(), y(), z() { }
Vector3( unsigned x_, unsigned y_, unsigned z_ ):
x( x_ ), y( y_ ), z( z_ )
{ }
};

bool operator<( const Vector3& left, const Vector3& right )
{
return left.x < right.x ||
left.x == right.x && left.y < right.y ||
left.x == right.x && left.y == right.y && left.z < right.z;
}

class Grid3D {
std::set<Vector3, bool> rep;
unsigned h, w, d;
public:
typedef std::map<Vector3, bool>::reference reference;
typedef std::map<Vector3, bool>::const_reference const_reference;

Grid3D( unsigned x, unsigned y, unsigned z ):
h( x ), w( y ), d( z ), rep( x * y * z )
{ }

const_reference at( unsigned x, unsigned y, unsigned z ) const {
assert( x < h && y < w && z < d );
return rep.find( Vector3( x, y, z ) != rep.end();
}

void set( unsigned x, unsigned y, unsigned z ) {
assert( x < h && y < w && z < d );
rep.insert( Vector3( x, y, z ) );
}

void unset( unsigned x, unsigned y, unsigned z ) {
assert( x < h && y < w && z < d );
rep.erase( Vector3( x, y, z ) );
}
// other functions to taste
};
 
R

r

Guardiano said:
Victor Bazarov ha scritto:

i solved the problem in a different way!! :D
i realized that that grid needs a lot of space (because it's a
1000x1000x1000 points grid!!) and i can have some memory problems, and
the point is that i dont actually need to memorize the grid, but only
the points which have the boolean value 1!! So i scan a "virtual"grid
(3 for cycles) and i store the points that i need!

Much more easier, simple and light!

thank you for you help!

ViTo

You have just rediscovered the "sparse matrix".
 
R

r

Guardiano said:
r ha scritto:



did i win something?! :)

You don't win anything, but you could save yourself a lot of
programming.

The problem you've just solved is a well-known problem in engineering.
So you might want to google: sparse matrix "C++"

and see what you come up with.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top