/* * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES) * * This file is part of Orfeo Toolbox * * https://www.orfeo-toolbox.org/ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef otbMaskMuParserFunctor_hxx #define otbMaskMuParserFunctor_hxx #include "otbMaskMuParserFunctor.h" namespace otb { namespace Functor { // constructor template MaskMuParserFunctor::MaskMuParserFunctor() : m_Parser(ParserType::New()), m_NbOfBands(0), m_ParserResult(0), m_Intensity(0), m_SpectralAngle(0) { } // Destructor template MaskMuParserFunctor::~MaskMuParserFunctor() { } template bool MaskMuParserFunctor::operator()(const PixelType& p) { double value; if (p.Size() != m_NbOfBands) { this->SetNumberOfBands(p.GetSize()); } // we fill the buffer for (unsigned int i = 0; i < m_NbOfBands; ++i) { m_AImage[i] = static_cast(p[i]); } // user defined variables m_Intensity = 0.0; for (unsigned int i = 0; i < m_NbOfBands; ++i) { m_Intensity += p[i]; } m_Intensity = m_Intensity / (static_cast(m_NbOfBands)); m_SpectralAngle = m_SpectralAngleFunctor(p, m_SpectralAngleReferencePixel); value = m_Parser->Eval(); return static_cast(value); } template const std::map& MaskMuParserFunctor::GetVar() const { return this->m_Parser->GetVar(); } template Parser::FunctionMapType MaskMuParserFunctor::GetFunList() const { return this->m_Parser->GetFunList(); } template void MaskMuParserFunctor::SetExpression(const std::string& expression) { m_Expression = expression; m_Parser->SetExpr(m_Expression); } /** Return the expression to be parsed */ template std::string MaskMuParserFunctor::GetExpression() const { return m_Expression; } template void MaskMuParserFunctor::SetNumberOfBands(unsigned int NbOfBands) { m_NbOfBands = NbOfBands; std::ostringstream varName; m_AImage.resize(NbOfBands, 0.0); for (unsigned int i = 0; i < NbOfBands; ++i) { varName << "b" << i + 1; m_Parser->DefineVar(varName.str(), &(m_AImage[i])); varName.str(""); } // customized data m_Parser->DefineVar("intensity", &m_Intensity); m_Parser->DefineVar("spectralAngle", &m_SpectralAngle); } /** Set the reference pixel used to compute the "spectralangle" parser variable */ template void MaskMuParserFunctor::SetSpectralAngleReferencePixel(const PixelType& refPixel) { m_SpectralAngleReferencePixel = refPixel; } /** Check the expression */ template bool MaskMuParserFunctor::CheckExpression() { return m_Parser->CheckExpr(); } } // end namespace Functor } // end namespace otb #endif