DGtal  1.4.2
KForm.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 KForm.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 KForm.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::Order order, DGtal::Duality duality>
37 DGtal::KForm<Calculus, order, duality>
38 DGtal::KForm<Calculus, order, duality>::ones(ConstAlias<Calculus> _calculus)
39 {
40  KForm<Calculus, order, duality> kform(_calculus);
41  kform.myContainer.fill(1);
42  return kform;
43 }
44 
45 template <typename Calculus, DGtal::Order order, DGtal::Duality duality>
46 DGtal::KForm<Calculus, order, duality>
47 DGtal::KForm<Calculus, order, duality>::zeros(ConstAlias<Calculus> _calculus)
48 {
49  KForm<Calculus, order, duality> kform(_calculus);
50  return kform;
51 }
52 
53 template <typename Calculus, DGtal::Order order, DGtal::Duality duality>
54 DGtal::KForm<Calculus, order, duality>
55 DGtal::KForm<Calculus, order, duality>::dirac(ConstAlias<Calculus> _calculus, const typename Calculus::Cell& _cell)
56 {
57  KForm<Calculus, order, duality> kform(_calculus);
58  kform.myContainer( _calculus->getCellIndex(_cell) ) = 1;
59  return kform;
60 }
61 
62 template <typename Calculus, DGtal::Order order, DGtal::Duality duality>
63 DGtal::KForm<Calculus, order, duality>::KForm(ConstAlias<Calculus> _calculus)
64  : myContainer((&_calculus)->kFormLength(order, duality)), myCalculus(&_calculus)
65 {
66  clear();
67 }
68 
69 template <typename Calculus, DGtal::Order order, DGtal::Duality duality>
70 DGtal::KForm<Calculus, order, duality>::KForm(ConstAlias<Calculus> _calculus, const Container& _container)
71  : myContainer(_container), myCalculus(&_calculus)
72 {
73  ASSERT( myCalculus->kFormLength(order, duality) == myContainer.rows() );
74 }
75 
76 template <typename Calculus, DGtal::Order order, DGtal::Duality duality>
77 DGtal::KForm<Calculus, order, duality>&
78 DGtal::KForm<Calculus, order, duality>::operator=(const KForm& _form)
79 {
80  myContainer = _form.myContainer;
81  myCalculus = _form.myCalculus;
82  return *this;
83 }
84 
85 ///////////////////////////////////////////////////////////////////////////////
86 // Interface - public :
87 
88 template <typename Calculus, DGtal::Order order, DGtal::Duality duality>
89 void
90 DGtal::KForm<Calculus, order, duality>::selfDisplay(std::ostream& out) const
91 {
92  out << "[" << duality << " " << order << "-form (" << myContainer.rows() << ")]";
93 }
94 
95 template <typename Calculus, DGtal::Order order, DGtal::Duality duality>
96 std::string
97 DGtal::KForm<Calculus, order, duality>::className() const
98 {
99  return "KForm";
100 }
101 
102 template <typename Calculus, DGtal::Order order, DGtal::Duality duality>
103 typename DGtal::KForm<Calculus, order, duality>::SCell
104 DGtal::KForm<Calculus, order, duality>::getSCell(const typename DGtal::KForm<Calculus, order, duality>::Index& index) const
105 {
106  return myCalculus->getSCell(order, duality, index);
107 }
108 
109 template <typename Calculus, DGtal::Order order, DGtal::Duality duality>
110 typename DGtal::KForm<Calculus, order, duality>::Index
111 DGtal::KForm<Calculus, order, duality>::length() const
112 {
113  return myCalculus->kFormLength(order, duality);
114 }
115 
116 template <typename Calculus, DGtal::Order order, DGtal::Duality duality>
117 void
118 DGtal::KForm<Calculus, order, duality>::clear()
119 {
120  myContainer.setZero();
121 }
122 
123 template <typename Calculus, DGtal::Order order, DGtal::Duality duality>
124 bool
125 DGtal::KForm<Calculus, order, duality>::isValid() const
126 {
127  return myCalculus != NULL;
128 }
129 
130 ///////////////////////////////////////////////////////////////////////////////
131 // Implementation of inline functions //
132 
133 template <typename Calculus, DGtal::Order order, DGtal::Duality duality>
134 std::ostream&
135 DGtal::operator<<(std::ostream& out, const DGtal::KForm<Calculus, order, duality>& object)
136 {
137  object.selfDisplay(out);
138  return out;
139 }
140 
141 template <typename Calculus, DGtal::Order order, DGtal::Duality duality>
142 DGtal::KForm<Calculus, order, duality>
143 DGtal::operator+(const DGtal::KForm<Calculus, order, duality>& form_a, const DGtal::KForm<Calculus, order, duality>& form_b)
144 {
145  ASSERT( form_a.myCalculus == form_b.myCalculus );
146  return DGtal::KForm<Calculus, order, duality>(*form_a.myCalculus, form_a.myContainer + form_b.myContainer);
147 }
148 
149 template <typename Calculus, DGtal::Order order, DGtal::Duality duality>
150 DGtal::KForm<Calculus, order, duality>
151 DGtal::operator-(const DGtal::KForm<Calculus, order, duality>& form_a, const DGtal::KForm<Calculus, order, duality>& form_b)
152 {
153  ASSERT( form_a.myCalculus == form_b.myCalculus );
154  return DGtal::KForm<Calculus, order, duality>(*form_a.myCalculus, form_a.myContainer - form_b.myContainer);
155 }
156 
157 template <typename Calculus, DGtal::Order order, DGtal::Duality duality>
158 DGtal::KForm<Calculus, order, duality>
159 DGtal::operator*(const typename Calculus::Scalar& scalar, const DGtal::KForm<Calculus, order, duality>& form)
160 {
161  return DGtal::KForm<Calculus, order, duality>(*form.myCalculus, scalar * form.myContainer);
162 }
163 
164 template <typename Calculus, DGtal::Order order, DGtal::Duality duality>
165 DGtal::KForm<Calculus, order, duality>
166 DGtal::operator-(const DGtal::KForm<Calculus, order, duality>& form)
167 {
168  return DGtal::KForm<Calculus, order, duality>(*form.myCalculus, -form.myContainer);
169 }
170 
171 
172 // //
173 ///////////////////////////////////////////////////////////////////////////////