Line data Source code
1 : #ifndef EE3A9F55_5D25_4810_82B7_406BD09800A1
2 : #define EE3A9F55_5D25_4810_82B7_406BD09800A1
3 :
4 : #include "base/first_include.h" // IWYU pragma: keep
5 : #include "math/linalg/matrix.h"
6 : #include "math/linalg/vector.h" // IWYU pragma: keep
7 :
8 : namespace tracking
9 : {
10 : namespace math
11 : {
12 :
13 : template <typename ValueType_, sint32 Rows_, sint32 Cols_, bool IsRowMajor_>
14 : class MatrixColumnView;
15 :
16 : /// \brief Non-owning submatrix view providing arithmetic operations on Matrix objects
17 : ///
18 : /// MatrixView provides a lightweight, zero-copy interface to access and manipulate
19 : /// rectangular subregions of a Matrix. It supports arithmetic operations, matrix
20 : /// multiplication, and element access without copying data.
21 : ///
22 : /// \tparam ValueType_ The atomic data type of internal elements
23 : /// \tparam Rows_ The number of rows in the viewed matrix
24 : /// \tparam Cols_ The number of columns in the viewed matrix
25 : ///
26 : /// \note This is a non-owning view - the lifetime of the MatrixView must not exceed
27 : /// the lifetime of the underlying Matrix object.
28 : /// \note All operations are performed in-place on the viewed subregion.
29 : /// \note Performance: Zero-copy operations, minimal overhead compared to direct matrix access.
30 : ///
31 : /// \see Matrix for the underlying matrix implementation
32 : /// \see MatrixRowView for single-row views
33 : /// \see MatrixColumnView for single-column views
34 : template <typename ValueType_, sint32 Rows_, sint32 Cols_>
35 : class MatrixView TEST_REMOVE_FINAL
36 : {
37 : public:
38 : /// \brief Construct a submatrix view on an existing Matrix
39 : ///
40 : /// Creates a non-owning view of a rectangular subregion of the given matrix.
41 : /// The view covers rows from rowBegin to rowEnd-1 and columns from colBegin to colEnd-1.
42 : ///
43 : /// \param[in] matrix The matrix to view (must outlive the view)
44 : /// \param[in] rowBegin Starting row index (inclusive, 0-based)
45 : /// \param[in] colBegin Starting column index (inclusive, 0-based)
46 : /// \param[in] rowEnd Ending row index (inclusive, 0-based)
47 : /// \param[in] colEnd Ending column index (inclusive, 0-based)
48 : ///
49 : /// \note The view does not own the data - ensure the matrix lifetime exceeds the view's
50 : /// \note Bounds are not checked at runtime - invalid indices may cause undefined behavior
51 : /// \note For full matrix views, use rowBegin=0, colBegin=0, rowEnd=Rows-1, colEnd=Cols-1
52 : explicit MatrixView(const Matrix<ValueType_, Rows_, Cols_>& matrix,
53 : const sint32 rowBegin,
54 : const sint32 colBegin,
55 : const sint32 rowEnd,
56 : const sint32 colEnd);
57 :
58 : /// \brief Access element at specified position within the view
59 : ///
60 : /// \param[in] row Row index relative to the view (0 to getRowCount()-1)
61 : /// \param[in] col Column index relative to the view (0 to getColCount()-1)
62 : /// \return The element value
63 : ///
64 : /// \note No bounds checking - invalid indices cause undefined behavior
65 : [[nodiscard]] auto operator()(sint32 row, sint32 col) const -> ValueType_;
66 :
67 : /// \brief Get the number of rows in this view
68 : /// \return Number of rows
69 9 : [[nodiscard]] auto getRowCount() const -> sint32 { return _rowCount; }
70 : /// \brief Get the number of columns in this view
71 : /// \return Number of columns
72 23 : [[nodiscard]] auto getColCount() const -> sint32 { return _colCount; }
73 :
74 : /// \brief Add this view to a full matrix
75 : ///
76 : /// Performs element-wise addition of the view's elements to the corresponding
77 : /// positions in the other matrix. The result is a full matrix.
78 : ///
79 : /// \param[in] other The matrix to add to
80 : /// \return New matrix containing the result
81 : [[nodiscard]] auto operator+(const Matrix<ValueType_, Rows_, Cols_>& other) const -> Matrix<ValueType_, Rows_, Cols_>;
82 : /// \brief Subtract a full matrix from this view
83 : ///
84 : /// Performs element-wise subtraction: view - other
85 : ///
86 : /// \param[in] other The matrix to subtract
87 : /// \return New matrix containing the result
88 : [[nodiscard]] auto operator-(const Matrix<ValueType_, Rows_, Cols_>& other) const -> Matrix<ValueType_, Rows_, Cols_>;
89 : /// \brief Multiply this view by another matrix
90 : ///
91 : /// Performs matrix multiplication: view * other
92 : ///
93 : /// \tparam Cols2 Number of columns in the other matrix
94 : /// \param[in] other Matrix to multiply with
95 : /// \return Result matrix of size Rows x Cols2
96 : template <sint32 Cols2_>
97 : [[nodiscard]] auto operator*(const Matrix<ValueType_, Cols_, Cols2_>& other) const -> Matrix<ValueType_, Rows_, Cols2_>;
98 :
99 : /// \brief Add a scalar to all elements of this view
100 : ///
101 : /// \param[in] other Scalar value to add
102 : /// \return New matrix with scalar added to all elements
103 : [[nodiscard]] auto operator+(const ValueType_& other) const -> Matrix<ValueType_, Rows_, Cols_>;
104 : /// \brief Subtract a scalar from all elements of this view
105 : ///
106 : /// \param[in] other Scalar value to subtract
107 : /// \return New matrix with scalar subtracted from all elements
108 : [[nodiscard]] auto operator-(const ValueType_& other) const -> Matrix<ValueType_, Rows_, Cols_>;
109 : /// \brief Multiply all elements of this view by a scalar
110 : ///
111 : /// \param[in] other Scalar value to multiply by
112 : /// \return New matrix with all elements multiplied by the scalar
113 : [[nodiscard]] auto operator*(const ValueType_& other) const -> Matrix<ValueType_, Rows_, Cols_>;
114 : /// \brief Divide all elements of this view by a scalar
115 : ///
116 : /// \param[in] other Scalar value to divide by
117 : /// \return New matrix with all elements divided by the scalar
118 : [[nodiscard]] auto operator/(const ValueType_& other) const -> Matrix<ValueType_, Rows_, Cols_>;
119 :
120 : // clang-format off
121 : TEST_REMOVE_PROTECTED:
122 : ; // workaround for correct indentation
123 : // clang-format on
124 : const Matrix<ValueType_, Rows_, Cols_>& _matrix;
125 : const sint32 _rowBegin;
126 : const sint32 _colBegin;
127 : const sint32 _rowCount;
128 : const sint32 _colCount;
129 : };
130 :
131 : /// \brief Add two matrix views element-wise
132 : ///
133 : /// Performs element-wise addition on the overlapping region.
134 : /// The result dimensions are the minimum of the input dimensions.
135 : ///
136 : /// \tparam ValueType_ The atomic data type of internal elements
137 : /// \tparam RowsA_ Rows of first view
138 : /// \tparam ColsA_ Cols of first view
139 : /// \tparam RowsB_ Rows of second view
140 : /// \tparam ColsB_ Cols of second view
141 : /// \param[in] a First view
142 : /// \param[in] b Second view
143 : /// \return Matrix containing the element-wise sum
144 : template <typename ValueType_, sint32 RowsA_, sint32 ColsA_, sint32 RowsB_, sint32 ColsB_>
145 : [[nodiscard]] auto operator+(const MatrixView<ValueType_, RowsA_, ColsA_>& a, const MatrixView<ValueType_, RowsB_, ColsB_>& b)
146 : -> Matrix<ValueType_, std::min(RowsA_, RowsB_), std::min(ColsA_, ColsB_)>;
147 :
148 : /// \brief Subtract two matrix views element-wise
149 : ///
150 : /// Performs element-wise subtraction: a - b
151 : ///
152 : /// \tparam ValueType_ The atomic data type of internal elements
153 : /// \tparam RowsA_ Rows of first view
154 : /// \tparam ColsA_ Cols of first view
155 : /// \tparam RowsB_ Rows of second view
156 : /// \tparam ColsB_ Cols of second view
157 : /// \param[in] a First view
158 : /// \param[in] b Second view
159 : /// \return Matrix containing the element-wise difference
160 : template <typename ValueType_, sint32 RowsA_, sint32 ColsA_, sint32 RowsB_, sint32 ColsB_>
161 : [[nodiscard]] auto operator-(const MatrixView<ValueType_, RowsA_, ColsA_>& a, const MatrixView<ValueType_, RowsB_, ColsB_>& b)
162 : -> Matrix<ValueType_, std::min(RowsA_, RowsB_), std::min(ColsA_, ColsB_)>;
163 :
164 : /// \brief Multiply two matrix views
165 : ///
166 : /// Performs matrix multiplication: a * b
167 : /// Requires that ColsA == RowsB
168 : ///
169 : /// \tparam ValueType_ The atomic data type of internal elements
170 : /// \tparam RowsA_ Rows of first view
171 : /// \tparam ColsA_ Cols of first view
172 : /// \tparam RowsB_ Rows of second view
173 : /// \tparam ColsB_ Cols of second view
174 : /// \param[in] a First view
175 : /// \param[in] b Second view
176 : /// \return Matrix containing the product
177 : template <typename ValueType_, sint32 RowsA_, sint32 ColsA_, sint32 RowsB_, sint32 ColsB_>
178 : [[nodiscard]] auto operator*(const MatrixView<ValueType_, RowsA_, ColsA_>& a,
179 : const MatrixView<ValueType_, RowsB_, ColsB_>& b) -> Matrix<ValueType_, RowsA_, ColsB_>;
180 :
181 :
182 : } // namespace math
183 : } // namespace tracking
184 :
185 : #endif // EE3A9F55_5D25_4810_82B7_406BD09800A1
|