body.h 3.69 KB
Newer Older
1 2
/* This file is part of StepCore library.
   Copyright (C) 2007 Vladimir Kuznetsov <ks.vladimir@gmail.com>
3
   Copyright (C) 2014 Inge Wallin        <inge@lysator.liu.se>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

   StepCore library is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   StepCore library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with StepCore; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

20 21
/** \file body.h
 *  \brief Contains the Body object.
22 23 24 25 26 27 28 29 30
 */

#ifndef STEPCORE_BODY_H
#define STEPCORE_BODY_H


#include <vector> // XXX: Replace if Qt is enabled.

#include "types.h"
Inge Wallin's avatar
Inge Wallin committed
31
#include "item.h"
32 33 34 35 36 37 38 39 40 41 42


namespace StepCore
{


/** \ingroup bodies
 *  \brief Interface for bodies
 *
 *  Body is anything that has dynamic variables that require ODE integration
 */
Inge Wallin's avatar
Inge Wallin committed
43
class Body : public Item
44 45 46 47
{
    STEPCORE_OBJECT(Body)

public:
Inge Wallin's avatar
Inge Wallin committed
48 49 50 51
    Body(const QString& name = QString())
        : Item(name)
	, _variablesOffset(0)
    {}
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    virtual ~Body() {}

    /** Get count of dynamic variables (not including velocities) */
    virtual int  variablesCount() = 0;

    /** Set positions, velocities and (possibly) its variances using values in arrays and
     *  also reset accelerations and its variances. Variances should only be copied
     *  and reseted if positionVariance != NULL. */
    virtual void setVariables(const double* position, const double* velocity,
               const double* positionVariance, const double* velocityVariance) = 0;

    /** Copy positions, velocities and (possibly) its variances to arrays.
     *  Variances should only be copied if positionVariance != NULL. */
    virtual void getVariables(double* position, double* velocity,
                     double* positionVariance, double* velocityVariance) = 0;

    /** Add force and (possibly) its variance to force accomulator.
     *  \note This function is used only by generic constraints handling code,
     *        force objects should use body-specific functions. */
    virtual void addForce(const double* force, const double* forceVariance) = 0;

    /** Reset force accomulator and (possibly) its variance to zero.
     *  Variance should only be reseted if resetVariance == true. */
    virtual void resetForce(bool resetVariance) = 0;

    /** Copy acceleration (forces left-multiplied by inverse mass)
     *  and (possibly) its variances to arrays.
     *  Variances should only be copied if accelerationVariance != NULL. */
    virtual void getAccelerations(double* acceleration, double* accelerationVariance) = 0;

    /** Get inverse mass and (possibly) its variance matrixes.
     *  Variance should only be copied of variance != NULL. */
    virtual void getInverseMass(VectorXd* inverseMass,
                                DynSparseRowMatrix* variance, int offset) = 0;
    
    /** Offset of body's variables in global arrays
     *  (meaningless if the body is not a part of the world) */
    int variablesOffset() const { return _variablesOffset; }

private:
    friend class World;

    /** \internal Set offset of body's variables in global arrays */
    void setVariablesOffset(int variablesOffset)
    {
        _variablesOffset = variablesOffset;
    }

    int _variablesOffset;
};


/** List of pointers to Body */
typedef std::vector<Body*>  BodyList;


} // namespace StepCore


#endif