Main Page | User's guide | Class Hierarchy | Class List | File List | 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 ( slopeAB == 0.0 )
00077       {
00078         slopeCD = D(0) - C(0);
00079         if ( slopeCD == 0.0 )
00080           return false;
00081         slopeCD = (D(1) - C(1)) / slopeCD;      
00082         T y = slopeCD * (A(0) - C(0)) + C(1);
00083         if (slopeCD < 0.0)
00084           return ( ( (y>A(1)) || (y>=B(1)) ) && ( (y<A(1)) || (y<=B(1)) )
00085                    && ( (B(0) >= D(0)) || (B(0) > C(0)) )
00086                    && ( (B(0) < C(0)) || (B(0) <= D(0)) ) );
00087         else
00088           return ( ( (y>=A(1)) || (y>B(1)) ) && ( (y<=A(1)) || (y<B(1)) )
00089                    && ( (A(0) >= D(0)) || (A(0) > C(0)) )
00090                    && ( (A(0) < C(0)) || (A(0) <= D(0)) ) );
00091       }
00092     else
00093       {
00094         slopeAB = (B(1) - A(1)) / slopeAB;
00095         slopeCD = D(0) - C(0);
00096         if ( slopeCD == 0.0 )
00097           {
00098             T y = slopeAB * (C(0) - A(0)) + A(1);
00099             if (slopeAB > 0.0)
00100               return ( ( (y > C(1)) || (y >= D(1)) ) && ( (y < C(1)) || (y <= D(1)) )
00101                        && ((D(0)>A(0)) || (D(0)>=B(0))) && ((D(0)<A(0)) || (D(0)<=B(0))) );
00102             else
00103               return ( ( (y > C(1)) || (y >= D(1)) ) && ( (y < C(1)) || (y <= D(1)) )
00104                        && ((D(0)>=A(0)) || (D(0)>B(0))) && ((D(0)<=A(0)) || (D(0)<B(0))) );
00105           }
00106         else
00107           {
00108             slopeCD = (D(1) - C(1)) / slopeCD;
00109             if ( slopeAB - slopeCD == 0.0 )
00110               return false;
00111             T x = ( A(1) - slopeAB * A(0) - C(1) + slopeCD * C(0) )
00112               / (slopeCD - slopeAB);
00113             if (slopeAB > slopeCD)
00114               return ( ( (x > C(0)) || (x >= D(0)) ) && ( (x < C(0)) || (x <= D(0)) )
00115                        && ( (x>A(0)) || (x>=B(0)) ) && ( (x<A(0)) || (x<=B(0))) );
00116             else
00117               return ( ( (x > C(0)) || (x >= D(0)) ) && ( (x < C(0)) || (x <= D(0)) )
00118                        && ( (x>=A(0)) || (x>B(0)) ) && ( (x<=A(0)) || (x<B(0))) );
00119           }
00120       }
00121 
00122   }
00123 
00124 
00125 
00127   //  C U R V E  //
00129 
00130 
00131   template <class T>
00132   class Curve: public SpaceTown
00133   {
00134 
00135     /************************
00136      * TYPEDEF DECLARATIONS *
00137      ************************/
00138 
00139   public:
00140     typedef T value_type;
00141     typedef T* pointer;
00142     typedef const T* const_pointer;
00143     typedef T& reference;
00144     typedef const T& const_reference;
00145 
00146 
00147     /**************
00148      * ATTRIBUTES *
00149      **************/
00150 
00151   protected:
00153     int n_;
00155     List<Vector<T> > points_;
00158     int orientation_;
00159 
00160 
00161     /***********
00162      * METHODS *
00163      ***********/
00164 
00165   public:
00166     // Constructors.
00167     Curve()  throw();
00168   
00169     // Destructor.
00170     ~Curve()  throw();
00171 
00172     // Basic methods.
00173     void InitWithUnsortedPoints_ClosestPointMethod(List<Vector<T> >& points);
00174     void AddPoint(Vector<T>& point);
00175 
00176     void Copy(Curve<T>& C);
00177 
00178     int GetNbPoints() const;
00179     List<Vector<T> >& GetPoints();
00180 
00181     int GetOrientation() const;
00182     int SetOrientation(int orientation);
00183 
00184     void ClearAll();
00185 
00186     T GetDistanceToTheCurve(T x, T y);
00187     T GetProjection(Vector<T>& X);
00188     T GetSignedDistanceToTheCurve(T x, T y);
00189 
00190     // Input/output methods.
00191     void WriteText(ofstream& f);
00192     void ReadText(string file_name, int orientation = -1);
00193 
00194   };  // Curve.
00195 
00196 
00197 }  // namespace Multivac.
00198 
00199 
00200 #define FILE_CURVE_HXX
00201 #endif

Generated on Mon Apr 19 01:59:14 2004 for Multivac by doxygen 1.3.6-20040222