From 3cfd5bec03b18c6ec08689c71aee039d6c430221 Mon Sep 17 00:00:00 2001 From: chatlanin Date: Thu, 24 Mar 2022 16:16:45 +0300 Subject: [PATCH] add vector --- bin/main.cpp | 24 +++++++++-- bin/meson.build | 3 +- src/logger/logger.hpp | 2 +- src/{matrix => math}/matrix.hpp | 0 src/{matrix => math}/meson.build | 6 +-- src/math/vector.hpp | 70 ++++++++++++++++++++++++++++++++ src/meson.build | 3 +- src/utils/utils.hpp | 2 +- src/vector/meson.build | 14 ------- src/vector/vector.hpp | 36 ---------------- 10 files changed, 97 insertions(+), 63 deletions(-) rename src/{matrix => math}/matrix.hpp (100%) rename src/{matrix => math}/meson.build (66%) create mode 100644 src/math/vector.hpp delete mode 100644 src/vector/meson.build delete mode 100644 src/vector/vector.hpp diff --git a/bin/main.cpp b/bin/main.cpp index 1f369f4..b44d787 100644 --- a/bin/main.cpp +++ b/bin/main.cpp @@ -10,8 +10,8 @@ #include "range/range.hpp" #include "container/container.hpp" #include "logger/logger.hpp" -#include "matrix/matrix.hpp" -#include "vector/vector.hpp" +#include "math/matrix.hpp" +#include "math/vector.hpp" #include @@ -110,7 +110,23 @@ int main(int argc, char *argv[]) // } {// ex: vector - hack::vector v3 { 1, 2, 3, }; - hack::log()(v3.get_value()); + hack::vector v3_1 { 8, 4, 9, }; + hack::vector v3_2 { 1, 2, 3, }; + hack::log()(v3_1.get_value()); + hack::log()(v3_2.get_value()); + + v3_1 = v3_2; + + hack::log()(v3_1.get_value()); + hack::log()(v3_2.get_value()); + + hack::log()("length 3", v3_2.length()); + + hack::vector v2_1 { 11, 22 }; + hack::log()("length 2", v2_1.length()); + + hack::vector lerp_1 { 1, 2, 3 }; + hack::vector lerp_2 { 5, 6, 7 }; + hack::log()("lerp", lerp_1.lerp(lerp_2, 0.75f)); } } diff --git a/bin/meson.build b/bin/meson.build index c13ffa9..7147830 100644 --- a/bin/meson.build +++ b/bin/meson.build @@ -6,8 +6,7 @@ deps += string_dep deps += range_dep deps += container_dep deps += logger_dep -deps += vector_dep -deps += matrix_dep +deps += math_dep executable( 'hack', 'main.cpp', diff --git a/src/logger/logger.hpp b/src/logger/logger.hpp index f4d881a..6ccec47 100644 --- a/src/logger/logger.hpp +++ b/src/logger/logger.hpp @@ -6,7 +6,7 @@ #include "concepts/concepts.hpp" #include "iterators/sequence_ostream_iterator.hpp" #include "iterators/associative_ostream_iterator.hpp" -#include "matrix/matrix.hpp" +#include "math/matrix.hpp" namespace hack { diff --git a/src/matrix/matrix.hpp b/src/math/matrix.hpp similarity index 100% rename from src/matrix/matrix.hpp rename to src/math/matrix.hpp diff --git a/src/matrix/meson.build b/src/math/meson.build similarity index 66% rename from src/matrix/meson.build rename to src/math/meson.build index 3a99718..af30931 100644 --- a/src/matrix/meson.build +++ b/src/math/meson.build @@ -1,14 +1,14 @@ -headers = ['matrix.hpp'] +headers = ['vector.hpp', 'matrix.hpp'] sources = [] lib = library( - 'matrix', + 'math', include_directories : inc, install : true, sources: [headers, sources] ) -matrix_dep = declare_dependency( +math_dep = declare_dependency( include_directories: inc, link_with: lib ) diff --git a/src/math/vector.hpp b/src/math/vector.hpp new file mode 100644 index 0000000..a0b8d37 --- /dev/null +++ b/src/math/vector.hpp @@ -0,0 +1,70 @@ +#pragma once + +#include +#include + +namespace hack +{ + template + struct generate_tuple + { + using type = decltype(std::tuple_cat(std::make_tuple(T{}), typename generate_tuple::type{})); + }; + + template + struct generate_tuple + { + using type = std::tuple; + }; +} + +namespace hack +{ + template + class vector + { + using value_t = decltype(typename generate_tuple::type{}); + + public: + vector(Args... args) : value_ { args... } {} + vector& operator=(const vector& v) + { + if (this == &v) return *this; + value_ = v.value_; + return *this; + } + + public: + value_t get_value() { return value_; }; + + auto length() + { + return std::sqrt(length_idx(std::make_index_sequence::value>{})); + } + + /* + dest.x = a.x + ((b.x - a.x) * t); + dest.y = a.y + ((b.y - a.y) * t); + */ + value_t lerp(vector& v, float t) + { + return lerp_idx(v.get_value(), t, std::make_index_sequence::value>{}); + } + + private: + template + auto length_idx(std::index_sequence) + { + return ((std::get(value_) * std::get(value_)) + ...); + } + + template + auto lerp_idx(const value_t& v, float t, std::index_sequence) + { + return std::make_tuple((std::get(value_) + (std::get(v) - std::get(value_)) * t)...); + } + + private: + value_t value_; + }; +} diff --git a/src/meson.build b/src/meson.build index f2b0581..f91c050 100644 --- a/src/meson.build +++ b/src/meson.build @@ -8,5 +8,4 @@ subdir('string') subdir('range') subdir('container') subdir('logger') -subdir('vector') -subdir('matrix') +subdir('math') diff --git a/src/utils/utils.hpp b/src/utils/utils.hpp index b671127..1cdbb2e 100644 --- a/src/utils/utils.hpp +++ b/src/utils/utils.hpp @@ -5,7 +5,7 @@ namespace hack::utils { - template + template struct generate_tuple { using type = decltype(std::tuple_cat(typename generate_tuple::type{}, std::make_tuple(T{}))); diff --git a/src/vector/meson.build b/src/vector/meson.build deleted file mode 100644 index 5ccc04d..0000000 --- a/src/vector/meson.build +++ /dev/null @@ -1,14 +0,0 @@ -headers = ['vector.hpp'] -sources = [] - -lib = library( - 'vector', - include_directories : inc, - install : true, - sources: [headers, sources] -) - -vector_dep = declare_dependency( - include_directories: inc, - link_with: lib -) diff --git a/src/vector/vector.hpp b/src/vector/vector.hpp deleted file mode 100644 index 81753d8..0000000 --- a/src/vector/vector.hpp +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#include - -namespace hack -{ - template - struct generate_tuple - { - using type = decltype(std::tuple_cat(std::make_tuple(T{}), typename generate_tuple::type{})); - }; - - template - struct generate_tuple - { - using type = std::tuple; - }; -} - -namespace hack -{ - template - class vector - { - using value_t = decltype(typename generate_tuple::type{}); - - public: - vector(Args... args) : value_ { args... } {} - - public: - value_t get_value() { return value_; }; - - private: - value_t value_; - }; -}