added vector3 to project

This commit is contained in:
Karl-Wilfried Zimmer 2024-07-07 20:42:39 +02:00
parent 14c8b3f26b
commit aa02ff3faa
9 changed files with 138 additions and 31 deletions

7
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/cmake-build-debug/_deps/vector-src" vcs="Git" />
</component>
</project>

View File

@ -21,11 +21,11 @@ include(FetchContent)
# Formatting library, adds fmt::fmt Always use the full git hash, not the tag, # Formatting library, adds fmt::fmt Always use the full git hash, not the tag,
# safer and faster to recompile # safer and faster to recompile
FetchContent_Declare( #FetchContent_Declare(
vector # vector
GIT_REPOSITORY https://concreteserver.ddns.net/gitea/CarboHydra/Vector.git # GIT_REPOSITORY https://concreteserver.ddns.net/gitea/CarboHydra/Vector.git
GIT_TAG main) # GIT_TAG main)
FetchContent_MakeAvailable(vector) #FetchContent_MakeAvailable(vector)
add_subdirectory(${PROJECT_SOURCE_DIR}/src) add_subdirectory(${PROJECT_SOURCE_DIR}/src)

View File

@ -1,3 +1,3 @@
add_executable(app main.cpp) add_executable(app main.cpp)
target_link_libraries(app PRIVATE vector) target_link_libraries(app PRIVATE block vector)

View File

@ -1,25 +1,8 @@
#include <iostream> #include <iostream>
#include "Vector/vector3.h" #include "Block/block.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;
Vector::Vector3 vec(1,2,3); Volumes::Block block;
Vector::Vector3 vec2(2,1,3);
std::cout<<vec<<std::endl;
std::cout<<vec.len()<<std::endl;
vec*=2;
std::cout<<vec<<std::endl;
std::cout<<vec.len()<<std::endl;
vec/=2;
std::cout<<vec<<std::endl;
std::cout<<vec.len()<<std::endl;
std::cout<<vec.dot(vec)<<std::endl;
std::cout<<vec.cross(vec2)<<std::endl;
vec.norm();
std::cout<<vec<<std::endl;
std::cout<<vec.len()<<std::endl;
vec-=vec;
std::cout<<vec<<std::endl;
std::cout<<vec.len()<<std::endl;
return 0; return 0;
} }

View File

@ -5,9 +5,15 @@
#ifndef BLOCK_BLOCK_H #ifndef BLOCK_BLOCK_H
#define BLOCK_BLOCK_H #define BLOCK_BLOCK_H
namespace Volumes{ #include <Vector/vector3.h>
class block{
namespace Volumes{
class Block{
public:
virtual ~Block() = default;
private:
Vector::Vector3 corners[8];
}; };
} }

65
include/Vector/vector3.h Normal file
View File

@ -0,0 +1,65 @@
//
// Created by nb on 07.07.24.
//
#ifndef VECTOR_VECTOR3_H
#define VECTOR_VECTOR3_H
#include "iostream"
#include <cmath>
namespace Vector{
class Vector3{
public:
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 tmp=v*-1;
return *this+tmp;
};
Vector3 operator*(double s) const;
Vector3 operator/(double s) const{
return *this*(1/s);
};
Vector3& operator*=(double 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& norm();
double dot(const Vector3& v) const;
double len() const;
Vector3 cross(const Vector3& v) const;
friend std::ostream& operator<<(std::ostream& os, const Vector3& v){
os<<"{"<<v.x<<", "<<v.y<<", "<<v.z<<"}";
return os;
}
private:
double x,y,z;
};
}
#endif //VECTOR_VECTOR3_H

View File

@ -1,8 +1,9 @@
file(GLOB HEADER_LIST CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/include/Block/*.hpp") file(GLOB HEADER_LIST CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/include/Block/*")
add_library(block block.cpp ${HEADER_LIST}) add_library(block block.cpp vector3.cpp ${HEADER_LIST})
add_library(block::block ALIAS block)
target_link_libraries(block PRIVATE vector) target_link_libraries(block PUBLIC vector::vector)
target_include_directories(block PUBLIC "${PROJECT_SOURCE_DIR}/include") target_include_directories(block PUBLIC "${PROJECT_SOURCE_DIR}/include")

45
src/vector3.cpp Normal file
View File

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