Program Listing for File ContactForceManager.h

Program Listing for File ContactForceManager.h#

Return to documentation for file (include/Karana/Collision/ContactForceManager.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 Delegating contact force model.
 */

#pragma once

#include "Karana/Collision/ContactForce.h"
#include "Karana/Frame/ChainedFrameToFrame.h"

namespace Karana::Collision {
    /**
     * @class ContactForceManager
     * @brief A ContactForceBase that delegates based on the Frame pair
     *
     * See \sref{collision_dynamics_sec} for more discussion on
     * contact and collision dynamics.
     */
    class ContactForceManager : public ContactForceBase {
      public:
        /**
         * @brief ContactForceManager constructor. The constructor is not meant to be called
         * directly. Please use the create(...) method instead to create an instance.
         *
         * @param name - The name of the ContactForceManager.
         */
        ContactForceManager(std::string_view name);

        /**
         * @brief Create an instance of ContactForceManager.
         *
         * @param name The name for the ContactForceManager instance.
         * @return A ks_ptr to the newly created ContactForceManager.
         */
        static Karana::Core::ks_ptr<ContactForceManager> create(std::string_view name);

        /// ContactForceManager destructor
        virtual ~ContactForceManager();

        /**
         * @brief Apply the contact force for the associated contact.
         *
         * This looks up the most specific delegate ContactForceBase
         * for the given frame pair and calls its applyForce.
         *
         * @param contact The contact to apply the force for.
         * @param st The SubTree we are computing forces for.
         */
        virtual void applyForce(const FrameContact &contact,
                                const kc::ks_ptr<Karana::Dynamics::SubTree> &st) override;

        /**
         * @brief Compute the contact force for the associated contact.
         *
         * This looks up the most specific delegate ContactForceBase
         * for the given frame pair and returns the force computed by
         * the delegate. If there is no default delegate or specific
         * delegate for this pair, returns zero.
         *
         * @param contact The contact to compute the force for.
         * @param nd_1 The contact node on the first body.
         * @param nd_2 The contact node on the second body.
         * @returns The contact force.
         */
        virtual Karana::Math::SpatialVector
        computeForce(const FrameContact &contact,
                     const Karana::Core::ks_ptr<Karana::Dynamics::Node> &nd_1,
                     const Karana::Core::ks_ptr<Karana::Dynamics::Node> &nd_2) override;

        /**
         * @brief Reset the most recent contact node pair to nullptr
         *
         * This also resets the last node pair for all delegates.
         */
        virtual void resetLastNodePair() override;

        /**
         * @brief Get the most specific matching ContactForceBase
         *
         * @param contact The contact to get the delegate for.
         * @return The ContactForceBase (may be nullptr)
         */
        const Karana::Core::ks_ptr<ContactForceBase> &
        getDelegate(const FrameContact &contact) const;

        /**
         * @brief Get the ContactForceBase used by default
         *
         * @return The ContactForceBase used by default (may be nullptr)
         */
        const Karana::Core::ks_ptr<ContactForceBase> &getDelegate() const;

        /**
         * @brief Set the ContactForceBase used by default
         *
         * @param force The ContactForceBase (may be nullptr)
         */
        void setDelegate(const Karana::Core::ks_ptr<ContactForceBase> &force = nullptr);

        /**
         * @brief Get the ContactForceBase used for a Frame pair
         *
         * Note the ordering of frame1 and frame2 does not matter.
         *
         * @param frame1 A frame that must be involved
         * @param frame2 Another frame that must be involved
         * @param strict If false and there's no delegate for
         * this pair, return the default delegate.
         * @return The ContactForceBase (may be nullptr)
         */
        const Karana::Core::ks_ptr<ContactForceBase> &
        getDelegate(const Karana::Frame::Frame &frame1,
                    const Karana::Frame::Frame &frame2,
                    bool strict = false) const;

        /**
         * @brief Set the ContactForceBase used for a Frame pair
         *
         * Note the ordering of frame1 and frame2 does not matter.
         *
         * @param frame1 A frame that must be involved
         * @param frame2 Another frame that must be involved
         * @param force The ContactForceBase (may be nullptr)
         */
        void setDelegate(const Karana::Frame::Frame &frame1,
                         const Karana::Frame::Frame &frame2,
                         const Karana::Core::ks_ptr<ContactForceBase> &force = nullptr);

        /**
         * @brief Clear out all registered ContactForceBase instances
         */
        void clearDelegates();

      private:
        std::unique_ptr<class ContactForceManagerImpl> _impl;
    };
} // namespace Karana::Collision