Change the Intersection to a class, and stop using memory allocation for it (and pointer)

A bit more clean (on the code side)
This commit is contained in:
Godzil
2020-02-17 11:48:29 +00:00
parent 513cd9d7eb
commit 1900d1f45d
6 changed files with 43 additions and 49 deletions

View File

@@ -15,14 +15,14 @@
class Intersect
{
private:
Intersection **list;
Intersection *list;
uint32_t num;
uint32_t allocated;
public:
Intersect();
void add(Intersection *i);
void add(Intersection i);
int count() { return this->num; };
Intersection *operator[](const int p) { return this->list[p]; }
Intersection operator[](const int p) { return this->list[p]; }
};
#endif //DORAYME_INTERSECT_H

View File

@@ -13,31 +13,14 @@
class Object;
struct Intersection
class Intersection
{
public:
double t;
Object *object;
public:
Intersection(double t, Object *object) : t(t), object(object) { };
};
static Intersection *newIntersection(double t, Object *object)
{
Intersection *ret = (Intersection *)calloc(sizeof(Intersection), 1);
if (ret != nullptr)
{
ret->t = t;
ret->object = object;
}
return ret;
}
static void freeIntersection(Intersection *i)
{
if ( i != nullptr )
{
free(i);
}
}
#endif //DORAYME_INTERSECTION_H

View File

@@ -15,16 +15,16 @@
Intersect::Intersect()
{
this->allocated = MIN_ALLOC;
this->list = (Intersection **)calloc(sizeof(Object *), MIN_ALLOC);
this->list = (Intersection *)calloc(sizeof(Object *), MIN_ALLOC);
this->num = 0;
}
void Intersect::add(Intersection *i)
void Intersect::add(Intersection i)
{
if ((this->num + 1) < this->allocated)
{
this->allocated *= 2;
this->list = (Intersection **)realloc(this->list, sizeof(Object *) * this->allocated);
this->list = (Intersection *)realloc(this->list, sizeof(Object *) * this->allocated);
}
this->list[this->num++] = i;
}

View File

@@ -27,8 +27,8 @@ Intersect Sphere::intersect(Ray r)
if (discriminant >= 0)
{
ret.add(newIntersection((-b - sqrt(discriminant)) / (2 * a), this));
ret.add(newIntersection((-b + sqrt(discriminant)) / (2 * a), this));
ret.add(Intersection((-b - sqrt(discriminant)) / (2 * a), this));
ret.add(Intersection((-b + sqrt(discriminant)) / (2 * a), this));
}
return ret;