/*
 *	osgART/Tracker/ARToolKit/ARToolKitPlusTracker
 *	osgART: AR ToolKit for OpenSceneGraph
 *
 *	Copyright (c) 2005-2007 ARToolworks, Inc. All rights reserved.
 *	
 *	Rev		Date		Who		Changes
 *  1.0   	2006-12-08  ---     Version 1.0 release.
 *
 */
/*
 * This file is part of osgART - AR Toolkit for OpenSceneGraph
 *
 * Copyright (c) 2005-2007 ARToolworks, Inc. All rights reserved.
 *
 * (See the AUTHORS file in the root of this distribution.)
 *
 *
 * OSGART is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * OSGART 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 the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OSGART; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */


#ifndef OSGART_ARTTOOLKITTRACKER
#define OSGART_ARTTOOLKITTRACKER

/**
 * \file  ARToolKitPlusTracker
 * \brief A wrapper class for ARToolkit tracking system.
 *
 * Support Marker-Based tracking based on computer vision based on ARToolKit
 * Algorithm. This class provided video/graphic independent marker detection and
 * support multiple marker tracking.
 *	
 * \author Philip Lamb philip.lamb@artoolworks.com
 * \author Julian Looser Julian.Looser@hitlabnz.org
 * \author Raphael Grasset Raphael.Grasset@hitlabnz.org
 */

#include "osgART/Export"
#include "osgART/Marker"
#include "osgART/GenericVideo"
#include "osgART/GenericTracker"

#include <string>

#include <ARToolKitPlus/TrackerMultiMarker.h>


namespace osgART {

	/**
	* \class ARToolKitPlusTracker
	* \brief A tracker based on ARToolKit 2x. 
	* 
	* This tracker supports single and multi-markers.
	*
	*/
	class ARToolKitPlusTracker : public GenericTracker
	{

	public:        
	   
		/** 
		* \brief constructor.
		*/
		ARToolKitPlusTracker(void);

		/** 
		* \brief set the openGL parameters for the projection matrix.
		* 
		* Initialize internal parameters computation for delivers an openGL Matrix (modify
		* nothing to openGL state machine). 
		* \param n near field of view
		* \param f far field of view
		*/
		void setProjection(const double n, const double f);
		
		/** 
		* \brief Set the threshold value for the tracking algorithm.
		* 
		* Define the threshold used to binarize the image in ARToolkit.
		* \param thresh integer value between 0 (dark) and 255 (bright) 
		*/
		void setThreshold(const int& thresh);
		
		/**
		* \brief Get the current threshold value for the tracking algorithm.
		*
		* \return The current threshold.
		*/
		int getThreshold() const;	
		

		unsigned char* getDebugImage();	
		void setDebugMode(bool d);
		bool getDebugMode();
		
		
		/** 
		* \brief Initialize ARToolKit.
		* 
		* This core function initializes different parameters of ARToolKit: load tracked markers, initialize
		* camera parameters.
		* \param xsize width of the image to analyze
		* \param ysize height of the image to analyze
		* \param pattlist_name a file structure of the marker
		* \param camera_name description file of the camera used (pinhole model)
		*/
		virtual bool init(int xsize, int ysize, 
			const std::string& pattlist_name = "Data/markers_list.dat", 
			const std::string& camera_name="Data/camera_para.dat");
		
		/** 
		* \brief Register a single marker with the tracker.
		* 
		* \param pattFile the pattern file name
		* \param width the width of the physical marker
		* \param center the x-y coordinates of the center of the marker (usually 0,0)
		* \return the internal ID of the new marker, or -1 on failure 
		*/
		int addSingleMarker(const std::string& pattFile, 
			double width, double center[2]);

		int addSimpleMarker(const std::string& pattFile, 
			double width, double center[2]);
		
		/** 
		* \brief Register a multi-marker with the tracker
		* 
		* \param multiFile the multi-marker configuration file name
		* \return the internal ID of the new marker, or -1 on failure
		*/
		int addMultiMarker(const std::string& multiFile);
		
		/** 
		* \brief set the image which will be used for tracking.
		* 
		* \param image Pointer to raw image pixels.
		* \param format OSGART-defined pixel format of the raw pixels. This will be converted
		*		to a format understood by ARToolKit internally.
		*/
		virtual void setImageRaw(unsigned char * image, PixelFormatType format = VIDEOFORMAT_GREY8);
		
		/** 
		* \brief update the tracking.
		* 
		* This core function applies the ARToolKit algorithm to the last image defined by setImage.
		*/
		void update();

                virtual Marker* getMarker(int markerId);
		
	protected:
	
		/** 
		 * \brief destructor.
		 */
		virtual ~ARToolKitPlusTracker();
		
		virtual void createUndistortedMesh(int,int,float,float,osg::Geometry&);
				
	private:
		
		struct				CameraParameter;
		CameraParameter*	m_cparam;
	
		bool setupMarkers(const std::string& patternListFile);
		void cleanupMarkers(void);

		std::string 		cparamName;
		int					m_threshold;

		bool 				m_debugmode;
		
		int					m_marker_num;
		
                int                 m_artoolkit_pixformat;
                int                 m_artoolkit_pixsize;

                ARToolKitPlus::TrackerMultiMarker *m_tracker;

	};




};


#endif 


