summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeuin <[email protected]>2022-04-11 10:07:48 +0800
committerKeuin <[email protected]>2022-04-11 10:07:48 +0800
commit00bfec66319e3e53a3b51d1d1ffbc899633f29a2 (patch)
tree6f2d2a6bf962a9b6d376847b03387d81723e7675
Basic 3d vector operations. Testing with GoogleTest.
-rw-r--r--.gitignore4
-rw-r--r--CMakeLists.txt32
-rw-r--r--main.cpp9
-rw-r--r--test.cpp15
-rw-r--r--vec.h74
5 files changed, 134 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..8a8a9e2
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+cmake-build*
+
+.code
+.idea \ No newline at end of file
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..a5c54e5
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,32 @@
+cmake_minimum_required(VERSION 3.0)
+project(rt)
+
+set(CMAKE_CXX_STANDARD 11)
+set(CMAKE_CXX_STANDARD_REQUIRED True)
+
+set(common_compiler_args "-Wall -Werror -Wno-unused -std=c++11 -pthread")
+set(CMAKE_CXX_FLAGS_DEBUG "${common_compiler_args} -g -ggdb -fsanitize=address")
+set(CMAKE_CXX_FLAGS_RELEASE "${common_compiler_args} -O2")
+
+set(CMAKE_VERBOSE_MAKEFILE on)
+
+# main executable
+
+add_executable(rt main.cpp vec.h)
+
+# googletest
+
+add_executable(all_tests test.cpp)
+
+target_link_libraries(all_tests gtest_main)
+include(FetchContent)
+FetchContent_Declare(
+ googletest
+ URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
+)
+
+FetchContent_MakeAvailable(googletest)
+
+include(GoogleTest)
+
+gtest_discover_tests(all_tests) \ No newline at end of file
diff --git a/main.cpp b/main.cpp
new file mode 100644
index 0000000..80cc4b4
--- /dev/null
+++ b/main.cpp
@@ -0,0 +1,9 @@
+/* rt - Threaded ray tracer in C++11.
+ * // TODO
+ */
+#include <iostream>
+
+int main() {
+ std::cout << "Hello, World!" << std::endl;
+ return 0;
+}
diff --git a/test.cpp b/test.cpp
new file mode 100644
index 0000000..b1e5199
--- /dev/null
+++ b/test.cpp
@@ -0,0 +1,15 @@
+//
+// Created by Keuin on 2022/4/11.
+//
+
+#include <gtest/gtest.h>
+
+#include "vec.h"
+
+TEST(Vec, VecAdd) {
+ vec3i a{1, 1, 1}, b{2, 2, 2}, c{3, 3, 3};
+ ASSERT_EQ(a + b, c);
+
+ vec3d d{1.1, 2.2, 3.3}, e{4.4, 5.5, 6.6}, f{5.5, 7.7, 9.9};
+ ASSERT_EQ(d + e, f);
+} \ No newline at end of file
diff --git a/vec.h b/vec.h
new file mode 100644
index 0000000..cd2a039
--- /dev/null
+++ b/vec.h
@@ -0,0 +1,74 @@
+//
+// Created by Keuin on 2022/4/11.
+//
+
+#include <cmath>
+
+static inline bool eq(int a, int b) {
+ return a == b;
+}
+
+static inline bool eq(double a, double b) {
+ // FIXME broken on large values
+ // https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
+ // https://stackoverflow.com/a/253874/14332799
+ const double c = a - b;
+ return c <= 1e-14 && c >= -1e-14;
+}
+
+// 3-dim vector
+template<typename T>
+struct vec3 {
+ T x;
+ T y;
+ T z;
+
+ vec3 operator+(vec3 &b) const {
+ return vec3{.x=x + b.x, .y=y + b.y, .z=z + b.z};
+ }
+
+ vec3 operator-() const {
+ return vec3{.x = -x, .y = -y, .z = -z};
+ }
+
+ vec3 operator-(vec3 &b) const {
+ return vec3{.x=x - b.x, .y=y - b.y, .z=z - b.z};
+ }
+
+ // cross product
+ vec3 operator*(vec3 &b) const {
+ return vec3{.x=y * b.z - z * b.y, .y=x * b.z - z * b.x, .z=x * b.y - y * b.x};
+ }
+
+ bool operator==(vec3 b) const {
+ return eq(x, b.x) && eq(y, b.y) && eq(z, b.z);
+ }
+
+ // dot product
+ int dot(vec3 &b) const {
+ return x * b.x + y * b.y + z * b.z;
+ }
+
+ // norm value
+ int norm(int level = 2) const {
+ if (level == 2) {
+ return std::abs(x * x + y * y + z * z);
+ } else if (level == 1) {
+ return std::abs(x + y + z);
+ } else {
+ return (int) std::abs(powl(x, level) + powl(y, level) + powl(z, level));
+ }
+ }
+};
+
+// 3-dim vector (int)
+using vec3i = vec3<int>;
+
+// 3-dim vector (long long)
+using vec3l = vec3<long long>;
+
+// 3-dim vector (float)
+using vec3f = vec3<float>;
+
+// 3-dim vector (double)
+using vec3d = vec3<double>;