Skip to content

Commit 46222b5

Browse files
committed
Added GmTriSearch::InterpWeightsTriangleIdx to get triangle index of interpolated to triangle.
1 parent a71919f commit 46222b5

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

xmsinterp/geometry/GmTriSearch.cpp

+43-4
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ class GmTriSearchImpl : public GmTriSearch
143143
virtual bool InterpWeights(const Pt3d& a_pt,
144144
std::vector<int>& a_idxs,
145145
std::vector<double>& a_wts) override;
146+
virtual bool InterpWeightsTriangleIdx(const Pt3d& a_pt,
147+
int& a_triangleIdx,
148+
std::vector<int>& a_idxs,
149+
std::vector<double>& a_wts) override;
146150

147151
virtual std::string ToString() const override;
148152

@@ -419,11 +423,28 @@ void GmTriSearchImpl::TriEnvelopesOverlap(const Pt3d& a_pMin, const Pt3d& a_pMax
419423
/// \return false if a_pt is not inside of any triangle
420424
//------------------------------------------------------------------------------
421425
bool GmTriSearchImpl::InterpWeights(const Pt3d& a_pt, VecInt& a_idxs, VecDbl& a_wts)
426+
{
427+
int triangleIdx;
428+
bool result = InterpWeightsTriangleIdx(a_pt, triangleIdx, a_idxs, a_wts);
429+
return result;
430+
} // GmTriSearchImpl::InterpWeights
431+
//------------------------------------------------------------------------------
432+
/// \brief Use the stored triangles to get interpolation weights for a point.
433+
/// Returns false if the point is outside the triangles.
434+
/// \param a_pt Location that is interpolated to.
435+
/// \param a_triangleIdx Found triangles index in triangle array
436+
/// (zero based count*3).
437+
/// \param a_idxs Vector of triangle point indices filled in by this method.
438+
/// \param a_wts Vector of triangle point weights filled in by this method.
439+
/// \return false if a_pt is not inside of any triangle
440+
//------------------------------------------------------------------------------
441+
bool GmTriSearchImpl::InterpWeightsTriangleIdx(const Pt3d& a_pt, int& a_triangleIdx,
442+
VecInt& a_idxs, VecDbl& a_wts)
422443
{
423444
int ix[3];
424445
Pt3d weights;
425-
int idx = FindTriangle(a_pt, ix, weights);
426-
if (idx != XM_NONE)
446+
a_triangleIdx = FindTriangle(a_pt, ix, weights);
447+
if (a_triangleIdx != XM_NONE)
427448
{
428449
a_wts.resize(0);
429450
a_wts.push_back(weights.x);
@@ -432,8 +453,13 @@ bool GmTriSearchImpl::InterpWeights(const Pt3d& a_pt, VecInt& a_idxs, VecDbl& a_
432453
a_idxs.assign(ix, ix + 3);
433454
return true;
434455
}
456+
else
457+
{
458+
a_idxs.clear();
459+
a_wts.clear();
460+
}
435461
return false;
436-
} // GmTriSearchImpl::InterpWeights
462+
} // GmTriSearchImpl::InterpWeightsTriangleIdx
437463
//------------------------------------------------------------------------------
438464
/// \brief Write the internals to a string.
439465
/// \return The string.
@@ -635,11 +661,24 @@ void TriSearchUnitTests::testInterpWeights()
635661
std::vector<int> idxs, baseIdx;
636662
std::vector<double> wt, baseWt;
637663
Pt3d pt(.5, .2, 0);
638-
tris->InterpWeights(pt, idxs, wt);
664+
TS_ASSERT(tris->InterpWeights(pt, idxs, wt));
639665
baseIdx = {0, 2, 1};
640666
baseWt = {.5, .3, .2};
641667
TS_ASSERT_EQUALS_VEC(baseIdx, idxs);
642668
TS_ASSERT_DELTA_VEC(baseWt, wt, 1e-7);
669+
670+
int triangleIdx;
671+
TS_ASSERT(tris->InterpWeightsTriangleIdx({.25, .75, 0}, triangleIdx, idxs, wt));
672+
TS_ASSERT_EQUALS(3, triangleIdx);
673+
baseIdx = {0, 1, 3};
674+
TS_ASSERT_EQUALS_VEC(baseIdx, idxs);
675+
baseWt = {.25, .25, .5};
676+
TS_ASSERT_DELTA_VEC(baseWt, wt, 1e-7);
677+
678+
TS_ASSERT(!tris->InterpWeightsTriangleIdx({0, 1.25, 0}, triangleIdx, idxs, wt));
679+
TS_ASSERT_EQUALS(XM_NONE, triangleIdx);
680+
TS_ASSERT_EQUALS_VEC(VecInt(), idxs);
681+
TS_ASSERT_DELTA_VEC(VecDbl(), wt, 1e-7);
643682
} // TriSearchUnitTests::testInterpWeights
644683
//------------------------------------------------------------------------------
645684
/// \brief tests setting the point activity

xmsinterp/geometry/GmTriSearch.h

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ class GmTriSearch
5050
virtual bool InterpWeights(const Pt3d& a_pt,
5151
std::vector<int>& a_idxs,
5252
std::vector<double>& a_wts) = 0;
53+
virtual bool InterpWeightsTriangleIdx(const Pt3d& a_pt,
54+
int& a_triangleIdx,
55+
std::vector<int>& a_idxs,
56+
std::vector<double>& a_wts) = 0;
5357

5458
virtual std::string ToString() const = 0;
5559

0 commit comments

Comments
 (0)