Add a way to remove shapes from groups

This commit is contained in:
Godzil
2020-03-10 09:16:39 +00:00
parent add3d7c861
commit b89f9ec331
3 changed files with 70 additions and 3 deletions

View File

@@ -37,7 +37,10 @@ public:
bool isEmpty();
void addObject(Shape *s);
Shape *operator[](const int p) { return this->objectList[p]; }
void removeObject(Shape *s);
Shape *operator[](const int p) { return this->objectList[p]; };
Shape *getUnboxable(const int p) { return this->unboxableObjectList[p]; };
Intersect intersect(Ray r);
BoundingBox getLocalBounds();
@@ -48,7 +51,8 @@ public:
bool includes(Shape *b);
uint32_t getObjectCount() { return this->objectCount + this->unboxableObjectCount; };
uint32_t getObjectCount() { return this->objectCount; };
uint32_t getUnboxableCount() { return this->unboxableObjectCount; };
Group(const char *name = nullptr);

View File

@@ -149,6 +149,37 @@ void Group::addObject(Shape *s)
}
}
void Group::removeObject(Shape *s)
{
int i;
if (s->haveFiniteBounds())
{
for (i = 0; i < this->objectCount; i++)
{
if (this->objectList[i] == s)
{
this->objectCount --;
this->objectList[i] = this->objectList[this->objectCount];
this->objectList[this->objectCount] = nullptr;
break;
}
}
}
else
{
for (i = 0; i < this->unboxableObjectCount; i++)
{
if (this->unboxableObjectList[i] == s)
{
this->unboxableObjectCount --;
this->unboxableObjectList[i] = this->unboxableObjectList[this->unboxableObjectCount];
this->unboxableObjectList[this->unboxableObjectCount] = nullptr;
break;
}
}
}
}
bool Group::isEmpty()
{
return (this->objectCount == 0) && (this->unboxableObjectCount == 0);

View File

@@ -97,4 +97,36 @@ TEST(GroupTest, Group_bounding_box)
ASSERT_EQ(res.min, b.min);
ASSERT_EQ(res.max, b.max);
}
}
TEST(GroupTest, Removing_an_object)
{
Group g = Group();
Sphere s1 = Sphere();
Sphere s2 = Sphere();
Sphere s3 = Sphere();
s1.setTransform(translation(-1, 0, 0));
s2.setTransform(scaling(0.5, 0.5, 0.5));
g.addObject(&s1);
g.addObject(&s2);
ASSERT_EQ(g.getObjectCount(), 2);
ASSERT_EQ(*(g[0]), s1);
ASSERT_EQ(*(g[1]), s2);
g.removeObject(&s1);
ASSERT_EQ(g.getObjectCount(), 1);
ASSERT_EQ(*(g[0]), s2);
ASSERT_EQ(g[1], nullptr);
g.removeObject(&s3);
ASSERT_EQ(g.getObjectCount(), 1);
g.removeObject(&s2);
ASSERT_EQ(g.getObjectCount(), 0);
ASSERT_EQ(g[0], nullptr);
}