You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

327 lines
11 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

//////////////////////////////////////////////////////////////////////////////
//文件 BaseFunction.h
//主要功能:
// 基础类库
//程序编写: 2005-12-07
/////////////////////////////////////////////////////////////////////////////
#pragma once
#include "math.h"
#include "afxcmn.h"
#include "Point2D.h"
#include "afxtempl.h"
#ifndef PI
#define PI 3.1415926535897932384626433832795
#endif
#ifndef RHO
#define RHO 57.295779513082320876798154814114 //(180.0/PI)
#endif
#ifndef CODE_GB2321
#define CODE_GB2321 0x00000000
#define CODE_UTF8 0x00000001
#endif
//#ifndef MAX_LINE
// #define MAX_LINE 2048
//#endif
class AFX_EXT_CLASS CBaseFunction
{
public:
static int _compare_double(const void *d1,const void *d2);
//static char m_tempLine[MAX_LINE];
//static char m_tempText[MAX_LINE];
//char m_tempLine[MAX_LINE];
//char m_tempText[MAX_LINE];
public:
CBaseFunction(void);
virtual ~CBaseFunction(void);
//双精度数转换为字符串
void FloatToString(CString& result, double m_float);
void FloatToString(CString& result, double m_float, int n);
void FloatToString(char* result, double m_float);
void FloatToString(char *result, double m_float, int n);
void FloatToString(double m_float); //将结果输出到m_tempLine中可以用GetTempString()获得
void FloatToString(double m_float, int n);
//输出数组到字符串中,用户可以自定义间隔符号(缺省是‘,’)
void PrintXYZ(char* string, double *v, int num, int digits, char split=',');
void PrintXYZ(CString& result, double *v, int num, int digits, char split=',');
void PrintXYZ(char* result, double *v, int num, char split=',');
void PrintXYZ(CString& result, double *v, int num, char split=',');
//二分法搜索及插值
int BinarySearch(double key, int num, double *x);
double LineValue(double x0, double *x, double *y);// 根据X值插值Y
double LineValue(double x0, double x1, double x2, double y1, double y2);
double LineValue(double m,double n,double z1,double z2,double z3,double z4);
//坐标旋转
void Rotate(double &x0, double &y0, double radians);
void Rotate(double &x, double &y, double cosa, double sina);
void Rotate(double &x0,double &y0,double &z0,double alfa,double beta);
//算术运算参数mode=0代表+ 1为减2为乘3为除
void Arithmetic(double& val, double factor, int mode);
void SortCoordinate(double* x, double* y, int n); //根据坐标按照最小距离排序, 曲线拟合
double TotalDistance(double *a, double *b, int n); //获得总的距离和
int GetMinValueIndex(double* a, int n); //获得数组中最小值的索引号
double Distance2(double x1,double y1,double x2,double y2);
double Distance(double x1,double y1,double x2,double y2);
double Distance3D(double x1, double y1, double z1, double x2,double y2,double z2);
double PointLineDistance(double x0,double y0,double k,double b); //点到直线的距离
// 点到线段的最短距离 --whl
double PointLineDistance(double x0, double y0, double x1, double y1, double x2, double y2);
///////////////////////////////////////////////////////////////////////
//
// 1 上侧
// p1--------------------p2
// 2 下侧
//
//
// p2
// |
// |
// 1 左侧 | 2 右侧
// |
// |
// p1
// 判断点在直线的哪侧 1-- 左侧 0--线上 2---右侧
/////////////////////////////////////////////////////////////////////////
int PointInLine(double x0, double y0, double x1, double y1, double x2, double y2);
//空间点到空间线段的最短距离 --Wangcw
double PointSegmentDistance3D(double x0, double y0, double z0, double x1, double y1,double z1, double x2, double y2, double z2);
double GetAngle(double x1,double y1,double x2,double y2,double x3,double y3); //获得两直线之间的夹角
double GetAngle(double x1, double y1, double x2, double y2); //获得直线与正X轴之间的夹角
double GetAngle(double x, double y);
double GetAzimuth(double x1, double y1, double x2, double y2); //计算方位角
double ToDegree(double dms); //将度分秒转换为度
double ToDMS(double degree); //将度转换为度分秒
void GetKB(double x1, double y1, double x2, double y2, double &k, double &b); //获得直线方程参数
BOOL GetKB(int num, double *x, double *y, double &k, double &b); //最小二乘法直线拟合, num>2, 拟合直线方程(Y=kX+b)
int Offset(double k, double z, double &delt, double &h, BOOL bOffset); //曲线上的点偏移或反偏移k为斜率
int Offset(double &dx, double &dy, double &z, BOOL bOffset); //曲面上的点偏移或反偏移dx、dx为偏导
//bOffset==TRUE时调用Offset2D为FALSE时调用UnOffset2D
int Offset2D(double k, double z, double &delt, double &h); //曲面上的点偏移
int UnOffset2D(double k, double h, double &delt, double &z); //曲面上的点反偏移
int Gauss(int n,double *a,double *c);
double GetAreaTrue(int num, double* x, double* y); //求闭合区域面积,面积可能是正值也可能是负值
double GetArea(int num,double *x,double *y); //求闭合区域面积(绝对值),GetArea()=fabs(GetAreaTrue())
double GetFirstValue(double x,double step);
bool IsZero(const double& val); //判断val是否是零
bool IsZero(const double& val, double deps); //判断val是否是零
bool IsEqual(const double& x, const double& y, double deps = 1e-8); //判断两个值是否相等
//获得等边多边形的顶点坐标
// centerPoint 中心点坐标
// endPoint 结束点坐标,该点为多边形的起点
// polyNum 等边多边形的边数
void GetEquilateralPolyPoints(CList<dfPoint, dfPoint>& pl, dfPoint &centerPoint, dfPoint &endPoint, int polyNum);
//根据角度获得圆上的点的坐标
dfPoint GetPointOnCircle(dfPoint& centerPoint, double r, double angle);
//根据角度获得椭圆上的点坐标
bool GetPointOnEllipse(double &x, double& y, const CSize8& szRadius, double angle);
bool GetPointOnEllipse(double &x, double& y, const CPoint2D& centerPoint, const CSize8& szRadius, double angle);
//x1,y1,x2,y2,x3,y3为三角形的三个顶点坐标x,y,r中返回的是三角形外接圆的圆心坐标及半径
BOOL GetCircumcenter(double &x, double &y, double &r,
double x1, double y1,
double x2, double y2,
double x3, double y3);
//五点三次平滑 --- wangcw
bool Smooth53(double* val, int n, int stimes = 100);
int GetArgument(CString cmd, CStringList &list);
int ScanfString(CStringArray &m_type, CString str, char split);
int ScanfString1(CStringArray &m_type, CString cmd, char split); //获得split间隔的串,引号内部的split不起作用
int ScanfString1(CStringArray &m_type, char* cmd, int n, char split); //获得split间隔的串,引号内部的split不起作用
int ScanfString(CStringArray &m_type, char* cmd); //获得以关键字空格、逗号、TAB键间隔的串,引号内部的关键字不起作用
void ReverseString(char *string, int n);
//ASCII码文件的读写
int ReadString(CFile& fr, CString& str);
int GetString(CFile& fr, CString& str);
BOOL IsLineEnd_GetString(int result); //判断GetString返回的值是否表示一行的结束
int ReadString(CFile & fr, char* str); //读取一行字符串
int GetString(CFile& fr, char* str); //读取一段字符串(以空格或回车为终止符)
int ReadString(CFile & fr); //读取一行字符串到临时数组中
int GetString(CFile& fr); //读取一段字符串(以空格或回车为终止符)到临时数组中
char* GetTempString(void); //获得临时数组
int GetTempStringLength(void); //获得临时数组长度
void ZeroTempString(void); //清空临时数组
int ReplaceChar(char* pString, char old_char, char new_char);
int FindString(char* pString, char* pSearchString);
int ReverseFind(const char* pString, char cFind); //反向查找指定的字符
void SerializeString(CArchive& ar, CString& str, const short& ver);
void WriteLine(CFile &fw, CString str); //写一行数据(包括回车),是WriteString与WriteReturn的组合
void WriteString(CFile &fw, CString str); //写一串数据
void WriteString(CFile &fw, double value, int n=6); //写一个双精度数据
void WriteString(CFile &fw, long value); //写一个长整形数据
void WriteReturn(CFile& fw); //写一个回车
void WriteXY(CFile& fw, double x, double y, char cSplit=','); //写X,Y数据
void WriteXY(CFile& fw, double& x, double& y, double& z, char cSplit);
BOOL WriteFile(LPCTSTR lpSaveFileName, void* pBuffer, DWORD nBufferSize); //快速保存指定内容
int ScanDouble(char *line,double *x,int n,int m);
int ScanDouble1(char *line,double *x,int n,int m);
long FloatToLong(double value);
void SortDouble(int num,double *x);
void SetDecimalDigits(int digits);
int GetDecimalDigits(void);
int IsDigit(CString &line);
int IsDigit(char* line);
double GetCanonicalStep(double step); //获得规范化的步长如参数为12返回10、0.13=0.1
double ContourStep(double m_step);
int GetCycValue(int i,int T);
void ExchangeXY(double& x, double& y); //x,y交换位置
void Swap(double& x, double& y); //x,y交换位置
void Swap(float& x, float& y); //x,y交换位置
//为了写元素类型(int 或 short)型
void WriteElementType(CArchive& ar, int nType, const short& ver);
void ReadElementType(CArchive& ar, int &nType, const short& ver);
HGLOBAL WINAPI CopyHandle(HGLOBAL h);
//Progress
void SetProgress(CProgressCtrl* pProgress);
CProgressCtrl* GetProgress(void);
void SetProgressPos(int nPos);
//执行指定命令
HANDLE ExecuteCommand(LPCTSTR lpFileCommand, LPCTSTR lpFileParameters, LPCTSTR lpFileDirectory, int nWinShow=SW_SHOW);
//曲线类型
DWORD GetCurveType(CString str);
CString GetCurveTypeString(DWORD type);
CString GetSplitPath(const CString sFullPathName, CString context=_T("ext"));
//对散点数据获得其外轮廓曲线返回为轮廓点个数坐标保存在参数x、y中
int GetOutline(int num, double *x, double *y);
//设置/获取编码
void SetCodeType(DWORD nType);
DWORD GetCodeType();
COLORREF HexToColorRef(CString strHex);
RGBQUAD HexToRgbQuad(CString strHex);
CString RgbQuadToHex(RGBQUAD clr);
CString ColorRefToHex(COLORREF clr);
protected:
CProgressCtrl* m_pProgress;
int __DecimalDigits;//缺省的保留小数位数 void FloatToString(CString& result, double m_float);
DWORD m_nCodeType;
};
extern thread_local CBaseFunction bf;
AFX_INLINE int CBaseFunction::GetDecimalDigits(void)
{
return __DecimalDigits;
}
AFX_INLINE void CBaseFunction::SetDecimalDigits(int digits)
{
__DecimalDigits=digits;
}
AFX_INLINE int CBaseFunction::IsDigit(CString &line)
{
if(line.GetLength()==0)
return 0;
if(line[0]<='9'&&line[0]>='0')
return 1;
if((line[0]=='-'||line[0]=='.'||line[0]=='+') && line.GetLength()>1)
return 1;
return 0;
}
AFX_INLINE int CBaseFunction::IsDigit(char* line)
{
if(line[0]<='9'&&line[0]>='0')
return 1;
if((line[0]=='-'||line[0]=='.'||line[0]=='+') && strlen(line)>1)
return 1;
return 0;
}
AFX_INLINE double CBaseFunction::Distance2(double x1,double y1,double x2,double y2)
{
x1-=x2;
y1-=y2;
return x1*x1+y1*y1;
}
AFX_INLINE double CBaseFunction::Distance3D(double x1, double y1, double z1, double x2,double y2,double z2)
{
x1 -= x2;
y1 -= y2;
z1 -= z2;
return sqrt(x1*x1 + y1*y1 + z1*z1);
}
AFX_INLINE double CBaseFunction::Distance(double x1,double y1,double x2,double y2)
{
return sqrt(Distance2(x1,y1,x2,y2));
}
AFX_INLINE long CBaseFunction::FloatToLong(double value)
{
if(value<0) return (long)(value-0.5);
return (long)(value+0.5);
}
AFX_INLINE CProgressCtrl* CBaseFunction::GetProgress(void)
{
return m_pProgress;
}
AFX_INLINE void CBaseFunction::SetProgressPos(int nPos)
{
// if(m_pProgress) m_pProgress->SetPos(nPos);
}
extern "C" AFX_EXT_API CBaseFunction* WINAPI AfxGetBaseFunction();
extern "C" AFX_EXT_API BOOL * WINAPI AfxGetNo_Trim_Zero();
extern "C" AFX_EXT_API void WINAPI SetVert(TRIVERTEX &v, int x, int y, COLORREF cr);
//extern "C" TRIVERTEX AFX_EXT_DATA vert[5];
//extern "C" GRADIENT_TRIANGLE AFX_EXT_DATA gTRi[4];
//extern "C" double AFX_EXT_DATA __z[4];
extern "C"
{
AFX_EXT_API TRIVERTEX* WINAPI AfxGetvert();
AFX_EXT_API GRADIENT_TRIANGLE* WINAPI AfxGetgTRi();
AFX_EXT_API double* WINAPI AfxGet__z();
};