Help with a 3D Grid\

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guardiano del Faro

    Help with a 3D Grid\

    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

    #2
    Re: Help with a 3D Grid\

    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


    Comment

    • Steven T. Hatton

      #3
      Re: Help with a 3D Grid\

      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/

      Comment

      • BobR

        #4
        Re: Help with a 3D Grid\


        Guardiano del Faro wrote in message
        <1166722407.664 509.168810@42g2 000cwt.googlegr oups.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<iostr eam>, <vector>
        // ------------------------------------
        void TriVector( std::ostream &cout ){
        typedef std::vector<std ::vector<std::v ector<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).siz e(); ++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).a t(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).a t(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


        Comment

        • Daniel T.

          #5
          Re: Help with a 3D Grid\

          In article <1166722407.664 509.168810@42g2 000cwt.googlegr oups.com>,
          "Guardiano del Faro" <vito.baldassar re@gmail.comwro te:
          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<boo lrep;
          unsigned h, w, d;
          public:
          typedef std::vector<boo l>::reference reference;
          typedef std::vector<boo l>::const_refer ence 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. :-)

          Comment

          • Guardiano del Faro

            #6
            Re: Help with a 3D Grid\


            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

            Comment

            • Victor Bazarov

              #7
              Re: Help with a 3D Grid\

              Daniel T. wrote:
              In article <1166722407.664 509.168810@42g2 000cwt.googlegr oups.com>,
              "Guardiano del Faro" <vito.baldassar re@gmail.comwro te:
              >
              >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<boo lrep;
              unsigned h, w, d;
              public:
              typedef std::vector<boo l>::reference reference;
              typedef std::vector<boo l>::const_refer ence 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


              Comment

              • Victor Bazarov

                #8
                Re: Help with a 3D Grid\

                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 ) = CheckIfInsidePr otein(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


                Comment

                • Daniel T.

                  #9
                  Re: Help with a 3D Grid\

                  "Victor Bazarov" <v.Abazarov@com Acast.netwrote:
                  Daniel T. wrote:
                  "Guardiano del Faro" <vito.baldassar re@gmail.comwro te:
                  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<boo lrep;
                  unsigned h, w, d;
                  public:
                  typedef std::vector<boo l>::reference reference;
                  typedef std::vector<boo l>::const_refer ence 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<boolpart ial 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<boolalre ady 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...

                  Comment

                  • Guardiano del Faro

                    #10
                    Re: Help with a 3D Grid\


                    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"gr id
                    (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

                    Comment

                    • Daniel T.

                      #11
                      Re: Help with a 3D Grid\

                      "Guardiano del Faro" <vito.baldassar re@gmail.comwro te:
                      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"gr id
                      (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<Vector 3, boolrep;
                      unsigned h, w, d;
                      public:
                      typedef std::map<Vector 3, bool>::referenc e reference;
                      typedef std::map<Vector 3, bool>::const_re ference 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
                      };

                      Comment

                      • r

                        #12
                        Re: Help with a 3D Grid\

                        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"gr id
                        (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".

                        Comment

                        • Guardiano del Faro

                          #13
                          Re: Help with a 3D Grid\


                          r ha scritto:

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

                          Comment

                          • r

                            #14
                            Re: Help with a 3D Grid\

                            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.

                            Comment

                            Working...