Skip to content
Snippets Groups Projects
Commit a226ac56 authored by Brandon Lai-Cheong's avatar Brandon Lai-Cheong
Browse files

init quadtree

parent e9f115dd
No related branches found
No related tags found
No related merge requests found
#pragma once
struct Triangle;
struct Box {
float minX, maxX, minY, maxY;
......
......@@ -3,29 +3,29 @@
#include <triangle.h>
#include <box.h>
enum Status {
Empty,
Single,
Quad
};
class QuadTree {
class AbstractQuadTree
{
protected:
Box b;
Status status;
QuadContent q;
AbstractQuadTree(Box b);
public:
void addTriangle(const Triangle &t);
QuadTree(Box b);
std::vector<Triangle> visibleSurface() const;
Triangle triangleIntersection(const Point &p) const;
virtual void addTriangle(const Triangle &t) = 0;
virtual std::vector<Triangle> visibleSurface() const = 0;
virtual Triangle triangleIntersection(const Point &p) const = 0;
};
class QuadTreeLeaf : AbstractQuadTree
{
Triangle t;
void addTriangle(const Triangle &t) override;
};
union QuadContent {
class QuadTree : AbstractQuadTree
{
std::vector<AbstractQuadTree> children;
Triangle t;
std::vector<QuadTree> children;
bool nothing;
QuadContent() {};
~QuadContent() {};
};
\ No newline at end of file
public:
QuadTree(Box b);
void addTriangle(const Triangle &t) override;
};
#include <quad_tree.h>
#include <triangulation.h>
QuadTree::QuadTree(Box b) : b{b}, status{Empty} {
q.nothing = false;
QuadTree::QuadTree(Box b) : AbstractQuadTree{b}
{
}
void QuadTree::addTriangle(const Triangle &t)
{
if (!b.isIn(t)) {
if (!b.isIn(t))
{
return;
}
switch (status)
{
case Empty:
status = Single;
q.t = t;
break;
case Single:
Triangle old = q.t;
status = Quad;
q.children = {
QuadTree(Box{b.minX, b.maxX / 2, b.minY, b.maxY}),
QuadTree(Box{b.maxX / 2, b.maxX, b.minY, b.maxY / 2}),
QuadTree(Box{b.maxX / 2, b.maxX, b.maxY / 2, b.maxY}),
QuadTree(Box{b.minX, b.maxX / 2, b.minY, b.maxY / 2})
};
// if the old and new triangle intersect, we want to avoid infinite additions to the tree.
// create new set of triangles that don't intersect
std::vector<Triangle> newTriangles = triangulate(old, t);
for (int i = 0; i < q.children.size(); i++) {
for (int j = 0; j < newTriangles.size(); i++) {
QuadTree(Box{b.maxX / 2, b.maxX, b.minY, b.maxY / 2}),
QuadTree(Box{b.maxX / 2, b.maxX, b.maxY / 2, b.maxY}),
QuadTree(Box{b.minX, b.maxX / 2, b.minY, b.maxY / 2})};
// if the old and new triangle intersect, we want to avoid infinite additions to the tree.
// create new set of triangles that don't intersect
std::vector<Triangle> newTriangles = triangulate(old, t);
for (int i = 0; i < q.children.size(); i++)
{
for (int j = 0; j < newTriangles.size(); i++)
{
q.children[i].addTriangle(newTriangles[i]);
}
}
for (int i = 0; i < q.children.size(); i++)
{
addTriangle(t);
}
}
std::vector<Triangle> QuadTree::visibleSurface() const
{
switch (status)
{
case Empty:
return {};
break;
case Single:
return {q.t};
case Quad:
for (int i = 0; i < q.children.size(); i++) {
addTriangle(t);
{
std::vector<Triangle> results;
for (const auto &t : q.children)
{
auto ts = t.visibleSurface();
results.insert(results.end(), ts.begin(), ts.end());
}
}
break;
default:
break;
}
}
std::vector<Triangle> QuadTree::visibleSurface() const {
// for every
}
\ No newline at end of file
#include <gtest/gtest.h>
#include <quad_tree.h>
TEST (QuadTreeTests, QuadTreeTest) {
QuadTree q{{0, 10, 0, 10}};
auto t1 = Triangle(Point{0,0}, Point{5,0}, Point{2,3}, 0);
q.addTriangle(t1);
EXPECT_TRUE(q.visibleSurface().size() > 0);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment