Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
This first version supports Cyclone dds Cxxx only. It requires QT5 and does not work with Vortex Opensplice.
  • Loading branch information
RamziKaroui authored Oct 22, 2020
1 parent 5f680eb commit c49b06f
Show file tree
Hide file tree
Showing 51 changed files with 9,839 additions and 203 deletions.
94 changes: 94 additions & 0 deletions BouncingShapeDynamics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* @file
*/
#include "BouncingShapeDynamics.hpp"
#include <math.h>
#include <stdlib.h>

#include <iostream>
static const float PI = 3.1415926535F;
#ifdef TESTBUILD
#include <QtCore/QDebug>
#include "os_time.h"
#endif
#ifdef WIN32
#define roundf(a) ((a)>0?floor((a)+0.5):ceil((a)-0.5))
#endif

namespace demo { namespace ishapes {

BouncingShapeDynamics::BouncingShapeDynamics(int x0, int y0,
const QRect& shapeBounds,
const QRect& constraint,
float angle,
float speed,
const ShapeType& shape,
dds::pub::DataWriter<ShapeType> shapeWriter)
: ShapeDynamics(x0, y0, constraint),
shapeBounds_(shapeBounds),
alpha_(angle),
angle_(angle),
speed_(speed),
shape_(shape),
shapeWriter_(shapeWriter)
{ }


BouncingShapeDynamics::~BouncingShapeDynamics()
{ }

bool
BouncingShapeDynamics::flip()
{
bool doflip = false;
if (rand() <= RAND_MAX/2)
doflip = true;

return doflip;
}

void
BouncingShapeDynamics::simulate()
{
pos_.rx() = roundf(pos_.rx() + speed_*cosf(angle_));
pos_.ry() = roundf(pos_.ry() + speed_*sinf(angle_));

if (pos_.x() <= 0)
{
angle_ = this->flip() ? -alpha_ : alpha_;
pos_.rx() = 0;
}
else if (pos_.x() >= (constraint_.width() - (shapeBounds_.width())))
{
angle_ = this->flip() ? (PI + alpha_) : (PI - alpha_);
pos_.rx() = constraint_.width() - shapeBounds_.width();
}
else if (pos_.y() <= 0)
{
angle_ = this->flip() ? alpha_ : PI - alpha_;
pos_.ry() = 0;
}
else if (pos_.y() >= (constraint_.height() - shapeBounds_.height()))
{
angle_ = this->flip() ? (PI+alpha_) : -alpha_;
pos_.ry() = constraint_.height() - shapeBounds_.height();
}

shape_.x() = pos_.x();
shape_.y() = pos_.y();
shapeWriter_.write(shape_);

plist_.erase(plist_.begin(), plist_.end());
plist_.push_back(pos_);
#ifdef TESTBUILD
qDebug() << "Time:"
<< os_timeGet().tv_sec
<< os_timeGet().tv_nsec
<< "Colour:"
<< shape_.color().c_str()
<< "Size:" << shape_.shapesize()
<< "x:" << shape_.x()
<< "y:" << shape_.y();
#endif
}
}}
59 changes: 59 additions & 0 deletions BouncingShapeDynamics.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef _BOUNCINGSHAPEDYNAMICS_HPP
#define _BOUNCINGSHAPEDYNAMICS_HPP

/** @file */
/**
* @addtogroup demos_iShapes
*/
/** @{*/

#include <dds/dds.hpp>

#include <ShapeDynamics.hpp>
#include <QtCore/QRect>

// -- Shaped Include
#include <topic-traits.hpp>
#include <ishape.h>
using namespace ShapeTypeDemo;
namespace demo { namespace ishapes {
class BouncingShapeDynamics : public ShapeDynamics
{
public:
BouncingShapeDynamics(int x0, int y0,
const QRect& shapeBounds,
const QRect& constraints,
float speed,
float angle,
const ShapeType& shape,
dds::pub::DataWriter<ShapeType> shapeWriter);

virtual ~BouncingShapeDynamics();

public:
virtual void simulate();

public:
typedef ::dds::core::smart_ptr_traits<BouncingShapeDynamics>::ref_type ref_type;

private:
BouncingShapeDynamics(const BouncingShapeDynamics& orig);
BouncingShapeDynamics& operator=(const BouncingShapeDynamics&);

private:
bool flip();

private:
QRect shapeBounds_;
float alpha_;
float angle_;
float speed_;
ShapeType shape_;
dds::pub::DataWriter<ShapeType> shapeWriter_;
};
}
}

/** @}*/

#endif /* _BOUNCINGSHAPEDYNAMICS_HPP */
Loading

0 comments on commit c49b06f

Please sign in to comment.