From 796b57fc9ad7eece40fdcc3d55ffb01c8c603d27 Mon Sep 17 00:00:00 2001
From: Yoel <s73017@beuth-hochschule.de>
Date: Mon, 7 Sep 2020 17:00:09 +0200
Subject: [PATCH] Groups can now only add other Groups. Added friend function
 that builds Groups with a single Shape in it

---
 RayTracer/shape/Group.cpp | 11 +++++++++++
 RayTracer/shape/Group.h   |  5 ++++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/RayTracer/shape/Group.cpp b/RayTracer/shape/Group.cpp
index 2ce3282..9d62d6e 100644
--- a/RayTracer/shape/Group.cpp
+++ b/RayTracer/shape/Group.cpp
@@ -31,6 +31,7 @@ std::shared_ptr<cam::Hit> Group::intersect(const cam::Ray& r) const {
 			}
 		}
 	}
+
 	if (result != nullptr) {
 		result = std::make_shared<cam::Hit>(
 		    cam::Hit(transform.toWorld.transformPoint(result->hit),
@@ -42,10 +43,15 @@ std::shared_ptr<cam::Hit> Group::intersect(const cam::Ray& r) const {
 util::AxisAlignedBoundingBox Group::bounds() const {
 	return boundingVolume;
 }
+void Group::add(const Group& group) {
+	shapeList.push_back(std::make_shared<Group>(group));
+	rebuildBoundingVolume();
+}
 void Group::add(const std::shared_ptr<Shape>& shape) {
 	shapeList.push_back(shape);
 	rebuildBoundingVolume();
 }
+
 void Group::rebuildBoundingVolume() {
 	util::AxisAlignedBoundingBox bb = shapeList[0]->bounds();
 	for (auto shape_bb : shapeList) {
@@ -53,5 +59,10 @@ void Group::rebuildBoundingVolume() {
 	}
 	boundingVolume = bb;
 }
+Group shapeGroup(util::Mat4& matrix, std::shared_ptr<Shape> shape) {
+	Group g(matrix);
+	g.add(shape);
+	return g;
+}
 
 }  // namespace shapes
\ No newline at end of file
diff --git a/RayTracer/shape/Group.h b/RayTracer/shape/Group.h
index 39eb7f1..e637bc3 100644
--- a/RayTracer/shape/Group.h
+++ b/RayTracer/shape/Group.h
@@ -14,7 +14,10 @@ class Group : public Shape {
 
 	std::shared_ptr<cam::Hit> intersect(const cam::Ray& r) const override;
 	util::AxisAlignedBoundingBox bounds() const override;
-	// protected:TODO
+	void add(const Group& group);
+	friend Group shapeGroup(util::Mat4& matrix, std::shared_ptr<Shape> shape);
+
+   protected:
 	void add(const std::shared_ptr<shapes::Shape>& shape);
 
    private:
-- 
GitLab