00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef MLN_FUN_X2P_CLOSEST_POINT_HH
00027 # define MLN_FUN_X2P_CLOSEST_POINT_HH
00028
00029 # include <mln/algebra/vec.hh>
00030 # include <mln/norm/l2.hh>
00031 # include <mln/core/site_set/p_array.hh>
00032 # include <mln/core/site_set/box.hh>
00033
00034 namespace mln
00035 {
00036
00037 namespace fun
00038 {
00039
00040 namespace x2p
00041 {
00042
00044 template <typename P>
00045 struct closest_point
00046 {
00047 typedef algebra::vec<P::dim, float> input;
00048 typedef P result;
00049
00050 closest_point(const p_array<P>& X, const box<P>& box)
00051 : box_(box), X(X)
00052 , log_functor_call(0)
00053
00054 { }
00055
00056 result
00057
00058 operator () (const input& Ck) const
00059 {
00060 ++log_functor_call;
00061
00062 algebra::vec<P::dim,float> Cki = Ck;
00063 algebra::vec<P::dim,float> best_x = convert::to< algebra::vec<P::dim,float> >(X[0]);
00064 float best_d = norm::l2(Cki - best_x);
00065 for (unsigned j = 1; j < X.nsites(); ++j)
00066 {
00067 algebra::vec<P::dim,float> Xj = convert::to< algebra::vec<P::dim,float> >(X[j]);
00068 float d = norm::l2(Cki - Xj);
00069 if (d < best_d)
00070 {
00071 best_d = d;
00072 best_x = Xj;
00073 }
00074 }
00075 return convert::to<P>(best_x);
00076 }
00077
00078 const box<P>& domain() const
00079 {
00080 return box_;
00081 }
00082
00083 const box<P> box_;
00084 const p_array<P>& X;
00085
00086
00087 mutable unsigned log_functor_call;
00088 };
00089
00090 }
00091
00092 }
00093
00094 }
00095
00096 #endif // ! MLN_FUN_X2P_CLOSEST_POINT_HH