|
|
@@ -1,431 +0,0 @@
|
|
|
-#ifndef SOPHIAR2_VECTORX_HTTP
|
|
|
-#define SOPHIAR2_VECTORX_HTTP
|
|
|
-
|
|
|
-#include "core/small_obj.hpp"
|
|
|
-#include "utility/versatile_buffer.hpp"
|
|
|
-
|
|
|
-#include <fmt/format.h>
|
|
|
-
|
|
|
-#include <Eigen/Core>
|
|
|
-
|
|
|
-#include <algorithm>
|
|
|
-#include <array>
|
|
|
-#include <cassert>
|
|
|
-#include <initializer_list>
|
|
|
-#include <stdexcept>
|
|
|
-#include <type_traits>
|
|
|
-#include <utility>
|
|
|
-
|
|
|
-namespace sophiar {
|
|
|
-
|
|
|
- template<typename FloatType, size_t Length>
|
|
|
- class vectorx;
|
|
|
-
|
|
|
- template<typename FreqTag, typename FloatType, size_t Length>
|
|
|
- class vectorx_obj;
|
|
|
-
|
|
|
- template<typename FloatPointerType, size_t Length>
|
|
|
- class vectorx_view {
|
|
|
- public:
|
|
|
-
|
|
|
- using FloatType = std::remove_pointer_t<FloatPointerType>;
|
|
|
- using RealFloatType = std::remove_const_t<FloatType>;
|
|
|
-
|
|
|
- static_assert(std::is_pointer_v<FloatPointerType>);
|
|
|
- static_assert(std::is_arithmetic_v<FloatType>);
|
|
|
-
|
|
|
- using eigen_row_vector_type = Eigen::Map<Eigen::Matrix<RealFloatType, 1, Length>>;
|
|
|
- using eigen_const_row_vector_type = Eigen::Map<const Eigen::Matrix<RealFloatType, 1, Length>>;
|
|
|
- using eigen_column_vector_type = Eigen::Map<Eigen::Matrix<RealFloatType, Length, 1>>;
|
|
|
- using eigen_const_column_vector_type = Eigen::Map<const Eigen::Matrix<RealFloatType, Length, 1>>;
|
|
|
-
|
|
|
- explicit vectorx_view(FloatPointerType other_data)
|
|
|
- : data(other_data) {}
|
|
|
-
|
|
|
- vectorx_view(vectorx<RealFloatType, Length> &other)
|
|
|
- : data(other.m.data()) {}
|
|
|
-
|
|
|
- template<typename ThisFloatType = FloatType,
|
|
|
- typename = std::enable_if_t<std::is_const_v<ThisFloatType>>>
|
|
|
- vectorx_view(const vectorx<RealFloatType, Length> &other)
|
|
|
- : data(other.m.data()) {}
|
|
|
-
|
|
|
- vectorx_view(const vectorx_view<RealFloatType *, Length> &other)
|
|
|
- : data(other.data) {}
|
|
|
-
|
|
|
- template<typename ThisFloatType = FloatType,
|
|
|
- typename = std::enable_if_t<std::is_const_v<ThisFloatType>>>
|
|
|
- vectorx_view(const vectorx_view<const RealFloatType *, Length> &other)
|
|
|
- : data(other.data) {}
|
|
|
-
|
|
|
- auto to_vectorx() const {
|
|
|
- return vectorx<RealFloatType, Length>(*this);
|
|
|
- }
|
|
|
-
|
|
|
- template<typename FreqTag>
|
|
|
- auto to_vectorx_obj() const {
|
|
|
- return vectorx_obj<FreqTag, RealFloatType, Length>::new_instance(*this);
|
|
|
- };
|
|
|
-
|
|
|
- template<size_t Pos,
|
|
|
- typename ThisFloatType = FloatType,
|
|
|
- typename = std::enable_if_t<!std::is_const_v<ThisFloatType>>>
|
|
|
- FloatType &at() {
|
|
|
- static_assert(Pos < Length);
|
|
|
- return data[Pos];
|
|
|
- }
|
|
|
-
|
|
|
- template<size_t Pos>
|
|
|
- const FloatType &at() const {
|
|
|
- static_assert(Pos < Length);
|
|
|
- return data[Pos];
|
|
|
- }
|
|
|
-
|
|
|
- template<typename ThisFloatType = FloatType,
|
|
|
- typename = std::enable_if_t<!std::is_const_v<ThisFloatType>>>
|
|
|
- FloatType &at(size_t pos) {
|
|
|
- assert(pos < Length);
|
|
|
- return data[pos];
|
|
|
- }
|
|
|
-
|
|
|
- const FloatType &at(size_t pos) const {
|
|
|
- assert(pos < Length);
|
|
|
- return data[pos];
|
|
|
- }
|
|
|
-
|
|
|
- template<typename ThisFloatType = FloatType,
|
|
|
- typename = std::enable_if_t<!std::is_const_v<ThisFloatType>>>
|
|
|
- FloatType &operator[](size_t pos) {
|
|
|
- return at(pos);
|
|
|
- }
|
|
|
-
|
|
|
- const FloatType &operator[](size_t pos) const {
|
|
|
- return at(pos);
|
|
|
- }
|
|
|
-
|
|
|
- template<typename OtherFloatType, size_t OtherLength,
|
|
|
- typename = std::enable_if_t<OtherLength < Length>>
|
|
|
- auto fill(const vectorx<OtherFloatType, OtherLength> &other) {
|
|
|
- copy_from_vectorx<OtherLength>(other);
|
|
|
- return vectorx_view<FloatPointerType, Length - OtherLength>(data + OtherLength);
|
|
|
- };
|
|
|
-
|
|
|
- template<typename OtherFloatPointerType, size_t OtherLength,
|
|
|
- typename = std::enable_if_t<OtherLength < Length>>
|
|
|
- auto fill(const vectorx_view<OtherFloatPointerType, OtherLength> &other) {
|
|
|
- copy_from_vectorx_view<OtherLength>(other);
|
|
|
- return vectorx_view<FloatPointerType, Length - OtherLength>(data + OtherLength);
|
|
|
- };
|
|
|
-
|
|
|
- template<typename OtherFloatType, int OtherRows, int OtherColumns,
|
|
|
- size_t CopyLength = OtherRows * OtherColumns,
|
|
|
- typename = std::enable_if_t<CopyLength < Length>>
|
|
|
- auto fill(const Eigen::Matrix<OtherFloatType, OtherRows, OtherColumns> &other) {
|
|
|
- copy_from_eigen_matrix<CopyLength>(other);
|
|
|
- return vectorx_view<FloatPointerType, Length - CopyLength>(data + CopyLength);
|
|
|
- }
|
|
|
-
|
|
|
- template<typename OtherFloatType, size_t OtherLength,
|
|
|
- typename = std::enable_if_t<OtherLength < Length>>
|
|
|
- auto fill(const OtherFloatType(&list)[OtherLength]) {
|
|
|
- copy_from_list<OtherLength>(list);
|
|
|
- return vectorx_view<FloatPointerType, Length - OtherLength>(data + OtherLength);
|
|
|
- };
|
|
|
-
|
|
|
- template<typename OtherFloatType, size_t OtherLength,
|
|
|
- typename = std::enable_if_t<OtherLength == Length>>
|
|
|
- void fill(const vectorx<OtherFloatType, OtherLength> &other) {
|
|
|
- copy_from_vectorx<OtherLength>(other);
|
|
|
- };
|
|
|
-
|
|
|
- template<typename OtherFloatPointerType, size_t OtherLength,
|
|
|
- typename = std::enable_if_t<OtherLength == Length>>
|
|
|
- void fill(const vectorx_view<OtherFloatPointerType, OtherLength> &other) {
|
|
|
- copy_from_vectorx_view<OtherLength>(other);
|
|
|
- };
|
|
|
-
|
|
|
- template<typename OtherFloatType, int OtherRows, int OtherColumns,
|
|
|
- size_t CopyLength = OtherRows * OtherColumns,
|
|
|
- typename = std::enable_if_t<CopyLength == Length>>
|
|
|
- void fill(const Eigen::Matrix<OtherFloatType, OtherRows, OtherColumns> &other) {
|
|
|
- copy_from_eigen_matrix<CopyLength>(other);
|
|
|
- }
|
|
|
-
|
|
|
- template<typename OtherFloatType, size_t OtherLength,
|
|
|
- typename = std::enable_if_t<OtherLength == Length>>
|
|
|
- void fill(const OtherFloatType(&list)[OtherLength]) {
|
|
|
- copy_from_list<OtherLength>(list);
|
|
|
- };
|
|
|
-
|
|
|
- static constexpr uint16_t length() {
|
|
|
- return sizeof(FloatType) * Length;
|
|
|
- }
|
|
|
-
|
|
|
- void write_to_buffer(versatile_buffer &buffer) const {
|
|
|
- for (size_t i = 0; i < Length; ++i) {
|
|
|
- buffer << data[i];
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- template<typename ThisFloatType = FloatType,
|
|
|
- typename = std::enable_if_t<!std::is_const_v<ThisFloatType>>>
|
|
|
- void fill_from_buffer(versatile_buffer &buffer) {
|
|
|
- for (size_t i = 0; i < Length; ++i) {
|
|
|
- buffer >> data[i];
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- template<typename ThisFloatType = FloatType,
|
|
|
- typename = std::enable_if_t<!std::is_const_v<ThisFloatType>>>
|
|
|
- auto as_eigen_row_view() {
|
|
|
- return eigen_row_vector_type(data);
|
|
|
- }
|
|
|
-
|
|
|
- auto as_eigen_const_row_view() const {
|
|
|
- return eigen_const_row_vector_type(data);
|
|
|
- }
|
|
|
-
|
|
|
- template<typename ThisFloatType = FloatType,
|
|
|
- typename = std::enable_if_t<!std::is_const_v<ThisFloatType>>>
|
|
|
- auto as_eigen_column_view() {
|
|
|
- return eigen_column_vector_type(data);
|
|
|
- }
|
|
|
-
|
|
|
- auto as_eigen_const_column_view() const {
|
|
|
- return eigen_const_column_vector_type(data);
|
|
|
- }
|
|
|
-
|
|
|
- private:
|
|
|
- FloatPointerType data;
|
|
|
-
|
|
|
- template<typename, size_t> friend
|
|
|
- class vectorx_view;
|
|
|
-
|
|
|
- template<typename, size_t> friend
|
|
|
- class vectorx;
|
|
|
-
|
|
|
- template<size_t CopyLength, typename OtherFloatType, size_t OtherLength,
|
|
|
- typename ThisFloatType = FloatType,
|
|
|
- typename = std::enable_if_t<!std::is_const_v<ThisFloatType>>>
|
|
|
- void copy_from_vectorx(const vectorx<OtherFloatType, OtherLength> &other) {
|
|
|
- static_assert(std::is_convertible_v<OtherFloatType, RealFloatType>);
|
|
|
- static_assert(CopyLength <= Length);
|
|
|
- std::copy_n(other.m.begin(), CopyLength, data);
|
|
|
- }
|
|
|
-
|
|
|
- template<size_t CopyLength, typename OtherFloatPointerType, size_t OtherLength,
|
|
|
- typename ThisFloatType = FloatType,
|
|
|
- typename = std::enable_if_t<!std::is_const_v<ThisFloatType>>>
|
|
|
- void copy_from_vectorx_view(const vectorx_view<OtherFloatPointerType, OtherLength> &other) {
|
|
|
- static_assert(std::is_convertible_v<
|
|
|
- std::remove_pointer_t<OtherFloatPointerType>, RealFloatType>);
|
|
|
- static_assert(CopyLength <= Length);
|
|
|
- std::copy_n(other.data, CopyLength, data);
|
|
|
- }
|
|
|
-
|
|
|
- template<size_t CopyLength, typename OtherFloatType, size_t OtherLength,
|
|
|
- typename ThisFloatType = FloatType,
|
|
|
- typename = std::enable_if_t<!std::is_const_v<ThisFloatType>>>
|
|
|
- void copy_from_list(const OtherFloatType(&list)[OtherLength]) {
|
|
|
- static_assert(std::is_convertible_v<OtherFloatType, RealFloatType>);
|
|
|
- static_assert(CopyLength <= Length);
|
|
|
- std::copy_n(list, CopyLength, data);
|
|
|
- }
|
|
|
-
|
|
|
- template<size_t CopyLength, typename OtherFloatType, int OtherRows, int OtherColumns,
|
|
|
- typename ThisFloatType = FloatType,
|
|
|
- typename = std::enable_if_t<!std::is_const_v<ThisFloatType>>>
|
|
|
- void copy_from_eigen_matrix(const Eigen::Matrix<OtherFloatType, OtherRows, OtherColumns> &other) {
|
|
|
- static_assert(std::is_convertible_v<OtherFloatType, RealFloatType>);
|
|
|
- static_assert(OtherRows == 1 || OtherColumns == 1);
|
|
|
- static_assert(OtherRows * OtherColumns == Length);
|
|
|
- static_assert(CopyLength <= Length);
|
|
|
- for (size_t i = 0; i < CopyLength; ++i) {
|
|
|
- data[i] = other(i);
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
- template<typename FloatType, size_t Length>
|
|
|
- class vectorx {
|
|
|
- public:
|
|
|
-
|
|
|
- static_assert(std::is_arithmetic_v<FloatType>);
|
|
|
-
|
|
|
- vectorx() = default;
|
|
|
-
|
|
|
- vectorx(std::initializer_list<FloatType> list) {
|
|
|
- copy_from_initial_list(list);
|
|
|
- }
|
|
|
-
|
|
|
- template<typename OtherFloatType>
|
|
|
- explicit vectorx(const vectorx<OtherFloatType, Length> &other) {
|
|
|
- copy_from_vectorx(other);
|
|
|
- }
|
|
|
-
|
|
|
- template<typename OtherFloatPointerType>
|
|
|
- explicit vectorx(const vectorx_view<OtherFloatPointerType, Length> &other) {
|
|
|
- copy_from_vectorx_view(other);
|
|
|
- }
|
|
|
-
|
|
|
- template<typename OtherFloatType, size_t OtherRows, size_t OtherColumns>
|
|
|
- explicit vectorx(const Eigen::Matrix<OtherFloatType, OtherRows, OtherColumns> &other) {
|
|
|
- copy_from_eigen_matrix(other);
|
|
|
- }
|
|
|
-
|
|
|
- vectorx &operator=(std::initializer_list<FloatType> list) {
|
|
|
- copy_from_initial_list(list);
|
|
|
- return *this;
|
|
|
- }
|
|
|
-
|
|
|
- template<typename OtherFloatType>
|
|
|
- vectorx &operator=(const vectorx<OtherFloatType, Length> &other) {
|
|
|
- copy_from_vectorx(other);
|
|
|
- return *this;
|
|
|
- }
|
|
|
-
|
|
|
- template<typename OtherFloatPointerType>
|
|
|
- vectorx &operator=(const vectorx_view<OtherFloatPointerType, Length> &other) {
|
|
|
- copy_from_vector_view(other);
|
|
|
- return *this;
|
|
|
- }
|
|
|
-
|
|
|
- template<typename OtherFloatType, int OtherRows, int OtherColumns>
|
|
|
- vectorx &operator=(const Eigen::Matrix<OtherFloatType, OtherRows, OtherColumns> &other) {
|
|
|
- copy_from_eigen_matrix(other);
|
|
|
- return *this;
|
|
|
- }
|
|
|
-
|
|
|
- template<size_t Pos>
|
|
|
- FloatType &at() {
|
|
|
- static_assert(Pos < Length);
|
|
|
- return m[Pos];
|
|
|
- }
|
|
|
-
|
|
|
- template<size_t Pos>
|
|
|
- const FloatType &at() const {
|
|
|
- static_assert(Pos < Length);
|
|
|
- return m[Pos];
|
|
|
- }
|
|
|
-
|
|
|
- FloatType &at(size_t pos) {
|
|
|
- assert(pos < Length);
|
|
|
- return m.at(pos);
|
|
|
- }
|
|
|
-
|
|
|
- const FloatType &at(size_t pos) const {
|
|
|
- assert(pos < Length);
|
|
|
- return m.at(pos);
|
|
|
- }
|
|
|
-
|
|
|
- FloatType &operator[](size_t pos) {
|
|
|
- assert(pos < Length);
|
|
|
- return m[pos];
|
|
|
- }
|
|
|
-
|
|
|
- const FloatType &operator[](size_t pos) const {
|
|
|
- assert(pos < Length);
|
|
|
- return m[pos];
|
|
|
- }
|
|
|
-
|
|
|
- template<size_t StartPos = 0, size_t EndPos = Length>
|
|
|
- auto get_view() {
|
|
|
- static_assert(StartPos < EndPos && EndPos <= Length);
|
|
|
- return vectorx_view<FloatType *, EndPos - StartPos>(m.data() + StartPos);
|
|
|
- };
|
|
|
-
|
|
|
- template<size_t StartPos = 0, size_t EndPos = Length>
|
|
|
- auto get_view() const {
|
|
|
- static_assert(StartPos < EndPos && EndPos <= Length);
|
|
|
- return vectorx_view<const FloatType *, EndPos - StartPos>(m.data() + StartPos);
|
|
|
- };
|
|
|
-
|
|
|
- static constexpr uint16_t length() {
|
|
|
- return sizeof(FloatType) * Length;
|
|
|
- }
|
|
|
-
|
|
|
- void write_to_buffer(versatile_buffer &buffer) const {
|
|
|
- for (const auto &item: m) {
|
|
|
- buffer << item;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- void fill_from_buffer(versatile_buffer &buffer) {
|
|
|
- for (auto &item: m) {
|
|
|
- buffer >> item;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private:
|
|
|
- std::array<FloatType, Length> m;
|
|
|
-
|
|
|
- template<typename, size_t> friend
|
|
|
- class vectorx_view;
|
|
|
-
|
|
|
- void copy_from_initial_list(const std::initializer_list<FloatType> &list) {
|
|
|
- assert(list.size() == Length);
|
|
|
- std::copy_n(list.begin(), Length, m.begin());
|
|
|
- }
|
|
|
-
|
|
|
- template<typename OtherFloatType, int OtherRows, int OtherColumns>
|
|
|
- void copy_from_eigen_matrix(const Eigen::Matrix<OtherFloatType, OtherRows, OtherColumns> &other) {
|
|
|
- static_assert(std::is_convertible_v<OtherFloatType, FloatType>);
|
|
|
- static_assert(OtherRows == 1 || OtherColumns == 1);
|
|
|
- static_assert(OtherRows * OtherColumns == Length);
|
|
|
- for (size_t i = 0; i < Length; ++i) {
|
|
|
- m[i] = other(i);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- template<typename OtherFloatType>
|
|
|
- void copy_from_vectorx(const vectorx<OtherFloatType, Length> &other) {
|
|
|
- static_assert(std::is_convertible_v<OtherFloatType, FloatType>);
|
|
|
- m = other.m;
|
|
|
- }
|
|
|
-
|
|
|
- template<typename OtherFloatPointerType>
|
|
|
- void copy_from_vectorx_view(const vectorx_view<OtherFloatPointerType, Length> &other) {
|
|
|
- static_assert(std::is_convertible_v<
|
|
|
- std::remove_pointer_t<OtherFloatPointerType>, FloatType>);
|
|
|
- std::copy_n(other.data, Length, m.begin());
|
|
|
- }
|
|
|
-
|
|
|
- };
|
|
|
-
|
|
|
- template<typename FreqTag, typename FloatType, size_t Length>
|
|
|
- class vectorx_obj : public vectorx<FloatType, Length>,
|
|
|
- public small_obj<vectorx_obj<FreqTag, FloatType, Length>> {
|
|
|
- public:
|
|
|
- vectorx_obj() = default;
|
|
|
-
|
|
|
- template<typename OtherFloatType>
|
|
|
- explicit vectorx_obj(const OtherFloatType(&list)[Length])
|
|
|
- :vectorx<FloatType, Length>(list) {}
|
|
|
-
|
|
|
- template<typename OtherFloatPointerType>
|
|
|
- explicit vectorx_obj(const vectorx_view<OtherFloatPointerType, Length> &other)
|
|
|
- :vectorx<FloatType, Length>(other) {}
|
|
|
-
|
|
|
- template<typename OtherFloatType>
|
|
|
- explicit vectorx_obj(const vectorx<OtherFloatType, Length> &other)
|
|
|
- :vectorx<FloatType, Length>(other) {}
|
|
|
-
|
|
|
- template<typename OtherFloatType, size_t OtherRows, size_t OtherColumns>
|
|
|
- explicit vectorx_obj(const Eigen::Matrix<OtherFloatType, OtherRows, OtherColumns> &other)
|
|
|
- :vectorx<FloatType, Length>(other) {}
|
|
|
- };
|
|
|
-
|
|
|
- using vector3d = vectorx<double, 3>;
|
|
|
- using vector6d = vectorx<double, 6>;
|
|
|
-
|
|
|
- using vector3d_cview = vectorx_view<const double *, 3>;
|
|
|
- using vector6d_cview = vectorx_view<const double *, 6>;
|
|
|
-
|
|
|
- template<typename FreqTag>
|
|
|
- using vector3d_obj = vectorx_obj<FreqTag, double, 3>;
|
|
|
- template<typename FreqTag>
|
|
|
- using vector6d_obj = vectorx_obj<FreqTag, double, 6>;
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-#endif //SOPHIAR2_VECTORX_HTTP
|