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