Program Listing for File AABB.h

Program Listing for File AABB.h#

Return to documentation for file (include/Karana/Math/AABB.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 AABB class and associated stream operator.
 */

#pragma once
#include "Karana/Math/Defs.h"
#include "Karana/Math/HomTran.h"
#include "Karana/Math/UnitQuaternion.h"
#include <iostream>
#include <string>

namespace Karana::Math {
    /**
     * @class AABB
     * @brief Represents an Axis-Aligned Bounding Box using a pair of 3-vectors
     *
     * An AABB is defined by its minimum and maximum corners. This class
     * provides functionality to combine two AABBs and to compute a new AABB
     * after transformation.
     */
    class AABB {
      public:
        /**
         * @brief Constructs an AABB with maximum and minimum values.
         *
         * This is a default constructor that creates an "empty" AABB, which can
         * be used as a starting point for combining other AABBs.
         */
        AABB();

        /**
         * @brief Constructs an AABB from a minimum and a maximum corner.
         * @param min The minimum corner vector.
         * @param max The maximum corner vector.
         * @param fix_minmax If true, recompute min and max to
         * ensure they are actually the minimum and maximum corners
         */
        AABB(const Vec3 &min, const Vec3 &max, bool fix_minmax = true);

        /**
         * @brief Copy an AABB from another one.
         * @param other The other AABB.
         */
        AABB(const AABB &other);

        /**
         * @brief Update the AABB from another one.
         * @param other The other AABB.
         * @return This AABB after updating.
         */
        AABB &operator=(const AABB &other);

        /**
         * @brief Get the minimum corner of the AABB.
         * @return The minimum corner.
         */
        const Vec3 &min() const;

        /**
         * @brief Get the maximum corner of the AABB.
         * @return The maximum corner.
         */
        const Vec3 &max() const;

        /**
         * @brief Returns a string representation of the AABB with options for formatting.
         *
         * @param prefix A string to prepend to each line.
         * @param precision The number of decimal places for floating-point values.
         * @param format_type The format type to use.
         * @return A string containing the min and max corners.
         */
        std::string dumpString(std::string_view prefix = "",
                               int precision = 6,
                               DumpFormatType format_type = DumpFormatType::DEFAULT_FLOAT) const;

        /**
         * @brief Prints a string representation of the AABB with options for formatting.
         *
         * @param prefix A string to prepend to each line.
         * @param precision The number of decimal places for floating-point values.
         * @param format_type The format type to use.
         */
        void dump(std::string_view prefix = "",
                  int precision = 10,
                  DumpFormatType format_type = DumpFormatType::DEFAULT_FLOAT) const;

        /**
         * @brief Combines this AABB with another AABB.
         *
         * The resulting AABB will be a new box that encompasses both the
         * original AABB and the 'other' AABB.
         *
         * @param other The AABB to combine with this one.
         * @return The combined AABB
         */
        AABB operator+(const AABB &other) const;

        /**
         * @brief Combines this AABB with another AABB in-place.
         *
         * The resulting AABB will be a new box that encompasses both the
         * original AABB and the 'other' AABB.
         *
         * @param other The AABB to combine with this one.
         * @return This AABB updated with the new combined box.
         */
        AABB &operator+=(const AABB &other);

        /**
         * @brief Computes a new AABB by translating this AABB by a given vector.
         *
         * @param t The 3-vector to translate by.
         * @return A new AABB that has been translated.
         */
        AABB translate(const Vec3 &t) const;

        /**
         * @brief Computes a new AABB by rotating this AABB by a given unit quaternion.
         *
         * @param q The unit quaternion to rotate by.
         * @return A new AABB that has been translated.
         */
        AABB rotate(const UnitQuaternion &q) const;

        /**
         * @brief Computes a new AABB by scaling this AABB by a given scalar.
         *
         * @param s The scalar to scale by.
         * @return A new AABB that has been scaled.
         */
        AABB scale(double s) const;

        /**
         * @brief Computes a new AABB by scaling this AABB by a given vector.
         *
         * @param s The 3-vector to scale by.
         * @return A new AABB that has been scaled.
         */
        AABB scale(const Vec3 &s) const;

        /**
         * @brief Computes a new AABB by transforming this AABB by a given HomTran.
         *
         * @param t The 3-vector to scale by.
         * @return A new AABB that has been transformed.
         */
        AABB transform(const HomTran &t) const;

      private:
        /// The minimum corner of the bounding box.
        Vec3 _min;

        /// The maximum corner of the bounding box.
        Vec3 _max;
    };
} // namespace Karana::Math

/**
 * @brief Overloads the stream insertion operator `<<` for the AABB class.
 *
 * This allows an AABB object to be printed directly to an output stream
 * using `std::cout << aabb_object;`. It uses the `dumpString()` method.
 *
 * @param os The output stream to write to.
 * @param aabb The AABB object to print.
 * @return A reference to the output stream.
 */
std::ostream &operator<<(std::ostream &os, const Karana::Math::AABB &aabb);