1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
/************************************************************************
*
* Copyright (C) 2009-2025 IRCAD France
* Copyright (C) 2012-2021 IHU Strasbourg
*
* This file is part of Sight.
*
* Sight is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sight. If not, see <https://www.gnu.org/licenses/>.
*
***********************************************************************/
#pragma once
#include "container.hpp"
#include <core/compare.hpp>
#include <array>
#include <iostream>
namespace sight::data
{
/**
* @brief This class represents a 3D transformation matrix (4x4).
*
* Our convention is a row-major representation.
*/
class SIGHT_DATA_CLASS_API matrix4 final : public container<std::array<double,
16> >
{
public:
/// This will enable common collection constructors / assignment operators
using container<matrix4::container_t>::container;
using container<matrix4::container_t>::operator=;
SIGHT_DECLARE_CLASS(matrix4, container<matrix4::container_t>);
/// Constructors
/// @{
constexpr matrix4() noexcept;
inline matrix4(std::initializer_list<value_type> _init_list);
template<typename T>
inline matrix4(const T& _data);
/// @}
//! @brief destructor
SIGHT_DATA_API ~matrix4() noexcept final = default;
/// Assignment operator
/// @{
inline matrix4& operator=(std::initializer_list<value_type> _init_list);
template<typename T>
inline matrix4& operator=(const T& _data);
/// @}
/**
* @{
* @brief Get/Set value of the coefficient in the given position (matrix[l][c])
*/
[[nodiscard]] constexpr matrix4::value_type& operator()(std::size_t _l, std::size_t _c) noexcept;
[[nodiscard]] constexpr const matrix4::value_type& operator()(std::size_t _l, std::size_t _c) const noexcept;
/// @}
/// maximum size of the matrix (MATRIX_SIZE x MATRIX_SIZE)
static constexpr std::size_t MATRIX_SIZE = 4;
/// Print the coefficients of the matrix
friend std::ostream& operator<<(std::ostream& _s, const matrix4& _mat)
{
for(std::size_t l = 0 ; l < MATRIX_SIZE ; l++)
{
for(std::size_t c = 0 ; c < MATRIX_SIZE ; c++)
{
_s << _mat(l, c) << "\t";
}
_s << std::endl;
}
return _s;
}
/// Defines shallow copy
/// @throws data::exception if an errors occurs during copy
/// @param[in] _source the source object to copy
SIGHT_DATA_API void shallow_copy(const object::csptr& _source) final;
/// Defines deep copy
/// @throws data::exception if an errors occurs during copy
/// @param _source source object to copy
/// @param _cache cache used to deduplicate pointers
SIGHT_DATA_API void deep_copy(
const object::csptr& _source,
const std::unique_ptr<deep_copy_cache_t>& _cache = std::make_unique<deep_copy_cache_t>()
) final;
/// Equality comparison operators
/// @{
constexpr bool operator==(const matrix4& _other) const noexcept;
constexpr bool operator!=(const matrix4& _other) const noexcept;
template<typename T>
constexpr bool operator==(const T& _other) const noexcept;
template<typename T>
constexpr bool operator!=(const T& _other) const noexcept;
/// @}
[[nodiscard]] static constexpr container_t identity() noexcept;
[[nodiscard]] constexpr bool is_identity() noexcept;
/// Convert the matrix to any "ordered" container type.
template<typename T = container_t>
[[nodiscard]] constexpr T values() const noexcept;
/// Helper function to set the orientation (9 cosines - row major) from a container
/// @param [in] _orientation in a container
template<typename T = std::array<double, 9> >
inline void set_orientation(const T& _orientation);
/// Helper function to return the orientation part of the matrix (9 cosines - row major) to a container
template<typename T = std::array<double, 9> >
[[nodiscard]] constexpr T orientation() const noexcept;
/// Helper function to set the position from a container
/// @param [in] _position in DICOM format
template<typename T = std::array<double, 3> >
inline void set_position(const T& _position);
/// Helper function to return the position part of the matrix (3 doubles) to a container
template<typename T = std::array<double, 3> >
[[nodiscard]] constexpr T position() const noexcept;
protected:
static constexpr container_t IDENTITY = {
1., 0., 0., 0.,
0., 1., 0., 0.,
0., 0., 1., 0.,
0., 0., 0., 1.
};
};
constexpr matrix4::matrix4() noexcept
{
*this = IDENTITY;
}
//------------------------------------------------------------------------------
inline matrix4::matrix4(std::initializer_list<value_type> _init_list)
{
this->operator=<std::initializer_list<value_type> >(_init_list);
}
//------------------------------------------------------------------------------
inline matrix4& matrix4::operator=(std::initializer_list<value_type> _init_list)
{
this->operator=<std::initializer_list<value_type> >(_init_list);
return *this;
}
//------------------------------------------------------------------------------
template<typename T>
inline matrix4::matrix4(const T& _data)
{
///@todo make this functions constexpr once we support C++23 which will support SIGHT_ASSERT
SIGHT_ASSERT(
std::string(__func__) + " need 16 elements: " + std::to_string(_data.size()) + " given.",
_data.size() == this->size()
);
std::copy(_data.begin(), _data.end(), this->begin());
}
//------------------------------------------------------------------------------
template<typename T>
inline matrix4& matrix4::operator=(const T& _data)
{
///@todo make this functions constexpr once we support C++23 which will support SIGHT_ASSERT
SIGHT_ASSERT(
std::string(__func__) + " need 16 elements: " + std::to_string(_data.size()) + " given.",
_data.size() == this->size()
);
std::copy(_data.begin(), _data.end(), this->begin());
return *this;
}
//------------------------------------------------------------------------------
template<typename T>
constexpr bool matrix4::operator==(const T& _other) const noexcept
{
return core::is_equal(*this, _other);
}
//------------------------------------------------------------------------------
template<typename T>
constexpr bool matrix4::operator!=(const T& _other) const noexcept
{
return !(*this == _other);
}
//------------------------------------------------------------------------------
template<typename T>
[[nodiscard]] constexpr T matrix4::values() const noexcept
{
T values;
if constexpr(!core::is_container_dynamic<T>::value)
{
std::copy(this->cbegin(), this->cend(), values.begin());
}
else
{
values.reserve(this->size());
std::copy(this->cbegin(), this->cend(), std::back_inserter(values));
}
return values;
}
//------------------------------------------------------------------------------
template<typename T>
inline void matrix4::set_orientation(const T& _orientation)
{
/// @todo make this constexpr once we support C++23 which will support std::stringstream
// If size() is available, check the size of the container.
if constexpr(core::is_container<T>::value)
{
SIGHT_ASSERT(
"matrix4::set_orientation() need 9 elements.",
_orientation.size() >= 9
);
}
*this = {
_orientation[0], _orientation[1], _orientation[2], (*this)(0, 3),
_orientation[3], _orientation[4], _orientation[5], (*this)(1, 3),
_orientation[6], _orientation[7], _orientation[8], (*this)(2, 3),
0.0, 0.0, 0.0, 1.0
};
}
//------------------------------------------------------------------------------
template<typename T>
[[nodiscard]] constexpr T matrix4::orientation() const noexcept
{
return T {
(*this)(0, 0), (*this)(0, 1), (*this)(0, 2),
(*this)(1, 0), (*this)(1, 1), (*this)(1, 2),
(*this)(2, 0), (*this)(2, 1), (*this)(2, 2)
};
}
//------------------------------------------------------------------------------
template<typename T>
inline void matrix4::set_position(const T& _position)
{
/// @todo make this constexpr once we support C++23 which will support std::stringstream
// If size() is available, check the size of the container.
if constexpr(core::is_container<T>::value)
{
SIGHT_ASSERT(
"matrix4::set_position() need 3 elements.",
_position.size() >= 3
);
}
(*this)(0, 3) = _position[0];
(*this)(1, 3) = _position[1];
(*this)(2, 3) = _position[2];
}
//------------------------------------------------------------------------------
template<typename T>
[[nodiscard]] constexpr T matrix4::position() const noexcept
{
return T {(*this)(0, 3), (*this)(1, 3), (*this)(2, 3)};
}
//------------------------------------------------------------------------------
constexpr matrix4::value_type& matrix4::operator()(std::size_t _l, std::size_t _c) noexcept
{
const std::size_t pos = _l * MATRIX_SIZE + _c;
return this->at(pos);
}
//------------------------------------------------------------------------------
constexpr const matrix4::value_type& matrix4::operator()(std::size_t _l, std::size_t _c) const noexcept
{
const std::size_t pos = _l * MATRIX_SIZE + _c;
return this->at(pos);
}
//-----------------------------------------------------------------------------
constexpr matrix4::container_t matrix4::identity() noexcept
{
return matrix4::IDENTITY;
}
//------------------------------------------------------------------------------
constexpr bool matrix4::is_identity() noexcept
{
return this->operator==(matrix4::IDENTITY);
}
//------------------------------------------------------------------------------
constexpr bool matrix4::operator==(const matrix4& _other) const noexcept
{
return base_class_t::operator==(_other);
}
//------------------------------------------------------------------------------
constexpr bool matrix4::operator!=(const matrix4& _other) const noexcept
{
return !(*this == _other);
}
} // namespace sight::data
|