Velocity Reviews > C++ > Help with a 3D Grid\

# Help with a 3D Grid\

Guardiano del Faro
Guest
Posts: n/a

 12-21-2006
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[i][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

Victor Bazarov
Guest
Posts: n/a

 12-21-2006
Guardiano del Faro wrote:
> 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[i][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
--

Steven T. Hatton
Guest
Posts: n/a

 12-21-2006
Guardiano del Faro wrote:

> 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[i][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.
--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/

BobR
Guest
Posts: n/a

 12-21-2006

Guardiano del Faro wrote in message
<(E-Mail Removed). com>...
>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[i][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: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:stream&)
// ------------------------------------

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

--
Bob R
POVrookie

Daniel T.
Guest
Posts: n/a

 12-22-2006
In article <(E-Mail Removed). com>,
"Guardiano del Faro" <(E-Mail Removed)> wrote:

> 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[i][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.

Guardiano del Faro
Guest
Posts: n/a

 12-22-2006

Victor Bazarov ha scritto:

> Guardiano del Faro wrote:
>

Hello everybody and thanks for your help!

> How to build what?

the data structure!
>
> > Does anybody has any clue?!

> 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

Victor Bazarov
Guest
Posts: n/a

 12-22-2006
Daniel T. wrote:
> In article <(E-Mail Removed). com>,
> "Guardiano del Faro" <(E-Mail Removed)> wrote:
>
>> 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[i][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
--

Victor Bazarov
Guest
Posts: n/a

 12-22-2006
Guardiano del Faro wrote:
> Victor Bazarov ha scritto:
>
>> Guardiano del Faro wrote:
>>

> Hello everybody and thanks for your help!
>
>> How to build what?

>
> the data structure!
>>
>>> Does anybody has any clue?!

>
>> 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
--

Daniel T.
Guest
Posts: n/a

 12-22-2006
"Victor Bazarov" <(E-Mail Removed)> wrote:
> Daniel T. wrote:
> > "Guardiano del Faro" <(E-Mail Removed)> wrote:
> >
> > > 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[i][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...

Guardiano del Faro
Guest
Posts: n/a

 12-22-2006

Victor Bazarov ha scritto:

i solved the problem in a different way!!
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