fart/shapes/Union.cc

57 lines
1.5 KiB
C++

#include "Union.h"
#include <iostream>
using namespace std;
Union::Union(refptr<Shape> shape1, refptr<Shape> shape2)
: BoolShape(shape1, shape2)
{
}
Shape::IntersectionList Union::intersect(refptr<Shape> _this, const Ray & ray)
{
IntersectionList res1 = m_shape1->intersect(m_shape1, ray);
IntersectionList res2 = m_shape2->intersect(m_shape2, ray);
BoolIntersectionList merged(res1, res2, ray.getOrigin());
IntersectionList res;
bool in1 = false, in2 = false;
bool in_bool = false;
for (int i = 0, sz = merged.size(); i < sz; i++)
{
Vector normal = merged[i].intersection.normal;
double dot = - (ray.getDirection() % normal);
bool front = dot > 0.0;
bool left = merged[i].left;
if (front)
{
if (left)
in1 = true;
else
in2 = true;
if (!in_bool && (in1 || in2))
{
/* we found an intersection point with the boolean object */
in_bool = true;
res.add( merged[i].intersection );
}
}
else
{
if (left)
in1 = false;
else
in2 = false;
if (in_bool && !(in1 || in2))
{
/* we found an intersection point with the boolean object */
res.add( merged[i].intersection );
in_bool = false;
}
}
}
return res;
}