[geos-devel] Coordinate & Coordinate list (20% faster now)

Norman Vine nhv at cape.com
Thu Nov 14 20:44:24 EST 2002


David Blasby writesL

> >  Coordinate *CoordinateList::getAt(int pos)
> >  {
> >      if ((pos>vect->size()-1) || (pos<0) )
> >           throw "Invalid argument: out of bounds\n" ;
> >      else
> >          current=pos;
> >      return &(*vect)[current];
> >  }
> 
> Yes - this is exactly what I'm saying.  There's probably a bunch of other
> places further "up-stream" that would have to be changed as well (so
> you're not copying coordinates there too).
> 
> The reason I'm looking at the CoordinateList and copy Constructor  so
> hard is because your results say it spent 72% of its time there!

Here is a first stab at a Coordinate class that might make the above 
easier to accomplish

Norman


const double DoubleNotANumber = -1;

class Coordinate {
public:
    double data[3];

    enum {PX, PY, PZ};

    inline static Coordinate getNull(void){
        return Coordinate(DoubleNotANumber,DoubleNotANumber,DoubleNotANumber);
    }
    
    Coordinate(){
        setx(0.0);
        sety(0.0);
        setz(DoubleNotANumber);
    }
    Coordinate(double xNew, double yNew, double zNew=DoubleNotANumber){
        setx(xNew);
        sety(yNew);
        setz(zNew);
    }
    Coordinate(const Coordinate& c){
        setx(c.x());
        sety(c.y());
        setz(c.z());
    }
    
    void setCoordinate(Coordinate& other){
        setx(other.x());
        sety(other.y());
        setz(other.z());
    }
    
    void setNull(void){
        setx(DoubleNotANumber);
        sety(DoubleNotANumber);
        setz(DoubleNotANumber);
    }
    
    double x() const { return data[PX]; }
    double y() const { return data[PY]; }
    double z() const { return data[PZ]; }

    void setx(const double& x){ data[PX] = x; }
    void sety(const double& y){ data[PY] = y; }
    void setz(const double& z){ data[PZ] = z; }
    
    double& operator [] (int i){
        assert(! (i < PX || i > PZ));
        return data[i];
    }
    double operator[] (int i) const{
        assert(! (i < PX || i > PZ));
        return data[i];
    }

    inline double *get() { return data; }
    
    bool equals2D(Coordinate& other){
        if (x() != other.x()) return false;
        if (y() != other.y()) return false;
        return true;
    }
    
    bool equals3D(Coordinate& other){
        return (x() == other.x()) && (y() == other.y())  &&
                (( z() == other.z()) ||
                 (z()==DoubleNotANumber && other.z()==DoubleNotANumber)  );
    }   
  
    double distance(Coordinate& p){
        double dx = x() - p.x();
        double dy = y() - p.y();
        return sqrt(dx * dx + dy * dy);
    }

    int compareTo(Coordinate& other){
        if (x() < other.x()) return -1;
        if (x() > other.x()) return 1;
        if (y() < other.y()) return -1;
        if (y() > other.y()) return 1;
        return 0;
    }
    
    void makePrecise();
    
    string toString();
};






More information about the geos-devel mailing list