www.pudn.com > MyImageDB(imageobject).rar > MyMath.cpp


// MyMath.cpp: implementation of the MyMath class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "MyImageDB.h" 
#include "MyMath.h" 
#include  
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
MyMath::MyMath() 
{ 
	srand( (unsigned)time( NULL ) ); 
} 
 
MyMath::~MyMath() 
{ 
 
} 
 
DOUBLE MyMath::LineDistance(My3DPoint inPoint1, My3DPoint inPoint2) 
{ 
	DOUBLE tempoffx = inPoint1.x - inPoint2.x; 
	DOUBLE tempoffy = inPoint1.y - inPoint2.y; 
	DOUBLE tempoffh = inPoint1.h - inPoint2.h; 
 
	return sqrt(tempoffx*tempoffx + tempoffy*tempoffy +  
		tempoffh*tempoffh); 
} 
 
DOUBLE MyMath::TriangleArea(My3DPoint inPoint1,  
	My3DPoint inPoint2, My3DPoint inPoint3) 
{ 
	//计算边长; 
	DOUBLE a = LineDistance(inPoint1, inPoint2); 
	DOUBLE b = LineDistance(inPoint2, inPoint3); 
	DOUBLE c = LineDistance(inPoint3, inPoint1); 
 
	//计算周长的1/2; 
	DOUBLE s = (a+b+c) / 2; 
 
	//计算面积; 
	return sqrt( s * (s-a) * (s-b) * (s-c) ); 
} 
 
DOUBLE MyMath::TetrahedronVol(My3DPoint inPoint1, 
		My3DPoint inPoint2, My3DPoint inPoint3, 
		My3DPoint inPoint4) 
{		 
	//四面体体积; 
	DOUBLE xa,ya,ha,xb,yb,hb,xc,yc,hc,xd,yd,hd;//四个点的三维坐标; 
	xa = inPoint1.x; 
	ya = inPoint1.y; 
	ha = inPoint1.h; 
	xb = inPoint2.x; 
	yb = inPoint2.y; 
	hb = inPoint2.h; 
	xc = inPoint3.x; 
	yc = inPoint3.y; 
	hc = inPoint3.h; 
	xd = inPoint4.x; 
	yd = inPoint4.y; 
	hd = inPoint4.h; 
 
	//求消减后的矩阵; 
	DOUBLE m11 = xb - xa; 
	DOUBLE m12 = yb - ya; 
	DOUBLE m13 = hb - ha; 
	DOUBLE m21 = xc - xa; 
	DOUBLE m22 = yc - ya; 
	DOUBLE m23 = hc - ha; 
	DOUBLE m31 = xd - xa; 
	DOUBLE m32 = yd - ya; 
	DOUBLE m33 = hd - ha; 
 
	//求行列式; 
	DOUBLE determinant = fabs ( m11 * (m22*m33 - m32*m23) - 
		m21 * (m12*m33 - m32*m13) + 
		m31 * (m12*m23 - m22*m13) ); 
 
	//返回体积; 
    return determinant / 6;	 
} 
 
DOUBLE MyMath::PointLineDistance(My3DPoint point, My3DPoint linePt1, My3DPoint linePt2) 
//点线距离; 
{ 
	DOUBLE area = TriangleArea(point, linePt1, linePt2); 
	DOUBLE edgelength = LineDistance(linePt1, linePt2); 
	return area/edgelength * 2; 
} 
 
DOUBLE MyMath::PrismVol(My3DPoint upPoint1, My3DPoint upPoint2,  
		My3DPoint upPoint3,	My3DPoint downPoint1,  
		My3DPoint downPoint2, My3DPoint downPoint3) 
//三棱柱体积; 
{ 
	DOUBLE tetra1, tetra2, tetra3;//分割后的三个四面体体积; 
	tetra1 = TetrahedronVol(downPoint1, downPoint2,  
		                    downPoint3, upPoint1); 
	tetra2 = TetrahedronVol(upPoint1, upPoint2,  
		                    upPoint3, downPoint2); 
	tetra3 = TetrahedronVol(upPoint1, upPoint3,  
		                    downPoint2, downPoint3); 
	return tetra1 + tetra2 + tetra3; 
} 
 
BOOL  MyMath::GetBinAt(LONG inLong, INT inPos) 
//输入一个整数,返回该数在inPos处的二进制值,inPos位置从右算起; 
//由于此函数是为了用于证据理论的组合公式,故其性能不宜被普通的转换二进 
//制函数调用,如需进行该种转换需要另写转换函数; 
{ 
	LONG backin1 = inLong; 
	LONG backin2 = backin1; 
 
	for (int i=0; i<=inPos; i++) 
	{ 
		backin1 = backin2; 
		backin2 = backin1 / 2;	 
	} 
 
	if ( backin2*2 != backin1 ) 
	{ 
		return 1; 
	}else 
	{ 
		return 0; 
	} 
} 
 
DOUBLE MyMath::Gaussian(DOUBLE inputx, DOUBLE mean, DOUBLE diff) 
//计算高斯函数值; 
{ 
	return exp( -1.0 * (inputx - mean) * (inputx - mean) /  
		( 2 * diff * diff) ); 
} 
 
DOUBLE MyMath::GetARandom() 
//得到一个介于0-1之间的双精度随机数; 
{ 
	return  ( (DOUBLE)rand() / (DOUBLE)RAND_MAX ); 
} 
 
void MyMath::RevertCopyMatrix(BYTE* inMatrix, INT width, BYTE* outMatrix, INT sita, INT mode) 
//sita 0-4对应0,45,90和135,mode 0对应上-下,左-右 
//mode 1对应下-上,右-左;输入必须为奇数阶方阵; 
{ 
	INT pos = -1; 
	INT replacepos = -1; 
	if (sita==0) 
	{ 
		if (mode==0) 
		{ 
			//0度上-下 
			INT tempi = INT ( width/2 ); 
			for (INT y=0; ytempi) 
					{						 
						replacepos = (width-y)*width + x; 
						outMatrix[pos] = inMatrix[replacepos]; 
					}else 
					{ 
						outMatrix[pos] = inMatrix[pos]; 
					} 
				} 
			} 
		}else 
		{ 
			//0度下-上 
			INT tempi = INT ( width/2 ); 
			for (INT y=0; y(width-x)) 
					{						 
						replacepos = (y-tempoff)*width + (x-tempoff); 
						outMatrix[pos] = inMatrix[replacepos]; 
					}else 
					{ 
						outMatrix[pos] = inMatrix[pos]; 
					} 
				} 
			} 
		}else 
		{ 
			//45度右-左 
			for (INT y=0; ytempi) 
					{						 
						replacepos = y*width + (width-x); 
						outMatrix[pos] = inMatrix[replacepos]; 
					}else 
					{ 
						outMatrix[pos] = inMatrix[pos]; 
					} 
				} 
			} 
		}else 
		{ 
			//90度右-左 
			INT tempi = INT ( width/2 ); 
			for (INT y=0; yx) 
					{ 
						//XY翻转; 
						replacepos = x*width + y; 
						outMatrix[pos] = inMatrix[replacepos]; 
					}else 
					{ 
						outMatrix[pos] = inMatrix[pos]; 
					} 
				} 
			} 
		}else 
		{ 
			//135度右-左 
			for (INT y=0; yinval2) 
	{ 
		if (inval1>inval3) 
		{ 
			return 0; 
		}else 
		{ 
			return 2; 
		} 
	}else 
	{ 
		if (inval2>inval3) 
		{ 
			return 1; 
		}else 
		{ 
			return 2; 
		} 
	} 
} 
 
DOUBLE MyMath::ReturnMinInThree(DOUBLE inval1, DOUBLE inval2, DOUBLE inval3) 
//找三个数中最小者,并将其返回; 
{ 
	if (inval1inval2) 
	{ 
		if (inval1>inval3) 
		{ 
			return inval1; 
		}else 
		{ 
			return inval3; 
		} 
	}else 
	{ 
		if (inval2>inval3) 
		{ 
			return inval2; 
		}else 
		{ 
			return inval3; 
		} 
	} 
} 
 
void MyMath::ClacuMean(DOUBLE* inData, LONG inNumber, FLOAT& outMean, FLOAT& outSerr) 
//计算输入数组的均值与方差; 
{ 
	DOUBLE total = 0; 
	//计算均值; 
	for (INT i=0; i0) 
		{ 
			total += inData[i]; 
			count++; 
		} 
		 
	} 
 
	outMean = (FLOAT) ( total / (FLOAT)count ); 
 
	//计算方差; 
	total = 0; 
	for (i=0; i0) 
		{ 
			total += (inData[i] - outMean)  
				* (inData[i] - outMean); 
		}		 
	} 
 
	total = (FLOAT) ( total / (FLOAT)count );//误差平方和的期望; 
	outSerr = (FLOAT) ( sqrt(total) );//标准差; 
} 
 
BOOL MyMath::isOdd(INT inInt) 
//是否奇数; 
{ 
	INT tempi = (INT) (inInt/2); 
	if (tempi*2 == inInt) 
	{ 
		//偶数; 
		return FALSE; 
	} 
 
	//奇数; 
	return TRUE; 
} 
 
/******************************************************************************* 
* 函数名称:GetMaxValue() 
* 
* 参数:    BYTE* pWinValue    -- 指向数组的指针 
*           int nWinLength     -- 数组的长度 
* 
* 返回值:  BYTE               -- 数组的最大值 
* 
* 说明:    该函数用于得到一个数组的最大值,由函数 LocalThresholding() 调用。 
********************************************************************************/ 
 
BYTE MyMath::GetMaxValue(BYTE* pWinValue,int nWinLength) 
{ 
	int i;    // 循环变量 
	BYTE Tmp = 0; // 临时变量 
    for(i=0;i Tmp) 
		{ 
			Tmp = pWinValue[i]; 
		} 
	} 
	return Tmp; 
} 
 
/******************************************************************************* 
* 函数名称:GetMinValue() 
* 
* 参数:    BYTE* pWinValue    -- 指向数组的指针 
*           int nWinLength     -- 数组的长度 
* 
* 返回值:  BYTE               -- 数组的最小值 
* 
* 说明:    该函数用于得到一个数组的最小值,由函数 LocalThresholding() 调用。 
********************************************************************************/ 
 
BYTE MyMath::GetMinValue(BYTE* pWinValue,int nWinLength) 
{ 
	int i;    // 循环变量 
	BYTE Tmp = 255; // 临时变量 
    for(i=0;i= 0 ) 
	{ 
		left = currentpos - 1; 
	}else 
	{ 
		left = -1; 
	} 
 
	if ( posx+1 < width) 
	{ 
		right = currentpos + 1; 
	}else 
	{ 
		right = -1; 
	} 
 
	if ( posy-1 >= 0 ) 
	{ 
		up = currentpos - width; 
	}else 
	{ 
		up = -1; 
	} 
 
	if ( posy+1 < height ) 
	{ 
		down = currentpos + width; 
	}else 
	{ 
		down = -1; 
	} 
}