CustusX  2023.01.05-dev+develop.0da12
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) 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 /*
14  * sscRT2DRep.cpp
15  *
16  * Created on: Oct 31, 2010
17  * Author: christiana
18  */
19 #include "cxVideoRep.h"
20 
21 #include "boost/bind.hpp"
22 
23 #include <vtkRenderer.h>
24 #include <vtkActor2D.h>
25 #include <vtkActor.h>
26 #include <vtkCamera.h>
27 
28 #include "cxView.h"
29 #include "cxTool.h"
30 #include "cxVideoSourceGraphics.h"
31 #include "cxVideoSource.h"
32 #include "cxViewportListener.h"
33 
34 namespace cx
35 {
36 
37 VideoFixedPlaneRep::VideoFixedPlaneRep(const QString& uid, const QString& name) :
38  RepImpl(uid, name)
39 {
40  SpaceProviderPtr nullProvider;
41 
42  mRTGraphics.reset(new VideoSourceGraphics(nullProvider));
43  connect(mRTGraphics.get(), &VideoSourceGraphics::newData, this, &VideoFixedPlaneRep::newDataSlot);
44  mRTGraphics->setShowInToolSpace(false);
45  mRTGraphics->setClipToSector(false);
46 
47  mInfoText.reset(new TextDisplay("", QColor::fromRgbF(1.0, 0.8, 0.0), 16));
48  mInfoText->getActor()->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
49  mInfoText->setCentered();
50  mInfoText->setPosition(0.5, 0.05);
51 
52  mStatusText.reset(new TextDisplay("", QColor::fromRgbF(1.0, 0.8, 0.0), 20));
53  mStatusText->getActor()->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
54  mStatusText->setCentered();
55  mStatusText->setPosition(0.5, 0.5);
56  mStatusText->updateText("Not Connected");
57 
58  mOrientationVText.reset(new TextDisplay("V", QColor::fromRgbF(1.0, 0.9, 0.0), 30));
59  mOrientationVText->getActor()->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
60  mOrientationVText->setCentered();
61  mOrientationVText->setPosition(0.05, 0.95);
62 
63  mProbeOrigin.reset(new GraphicalPolyData3D());
64  mProbeOrigin->setColor(Vector3D(1, 165.0/255.0, 0)); // orange
65  mProbeSector.reset(new GraphicalPolyData3D());
66  mProbeSector->setColor(Vector3D(1, 165.0/255.0, 0)); // orange
67  mProbeClipRect.reset(new GraphicalPolyData3D());
68  mProbeClipRect->setColor(Vector3D(1, 0.9, 0)); // yellow
69 
70  mViewportListener.reset(new ViewportListener());
71  mViewportListener->setCallback(boost::bind(&VideoFixedPlaneRep::setCamera, this));
72 }
73 
75 {
76 }
77 
79 {
80  mShowSector = on;
81  this->updateSector();
82 }
83 
85 {
86  return mShowSector;
87 }
88 
89 void VideoFixedPlaneRep::updateSector()
90 {
91  bool show = mTool && this->getShowSector() && mTool->getProbe()->getProbeDefinition().getType()!=ProbeDefinition::tNONE;
92 
93  mProbeOrigin->getActor()->SetVisibility(show);
94  mProbeSector->getActor()->SetVisibility(show);
95  mProbeClipRect->getActor()->SetVisibility(show);
96  if (!show)
97  return;
98 
99  mProbeDefinition.setData(mTool->getProbe()->getProbeDefinition());
100 
101  mProbeOrigin->setData(mProbeDefinition.getOriginPolyData());
102  mProbeSector->setData(mProbeDefinition.getSectorSectorOnlyLinesOnly());
103  mProbeClipRect->setData(mProbeDefinition.getClipRectLinesOnly());
104 }
105 
107 {
108  mRTGraphics->setTool(tool);
109  mTool = tool;
110 }
111 
113 {
114  mData = data;
115  mRTGraphics->setRealtimeStream(data);
116 }
117 
118 void VideoFixedPlaneRep::newDataSlot()
119 {
120  if (!mData)
121  return;
122 
123  mInfoText->updateText(mData->getInfoString());
124  mStatusText->updateText(mData->getStatusString());
125  mStatusText->getActor()->SetVisibility(!mData->validData());
126  mOrientationVText->getActor()->SetVisibility(mData->validData());
127  this->setCamera();
128  this->updateSector();
129 }
130 
135 void VideoFixedPlaneRep::setCamera()
136 {
137  if (!mRenderer)
138  return;
139  mViewportListener->stopListen();
140  vtkCamera* camera = mRenderer->GetActiveCamera();
141  camera->ParallelProjectionOn();
142  mRenderer->ResetCamera();
143 
144  DoubleBoundingBox3D bounds(mRTGraphics->getActor()->GetBounds());
145  if (similar(bounds.range()[0], 0.0) || similar(bounds.range()[1], 0.0))
146  return;
147 
148  double* vpRange = mRenderer->GetAspect();
149 
150  double vw = vpRange[0];
151  double vh = vpRange[1];
152 
153  double w = bounds.range()[0];
154  double h = bounds.range()[1];
155 
156  double scale = 1;
157  double w_vp = vh * (w/h); // width of image in viewport space
158  if (w_vp > vw) // if image too wide: reduce scale
159  scale = w_vp/vw;
160 
161  //Set camera position and focus so that it looks at the video stream center
162  double position[3];
163  camera->GetPosition(position);
164  position[0] = bounds.center()[0];
165  position[1] = bounds.center()[1];
166  camera->SetPosition(position);
167 
168  camera->GetFocalPoint(position);
169  position[0] = bounds.center()[0];
170  position[1] = bounds.center()[1];
171  camera->SetFocalPoint(position);
172 
173  camera->SetParallelScale(h/2*scale*1.01); // exactly fill the viewport height
174  mViewportListener->startListen(mRenderer);
175 }
176 
177 
179 {
180  mRenderer = view->getRenderer();
181  mViewportListener->startListen(mRenderer);
182 
183  view->getRenderer()->AddActor(mRTGraphics->getActor());
184  view->getRenderer()->AddActor(mInfoText->getActor());
185  view->getRenderer()->AddActor(mStatusText->getActor());
186  view->getRenderer()->AddActor(mOrientationVText->getActor());
187 
188  mProbeClipRect->setRenderer(view->getRenderer());
189  mProbeOrigin->setRenderer(view->getRenderer());
190  mProbeSector->setRenderer(view->getRenderer());
191 }
192 
194 {
195  mRenderer = vtkRendererPtr();
196  view->getRenderer()->RemoveActor(mRTGraphics->getActor());
197  view->getRenderer()->RemoveActor(mInfoText->getActor());
198  view->getRenderer()->RemoveActor(mStatusText->getActor());
199  view->getRenderer()->RemoveActor(mOrientationVText->getActor());
200  mProbeOrigin->setRenderer(NULL);
201  mProbeSector->setRenderer(NULL);
202  mProbeClipRect->setRenderer(NULL);
203 
204  mViewportListener->stopListen();
205 }
206 
207 } // namespace cx
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
bool getShowSector() const
Definition: cxVideoRep.cpp:84
VideoFixedPlaneRep(const QString &uid, const QString &name="")
Definition: cxVideoRep.cpp:37
void setTool(ToolPtr tool)
Definition: cxVideoRep.cpp:106
void setRealtimeStream(VideoSourcePtr data)
Definition: cxVideoRep.cpp:112
virtual void addRepActorsToViewRenderer(ViewPtr view)
Definition: cxVideoRep.cpp:178
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:193
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:74
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:42
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:42
void setShowSector(bool on)
Definition: cxVideoRep.cpp:78
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