digiKam
Loading...
Searching...
No Matches
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 * SPDX-FileCopyrightText: 2016 by Omar Amin <Omar dot moh dot amin at gmail dot com>
10 * SPDX-FileCopyrightText: 2016-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
11 *
12 * SPDX-License-Identifier: GPL-2.0-or-later
13 *
14 * ============================================================ */
15
16#pragma once
17
18// C++ includes
19
20#include <vector>
21
22// Qt includes
23
24#include <QtGlobal>
25
26namespace Digikam
27{
28
29template<class T>
30inline T length_squared(const std::vector<T>& diff)
31{
32 T sum = 0;
33
34 for (unsigned int i = 0 ; i < diff.size() ; ++i)
35 {
36 sum += diff[i] * diff[i];
37 }
38
39 return sum;
40}
41
42template<class T>
43std::vector<T> operator-(const std::vector<T>& v1, const std::vector<T>& v2)
44{
45 Q_ASSERT(v1.size() == v2.size());
46
47 std::vector<T> result(v1.size());
48
49 for (unsigned int i = 0 ; i < v1.size() ; ++i)
50 {
51 result[i] = v1[i] - v2[i];
52 }
53
54 return result;
55}
56
57template<class T>
58std::vector<T> operator-(const std::vector<T>& v1)
59{
60 std::vector<T> result(v1.size());
61
62 for (unsigned int i = 0 ; i < v1.size() ; ++i)
63 {
64 result[i] = -v1[i];
65 }
66
67 return result;
68}
69
70template<class T>
71std::vector<T> operator/(const std::vector<T>& v1, int divisor)
72{
73 std::vector<T> result(v1.size());
74
75 for (unsigned int i = 0 ; i < v1.size() ; ++i)
76 {
77 result[i] = v1[i] / divisor;
78 }
79
80 return result;
81}
82
83template<class T>
84std::vector<std::vector<T> > operator/(const std::vector<std::vector<T> >& v1, int divisor)
85{
86/*
87 Q_ASSERT(v1[0].size() != v2.size());
88*/
89 std::vector<std::vector<T> > result(v1.size(),std::vector<T>(v1[0].size(),0));
90
91 for (unsigned int i = 0 ; i < v1.size() ; ++i)
92 {
93 for (unsigned int j = 0 ; j < v1[0].size() ; ++j)
94 {
95 result[i][j] = v1[i][j] / divisor;
96 }
97 }
98
99 return result;
100}
101
102template<class T>
103std::vector<T> operator+(const std::vector<T>& v1, const std::vector<T>& v2)
104{
105 Q_ASSERT(v1.size() == v2.size());
106
107 std::vector<T> result(v1.size());
108
109 for (unsigned int i = 0 ; i < v1.size() ; ++i)
110 {
111 result[i] = v1[i] + v2[i];
112 }
113
114 return result;
115}
116
117template<class T>
118std::vector<std::vector<T> > operator+(const std::vector<std::vector<T> >& v1,
119 const std::vector<std::vector<T> >& v2)
120{
121 Q_ASSERT(
122 (v1.size() == v2.size()) &&
123 (v1[0].size() == v2[0].size())
124 );
125
126 std::vector<std::vector<T> > result(v1.size(), std::vector<T>(v1[0].size(),0));
127
128 for (unsigned int i = 0 ; i < v1.size() ; ++i)
129 {
130 for (unsigned int j = 0 ; j < v2[0].size() ; ++j)
131 {
132 result[i][j] += v1[i][j] + v2[i][j];
133 }
134
135 }
136
137 return result;
138}
139
140template<class T>
141std::vector<T> operator*(const std::vector<std::vector<T> >& v1,
142 const std::vector<T>& v2)
143{
144 Q_ASSERT(v1[0].size() == v2.size());
145
146 std::vector<T> result(v1.size());
147
148 for (unsigned int i = 0 ; i < v1.size() ; ++i)
149 {
150 result[i] = 0;
151
152 for (unsigned int j = 0 ; j < v1[0].size() ; ++j)
153 {
154 result[i] += v1[i][j] * v2[j];
155 }
156 }
157
158 return result;
159}
160
161template<class T>
162std::vector<std::vector<T> > operator*(const std::vector<std::vector<T> >& v1,
163 const std::vector<std::vector<T> >& v2)
164{
165 Q_ASSERT(v1[0].size() == v2.size());
166
167 std::vector<std::vector<T> > result(v1.size(), std::vector<T>(v2[0].size(),0));
168
169 for (unsigned int i = 0 ; i < v1.size() ; ++i)
170 {
171 for (unsigned int k = 0 ; k < v1[0].size() ; ++k)
172 {
173 // swapping j and k loops for cache optimization
174
175 for (unsigned int j = 0 ; j < v2[0].size() ; ++j)
176 {
177
178 result[i][j] += v1[i][k] * v2[k][j];
179 }
180 }
181 }
182
183 return result;
184}
185
186template<class T>
187std::vector<std::vector<T> > operator*(const std::vector<T>& v1,
188 const std::vector<T>& v2)
189{
190 Q_ASSERT(v1.size() == v2.size());
191
192 std::vector<std::vector<T> > result(v1.size(), std::vector<T>(v2.size(), 0));
193
194 for (unsigned int i = 0 ; i < v1.size() ; ++i)
195 {
196 for (unsigned int j = 0 ; j < v1.size() ; ++j)
197 {
198 result[i][j] = v1[i] * v2[j];
199 }
200 }
201
202 return result;
203}
204
205template<class T>
206std::vector<std::vector<T> > operator+(const std::vector<std::vector<T> >& v1,
207 float d)
208{
209/*
210 Q_ASSERT((v1.size() == v2.size()) &&
211 (v1[0].size() == v2[0].size()));
212*/
213 std::vector<std::vector<T> > result(v1.size(), std::vector<T>(v1[0].size(), 0));
214
215 for (unsigned int i = 0 ; i < v1.size() ; ++i)
216 {
217 for (unsigned int j = 0 ; j < v1[0].size() ; ++j)
218 {
219 result[i][j] = v1[i][j] * d;
220 }
221
222 }
223
224 return result;
225}
226
227template<class T>
228std::vector<T> operator*(const std::vector<T>& v1,
229 float d)
230{
231/*
232 Q_ASSERT((v1.size() == v2.size()) &&
233 (v1[0].size() == v2[0].size()));
234*/
235 std::vector<T> result(v1.size());
236
237 for (unsigned int i = 0 ; i < v1.size() ; ++i)
238 {
239 result[i] = v1[i] * d;
240
241 }
242
243 return result;
244}
245
246} // namespace Digikam
#define T
Definition datefolderview.cpp:34
PointTransformAffine operator*(const PointTransformAffine &lhs, const PointTransformAffine &rhs)
Definition pointtransformaffine.cpp:76
T length_squared(const std::vector< T > &diff)
Definition vectoroperations.h:30
std::vector< T > operator+(const std::vector< T > &v1, const std::vector< T > &v2)
Definition vectoroperations.h:103
std::vector< T > operator/(const std::vector< T > &v1, int divisor)
Definition vectoroperations.h:71
std::vector< T > operator-(const std::vector< T > &v1, const std::vector< T > &v2)
Definition vectoroperations.h:43