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), v(0)
00031 {
00032 }
00033 
00034 
00035 Ptv::Ptv(double x, double y)
00036 {
00037         dim = 2;
00038         v = new double[2];
00039         v[0] = x;
00040         v[1] = y;
00041 }
00042 
00043 
00044 Ptv::Ptv(double x, double y, double z)
00045 {
00046         dim = 3;
00047         v = new double[3];
00048         v[0] = x;
00049         v[1] = y;
00050         v[2] = z;
00051 }
00052 
00053 
00054 Ptv::Ptv(unsigned int size_)
00055 {
00056         dim = size_;
00057         v = new double[dim];
00058         for (unsigned int i=0; i< dim; i++)
00059         {
00060                 v[i] = 0.0;
00061         }
00062 }
00063 
00064 
00065 // FIXME:
00066 // The constructor which takes int, double* could/should work
00067 // on the double* provided instead of creating a copy.
00068 // This however affects the destructor. Since Ptv should
00069 // not delete memory.
00070 // We could introduce a bool external_storage in Ptv which
00071 // is used as a test in the destructor.
00072 
00073 Ptv::Ptv(unsigned int size_, double* v_)
00074 {
00075         dim = size_;
00076         v = new double[dim];
00077         for (unsigned int i=0; i< dim; i++)
00078         {
00079                 v[i] = v_[i];
00080         }
00081 }
00082 
00083 
00084 Ptv::Ptv(const Ptv& p)
00085 {
00086         dim = p.size();
00087         v = new double[dim];
00088         for (unsigned int i=0; i< dim; i++)
00089         {
00090                 v[i] = p[i];
00091         }
00092 
00093 }
00094 
00095 
00096 Ptv::~Ptv()
00097 {
00098         if (v)
00099         {
00100                 delete [] v;
00101                 v = 0;
00102         }
00103 }
00104 
00105 
00106 void Ptv::redim(unsigned int size_, double* v_)
00107 {
00108         if (dim != size_ )
00109         {
00110                 if (v) delete [] v;
00111                 dim = size_;
00112                 v = new double[dim];
00113         }
00114 
00115         for (unsigned int i=0; i< dim; i++)
00116         {
00117                 v[i] = v_[i];
00118         }
00119 }
00120 
00121 
00122 void Ptv::redim(unsigned int size_)
00123 {
00124         if (dim != size_ )
00125         {
00126                 if (v) delete [] v;
00127                 dim = size_;
00128                 v = new double[dim];
00129         }
00130         for (unsigned int i=0; i< dim; i++)
00131         {
00132                 v[i] = 0.0;
00133         }
00134 }
00135 
00136 
00137 void Ptv::fill(double* v_)
00138 {
00139         for (unsigned int i=0; i< dim; i++)
00140         {
00141                 v[i] = v_[i];
00142         }
00143 }
00144 
00145 
00146 const unsigned int Ptv::size() const { return dim;}
00147 
00148 const double& Ptv::operator [] (unsigned int i) const
00149 {
00150         return v[i];
00151 }
00152 
00153 
00154 double& Ptv::operator [] (unsigned int i)
00155 {
00156         return v[i];
00157 }
00158 
00159 
00160 Ptv& Ptv::operator = (const Ptv& p)
00161 {
00162         if ( this != &p)
00163         {
00164                 if ( dim != p.size())
00165                 {
00166                         if (v) delete [] v;
00167                         dim = p.size();
00168                         v = new double[dim];
00169                 }
00170                 for (unsigned int i=0; i< dim; i++)
00171                 {
00172                         v[i] = p[i];
00173                 }
00174         }
00175         return *this;
00176 }
00177 
00178 
00179 bool Ptv::less(const Ptv& p) const
00180 {
00181 
00182         if ( dim <  p.size() ) return true ;
00183         if ( dim >  p.size() ) return false;
00184 
00185         /*
00186         for (int i=dim-1; i>= 0; i--) {
00187           if ( fabs(v[i] - p[i]) > tol ) {
00188                 if (v[i] < p[i])
00189                   return true;
00190                 else
00191         return false;
00192         }
00193         }
00194         */
00195 
00196         for (int i=dim-1; i>= 0; i--)
00197         {
00198                 if ( v[i] + tol >= p[i] - tol &&  v[i] - tol <= p[i] + tol )
00199                 {
00200                 }
00201                 else if (v[i] + tol  < p[i] - tol  )
00202                 {
00203                         return true;
00204                 }
00205                 else if ( v[i] - tol > p[i] + tol  )
00206                 {
00207                         return false;
00208                 }
00209         }
00210 
00211         return false;
00212 }
00213 
00214 
00215 ostream & operator<< ( ostream& os, const Ptv& p)
00216 {
00217         if (p.size() >= 1)
00218         {
00219                 os <<"[";
00220                 for (unsigned int i=0; i< p.size()-1; i++)
00221                 {
00222                         os <<p[i]<<",";
00223                 }
00224                 os <<p[p.size()-1]<<"]";
00225         }
00226         else
00227         {
00228                 os <<"Ptv not created properly"<<endl;
00229         }
00230         return os;
00231 }
00232 
00233 
00234 Ptv_match::Ptv_match()
00235 {
00236         d = 0 ; v = 0.0; // FIXME: What is this?
00237 }
00238 
00239 
00240 Ptv_match::Ptv_match(unsigned int d_, double v_)
00241 {
00242         d = d_ ; v = v_;
00243 }
00244 
00245 
00246 bool Ptv_match:: operator () (const Ptv &p)
00247 {
00248         if ( v + tol >= p[d] && v - tol <= p[d] ) return true;
00249         else return false;
00250 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator