/*======================================================================================

	Program:		Range Imaging Toolkit (RITK)
	Module:			ritkOpenGLPointPickerCore.h
	Version:		$Revision: 2774 $
	Date:			$Date: 2012-03-13 10:52:35 +0100 (Di, 13 Mrz 2012) $

	Copyright (c) 2010-2011
	Pattern Recognition Lab, 
    University Erlangen-Nuremberg, Germany
    All rights reserved.

	This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
	See RITKCopyright.txt for more information.

	http://www5.informatik.uni-erlangen.de/research/software/ritk

========================================================================================*/

#ifndef OPENGLPOINTPICKERPOLYGONENTITY_H__
#define OPENGLPOINTPICKERPOLYGONENTITY_H__

#include <itkObject.h>
#include <itkObjectFactory.h>
#include <itkSimpleFastMutexLock.h>
#include <QVector3D>
#include <QMouseEvent>

#include "ritkOpenGLPointPickerEntity.h"

#include <GL/glu.h>

namespace ritk
{

/**	@ingroup	Visualization
 *	@class		OpenGLPointPickerPolygonEntity
 *	@author		Martin Stumpf
 *	@brief		PointPicker used to select Polygons
 *
 *	@details
 *	THIS CLASS CURRENTLY ONLY SUPPORTS 2D MODE!!!
 */





class VISUALIZATION_EXPORT OpenGLPointPickerPolygonEntity : public OpenGLPointPickerEntity
{
	Q_OBJECT	

public:
	/**	@name	Standard ITK typedefs */
	//@{
	typedef OpenGLPointPickerPolygonEntity					Self;
	typedef OpenGLPointPickerEntity							Superclass;
	typedef itk::SmartPointer<Self>							Pointer;
	typedef itk::SmartPointer<const Self>					ConstPointer;
	//@}

	typedef QLinkedList<PickedPoint *>						Polygon;
	typedef QLinkedList<Polygon *>							PolygonList;

	/// Object creation
	itkNewMacro(Self);
	
	/// RTTI
	itkTypeMacro(OpenGLPointPickerEntity, itk::Object);
	
	/// Overridden Render 2D to visualize the inside of the polygon
	virtual void Render2D(const OpenGLVisualizationWidget *Widget, QVector2D ImgSize);

	
	/// Locks for read access
	virtual void ReadLock();
	/// Unlocks; disables read access
	virtual void ReadUnlock();

	/** @name	ReadAccess functions using readLock */
	//@{
	virtual PolygonList * GetPolygonList();
	virtual QPoint GetDimensions();
	//@}

	/// Generates a new Polygon
	virtual void NewPolygon();

	/// Set Polygons
	virtual void SetPolygons(QList<QList<QVector2D> > &polyList);


protected:
	/// Constructor
	OpenGLPointPickerPolygonEntity();
	/// Destructor
	virtual ~OpenGLPointPickerPolygonEntity();
	
	virtual bool Event_NewPoint(PickedPoint p);
	virtual bool Event_LineClicked(int index);
	virtual bool Event_PointClicked(int index);
	virtual bool Event_PointDragged(int index, PickedPoint newPos);
	virtual bool Event_DeletePoint(int index);
	virtual bool Event_DeleteLine(int index);
	virtual bool Event_PointReleased(int index);
	virtual bool Event_ResetAll();

	/// Generates the LineList
	virtual void updateLists();
	/// Initializes Polygon Texture
	virtual void initPolyTexture();
	/// Generates Polygon Texture
	virtual void updatePolyTexture();
	/// Overridden to add Polygon detection
	virtual void calculateMouseOver();

signals:
	/**	@name Signals connected to the control unit. !*/
	//@{
	

	//@}
	/**	@name Other Signals !*/
	//@{
	void MouseOverPolygonChanged(int);
	void MouseOverPointChanged(ritk::OpenGLPointPickerPolygonEntity::PickedPoint *);

	//@}
	
	
	

protected:
	/// List to store Polygons
	PolygonList m_PolygonList;

	/// Texture for visualization of inside/outside of polygon
	GLuint m_PolygonTexture;
	QPoint m_PolygonTextureDimensions;

	/// Polygon index that is currently under the mouse pointer
	int m_mouseOverPolygon;


	/** Lock variable used to lock widget for read access */
	//@{
	bool m_ReadLock;
	itk::SimpleFastMutexLock m_ReadLockMutex;
	//@}

};




} // namespace ritk

		
#endif //OPENGLPOINTPICKERPOLYGONENTITY_H__
