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