Fraxinus  17.12-rc1
An IGT application
cxVideoRep.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 /*
35  * sscRT2DRep.cpp
36  *
37  * Created on: Oct 31, 2010
38  * Author: christiana
39  */
40 #include "cxVideoRep.h"
41 
42 #include "boost/bind.hpp"
43 
44 #include <vtkRenderer.h>
45 #include <vtkActor2D.h>
46 #include <vtkActor.h>
47 #include <vtkCamera.h>
48 
49 #include "cxView.h"
50 #include "cxTool.h"
51 #include "cxVideoSourceGraphics.h"
52 #include "cxVideoSource.h"
53 #include "cxViewportListener.h"
54 
55 namespace cx
56 {
57 
58 VideoFixedPlaneRep::VideoFixedPlaneRep(const QString& uid, const QString& name) :
59  RepImpl(uid, name)
60 {
61  SpaceProviderPtr nullProvider;
62 
63  mRTGraphics.reset(new VideoSourceGraphics(nullProvider));
64  connect(mRTGraphics.get(), &VideoSourceGraphics::newData, this, &VideoFixedPlaneRep::newDataSlot);
65  mRTGraphics->setShowInToolSpace(false);
66  mRTGraphics->setClipToSector(false);
67 
68  mInfoText.reset(new TextDisplay("", QColor::fromRgbF(1.0, 0.8, 0.0), 16));
69  mInfoText->getActor()->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
70  mInfoText->setCentered();
71  mInfoText->setPosition(0.5, 0.05);
72 
73  mStatusText.reset(new TextDisplay("", QColor::fromRgbF(1.0, 0.8, 0.0), 20));
74  mStatusText->getActor()->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
75  mStatusText->setCentered();
76  mStatusText->setPosition(0.5, 0.5);
77  mStatusText->updateText("Not Connected");
78 
79  mOrientationVText.reset(new TextDisplay("V", QColor::fromRgbF(1.0, 0.9, 0.0), 30));
80  mOrientationVText->getActor()->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
81  mOrientationVText->setCentered();
82  mOrientationVText->setPosition(0.05, 0.95);
83 
84  mProbeOrigin.reset(new GraphicalPolyData3D());
85  mProbeOrigin->setColor(Vector3D(1, 165.0/255.0, 0)); // orange
86  mProbeSector.reset(new GraphicalPolyData3D());
87  mProbeSector->setColor(Vector3D(1, 165.0/255.0, 0)); // orange
88  mProbeClipRect.reset(new GraphicalPolyData3D());
89  mProbeClipRect->setColor(Vector3D(1, 0.9, 0)); // yellow
90 
91  mViewportListener.reset(new ViewportListener());
92  mViewportListener->setCallback(boost::bind(&VideoFixedPlaneRep::setCamera, this));
93 }
94 
96 {
97 }
98 
100 {
101  mShowSector = on;
102  this->updateSector();
103 }
104 
106 {
107  return mShowSector;
108 }
109 
110 void VideoFixedPlaneRep::updateSector()
111 {
112  bool show = mTool && this->getShowSector() && mTool->getProbe()->getProbeDefinition().getType()!=ProbeDefinition::tNONE;
113 
114  mProbeOrigin->getActor()->SetVisibility(show);
115  mProbeSector->getActor()->SetVisibility(show);
116  mProbeClipRect->getActor()->SetVisibility(show);
117  if (!show)
118  return;
119 
120  mProbeDefinition.setData(mTool->getProbe()->getProbeDefinition());
121 
122  mProbeOrigin->setData(mProbeDefinition.getOriginPolyData());
123  mProbeSector->setData(mProbeDefinition.getSectorSectorOnlyLinesOnly());
124  mProbeClipRect->setData(mProbeDefinition.getClipRectLinesOnly());
125 }
126 
128 {
129  mRTGraphics->setTool(tool);
130  mTool = tool;
131 }
132 
134 {
135  mData = data;
136  mRTGraphics->setRealtimeStream(data);
137 }
138 
139 void VideoFixedPlaneRep::newDataSlot()
140 {
141  if (!mData)
142  return;
143 
144  mInfoText->updateText(mData->getInfoString());
145  mStatusText->updateText(mData->getStatusString());
146  mStatusText->getActor()->SetVisibility(!mData->validData());
147  mOrientationVText->getActor()->SetVisibility(mData->validData());
148  this->setCamera();
149  this->updateSector();
150 }
151 
156 void VideoFixedPlaneRep::setCamera()
157 {
158  if (!mRenderer)
159  return;
160  mViewportListener->stopListen();
161  vtkCamera* camera = mRenderer->GetActiveCamera();
162  camera->ParallelProjectionOn();
163  mRenderer->ResetCamera();
164 
165  DoubleBoundingBox3D bounds(mRTGraphics->getActor()->GetBounds());
166  if (similar(bounds.range()[0], 0.0) || similar(bounds.range()[1], 0.0))
167  return;
168 
169  double* vpRange = mRenderer->GetAspect();
170 
171  double vw = vpRange[0];
172  double vh = vpRange[1];
173 
174  double w = bounds.range()[0];
175  double h = bounds.range()[1];
176 
177  double scale = 1;
178  double w_vp = vh * (w/h); // width of image in viewport space
179  if (w_vp > vw) // if image too wide: reduce scale
180  scale = w_vp/vw;
181 
182  //Set camera position and focus so that it looks at the video stream center
183  double position[3];
184  camera->GetPosition(position);
185  position[0] = bounds.center()[0];
186  position[1] = bounds.center()[1];
187  camera->SetPosition(position);
188 
189  camera->GetFocalPoint(position);
190  position[0] = bounds.center()[0];
191  position[1] = bounds.center()[1];
192  camera->SetFocalPoint(position);
193 
194  camera->SetParallelScale(h/2*scale*1.01); // exactly fill the viewport height
195  mViewportListener->startListen(mRenderer);
196 }
197 
198 
200 {
201  mRenderer = view->getRenderer();
202  mViewportListener->startListen(mRenderer);
203 
204  view->getRenderer()->AddActor(mRTGraphics->getActor());
205  view->getRenderer()->AddActor(mInfoText->getActor());
206  view->getRenderer()->AddActor(mStatusText->getActor());
207  view->getRenderer()->AddActor(mOrientationVText->getActor());
208 
209  mProbeClipRect->setRenderer(view->getRenderer());
210  mProbeOrigin->setRenderer(view->getRenderer());
211  mProbeSector->setRenderer(view->getRenderer());
212 }
213 
215 {
216  mRenderer = vtkRendererPtr();
217  view->getRenderer()->RemoveActor(mRTGraphics->getActor());
218  view->getRenderer()->RemoveActor(mInfoText->getActor());
219  view->getRenderer()->RemoveActor(mStatusText->getActor());
220  view->getRenderer()->RemoveActor(mOrientationVText->getActor());
221  mProbeOrigin->setRenderer(NULL);
222  mProbeSector->setRenderer(NULL);
223  mProbeClipRect->setRenderer(NULL);
224 
225  mViewportListener->stopListen();
226 }
227 
228 } // namespace cx
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
bool getShowSector() const
Definition: cxVideoRep.cpp:105
VideoFixedPlaneRep(const QString &uid, const QString &name="")
Definition: cxVideoRep.cpp:58
void setTool(ToolPtr tool)
Definition: cxVideoRep.cpp:127
void setRealtimeStream(VideoSourcePtr data)
Definition: cxVideoRep.cpp:133
virtual void addRepActorsToViewRenderer(ViewPtr view)
Definition: cxVideoRep.cpp:199
boost::shared_ptr< class View > ViewPtr
Listens to changes in viewport and camera matrix.
Helper class for displaying a VideoSource.
virtual void removeRepActorsFromViewRenderer(ViewPtr view)
Definition: cxVideoRep.cpp:214
vtkSmartPointer< class vtkRenderer > vtkRendererPtr
Helper for rendering a a polydata in 3D.
vtkPolyDataPtr getSectorSectorOnlyLinesOnly()
get a polydata representation of the us sector
virtual ~VideoFixedPlaneRep()
Definition: cxVideoRep.cpp:95
Helper for drawing text in 2D.
boost::shared_ptr< class VideoSource > VideoSourcePtr
vtkPolyDataPtr getClipRectLinesOnly()
get a polydata representation of the us clip rect
Default implementation of Rep.
Definition: cxRepImpl.h:63
Representation of a floating-point bounding box in 3D. The data are stored as {xmin,xmax,ymin,ymax,zmin,zmax}, in order to simplify communication with vtk.
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
void setShowSector(bool on)
Definition: cxVideoRep.cpp:99
bool similar(const CameraInfo &lhs, const CameraInfo &rhs, double tol)
vtkPolyDataPtr getOriginPolyData()
get a polydata representation of the origin
void setData(ProbeDefinition data)
Namespace for all CustusX production code.
boost::shared_ptr< class Tool > ToolPtr