CustusX  15.8
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 {
51  mStatus = "USE_DEFAULT";
52  mRedirecter = vtkSmartPointer<vtkImageChangeInformation>::New(); // used for forwarding only.
53 
54 
55  vtkImageDataPtr emptyImage = generateVtkImageData(Eigen::Array3i(3,3,1),
56  Vector3D(1,1,1),
57  0);
58  mEmptyImage.reset(new Image(uid, emptyImage));
59  mReceivedImage = mEmptyImage;
60  mRedirecter->SetInputData(mEmptyImage->getBaseVtkImageData());
61 
62  mTimeout = true; // must start invalid
63  mTimeoutTimer = new QTimer(this);
64  mTimeoutTimer->setInterval(1000);
65  connect(mTimeoutTimer, SIGNAL(timeout()), this, SLOT(timeout()));
66 }
67 
69 {
70  stop();
71 }
72 
74 {
75  if (mTimeoutTimer)
76  {
77  mTimeoutTimer->setParent(NULL);
78  delete mTimeoutTimer;
79  mTimeoutTimer = NULL;
80  }
81 
82  mTimeout = timeout;
83 }
84 
86 {
87  return mReceivedImage->getUid();
88 }
90 {
91  return mReceivedImage->getName();
92 }
93 
94 void BasicVideoSource::timeout()
95 {
96  if (mTimeout)
97  return;
98 
99  reportWarning("Timeout!");
100  mTimeout = true;
101  emit newFrame();
102 }
103 
105 {
106  return this->isConnected() && !mTimeout;
107 }
108 
110 {
111  return mReceivedImage->getAcquisitionTime().toMSecsSinceEpoch();
112 }
113 
115 {
116  return (mReceivedImage!=mEmptyImage);
117 }
118 
120 {
121  return this->isConnected() && mStreaming;
122 }
123 
124 void BasicVideoSource::setResolution(double resolution)
125 {
126  mRedirecter->SetOutputSpacing(resolution, resolution, resolution);
127 }
128 
130 {
131  mRedirecter->Update();
132  return mRedirecter->GetOutput();
133 }
134 
136 {
137  if (mStreaming)
138  return;
139 
140  mStreaming = true;
141 
142  if (mTimeoutTimer)
143  {
144  mTimeoutTimer->start();
145  }
146 
147  if (!this->isConnected())
148  return;
149 
150  emit streaming(true);
151  emit newFrame();
152 }
153 
155 {
156  if (!mStreaming)
157  return;
158 
159  mStreaming = false;
160  if (mTimeoutTimer)
161  {
162  mTimeoutTimer->stop();
163  }
164 
165  emit streaming(false);
166  emit newFrame();
167 }
168 
170 {
171  if (mStatus!="USE_DEFAULT")
172  return mStatus;
173 
174 // { return mStatus; }
175  if (!this->isConnected())
176  return "Not connected";
177  if (!this->isStreaming())
178  return "Not streaming";
179  if (!this->validData())
180  return "Timeout";
181 // return "Running";
182  return "";
183 }
184 
185 
187 {
188  bool wasConnected = this->isConnected();
189 
190  if (input)
191  {
192  mReceivedImage = input;
193  }
194  else
195  {
196  if (mReceivedImage)
197  {
198  // create an empty image with the same uid as the stream.
199  mEmptyImage.reset(new Image(mReceivedImage->getUid(), mEmptyImage->getBaseVtkImageData()));
200  }
201  mReceivedImage = mEmptyImage;
202  }
203  mRedirecter->SetInputData(mReceivedImage->getBaseVtkImageData());
204  mRedirecter->Update();
205 
206  if (mTimeoutTimer)
207  {
208  mTimeout = false;
209  mTimeoutTimer->start();
210  }
211 
212  if (this->isConnected() != wasConnected)
213  emit connected(this->isConnected());
214 
215  emit newFrame();
216 }
217 
218 } // 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 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.