Skip to content

Commit 4eafea9

Browse files
committed
changes that could not be git cherry picked
1 parent a2384b5 commit 4eafea9

25 files changed

+214
-167
lines changed

common/CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ endif()
7373

7474
target_link_libraries(clitkCommon ${VTK_LIBRARIES} ${ITK_LIBRARIES})
7575
# I have some linking problems on linux if VTK 7.1.1 (at least) is built statically - It asks for Qt5::X11Extras - I do not know why
76-
# workaround - link clitkCommon with Qt5::X11Extras - Qt5::X11Extras is not necessary if VTK is built dynamically
76+
# First workaround - link clitkCommon with Qt5::X11Extras - Qt5::X11Extras is not necessary if VTK is built dynamically
77+
# Second workaround - oblige the user to recompile VTK as a dynamic library (better solution)
7778
if(UNIX AND NOT APPLE)
7879
if(vv_QT_VERSION VERSION_EQUAL "5") #5
7980
if(VTK_VERSION VERSION_EQUAL "7.1.1") #7.1.1
@@ -83,9 +84,9 @@ if(UNIX AND NOT APPLE)
8384
file(GLOB FIRST_VTK_LIB ${VTK_DIR}/../../*${FIRST_VTK_ELEMENT}*.a)
8485
#message(${FIRST_VTK_LIB})
8586
if(EXISTS ${FIRST_VTK_LIB})
86-
#message("VTK is built statically")
87-
find_package(Qt5X11Extras REQUIRED)
88-
target_link_libraries(clitkCommon Qt5::X11Extras)
87+
message(FATAL_ERROR "VTK is built as a statically library - you need to recompile VTK and/or ITK as dynamic libraries")
88+
#find_package(Qt5X11Extras REQUIRED)
89+
#target_link_libraries(clitkCommon Qt5::X11Extras)
8990
endif()
9091
endif()
9192
endif()

common/clitkCommon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ namespace clitk {
7676
//--------------------------------------------------------------------
7777
// when everything goes wrong
7878
#define WHEREAMI "[ " << __FILE__ << " ] line " << __LINE__
79-
#define FATAL(a) { std::cerr << "ERROR in " << WHEREAMI << ": " << a; exit(0); }
79+
#define FATAL(a) { std::cerr << "ERROR in " << WHEREAMI << ": " << a << std::endl; exit(0); }
8080

8181
//--------------------------------------------------------------------
8282
// GGO with modified struct name

common/clitkDicomRTStruct2ImageFilter.cxx

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
// clitk
2424
#include "clitkDicomRTStruct2ImageFilter.h"
2525
#include "clitkImageCommon.h"
26+
#include "vvImageWriter.h"
2627

2728
// vtk
2829
#include <vtkVersion.h>
@@ -32,6 +33,7 @@
3233
#include <vtkLinearExtrusionFilter.h>
3334
#include <vtkMetaImageWriter.h>
3435
#include <vtkXMLPolyDataWriter.h>
36+
#include <vtkTransformPolyDataFilter.h>
3537

3638

3739
//--------------------------------------------------------------------
@@ -107,13 +109,19 @@ void clitk::DicomRTStruct2ImageFilter::SetImage(vvImage::Pointer image)
107109
{
108110
if (image->GetNumberOfDimensions() != 3) {
109111
std::cerr << "Error. Please provide a 3D image." << std::endl;
110-
exit(0);
112+
exit(EXIT_FAILURE);
111113
}
112114
mSpacing.resize(3);
113115
mOrigin.resize(3);
114116
mSize.resize(3);
115117
mDirection.resize(3);
116-
mTransformMatrix = image->GetTransform()[0]->GetMatrix();
118+
//mTransformMatrix = image->GetTransform()[0]->GetMatrix();
119+
mTransformMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
120+
for(unsigned int i=0;i<4;i++) {
121+
for(unsigned int j=0;j<4;j++) {
122+
mTransformMatrix->SetElement(i,j,image->GetTransform()[0]->GetMatrix()->GetElement(i,j));
123+
}
124+
}
117125
for(unsigned int i=0; i<3; i++) {
118126
mSpacing[i] = image->GetSpacing()[i];
119127
mOrigin[i] = image->GetOrigin()[i];
@@ -132,7 +140,7 @@ void clitk::DicomRTStruct2ImageFilter::SetImageFilename(std::string f)
132140
itk::ImageIOBase::Pointer header = clitk::readImageHeader(f);
133141
if (header->GetNumberOfDimensions() < 3) {
134142
std::cerr << "Error. Please provide a 3D image instead of " << f << std::endl;
135-
exit(0);
143+
exit(EXIT_FAILURE);
136144
}
137145
if (header->GetNumberOfDimensions() > 3) {
138146
std::cerr << "Warning dimension > 3 are ignored" << std::endl;
@@ -149,6 +157,18 @@ void clitk::DicomRTStruct2ImageFilter::SetImageFilename(std::string f)
149157
for(unsigned int j=0; j<3; j++)
150158
mDirection[i][j] = header->GetDirection(i)[j];
151159
}
160+
//cf. AddItkImage function in vvImage.txx
161+
mTransformMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
162+
mTransformMatrix->Identity();
163+
for(unsigned int i=0; i<3; i++) {
164+
double tmp = 0;
165+
for(unsigned int j=0; j<3; j++) {
166+
mTransformMatrix->SetElement(i,j,mDirection[i][j]);
167+
tmp -= mDirection[i][j] * mOrigin[j];
168+
}
169+
tmp += mOrigin[i];
170+
mTransformMatrix->SetElement(i,3,tmp);
171+
}
152172
}
153173
//--------------------------------------------------------------------
154174

@@ -206,7 +226,7 @@ void clitk::DicomRTStruct2ImageFilter::Update()
206226

207227
// Get bounds
208228
double *bounds=mesh->GetBounds();
209-
229+
/*
210230
//Change mOrigin, mSize and mSpacing with respect to the directions
211231
// Spacing is influenced by input direction
212232
std::vector<double> tempSpacing;
@@ -238,7 +258,7 @@ void clitk::DicomRTStruct2ImageFilter::Update()
238258
}
239259
mSize[i] = lrint(tempSize[i]);
240260
}
241-
261+
*/
242262
// Compute origin
243263
std::vector<double> origin;
244264
origin.resize(3);
@@ -259,7 +279,18 @@ void clitk::DicomRTStruct2ImageFilter::Update()
259279
extend[i] = mSize[i]-1;
260280
}
261281
}
262-
282+
//Apply the transform to the mesh
283+
vtkSmartPointer<vtkTransform> outputLabelmapGeometryTransform = vtkSmartPointer<vtkTransform>::New();
284+
outputLabelmapGeometryTransform->SetMatrix(mTransformMatrix);
285+
// Apparently the inverse is wrong...
286+
//outputLabelmapGeometryTransform->Inverse();
287+
vtkSmartPointer<vtkTransformPolyDataFilter> transformPolyDataFilter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
288+
#if VTK_MAJOR_VERSION <= 5
289+
transformPolyDataFilter->SetInput(mesh);
290+
#else
291+
transformPolyDataFilter->SetInputData(mesh);
292+
#endif
293+
transformPolyDataFilter->SetTransform(outputLabelmapGeometryTransform);
263294
// Create new output image
264295
mBinaryImage = vtkSmartPointer<vtkImageData>::New();
265296
#if VTK_MAJOR_VERSION <= 5
@@ -281,11 +312,7 @@ void clitk::DicomRTStruct2ImageFilter::Update()
281312

282313
// Extrude
283314
vtkSmartPointer<vtkLinearExtrusionFilter> extrude=vtkSmartPointer<vtkLinearExtrusionFilter>::New();
284-
#if VTK_MAJOR_VERSION <= 5
285-
extrude->SetInput(mesh);
286-
#else
287-
extrude->SetInputData(mesh);
288-
#endif
315+
extrude->SetInputConnection(transformPolyDataFilter->GetOutputPort());
289316
///We extrude in the -slice_spacing direction to respect the FOCAL convention (NEEDED !)
290317
extrude->SetVector(0, 0, -mSpacing[2]);
291318

@@ -295,11 +322,7 @@ void clitk::DicomRTStruct2ImageFilter::Update()
295322
//http://www.nabble.com/Bug-in-vtkPolyDataToImageStencil--td23368312.html#a23370933
296323
sts->SetTolerance(0);
297324
sts->SetInformationInput(mBinaryImage);
298-
#if VTK_MAJOR_VERSION <= 5
299-
sts->SetInput(extrude->GetOutput());
300-
#else
301325
sts->SetInputConnection(extrude->GetOutputPort(0));
302-
#endif
303326
//sts->SetInput(mesh);
304327

305328
vtkSmartPointer<vtkImageStencil> stencil=vtkSmartPointer<vtkImageStencil>::New();
@@ -316,22 +339,26 @@ void clitk::DicomRTStruct2ImageFilter::Update()
316339
stencil->ReverseStencilOn();
317340
stencil->Update();
318341

319-
/*
320-
vtkSmartPointer<vtkMetaImageWriter> w = vtkSmartPointer<vtkMetaImageWriter>::New();
321-
w->SetInput(stencil->GetOutput());
322-
w->SetFileName("binary2.mhd");
323-
w->Write();
324-
*/
325-
326342
mBinaryImage->ShallowCopy(stencil->GetOutput());
327343

344+
vvImage::Pointer vvBinaryImage = vvImage::New();
345+
vtkSmartPointer<vtkTransform> vvBinaryImageT = vtkSmartPointer<vtkTransform>::New();
346+
vvBinaryImageT->SetMatrix(mTransformMatrix);
347+
vvBinaryImage->AddVtkImage(mBinaryImage, vvBinaryImageT);
348+
328349
if (mWriteOutput) {
329-
typedef itk::Image<unsigned char, 3> ImageType;
330-
typedef itk::VTKImageToImageFilter<ImageType> ConnectorType;
331-
ConnectorType::Pointer connector = ConnectorType::New();
332-
connector->SetInput(GetOutput());
333-
connector->Update();
334-
clitk::writeImage<ImageType>(connector->GetOutput(), mOutputFilename);
350+
//typedef itk::Image<unsigned char, 3> ImageType;
351+
//typedef itk::VTKImageToImageFilter<ImageType> ConnectorType;
352+
//ConnectorType::Pointer connector = ConnectorType::New();
353+
//connector->SetInput(GetOutput());
354+
//connector->Update();
355+
//clitk::writeImage<ImageType>(connector->GetOutput(), mOutputFilename);
356+
vvImageWriter::Pointer writer = vvImageWriter::New();
357+
writer->SetInput(vvBinaryImage);
358+
if (!vvBinaryImage->GetTransform().empty())
359+
writer->SetSaveTransform(true);
360+
writer->SetOutputFileName(mOutputFilename);
361+
writer->Update();
335362
}
336363
}
337364
//--------------------------------------------------------------------
@@ -341,7 +368,10 @@ void clitk::DicomRTStruct2ImageFilter::Update()
341368
//--------------------------------------------------------------------
342369
vtkImageData * clitk::DicomRTStruct2ImageFilter::GetOutput()
343370
{
344-
assert(mBinaryImage);
371+
//assert(mBinaryImage);
372+
if (mBinaryImage == NULL) {
373+
FATAL("The binary RTStruct image is NULL");
374+
}
345375
return mBinaryImage;
346376
}
347377
//--------------------------------------------------------------------

common/clitkDicomRTStruct2ImageFilter.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,17 @@ namespace clitk {
7272

7373
//--------------------------------------------------------------------
7474

75-
template <int Dimension>
76-
typename itk::Image<unsigned char,Dimension>::ConstPointer clitk::DicomRTStruct2ImageFilter::GetITKOutput()
77-
{
78-
assert(mBinaryImage);
79-
typedef itk::Image<unsigned char,Dimension> ConnectorImageType;
80-
typedef itk::VTKImageToImageFilter <ConnectorImageType> ConnectorType;
81-
typename ConnectorType::Pointer connector = ConnectorType::New();
82-
connector->SetInput(mBinaryImage);
83-
connector->Update();
84-
return connector->GetOutput();
85-
}
75+
//template <int Dimension>
76+
//typename itk::Image<unsigned char,Dimension>::ConstPointer clitk::DicomRTStruct2ImageFilter::GetITKOutput()
77+
//{
78+
// assert(mBinaryImage);
79+
// typedef itk::Image<unsigned char,Dimension> ConnectorImageType;
80+
// typedef itk::VTKImageToImageFilter <ConnectorImageType> ConnectorType;
81+
// typename ConnectorType::Pointer connector = ConnectorType::New();
82+
// connector->SetInput(mBinaryImage);
83+
// connector->Update();
84+
// return connector->GetOutput();
85+
//}
8686
//--------------------------------------------------------------------
8787
#endif // CLITKDICOMRT_TRUCT2IMAGEFILTER_H
8888

common/clitkDicomRT_Contour.cxx

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@ void clitk::DicomRT_Contour::UpdateDicomItem()
8282
double * p = mData->GetPoint(i);
8383
points[i*3] = p[0];
8484
points[i*3+1] = p[1];
85-
#if VTK_MAJOR_VERSION <= 5
86-
points[i*3+1] = p[2];
87-
#else
8885
points[i*3+1] = p[2]-0.5;
89-
#endif
9086
}
9187

9288
// Get attribute
@@ -96,7 +92,7 @@ void clitk::DicomRT_Contour::UpdateDicomItem()
9692
at.SetFromDataElement( contourdata );
9793

9894
// Set attribute
99-
at.SetValues(&points[0], points.size(), false);
95+
at.SetValues(&points[0], points.size());
10096
DD(at.GetValues()[0]);
10197

10298
DD("replace");
@@ -151,7 +147,10 @@ bool clitk::DicomRT_Contour::Read(gdcm::Item * item)
151147
at.SetFromDataElement( contourdata );
152148
const double* points = at.GetValues();
153149
// unsigned int npts = at.GetNumberOfValues() / 3;
154-
assert(at.GetNumberOfValues() == static_cast<unsigned int>(mNbOfPoints)*3);
150+
//assert(at.GetNumberOfValues() == static_cast<unsigned int>(mNbOfPoints)*3);
151+
if (at.GetNumberOfValues() != static_cast<unsigned int>(mNbOfPoints)*3) {
152+
FATAL("The number of contour points is inconsistent with the number of triplets defining the contour");
153+
}
155154

156155
// Organize values
157156
mData = vtkSmartPointer<vtkPoints>::New();
@@ -161,19 +160,16 @@ bool clitk::DicomRT_Contour::Read(gdcm::Item * item)
161160
double p[3];
162161
p[0] = points[i*3];
163162
p[1] = points[i*3+1];
164-
#if VTK_MAJOR_VERSION <= 5
165-
p[2] = points[i*3+2];
166-
#else
167163
p[2] = points[i*3+2]+0.5;
168-
#endif
169164
mData->SetPoint(i, p);
170165
if (mZ == -1) mZ = p[2];
171166
if (std::fabs(p[2] - mZ) > mTolerance) {
172167
DD(i);
173168
DD(p[2]);
174169
DD(mZ);
175-
std::cout << "ERROR ! contour not in the same slice" << std::endl;
176-
assert(p[2] == mZ);
170+
//std::cout << "ERROR ! contour not in the same slice" << std::endl;
171+
//assert(p[2] == mZ);
172+
FATAL("ERROR ! contour not in the same slice");
177173
}
178174
}
179175

@@ -202,7 +198,10 @@ bool clitk::DicomRT_Contour::Read(gdcm::SQItem * item)
202198

203199
// Read values [Contour Data]
204200
std::vector<float> points = parse_string<float>(item->GetEntryValue(0x3006,0x0050),'\\');
205-
assert(points.size() == static_cast<unsigned int>(mNbOfPoints)*3);
201+
//assert(points.size() == static_cast<unsigned int>(mNbOfPoints)*3);
202+
if (points.size() != static_cast<unsigned int>(mNbOfPoints)*3) {
203+
FATAL("The number of contour points is inconsistent with the number of triplets defining the contour");
204+
}
206205

207206
// Organize values
208207
mData = vtkSmartPointer<vtkPoints>::New();
@@ -212,19 +211,16 @@ bool clitk::DicomRT_Contour::Read(gdcm::SQItem * item)
212211
double p[3];
213212
p[0] = points[i*3];
214213
p[1] = points[i*3+1];
215-
#if VTK_MAJOR_VERSION <= 5
216-
p[2] = points[i*3+2];
217-
#else
218214
p[2] = points[i*3+2]+0.5;
219-
#endif
220215
mData->SetPoint(i, p);
221216
if (mZ == -1) mZ = p[2];
222217
if (std::fabs(p[2] - mZ) > mTolerance) {
223218
DD(i);
224219
DD(p[2]);
225220
DD(mZ);
226-
std::cout << "ERROR ! contour not in the same slice" << std::endl;
227-
assert(p[2] == mZ);
221+
//std::cout << "ERROR ! contour not in the same slice" << std::endl;
222+
//assert(p[2] == mZ);
223+
FATAL("ERROR ! contour not in the same slice");
228224
}
229225
}
230226

@@ -280,10 +276,10 @@ void clitk::DicomRT_Contour::ComputeMeshFromDataPoints()
280276
mMesh->SetPoints(mPoints);
281277
vtkIdType ids[2];
282278
for (unsigned int idx=0 ; idx<mNbOfPoints ; idx++) {
283-
double pointIn[4];
284-
for (unsigned int j=0 ; j<3; ++j)
285-
pointIn[j] = mData->GetPoint(idx)[j];
286-
pointIn[3] = 1.0;
279+
//double pointIn[4];
280+
//for (unsigned int j=0 ; j<3; ++j)
281+
// pointIn[j] = mData->GetPoint(idx)[j];
282+
//pointIn[3] = 1.0;
287283
/*double pointOut[4];
288284
mTransformMatrix->MultiplyPoint(pointIn, pointOut);
289285
std::cout << pointOut[0] << " " << pointOut[1] << " " << pointOut[2] << " " << pointOut[3] << std::endl;

common/clitkDicomRT_ROI.cxx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,10 @@ bool clitk::DicomRT_ROI::Read(gdcm::Item * itemInfo, gdcm::Item * itemContour, d
186186
// ROI Color [ROI Display Color]
187187
gdcm::Attribute<0x3006,0x002a> color = {};
188188
color.SetFromDataSet( nestedds );
189-
assert( color.GetNumberOfValues() == 3 );
189+
//assert( color.GetNumberOfValues() == 3 );
190+
if (color.GetNumberOfValues() != 3) {
191+
FATAL("The RGB triplet color representation for ROI is not a triplet");
192+
}
190193
mColor[0] = color.GetValue(0);
191194
mColor[1] = color.GetValue(1);
192195
mColor[2] = color.GetValue(2);

common/clitkDicomRT_StructureSet.cxx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,10 @@ void clitk::DicomRT_StructureSet::Read(const std::string & filename, double tol)
397397
//const gdcm::DataElement &ssroisq = ds.GetDataElement( tssroisq );
398398
mROIInfoSequenceOfItems = ssroisq.GetValueAsSQ();
399399
gdcm::SmartPointer<gdcm::SequenceOfItems> & roi_seq = mROIInfoSequenceOfItems;
400-
assert(roi_seq); // TODO error message
400+
//assert(roi_seq); // TODO error message
401+
if (roi_seq == NULL) {
402+
FATAL("The Structure Set ROI Sequence tag does not contain any value");
403+
}
401404
for(unsigned int ridx = 0; ridx < roi_seq->GetNumberOfItems(); ++ridx)
402405
{
403406
gdcm::Item & item = roi_seq->GetItem( ridx + 1); // Item starts at 1
@@ -429,7 +432,10 @@ void clitk::DicomRT_StructureSet::Read(const std::string & filename, double tol)
429432
//const gdcm::DataElement &roicsq = ds.GetDataElement( troicsq );
430433
gdcm::SmartPointer<gdcm::SequenceOfItems> roi_contour_seq = roicsq.GetValueAsSQ();
431434
mROIContoursSequenceOfItems = roi_contour_seq;
432-
assert(roi_contour_seq); // TODO error message
435+
//assert(roi_contour_seq); // TODO error message
436+
if (roi_contour_seq == NULL) {
437+
FATAL("The ROI Contour Sequence tag does not contain any value");
438+
}
433439
for(unsigned int ridx = 0; ridx < roi_contour_seq->GetNumberOfItems(); ++ridx) {
434440
gdcm::Item & item = roi_contour_seq->GetItem( ridx + 1); // Item starts at 1
435441
// ROI number [Referenced ROI Number]

common/clitkDicomRT_StructureSet.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class DicomRT_StructureSet : public itk::LightObject{
6464
void SetTransformMatrix(vtkMatrix4x4* matrix);
6565
bool IsDicomRTStruct(const std::string & filename);
6666
void Write(const std::string & filename);
67+
void Anon(const std::string & filename,
68+
const std::string & outputfilename,
69+
const std::string newPID);
6770

6871
clitk::DicomRT_ROI * GetROIFromROINumber(int n);
6972
clitk::DicomRT_ROI* GetROIFromROIName(const std::string& name);

0 commit comments

Comments
 (0)