const treeLeaf<Type>* treeNode<Type>::findLeafLineOctant ( const int level, const Type& shapes, const label octant, const vector& direction, point& start, const point& end ) const { static const char* functionName = "treeNode<Type>::findLeafLineOctant" "(const int, const Type&, const label, const vector&," " point&, const point&)"; if (debug & 2) { space(Pout, 2*level); Pout<< "findLeafLineOctant : bb:" << this->bb() << " start:" << start << " end:" << end << " mid:" << mid() << " Searching octant:" << octant << endl; } if (subNodes()[octant]) { if (isNode(octant)) { // Node: recurse into subnodes const treeNode<Type>* subNodePtr = getNodePtr(octant); if (subNodePtr->bb().contains(direction, start)) { // Search on lower level const treeLeaf<Type>* subLeafPtr = subNodePtr->findLeafLine ( level + 1, shapes, start, end ); if (debug & 2) { space(Pout, 2*level); Pout<< "findLeafLineOctant : bb:" << this->bb() << " returning from sub treeNode" << " with start:" << start << " subLeaf:" << long(subLeafPtr) << endl; } return subLeafPtr; } else { FatalErrorIn(functionName) << "Sub node " << subNodePtr->bb() << " at octant " << octant << " does not contain start " << start << abort(FatalError); } } else { // Leaf const treeLeaf<Type>* subLeafPtr = getLeafPtr(octant); if (subLeafPtr->bb().contains(direction, start)) { // Step to end of subleaf bb point tmp; if ( !subLeafPtr->bb().intersects ( end, start, tmp ) ) { FatalErrorIn(functionName) << "Sub leaf contains start " << start << " but line does not intersect its bb " << subLeafPtr->bb() << abort(FatalError); } start = tmp; if (debug & 2) { space(Pout, 2*level); Pout<< "findLeafLineOctant : returning from intersecting" << " treeLeaf " << subLeafPtr->bb() << " with start:" << start << " subLeaf:" << long(subLeafPtr) << endl; } return subLeafPtr; } else { FatalErrorIn(functionName) << "Sub leaf " << subLeafPtr->bb() << " at octant " << octant << " does not contain start " << start << abort(FatalError); } } } else { // Empty subNode. Transfer across. const treeBoundBox emptyBb = this->bb().subBbox(mid(), octant); if (emptyBb.contains(direction, start)) { if (debug & 2) { space(Pout, 2*level); Pout<< "findLeafLineOctant : Empty node. Octant:" << octant << " start:" << start << " bb:" << this->bb() << " emptyBb:" << emptyBb << endl; } // Update start by clipping to emptyBb point tmp; if ( !emptyBb.intersects ( end, start, tmp ) ) { FatalErrorIn(functionName) << "Empty node contains start " << start << " but line does not intersect its (calculated)" << " bb " << emptyBb << endl << "This might be due to truncation error" << abort(FatalError); } start = tmp; if (debug & 2) { space(Pout, 2*level); Pout<< "findLeafLineOctant : returning from intersecting with" << " empty " << emptyBb << " with start:" << start << " subLeaf:" << 0 << endl; } return NULL; } else { FatalErrorIn(functionName) << "Empty node " << emptyBb << " at octant " << octant << " does not contain start " << start << abort(FatalError); } } FatalErrorIn(functionName) << "Octant " << octant << " of cube " << this->bb() << " does not contain start " << start << abort(FatalError); return NULL; }
// Intersect triangle with bounding box. Return true if // any of the faces of bb intersect triangle. // Note: so returns false if triangle inside bb. bool Foam::triangleFuncs::intersectBb ( const point& p0, const point& p1, const point& p2, const treeBoundBox& cubeBb ) { const vector p10 = p1 - p0; const vector p20 = p2 - p0; // cubeBb points; counted as if cell with vertex0 at cubeBb.min(). const point& min = cubeBb.min(); const point& max = cubeBb.max(); const point& cube0 = min; const point cube1(min.x(), min.y(), max.z()); const point cube2(max.x(), min.y(), max.z()); const point cube3(max.x(), min.y(), min.z()); const point cube4(min.x(), max.y(), min.z()); const point cube5(min.x(), max.y(), max.z()); const point cube7(max.x(), max.y(), min.z()); // // Intersect all 12 edges of cube with triangle // point pInter; pointField origin(4); // edges in x direction origin[0] = cube0; origin[1] = cube1; origin[2] = cube5; origin[3] = cube4; scalar maxSx = max.x() - min.x(); if (intersectAxesBundle(p0, p10, p20, 0, origin, maxSx, pInter)) { return true; } // edges in y direction origin[0] = cube0; origin[1] = cube1; origin[2] = cube2; origin[3] = cube3; scalar maxSy = max.y() - min.y(); if (intersectAxesBundle(p0, p10, p20, 1, origin, maxSy, pInter)) { return true; } // edges in z direction origin[0] = cube0; origin[1] = cube3; origin[2] = cube7; origin[3] = cube4; scalar maxSz = max.z() - min.z(); if (intersectAxesBundle(p0, p10, p20, 2, origin, maxSz, pInter)) { return true; } // Intersect triangle edges with bounding box if (cubeBb.intersects(p0, p1, pInter)) { return true; } if (cubeBb.intersects(p1, p2, pInter)) { return true; } if (cubeBb.intersects(p2, p0, pInter)) { return true; } return false; }