Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_MATH_COMPUTE_CONTIGUOUS_ANGLE_INTERVAL_H
00002 #define OPENTISSUE_CORE_MATH_COMPUTE_CONTIGUOUS_ANGLE_INTERVAL_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/math/math_value_traits.h>
00013 #include <OpenTissue/core/math/math_is_number.h>
00014
00015 #include <algorithm>
00016 #include <vector>
00017 #include <stdexcept>
00018 #include <cmath>
00019
00020 namespace OpenTissue
00021 {
00022 namespace math
00023 {
00024
00055 template<typename iterator_type>
00056 inline void compute_contiguous_angle_interval(
00057 iterator_type const & begin
00058 , iterator_type const & end
00059 , typename iterator_type::value_type & theta_min
00060 , typename iterator_type::value_type & theta_max )
00061 {
00062
00063
00064
00065
00066
00067 typedef typename iterator_type::value_type T;
00068 typedef OpenTissue::math::ValueTraits<T> value_traits;
00069
00070
00071 T const two_pi = value_traits::pi()*value_traits::two();
00072
00073
00074 size_t const N = std::distance( begin, end);
00075
00076
00077 std::vector<T> storage;
00078 storage.resize( N );
00079 std::copy( begin, end, storage.begin() );
00080 std::sort( storage.begin(), storage.end() );
00081
00082
00083
00084 T max_delta_theta = value_traits::zero();
00085 size_t max_i = N;
00086
00087 for(size_t i = 0u;i<N;++i)
00088 {
00089 T const & theta_i = storage[i];
00090 T const & theta_j = storage[(i+1)%N];
00091 T const delta_theta = (theta_j < theta_i) ? theta_j+two_pi-theta_i : theta_j-theta_i;
00092 if(delta_theta > max_delta_theta)
00093 {
00094 max_i = i;
00095 max_delta_theta = delta_theta;
00096 theta_min = theta_j;
00097 theta_max = theta_i;
00098 }
00099 }
00100
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 while(theta_min < value_traits::zero())
00126 theta_min += two_pi;
00127
00128 while(theta_max < theta_min)
00129 theta_max += two_pi;
00130
00131 assert( theta_min <= theta_max || !"compute_contiguous_angle_interval(): invalid interval");
00132 }
00133
00134
00135 }
00136 }
00137
00138
00139 #endif