CustusX  15.8
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxEraserWidget.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 #include <cxEraserWidget.h>
34 
35 #include <QTimer>
36 #include <QCheckBox>
37 
38 #include "vtkSphereWidget.h"
39 #include "vtkSplineWidget.h"
40 #include "vtkSplineWidget2.h"
41 #include "vtkSplineRepresentation.h"
42 #include "vtkRenderWindow.h"
43 #include <vtkSphere.h>
44 #include <vtkClipPolyData.h>
45 #include <vtkImageData.h>
46 
47 #include "cxMesh.h"
48 #include "cxStringPropertyBase.h"
50 #include "cxDefinitionStrings.h"
51 #include "cxUtilHelpers.h"
52 
54 #include "cxImageAlgorithms.h"
55 #include "cxDoubleWidgets.h"
56 #include "cxImage.h"
57 #include "cxVolumeHelpers.h"
58 #include "cxPatientModelService.h"
59 #include "cxViewService.h"
60 #include "cxViewGroupData.h"
61 #include "cxReporter.h"
62 
63 namespace cx
64 {
65 
66 EraserWidget::EraserWidget(PatientModelServicePtr patientModelService, VisualizationServicePtr visualizationService, QWidget* parent) :
67  BaseWidget(parent, "EraserWidget", "Eraser"),
68  mPreviousCenter(0,0,0),
69  mPreviousRadius(0),
70  mActiveImageProxy(ActiveImageProxyPtr()),
71  mPatientModelService(patientModelService),
72  mVisualizationService(visualizationService)
73 
74 {
75 
76  QVBoxLayout* layout = new QVBoxLayout(this);
77  this->setToolTip("Erase parts of volumes/models");
78 
79  mContinousEraseTimer = new QTimer(this);
80  connect(mContinousEraseTimer, SIGNAL(timeout()), this, SLOT(continousRemoveSlot())); // this signal will be executed in the thread of THIS, i.e. the main thread.
81 
82  QHBoxLayout* buttonLayout = new QHBoxLayout;
83  layout->addLayout(buttonLayout);
84  QHBoxLayout* buttonLayout2 = new QHBoxLayout;
85  layout->addLayout(buttonLayout2);
86 
87  mShowEraserCheckBox = new QCheckBox("Show");
88  mShowEraserCheckBox->setToolTip("Show eraser sphere in the views.");
89  connect(mShowEraserCheckBox, SIGNAL(toggled(bool)), this, SLOT(toggleShowEraser(bool)));
90  buttonLayout->addWidget(mShowEraserCheckBox);
91 
92  mContinousEraseCheckBox = new QCheckBox("Continous");
93  mContinousEraseCheckBox->setToolTip("Erase continously using the sphere. (might be slow)");
94  connect(mContinousEraseCheckBox, SIGNAL(toggled(bool)), this, SLOT(toggleContinous(bool)));
95  buttonLayout2->addWidget(mContinousEraseCheckBox);
96 
97  mDuplicateAction = this->createAction(this, QIcon(), "Duplicate", "Duplicate active volume - do this before erasing!",
98  SLOT(duplicateSlot()), buttonLayout);
99 
100  mSaveAction = this->createAction(this, QIcon(), "Save", "Save modified image to disk",
101  SLOT(saveSlot()), buttonLayout);
102 
103  mRemoveAction = this->createAction(this, QIcon(), "Erase", "Erase everything inside sphere",
104  SLOT(removeSlot()), buttonLayout2);
105 
106 
107  double sphereRadius = 10;
108  mSphereSizeAdapter = DoubleProperty::initialize("SphereSize", "Sphere Size", "Radius of Eraser Sphere", sphereRadius, DoubleRange(0,200,1), 0, QDomNode());
109  connect(mSphereSizeAdapter.get(), &DoubleProperty::changed, this, &EraserWidget::sphereSizeChangedSlot);
110  mSphereSize = new SpinBoxAndSliderGroupWidget(this, mSphereSizeAdapter);
111  layout->addWidget(mSphereSize);
112 
113  ImagePtr image = mPatientModelService->getActiveImage();
114  int eraseValue = 0;
115  if(image)
116  eraseValue = image->getMin();
117  mEraseValueAdapter = DoubleProperty::initialize("EraseValue", "Erase value", "Erase/draw with value", eraseValue, DoubleRange(1,200,1), 0, QDomNode());
118 
119  mActiveImageProxy = ActiveImageProxy::New(mPatientModelService);
120  connect(mActiveImageProxy.get(), &ActiveImageProxy::activeImageChanged, this, &EraserWidget::activeImageChangedSlot);
121 
122  mEraseValueWidget = new SpinBoxAndSliderGroupWidget(this, mEraseValueAdapter);
123  layout->addWidget(mEraseValueWidget);
124 
125  layout->addStretch();
126 
127  this->enableButtons();
128 }
129 
130 void EraserWidget::activeImageChangedSlot()
131 {
132  ImagePtr image = mPatientModelService->getActiveImage();
133  if(!image)
134  return;
135 
136  mEraseValueAdapter->setValueRange(DoubleRange(image->getVTKMinValue(), image->getVTKMaxValue(), 1));
137  mEraseValueAdapter->setValue(image->getMin());
138 }
139 
140 void EraserWidget::enableButtons()
141 {
142  bool e = mShowEraserCheckBox->isChecked();
143 
144  mContinousEraseCheckBox->setEnabled(e);
145 // mDuplicateAction->setEnabled(e);
146 // mSaveAction->setEnabled(e);
147  mRemoveAction->setEnabled(e);
148  mSphereSize->setEnabled(e);
149 }
150 
152 {
153 }
154 
155 void EraserWidget::toggleContinous(bool on)
156 {
157  if (on)
158  {
159  mContinousEraseTimer->start(300);
160  }
161  else
162  {
163  mContinousEraseTimer->stop();
164  }
165 }
166 
167 void EraserWidget::continousRemoveSlot()
168 {
169  Transform3D rMd = mVisualizationService->getGroup(0)->getOptions().mPickerGlyph->get_rMd();
170  //Transform3D rMd = mVisualizationService->getViewGroupDatas().front()->getData()->getOptions().mPickerGlyph->get_rMd();
171  Vector3D c(mSphere->GetCenter());
172  c = rMd.coord(c);
173  double r = mSphere->GetRadius();
174 
175  // optimization: dont remove if idle
176  if (similar(mPreviousCenter, c) && similar(mPreviousRadius, r))
177  return;
178 
179  this->removeSlot();
180 }
181 
182 void EraserWidget::duplicateSlot()
183 {
184  ImagePtr original = mPatientModelService->getActiveImage();
185 
186  ImagePtr duplicate = duplicateImage(mPatientModelService, original);
187  mPatientModelService->insertData(duplicate);
188  mPatientModelService->setActiveImage(duplicate);
189 
190  // replace viz of original with duplicate
191 // std::vector<ViewGroupPtr> viewGroups = mVisualizationService->getViewGroupDatas();
192  for (unsigned i = 0; i < mVisualizationService->groupCount(); ++i)
193  {
194  if (mVisualizationService->getGroup(i)->removeData(original->getUid()))
195  mVisualizationService->getGroup(i)->addData(duplicate->getUid());
196  }
197 }
198 
199 void EraserWidget::sphereSizeChangedSlot()
200 {
201  if (mSphere)
202  {
203  mSphere->SetRadius(mSphereSizeAdapter->getValue());
204  mSphere->Update();
205  }
206 }
207 
212 void EraserWidget::saveSlot()
213 {
214  mPatientModelService->insertData(mPatientModelService->getActiveImage());
215 }
216 
217 
218 template <class TYPE>
219 void EraserWidget::eraseVolume(TYPE* volumePointer)
220 {
221  ImagePtr image = mPatientModelService->getActiveImage();
222  vtkImageDataPtr img = image->getBaseVtkImageData();
223 
224 
225  Eigen::Array3i dim(img->GetDimensions());
226  Vector3D spacing(img->GetSpacing());
227 
228  Transform3D rMd = mVisualizationService->getGroup(0)->getOptions().mPickerGlyph->get_rMd();
229  Vector3D c(mSphere->GetCenter());
230  c = rMd.coord(c);
231  double r = mSphere->GetRadius();
232  mPreviousCenter = c;
233  mPreviousRadius = r;
234 
235  DoubleBoundingBox3D bb_r(c[0]-r, c[0]+r, c[1]-r, c[1]+r, c[2]-r, c[2]+r);
236 
237  Transform3D dMr = image->get_rMd().inv();
238  Transform3D rawMd = createTransformScale(spacing).inv();
239  Transform3D rawMr = rawMd * dMr;
240  Vector3D c_d = dMr.coord(c);
241  double r_d = dMr.vector(r * Vector3D::UnitX()).length();
242  c = rawMr.coord(c);
243  r = rawMr.vector(r * Vector3D::UnitX()).length();
244  DoubleBoundingBox3D bb0_raw = transform(rawMr, bb_r);
245  IntBoundingBox3D bb1_raw(0, dim[0], 0, dim[1], 0, dim[2]);
246 
247 // std::cout << " sphere: " << bb0_raw << std::endl;
248 // std::cout << " raw: " << bb1_raw << std::endl;
249 
250  for (int i=0; i<3; ++i)
251  {
252  bb1_raw[2*i] = std::max<double>(bb1_raw[2*i], bb0_raw[2*i]);
253  bb1_raw[2*i+1] = std::min<double>(bb1_raw[2*i+1], bb0_raw[2*i+1]);
254  }
255 
256  int replaceVal = mEraseValueAdapter->getValue();
257 
258  for (int x = bb1_raw[0]; x < bb1_raw[1]; ++x)
259  for (int y = bb1_raw[2]; y < bb1_raw[3]; ++y)
260  for (int z = bb1_raw[4]; z < bb1_raw[5]; ++z)
261  {
262  int index = x + y * dim[0] + z * dim[0] * dim[1];
263  if ((Vector3D(x*spacing[0], y*spacing[1], z*spacing[2]) - c_d).length() < r_d)
264  volumePointer[index] = replaceVal;
265  }
266 }
267 
268 //#define VTK_VOID 0
269 //#define VTK_BIT 1
270 //#define VTK_CHAR 2
271 //#define VTK_SIGNED_CHAR 15
272 //#define VTK_UNSIGNED_CHAR 3
273 //#define VTK_SHORT 4
274 //#define VTK_UNSIGNED_SHORT 5
275 //#define VTK_INT 6
276 //#define VTK_UNSIGNED_INT 7
277 //#define VTK_LONG 8
278 //#define VTK_UNSIGNED_LONG 9
279 //#define VTK_FLOAT 10
280 //#define VTK_DOUBLE 11
281 //#define VTK_ID_TYPE 12
282 
283 void EraserWidget::removeSlot()
284 {
285  if (!mSphere)
286  return;
287 
288  ImagePtr image = mPatientModelService->getActiveImage();
289  vtkImageDataPtr img = image->getBaseVtkImageData();
290 
291  int vtkScalarType = img->GetScalarType();
292 
293  if (vtkScalarType==VTK_CHAR)
294  this->eraseVolume(static_cast<char*> (img->GetScalarPointer()));
295  else if (vtkScalarType==VTK_UNSIGNED_CHAR)
296  this->eraseVolume(static_cast<unsigned char*> (img->GetScalarPointer()));
297  else if (vtkScalarType==VTK_SIGNED_CHAR)
298  this->eraseVolume(static_cast<signed char*> (img->GetScalarPointer()));
299  else if (vtkScalarType==VTK_UNSIGNED_SHORT)
300  this->eraseVolume(static_cast<unsigned short*> (img->GetScalarPointer()));
301  else if (vtkScalarType==VTK_SHORT)
302  this->eraseVolume(static_cast<short*> (img->GetScalarPointer()));
303  else if (vtkScalarType==VTK_UNSIGNED_INT)
304  this->eraseVolume(static_cast<unsigned int*> (img->GetScalarPointer()));
305  else if (vtkScalarType==VTK_INT)
306  this->eraseVolume(static_cast<int*> (img->GetScalarPointer()));
307  else
308  reportError(QString("Unknown VTK ScalarType: %1").arg(vtkScalarType));
309 
310  ImageLUT2DPtr tf2D = image->getLookupTable2D();
311  ImageTF3DPtr tf3D = image->getTransferFunctions3D();
312 
313 // img->Modified();
314  setDeepModified(img);
315  image->setVtkImageData(img);
316 
317  // keep existing transfer functions
318  image->setLookupTable2D(tf2D);
319  image->setTransferFunctions3D(tf3D);
320 }
321 
322 void EraserWidget::toggleShowEraser(bool on)
323 {
324  if (on)
325  {
326 // std::vector<ViewGroupPtr> viewGroups = mVisualizationService->getViewGroups();
327  mSphere = vtkSphereSourcePtr::New();
328 
329  mSphere->SetRadius(40);
330  mSphere->SetThetaResolution(16);
331  mSphere->SetPhiResolution(12);
332  mSphere->LatLongTessellationOn(); // more natural wireframe view
333 
334  double a = mSphereSizeAdapter->getValue();
335  mSphere->SetRadius(a);
336  mSphere->Update();
337  MeshPtr glyph = mVisualizationService->getGroup(0)->getOptions().mPickerGlyph;
338  glyph->setVtkPolyData(mSphere->GetOutput());
339  glyph->setColor(QColor(255, 204, 0)); // same as tool
340  glyph->setIsWireframe(true);
341 
342  // set same glyph in all groups
343  for (unsigned i=0; i<mVisualizationService->groupCount(); ++i)
344  {
345  ViewGroupData::Options options = mVisualizationService->getGroup(i)->getOptions();
346  options.mPickerGlyph = glyph;
347  mVisualizationService->getGroup(i)->setOptions(options);
348  }
349  }
350  else
351  {
352  mVisualizationService->getGroup(0)->getOptions().mPickerGlyph->setVtkPolyData(NULL);
353  mContinousEraseCheckBox->setChecked(false);
354  }
355 
356  this->enableButtons();
357 }
358 
359 }
DoubleBoundingBox3D transform(const Transform3D &m, const DoubleBoundingBox3D &bb)
void reportError(QString msg)
Definition: cxLogger.cpp:92
Transform3D createTransformScale(const Vector3D &scale_)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
Utility class for describing a bounded numeric range.
Definition: cxDoubleRange.h:53
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
boost::shared_ptr< class ActiveImageProxy > ActiveImageProxyPtr
QAction * createAction(QObject *parent, QIcon iconName, QString text, QString tip, T slot, QLayout *layout=NULL, QToolButton *button=new QToolButton())
Definition: cxBaseWidget.h:129
bool similar(const DoubleBoundingBox3D &a, const DoubleBoundingBox3D &b, double tol)
Composite widget for scalar data manipulation.
ImagePtr duplicateImage(PatientModelServicePtr dataManager, ImagePtr image)
boost::shared_ptr< class ImageLUT2D > ImageLUT2DPtr
boost::shared_ptr< class VisualizationService > VisualizationServicePtr
Definition: cxRegServices.h:43
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
EraserWidget(PatientModelServicePtr patientModelService, VisualizationServicePtr visualizationService, QWidget *parent)
void changed()
emit when the underlying data value is changed: The user interface will be updated.
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:108
void setDeepModified(vtkImageDataPtr image)
static DoublePropertyPtr initialize(const QString &uid, QString name, QString help, double value, DoubleRange range, int decimals, QDomNode root=QDomNode())
RealScalar length() const
static ActiveImageProxyPtr New(PatientModelServicePtr patientModelService)
void activeImageChanged(const QString &uid)
The original image changed signal from DataManager.
boost::shared_ptr< class Mesh > MeshPtr
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
boost::shared_ptr< class ImageTF3D > ImageTF3DPtr