From 8ed95d47505f1ec5f22d85e992eed10b35265edf Mon Sep 17 00:00:00 2001 From: laurence Date: Fri, 15 Mar 2013 12:38:57 +0000 Subject: [PATCH] ENH: Add extra octree functionality + Make intersection and nearest functions functors. This makes adding different intersection and nearest routines easier. + treeDataPrimitivePatch takes its tolerance as a constructor argument + Make treeDataTriSurface a typedef of treeDataPrimitivePatch Conflicts: src/OpenFOAM/algorithms/indexedOctree/indexedOctree.C src/meshTools/AMIInterpolation/AMIInterpolation/AMIInterpolation.C src/meshTools/indexedOctree/treeDataTriSurface.C --- .../algorithms/indexedOctree/indexedOctree.C | 149 +++++- .../algorithms/indexedOctree/indexedOctree.H | 60 ++- .../algorithms/indexedOctree/treeDataCell.C | 72 ++- .../algorithms/indexedOctree/treeDataCell.H | 95 ++-- src/dynamicMesh/boundaryMesh/boundaryMesh.C | 13 +- .../surfaceInterpolationScheme.C | 2 +- .../cellClassification/cellClassification.C | 3 +- .../cellClassification/cellClassification.H | 4 +- src/meshTools/indexedOctree/treeDataEdge.C | 57 ++- src/meshTools/indexedOctree/treeDataEdge.H | 108 ++-- src/meshTools/indexedOctree/treeDataFace.C | 72 ++- src/meshTools/indexedOctree/treeDataFace.H | 100 ++-- src/meshTools/indexedOctree/treeDataPoint.C | 65 ++- src/meshTools/indexedOctree/treeDataPoint.H | 103 ++-- .../indexedOctree/treeDataPrimitivePatch.C | 250 ++++++--- .../indexedOctree/treeDataPrimitivePatch.H | 149 ++++-- .../indexedOctree/treeDataTriSurface.C | 477 +----------------- .../indexedOctree/treeDataTriSurface.H | 141 +----- .../searchableSurface/triSurfaceMesh.C | 6 +- .../surfaceToPoint/surfaceToPoint.C | 3 +- .../triSurfaceSearch/triSurfaceSearch.C | 3 +- .../triSurfaceSearch/triSurfaceSearch.H | 3 +- 22 files changed, 968 insertions(+), 967 deletions(-) diff --git a/src/OpenFOAM/algorithms/indexedOctree/indexedOctree.C b/src/OpenFOAM/algorithms/indexedOctree/indexedOctree.C index 83ac739941..4b703bbb42 100644 --- a/src/OpenFOAM/algorithms/indexedOctree/indexedOctree.C +++ b/src/OpenFOAM/algorithms/indexedOctree/indexedOctree.C @@ -576,6 +576,7 @@ Foam::indexedOctree::getSide // Find nearest point starting from nodeI template +template void Foam::indexedOctree::findNearest ( const label nodeI, @@ -583,7 +584,9 @@ void Foam::indexedOctree::findNearest scalar& nearestDistSqr, label& nearestShapeI, - point& nearestPoint + point& nearestPoint, + + const FindNearestOp& fnOp ) const { const node& nod = nodes_[nodeI]; @@ -614,7 +617,9 @@ void Foam::indexedOctree::findNearest nearestDistSqr, nearestShapeI, - nearestPoint + nearestPoint, + + fnOp ); } } @@ -631,7 +636,7 @@ void Foam::indexedOctree::findNearest ) ) { - shapes_.findNearest + fnOp ( contents_[getContent(index)], sample, @@ -648,6 +653,7 @@ void Foam::indexedOctree::findNearest // Find nearest point to line. template +template void Foam::indexedOctree::findNearest ( const label nodeI, @@ -656,7 +662,9 @@ void Foam::indexedOctree::findNearest treeBoundBox& tightest, label& nearestShapeI, point& linePoint, - point& nearestPoint + point& nearestPoint, + + const FindNearestOp& fnOp ) const { const node& nod = nodes_[nodeI]; @@ -687,7 +695,9 @@ void Foam::indexedOctree::findNearest tightest, nearestShapeI, linePoint, - nearestPoint + nearestPoint, + + fnOp ); } } @@ -697,7 +707,7 @@ void Foam::indexedOctree::findNearest if (subBb.overlaps(tightest)) { - shapes_.findNearest + fnOp ( contents_[getContent(index)], ln, @@ -1620,6 +1630,7 @@ Foam::word Foam::indexedOctree::faceString // hitInfo.point = coordinate of intersection of ray with bounding box // hitBits = posbits of point on bounding box template +template void Foam::indexedOctree::traverseNode ( const bool findAny, @@ -1632,7 +1643,9 @@ void Foam::indexedOctree::traverseNode const direction octant, pointIndexHit& hitInfo, - direction& hitBits + direction& hitBits, + + const FindIntersectOp& fiOp ) const { if (debug) @@ -1667,7 +1680,7 @@ void Foam::indexedOctree::traverseNode label shapeI = indices[elemI]; point pt; - bool hit = shapes_.intersects(shapeI, start, end, pt); + bool hit = fiOp(shapeI, start, end, pt); // Note that intersection of shape might actually be // in a neighbouring box. For findAny this is not important. @@ -1695,13 +1708,7 @@ void Foam::indexedOctree::traverseNode label shapeI = indices[elemI]; point pt; - bool hit = shapes_.intersects - ( - shapeI, - start, - nearestPoint, - pt - ); + bool hit = fiOp(shapeI, start, nearestPoint, pt); // Note that intersection of shape might actually be // in a neighbouring box. Since we need to maintain strict @@ -1774,7 +1781,9 @@ void Foam::indexedOctree::traverseNode octant, hitInfo, - hitBits + hitBits, + + fiOp ); } } @@ -1782,6 +1791,7 @@ void Foam::indexedOctree::traverseNode // Find first intersection template +template Foam::pointIndexHit Foam::indexedOctree::findLine ( const bool findAny, @@ -1789,6 +1799,7 @@ Foam::pointIndexHit Foam::indexedOctree::findLine const point& treeEnd, const label startNodeI, const direction startOctant, + const FindIntersectOp& fiOp, const bool verbose ) const { @@ -1864,7 +1875,9 @@ Foam::pointIndexHit Foam::indexedOctree::findLine octant, hitInfo, - hitFaceID + hitFaceID, + + fiOp ); // Did we hit a triangle? @@ -1948,7 +1961,8 @@ Foam::pointIndexHit Foam::indexedOctree::findLine treeEnd, startNodeI, startOctant, - true //verbose + fiOp, + true //verbose, ); } if (debug) @@ -2007,11 +2021,13 @@ Foam::pointIndexHit Foam::indexedOctree::findLine // Find first intersection template +template Foam::pointIndexHit Foam::indexedOctree::findLine ( const bool findAny, const point& start, - const point& end + const point& end, + const FindIntersectOp& fiOp ) const { pointIndexHit hitInfo; @@ -2069,7 +2085,8 @@ Foam::pointIndexHit Foam::indexedOctree::findLine trackStart, trackEnd, parentNodeI, - octant + octant, + fiOp ); } @@ -2656,6 +2673,25 @@ Foam::pointIndexHit Foam::indexedOctree::findNearest const point& sample, const scalar startDistSqr ) const +{ + return findNearest + ( + sample, + startDistSqr, + typename Type::findNearestOp(*this) + ); +} + + +template +template +Foam::pointIndexHit Foam::indexedOctree::findNearest +( + const point& sample, + const scalar startDistSqr, + + const FindNearestOp& fnOp +) const { scalar nearestDistSqr = startDistSqr; label nearestShapeI = -1; @@ -2670,7 +2706,9 @@ Foam::pointIndexHit Foam::indexedOctree::findNearest nearestDistSqr, nearestShapeI, - nearestPoint + nearestPoint, + + fnOp ); } @@ -2685,6 +2723,27 @@ Foam::pointIndexHit Foam::indexedOctree::findNearest treeBoundBox& tightest, point& linePoint ) const +{ + return findNearest + ( + ln, + tightest, + linePoint, + typename Type::findNearestOp(*this) + ); +} + + +template +template +Foam::pointIndexHit Foam::indexedOctree::findNearest +( + const linePointRef& ln, + treeBoundBox& tightest, + point& linePoint, + + const FindNearestOp& fnOp +) const { label nearestShapeI = -1; point nearestPoint = vector::zero; @@ -2699,7 +2758,9 @@ Foam::pointIndexHit Foam::indexedOctree::findNearest tightest, nearestShapeI, linePoint, - nearestPoint + nearestPoint, + + fnOp ); } @@ -2715,7 +2776,13 @@ Foam::pointIndexHit Foam::indexedOctree::findLine const point& end ) const { - return findLine(false, start, end); + return findLine + ( + false, + start, + end, + typename Type::findIntersectOp(*this) + ); } @@ -2727,7 +2794,41 @@ Foam::pointIndexHit Foam::indexedOctree::findLineAny const point& end ) const { - return findLine(true, start, end); + return findLine + ( + true, + start, + end, + typename Type::findIntersectOp(*this) + ); +} + + +// Find nearest intersection +template +template +Foam::pointIndexHit Foam::indexedOctree::findLine +( + const point& start, + const point& end, + const FindIntersectOp& fiOp +) const +{ + return findLine(false, start, end, fiOp); +} + + +// Find nearest intersection +template +template +Foam::pointIndexHit Foam::indexedOctree::findLineAny +( + const point& start, + const point& end, + const FindIntersectOp& fiOp +) const +{ + return findLine(true, start, end, fiOp); } diff --git a/src/OpenFOAM/algorithms/indexedOctree/indexedOctree.H b/src/OpenFOAM/algorithms/indexedOctree/indexedOctree.H index 6a5c56ac7b..154cb34bd0 100644 --- a/src/OpenFOAM/algorithms/indexedOctree/indexedOctree.H +++ b/src/OpenFOAM/algorithms/indexedOctree/indexedOctree.H @@ -212,6 +212,7 @@ private: // Query //- Find nearest point to line. + template void findNearest ( const label nodeI, @@ -220,7 +221,9 @@ private: treeBoundBox& tightest, label& nearestShapeI, point& linePoint, - point& nearestPoint + point& nearestPoint, + + const FindNearestOp& fnOp ) const; //- Return bbox of octant @@ -294,6 +297,7 @@ private: // intersection point. // findAny=true : return any intersection // findAny=false: return nearest (to start) intersection + template void traverseNode ( const bool findAny, @@ -306,10 +310,13 @@ private: const direction octantI, pointIndexHit& hitInfo, - direction& faceID + direction& faceID, + + const FindIntersectOp& fiOp ) const; //- Find any or nearest intersection + template pointIndexHit findLine ( const bool findAny, @@ -317,6 +324,7 @@ private: const point& treeEnd, const label startNodeI, const direction startOctantI, + const FindIntersectOp& fiOp, const bool verbose = false ) const; @@ -328,11 +336,13 @@ private: // ) const; //- Find any or nearest intersection of line between start and end. + template pointIndexHit findLine ( const bool findAny, const point& start, - const point& end + const point& end, + const FindIntersectOp& fiOp ) const; //- Find all elements intersecting box. @@ -528,15 +538,24 @@ public: // Queries + pointIndexHit findNearest + ( + const point& sample, + const scalar nearestDistSqr + ) const; + //- Calculate nearest point on nearest shape. // Returns // - bool : any point found nearer than nearestDistSqr // - label: index in shapes // - point: actual nearest point found + template pointIndexHit findNearest ( const point& sample, - const scalar nearestDistSqr + const scalar nearestDistSqr, + + const FindNearestOp& fnOp ) const; // bool findAnyOverlap @@ -553,6 +572,7 @@ public: // ) const; //- Low level: calculate nearest starting from subnode. + template void findNearest ( const label nodeI, @@ -560,7 +580,9 @@ public: scalar& nearestDistSqr, label& nearestShapeI, - point& nearestPoint + point& nearestPoint, + + const FindNearestOp& fnOp ) const; //- Find nearest to line. @@ -577,6 +599,16 @@ public: point& linePoint ) const; + template + pointIndexHit findNearest + ( + const linePointRef& ln, + treeBoundBox& tightest, + point& linePoint, + + const FindNearestOp& fnOp + ) const; + //- Find nearest intersection of line between start and end. pointIndexHit findLine ( @@ -591,6 +623,24 @@ public: const point& end ) const; + //- Find nearest intersection of line between start and end. + template + pointIndexHit findLine + ( + const point& start, + const point& end, + const FindIntersectOp& fiOp + ) const; + + //- Find any intersection of line between start and end. + template + pointIndexHit findLineAny + ( + const point& start, + const point& end, + const FindIntersectOp& fiOp + ) const; + //- Find (in no particular order) indices of all shapes inside or // overlapping bounding box (i.e. all shapes not outside box) labelList findBox(const treeBoundBox& bb) const; diff --git a/src/OpenFOAM/algorithms/indexedOctree/treeDataCell.C b/src/OpenFOAM/algorithms/indexedOctree/treeDataCell.C index e4d78fc83d..2ab17d0de8 100644 --- a/src/OpenFOAM/algorithms/indexedOctree/treeDataCell.C +++ b/src/OpenFOAM/algorithms/indexedOctree/treeDataCell.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -133,6 +133,24 @@ Foam::treeDataCell::treeDataCell } +Foam::treeDataCell::findNearestOp::findNearestOp +( + const indexedOctree& tree +) +: + tree_(tree) +{} + + +Foam::treeDataCell::findIntersectOp::findIntersectOp +( + const indexedOctree& tree +) +: + tree_(tree) +{} + + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // Foam::pointField Foam::treeDataCell::shapePoints() const @@ -175,7 +193,7 @@ bool Foam::treeDataCell::contains } -void Foam::treeDataCell::findNearest +void Foam::treeDataCell::findNearestOp::operator() ( const labelUList& indices, const point& sample, @@ -185,23 +203,51 @@ void Foam::treeDataCell::findNearest point& nearestPoint ) const { + const treeDataCell& shape = tree_.shapes(); + forAll(indices, i) { label index = indices[i]; - label cellI = cellLabels_[index]; - scalar distSqr = magSqr(sample - mesh_.cellCentres()[cellI]); + label cellI = shape.cellLabels()[index]; + scalar distSqr = magSqr(sample - shape.mesh().cellCentres()[cellI]); if (distSqr < nearestDistSqr) { nearestDistSqr = distSqr; minIndex = index; - nearestPoint = mesh_.cellCentres()[cellI]; + nearestPoint = shape.mesh().cellCentres()[cellI]; } } } -bool Foam::treeDataCell::intersects +void Foam::treeDataCell::findNearestOp::operator() +( + const labelUList& indices, + const linePointRef& ln, + + treeBoundBox& tightest, + label& minIndex, + point& linePoint, + point& nearestPoint +) const +{ + notImplemented + ( + "treeDataCell::findNearestOp::operator()" + "(" + " const labelUList&," + " const linePointRef&," + " treeBoundBox&," + " label&," + " point&," + " point&" + ") const" + ); +} + + +bool Foam::treeDataCell::findIntersectOp::operator() ( const label index, const point& start, @@ -209,10 +255,12 @@ bool Foam::treeDataCell::intersects point& intersectionPoint ) const { + const treeDataCell& shape = tree_.shapes(); + // Do quick rejection test - if (cacheBb_) + if (shape.cacheBb_) { - const treeBoundBox& cellBb = bbs_[index]; + const treeBoundBox& cellBb = shape.bbs_[index]; if ((cellBb.posBits(start) & cellBb.posBits(end)) != 0) { @@ -222,7 +270,7 @@ bool Foam::treeDataCell::intersects } else { - const treeBoundBox cellBb = calcCellBb(cellLabels_[index]); + const treeBoundBox cellBb = shape.calcCellBb(shape.cellLabels_[index]); if ((cellBb.posBits(start) & cellBb.posBits(end)) != 0) { @@ -238,7 +286,7 @@ bool Foam::treeDataCell::intersects // Disable picking up intersections behind us. scalar oldTol = intersection::setPlanarTol(0.0); - const cell& cFaces = mesh_.cells()[cellLabels_[index]]; + const cell& cFaces = shape.mesh_.cells()[shape.cellLabels_[index]]; const vector dir(end - start); scalar minDistSqr = magSqr(dir); @@ -246,13 +294,13 @@ bool Foam::treeDataCell::intersects forAll(cFaces, i) { - const face& f = mesh_.faces()[cFaces[i]]; + const face& f = shape.mesh_.faces()[cFaces[i]]; pointHit inter = f.ray ( start, dir, - mesh_.points(), + shape.mesh_.points(), intersection::HALF_RAY ); diff --git a/src/OpenFOAM/algorithms/indexedOctree/treeDataCell.H b/src/OpenFOAM/algorithms/indexedOctree/treeDataCell.H index 476266faf9..fe65b9b9bd 100644 --- a/src/OpenFOAM/algorithms/indexedOctree/treeDataCell.H +++ b/src/OpenFOAM/algorithms/indexedOctree/treeDataCell.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -80,6 +80,56 @@ class treeDataCell public: + + class findNearestOp + { + const indexedOctree& tree_; + + public: + + findNearestOp(const indexedOctree& tree); + + void operator() + ( + const labelUList& indices, + const point& sample, + + scalar& nearestDistSqr, + label& minIndex, + point& nearestPoint + ) const; + + void operator() + ( + const labelUList& indices, + const linePointRef& ln, + + treeBoundBox& tightest, + label& minIndex, + point& linePoint, + point& nearestPoint + ) const; + }; + + + class findIntersectOp + { + const indexedOctree& tree_; + + public: + + findIntersectOp(const indexedOctree& tree); + + bool operator() + ( + const label index, + const point& start, + const point& end, + point& intersectionPoint + ) const; + }; + + // Declare name of the class and its debug switch ClassName("treeDataCell"); @@ -173,49 +223,6 @@ public: const label index, const point& sample ) const; - - //- Calculates nearest (to sample) point in shape. - // Returns actual point and distance (squared) - void findNearest - ( - const labelUList& indices, - const point& sample, - - scalar& nearestDistSqr, - label& nearestIndex, - point& nearestPoint - ) const; - - //- Calculates nearest (to line) point in shape. - // Returns point and distance (squared) - void findNearest - ( - const labelUList& indices, - const linePointRef& ln, - - treeBoundBox& tightest, - label& minIndex, - point& linePoint, - point& nearestPoint - ) const - { - notImplemented - ( - "treeDataCell::findNearest" - "(const labelUList&, const linePointRef&, ..)" - ); - } - - //- Calculate intersection of shape with ray. Sets result - // accordingly - bool intersects - ( - const label index, - const point& start, - const point& end, - point& result - ) const; - }; diff --git a/src/dynamicMesh/boundaryMesh/boundaryMesh.C b/src/dynamicMesh/boundaryMesh/boundaryMesh.C index 1abf219dfb..ad204d7c15 100644 --- a/src/dynamicMesh/boundaryMesh/boundaryMesh.C +++ b/src/dynamicMesh/boundaryMesh/boundaryMesh.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -924,6 +924,11 @@ Foam::labelList Foam::boundaryMesh::getNearest bbMax.y() += 2*tol; bbMax.z() += 2*tol; + const scalar planarTol = + indexedOctree >:: + perturbTol(); + + // Create the octrees indexedOctree < @@ -933,7 +938,8 @@ Foam::labelList Foam::boundaryMesh::getNearest treeDataPrimitivePatch ( false, // cacheBb - leftPatch + leftPatch, + planarTol ), overallBb, 10, // maxLevel @@ -948,7 +954,8 @@ Foam::labelList Foam::boundaryMesh::getNearest treeDataPrimitivePatch ( false, // cacheBb - rightPatch + rightPatch, + planarTol ), overallBb, 10, // maxLevel diff --git a/src/finiteVolume/interpolation/surfaceInterpolation/surfaceInterpolationScheme/surfaceInterpolationScheme.C b/src/finiteVolume/interpolation/surfaceInterpolation/surfaceInterpolationScheme/surfaceInterpolationScheme.C index 1a69959095..f66c3acfa6 100644 --- a/src/finiteVolume/interpolation/surfaceInterpolation/surfaceInterpolationScheme/surfaceInterpolationScheme.C +++ b/src/finiteVolume/interpolation/surfaceInterpolation/surfaceInterpolationScheme/surfaceInterpolationScheme.C @@ -241,7 +241,7 @@ surfaceInterpolationScheme::interpolate //- Return the face-interpolate of the given cell field -// with the given weigting factors +// with the given weighting factors template tmp > surfaceInterpolationScheme::interpolate diff --git a/src/meshTools/cellClassification/cellClassification.C b/src/meshTools/cellClassification/cellClassification.C index 09e84ac93c..800d007f6f 100644 --- a/src/meshTools/cellClassification/cellClassification.C +++ b/src/meshTools/cellClassification/cellClassification.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -34,6 +34,7 @@ License #include "ListOps.H" #include "meshTools.H" #include "cpuTime.H" +#include "triSurface.H" #include "globalMeshData.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // diff --git a/src/meshTools/cellClassification/cellClassification.H b/src/meshTools/cellClassification/cellClassification.H index aeb7a8102c..b040819fb2 100644 --- a/src/meshTools/cellClassification/cellClassification.H +++ b/src/meshTools/cellClassification/cellClassification.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -107,9 +107,7 @@ namespace Foam class triSurfaceSearch; class meshSearch; class polyMesh; -class polyMesh; class primitiveMesh; -class triSurface; /*---------------------------------------------------------------------------*\ Class cellClassification Declaration diff --git a/src/meshTools/indexedOctree/treeDataEdge.C b/src/meshTools/indexedOctree/treeDataEdge.C index 567fae0478..9907053e6f 100644 --- a/src/meshTools/indexedOctree/treeDataEdge.C +++ b/src/meshTools/indexedOctree/treeDataEdge.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -96,6 +96,24 @@ Foam::treeDataEdge::treeDataEdge } +Foam::treeDataEdge::findNearestOp::findNearestOp +( + const indexedOctree& tree +) +: + tree_(tree) +{} + + +Foam::treeDataEdge::findIntersectOp::findIntersectOp +( + const indexedOctree& tree +) +: + tree_(tree) +{} + + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // Foam::pointField Foam::treeDataEdge::shapePoints() const @@ -165,9 +183,7 @@ bool Foam::treeDataEdge::overlaps } -// Calculate nearest point to sample. Updates (if any) nearestDistSqr, minIndex, -// nearestPoint. -void Foam::treeDataEdge::findNearest +void Foam::treeDataEdge::findNearestOp::operator() ( const labelUList& indices, const point& sample, @@ -177,13 +193,15 @@ void Foam::treeDataEdge::findNearest point& nearestPoint ) const { + const treeDataEdge& shape = tree_.shapes(); + forAll(indices, i) { const label index = indices[i]; - const edge& e = edges_[edgeLabels_[index]]; + const edge& e = shape.edges()[shape.edgeLabels()[index]]; - pointHit nearHit = e.line(points_).nearestDist(sample); + pointHit nearHit = e.line(shape.points()).nearestDist(sample); scalar distSqr = sqr(nearHit.distance()); @@ -197,9 +215,7 @@ void Foam::treeDataEdge::findNearest } -//- Calculates nearest (to line) point in shape. -// Returns point and distance (squared) -void Foam::treeDataEdge::findNearest +void Foam::treeDataEdge::findNearestOp::operator() ( const labelUList& indices, const linePointRef& ln, @@ -210,6 +226,8 @@ void Foam::treeDataEdge::findNearest point& nearestPoint ) const { + const treeDataEdge& shape = tree_.shapes(); + // Best so far scalar nearestDistSqr = magSqr(linePoint - nearestPoint); @@ -217,13 +235,13 @@ void Foam::treeDataEdge::findNearest { const label index = indices[i]; - const edge& e = edges_[edgeLabels_[index]]; + const edge& e = shape.edges()[shape.edgeLabels()[index]]; // Note: could do bb test ? Worthwhile? // Nearest point on line point ePoint, lnPt; - scalar dist = e.line(points_).nearestDist(ln, ePoint, lnPt); + scalar dist = e.line(shape.points()).nearestDist(ln, ePoint, lnPt); scalar distSqr = sqr(dist); if (distSqr < nearestDistSqr) @@ -252,4 +270,21 @@ void Foam::treeDataEdge::findNearest } +bool Foam::treeDataEdge::findIntersectOp::operator() +( + const label index, + const point& start, + const point& end, + point& result +) const +{ + notImplemented + ( + "treeDataEdge::intersects(const label, const point&," + "const point&, point&)" + ); + return false; +} + + // ************************************************************************* // diff --git a/src/meshTools/indexedOctree/treeDataEdge.H b/src/meshTools/indexedOctree/treeDataEdge.H index 9a8deadb73..a27ab39ff5 100644 --- a/src/meshTools/indexedOctree/treeDataEdge.H +++ b/src/meshTools/indexedOctree/treeDataEdge.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -86,6 +86,58 @@ class treeDataEdge public: + + class findNearestOp + { + const indexedOctree& tree_; + + public: + + findNearestOp(const indexedOctree& tree); + + void operator() + ( + const labelUList& indices, + const point& sample, + + scalar& nearestDistSqr, + label& minIndex, + point& nearestPoint + ) const; + + void operator() + ( + const labelUList& indices, + const linePointRef& ln, + + treeBoundBox& tightest, + label& minIndex, + point& linePoint, + point& nearestPoint + ) const; + }; + + + class findIntersectOp + { + const indexedOctree& tree_; + + public: + + findIntersectOp(const indexedOctree& tree); + + //- Calculate intersection of triangle with ray. Sets result + // accordingly + bool operator() + ( + const label index, + const point& start, + const point& end, + point& intersectionPoint + ) const; + }; + + // Declare name of the class and its debug switch ClassName("treeDataEdge"); @@ -116,6 +168,16 @@ public: // Access + const edgeList& edges() const + { + return edges_; + } + + const pointField& points() const + { + return points_; + } + const labelList& edgeLabels() const { return edgeLabels_; @@ -155,50 +217,6 @@ public: const point& centre, const scalar radiusSqr ) const; - - //- Calculates nearest (to sample) point in shape. - // Returns actual point and distance (squared) - void findNearest - ( - const labelUList& indices, - const point& sample, - - scalar& nearestDistSqr, - label& nearestIndex, - point& nearestPoint - ) const; - - //- Calculates nearest (to line) point in shape. - // Returns point and distance (squared) - void findNearest - ( - const labelUList& indices, - const linePointRef& ln, - - treeBoundBox& tightest, - label& minIndex, - point& linePoint, - point& nearestPoint - ) const; - - //- Calculate intersection of shape with ray. Sets result - // accordingly - bool intersects - ( - const label index, - const point& start, - const point& end, - point& result - ) const - { - notImplemented - ( - "treeDataEdge::intersects(const label, const point&," - "const point&, point&)" - ); - return false; - } - }; diff --git a/src/meshTools/indexedOctree/treeDataFace.C b/src/meshTools/indexedOctree/treeDataFace.C index 9699416c13..ccaf4d35fe 100644 --- a/src/meshTools/indexedOctree/treeDataFace.C +++ b/src/meshTools/indexedOctree/treeDataFace.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -145,6 +145,24 @@ Foam::treeDataFace::treeDataFace } +Foam::treeDataFace::findNearestOp::findNearestOp +( + const indexedOctree& tree +) +: + tree_(tree) +{} + + +Foam::treeDataFace::findIntersectOp::findIntersectOp +( + const indexedOctree& tree +) +: + tree_(tree) +{} + + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // Foam::pointField Foam::treeDataFace::shapePoints() const @@ -477,9 +495,7 @@ bool Foam::treeDataFace::overlaps } -// Calculate nearest point to sample. Updates (if any) nearestDistSqr, minIndex, -// nearestPoint. -void Foam::treeDataFace::findNearest +void Foam::treeDataFace::findNearestOp::operator() ( const labelUList& indices, const point& sample, @@ -489,13 +505,15 @@ void Foam::treeDataFace::findNearest point& nearestPoint ) const { + const treeDataFace& shape = tree_.shapes(); + forAll(indices, i) { const label index = indices[i]; - const face& f = mesh_.faces()[faceLabels_[index]]; + const face& f = shape.mesh().faces()[shape.faceLabels()[index]]; - pointHit nearHit = f.nearestPoint(sample, mesh_.points()); + pointHit nearHit = f.nearestPoint(sample, shape.mesh().points()); scalar distSqr = sqr(nearHit.distance()); if (distSqr < nearestDistSqr) @@ -508,7 +526,33 @@ void Foam::treeDataFace::findNearest } -bool Foam::treeDataFace::intersects +void Foam::treeDataFace::findNearestOp::operator() +( + const labelUList& indices, + const linePointRef& ln, + + treeBoundBox& tightest, + label& minIndex, + point& linePoint, + point& nearestPoint +) const +{ + notImplemented + ( + "treeDataFace::findNearestOp::operator()" + "(" + " const labelUList&," + " const linePointRef&," + " treeBoundBox&," + " label&," + " point&," + " point&" + ") const" + ); +} + + +bool Foam::treeDataFace::findIntersectOp::operator() ( const label index, const point& start, @@ -516,10 +560,12 @@ bool Foam::treeDataFace::intersects point& intersectionPoint ) const { + const treeDataFace& shape = tree_.shapes(); + // Do quick rejection test - if (cacheBb_) + if (shape.cacheBb_) { - const treeBoundBox& faceBb = bbs_[index]; + const treeBoundBox& faceBb = shape.bbs_[index]; if ((faceBb.posBits(start) & faceBb.posBits(end)) != 0) { @@ -528,16 +574,16 @@ bool Foam::treeDataFace::intersects } } - const label faceI = faceLabels_[index]; + const label faceI = shape.faceLabels_[index]; const vector dir(end - start); - pointHit inter = mesh_.faces()[faceI].intersection + pointHit inter = shape.mesh_.faces()[faceI].intersection ( start, dir, - mesh_.faceCentres()[faceI], - mesh_.points(), + shape.mesh_.faceCentres()[faceI], + shape.mesh_.points(), intersection::HALF_RAY ); diff --git a/src/meshTools/indexedOctree/treeDataFace.H b/src/meshTools/indexedOctree/treeDataFace.H index 2b8ab25831..1cf0c0d8e5 100644 --- a/src/meshTools/indexedOctree/treeDataFace.H +++ b/src/meshTools/indexedOctree/treeDataFace.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -39,6 +39,7 @@ SourceFiles #include "indexedOctree.H" #include "treeBoundBoxList.H" #include "PackedBoolList.H" +#include "primitiveMesh.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -46,7 +47,7 @@ namespace Foam { // Forward declaration of classes -class primitiveMesh; +//class primitiveMesh; //template class indexedOctree; class polyPatch; @@ -90,6 +91,58 @@ class treeDataFace public: + + class findNearestOp + { + const indexedOctree& tree_; + + public: + + findNearestOp(const indexedOctree& tree); + + void operator() + ( + const labelUList& indices, + const point& sample, + + scalar& nearestDistSqr, + label& minIndex, + point& nearestPoint + ) const; + + void operator() + ( + const labelUList& indices, + const linePointRef& ln, + + treeBoundBox& tightest, + label& minIndex, + point& linePoint, + point& nearestPoint + ) const; + }; + + + class findIntersectOp + { + const indexedOctree& tree_; + + public: + + findIntersectOp(const indexedOctree& tree); + + //- Calculate intersection of triangle with ray. Sets result + // accordingly + bool operator() + ( + const label index, + const point& start, + const point& end, + point& intersectionPoint + ) const; + }; + + // Declare name of the class and its debug switch ClassName("treeDataFace"); @@ -159,49 +212,6 @@ public: const label index, const treeBoundBox& sampleBb ) const; - - //- Calculates nearest (to sample) point in shape. - // Returns actual point and distance (squared) - void findNearest - ( - const labelUList& indices, - const point& sample, - - scalar& nearestDistSqr, - label& nearestIndex, - point& nearestPoint - ) const; - - //- Calculates nearest (to line) point in shape. - // Returns point and distance (squared) - void findNearest - ( - const labelUList& indices, - const linePointRef& ln, - - treeBoundBox& tightest, - label& minIndex, - point& linePoint, - point& nearestPoint - ) const - { - notImplemented - ( - "treeDataFace::findNearest" - "(const labelUList&, const linePointRef&, ..)" - ); - } - - //- Calculate intersection of shape with ray. Sets result - // accordingly - bool intersects - ( - const label index, - const point& start, - const point& end, - point& result - ) const; - }; diff --git a/src/meshTools/indexedOctree/treeDataPoint.C b/src/meshTools/indexedOctree/treeDataPoint.C index 1d7f3d133e..24ada4a839 100644 --- a/src/meshTools/indexedOctree/treeDataPoint.C +++ b/src/meshTools/indexedOctree/treeDataPoint.C @@ -57,6 +57,24 @@ Foam::treeDataPoint::treeDataPoint {} +Foam::treeDataPoint::findNearestOp::findNearestOp +( + const indexedOctree& tree +) +: + tree_(tree) +{} + + +Foam::treeDataPoint::findIntersectOp::findIntersectOp +( + const indexedOctree& tree +) +: + tree_(tree) +{} + + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // Foam::pointField Foam::treeDataPoint::shapePoints() const @@ -115,9 +133,7 @@ bool Foam::treeDataPoint::overlaps } -// Calculate nearest point to sample. Updates (if any) nearestDistSqr, minIndex, -// nearestPoint. -void Foam::treeDataPoint::findNearest +void Foam::treeDataPoint::findNearestOp::operator() ( const labelUList& indices, const point& sample, @@ -127,12 +143,19 @@ void Foam::treeDataPoint::findNearest point& nearestPoint ) const { + const treeDataPoint& shape = tree_.shapes(); + forAll(indices, i) { const label index = indices[i]; - label pointI = (useSubset_ ? pointLabels_[index] : index); + label pointI = + ( + shape.useSubset() + ? shape.pointLabels()[index] + : index + ); - const point& pt = points_[pointI]; + const point& pt = shape.points()[pointI]; scalar distSqr = magSqr(pt - sample); @@ -146,9 +169,7 @@ void Foam::treeDataPoint::findNearest } -//- Calculates nearest (to line) point in shape. -// Returns point and distance (squared) -void Foam::treeDataPoint::findNearest +void Foam::treeDataPoint::findNearestOp::operator() ( const labelUList& indices, const linePointRef& ln, @@ -159,6 +180,8 @@ void Foam::treeDataPoint::findNearest point& nearestPoint ) const { + const treeDataPoint& shape = tree_.shapes(); + // Best so far scalar nearestDistSqr = GREAT; if (minIndex >= 0) @@ -169,9 +192,14 @@ void Foam::treeDataPoint::findNearest forAll(indices, i) { const label index = indices[i]; - label pointI = (useSubset_ ? pointLabels_[index] : index); + label pointI = + ( + shape.useSubset() + ? shape.pointLabels()[index] + : index + ); - const point& shapePt = points_[pointI]; + const point& shapePt = shape.points()[pointI]; if (tightest.contains(shapePt)) { @@ -206,4 +234,21 @@ void Foam::treeDataPoint::findNearest } +bool Foam::treeDataPoint::findIntersectOp::operator() +( + const label index, + const point& start, + const point& end, + point& result +) const +{ + notImplemented + ( + "treeDataPoint::intersects(const label, const point&," + "const point&, point&)" + ); + return false; +} + + // ************************************************************************* // diff --git a/src/meshTools/indexedOctree/treeDataPoint.H b/src/meshTools/indexedOctree/treeDataPoint.H index f867a3274a..5e09383079 100644 --- a/src/meshTools/indexedOctree/treeDataPoint.H +++ b/src/meshTools/indexedOctree/treeDataPoint.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -69,6 +69,58 @@ class treeDataPoint public: + + class findNearestOp + { + const indexedOctree& tree_; + + public: + + findNearestOp(const indexedOctree& tree); + + void operator() + ( + const labelUList& indices, + const point& sample, + + scalar& nearestDistSqr, + label& minIndex, + point& nearestPoint + ) const; + + void operator() + ( + const labelUList& indices, + const linePointRef& ln, + + treeBoundBox& tightest, + label& minIndex, + point& linePoint, + point& nearestPoint + ) const; + }; + + + class findIntersectOp + { + const indexedOctree& tree_; + + public: + + findIntersectOp(const indexedOctree& tree); + + //- Calculate intersection of triangle with ray. Sets result + // accordingly + bool operator() + ( + const label index, + const point& start, + const point& end, + point& intersectionPoint + ) const; + }; + + // Declare name of the class and its debug switch ClassName("treeDataPoint"); @@ -106,6 +158,11 @@ public: return points_; } + bool useSubset() const + { + return useSubset_; + } + //- Get representative point cloud for all shapes inside // (one point per shape) pointField shapePoints() const; @@ -135,50 +192,6 @@ public: const point& centre, const scalar radiusSqr ) const; - - //- Calculates nearest (to sample) point in shape. - // Returns actual point and distance (squared) - void findNearest - ( - const labelUList& indices, - const point& sample, - - scalar& nearestDistSqr, - label& nearestIndex, - point& nearestPoint - ) const; - - //- Calculates nearest (to line) point in shape. - // Returns point and distance (squared) - void findNearest - ( - const labelUList& indices, - const linePointRef& ln, - - treeBoundBox& tightest, - label& minIndex, - point& linePoint, - point& nearestPoint - ) const; - - //- Calculate intersection of shape with ray. Sets result - // accordingly - bool intersects - ( - const label index, - const point& start, - const point& end, - point& result - ) const - { - notImplemented - ( - "treeDataPoint::intersects(const label, const point&," - "const point&, point&)" - ); - return false; - } - }; diff --git a/src/meshTools/indexedOctree/treeDataPrimitivePatch.C b/src/meshTools/indexedOctree/treeDataPrimitivePatch.C index 4cd2968232..2664849ab1 100644 --- a/src/meshTools/indexedOctree/treeDataPrimitivePatch.C +++ b/src/meshTools/indexedOctree/treeDataPrimitivePatch.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -26,12 +26,8 @@ License #include "treeDataPrimitivePatch.H" #include "indexedOctree.H" #include "triangleFuncs.H" - -// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // - -template -Foam::scalar Foam::treeDataPrimitivePatch::tolSqr = sqr(1e-6); - +#include "triSurfaceTools.H" +#include "triFace.H" // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // @@ -70,6 +66,75 @@ void Foam::treeDataPrimitivePatch::update() } +template +bool Foam::treeDataPrimitivePatch::findIntersection +( + const indexedOctree >& tree, + const label index, + const point& start, + const point& end, + point& intersectionPoint +) +{ + const treeDataPrimitivePatch& shape = tree.shapes(); + const PatchType& patch = shape.patch(); + + const pointField& points = patch.points(); + const typename PatchType::FaceType& f = patch[index]; + + // Do quick rejection test + if (shape.cacheBb_) + { + const treeBoundBox& faceBb = shape.bbs_[index]; + + if ((faceBb.posBits(start) & faceBb.posBits(end)) != 0) + { + // start and end in same block outside of faceBb. + return false; + } + } + + const vector dir(end - start); + pointHit inter; + + if (f.size() == 3) + { + inter = triPointRef + ( + points[f[0]], + points[f[1]], + points[f[2]] + ).intersection(start, dir, intersection::HALF_RAY, shape.planarTol_); + } + else + { + const pointField& faceCentres = patch.faceCentres(); + + inter = f.intersection + ( + start, + dir, + faceCentres[index], + points, + intersection::HALF_RAY + ); + } + + if (inter.hit() && inter.distance() <= 1) + { + // Note: no extra test on whether intersection is in front of us + // since using half_ray + intersectionPoint = inter.hitPoint(); + + return true; + } + else + { + return false; + } +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // Construct from components @@ -77,16 +142,50 @@ template Foam::treeDataPrimitivePatch::treeDataPrimitivePatch ( const bool cacheBb, - const PatchType& patch + const PatchType& patch, + const scalar planarTol ) : patch_(patch), - cacheBb_(cacheBb) + cacheBb_(cacheBb), + planarTol_(planarTol) { update(); } +template +Foam::treeDataPrimitivePatch::findNearestOp::findNearestOp +( + const indexedOctree >& tree +) +: + tree_(tree) +{} + + +template +Foam::treeDataPrimitivePatch::findIntersectOp::findIntersectOp +( + const indexedOctree >& tree +) +: + tree_(tree) +{} + + +template +Foam::treeDataPrimitivePatch::findAllIntersectOp::findAllIntersectOp +( + const indexedOctree >& tree, + DynamicList