Skip to content
Snippets Groups Projects
Group.cpp 1.8 KiB
Newer Older
Postea's avatar
Postea committed
#include "Group.h"
Yoel's avatar
Yoel committed

#include "../material/Material.h"
#include "../tools/Vec3.h"
Postea's avatar
Postea committed

namespace shapes {
Group::Group(const util::Transformation& transform)
Yoel's avatar
Yoel committed
    : shapeList(std::vector<std::shared_ptr<Shape>>()), transform(transform) {
}
Group::Group(const util::Mat4& matrix)
Yoel's avatar
Yoel committed
    : shapeList(std::vector<std::shared_ptr<Shape>>()),
      transform(util::Transformation(matrix)) {
Postea's avatar
Postea committed

Yoel's avatar
Yoel committed
std::optional<cam::Hit> Group::intersect(const cam::Ray& r) const {
Yoel's avatar
Yoel committed
	cam::Ray imagR(transform.fromWorld.transformPoint(r.x0),
	               transform.fromWorld.transformDir(r.d), r.tmin, r.tmax,
	               r.normalize);
Postea's avatar
Postea committed

Yoel's avatar
Yoel committed
	std::optional<cam::Hit> result = std::nullopt;
Postea's avatar
Postea committed

Yoel's avatar
Yoel committed
	for (std::shared_ptr<Shape> s : shapeList) {
Yoel's avatar
Yoel committed
			std::optional<cam::Hit> temp = s->intersect(imagR);
			if (temp) {
				if (!result) {
Yoel's avatar
Yoel committed
				} else if (result->scalar() > temp->scalar()) {
Yoel's avatar
Yoel committed
			}
		}
	}
Yoel's avatar
Yoel committed
	if (result) {
		result = std::optional<cam::Hit>(
		    {transform.toWorld.transformPoint(result->hitpoint()),
		     transform.toWorldN.transformDir(result->normal()),
		     result->scalar(), result->material});
Yoel's avatar
Yoel committed
	}
	return result;
util::AxisAlignedBoundingBox Group::bounds() const {
void Group::add(const Group& group) {
	shapeList.push_back(std::make_shared<Group>(group));
	rebuildBoundingVolume();
}
Yoel's avatar
Yoel committed
void Group::add(const std::shared_ptr<Shape>& shape) {
	shapeList.push_back(shape);
void Group::rebuildBoundingVolume() {
	util::AxisAlignedBoundingBox bb = shapeList[0]->bounds();
	for (auto shape_bb : shapeList) {
		bb = bb + shape_bb->bounds();
	}
	boundingVolume = bb * transform.toWorld;
Group shapeGroup(util::Mat4& matrix, std::shared_ptr<Shape> shape) {
	Group g(matrix);
	g.add(shape);
	return g;
}
Yoel's avatar
Yoel committed
}  // namespace shapes