Skip to content

Commit

Permalink
octree
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenxiZhou0619 committed Mar 11, 2023
1 parent 06669ee commit d912ce9
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 7 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ add_executable(Moer
src/FunctionLayer/Acceleration/Acceleration.cpp
src/FunctionLayer/Acceleration/EmbreeBVH.cpp
src/FunctionLayer/Acceleration/Linear.cpp
src/FunctionLayer/Acceleration/Octree.cpp

src/FunctionLayer/Integrator/NormalIntegrator.cpp
src/FunctionLayer/Integrator/DirectIntegrator.cpp
Expand Down
16 changes: 15 additions & 1 deletion src/FunctionLayer/Acceleration/AABB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ void AABB::Expand(const Point3f &other) {
pMax = maxP(pMax, other);
}

bool AABB::rayIntersect(const Ray &ray, float *tMin, float *tMax) const {
bool AABB::Overlap(const AABB &other) const {
for (int dim = 0; dim < 3; ++dim) {
if (pMin[dim] > other.pMax[dim] || pMax[dim] < other.pMin[dim]) {
return false;
}
}
return true;
}

bool AABB::RayIntersect(const Ray &ray, float *tMin, float *tMax) const {
float tNear = ray.tNear, tFar = ray.tFar;
for (int i = 0; i < 3; ++i) {
float invDir = 1.f / ray.direction[i];
Expand All @@ -49,4 +58,9 @@ bool AABB::rayIntersect(const Ray &ray, float *tMin, float *tMax) const {
if (tMax)
*tMax = tFar;
return true;
}

Point3f AABB::Center() const {
return Point3f{(pMin[0] + pMax[0]) * .5f, (pMin[1] + pMax[1]) * .5f,
(pMin[2] + pMax[2]) * .5f};
}
6 changes: 5 additions & 1 deletion src/FunctionLayer/Acceleration/AABB.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ class AABB {

void Expand(const Point3f &other);

bool rayIntersect(const Ray &ray, float *tMin = nullptr,
bool Overlap(const AABB &other) const;

bool RayIntersect(const Ray &ray, float *tMin = nullptr,
float *tMax = nullptr) const;

Point3f Center() const;

public:
Point3f pMin, pMax;
};
9 changes: 6 additions & 3 deletions src/FunctionLayer/Acceleration/Acceleration.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#include "Acceleration.h"
#include "EmbreeBVH.h"
#include "Linear.h"

#include "Octree.h"
// Default acceleration type is embree
AccelerationType Acceleration::type = AccelerationType::Embree;

std::map<std::string, AccelerationType> accelerationTypeMap = {
{"embree", AccelerationType::Embree}, {"linear", AccelerationType::Linear}};
{"embree", AccelerationType::Embree},
{"linear", AccelerationType::Linear},
{"octree", AccelerationType::Octree}};

std::map<AccelerationType, std::function<std::shared_ptr<Acceleration>()>>
accelerationBuildMap = {
{AccelerationType::Embree, std::make_shared<EmbreeBVH>},
{AccelerationType::Linear, std::make_shared<LinearAcceleration>}};
{AccelerationType::Linear, std::make_shared<LinearAcceleration>},
{AccelerationType::Octree, std::make_shared<OctreeAcceleration>}};

void Acceleration::setAccelerationType(std::string type) {
if (accelerationTypeMap.count(type) == 0) {
Expand Down
4 changes: 3 additions & 1 deletion src/FunctionLayer/Acceleration/Acceleration.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <optional>
#include <vector>

enum class AccelerationType { Embree, Linear };
enum class AccelerationType { Embree, Linear, Octree };

//* 所有空间加速结构的基类
class Acceleration {
Expand Down Expand Up @@ -42,6 +42,8 @@ class Acceleration {
public:
static AccelerationType type;

AABB boundingBox;

protected:
std::vector<std::shared_ptr<Shape>> shapes; //* 场景中的所有几何体
};
39 changes: 39 additions & 0 deletions src/FunctionLayer/Acceleration/Octree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once
#include "Acceleration.h"

// MAX_LEAF_SIZE表示叶节点中至多包含的shape的个数
// 若构建时,某个节点的Shape个数多于MAX_LEAF_SIZE,那么就会对该节点继续划分

#define MAX_LEAF_SIZE 64

struct OctreeNode {
AABB boundingBox; // 该节点的包围盒

std::shared_ptr<OctreeNode> subNodes[8]; // 该节点的八个子节点

// primCount设为-1时表示该节点不是叶节点,primCount为正值时表示叶节点有多少个shape
int primCount = -1;

int primIdxBuffer[MAX_LEAF_SIZE];
};

//* 八叉树 Octree

class OctreeAcceleration : public Acceleration {
public:
OctreeAcceleration();

virtual ~OctreeAcceleration();

virtual void build() override;

virtual bool rayIntersect(Ray &ray, int *geomID, int *primID, float *u,
float *v) const override;

protected:
std::shared_ptr<OctreeNode>
recursiveBuild(const AABB &aabb, const std::vector<int> &primIdxBuffer);

protected:
std::shared_ptr<OctreeNode> root;
};
4 changes: 3 additions & 1 deletion src/FunctionLayer/Shape/Triangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void TriangleMesh::fillIntersection(float distance, int primID, float u,
}

void TriangleMesh::initInternalAcceleration() {
acceleration = std::make_shared<LinearAcceleration>();
acceleration = Acceleration::createAcceleration();
int primCount = meshData->faceCount;
for (int primID = 0; primID < primCount; ++primID) {
int vtx0Idx = meshData->faceBuffer[primID][0].vertexIndex,
Expand All @@ -168,5 +168,7 @@ void TriangleMesh::initInternalAcceleration() {
acceleration->attachShape(triangle);
}
acceleration->build();
// TriangleMesh的包围盒就是其内部加速结构的包围盒
boundingBox = acceleration->boundingBox;
}
REGISTER_CLASS(TriangleMesh, "triangle")

0 comments on commit d912ce9

Please sign in to comment.