NorMIT-nav  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 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 
34 #include "cxBasicVideoSource.h"
35 
36 #include <QTimer>
37 #include <vtkImageChangeInformation.h>
38 #include <vtkImageData.h>
39 #include "cxImage.h"
40 #include "cxBoundingBox3D.h"
41 #include "cxLogger.h"
42 #include "cxVolumeHelpers.h"
43 #include "cxTypeConversions.h"
44 
45 
46 namespace cx
47 {
48 
50  mStreaming(false)
51 {
52  mStatus = "USE_DEFAULT";
53  mRedirecter = vtkSmartPointer<vtkImageChangeInformation>::New(); // used for forwarding only.
54 
55 
56  vtkImageDataPtr emptyImage = generateVtkImageData(Eigen::Array3i(3,3,1),
57  Vector3D(1,1,1),
58  0);
59  mEmptyImage.reset(new Image(uid, emptyImage));
60  mReceivedImage = mEmptyImage;
61  mRedirecter->SetInputData(mEmptyImage->getBaseVtkImageData());
62 
63  mTimeout = true; // must start invalid
64  mTimeoutTimer = new QTimer(this);
65  mTimeoutTimer->setInterval(1000);
66  connect(mTimeoutTimer, SIGNAL(timeout()), this, SLOT(timeout()));
67 }
68 
70 {
71  stop();
72 }
73 
75 {
76  if (mTimeoutTimer)
77  {
78  mTimeoutTimer->setParent(NULL);
79  delete mTimeoutTimer;
80  mTimeoutTimer = NULL;
81  }
82 
83  mTimeout = timeout;
84 }
85 
87 {
88  return mReceivedImage->getUid();
89 }
91 {
92  return mReceivedImage->getName();
93 }
94 
95 void BasicVideoSource::timeout()
96 {
97  if (mTimeout)
98  return;
99 
100  reportWarning("Timeout!");
101  mTimeout = true;
102  emit newFrame();
103 }
104 
106 {
107  return this->isConnected() && !mTimeout;
108 }
109 
111 {
112  return mReceivedImage->getAcquisitionTime().toMSecsSinceEpoch();
113 }
114 
116 {
117  return mReceivedImage->getAdvancedTimeInfo();
118 }
119 
121 {
122  return (mReceivedImage!=mEmptyImage);
123 }
124 
126 {
127  return this->isConnected() && mStreaming;
128 }
129 
130 void BasicVideoSource::setResolution(double resolution)
131 {
132  mRedirecter->SetOutputSpacing(resolution, resolution, resolution);
133 }
134 
136 {
137  mRedirecter->Update();
138  return mRedirecter->GetOutput();
139 }
140 
142 {
143  if (mStreaming)
144  return;
145 
146  mStreaming = true;
147 
148  if (mTimeoutTimer)
149  {
150  mTimeoutTimer->start();
151  }
152 
153  if (!this->isConnected())
154  return;
155 
156  emit streaming(true);
157  emit newFrame();
158 }
159 
161 {
162  if (!mStreaming)
163  return;
164 
165  mStreaming = false;
166  if (mTimeoutTimer)
167  {
168  mTimeoutTimer->stop();
169  }
170 
171  emit streaming(false);
172  emit newFrame();
173 }
174 
176 {
177  if (mStatus!="USE_DEFAULT")
178  return mStatus;
179 
180 // { return mStatus; }
181  if (!this->isConnected())
182  return "Not connected";
183  if (!this->isStreaming())
184  return "Not streaming";
185  if (!this->validData())
186  return "Timeout";
187 // return "Running";
188  return "";
189 }
190 
191 
193 {
194  bool wasConnected = this->isConnected();
195 
196  if (input)
197  {
198  mReceivedImage = input;
199  }
200  else
201  {
202  if (mReceivedImage)
203  {
204  // create an empty image with the same uid as the stream.
205  mEmptyImage.reset(new Image(mReceivedImage->getUid(), mEmptyImage->getBaseVtkImageData()));
206  }
207  mReceivedImage = mEmptyImage;
208  }
209  mRedirecter->SetInputData(mReceivedImage->getBaseVtkImageData());
210  mRedirecter->Update();
211 
212  if (mTimeoutTimer)
213  {
214  mTimeout = false;
215  mTimeoutTimer->start();
216  }
217 
218  if (this->isConnected() != wasConnected)
219  emit connected(this->isConnected());
220 
221  emit newFrame();
222 }
223 
224 } // 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:48
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:91
virtual void start()
start streaming
A volumetric data set.
Definition: cxImage.h:66
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:63
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.