Program Listing for File SpatialVector.h

Program Listing for File SpatialVector.h#

Return to documentation for file (include/Karana/Math/SpatialVector.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 the declarations for the SpatialVector class.
 */

#pragma once

#include "Karana/Math/Defs.h"

namespace Karana::Math {
    /**
     * @class SpatialVector
     * @brief Represents a 6D spatial vector, split into angular (w) and linear (v) components.
     *
     * See \sref{spatial_notation_sec} section for more discussion on
     * spatial notation.
     */
    class SpatialVector {
        // Constructors
      public:
        /**
         * @brief Construct initialized to zero.
         */
        SpatialVector();

        /**
         * @brief Construct using a 6 vector. The first 3 components will be put into w and
         * the last 3 components will be put into v.
         *
         * @param sv 6-element vector representing the entire spatial vector.
         */
        SpatialVector(const Vec6 &sv);

        /**
         * @brief Construct using two 3-element vectors. The first will become w and the
         * second will become v.
         *
         * @param w Angular portion of the spatial vector.
         * @param v Linear portion of the spatial vector.
         */
        SpatialVector(const Vec3 &w, const Vec3 &v);

        // Copy and move operations
      public:
        /**
         * @brief Copy constructor.
         *
         * @param other The SpatialVector to copy from.
         */
        SpatialVector(const SpatialVector &other);

        /**
         * @brief Copy assignment operator.
         *
         * @param other The SpatialVector to assign from.
         * @return Reference to this object.
         */
        SpatialVector &operator=(const SpatialVector &other);

        /**
         * @brief Move constructor.
         *
         * @param other The SpatialVector to move from.
         */
        SpatialVector(SpatialVector &&other) noexcept;

        /**
         * @brief Move assignment operator.
         *
         * @param other The SpatialVector to move from.
         * @return Reference to this object.
         */
        SpatialVector &operator=(SpatialVector &&other) noexcept;

        /**
         * @brief Return object type as a string.
         *
         * @return String "SpatialVector".
         */
        std::string_view typeString() const;

        /**
         * @brief Dump the object as a formatted string.
         *
         * @param prefix Optional prefix for each line.
         * @param precision Number of digits to use for floating point values.
         * @param format_type The format type to use.
         * @return Formatted string representation.
         */
        std::string dumpString(std::string_view prefix = "",
                               unsigned int precision = 10,
                               DumpFormatType format_type = DumpFormatType::DEFAULT_FLOAT) const;

        /**
         * @brief Write dumpString to std::cout.
         *
         * @param prefix Optional prefix for each line.
         * @param precision Number of digits to use for floating point values.
         * @param format_type The format type to use.
         */
        void dump(std::string_view prefix = "",
                  unsigned int precision = 10,
                  DumpFormatType format_type = DumpFormatType::DEFAULT_FLOAT) const;

        /**
         * @brief Return true if the spatial vector is zero.
         *
         * @param prec Tolerance to use when comparing with zero.
         * @return True if both angular and linear components are zero.
         */
        bool isZero(double prec = MATH_EPSILON) const;

        /**
         * @brief Zero out the spatial vector's values.
         */
        void setZero();

        /**
         * @brief Mark the spatial vector as not ready.
         */
        void makeNotReady();

        /**
         * @brief Check if the spatial vector is initialized.
         *
         * @return True if both angular and linear components are initialized.
         */
        bool isReady() const;

        // Operators
      public:
        /**
         * @brief Equality operator.
         *
         * @param other Another SpatialVector to compare with.
         * @return True if both components are equal.
         */
        bool operator==(const SpatialVector &other) const;

        /**
         * @brief Dot product with another spatial vector.
         *
         * @param other The SpatialVector to compute the dot product with.
         * @return Dot product result.
         */
        double operator*(const SpatialVector &other) const;

        /**
         * @brief Scalar multiplication.
         *
         * @param scale Scalar value to multiply by.
         * @return Scaled SpatialVector.
         */
        SpatialVector operator*(double scale) const;

        /**
         * @brief Component-wise addition.
         *
         * @param other The SpatialVector to add to this one.
         * @return Resulting SpatialVector sum.
         */
        SpatialVector operator+(const SpatialVector &other) const;

        /**
         * @brief In-place component-wise addition.
         *
         * @param other The SpatialVector to add to this one in-place.
         * @return Reference to this object.
         */
        SpatialVector &operator+=(const SpatialVector &other);

        /**
         * @brief Negation operator.
         *
         * @return Negated SpatialVector.
         */
        SpatialVector operator-() const;

        /**
         * @brief Subtract another SpatialVector.
         *
         * @param other The SpatialVector to subtract from this one.
         * @return Resulting SpatialVector.
         */
        SpatialVector operator-(const SpatialVector &other) const;

        /**
         * @brief In-place subtraction.
         *
         * @param other The SpatialVector to subtract from this one.
         * @return Reference to this object.
         */
        SpatialVector operator-=(const SpatialVector &other);

        /**
         * @brief Add a Vec6 to this spatial vector.
         *
         * @param vec The 6D vector to add to this SpatialVector.
         * @return Resulting SpatialVector.
         */
        SpatialVector operator+(const Vec6 &vec) const;

        /**
         * @brief Subtract a Vec6 from this spatial vector.
         *
         * @param vec The 6D vector to subtract from this SpatialVector.
         * @return Resulting SpatialVector.
         */
        SpatialVector operator-(const Vec6 &vec) const;

        // SpatialVector methods
      public:
        /**
         * @brief Check if another spatial vector is approximately equal within precision.
         *
         * @param other The SpatialVector to compare to this one.
         * @param prec Precision tolerance.
         * @return True if approximately equal.
         */
        bool isApprox(const SpatialVector &other, double prec = MATH_EPSILON) const;

        /**
         * @brief Get the angular portion of the spatial vector.
         *
         * @return Const reference to angular vector.
         */
        const Vec3 &getw() const;

        /**
         * @brief Set the angular portion of the spatial vector.
         *
         * @param w New angular vector.
         */
        void setw(const Vec3 &w);

        /**
         * @brief Get the linear portion of the spatial vector.
         *
         * @return Const reference to linear vector.
         */
        const Vec3 &getv() const;

        /**
         * @brief Set the linear portion of the spatial vector.
         *
         * @param v New linear vector.
         */
        void setv(const Vec3 &v);

        /**
         * @brief Convert to a 6-vector by combining the w and v parts as [w;v].
         *
         * @return Combined 6D vector.
         */
        Vec6 toVector6() const;

        /**
         * @brief Compute 6-D cross product with another spatial vector.
         *
         * @f[
         *  A \times B = \tilde A  B = \begin{bmatrix} \tilde A_w & \tilde A_v \\ 0 & \tilde A_w
         * \end{bmatrix} \begin{bmatrix} B_w \\ B_v \end{bmatrix}
         *  @f]
         *
         * @param other The SpatialVector to compute the cross product with.
         * @return Resulting SpatialVector.
         */
        SpatialVector cross(const SpatialVector &other);

        /**
         * @brief Compute 6-D cross product with another spatial vector.
         *
         * @f[
         *  \tilde A   = \begin{bmatrix} \tilde A_w & 0 \\ \tilde A_v & \tilde A_w
         * \end{bmatrix} \begin{bmatrix} B_w \\ B_v \end{bmatrix}
         *  @f]
         *
         * @return Resulting SpatialVector.
         */
        Mat66 tilde() const;

        /**
         * @brief Compute the bar product with another spatial vector.
         *
         * \f[ \bar A B = - {\tilde A}^* B  = \begin{bmatrix} \tilde A_w & \tilde A_v \\  0  &
         * \tilde A_w \end{bmatrix} \begin{bmatrix} B_w \\ B_v \end{bmatrix} \f]
         *
         * @param other The SpatialVector to compute the bar product with.
         * @return Resulting SpatialVector.
         */
        SpatialVector barprod(const SpatialVector &other);

        /**
         * @brief Multiply from the vector from left with a matrix, i.e v * mat.
         *
         * @param mat Matrix to multiply.
         * @return Resulting SpatialVector.
         */
        SpatialVector multiplyFromLeft(const Mat66 &mat) const;

      private:
        /// Angular portion of the spatial vector
        Vec3 _w;

        /// Linear portion of the spatial vector
        Vec3 _v;
    };
} // namespace Karana::Math