diff --git a/alpha/alpha.pro b/alpha/alpha.pro index a1257d9..47a9fe1 100644 --- a/alpha/alpha.pro +++ b/alpha/alpha.pro @@ -1,48 +1,48 @@ TEMPLATE = app QT += opengl CONFIG += c++11 release warn_on greaterThan(QT_MAJOR_VERSION, 4): QT += widgets DESTDIR += bin TARGET = alpha INCLUDEPATH += \ $$PWD/../symath \ $$PWD/../symgraph HEADERS += \ alphascene.h \ descriptors/ressort.h \ descriptors/pendules.h \ marbled.h \ pendule.h \ ressor.h \ assets.h \ newtonsphere.h \ - descriptors/pendulecouple.h \ descriptors/pendulec1.h \ - descriptors/pendulec2.h + descriptors/pendulec2.h \ + descriptors/pendulec.h SOURCES += \ main.cpp \ alphascene.cpp \ descriptors/ressort.cpp \ descriptors/pendules.cpp \ marbled.cpp \ pendule.cpp \ ressor.cpp \ assets.cpp \ newtonsphere.cpp \ - descriptors/pendulecouple.cpp \ descriptors/pendulec1.cpp \ - descriptors/pendulec2.cpp + descriptors/pendulec2.cpp \ + descriptors/pendulec.cpp \ win32:LIBS += opengl32.lib LIBS += -L../symath/lib/ \ -L../symgraph/lib/ \ -lsymath \ -lsymgraph diff --git a/alpha/alphascene.cpp b/alpha/alphascene.cpp index aad4923..9a454b0 100644 --- a/alpha/alphascene.cpp +++ b/alpha/alphascene.cpp @@ -1,171 +1,179 @@ #define _USE_SVECTOR_OSTREAM #include "alphascene.h" #include <iostream> #include "newtonsphere.h" #include "pendule.h" #include "marbled.h" #include "ressor.h" +//#include "pendulecoupl.h" #include <QKeyEvent> #include "skerror.h" #include "models/glsphere.h" #include "models/glmarbled.h" using namespace std; using namespace symkit; using namespace models; #include "camera.h" #define DARK_BLUE 0.0f, 0.05f, 0.15f #define BLUE_SKY 0.48f, 0.8f, 0.97f AlphaScene::AlphaScene(QWidget *parent) : Scene(parent), KeyListener() // enabled { sphereModel = new GLSphere(); marbledModel = new GLMarbled(); sphere = new NewtonSphere(sphereModel); pendule = new Pendule(sphereModel); ressor = new Ressor(sphereModel); + //pendulecoupl = new PenduleCoupl(sphereModel); marbled = new Marbled(marbledModel); /* Set the color of the sky */ setClearColor(DARK_BLUE); try { /* Load shapes */ addActor(sphere); addActor(pendule); addActor(marbled); addActor(ressor); + //addActor(pendulecoupl); } catch (const SKError& err) { cerr << err << endl; } /* Set a camera offset */ getCamera()->move(10, Z); // register key events addKeyListener(this); } AlphaScene::~AlphaScene() { delete sphere; delete marbled; delete pendule; delete ressor; + //delete pendulecoupl; delete sphereModel; delete marbledModel; } #include "assets.h" void AlphaScene::initialize() { sphereModel->initialize(); marbledModel->initialize(); } void AlphaScene::closeEvent(QCloseEvent*) { /* Remove all shapes */ this->removeAllActors(); cout << "Closing OpenGL window" << endl; } #define T_UNIT 0.25 #define R_UNIT 2.5 void AlphaScene::onKeyPress(int key) { switch(key) { case Qt::Key_W: getCamera()->move(-T_UNIT, Z); break; case Qt::Key_S: getCamera()->move(T_UNIT, Z); break; case Qt::Key_A: getCamera()->move(-T_UNIT, X); break; case Qt::Key_D: getCamera()->move(T_UNIT, X); break; case Qt::Key_K: getCamera()->move(T_UNIT, Y); break; case Qt::Key_J: getCamera()->move(-T_UNIT, Y); break; case Qt::Key_Up: getCamera()->rotate(-R_UNIT, X); break; case Qt::Key_Down: getCamera()->rotate(R_UNIT, X); break; case Qt::Key_Right: getCamera()->rotate(-R_UNIT, Y); break; case Qt::Key_Left: getCamera()->rotate(R_UNIT, Y); break; case Qt::Key_Space: // give the sphere an impulse sphere->applyRepulsion( getCamera() ); break; case Qt::Key_P: // stop the sphere sphere->toggleEvolving(); sphere->toggleRunningDescriptor(); pendule->toggleEvolving(); pendule->toggleRunningDescriptor(); ressor->toggleEvolving(); ressor->toggleRunningDescriptor(); + +/* pendulecoupl->toggleEvolving(); + pendulecoupl->toggleRunningDescriptor();*/ + break; case Qt::Key_V: // set the sphere to verbose mode sphere->setVerbose( !sphere->getVerbose() ); break; case Qt::Key_Escape: QWidget::close(); break; default: break; } } void AlphaScene::onKeyRelease(int) { } diff --git a/alpha/alphascene.h b/alpha/alphascene.h index 5f1cda8..b5ced6f 100644 --- a/alpha/alphascene.h +++ b/alpha/alphascene.h @@ -1,58 +1,62 @@ #ifndef __ALPHA_SCENE_H__ #define __ALPHA_SCENE_H__ #include "scene.h" #include "listeners/keylistener.h" //Forward include class NewtonSphere; class Marbled; class Pendule; class Ressor; +//class PenduleCoupl; namespace models { class GLSphere; class GLMarbled; } class AlphaScene : public Scene, KeyListener { public: /* Constructor and destructor */ AlphaScene(QWidget* parent = 0); ~AlphaScene(); private: /* initialize in order to load sphere buffer */ virtual void initialize() override; /* Window close qt handler */ virtual void closeEvent(QCloseEvent*) override; virtual void onKeyPress(int key) override; virtual void onKeyRelease(int key) override; /* A sphere object */ NewtonSphere * sphere; /* The murbled plan */ Marbled * marbled; /* The pendule */ Pendule * pendule; /* The ressor */ Ressor * ressor; + /* The pendulecoupl */ + //PenduleCoupl * pendulecoupl; + models::GLSphere * sphereModel; models::GLMarbled * marbledModel; }; #endif diff --git a/alpha/descriptors/pendulecouple.cpp b/alpha/descriptors/pendulec.cpp similarity index 97% rename from alpha/descriptors/pendulecouple.cpp rename to alpha/descriptors/pendulec.cpp index 6432314..63fcddf 100644 --- a/alpha/descriptors/pendulecouple.cpp +++ b/alpha/descriptors/pendulec.cpp @@ -1,55 +1,55 @@ -#include "pendulecouple.h" +#include "pendulec.h" #include <cmath> double PenduleC::g = 9.81; void PenduleC::setGravity(const double& g) { PenduleC::g=g; } PenduleC::PenduleC(const Vector& p, const Vector& p_prime, const double& l, const double& m) : Oscillateur(p, p_prime), l(l), m(m) {} PenduleC::PenduleC(const std::vector<double>& p, const std::vector<double>& p_prime, const double& l, const double& m) : Oscillateur(p, p_prime), l(l), m(m) {} void PenduleC::setOtherPendule(const PenduleC * other) { this->other=other; } double PenduleC::getl() const { return l; } double PenduleC::getm() const { return m; } void PenduleC::setl(const double& l) { this->l=l; } void PenduleC::setm(const double& m) { this->m=m; } Vector PenduleC::equation() const { double a(mp()*g*cos(dteta())*sin(other->getp()[0])); double b((m+other->getm())*g*sin(p[0])); double c(m2()*l*p_prime[0]*p_prime[0]*cos(dteta())*sin(dteta())); double d(mp()*other->getl()*other->getp_prime()[0]*other->getp_prime()[0]*sin(dteta())); return { (a -b +c -d)/(l*m1() + l*m2()*sin(dteta())*sin(dteta())) }; } diff --git a/alpha/descriptors/pendulecouple.h b/alpha/descriptors/pendulec.h similarity index 100% rename from alpha/descriptors/pendulecouple.h rename to alpha/descriptors/pendulec.h diff --git a/alpha/descriptors/pendulec1.cpp b/alpha/descriptors/pendulec1.cpp index 38c38a9..babdb45 100644 --- a/alpha/descriptors/pendulec1.cpp +++ b/alpha/descriptors/pendulec1.cpp @@ -1,29 +1,34 @@ #include "pendulec1.h" PenduleC1::PenduleC1(const Vector& p, const Vector& p_prime, const double& l, const double& m) : PenduleC(p, p_prime, l, m) {} PenduleC1::PenduleC1(const std::vector<double>& p, const std::vector<double>& p_prime, const double& l, const double& m) : PenduleC(p, p_prime, l, m) {} double PenduleC1::mp() const { return other->getm(); } double PenduleC1::dteta() const { return p[0] -other->getp()[0]; } double PenduleC1::m1() const { return getm(); } double PenduleC1::m2() const { return other->getm(); } + +SVector<3> PenduleC1::cartesiennes() const +{ + return { sin(p[0]) * getl(), -cos(p[0]) * getl(), 0 }; +} diff --git a/alpha/descriptors/pendulec1.h b/alpha/descriptors/pendulec1.h index 3c1c8d5..7dd5454 100644 --- a/alpha/descriptors/pendulec1.h +++ b/alpha/descriptors/pendulec1.h @@ -1,24 +1,30 @@ #ifndef PENDULEC1_H #define PENDULEC1_H -#include "pendulecouple.h" +#include "pendulec.h" /* Premier pendule de la couple */ class PenduleC1 : public PenduleC { public: /* Constructeurs (qui appellent juste celui de "PenduleC" ) */ PenduleC1(const Vector&, const Vector&, const double&, const double&); PenduleC1(const std::vector<double>&, const std::vector<double>&, const double&, const double&); - /* Override des fonctions pour la désimbiguation des paramètres dans la fonction "equation" */ + /* Override des fonctions pour la désimbiguation des paramètres dans la fonction "equation" + * Pour qu'à chaque sous classe de PenduleC corresponde la bonne equation differentielle + */ virtual double mp() const override; virtual double dteta() const override; virtual double m2() const override; virtual double m1() const override; + + /* Convertit position en un SVector<3> pour l'implementation graphique */ + virtual SVector<3> cartesiennes() const override; + }; #endif // PENDULEC1_H diff --git a/alpha/descriptors/pendulec2.cpp b/alpha/descriptors/pendulec2.cpp index 6bf9ea4..e17b22f 100644 --- a/alpha/descriptors/pendulec2.cpp +++ b/alpha/descriptors/pendulec2.cpp @@ -1,29 +1,36 @@ -#include "pendulec2.h".h" +#include "pendulec2.h" PenduleC2::PenduleC2(const Vector& p, const Vector& p_prime, const double& l, const double& m) : PenduleC(p, p_prime, l, m) {} PenduleC2::PenduleC2(const std::vector<double>& p, const std::vector<double>& p_prime, const double& l, const double& m) : PenduleC(p, p_prime, l, m) {} double PenduleC2::mp() const { return getm()+ other->getm(); } double PenduleC2::dteta() const { return other->getp()[0] -p[0]; } double PenduleC2::m1() const { return other->getm(); } double PenduleC2::m2() const { return getm(); } + + +SVector<3> PenduleC2::cartesiennes() const +{ + SVector<3> v({ sin(p[0]) * getl(), -cos(p[0]) * getl(), 0 }); + return other->cartesiennes() + v; +} diff --git a/alpha/descriptors/pendulec2.h b/alpha/descriptors/pendulec2.h index 0d598ce..2f1193c 100644 --- a/alpha/descriptors/pendulec2.h +++ b/alpha/descriptors/pendulec2.h @@ -1,25 +1,30 @@ #ifndef PENDULEC2_H #define PENDULEC2_H - -#include "pendulecouple.h" +#include "pendulec.h" /* Deuxième pendule de la couple */ class PenduleC2 : public PenduleC { public: /* Constructeurs (qui appellent juste celui de "PenduleC" ) */ PenduleC2(const Vector&, const Vector&, const double&, const double&); PenduleC2(const std::vector<double>&, const std::vector<double>&, const double&, const double&); - /* Override des fonctions pour la désimbiguation des paramètres dans la fonction "equation" */ + /* Override des fonctions pour la désimbiguation des paramètres dans la fonction "equation" + * Pour qu'à chaque sous classe de PenduleC corresponde la bonne equation differentielle + */ virtual double mp() const override; virtual double dteta() const override; virtual double m2() const override; virtual double m1() const override; + + /* Convertit position en un SVector<3> pour l'implementation graphique */ + virtual SVector<3> cartesiennes() const override; + }; #endif // PENDULEC2_H diff --git a/alpha/pendule.h b/alpha/pendule.h index af55ce2..b40eca1 100644 --- a/alpha/pendule.h +++ b/alpha/pendule.h @@ -1,46 +1,46 @@ #ifndef PENDULE_H #define PENDULE_H #include "actors/particles/oscillatorparticle.h" #include "descriptors/pendules.h" #include "specs/evolvable.h" namespace models { class GLSphere; } class Pendule : public OscillatorParticle, public Evolvable { public: /* Constructor and destructor */ Pendule(models::GLSphere *); ~Pendule(); /* initialize function */ virtual void initialize() override; /* set color */ virtual void evolve(float dt) override; SPECS_EVOLVE /* draw/render function */ virtual void render(symkit::render_s&) override; private: PenduleS * pendule; static const Vector p; - static const Vector p_prime; + static const Vector p_prime; static const double l; static const double m; }; #endif // PENDULE_H diff --git a/alpha/pendulecoupl.cpp b/alpha/pendulecoupl.cpp new file mode 100644 index 0000000..7b72ec4 --- /dev/null +++ b/alpha/pendulecoupl.cpp @@ -0,0 +1,93 @@ +#include "pendulecoupl.h" + +#include "models/glsphere.h" + +#include "sktools.h" + +using namespace symkit; + +#ifndef M_PI +#define M_PI 3.141592654 +#endif + +const Vector PenduleCoupl::p1 = {M_PI}; +const Vector PenduleCoupl::p_prime1 = {0}; +const Vector PenduleCoupl::p2 = {M_PI}; +const Vector PenduleCoupl::p_prime2 = {0}; + +const double PenduleCoupl::l1 = 5.0; +const double PenduleCoupl::m1 = 1.0; +const double PenduleCoupl::l2= 5.0; +const double PenduleCoupl::m2 = 1.0; + +PenduleCoupl::PenduleCoupl(models::GLSphere * model) + + : OscillatorParticle(NULL, model) +{ + /* initialize descriptors */ + pendulec1 = new PenduleC1(p1, p_prime1, l1, m1); + pendulec2 = new PenduleC2(p2, p_prime2, l2, m1); + + pendulec1->setOtherPendule(pendulec2); + pendulec2->setOtherPendule(pendulec1); + + /* bind the descriptors */ + bind(pendulec1); + bind(pendulec2); + + /* set fill mode to false (default true) */ + toggleFillMode(); + + /* set evolving to true (default false) */ + toggleEvolving(); +} + +PenduleCoupl::~PenduleCoupl() +{ + /* Release descriptor */ + release(); + + delete pendulec1; + delete pendulec2; +} + +#include "skerror.h" + +using namespace symkit; + +void PenduleCoupl::initialize() +{ + if (!isBound()) + throw SKError(ERR_INIT, + "initialize", + "alpha.PenduleCoupl", + "Descriptor isn't bound"); +} + +#include "assets.h" // colorGradientFunction + +/* set color */ +void PenduleCoupl::evolve(float) +{ + setColor( alpha::colorGradientFunction(pendulec1->getp_prime().module(), 3.0) ); + setColor( alpha::colorGradientFunction(pendulec2->getp_prime().module(), 3.0) ); +} + +#include <QGLShaderProgram> + +/* draw/render function */ +void PenduleCoupl::render(render_s& args) +{ + OscillatorParticle::render(args); + + glBegin(GL_LINES); + + args.shaderProgram->setAttributeValue(AttributeId::vertex, ARRAY_3D_FLOAT_ARGS(SVector<3>::nullv)); // center + args.shaderProgram->setAttributeValue(AttributeId::vertex, ARRAY_3D_FLOAT_ARGS(pendulec1->cartesiennes())); + + glBegin(GL_LINES); + args.shaderProgram->setAttributeValue(AttributeId::vertex, ARRAY_3D_FLOAT_ARGS(pendulec1->cartesiennes())); + args.shaderProgram->setAttributeValue(AttributeId::vertex, ARRAY_3D_FLOAT_ARGS(pendulec2->cartesiennes())); + + glEnd(); +} diff --git a/alpha/pendulecoupl.h b/alpha/pendulecoupl.h new file mode 100644 index 0000000..abcc773 --- /dev/null +++ b/alpha/pendulecoupl.h @@ -0,0 +1,50 @@ +#ifndef PENDULEC_H +#define PENDULEC_H + + +#include "actors/particles/oscillatorparticle.h" +#include "descriptors/pendulec1.h" +#include "descriptors/pendulec2.h" +#include "specs/evolvable.h" + +namespace models +{ + class GLSphere; +} + +class PenduleCoupl : public OscillatorParticle, public Evolvable +{ +public: + + /* Constructor and Destructor */ + PenduleCoupl(models::GLSphere *); + ~PenduleCoupl(); + + SPECS_EVOLVE + + /* set color */ + virtual void evolve(float) override; + + /* draw/render function */ + virtual void render(symkit::render_s&) override; + + /* initialize function */ + virtual void initialize() override; + +private: + + PenduleC1 * pendulec1; + PenduleC2 * pendulec2; + + static const Vector p1; + static const Vector p_prime1; + static const Vector p2; + static const Vector p_prime2; + + static const double l1; + static const double m1; + static const double l2; + static const double m2; +}; + +#endif // PENDULEC_H