Skip to content

Commit

Permalink
Refs #4703 multiple original workspaces on a MD
Browse files Browse the repository at this point in the history
  • Loading branch information
Janik Zikovsky committed Feb 2, 2012
1 parent 0c4503d commit c45fdde
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 45 deletions.
23 changes: 12 additions & 11 deletions Code/Mantid/Framework/API/inc/MantidAPI/MDGeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ namespace API
void setBasisVector(size_t index, const Mantid::Kernel::VMD & vec);

// --------------------------------------------------------------------------------------------
bool hasOriginalWorkspace() const;
boost::shared_ptr<Workspace> getOriginalWorkspace() const;
void setOriginalWorkspace(boost::shared_ptr<Workspace> ws);
Mantid::API::CoordTransform * getTransformFromOriginal() const;
void setTransformFromOriginal(Mantid::API::CoordTransform * transform);
Mantid::API::CoordTransform * getTransformToOriginal() const;
void setTransformToOriginal(Mantid::API::CoordTransform * transform);
bool hasOriginalWorkspace(size_t index=0) const;
size_t numOriginalWorkspaces() const;
boost::shared_ptr<Workspace> getOriginalWorkspace(size_t index=0) const;
void setOriginalWorkspace(boost::shared_ptr<Workspace> ws, size_t index=0);
Mantid::API::CoordTransform * getTransformFromOriginal(size_t index=0) const;
void setTransformFromOriginal(Mantid::API::CoordTransform * transform, size_t index=0);
Mantid::API::CoordTransform * getTransformToOriginal(size_t index=0) const;
void setTransformToOriginal(Mantid::API::CoordTransform * transform, size_t index=0);

// --------------------------------------------------------------------------------------------
///@return the vector of the origin (in the original workspace) that corresponds to 0,0,0... in this workspace
Expand All @@ -110,8 +111,8 @@ namespace API
/// Vector of the dimensions used, in the order X Y Z t, etc.
std::vector<Mantid::Geometry::IMDDimension_sptr> m_dimensions;

/// Pointer to the original workspace, if this workspace is a coordinate transformation from an original workspace.
boost::shared_ptr<Workspace> m_originalWorkspace;
/// Pointer to the original workspace(s), if this workspace is a coordinate transformation from an original workspace.
std::vector<boost::shared_ptr<Workspace> > m_originalWorkspaces;

/// Vector of the basis vector (in the original workspace) for each dimension of this workspace.
std::vector<Mantid::Kernel::VMD> m_basisVectors;
Expand All @@ -120,10 +121,10 @@ namespace API
Mantid::Kernel::VMD m_origin;

/// Coordinate Transformation that goes from the original workspace to this workspace's coordinates.
Mantid::API::CoordTransform * m_transformFromOriginal;
std::vector<Mantid::API::CoordTransform *> m_transforms_FromOriginal;

/// Coordinate Transformation that goes from this workspace's coordinates to the original workspace coordinates.
Mantid::API::CoordTransform * m_transformToOriginal;
std::vector<Mantid::API::CoordTransform *> m_transforms_ToOriginal;

/// Poco delete notification observer object
Poco::NObserver<MDGeometry, Mantid::API::WorkspacePreDeleteNotification> m_delete_observer;
Expand Down
102 changes: 68 additions & 34 deletions Code/Mantid/Framework/API/src/MDGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ namespace API
/** Constructor
*/
MDGeometry::MDGeometry() :
m_originalWorkspace(),
m_transformFromOriginal(NULL), m_transformToOriginal(NULL),
m_originalWorkspaces(),
m_transforms_FromOriginal(), m_transforms_ToOriginal(),
m_delete_observer(*this, &MDGeometry::deleteNotificationReceived),
m_observingDelete(false)
{
Expand All @@ -28,10 +28,10 @@ namespace API
*/
MDGeometry::~MDGeometry()
{
if (m_transformFromOriginal)
delete m_transformFromOriginal;
if (m_transformToOriginal)
delete m_transformToOriginal;
for (size_t i=0; i<m_transforms_FromOriginal.size(); i++)
delete m_transforms_FromOriginal[i];
for (size_t i=0; i<m_transforms_ToOriginal.size(); i++)
delete m_transforms_ToOriginal[i];
if (m_observingDelete)
{
// Stop watching once object is deleted
Expand Down Expand Up @@ -225,21 +225,39 @@ namespace API

//---------------------------------------------------------------------------------------------------
/// @return true if the geometry is defined relative to another workspace.
bool MDGeometry::hasOriginalWorkspace() const
/// @param index :: index into the vector of original workspaces
bool MDGeometry::hasOriginalWorkspace(size_t index) const
{
return bool(m_originalWorkspace);
if (index >= m_originalWorkspaces.size())
return false;
return bool(m_originalWorkspaces[index]);
}

/// @return the number of original workspaces attached to this one
size_t MDGeometry::numOriginalWorkspaces() const
{
return m_originalWorkspaces.size();
}


//---------------------------------------------------------------------------------------------------
/// @return the original workspace to which the basis vectors relate
boost::shared_ptr<Workspace> MDGeometry::getOriginalWorkspace() const
/// @param index :: index into the vector of original workspaces
boost::shared_ptr<Workspace> MDGeometry::getOriginalWorkspace(size_t index) const
{
return m_originalWorkspace;
if (index >= m_originalWorkspaces.size())
throw std::runtime_error("MDGeometry::getOriginalWorkspace() invalid index.");
return m_originalWorkspaces[index];
}

/// Set the original workspace to which the basis vectors relate
void MDGeometry::setOriginalWorkspace(boost::shared_ptr<Workspace> ws)
/// @param ws :: original workspace shared pointer
/// @param index :: index into the vector of original workspaces
void MDGeometry::setOriginalWorkspace(boost::shared_ptr<Workspace> ws, size_t index)
{
m_originalWorkspace = ws;
if (index >= m_originalWorkspaces.size())
m_originalWorkspaces.resize(index+1);
m_originalWorkspaces[index] = ws;
// Watch for workspace deletions
if (!m_observingDelete)
{
Expand All @@ -261,48 +279,64 @@ namespace API
*/
void MDGeometry::deleteNotificationReceived(Mantid::API::WorkspacePreDeleteNotification_ptr notice)
{
if (bool(m_originalWorkspace))
for (size_t i=0; i<m_originalWorkspaces.size(); i++)
{
// Compare the pointer being deleted to the one stored as the original.
Workspace_sptr deleted = notice->object();
if (this->m_originalWorkspace == deleted)
Workspace_sptr original = m_originalWorkspaces[i];
if (original)
{
// Clear the reference
m_originalWorkspace.reset();
// Compare the pointer being deleted to the one stored as the original.
Workspace_sptr deleted = notice->object();
if (original == deleted)
{
// Clear the reference
m_originalWorkspaces[i].reset();
}
}
}
}

//---------------------------------------------------------------------------------------------------
/// @return Coordinate Transformation that goes from the original workspace to this workspace's coordinates.
Mantid::API::CoordTransform * MDGeometry::getTransformFromOriginal() const
/** @return Coordinate Transformation that goes from the original workspace to this workspace's coordinates.
* @param index :: index into the array of original workspaces */
Mantid::API::CoordTransform * MDGeometry::getTransformFromOriginal(size_t index) const
{
return m_transformFromOriginal;
if (index >= m_transforms_FromOriginal.size())
throw std::runtime_error("MDGeometry::getTransformFromOriginal(): invalid index.");
return m_transforms_FromOriginal[index];
}

/** Set Coordinate Transformation that goes from the original workspace to this workspace's coordinates.
* @param transform :: CoordTransform pointer (this assumes pointer ownership) */
void MDGeometry::setTransformFromOriginal(Mantid::API::CoordTransform * transform)
* @param transform :: CoordTransform pointer (this assumes pointer ownership)
* @param index :: index into the array of original workspaces */
void MDGeometry::setTransformFromOriginal(Mantid::API::CoordTransform * transform, size_t index)
{
if (m_transformFromOriginal)
delete m_transformFromOriginal;
m_transformFromOriginal = transform;
if (index >= m_transforms_FromOriginal.size())
m_transforms_FromOriginal.resize(index+1);
if (m_transforms_FromOriginal[index])
delete m_transforms_FromOriginal[index];
m_transforms_FromOriginal[index] = transform;
}

//---------------------------------------------------------------------------------------------------
/// @return Coordinate Transformation that goes from this workspace's coordinates to the original workspace coordinates.
Mantid::API::CoordTransform * MDGeometry::getTransformToOriginal() const
/** @return Coordinate Transformation that goes from this workspace's coordinates to the original workspace coordinates.
* @param index :: index into the array of original workspaces */
Mantid::API::CoordTransform * MDGeometry::getTransformToOriginal(size_t index) const
{
return m_transformToOriginal;
if (index >= m_transforms_ToOriginal.size())
throw std::runtime_error("MDGeometry::getTransformFromOriginal(): invalid index.");
return m_transforms_ToOriginal[index];
}

/** Set Coordinate Transformation that goes from this workspace's coordinates to the original workspace coordinates.
* @param transform :: CoordTransform pointer (this assumes pointer ownership) */
void MDGeometry::setTransformToOriginal(Mantid::API::CoordTransform * transform)
* @param transform :: CoordTransform pointer (this assumes pointer ownership)
* @param index :: index into the array of original workspaces */
void MDGeometry::setTransformToOriginal(Mantid::API::CoordTransform * transform, size_t index)
{
if (m_transformToOriginal)
delete m_transformToOriginal;
m_transformToOriginal = transform;
if (index >= m_transforms_ToOriginal.size())
m_transforms_ToOriginal.resize(index+1);
if (m_transforms_ToOriginal[index])
delete m_transforms_ToOriginal[index];
m_transforms_ToOriginal[index] = transform;
}

//---------------------------------------------------------------------------------------------------
Expand Down
13 changes: 13 additions & 0 deletions Code/Mantid/Framework/API/test/MDGeometryTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ class MDGeometryTest : public CxxTest::TestSuite
TS_ASSERT(g.hasOriginalWorkspace());
}

void test_OriginalWorkspace_multiple()
{
MDGeometry g;
TS_ASSERT(!g.hasOriginalWorkspace());
boost::shared_ptr<WorkspaceTester> ws0(new WorkspaceTester());
boost::shared_ptr<WorkspaceTester> ws1(new WorkspaceTester());
g.setOriginalWorkspace(ws0);
g.setOriginalWorkspace(ws1, 1);
TS_ASSERT(g.hasOriginalWorkspace());
TS_ASSERT(g.hasOriginalWorkspace(1));
TS_ASSERT_EQUALS(g.numOriginalWorkspaces(), 2);
}

/** If a MDGeometry workspace holds a pointer to an original workspace
* that gets deleted, remove the pointer and allow it to be destructed.
*/
Expand Down

0 comments on commit c45fdde

Please sign in to comment.