/*
 * ClippedFrame.h
 *
 *  Created on: Jul 27, 2014
 *      Author: jared
 */
#include "opencv2/opencv.hpp"

#ifndef CLIPPEDFRAME_H_
#define CLIPPEDFRAME_H_

class ClippedFrame {
public:
	ClippedFrame();
	/* Create a new frame centered at (x,y) with the given dimensions and FPS. */
	ClippedFrame(int x, int y, int width, int height, int fps);
	virtual ~ClippedFrame();

	// Get Frames per second
	int getFps() const { return _fps; }

	// Set Frames per second
	void setFps(int fps) { _fps = fps; }

	// Get current x coordinate
	int getX() const { return _x; }

	// Get current y coordinate
	int getY() const { return _y; }

	// Get frame height
	int getHeight() const { return _height; }

	// Get frame width
	int getWidth() const { return _width; }

	// Start the frame moving to (x,y) in dt seconds.
	void moveTo(int x, int y, double dt);

	// Update the motion by 1 frame
	void update();

	// Draw the clipped frame as a rectangle over the canvas
	void drawTo(cv::Mat canvas, cv::Scalar colour = cv::Scalar(0,0,255));

	// Get the current clipping as a matrix
	cv::Mat getAsMat(cv::Mat canvas);

	// Get the percent of time by which acceleration should become deceleration
	int getMotionAccelerationPercent() const { return _motionAccelerationPercent; }

	// Set the percent of time by which acceleration should become deceleration
	void setMotionAccelerationPercent(int motionAccelerationPercent) { _motionAccelerationPercent = motionAccelerationPercent; }

	// Modify the given x,y co-ordinates so the frame can be there, given the canvas bounds "width" and "height".
	void snapToWithinBounds(int& x, int& y, int canvasWidth, int canvasHeight);
	void setPosition(int x, int y, int canvasWidth, int canvasHeight);

private:
	int _x, _y, _width, _height, _fps;

	// Variables dealing with motion
	bool _isMoving;
	int _motionFrameStart, _motionFrameEnd;
	int _motionXStart, _motionYStart;
	int _motionXEnd, _motionYEnd;
	int _motionFrameMid;
	int _motionFrameCurrent;
	int _motionAccelerationPercent;

	// Get the angle between 2 points relative to the positive x-axis
	double getTheta(int x1, int y1, int x2, int y2);

	// Get the distance between 2 points
	double getDistance(double dX0, double dY0, double dX1, double dY1);
};

#endif /* CLIPPEDFRAME_H_ */
