Point.cpp
00001 
00002 
00003 /******************************************************************************
00004 
00005  Copyright 2008 Departamento de Realidad Virtual
00006  y Unidad de Cómputo Aplicado DGSGA, UNAM.
00007 
00008 
00009  This file is part of RBF++.
00010 
00011  RBF++ is free software: you can redistribute it and/or modify
00012  it under the terms of the GNU General Public License as published by
00013  the Free Software Foundation, either version 3 of the License, or
00014  (at your option) any later version.
00015 
00016  RBF++ is distributed in the hope that it will be useful,
00017  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019  GNU General Public License for more details.
00020 
00021  You should have received a copy of the GNU General Public License
00022  along with RBF++. If not, see .
00023 
00024 
00025 *******************************************************************************/
00026 
00027 
00028 /* 
00029  Author: Daniel Cervantes Cabrera.
00030  Project: RBF++
00031  Institution: DGSCA, UNAM.
00032  Date: 3/05/08
00033  Description: Template class base point.
00034  */
00035 
00036 
00037 #include 
00038 
00039 namespace LA{
00040 
00041 
00042 template
00043 Point::Point(){
00044   dim = 0;
00045 }
00046 
00047 
00048 template
00049 Point::Point(int Dim){
00050   _v = new T [Dim];
00051   dim = Dim;
00052   for(int i=0; i < Dim; i++)
00053     _v[i]= 0.0;  
00054 }
00055 
00056 template
00057 Point::Point(const Point& v)
00058 {
00059   dim = v.dimension();  
00060   _v = new T[dim];
00061   
00062   for(int i=0; i< dim; i++)
00063     _v[i] = v[i];    
00064 }
00065 
00066 
00067 
00068 template
00069 Point::~Point()
00070 {
00071   delete [] _v;  
00072 }
00073 
00074 template
00075 void Point::resize(int n){
00076   if(dim !=0)
00077     delete [] _v;
00078   dim = n;
00079   
00080   _v = new T[n];
00081   for(int i=0; i< dim; i++)
00082     _v[i] = 0.0;
00083 }
00084 
00085 
00086 template
00087 T& Point::operator [] (int i){
00088   if(i < 0 && i >= dim)
00089     cout << "Error: index out of range " << endl;
00090   
00091   return _v[i];
00092 }
00093 
00094 template
00095 const T& Point::operator [] (int i) const{
00096   if(i < 0 && i >= dim)
00097     cout << "Error: index out of range " << endl;
00098   
00099   return _v[i];
00100 }
00101 
00102 
00103 template
00104 ostream& operator<<(ostream& s, const Point& v){  
00105   
00106   for(int i=0; i< v.dimension(); i++)
00107     s << v[i] << " ";
00108   return s;
00109 }
00110 
00111 
00112 template
00113 Point& Point::operator=(const  Point& v2)
00114 {  
00115   
00116    if(this != &v2)
00117     for(int i=0; i< dim; i++)
00118       _v[i] = v2[i];
00119   
00120   return *this;
00121   
00122 }
00123 
00124 template 
00125 Point  operator-(const Point& v1, const Point& v2)  
00126 {
00127   
00128   Point v3;
00129   
00130   if(v1.dimension() != v2.dimension()){
00131     cout << "Error: dimensions must be equals"<< endl;
00132     return v3;
00133   }
00134   
00135   v3.resize(v2.dim);
00136   
00137   for(int i=0; i< v2.dim; i++)
00138     v3[i] = v1[i]-v2[i];
00139   
00140   return v3;           
00141 }             
00142 
00143 template 
00144 Point  operator+(const Point& v1, const Point& v2)  
00145 {
00146   
00147   Point v3;
00148   
00149   if(v1.dim != v2.dim){
00150     cout << "Error: dimensions must be equals"<< endl;
00151     return v3;
00152   }
00153   
00154   v3.resize(v2.dim);
00155   
00156   for(int i=0; i< v2.dim; i++)
00157     v3[i] = v1[i]+v2[i];
00158   
00159   return v3;          
00160   
00161 }             
00162 
00163 
00164 
00165 template    
00166 Point operator*(const U & c, const Point& v )
00167 {
00168   Point vt(v.dim);
00169   
00170   for(int i=0; i    
00178 Point operator*(const Point& v, const U & c)
00179 {
00180   Point vt(v.dim);
00181   
00182   for(int i=0; i    
00191 U operator*(const Point& v1, const Point & v2 )
00192 {
00193 
00194   if(v1.dim != v2.dim){
00195     cout << "Error: dimensions must be equals"<< endl;
00196     return 0;
00197   }
00198 
00199   
00200   U temp = v1[0]*v2[0];
00201   for(int i=1; i    
00210 Point operator+=( Point & v1,const Point & v2 )
00211 {
00212   
00213   if(v1.dim != v2.dim){
00214     cout << "Error: dimensions must be equals"<< endl;
00215     return v1;
00216   }
00217 
00218     for(int i=0; i