From ace7d53571b0d1131b9a7d0d8b9ca9b15135669e Mon Sep 17 00:00:00 2001 From: Godzil Date: Mon, 2 Mar 2020 08:24:09 +0000 Subject: [PATCH] Clearing up some memory to prevent stupid issues Preparing for some optimisations. (absolutely need to reduce the ammount of allocations done.) --- source/include/intersect.h | 1 + source/include/list.h | 8 ++++++++ source/intersect.cpp | 11 +++++++++++ source/intersection.cpp | 7 ++++--- source/matrix.cpp | 1 - 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/source/include/intersect.h b/source/include/intersect.h index 52d2686..6011082 100644 --- a/source/include/intersect.h +++ b/source/include/intersect.h @@ -21,6 +21,7 @@ class Intersect public: Intersect(); ~Intersect(); + void reset(); void add(Intersection i); int count() { return this->num; }; Intersection operator[](const int p) { return *this->list[p]; } diff --git a/source/include/list.h b/source/include/list.h index b3ad96d..c49538d 100644 --- a/source/include/list.h +++ b/source/include/list.h @@ -31,6 +31,14 @@ class List if (p == nullptr) { return; } /* clear up the list */ + do + { + ChainList *next = p->next; + free(p); + p = next; + } + while(p != nullptr); + } Shape *last() diff --git a/source/intersect.cpp b/source/intersect.cpp index a6ccf4a..4d25979 100644 --- a/source/intersect.cpp +++ b/source/intersect.cpp @@ -31,10 +31,20 @@ Intersect::Intersect() Intersect::~Intersect() { + int i; + for(i = 0; i < this->num; i++) + { + free(this->list[i]); + } /* Free stuff */ free(this->list); } +void Intersect::reset() +{ + this->num = 0; +} + void Intersect::add(Intersection i) { Intersection *x; @@ -46,6 +56,7 @@ void Intersect::add(Intersection i) stats.addRealloc(); this->list = (Intersection **)realloc(this->list, sizeof(Intersection *) * this->allocated); } + this->list[this->num++] = new Intersection(i.t, i.object); stats.setMaxIntersect(this->num); diff --git a/source/intersection.cpp b/source/intersection.cpp index 09ddd23..cd04c4c 100644 --- a/source/intersection.cpp +++ b/source/intersection.cpp @@ -30,14 +30,15 @@ Computation Intersection::prepareComputation(Ray r, Intersect *xs) Tuple underHitP = hitP - normalV * getEpsilon(); Tuple reflectV = r.direction.reflect(normalV); - if (xs != nullptr) + /* If the hit object is not transparent, there is no need to do that. I think .*/ + if ((xs != nullptr) && (xs->hit().object->material.transparency > 0)) { List containers; int j, k; - for(j = 0; j < xs->count(); j++) + for (j = 0 ; j < xs->count() ; j++) { - Intersection i = (*xs)[j]; + Intersection i = ( *xs )[j]; if (*this == i) { if (!containers.isEmpty()) diff --git a/source/matrix.cpp b/source/matrix.cpp index b67e4ea..801a7ee 100644 --- a/source/matrix.cpp +++ b/source/matrix.cpp @@ -125,7 +125,6 @@ Matrix Matrix::transpose() int x, y; Matrix ret = Matrix(this->size); - #pragma omp parallel for simd private(y, x) for (y = 0 ; y < this->size ; y++) { for (x = 0 ; x < this->size ; x++)