123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- /*
- * DoRayMe - a quick and dirty Raytracer
- * Group implementation
- *
- * Created by Manoël Trapier
- * Copyright (c) 2020 986-Studio.
- *
- */
- #include <tuple.h>
- #include <ray.h>
- #include <group.h>
- #include <math_helper.h>
- #include <renderstat.h>
- #include <string.h>
- #define MIN_ALLOC (2)
- Group::Group(const char *name) : Shape(Shape::GROUP)
- {
- stats.addGroup();
- this->allocatedObjectCount = MIN_ALLOC;
- this->objectList = (Shape **)calloc(sizeof(Shape **), MIN_ALLOC);
- this->objectCount = 0;
- this->allocatedUnboxableObjectCount = MIN_ALLOC;
- this->unboxableObjectList = (Shape **)calloc(sizeof(Shape **), MIN_ALLOC);
- this->unboxableObjectCount = 0;
- if (name != nullptr)
- {
- strncpy(this->name, name, 32);
- }
- else
- {
- strncpy(this->name, "untitled", 32);
- }
- }
- Intersect Group::intersect(Ray r)
- {
- Intersect ret;
- int i, j;
- if (this->objectCount > 0)
- {
- if (this->bounds.intesectMe(r))
- {
- for (i = 0 ; i < this->objectCount ; i++)
- {
- Intersect xs = this->objectList[i]->intersect(r);
- if (xs.count() > 0)
- {
- for (j = 0 ; j < xs.count() ; j++)
- {
- ret.add(xs[j]);
- }
- }
- }
- }
- }
- /* We are force to do them all the time */
- if (this->unboxableObjectCount > 0)
- {
- for(i = 0; i < this->unboxableObjectCount; i++)
- {
- Intersect xs = this->unboxableObjectList[i]->intersect(r);
- if (xs.count() > 0)
- {
- for(j = 0; j < xs.count(); j++)
- {
- ret.add(xs[j]);
- }
- }
- }
- }
- return ret;
- }
- bool Group::includes(Shape *b)
- {
- if (this->objectCount > 0)
- {
- int i;
- for (i = 0 ; i < this->objectCount ; i++)
- {
- if (this->objectList[i] == b)
- {
- return true;
- }
- }
- }
- /* We are force to do them all the time */
- if (this->unboxableObjectCount > 0)
- {
- int i;
- for(i = 0; i < this->unboxableObjectCount; i++)
- {
- if (this->unboxableObjectList[i] == b)
- {
- return true;
- }
- }
- }
- return false;
- }
- Intersect Group::localIntersect(Ray r)
- {
- return Intersect();
- }
- Tuple Group::localNormalAt(Tuple point, Intersection *hit)
- {
- return Vector(1, 0, 0);
- }
- /* ONLY INSERT SHAPES THAT ARE NOT GOING TO CHANGE ELSE..! */
- void Group::addObject(Shape *s)
- {
- if (s->haveFiniteBounds())
- {
- if ((this->objectCount + 1) > this->allocatedObjectCount)
- {
- this->allocatedObjectCount *= 2;
- this->objectList = (Shape **)realloc(this->objectList, sizeof(Shape **) * this->allocatedObjectCount);
- }
- s->parent = this;
- s->updateTransform();
- this->objectList[this->objectCount++] = s;
- this->bounds | s->getBounds();
- }
- else
- {
- if ((this->unboxableObjectCount + 1) > this->allocatedUnboxableObjectCount)
- {
- this->allocatedUnboxableObjectCount *= 2;
- this->unboxableObjectList = (Shape **)realloc(this->unboxableObjectList, sizeof(Shape **) * this->allocatedUnboxableObjectCount);
- }
- s->parent = this;
- s->updateTransform();
- this->unboxableObjectList[this->unboxableObjectCount++] = 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);
- }
- BoundingBox Group::getLocalBounds()
- {
- return this->bounds;
- }
- BoundingBox Group::getBounds()
- {
- if (this->bounds.isEmpty()) { this->updateBoundingBox(); }
- return this->bounds;
- }
- void Group::updateBoundingBox()
- {
- this->bounds.reset();
- if (this->objectCount > 0)
- {
- int i;
- for(i = 0; i < this->objectCount; i++)
- {
- if (this->objectList[i]->haveFiniteBounds())
- {
- BoundingBox objB = this->objectList[i]->getBounds();
- this->bounds | objB;
- }
- }
- }
- }
- void Group::updateTransform()
- {
- int i;
- Shape::updateTransform();
- if (this->objectCount > 0)
- {
- for (i = 0 ; i < this->objectCount ; i++)
- {
- this->objectList[i]->updateTransform();
- }
- }
- if (this->unboxableObjectCount > 0)
- {
- for(i = 0; i < this->unboxableObjectCount; i++)
- {
- this->unboxableObjectList[i]->updateTransform();
- }
- }
- /* Once the full stack being notified of the changes, let's update the
- * bounding box
- */
- this->updateBoundingBox();
- }
- void Group::dumpMe(FILE *fp)
- {
- int i;
- fprintf(fp, "\"Type\": \"Group\",\n");
- fprintf(fp, "\"Name\": \"%s\",\n", this->name);
- if (this->objectCount > 0)
- {
- fprintf(fp, "\"Objects\": {\n");
- for(i = 0; i < this->objectCount; i++)
- {
- fprintf(fp, "\"%d\": {\n", i);
- this->objectList[i]->dumpMe(fp);
- fprintf(fp, "},\n");
- }
- fprintf(fp, "},\n");
- }
- if (this->unboxableObjectCount > 0)
- {
- fprintf(fp, "\"UnboxableObjects\": {\n");
- for(i = 0; i < this->objectCount; i++)
- {
- fprintf(fp, "\"%d\": {\n", i);
- this->objectList[i]->dumpMe(fp);
- fprintf(fp, "},\n");
- }
- fprintf(fp, "},\n");
- }
- Shape::dumpMe(fp);
- }
|