Complex toy classes & DataAnalysis initial

This commit is contained in:
IlyaShurupov 2023-10-21 14:33:33 +03:00 committed by Ilya Shurupov
parent e9daedd6ea
commit b4ae5dde77
12 changed files with 376 additions and 169 deletions

View file

@ -25,4 +25,5 @@ add_subdirectory(Externals)
add_subdirectory(Connection)
add_subdirectory(Graphics)
add_subdirectory(RayTracer)
add_subdirectory(DataAnalysis)
add_subdirectory(Objects)

View file

@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 3.2)
set(CMAKE_CXX_STANDARD 23)
project(DataAnalysis)
### ---------------------- Static Library --------------------- ###
file(GLOB SOURCES "./private/*.cpp" "./private/*/*.cpp")
file(GLOB HEADERS "./public/*.hpp" "./public/*/*.hpp" "./applications/*.hpp")
add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADERS})
target_include_directories(${PROJECT_NAME} PUBLIC ./public/)
target_link_libraries(${PROJECT_NAME} PUBLIC Math Containers)
### -------------------------- Applications -------------------------- ###
add_executable(NumberRec ./applications/NumberRecognition.cpp)
target_link_libraries(NumberRec ${PROJECT_NAME})
#file(COPY "applications/rsc" DESTINATION "${CMAKE_BINARY_DIR}/${PROJECT_NAME}/")
### -------------------------- Tests -------------------------- ###
enable_testing()
file(GLOB TEST_SOURCES "./tests/*.cpp" "./tests/*/*.cpp")
add_executable(${PROJECT_NAME}Tests ${TEST_SOURCES})
target_include_directories(${PROJECT_NAME}Tests PUBLIC ./applications/)
target_link_libraries(${PROJECT_NAME}Tests ${PROJECT_NAME} Utils)
add_test(NAME ${PROJECT_NAME}Tests COMMAND ${PROJECT_NAME}Tests)
install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}/lib)

View file

@ -0,0 +1,3 @@
#include "FullyConnectedNN.hpp"
int main() { return 0; }

View file

@ -0,0 +1,8 @@
#include "DataAnalysisCommon.hpp"
#include "ContainersCommon.hpp"
#include "MathCommon.hpp"
namespace tp {
static ModuleManifest* deps[] = {&gModuleMath, &gModuleContainers, nullptr};
ModuleManifest gModuleDataAnalysis = ModuleManifest("DataAnalysis", nullptr, nullptr, deps);
}

View file

@ -0,0 +1,7 @@
#pragma once
#include "Module.hpp"
namespace tp {
extern ModuleManifest gModuleDataAnalysis;
}

View file

@ -0,0 +1,20 @@
#pragma once
#include "Buffer.hpp"
#include "DataAnalysisCommon.hpp"
namespace tp {
class FullyConnectedNN {
public:
struct Layer {
halnf mBias;
Buffer<halnf> mWeights;
};
public:
FullyConnectedNN() = default;
private:
Buffer<Layer> mLayers;
};
};

View file

@ -0,0 +1,26 @@
#include "FullyConnectedNN.hpp"
#include "Testing.hpp"
#include "Utils.hpp"
static bool init(const tp::ModuleManifest* self) {
tp::gTesting.setRootName(self->getName());
return true;
}
void test() {}
int main() {
tp::ModuleManifest* deps[] = {&tp::gModuleDataAnalysis, &tp::gModuleUtils, nullptr};
tp::ModuleManifest testModule("DataAnalysisTest", init, nullptr, deps);
if (!testModule.initialize()) {
return 1;
}
test();
testModule.deinitialize();
return 0;
}

View file

@ -0,0 +1,57 @@
#pragma once
#include "MathCommon.hpp"
namespace tp {
struct ComplexCart {
typedef alnf Number;
Number r = 0;
Number i = 0;
public:
[[nodiscard]] ComplexCart operator*(const ComplexCart& in) const { return {in.r * r - in.i * i, in.r * i + in.i * r}; }
[[nodiscard]] ComplexCart operator*(Number in) const { return {in * r, in * i}; }
[[nodiscard]] ComplexCart operator+(const ComplexCart& in) const { return {in.r + r, in.i + i}; }
[[nodiscard]] ComplexCart operator-(const ComplexCart& in) const { return {in.r - r, in.i - i}; }
[[nodiscard]] Number mod() const { return tp::sqrt(i * i + r * r); }
[[nodiscard]] Number arg() const { return atan2(r, i); }
[[nodiscard]] Number norm2() const { return this->operator*(conjugate()).r; }
[[nodiscard]] ComplexCart reciprocal() const { return {r / norm2(), -i / norm2()}; }
void set(Number mod, Number arg) {
r = mod * cos(arg);
i = mod * sin(arg);
}
bool operator==(const ComplexCart& in) const { return in.r == r && in.i == i; }
[[nodiscard]] ComplexCart conjugate() const { return {r, -i}; }
};
struct ComplexRad {
typedef alnf Number;
Number r = 0;
Number a = 0;
public:
[[nodiscard]] ComplexRad operator*(const ComplexRad& in) const { return {r * in.r, a + in.a}; }
[[nodiscard]] ComplexRad operator*(Number in) const { return {r * in, a}; }
[[nodiscard]] ComplexCart cart() const {
ComplexCart out;
out.set(r, a);
return out;
}
bool operator==(const ComplexRad& in) const { return in.r == r && in.a == a; }
};
}

View file

@ -1,14 +1,41 @@
#include "ComplexNumbers.hpp"
#include "Testing.hpp"
#include <iostream>
using namespace tp;
TEST_DEF_STATIC(Simple) {
TEST_DEF(ComplexNumber) {
TEST((ComplexCart {3, 4}.mod() == 5));
TEST((ComplexCart {1, 1}.arg() == 0.7853981633974483));
TEST((ComplexCart {3, 4}.norm2() == 25));
{
ComplexCart cn {3, 4};
ComplexCart expected {0.12, -0.16};
auto val = cn.reciprocal();
TEST((val == expected));
}
{
ComplexCart cn {3, 4};
ComplexCart expected {3, -4};
TEST((cn.conjugate() == expected));
}
{
ComplexCart cn1 {3, 4};
ComplexCart cn2 {1, 2};
ComplexCart expected {-5, 10};
TEST((cn1 * cn2 == expected));
}
{
ComplexRad c1 {5, 7};
ComplexRad c2 {10, 3};
ComplexCart tmp1 = (c1 * c2).cart();
ComplexCart tmp2 = (c1.cart() * c2.cart());
TEST((tmp1 - tmp2).mod() < 0.001);
}
}
TEST_DEF(Math) {
testSimple();
}
TEST_DEF(Math) { testComplexNumber(); }

View file

@ -1,8 +1,6 @@
#include "MathCommon.hpp"
#include "Testing.hpp"
#include "Utils.hpp"
#include <cstdlib>
static bool init(const tp::ModuleManifest* self) {
tp::gTesting.setRootName(self->getName());
@ -12,8 +10,7 @@ static bool init(const tp::ModuleManifest* self) {
void testMath();
int main() {
tp::ModuleManifest* deps[] = { &tp::gModuleUtils, nullptr };
tp::ModuleManifest* deps[] = {&tp::gModuleMath, &tp::gModuleUtils, nullptr};
tp::ModuleManifest testModule("MathTest", init, nullptr, deps);
if (!testModule.initialize()) {

View file

@ -8,195 +8,223 @@ using namespace tp;
bool sFailed = false;
struct Undef {
char character1 = 'a';
bool boolean = true;
ualni integer = 321;
char character2 = 'a';
char character1 = 'a';
bool boolean = true;
ualni integer = 321;
char character2 = 'a';
void change() {
character1++;
character2--;
integer++;
boolean = !boolean;
}
void change() {
character1++;
character2--;
integer++;
boolean = !boolean;
}
[[nodiscard]] bool operator!=(const Undef& in) const {
if (character1 != in.character1) return true;
if (character2 != in.character2) return true;
if (integer != in.integer) return true;
if (boolean != in.boolean) return true;
return false;
}
[[nodiscard]] bool operator!=(const Undef& in) const {
if (character1 != in.character1) {
return true;
}
if (character2 != in.character2) {
return true;
}
if (integer != in.integer) {
return true;
}
if (boolean != in.boolean) {
return true;
}
return false;
}
};
struct SimpleArchive {
char character = 'a';
ualni integer = 123;
bool boolean = true;
Undef undef;
char character = 'a';
ualni integer = 123;
bool boolean = true;
Undef undef;
template<typename tArchiver>
void archive(tArchiver& ar) {
ar % character;
ar % integer;
ar % boolean;
ar % undef;
}
template <typename tArchiver>
void archive(tArchiver& ar) {
ar % character;
ar % integer;
ar % boolean;
ar % undef;
}
void change() {
character++;
integer++;
boolean = !boolean;
undef.change();
}
void change() {
character++;
integer++;
boolean = !boolean;
undef.change();
}
[[nodiscard]] bool operator!=(const SimpleArchive& in) const {
if (character != in.character) return true;
if (integer != in.integer) return true;
if (boolean != in.boolean) return true;
if (undef != in.undef) return true;
return false;
}
[[nodiscard]] bool operator!=(const SimpleArchive& in) const {
if (character != in.character) {
return true;
}
if (integer != in.integer) {
return true;
}
if (boolean != in.boolean) {
return true;
}
if (undef != in.undef) {
return true;
}
return false;
}
};
struct Simple {
char character = 'a';
ualni integer = 123;
bool boolean = true;
Undef undef;
char character = 'a';
ualni integer = 123;
bool boolean = true;
Undef undef;
template<typename tArchiver>
void archiveRead(tArchiver& ar) {
ar >> character;
ar >> integer;
ar >> boolean;
ar >> undef;
}
template <typename tArchiver>
void archiveRead(tArchiver& ar) {
ar >> character;
ar >> integer;
ar >> boolean;
ar >> undef;
}
template<typename tArchiver>
void archiveWrite(tArchiver& ar) const {
ar << character;
ar << integer;
ar << boolean;
ar << undef;
}
template <typename tArchiver>
void archiveWrite(tArchiver& ar) const {
ar << character;
ar << integer;
ar << boolean;
ar << undef;
}
void change() {
character++;
integer++;
boolean = !boolean;
undef.change();
}
void change() {
character++;
integer++;
boolean = !boolean;
undef.change();
}
[[nodiscard]] bool operator!=(const Simple& in) const {
if (character != in.character) return true;
if (integer != in.integer) return true;
if (boolean != in.boolean) return true;
if (undef != in.undef) return true;
return false;
}
[[nodiscard]] bool operator!=(const Simple& in) const {
if (character != in.character) {
return true;
}
if (integer != in.integer) {
return true;
}
if (boolean != in.boolean) {
return true;
}
if (undef != in.undef) {
return true;
}
return false;
}
};
template<typename tType>
template <typename tType>
struct Set {
tType buff[10];
ualni len = 5;
tType buff[10];
ualni len = 5;
template<typename tArchiver>
void archiveRead(tArchiver& ar) {
ar >> len;
for (auto i = 0; i < len; i++) {
ar >> buff[i];
}
}
template <typename tArchiver>
void archiveRead(tArchiver& ar) {
ar >> len;
for (auto i = 0; i < len; i++) {
ar >> buff[i];
}
}
template<typename tArchiver>
void archiveWrite(tArchiver& ar) const {
ar << len;
for (auto i = 0; i < len; i++) {
ar << buff[i];
}
}
template <typename tArchiver>
void archiveWrite(tArchiver& ar) const {
ar << len;
for (auto i = 0; i < len; i++) {
ar << buff[i];
}
}
void change() {
len = 2;
for (auto i = 0; i < len; i++) {
buff[i].change();
}
}
void change() {
len = 2;
for (auto i = 0; i < len; i++) {
buff[i].change();
}
}
[[nodiscard]] bool operator!=(const Set& in) const {
for (auto i = 0; i < len; i++) {
if (buff[i] != in.buff[i]) return true;
}
[[nodiscard]] bool operator!=(const Set& in) const {
for (auto i = 0; i < len; i++) {
if (buff[i] != in.buff[i]) {
return true;
}
}
if (len != in.len) return true;
return false;
}
if (len != in.len) {
return true;
}
return false;
}
};
template <typename tType>
struct ComplexCart {
Undef undef;
Set<tType> set;
template<typename tType>
struct Complex {
Undef undef;
Set<tType> set;
template <typename tArchiver>
void archive(tArchiver& ar) {
ar % set;
ar % undef;
}
template<typename tArchiver>
void archive(tArchiver& ar) {
ar % set;
ar % undef;
}
void change() {
undef.change();
set.change();
}
void change() {
undef.change();
set.change();
}
[[nodiscard]] bool operator!=(const Complex& in) const {
return undef != in.undef || set != in.set;
}
[[nodiscard]] bool operator!=(const ComplexCart& in) const { return undef != in.undef || set != in.set; }
};
template<typename tType>
template <typename tType>
void test() {
constexpr auto size = 1024;
constexpr auto size = 1024;
const tType val;
tType res;
const tType val;
tType res;
res.change();
res.change();
ArchiverExample<size, false> write;
ArchiverExample<size, true> read;
ArchiverExample<size, false> write;
ArchiverExample<size, true> read;
write % val;
write % val;
for (auto i = 0; i < size; i++) read.mBuff[i] = write.mBuff[i];
for (auto i = 0; i < size; i++) {
read.mBuff[i] = write.mBuff[i];
}
read % res;
read % res;
if (val != res) {
printf("Test %s Failed\n", typeid(tType).name());
sFailed = true;
}
if (val != res) {
printf("Test %s Failed\n", typeid(tType).name());
sFailed = true;
}
}
template <typename T, typename A, typename = void> struct HasArchive : FalseType {};
template <typename T, typename A> struct HasArchive<T, A, VoidType<decltype(DeclareValue<T>()().archive(DeclareValue<A&>()()))>> : TrueType {};
template <typename T, typename A, typename = void>
struct HasArchive : FalseType {};
template <typename T, typename A>
struct HasArchive<T, A, VoidType<decltype(DeclareValue<T>()().archive(DeclareValue<A&>()()))>> : TrueType {};
void testArchiver() {
printf("has archive method - %i\n", HasArchive<SimpleArchive, ArchiverExample<10, false>>::value);
printf("has archive method - %i\n", ArchiverExample<10, false>::HasFunc::Archive<SimpleArchive>::value);
printf("has archive method - %i\n", ArchiverExample<10, false>::HasFunc::Archive<Simple>::value);
printf("has archive method - %i\n", HasArchive<SimpleArchive, ArchiverExample<10, false>>::value);
printf("has archive method - %i\n", ArchiverExample<10, false>::HasFunc::Archive<SimpleArchive>::value);
printf("has archive method - %i\n", ArchiverExample<10, false>::HasFunc::Archive<Simple>::value);
test<SimpleArchive>();
test<Simple>();
test<Set<Simple>>();
test<ComplexCart<Simple>>();
test<SimpleArchive>();
test<Simple>();
test<Set<Simple>>();
test<Complex<Simple>>();
if (sFailed) {
exit(1);
}
if (sFailed) {
exit(1);
}
}

View file

@ -85,26 +85,27 @@ void testRT() {
RayTracer::RenderSettings settings = {
0,
0,
1,
{10, 10},
};
RayTracer::RenderBuffer output;
output.reserve(RayTracer::RenderBuffer::Index2D(settings.size.x, settings.size.y));
RayTracer::OutputBuffers output;
// output.reserve(RayTracer::RenderBuffer::Index2D(settings.size.x, settings.size.y));
RayTracer rt;
rt.render(scene, output, settings);
TEST(compareCols(output.get({6, 4}), RGBA {0.560100f, 0.560100f, 0.560100f, 1.000000f}));
TEST(compareCols(output.get({6, 5}), RGBA {0.353739f, 0.353739f, 0.353739f, 1.000000f}));
TEST(compareCols(output.get({6, 6}), RGBA {0.242577f, 0.242577f, 0.242577f, 1.000000f}));
TEST(compareCols(output.get({6, 7}), RGBA {0.176313f, 0.176313f, 0.176313f, 1.000000f}));
TEST(compareCols(output.get({6, 8}), RGBA {0.000000f, 0.000000f, 0.000000f, 0.000000f}));
TEST(compareCols(output.color.get({6, 4}), RGBA {0.560100f, 0.560100f, 0.560100f, 1.000000f}));
TEST(compareCols(output.color.get({6, 5}), RGBA {0.353739f, 0.353739f, 0.353739f, 1.000000f}));
TEST(compareCols(output.color.get({6, 6}), RGBA {0.242577f, 0.242577f, 0.242577f, 1.000000f}));
TEST(compareCols(output.color.get({6, 7}), RGBA {0.176313f, 0.176313f, 0.176313f, 1.000000f}));
TEST(compareCols(output.color.get({6, 8}), RGBA {0.000000f, 0.000000f, 0.000000f, 0.000000f}));
if (0) {
writeImage(output);
for (auto i = 0; i < output.size().x; i++) {
for (auto j = 0; j < output.size().y; j++) {
auto tmp = output.get({i, j});
writeImage(output.color);
for (auto i = 0; i < output.color.size().x; i++) {
for (auto j = 0; j < output.color.size().y; j++) {
auto tmp = output.color.get({i, j});
printf("TEST(compareCols(output.get({%i, %i}), RGBA{ %ff, %ff, %ff, %ff }));\n", i, j, tmp.r, tmp.g, tmp.b, tmp.a);
}
}