From 6797ac4c3ac47c5ed169ef6e6f918fd80c8b9fa1 Mon Sep 17 00:00:00 2001 From: Pete Gadomski Date: Mon, 15 Sep 2014 18:32:03 +0000 Subject: [PATCH] Catch bad_numeric_cast, rethrow better Also, use long, not int32_t, for rxp moving average. This seems slightly clearer to me. Per @abellgithub's comments on 51b11498cdc4f11c765cf723625100d826aa4da6. --- src/drivers/rxp/RxpReader.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/drivers/rxp/RxpReader.cpp b/src/drivers/rxp/RxpReader.cpp index e2423ea7e7..7bc391893a 100644 --- a/src/drivers/rxp/RxpReader.cpp +++ b/src/drivers/rxp/RxpReader.cpp @@ -353,7 +353,7 @@ Inclination RxpInclFixPointcloud::getInclMovingAverage() const { auto middle = m_incl.begin() + m_inclIdx; double time = (m_incl[m_inclIdx].time + m_incl[m_inclIdx + 1].time) / 2; - int32_t roll(0), pitch(0); + long roll(0), pitch(0); for (auto it = middle - m_windowSize + 1; it != middle + m_windowSize + 1; ++it) @@ -361,11 +361,21 @@ Inclination RxpInclFixPointcloud::getInclMovingAverage() const roll += it->roll; pitch += it->pitch; } - return { - time, - boost::numeric_cast(static_cast(roll / (m_windowSize * 2))), - boost::numeric_cast(static_cast(pitch / (m_windowSize * 2))) - }; + try + { + return { + time, + boost::numeric_cast(roll + / (static_cast(m_windowSize) * 2)), + boost::numeric_cast(pitch + / (static_cast(m_windowSize) * 2)) + }; + } + catch (boost::numeric::bad_numeric_cast& e) + { + throw pdal_error("Unable to calculate inclination moving average due " + " to invalid roll or pitch value."); + } }