Main Page | User's Guide | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members

curve.hxx

00001 // Copyright (C) 2002-2004 Vivien Mallet
00002 //
00003 // This file is part of Multivac library.
00004 // Multivac library provides front-tracking algorithms.
00005 // 
00006 // Multivac is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 2 of the License, or
00009 // (at your option) any later version.
00010 // 
00011 // Multivac is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License (file "license") for more details.
00015 //
00016 // For more information, please see the Multivac home page:
00017 //     http://spacetown.free.fr/fronts/
00018 
00019 
00020 #ifndef FILE_CURVE_HXX
00021 
00022 #include <stdio.h>
00023 #include <math.h>
00024 
00025 #include <string>
00026 #include <sstream>
00027 #include <fstream>
00028 
00029 using std::string;
00030 using std::istringstream;
00031 using std::ifstream;
00032 using std::getline;
00033 
00034 
00035 namespace Multivac
00036 {
00037 
00038 
00040 
00045   template <class T>
00046   T DistanceBetween(Vector<T>& A, Vector<T>& B)
00047   {
00048 
00049     T tempx = A(0) - B(0);
00050     T tempy = A(1) - B(1);
00051 
00052     return sqrt( tempx*tempx + tempy*tempy );
00053 
00054   }
00055 
00056 
00058 
00067   template <class T>
00068   bool Intersect(Vector<T>& A, Vector<T>& B,
00069                  Vector<T>& C, Vector<T>& D)
00070   {
00071 
00072     T slopeAB, slopeCD;
00073 
00074     slopeAB = B(0) - A(0);
00075 
00076     if (B(0) == D(0) && B(1) == D(1))
00077       return true;
00078     else if ( slopeAB == 0.0 )
00079       {
00080         slopeCD = D(0) - C(0);
00081         if ( slopeCD == 0.0 )
00082           return false;
00083         slopeCD = (D(1) - C(1)) / slopeCD;      
00084         T y = slopeCD * (A(0) - C(0)) + C(1);
00085         return ( ( (y > A(1) && y <= B(1)) || (y < A(1) && y >= B(1))
00086                    || (y == A(1) && y == B(1)) )
00087                  && ( (C(0) > A(0) && D(0) <= A(0))
00088                       || (C(0) < A(0) && D(0) >= A(0)) ) );
00089       }
00090     else
00091       {
00092         slopeAB = (B(1) - A(1)) / slopeAB;
00093         slopeCD = D(0) - C(0);
00094         if ( slopeCD == 0.0 )
00095           {
00096             T y = slopeAB * (C(0) - A(0)) + A(1);
00097             return ( ( (y > C(1) && y <= D(1)) || (y < C(1) && y >= D(1))
00098                        || (y == C(1) && y == D(1)) )
00099                      && ( (D(0) > A(0) && D(0) <= B(0))
00100                           || (D(0) < A(0) && D(0) >= B(0)) ) );
00101           }
00102         else
00103           {
00104             slopeCD = (D(1) - C(1)) / slopeCD;
00105             if ( slopeAB - slopeCD == 0.0 )
00106               return false;
00107             T x = ( A(1) - slopeAB * A(0) - C(1) + slopeCD * C(0) )
00108               / (slopeCD - slopeAB);
00109             return ( ( (x > C(0) && x <= D(0)) || (x < C(0) && x >= D(0))
00110                        || (x == C(0) && x == D(0)) )
00111                      && ( (x > A(0) && x <= B(0))
00112                           || (x < A(0) && x >= B(0)) ) );
00113           }
00114       }
00115 
00116   }
00117 
00118 
00119 
00121   //  C U R V E  //
00123 
00124 
00125   template <class T>
00126   class Curve: public SpaceTown
00127   {
00128 
00129     /************************
00130      * TYPEDEF DECLARATIONS *
00131      ************************/
00132 
00133   public:
00134     typedef T value_type;
00135     typedef T* pointer;
00136     typedef const T* const_pointer;
00137     typedef T& reference;
00138     typedef const T& const_reference;
00139 
00140 
00141     /**************
00142      * ATTRIBUTES *
00143      **************/
00144 
00145   protected:
00147     int n_;
00149     List<Vector<T> > points_;
00152     int orientation_;
00153 
00154 
00155     /***********
00156      * METHODS *
00157      ***********/
00158 
00159   public:
00160     // Constructors.
00161     Curve()  throw();
00162   
00163     // Destructor.
00164     ~Curve()  throw();
00165 
00166     // Basic methods.
00167     void InitWithUnsortedPoints_ClosestPointMethod(List<Vector<T> >& points);
00168     void AddPoint(Vector<T>& point);
00169 
00170     void Copy(Curve<T>& C);
00171 
00172     int GetNbPoints() const;
00173     List<Vector<T> >& GetPoints();
00174 
00175     int GetOrientation() const;
00176     int SetOrientation(int orientation);
00177 
00178     void ClearAll();
00179 
00180     T GetDistanceToTheCurve(T x, T y);
00181     T GetProjection(Vector<T>& X);
00182     T GetSignedDistanceToTheCurve(T x, T y);
00183 
00184     // Input/output methods.
00185     void WriteText(ofstream& f);
00186     void ReadText(string file_name, int orientation = -1);
00187 
00188   };  // Curve.
00189 
00190 
00191 }  // namespace Multivac.
00192 
00193 
00194 #define FILE_CURVE_HXX
00195 #endif

Generated on Sun Jan 16 23:37:57 2005 for Multivac by  doxygen 1.4.0