Skip to content

Commit

Permalink
Refs #5187 handle line plot from a Workspace2D
Browse files Browse the repository at this point in the history
in SliceViewer, but only for perpendicular lines. Uses Rebin2D
  • Loading branch information
Janik Zikovsky committed May 1, 2012
1 parent e1f5f5b commit 7083e29
Show file tree
Hide file tree
Showing 11 changed files with 248 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/API/inc/MantidAPI/IMDWorkspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ namespace Mantid

/// Method to generate a line plot through a MD-workspace
virtual void getLinePlot(const Mantid::Kernel::VMD & start, const Mantid::Kernel::VMD & end,
Mantid::API::MDNormalization normalize, std::vector<coord_t> & x, std::vector<signal_t> & y, std::vector<signal_t> & e) const = 0;
Mantid::API::MDNormalization normalize, std::vector<coord_t> & x, std::vector<signal_t> & y, std::vector<signal_t> & e) const;


IMDIterator* createIterator(Mantid::Geometry::MDImplicitFunction * function = NULL) const;
Expand Down
42 changes: 42 additions & 0 deletions Code/Mantid/Framework/API/src/IMDWorkspace.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "MantidAPI/IMDWorkspace.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/IPropertyManager.h"
#include "MantidKernel/VMD.h"

using Mantid::Kernel::VMD;

namespace Mantid
{
Expand Down Expand Up @@ -57,6 +60,45 @@ namespace Mantid
return this->getSignalAtCoord(coords.getBareArray(), normalization);
}

//-----------------------------------------------------------------------------------------------
/** Obtain coordinates for a line plot through a IMDWorkspace.
* Cross the workspace from start to end points, recording the signal along the line.
* Sets the x,y vectors to the histogram bin boundaries and counts
*
* @param start :: coordinates of the start point of the line
* @param end :: coordinates of the end point of the line
* @param normalize :: how to normalize the signal
* @param x :: is set to the boundaries of the bins, relative to start of the line.
* @param y :: is set to the normalized signal for each bin. Length = length(x) - 1
*/
void IMDWorkspace::getLinePlot(const Mantid::Kernel::VMD & start, const Mantid::Kernel::VMD & end,
Mantid::API::MDNormalization normalize, std::vector<coord_t> & x, std::vector<signal_t> & y, std::vector<signal_t> & e) const
{
// TODO: Don't use a fixed number of points later
size_t numPoints = 200;

VMD step = (end-start) / double(numPoints);
double stepLength = step.norm();

// These will be the curve as plotted
x.clear();
y.clear();
e.clear();
for (size_t i=0; i<numPoints; i++)
{
// Coordinate along the line
VMD coord = start + step * double(i);
// Record the position along the line
x.push_back(static_cast<coord_t>(stepLength * double(i)));

signal_t yVal = this->getSignalAtCoord(coord.getBareArray(), normalize);
y.push_back(yVal);
e.push_back(0.0);
}
// And the last point
x.push_back( (end-start).norm() );
}

}
}

Expand Down
3 changes: 1 addition & 2 deletions Code/Mantid/Framework/API/src/MatrixWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1569,8 +1569,7 @@ namespace Mantid
void MatrixWorkspace::getLinePlot(const Mantid::Kernel::VMD & start, const Mantid::Kernel::VMD & end,
Mantid::API::MDNormalization normalize, std::vector<coord_t> & x, std::vector<signal_t> & y, std::vector<signal_t> & e) const
{
UNUSED_ARG(start);UNUSED_ARG(end);UNUSED_ARG(normalize);UNUSED_ARG(x);UNUSED_ARG(y);UNUSED_ARG(e);
//throw std::runtime_error("MatrixWorkspace::getLinePlot() not yet implemented.");
IMDWorkspace::getLinePlot(start,end,normalize,x,y,e);
}

//------------------------------------------------------------------------------------
Expand Down
13 changes: 13 additions & 0 deletions Code/Mantid/Framework/Algorithms/src/Rebin2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ namespace Mantid
declareProperty(new PropertyWithValue<bool>("UseFractionalArea", false, Direction::Input),
"Flag to turn on the using the fractional area tracking RebinnedOutput workspace\n."
"Default is false.");
declareProperty(new PropertyWithValue<bool>("Transpose", false),
"Run the Transpose algorithm on the resulting matrix.");
}

/**
Expand Down Expand Up @@ -153,6 +155,17 @@ namespace Mantid
boost::dynamic_pointer_cast<RebinnedOutput>(outputWS)->finalize();
}
normaliseOutput(outputWS, inputWS);

bool Transpose = this->getProperty("Transpose");
if (Transpose)
{
IAlgorithm_sptr alg = this->createSubAlgorithm("Transpose", 0.9, 1.0);
alg->setProperty("InputWorkspace", outputWS);
alg->setPropertyValue("OutputWorkspace", "__anonymous");
alg->execute();
outputWS = alg->getProperty("OutputWorkspace");
}

setProperty("OutputWorkspace", outputWS);
}

Expand Down
5 changes: 4 additions & 1 deletion Code/Mantid/MantidQt/API/src/AlgorithmRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ namespace API
AlgorithmRunner::AlgorithmRunner() :
m_finishedObserver(*this, &AlgorithmRunner::handleAlgorithmFinishedNotification),
m_progressObserver(*this, &AlgorithmRunner::handleAlgorithmProgressNotification),
m_errorObserver(*this, &AlgorithmRunner::handleAlgorithmErrorNotification)
m_errorObserver(*this, &AlgorithmRunner::handleAlgorithmErrorNotification),
m_asyncRebinResult(NULL)
{
}

Expand Down Expand Up @@ -44,7 +45,9 @@ namespace API
{
m_asyncRebinResult->tryWait(1000);
delete m_asyncRebinResult;
m_asyncRebinResult = NULL;
}
m_asyncRebinAlg.reset();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ namespace SliceViewer
void setShowHandles(bool shown);
void setShowLine(bool shown);
void setCreationMode(bool creation);
void setAngleSnapMode(bool angleSnap);
void setAngleSnap(double snapDegrees);

///@return the snap-to X interval
double getSnapX()
Expand Down Expand Up @@ -165,6 +167,12 @@ namespace SliceViewer
/// Show the central line?
bool m_showLine;

/// If true, then you are in always-snap mode
bool m_angleSnapMode;

/// Angle (in degrees) to snap to.
double m_angleSnap;

};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,13 @@ public slots:
/// Signal emitted when changing fixed bin width mode
void changedFixedBinWidth(bool, double);


private:
Mantid::API::IAlgorithm_sptr applyMDWorkspace(Mantid::API::IMDWorkspace_sptr ws);
Mantid::API::IAlgorithm_sptr applyMatrixWorkspace(Mantid::API::MatrixWorkspace_sptr ws);

/// Logger object
Mantid::Kernel::Logger & g_log;

// -------------------------- Widgets ----------------------------

/// Auto-generated UI controls.
Expand Down
30 changes: 25 additions & 5 deletions Code/Mantid/MantidQt/SliceViewer/src/LineOverlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ namespace SliceViewer
m_plot(plot),
m_snapEnabled(false),
m_snapX(0.1), m_snapY(0.1), m_snapLength(0),
m_shown(true), m_showHandles(true), m_showLine(true)
m_shown(true), m_showHandles(true), m_showLine(true),
m_angleSnapMode(false), m_angleSnap(45)
{
m_creation = true; // Will create with the mouse
m_rightButton = false;
Expand Down Expand Up @@ -158,6 +159,21 @@ namespace SliceViewer
this->update();
}

//----------------------------------------------------------------------------------------------
/** Turn angle snap on/off
* @param angleSnap :: true for always angle snap. */
void LineOverlay::setAngleSnapMode(bool angleSnap)
{
m_angleSnapMode = angleSnap;
}

/** Sets the angle increments to snap to.
* @param snapDegrees :: snap amount, in degrees */
void LineOverlay::setAngleSnap(double snapDegrees)
{
m_angleSnap = snapDegrees;
}

//----------------------------------------------------------------------------------------------
/// @return point A's position in plot coordinates
const QPointF & LineOverlay::getPointA() const
Expand Down Expand Up @@ -395,7 +411,7 @@ namespace SliceViewer
double length = 0;

// Adjust the current mouse position if needed.
if ((m_snapLength > 0) || shiftPressed)
if ((m_snapLength > 0) || shiftPressed || m_angleSnapMode)
{
// This is the distance between the fixed and dragged point
QPointF currentDiff;
Expand All @@ -406,9 +422,13 @@ namespace SliceViewer

// Limit angles to 45 degree increments with shift pressed
angle = atan2(currentDiff.y(), currentDiff.x());
// Round angle to closest 45 degrees
if (shiftPressed)
angle = Utils::rounddbl(angle / (M_PI / 4.0)) * (M_PI / 4.0);
// Round angle to closest 45 degrees, if in angle snap mode
if (shiftPressed || m_angleSnapMode)
{
// Convert the snap angle from degrees to radians
double angleSnapRad = m_angleSnap / (180.0 / M_PI);
angle = Utils::rounddbl(angle / angleSnapRad) * angleSnapRad;
}

// Round length to m_snapLength, if specified
length = sqrt(currentDiff.x()*currentDiff.x() + currentDiff.y()*currentDiff.y());
Expand Down

0 comments on commit 7083e29

Please sign in to comment.