Results 1 to 5 of 5

Thread: Sort vector of 3D point by X axis

  1. #1
    Join Date
    Aug 2009
    Posts
    98

    Sort vector of 3D point by X axis

    I have a difficulty. I want to sort a vector <PointType> myVector by X axis increasing or decreasing, whatever. PointType is a type defined in ITK and typedef itk::Point<double,3> PointType;

    - Can't we use the function sort of std vector to sort a vector in 3 dimensions based on a component?

    - It would be the quickest way to sort this vector points according to the X axis.

    - I tried to bubble sort but that didn't helped. I am still a novice in C++ so I probably take me wrong level data structures.

    The result does not seem correct to me either?

    void Exchange(vector<PointType> table, const int i, const int j)
    {
    int temp1,temp2,temp3;
    temp1 = table[i][0];
    temp2 = table[i][1];
    temp3 = table[i][2];

    table[i][0] = table[j][0];
    table[i][1] = table[j][1];
    table[i][2] = table[j][2];

    table[j][0] = temp1;
    table[j][1] = temp2;
    table[j][2] = temp3;
    }

    void triABubble(vector<PointType> table,int length)
    {
    bool permutation;
    do
    {
    permutation = false;
    for(int i=0; i<length-1; i++)
    {
    if(table[i][0]>table[i+1][0])
    {
    Exchange(table, i, i+1);
    permutation = true;
    }
    }
    length--;
    }
    while(permutation);
    }

  2. #2
    Join Date
    Nov 2008
    Posts
    1,022

    Re: Sort vector of 3D point by X axis

    To sort your vector you can use this:

    Code:
    std::sort(vector.begin(), vector.end(), myCompareLess);
    with the defined function :
    bool myCompareLess(PointType *p1, PointType *p2)
    {
      // returns true if p1 <p2 based on criteria that you want
    }

  3. #3
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Sort vector of 3D point by X axis

    Can't we use the function sort of std vector to sort a vector in 3 dimensions based on a component?
    Yes, passing a function performing the test on component that interests you.

    For CitricAcid suggestion, it works unless you take references instead of pointers

    Why not just implement the "<" operator in class PointType?

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Sort vector of 3D point by X axis

    Why to use operator < because it would just test only the X axis?

    <time prog. generic>
    the plan would be good to have a class of predicate template type:
    Code:
    template<int Idx> struct order_by_component;
    template<> struct order_by_component<1>
    {
       bool operator ()( PointType const& i,PointType const& j) { return /* test on x */; }
    };
    template<> struct order_by_component<2>
    {
       bool operator ()( PointType const& i,PointType const& j) { return /* test on y */; }
    };
    template<> struct order_by_component<3>
    {
       bool operator ()( PointType const& i,PointType const& j) { return /* test on z */; }
    };
    </ time prog. generic>

  5. #5
    Join Date
    Aug 2009
    Posts
    98

    Re: Sort vector of 3D point by X axis

    Thank you for all your responses. As I mentioned, I am new to c++. So I tested one of the proposed solution with the lot but I do not know how to retrieve evidence;

    bool myCompareLess(PointType pt1, PointType pt2)
    {
    if (pt1[0]<pt2[0])
    return true;
    else
    return false;
    }

    sort(vect.begin(), vect.end()-1, myCompareLess(vect[vect.begin()],vect[vect.begin()+1]);

    I tested this but in vain

Similar Threads

  1. Replies: 1
    Last Post: 12-05-2012, 01:16 PM
  2. Replies: 1
    Last Post: 25-02-2012, 11:31 AM
  3. Replies: 3
    Last Post: 04-01-2011, 01:25 AM
  4. How to sort points_vec vector in c++?
    By kamina23 in forum Software Development
    Replies: 5
    Last Post: 20-02-2010, 04:31 PM
  5. How to sort vector objects by class member variables in c++?
    By Linoo in forum Software Development
    Replies: 4
    Last Post: 29-01-2010, 07:15 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,108,207.22206 seconds with 17 queries