From 40452a009bbfabbc5edbf53f341a0852c939f187 Mon Sep 17 00:00:00 2001 From: Karl-Wilfried Zimmer Date: Sun, 18 Aug 2024 21:57:24 +0200 Subject: [PATCH] snap --- apps/main.cpp | 21 ++++++---- include/Block/block.h | 51 ------------------------- include/LinAlg/coordinates.h | 22 +++++++++++ include/LinAlg/matrix3.h | 4 +- include/LinAlg/plane.h | 9 +++-- include/LinAlg/vector3.h | 48 +++++++++++------------ include/Volumes/BlockFactory.h | 20 ++++++++++ include/Volumes/block.h | 70 ++++++++++++++++++++++++++++++++++ include/Volumes/eukblock.h | 41 ++++++++++++++++++++ include/Volumes/volume.h | 30 +++++++++++++++ src/BlockFactory.cpp | 24 ++++++++++++ src/block.cpp | 51 ------------------------- src/eukblock.cpp | 40 +++++++++++++++++++ src/matrix3.cpp | 2 +- src/matrixmxn.cpp | 2 +- src/plane.cpp | 2 +- src/vector3.cpp | 28 +++++++------- 17 files changed, 309 insertions(+), 156 deletions(-) delete mode 100644 include/Block/block.h create mode 100644 include/LinAlg/coordinates.h create mode 100644 include/Volumes/BlockFactory.h create mode 100644 include/Volumes/block.h create mode 100644 include/Volumes/eukblock.h create mode 100644 include/Volumes/volume.h create mode 100644 src/BlockFactory.cpp delete mode 100644 src/block.cpp create mode 100644 src/eukblock.cpp diff --git a/apps/main.cpp b/apps/main.cpp index e4b0f54..8191af1 100644 --- a/apps/main.cpp +++ b/apps/main.cpp @@ -1,11 +1,11 @@ #include -#include "Block/block.h" +#include "LinAlg/vector3.h" +#include "Volumes/BlockFactory.h" int main(int argc, char **argv) { - std::cout << "Hello, world!" << std::endl; + //std::cout << "Hello, world!" << std::endl; LinAlg::Vector3 vec(1, 2, 3); LinAlg::Vector3 vec2(2, 1, 3); - Volumes::Block block(vec,1,1,1); std::cout< normals=block.getNormals(); - for(LinAlg::Vector3 vector:normals){ - std::cout< -#include -#include - -namespace Volumes{ - class Block{ - public: - Block(const LinAlg::Vector3& v1, double x, double y, double z): corners{v1, - v1 + LinAlg::Vector3(x, 0, 0), - v1 + LinAlg::Vector3(x, y, 0), - v1 + LinAlg::Vector3(0, y, 0), - v1+ LinAlg::Vector3(0, y, z), - v1 + LinAlg::Vector3(x, y, z), - v1 + LinAlg::Vector3(x, 0, z), - v1 + LinAlg::Vector3(0, 0, z)}{}; - virtual ~Block() = default; - - double getVolume(); - - friend std::ostream& operator<<(std::ostream& os, const Block& v){ - os<<"["; - short count=0; - for(LinAlg::Vector3 corner:v.corners){ - count++; - os< getNormals(); - - LinAlg::Vector3 getNormal(u_int8_t index); - - private: - - LinAlg::Vector3 corners[8]; - }; -} - -#endif //BLOCK_BLOCK_H diff --git a/include/LinAlg/coordinates.h b/include/LinAlg/coordinates.h new file mode 100644 index 0000000..795b8ab --- /dev/null +++ b/include/LinAlg/coordinates.h @@ -0,0 +1,22 @@ +// +// Created by nb on 18.08.24. +// + +#ifndef VOLSVECS_COORDINATES_H +#define VOLSVECS_COORDINATES_H + +namespace LinAlg { + namespace CoordSys { + template + struct CoordinateSystem { + static constexpr int level = ParentSystem::level + 1; + using Parent = ParentSystem; + }; + + struct Root { + static constexpr int level = 0; + }; + } +} + +#endif //VOLSVECS_COORDINATES_H diff --git a/include/LinAlg/matrix3.h b/include/LinAlg/matrix3.h index e227237..a8c1eab 100644 --- a/include/LinAlg/matrix3.h +++ b/include/LinAlg/matrix3.h @@ -7,8 +7,8 @@ #include "LinAlg/matrixmxn.h" -namespace LinAlg{ - class Matrix3: public Matrix<3,3>{ +namespace LinAlg { + class Matrix3 : public Matrix<3, 3> { public: private: diff --git a/include/LinAlg/plane.h b/include/LinAlg/plane.h index 2722c2e..1f0f5b3 100644 --- a/include/LinAlg/plane.h +++ b/include/LinAlg/plane.h @@ -7,14 +7,15 @@ #include "LinAlg/vector3.h" -namespace LinAlg{ - class Plane{ +namespace LinAlg { + class Plane { public: - Plane(const Vector3& a, const Vector3& b, const Vector3& c): _entries{a, b-a, c-a}{}; + Plane(const Vector3 &a, const Vector3 &b, const Vector3 &c) : _entries{a, b - a, c - a} {}; Vector3 getNormal(); - virtual ~Plane()=default; + virtual ~Plane() = default; + private: Vector3 _entries[3]; }; diff --git a/include/LinAlg/vector3.h b/include/LinAlg/vector3.h index 6f7bed6..b24b46c 100644 --- a/include/LinAlg/vector3.h +++ b/include/LinAlg/vector3.h @@ -8,56 +8,56 @@ #include "iostream" #include -namespace LinAlg{ - class Vector3{ +namespace LinAlg { + class Vector3 { public: - Vector3(double x,double y, double z):x(x),y(y),z(z){}; - Vector3(): Vector3(0,0,0){}; + Vector3(double x, double y, double z) : x(x), y(y), z(z) {}; + + Vector3() : Vector3(0, 0, 0) {}; virtual ~Vector3() = default; - Vector3 operator+(const Vector3& v) const; + Vector3 operator+(const Vector3 &v) const; - Vector3 operator-(const Vector3& v) const{ - Vector3 tmp=v*-1; - return *this+tmp; + Vector3 operator-(const Vector3 &v) const { + Vector3 tmp = v * -1; + return *this + tmp; }; Vector3 operator*(double s) const; - Vector3 operator/(double s) const{ - return *this*(1/s); + Vector3 operator/(double s) const { + return *this * (1 / s); }; - Vector3& operator*=(double s); + Vector3 &operator*=(double s); - Vector3& operator/=(double s){ - return *this*=(1/s); + Vector3 &operator/=(double s) { + return *this *= (1 / s); }; - Vector3& operator+=(const Vector3& v); + Vector3 &operator+=(const Vector3 &v); - Vector3& operator-=(const Vector3& v); + Vector3 &operator-=(const Vector3 &v); - Vector3& operator=(const Vector3& v); + Vector3 &operator=(const Vector3 &v); + Vector3 &norm(); - Vector3& norm(); + [[nodiscard]] double dot(const Vector3 &v) const; - double dot(const Vector3& v) const; + [[nodiscard]] double len() const; - double len() const; + [[nodiscard]] Vector3 cross(const Vector3 &v) const; - Vector3 cross(const Vector3& v) const; - - friend std::ostream& operator<<(std::ostream& os, const Vector3& v){ - os<<"{"< +#include + +namespace Volumes { + class Block { + public: + explicit Block(LinAlg::Vector3 arr[8]) { + std::copy(arr, arr + 8, _corners); + }; + + virtual ~Block() = default; + + std::vector> getNormals() { + if (!_set) { + calculateNormals(); + _set = true; + } + return _normals; + }; + + + protected: + virtual void calculateNormals() = 0; + + virtual std::function calculateNormal(u_int8_t dir) = 0; + + LinAlg::Vector3 _corners[8]; + std::vector> _normals; + + static std::vector getSurfaceIndex(short index) { + switch (index) { + case BLOCK_BOTTOM: + return {0, 3, 2, 1}; + case BLOCK_FRONT: + return {0, 1, 6, 7}; + case BLOCK_LEFT: + return {0, 7, 4, 3}; + case BLOCK_RIGHT: + return {1, 2, 5, 6}; + case BLOCK_TOP: + return {4, 7, 6, 5}; + case BLOCK_BACK: + return {2, 3, 4, 5}; + default: + return {-1, -1, -1, -1}; + } + } + + private: + bool _set = false; + }; +} + +#endif //VOLSVECS_BLOCK_H diff --git a/include/Volumes/eukblock.h b/include/Volumes/eukblock.h new file mode 100644 index 0000000..469d466 --- /dev/null +++ b/include/Volumes/eukblock.h @@ -0,0 +1,41 @@ +// +// Created by nb on 04.08.24. +// + +#ifndef VOLSVECS_EUKBLOCK_H +#define VOLSVECS_EUKBLOCK_H + +#include "Volumes/block.h" +#include "Volumes/volume.h" +#include + +namespace Volumes { + class EuklidianBlock : public Block, public Volume { + public: + explicit EuklidianBlock(LinAlg::Vector3 arr[8]) : Block(arr), Volume() { + for (int i = 0; i < 8; i++) { + std::cout << arr[i]; + } + std::cout << std::endl; + } + + ~EuklidianBlock() override {}; + + friend std::ostream &operator<<(std::ostream &os, const EuklidianBlock &m) { + os << "{"; + for (int i = 0; i < 8; i++) { + os << m._corners[i]; + } + os << "}"; + return os; + }; + protected: + void calculateVolume() override; + + std::function calculateNormal(u_int8_t dir) override; + + void calculateNormals() override; + }; + +} +#endif //VOLSVECS_EUKBLOCK_H diff --git a/include/Volumes/volume.h b/include/Volumes/volume.h new file mode 100644 index 0000000..4699f4e --- /dev/null +++ b/include/Volumes/volume.h @@ -0,0 +1,30 @@ +// +// Created by nb on 04.08.24. +// + +#ifndef VOLSVECS_VOLUME_H +#define VOLSVECS_VOLUME_H + +namespace Volumes { + class Volume { + public: + virtual ~Volume() = default; + + double getVolume() { + if (!_set) { + calculateVolume(); + _set = true; + } + return _volume; + }; + + protected: + + virtual void calculateVolume() = 0; + + double _volume = -1; + bool _set = false; + }; +} + +#endif //VOLSVECS_VOLUME_H diff --git a/src/BlockFactory.cpp b/src/BlockFactory.cpp new file mode 100644 index 0000000..72d21e1 --- /dev/null +++ b/src/BlockFactory.cpp @@ -0,0 +1,24 @@ +// +// Created by nb on 18.08.24. +// +#include "Volumes/BlockFactory.h" +#include + +namespace Volumes { + EuklidianBlock BlockFactory::createCuboid(LinAlg::Vector3 v, double x, double y, double z) { + LinAlg::Vector3 _x(-x, 0, 0); + LinAlg::Vector3 _y(0, y, 0); + LinAlg::Vector3 _z(0, 0, z); + LinAlg::Vector3 arr[8] = {v, v + _y, v + _y + _x, v + _x, v + _x + _z, v + _x + _z + _y, v + _z + _y, v + _z}; + for (auto ele: arr) { + std::cout << ele << " "; + } + std::cout << std::endl; + return EuklidianBlock(arr); + } + + EuklidianBlock BlockFactory::createCube(LinAlg::Vector3 v, double a) { + return createCuboid(v, a, a, a); + } + +} \ No newline at end of file diff --git a/src/block.cpp b/src/block.cpp deleted file mode 100644 index f73f366..0000000 --- a/src/block.cpp +++ /dev/null @@ -1,51 +0,0 @@ -// -// Created by nb on 07.07.24. -// -#include "Block/block.h" - -namespace Volumes{ - double Block::getVolume() { - LinAlg::Vector3 x= corners[1] - corners[0]; - LinAlg::Vector3 y= corners[3] - corners[0]; - LinAlg::Vector3 z= corners[7] - corners[0]; - return x.len()*y.len()*z.len(); - } - - std::vector Block::getNormals() { - std::vector ret; - for(int i=0;i<6;i++) { - ret.push_back(getNormal(i)); - } - return ret; - } - - LinAlg::Vector3 Block::getNormal(u_int8_t index) { - switch(index){ - case 0:{ - LinAlg::Plane tmp(corners[1],corners[0],corners[2]); - return tmp.getNormal(); - }; - case 1:{ - LinAlg::Plane tmp(corners[3],corners[4],corners[2]); - return tmp.getNormal(); - } - case 2:{ - LinAlg::Plane tmp(corners[7],corners[4],corners[0]); - return tmp.getNormal(); - } - case 3:{ - LinAlg::Plane tmp(corners[5],corners[6],corners[2]); - return tmp.getNormal(); - } - case 4:{ - LinAlg::Plane tmp(corners[5],corners[4],corners[6]); - return tmp.getNormal(); - } - case 5:{ - LinAlg::Plane tmp(corners[6],corners[7],corners[1]); - return tmp.getNormal(); - } - default: return LinAlg::Vector3(); - } - } -} \ No newline at end of file diff --git a/src/eukblock.cpp b/src/eukblock.cpp new file mode 100644 index 0000000..1f5a4d1 --- /dev/null +++ b/src/eukblock.cpp @@ -0,0 +1,40 @@ +// +// Created by nb on 18.08.24. +// +#include "Volumes/eukblock.h" + +namespace Volumes { + + + void EuklidianBlock::calculateVolume() { + auto a = _corners[1] - _corners[0]; + auto b = _corners[3] - _corners[0]; + auto c = _corners[7] - _corners[0]; + _volume = a.len() * b.len() * c.len(); + } + + std::function EuklidianBlock::calculateNormal(u_int8_t dir) { + if (dir > 7) { + return [](double x, double y, double z) { + return LinAlg::Vector3(); + }; + } + auto indexes = getSurfaceIndex(dir); + return [=](double x, double y, double z) { + auto a = _corners[indexes[1]]; + auto b = _corners[indexes[0]]; + auto c = _corners[indexes[2]]; + auto A = b - a; + auto B = c - a; + auto C = B.cross(A); + C.norm(); + return C; + }; + } + + void EuklidianBlock::calculateNormals() { + for (int i = 0; i < 6; i++) { + _normals.push_back(calculateNormal(i)); + } + } +} \ No newline at end of file diff --git a/src/matrix3.cpp b/src/matrix3.cpp index 20f4698..947d1f9 100644 --- a/src/matrix3.cpp +++ b/src/matrix3.cpp @@ -3,5 +3,5 @@ // #include "LinAlg/matrix3.h" -namespace LinAlg{ +namespace LinAlg { } \ No newline at end of file diff --git a/src/matrixmxn.cpp b/src/matrixmxn.cpp index 4cae6d6..b135e24 100644 --- a/src/matrixmxn.cpp +++ b/src/matrixmxn.cpp @@ -3,6 +3,6 @@ // #include "LinAlg/matrixmxn.h" -namespace LinAlg{ +namespace LinAlg { } \ No newline at end of file diff --git a/src/plane.cpp b/src/plane.cpp index 388ba42..fdddb7d 100644 --- a/src/plane.cpp +++ b/src/plane.cpp @@ -3,7 +3,7 @@ // #include "LinAlg/plane.h" -namespace LinAlg{ +namespace LinAlg { Vector3 Plane::getNormal() { Vector3 temp = (_entries[1].cross(_entries[2])).norm(); return temp; diff --git a/src/vector3.cpp b/src/vector3.cpp index a2bb1f2..835d8ad 100644 --- a/src/vector3.cpp +++ b/src/vector3.cpp @@ -1,45 +1,45 @@ #include "LinAlg/vector3.h" -namespace LinAlg{ +namespace LinAlg { Vector3 Vector3::operator+(const LinAlg::Vector3 &v) const { - return {x+v.x,y+v.y,z+v.z}; + return {x + v.x, y + v.y, z + v.z}; } - Vector3& Vector3::operator=(const LinAlg::Vector3 &v) = default; + Vector3 &Vector3::operator=(const LinAlg::Vector3 &v) = default; - Vector3& Vector3::operator+=(const LinAlg::Vector3 &v) { - *this=*this+v; + Vector3 &Vector3::operator+=(const LinAlg::Vector3 &v) { + *this = *this + v; return *this; } double Vector3::dot(const LinAlg::Vector3 &v) const { - return x*v.x+y*v.y+z*v.z; + return x * v.x + y * v.y + z * v.z; } - Vector3 Vector3::cross(const LinAlg::Vector3& v) const { - return {y*v.z-v.y*z,z*v.x-v.z*x,x*v.y-v.x*y}; + Vector3 Vector3::cross(const LinAlg::Vector3 &v) const { + return {y * v.z - v.y * z, z * v.x - v.z * x, x * v.y - v.x * y}; } double Vector3::len() const { - return std::sqrt(x*x+y*y+z*z); + return std::sqrt(x * x + y * y + z * z); } Vector3 Vector3::operator*(double s) const { - return {x*s, y*s, z*s}; + return {x * s, y * s, z * s}; } - Vector3& Vector3::operator*=(double s) { - *this=*this*s; + Vector3 &Vector3::operator*=(double s) { + *this = *this * s; return *this; } Vector3 &Vector3::operator-=(const LinAlg::Vector3 &v) { - *this=*this-v; + *this = *this - v; return *this; } Vector3 &Vector3::norm() { - *this=*this/=this->len(); + *this = *this /= this->len(); return *this; } } \ No newline at end of file