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_CORE_DPOINT_HH
00027 # define MLN_CORE_DPOINT_HH
00028
00032
00033 # include <mln/core/def/coord.hh>
00034 # include <mln/core/concept/gdpoint.hh>
00035 # include <mln/core/internal/coord_impl.hh>
00036 # include <mln/fun/i2v/all.hh>
00037 # include <mln/algebra/vec.hh>
00038 # include <mln/metal/converts_to.hh>
00039
00040
00041 namespace mln
00042 {
00043
00045 template <typename G, typename C> struct point;
00046 namespace literal {
00047 struct zero_t;
00048 struct one_t;
00049 }
00051
00052
00057 template <typename G, typename C>
00058 struct dpoint : public Gdpoint< dpoint<G,C> >,
00059 public internal::mutable_coord_impl_< G::dim, C, dpoint<G,C> >
00060 {
00064 enum { dim = G::dim };
00065
00067 typedef G grid;
00068
00070 typedef point<G,C> psite;
00071
00073 typedef point<G,C> site;
00074
00076 typedef C coord;
00077
00079 typedef algebra::vec<G::dim, C> vec;
00080
00084 C operator[](unsigned i) const;
00085
00089 C& operator[](unsigned i);
00090
00092 dpoint();
00093
00095 template <typename C2>
00096 dpoint(const algebra::vec<dim,C2>& v);
00097
00100 dpoint(C ind);
00101 dpoint(C row, C col);
00102 dpoint(C sli, C row, C col);
00104
00106 dpoint(const literal::zero_t&);
00107 dpoint<G,C>& operator=(const literal::zero_t&);
00108
00109 dpoint(const literal::one_t&);
00110 dpoint<G,C>& operator=(const literal::one_t&);
00112
00114 template <typename F>
00115 dpoint(const Function_v2v<F>& f);
00116
00118 void set_all(C c);
00119
00121 template <typename Q>
00122 operator mln::algebra::vec<dpoint<G,C>::dim, Q>() const;
00123
00125 vec to_vec() const;
00126
00127 protected:
00128 mln::algebra::vec<G::dim, C> coord_;
00129 };
00130
00131
00132 # ifndef MLN_INCLUDE_ONLY
00133
00134 template <typename G, typename C>
00135 inline
00136 C dpoint<G,C>::operator[](unsigned i) const
00137 {
00138 assert(i < dim);
00139 return coord_[i];
00140 }
00141
00142 template <typename G, typename C>
00143 inline
00144 C& dpoint<G,C>::operator[](unsigned i)
00145 {
00146 assert(i < dim);
00147 return coord_[i];
00148 }
00149
00150 template <typename G, typename C>
00151 inline
00152 dpoint<G,C>::dpoint()
00153 {
00154 }
00155
00156 template <typename G, typename C>
00157 template <typename C2>
00158 inline
00159 dpoint<G,C>::dpoint(const algebra::vec<dim,C2>& v)
00160 {
00161
00162 if (dim < 3)
00163 coord_ = v;
00164 else
00165 {
00166 unsigned j = 0;
00167 for (unsigned i = dim - 2; i < dim; ++i)
00168 coord_[i] = static_cast<C>(v[j++]);
00169 for (unsigned i = 2; i < dim; ++i, ++j)
00170 coord_[i-j] = static_cast<C>(v[j]);
00171 }
00172 }
00173
00174 template <typename G, typename C>
00175 inline
00176 dpoint<G,C>::dpoint(C ind)
00177 {
00178 metal::bool_<(dim == 1)>::check();
00179 coord_[0] = ind;
00180 }
00181
00182 template <typename G, typename C>
00183 inline
00184 dpoint<G,C>::dpoint(C row, C col)
00185 {
00186 metal::bool_<(dim == 2)>::check();
00187 coord_[0] = row;
00188 coord_[1] = col;
00189 }
00190
00191 template <typename G, typename C>
00192 inline
00193 dpoint<G,C>::dpoint(C sli, C row, C col)
00194 {
00195 metal::bool_<(dim == 3)>::check();
00196 coord_[0] = sli;
00197 coord_[1] = row;
00198 coord_[2] = col;
00199 }
00200
00201 template <typename G, typename C>
00202 inline
00203 dpoint<G,C>::dpoint(const literal::zero_t&)
00204 {
00205 coord_.set_all(0);
00206 }
00207
00208 template <typename G, typename C>
00209 inline
00210 dpoint<G,C>&
00211 dpoint<G,C>::operator=(const literal::zero_t&)
00212 {
00213 coord_.set_all(0);
00214 return *this;
00215 }
00216
00217 template <typename G, typename C>
00218 inline
00219 dpoint<G,C>::dpoint(const literal::one_t&)
00220 {
00221 metal::bool_<(dim == 1)>::check();
00222 coord_[0] = 1;
00223 }
00224
00225 template <typename G, typename C>
00226 inline
00227 dpoint<G,C>&
00228 dpoint<G,C>::operator=(const literal::one_t&)
00229 {
00230 metal::bool_<(dim == 1)>::check();
00231 coord_[0] = 1;
00232 return *this;
00233 }
00234
00235 template <typename G, typename C>
00236 template <typename F>
00237 inline
00238 dpoint<G,C>::dpoint(const Function_v2v<F>& f_)
00239 {
00240 mlc_converts_to(mln_result(F), C)::check();
00241 const F& f = exact(f_);
00242 for (unsigned i = 0; i < dim; ++i)
00243 coord_[i] = static_cast<C>(f(i));
00244 }
00245
00246 template <typename G, typename C>
00247 inline
00248 void dpoint<G,C>::set_all(C c)
00249 {
00250 for (unsigned i = 0; i < dim; ++i)
00251 coord_[i] = c;
00252 }
00253
00254 template <typename G, typename C>
00255 template <typename Q>
00256 inline
00257 dpoint<G,C>::operator mln::algebra::vec<dpoint<G,C>::dim, Q> () const
00258 {
00259 return to_vec();
00260 }
00261
00262 template <typename G, typename C>
00263 inline
00264 typename dpoint<G,C>::vec
00265 dpoint<G,C>::to_vec() const
00266 {
00267 algebra::vec<G::dim, float> tmp;
00268
00269
00270 if (dim == 1)
00271 tmp[0] = coord_[0];
00272 else
00273 {
00274 unsigned j = 0;
00275 for (unsigned i = dim - 2; i < dim; ++i)
00276 tmp[j++] = coord_[i];
00277 for (unsigned i = 2; i < dim; ++i, ++j)
00278 tmp[j] = coord_[i-j];
00279 }
00280
00281 return tmp;
00282 }
00283
00284 # endif // ! MLN_INCLUDE_ONLY
00285
00286 }
00287
00288
00289 #endif // ! MLN_CORE_DPOINT_HH