CustusX  2023.01.05-dev+develop.0da12
An IGT application
cxBasicVideoSource.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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 
13 #include "cxBasicVideoSource.h"
14 
15 #include <QTimer>
16 #include <vtkImageChangeInformation.h>
17 #include <vtkImageData.h>
18 #include "cxImage.h"
19 #include "cxBoundingBox3D.h"
20 #include "cxLogger.h"
21 #include "cxVolumeHelpers.h"
22 #include "cxTypeConversions.h"
23 
24 
25 namespace cx
26 {
27 
29  mStreaming(false)
30 {
31  mStatus = "USE_DEFAULT";
32  mRedirecter = vtkSmartPointer<vtkImageChangeInformation>::New(); // used for forwarding only.
33 
34 
35  vtkImageDataPtr emptyImage = generateVtkImageData(Eigen::Array3i(3,3,1),
36  Vector3D(1,1,1),
37  0);
38  mEmptyImage.reset(new Image(uid, emptyImage));
39  mReceivedImage = mEmptyImage;
40  mRedirecter->SetInputData(mEmptyImage->getBaseVtkImageData());
41 
42  mTimeout = true; // must start invalid
43  mTimeoutTimer = new QTimer(this);
44  mTimeoutTimer->setInterval(1000);
45  connect(mTimeoutTimer, SIGNAL(timeout()), this, SLOT(timeout()));
46 }
47 
49 {
50  stop();
51 }
52 
54 {
55  if (mTimeoutTimer)
56  {
57  mTimeoutTimer->setParent(NULL);
58  delete mTimeoutTimer;
59  mTimeoutTimer = NULL;
60  }
61 
62  mTimeout = timeout;
63 }
64 
66 {
67  return mReceivedImage->getUid();
68 }
70 {
71  return mReceivedImage->getName();
72 }
73 
74 void BasicVideoSource::timeout()
75 {
76  if (mTimeout)
77  return;
78 
79  reportWarning("Timeout!");
80  mTimeout = true;
81  emit newFrame();
82 }
83 
85 {
86  return this->isConnected() && !mTimeout;
87 }
88 
90 {
91  return mReceivedImage->getAcquisitionTime().toMSecsSinceEpoch();
92 }
93 
95 {
96  return mReceivedImage->getAdvancedTimeInfo();
97 }
98 
100 {
101  return (mReceivedImage!=mEmptyImage);
102 }
103 
105 {
106  return this->isConnected() && mStreaming;
107 }
108 
109 void BasicVideoSource::setResolution(double resolution)
110 {
111  mRedirecter->SetOutputSpacing(resolution, resolution, resolution);
112 }
113 
115 {
116  mRedirecter->Update();
117  return mRedirecter->GetOutput();
118 }
119 
121 {
122  if (mStreaming)
123  return;
124 
125  mStreaming = true;
126 
127  if (mTimeoutTimer)
128  {
129  mTimeoutTimer->start();
130  }
131 
132  if (!this->isConnected())
133  return;
134 
135  emit streaming(true);
136  emit newFrame();
137 }
138 
140 {
141  if (!mStreaming)
142  return;
143 
144  mStreaming = false;
145  if (mTimeoutTimer)
146  {
147  mTimeoutTimer->stop();
148  }
149 
150  emit streaming(false);
151  emit newFrame();
152 }
153 
155 {
156  if (mStatus!="USE_DEFAULT")
157  return mStatus;
158 
159 // { return mStatus; }
160  if (!this->isConnected())
161  return "Not connected";
162  if (!this->isStreaming())
163  return "Not streaming";
164  if (!this->validData())
165  return "Timeout";
166 // return "Running";
167  return "";
168 }
169 
170 
172 {
173  bool wasConnected = this->isConnected();
174 
175  if (input)
176  {
177  mReceivedImage = input;
178  }
179  else
180  {
181  if (mReceivedImage)
182  {
183  // create an empty image with the same uid as the stream.
184  mEmptyImage.reset(new Image(mReceivedImage->getUid(), mEmptyImage->getBaseVtkImageData()));
185  }
186  mReceivedImage = mEmptyImage;
187  }
188  mRedirecter->SetInputData(mReceivedImage->getBaseVtkImageData());
189  mRedirecter->Update();
190 
191  if (mTimeoutTimer)
192  {
193  mTimeout = false;
194  mTimeoutTimer->start();
195  }
196 
197  if (this->isConnected() != wasConnected)
198  emit connected(this->isConnected());
199 
200  emit newFrame();
201 }
202 
203 } // namespace cx
void overrideTimeout(bool timeout)
void connected(bool on)
emitted when source is connected/disconnected
virtual double getTimestamp()
void streaming(bool on)
emitted when streaming started/stopped
virtual void setResolution(double resolution)
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:27
virtual void stop()
stop streaming
virtual bool isStreaming() const
return true when the source is streaming data.
virtual TimeInfo getAdvancedTimeInfo()
virtual QString getUid()
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
virtual void start()
start streaming
A volumetric data set.
Definition: cxImage.h:45
BasicVideoSource(QString uid="<none>")
vtkImageDataPtr generateVtkImageData(Eigen::Array3i dim, Vector3D spacing, const unsigned char initValue, int components)
virtual QString getStatusString() const
status text describing the stream state, display instead of stream when the stream is invalid...
virtual vtkImageDataPtr getVtkImageData()
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
virtual bool validData() const
return true is data stream is ok to display. This is a heuristic based on the data rate...
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
void setInput(ImagePtr input)
void newFrame()
emitted when a new frame has arrived (getVtkImageData() returns something new). info/status/name/vali...
virtual QString getName()
virtual bool isConnected() const
return true when a connection to the data source is established.
Namespace for all CustusX production code.