File indexing completed on 2024-04-14 03:49:25

0001 /*
0002     SPDX-FileCopyrightText: 2007 Vladimir Kuznetsov <ks.vladimir@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 /** \file gravitation.h
0008  *  \brief GravitationForce and WeightForce classes
0009  */
0010 
0011 #ifndef STEPCORE_GRAVITATION_H
0012 #define STEPCORE_GRAVITATION_H
0013 
0014 #include "world.h"
0015 #include "object.h"
0016 #include "constants.h"
0017 
0018 namespace StepCore
0019 {
0020 
0021 class GravitationForce;
0022 class WeightForce;
0023 
0024 /** \ingroup errors
0025  *  \brief Errors object for GravitationForce
0026  */
0027 class GravitationForceErrors: public ObjectErrors
0028 {
0029     STEPCORE_OBJECT(GravitationForceErrors)
0030 
0031 public:
0032     /** Constructs GravitationForceErrors */
0033     explicit GravitationForceErrors(Item* owner = nullptr)
0034         : ObjectErrors(owner), _gravitationConstVariance(0) {}
0035 
0036     /** Get owner as GravitationForce */
0037     GravitationForce* gravitationForce() const;
0038 
0039     /** Get gravitationConst variance */
0040     double gravitationConstVariance() const { return _gravitationConstVariance; }
0041     /** Set gravitationConst variance */
0042     void   setGravitationConstVariance(double gravitationConstVariance) {
0043         _gravitationConstVariance = gravitationConstVariance; }
0044 
0045 protected:
0046     double _gravitationConstVariance;
0047     friend class GravitationForce;
0048 };
0049 
0050 /** \ingroup forces
0051  *  \brief Newton gravitational force.
0052  *
0053  *  The force acts between pairs of massive bodies (currently only Particle)
0054  *  and equals:
0055  *  \f[
0056  *      \overrightarrow{f} = G \frac{m_1 m_2 \overrightarrow{r}}
0057  *                                  {|\overrightarrow{r}|^3}
0058  *  \f]
0059  *  where:\n
0060  *  \f$G\f$ is GravitationForce::gravitationConst\n
0061  *  \f$m_1\f$ and \f$m_2\f$ is Particle::mass of first and second body\n
0062  *  \f$\overrightarrow{r}\f$ is difference of Particle::position
0063  *  of the first and second body
0064  *
0065  *  \todo Add interface for massive bodies, support bodies with
0066  *        distributed mass
0067  */
0068 class GravitationForce : public Force
0069 {
0070     STEPCORE_OBJECT(GravitationForce)
0071 
0072 public:
0073     /** Constructs GravitationForce */
0074     explicit GravitationForce(double gravitationConst = Constants::Gravitational);
0075 
0076     void calcForce(bool calcVariances) override;
0077 
0078     /** Get gravitational constant */
0079     double gravitationConst() const { return _gravitationConst; }
0080     /** Set gravitational constant */
0081     void   setGravitationConst(double gravitationConst) { _gravitationConst = gravitationConst; }
0082 
0083     /** Get (and possibly create) GravitationForceErrors object */
0084     GravitationForceErrors* gravitationForceErrors() {
0085         return static_cast<GravitationForceErrors*>(objectErrors()); }
0086 
0087 protected:
0088     ObjectErrors* createObjectErrors() override { return new GravitationForceErrors(this); }
0089 
0090     double _gravitationConst;
0091 };
0092 
0093 /** \ingroup errors
0094  *  \brief Errors object for WeightForce
0095  */
0096 class WeightForceErrors: public ObjectErrors
0097 {
0098     STEPCORE_OBJECT(WeightForceErrors)
0099 
0100 public:
0101     /** Constructs WeightForceErrors */
0102     explicit WeightForceErrors(Item* owner = nullptr)
0103         : ObjectErrors(owner), _weightConstVariance(0) {}
0104 
0105     /** Get owner as WeightForce */
0106     WeightForce* weightForce() const;
0107 
0108     /** Get weightConst variance */
0109     double weightConstVariance() const { return _weightConstVariance; }
0110     /** Set weightConst variance */
0111     void   setWeightConstVariance(double weightConstVariance) {
0112         _weightConstVariance = weightConstVariance; }
0113 
0114 protected:
0115     double _weightConstVariance;
0116     friend class WeightForce;
0117 };
0118 
0119 /** \ingroup forces
0120  *  \brief Weight force (constant gravitational force).
0121  *
0122  *  The force acts between on massive bodies (currently only Particle)
0123  *  and equals:
0124  *  \f[
0125  *      \overrightarrow{f} = \left( \begin{array}{c} 0 \\ mg \end{array} \right)
0126  *  \f]
0127  *  where:\n
0128  *  \f$m\f$ is Particle::mass\n
0129  *  \f$g\f$ is WeightForce::weightConst
0130  *
0131  *  \todo Add interface for massive bodies, support bodies with distributed mass
0132  */
0133 class WeightForce : public Force
0134 {
0135     STEPCORE_OBJECT(WeightForce)
0136 
0137 public:
0138     /** Constructs WeightForce */
0139     explicit WeightForce(double weightConst = Constants::WeightAccel);
0140 
0141     void calcForce(bool calcVariances) override;
0142 
0143     /** Get weight constant */
0144     double weightConst() const { return _weightConst; }
0145     /** Set weight constant */
0146     void   setWeightConst(double weightConst) { _weightConst = weightConst; }
0147 
0148     /** Get (and possibly create) WeightForceErrors object */
0149     WeightForceErrors* weightForceErrors() {
0150         return static_cast<WeightForceErrors*>(objectErrors()); }
0151 
0152 protected:
0153     ObjectErrors* createObjectErrors() override { return new WeightForceErrors(this); }
0154 
0155     double _weightConst;
0156 };
0157 
0158 } // namespace StepCore
0159 
0160 #endif
0161