File: select_cmdline.cpp

package info (click to toggle)
mrtrix 0.2.12-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,980 kB
  • ctags: 4,172
  • sloc: cpp: 26,485; python: 913; xml: 39; makefile: 22; sh: 10
file content (159 lines) | stat: -rw-r--r-- 5,456 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
    Copyright 2008 Brain Research Institute, Melbourne, Australia

    Written by J-Donald Tournier, 27/06/08.

    This file is part of MRtrix.

    MRtrix is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MRtrix is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MRtrix.  If not, see <http://www.gnu.org/licenses/>.


    18-12-2008 J-Donald Tournier <d.tournier@brain.org.au>
    * change numbering of series to handle cases where the series number is not set

*/

#include <glib.h>
#include "file/dicom/image.h"
#include "file/dicom/series.h"
#include "file/dicom/study.h"
#include "file/dicom/patient.h"
#include "file/dicom/tree.h"

namespace MR {
  namespace File {
    namespace Dicom {

      std::vector< RefPtr<Series> > select_cmdline (const Tree& tree)
      {
        std::vector< RefPtr<Series> > series;

        if (tree.size() == 0) throw Exception ("DICOM tree its empty");

        String buf;
        const Patient* patient_p = NULL;
        if (tree.size() > 1) {
          while (patient_p == NULL) {
            fprintf(stderr, "Select patient (q to abort):\n");
            for (guint i = 0; i < tree.size(); i++) {
              fprintf (stderr, "  %2u - %s %s %s\n", 
                  i+1, 
                  tree[i]->name.c_str(),  
                  format_ID(tree[i]->ID).c_str(),
                  format_date (tree[i]->DOB).c_str() );
            }
            std::cerr << "? ";
            std::cin >> buf;
            if (!g_ascii_isdigit (buf[0])) { series.clear(); return (series); }
            gint n = to<gint>(buf) - 1;
            if (n > (gint) tree.size()) fprintf (stderr, "invalid selection - try again\n");
            else patient_p = tree[n].get();
          }
        }
        else patient_p = tree[0].get();



        const Patient& patient (*patient_p);

        if (tree.size() > 1) {
          fprintf (stderr, "patient: %s %s %s\n", 
              patient.name.c_str(), 
              format_ID (patient.ID).c_str(),
              format_date (patient.DOB).c_str() );
        }


        const Study* study_p = NULL;
        if (patient.size() > 1) {
          while (study_p == NULL) {
            fprintf (stderr, "Select study (q to abort):\n");
            for (guint i = 0; i < patient.size(); i++) {
              fprintf (stderr, "  %4u - %s %s %s %s\n", 
                  i+1, 
                  ( patient[i]->name.size() ? patient[i]->name.c_str() : "unnamed" ),
                  format_ID (patient[i]->ID).c_str(), 
                  format_date (patient[i]->date).c_str(), 
                  format_time (patient[i]->time).c_str() );
            }
            std::cerr << "? ";
            std::cin >> buf;
            if (!g_ascii_isdigit (buf[0])) { series.clear(); return (series); }
            gint n = to<gint>(buf) - 1;
            if (n > (gint) patient.size()) fprintf (stderr, "invalid selection - try again\n");
            else study_p = patient[n].get();
          }
        }
        else study_p = patient[0].get();



        const Study& study(*study_p);

        if (patient.size() > 1) {
          fprintf (stderr, "study: %s %s %s %s\n", 
              ( study.name.size() ? study.name.c_str() : "unnamed" ),
              format_ID (study.ID).c_str(),
              format_date (study.date).c_str(),
              format_time (study.time).c_str() );
        }



        if (study.size() > 1) {
          while (series.size() == 0) {
            fprintf (stderr, "Select series ('q' to abort):\n");
            for (guint i = 0; i < study.size(); i++) {
              fprintf (stderr, "  %2u - %4"G_GSIZE_FORMAT" %s images %8s %s (%s) [%u]\n", 
                  i,
                  study[i]->size(), 
                  ( study[i]->modality.size() ? study[i]->modality.c_str() : "" ), 
                  format_time (study[i]->time).c_str(), 
                  ( study[i]->name.size() ? study[i]->name.c_str() : "unnamed" ),
                  ( (*study[i])[0]->sequence_name.size() ? (*study[i])[0]->sequence_name.c_str() : "?" ),
                  study[i]->number);
            }
            std::cerr << "? ";
            std::cin >> buf;
            if (!g_ascii_isdigit (buf[0])) { series.clear(); return (series); }
            std::vector<int> seq;
            try { seq = parse_ints (buf); }
            catch (Exception) { fprintf (stderr, "Invalid number sequence - please try again\n"); seq.clear(); }
            if (seq.size()) {
              for (guint i = 0; i < seq.size(); i++) {
                if (seq[i] < 0 || seq[i] >= (int) study.size()) {
                  fprintf (stderr, "invalid selection - try again\n");
                  series.clear();
                  break;
                }
                series.push_back (study[seq[i]]);
              }
            }
          }
        }
        else series.push_back (study[0]);

        return (series);
      }



      std::vector< RefPtr<Series> > (*select_func) (const Tree& tree) = select_cmdline;



    }
  }
}