DGtal 1.3.0
Loading...
Searching...
No Matches
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
36template <typename Calculus, DGtal::Duality duality>
37DGtal::VectorField<Calculus, duality>::VectorField(ConstAlias<Calculus> _calculus)
38 : myCoordinates((&_calculus)->kFormLength(0, duality), DGtal::Dimension(Calculus::dimensionAmbient)), myCalculus(&_calculus)
39{
40 clear();
41}
42
43template <typename Calculus, DGtal::Duality duality>
44DGtal::VectorField<Calculus, duality>::VectorField(ConstAlias<Calculus> _calculus, const Coordinates& _coordinates)
45 : myCoordinates(_coordinates), myCalculus(&_calculus)
46{
47}
48
49template <typename Calculus, DGtal::Duality duality>
50DGtal::VectorField<Calculus, duality>&
51DGtal::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
61template <typename Calculus, DGtal::Duality duality>
62void
63DGtal::VectorField<Calculus, duality>::selfDisplay(std::ostream& out) const
64{
65 out << "[" << duality << " vector field (" << myCoordinates.rows() << "x" << myCoordinates.cols() << ")]";
66}
67
68template <typename Calculus, DGtal::Duality duality>
69std::string
70DGtal::VectorField<Calculus, duality>::className() const
71{
72 return "VectorField";
73}
74
75template <typename Calculus, DGtal::Duality duality>
76void
77DGtal::VectorField<Calculus, duality>::clear()
78{
79 myCoordinates.clear();
80}
81
82template <typename Calculus, DGtal::Duality duality>
83bool
84DGtal::VectorField<Calculus, duality>::isValid() const
85{
86 return myCalculus != NULL;
87}
88
89template <typename Calculus, DGtal::Duality duality>
90DGtal::VectorField<Calculus, duality>
91DGtal::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
109template <typename Calculus, DGtal::Duality duality>
110DGtal::KForm<Calculus, 0, duality>
111DGtal::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
126template <typename Calculus, DGtal::Duality duality>
127DGtal::KForm<Calculus, 0, duality>
128DGtal::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
139template <typename Calculus, DGtal::Duality duality>
140typename DGtal::VectorField<Calculus, duality>::SCell
141DGtal::VectorField<Calculus, duality>::getSCell(const Index& index) const
142{
143 return myCalculus->getSCell(0, duality, index);
144}
145
146template <typename Calculus, DGtal::Duality duality>
147typename DGtal::VectorField<Calculus, duality>::Index
148DGtal::VectorField<Calculus, duality>::length() const
149{
150 return myCalculus->kFormLength(0, duality);
151}
152
153template <typename Calculus, DGtal::Duality duality>
154typename DGtal::VectorField<Calculus, duality>::Vector
155DGtal::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
165template <typename Calculus, DGtal::Duality duality>
166void
167DGtal::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
176template <typename Calculus, DGtal::Duality duality>
177std::ostream&
178DGtal::operator<<(std::ostream & out, const DGtal::VectorField<Calculus, duality>& object)
179{
180 object.selfDisplay(out);
181 return out;
182}
183
184template <typename Calculus, DGtal::Duality duality>
185DGtal::VectorField<Calculus, duality>
186DGtal::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
192template <typename Calculus, DGtal::Duality duality>
193DGtal::VectorField<Calculus, duality>
194DGtal::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
200template <typename Calculus, DGtal::Duality duality>
201DGtal::VectorField<Calculus, duality>
202DGtal::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
207template <typename Calculus, DGtal::Duality duality>
208DGtal::VectorField<Calculus, duality>
209DGtal::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