00001 // Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE) 00002 // 00003 // This file is part of Olena. 00004 // 00005 // Olena is free software: you can redistribute it and/or modify it under 00006 // the terms of the GNU General Public License as published by the Free 00007 // Software Foundation, version 2 of the License. 00008 // 00009 // Olena is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 // General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Olena. If not, see <http://www.gnu.org/licenses/>. 00016 // 00017 // As a special exception, you may use this file as part of a free 00018 // software project without restriction. Specifically, if other files 00019 // instantiate templates or use macros or inline functions from this 00020 // file, or you compile this file and link it with other files to produce 00021 // an executable, this file does not by itself cause the resulting 00022 // executable to be covered by the GNU General Public License. This 00023 // exception does not however invalidate any other reasons why the 00024 // executable file might be covered by the GNU General Public License. 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 //inline 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 // log call to the functor 00087 mutable unsigned log_functor_call; 00088 }; 00089 00090 } // end of namespace mln::fun::x2p 00091 00092 } // end of namespace mln::fun 00093 00094 } // end of namespace mln 00095 00096 #endif // ! MLN_FUN_X2P_CLOSEST_POINT_HH