partrrtzm.cpp

来自「在人脸检测的基础之上,对嘴部的运动表情进行分析,进行语音模拟.」· C++ 代码 · 共 173 行

CPP
173
字号
// ParTrRtZm.cpp: implementation of the ParTrRtZm class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SeqProcess.h"
#include "ParTrRtZm.h"
#include "Point2d.h"
#include "Tri2d.h"
#include "Quad2d.h"
#include "Poly2dGroup.h"
#include "Vector.h"
#include "MatrixNew.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//----------------------------------------------------------------------------
// a1 = (Zoom factor) * (cosine of the rotation angle).
// a2 = (Zoom factor) * (sine of the rotation angle).
// (a3, a4) = Displacement in x and y directions.
//----------------------------------------------------------------------------
ParTrRtZm::ParTrRtZm()
{
	a1 = 1; a2 = 0;
	a3 = 0; a4 = 0;
}

ParTrRtZm::ParTrRtZm( double i1, double i2, double i3, double i4)
{
	a1 = i1; a2 = i2;
	a3 = i3; a4 = i4;
}

ParTrRtZm::ParTrRtZm( const ParTrRtZm& from)
{
	a1 = from.a1; a2 = from.a2;
	a3 = from.a3; a4 = from.a4;
}

ParTrRtZm& ParTrRtZm::operator=( const ParTrRtZm& from)
{
	if( this == &from) 
	{
		// Attempting to copy onto itself. Do nothing.
	}
	else 
	{
		a1 = from.a1; a2 = from.a2;
		a3 = from.a3; a4 = from.a4;
	}
	return *this;
}

Point2d ParTrRtZm::tform( Point2d& tmp)
{
	double x, y;
	x = tmp.X() * a1 - tmp.Y() * a2 + a3;
	y = tmp.X() * a2 + tmp.Y() * a1 + a4;
	return Point2d(x, y);
}

int ParTrRtZm::getPars( Poly2d poly1, Poly2d poly2)
{
	int size = poly1.getSize();
	
	// Polygons must have the same size
	ASSERT( poly2.getSize() == size);
	
	// Polygons must have at least two points
	ASSERT( size >= 2);
	
	// Both polygons must be proper
	ASSERT( poly1.isProper() && poly2.isProper());
	
	// Both polygons must be convex
	ASSERT( poly1.isConvex() && poly2.isConvex());
	
	if( size == 2)
	{
		//
		//        R     ->     T
		//       /            '
		//      /           '
		//     /          '
		//    /         '
		//   P  ->  S '
		//
		double pxrx = poly1[0].X() - poly1[1].X();
		double pyry = poly1[0].Y() - poly1[1].Y();
		double deltaSqr = pxrx*pxrx + pyry*pyry;
		
		a1 = ( pxrx*(poly2[0].X() - poly2[1].X())
			+ pyry*(poly2[0].Y() - poly2[1].Y()) ) / deltaSqr;
		a2 = ( pxrx*(poly2[0].Y() - poly2[1].Y())
			- pyry*(poly2[0].X() - poly2[1].X()) ) / deltaSqr;
		a3 = poly2[0].X() - ( poly1[0].X()*a1 - poly1[0].Y()*a2 );
		a4 = poly2[0].Y() - ( poly1[0].Y()*a1 + poly1[0].X()*a2 );
	}
	else
	{
		//form the matrix first.
		int i,j,index;
		int limit;
		limit=2*size;
		Matrix system(limit , 4);
		Vector sol(limit);
		for ( i=0; i<limit; i++)
		{
			index=i/2;
			if( i%2) 
			{
				sol[i]=poly2[index].X();
				for ( j=0; j<4; j++)
				{ 
					switch (j%4) {
					case 0: 
						system(i,j)=poly1[index].Y();
						break;
					case 1:
						system(i,j)=poly1[index].X();
						break;
					case 3: 
						system(i,j)=1.0;
						break;
					default:
						system(i,j)=0.0;
						break;
					}
				}
			} 
			else
			{
				sol[i]=poly2[index].Y();
				for ( j=0; j<4; j++)
				{ 
					switch (j%6) {
					case 0: 
						system(i,j)=poly1[index].X();
						break;
					case 1:
						system(i,j)=-poly1[index].Y();
						break;
					case 2: 
						system(i,j)=1.0;
						break;
					default:
						system(i,j)=0.0;
						break;
					}  //switch
				}    //for j
			}  //else
		}        //for i
		system.leastSquares(sol);
		a1 = sol[0]; a2 = sol[1];
		a3 = sol[2]; a4 = sol[3]; 
	}
	return 1;
}

mapMethod ParTrRtZm::type()
{
	return TRRTZM;
}

int ParTrRtZm::sizePar()
{
	return 4;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?