Browse Source

first commit

master
blobt 4 years ago
commit
1784db117e
  1. 43
      .gitignore
  2. BIN
      .vs/3dMath/v15/.suo
  3. BIN
      .vs/3dMath/v15/Browse.VC.opendb
  4. 31
      3dMath.sln
  5. 135
      3dMath.vcxproj
  6. 30
      3dMath.vcxproj.filters
  7. 4
      3dMath.vcxproj.user
  8. 1041
      GMath.cpp
  9. 202
      GMath.h
  10. 17
      main.cpp

43
.gitignore

@ -0,0 +1,43 @@
# ---> C++
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
#vs2015
Debug
Release
x64
.git
ipch
*.VC.db
*.VC.VC.opendb

BIN
.vs/3dMath/v15/.suo

BIN
.vs/3dMath/v15/Browse.VC.opendb

31
3dMath.sln

@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.1300
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "3dMath", "3dMath.vcxproj", "{BAB2F10C-2B89-463F-B785-9A18937F6238}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BAB2F10C-2B89-463F-B785-9A18937F6238}.Debug|x64.ActiveCfg = Debug|x64
{BAB2F10C-2B89-463F-B785-9A18937F6238}.Debug|x64.Build.0 = Debug|x64
{BAB2F10C-2B89-463F-B785-9A18937F6238}.Debug|x86.ActiveCfg = Debug|Win32
{BAB2F10C-2B89-463F-B785-9A18937F6238}.Debug|x86.Build.0 = Debug|Win32
{BAB2F10C-2B89-463F-B785-9A18937F6238}.Release|x64.ActiveCfg = Release|x64
{BAB2F10C-2B89-463F-B785-9A18937F6238}.Release|x64.Build.0 = Release|x64
{BAB2F10C-2B89-463F-B785-9A18937F6238}.Release|x86.ActiveCfg = Release|Win32
{BAB2F10C-2B89-463F-B785-9A18937F6238}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B523576E-FA21-49B7-90A5-0F9CF9D239BD}
EndGlobalSection
EndGlobal

135
3dMath.vcxproj

@ -0,0 +1,135 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{BAB2F10C-2B89-463F-B785-9A18937F6238}</ProjectGuid>
<RootNamespace>My3dMath</RootNamespace>
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="GMath.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="GMath.cpp" />
<ClCompile Include="main.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

30
3dMath.vcxproj.filters

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="GMath.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="GMath.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="main.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>

4
3dMath.vcxproj.user

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>

1041
GMath.cpp
File diff suppressed because it is too large
View File

202
GMath.h

@ -0,0 +1,202 @@
#pragma once
#include <cmath>
#include <stdio.h>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <stdarg.h>
#define PRECISION 0.000001
#define SQRT(X) sqrt((X)) //求平方根
#define SQR(X) ((X)*(X)) //求平方
#define ABS(X) abs((X))
#define EQ(X,Y,EPS) (ABS((X)-(Y))<EPS)//通过误差去判断,两个数是否相等
#define EQ_ZERO(X,EPS) (ABS((X))<EPS)
#define ARR_ZERO(A, N) memset((A), 0, sizeof(A[0])*(N))
#define MIN(x,y) (x)<(y)?(x):(y);
#define SWAP(type, x, y) {type tmp=(x); (x)=(y); (y) = tmp;}
using namespace std;
class GVector3 {
private:
float v[3];
public:
GVector3(float x = 0.0f, float y = 0.0f, float z = 0.0f);
GVector3(const GVector3& copy);
GVector3& operator =(const GVector3& rhs);
//
GVector3 operator +(const GVector3& rhs) const; // u + v
GVector3 operator -(const GVector3& rhs) const; // u - v
//
friend GVector3 operator *(const GVector3& lhs, const float& k); // u * k
friend GVector3 operator *(const float& k, const GVector3& rhs); // k * u
friend GVector3 operator /(const GVector3& lhs, const float& k); // u / k
//
friend float norm(const GVector3& v); //
GVector3& normalize(); //
//
float operator *(const GVector3& rhs) const;
//
friend GVector3 proj(const GVector3& p, const GVector3& q);
friend GVector3 perp(const GVector3& p, const GVector3& q);
//
GVector3 operator^(const GVector3& rhs) const;
//1
GVector3& Set(const float& x, const float& y, const float& z);
friend float distance(const GVector3& v, const GVector3 u);
//2
GVector3& operator +=(const GVector3& rhs);
GVector3& operator -=(const GVector3& rhs);
friend ostream& operator<<(ostream& os, const GVector3& v);
//3
GVector3& operator *=(const float& k);
GVector3& operator /=(const float& k);
GVector3& operator ^=(const GVector3& rhs);
//4
bool operator ==(const GVector3 rhs) const;
bool operator !=(const GVector3 rhs) const;
//5
GVector3 operator+() const;
GVector3 operator-() const;
float& operator [](const int& idx); //
const float operator [](const int& idx) const;//
};
class GMatrix;
class GVector {
public:
//GVector 1~2
GVector(int dim = 3);
GVector(int dim, double x, ...);
GVector(const GVector3& copy);
GVector(const GVector& copy);
~GVector();
//GVector 3
GVector& Set(double x, ...);
GVector& Set(float *p);
//GVector 4
GVector& operator =(const GVector& rhs);
GVector& operator +=(const GVector& rhs);
GVector& operator -=(const GVector& rhs);
GVector& operator +=(const float& k);
GVector& operator -=(const float& k);
GVector &operator *=(const float &k);
GVector &operator /=(const float &k);
bool operator ==(const GVector& rhs) const;
bool operator !=(const GVector& rhs) const;
GVector operator +() const;
GVector operator -() const;
GVector operator +(const GVector& rhs) const;
GVector operator -(const GVector& rhs) const;
float operator *(const GVector& rhs) const;
GVector operator /(const float& k) const;
float& operator [](const int& idx);
const float& operator [](const int& idx) const;
GVector& Nornalize();
int GetDim() const;
friend GVector operator *(const float& k, const GVector& rhs);
friend GVector operator *(const GVector& lhs, const float& k);
friend GVector operator *(const GMatrix& m, const GVector& v);
friend GMatrix operator *(const GVector& v, const GMatrix& m);
friend float norm(const GVector& v);
friend float distance(const GVector& v, const GVector& u);
friend ostream& operator <<(ostream& os, const GVector& v);
friend class GMatrix;
private:
int n;
float* v;
};
class GMatrix {
public:
GMatrix(int row = 4, int col = 4, float *elem = NULL);
GMatrix(const GMatrix& copy);
~GMatrix();
GMatrix& operator =(const GMatrix& rhs);
GMatrix& operator +=(const GMatrix& rhs);
GMatrix& operator -=(const GMatrix& rhs);
GMatrix& operator *=(const float& k);
GMatrix& operator *=(const GMatrix& rhs);
GMatrix& operator /=(const float& k);
bool operator ==(const GMatrix& rhs) const;
bool operator !=(const GMatrix& rhs) const;
float* operator [](const int idx);
const float* operator [](const int idx) const;
GMatrix operator +() const;
GMatrix operator -() const;
GMatrix operator +(const GMatrix& rhs) const;
GMatrix operator -(const GMatrix& rhs) const;
GMatrix operator *(const GMatrix& rhs) const;
GMatrix operator /(const float& k) const;
GMatrix& SetTranspose();
GMatrix& SetIdentity();
GMatrix& SetZeros();
GMatrix& SetRowVec(const int idx, const GVector& v);
GMatrix& SetColVec(const int idx, const GVector& v);
GVector GetRowVec(const int idx) const;
GVector GetColVec(const int idx) const;
GMatrix& ExchangeRows(const int idx0, const int idx1);
GMatrix& ExchangeCols(const int idx0, const int idx1);
int GetRowNum() const;
int GetColNum() const;
bool IsSquare() const;
friend GMatrix operator *(const GMatrix& lhs, const float& k);
friend GMatrix operator *(const float& k , const GMatrix& rhs);
friend GVector operator *(const GMatrix& m, const GVector& v);
friend GMatrix operator *(const GVector& v, const GMatrix& m);
friend ostream& operator <<(ostream& os, const GMatrix& m);
friend GMatrix RowEchelonForm(const GMatrix& m);
friend GMatrix ReduceRowEchelonForm(const GMatrix& m);
friend float* from_arr(const GMatrix& m);
friend int Rank(const GMatrix& m);//
friend int Nullity(const GMatrix& m);//
friend GMatrix Mij(const GMatrix& m, int r, int c); //
friend float Det(const GMatrix& m);
private:
int r;
int c;
float *m;
};

17
main.cpp

@ -0,0 +1,17 @@
#include "GMath.h"
int main() {
float f1[] = {
1.0f, 4.0f, -8.0,
0.0f, 2.0f, 9.0,
0.0f, 6.0f, 1.0f
};
GMatrix M1(3,3, f1);
cout << M1 << endl;
cout << Det(M1) << endl;
return 0;
}
Loading…
Cancel
Save