CustusX  16.5.0-rc9
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, bool silent)
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  if(!silent)
79  report(QString(""
80  "Update Registration using\n"
81  "\tFixed:\t%1\n"
82  "\tMoving:\t%2\n"
83  "\tDelta matrix (rMd'=Delta*rMd)\n"
84  "%3")
85  .arg(delta_pre_rMd.mFixed)
86  .arg(movingData->getUid())
87  .arg(qstring_cast(delta_pre_rMd.mValue)));
88 
89  this->updateTransform(oldTime, allMovingData, delta_pre_rMd, silent);
90 
91  // reconnect only if master and target are unconnected, i.e. doesnt share a common ancestor.
92  // If we are registrating inside an already connected tree we only want to change transforms,
93  // not change the topology of the tree.
94  if (forest.getOldestAncestor(moving) != forest.getOldestAncestor(fixed))
95  {
96  // connect the target to the master's ancestor, i.e. replace targetBase with masterAncestor:
97 
98  QDomNode fixedAncestor = forest.getOldestAncestor(fixed);
99  QString fixedAncestorUid = fixedAncestor.toElement().tagName();
100 
101  QString newFixedSpace = fixedAncestorUid;
102 
103  // if fixedAncestor is a data, insert a pure space above it
104  if (mSource.count(fixedAncestorUid) && mSource[fixedAncestorUid]->getParentSpace()=="")
105  {
106  newFixedSpace = this->generateNewSpaceUid();
107  ParentSpace newParentSpace(newFixedSpace, delta_pre_rMd.mTimestamp, delta_pre_rMd.mType);
108  this->changeParentSpace(oldTime, mSource[fixedAncestorUid], newParentSpace);
109  }
110 
111  QString movingBaseUid = movingBase.toElement().tagName();
112  // if movingBaseUid is a data, then move the space above it
113  if (mSource.count(movingBaseUid))
114  {
115  movingBaseUid = mSource[movingBaseUid]->getParentSpace();
116  }
117 
118  // change parent space of all moving spaces connected to base
119  ParentSpace newParentSpace(newFixedSpace, delta_pre_rMd.mTimestamp, delta_pre_rMd.mType);
120  this->changeParentSpace(oldTime, allMovingData, movingBaseUid, newParentSpace);
121  }
122 }
123 
124 QString RegistrationApplicator::generateNewSpaceUid() const
125 {
126  int max = 0;
127  std::map<QString, DataPtr>::const_iterator iter;
128  for (iter = mSource.begin(); iter != mSource.end(); ++iter)
129  {
130  QStringList parentList = qstring_cast(iter->second->getParentSpace()).split("_");
131  if (parentList.size() < 2)
132  continue;
133  max = std::max(max, parentList[1].toInt());
134  }
135  QString parentFrame = "frame_" + qstring_cast(max + 1);
136  return parentFrame;
137 }
138 
139 void RegistrationApplicator::updateTransform(QDateTime oldTime, std::vector<DataPtr> data, RegistrationTransform delta_pre_rMd, bool silent)
140 {
141  // update the transform on all target data:
142  for (unsigned i=0; i<data.size(); ++i)
143  {
144  RegistrationTransform newTransform = delta_pre_rMd;
145  newTransform.mValue = delta_pre_rMd.mValue * data[i]->get_rMd();
146  data[i]->get_rMd_History()->updateRegistration(oldTime, newTransform);
147 
148  if(!silent)
149  report("Updated registration of data " + data[i]->getName());
150  }
151 }
152 
153 void RegistrationApplicator::changeParentSpace(QDateTime oldTime, std::vector<DataPtr> data, QString oldParentSpace, ParentSpace newParentSpace)
154 {
155  for (unsigned i=0; i<data.size(); ++i)
156  {
157  if (data[i]->getParentSpace() != oldParentSpace)
158  continue;
159  this->changeParentSpace(oldTime, data[i], newParentSpace);
160  }
161 }
162 
163 void RegistrationApplicator::changeParentSpace(QDateTime oldTime, DataPtr data, ParentSpace newParentSpace)
164 {
165  report(QString("Reset parent frame of %1 from [%2] to [%3].")
166  .arg(data->getName())
167  .arg(data->getParentSpace())
168  .arg(newParentSpace.mValue));
169 
170  data->get_rMd_History()->updateParentSpace(oldTime, newParentSpace);
171 }
172 
173 } // 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
virtual void updateRegistration(QDateTime oldTime, RegistrationTransform deltaTransform, DataPtr data, bool silent=false)
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
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)