libcamera: src/ipa/libipa/vector.h Source File

libcamera v0.3.2 Supporting cameras in Linux since 2019

Loading...
Searching...
No Matches
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
17#include "libcamera/internal/yaml_parser.h"
18
19#include "matrix.h"
20
21namespace libcamera {
22
23 LOG_DECLARE_CATEGORY(Vector)
24
25namespace ipa {
26
27#ifndef __DOXYGEN__
28template<typename T, unsigned int Rows,
29 std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr>
30#else
31template<typename T, unsigned int Rows>
32#endif /* __DOXYGEN__ */
33 class Vector
34{
35public:
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
141private:
142 std::array<T, Rows> data_;
143};
144
145template<typename T, unsigned int Rows, unsigned int Cols>
146 Vector<T, Rows> operator*(const Matrix<T, Rows, Cols> &m, const Vector<T, Cols> &v)
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
160template<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
171template<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__
178bool vectorValidateYaml(const YamlObject &obj, unsigned int size);
179#endif /* __DOXYGEN__ */
180
181} /* namespace ipa */
182
183#ifndef __DOXYGEN__
184template<typename T, unsigned int Rows>
185std::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
197template<typename T, unsigned int Rows>
198struct 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 */
libcamera::YamlObject
A class representing the tree structure of the YAML content.
Definition yaml_parser.h:27
libcamera::YamlObject::get
std::optional< T > get() const
Parse the YamlObject as a T value.
Definition yaml_parser.h:174
libcamera::ipa::Matrix
Matrix class.
Definition matrix.h:31
libcamera::ipa::Vector
Vector class.
Definition vector.h:34
libcamera::ipa::Vector::y
constexpr T y() const
Convenience function to access the second element of the vector.
Definition vector.h:67
libcamera::ipa::Vector::x
constexpr T x() const
Convenience function to access the first element of the vector.
Definition vector.h:59
libcamera::ipa::Vector::operator-
constexpr Vector< T, Rows > operator-(const Vector< T, Rows > &other) const
Subtract one vector from another.
Definition vector.h:88
libcamera::ipa::Vector::Vector
constexpr Vector()=default
Construct a zero vector.
libcamera::ipa::Vector::operator*
constexpr Vector< T, Rows > operator*(T factor) const
Multiply the vector by a scalar.
Definition vector.h:112
libcamera::ipa::Vector::operator+
constexpr Vector< T, Rows > operator+(const Vector< T, Rows > &other) const
Add two vectors together.
Definition vector.h:96
libcamera::ipa::Vector::operator/
constexpr Vector< T, Rows > operator/(T factor) const
Divide the vector by a scalar.
Definition vector.h:120
libcamera::ipa::Vector::Vector
constexpr Vector(const std::array< T, Rows > &data)
Construct vector from supplied data.
Definition vector.h:38
libcamera::ipa::Vector::operator-
constexpr Vector< T, Rows > operator-() const
Negate a Vector by negating both all of its coordinates.
Definition vector.h:80
libcamera::ipa::Vector::length
constexpr double length() const
Get the length of the vector.
Definition vector.h:136
libcamera::ipa::Vector::operator[]
const T & operator[](size_t i) const
Index to an element in the vector.
Definition vector.h:44
libcamera::ipa::Vector::operator*
constexpr T operator*(const Vector< T, Rows > &other) const
Compute the dot product.
Definition vector.h:104
libcamera::ipa::Vector::length2
constexpr double length2() const
Get the squared length of the vector.
Definition vector.h:128
libcamera::ipa::Vector::z
constexpr T z() const
Convenience function to access the third element of the vector.
Definition vector.h:75
libcamera::ipa::Vector::operator[]
T & operator[](size_t i)
Index to an element in the vector.
Definition vector.h:50
log.h
Logging infrastructure.
LOG_DECLARE_CATEGORY
#define LOG_DECLARE_CATEGORY(name)
Declare a category of log messages.
Definition log.h:46
ASSERT
#define ASSERT(condition)
Abort program execution if assertion fails.
Definition log.h:126
matrix.h
Matrix class.
libcamera::ipa::operator==
bool operator==(const Vector< T, Rows > &lhs, const Vector< T, Rows > &rhs)
Compare vectors for equality.
Definition vector.h:161
libcamera::ipa::operator*
Matrix< U, Rows, Cols > operator*(T d, const Matrix< U, Rows, Cols > &m)
Multiply the matrix by a scalar.
Definition matrix.h:103
libcamera::ipa::operator!=
bool operator!=(const Vector< T, Rows > &lhs, const Vector< T, Rows > &rhs)
Compare vectors for inequality.
Definition vector.h:172
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
libcamera::operator<<
std::ostream & operator<<(std::ostream &out, const Point &p)
Insert a text representation of a Point into an output stream.
Definition geometry.cpp:91
yaml_parser.h
A YAML parser helper.