DGtal  1.5.beta
VectorField.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file VectorField.ih
19  * @author Pierre Gueth (\c pierre.gueth@liris.cnrs.fr )
20  * Laboratoire d'InfoRmatique en Image et Systemes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
21  *
22  * @date 2014/03/26
23  *
24  * Implementation of inline methods defined in VectorField.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 ///////////////////////////////////////////////////////////////////////////////
30 // IMPLEMENTATION of inline methods.
31 ///////////////////////////////////////////////////////////////////////////////
32 
33 ///////////////////////////////////////////////////////////////////////////////
34 // ----------------------- Standard services ------------------------------
35 
36 template <typename Calculus, DGtal::Duality duality>
37 DGtal::VectorField<Calculus, duality>::VectorField(ConstAlias<Calculus> _calculus)
38  : myCoordinates((&_calculus)->kFormLength(0, duality), DGtal::Dimension(Calculus::dimensionAmbient)), myCalculus(&_calculus)
39 {
40  clear();
41 }
42 
43 template <typename Calculus, DGtal::Duality duality>
44 DGtal::VectorField<Calculus, duality>::VectorField(ConstAlias<Calculus> _calculus, const Coordinates& _coordinates)
45  : myCoordinates(_coordinates), myCalculus(&_calculus)
46 {
47 }
48 
49 template <typename Calculus, DGtal::Duality duality>
50 DGtal::VectorField<Calculus, duality>&
51 DGtal::VectorField<Calculus, duality>::operator=(const VectorField& _vector_field)
52 {
53  myCoordinates = _vector_field.myCoordinates;
54  myCalculus = _vector_field.myCalculus;
55  return *this;
56 }
57 
58 ///////////////////////////////////////////////////////////////////////////////
59 // Interface - public :
60 
61 template <typename Calculus, DGtal::Duality duality>
62 void
63 DGtal::VectorField<Calculus, duality>::selfDisplay(std::ostream& out) const
64 {
65  out << "[" << duality << " vector field (" << myCoordinates.rows() << "x" << myCoordinates.cols() << ")]";
66 }
67 
68 template <typename Calculus, DGtal::Duality duality>
69 std::string
70 DGtal::VectorField<Calculus, duality>::className() const
71 {
72  return "VectorField";
73 }
74 
75 template <typename Calculus, DGtal::Duality duality>
76 void
77 DGtal::VectorField<Calculus, duality>::clear()
78 {
79  myCoordinates.clear();
80 }
81 
82 template <typename Calculus, DGtal::Duality duality>
83 bool
84 DGtal::VectorField<Calculus, duality>::isValid() const
85 {
86  return myCalculus != NULL;
87 }
88 
89 template <typename Calculus, DGtal::Duality duality>
90 DGtal::VectorField<Calculus, duality>
91 DGtal::VectorField<Calculus, duality>::normalized(const typename Calculus::Scalar& epsilon) const
92 {
93  VectorField normalized_vector_field(*myCalculus);
94 
95  for (typename Calculus::Index index=0; index<myCalculus->kFormLength(0, duality); index++)
96  {
97  Vector vector = getVector(index);
98 
99  const typename Calculus::Scalar& norm = vector.norm();
100  if (norm <= epsilon) continue;
101  vector /= norm;
102 
103  normalized_vector_field.setVector(index, vector);
104  }
105 
106  return normalized_vector_field;
107 }
108 
109 template <typename Calculus, DGtal::Duality duality>
110 DGtal::KForm<Calculus, 0, duality>
111 DGtal::VectorField<Calculus, duality>::intensity(const typename Vector::NormType norm_type) const
112 {
113  typedef KForm<Calculus, 0, duality> ZeroForm;
114 
115  ZeroForm zero_form(*myCalculus);
116 
117  for (typename Calculus::Index index=0; index<zero_form.length(); index++)
118  {
119  const Vector vector = getVector(index);
120  zero_form.myContainer(index) = vector.norm(norm_type);
121  }
122 
123  return zero_form;
124 }
125 
126 template <typename Calculus, DGtal::Duality duality>
127 DGtal::KForm<Calculus, 0, duality>
128 DGtal::VectorField<Calculus, duality>::coordAlongDirection(const DGtal::Dimension& dim) const
129 {
130  ASSERT( dim < Calculus::dimensionAmbient );
131 
132  typedef KForm<Calculus, 0, duality> ZeroForm;
133  const ZeroForm zero_form(*myCalculus, myCoordinates.col(dim));
134  ASSERT( zero_form.myContainer.rows() == myCoordinates.rows() );
135 
136  return zero_form;
137 }
138 
139 template <typename Calculus, DGtal::Duality duality>
140 typename DGtal::VectorField<Calculus, duality>::SCell
141 DGtal::VectorField<Calculus, duality>::getSCell(const Index& index) const
142 {
143  return myCalculus->getSCell(0, duality, index);
144 }
145 
146 template <typename Calculus, DGtal::Duality duality>
147 typename DGtal::VectorField<Calculus, duality>::Index
148 DGtal::VectorField<Calculus, duality>::length() const
149 {
150  return myCalculus->kFormLength(0, duality);
151 }
152 
153 template <typename Calculus, DGtal::Duality duality>
154 typename DGtal::VectorField<Calculus, duality>::Vector
155 DGtal::VectorField<Calculus, duality>::getVector(const Index& index) const
156 {
157  Vector vector;
158 
159  for (DGtal::Dimension dim=0; dim<Calculus::dimensionAmbient; dim++)
160  vector[dim] = myCoordinates(index, dim);
161 
162  return vector;
163 }
164 
165 template <typename Calculus, DGtal::Duality duality>
166 void
167 DGtal::VectorField<Calculus, duality>::setVector(const Index& index, const Vector& vector)
168 {
169  for (DGtal::Dimension dim=0; dim<Calculus::dimensionAmbient; dim++)
170  myCoordinates(index, dim) = vector[dim];
171 }
172 
173 ///////////////////////////////////////////////////////////////////////////////
174 // Implementation of inline functions //
175 
176 template <typename Calculus, DGtal::Duality duality>
177 std::ostream&
178 DGtal::operator<<(std::ostream & out, const DGtal::VectorField<Calculus, duality>& object)
179 {
180  object.selfDisplay(out);
181  return out;
182 }
183 
184 template <typename Calculus, DGtal::Duality duality>
185 DGtal::VectorField<Calculus, duality>
186 DGtal::operator+(const DGtal::VectorField<Calculus, duality>& _vector_field_a, const DGtal::VectorField<Calculus, duality>& _vector_field_b)
187 {
188  ASSERT( _vector_field_a.myCalculus == _vector_field_b.myCalculus );
189  return DGtal::VectorField<Calculus, duality>(*_vector_field_a.myCalculus, _vector_field_a.myCoordinates + _vector_field_b.myCoordinates);
190 }
191 
192 template <typename Calculus, DGtal::Duality duality>
193 DGtal::VectorField<Calculus, duality>
194 DGtal::operator-(const DGtal::VectorField<Calculus, duality>& _vector_field_a, const DGtal::VectorField<Calculus, duality>& _vector_field_b)
195 {
196  ASSERT( _vector_field_a.myCalculus == _vector_field_b.myCalculus );
197  return DGtal::VectorField<Calculus, duality>(*_vector_field_a.myCalculus, _vector_field_a.myCoordinates - _vector_field_b.myCoordinates);
198 }
199 
200 template <typename Calculus, DGtal::Duality duality>
201 DGtal::VectorField<Calculus, duality>
202 DGtal::operator*(const typename Calculus::Scalar& _scalar, const DGtal::VectorField<Calculus, duality>& _vector_field)
203 {
204  return DGtal::VectorField<Calculus, duality>(*_vector_field.myCalculus, _scalar * _vector_field.myCoordinates);
205 }
206 
207 template <typename Calculus, DGtal::Duality duality>
208 DGtal::VectorField<Calculus, duality>
209 DGtal::operator-(const DGtal::VectorField<Calculus, duality>& _vector_field)
210 {
211  return DGtal::VectorField<Calculus, duality>(*_vector_field.myCalculus, -_vector_field.myCoordinates);
212 }
213 
214 // //
215 ///////////////////////////////////////////////////////////////////////////////
216 
217