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