CustusX  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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  mMarker->SetEnabled(on);
118 }
119 
121 {
122  mSize = size;
123  this->rebuild(mMarker->GetInteractor());
124 }
125 
126 void OrientationAnnotation3DRep::rebuild(vtkRenderWindowInteractorPtr interactor)
127 {
128  bool enable = true;
129  if (mMarker)
130  {
131  enable = mMarker->GetEnabled();
132  mMarker->SetInteractor(NULL);
133  }
134 
135  mMarker = vtkOrientationMarkerWidgetPtr::New();
136  mMarker->SetOutlineColor(mColor[0], mColor[1], mColor[2]);
137  mMarker->SetViewport(0.0, 1.0 - mSize, mSize, 1.0);
138  mMarker->SetOrientationMarker(mMarkerCache.second);
139 
140  if (interactor)
141  {
142  mMarker->KeyPressActivationOff();
143  mMarker->SetInteractor(interactor);
144  mMarker->SetEnabled(true);
145  mMarker->InteractiveOff();//This line prints a VTK warning if enabled is false
146  mMarker->SetEnabled(enable);
147  }
148 }
149 
151 {
152 
153  if (!mMarkerCache.second || (mMarkerCache.first != filename))
154  {
155  mMarkerCache.first = filename;
156  ;
157  mMarkerCache.second = this->readMarkerFromFile(filename);
158  }
159 
160  this->rebuild(mMarker->GetInteractor());
161 }
162 
163 vtkPropPtr OrientationAnnotation3DRep::readMarkerFromFile(const QString filename)
164 {
165  if (filename.isEmpty() || !QFileInfo(filename).exists() || QFileInfo(filename).isDir())
166  {
167  return this->createCube();
168  }
169 
170 // std::cout << "OrientationAnnotation3DRep::readMarkerFromFile " << filename << std::endl;
171 
172  vtkSTLReaderPtr STLReader = vtkSTLReaderPtr::New();
173  STLReader->SetFileName(cstring_cast(filename));
174 
175 // vtkPolyDataPtr person = STLReader->GetOutput();
176 
177  vtkPolyDataNormalsPtr normals = vtkPolyDataNormalsPtr::New();
178  normals->SetInputConnection(STLReader->GetOutputPort());
179  normals->Update();
180 // person = normals->GetOutput();
181 
182  vtkPolyDataMapperPtr polyDataMapper = vtkPolyDataMapperPtr::New();
183  polyDataMapper->SetInputConnection(normals->GetOutputPort()); //read a 3D model file of the tool
184  polyDataMapper->Update();
185 
186  vtkActorPtr actor = vtkActorPtr::New();
187  actor->SetMapper(polyDataMapper);
188  actor->GetProperty()->SetColor(0.5, 1, 1);
189  actor->GetProperty()->SetSpecularPower(15);
190  actor->GetProperty()->SetSpecular(0.3);
191 
192  return actor;
193 }
194 
195 vtkAnnotatedCubeActorPtr OrientationAnnotation3DRep::createCube()
196 {
197  vtkAnnotatedCubeActorPtr cube = vtkAnnotatedCubeActorPtr::New();
198 
199  cube->SetXPlusFaceText("L");
200  cube->SetXMinusFaceText("R");
201  cube->SetYPlusFaceText("P");
202  cube->SetYMinusFaceText("A");
203  cube->SetZPlusFaceText("S");
204  cube->SetZMinusFaceText("I");
205  cube->SetZFaceTextRotation(-90);
206  cube->SetFaceTextScale(0.65);
207 
208  vtkPropertyPtr property;
209 
210  Vector3D red(1, 0, 0);
211  Vector3D green(0, 1, 0);
212  Vector3D blue(0, 0, 1);
213 
214  property = cube->GetCubeProperty();
215  property->SetColor(0.5, 1, 1);
216  property = cube->GetTextEdgesProperty();
217  property->SetLineWidth(1);
218  property->SetDiffuse(0);
219  property->SetAmbient(1);
220  property->SetColor(0.18, 0.28, 0.23);
221 
222  property = cube->GetXPlusFaceProperty();
223  property->SetColor(red.begin());
224  property->SetInterpolationToFlat();
225  property = cube->GetXMinusFaceProperty();
226  property->SetColor(red.begin());
227  property->SetInterpolationToFlat();
228 
229  property = cube->GetYPlusFaceProperty();
230  property->SetColor(green.begin());
231  property->SetInterpolationToFlat();
232  property = cube->GetYMinusFaceProperty();
233  property->SetColor(green.begin());
234  property->SetInterpolationToFlat();
235 
236  property = cube->GetZPlusFaceProperty();
237  property->SetColor(blue.begin());
238  property->SetInterpolationToFlat();
239  property = cube->GetZMinusFaceProperty();
240  property->SetColor(blue.begin());
241  property->SetInterpolationToFlat();
242 
243  return cube;
244 }
245 
246 vtkAxesActorPtr OrientationAnnotation3DRep::createAxes()
247 {
248  vtkAxesActorPtr axes = vtkAxesActorPtr::New();
249  axes->SetShaftTypeToCylinder();
250  axes->SetXAxisLabelText("x");
251  axes->SetYAxisLabelText("y");
252  axes->SetZAxisLabelText("z");
253  axes->SetTotalLength(1.5, 1.5, 1.5);
254  vtkTextPropertyPtr tprop = vtkTextPropertyPtr::New();
255  tprop->ItalicOn();
256  tprop->ShadowOn();
257  tprop->SetFontFamilyToTimes();
258  axes->GetXAxisCaptionActor2D()->SetCaptionTextProperty(tprop);
259  vtkTextPropertyPtr tprop2 = vtkTextPropertyPtr::New();
260  tprop2->ShallowCopy(tprop);
261  axes->GetYAxisCaptionActor2D()->SetCaptionTextProperty(tprop2);
262  vtkTextPropertyPtr tprop3 = vtkTextPropertyPtr::New();
263  tprop3->ShallowCopy(tprop);
264  axes->GetZAxisCaptionActor2D()->SetCaptionTextProperty(tprop3);
265  return axes;
266 }
267 
268 }
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