Skip to content

Commit

Permalink
Refs #5827 classes for the scaling transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
jmborr committed Aug 31, 2012
1 parent 77ca62b commit e76dfc9
Show file tree
Hide file tree
Showing 8 changed files with 433 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Code/Mantid/Framework/API/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
set ( SRC_FILES
# src/GridDomain.cpp
# src/GridDomain1D.cpp
src/TransformScaleFactory.cpp
src/LinearScale.cpp
src/LogarithmScale.cpp
src/Algorithm.cpp
src/AlgorithmFactory.cpp
src/AlgorithmHasProperty.cpp
Expand Down Expand Up @@ -110,6 +115,12 @@ set ( SRC_UNITY_IGNORE_FILES src/CompositeFunction.cpp

set ( INC_FILES
# inc/MantidAPI/BoxCtrlChangesInterface.h
# inc/MantidAPI/GridDomain.h
# inc/MantidAPI/GridDomain1D.h
inc/MantidAPI/ITransformScale.h
inc/MantidAPI/TransformScaleFactory.h
inc/MantidAPI/LinearScale.h
inc/MantidAPI/LogarithmScale.h
inc/MantidAPI/Algorithm.h
inc/MantidAPI/AlgorithmFactory.h
inc/MantidAPI/AlgorithmHasProperty.h
Expand Down
70 changes: 70 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/ITransformScale.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#ifndef MANTID_API_ITRANSFORMSCALE_H_
#define MANTID_API_ITRANSFORMSCALE_H_

//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include <boost/shared_ptr.hpp>

#include "MantidAPI/DllConfig.h"
#include "MantidKernel/Logger.h"

namespace Mantid
{
namespace API
{
/*Base class representing a scaling transformation acting on a one-dimensional grid domain
@author Jose Borreguero
@date Aug/28/2012
Copyright &copy; 2009 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>.
*/

class MANTID_API_DLL ITransformScale
{
public:
/// Virtual destructor
virtual ~ITransformScale() {}
virtual void initialize();
virtual const std::string name() const { return "ITransformScale"; }
/// The scaling transformation. Override with method of derived classes
virtual void transform( std::vector<double> &gd );

protected:
static Kernel::Logger& g_log;
}; // class ITransformScale

/// typedef for a shared pointer
typedef boost::shared_ptr<ITransformScale> ITransformScale_sptr;

} // namespace API
} // namespace Mantid

#define DECLARE_TRANSFORMSCALE(classname) \
namespace { \
Mantid::Kernel::RegistrationHelper register_trs_##classname( \
((Mantid::API::TransformScaleFactory::Instance().subscribe<classname>(#classname)) \
, 0)); \
}


#endif /*MANTID_API_ITRANSFORMSCALE_H_*/
58 changes: 58 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/LinearScale.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#ifndef MANTID_API_LINEARSCALE_H_
#define MANTID_API_LINEARSCALE_H_

//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/DllConfig.h"
#include "MantidAPI/ITransformScale.h"

#include <boost/shared_ptr.hpp>

#include <stdexcept>

namespace Mantid
{
namespace API
{
/*Base class representing a linear scaling transformation acting on a one-dimensional grid domain
@author Jose Borreguero
@date Aug/28/2012
Copyright &copy; 2009 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>.
*/

class MANTID_API_DLL LinearScale : public ITransformScale
{
public:
LinearScale();
~LinearScale();
/// The scaling transformation. First and last elements of the grid remain unchanged
const std::string name() const { return "LinearScale"; }
void transform( std::vector<double> &gd );
}; // class LinearScale


} // namespace API
} // namespace Mantid

#endif /*MANTID_API_LINEARSCALE_H_*/
62 changes: 62 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/LogarithmScale.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#ifndef MANTID_API_LOGARITHMSCALE_H_
#define MANTID_API_LOGARITHMSCALE_H_

//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include <boost/shared_ptr.hpp>
#include <cmath>

#include "MantidAPI/DllConfig.h"
#include "MantidAPI/ITransformScale.h"

namespace Mantid
{
namespace API
{
/*Base class representing a logarithm scaling transformation acting on a one-dimensional grid domain
@author Jose Borreguero
@date Aug/28/2012
Copyright &copy; 2009 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>.
*/

class MANTID_API_DLL LogarithmScale : public ITransformScale
{
public:
LogarithmScale() : m_base(M_E) {};
~LogarithmScale();
void setBase( double &base);
/// The scaling transformation. First and last elements of the grid remain unchanged
void transform( std::vector<double> &gd );
const std::string name() const { return "LogarithmScale"; }

private:
double m_base; //base of the logarithm

}; // class LogarithmScale


} // namespace API
} // namespace Mantid

#endif /*MANTID_API_LOGARITHMSCALE_H_*/
84 changes: 84 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/TransformScaleFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#ifndef MANTID_API_TRANSFORMSCALEFACTORY_H_
#define MANTID_API_TRANSFORMSCALEFACTORY_H_

#include "MantidAPI/DllConfig.h"
#include "MantidKernel/ClassMacros.h"
#include "MantidKernel/DynamicFactory.h"
#include "MantidKernel/SingletonHolder.h"

namespace Mantid
{
//
// Forward declarations
//
namespace Kernel
{
class IPropertyManager;
}
namespace API
{
//
// Forward declarations
//
class ITransformScale;

/**
Constructs a scaling transform object from a string
Copyright &copy; 2012 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class MANTID_API_DLL TransformScaleFactoryImpl : public Kernel::DynamicFactory<ITransformScale>
{
public:
/// Returns scaling transform
boost::shared_ptr<ITransformScale> create(const std::string &type ) const;

private:
friend struct Mantid::Kernel::CreateUsingNew<TransformScaleFactoryImpl>;

/// Private Constructor for singleton class
TransformScaleFactoryImpl();
///Private Destructor for singleton
virtual ~TransformScaleFactoryImpl();
/// Override the DynamicFactory::createUnwrapped() method. We don't want it used here.
ITransformScale* createUnwrapped(const std::string& className) const;
/// Private copy constructor - NO COPY ALLOWED
TransformScaleFactoryImpl(const TransformScaleFactoryImpl&);
/// Private assignment operator - NO ASSIGNMENT ALLOWED
TransformScaleFactoryImpl& operator = (const TransformScaleFactoryImpl&);
///reference to the logger class
Kernel::Logger& g_log;
// Do not use default methods
};

///Forward declaration of a specialisation of SingletonHolder for TransformScaleFactoryImpl (needed for dllexport/dllimport) and a typedef for it.
#ifdef _WIN32
// this breaks new namespace declaraion rules; need to find a better fix
template class MANTID_API_DLL Mantid::Kernel::SingletonHolder<TransformScaleFactoryImpl>;
#endif /* _WIN32 */
typedef MANTID_API_DLL Mantid::Kernel::SingletonHolder<TransformScaleFactoryImpl> TransformScaleFactory;


} // namespace API
} // namespace Mantid

#endif /* MANTID_API_TRANSFORMSCALEFACTORY_H_ */
38 changes: 38 additions & 0 deletions Code/Mantid/Framework/API/src/LinearScale.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include <iostream>

#include "MantidAPI/LinearScale.h"
#include "MantidAPI/GridDomain1D.h"
#include "MantidAPI/TransformScaleFactory.h"

namespace Mantid
{
namespace API
{

DECLARE_TRANSFORMSCALE(LinearScale);

/* Transform the grid to adopt a linear scale
* @param gd a grid object
*/
void LinearScale::transform( std::vector<double> &gd )
{
size_t n = gd.size();
if(n<3) return; //no need to process
double startX = gd.front();
double endX = gd.back();
double spacing = (endX-startX)/double(n);
double x = startX + spacing;
for(auto it=gd.begin()+1; it!=gd.end()-1; it++)
{
*it = x;
x += spacing;
}
}



} // namespace API
} // namespace Mantid
57 changes: 57 additions & 0 deletions Code/Mantid/Framework/API/src/LogarithmScale.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include <iostream>
#include <cmath>
#include <stdexcept>

#include "MantidAPI/LogarithmScale.h"
#include "MantidAPI/GridDomain1D.h"
#include "MantidAPI/TransformScaleFactory.h"

namespace Mantid
{
namespace API
{

DECLARE_TRANSFORMSCALE(LogarithmScale);

void LogarithmScale::setBase( double &base)
{
if( base <=0 )
{
g_log.error("Error: logarithm base must be a positive number");
}
m_base = base;
}

/* Transform the grid to adopt a logarithm scale.
* @param gd a grid object
*/
void LogarithmScale::transform( std::vector<double> &gd )
{
double a = 1.0/log(m_base);
size_t n = gd.size();
if(n==0) return; //no need to process
if( gd.front() <=0 )
{
g_log.error("LogarithmScale::transform Error: negative values");
return;
}
if(n<3) return; //no need to process
double startX = a * log( gd.front() );
double endX = a * log( gd.back() );
double spacing = (endX-startX)/double(n);

double x = startX + spacing;
for(auto it=gd.begin()+1; it!=gd.end()-1; it++)
{
*it = pow(m_base,x);
x += spacing;
}
}



} // namespace API
} // namespace Mantid

0 comments on commit e76dfc9

Please sign in to comment.