Main Page | Namespace List | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | Examples

Point.h

00001 /* Copyright 2004
00002 Stanford University
00003 
00004 This file is part of the DSR PDB Library.
00005 
00006 The DSR PDB Library is free software; you can redistribute it and/or modify
00007 it under the terms of the GNU Lesser General Public License as published by
00008 the Free Software Foundation; either version 2.1 of the License, or (at your
00009 option) any later version.
00010 
00011 The DSR PDB Library is distributed in the hope that it will be useful, but
00012 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00013 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00014 License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License
00017 along with the DSR PDB Library; see the file COPYING.LIB.  If not, write to
00018 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00019 MA 02111-1307, USA. */
00020 
00021 #ifndef DSR_PDB_POINT_H
00022 #define DSR_PDB_POINT_H
00023 #include <iostream>
00024 #include <dsrpdb/config.h>
00025 
00026 #ifdef PDB_USE_CGAL 
00027 #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
00028 #include <CGAL/squared_distance_3.h>
00029 namespace dsrpdb {
00030   // An alternative point class.
00031   typedef CGAL::Exact_predicates_inexact_constructions_kernel::Point_3 Point;
00032   typedef CGAL::Exact_predicates_inexact_constructions_kernel::Vector_3 Vector;
00033 
00034   struct Squared_distance{
00035     double operator()(const Point &a, const Point &b) const {
00036       return CGAL::squared_distance(a,b);
00037     }
00038   };
00039 }
00040 
00041 #else 
00042 namespace dsrpdb {
00044 
00047   class Point {
00048   public:
00050     Point(): x_(0), y_(0), z_(0){}
00052     Point(double x, double y, double z): x_(x), y_(y), z_(z){}
00053 
00055     double x() const {return x_;}
00057     double y() const {return y_;}
00058 
00060     double z() const {return z_;}
00061 
00063     double operator[](unsigned int i) const {
00064       switch(i) {
00065       case 0: return x_;
00066       case 1: return y_;
00067       case 2: return z_;
00068       default:
00069         assert(i < 3);
00070         return -1;
00071       }
00072     }
00073 
00075     Point operator-(const Point &o) const {
00076       return Point(x_-o.x_, y_-o.y_, z_-o.z_);
00077     }
00078 
00080     Point operator+(const Point &o) const {
00081       return Point(x_+o.x_, y_+o.y_, z_+o.z_);
00082     }
00083 
00085     Point operator/(double d) const {
00086       return Point(x_/d, y_/d, z_/d);
00087     }
00088 
00090     double operator*(const Point &o) const {
00091       return x_*o.x_ + y_*o.y_ + z_*o.z_;
00092     }
00093   protected:
00094     double x_, y_, z_;
00095   };
00096   
00097   typedef Point Vector;
00098 
00099   inline std::ostream &operator<<(std::ostream &o, const Point &p) {
00100     o << p.x() << " " << p.y() << " " << p.z();
00101     return o;
00102   }
00103 
00104   inline std::istream &operator>>(std::istream &i, Point &p) {
00105     double x,y,z;
00106     i >> x >> y >> z;
00107     if (!i) return i;
00108     p= Point(x,y,z);
00109     return i;
00110   }
00111 
00113   struct Squared_distance{
00114     double operator()(const Point &a, const Point &b) const {
00115       return (a.x()-b.x())*(a.x()-b.x()) 
00116         + (a.y()-b.y())*(a.y()-b.y())
00117         + (a.z()-b.z())*(a.z()-b.z());
00118     }
00119   };
00120   
00121 };
00122 #endif
00123 
00124 #endif