File indexing completed on 2024-10-06 06:37:52

0001 /*
0002     SPDX-FileCopyrightText: 2007 Vladimir Kuznetsov <ks.vladimir@gmail.com>
0003     SPDX-FileCopyrightText: 2014 Inge Wallin <inge@lysator.liu.se>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 /** \file body.h
0009  *  \brief Contains the Body object.
0010  */
0011 
0012 #ifndef STEPCORE_BODY_H
0013 #define STEPCORE_BODY_H
0014 
0015 
0016 #include <vector> // XXX: Replace if Qt is enabled.
0017 
0018 #include "types.h"
0019 #include "material.h"
0020 #include "item.h"
0021 
0022 
0023 namespace StepCore
0024 {
0025 
0026 
0027 class Material;
0028 /** \ingroup bodies
0029  *  \brief Interface for bodies
0030  *
0031  *  Body is anything that has dynamic variables that require ODE integration
0032  */
0033 class Body : public Item
0034 {
0035     STEPCORE_OBJECT(Body)
0036 
0037 public:
0038     explicit Body(const QString& name = QString())
0039         : Item(name)
0040         , _material(&GenericMaterial)
0041     , _variablesOffset(0)
0042     {}
0043     virtual ~Body() {}
0044 
0045     /** Get the material of the body. */
0046     Material *material() const { return _material; }
0047 
0048     /** Set material of the  body (FIXME: Must be enhanced with META_PROPERTY later) */
0049     void setMaterial(Material *mtrl) { _material = mtrl; }
0050 
0051     /** Get count of dynamic variables (not including velocities) */
0052     virtual int  variablesCount() = 0;
0053 
0054     /** Set positions, velocities and (possibly) its variances using values in arrays and
0055      *  also reset accelerations and its variances. Variances should only be copied
0056      *  and reset if positionVariance != NULL. */
0057     virtual void setVariables(const double* position, const double* velocity,
0058                const double* positionVariance, const double* velocityVariance) = 0;
0059 
0060     /** Copy positions, velocities and (possibly) its variances to arrays.
0061      *  Variances should only be copied if positionVariance != NULL. */
0062     virtual void getVariables(double* position, double* velocity,
0063                      double* positionVariance, double* velocityVariance) = 0;
0064 
0065     /** Add force and (possibly) its variance to force accumulator.
0066      *  \note This function is used only by generic constraints handling code,
0067      *        force objects should use body-specific functions. */
0068     virtual void addForce(const double* force, const double* forceVariance) = 0;
0069 
0070     /** Reset force accumulator and (possibly) its variance to zero.
0071      *  Variance should only be reset if resetVariance == true. */
0072     virtual void resetForce(bool resetVariance) = 0;
0073 
0074     /** Copy acceleration (forces left-multiplied by inverse mass)
0075      *  and (possibly) its variances to arrays.
0076      *  Variances should only be copied if accelerationVariance != NULL. */
0077     virtual void getAccelerations(double* acceleration, double* accelerationVariance) = 0;
0078 
0079     /** Get inverse mass and (possibly) its variance matrices.
0080      *  Variance should only be copied of variance != NULL. */
0081     virtual void getInverseMass(VectorXd* inverseMass,
0082                                 DynSparseRowMatrix* variance, int offset) = 0;
0083     
0084     /** Offset of body's variables in global arrays
0085      *  (meaningless if the body is not a part of the world) */
0086     int variablesOffset() const { return _variablesOffset; }
0087 
0088 private:
0089     friend class World;
0090 
0091     Material  *_material;
0092 
0093     /** \internal Set offset of body's variables in global arrays */
0094     void setVariablesOffset(int variablesOffset)
0095     {
0096         _variablesOffset = variablesOffset;
0097     }
0098 
0099     int _variablesOffset;
0100 };
0101 
0102 
0103 /** List of pointers to Body */
0104 typedef std::vector<Body*>  BodyList;
0105 
0106 
0107 } // namespace StepCore
0108 
0109 
0110 #endif