Milena (Olena)
User documentation 2.0a Id
|
00001 // Copyright (C) 2007, 2008, 2009, 2010 EPITA Research and Development 00002 // Laboratory (LRDE) 00003 // 00004 // This file is part of Olena. 00005 // 00006 // Olena is free software: you can redistribute it and/or modify it under 00007 // the terms of the GNU General Public License as published by the Free 00008 // Software Foundation, version 2 of the License. 00009 // 00010 // Olena is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 // General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Olena. If not, see <http://www.gnu.org/licenses/>. 00017 // 00018 // As a special exception, you may use this file as part of a free 00019 // software project without restriction. Specifically, if other files 00020 // instantiate templates or use macros or inline functions from this 00021 // file, or you compile this file and link it with other files to produce 00022 // an executable, this file does not by itself cause the resulting 00023 // executable to be covered by the GNU General Public License. This 00024 // exception does not however invalidate any other reasons why the 00025 // executable file might be covered by the GNU General Public License. 00026 00027 #ifndef MLN_ALGEBRA_H_VEC_HH 00028 # define MLN_ALGEBRA_H_VEC_HH 00029 00033 00034 # include <mln/algebra/vec.hh> 00035 # include <mln/literal/one.hh> 00036 00037 00038 namespace mln 00039 { 00040 00041 // Forward declaration. 00042 namespace algebra { 00043 template <unsigned d, typename C> class h_vec; 00044 } 00045 00046 00047 namespace trait 00048 { 00049 00050 // For unary traits. 00051 00052 template < template <class> class Name, 00053 unsigned d, typename C > 00054 struct set_precise_unary_< Name, algebra::h_vec<d, C> > 00055 { 00056 typedef mln_trait_unary(Name, C) V; 00057 typedef algebra::h_vec<d, V> ret; 00058 }; 00059 00060 // For binary traits. 00061 00062 template < template <class, class> class Name, 00063 unsigned d, typename C, 00064 typename Q > 00065 struct set_precise_binary_< Name, 00066 algebra::h_vec<d, C>, algebra::h_vec<d, Q> > 00067 { 00068 typedef mln_trait_binary(Name, C, Q) V; 00069 typedef algebra::h_vec<d, V> ret; 00070 }; 00071 00072 template < template <class, class> class Name, 00073 unsigned d, typename C, 00074 typename S > 00075 struct set_precise_binary_< Name, 00076 algebra::h_vec<d, C>, mln::value::scalar_<S> > 00077 { 00078 typedef mln_trait_binary(Name, C, S) V; 00079 typedef algebra::h_vec<d, V> ret; 00080 }; 00081 00082 } // end of namespace mln::trait 00083 00084 00085 00086 namespace algebra 00087 { 00088 00089 00093 template <unsigned d, typename C> 00094 class h_vec : public vec<d + 1, C> 00095 { 00096 public: 00098 enum { dim = d }; 00099 00101 h_vec(); 00103 h_vec(const vec<d+1, C>& other); 00104 00105 h_vec& operator=(const vec<d+1, C>& rhs); 00106 00108 vec<d,C> to_vec() const; 00109 }; 00110 00111 00112 00113 # ifndef MLN_INCLUDE_ONLY 00114 00115 template <unsigned d, typename C> 00116 inline 00117 h_vec<d,C>::h_vec() 00118 { 00119 /* Safety measure: set the last component to the unit (1). This 00120 way, converting an unitialized h_vec to a vector won't trigger 00121 division-by-zero errors if this last component were randomly 00122 initialized to 0. */ 00123 this->data_[d] = literal::one; 00124 } 00125 00126 template <unsigned d, typename C> 00127 inline 00128 h_vec<d,C>::h_vec(const vec<d+1, C>& other) 00129 : vec<d+1, C>(other) 00130 { 00131 } 00132 00133 template <unsigned d, typename C> 00134 inline 00135 h_vec<d,C>& h_vec<d,C>::operator=(const vec<d+1, C>& rhs) 00136 { 00137 if (& rhs == this) 00138 return *this; 00139 this->vec<d+1, C>::operator=(rhs); 00140 return *this; 00141 } 00142 00143 template <unsigned d, typename C> 00144 inline 00145 vec<d,C> h_vec<d,C>::to_vec() const 00146 { 00147 const C w = this->data_[d]; 00148 mln_assertion(w != 0); 00149 00150 vec<d,C> tmp; 00151 for (unsigned i = 0; i < d; ++i) 00152 tmp[i] = static_cast<C>(this->data_[i] / w); 00153 return tmp; 00154 } 00155 00156 // Immersion of a vector in its homogeneous space. 00157 00158 template <unsigned n, typename T> 00159 inline 00160 h_vec<n, T> 00161 vec<n,T>::to_h_vec() const 00162 { 00163 h_vec<n, T> tmp; 00164 for (unsigned i = 0; i < n; ++i) 00165 tmp[i] = this->data_[i]; 00166 tmp[n] = literal::one; 00167 return tmp; 00168 } 00169 00170 # endif // ! MLN_INCLUDE_ONLY 00171 00172 } // end of namespace mln::algebra 00173 00174 } // end of namespace mln 00175 00176 00177 #endif // ! MLN_ALGEBRA_H_VEC_HH