BOOL cNodeTreeMesh::IsPolygonContained(sPolygon *Polygon, / float XPos, float YPos, float ZPos, / float Size) { float XMin, XMax, YMin, YMax, ZMin, ZMax; sVertex *Vertex[3]; // Get the polygon’s vertices Vertex[0] = (sVertex*)&m_VertexPtr[m_VertexSize * / Polygon->Vertex[0]]; Vertex[1] = (sVertex*)&m_VertexPtr[m_VertexSize * / Polygon->Vertex[1]]; Vertex[2] = (sVertex*)&m_VertexPtr[m_VertexSize * / Polygon->Vertex[2]]; // Check the X-axis of specified 3-D space XMin = min(Vertex[0]->x, min(Vertex[1]->x, Vertex[2]->x)); XMax = max(Vertex[0]->x, max(Vertex[1]->x, Vertex[2]->x)); if(XMax < (XPos - Size / 2.0f)) return FALSE; if(XMin > (XPos + Size / 2.0f)) return FALSE; |
这段代码(包括接下来的代码)决定了一个顶点在x轴的最远范围。如果多边形被验证为在盒子中太远,返回FALSE。同样的,其他两个轴:
// Check the Y-axis of specified 3-D space (if octree) if(m_TreeType == OCTREE) { YMin = min(Vertex[0]->y, min(Vertex[1]->y, Vertex[2]->y)); YMax = max(Vertex[0]->y, max(Vertex[1]->y, Vertex[2]->y)); if(YMax < (YPos - Size / 2.0f)) return FALSE; if(YMin > (YPos + Size / 2.0f)) return FALSE; } // Check the Z-axis of specified 3-D space ZMin = min(Vertex[0]->z, min(Vertex[1]->z, Vertex[2]->z)); ZMax = max(Vertex[0]->z, max(Vertex[1]->z, Vertex[2]->z)); if(ZMax < (ZPos - Size / 2.0f)) return FALSE; if(ZMin > (ZPos + Size / 2.0f)) return FALSE; return TRUE; } unsigned long cNodeTreeMesh::CountPolygons( / float XPos, float YPos, float ZPos, float Size) { unsigned long i, Num; // Return if no polygons to process if(!m_NumPolygons) return 0; // Go through every polygon and keep count of those // contained in the specified 3-D space. Num = 0; for(i=0;i<m_NumPolygons;i++) { if(IsPolygonContained(&m_PolygonList[i],XPos,YPos,ZPos, / Size) == TRUE) Num++; } return Num; } |
你可以看到,CountPolygons循环只有在通网格的每个多边形,并验证验证他是否在盒子里边。每个多边形包含他的数目,和返回一个最后记录。你使用这个函数,当分裂和保存节点时要决定在一个节点中有多少多边形。