/**********************************************************************************
* AUTHOR		: Deepu V.
* DATE			: 22-AUG-2005
* NAME			: recognizeTraces
* DESCRIPTION	: performs the recognition of the new strokes added to rc
*               : pre condition - markers are present in this vector
*               :               - m_numTracesProcessed  and m_numCharsProcessed
*               :                 set to proper value
* ARGUMENTS		: rc - The recognitino context
* RETURNS		:
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of
*************************************************************************************/
int BoxedFieldRecognizer::recognizeTraces(LTKRecognitionContext& rc )
{
	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
	    <<"Entering: BoxedFieldRecognizer::recognizeTraces"
	    <<endl;

	vector<LTKTrace>::const_iterator traceIter,traceEnd,traceBegin;
	                 //iterator for the traces

	int errorCode = FAILURE;

	int recUnit;     //unit for recognition (should be char)

	LTKTraceGroup emptyChar;
	                 //TraceGroup object that buffers
	                 //all ink corresponding to a character

	vector<int> subSet;
	                 //passing a null arguement for shape subset

	vector<LTKShapeRecoResult> shapeRecoResults;
	                 //The object to hold the output from shape recognizer

	LTKScreenContext screenContext = rc.getScreenContext();
	                 //retrieving the screen context

	LTKCaptureDevice captureDevice = rc.getDeviceContext();
	                 //retrieving the device context

	const LTKTraceVector & traces = rc.getAllInk();
	                //retrieving the traces from recognition context

	string tempStr;  //temporary string object


	if(m_shapeRecognizer == NULL)
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<"Shape recognizer not initialized" <<endl;

		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
		    <<"Error : "<< ENULL_POINTER <<":"<< getErrorMessage(ENULL_POINTER)
            <<" BoxedFieldRecognizer::recognizeTraces" <<endl;

		LTKReturnError(ENULL_POINTER);

	}
	else if( (errorCode = m_shapeRecognizer->setDeviceContext(captureDevice)) != SUCCESS)
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Unable to set device context in shape rec : " << getErrorMessage(errorCode) <<endl;

		 LOG(LTKLogger::LTK_LOGLEVEL_ERR)
             <<"Error: BoxedFieldRecognizer::recognizeTraces"<<endl;

		LTKReturnError(errorCode);
	}

	shapeRecoResults.reserve(m_numShapeRecoResults+1);//reserving memory


	if(m_numTracesProcessed > traces.size())
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Invalid number of traces processed. "
		    << "Traces processed = " << m_numTracesProcessed
            <<  " > total number of traces" << traces.size() <<endl;

        LOG(LTKLogger::LTK_LOGLEVEL_ERR)
		             <<"Error : "<< EINVALID_NUM_OF_TRACES <<":"<< getErrorMessage(EINVALID_NUM_OF_TRACES)
             <<" BoxedFieldRecognizer::recognizeTraces" <<endl;

		LTKReturnError(EINVALID_NUM_OF_TRACES);
	}
	//Start processing from the number of traces processed.
	traceBegin = traces.begin() + m_numTracesProcessed;
	traceEnd = traces.end();int r=0;

	for(traceIter = traceBegin; traceIter != traceEnd; ++traceIter)
	{
		/* Marker strokes are inserted to detect
		 * end of segment.  The marker strokes are
		 * identified by 9number of channels == 0)
         */
		if((*traceIter).getNumberOfPoints() == 0)
		{
			tempStr = REC_UNIT_INFO;
			if((errorCode = rc.getFlag(tempStr,recUnit)) != SUCCESS)
			{
				LOG(LTKLogger::LTK_LOGLEVEL_ERR)
            		<<"Error: BoxedFieldRecognizer::recognizeTraces"<<endl;

				LTKReturnError(errorCode);
			}
			switch(recUnit)
			{

			/* The segment is character
			 * This algorithm recognizes
			 * only character segments
			 */
			case REC_UNIT_CHAR:
				shapeRecoResults.clear();
				//calling the shape recognizer's recognize method.

				if(m_boxedChar.getNumTraces() == 0)
				{
					LTKShapeRecoResult T;
					T.setShapeId(SHRT_MAX);
					T.setConfidence(1.0);
					shapeRecoResults.push_back(T);
				}
				else if( (errorCode =	m_shapeRecognizer->recognize(m_boxedChar,screenContext,subSet,
					m_shapeRecoMinConfidence, m_numShapeRecoResults, shapeRecoResults ))!= SUCCESS )
				{
					LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Shape recognition failed : " << getErrorMessage(errorCode) <<endl;

					LOG(LTKLogger::LTK_LOGLEVEL_ERR)
            			<<"Error: BoxedFieldRecognizer::recognizeTraces"<<endl;

					LTKReturnError(errorCode);
				}


				//This updates the recognition results using
				//current shape recognition results

				if((errorCode = updateRecognitionResults(shapeRecoResults,rc)) != SUCCESS)
				{
					LOG(LTKLogger::LTK_LOGLEVEL_ERR)
            			<<"Error: BoxedFieldRecognizer::recognizeTraces"<<endl;

					LTKReturnError(errorCode);
				}

				for(r=0;r<shapeRecoResults.size();++r)
				{
					LTKShapeRecoResult& tempResult=shapeRecoResults[r];

				}

				m_boxedChar = emptyChar;//making the trace group empty again
				break;

			default:
				LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Unsupported reccognizer mode by Box Field" <<endl;

				LOG(LTKLogger::LTK_LOGLEVEL_ERR)
				    <<"Error : "<< EINVALID_RECOGNITION_MODE <<":"<< getErrorMessage(EINVALID_RECOGNITION_MODE)
             		<<" BoxedFieldRecognizer::recognizeTraces" <<endl;

				LTKReturnError(EINVALID_RECOGNITION_MODE);

			}
			++m_numCharsProcessed;      //incrementing number of characters processed
		}
		else
		{
			m_boxedChar.addTrace(*traceIter); //buffering the trace to the temp TraceGroup for recognition
		}
		++m_numTracesProcessed;         //incrementing the number of traces processed
	}


	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
		<<"Exiting: BoxedFieldRecognizer::recognizeTraces"
		<<endl;

	return SUCCESS;
}