2013-04-18 10 views
6

मैं वर्तमान में यह समझने की कोशिश कर रहा हूं कि निम्न कोड (http://pastebin.com/zTHUrmyx) कैसे काम करता है, मेरा दृष्टिकोण वर्तमान में डीबग में सॉफ़्टवेयर संकलित कर रहा है और कोड के माध्यम से चरणबद्ध करने के लिए gdb का उपयोग कर रहा है।कुछ सी ++ कोडिंग अभ्यास को समझना

हालांकि, मैं इस समस्या में भाग रहा हूं कि 'कदम' हमेशा मुझे नहीं बताता कि क्या हो रहा है। मेरे लिए विशेष रूप से अस्पष्ट EXECUTE {...} है जिसमें मैं कदम नहीं उठा सकता।

कोड सीखने के बारे में मैं कैसे जा सकता हूं?

1  /* 
    2  Copyright 2008 Brain Research Institute, Melbourne, Australia 
    3 
    4  Written by J-Donald Tournier, 27/06/08. 
    5 
    6  This file is part of MRtrix. 
    7 
    8  MRtrix is free software: you can redistribute it and/or modify 
    9  it under the terms of the GNU General Public License as published by 
    10  the Free Software Foundation, either version 3 of the License, or 
    11  (at your option) any later version. 
    12 
    13  MRtrix is distributed in the hope that it will be useful, 
    14  but WITHOUT ANY WARRANTY; without even the implied warranty of 
    15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
    16  GNU General Public License for more details. 
    17 
    18  You should have received a copy of the GNU General Public License 
    19  along with MRtrix. If not, see <http://www.gnu.org/licenses/>. 
    20 
    21 
    22  15-10-2008 J-Donald Tournier <[email protected]> 
    23  * fix -prs option handling 
    24  * remove MR::DICOM_DW_gradients_PRS flag 
    25 
    26  15-10-2008 J-Donald Tournier <[email protected]> 
    27  * add -layout option to manipulate data ordering within the image file 
    28 
    29  14-02-2010 J-Donald Tournier <[email protected]> 
    30  * fix -coord option so that the "end" keyword can be used 
    31 
    32 
    33 */ 
    34 
    35 #include "app.h" 
    36 #include "image/position.h" 
    37 #include "image/axis.h" 
    38 #include "math/linalg.h" 
    39 
    40 using namespace std; 
    41 using namespace MR; 
    42 
    43 SET_VERSION_DEFAULT; 
    44 
    45 DESCRIPTION = { 
    46 "perform conversion between different file types and optionally extract a subset of the input image.", 
    47 "If used correctly, this program can be a very useful workhorse. In addition to converting images between different formats, it can be used to extract specific studies from a data set, extract a specific region of interest, flip the images, or to scale the intensity of the images.", 
    48 NULL 
    49 }; 
    50 
    51 ARGUMENTS = { 
    52 Argument ("input", "input image", "the input image.").type_image_in(), 
    53 Argument ("ouput", "output image", "the output image.").type_image_out(), 
    54 Argument::End 
    55 }; 
    56 
    57 
    58 const gchar* type_choices[] = { "REAL", "IMAG", "MAG", "PHASE", "COMPLEX", NULL }; 
    59 const gchar* data_type_choices[] = { "FLOAT32", "FLOAT32LE", "FLOAT32BE", "FLOAT64", "FLOAT64LE", "FLOAT64BE", 
    60  "INT32", "UINT32", "INT32LE", "UINT32LE", "INT32BE", "UINT32BE", 
    61  "INT16", "UINT16", "INT16LE", "UINT16LE", "INT16BE", "UINT16BE", 
    62  "CFLOAT32", "CFLOAT32LE", "CFLOAT32BE", "CFLOAT64", "CFLOAT64LE", "CFLOAT64BE", 
    63  "INT8", "UINT8", "BIT", NULL }; 
    64 
    65 OPTIONS = { 
    66 Option ("coord", "select coordinates", "extract data only at the coordinates specified.", false, true) 
    67  .append (Argument ("axis", "axis", "the axis of interest").type_integer (0, INT_MAX, 0)) 
    68  .append (Argument ("coord", "coordinates", "the coordinates of interest").type_sequence_int()), 
    69 
    70 Option ("vox", "voxel size", "change the voxel dimensions.") 
    71  .append (Argument ("sizes", "new dimensions", "A comma-separated list of values. Only those values specified will be changed. For example: 1,,3.5 will change the voxel size along the x & z axes, and leave the y-axis voxel size unchanged.") 
    72   .type_sequence_float()), 
    73 
    74 Option ("datatype", "data type", "specify output image data type.") 
    75  .append (Argument ("spec", "specifier", "the data type specifier.").type_choice (data_type_choices)), 
    76 
    77 Option ("scale", "scaling factor", "apply scaling to the intensity values.") 
    78  .append (Argument ("factor", "factor", "the factor by which to multiply the intensities.").type_float (NAN, NAN, 1.0)), 
    79 
    80 Option ("offset", "offset", "apply offset to the intensity values.") 
    81  .append (Argument ("bias", "bias", "the value of the offset.").type_float (NAN, NAN, 0.0)), 
    82 
    83 Option ("zero", "replace NaN by zero", "replace all NaN values with zero."), 
    84 
    85 Option ("output", "output type", "specify type of output") 
    86  .append (Argument ("type", "type", "type of output.") 
    87   .type_choice (type_choices)), 
    88 
    89 Option ("layout", "data layout", "specify the layout of the data in memory. The actual layout produced will depend on whether the output image format can support it.") 
    90  .append (Argument ("spec", "specifier", "the data layout specifier.").type_string()), 
    91 
    92 Option ("prs", "DW gradient specified as PRS", "assume that the DW gradients are specified in the PRS frame (Siemens DICOM only)."), 
    93 
    94 Option::End 
    95 }; 
    96 
    97 
    98 
    99 inline bool next (Image::Position& ref, Image::Position& other, const std::vector<int>* pos) 
100 { 
101 int axis = 0; 
102 do { 
103  ref.inc (axis); 
104  if (ref[axis] < ref.dim(axis)) { 
105  other.set (axis, pos[axis][ref[axis]]); 
106  return (true); 
107  } 
108  ref.set (axis, 0); 
109  other.set (axis, pos[axis][0]); 
110  axis++; 
111 } while (axis < ref.ndim()); 
112 return (false); 
113 } 
114 
115 
116 
117 
118 
119 EXECUTE { 
120 std::vector<OptBase> opt = get_options (1); // vox 
121 std::vector<float> vox; 
122 if (opt.size()) 
123  vox = parse_floats (opt[0][0].get_string()); 
124 
125 
126 opt = get_options (3); // scale 
127 float scale = 1.0; 
128 if (opt.size()) scale = opt[0][0].get_float(); 
129 
130 opt = get_options (4); // offset 
131 float offset = 0.0; 
132 if (opt.size()) offset = opt[0][0].get_float(); 
133 
134 opt = get_options (5); // zero 
135 bool replace_NaN = opt.size(); 
136 
137 opt = get_options (6); // output 
138 Image::OutputType output_type = Image::Default; 
139 if (opt.size()) { 
140  switch (opt[0][0].get_int()) { 
141  case 0: output_type = Image::Real; break; 
142  case 1: output_type = Image::Imaginary; break; 
143  case 2: output_type = Image::Magnitude; break; 
144  case 3: output_type = Image::Phase; break; 
145  case 4: output_type = Image::RealImag; break; 
146  } 
147 } 
148 
149 
150 
151 
152 Image::Object &in_obj (*argument[0].get_image()); 
153 
154 Image::Header header (in_obj); 
155 
156 if (output_type == 0) { 
157  if (in_obj.is_complex()) output_type = Image::RealImag; 
158  else output_type = Image::Default; 
159 } 
160 
161 if (output_type == Image::RealImag) header.data_type = DataType::CFloat32; 
162 else if (output_type == Image::Phase) header.data_type = DataType::Float32; 
163 else header.data_type.unset_flag (DataType::ComplexNumber); 
164 
165 
166 opt = get_options (2); // datatype 
167 if (opt.size()) header.data_type.parse (data_type_choices[opt[0][0].get_int()]); 
168 
169 for (guint n = 0; n < vox.size(); n++) 
170  if (isfinite (vox[n])) header.axes.vox[n] = vox[n]; 
171 
172 opt = get_options (7); // layout 
173 if (opt.size()) { 
174  std::vector<Image::Axis> ax = parse_axes_specifier (header.axes, opt[0][0].get_string()); 
175  if (ax.size() != (guint) header.axes.ndim()) 
176  throw Exception (String("specified layout \"") + opt[0][0].get_string() + "\" does not match image dimensions"); 
177 
178  for (guint i = 0; i < ax.size(); i++) { 
179  header.axes.axis[i] = ax[i].axis; 
180  header.axes.forward[i] = ax[i].forward; 
181  } 
182 } 
183 
184 
185 opt = get_options (8); // prs 
186 if (opt.size() && header.DW_scheme.rows() && header.DW_scheme.columns()) { 
187  for (guint row = 0; row < header.DW_scheme.rows(); row++) { 
188  double tmp = header.DW_scheme(row, 0); 
189  header.DW_scheme(row, 0) = header.DW_scheme(row, 1); 
190  header.DW_scheme(row, 1) = tmp; 
191  header.DW_scheme(row, 2) = -header.DW_scheme(row, 2); 
192  } 
193 } 
194 
195 std::vector<int> pos[in_obj.ndim()]; 
196 
197 opt = get_options (0); // coord 
198 for (guint n = 0; n < opt.size(); n++) { 
199  int axis = opt[n][0].get_int(); 
200  if (pos[axis].size()) throw Exception ("\"coord\" option specified twice for axis " + str (axis)); 
201  pos[axis] = parse_ints (opt[n][1].get_string(), header.dim(axis)-1); 
202  header.axes.dim[axis] = pos[axis].size(); 
203 } 
204 
205 for (int n = 0; n < in_obj.ndim(); n++) { 
206  if (pos[n].empty()) { 
207  pos[n].resize (in_obj.dim(n)); 
208  for (guint i = 0; i < pos[n].size(); i++) pos[n][i] = i; 
209  } 
210 } 
211 
212 
213 in_obj.apply_scaling (scale, offset); 
214 
215 
216 
217 
218 
219 
220 Image::Position in (in_obj); 
221 Image::Position out (*argument[1].get_image (header)); 
222 
223 for (int n = 0; n < in.ndim(); n++) in.set (n, pos[n][0]); 
224 
225 ProgressBar::init (out.voxel_count(), "copying data..."); 
226 
227 do { 
228 
229  float re, im = 0.0; 
230  in.get (output_type, re, im); 
231  if (replace_NaN) if (gsl_isnan (re)) re = 0.0; 
232  out.re (re); 
233 
234  if (output_type == Image::RealImag) { 
235  if (replace_NaN) if (gsl_isnan (im)) im = 0.0; 
236  out.im (im); 
237  } 
238 
239  ProgressBar::inc(); 
240 } while (next (out, in, pos)); 
241 
242 ProgressBar::done(); 
243 } 
+4

यह कोड स्पष्ट रूप से कुछ अजीब- * गधे * मैक्रोज़ का उपयोग करता है। इसे समझने में सक्षम होने के लिए, हमें सभी शीर्षकों को भी देखना होगा। फिर भी, यह प्रश्न शायद [कोड समीक्षा] (http://codereview.stackexchange.com/) के लिए बेहतर फिट है। – Angew

+0

आपको केवल प्रीप्रोकैसिंग/मैक्रो-विस्तार कंपाइलर चरण चलाकर कुछ अंतर्दृष्टि मिल सकती है। उदाहरण के लिए, जीएनयू जीसीसी के साथ यह 'g ++ -E' है: तो आप देख सकते हैं कि कोड वास्तव में क्या करता है। –

+5

मैं आपको एक डीबगर के साथ कदम उठाने की पहल करने के लिए सिर्फ एक अपवर्तित करने का लुत्फ उठा रहा हूं, जो आपको हाल ही में यहां पूछे जाने वाले 99% लोगों से आगे रखता है। – WhozCraig

उत्तर

3

के रूप में टिप्पणी में नोट किया गया था, EXECUTE किसी मैक्रो, संदर्भ एक समारोह हैडर (और शायद थोड़ा अधिक है, जैसे कुछ वैश्विक चर और कार्यों), इसलिए घुंघराले ब्रेसिज़ में हिस्सा है से स्पष्ट हो रहा है समारोह शरीर

EXECUTE की परिभाषा को प्राप्त करने के लिए, आपको शीर्षलेखों की जांच करनी होगी।

हालांकि, अगर आप डीबगिंग के दौरान कोड के कुछ हिस्से तक पहुँच सकते हैं, तो आप एक string या उस बिंदु पर char[], यह EXECUTE की stringified संस्करण देने डाल सकता है, ताकि आप जो कुछ भी पूर्वप्रक्रमक है कि कम से EXECUTEके लिए उत्सर्जित करेगा कोड में स्थिति।

#define STR(x) #x 
#define STRINGIFY(x) STR(x) 
char c[] = STRINGIFY(EXECUTE); 

दो मैक्रो एक स्ट्रिंग शाब्दिक रूप में किसी भी मैक्रो की सामग्री प्राप्त करने के एक ज्ञात थोड़ा मैक्रो चाल है। निष्पादित करने की सामग्री प्राप्त करने के लिए इसे अपने डीबगर में चार सरणी का आज़माएं और देखें।

मेरे जंगली अनुमान यहाँ: EXECUTE मुख्य कार्य या इसके लिए एक प्रतिस्थापन, OPTIONS और ARGUMENTS क्या तर्क कार्यक्रम की उम्मीद है का वर्णन है और क्या कमांड लाइन विकल्प आप इसे करने के लिए पारित कर सकते हैं है। उन मैक्रोज़ और कुछ उपयोग किए गए कार्यों और चर (get_options, argument) एक छोटे ढांचे का हिस्सा हैं जो कमांड लाइन विकल्पों के उपयोग, मूल्यांकन और उपयोगकर्ता की जानकारी को सुविधाजनक बनाना चाहिए।

+0

हाय आर्ने, उत्तर के लिए धन्यवाद। मुझे app.h मिला है जिसमें EXECUTE की परिभाषा है, http://pastebin.com/6ZnAKxDC। यह परिभाषा STRINGIFY द्वारा उत्पादित वही होगी? –

+0

@ जेसेईआरजे हाँ, जैसा कि आप देखते हैं, यह केवल मुख्य कार्य से थोड़ा अधिक है: यह निष्पादन विधि के साथ एक वर्ग को परिभाषित करता है, और एक मुख्य फ़ंक्शन जो उस वर्ग की किसी ऑब्जेक्ट को तुरंत चालू करता है, निष्पादित फ़ंक्शन को कॉल करता है और किसी भी अपवाद को संभालता है। फ़ंक्शन बॉडी के बारे में मैंने निष्पादन विधि का कार्यान्वयन किया है। –

+0

अब मैं देखता हूं कि आपका मतलब क्या है 'int main (int argc, gchar * argv []) {\ कोशिश करें {\ MyApp ऐप (...)' मैं 'MyApp' और 'App' की परिभाषा देखता हूं, लेकिन नहीं 'ऐप' के लिए। मुझे पूरा यकीन है कि 'ऐप' और 'ऐप' स्वतंत्र वर्गों या कार्यों को पूरी तरह से अलग कर रहे हैं? मुझे ऐप को ट्रैक करना होगा? –

संबंधित मुद्दे