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:
@@ -15,14 +15,14 @@
|
|||||||
class Intersect
|
class Intersect
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
Intersection **list;
|
Intersection *list;
|
||||||
uint32_t num;
|
uint32_t num;
|
||||||
uint32_t allocated;
|
uint32_t allocated;
|
||||||
public:
|
public:
|
||||||
Intersect();
|
Intersect();
|
||||||
void add(Intersection *i);
|
void add(Intersection i);
|
||||||
int count() { return this->num; };
|
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
|
#endif //DORAYME_INTERSECT_H
|
||||||
|
|||||||
@@ -13,31 +13,14 @@
|
|||||||
|
|
||||||
class Object;
|
class Object;
|
||||||
|
|
||||||
struct Intersection
|
class Intersection
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
double t;
|
double t;
|
||||||
Object *object;
|
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
|
#endif //DORAYME_INTERSECTION_H
|
||||||
|
|||||||
@@ -15,16 +15,16 @@
|
|||||||
Intersect::Intersect()
|
Intersect::Intersect()
|
||||||
{
|
{
|
||||||
this->allocated = MIN_ALLOC;
|
this->allocated = MIN_ALLOC;
|
||||||
this->list = (Intersection **)calloc(sizeof(Object *), MIN_ALLOC);
|
this->list = (Intersection *)calloc(sizeof(Object *), MIN_ALLOC);
|
||||||
this->num = 0;
|
this->num = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Intersect::add(Intersection *i)
|
void Intersect::add(Intersection i)
|
||||||
{
|
{
|
||||||
if ((this->num + 1) < this->allocated)
|
if ((this->num + 1) < this->allocated)
|
||||||
{
|
{
|
||||||
this->allocated *= 2;
|
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;
|
this->list[this->num++] = i;
|
||||||
}
|
}
|
||||||
@@ -27,8 +27,8 @@ Intersect Sphere::intersect(Ray r)
|
|||||||
|
|
||||||
if (discriminant >= 0)
|
if (discriminant >= 0)
|
||||||
{
|
{
|
||||||
ret.add(newIntersection((-b - sqrt(discriminant)) / (2 * a), this));
|
ret.add(Intersection((-b - sqrt(discriminant)) / (2 * a), this));
|
||||||
ret.add(newIntersection((-b + sqrt(discriminant)) / (2 * a), this));
|
ret.add(Intersection((-b + sqrt(discriminant)) / (2 * a), this));
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|||||||
@@ -17,35 +17,46 @@ TEST(IntersectTest, Creating_an_intersect_and_do_some_check)
|
|||||||
|
|
||||||
ASSERT_EQ(i.count(), 0);
|
ASSERT_EQ(i.count(), 0);
|
||||||
|
|
||||||
i.add(newIntersection(1.0, nullptr));
|
i.add(Intersection(1.0, nullptr));
|
||||||
i.add(newIntersection(4.2, nullptr));
|
i.add(Intersection(4.2, nullptr));
|
||||||
|
|
||||||
ASSERT_EQ(i.count(), 2);
|
ASSERT_EQ(i.count(), 2);
|
||||||
|
|
||||||
ASSERT_EQ(i[0]->t, 1.0);
|
ASSERT_EQ(i[0].t, 1.0);
|
||||||
ASSERT_EQ(i[1]->t, 4.2);
|
ASSERT_EQ(i[1].t, 4.2);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(IntersectTest, An_intersection_encapsulate_t_and_object)
|
TEST(IntersectTest, An_intersection_encapsulate_t_and_object)
|
||||||
{
|
{
|
||||||
Sphere s = Sphere();
|
Sphere s = Sphere();
|
||||||
Intersection *i = newIntersection(3.5, &s);
|
Intersection i = Intersection(3.5, &s);
|
||||||
|
|
||||||
ASSERT_EQ(i->t, 3.5);
|
ASSERT_EQ(i.t, 3.5);
|
||||||
ASSERT_EQ(i->object, (Object *)&s);
|
ASSERT_EQ(i.object, (Object *)&s);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(IntersectTest, Aggregating_intersections)
|
TEST(IntersectTest, Aggregating_intersections)
|
||||||
{
|
{
|
||||||
Sphere s = Sphere();
|
Sphere s = Sphere();
|
||||||
Intersection *i1 = newIntersection(1, &s);
|
Intersection i1 = Intersection(1, &s);
|
||||||
Intersection *i2 = newIntersection(2, &s);
|
Intersection i2 = Intersection(2, &s);
|
||||||
|
|
||||||
Intersect xs = Intersect();
|
Intersect xs = Intersect();
|
||||||
xs.add(i1);
|
xs.add(i1);
|
||||||
xs.add(i2);
|
xs.add(i2);
|
||||||
|
|
||||||
ASSERT_EQ(xs.count(), 2);
|
ASSERT_EQ(xs.count(), 2);
|
||||||
ASSERT_EQ(xs[0]->t, 1);
|
ASSERT_EQ(xs[0].t, 1);
|
||||||
ASSERT_EQ(xs[1]->t, 2);
|
ASSERT_EQ(xs[1].t, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(IntersectTest, Intersect_sets_the_object_on_the_intersection)
|
||||||
|
{
|
||||||
|
Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
|
||||||
|
Sphere s = Sphere();
|
||||||
|
Intersect xs = s.intersect(r);
|
||||||
|
|
||||||
|
ASSERT_EQ(xs.count(), 2);
|
||||||
|
ASSERT_EQ(xs[0].object, (Object *)&s);
|
||||||
|
ASSERT_EQ(xs[1].object, (Object *)&s);
|
||||||
}
|
}
|
||||||
@@ -18,8 +18,8 @@ TEST(SphereTest, A_ray_intersect_a_sphere_at_two_points)
|
|||||||
Intersect xs = s.intersect(r);
|
Intersect xs = s.intersect(r);
|
||||||
|
|
||||||
ASSERT_EQ(xs.count(), 2);
|
ASSERT_EQ(xs.count(), 2);
|
||||||
ASSERT_EQ(xs[0]->t, 4.0);
|
ASSERT_EQ(xs[0].t, 4.0);
|
||||||
ASSERT_EQ(xs[1]->t, 6.0);
|
ASSERT_EQ(xs[1].t, 6.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(SphereTest, A_ray_intersect_a_sphere_at_a_tangent)
|
TEST(SphereTest, A_ray_intersect_a_sphere_at_a_tangent)
|
||||||
@@ -29,8 +29,8 @@ TEST(SphereTest, A_ray_intersect_a_sphere_at_a_tangent)
|
|||||||
Intersect xs = s.intersect(r);
|
Intersect xs = s.intersect(r);
|
||||||
|
|
||||||
ASSERT_EQ(xs.count(), 2);
|
ASSERT_EQ(xs.count(), 2);
|
||||||
ASSERT_EQ(xs[0]->t, 5.0);
|
ASSERT_EQ(xs[0].t, 5.0);
|
||||||
ASSERT_EQ(xs[1]->t, 5.0);
|
ASSERT_EQ(xs[1].t, 5.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(SphereTest, A_ray_miss_a_sphere)
|
TEST(SphereTest, A_ray_miss_a_sphere)
|
||||||
@@ -49,8 +49,8 @@ TEST(SphereTest, A_ray_originate_inside_a_sphere)
|
|||||||
Intersect xs = s.intersect(r);
|
Intersect xs = s.intersect(r);
|
||||||
|
|
||||||
ASSERT_EQ(xs.count(), 2);
|
ASSERT_EQ(xs.count(), 2);
|
||||||
ASSERT_EQ(xs[0]->t, -1.0);
|
ASSERT_EQ(xs[0].t, -1.0);
|
||||||
ASSERT_EQ(xs[1]->t, 1.0);
|
ASSERT_EQ(xs[1].t, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(SphereTest, A_sphere_is_behind_a_ray)
|
TEST(SphereTest, A_sphere_is_behind_a_ray)
|
||||||
@@ -60,6 +60,6 @@ TEST(SphereTest, A_sphere_is_behind_a_ray)
|
|||||||
Intersect xs = s.intersect(r);
|
Intersect xs = s.intersect(r);
|
||||||
|
|
||||||
ASSERT_EQ(xs.count(), 2);
|
ASSERT_EQ(xs.count(), 2);
|
||||||
ASSERT_EQ(xs[0]->t, -6.0);
|
ASSERT_EQ(xs[0].t, -6.0);
|
||||||
ASSERT_EQ(xs[1]->t, -4.0);
|
ASSERT_EQ(xs[1].t, -4.0);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user