CanadianMachines / MachineLib / Tubing.cpp
Tubing.cpp
Raw
/**
 * @file Tubing.cpp
 * @author sriram
 */

#include "pch.h"
#include "Tubing.h"
#include "Clamp.h"

///rate of increase of stiffness
const double StiffnessRate = 0.05;

Tubing::Tubing()
{
	mSink.SetComponent(this);
}

void Tubing::AddClamp(double x, double y, double rotation, double speed)
{
	auto clamp = std::make_shared<Clamp>();
	clamp->SetPosition(x,y);
	clamp->SetRotation(rotation);
	clamp->SetSpeed(speed);
	mClamps.push_back(clamp);
}

/**
 * draws
 * @param graphics
 * @param x
 * @param y
 */
void Tubing::Draw(std::shared_ptr<wxGraphicsContext> graphics, double x, double y)
{
	//graphics->Translate(x, y);
	auto path = graphics->CreatePath();

	auto source = mSink.GetSource();  // Where to draw from
	auto sink = mSource.GetSink();     // Where to draw to

	auto p1 = source->GetPosition();
	auto rotation = source->GetRotation();
	auto speed = source->GetSpeed();

	double stiffness = 1.0 + StiffnessRate * (mPressure - 1.0);

	for(auto clamp: mClamps)
	{
		path.MoveToPoint(wxPoint2DDouble(p1.m_x + x, p1.m_y + y));

		auto p2x = p1.m_x - sin(rotation * M_PI * 2) * speed * stiffness;
		auto p2y = p1.m_y + cos(rotation * M_PI * 2) * speed * stiffness;
		auto p2 = wxPoint2DDouble(p2x + x, p2y + y);

		auto p4 = clamp->GetPosition();
		rotation = fmod(clamp->GetRotation(),1);
		speed = clamp->GetSpeed();

		auto p3x = p4.m_x + sin(rotation * M_PI * 2) * speed * stiffness;
		auto p3y = p4.m_y - cos(rotation * M_PI * 2) * speed * stiffness;
		auto p3 = wxPoint2DDouble(p3x + x, (p3y+y ));

		path.AddCurveToPoint(p2, p3, wxPoint2DDouble(p4.m_x + x, p4.m_y + y));

		p1 = p4;
	}

	path.MoveToPoint(wxPoint2DDouble(p1.m_x + x, p1.m_y + y ));

	auto p2x = p1.m_x - sin(rotation * M_PI * 2) * speed * stiffness;
	auto p2y = p1.m_y + cos(rotation * M_PI * 2) * speed * stiffness;
	auto p2 = wxPoint2DDouble(p2x + x, p2y + y);

	auto p4 = sink->GetComponent()->GetPosition();
	rotation = fmod(sink->GetComponent()->GetRotation()+0.5,1);
	speed = sink->GetSpeed();

	auto p3x = p4.x + sin(rotation * M_PI * 2) * speed * stiffness;
	auto p3y = p4.y - cos(rotation * M_PI * 2) * speed * stiffness;
	auto p3 = wxPoint2DDouble(p3x + x, (p3y + y));

	path.AddCurveToPoint(p2, p3, wxPoint2DDouble(p4.x + x, p4.y + y));

	graphics->SetPen(wxPen(*wxBLACK ,3));
	graphics->StrokePath(path);
}

/**
 * sends pressure to the source
 * @param pressure
 */
void Tubing::SendPressure(double pressure)
{
	mPressure = pressure;
	mSource.SendPressure(pressure);
}