| 1 |
/* |
| 2 |
This program is free software; you can redistribute it and/or modify |
| 3 |
it under the terms of the GNU General Public License as published by |
| 4 |
the Free Software Foundation; either version 2 of the License, or |
| 5 |
(at your option) any later version. |
| 6 |
|
| 7 |
This program is distributed in the hope that it will be useful, |
| 8 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 |
GNU General Public License for more details. |
| 11 |
|
| 12 |
You should have received a copy of the GNU General Public License |
| 13 |
along with this program; if not, write to the Free Software |
| 14 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "calibration_set.hpp" |
| 18 |
|
| 19 |
#include <algorithm> |
| 20 |
#include <boost/bind.hpp> |
| 21 |
#include <cstdlib> |
| 22 |
#include <sstream> |
| 23 |
#include <stdexcept> |
| 24 |
#include <cstdlib> |
| 25 |
|
| 26 |
// SubdeviceCalibration |
| 27 |
|
| 28 |
const Polynomial& SubdeviceCalibration::polynomial(unsigned channel, unsigned range) const |
| 29 |
{ |
| 30 |
std::pair<unsigned, unsigned> channelRange; |
| 31 |
channelRange.first = channel; |
| 32 |
channelRange.second = range; |
| 33 |
std::map<std::pair<unsigned, unsigned>, Polynomial>::const_iterator it = |
| 34 |
std::find_if(_polynomials.begin(), _polynomials.end(), boost::bind(&SubdeviceCalibration::channelRangeMatch, channel, range, _1)); |
| 35 |
if(it == _polynomials.end()) |
| 36 |
{ |
| 37 |
std::ostringstream message; |
| 38 |
message << __FUNCTION__ << ": failed to find calibration polynomial for channel " << channel << ", range " << range << " .\n"; |
| 39 |
throw std::invalid_argument(message.str()); |
| 40 |
} |
| 41 |
return it->second; |
| 42 |
} |
| 43 |
|
| 44 |
void SubdeviceCalibration::insertPolynomial(const Polynomial &polynomial, unsigned channel, unsigned range) |
| 45 |
{ |
| 46 |
std::pair<unsigned, unsigned> channelRange; |
| 47 |
channelRange.first = channel; |
| 48 |
channelRange.second = range; |
| 49 |
_polynomials[channelRange] = polynomial; |
| 50 |
}; |
| 51 |
|
| 52 |
// private functions |
| 53 |
|
| 54 |
bool SubdeviceCalibration::channelRangeMatch(unsigned channel, unsigned range, |
| 55 |
const std::pair<std::pair<unsigned, unsigned>, Polynomial> &calibration) |
| 56 |
{ |
| 57 |
std::pair<unsigned, unsigned> channelRange1; |
| 58 |
channelRange1.first = channel; |
| 59 |
channelRange1.second = range; |
| 60 |
const std::pair<unsigned, unsigned> &channelRange2 = calibration.first; |
| 61 |
if(channelRange1.first == channelRange2.first || channelRange1.first == allChannels || channelRange2.first == allChannels) |
| 62 |
{ |
| 63 |
if(channelRange1.second == channelRange2.second || channelRange1.second == allRanges || channelRange2.second == allRanges) |
| 64 |
{ |
| 65 |
return true; |
| 66 |
} |
| 67 |
} |
| 68 |
return false; |
| 69 |
} |