libcamera v0.3.0+65-6ddd79b5
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 <algorithm>
10#include <array>
11#include <cmath>
12#include <sstream>
13
14#include <libcamera/base/log.h>
15#include <libcamera/base/span.h>
16
18
19namespace libcamera {
20
22
23namespace ipa {
24
25#ifndef __DOXYGEN__
26template<typename T, unsigned int Rows,
27 std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr>
28#else
29template<typename T, unsigned int Rows>
30#endif /* __DOXYGEN__ */
31class Vector
32{
33public:
34 constexpr Vector() = default;
35
36 constexpr Vector(const std::array<T, Rows> &data)
37 {
38 for (unsigned int i = 0; i < Rows; i++)
39 data_[i] = data[i];
40 }
41
43 {
44 if (yaml.size() != Rows) {
45 LOG(Vector, Error)
46 << "Wrong number of values in vector: expected "
47 << Rows << ", got " << yaml.size();
48 return -EINVAL;
49 }
50
51 unsigned int i = 0;
52 for (const auto &x : yaml.asList()) {
53 auto value = x.get<T>();
54 if (!value) {
55 LOG(Vector, Error) << "Failed to read vector value";
56 return -EINVAL;
57 }
58
59 data_[i++] = *value;
60 }
61
62 return 0;
63 }
64
65 const T &operator[](size_t i) const
66 {
67 ASSERT(i < data_.size());
68 return data_[i];
69 }
70
71 T &operator[](size_t i)
72 {
73 ASSERT(i < data_.size());
74 return data_[i];
75 }
76
77#ifndef __DOXYGEN__
78 template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 1>>
79#endif /* __DOXYGEN__ */
80 constexpr T x() const
81 {
82 return data_[0];
83 }
84
85#ifndef __DOXYGEN__
86 template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 2>>
87#endif /* __DOXYGEN__ */
88 constexpr T y() const
89 {
90 return data_[1];
91 }
92
93#ifndef __DOXYGEN__
94 template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 3>>
95#endif /* __DOXYGEN__ */
96 constexpr T z() const
97 {
98 return data_[2];
99 }
100
101 constexpr Vector<T, Rows> operator-() const
102 {
103 Vector<T, Rows> ret;
104 for (unsigned int i = 0; i < Rows; i++)
105 ret[i] = -data_[i];
106 return ret;
107 }
108
109 constexpr Vector<T, Rows> operator-(const Vector<T, Rows> &other) const
110 {
111 Vector<T, Rows> ret;
112 for (unsigned int i = 0; i < Rows; i++)
113 ret[i] = data_[i] - other[i];
114 return ret;
115 }
116
117 constexpr Vector<T, Rows> operator+(const Vector<T, Rows> &other) const
118 {
119 Vector<T, Rows> ret;
120 for (unsigned int i = 0; i < Rows; i++)
121 ret[i] = data_[i] + other[i];
122 return ret;
123 }
124
125 constexpr T operator*(const Vector<T, Rows> &other) const
126 {
127 T ret = 0;
128 for (unsigned int i = 0; i < Rows; i++)
129 ret += data_[i] * other[i];
130 return ret;
131 }
132
133 constexpr Vector<T, Rows> operator*(T factor) const
134 {
135 Vector<T, Rows> ret;
136 for (unsigned int i = 0; i < Rows; i++)
137 ret[i] = data_[i] * factor;
138 return ret;
139 }
140
141 constexpr Vector<T, Rows> operator/(T factor) const
142 {
143 Vector<T, Rows> ret;
144 for (unsigned int i = 0; i < Rows; i++)
145 ret[i] = data_[i] / factor;
146 return ret;
147 }
148
149 constexpr double length2() const
150 {
151 double ret = 0;
152 for (unsigned int i = 0; i < Rows; i++)
153 ret += data_[i] * data_[i];
154 return ret;
155 }
156
157 constexpr double length() const
158 {
159 return std::sqrt(length2());
160 }
161
162private:
163 std::array<T, Rows> data_;
164};
165
166template<typename T, unsigned int Rows>
167bool operator==(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs)
168{
169 for (unsigned int i = 0; i < Rows; i++) {
170 if (lhs[i] != rhs[i])
171 return false;
172 }
173
174 return true;
175}
176
177template<typename T, unsigned int Rows>
178bool operator!=(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs)
179{
180 return !(lhs == rhs);
181}
182
183} /* namespace ipa */
184
185#ifndef __DOXYGEN__
186template<typename T, unsigned int Rows>
187std::ostream &operator<<(std::ostream &out, const ipa::Vector<T, Rows> &v)
188{
189 out << "Vector { ";
190 for (unsigned int i = 0; i < Rows; i++) {
191 out << v[i];
192 out << ((i + 1 < Rows) ? ", " : " ");
193 }
194 out << " }";
195
196 return out;
197}
198#endif /* __DOXYGEN__ */
199
200} /* namespace libcamera */
A class representing the tree structure of the YAML content.
Definition: yaml_parser.h:26
std::size_t size() const
Retrieve the number of elements in a dictionary or list YamlObject.
Definition: yaml_parser.cpp:84
ListAdapter asList() const
Wrap a list YamlObject in an adapter that exposes iterators.
Definition: yaml_parser.h:207
Vector class.
Definition: vector.h:32
int readYaml(const libcamera::YamlObject &yaml)
Populate the vector with yaml data.
Definition: vector.h:42
constexpr T x() const
Convenience function to access the first element of the vector.
Definition: vector.h:80
constexpr Vector< T, Rows > operator-(const Vector< T, Rows > &other) const
Subtract one vector from another.
Definition: vector.h:109
constexpr T operator*(const Vector< T, Rows > &other) const
Compute the dot product.
Definition: vector.h:125
constexpr Vector< T, Rows > operator*(T factor) const
Multiply the vector by a scalar.
Definition: vector.h:133
constexpr double length2() const
Get the squared length of the vector.
Definition: vector.h:149
constexpr Vector(const std::array< T, Rows > &data)
Construct vector from supplied data.
Definition: vector.h:36
const T & operator[](size_t i) const
Index to an element in the vector.
Definition: vector.h:65
constexpr double length() const
Get the length of the vector.
Definition: vector.h:157
constexpr T z() const
Convenience function to access the third element of the vector.
Definition: vector.h:96
constexpr Vector()=default
Construct a zero vector.
constexpr Vector< T, Rows > operator/(T factor) const
Divide the vector by a scalar.
Definition: vector.h:141
constexpr Vector< T, Rows > operator+(const Vector< T, Rows > &other) const
Add two vectors together.
Definition: vector.h:117
constexpr Vector< T, Rows > operator-() const
Negate a Vector by negating both all of its coordinates.
Definition: vector.h:101
T & operator[](size_t i)
Definition: vector.h:71
constexpr T y() const
Convenience function to access the second element of the vector.
Definition: vector.h:88
Logging infrastructure.
#define LOG_DECLARE_CATEGORY(name)
Declare a category of log messages.
#define LOG(category, severity)
Log a message.
#define ASSERT(condition)
Abort program execution if assertion fails.
bool operator==(const Vector< T, Rows > &lhs, const Vector< T, Rows > &rhs)
Compare vectors for equality.
Definition: vector.h:167
bool operator!=(const Vector< T, Rows > &lhs, const Vector< T, Rows > &rhs)
Compare vectors for inequality.
Definition: vector.h:178
Top-level libcamera namespace.
Definition: backtrace.h:17
std::ostream & operator<<(std::ostream &out, const Point &p)
Insert a text representation of a Point into an output stream.
Definition: geometry.cpp:91
A YAML parser helper.