CustusX  15.4.0-beta
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxMemoryTester.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 "cxMemoryTester.h"
34 #include <vtkImageData.h>
35 #include <vtkUnsignedCharArray.h>
36 #include <vtkPointData.h>
37 
38 namespace cx
39 {
40 
41 vtkImageDataPtr MemHolder::generateVtkImageData()
42 {
43  int dim[3];
44  dim[0] = 1024;
45  dim[1] = 768;
46  dim[2] = 1;
47  int numComp = 3;
48 
49  vtkImageDataPtr data = vtkImageDataPtr::New();
50  data->SetSpacing(1, 1, 1);
51  data->SetExtent(0, dim[0]-1, 0, dim[1]-1, 0, dim[2]-1);
52 // data->SetScalarTypeToUnsignedChar();
53 // data->SetScalarType(VTK_UNSIGNED_CHAR, 3);
54 // data->SetNumberOfScalarComponents(numComp);
55 
56  int scalarSize = dim[0]*dim[1]*dim[2]*numComp;
57 
58  unsigned char *rawchars = (unsigned char*)malloc(scalarSize+1);
59  char initValue = 1;
60  std::fill(rawchars,rawchars+scalarSize, initValue);
61 
62  vtkUnsignedCharArrayPtr array = vtkUnsignedCharArrayPtr::New();
63  array->SetNumberOfComponents(1);
64  //TODO: Whithout the +1 the volume is black
65  array->SetArray(rawchars, scalarSize+1, 0); // take ownership
66  data->GetPointData()->SetScalars(array);
67 
68  // A trick to get a full LUT in Image (automatic LUT generation)
69  // Can't seem to fix this by calling Image::resetTransferFunctions() after volume is modified
70  rawchars[0] = 255;
71  data->GetScalarRange();// Update internal data in vtkImageData. Seems like it is not possible to update this data after the volume has been changed.
72  rawchars[0] = 0;
73 
74  return data;
75 }
76 
78 {
79  int N = 500;
80 
81  std::vector<vtkImageDataPtr> storage;
82  for (unsigned i=0; i<N; ++i)
83  {
84  vtkImageDataPtr data = generateVtkImageData();
85  storage.push_back(data);
86  }
87  std::cout << "generated leak for images=" << N << std::endl;
88 
89 // vtkImageDataPtr temp = vtkImageDataPtr::New();
90 // temp->DeepCopy(mRedirecter->GetOutput());
91 // mTestStorage.push_back(temp);
92 //
93 // if (mTestStorage.size()==500)
94 // {
95 // mTestStorage.clear();
96 // mTestStorageCleared = true;
97 // std::cout << "cleared test storage for vtkImageData leak" << std::endl;
98 // }
99 
100 }
101 
102 //vtkImageDataPtr generateVtkImageData()
103 //{
104 // int dimension = 100; // generate 100^3 = 1Mb volume.
105 //
106 // vtkImageDataPtr data = vtkImageDataPtr::New();
107 // data->SetSpacing(1, 1, 1);
108 // data->SetExtent(0, dimension-1, 0, dimension-1, 0, dimension-1);
109 // data->SetScalarTypeToUnsignedChar();
110 // data->SetNumberOfScalarComponents(1);
111 //
112 // int scalarSize = dimension*dimension*dimension;
113 //
114 // unsigned char *rawchars = (unsigned char*)malloc(scalarSize+1);
115 // char initValue = 1;
116 // std::fill(rawchars,rawchars+scalarSize, initValue);
117 //
118 // vtkUnsignedCharArrayPtr array = vtkUnsignedCharArrayPtr::New();
119 // array->SetNumberOfComponents(1);
120 // //TODO: Whithout the +1 the volume is black
121 // array->SetArray(rawchars, scalarSize+1, 0); // take ownership
122 // data->GetPointData()->SetScalars(array);
123 //
124 // // A trick to get a full LUT in Image (automatic LUT generation)
125 // // Can't seem to fix this by calling Image::resetTransferFunctions() after volume is modified
126 // rawchars[0] = 255;
127 // data->GetScalarRange();// Update internal data in vtkImageData. Seems like it is not possible to update this data after the volume has been changed.
128 // rawchars[0] = 0;
129 //
130 // return data;
131 //}
132 
134 {
135  Block block;
136  int N = 100;
137  for (unsigned i=0; i<N; ++i)
138  {
139  block.mData.push_back(generateVtkImageData());
140  }
141 
142  mBlocks.push_back(block);
143  std::cout << QString("generated memory: %1 Mb, %2 blocks").arg(double(block.mData.front()->GetActualMemorySize() * N) / 1000.0).arg(mBlocks.size()).toStdString() << std::endl;
144  return N;
145 }
146 
148 {
149  mBlocks.pop_back();
150  std::cout << QString("removed one block, %1 left").arg(mBlocks.size()).toStdString() << std::endl;
151  return mBlocks.size();
152 }
153 
154 
155 
156 
157 
158 MemoryTester::MemoryTester(QWidget* parent) : QMainWindow(parent)
159 {
160  mMemory.reset(new MemHolder);
161 
162  this->setWindowTitle("QtSandbox");
163 
164 // mTextEdit = new QPlainTextEdit;
165 // setCentralWidget(mTextEdit);
166  this->setCentralWidget(new QPlainTextEdit);
167 
168  addActions();
169  addToolbar();
170  addMenu();
171  // play with this one:
172  //setUnifiedTitleAndToolBarOnMac(true);
173 
174  createStatusBar();
175 
176 // cx::LayoutEditor* editor = new cx::LayoutEditor(this);
177 // this->setCentralWidget(editor);
178 }
179 
180 void MemoryTester::addActions()
181 {
182  mAction1 = new QAction("Action1", this);
183  mAction1->setIcon(QIcon(":/images/go-home.png"));
184 // mAction1->setIcon(QIcon(":/images/workflow_state_navigation.png"));
185 
186  mAddMemAction = new QAction("AddMem", this);
187  mRemoveMemAction = new QAction("RemMem", this);
188  mLeakAction = new QAction("Leak", this);
189 
190  connect(mAddMemAction, SIGNAL(triggered()), mMemory.get(), SLOT(addBlock()));
191  connect(mRemoveMemAction, SIGNAL(triggered()), mMemory.get(), SLOT(removeBlock()));
192  connect(mLeakAction, SIGNAL(triggered()), mMemory.get(), SLOT(generateLeak()));
193 
194 // mAction2 = new QAction("Action2", this);
196 // mAction2->setIcon(QIcon(":/images/workflow_state_navigation2.png"));
197 
198 // mAction3 = new QAction("Action3", this);
199 // mAction3->setIcon(QIcon(":/images/workflow_state_patient_data.png"));
200 
201 // mAction4 = new QAction("Action4", this);
202 // mAction4->setIcon(QIcon(":/images/workflow_state_patient_data2.png"));
203 
204  mAboutQtAct = new QAction(tr("About &Qt"), this);
205  mAboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
206  connect(mAboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
207 
208  mAboutAct = new QAction(tr("About QtSandbox"), this);
209  mAboutAct->setStatusTip(tr("Show the Application's About box"));
210  connect(mAboutAct, SIGNAL(triggered()), this, SLOT(about()));
211 
212  mCrashAct = new QAction(tr("Color crash"), this);
213  connect(mCrashAct, SIGNAL(triggered()), this, SLOT(colorCrash()));
214 }
215 
216 void MemoryTester::addToolbar()
217 {
218  mToolbar = this->addToolBar("Mytoolbar");
219  mToolbar->addAction(mAction1);
220  mToolbar->addAction(mAddMemAction);
221  mToolbar->addAction(mLeakAction);
222  mToolbar->addAction(mRemoveMemAction);
223 // mToolbar->addAction(mCrashAct);
224 }
225 
226 void MemoryTester::addMenu()
227 {
228  QMenu* fileMenu = menuBar()->addMenu(tr("&File"));
229  fileMenu->addAction(mAction1);
230  fileMenu->addAction(mCrashAct);
231 
232  QMenu* helpMenu = menuBar()->addMenu(tr("&Help"));
233  helpMenu->addAction(mAboutQtAct);
234  helpMenu->addAction(mAboutAct);
235 }
236 
237 
238 void MemoryTester::about()
239 {
240  QMessageBox::about(this, tr("About QtSandbox"),
241  tr("The <b>QtSandbox</b> is a place where you can "
242  "test out nifty Qt features in a small app. Enjoy!"));
243 }
244 
245 void MemoryTester::createStatusBar()
246 {
247  statusBar()->showMessage(tr("Ready"));
248 }
249 
250 void MemoryTester::colorCrash()
251 {
252 #if QT_VERSION >= 0x040500
253  QColorDialog dialog(QColor("white"), this);
254  dialog.exec();
255 #else
256  QColor result = QColorDialog::getColor(QColor("white"), this);
257 #endif
258 }
259 }
std::vector< Block > mBlocks
MemoryTester(QWidget *parent=0)
void fill(Eigen::Affine3d *self, vtkMatrix4x4Ptr m)
QToolBar * mToolbar
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
vtkSmartPointer< class vtkUnsignedCharArray > vtkUnsignedCharArrayPtr
std::vector< vtkImageDataPtr > mData