Help with a 3D Grid\

Discussion in 'C++' started by Guardiano del Faro, Dec 21, 2006.

  1. 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
    Guardiano del Faro, Dec 21, 2006
    #1
    1. Advertising

  2. 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[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
    Victor Bazarov, Dec 21, 2006
    #2
    1. Advertising

  3. 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[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/
    Steven T. Hatton, Dec 21, 2006
    #3
  4. Guardiano del Faro

    BobR Guest

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

    --
    Bob R
    POVrookie
    BobR, Dec 21, 2006
    #4
  5. Guardiano del Faro

    Daniel T. Guest

    In article <>,
    "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[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. :)
    Daniel T., Dec 22, 2006
    #5
  6. 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
    Guardiano del Faro, Dec 22, 2006
    #6
  7. Daniel T. wrote:
    > In article <>,
    > "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[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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Dec 22, 2006
    #7
  8. 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
    Victor Bazarov, Dec 22, 2006
    #8
  9. Guardiano del Faro

    Daniel T. Guest

    "Victor Bazarov" <> wrote:
    > Daniel T. wrote:
    > > "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[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...
    Daniel T., Dec 22, 2006
    #9
  10. 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
    Guardiano del Faro, Dec 22, 2006
    #10
  11. Guardiano del Faro

    Daniel T. Guest

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


    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
    };
    Daniel T., Dec 22, 2006
    #11
  12. Guardiano del Faro

    r Guest

    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".
    r, Dec 22, 2006
    #12
  13. r ha scritto:


    >
    > You have just rediscovered the "sparse matrix".


    did i win something?! :)
    Guardiano del Faro, Dec 23, 2006
    #13
  14. Guardiano del Faro

    r Guest

    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.
    r, Dec 23, 2006
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Girish
    Replies:
    4
    Views:
    667
    Patrick.O.Ige
    Jul 19, 2005
  2. =?Utf-8?B?QXNzYW5LaGFuIElzbWFpbA==?=

    How to transfer a value from an Invisible Grid to an visible Grid

    =?Utf-8?B?QXNzYW5LaGFuIElzbWFpbA==?=, Oct 13, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    463
    =?Utf-8?B?QXNzYW5LaGFuIElzbWFpbA==?=
    Oct 17, 2005
  3. Danny Tuppeny

    Grouped grid? Or grid within datalist?

    Danny Tuppeny, Oct 27, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    495
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    Oct 27, 2005
  4. Marc
    Replies:
    0
    Views:
    450
  5. Simon L
    Replies:
    3
    Views:
    402
    Jeff Flinn
    Mar 31, 2006
Loading...

Share This Page