Program Listing for File Defs.h

Program Listing for File Defs.h#

Return to documentation for file (include/Karana/KUtils/Defs.h)

/*
 * Copyright (c) 2024-2026 Karana Dynamics Pty Ltd. All rights reserved.
 *
 * NOTICE TO USER:
 *
 * This source code and/or documentation (the "Licensed Materials") is
 * the confidential and proprietary information of Karana Dynamics Inc.
 * Use of these Licensed Materials is governed by the terms and conditions
 * of a separate software license agreement between Karana Dynamics and the
 * Licensee ("License Agreement"). Unless expressly permitted under that
 * agreement, any reproduction, modification, distribution, or disclosure
 * of the Licensed Materials, in whole or in part, to any third party
 * without the prior written consent of Karana Dynamics is strictly prohibited.
 *
 * THE LICENSED MATERIALS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
 * KARANA DYNAMICS DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-INFRINGEMENT, AND
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * IN NO EVENT SHALL KARANA DYNAMICS BE LIABLE FOR ANY DAMAGES WHATSOEVER,
 * INCLUDING BUT NOT LIMITED TO LOSS OF PROFITS, DATA, OR USE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, WHETHER IN CONTRACT, TORT,
 * OR OTHERWISE ARISING OUT OF OR IN CONNECTION WITH THE LICENSED MATERIALS.
 *
 * U.S. Government End Users: The Licensed Materials are a "commercial item"
 * as defined at 48 C.F.R. 2.101, and are provided to the U.S. Government
 * only as a commercial end item under the terms of this license.
 *
 * Any use of the Licensed Materials in individual or commercial software must
 * include, in the user documentation and internal source code comments,
 * this Notice, Disclaimer, and U.S. Government Use Provision.
 */

/**
 * @file
 * @brief Contains common delcarations for Karana KUtils.
 */

#pragma once
#include "Karana/Math/Defs.h"
#include <type_traits>

namespace Karana::KUtils {

    /// Primary template (default case: false)
    template <typename T>
    struct is_eigen_type : std::false_type {}; // NOLINT(readability-identifier-naming)
    template <typename T>
    struct is_eigen_int_type : std::false_type {}; // NOLINT(readability-identifier-naming)

    /// Specialization for Eigen::Matrix
    template <int Rows, int Cols>
    struct is_eigen_type<Eigen::Matrix<double, Rows, Cols, Eigen::RowMajor>> : std::true_type {};

    /// Specialization for Eigen::Vector
    template <int Rows> struct is_eigen_type<Eigen::Matrix<double, Rows, 1>> : std::true_type {};

    /// Specialization for Eigen::Vector of int type
    template <int Rows> struct is_eigen_int_type<Eigen::Matrix<int, Rows, 1>> : std::true_type {};

    /// Helper variable template to get the value
    template <typename T> inline constexpr bool is_eigen_type_v = is_eigen_type<T>::value;
    /// Helper variable template to get the value for integer types
    template <typename T> inline constexpr bool is_eigen_int_type_v = is_eigen_int_type<T>::value;

    /// Primary template (default case: false)
    template <typename T>
    struct is_eigen_vector : std::false_type {}; // NOLINT(readability-identifier-naming)
    // template <typename T>
    // struct is_eigen_int_vector : std::false_type {}; // NOLINT(readability-identifier-naming)

    /// Specialization for Eigen::Vector
    template <int Rows> struct is_eigen_vector<Eigen::Matrix<double, Rows, 1>> : std::true_type {};

    /// Specialization for Eigen::Vector
    // template <int Rows> struct is_eigen_int_vector<Eigen::Matrix<int, Rows, 1>> : std::true_type
    // {};

    /// Helper variable template to get the value
    template <typename T> inline constexpr bool is_eigen_vector_v = is_eigen_vector<T>::value;
    // template <typename T> inline constexpr bool is_eigen_int_vector_v =
    // is_eigen_int_vector<T>::value;

    /// Default template for extracting matrix dimensions (defaults to -1 x -1)
    template <typename T>
    struct extract_matrix_dimensions { // NOLINT(readability-identifier-naming)
        /// Number of rows in the matrix
        static constexpr int rows = -1;

        /// Number of cols in the matrix
        static constexpr int cols = -1;
    };

    /// Template to extract matrix dimensions from a vector.
    template <int _rows> struct extract_matrix_dimensions<Eigen::Matrix<double, _rows, 1>> {
        /// Number of rows in the matrix
        static constexpr int rows = _rows;

        /// Number of cols in the matrix
        static constexpr int cols = 1;
    };

    /// Template to extract matrix dimensions from a RowMajor matrix.
    template <int _rows, int _cols>
    struct extract_matrix_dimensions<Eigen::Matrix<double, _rows, _cols, Eigen::RowMajor>> {
        /// Number of rows in the matrix
        static constexpr int rows = _rows;

        /// Number of cols in the matrix
        static constexpr int cols = _cols;
    };

    /// Get the rows value from extract_matrix_dimensions
    template <typename T> constexpr int extract_rows_v = extract_matrix_dimensions<T>::rows;

    /// Get the cols value from extract_matrix_dimensions
    template <typename T> constexpr int extract_cols_v = extract_matrix_dimensions<T>::cols;

    /**
     * @brief Helper to extract rows from a Type T.
     *
     * @tparam T The object to extract rows from.
     */
    template <typename T> struct extract_rows { // NOLINT(readability-identifier-naming)
        /// Number of rows in the matrix
        static constexpr int value = -1; // Default case (should never match)
    };

    /// Specialization for fixed-size Eigen::Matrix<double, rows, 1>
    template <int rows> struct extract_rows<Eigen::Matrix<double, rows, 1>> {
        /// Number of rows in the matrix
        static constexpr int value = rows;
    };

    /// Helper variable template to get the value
    template <typename T> constexpr int extract_vector_rows_v = extract_rows<T>::value;

#ifndef PYBIND11_MKDOC_SKIP
    template <typename T>
    concept isPlotDataType = std::is_same_v<double, T> or std::is_same_v<Karana::Math::Vec3, T> or
                             std::is_same_v<Karana::Math::Vec, T>;

    template <typename T>
    concept isLoggingDataType =
        std::is_same_v<double, T> or std::is_same_v<float, T> or is_eigen_type_v<T> or
        std::is_same_v<short, T> or std::is_same_v<int, T> or std::is_same_v<long, T> or
        std::is_same_v<char, T>;

#endif

} // namespace Karana::KUtils