CustusX  15.3.4-beta
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxProbeImpl.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 
33 #include "cxProbeImpl.h"
34 
35 #include <QStringList>
36 #include "cxVideoSource.h"
37 #include "cxLogger.h"
38 #include "cxProbeSector.h"
39 #include "cxProbeAdapterRTSource.h"
40 #include "cxTypeConversions.h"
41 #include "cxVector3D.h"
42 
43 #include "cxDataLocations.h"
45 
46 namespace cx
47 {
48 
49 ProbeImplPtr ProbeImpl::New(QString instrumentUid, QString scannerUid, ProbeXmlConfigParserPtr xml)
50 {
51  ProbeImpl* object = new ProbeImpl(instrumentUid, scannerUid);
52  ProbeImplPtr retval(object);
53  object->mSelf = retval;
54  retval->initProbeXmlConfigParser(xml);
55  retval->initConfigId();
56  return retval;
57 }
58 
59 bool ProbeImpl::isValid() const
60 {
61  return this->getProbeData("active").getType() != ProbeDefinition::tNONE;
62 }
63 
65 {
66  QStringList retval;
67  for (std::map<QString, VideoSourcePtr>::iterator iter=mSource.begin(); iter!=mSource.end(); ++iter)
68  retval << iter->first;
69  return retval;
70 }
71 
73 {
74  if (mSource.empty())
75  return VideoSourcePtr();
76  if (uid=="active")
77  uid = mActiveUid;
78  if (mSource.count(uid))
79  return mSource.find(uid)->second;
80  return mSource.begin()->second;
81 }
82 
84 {
85  ProbeDefinition retval;
86 
87  if (uid=="active")
88  uid = mActiveUid;
89  if (mProbeData.count(uid))
90  retval = mProbeData.find(uid)->second;
91  else if (mProbeData.count("default"))
92  retval = mProbeData.find("default")->second;
93  else
94  retval = mProbeData.begin()->second;
95 
96  // ensure uid is matching the requested uid even if not found.
97  retval.setUid(uid);
98  return retval;
99 }
100 
102 {
103  ProbeSectorPtr retval(new ProbeSector());
104  retval->setData(this->getProbeData(uid));
105  return retval;
106 }
107 
108 void ProbeImpl::addXml(QDomNode& dataNode)
109 {
110  QDomDocument doc = dataNode.ownerDocument();
111  dataNode.toElement().setAttribute("config", mConfigurationId);
112 }
113 
114 void ProbeImpl::parseXml(QDomNode& dataNode)
115 {
116  if (dataNode.isNull())
117  return;
118  QString cfg = dataNode.toElement().attribute("config");
119  if (cfg.isEmpty())
120  return;
121  this->applyNewConfigurationWithId(cfg);
122 }
123 
124 QStringList ProbeImpl::getConfigIdList() const
125 {
126  if (!this->hasRtSource())
127  return QStringList();
128  QStringList configIdList = mXml->getConfigIdList(
129  this->getInstrumentScannerId(), this->getInstrumentId(), this->getRtSourceName());
130  return configIdList;
131 }
132 
133 QString ProbeImpl::getConfigName(QString configString)
134 {
135  ProbeXmlConfigParser::Configuration config = this->getConfiguration(configString);
136  return config.mName;
137 }
138 
139 QString ProbeImpl::getConfigId() const
140 {
141  if (this->getProbeData().getUseDigitalVideo())
142  return "Digital";
143  return mConfigurationId;
144 }
145 
147 {
148  if (!this->hasRtSource())
149  return "";
150  QStringList retval;
151  retval << this->getInstrumentScannerId() << this->getInstrumentId() << this->getRtSourceName() << this->getConfigId();
152  return retval.join(":");
153 }
154 
156 {
157  this->setConfigId(uid);
158  this->applyConfig();
159  emit activeConfigChanged();
160 }
161 
162 void ProbeImpl::applyConfig()
163 {
164  this->updateProbeSector();
165  this->updateTemporalCalibration();
166  this->setSoundSpeedCompensationFactor(mSoundSpeedCompensationFactor);
167  emit sectorChanged();
168 }
169 
171 {
172  mOverrideTemporalCalibration = true;
173  mTemporalCalibration = val;
174  for (std::map<QString, ProbeDefinition>::iterator iter=mProbeData.begin(); iter!=mProbeData.end(); ++iter)
175  iter->second.setTemporalCalibration(mTemporalCalibration);
176 }
177 
179 {
180  if(similar(mSoundSpeedCompensationFactor, factor))
181  return;
182  mSoundSpeedCompensationFactor = factor;
183  for (std::map<QString, ProbeDefinition>::iterator iter=mProbeData.begin(); iter!=mProbeData.end(); ++iter)
184  iter->second.applySoundSpeedCompensationFactor(mSoundSpeedCompensationFactor);
185  emit sectorChanged();
186 }
187 
189 {
190  if (probeSector.getUid().isEmpty())
191  probeSector.setUid(mActiveUid);
192 
193  mProbeData[probeSector.getUid()] = probeSector;
194  emit sectorChanged();
195 }
196 
198 {
199  CX_ASSERT(source); // not handled after refactoring - add clear method??
200  if (!source)
201  return;
202 
203  // uid already exist: check if base object is the same
204  if (mSource.count(source->getUid()))
205  {
206  VideoSourcePtr old = mSource.find(source->getUid())->second;
207 
208  ProbeAdapterRTSourcePtr oldAdapter;
209  oldAdapter = boost::dynamic_pointer_cast<ProbeAdapterRTSource>(old);
210  // check for identity, ignore if no change
211  if (oldAdapter && (source==oldAdapter->getBaseSource()))
212  return;
213  }
214 
215  // must have same uid as original: the uid identifies the video source
216  mSource[source->getUid()].reset(new ProbeAdapterRTSource(source->getUid(), mSelf.lock(), source));
217  emit sectorChanged();
218 
219  emit videoSourceAdded(mSource[source->getUid()]);
220 }
221 
223 {
224  if (!source)
225  return;
226  if (!mSource.count(source->getUid()))
227  return;
228 
229  mSource.erase(source->getUid());
230  mProbeData.erase(source->getUid());
231  this->applyConfig();//May need to re-create config, as the old ProbeDefinition may be deleted
232 }
233 
234 void ProbeImpl::setActiveStream(QString uid)
235 {
236  if (uid.isEmpty())
237  return;
238  mActiveUid = uid;
239  emit sectorChanged();
240 }
241 
243 {
244  return mActiveUid;
245 }
246 
248 {
250 
251  int index = this->getConfigIdList().indexOf(config.mConfigId);
252  if (index<0)
253  return;
254  if (index!=0)
255  --index;
256 
257  mXml->removeConfig(config.mUsScanner, config.mUsProbe, config.mRtSource, config.mConfigId);
258  if (index < this->getConfigIdList().size())
259  this->applyNewConfigurationWithId(this->getConfigIdList()[index]);
260  emit sectorChanged();
261 }
262 
263 void ProbeImpl::saveCurrentConfig(QString uid, QString name)
264 {
266  config.mConfigId = uid;
267  config.mName = name;
268  config = createConfigurationFromProbeData(config, this->getProbeData("active"));
269 
270  mXml->saveCurrentConfig(config);
271  this->applyNewConfigurationWithId(uid);
272 }
273 
274 //void ProbeImpl::useDigitalVideo(bool digitalStatus)
275 //{
276 // mDigitalInterface = digitalStatus;
277 // if (mDigitalInterface)
278 // this->applyNewConfigurationWithId("Digital");
279 //}
280 
281 //bool ProbeImpl::isUsingDigitalVideo() const
282 //{
283 // return mDigitalInterface;
284 //}
285 
287 {
288  QStringList rtSourceList = mXml->getRtSourceList(this->getInstrumentScannerId(), this->getInstrumentId());
289  if (rtSourceList.empty())
290  return QString();
291  QString rtSource = rtSourceList.at(0);
292  if (this->getProbeData().getUseDigitalVideo())
293  rtSource = "Digital";
294  return rtSource;
295 }
296 
297 ProbeImpl::ProbeImpl(QString instrumentUid, QString scannerUid) :
298  mInstrumentUid(instrumentUid),
299  mScannerUid(scannerUid),
300  mSoundSpeedCompensationFactor(1.0),
301  mOverrideTemporalCalibration(false),
302  mTemporalCalibration(0.0)
303 {
304  ProbeDefinition probeData;
305  mProbeData[probeData.getUid()] = probeData;
306  mActiveUid = probeData.getUid();
307 }
308 
309 void ProbeImpl::initProbeXmlConfigParser(ProbeXmlConfigParserPtr xml = ProbeXmlConfigParserPtr())
310 {
311  if (!xml)
312  {
313  QString xmlFileName = cx::DataLocations::getRootConfigPath() + QString("/tool/ProbeCalibConfigs.xml");
314  mXml.reset(new ProbeXmlConfigParserImpl(xmlFileName));
315  } else
316  mXml = xml;
317 }
318 
319 void ProbeImpl::initConfigId()
320 {
321  QStringList configs = this->getConfigIdList();
322  if (!configs.isEmpty())
323  this->applyNewConfigurationWithId(configs[0]);
324  else
325  {
326  reportWarning(QString("Found no probe configuration for:\n"
327  "scanner=[%1] instrument=[%2].\n"
328  "Check that your %3 file contains entries\n"
329  "<USScanner> <Name>%1</Name> ... <USProbe> <Name>%2</Name>").arg(mScannerUid).arg(mInstrumentUid).arg(mXml->getFileName()));
330  }
331 }
332 
334 {
335  if (mConfig.mConfigId != this->getConfigId())
336  {
337  mConfig = this->getConfiguration(this->getConfigId());
338  }
339  return mConfig;
340 }
341 
343 {
344  if (mConfig.mConfigId != uid)
345  {
347  if(this->hasRtSource())
348  config = mXml->getConfiguration(mScannerUid, mInstrumentUid, this->getRtSourceName(), uid);
349  return config;
350  }
351  return mConfig;
352 }
353 
354 QString ProbeImpl::getInstrumentId() const
355 {
356  return mInstrumentUid;
357 }
358 
359 QString ProbeImpl::getInstrumentScannerId() const
360 {
361  return mScannerUid;
362 }
363 
364 bool ProbeImpl::hasRtSource() const
365 {
366  return !(this->getRtSourceName().isEmpty());
367 }
368 
369 void ProbeImpl::setConfigId(QString uid)
370 {
371  mConfigurationId = uid;
372 }
373 
374 void ProbeImpl::updateProbeSector()
375 {
376  if(this->isValidConfigId() && !this->getProbeData().getUseDigitalVideo())
377  {
378  ProbeDefinition probeSector = this->createProbeSector();
379  this->setProbeSector(probeSector);
380  }
381 }
382 
383 bool ProbeImpl::isValidConfigId()
384 {
385  //May need to create ProbeXmlConfigParser::isValidConfig(...) also
386  return !this->getConfiguration().isEmpty();
387 }
388 
389 ProbeDefinition ProbeImpl::createProbeSector()
390 {
392  ProbeDefinition probeSector = createProbeDataFromConfiguration(config);
393  probeSector.setUid(mActiveUid);
394  return probeSector;
395 }
396 
397 void ProbeImpl::updateTemporalCalibration()
398 {
399  if (mOverrideTemporalCalibration)
400  this->setTemporalCalibration(mTemporalCalibration);
401 }
402 
403 
404 } //namespace cx
< a easy-to-work-with struct for a specific xml configuration
ProbeXmlConfigParser::Configuration getConfiguration()
virtual void saveCurrentConfig(QString uid, QString name)
save current config to disk under ids (uid,name).
ProbeXmlConfigParser::Configuration createConfigurationFromProbeData(ProbeXmlConfigParser::Configuration basis, ProbeDefinition data)
#define CX_ASSERT(statement)
Definition: cxLogger.h:128
virtual void removeCurrentConfig()
remove the current config from disk
virtual void setActiveStream(QString uid)
virtual VideoSourcePtr getRTSource(QString uid="active") const
Return a VideoSource for the given uid. Use 'active' to get the default stream.
Definition: cxProbeImpl.cpp:72
QString getRtSourceName() const
VideoSource that applies the parameters from a Probe to the VideoSource.
virtual QString getConfigName(QString uid)
QString getUid() const
virtual void setTemporalCalibration(double val)
virtual QString getActiveStream() const
virtual void parseXml(QDomNode &dataNode)
virtual QString getConfigurationPath() const
virtual ProbeSectorPtr getSector(QString uid="active")
virtual void removeRTSource(VideoSourcePtr source)
bool similar(const DoubleBoundingBox3D &a, const DoubleBoundingBox3D &b, double tol)
void videoSourceAdded(VideoSourcePtr source)
virtual void setProbeSector(ProbeDefinition probeSector)
virtual void applyNewConfigurationWithId(QString uid)
TYPE getType() const
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
void setUid(QString uid)
Implementation of abstract interface ProbeXmlConfigParser Interface to ProbeCalibConfigs.xml.
boost::shared_ptr< class VideoSource > VideoSourcePtr
boost::shared_ptr< ProbeXmlConfigParser > ProbeXmlConfigParserPtr
void sectorChanged()
< Return a ProbeSectorPtr for the given uid. Use 'active' to get the default.
virtual QString getConfigId() const
boost::shared_ptr< class ProbeSector > ProbeSectorPtr
Definition of characteristics for an Ultrasound Probe Sector.
Definition: cxProbeData.h:118
virtual ProbeDefinition getProbeData(QString uid="active") const
Return a ProbeData for the given uid. Use 'active' to get the default.
Definition: cxProbeImpl.cpp:83
void activeConfigChanged()
static QString getRootConfigPath()
return path to root config folder. May be replaced with getExistingConfigPath()
virtual void setSoundSpeedCompensationFactor(double val)
virtual QStringList getAvailableVideoSources()
Return a list of all available video source. The default is one with uid=='active'.
Definition: cxProbeImpl.cpp:64
static ProbeImplPtr New(QString instrumentUid, QString scannerUid, ProbeXmlConfigParserPtr xml=ProbeXmlConfigParserPtr())
Definition: cxProbeImpl.cpp:49
QString mName
Name of config set.
virtual QStringList getConfigIdList() const
virtual bool isValid() const
Definition: cxProbeImpl.cpp:59
virtual void addXml(QDomNode &dataNode)
Utility functions for drawing an US Probe sector.
Definition: cxProbeSector.h:59
boost::shared_ptr< class ProbeImpl > ProbeImplPtr
boost::shared_ptr< ProbeAdapterRTSource > ProbeAdapterRTSourcePtr
ProbeDefinition createProbeDataFromConfiguration(ProbeXmlConfigParser::Configuration config)
virtual void setRTSource(VideoSourcePtr source)