CustusX  15.4.0-beta
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 
62 namespace cx
63 {
64 
65 EraserWidget::EraserWidget(QWidget* parent) :
66  BaseWidget(parent, "EraserWidget", "Eraser"),
67  mPreviousCenter(0,0,0),
68  mPreviousRadius(0)
69 {
70 
71  QVBoxLayout* layout = new QVBoxLayout(this);
72  this->setToolTip("Erase parts of volumes/models");
73 
74  mContinousEraseTimer = new QTimer(this);
75  connect(mContinousEraseTimer, SIGNAL(timeout()), this, SLOT(continousRemoveSlot())); // this signal will be executed in the thread of THIS, i.e. the main thread.
76 
77  QHBoxLayout* buttonLayout = new QHBoxLayout;
78  layout->addLayout(buttonLayout);
79  QHBoxLayout* buttonLayout2 = new QHBoxLayout;
80  layout->addLayout(buttonLayout2);
81 
82  mShowEraserCheckBox = new QCheckBox("Show");
83  mShowEraserCheckBox->setToolTip("Show eraser sphere in the views.");
84  connect(mShowEraserCheckBox, SIGNAL(toggled(bool)), this, SLOT(toggleShowEraser(bool)));
85  buttonLayout->addWidget(mShowEraserCheckBox);
86 
87  mContinousEraseCheckBox = new QCheckBox("Continous");
88  mContinousEraseCheckBox->setToolTip("Erase continously using the sphere. (might be slow)");
89  connect(mContinousEraseCheckBox, SIGNAL(toggled(bool)), this, SLOT(toggleContinous(bool)));
90  buttonLayout2->addWidget(mContinousEraseCheckBox);
91 
92  mDuplicateAction = this->createAction(this, QIcon(), "Duplicate", "Duplicate active volume - do this before erasing!",
93  SLOT(duplicateSlot()), buttonLayout);
94 
95  mSaveAction = this->createAction(this, QIcon(), "Save", "Save modified image to disk",
96  SLOT(saveSlot()), buttonLayout);
97 
98  mRemoveAction = this->createAction(this, QIcon(), "Erase", "Erase everything inside sphere",
99  SLOT(removeSlot()), buttonLayout2);
100 
101 
102  double sphereRadius = 10;
103  mSphereSizeAdapter = DoubleProperty::initialize("SphereSize", "Sphere Size", "Radius of Eraser Sphere", sphereRadius, DoubleRange(1,200,1), 0, QDomNode());
104  connect(mSphereSizeAdapter.get(), SIGNAL(changed()), this, SLOT(sphereSizeChangedSlot()));
105  mSphereSize = new SpinBoxAndSliderGroupWidget(this, mSphereSizeAdapter);
106  layout->addWidget(mSphereSize);
107 
108  layout->addStretch();
109 
110  this->enableButtons();
111 }
112 
113 void EraserWidget::enableButtons()
114 {
115  bool e = mShowEraserCheckBox->isChecked();
116 
117  mContinousEraseCheckBox->setEnabled(e);
118 // mDuplicateAction->setEnabled(e);
119 // mSaveAction->setEnabled(e);
120  mRemoveAction->setEnabled(e);
121  mSphereSize->setEnabled(e);
122 }
123 
125 {
126 }
127 
128 void EraserWidget::toggleContinous(bool on)
129 {
130  if (on)
131  {
132  mContinousEraseTimer->start(300);
133  }
134  else
135  {
136  mContinousEraseTimer->stop();
137  }
138 }
139 
140 void EraserWidget::continousRemoveSlot()
141 {
142  Transform3D rMd = viewService()->getGroup(0)->getOptions().mPickerGlyph->get_rMd();
143  //Transform3D rMd = viewService()->getViewGroupDatas().front()->getData()->getOptions().mPickerGlyph->get_rMd();
144  Vector3D c(mSphere->GetCenter());
145  c = rMd.coord(c);
146  double r = mSphere->GetRadius();
147 
148  // optimization: dont remove if idle
149  if (similar(mPreviousCenter, c) && similar(mPreviousRadius, r))
150  return;
151 
152  this->removeSlot();
153 }
154 
155 void EraserWidget::duplicateSlot()
156 {
157  ImagePtr original = patientService()->getActiveImage();
158 
159  ImagePtr duplicate = duplicateImage(patientService(), original);
160  patientService()->insertData(duplicate);
161  patientService()->setActiveImage(duplicate);
162 
163  // replace viz of original with duplicate
164 // std::vector<ViewGroupPtr> viewGroups = viewService()->getViewGroupDatas();
165  for (unsigned i = 0; i < viewService()->groupCount(); ++i)
166  {
167  if (viewService()->getGroup(i)->removeData(original->getUid()))
168  viewService()->getGroup(i)->addData(duplicate->getUid());
169  }
170 }
171 
172 void EraserWidget::sphereSizeChangedSlot()
173 {
174  if (mSphere)
175  {
176  mSphere->SetRadius(mSphereSizeAdapter->getValue());
177  mSphere->Update();
178  }
179 }
180 
185 void EraserWidget::saveSlot()
186 {
187  patientService()->insertData(patientService()->getActiveImage());
188 }
189 
190 
191 template <class TYPE>
192 void EraserWidget::eraseVolume(TYPE* volumePointer, TYPE replaceVal)
193 {
194  ImagePtr image = patientService()->getActiveImage();
195  vtkImageDataPtr img = image->getBaseVtkImageData();
196 
197  Eigen::Array3i dim(img->GetDimensions());
198  Vector3D spacing(img->GetSpacing());
199 
200  Transform3D rMd = viewService()->getGroup(0)->getOptions().mPickerGlyph->get_rMd();
201  Vector3D c(mSphere->GetCenter());
202  c = rMd.coord(c);
203  double r = mSphere->GetRadius();
204  mPreviousCenter = c;
205  mPreviousRadius = r;
206 
207  DoubleBoundingBox3D bb_r(c[0]-r, c[0]+r, c[1]-r, c[1]+r, c[2]-r, c[2]+r);
208 
209  Transform3D dMr = image->get_rMd().inv();
210  Transform3D rawMd = createTransformScale(spacing).inv();
211  Transform3D rawMr = rawMd * dMr;
212  Vector3D c_d = dMr.coord(c);
213  double r_d = dMr.vector(r * Vector3D::UnitX()).length();
214  c = rawMr.coord(c);
215  r = rawMr.vector(r * Vector3D::UnitX()).length();
216  DoubleBoundingBox3D bb0_raw = transform(rawMr, bb_r);
217  IntBoundingBox3D bb1_raw(0, dim[0], 0, dim[1], 0, dim[2]);
218 
219 // std::cout << " sphere: " << bb0_raw << std::endl;
220 // std::cout << " raw: " << bb1_raw << std::endl;
221 
222  for (int i=0; i<3; ++i)
223  {
224  bb1_raw[2*i] = std::max<double>(bb1_raw[2*i], bb0_raw[2*i]);
225  bb1_raw[2*i+1] = std::min<double>(bb1_raw[2*i+1], bb0_raw[2*i+1]);
226  }
227 
228  for (int x = bb1_raw[0]; x < bb1_raw[1]; ++x)
229  for (int y = bb1_raw[2]; y < bb1_raw[3]; ++y)
230  for (int z = bb1_raw[4]; z < bb1_raw[5]; ++z)
231  {
232  int index = x + y * dim[0] + z * dim[0] * dim[1];
233  if ((Vector3D(x*spacing[0], y*spacing[1], z*spacing[2]) - c_d).length() < r_d)
234  volumePointer[index] = replaceVal;
235  }
236 }
237 
238 //#define VTK_VOID 0
239 //#define VTK_BIT 1
240 //#define VTK_CHAR 2
241 //#define VTK_SIGNED_CHAR 15
242 //#define VTK_UNSIGNED_CHAR 3
243 //#define VTK_SHORT 4
244 //#define VTK_UNSIGNED_SHORT 5
245 //#define VTK_INT 6
246 //#define VTK_UNSIGNED_INT 7
247 //#define VTK_LONG 8
248 //#define VTK_UNSIGNED_LONG 9
249 //#define VTK_FLOAT 10
250 //#define VTK_DOUBLE 11
251 //#define VTK_ID_TYPE 12
252 
253 void EraserWidget::removeSlot()
254 {
255  if (!mSphere)
256  return;
257 
258  ImagePtr image = patientService()->getActiveImage();
259  vtkImageDataPtr img = image->getBaseVtkImageData();
260 
261  if (img->GetScalarType()==VTK_CHAR)
262  this->eraseVolume(static_cast<char*> (img->GetScalarPointer()), VTK_CHAR_MIN);
263  if (img->GetScalarType()==VTK_UNSIGNED_CHAR)
264  this->eraseVolume(static_cast<unsigned char*> (img->GetScalarPointer()), VTK_UNSIGNED_CHAR_MIN);
265  if (img->GetScalarType()==VTK_UNSIGNED_SHORT)
266  this->eraseVolume(static_cast<unsigned short*> (img->GetScalarPointer()), VTK_UNSIGNED_SHORT_MIN);
267  if (img->GetScalarType()==VTK_SHORT)
268  this->eraseVolume(static_cast<short*> (img->GetScalarPointer()), VTK_SHORT_MIN);
269  if (img->GetScalarType()==VTK_UNSIGNED_INT)
270  this->eraseVolume(static_cast<unsigned int*> (img->GetScalarPointer()), VTK_UNSIGNED_INT_MIN);
271  if (img->GetScalarType()==VTK_INT)
272  this->eraseVolume(static_cast<int*> (img->GetScalarPointer()), VTK_INT_MIN);
273 
274  ImageLUT2DPtr tf2D = image->getLookupTable2D();
275  ImageTF3DPtr tf3D = image->getTransferFunctions3D();
276 
277 // img->Modified();
278  setDeepModified(img);
279  image->setVtkImageData(img);
280 
281  // keep existing transfer functions
282  image->setLookupTable2D(tf2D);
283  image->setTransferFunctions3D(tf3D);
284 }
285 
286 void EraserWidget::toggleShowEraser(bool on)
287 {
288  if (on)
289  {
290 // std::vector<ViewGroupPtr> viewGroups = viewService()->getViewGroups();
291  mSphere = vtkSphereSourcePtr::New();
292 
293  mSphere->SetRadius(40);
294  mSphere->SetThetaResolution(16);
295  mSphere->SetPhiResolution(12);
296  mSphere->LatLongTessellationOn(); // more natural wireframe view
297 
298  double a = mSphereSizeAdapter->getValue();
299  mSphere->SetRadius(a);
300  mSphere->Update();
301  MeshPtr glyph = viewService()->getGroup(0)->getOptions().mPickerGlyph;
302  glyph->setVtkPolyData(mSphere->GetOutput());
303  glyph->setColor(QColor(255, 204, 0)); // same as tool
304  glyph->setIsWireframe(true);
305 
306  // set same glyph in all groups
307  for (unsigned i=0; i<viewService()->groupCount(); ++i)
308  {
309  ViewGroupData::Options options = viewService()->getGroup(i)->getOptions();
310  options.mPickerGlyph = glyph;
311  viewService()->getGroup(i)->setOptions(options);
312  }
313  }
314  else
315  {
316  viewService()->getGroup(0)->getOptions().mPickerGlyph->setVtkPolyData(NULL);
317  mContinousEraseCheckBox->setChecked(false);
318  }
319 
320  this->enableButtons();
321 }
322 
323 }
DoubleBoundingBox3D transform(const Transform3D &m, const DoubleBoundingBox3D &bb)
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
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)
EraserWidget(QWidget *parent)
Composite widget for scalar data manipulation.
ImagePtr duplicateImage(PatientModelServicePtr dataManager, ImagePtr image)
boost::shared_ptr< class ImageLUT2D > ImageLUT2DPtr
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)
cxLogicManager_EXPORT PatientModelServicePtr patientService()
static DoublePropertyPtr initialize(const QString &uid, QString name, QString help, double value, DoubleRange range, int decimals, QDomNode root=QDomNode())
RealScalar length() const
boost::shared_ptr< class Mesh > MeshPtr
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
boost::shared_ptr< class ImageTF3D > ImageTF3DPtr