worldcopy.cc 5.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* This file is part of Step
   Copyright (C) 2007 Vladimir Kuznetsov <ks.vladimir@gmail.com>

   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
*/
18 19 20 21
#include "maintest.h"

#include <stepcore/world.h>
#include <stepcore/solver.h>
Vladimir Kuznetsov's avatar
Vladimir Kuznetsov committed
22
#include <stepcore/types.h>
23

Inge Wallin's avatar
Inge Wallin committed
24
class WorldCopyTestItem: public StepCore::Body,
25
                         public StepCore::Force
26 27 28 29 30 31 32 33 34 35 36 37 38
{
    STEPCORE_OBJECT(WorldCopyTestItem)

public:
    WorldCopyTestItem(): _world1(NULL), _link(NULL) {}
    void worldItemRemoved(Item* item);
    void setWorld(StepCore::World* world);

    WorldCopyTestItem* link() const { return _link; }
    void setLink(WorldCopyTestItem* link) { _link = link; }

    StepCore::World* world1() const { return _world1; }

39
    virtual int  variablesCount() { return 0; }
Vladimir Kuznetsov's avatar
Vladimir Kuznetsov committed
40 41 42 43 44 45 46
    virtual void setVariables(const double* position, const double* velocity,
               const double* positionVariance, const double* velocityVariance) {}
    virtual void getVariables(double* position, double* velocity,
                     double* positionVariance, double* velocityVariance) {}
    virtual void addForce(const double* force, const double* forceVariance) {}
    virtual void resetForce(bool resetVariance) {}
    virtual void getAccelerations(double* acceleration, double* accelerationVariance) {}
Vladimir Kuznetsov's avatar
Vladimir Kuznetsov committed
47 48
    virtual void getInverseMass(StepCore::VectorXd* inverseMass,
                            StepCore::DynSparseRowMatrix* variance, int offset) {}
Vladimir Kuznetsov's avatar
Vladimir Kuznetsov committed
49 50

    virtual void calcForce(bool calcVariances) {}
51

52 53 54 55 56 57 58 59 60 61
private:
    StepCore::World* _world1;
    WorldCopyTestItem* _link;
};

class WorldCopyTestSolver: public StepCore::Solver
{
    STEPCORE_OBJECT(WorldCopyTestSolver)

public:
Vladimir Kuznetsov's avatar
Vladimir Kuznetsov committed
62 63 64
    virtual int doCalcFn(double* t, const StepCore::VectorXd* y, const StepCore::VectorXd* yvar = 0,
                            StepCore::VectorXd* f = 0, StepCore::VectorXd* fvar = 0) { return Solver::OK; }
    virtual int doEvolve(double* t, double t1, StepCore::VectorXd* y, StepCore::VectorXd* yvar) { return Solver::OK; }
65 66
};

Vladimir Kuznetsov's avatar
Vladimir Kuznetsov committed
67
STEPCORE_META_OBJECT(WorldCopyTestItem, "WorldCopyTestItem", "TestItem", 0,
Vladimir Kuznetsov's avatar
Vladimir Kuznetsov committed
68
    STEPCORE_SUPER_CLASS(StepCore::Item) STEPCORE_SUPER_CLASS(StepCore::Body) STEPCORE_SUPER_CLASS(StepCore::Force),)
Vladimir Kuznetsov's avatar
Vladimir Kuznetsov committed
69
STEPCORE_META_OBJECT(WorldCopyTestSolver, "WorldCopyTestSolver", "TestSolver", 0, STEPCORE_SUPER_CLASS(StepCore::Solver),)
70 71 72 73 74 75 76 77 78 79 80 81

void WorldCopyTestItem::worldItemRemoved(Item* item)
{
    if(item == _link) _link = 0;
}

void WorldCopyTestItem::setWorld(StepCore::World* world)
{
    _world1 = world;
    if(world == NULL) _link = NULL;
    else if(this->world() != NULL && _link != NULL) {
        _link = dynamic_cast<WorldCopyTestItem*>(
82
            world->items()[ this->world()->childItemIndex(dynamic_cast<const Item*>(_link)) ]);
83 84 85 86 87 88 89 90 91 92
    }
    StepCore::Item::setWorld(world);
}

void MainTest::testWorldCopy()
{
    /* Initialize and create some objects */
    StepCore::World* world = new StepCore::World();
    world->addItem(new WorldCopyTestItem());
    world->addItem(new WorldCopyTestItem());
93
    world->setSolver(new WorldCopyTestSolver());
94

Vladimir Kuznetsov's avatar
Vladimir Kuznetsov committed
95 96 97
    QCOMPARE( int(world->items().size()), 2 );
    QCOMPARE( int(world->bodies().size()), 2 );
    QCOMPARE( int(world->forces().size()), 2 );
98

99 100
    world->setName("world1");
    world->setTime(10);
Vladimir Kuznetsov's avatar
Vladimir Kuznetsov committed
101
    world->setTimeScale(20);
102 103 104 105 106 107 108 109 110 111 112 113
    world->items()[0]->setName("item0");
    world->items()[1]->setName("item1");
    world->solver()->setName("solver1");

    dynamic_cast<WorldCopyTestItem*>(world->items()[0])->setLink(
                    dynamic_cast<WorldCopyTestItem*>(world->items()[1]));

    /* Copy constructor */
    StepCore::World* world1 = new StepCore::World(*world);
    
    QCOMPARE(world1->name(), world->name());
    QCOMPARE(world1->time(), world->time());
Vladimir Kuznetsov's avatar
Vladimir Kuznetsov committed
114
    QCOMPARE(world1->timeScale(), world->timeScale());
115
    QCOMPARE(world1->items().size(), world->items().size());
116 117
    QCOMPARE(world1->bodies().size(), world->bodies().size());
    QCOMPARE(world1->forces().size(), world->forces().size());
118 119 120 121 122 123 124 125
    QCOMPARE(world1->items()[0]->name(), world->items()[0]->name());
    QCOMPARE(world1->items()[1]->name(), world->items()[1]->name());
    QCOMPARE(world1->solver()->name(), world->solver()->name());

    QVERIFY(world1->items()[0] != world->items()[0]);
    QVERIFY(world1->items()[1] != world->items()[1]);
    QVERIFY(world1->solver() != world->solver());

126 127 128 129 130
    QVERIFY(dynamic_cast<StepCore::Item*>(world1->bodies()[0]) == world1->items()[0]);
    QVERIFY(dynamic_cast<StepCore::Item*>(world1->bodies()[1]) == world1->items()[1]);
    QVERIFY(dynamic_cast<StepCore::Item*>(world1->forces()[0]) == world1->items()[0]);
    QVERIFY(dynamic_cast<StepCore::Item*>(world1->forces()[1]) == world1->items()[1]);

131 132 133 134 135 136 137 138 139 140 141
    QVERIFY(dynamic_cast<WorldCopyTestItem*>(world1->items()[0])->world1() == world1);
    QVERIFY(dynamic_cast<WorldCopyTestItem*>(world1->items()[1])->world1() == world1);
    QVERIFY(dynamic_cast<WorldCopyTestItem*>(world1->items()[0])->link() ==
                    dynamic_cast<WorldCopyTestItem*>(world1->items()[1]));

    /* Remove item */
    //world1->deleteItem(world1->items()[1]);
    //QVERIFY(world1->items().size() == 1);
    //QVERIFY(dynamic_cast<WorldCopyTestItem*>(world1->items()[0])->link() == NULL);
}