Fraxinus  18.10
An IGT application
cxOrientationAnnotation3DRep.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  * sscOrientationAnnotation3DRep.cpp
14  *
15  * \date Mar 24, 2011
16  * \author christiana
17  */
18 
20 
21 #include <vtkOrientationMarkerWidget.h>
22 #include <vtkAnnotatedCubeActor.h>
23 #include <vtkProperty.h>
24 #include <vtkAxesActor.h>
25 #include <vtkTextProperty.h>
26 #include <vtkCaptionActor2D.h>
27 #include <vtkPropAssembly.h>
28 #include <vtkRenderWindow.h>
29 #include <QFileInfo>
30 #include "vtkSTLReader.h"
31 #include "vtkSTLWriter.h"
32 #include "vtkPolyDataMapper.h"
33 #include "vtkPolyData.h"
34 #include "vtkActor.h"
35 #include "vtkProperty.h"
36 #include <vtkImageShrink3D.h>
37 #include <vtkMarchingCubes.h>
38 #include <vtkWindowedSincPolyDataFilter.h>
39 #include <vtkTriangleFilter.h>
40 #include <vtkDecimatePro.h>
41 #include <vtkPolyDataNormals.h>
42 #include <vtkQuadricDecimation.h>
43 
44 #include "cxVector3D.h"
45 #include "cxView.h"
46 #include "cxTypeConversions.h"
47 #include "cxForwardDeclarations.h"
48 #include <vtkRenderWindowInteractor.h>
49 
50 typedef vtkSmartPointer<vtkAxesActor> vtkAxesActorPtr;
51 typedef vtkSmartPointer<vtkTextProperty> vtkTextPropertyPtr;
52 typedef vtkSmartPointer<vtkPropAssembly> vtkPropAssemblyPtr;
53 typedef vtkSmartPointer<vtkSTLWriter> vtkSTLWriterPtr;
54 typedef vtkSmartPointer<vtkQuadricDecimation> vtkQuadricDecimationPtr;
55 
56 namespace cx
57 {
58 
60 //std::pair<QString, vtkPropPtr> OrientationAnnotation3DRep::mMarkerCache;
62 
64  RepImpl(), mSize(0.2), mColor(1, 0.5, 0.5)
65 {
66  this->rebuild(NULL);
67 }
68 
70 {
71  return wrap_new(new OrientationAnnotation3DRep(), uid);
72 }
73 
75 {
76 
77 }
78 
80 {
81  this->rebuild(view->getRenderWindow()->GetInteractor());
82 }
83 
85 {
86  mMarker->SetInteractor(NULL);
87 }
88 
90 {
91  return mMarker->GetEnabled();
92 }
93 
95 {
96  if (!this->getView())
97  return;
98  mMarker->SetEnabled(on);
99 }
100 
102 {
103  mSize = size;
104  this->rebuild(mMarker->GetInteractor());
105 }
106 
107 void OrientationAnnotation3DRep::rebuild(vtkRenderWindowInteractorPtr interactor)
108 {
109  bool enable = true;
110  if (mMarker)
111  {
112  enable = mMarker->GetEnabled();
113  mMarker->SetInteractor(NULL);
114  }
115 
116  mMarker = vtkOrientationMarkerWidgetPtr::New();
117  mMarker->SetOutlineColor(mColor[0], mColor[1], mColor[2]);
118  mMarker->SetViewport(0.0, 1.0 - mSize, mSize, 1.0);
119  mMarker->SetOrientationMarker(mMarkerCache.second);
120 
121  if (interactor)
122  {
123  mMarker->KeyPressActivationOff();
124  mMarker->SetInteractor(interactor);
125  mMarker->SetEnabled(true);
126  mMarker->InteractiveOff();//This line prints a VTK warning if enabled is false
127  mMarker->SetEnabled(enable);
128  }
129 }
130 
132 {
133 
134  if (!mMarkerCache.second || (mMarkerCache.first != filename))
135  {
136  mMarkerCache.first = filename;
137  ;
138  mMarkerCache.second = this->readMarkerFromFile(filename);
139  }
140 
141  this->rebuild(mMarker->GetInteractor());
142 }
143 
144 vtkPropPtr OrientationAnnotation3DRep::readMarkerFromFile(const QString filename)
145 {
146  if (filename.isEmpty() || !QFileInfo(filename).exists() || QFileInfo(filename).isDir())
147  {
148  return this->createCube();
149  }
150 
151 // std::cout << "OrientationAnnotation3DRep::readMarkerFromFile " << filename << std::endl;
152 
153  vtkSTLReaderPtr STLReader = vtkSTLReaderPtr::New();
154  STLReader->SetFileName(cstring_cast(filename));
155 
156 // vtkPolyDataPtr person = STLReader->GetOutput();
157 
158  vtkPolyDataNormalsPtr normals = vtkPolyDataNormalsPtr::New();
159  normals->SetInputConnection(STLReader->GetOutputPort());
160  normals->Update();
161 // person = normals->GetOutput();
162 
163  vtkPolyDataMapperPtr polyDataMapper = vtkPolyDataMapperPtr::New();
164  polyDataMapper->SetInputConnection(normals->GetOutputPort()); //read a 3D model file of the tool
165  polyDataMapper->Update();
166 
167  vtkActorPtr actor = vtkActorPtr::New();
168  actor->SetMapper(polyDataMapper);
169  actor->GetProperty()->SetColor(0.5, 1, 1);
170  actor->GetProperty()->SetSpecularPower(15);
171  actor->GetProperty()->SetSpecular(0.3);
172 
173  return actor;
174 }
175 
176 vtkAnnotatedCubeActorPtr OrientationAnnotation3DRep::createCube()
177 {
178  vtkAnnotatedCubeActorPtr cube = vtkAnnotatedCubeActorPtr::New();
179 
180  cube->SetXPlusFaceText("L");
181  cube->SetXMinusFaceText("R");
182  cube->SetYPlusFaceText("P");
183  cube->SetYMinusFaceText("A");
184  cube->SetZPlusFaceText("S");
185  cube->SetZMinusFaceText("I");
186  cube->SetZFaceTextRotation(-90);
187  cube->SetFaceTextScale(0.65);
188 
189  vtkPropertyPtr property;
190 
191  Vector3D red(1, 0, 0);
192  Vector3D green(0, 1, 0);
193  Vector3D blue(0, 0, 1);
194 
195  property = cube->GetCubeProperty();
196  property->SetColor(0.5, 1, 1);
197  property = cube->GetTextEdgesProperty();
198  property->SetLineWidth(1);
199  property->SetDiffuse(0);
200  property->SetAmbient(1);
201  property->SetColor(0.18, 0.28, 0.23);
202 
203  property = cube->GetXPlusFaceProperty();
204  property->SetColor(red.begin());
205  property->SetInterpolationToFlat();
206  property = cube->GetXMinusFaceProperty();
207  property->SetColor(red.begin());
208  property->SetInterpolationToFlat();
209 
210  property = cube->GetYPlusFaceProperty();
211  property->SetColor(green.begin());
212  property->SetInterpolationToFlat();
213  property = cube->GetYMinusFaceProperty();
214  property->SetColor(green.begin());
215  property->SetInterpolationToFlat();
216 
217  property = cube->GetZPlusFaceProperty();
218  property->SetColor(blue.begin());
219  property->SetInterpolationToFlat();
220  property = cube->GetZMinusFaceProperty();
221  property->SetColor(blue.begin());
222  property->SetInterpolationToFlat();
223 
224  return cube;
225 }
226 
227 vtkAxesActorPtr OrientationAnnotation3DRep::createAxes()
228 {
229  vtkAxesActorPtr axes = vtkAxesActorPtr::New();
230  axes->SetShaftTypeToCylinder();
231  axes->SetXAxisLabelText("x");
232  axes->SetYAxisLabelText("y");
233  axes->SetZAxisLabelText("z");
234  axes->SetTotalLength(1.5, 1.5, 1.5);
235  vtkTextPropertyPtr tprop = vtkTextPropertyPtr::New();
236  tprop->ItalicOn();
237  tprop->ShadowOn();
238  tprop->SetFontFamilyToTimes();
239  axes->GetXAxisCaptionActor2D()->SetCaptionTextProperty(tprop);
240  vtkTextPropertyPtr tprop2 = vtkTextPropertyPtr::New();
241  tprop2->ShallowCopy(tprop);
242  axes->GetYAxisCaptionActor2D()->SetCaptionTextProperty(tprop2);
243  vtkTextPropertyPtr tprop3 = vtkTextPropertyPtr::New();
244  tprop3->ShallowCopy(tprop);
245  axes->GetZAxisCaptionActor2D()->SetCaptionTextProperty(tprop3);
246  return axes;
247 }
248 
249 }
ViewPtr getView() const
Definition: cxRepImpl.cpp:83
vtkSmartPointer< class vtkActor > vtkActorPtr
vtkSmartPointer< class vtkAxesActor > vtkAxesActorPtr
vtkSmartPointer< class vtkPolyDataMapper > vtkPolyDataMapperPtr
vtkSmartPointer< class vtkProperty > vtkPropertyPtr
vtkSmartPointer< class vtkRenderWindowInteractor > vtkRenderWindowInteractorPtr
static boost::shared_ptr< REP > wrap_new(REP *object, QString uid)
Definition: cxRepImpl.h:62
cstring_cast_Placeholder cstring_cast(const T &val)
boost::shared_ptr< class View > ViewPtr
virtual void removeRepActorsFromViewRenderer(ViewPtr view)
void setSize(double size)
fraction of viewport to use
vtkSmartPointer< class vtkSTLReader > vtkSTLReaderPtr
vtkSmartPointer< class vtkPolyDataNormals > vtkPolyDataNormalsPtr
vtkSmartPointer< vtkPropAssembly > vtkPropAssemblyPtr
Default implementation of Rep.
Definition: cxRepImpl.h:42
vtkSmartPointer< vtkSTLWriter > vtkSTLWriterPtr
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
boost::shared_ptr< class OrientationAnnotation3DRep > OrientationAnnotation3DRepPtr
void setMarkerFilename(const QString filename)
empty and default means a cube
static OrientationAnnotation3DRepPtr New(const QString &uid="")
vtkSmartPointer< vtkAxesActor > vtkAxesActorPtr
virtual void addRepActorsToViewRenderer(ViewPtr view)
vtkSmartPointer< vtkTextProperty > vtkTextPropertyPtr
vtkSmartPointer< vtkQuadricDecimation > vtkQuadricDecimationPtr
vtkSmartPointer< class vtkProp > vtkPropPtr
vtkSmartPointer< class vtkAnnotatedCubeActor > vtkAnnotatedCubeActorPtr
Namespace for all CustusX production code.