Skip to content

Commit

Permalink
Refs #4165: Parameter to resize RectangularDetectors.
Browse files Browse the repository at this point in the history
Note to testers: Use the ResizeRectangularDetector algorithm to change the X/Y
size of a bank.
Try ray-tracing to it (e.g. peak finding or RayTracerTester).
It should look scaled in the instrument view.
  • Loading branch information
Janik Zikovsky committed Nov 22, 2011
1 parent 888f7ba commit d6f4e19
Show file tree
Hide file tree
Showing 18 changed files with 577 additions and 25 deletions.
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/Algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ set ( SRC_FILES
src/RemovePromptPulse.cpp
src/RenameWorkspace.cpp
src/ReplaceSpecialValues.cpp
src/ResizeRectangularDetector.cpp
src/SANSDirectBeamScaling.cpp
src/Scale.cpp
src/ScaleX.cpp
Expand Down Expand Up @@ -308,6 +309,7 @@ set ( INC_FILES
inc/MantidAlgorithms/RemovePromptPulse.h
inc/MantidAlgorithms/RenameWorkspace.h
inc/MantidAlgorithms/ReplaceSpecialValues.h
inc/MantidAlgorithms/ResizeRectangularDetector.h
inc/MantidAlgorithms/SANSDirectBeamScaling.h
inc/MantidAlgorithms/Scale.h
inc/MantidAlgorithms/ScaleX.h
Expand Down Expand Up @@ -464,6 +466,7 @@ set ( TEST_FILES
test/RemovePromptPulseTest.h
test/RenameWorkspaceTest.h
test/ReplaceSpecialValuesTest.h
test/ResizeRectangularDetectorTest.h
test/ScaleTest.h
test/ScaleXTest.h
test/ShiftLogTimeTest.h
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#ifndef MANTID_ALGORITHMS_RESIZERECTANGULARDETECTOR_H_
#define MANTID_ALGORITHMS_RESIZERECTANGULARDETECTOR_H_

#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"

namespace Mantid
{
namespace Algorithms
{

/** ResizeRectangularDetector : TODO: DESCRIPTION
@date 2011-11-22
Copyright © 2011 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://svn.mantidproject.org/mantid/trunk/Code/Mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport ResizeRectangularDetector : public API::Algorithm
{
public:
ResizeRectangularDetector();
virtual ~ResizeRectangularDetector();

virtual const std::string name() const;
virtual int version() const;
virtual const std::string category() const;

private:
virtual void initDocs();
void init();
void exec();


};


} // namespace Algorithms
} // namespace Mantid

#endif /* MANTID_ALGORITHMS_RESIZERECTANGULARDETECTOR_H_ */
124 changes: 124 additions & 0 deletions Code/Mantid/Framework/Algorithms/src/ResizeRectangularDetector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*WIKI*
This algorithm will resize a [[RectangularDetector]] by applying X and Y scaling factors.
Each pixel's position will be modifed relative to the 0,0 point of the detector by these factors.
Typically, a RectangularDetector is constructed around its center, so this would scale the detector around its center.
This only works on [[RectangularDetector]]s. Banks formed by e.g. tubes cannot be scaled in this way.
Internally, this sets the "scalex" and "scaley" parameters on the [[RectangularDetector]].
Note that the scaling is relative to the original size, and is not cumulative: that is,
if you Resize * 2 and again * 3, your final detector is 3 times larger than the original, not 6 times.
Note: As of this writing, the algorithm does NOT modify the shape of individual pixels. This means
that algorithms based on solid angle calculations might be off.
Ray-tracing (e.g. peak finding) are unaffected.
See also [[MoveInstrumentComponent]] and [[RotateInstrumentComponent]] for other ways to move components.
*WIKI*/

#include "MantidAlgorithms/ResizeRectangularDetector.h"
#include "MantidKernel/System.h"
#include "MantidAPI/WorkspaceProperty.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/IComponent.h"
#include "MantidGeometry/Instrument/RectangularDetector.h"

using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::Geometry;

namespace Mantid
{
namespace Algorithms
{

// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(ResizeRectangularDetector)



//----------------------------------------------------------------------------------------------
/** Constructor
*/
ResizeRectangularDetector::ResizeRectangularDetector()
{
}

//----------------------------------------------------------------------------------------------
/** Destructor
*/
ResizeRectangularDetector::~ResizeRectangularDetector()
{
}


//----------------------------------------------------------------------------------------------
/// Algorithm's name for identification. @see Algorithm::name
const std::string ResizeRectangularDetector::name() const { return "ResizeRectangularDetector";};

/// Algorithm's version for identification. @see Algorithm::version
int ResizeRectangularDetector::version() const { return 1;};

/// Algorithm's category for identification. @see Algorithm::category
const std::string ResizeRectangularDetector::category() const { return "DataHandling\\Instrument";}

//----------------------------------------------------------------------------------------------
/// Sets documentation strings for this algorithm
void ResizeRectangularDetector::initDocs()
{
this->setWikiSummary("Resize a RectangularDetector in X and/or Y.");
this->setOptionalMessage("Resize a RectangularDetector in X and/or Y.");
}

//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void ResizeRectangularDetector::init()
{
// When used as a sub-algorithm the workspace name is not used - hence the "Anonymous" to satisfy the validator
declareProperty(new WorkspaceProperty<MatrixWorkspace>("Workspace","Anonymous",Direction::InOut));
declareProperty("ComponentName","",
"The name of the RectangularDetector to resize.");
declareProperty("ScaleX", 1.0,
"The scaling factor in the X direction. Default 1.0");
declareProperty("ScaleY", 1.0,
"The scaling factor in the Y direction. Default 1.0");
}

//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void ResizeRectangularDetector::exec()
{
MatrixWorkspace_sptr WS = getProperty("Workspace");
std::string ComponentName = getPropertyValue("ComponentName");
double ScaleX = getProperty("ScaleX");
double ScaleY = getProperty("ScaleY");

if (ComponentName.empty())
throw std::runtime_error("You must specify a ComponentName.");

Instrument_const_sptr inst = WS->getInstrument();
IComponent_const_sptr comp;

comp = inst->getComponentByName(ComponentName);
if (!comp)
throw std::runtime_error("Component with name " + ComponentName + " was not found.");

RectangularDetector_const_sptr det = boost::dynamic_pointer_cast<const RectangularDetector>(comp);
if (!det)
throw std::runtime_error("Component with name " + ComponentName + " is not a RectangularDetector.");

Geometry::ParameterMap& pmap = WS->instrumentParameters();
// Add a parameter for the new scale factors
pmap.addDouble(det.get(), "scalex", ScaleX);
pmap.addDouble(det.get(), "scaley", ScaleY);
}



} // namespace Mantid
} // namespace Algorithms
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#ifndef MANTID_ALGORITHMS_RESIZERECTANGULARDETECTORTEST_H_
#define MANTID_ALGORITHMS_RESIZERECTANGULARDETECTORTEST_H_

#include "MantidAlgorithms/ResizeRectangularDetector.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/RectangularDetector.h"
#include "MantidKernel/System.h"
#include "MantidKernel/Timer.h"
#include "MantidKernel/V3D.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
#include <cxxtest/TestSuite.h>
#include <iomanip>
#include <iostream>

using namespace Mantid;
using namespace Mantid::Kernel;
using namespace Mantid::Geometry;
using namespace Mantid::Algorithms;
using namespace Mantid::API;

class ResizeRectangularDetectorTest : public CxxTest::TestSuite
{
public:
void test_Init()
{
ResizeRectangularDetector alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
}

void test_exec()
{
Mantid::DataObjects::EventWorkspace_sptr ews = WorkspaceCreationHelper::createEventWorkspaceWithFullInstrument(2, 10);

MatrixWorkspace_sptr ws = boost::dynamic_pointer_cast<MatrixWorkspace>(ews);

ResizeRectangularDetector alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setProperty("Workspace", ws) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("ComponentName", "bank1") );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("ScaleX", 2.0) );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("ScaleY", 0.5) );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );

Instrument_const_sptr inst = ws->getInstrument();
boost::shared_ptr<const RectangularDetector> det = boost::dynamic_pointer_cast<const RectangularDetector>(inst->getComponentByName("bank1"));

// Bank 1 got scaled
V3D pos;
pos = det->getAtXY(1,1)->getPos();
TS_ASSERT( ws->instrumentParameters().contains(det.get(), "scalex") );
TS_ASSERT( ws->instrumentParameters().contains(det.get(), "scaley") );
TS_ASSERT_EQUALS( pos,V3D(0.008*2, 0.008*0.5, 5.0) );
TS_ASSERT_DELTA( det->xstep(), 0.008 * 2, 1e-6);

// Bank 2 did not get scaled
det = boost::dynamic_pointer_cast<const RectangularDetector>(inst->getComponentByName("bank2"));
pos = det->getAtXY(1,1)->getPos();
TS_ASSERT_EQUALS( pos,V3D(0.008*1.0, 0.008*1.0, 10.0) );
TS_ASSERT_DELTA( det->xstep(), 0.008 * 1, 1e-6);

}


};


#endif /* MANTID_ALGORITHMS_RESIZERECTANGULARDETECTORTEST_H_ */
26 changes: 18 additions & 8 deletions Code/Mantid/Framework/DataHandling/src/MoveInstrumentComponent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/*WIKI*
This moves an instrument component, e.g. a bank or a pixel.
You can either specify an absolute position or a relative position.
The relative position will be applied to the current position, so applying this twice will move the detector twice.
*WIKI*/
//----------------------------------------------------------------------
Expand All @@ -20,7 +23,7 @@ DECLARE_ALGORITHM(MoveInstrumentComponent)
/// Sets documentation strings for this algorithm
void MoveInstrumentComponent::initDocs()
{
this->setWikiSummary(" Moves an instrument component to a new position. ");
this->setWikiSummary("Moves an instrument component to a new position.");
this->setOptionalMessage("Moves an instrument component to a new position.");
}

Expand All @@ -37,13 +40,20 @@ MoveInstrumentComponent::MoveInstrumentComponent()
void MoveInstrumentComponent::init()
{
// When used as a sub-algorithm the workspace name is not used - hence the "Anonymous" to satisfy the validator
declareProperty(new WorkspaceProperty<MatrixWorkspace>("Workspace","Anonymous",Direction::InOut));
declareProperty("ComponentName","");
declareProperty("DetectorID",-1);
declareProperty("X",0.0);
declareProperty("Y",0.0);
declareProperty("Z",0.0);
declareProperty("RelativePosition",true);
declareProperty(new WorkspaceProperty<MatrixWorkspace>("Workspace","Anonymous",Direction::InOut),
"The name of the workspace for which the new instrument configuration will have an effect. Any other workspaces stored in the analysis data service will be unaffected.");
declareProperty("ComponentName","",
"The name of the component to move. Component names are defined in the instrument definition files.");
declareProperty("DetectorID",-1,
"The ID of the detector to move. If both the component name and the detector ID are set the latter will be used.");
declareProperty("X",0.0,
"The x-part of the new location vector.");
declareProperty("Y",0.0,
"The y-part of the new location vector.");
declareProperty("Z",0.0,
"The z-part of the new location vector.");
declareProperty("RelativePosition",true,
"The property defining how the (X,Y,Z) vector should be interpreted. If true it is a vector relative to the initial component's position. Otherwise it is a new position in the absolute co-ordinates.");
}

/** Executes the algorithm.
Expand Down
5 changes: 4 additions & 1 deletion Code/Mantid/Framework/Geometry/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set ( SRC_FILES
src/Instrument/Parameter.cpp
src/Instrument/ParameterMap.cpp
src/Instrument/RectangularDetector.cpp
src/Instrument/RectangularDetectorPixel.cpp
src/Instrument/XMLlogfile.cpp
src/MDGeometry/IMDDimension.cpp
src/MDGeometry/IMDDimensionFactory.cpp
Expand Down Expand Up @@ -112,6 +113,7 @@ set ( INC_FILES
inc/MantidGeometry/Instrument/ParameterFactory.h
inc/MantidGeometry/Instrument/ParameterMap.h
inc/MantidGeometry/Instrument/RectangularDetector.h
inc/MantidGeometry/Instrument/RectangularDetectorPixel.h
inc/MantidGeometry/Instrument/XMLlogfile.h
inc/MantidGeometry/MDGeometry/IMDDimension.h
inc/MantidGeometry/MDGeometry/IMDDimensionFactory.h
Expand Down Expand Up @@ -203,17 +205,18 @@ set ( TEST_FILES
test/ObjectTest.h
test/OneToOneSpectraDetectorMapTest.h
test/OrientedLatticeTest.h
test/ParameterMapTest.h
test/ParCompAssemblyTest.h
test/ParComponentFactoryTest.h
test/ParDetectorTest.h
test/ParInstrumentTest.h
test/ParObjCompAssemblyTest.h
test/ParObjComponentTest.h
test/ParameterMapTest.h
test/ParametrizedComponentTest.h
test/PlaneTest.h
test/PolygonEdgeTest.h
test/QuadrilateralTest.h
test/RectangularDetectorPixelTest.h
test/RectangularDetectorTest.h
test/ReflectionConditionTest.h
test/RotCounterTest.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ namespace Mantid
//! Rotate the IComponent by an angle in degrees with respect to an axis.
virtual void rotate(double,const Kernel::V3D&) = 0;
//! Get the position relative to the parent IComponent (absolute if no parent)
virtual const Kernel::V3D & getRelativePos() const = 0;
virtual const Kernel::V3D getRelativePos() const = 0;
//! Get the position of the IComponent. Tree structure is traverse through the parent chain
virtual Kernel::V3D getPos() const = 0;
//! Get the relative Orientation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ namespace Mantid
void rotate(double,const Kernel::V3D&);

//! Get the position relative to the parent IComponent (absolute if no parent)
const Kernel::V3D & getRelativePos() const;
virtual const Kernel::V3D getRelativePos() const;

//! Get the position of the IComponent. Tree structure is traverse through the parent chain
virtual Kernel::V3D getPos() const;
Expand Down

0 comments on commit d6f4e19

Please sign in to comment.