FougTools  0.7.0dev-046fb6a
Handy tools for C++, Qt and OpenCascade
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
norm.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** FougTools
3 ** Copyright Fougue (30 Mar. 2015)
4 ** contact@fougue.pro
5 **
6 ** This software is a computer program whose purpose is to provide utility
7 ** tools for the C++ language and the Qt toolkit.
8 **
9 ** This software is governed by the CeCILL-C license under French law and
10 ** abiding by the rules of distribution of free software. You can use,
11 ** modify and/ or redistribute the software under the terms of the CeCILL-C
12 ** license as circulated by CEA, CNRS and INRIA at the following URL
13 ** "http://www.cecill.info".
14 ****************************************************************************/
15 
16 #ifndef MATHTOOLS_NORM_H
17 #define MATHTOOLS_NORM_H
18 
19 #include "norm_traits.h"
20 #include "vec_traits.h"
21 
22 namespace math {
23 
24 namespace internal {
25 
26 template<typename VEC> struct VecTraitsHelper
27 {
28  typedef typename NumTraits< typename VecTraits<VEC>::CoordType >::Real Real;
29 };
30 
31 } // namespace internal
32 
59 template<typename FUNC>
60 struct Norm
61 {
65  template<typename COORD_ITERATOR>
66 #ifndef DOXYGEN
67  static auto fromRange(COORD_ITERATOR begin, COORD_ITERATOR end) -> decltype(internal::typeHelper(*begin))
68  { return FUNC::fromRange(begin, end); }
69 #else
70  static CompatibleRealType fromRange(COORD_ITERATOR begin, COORD_ITERATOR end);
71 #endif
72 
73  template<typename COORD_ITERATOR>
74  static bool isNullRange(COORD_ITERATOR begin, COORD_ITERATOR end)
75  {
76  typedef decltype(*begin) CoordType;
77  return Norm::fromRange(begin, end) < NumTraits<CoordType>::precision();
78  }
79 
80 
85  template<std::size_t N, typename COORD_TYPE>
86 #ifndef DOXYGEN
87  static typename NumTraits<COORD_TYPE>::Real fromPtr(const COORD_TYPE* coordPtr)
88  {
89  typename internal::NormTraits<FUNC>::NormCategory normCategory;
90  return Norm::fromPtrDispatch<N, COORD_TYPE>(coordPtr, normCategory);
91  }
92 #else
93  static CompatibleRealType fromPtr(const COORD_TYPE* coordPtr);
94 #endif
95 
96  template<std::size_t N, typename COORD_TYPE>
97  static bool isNullPtr(const COORD_TYPE* coordPtr)
98  { return Norm::fromPtr(coordPtr) < NumTraits<COORD_TYPE>::precision(); }
99 
100 
103  template<std::size_t N, typename COORD_TYPE>
104 #ifndef DOXYGEN
105  static typename NumTraits<COORD_TYPE>::Real fromArray(const COORD_TYPE (&coordArray)[N])
106  { return Norm::fromPtr<N, COORD_TYPE>(&coordArray[0]); }
107 #else
108  static CompatibleRealType fromArray(const COORD_TYPE (&coordArray)[N]);
109 #endif
110 
111  template<std::size_t N, typename COORD_TYPE>
112  static bool isNullArray(const COORD_TYPE (&coordArray)[N])
113  { return Norm::fromArray(coordArray) < NumTraits<COORD_TYPE>::precision(); }
114 
115 
118  template<typename VEC>
119 #ifndef DOXYGEN
120  static typename internal::VecTraitsHelper<VEC>::Real fromObject(const VEC& vec)
121  {
122  typename VecTraits<VEC>::AccessCategory accessCategory;
123  return Norm::fromObjectDispatch(vec, accessCategory);
124  }
125 #else
126  static CompatibleRealType fromObject(const VEC& vec);
127 #endif
128 
129  template<typename VEC>
130  static bool isNullObject(const VEC& vec)
131  {
132  typedef typename VecTraits<VEC>::CoordType CoordType;
134  }
135 
136 #ifndef DOXYGEN
137  // -- Implementation
138 
139 private:
140  template<std::size_t N, typename COORD_TYPE>
141  static typename NumTraits<COORD_TYPE>::Real fromPtrDispatch(const COORD_TYPE* coordPtr,
142  internal::DefaultNormSpecializationTag)
143  { return Norm::fromRange(coordPtr, coordPtr + N); }
144 
145 
146  template<std::size_t N, typename COORD_TYPE>
147  static typename NumTraits<COORD_TYPE>::Real fromPtrDispatch(const COORD_TYPE* coordPtr,
148  internal::ArityNormSpecializationTag)
149  { return FUNC::template fromPtr<N, COORD_TYPE>(coordPtr); }
150 
151 
152  template<typename VEC>
153  static typename internal::VecTraitsHelper<VEC>::Real fromObjectDispatch(const VEC& vec,
154  StlIteratorVecAccessTag)
155  { return Norm::fromRange(vec.begin(), vec.end()); }
156 
157 
158  template<typename VEC>
159  static typename internal::VecTraitsHelper<VEC>::Real fromObjectDispatch(const VEC& vec,
160  FuncIteratorVecAccessTag)
161  { return Norm::fromRange(VecAccess<VEC>::begin(vec), VecAccess<VEC>::end(vec)); }
162 
163 
164  template<typename VEC>
165  static typename internal::VecTraitsHelper<VEC>::Real fromObjectDispatch(const VEC& vec,
166  PointerVecAccessTag)
167  {
168  typedef typename VecTraits<VEC>::CoordType CoordType;
169  return Norm::fromPtr<VecTraits<VEC>::Arity, CoordType>(VecAccess<VEC>::pointer(vec));
170  }
171 
172 
173  template<typename VEC>
174  static typename internal::VecTraitsHelper<VEC>::Real fromObjectDispatch(const VEC& vec,
175  StdArrayVecAccessTag)
176  {
177  const auto&& array = VecAccess<VEC>::stdarray(vec);
178  return Norm::fromRange(array.cbegin(), array.cend());
179  }
180 
181 #endif // !DOXYGEN
182 };
183 
184 } // namespace math
185 
186 #endif // MATHTOOLS_NORM_H
Computation of norms in K-vector space.
Definition: norm.h:60
static CompatibleRealType fromPtr(const COORD_TYPE *coordPtr)
Returns the norm of the vector with its N coordinates stored in memory at coordPtr.
static bool isNullArray(const COORD_TYPE(&coordArray)[N])
Definition: norm.h:112
static CompatibleRealType fromArray(const COORD_TYPE(&coordArray)[N])
Returns the norm of the vector with its N coordinates stored in array coordArray. ...
static bool isNullObject(const VEC &vec)
Definition: norm.h:130
static Real precision()
Definition: num_traits.h:30
static bool isNullRange(COORD_ITERATOR begin, COORD_ITERATOR end)
Definition: norm.h:74
Definition: consts.h:18
static CompatibleRealType fromObject(const VEC &vec)
Returns the norm of the vector object vec.
static CompatibleRealType fromRange(COORD_ITERATOR begin, COORD_ITERATOR end)
Returns the norm of the vector with coordinates in iterator range [ begin , end ].
T Real
Definition: num_traits.h:28
static bool isNullPtr(const COORD_TYPE *coordPtr)
Definition: norm.h:97
Type traits on vector types.
Definition: vec_traits.h:167