digiKam
vectoroperations.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date : 16/08/2016
7  * Description : Vector operations methods for red eyes detection.
8  *
9  * Copyright (C) 2016 by Omar Amin <Omar dot moh dot amin at gmail dot com>
10  * Copyright (C) 2016-2022 by Gilles Caulier <caulier dot gilles at gmail dot com>
11  *
12  * This program is free software; you can redistribute it
13  * and/or modify it under the terms of the GNU General
14  * Public License as published by the Free Software Foundation;
15  * either version 2, or (at your option)
16  * any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * ============================================================ */
24 
25 #ifndef DIGIKAM_VECTOR_OPERATIONS_H
26 #define DIGIKAM_VECTOR_OPERATIONS_H
27 
28 // C++ includes
29 
30 #include <vector>
31 
32 // Qt includes
33 
34 #include <QtGlobal>
35 
36 namespace Digikam
37 {
38 
39 template<class T>
40 inline T length_squared(const std::vector<T>& diff)
41 {
42  T sum = 0;
43 
44  for (unsigned int i = 0 ; i < diff.size() ; ++i)
45  {
46  sum += diff[i] * diff[i];
47  }
48 
49  return sum;
50 }
51 
52 template<class T>
53 std::vector<T> operator-(const std::vector<T>& v1, const std::vector<T>& v2)
54 {
55  Q_ASSERT(v1.size() == v2.size());
56 
57  std::vector<T> result(v1.size());
58 
59  for (unsigned int i = 0 ; i < v1.size() ; ++i)
60  {
61  result[i] = v1[i] - v2[i];
62  }
63 
64  return result;
65 }
66 
67 template<class T>
68 std::vector<T> operator-(const std::vector<T>& v1)
69 {
70  std::vector<T> result(v1.size());
71 
72  for (unsigned int i = 0 ; i < v1.size() ; ++i)
73  {
74  result[i] = -v1[i];
75  }
76 
77  return result;
78 }
79 
80 template<class T>
81 std::vector<T> operator/(const std::vector<T>& v1, int divisor)
82 {
83  std::vector<T> result(v1.size());
84 
85  for (unsigned int i = 0 ; i < v1.size() ; ++i)
86  {
87  result[i] = v1[i] / divisor;
88  }
89 
90  return result;
91 }
92 
93 template<class T>
94 std::vector<std::vector<T> > operator/(const std::vector<std::vector<T> >& v1, int divisor)
95 {
96 /*
97  Q_ASSERT(v1[0].size() != v2.size());
98 */
99  std::vector<std::vector<T> > result(v1.size(),std::vector<T>(v1[0].size(),0));
100 
101  for (unsigned int i = 0 ; i < v1.size() ; ++i)
102  {
103  for (unsigned int j = 0 ; j < v1[0].size() ; ++j)
104  {
105  result[i][j] = v1[i][j] / divisor;
106  }
107  }
108 
109  return result;
110 }
111 
112 template<class T>
113 std::vector<T> operator+(const std::vector<T>& v1, const std::vector<T>& v2)
114 {
115  Q_ASSERT(v1.size() == v2.size());
116 
117  std::vector<T> result(v1.size());
118 
119  for (unsigned int i = 0 ; i < v1.size() ; ++i)
120  {
121  result[i] = v1[i] + v2[i];
122  }
123 
124  return result;
125 }
126 
127 template<class T>
128 std::vector<std::vector<T> > operator+(const std::vector<std::vector<T> >& v1,
129  const std::vector<std::vector<T> >& v2)
130 {
131  Q_ASSERT((v1.size() == v2.size()) &&
132  (v1[0].size() == v2[0].size()));
133 
134  std::vector<std::vector<T> > result(v1.size(), std::vector<T>(v1[0].size(),0));
135 
136  for (unsigned int i = 0 ; i < v1.size() ; ++i)
137  {
138  for (unsigned int j = 0 ; j < v2[0].size() ; ++j)
139  {
140  result[i][j] += v1[i][j] + v2[i][j];
141  }
142 
143  }
144 
145  return result;
146 }
147 
148 template<class T>
149 std::vector<T> operator*(const std::vector<std::vector<T> >& v1,
150  const std::vector<T>& v2)
151 {
152  Q_ASSERT(v1[0].size() == v2.size());
153 
154  std::vector<T> result(v1.size());
155 
156  for (unsigned int i = 0 ; i < v1.size() ; ++i)
157  {
158  result[i] = 0;
159 
160  for (unsigned int j = 0 ; j < v1[0].size() ; ++j)
161  {
162  result[i] += v1[i][j] * v2[j];
163  }
164  }
165 
166  return result;
167 }
168 
169 template<class T>
170 std::vector<std::vector<T> > operator*(const std::vector<std::vector<T> >& v1,
171  const std::vector<std::vector<T> >& v2)
172 {
173  Q_ASSERT(v1[0].size() == v2.size());
174 
175  std::vector<std::vector<T> > result(v1.size(), std::vector<T>(v2[0].size(),0));
176 
177  for (unsigned int i = 0 ; i < v1.size() ; ++i)
178  {
179  for (unsigned int k = 0 ; k < v1[0].size() ; ++k)
180  {
181  // swapping j and k loops for cache optimization
182 
183  for (unsigned int j = 0 ; j < v2[0].size() ; ++j)
184  {
185 
186  result[i][j] += v1[i][k] * v2[k][j];
187  }
188  }
189  }
190 
191  return result;
192 }
193 
194 
195 template<class T>
196 std::vector<std::vector<T> > operator*(const std::vector<T>& v1,
197  const std::vector<T>& v2)
198 {
199  Q_ASSERT(v1.size() == v2.size());
200 
201  std::vector<std::vector<T> > result(v1.size(), std::vector<T>(v2.size(), 0));
202 
203  for (unsigned int i = 0 ; i < v1.size() ; ++i)
204  {
205  for (unsigned int j = 0 ; j < v1.size() ; ++j)
206  {
207  result[i][j] = v1[i] * v2[j];
208  }
209  }
210 
211  return result;
212 }
213 
214 template<class T>
215 std::vector<std::vector<T> > operator+(const std::vector<std::vector<T> >& v1,
216  float d)
217 {
218 /*
219  Q_ASSERT((v1.size() == v2.size()) &&
220  (v1[0].size() == v2[0].size()));
221 */
222  std::vector<std::vector<T> > result(v1.size(), std::vector<T>(v1[0].size(), 0));
223 
224  for (unsigned int i = 0 ; i < v1.size() ; ++i)
225  {
226  for (unsigned int j = 0 ; j < v1[0].size() ; ++j)
227  {
228  result[i][j] = v1[i][j] * d;
229  }
230 
231  }
232 
233  return result;
234 }
235 
236 template<class T>
237 std::vector<T> operator*(const std::vector<T>& v1,
238  float d)
239 {
240 /*
241  Q_ASSERT((v1.size() == v2.size()) &&
242  (v1[0].size() == v2[0].size()));
243 */
244  std::vector<T> result(v1.size());
245 
246  for (unsigned int i = 0 ; i < v1.size() ; ++i)
247  {
248  result[i] = v1[i] * d;
249 
250  }
251 
252  return result;
253 }
254 
255 } // namespace Digikam
256 
257 #endif // DIGIKAM_VECTOR_OPERATIONS_H
#define T
Definition: datefolderview.cpp:43
PointTransformAffine operator*(const PointTransformAffine &lhs, const PointTransformAffine &rhs)
Definition: pointtransformaffine.cpp:86
T length_squared(const std::vector< T > &diff)
Definition: vectoroperations.h:40
std::vector< T > operator-(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: vectoroperations.h:53
std::vector< T > operator+(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: vectoroperations.h:113
std::vector< T > operator/(const std::vector< T > &v1, int divisor)
Definition: vectoroperations.h:81