CustusX  15.8
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxRegistrationApplicator.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
34 
35 #include "cxData.h"
36 #include "cxTypeConversions.h"
37 #include "cxLogger.h"
38 
40 #include "cxFrameForest.h"
41 
42 
43 
44 namespace cx
45 {
46 
47 RegistrationApplicator::RegistrationApplicator(const std::map<QString, DataPtr> &source) :
48  mSource(source)
49 {
50 
51 }
52 
54 {
55 
56 }
57 
63 void RegistrationApplicator::updateRegistration(QDateTime oldTime, RegistrationTransform delta_pre_rMd, DataPtr movingData)
64 {
65  FrameForest forest(mSource);
66  QDomNode moving = forest.getNode(movingData->getUid());
67  QDomNode fixed = forest.getNode(delta_pre_rMd.mFixed);
68 
69  // if no parent, assume this is an operation on the moving image, thus set fixed to its parent.
70  if (delta_pre_rMd.mFixed == "")
71  {
72  fixed = forest.getNode(movingData->getParentSpace());
73  }
74  QDomNode movingBase = forest.getOldestAncestorNotCommonToRef(moving, fixed);
75 
76  std::vector<DataPtr> allMovingData = forest.getDataFromDescendantsAndSelf(movingBase);
77 
78  report(QString(""
79  "Update Registration using\n"
80  "\tFixed:\t%1\n"
81  "\tMoving:\t%2\n"
82  "\tDelta matrix (rMd'=Delta*rMd)\n"
83  "%3")
84  .arg(delta_pre_rMd.mFixed)
85  .arg(movingData->getUid())
86  .arg(qstring_cast(delta_pre_rMd.mValue)));
87 
88  this->updateTransform(oldTime, allMovingData, delta_pre_rMd);
89 
90  // reconnect only if master and target are unconnected, i.e. doesnt share a common ancestor.
91  // If we are registrating inside an already connected tree we only want to change transforms,
92  // not change the topology of the tree.
93  if (forest.getOldestAncestor(moving) != forest.getOldestAncestor(fixed))
94  {
95  // connect the target to the master's ancestor, i.e. replace targetBase with masterAncestor:
96 
97  QDomNode fixedAncestor = forest.getOldestAncestor(fixed);
98  QString fixedAncestorUid = fixedAncestor.toElement().tagName();
99 
100  QString newFixedSpace = fixedAncestorUid;
101 
102  // if fixedAncestor is a data, insert a pure space above it
103  if (mSource.count(fixedAncestorUid) && mSource[fixedAncestorUid]->getParentSpace()=="")
104  {
105  newFixedSpace = this->generateNewSpaceUid();
106  ParentSpace newParentSpace(newFixedSpace, delta_pre_rMd.mTimestamp, delta_pre_rMd.mType);
107  this->changeParentSpace(oldTime, mSource[fixedAncestorUid], newParentSpace);
108  }
109 
110  QString movingBaseUid = movingBase.toElement().tagName();
111  // if movingBaseUid is a data, then move the space above it
112  if (mSource.count(movingBaseUid))
113  {
114  movingBaseUid = mSource[movingBaseUid]->getParentSpace();
115  }
116 
117  // change parent space of all moving spaces connected to base
118  ParentSpace newParentSpace(newFixedSpace, delta_pre_rMd.mTimestamp, delta_pre_rMd.mType);
119  this->changeParentSpace(oldTime, allMovingData, movingBaseUid, newParentSpace);
120  }
121 }
122 
123 QString RegistrationApplicator::generateNewSpaceUid() const
124 {
125  int max = 0;
126  std::map<QString, DataPtr>::const_iterator iter;
127  for (iter = mSource.begin(); iter != mSource.end(); ++iter)
128  {
129  QStringList parentList = qstring_cast(iter->second->getParentSpace()).split("_");
130  if (parentList.size() < 2)
131  continue;
132  max = std::max(max, parentList[1].toInt());
133  }
134  QString parentFrame = "frame_" + qstring_cast(max + 1);
135  return parentFrame;
136 }
137 
138 void RegistrationApplicator::updateTransform(QDateTime oldTime, std::vector<DataPtr> data, RegistrationTransform delta_pre_rMd)
139 {
140  // update the transform on all target data:
141  for (unsigned i=0; i<data.size(); ++i)
142  {
143  RegistrationTransform newTransform = delta_pre_rMd;
144  newTransform.mValue = delta_pre_rMd.mValue * data[i]->get_rMd();
145  data[i]->get_rMd_History()->updateRegistration(oldTime, newTransform);
146 
147  report("Updated registration of data " + data[i]->getName());
148  }
149 }
150 
151 void RegistrationApplicator::changeParentSpace(QDateTime oldTime, std::vector<DataPtr> data, QString oldParentSpace, ParentSpace newParentSpace)
152 {
153  for (unsigned i=0; i<data.size(); ++i)
154  {
155  if (data[i]->getParentSpace() != oldParentSpace)
156  continue;
157  this->changeParentSpace(oldTime, data[i], newParentSpace);
158  }
159 }
160 
161 void RegistrationApplicator::changeParentSpace(QDateTime oldTime, DataPtr data, ParentSpace newParentSpace)
162 {
163  report(QString("Reset parent frame of %1 from [%2] to [%3].")
164  .arg(data->getName())
165  .arg(data->getParentSpace())
166  .arg(newParentSpace.mValue));
167 
168  data->get_rMd_History()->updateParentSpace(oldTime, newParentSpace);
169 }
170 
171 } // namespace cx
QString qstring_cast(const T &val)
QDomNode getOldestAncestor(QDomNode node)
Definition of a parent space event.
A graph combining Space dependencies between all Data.Relations between coordinate spaces among Data ...
Definition: cxFrameForest.h:85
std::vector< DataPtr > getDataFromDescendantsAndSelf(QDomNode node)
boost::shared_ptr< class Data > DataPtr
RegistrationApplicator(const std::map< QString, DataPtr > &source)
QDateTime mTimestamp
time the transform was registrated.
Transform3D mValue
value of transform
virtual void updateRegistration(QDateTime oldTime, RegistrationTransform deltaTransform, DataPtr data)
QDomNode getNode(QString frame)
QString mType
description of the kind if registration (manual, patient, landmark, coregistration etc) ...
A registration event and its transform.
void report(QString msg)
Definition: cxLogger.cpp:90
QDomNode getOldestAncestorNotCommonToRef(QDomNode child, QDomNode ref)