Program Listing for File SimTran.h

Program Listing for File SimTran.h#

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

#pragma once

#include "Karana/Math/Defs.h"
#include "Karana/Math/HomTran.h"
#include "Karana/Math/UnitQuaternion.h"
#include <cmath>

namespace Karana::Math {
    using namespace Eigen;

    /**
     * @class SimTran
     * @brief Similarity transform consisting of translation, rotation, and uniform scale factor.
     */
    class SimTran {

        // Constructors
      public:
        /**
         * @brief Default constructor.
         */
        SimTran();

        /**
         * @brief Constructor with only homogeneous transform.
         * @param transform Homogeneous transform with translation and rotation.
         */
        SimTran(const HomTran &transform);

        /**
         * @brief Constructor with only scale.
         * @param scale Scale factor.
         */
        SimTran(double scale);

        /**
         * @brief Constructor with both homogeneous transform and scale.
         * @param transform Homogeneous transform with translation and rotation.
         * @param scale Scale factor.
         */
        SimTran(const HomTran &transform, double scale);

        /**
         * @brief Copy constructor.
         * @param T Other SimTran to copy from.
         */
        SimTran(const SimTran &T);

        /**
         * @brief Copy assignment operator.
         * @param T Other SimTran to copy from.
         * @return Reference to this object.
         */
        SimTran &operator=(const SimTran &T);

        /**
         * @brief Move constructor.
         * @param T SimTran to move from.
         */
        SimTran(SimTran &&T) noexcept;

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

        /**
         * @brief Equality operator.
         * @param other SimTran to compare with.
         * @return True if equal, false otherwise.
         */
        bool operator==(const SimTran &other) const;

        /**
         * @brief Approximate equality check.
         * @param other SimTran to compare with.
         * @param prec Precision threshold.
         * @return True if approximately equal, false otherwise.
         */
        bool isApprox(const SimTran &other, double prec = MATH_EPSILON) const;

        /**
         * @brief Get the type string of this class.
         * @return "SimTran"
         */
        std::string typeString() const;

        /**
         * @brief Set the translation vector.
         * @param vec Translation vector.
         * @param epsilon Precision threshold used to check if the translation is zero.
         */
        void setTranslation(const Vec3 &vec, double epsilon = MATH_EPSILON);

        /**
         * @brief Get a string representation of the SimTran.
         *
         * @param prefix String prefix for each line.
         * @param precision Number of digits to use for floating point values.
         * @param format_type The format type to use.
         * @return String with information about this SimTran.
         */
        std::string dumpString(std::string_view prefix = "",
                               unsigned int precision = 6,
                               DumpFormatType format_type = DumpFormatType::DEFAULT_FLOAT) const;

        /**
         * @brief Print dumpString to std::cout.
         *
         * @param prefix String 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 Set the rotation using a unit quaternion.
         * @param q Unit quaternion.
         * @param epsilon Precision threshold to check if the unit quaternion is identity.
         */
        void setUnitQuaternion(const UnitQuaternion &q, double epsilon = MATH_EPSILON);

        /**
         * @brief Set the scale factor.
         * @param scale Scale value.
         */
        void setScale(double scale);

        /**
         * @brief Get the full 4x4 transformation matrix.
         * @return 4x4 transformation matrix.
         */
        const Mat44 getMatrix() const;

        /**
         * @brief Get the translation vector.
         * @return Translation vector.
         */
        const Vec3 &getTranslation() const;

        /**
         * @brief Get the rotation of the SimTran as a unit quaternion.
         * @return Unit quaternion.
         */
        const UnitQuaternion &getUnitQuaternion() const;

        /**
         * @brief Get the scale factor.
         * @return Scale factor.
         */
        double getScale() const;

        /**
         * @brief Set the SimTran to identity.
         */
        void setIdentity();

        /**
         * @brief Check if the SimTran is identity.
         * @return True if identity, false otherwise.
         */
        bool isIdentity() const;

        /**
         * @brief Check if the SimTran has a non-zero translation.
         * @return True if translation is non-zero, false otherwise.
         */
        bool hasTranslation() const;

        /**
         * @brief Check if the SimTran has a rotation.
         * @return True if unit quaternion is non-identity, false otherwise.
         */
        bool hasRotation() const;

        /**
         * @brief Check if the SimTran includes a scale.
         * @return True if scale is not 1.0, false otherwise.
         */
        bool hasScale() const;

        /**
         * @brief Compose this SimTran with another.
         * @param T Another SimTran to apply after this one.
         * @return Combined transform.
         */
        SimTran operator*(const SimTran &T) const;

        /**
         * @brief Transform a vector by this SimTran.
         * @param v Vector to transform.
         * @return Transformed vector.
         */
        Vec3 operator*(const Vec3 &v) const;

        /**
         * @brief Get the inverse of this SimTran.
         * @return Inverse SimTran.
         */
        SimTran inverse() const;

        /**
         * @brief Mark this SimTran as not ready.
         */
        void makeNotReady();

        /**
         * @brief Check whether this SimTran is initialized.
         * @return True if initialized, false otherwise.
         */
        bool isReady();

      private:
        double _scale = 1.0;           ///< Uniform scale factor
        HomTran _hom_tran = HomTran(); ///< Homogeneous transformation (rotation and translation)
    };

} // namespace Karana::Math