This commit is contained in:
Karl-Wilfried Zimmer 2024-08-18 21:57:24 +02:00
parent 66ae828379
commit 40452a009b
17 changed files with 309 additions and 156 deletions

View File

@ -1,11 +1,11 @@
#include <iostream> #include <iostream>
#include "Block/block.h" #include "LinAlg/vector3.h"
#include "Volumes/BlockFactory.h"
int main(int argc, char **argv) { 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 vec(1, 2, 3);
LinAlg::Vector3 vec2(2, 1, 3); LinAlg::Vector3 vec2(2, 1, 3);
Volumes::Block block(vec,1,1,1);
std::cout<<vec<<std::endl; std::cout<<vec<<std::endl;
std::cout<<vec.len()<<std::endl; std::cout<<vec.len()<<std::endl;
vec*=2; vec*=2;
@ -22,10 +22,17 @@ int main(int argc, char **argv) {
vec-=vec; vec-=vec;
std::cout<<vec<<std::endl; std::cout<<vec<<std::endl;
std::cout<<vec.len()<<std::endl; std::cout<<vec.len()<<std::endl;
std::cout<<block<<std::endl; auto cuboid=Volumes::BlockFactory::createCuboid(vec,1,2,3);
std::vector<LinAlg::Vector3> normals=block.getNormals(); auto cube=Volumes::BlockFactory::createCube(vec,1);
for(LinAlg::Vector3 vector:normals){ std::cout<<cube<<std::endl;
std::cout<<vector<<std::endl; std::cout<<cube.getVolume()<<std::endl;
std::cout<<cuboid<<std::endl;
std::cout<<cuboid.getVolume()<<std::endl;
auto normals=cuboid.getNormals();
std::cout<<normals.size()<<std::endl;
for (auto normal: normals) {
auto value=normal(0,0,0);
std::cout<<value<<std::endl;
} }
return 0; return 0;
} }

View File

@ -1,51 +0,0 @@
//
// Created by nb on 07.07.24.
//
#ifndef BLOCK_BLOCK_H
#define BLOCK_BLOCK_H
#include <LinAlg/vector3.h>
#include <LinAlg/plane.h>
#include <vector>
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<<corner;
if(count<8) {
os << std::endl;
}
}
os<<"]";
return os;
}
std::vector<LinAlg::Vector3> getNormals();
LinAlg::Vector3 getNormal(u_int8_t index);
private:
LinAlg::Vector3 corners[8];
};
}
#endif //BLOCK_BLOCK_H

View File

@ -0,0 +1,22 @@
//
// Created by nb on 18.08.24.
//
#ifndef VOLSVECS_COORDINATES_H
#define VOLSVECS_COORDINATES_H
namespace LinAlg {
namespace CoordSys {
template<typename System, typename ParentSystem>
struct CoordinateSystem {
static constexpr int level = ParentSystem::level + 1;
using Parent = ParentSystem;
};
struct Root {
static constexpr int level = 0;
};
}
}
#endif //VOLSVECS_COORDINATES_H

View File

@ -7,8 +7,8 @@
#include "LinAlg/matrixmxn.h" #include "LinAlg/matrixmxn.h"
namespace LinAlg{ namespace LinAlg {
class Matrix3: public Matrix<3,3>{ class Matrix3 : public Matrix<3, 3> {
public: public:
private: private:

View File

@ -7,14 +7,15 @@
#include "LinAlg/vector3.h" #include "LinAlg/vector3.h"
namespace LinAlg{ namespace LinAlg {
class Plane{ class Plane {
public: 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(); Vector3 getNormal();
virtual ~Plane()=default; virtual ~Plane() = default;
private: private:
Vector3 _entries[3]; Vector3 _entries[3];
}; };

View File

@ -8,56 +8,56 @@
#include "iostream" #include "iostream"
#include <cmath> #include <cmath>
namespace LinAlg{ namespace LinAlg {
class Vector3{ class Vector3 {
public: public:
Vector3(double x,double y, double z):x(x),y(y),z(z){}; Vector3(double x, double y, double z) : x(x), y(y), z(z) {};
Vector3(): Vector3(0,0,0){};
Vector3() : Vector3(0, 0, 0) {};
virtual ~Vector3() = default; virtual ~Vector3() = default;
Vector3 operator+(const Vector3& v) const; Vector3 operator+(const Vector3 &v) const;
Vector3 operator-(const Vector3& v) const{ Vector3 operator-(const Vector3 &v) const {
Vector3 tmp=v*-1; Vector3 tmp = v * -1;
return *this+tmp; return *this + tmp;
}; };
Vector3 operator*(double s) const; Vector3 operator*(double s) const;
Vector3 operator/(double s) const{ Vector3 operator/(double s) const {
return *this*(1/s); return *this * (1 / s);
}; };
Vector3& operator*=(double s); Vector3 &operator*=(double s);
Vector3& operator/=(double s){ Vector3 &operator/=(double s) {
return *this*=(1/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 << "{" << v.x << ", " << v.y << ", " << v.z << "}";
friend std::ostream& operator<<(std::ostream& os, const Vector3& v){
os<<"{"<<v.x<<", "<<v.y<<", "<<v.z<<"}";
return os; return os;
} }
private: private:
double x,y,z; double x, y, z;
}; };
} }

View File

@ -0,0 +1,20 @@
//
// Created by nb on 11.08.24.
//
#ifndef VOLSVECS_BLOCKFACTORY_H
#define VOLSVECS_BLOCKFACTORY_H
#include "LinAlg/vector3.h"
#include "Volumes/eukblock.h"
namespace Volumes {
class BlockFactory {
public:
static EuklidianBlock createCuboid(LinAlg::Vector3 v, double x, double y, double z);
static EuklidianBlock createCube(LinAlg::Vector3 v, double a);
};
}
#endif //VOLSVECS_BLOCKFACTORY_H

70
include/Volumes/block.h Normal file
View File

@ -0,0 +1,70 @@
//
// Created by nb on 04.08.24.
//
#ifndef VOLSVECS_BLOCK_H
#define VOLSVECS_BLOCK_H
#define BLOCK_BOTTOM 0
#define BLOCK_FRONT 1
#define BLOCK_LEFT 2
#define BLOCK_RIGHT 3
#define BLOCK_TOP 4
#define BLOCK_BACK 5
#include "LinAlg/vector3.h"
#include <vector>
#include <functional>
namespace Volumes {
class Block {
public:
explicit Block(LinAlg::Vector3 arr[8]) {
std::copy(arr, arr + 8, _corners);
};
virtual ~Block() = default;
std::vector<std::function<LinAlg::Vector3(double, double, double)>> getNormals() {
if (!_set) {
calculateNormals();
_set = true;
}
return _normals;
};
protected:
virtual void calculateNormals() = 0;
virtual std::function<LinAlg::Vector3(double, double, double)> calculateNormal(u_int8_t dir) = 0;
LinAlg::Vector3 _corners[8];
std::vector<std::function<LinAlg::Vector3(double, double, double)>> _normals;
static std::vector<short> 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

View File

@ -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 <iostream>
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<LinAlg::Vector3(double, double, double)> calculateNormal(u_int8_t dir) override;
void calculateNormals() override;
};
}
#endif //VOLSVECS_EUKBLOCK_H

30
include/Volumes/volume.h Normal file
View File

@ -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

24
src/BlockFactory.cpp Normal file
View File

@ -0,0 +1,24 @@
//
// Created by nb on 18.08.24.
//
#include "Volumes/BlockFactory.h"
#include <iostream>
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);
}
}

View File

@ -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<LinAlg::Vector3> Block::getNormals() {
std::vector<LinAlg::Vector3> 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();
}
}
}

40
src/eukblock.cpp Normal file
View File

@ -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<LinAlg::Vector3(double, double, double)> 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));
}
}
}

View File

@ -3,5 +3,5 @@
// //
#include "LinAlg/matrix3.h" #include "LinAlg/matrix3.h"
namespace LinAlg{ namespace LinAlg {
} }

View File

@ -3,6 +3,6 @@
// //
#include "LinAlg/matrixmxn.h" #include "LinAlg/matrixmxn.h"
namespace LinAlg{ namespace LinAlg {
} }

View File

@ -3,7 +3,7 @@
// //
#include "LinAlg/plane.h" #include "LinAlg/plane.h"
namespace LinAlg{ namespace LinAlg {
Vector3 Plane::getNormal() { Vector3 Plane::getNormal() {
Vector3 temp = (_entries[1].cross(_entries[2])).norm(); Vector3 temp = (_entries[1].cross(_entries[2])).norm();
return temp; return temp;

View File

@ -1,45 +1,45 @@
#include "LinAlg/vector3.h" #include "LinAlg/vector3.h"
namespace LinAlg{ namespace LinAlg {
Vector3 Vector3::operator+(const LinAlg::Vector3 &v) const { 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) { Vector3 &Vector3::operator+=(const LinAlg::Vector3 &v) {
*this=*this+v; *this = *this + v;
return *this; return *this;
} }
double Vector3::dot(const LinAlg::Vector3 &v) const { 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 { 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}; return {y * v.z - v.y * z, z * v.x - v.z * x, x * v.y - v.x * y};
} }
double Vector3::len() const { 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 { 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) { Vector3 &Vector3::operator*=(double s) {
*this=*this*s; *this = *this * s;
return *this; return *this;
} }
Vector3 &Vector3::operator-=(const LinAlg::Vector3 &v) { Vector3 &Vector3::operator-=(const LinAlg::Vector3 &v) {
*this=*this-v; *this = *this - v;
return *this; return *this;
} }
Vector3 &Vector3::norm() { Vector3 &Vector3::norm() {
*this=*this/=this->len(); *this = *this /= this->len();
return *this; return *this;
} }
} }