By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,125 Members | 921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,125 IT Pros & Developers. It's quick & easy.

Help with a 3D Grid\

P: n/a
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

Dec 21 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 21 '06 #2

P: n/a
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/
Dec 21 '06 #3

P: n/a

Guardiano del Faro wrote in message
<11**********************@42g2000cwt.googlegroups. 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::ostream &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::ostream&)
// ------------------------------------

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

--
Bob R
POVrookie
Dec 21 '06 #4

P: n/a
In article <11**********************@42g2000cwt.googlegroups. com>,
"Guardiano del Faro" <vi**************@gmail.comwrote:
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<boolrep;
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. :-)
Dec 22 '06 #5

P: n/a

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

Dec 22 '06 #6

P: n/a
Daniel T. wrote:
In article <11**********************@42g2000cwt.googlegroups. com>,
"Guardiano del Faro" <vi**************@gmail.comwrote:
>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<boolrep;
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 22 '06 #7

P: n/a
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 22 '06 #8

P: n/a
"Victor Bazarov" <v.********@comAcast.netwrote:
Daniel T. wrote:
"Guardiano del Faro" <vi**************@gmail.comwrote:
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<boolrep;
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<boolpartial 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<boolalready has that work
done for me, I decided to switch to vector<boolbut 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...
Dec 22 '06 #9

P: n/a

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
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 22 '06 #10

P: n/a
"Guardiano del Faro" <vi**************@gmail.comwrote:
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, boolrep;
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
};
Dec 22 '06 #11

P: n/a
r
Guardiano del Faro wrote:
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".

Dec 22 '06 #12

P: n/a

r ha scritto:

>
You have just rediscovered the "sparse matrix".
did i win something?! :)

Dec 23 '06 #13

P: n/a
r
Guardiano del Faro wrote:
r ha scritto:


You have just rediscovered the "sparse matrix".

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.

Dec 23 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.