目录

0、公用头文件(Smath)

头文件Smath.h

//Smath.h

#pragma once
#ifndef UNIVERSE_SPACE_SMATH
#define UNIVERSE_SPACE_SMATH
#define SPi  3.141592653578
#define SPRECI  0.000001
#define SMAX(val1, val2) (val1 > val2) ? val1 : val2
#define SMIN(val1, val2) (val1 < val2) ? val1 : val2
#define SEqual(val1, val2, preci) ((1 + preci >= (val1 / val2) && (val1 / val2) >= 1 - preci)) ? true : false
#define SEqual(val1, val2, preci) ((1 + preci >= (val1 / val2) && (val1 / val2) >= 1 - preci)) ? true : false
#define SEqualDouble(val1, val2) Equal(val1, val2, SPRECI)
#include <cmath>
#include <vector>
struct SEquation {
	double a;
	double b;
	double c;
	double d;
	SEquation(double _a = 0, double _b = 0, double _c = 0, double _d = 0) { a = _a; b = _b; c = _c; d = _d; }
	// 0 = a*x + b*y + c*z + d
};
struct SRGB {
	int R;
	int G;
	int B;
	float A;
	SRGB(int r = 255, int g = 255, int b = 255, float a = 1.0) { R = r; G = g; B = b; A = a; }
};
typedef std::vector<SEquation> SEquationS;
#endif // !UNIVERSE_SPACE_SMATH

1、向量(SVector)

为了用法更加接近于数学并且更加方便,重载一些符号

//SVector.h

#pragma once
#ifndef UNIVERSE_SPACE_SVECTOR
#define UNIVERSE_SPACE_SVECTOR
#include "Smath.h"
#include "SPoint.h"
#include "SLine.h"
class SVector{
protected:
	double _vars[3] = { 0,0,0 };
public:
	SVector(double var1 = 0,double var2 = 0, double var3 = 0) { _vars[0] = var1; _vars[1] = var2; _vars[2] = var3; };
	SVector(SPoint star, SPoint end) { _vars[0] = end.x - star.x; _vars[1] = end.y - star.y; _vars[2] = end.z - star.z; };

	SPoint ToPoint(){ return SPoint(_vars[0], _vars[1], _vars[2]); }

	void fill(double var) { _vars[0] = _vars[1] = _vars[2] = var; };
	void copy(SVector vect){ for (int i = 0; i < 3; i++)_vars[i] = vect[i]; };
	double GetModulus() { return sqrt((*this) * (*this)); };
	double& operator[](const int n) { return _vars[n]; };
	bool operator == (const SVector vect) { return (((*this) - vect).GetModulus() <= SPRECI); };
	bool operator != (const SVector vect) { return !(*this == vect); }
	SVector operator + (const SVector vect) { SVector res; for (int i = 0; i < 3; i++) res._vars[i] = +_vars[i]; return res; };
	SVector operator - (const SVector vect) { SVector res; for (int i = 0; i < 3; i++) res._vars[i] = -_vars[i]; return res; };
	SVector operator = (const SVector vect) { for (int i = 0; i < 3; i++) _vars[i] = vect._vars[i]; return vect; };

	friend SVector operator + (const SVector& A, const SVector& B) { SVector C; for (int i = 0; i < 3; i++) C[i] = A._vars[i] + B._vars[i];  return C; };
	friend SVector operator - (const SVector& A, const SVector& B) { SVector C; for (int i = 0; i < 3; i++) C[i] = A._vars[i] - B._vars[i];  return C; };
	friend double operator * (const SVector& A, const SVector& B) { double c = 0; for (int i = 0; i < 3; i++) c = c + (A._vars[i] * B._vars[i]); return c; };
	friend double operator / (const SVector& A, const SVector& B) { double c = 0; for (int i = 0; i < 3; i++) c = c + (A._vars[i] / B._vars[i]); return c; };
	static SVector Mul(const SVector& A, const SVector& B) //叉乘
	{ return SVector(A._vars[1]*B._vars[2] - B._vars[1] * A._vars[2], A._vars[2] * B._vars[0] - B._vars[2] * A._vars[0], A._vars[0] * B._vars[1] - B._vars[0] * A._vars[1]); };
	static  double Dot(const SVector& A, const SVector& B) //点乘
	{ return A._vars[0] * B._vars[0] + A._vars[1] * B._vars[1] + A._vars[2] * B._vars[2]; };

	friend SVector operator + (const double& a, const SVector& B) { SVector C; for (int i = 0; i < 3; i++) C[i] = a + B._vars[i];  return C; };
	friend SVector operator - (const double& a, const SVector& B) { SVector C; for (int i = 0; i < 3; i++) C[i] = a - B._vars[i];  return C; };
	friend SVector operator * (const double& a, const SVector& B) { SVector C; for (int i = 0; i < 3; i++) C[i] = a * B._vars[i];  return C; };
	friend SVector operator / (const double& a, const SVector& B) { SVector C; for (int i = 0; i < 3; i++) C[i] = a / B._vars[i];  return C; };
	friend SVector operator + (const SVector& A, const double& b) { SVector C; for (int i = 0; i < 3; i++) C[i] = A._vars[i] + b;  return C; };
	friend SVector operator - (const SVector& A, const double& b) { SVector C; for (int i = 0; i < 3; i++) C[i] = A._vars[i] - b;  return C; };
	friend SVector operator * (const SVector& A, const double& b) { SVector C; for (int i = 0; i < 3; i++) C[i] = A._vars[i] * b;  return C; };
	friend SVector operator / (const SVector& A, const double& b) { SVector C; for (int i = 0; i < 3; i++) C[i] = A._vars[i] / b;  return C; };
	SVector GetUnitVec() { return *this / GetModulus(); };		//单位化
	SVector GetTraVec() { SVector Tra; for (int i = 0; i < 3; i++) Tra[i] = _vars[3 - i - 1]; return Tra; };	//转置向量
	static double GetCos(const SVector& A, const SVector& B) { return (A * B) / (sqrt(A * A) * sqrt(B * B)); }
	static double GetSin(const SVector& A, const SVector& B) { return SVector::Mul(A, B).GetModulus() / (sqrt(A * A) * sqrt(B * B)); }
};

typedef std::vector<SVector> SVectors;

#endif // !UNIVERSE_SPACE_SVECTOR

2、矩阵(SMatrix)

头文件SMatrix.h

//SMatrix.h

#pragma once
#ifndef UNIVERSE_SPACE_SMATRIX
#define UNIVERSE_SPACE_SMATRIX
#include "Smath.h"
#include "SVector.h"
class SMatrix
{
public:
	SMatrix(double var = 0);
	
	void copy(SMatrix res, int n, int m);
	void fill(double var,int n, int m);
	bool fillline(int n, SVector vet);
	bool fillcove(int m, SVector vet);

	SMatrix delline(int n);//n下标
	SMatrix delcolu(int m);//m下标
	SMatrix dele(int n, int m);

	double GetDete(int n = 3);							//得到行列式的值

	SMatrix GetInvMat(int n = 3);							//逆矩阵	
	SMatrix GetTraMat(int n = 3);							//转置矩阵
	SMatrix GetAdjMat(int n = 3);							//伴随矩阵

	static SMatrix GetMatE();					//单位矩阵
	static SMatrix GetMove(double varX, double varY, double varZ);		//移变矩阵
	static SMatrix GetFlexible(double varX, double varY, double varZ);	//抻变矩阵
	//三个旋变矩阵
	static SMatrix GetRotateX(double var);
	static SMatrix GetRotateY(double var);
	static SMatrix GetRotateZ(double var);
	//三个扭变矩阵
	static SMatrix GetTwistX(double var);
	static SMatrix GetTwistY(double var);
	static SMatrix GetTwistZ(double var);

	double* operator[](const int n) { return _vars[n]; };
	SMatrix operator = (const SMatrix matrix);

	friend SMatrix operator + (const SMatrix& A, const SMatrix& B);
	friend SMatrix operator - (const SMatrix& A, const SMatrix& B);
	friend SMatrix operator * (const SMatrix& A, const SMatrix& B);
	friend SMatrix operator / (const SMatrix& A, const SMatrix& B);

	friend SMatrix operator + (const double& a, const SMatrix& B);
	friend SMatrix operator - (const double& a, const SMatrix& B);
	friend SMatrix operator * (const double& a, const SMatrix& B);
	friend SMatrix operator / (const double& a, const SMatrix& B);

	friend SMatrix operator + (const SMatrix& A, const double& b);
	friend SMatrix operator - (const SMatrix& A, const double& b);
	friend SMatrix operator * (const SMatrix& A, const double& b);
	friend SMatrix operator / (const SMatrix& A, const double& b);
	friend SMatrix operator ^ (const SMatrix& A, int n);

	friend SVector operator * (SVector a, const SMatrix& B);
	friend SVector operator * (const SMatrix& A, SVector b);
protected:
	double _vars[3][3];
};
#endif // !UNIVERSE_SPACE_SMATRIX

源文件SMatrix.cpp

//SMatrix.cpp

#include "cmath"
#include "SMatrix.h"

void SMatrix::copy(SMatrix res, int n, int m)
{
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			_vars[i][j] = res[i][j];
}

void SMatrix::fill(double var, int n, int m)
{
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			_vars[i][j] = var;
}

bool SMatrix::fillline(int n, SVector vet)
{
	if (n < 0 || n > 2)
		return false;
	for (int j = 0; j < 3; j++)
		_vars[n][j] = vet[j];
	return true;
}

bool SMatrix::fillcove(int m, SVector vet)
{
	if (m < 0 || m > 2)
		return false;
	for (int i = 0; i < 3; i++)
		_vars[i][m] = vet[i];
	return true;
}

SMatrix SMatrix::delline(int n)
{
	SMatrix res = *this;
	if (n < 0 || n > 2)
		return res;
	else {
		for (int i = n; i < 2; i++) {
			res._vars[i][0] = _vars[i + 1][0];
			res._vars[i][1] = _vars[i + 1][1];
			res._vars[i][2] = _vars[i + 1][2];
		}
		res._vars[2][0] = 0;
		res._vars[2][1] = 0;
		res._vars[2][2] = 0;
	}return res;
}
SMatrix SMatrix::delcolu(int m)
{
	SMatrix res = *this;
	if (m < 0 || m > 2)
		return res;
	else {
		for (int i = m; i < 2; i++) {
			res._vars[0][i] = _vars[0][i + 1];
			res._vars[1][i] = _vars[1][i + 1];
			res._vars[2][i] = _vars[2][i + 1];
		}
		res._vars[0][2] = 0;
		res._vars[1][2] = 0;
		res._vars[2][2] = 0;
	}return res;
}

SMatrix SMatrix::dele(int n, int m)
{
	return delline(n).delcolu(m);
}

SMatrix::SMatrix(double var)
{
	fill(var, 3, 3);
}



double SMatrix::GetDete(int n)
{
	if (n == 1)
		return _vars[0][0];
	int sig = -1;
	double mea = 0;
	SMatrix copymat;
	copymat.copy(*this, n, n);
	for (int i = 0; i < n; i++) {
		sig = sig * -1;
		mea += (_vars[i][0] * copymat.dele(i, 0).GetDete(n - 1) * sig);
	}return mea;
}

SMatrix SMatrix::GetMatE()
{
	SMatrix E;
	for (int i = 0; i < 3; i++)
		E._vars[i][i] = 1;
	return E;
}

SMatrix SMatrix::GetInvMat(int n)
{
	return GetAdjMat(n) / GetDete(n);
}

SMatrix SMatrix::GetTraMat(int n)
{
	SMatrix T;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			T._vars[j][i] = _vars[i][j];
	return T;
}

SMatrix SMatrix::GetAdjMat(int n)
{
	SMatrix A;
	int sig = -1;
	SMatrix copymat;
	copymat.copy(*this, n, n);
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++) {
			sig = sig * -1;
			A._vars[i][j] = copymat.dele(i, j).GetDete(n - 1) * sig;
		}
	return A;
}

SMatrix SMatrix::GetMove(double varX, double varY, double varZ)
{
	SMatrix res;
	res = res.GetMatE();
	for (int i = 0; i < 3; i++) {
		res[0][i] = varX;
		res[1][i] = varY;
		res[2][i] = varZ;
	}
	return res;
}

SMatrix SMatrix::GetFlexible(double varX, double varY, double varZ)
{
	SMatrix res;
	res = res.GetMatE();
	res[0][0] = varX;
	res[1][1] = varY;
	res[2][2] = varZ;
	return res;
}

SMatrix SMatrix::GetRotateX(double var)
{
	SMatrix res;
	res = res.GetMatE();
	res[1][1] = cos(var);
	res[1][2] = sin(var);
	res[2][1] = -res[1][2];
	res[2][2] = res[1][1];
	return res;
}

SMatrix SMatrix::GetRotateY(double var)
{
	SMatrix res;
	res = res.GetMatE();
	res[0][0] = cos(var);
	res[2][0] = sin(var);
	res[0][2] = -res[2][0];
	res[2][2] = res[0][0];
	return res;
}

SMatrix SMatrix::GetRotateZ(double var)
{
	SMatrix res;
	res = res.GetMatE();
	res[0][0] = cos(var);
	res[0][1] = sin(var);
	res[1][0] = -res[0][1];
	res[1][1] = res[0][0];
	return res;
}

SMatrix SMatrix::operator=(const SMatrix matrix)
{
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			_vars[i][j] = matrix._vars[i][j];
	return matrix;
}

SMatrix operator+(const SMatrix& A, const SMatrix& B)
{
	SMatrix add;
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			add._vars[i][j] = A._vars[i][j] + B._vars[i][j];
	return add;
}

SMatrix operator-(const SMatrix& A, const SMatrix& B)
{
	SMatrix sub;
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			sub._vars[i][j] = A._vars[i][j] - B._vars[i][j];
	return sub;
}

SMatrix operator*(const SMatrix& A, const SMatrix& B)
{
	SMatrix res;
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			for (int k = 0; k < 3; k++)
				res._vars[i][j] += (A._vars[i][k] * B._vars[k][j]);
	return res;
}

SMatrix operator/(const SMatrix& A, const SMatrix& B)
{
	SMatrix C = B;
	return A * C.GetInvMat();
}

SMatrix operator+(const double& a, const SMatrix& B)
{
	SMatrix matrix = B;
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			matrix._vars[i][j] = a + matrix._vars[i][j];
	return matrix;
}

SMatrix operator-(const double& a, const SMatrix& B)
{
	SMatrix matrix = B;
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			matrix._vars[i][j] = a - matrix._vars[i][j];
	return matrix;
}

SMatrix operator*(const double& a, const SMatrix& B)
{
	SMatrix matrix = B;
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			matrix._vars[i][j] = a * matrix._vars[i][j];
	return matrix;
}

SMatrix operator/(const double& a, const SMatrix& B)
{
	SMatrix matrix = B;
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			matrix._vars[i][j] = a / matrix._vars[i][j];
	return matrix;
}

SMatrix operator+(const SMatrix& A, const double& b)
{
	SMatrix matrix = A;
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			matrix._vars[i][j] = matrix._vars[i][j] + b;
	return matrix;
}

SMatrix operator-(const SMatrix& A, const double& b)
{
	SMatrix matrix = A;
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			matrix._vars[i][j] = matrix._vars[i][j] - b;
	return matrix;
}

SMatrix operator*(const SMatrix& A, const double& b)
{
	SMatrix matrix = A;
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			matrix._vars[i][j] = matrix._vars[i][j] * b;
	return matrix;
}

SMatrix operator/(const SMatrix& A, const double& b)
{
	SMatrix matrix = A;
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			matrix._vars[i][j] = matrix._vars[i][j] / b;
	return matrix;
}

SMatrix operator^(const SMatrix& A, int n)
{
	SMatrix res = A;
	SMatrix matrixE = res.GetMatE();
	SMatrix matrixI = res.GetInvMat();
	if (n > 0)
		for (int i = 0; i < n; i++)
			matrixE = matrixE * res;
	if (n < 0) {
		n = -n;
		for (int i = 0; i < n; i++)
			matrixE = matrixE * matrixI;
	}return matrixE;
}

SVector operator*(SVector A, const SMatrix& B)
{
	double mea[3] = { 0,0,0 };
	mea[0] = A[0] * B._vars[0][0] + A[1] * B._vars[1][0] + A[2] * B._vars[2][0];
	mea[1] = A[0] * B._vars[0][1] + A[1] * B._vars[1][1] + A[2] * B._vars[2][1];
	mea[2] = A[0] * B._vars[0][2] + A[1] * B._vars[1][2] + A[2] * B._vars[2][2];
	return SVector(mea[0], mea[1], mea[2]);
}

SVector operator*(const SMatrix& A, SVector B)
{
	double mea[3] = { 0,0,0 };
	mea[0] = A._vars[0][0] * B[0] + A._vars[0][1] * B[1] + A._vars[0][2] * B[2];
	mea[1] = A._vars[1][0] * B[0] + A._vars[1][1] * B[1] + A._vars[1][2] * B[2];
	mea[2] = A._vars[2][0] * B[0] + A._vars[2][1] * B[1] + A._vars[2][2] * B[2];
	return SVector(mea[0], mea[1], mea[2]);
}

——星辉 20211215