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