From dd8c58214bc2dc0dc4faa719adb8296d803b7e23 Mon Sep 17 00:00:00 2001 From: stg05 Date: Sun, 17 Nov 2024 23:15:19 +0300 Subject: [PATCH 1/4] grid mistakes? --- week06/main.cpp | 227 ++++++++++++++++++++++++++---------------------- 1 file changed, 121 insertions(+), 106 deletions(-) diff --git a/week06/main.cpp b/week06/main.cpp index eae8dbe..e8b36f7 100644 --- a/week06/main.cpp +++ b/week06/main.cpp @@ -1,188 +1,203 @@ #include #include +#include template class Grid; -/* Specialization for 1-dim array */ -template -class Grid +template +class Grid { public: + static_assert(Dimension > 0, "Zero-dimensional array"); using value_type = T; using size_type = unsigned; private: - T *data; + Grid *m_subgrids; size_type dim_size; + // todo allocate + void mem_allocate(size_type size) + { + m_subgrids = static_cast *>(::operator new(sizeof(Grid) * size)); + } -public: - Grid(T &&value) + void mem_free() { - data = new T[1]; - dim_size = 1; - data[0] = value; + delete m_subgrids; } - Grid(size_type size, T const &fill_value) : dim_size{size} +public: + template 0), bool> = true> + Grid(const First first, const Rest... rest) : dim_size{(size_type)first} { - data = new T[size]; - for (size_type i = 0; i < size; i++) + mem_allocate(dim_size); + + for (size_type i = 0; i < first; i++) { - data[i] = fill_value; + new (m_subgrids + i) Grid(rest...); } } // Copy constructor - Grid(Grid const &other) + Grid(Grid const &other) { - size_type other_size = other.dim_size; - data = new T[other_size]; - dim_size = other_size; - for (size_type i = 0; i < other_size; i++) + dim_size = other.dim_size; + mem_allocate(dim_size); + + for (size_type i = 0; i < dim_size; i++) { - data[i] = other.data[i]; + m_subgrids[i] = Grid(other.m_subgrids[i]); + } + } + + // Move constructor + Grid(Grid &&other) + { + dim_size = other.dim_size; + mem_allocate(dim_size); + + for (size_type i = 0; i < dim_size; i++) + { + std::swap(m_subgrids[i], other.m_subgrids[i]); } } - const T &operator[](size_type rhs) + Grid &operator=(const Grid &rhs) + { + if (this == &rhs) + return *this; + // copy and swap + + if (this->dim_size != rhs.dim_size) + { + this->mem_free(); + this->dim_size = rhs.dim_size; + this->mem_allocate(this->dim_size); + } + std::copy(rhs.m_subgrids, rhs.m_subgrids + rhs.dim_size, this->m_subgrids); + return *this; + } + + Grid &operator=(Grid &&rhs) + { + if (this == &rhs) + return *this; + if (this->dim_size != rhs.dim_size) + { + this->mem_free(); + this->dim_size = rhs.dim_size; + this->mem_allocate(this->dim_size); + } + std::swap(this->m_subgrids, rhs.m_subgrids); + return *this; + } + + // Copying operator + // non const version + const Grid &operator[](size_type rhs) const { if ((rhs < 0) || (rhs >= dim_size)) throw std::out_of_range("Out of bounds"); - return data[rhs]; + return m_subgrids[rhs]; + } + + Grid &operator[](size_type rhs) + { + if ((rhs < 0) || (rhs >= dim_size)) + throw std::out_of_range("Out of bounds"); + return m_subgrids[rhs]; } ~Grid() { - // std::cout << "trying" << std::endl; - delete[] data; - // std::cout << "del data (Dim=1), size = " << dim_size << std::endl; + mem_free(); // #TODO почему не delete[]? (тогда в valgrind будет mismatch) } }; -template -class Grid +/* Specialization for 1-dim array */ +template +class Grid { public: - static_assert(Dimension > 0, "Zero-dimensional array"); using value_type = T; using size_type = unsigned; private: - Grid **m_subgrids; + T *data; size_type dim_size; + void mem_allocate(size_type size) + { + data = new T[size]; + } -public: - template - Grid(const First first, const Rest... rest) : dim_size{(size_type)first} + void mem_free() { - m_subgrids = static_cast **>(::operator new(sizeof(Grid) * first)); - for (size_type i = 0; i < first; i++) - { - m_subgrids[i] = new Grid(rest...); - } + delete[] data; } - // Copy constructor - Grid(Grid const &other) +public: + Grid(const T value) { - size_type other_size = other.dim_size; - dim_size = other_size; - m_subgrids = static_cast **>(::operator new(sizeof(Grid) * other_size)); + data = new T[1]; + dim_size = 1; + data[0] = value; + } - for (size_type i = 0; i < other_size; i++) + Grid(size_type size, T const &fill_value) : dim_size{size} + { + mem_allocate(size); + for (size_type i = 0; i < size; i++) { - m_subgrids[i] = new Grid(*(other.m_subgrids[i])); + data[i] = fill_value; } } - // Move constructor - Grid(Grid &&other) + // Copy constructor + Grid(Grid const &other) { - size_type other_size = other.dim_size; - dim_size = other_size; - m_subgrids = static_cast **>(::operator new(sizeof(Grid) * other_size)); + dim_size = other.dim_size; + mem_allocate(dim_size); - for (size_type i = 0; i < other_size; i++) + for (size_type i = 0; i < dim_size; i++) { - m_subgrids[i] = new Grid(*(other.m_subgrids[i])); + data[i] = other.data[i]; } } - Grid &operator=(Grid &rhs) + const T &operator[](size_type rhs) const { - return rhs; - } - Grid &operator=(Grid &&rhs) - { - return rhs; + if ((rhs < 0) || (rhs >= dim_size)) + throw std::out_of_range("Out of bounds"); + return data[rhs]; } - // Copying operator - - Grid &operator[] (size_type rhs) const + T &operator[](size_type rhs) { if ((rhs < 0) || (rhs >= dim_size)) throw std::out_of_range("Out of bounds"); - return *m_subgrids[rhs]; + return data[rhs]; } ~Grid() { - for (size_type i = 0; i < dim_size; i++) - { - delete m_subgrids[i]; - } - // std::cout << "Subgrids of dim " << Dimension - 1 << " del'd, proceeding to this of dim " - // << Dimension << " with " << dim_size << " subgrids." << std::endl; - - delete m_subgrids; // #TODO почему не delete[]? (тогда в valgrind будет mismatch) + // std::cout << "trying" << std::endl; + mem_free(); + // std::cout << "del data (Dim=1), size = " << dim_size << std::endl; } }; -// template -// class Grid final -// { -// public: -// using value_type = T; -// using size_type = unsigned; - -// private: -// T *data; -// size_type const m_y_size, m_x_size; - -// public: -// Grid(size_type y_size, size_type x_size) : m_y_size(y_size), m_x_size(x_size), data(new T[x_size * y_size]) -// { -// } - -// Grid(T const &t) -// { -// data = new T[1]; -// data[0] = t; -// } - -// // Copy constructor -// Grid(Grid const &) = delete; - -// // Move constructor -// Grid(Grid &&) = delete; - -// // Copy assginment -// Grid &operator=(Grid &) = delete; - -// // Move assignment -// Grid &operator=(Grid &&) = delete; - int main() { - Grid const g3(2, 3, 4, 1.00f); - assert(1.0f == g3[1][1][1]); + // Grid const g3(10, 10, 10, 1.00f); + // assert(1.0f == g3[1][1][1]); Grid g2(2, 5, 2.0f); assert(2.0f == g2[1][1]); - g2 = g3[1]; - assert(2.0f == g2[1][1]); + // g2 = g3[1]; + // assert(1.0f == g2[1][1]); + + // auto g1 = Grid(1u); return 0; } \ No newline at end of file From f5b80ccd53aa82ffc4bfedd277c804ed8a167aab Mon Sep 17 00:00:00 2001 From: stg05 Date: Tue, 19 Nov 2024 21:13:42 +0300 Subject: [PATCH 2/4] grid (done?): mem errors fixed, operator skobochka added --- week06/main | Bin 36872 -> 0 bytes week06/main.cpp | 118 +++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 102 insertions(+), 16 deletions(-) delete mode 100755 week06/main diff --git a/week06/main b/week06/main deleted file mode 100755 index 0be4fb1f39273d47317de147e71f97c4511ee479..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 36872 zcmeHwdwg6~_3u7&=47Thd8bX&rjJQzp-`GO>4QGfl0H%rXiMn>%5$2`Op+@C7+IPVbI|-0hz9vfg#{HO;#>+H6&!Z;jZh7bc_pCnZ}vUV$i|!VR1n7 zxzLTz<`>fODi8KK<#VAo9c^>)c%ANG(_lEoDR;M97)LLPW4h{Tn!kGCU|8nl=R((> z6vN+KM{Yixp~WZ{=BMj+^~r~A12>BhM~VQjGURn&_6YdoNI>RXBRqDc_$1*e`Of? ztA?S|FGBd`U(XIBALJqV*&T)Qg#aHwg1RB-cY$7xUbwkgN0tHSwH2_I-sbjCnygef z+|`>*gwvV!R3;o&;kI=v!_inO))h}@VySg2m-ZwRv32boJuw|Obj(@Np4FRsRrst` zYm+_cmN-IdGqYleXir;YZKk0i(%qg4XHxC)Ou8-7(y|7$nH}xvcqE)mXHv2D-nMn& zbu+>(Yv(`(2+1@wCi^qtWM??lp6H4#YiQY=6~21;nzhNb;T8}Q?K;mq_N5Iit51ob zXpyb$;Y@cbxy1>$_w*zq?U`6Owlxy#%fyojQM9oko=k_KPb`dXB-NhU7LF(48Dtsm z+PXE|7fYp+iT0j&W?Ojk45Wy*uckU{Gd=0H!z*269m+O4ELxqF*g~z>G3lUEdKHE? z(Qtbz)xIs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=g)_38I^fQyT{8b=#+$y|N$Qu5(=&8pO>tLw zW6ktq@ykztD`?EZm7iUgIO`MoxA)4g|C^(pIJ%|~Uzmq=Xz)B$L(SiB) z`=OKGt$!7A7#;Yp?Eh?D`f*M=7e*a%F38XS<-GLUo%AlekA?GGuK&2CHxM9;tN*Mz z9pl0#m(hWF;?!S3$IIr@Hpp_&NpSFZVmW`+SPpmfFd-2CHkD29en-S5y(nJ(<= zF%7bJH@yodBe6HVYIay>Xqd0Ro6Z$D6^Ti6^>1!=(z)p=+;cU1!VJ!E@^{#?vDFUY z=qyDCTyZX(=cLbLQ;VGR*X2dH>0EN_FzJ8lq`xpvf&BFAhDqPbfeqKQtX$8PpT1+5 z^rOzxiB5G5>&VDYpK#JU>Sl9_w(DSq8>yblP4B{O!=!(S8|=7lcl=Ld+E*BM({o?c z4ZA#q+qYdSEF{GTZhFUltOowOi0gl@Uvhsv&qR{Gj{_^Nf9CbS`T4V( TA1ZzCWs+R+-zdCS9;*HqsCC&( diff --git a/week06/main.cpp b/week06/main.cpp index e8b36f7..cd84904 100644 --- a/week06/main.cpp +++ b/week06/main.cpp @@ -5,7 +5,7 @@ template class Grid; -template +template // Todo operator skobochki class Grid { public: @@ -16,15 +16,23 @@ class Grid private: Grid *m_subgrids; size_type dim_size; - // todo allocate + + /** Allocating a buffer of a specific size for subgrids of inferior dimensions */ void mem_allocate(size_type size) { m_subgrids = static_cast *>(::operator new(sizeof(Grid) * size)); } + /** Deleting the buffer: firstly recursively remove objects placed into the buffer via + * placement news (by manually calling destructors), then freeing the buffer itself. */ void mem_free() { - delete m_subgrids; + for (size_type i = 0; i < dim_size; i++) + { + (m_subgrids + i)->~Grid(); + } + + ::operator delete(m_subgrids); } public: @@ -39,7 +47,7 @@ class Grid } } - // Copy constructor + /** Copy constructor (does not mutate the argument as may be seen from the signature) */ Grid(Grid const &other) { dim_size = other.dim_size; @@ -51,7 +59,7 @@ class Grid } } - // Move constructor + /** Move constructor (possibly takes rvalue as an argument) */ Grid(Grid &&other) { dim_size = other.dim_size; @@ -63,38 +71,47 @@ class Grid } } + /** Copy assignment */ Grid &operator=(const Grid &rhs) { + // Self-assignment check if (this == &rhs) + { return *this; - // copy and swap - + } + // Reallocating the buffer if only dim-sizes do not correspond if (this->dim_size != rhs.dim_size) { this->mem_free(); this->dim_size = rhs.dim_size; this->mem_allocate(this->dim_size); } - std::copy(rhs.m_subgrids, rhs.m_subgrids + rhs.dim_size, this->m_subgrids); + // Utilizing copy-construction [recursively] + for (size_type i = 0; i < this->dim_size; i++) + { + new (m_subgrids + i) Grid(rhs.m_subgrids[i]); + } return *this; } + /** Move assignment */ Grid &operator=(Grid &&rhs) { + // Self-assignment check if (this == &rhs) return *this; + // Reallocating the buffer if only dim-sizes do not correspond if (this->dim_size != rhs.dim_size) { this->mem_free(); this->dim_size = rhs.dim_size; this->mem_allocate(this->dim_size); } + // Merely swapping the contents std::swap(this->m_subgrids, rhs.m_subgrids); return *this; } - // Copying operator - // non const version const Grid &operator[](size_type rhs) const { if ((rhs < 0) || (rhs >= dim_size)) @@ -109,13 +126,61 @@ class Grid return m_subgrids[rhs]; } + /** Template for functor op.: returning one of recursively obtained subgrids */ + template + typename std::enable_if<(Dimension == sizeof...(Rest) + 1), T &>::type + operator()(First first, Rest... rest) + { + return this->m_subgrids[(size_type)first](rest...); + } + + /** Template for functor op.: returning value of the lowest-level subgrid */ + template + typename std::enable_if<(Dimension > sizeof...(Rest) + 1), Grid &>::type + operator()(First first, Rest... rest) + { + return this->m_subgrids[(size_type)first](rest...); + } + + /** Template for functor op.: finalizing the recursion for subgrid returning */ + template + typename std::enable_if<(Dimension > 1), Grid &>::type + operator()(First first) + { + return this->m_subgrids[(size_type)first]; + } + + /** Template for functor op.: returning one of recursively obtained subgrids */ + template + typename std::enable_if<(Dimension == sizeof...(Rest) + 1), const T &>::type + operator()(First first, Rest... rest) const + { + return this->m_subgrids[(size_type)first](rest...); + } + + /** Template for functor op.: returning value of the lowest-level subgrid */ + template + typename std::enable_if<(Dimension > sizeof...(Rest) + 1), const Grid &>::type + operator()(First first, Rest... rest) const + { + return this->m_subgrids[(size_type)first](rest...); + } + + /** Template for functor op.: finalizing the recursion for subgrid returning */ + template + typename std::enable_if<(Dimension > 1), const Grid &>::type + operator()(First first) const + { + return this->m_subgrids[(size_type)first]; + } + ~Grid() { mem_free(); // #TODO почему не delete[]? (тогда в valgrind будет mismatch) } }; -/* Specialization for 1-dim array */ +/** Specialization for 1-dim array */ template class Grid { @@ -179,6 +244,20 @@ class Grid return data[rhs]; } + /** Template for functor op.: finalizing the recursion for value returning */ + template + const T &operator()(First first) const + { + return this->data[(size_type)first]; + } + + /** Template for functor op.: finalizing the recursion for value returning */ + template + T &operator()(First first) + { + return this->data[(size_type)first]; + } + ~Grid() { // std::cout << "trying" << std::endl; @@ -189,15 +268,22 @@ class Grid int main() { - // Grid const g3(10, 10, 10, 1.00f); - // assert(1.0f == g3[1][1][1]); + Grid const g3(10, 10, 10, 1.00f); + assert(1.0f == g3[1][1][1]); Grid g2(2, 5, 2.0f); assert(2.0f == g2[1][1]); - // g2 = g3[1]; - // assert(1.0f == g2[1][1]); + std::cout << g3(1, 1, 1) << std::endl; + + g2(1, 1) = 2.5; + assert(g2(1, 1) == g2[1][1]); // Operator skokbochka (_, _, ... _) working same as [][] ... [] + assert(g2(1, 1) != g2(1, 0)); // [g2(1,1)=__2.5f__] != [g2(1,0)=__2.0f__] + g2 = g3[1]; + assert(1.0f == g2[1][1]); // g2(1,1) rewritten + + assert(&g3[1] != &g2); // g2 was copied properly - // auto g1 = Grid(1u); + auto g1 = Grid(1u); return 0; } \ No newline at end of file From 2b20ac0ee6a39f83f5e59771f387c5bdf2e7a5ed Mon Sep 17 00:00:00 2001 From: stg05 Date: Sat, 30 Nov 2024 05:19:02 +0300 Subject: [PATCH 3/4] raytracer updates --- .idea/.gitignore | 8 -------- .idea/.name | 1 - .idea/labs3sem.iml | 8 -------- .idea/misc.xml | 6 ------ .idea/modules.xml | 8 -------- .idea/vcs.xml | 6 ------ week02/raytracer/CMakeLists.txt | 4 ++-- week02/raytracer/fun/video_render.py | 12 ++++++++---- week02/raytracer/include/object.h | 6 +++--- week02/raytracer/include/ray.h | 25 +++++++++++++++++++++--- week02/raytracer/main.cpp | 3 +++ week02/raytracer/src/object.cpp | 14 ++++++++----- week02/raytracer/src/ray.cpp | 18 ++++++++++++++--- week02/raytracer/src/transformations.cpp | 2 +- week02/raytracer/src/utils.cpp | 5 +++-- 15 files changed, 66 insertions(+), 60 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/.name delete mode 100644 .idea/labs3sem.iml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 73f69e0..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml -# Editor-based HTTP Client requests -/httpRequests/ diff --git a/.idea/.name b/.idea/.name deleted file mode 100644 index 88d050b..0000000 --- a/.idea/.name +++ /dev/null @@ -1 +0,0 @@ -main \ No newline at end of file diff --git a/.idea/labs3sem.iml b/.idea/labs3sem.iml deleted file mode 100644 index e86c114..0000000 --- a/.idea/labs3sem.iml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 977d7ec..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index e315d18..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/week02/raytracer/CMakeLists.txt b/week02/raytracer/CMakeLists.txt index 52ef39c..d27763c 100644 --- a/week02/raytracer/CMakeLists.txt +++ b/week02/raytracer/CMakeLists.txt @@ -1,8 +1,8 @@ cmake_minimum_required(VERSION 3.20) -project(main) +project(raytracer) set(SOURCES main.cpp src/utils.cpp src/vec3.cpp src/transformations.cpp src/ray.cpp src/viewport.cpp include/viewport.h src/scene.cpp include/scene.h include/color.h src/color.cpp src/object.cpp) set(CMAKE_CXX_STANDARD 20) -add_executable(main ${SOURCES}) +add_executable(raytracer ${SOURCES}) diff --git a/week02/raytracer/fun/video_render.py b/week02/raytracer/fun/video_render.py index 7a2ce73..623d366 100644 --- a/week02/raytracer/fun/video_render.py +++ b/week02/raytracer/fun/video_render.py @@ -1,14 +1,18 @@ import os - +import time import cv2 -img1 = cv2.imread(r'C:/Users/stg05/CLionProjects/labs3sem/week02/raytracer/cmake-build-release/out/f1.ppm') +img1 = cv2.imread(r'/home/tunaspb/vscode/cpp3sem/build/out/f1.ppm') height, width, layers = img1.shape -video = cv2.VideoWriter(filename='video.mp4', fourcc=-1, frameSize=(width, height), +path = '/home/tunaspb/vscode/cpp3sem/build/out/' +path_vid = '/home/tunaspb/vscode/cpp3sem/build/vid/' + +cc4 = cv2.VideoWriter_fourcc(*'mp4v') +video = cv2.VideoWriter(filename=path_vid+time.asctime(time.localtime())+'.mp4', fourcc=cc4, frameSize=(width, height), fps=30) -path = '../cmake-build-release/out/' + filenames = [] for i in range(1, 1+len(os.listdir(path))): filenames.append('f' + str(i) + '.ppm') diff --git a/week02/raytracer/include/object.h b/week02/raytracer/include/object.h index 198858d..fae06ed 100644 --- a/week02/raytracer/include/object.h +++ b/week02/raytracer/include/object.h @@ -6,7 +6,7 @@ #include "ray.h" -/** General passive object included in scene \n +/** General passive object included in scene * e.g: refractors, mat objects, mirrors */ class object { public: @@ -15,7 +15,7 @@ class object { virtual ~object() = default; [[nodiscard]] virtual bool - hit(const ray &r, double ray_tmin, double ray_tmax, hit_record &record) const = 0; + hit(const ray &r, interval scope, hit_record &record) const = 0; }; class sphere : public object { @@ -24,7 +24,7 @@ class sphere : public object { sphere(vec3, double); - [[nodiscard]] bool hit(const ray &r, double ray_tmin, double ray_tmax, hit_record &record) const override; + [[nodiscard]] bool hit(const ray &r, interval scope, hit_record &record) const override; private: vec3 m_center{0, 0, 0}; diff --git a/week02/raytracer/include/ray.h b/week02/raytracer/include/ray.h index af96859..cbf424e 100644 --- a/week02/raytracer/include/ray.h +++ b/week02/raytracer/include/ray.h @@ -2,12 +2,16 @@ // Created by stg05 on 28.09.2024. // +#define COMP_PRECISION 0.00001 + #include "color.h" +#include #ifndef MAIN_RAY_H #define MAIN_RAY_H -class ray { +class ray +{ public: ray() = default; @@ -25,7 +29,8 @@ class ray { vec3 m_pivot{0, 0, 0}, m_dir{0, 0, 0}; }; -class hit_record { +class hit_record +{ public: hit_record() = default; @@ -40,4 +45,18 @@ class hit_record { bool m_front_face = false; }; -#endif //MAIN_RAY_H \ No newline at end of file +class interval +{ +public: + static const double inline INF = std::numeric_limits::infinity(); + +private: + double m_min, m_max; + +public: + interval() = default; + interval(double, double); + bool operator()(double); +}; + +#endif // MAIN_RAY_H \ No newline at end of file diff --git a/week02/raytracer/main.cpp b/week02/raytracer/main.cpp index 938fe35..2bb2b15 100644 --- a/week02/raytracer/main.cpp +++ b/week02/raytracer/main.cpp @@ -8,8 +8,11 @@ int main() scene s; sphere sph{10, 0, 0, 2}; sphere sph2{0, 0, -10, 2}; + sphere sph3{0, -100, 0, 99}; s.objects.push_front(&sph); s.objects.push_front(&sph2); + s.objects.push_front(&sph3); + for (double i = 5.0; i > 0.5; i /= 1.05) { s.v = viewport{vec3{1, 0, 0} * i}; diff --git a/week02/raytracer/src/object.cpp b/week02/raytracer/src/object.cpp index 119e82c..0fd3aa9 100644 --- a/week02/raytracer/src/object.cpp +++ b/week02/raytracer/src/object.cpp @@ -10,23 +10,27 @@ sphere::sphere(double x, double y, double z, double r) : object(), m_center{x, y sphere::sphere(vec3 center, double r) : object(), m_center{center}, m_r{r} {} -bool sphere::hit(const ray &r, double ray_tmin, double ray_tmax, hit_record &record) const { +bool sphere::hit(const ray &r, interval scope, hit_record &record) const +{ vec3 delta = m_center - r.get_pivot(); vec3 dir = r.get_dir(); vec3 proj = (delta * dir.e()) * (dir.e()); vec3 delta_per = delta - proj; bool result = (double(delta_per) < m_r); - if (!result) { + if (!result) + { return false; } double offset_from_center = std::sqrt(m_r * m_r - delta_per.length_squared()); vec3 intersection = proj - offset_from_center * dir.e(); - if ((double(intersection) > ray_tmax) || (double(intersection) < ray_tmin)) { + if (!scope(double(intersection)) || (intersection * dir < 0)) + { return false; - } else { + } + else + { vec3 normal_vec = (intersection - m_center).e(); record.set_face_normal(r, normal_vec); return true; } } - diff --git a/week02/raytracer/src/ray.cpp b/week02/raytracer/src/ray.cpp index a55094f..c739fb8 100644 --- a/week02/raytracer/src/ray.cpp +++ b/week02/raytracer/src/ray.cpp @@ -4,7 +4,6 @@ #include "../include/ray.h" - ray::ray(vec3 pivot, vec3 dir) : m_dir{dir}, m_pivot{pivot} {} vec3 ray::at(double t) { return vec3{m_pivot + m_dir * t}; } @@ -15,11 +14,24 @@ vec3 ray::get_dir() const { return m_dir; } ray::ray(vec3 dir) : m_dir{dir} {} -void hit_record::set_face_normal(const ray &r, const vec3 &outward_normal) { +void hit_record::set_face_normal(const ray &r, const vec3 &outward_normal) +{ m_normal = outward_normal; m_front_face = r.get_dir() * outward_normal < 0; } -vec3 hit_record::normal() const { +vec3 hit_record::normal() const +{ return m_normal; } + +interval::interval(double min, double max) +{ + this->m_min = min; + this->m_max = max; +} + +bool interval::operator()(double x) +{ + return (x > m_min - COMP_PRECISION) && (x < m_max + COMP_PRECISION); +} \ No newline at end of file diff --git a/week02/raytracer/src/transformations.cpp b/week02/raytracer/src/transformations.cpp index e39a9c7..170e9df 100644 --- a/week02/raytracer/src/transformations.cpp +++ b/week02/raytracer/src/transformations.cpp @@ -43,7 +43,7 @@ color color2bw(const color ¶m) { color ray_color(const ray &r, const scene &s) { hit_record hr{}; for(const auto* iter : s.objects){ - if (iter->hit(r, 0, 10000.0, hr)) { + if (iter->hit(r, interval{0, 10000}, hr)) { return color{.5, .5, .5} + 0.5*hr.normal(); } } diff --git a/week02/raytracer/src/utils.cpp b/week02/raytracer/src/utils.cpp index ae7837a..502cfa1 100644 --- a/week02/raytracer/src/utils.cpp +++ b/week02/raytracer/src/utils.cpp @@ -25,12 +25,13 @@ void write_color(std::ostream &out, const color &pixel_color) { } sequence_renderer::sequence_renderer() { - std::string command = "cd " + path + "&& del *.* /q /s"; + //std::string command = "cd " + path + "&& rm *.* /q /s"; + std::string command = "find ./out/ -type f -name 'f*' -delete"; system(command.c_str()); } void sequence_renderer::render_frame(const scene &s) const { - std::string file_path(path + "\\f" + std::to_string(frame_counter) + ".ppm"); + std::string file_path("./out/f" + std::to_string(frame_counter) + ".ppm"); frame_counter++; std::ofstream os(file_path); write_image(os, s); From 23fedee181022cad236cf288d448f26704ceac99 Mon Sep 17 00:00:00 2001 From: stg05 Date: Sat, 30 Nov 2024 05:24:09 +0300 Subject: [PATCH 4/4] Revert "raytracer updates" This reverts commit 2b20ac0ee6a39f83f5e59771f387c5bdf2e7a5ed. --- .idea/.gitignore | 8 ++++++++ .idea/.name | 1 + .idea/labs3sem.iml | 8 ++++++++ .idea/misc.xml | 6 ++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ week02/raytracer/CMakeLists.txt | 4 ++-- week02/raytracer/fun/video_render.py | 12 ++++-------- week02/raytracer/include/object.h | 6 +++--- week02/raytracer/include/ray.h | 25 +++--------------------- week02/raytracer/main.cpp | 3 --- week02/raytracer/src/object.cpp | 14 +++++-------- week02/raytracer/src/ray.cpp | 18 +++-------------- week02/raytracer/src/transformations.cpp | 2 +- week02/raytracer/src/utils.cpp | 5 ++--- 15 files changed, 60 insertions(+), 66 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/.name create mode 100644 .idea/labs3sem.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..88d050b --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +main \ No newline at end of file diff --git a/.idea/labs3sem.iml b/.idea/labs3sem.iml new file mode 100644 index 0000000..e86c114 --- /dev/null +++ b/.idea/labs3sem.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..977d7ec --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..e315d18 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/week02/raytracer/CMakeLists.txt b/week02/raytracer/CMakeLists.txt index d27763c..52ef39c 100644 --- a/week02/raytracer/CMakeLists.txt +++ b/week02/raytracer/CMakeLists.txt @@ -1,8 +1,8 @@ cmake_minimum_required(VERSION 3.20) -project(raytracer) +project(main) set(SOURCES main.cpp src/utils.cpp src/vec3.cpp src/transformations.cpp src/ray.cpp src/viewport.cpp include/viewport.h src/scene.cpp include/scene.h include/color.h src/color.cpp src/object.cpp) set(CMAKE_CXX_STANDARD 20) -add_executable(raytracer ${SOURCES}) +add_executable(main ${SOURCES}) diff --git a/week02/raytracer/fun/video_render.py b/week02/raytracer/fun/video_render.py index 623d366..7a2ce73 100644 --- a/week02/raytracer/fun/video_render.py +++ b/week02/raytracer/fun/video_render.py @@ -1,18 +1,14 @@ import os -import time + import cv2 -img1 = cv2.imread(r'/home/tunaspb/vscode/cpp3sem/build/out/f1.ppm') +img1 = cv2.imread(r'C:/Users/stg05/CLionProjects/labs3sem/week02/raytracer/cmake-build-release/out/f1.ppm') height, width, layers = img1.shape -path = '/home/tunaspb/vscode/cpp3sem/build/out/' -path_vid = '/home/tunaspb/vscode/cpp3sem/build/vid/' - -cc4 = cv2.VideoWriter_fourcc(*'mp4v') -video = cv2.VideoWriter(filename=path_vid+time.asctime(time.localtime())+'.mp4', fourcc=cc4, frameSize=(width, height), +video = cv2.VideoWriter(filename='video.mp4', fourcc=-1, frameSize=(width, height), fps=30) - +path = '../cmake-build-release/out/' filenames = [] for i in range(1, 1+len(os.listdir(path))): filenames.append('f' + str(i) + '.ppm') diff --git a/week02/raytracer/include/object.h b/week02/raytracer/include/object.h index fae06ed..198858d 100644 --- a/week02/raytracer/include/object.h +++ b/week02/raytracer/include/object.h @@ -6,7 +6,7 @@ #include "ray.h" -/** General passive object included in scene +/** General passive object included in scene \n * e.g: refractors, mat objects, mirrors */ class object { public: @@ -15,7 +15,7 @@ class object { virtual ~object() = default; [[nodiscard]] virtual bool - hit(const ray &r, interval scope, hit_record &record) const = 0; + hit(const ray &r, double ray_tmin, double ray_tmax, hit_record &record) const = 0; }; class sphere : public object { @@ -24,7 +24,7 @@ class sphere : public object { sphere(vec3, double); - [[nodiscard]] bool hit(const ray &r, interval scope, hit_record &record) const override; + [[nodiscard]] bool hit(const ray &r, double ray_tmin, double ray_tmax, hit_record &record) const override; private: vec3 m_center{0, 0, 0}; diff --git a/week02/raytracer/include/ray.h b/week02/raytracer/include/ray.h index cbf424e..af96859 100644 --- a/week02/raytracer/include/ray.h +++ b/week02/raytracer/include/ray.h @@ -2,16 +2,12 @@ // Created by stg05 on 28.09.2024. // -#define COMP_PRECISION 0.00001 - #include "color.h" -#include #ifndef MAIN_RAY_H #define MAIN_RAY_H -class ray -{ +class ray { public: ray() = default; @@ -29,8 +25,7 @@ class ray vec3 m_pivot{0, 0, 0}, m_dir{0, 0, 0}; }; -class hit_record -{ +class hit_record { public: hit_record() = default; @@ -45,18 +40,4 @@ class hit_record bool m_front_face = false; }; -class interval -{ -public: - static const double inline INF = std::numeric_limits::infinity(); - -private: - double m_min, m_max; - -public: - interval() = default; - interval(double, double); - bool operator()(double); -}; - -#endif // MAIN_RAY_H \ No newline at end of file +#endif //MAIN_RAY_H \ No newline at end of file diff --git a/week02/raytracer/main.cpp b/week02/raytracer/main.cpp index 2bb2b15..938fe35 100644 --- a/week02/raytracer/main.cpp +++ b/week02/raytracer/main.cpp @@ -8,11 +8,8 @@ int main() scene s; sphere sph{10, 0, 0, 2}; sphere sph2{0, 0, -10, 2}; - sphere sph3{0, -100, 0, 99}; s.objects.push_front(&sph); s.objects.push_front(&sph2); - s.objects.push_front(&sph3); - for (double i = 5.0; i > 0.5; i /= 1.05) { s.v = viewport{vec3{1, 0, 0} * i}; diff --git a/week02/raytracer/src/object.cpp b/week02/raytracer/src/object.cpp index 0fd3aa9..119e82c 100644 --- a/week02/raytracer/src/object.cpp +++ b/week02/raytracer/src/object.cpp @@ -10,27 +10,23 @@ sphere::sphere(double x, double y, double z, double r) : object(), m_center{x, y sphere::sphere(vec3 center, double r) : object(), m_center{center}, m_r{r} {} -bool sphere::hit(const ray &r, interval scope, hit_record &record) const -{ +bool sphere::hit(const ray &r, double ray_tmin, double ray_tmax, hit_record &record) const { vec3 delta = m_center - r.get_pivot(); vec3 dir = r.get_dir(); vec3 proj = (delta * dir.e()) * (dir.e()); vec3 delta_per = delta - proj; bool result = (double(delta_per) < m_r); - if (!result) - { + if (!result) { return false; } double offset_from_center = std::sqrt(m_r * m_r - delta_per.length_squared()); vec3 intersection = proj - offset_from_center * dir.e(); - if (!scope(double(intersection)) || (intersection * dir < 0)) - { + if ((double(intersection) > ray_tmax) || (double(intersection) < ray_tmin)) { return false; - } - else - { + } else { vec3 normal_vec = (intersection - m_center).e(); record.set_face_normal(r, normal_vec); return true; } } + diff --git a/week02/raytracer/src/ray.cpp b/week02/raytracer/src/ray.cpp index c739fb8..a55094f 100644 --- a/week02/raytracer/src/ray.cpp +++ b/week02/raytracer/src/ray.cpp @@ -4,6 +4,7 @@ #include "../include/ray.h" + ray::ray(vec3 pivot, vec3 dir) : m_dir{dir}, m_pivot{pivot} {} vec3 ray::at(double t) { return vec3{m_pivot + m_dir * t}; } @@ -14,24 +15,11 @@ vec3 ray::get_dir() const { return m_dir; } ray::ray(vec3 dir) : m_dir{dir} {} -void hit_record::set_face_normal(const ray &r, const vec3 &outward_normal) -{ +void hit_record::set_face_normal(const ray &r, const vec3 &outward_normal) { m_normal = outward_normal; m_front_face = r.get_dir() * outward_normal < 0; } -vec3 hit_record::normal() const -{ +vec3 hit_record::normal() const { return m_normal; } - -interval::interval(double min, double max) -{ - this->m_min = min; - this->m_max = max; -} - -bool interval::operator()(double x) -{ - return (x > m_min - COMP_PRECISION) && (x < m_max + COMP_PRECISION); -} \ No newline at end of file diff --git a/week02/raytracer/src/transformations.cpp b/week02/raytracer/src/transformations.cpp index 170e9df..e39a9c7 100644 --- a/week02/raytracer/src/transformations.cpp +++ b/week02/raytracer/src/transformations.cpp @@ -43,7 +43,7 @@ color color2bw(const color ¶m) { color ray_color(const ray &r, const scene &s) { hit_record hr{}; for(const auto* iter : s.objects){ - if (iter->hit(r, interval{0, 10000}, hr)) { + if (iter->hit(r, 0, 10000.0, hr)) { return color{.5, .5, .5} + 0.5*hr.normal(); } } diff --git a/week02/raytracer/src/utils.cpp b/week02/raytracer/src/utils.cpp index 502cfa1..ae7837a 100644 --- a/week02/raytracer/src/utils.cpp +++ b/week02/raytracer/src/utils.cpp @@ -25,13 +25,12 @@ void write_color(std::ostream &out, const color &pixel_color) { } sequence_renderer::sequence_renderer() { - //std::string command = "cd " + path + "&& rm *.* /q /s"; - std::string command = "find ./out/ -type f -name 'f*' -delete"; + std::string command = "cd " + path + "&& del *.* /q /s"; system(command.c_str()); } void sequence_renderer::render_frame(const scene &s) const { - std::string file_path("./out/f" + std::to_string(frame_counter) + ".ppm"); + std::string file_path(path + "\\f" + std::to_string(frame_counter) + ".ppm"); frame_counter++; std::ofstream os(file_path); write_image(os, s);