SyFi  0.3
Ptv.cpp
Go to the documentation of this file.
00001 // Copyright (C) 2006-2009 Kent-Andre Mardal and Simula Research Laboratory
00002 //
00003 // This file is part of SyFi.
00004 //
00005 // SyFi is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 2 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // SyFi is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with SyFi. If not, see <http://www.gnu.org/licenses/>.
00017 
00018 #include "Ptv.h"
00019 #include <iostream>
00020 #include <math.h>
00021 
00022 using std::ostream;
00023 using std::endl;
00024 
00025 double Ptv::tol = 1.0e-9;
00026 double Ptv_match::tol = 1.0e-9;
00027 //double Ptv::tol = .0;
00028 //double Ptv_match::tol = .0;
00029 
00030 Ptv::Ptv() : dim(0)
00031 {
00032         v = new double[0];
00033 }
00034 
00035 
00036 Ptv::Ptv(double x, double y)
00037 {
00038         dim = 2;
00039         v = new double[2];
00040         v[0] = x;
00041         v[1] = y;
00042 }
00043 
00044 
00045 Ptv::Ptv(double x, double y, double z)
00046 {
00047         dim = 3;
00048         v = new double[3];
00049         v[0] = x;
00050         v[1] = y;
00051         v[2] = z;
00052 }
00053 
00054 
00055 Ptv::Ptv(unsigned int size_)
00056 {
00057         dim = size_;
00058         v = new double[dim];
00059         for (unsigned int i=0; i< dim; i++)
00060         {
00061                 v[i] = 0.0;
00062         }
00063 }
00064 
00065 
00066 // FIXME:
00067 // The constructor which takes int, double* could/should work
00068 // on the double* provided instead of creating a copy.
00069 // This however affects the destructor. Since Ptv should
00070 // not delete memory.
00071 // We could introduce a bool external_storage in Ptv which
00072 // is used as a test in the destructor.
00073 
00074 Ptv::Ptv(unsigned int size_, double* v_)
00075 {
00076         dim = size_;
00077         v = new double[dim];
00078         for (unsigned int i=0; i< dim; i++)
00079         {
00080                 v[i] = v_[i];
00081         }
00082 }
00083 
00084 
00085 Ptv::Ptv(const Ptv& p)
00086 {
00087         dim = p.size();
00088         v = new double[dim];
00089         for (unsigned int i=0; i< dim; i++)
00090         {
00091                 v[i] = p[i];
00092         }
00093 
00094 }
00095 
00096 
00097 Ptv::~Ptv()
00098 {
00099         delete [] v;
00100 }
00101 
00102 
00103 void Ptv::redim(unsigned int size_, double* v_)
00104 {
00105         if (dim != size_ )
00106         {
00107                 delete [] v;
00108                 dim = size_;
00109                 v = new double[dim];
00110         }
00111 
00112         for (unsigned int i=0; i< dim; i++)
00113         {
00114                 v[i] = v_[i];
00115         }
00116 }
00117 
00118 
00119 void Ptv::redim(unsigned int size_)
00120 {
00121         if (dim != size_ )
00122         {
00123                 delete [] v;
00124                 dim = size_;
00125                 v = new double[dim];
00126         }
00127         for (unsigned int i=0; i< dim; i++)
00128         {
00129                 v[i] = 0.0;
00130         }
00131 }
00132 
00133 
00134 void Ptv::fill(double* v_)
00135 {
00136         for (unsigned int i=0; i< dim; i++)
00137         {
00138                 v[i] = v_[i];
00139         }
00140 }
00141 
00142 
00143 const unsigned int Ptv::size() const { return dim;}
00144 
00145 const double& Ptv::operator [] (unsigned int i) const
00146 {
00147         return v[i];
00148 }
00149 
00150 
00151 double& Ptv::operator [] (unsigned int i)
00152 {
00153         return v[i];
00154 }
00155 
00156 
00157 Ptv& Ptv::operator = (const Ptv& p)
00158 {
00159         if ( this != &p)
00160         {
00161                 if ( dim != p.size())
00162                 {
00163                         delete [] v;
00164                         dim = p.size();
00165                         v = new double[dim];
00166                 }
00167                 for (unsigned int i=0; i< dim; i++)
00168                 {
00169                         v[i] = p[i];
00170                 }
00171         }
00172         return *this;
00173 }
00174 
00175 
00176 bool Ptv::less(const Ptv& p) const
00177 {
00178 
00179         if ( dim <  p.size() ) return true ;
00180         if ( dim >  p.size() ) return false;
00181 
00182         /* 
00183         for (int i=dim-1; i>= 0; i--) {
00184           if ( fabs(v[i] - p[i]) > tol ) {
00185                 if (v[i] < p[i])
00186                   return true;
00187                 else
00188         return false;
00189         }
00190         }
00191         */
00192 
00193         for (int i=dim-1; i>= 0; i--)
00194         {
00195                 if ( v[i] + tol >= p[i] - tol &&  v[i] - tol <= p[i] + tol )
00196                 {
00197                 }
00198                 else if (v[i] + tol  < p[i] - tol  )
00199                 {
00200                         return true;
00201                 }
00202                 else if ( v[i] - tol > p[i] + tol  )
00203                 {
00204                         return false;
00205                 }
00206         }
00207 
00208         return false;
00209 }
00210 
00211 
00212 ostream & operator<< ( ostream& os, const Ptv& p)
00213 {
00214         if (p.size() >= 1)
00215         {
00216                 os <<"[";
00217                 for (unsigned int i=0; i< p.size()-1; i++)
00218                 {
00219                         os <<p[i]<<",";
00220                 }
00221                 os <<p[p.size()-1]<<"]";
00222         }
00223         else
00224         {
00225                 os <<"Ptv not created properly"<<endl;
00226         }
00227         return os;
00228 }
00229 
00230 
00231 Ptv_match::Ptv_match()
00232 {
00233         d = 0 ; v = 0.0;
00234 }
00235 
00236 
00237 Ptv_match::Ptv_match(unsigned int d_, double v_)
00238 {
00239         d = d_ ; v = v_;
00240 }
00241 
00242 
00243 bool Ptv_match:: operator () (const Ptv &p)
00244 {
00245         if ( v + tol >= p[d] && v - tol <= p[d] ) return true;
00246         else return false;
00247 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines