Utils.h

00001 //===========================================================================
00002 // GoTools - SINTEF Geometry Tools version 1.1
00003 //
00004 // GoTools module: CORE
00005 //
00006 // Copyright (C) 2000-2007 SINTEF ICT, Applied Mathematics, Norway.
00007 //
00008 // This program is free software; you can redistribute it and/or          
00009 // modify it under the terms of the GNU General Public License            
00010 // as published by the Free Software Foundation version 2 of the License. 
00011 //
00012 // This program is distributed in the hope that it will be useful,        
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of         
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          
00015 // GNU General Public License for more details.                           
00016 //
00017 // You should have received a copy of the GNU General Public License      
00018 // along with this program; if not, write to the Free Software            
00019 // Foundation, Inc.,                                                      
00020 // 59 Temple Place - Suite 330,                                           
00021 // Boston, MA  02111-1307, USA.                                           
00022 //
00023 // Contact information: E-mail: tor.dokken@sintef.no                      
00024 // SINTEF ICT, Department of Applied Mathematics,                         
00025 // P.O. Box 124 Blindern,                                                 
00026 // 0314 Oslo, Norway.                                                     
00027 //
00028 // Other licenses are also available for this software, notably licenses
00029 // for:
00030 // - Building commercial software.                                        
00031 // - Building software whose source code you wish to keep private.        
00032 //===========================================================================
00033 #ifndef _GOUTILS_H
00034 #define _GOUTILS_H
00035 
00036 
00037 #include "SplineCurve.h"
00038 #include "errormacros.h"
00039 #include <math.h>
00040 #include <algorithm>
00041 #include <ctype.h>
00042 
00043 
00044 namespace Go
00045 {
00048 
00049 
00050 
00053 
00054 template <class Iterator>
00055 struct go_iterator_traits {
00056   typedef typename Iterator::value_type        value_type;
00057   typedef typename Iterator::difference_type   difference_type;
00058   typedef typename Iterator::pointer           pointer;
00059   typedef typename Iterator::reference         reference;
00060 };
00061 
00062 // #ifndef _MSC_VER
00063 template <class T>
00064 struct go_iterator_traits<T*> {
00065   typedef T                          value_type;
00066   typedef int                        difference_type;
00067   typedef T*                         pointer;
00068   typedef T&                         reference;
00069 };
00070 
00071 
00072 template <class T>
00073 struct go_iterator_traits<const T*> {
00074   typedef T                          value_type;
00075   typedef int                        difference_type;
00076   typedef const T*                   pointer;
00077   typedef const T&                   reference;
00078 };
00079 // #endif // !MSC_VER
00080 
00081 #ifdef _MSC_VER
00082 
00084     inline double
00085         sum(double* first,
00086             double* last)
00087         {
00088             double sum = 0.0;
00089             for (; first != last; ++first)
00090                 sum += *first;
00091             return sum;
00092         }
00093 
00095     inline double
00096         sum_squared(double* first,
00097                     double* last)
00098         {
00099             double sum = 0.0;
00100             for (; first != last; ++first)
00101                 sum += (*first)*(*first);
00102             return sum;
00103         }
00105     inline double
00106         distance_squared(const double* first1,
00107                          const double* last1,
00108                          const double* first2)
00109         {
00110             double sum = 0;
00111             for (; first1 != last1; ++first1, ++first2)
00112                 sum += (*first1 - *first2)*(*first1 - *first2);
00113             return sum;
00114         }    
00115 
00117     inline void
00118         normalize(double* first,
00119                   double* last)
00120         {
00121             double d
00122                 = sqrt(sum_squared(first, last));
00123             d = 1.0/d;
00124             for (; first != last; ++first)
00125                 (*first) *= d;
00126         }
00127 
00129     inline double
00130         inner(double* first,
00131               double* last,
00132               double* second)
00133         {
00134             double sum = 0;
00135             for (; first != last; ++first, ++second)
00136                 sum += (*first)*(*second);
00137             return sum;
00138         }
00139 #else
00140 
00142     template <typename ForwardIterator>
00143     inline typename go_iterator_traits<ForwardIterator>::value_type 
00144         sum(ForwardIterator first,
00145             ForwardIterator last)
00146         {
00147             typename go_iterator_traits<ForwardIterator>::value_type sum = 0;
00148             for (; first != last; ++first)
00149                 sum += *first;
00150             return sum;
00151         }
00152 
00154     template <typename ForwardIterator>
00155     inline typename go_iterator_traits<ForwardIterator>::value_type
00156         sum_squared(ForwardIterator first,
00157                     ForwardIterator last)
00158         {
00159             typename go_iterator_traits<ForwardIterator>::value_type sum = 0;
00160             for (; first != last; ++first)
00161                 sum += (*first)*(*first);
00162             return sum;
00163         }
00164 
00166     template <typename ForwardIterator>
00167     inline typename go_iterator_traits<ForwardIterator>::value_type
00168         distance_squared(ForwardIterator first1,
00169                          ForwardIterator last1,
00170                          ForwardIterator first2)
00171         {
00172             typename go_iterator_traits<ForwardIterator>::value_type sum = 0;
00173             for (; first1 != last1; ++first1, ++first2)
00174                 sum += (*first1 - *first2)*(*first1 - *first2);
00175             return sum;
00176         }
00177 
00179     template <typename ForwardIterator>
00180     inline void
00181         normalize(ForwardIterator first,
00182                   ForwardIterator last)
00183         {
00184             typename go_iterator_traits<ForwardIterator>::value_type d
00185                 = sqrt(sum_squared(first, last));
00186             d = 1.0/d;
00187             for (; first != last; ++first)
00188                 (*first) *= d;
00189         }
00190 
00192     template <typename ForwardIterator>
00193     inline typename go_iterator_traits<ForwardIterator>::value_type
00194         inner(ForwardIterator first,
00195               ForwardIterator last,
00196               ForwardIterator second)
00197         {
00198             typename go_iterator_traits<ForwardIterator>::value_type sum = 0;
00199             for (; first != last; ++first, ++second)
00200                 sum += (*first)*(*second);
00201             return sum;
00202         }
00203 #endif // _MSC_VER
00204 
00206     template <typename InputStream>
00207     inline InputStream& eatwhite(InputStream& is)
00208         {
00209             char c;
00210             while (is.get(c)) {
00211                 if (isspace(c)==0) {
00212                     is.putback(c);
00213                     break;
00214                 }
00215             }
00216             return is;
00217         }
00218 
00220 } // End of namespace Go
00221 
00222 
00223 #endif
00224 

Generated on Mon Jun 11 14:48:18 2007 for GoTools Core Library by  doxygen 1.5.1