From 14240e93893dacc77385d8b156727591cc254269 Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Mon, 22 Apr 2013 12:21:26 +0100
Subject: [PATCH] ENH: treeDataTrisurface: nearest to line

---
 src/meshTools/indexedOctree/treeDataPoint.C   |  8 ++-
 .../indexedOctree/treeDataTriSurface.C        | 51 ++++++++++++++++---
 2 files changed, 51 insertions(+), 8 deletions(-)

diff --git a/src/meshTools/indexedOctree/treeDataPoint.C b/src/meshTools/indexedOctree/treeDataPoint.C
index 7c3a831159c..1d7f3d133ec 100644
--- a/src/meshTools/indexedOctree/treeDataPoint.C
+++ b/src/meshTools/indexedOctree/treeDataPoint.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
@@ -160,7 +160,11 @@ void Foam::treeDataPoint::findNearest
 ) const
 {
     // Best so far
-    scalar nearestDistSqr = magSqr(linePoint - nearestPoint);
+    scalar nearestDistSqr = GREAT;
+    if (minIndex >= 0)
+    {
+        nearestDistSqr = magSqr(linePoint - nearestPoint);
+    }
 
     forAll(indices, i)
     {
diff --git a/src/meshTools/indexedOctree/treeDataTriSurface.C b/src/meshTools/indexedOctree/treeDataTriSurface.C
index fb1a4645f69..0aa567f68b0 100644
--- a/src/meshTools/indexedOctree/treeDataTriSurface.C
+++ b/src/meshTools/indexedOctree/treeDataTriSurface.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
@@ -404,11 +404,50 @@ void Foam::treeDataTriSurface::findNearest
     point& nearestPoint
 ) const
 {
-    notImplemented
-    (
-        "treeDataTriSurface::findNearest(const labelUList&"
-        ", const linePointRef&, treeBoundBox&, label&, point&, point&) const"
-    );
+    // Best so far
+    scalar nearestDistSqr = VGREAT;
+    if (minIndex >= 0)
+    {
+        nearestDistSqr = magSqr(linePoint - nearestPoint);
+    }
+
+    const pointField& points = surface_.points();
+
+    forAll(indices, i)
+    {
+        label index = indices[i];
+        const triSurface::FaceType& f = surface_[index];
+
+        triPointRef tri(f.tri(points));
+
+        pointHit lineInfo(point::max);
+        pointHit pHit = tri.nearestPoint(ln, lineInfo);
+
+        scalar distSqr = sqr(pHit.distance());
+
+        if (distSqr < nearestDistSqr)
+        {
+            nearestDistSqr = distSqr;
+            minIndex = index;
+            linePoint = lineInfo.rawPoint();
+            nearestPoint = pHit.rawPoint();
+
+            {
+                point& minPt = tightest.min();
+                minPt = min(ln.start(), ln.end());
+                minPt.x() -= pHit.distance();
+                minPt.y() -= pHit.distance();
+                minPt.z() -= pHit.distance();
+            }
+            {
+                point& maxPt = tightest.max();
+                maxPt = max(ln.start(), ln.end());
+                maxPt.x() += pHit.distance();
+                maxPt.y() += pHit.distance();
+                maxPt.z() += pHit.distance();
+            }
+        }
+    }
 }
 
 
-- 
GitLab