Browse Source

模拟TS的类型数组

master
blobt 4 years ago
parent
commit
d90393c634
  1. BIN
      .vs/3dMath/v15/.suo
  2. 1
      3dMath.vcxproj
  3. 3
      3dMath.vcxproj.filters
  4. 46
      CFloat32Array.h
  5. 4
      GMath.cpp
  6. 68
      main.cpp

BIN
.vs/3dMath/v15/.suo

1
3dMath.vcxproj

@ -125,6 +125,7 @@
<ItemGroup>
<ClInclude Include="CArrayBuffer.h" />
<ClInclude Include="CDataView.h" />
<ClInclude Include="CFloat32Array.h" />
<ClInclude Include="GMath.h" />
</ItemGroup>
<ItemGroup>

3
3dMath.vcxproj.filters

@ -24,6 +24,9 @@
<ClInclude Include="CDataView.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CFloat32Array.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="GMath.cpp">

46
CFloat32Array.h

@ -0,0 +1,46 @@
#pragma once
#include <iostream>
#include "CArrayBuffer.h"
#include "CDataView.h"
class CFloat32Array {
public:
CArrayBuffer* buffer;
int byteOffset;
int byteLength;
private:
int _length;
bool _deleteBuffer;
public:
CFloat32Array(int length) {
this->buffer = new CArrayBuffer(length * sizeof(float32));
this->_length = length;
this->_deleteBuffer = true;
}
CFloat32Array(CArrayBuffer* buffer, int byteOffset, int length) {
this->buffer = buffer;
this->byteOffset = byteOffset;
this->_length = length;
this->byteLength = length * sizeof(float32);
this->_deleteBuffer = false;
}
~CFloat32Array() {
if (this->_deleteBuffer) {
delete this->buffer;
this->buffer = nullptr;
printf("delete");
}
}
float32& operator [](int idx) {
return *((float32*)(this->buffer->pData + (this->byteOffset + idx * sizeof(float32))));
}
int length() {
return this->_length;
}
};

4
GMath.cpp

@ -452,7 +452,7 @@ GVector operator*(const float & k, const GVector & rhs)
{
GVector ret(rhs.n);
for (int i = 0; i < ret.n; i++) {
ret.v[i] *= k;
ret.v[i] = rhs[i] * k;
}
return ret;
}
@ -461,7 +461,7 @@ GVector operator*(const GVector & lhs, const float & k)
{
GVector ret(lhs.n);
for (int i = 0; i < ret.n; i++) {
ret.v[i] *= k;
ret.v[i] = lhs[i] * k;
}
return ret;
}

68
main.cpp

@ -1,42 +1,66 @@
#include "GMath.h"
#include "CArrayBuffer.h"
#include "CDataView.h"
#include "CFloat32Array.h"
int main() {
CArrayBuffer buffer(16);
CDataView view0(&buffer, 0, buffer.byteLength());
printf("arr0 内部生成CArrayBuffer\n");
CFloat32Array arr0(8);
for (int i = 0; i < arr0.length(); i++) {
arr0[i] = i * 100.0;
}
for (int i = 0; i < arr0.length(); i++) {
printf("%f\n", arr0[i]);
}
view0.setFloat32(8, 99.99);
view0.setUint16(8 + 4, 2048);
printf("arr1 共享 arr0 中 CArrayBuffer 的部分区块\n");
CFloat32Array arr1(arr0.buffer, 4 * sizeof(float32), 4);
for (int i = 0; i < arr1.length(); i++) {
printf("%f\n", arr1[i]);
}
printf("%f\n", view0.getFloat32(8));
printf("%d\n", view0.getUint16(8+4));
printf("重置arr1的各个元素为10.0的倍数\n");
for (int i = 0; i < arr1.length(); i++) {
arr1[i] = i * 10.0;
}
for (int i = 0; i < arr1.length(); i++) {
printf("%f\n", arr1[i]);
}
CDataView view1(&buffer, 8, 18);
/*GVector l(3);
l += 2;
cout << l*3 << endl;
printf("%f\n", view1.getFloat32(0));
printf("%d\n", view1.getUint16(4));
/*float a[] = {
2, 2, 2, 2,
3, 3, 3, 3,
4, 4, 4, 4,
5, 5, 5, 5
float a[] = {
2, -1, 3,
4, -2, 5,
6, -3, 8
};
GMatrix A(4, 4, a);
GMatrix A(3, 3, a);
float b[] = {
2,
2,
2,
2
2, -1, 9,
2, -2, 5,
3, 1, 2
};
GMatrix B(4, 1, b);
GMatrix B(3, 3, b);
GMatrix C = A*B;
cout << A*B << endl;*/
cout << A << endl;
cout << B << endl;
cout << C << endl;
GVector ret(3);
GVector rv = B.GetColVec(1);
for (int i = 0; i < A.GetColNum(); i++) {
GVector l = A.GetColVec(i);
ret += rv[i]*l;
}
cout << ret << endl;
*/
/*float a[] = {
1,0,
1,4

Loading…
Cancel
Save