libcamera  v0.3.2+116-83c5ad0f
Supporting cameras in Linux since 2019
vector.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /*
3  * Copyright (C) 2024, Paul Elder <paul.elder@ideasonboard.com>
4  *
5  * Vector and related operations
6  */
7 #pragma once
8 
9 #include <array>
10 #include <cmath>
11 #include <optional>
12 #include <ostream>
13 
14 #include <libcamera/base/log.h>
15 #include <libcamera/base/span.h>
16 
18 
19 #include "matrix.h"
20 
21 namespace libcamera {
22 
24 
25 namespace ipa {
26 
27 #ifndef __DOXYGEN__
28 template<typename T, unsigned int Rows,
29  std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr>
30 #else
31 template<typename T, unsigned int Rows>
32 #endif /* __DOXYGEN__ */
33 class Vector
34 {
35 public:
36  constexpr Vector() = default;
37 
38  constexpr Vector(const std::array<T, Rows> &data)
39  {
40  for (unsigned int i = 0; i < Rows; i++)
41  data_[i] = data[i];
42  }
43 
44  const T &operator[](size_t i) const
45  {
46  ASSERT(i < data_.size());
47  return data_[i];
48  }
49 
50  T &operator[](size_t i)
51  {
52  ASSERT(i < data_.size());
53  return data_[i];
54  }
55 
56 #ifndef __DOXYGEN__
57  template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 1>>
58 #endif /* __DOXYGEN__ */
59  constexpr T x() const
60  {
61  return data_[0];
62  }
63 
64 #ifndef __DOXYGEN__
65  template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 2>>
66 #endif /* __DOXYGEN__ */
67  constexpr T y() const
68  {
69  return data_[1];
70  }
71 
72 #ifndef __DOXYGEN__
73  template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 3>>
74 #endif /* __DOXYGEN__ */
75  constexpr T z() const
76  {
77  return data_[2];
78  }
79 
80  constexpr Vector<T, Rows> operator-() const
81  {
82  Vector<T, Rows> ret;
83  for (unsigned int i = 0; i < Rows; i++)
84  ret[i] = -data_[i];
85  return ret;
86  }
87 
88  constexpr Vector<T, Rows> operator-(const Vector<T, Rows> &other) const
89  {
90  Vector<T, Rows> ret;
91  for (unsigned int i = 0; i < Rows; i++)
92  ret[i] = data_[i] - other[i];
93  return ret;
94  }
95 
96  constexpr Vector<T, Rows> operator+(const Vector<T, Rows> &other) const
97  {
98  Vector<T, Rows> ret;
99  for (unsigned int i = 0; i < Rows; i++)
100  ret[i] = data_[i] + other[i];
101  return ret;
102  }
103 
104  constexpr T operator*(const Vector<T, Rows> &other) const
105  {
106  T ret = 0;
107  for (unsigned int i = 0; i < Rows; i++)
108  ret += data_[i] * other[i];
109  return ret;
110  }
111 
112  constexpr Vector<T, Rows> operator*(T factor) const
113  {
114  Vector<T, Rows> ret;
115  for (unsigned int i = 0; i < Rows; i++)
116  ret[i] = data_[i] * factor;
117  return ret;
118  }
119 
120  constexpr Vector<T, Rows> operator/(T factor) const
121  {
122  Vector<T, Rows> ret;
123  for (unsigned int i = 0; i < Rows; i++)
124  ret[i] = data_[i] / factor;
125  return ret;
126  }
127 
128  constexpr double length2() const
129  {
130  double ret = 0;
131  for (unsigned int i = 0; i < Rows; i++)
132  ret += data_[i] * data_[i];
133  return ret;
134  }
135 
136  constexpr double length() const
137  {
138  return std::sqrt(length2());
139  }
140 
141 private:
142  std::array<T, Rows> data_;
143 };
144 
145 template<typename T, unsigned int Rows, unsigned int Cols>
147 {
148  Vector<T, Rows> result;
149 
150  for (unsigned int i = 0; i < Rows; i++) {
151  T sum = 0;
152  for (unsigned int j = 0; j < Cols; j++)
153  sum += m[i][j] * v[j];
154  result[i] = sum;
155  }
156 
157  return result;
158 }
159 
160 template<typename T, unsigned int Rows>
161 bool operator==(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs)
162 {
163  for (unsigned int i = 0; i < Rows; i++) {
164  if (lhs[i] != rhs[i])
165  return false;
166  }
167 
168  return true;
169 }
170 
171 template<typename T, unsigned int Rows>
172 bool operator!=(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs)
173 {
174  return !(lhs == rhs);
175 }
176 
177 #ifndef __DOXYGEN__
178 bool vectorValidateYaml(const YamlObject &obj, unsigned int size);
179 #endif /* __DOXYGEN__ */
180 
181 } /* namespace ipa */
182 
183 #ifndef __DOXYGEN__
184 template<typename T, unsigned int Rows>
185 std::ostream &operator<<(std::ostream &out, const ipa::Vector<T, Rows> &v)
186 {
187  out << "Vector { ";
188  for (unsigned int i = 0; i < Rows; i++) {
189  out << v[i];
190  out << ((i + 1 < Rows) ? ", " : " ");
191  }
192  out << " }";
193 
194  return out;
195 }
196 
197 template<typename T, unsigned int Rows>
198 struct YamlObject::Getter<ipa::Vector<T, Rows>> {
199  std::optional<ipa::Vector<T, Rows>> get(const YamlObject &obj) const
200  {
201  if (!ipa::vectorValidateYaml(obj, Rows))
202  return std::nullopt;
203 
204  ipa::Vector<T, Rows> vector;
205 
206  unsigned int i = 0;
207  for (const YamlObject &entry : obj.asList()) {
208  const auto value = entry.get<T>();
209  if (!value)
210  return std::nullopt;
211  vector[i++] = *value;
212  }
213 
214  return vector;
215  }
216 };
217 #endif /* __DOXYGEN__ */
218 
219 } /* namespace libcamera */
T & operator[](size_t i)
Index to an element in the vector.
Definition: vector.h:50
const T & operator[](size_t i) const
Index to an element in the vector.
Definition: vector.h:44
Top-level libcamera namespace.
Definition: backtrace.h:17
constexpr Vector< T, Rows > operator+(const Vector< T, Rows > &other) const
Add two vectors together.
Definition: vector.h:96
ListAdapter asList() const
Wrap a list YamlObject in an adapter that exposes iterators.
Definition: yaml_parser.h:206
constexpr double length2() const
Get the squared length of the vector.
Definition: vector.h:128
bool operator==(const Vector< T, Rows > &lhs, const Vector< T, Rows > &rhs)
Compare vectors for equality.
Definition: vector.h:161
#define ASSERT(condition)
Abort program execution if assertion fails.
constexpr double length() const
Get the length of the vector.
Definition: vector.h:136
constexpr T x() const
Convenience function to access the first element of the vector.
Definition: vector.h:59
#define LOG_DECLARE_CATEGORY(name)
Declare a category of log messages.
constexpr Vector()=default
Construct a zero vector.
Matrix class.
constexpr Vector(const std::array< T, Rows > &data)
Construct vector from supplied data.
Definition: vector.h:38
constexpr Vector< T, Rows > operator-(const Vector< T, Rows > &other) const
Subtract one vector from another.
Definition: vector.h:88
Vector class.
Definition: vector.h:33
constexpr Vector< T, Rows > operator-() const
Negate a Vector by negating both all of its coordinates.
Definition: vector.h:80
constexpr T z() const
Convenience function to access the third element of the vector.
Definition: vector.h:75
A class representing the tree structure of the YAML content.
Definition: yaml_parser.h:27
bool operator!=(const Vector< T, Rows > &lhs, const Vector< T, Rows > &rhs)
Compare vectors for inequality.
Definition: vector.h:172
constexpr T y() const
Convenience function to access the second element of the vector.
Definition: vector.h:67
constexpr Vector< T, Rows > operator/(T factor) const
Divide the vector by a scalar.
Definition: vector.h:120
Matrix class.
Definition: matrix.h:30
Logging infrastructure.
constexpr Vector< T, Rows > operator*(T factor) const
Multiply the vector by a scalar.
Definition: vector.h:112
A YAML parser helper.
constexpr T operator*(const Vector< T, Rows > &other) const
Compute the dot product.
Definition: vector.h:104