source: trunk/gui/src/RpDicomToVtk.cc @ 4503

Last change on this file since 4503 was 4128, checked in by ldelgass, 7 years ago

Fix for building --without-vtkdicom (the current default)

File size: 12.8 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  RpDicomToVtk -
4 *
5 * ======================================================================
6 *  AUTHOR:  Leif Delgass, Purdue University
7 *  Copyright (c) 2014  HUBzero Foundation, LLC
8 *
9 *  See the file "license.terms" for information on usage and
10 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 * ======================================================================
12 */
13
14#include <vtkSmartPointer.h>
15#include <vtkStringArray.h>
16#include <vtkIntArray.h>
17#include <vtkDataSetWriter.h>
18#include <vtkDirectory.h>
19#ifdef USE_VTK_DICOM_PACKAGE
20#include <vtkDICOMSorter.h>
21#include <vtkDICOMReader.h>
22#include <vtkDICOMMetaData.h>
23#else
24#include <vtkDICOMImageReader.h>
25#endif
26
27#include <stdio.h>
28#include "tcl.h"
29
30static int
31DicomToVtkCmd(ClientData clientData, Tcl_Interp *interp, int objc,
32              Tcl_Obj *const *objv)
33{
34    if (objc != 3) {
35        Tcl_AppendResult(interp, "wrong # args: should be \"",
36                         Tcl_GetString(objv[0]), " dir|files fileList\"", (char*)NULL);
37        return TCL_ERROR;
38    }
39    bool isDir = false;
40    char *arg = Tcl_GetString(objv[1]);
41    if (arg[0] == 'd' && strcmp(arg, "dir") == 0) {
42        isDir = true;
43    }
44
45    char *dirName = NULL;
46    vtkSmartPointer<vtkStringArray> pathArray = vtkSmartPointer<vtkStringArray>::New();
47    if (isDir) {
48        dirName = Tcl_GetString(objv[2]);
49    } else {
50        // Get path array from Tcl list
51        int pathListc;
52        Tcl_Obj **pathListv = NULL;
53        if (Tcl_ListObjGetElements(interp, objv[2], &pathListc, &pathListv) != TCL_OK) {
54            return TCL_ERROR;
55        }
56        pathArray->SetNumberOfValues(pathListc);
57        for (int i = 0; i < pathListc; i++) {
58            pathArray->SetValue(i, Tcl_GetString(pathListv[i]));
59        }
60    }
61
62#ifdef USE_VTK_DICOM_PACKAGE
63    vtkSmartPointer<vtkDICOMReader> reader = vtkSmartPointer<vtkDICOMReader>::New();
64    vtkSmartPointer<vtkDICOMSorter> sorter = vtkSmartPointer<vtkDICOMSorter>::New();
65    if (isDir) {
66        vtkSmartPointer<vtkDirectory> dir = vtkSmartPointer<vtkDirectory>::New();
67        if (!dir->Open(dirName)) {
68            return TCL_ERROR;
69        }
70
71        int numFiles = dir->GetNumberOfFiles();
72        for (int i = 0; i < numFiles; i++) {
73            if (dir->GetFile(i)[0] == '.') {
74                continue;
75            }
76
77            std::string path(dirName);
78            path += "/";
79            path += dir->GetFile(i);
80
81            if (!dir->FileIsDirectory(path.c_str()) &&
82                reader->CanReadFile(path.c_str())) {
83                pathArray->InsertNextValue(path);
84            } else {
85                fprintf(stderr, "Skipping file %s\n", path.c_str());
86            }
87        }
88    }
89
90    sorter->SetInputFileNames(pathArray);
91    sorter->Update();
92
93    int numStudies = sorter->GetNumberOfStudies();
94    int study = 0;
95    int series = 0;
96
97    fprintf(stderr, "Num Studies: %d\n", numStudies);
98    vtkStringArray *files;
99#if 0
100    for (int i = 0; i < numStudies; i++) {
101        int numSeries = sorter->GetNumberOfSeriesInStudy(i);
102        fprintf(stderr, "Study %d: %d series\n", i, numSeries);
103        int k = sorter->GetFirstSeriesInStudy(i);
104        for (int j = 0; j < numSeries; j++) {
105            files = sorter->GetFileNamesForSeries(k+j);
106        }
107    }
108#else
109    if (study < 0 || study > sorter->GetNumberOfStudies()-1) {
110        return TCL_ERROR;
111    }
112    if (series < 0 || series > sorter->GetNumberOfSeriesInStudy(study)-1) {
113        return TCL_ERROR;
114    }
115    files = sorter->GetFileNamesForSeries(sorter->GetFirstSeriesInStudy(study)+series);
116#endif
117
118    Tcl_Obj *objPtr = Tcl_NewListObj(0, NULL);
119    Tcl_Obj *metaDataObj = Tcl_NewListObj(0, NULL);
120    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("num_studies", -1));
121    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewIntObj(numStudies));
122    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("study", -1));
123    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewIntObj(study));
124    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("num_series", -1));
125    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewIntObj(sorter->GetNumberOfSeriesInStudy(study)));
126    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("series", -1));
127    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewIntObj(series));
128
129    reader->SetFileNames(files);
130#else
131    Tcl_Obj *objPtr = Tcl_NewListObj(0, NULL);
132    Tcl_Obj *metaDataObj = Tcl_NewListObj(0, NULL);
133
134    vtkSmartPointer<vtkDICOMImageReader> reader = vtkSmartPointer<vtkDICOMImageReader>::New();
135    if (isDir) {
136        reader->SetDirectoryName(dirName);
137    } else {
138        reader->SetFileName(pathArray->GetValue(0));
139    }
140#endif
141    reader->Update();
142
143    Tcl_Obj *eltObj;
144#ifdef USE_VTK_DICOM_PACKAGE
145    vtkStringArray *ids = reader->GetStackIDs();
146    for (int i = 0; i < ids->GetNumberOfValues(); i++) {
147        fprintf(stderr, "Stack: %s\n", ids->GetValue(i).c_str());
148    }
149    vtkIntArray *fidxArray = reader->GetFileIndexArray();
150    vtkDICOMMetaData *md = reader->GetMetaData();
151    int numComp = fidxArray->GetNumberOfComponents();
152#if 0
153    for (int i = 0; i < fidxArray->GetNumberOfTuples(); i++) {
154        fprintf(stderr, "%d:", i);
155        for (int j = 0; j < fidxArray->GetNumberOfComponents(); j++) {
156            int idx = (int)fidxArray->GetComponent(i, j);
157            fprintf(stderr, " %d", idx);
158        }
159        fprintf(stderr, "\n");
160    }
161#endif
162    fprintf(stderr, "Number of data elements: %d\n", md->GetNumberOfDataElements());
163
164    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("num_files", -1));
165    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewIntObj(md->GetNumberOfInstances()));
166    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("num_components", -1));
167    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewIntObj(numComp));
168    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("modality", -1));
169    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj(md->GetAttributeValue(DC::Modality).AsString().c_str(), -1));
170    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("patient_name", -1));
171    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj(md->GetAttributeValue(DC::PatientName).AsString().c_str(), -1));
172    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("transfer_syntax_uid", -1));
173    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj(md->GetAttributeValue(DC::TransferSyntaxUID).AsString().c_str(), -1));
174    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("study_uid", -1));
175    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj(md->GetAttributeValue(DC::StudyInstanceUID).AsString().c_str(), -1));
176    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("study_id", -1));
177    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj(md->GetAttributeValue(DC::StudyID).AsString().c_str(), -1));
178    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("series_number", -1));
179    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewIntObj(md->GetAttributeValue(DC::SeriesNumber).AsInt()));
180    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("series_in_study", -1));
181    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewIntObj(md->GetAttributeValue(DC::SeriesInStudy).AsInt()));
182    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("series_uid", -1));
183    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj(md->GetAttributeValue(DC::SeriesInstanceUID).AsString().c_str(), -1));
184    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("bits_allocated", -1));
185    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewIntObj(md->GetAttributeValue(DC::BitsAllocated).AsInt()));
186    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("bits_stored", -1));
187    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewIntObj(md->GetAttributeValue(DC::BitsStored).AsInt()));
188    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("pixel_representation", -1));
189    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj(md->GetAttributeValue(DC::PixelRepresentation).AsString().c_str(), -1));
190    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("rescale_slope", -1));
191    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewDoubleObj(md->GetAttributeValue(DC::RescaleSlope).AsDouble()));
192    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("rescale_intercept", -1));
193    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewDoubleObj(md->GetAttributeValue(DC::RescaleIntercept).AsDouble()));
194    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("time_dimension", -1));
195    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewIntObj(reader->GetTimeDimension()));
196    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("time_spacing", -1));
197    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewDoubleObj(reader->GetTimeSpacing()));
198#else
199    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("patient_name", -1));
200    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj(reader->GetPatientName(), -1));
201    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("transfer_syntax_uid", -1));
202    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj(reader->GetTransferSyntaxUID(), -1));
203    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("study_uid", -1));
204    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj(reader->GetStudyUID(), -1));
205    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("study_id", -1));
206    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj(reader->GetStudyID(), -1));
207    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("num_components", -1));
208    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewIntObj(reader->GetNumberOfComponents()));
209    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("bits_allocated", -1));
210    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewIntObj(reader->GetBitsAllocated()));
211    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("pixel_representation", -1));
212    Tcl_ListObjAppendElement(interp, metaDataObj,
213                             Tcl_NewStringObj((reader->GetPixelRepresentation() ? "signed" : "unsigned"), -1));
214    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("rescale_slope", -1));
215    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewDoubleObj(reader->GetRescaleSlope()));
216    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("rescale_offset", -1));
217    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewDoubleObj(reader->GetRescaleOffset()));
218    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewStringObj("gantry_angle", -1));
219    Tcl_ListObjAppendElement(interp, metaDataObj, Tcl_NewDoubleObj(reader->GetGantryAngle()));
220#if 0
221    fprintf(stderr, "Position: %g %g %g\n",
222            reader->GetImagePositionPatient()[0],
223            reader->GetImagePositionPatient()[1],
224            reader->GetImagePositionPatient()[2]);
225    fprintf(stderr, "Orientation: %g %g %g, %g %g %g\n",
226            reader->GetImageOrientationPatient()[0],
227            reader->GetImageOrientationPatient()[1],
228            reader->GetImageOrientationPatient()[2],
229            reader->GetImageOrientationPatient()[3],
230            reader->GetImageOrientationPatient()[4],
231            reader->GetImageOrientationPatient()[5]);
232#endif
233#endif
234
235    Tcl_ListObjAppendList(interp, objPtr, metaDataObj);
236
237    fprintf(stderr, "writing VTK\n");
238
239    vtkSmartPointer<vtkDataSetWriter> writer = vtkSmartPointer<vtkDataSetWriter>::New();
240    writer->SetInputConnection(reader->GetOutputPort());
241    writer->SetFileTypeToBinary();
242    writer->WriteToOutputStringOn();
243    writer->Update();
244
245    fprintf(stderr, "writing VTK...done\n");
246
247    Tcl_ListObjAppendElement(interp, objPtr, Tcl_NewStringObj("vtkdata", -1));
248
249    eltObj = Tcl_NewByteArrayObj((unsigned char *)writer->GetBinaryOutputString(),
250                                 writer->GetOutputStringLength());
251    Tcl_ListObjAppendElement(interp, objPtr, eltObj);
252
253    Tcl_SetObjResult(interp, objPtr);
254    return TCL_OK;
255}
256
257extern "C" {
258/*
259 * ------------------------------------------------------------------------
260 *  RpDicomToVtk_Init --
261 *
262 *  Invoked when the Rappture GUI library is being initialized
263 *  to install the "DicomToVtk" command into the interpreter.
264 *
265 *  Returns TCL_OK if successful, or TCL_ERROR (along with an error
266 *  message in the interp) if anything goes wrong.
267 * ------------------------------------------------------------------------
268 */
269int
270RpDicomToVtk_Init(Tcl_Interp *interp)
271{
272    /* install the widget command */
273    Tcl_CreateObjCommand(interp, "Rappture::DicomToVtk", DicomToVtkCmd,
274                         NULL, NULL);
275    return TCL_OK;
276}
277
278}
Note: See TracBrowser for help on using the repository browser.