#include "logic.h"
#include "camatchncc_external.h"
#include "cpp/HalconCpp.h"

CAMatchNCC_External::CAMatchNCC_External(const QString& name,int32_t id)
	: CAMatchNCC_H(name, id)
{

}

CAMatchNCC_External::~CAMatchNCC_External(void)
{
}

void CAMatchNCC_External::showresult_string(CImageContainer* pContainer)
{
	QList<CMPointd>::const_iterator iter = lcenters.cbegin();
	//pContainer->addString(m_sName);
	//for(;iter!=lcenters.cend();iter++)
	//{
	//	pContainer->addString(QString("X%1,Y%2").arg((*iter).tX).arg((*iter).tY));
	//}
	CAMatchNCC_H::showresult_string(pContainer);
}

void CAMatchNCC_External::showresult_shape(CImageContainer* pContainer)
{
	CAMatchNCC_H::showresult_shape(pContainer);
}

int32_t CAMatchNCC_External::exe_morphology(CMImage* pmimage)
{
	QMutexLocker l(&m_lMorphology.mutex);
	for (auto ii=m_lMorphology.cbegin();ii!=m_lMorphology.cend();ii++)
	{
		(*ii)->exe(pmimage);
	}

	return 0;
}

int32_t CAMatchNCC_External::exe(CMImage* pmimage)
{
	using namespace Halcon;
	Hobject   Image, Region, ImageResult;
	HTuple errorText;
	HTuple Pointer, Type, Width, Height, Pointer1;
	Herror err = gen_image1_extern(&Image, "byte", pmimage->m_width, pmimage->m_height, (uintptr_t)pmimage->m_pRawImage, 0);

	gray_closing_rect(Image, &Image, 10, 10);
	//Var_Match_NumLevels_ID,		//金字塔层数
	//	Var_Match_AngleStart_ID,	//开始角度
	//	Var_Match_AngleExtent_ID,	//角度范围
	//	Var_Match_AngleStep_ID,		//角度步进
	//	Var_Match_MiniScore_ID,		//最小分数
	//	Var_Match_NumMatches_ID,	//匹配数量
	//	Var_Match_MaxOverlap_ID,	//最大重叠
	//	Var_Match_SubPixel_ID,		//亚像素匹配
	//	Var_Match_Greediness_ID,	//形状匹配，贪婪
	//	Var_Match_Contrast_ID,		//形状匹配，Contrast
	//	Var_Match_MinContrast_ID,	//形状匹配，MinContrast
	//	Var_Match_ThresScore_ID,

	lresults.clear();
	lcenters.clear();

	double angleStart = m_Variants.get_VData(Var_Match_AngleStart_ID)->toDouble();
	double angleExtent = m_Variants.get_VData(Var_Match_AngleExtent_ID)->toDouble();
	double minScore = m_Variants.get_VData(Var_Match_MiniScore_ID)->toDouble();
	int numMatches = m_Variants.get_VData(Var_Match_NumMatches_ID)->toInt();
	double maxOverlap = m_Variants.get_VData(Var_Match_MaxOverlap_ID)->toDouble();
	bool bSubPixel = m_Variants.get_VData(Var_Match_SubPixel_ID)->toBool();
	int numLevels = m_Variants.get_VData(Var_Match_NumLevels_ID)->toInt();
	//bool bEnableAoi = m_Variants.get_VData(Var_ma)
	Hobject	 ImageReduced;	
	if(isROIEnabled()){
		Hobject  Region;
		gen_rectangle1(&Region, m_rect_ROI.topleft().tX, m_rect_ROI.topleft().tY, m_rect_ROI.m_width, m_rect_ROI.m_height);
		reduce_domain(Image, Region, &ImageReduced);
	}
	HTuple  Row, Column, Angle, Score;
	if(true == bSubPixel){
		find_ncc_model(Image, modelData.iModelID, RAD(angleStart), RAD(angleExtent), minScore, numMatches, maxOverlap, "true", numLevels, &Row, &Column, 
			&Angle, &Score);
	}
	else{
		find_ncc_model(Image, modelData.iModelID, RAD(angleStart), RAD(angleExtent), minScore, numMatches, maxOverlap, "false", numLevels, &Row, &Column, 
			&Angle, &Score);
	}

	if(Row.Num()<=0)
		return 0;

	for(int i=0;i<Row.Num();i++){
		RESULT data;
		data.angle = Angle[i];
		data.score = Score[i];

		lresults.push_back(data);

		CMPointd ptCenter;
		ptCenter.tX = Column[i];
		ptCenter.tY = Row[i];

		lcenters.push_back(ptCenter);
	}

	return 0;
	//return CAMatchNCC_H::exe(pmimage);
}
