ArchitectureColoredPainting/ArchitectureColoredPainting/BvhTree.h

87 lines
1.9 KiB
C
Raw Normal View History

#pragma once
#include <QOpenGLTexture>
#include <QOpenGLContext>
#include <QTextCodec>
#include <algorithm>
#include <QVector4D>
#include <iostream>
#include <memory>
#include <vector>
#include <cmath>
2022-09-06 22:16:04 +08:00
using std::tr1::shared_ptr;
struct BvhTreeData {
QVector4D bound;
GLuint leftSon, rightSon;
void show() {
printf("Bvh: (%lf, %lf) (%lf, %lf): %d %d\n",
bound.x(), bound.y(), bound.z(), bound.w(),
leftSon, rightSon);
}
BvhTreeData()
: leftSon(0), rightSon(0) {}
~BvhTreeData() {}
};
// BvhTree <20>ڵ<EFBFBD>
struct BvhNode {
GLuint lab;
bool isLeaf;
QVector4D bound;
2022-09-06 22:16:04 +08:00
shared_ptr<BvhNode> child[2];
static bool x_compare(BvhTreeData a, BvhTreeData b) {
return a.bound.x() < b.bound.x();
}
static bool y_compare(BvhTreeData a, BvhTreeData b) {
return a.bound.y() < b.bound.y();
}
GLuint getLeftSon(int k) {
if (isLeaf) {
return 0x80000000+child[0]->lab;
}
return child[0]->lab;
}
GLuint getRightSon(int k) {
if (isLeaf) {
return child[1]->lab;
}
return child[1]->lab;
}
BvhNode() {
child[0] = child[1] = NULL;
isLeaf = false;
lab = 0;
}
int maximumBound() {
return (std::fabs(bound.x() - bound.w()) < std::fabs(bound.y() - bound.z()));
}
~BvhNode() {}
};
typedef std::tr1::shared_ptr<BvhNode> BvhPtr;
2022-09-06 22:16:04 +08:00
class BvhTree
{
2022-09-06 22:16:04 +08:00
private:
GLuint tot;
BvhPtr root;
static QVector4D calcBound(BvhTreeData initBound[], int l, int r);
static QVector4D Union(QVector4D a, QVector4D b);
BvhPtr subBvhTree(BvhTreeData initBound[], int l, int r);
void traverseBvhTree(BvhPtr now, std::vector<GLuint>& children, std::vector<QVector4D>& bounds);
public:
BvhTree() {
tot = 0;
root = NULL;
}
// <20><><EFBFBD>ݵײ<DDB5><D7B2><EFBFBD>Χ<EFBFBD><CEA7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>bvh<76><68>
void buildBvhTree(BvhTreeData initBound[], int len);
// <20><><EFBFBD><EFBFBD> Bvh <20><>rootBvh<76><68><EFBFBD>֣<EFBFBD><D6A3><EFBFBD> children <20><><EFBFBD><EFBFBD><E9A3AC>Χ<EFBFBD><CEA7><EFBFBD><EFBFBD><EFBFBD><EFBFBD> vector <20><><EFBFBD><EFBFBD>
void getBvhArray(std::vector<GLuint>& children, std::vector<QVector4D>& bounds);
// <20><><EFBFBD><EFBFBD> BvhTree <20>нڵ<D0BD><DAB5><EFBFBD><EFBFBD><EFBFBD>
GLuint getBvhNodeNum();
};