- 下面介绍一种简单易用的分割方法:分割步骤:一、将空间中的所有的面,加入的根部节点。二、遍历根部节点的所有面,分别找到x、y、z的最大最小值,给根节点指定一个合适的包围空间。三、在这个节点的包围空间里找到最长轴、并按这个最长轴的中间点做为新的分割点,遍历该节点的所有面将其分在其左右子节点中。四、返回第2步,直到达到一定限制条件,结束,此时空间已经被分开了。当然闲话不说,贴上实现模拟代码:
- #include <map>
- #include <vector>
- #include <iostream>
- using namespace std;
- struct point
- {
- float x,y,z;
- point():x(0.0f),y(0.0f),z(0.0f){};
- point(float a,float b,float c):x(a),y(b),z(c){}
- void operator += (int n)
- {
- x += n;
- y += n;
- z += n;
- }
- void operator = (point& p)
- {
- memcpy(this,(void*)&p,sizeof(*this));
- }
- };
- struct face
- {
- point P[3];
- void operator +=(int n)
- {
- P[0] += n;
- P[1] += n;
- P[2] += n;
- }
- };
- struct BBox
- {
- point Min;
- point Max;
- BBox():Min(),Max(){}
- };
- enum EAxis
- {
- Axis_X,
- Axis_Y,
- Axis_Z,
- };
- struct