/*
 * imageTrigTable.h
 *
 *  Created on: 11/06/2013
 *      Author: timothyw
 */

#ifndef THERMAL_BLOBBER_IMAGETRIGTABLE_H_
#define THERMAL_BLOBBER_IMAGETRIGTABLE_H_

#include <crosbot/data.hpp>
#include <crosbot/geometry.hpp>
#include <crosbot/handle.hpp>

using namespace crosbot;

namespace casualty {

/**
 * A size given in 2D.
 */
typedef struct Size2D {
	// The width and height [m]
	double width, height;

	Size2D() { width = height = 0; }
	Size2D(double width, double height) : width(width), height(height) {}
	Size2D(const Size2D& s) : width(s.width), height(s.height) {}

	//inline bool hasNaN() {return std::isnan(width) || std::isnan(height);}

   inline std::string toString() const {
    	char cStr[256];
    	sprintf(cStr, "(%.3lf, %.3lf)", width, height);
    	return std::string(cStr);
    }

    inline static bool parseString(const std::string& str, Size2D& s) {
    	if (sscanf(str.c_str(), "(%lf,%lf)", &s.width, &s.height) == 2) {
			return true;
		} else if (sscanf(str.c_str(), "(%lf, %lf)", &s.width, &s.height) == 2) {
			return true;
		} else if (sscanf(str.c_str(), "( %lf, %lf )", &s.width, &s.height) == 2) {
			return true;
		} else if (sscanf(str.c_str(), "( %lf , %lf )", &s.width, &s.height) == 2) {
			return true;
		}
    	return false;
    }

    inline static Size2D parseString(const std::string& str) {
    	Size2D rval;
    	if (parseString(str, rval))
    		return rval;
    	return Size2D(0,0);
    }
} Size2D;

inline bool operator==(const Size2D& lhs, const Size2D& rhs) {
	return lhs.width == rhs.width && lhs.height == rhs.height;
}

inline bool operator!=(const Size2D& lhs, const Size2D& rhs) {
	return lhs.width != rhs.width || lhs.height != rhs.height;
}

class _ImageTrigTable : public HandledObject {
public:
	unsigned int width, height;
	Size2D fov;

	double *tx; // The horizontal angle for a given pixel.
	double *ty; // The vertical angle for a given pixel.
	double *sintx; // The sine of the horizontal angle.
	double *costx; // The cosine of the horizontal angle.
	double *tantx; // The tangent of the horizontal angle.
	double *sinty; // The sine of the vertical angle.
	double *costy; // The cosine of the vertical angle.
	double *tanty; // The tangent of the vertical angle.

	_ImageTrigTable(unsigned int width, unsigned int height, Size2D fov);
	~_ImageTrigTable();
};
typedef Handle<_ImageTrigTable> ImageTrigTable;

} // namespace casualty

#endif /* THERMAL_BLOBBER_IMAGETRIGTABLE_H_ */
