core/vnl/vnl_matlab_print_scalar.cxx
Go to the documentation of this file.
00001 // This is core/vnl/vnl_matlab_print_scalar.cxx
00002 
00003 #include "vnl_matlab_print_scalar.h"
00004 
00005 #include <vcl_cstdio.h>  // sprintf()
00006 #include <vcl_cstdlib.h> // abort()
00007 #include <vcl_cstring.h> // strlen()
00008 #include <vcl_complex.h>
00009 
00010 void vnl_matlab_print_scalar(int v,
00011                              char *buf,
00012                              vnl_matlab_print_format)
00013 {
00014   vcl_sprintf(buf, "%4d ", v);
00015 }
00016 
00017 void vnl_matlab_print_scalar(unsigned v,
00018                              char *buf,
00019                              vnl_matlab_print_format)
00020 {
00021   vcl_sprintf(buf, "%4u ", v);
00022 }
00023 
00024 void vnl_matlab_print_scalar(float v,
00025                              char *buf,
00026                              vnl_matlab_print_format format)
00027 {
00028   if (format == vnl_matlab_print_format_default)
00029     format = vnl_matlab_print_format_top();
00030   switch (format) {
00031   case vnl_matlab_print_format_long:
00032     if (v == 0.0)
00033       vcl_sprintf(buf, "%8d ", 0);
00034     else
00035       vcl_sprintf(buf, "%8.5f ", v);
00036     break;
00037   case vnl_matlab_print_format_short:
00038     if (v == 0.0)
00039       vcl_sprintf(buf, "%6d ", 0);
00040     else
00041       vcl_sprintf(buf, "%6.3f ", v);
00042     break;
00043   case vnl_matlab_print_format_long_e:
00044     vcl_sprintf(buf, "%11.7e ", v);
00045     break;
00046   case vnl_matlab_print_format_short_e:
00047     vcl_sprintf(buf, "%8.4e ", v);
00048     break;
00049   default:/*vnl_matlab_print_format_default:*/ vcl_abort(); break;
00050   }
00051 }
00052 
00053 void vnl_matlab_print_scalar(double v,
00054                              char *buf,
00055                              vnl_matlab_print_format format)
00056 {
00057   if (format == vnl_matlab_print_format_default)
00058     format = vnl_matlab_print_format_top();
00059   switch (format) {
00060   case vnl_matlab_print_format_long:
00061     if (v == 0.0)
00062       vcl_sprintf(buf, "%16d ", 0);
00063     else
00064       vcl_sprintf(buf, "%16.13f ", v);
00065     break;
00066   case vnl_matlab_print_format_short:
00067     if (v == 0.0)
00068       vcl_sprintf(buf, "%8d ", 0);
00069     else
00070       vcl_sprintf(buf, "%8.4f ", v);
00071     break;
00072   case vnl_matlab_print_format_long_e:
00073     vcl_sprintf(buf, "%20.14e ", v);
00074     break;
00075   case vnl_matlab_print_format_short_e:
00076     vcl_sprintf(buf, "%10.4e ", v);
00077     break;
00078   default:/*vnl_matlab_print_format_default:*/ vcl_abort(); break;
00079   }
00080 }
00081 
00082 void vnl_matlab_print_scalar(long double v,
00083                              char *buf,
00084                              vnl_matlab_print_format format)
00085 {
00086   vnl_matlab_print_scalar(double(v), buf, format); // FIXME
00087 }
00088 
00089 void vnl_matlab_print_scalar(vcl_complex<double> v,
00090                              char *buf,
00091                              vnl_matlab_print_format format)
00092 {
00093   if (format == vnl_matlab_print_format_default)
00094     format = vnl_matlab_print_format_top();
00095   int width = 16;
00096   int precision = 12;
00097   char conv = 'f';
00098 
00099   switch (format) {
00100   case vnl_matlab_print_format_long:
00101   case vnl_matlab_print_format_long_e:
00102     width = 16;
00103     precision = 12;
00104     break;
00105   case vnl_matlab_print_format_short:
00106   case vnl_matlab_print_format_short_e:
00107     width = 8;
00108     precision = 4;
00109     break;
00110   default:/*vnl_matlab_print_format_default:*/ vcl_abort(); break;
00111   }
00112 
00113   switch (format) {
00114   case vnl_matlab_print_format_long:
00115   case vnl_matlab_print_format_short:
00116     conv = 'f';
00117     break;
00118   case vnl_matlab_print_format_long_e:
00119   case vnl_matlab_print_format_short_e:
00120     conv = 'e';
00121     break;
00122   default:/*vnl_matlab_print_format_default:*/ vcl_abort(); break;
00123   }
00124 
00125   double r = vcl_real(v);
00126   double i = vcl_imag(v);
00127 
00128   char fmt[1024];
00129   // Real part
00130   if (r == 0) {
00131     vcl_sprintf(fmt, "%%" "%d" "d ", width);
00132     vcl_sprintf(buf, fmt, 0);
00133 
00134   } else {
00135     vcl_sprintf(fmt, "%%" "%d" "." "%d" "%c ", width, precision, conv);
00136     vcl_sprintf(buf, fmt, r);
00137   }
00138 
00139   buf += vcl_strlen(buf);
00140 
00141   // Imaginary part.  Width is reduced as sign is taken care of separately
00142   if (i == 0) {
00143     vcl_sprintf(fmt, " %%" "%d" "s  ", width-1);
00144     vcl_sprintf(buf, fmt, "");
00145   } else {
00146     char sign = '+';
00147     if (i < 0) {
00148       sign = '-';
00149       i = -i;
00150     }
00151     vcl_sprintf(fmt, "%c%%" "%d.%d%ci ", sign, width-1, precision, conv);
00152     vcl_sprintf(buf, fmt, i);
00153   }
00154 }
00155 
00156 void vnl_matlab_print_scalar(vcl_complex<float> v,
00157                              char *buf,
00158                              vnl_matlab_print_format format)
00159 {
00160   if (format == vnl_matlab_print_format_default)
00161     format = vnl_matlab_print_format_top();
00162   int width = 10;
00163   int precision = 6;
00164   char conv = 'f';
00165 
00166   switch (format) {
00167   case vnl_matlab_print_format_long:
00168   case vnl_matlab_print_format_long_e:
00169     width = 10;
00170     precision = 6;
00171     break;
00172   case vnl_matlab_print_format_short:
00173   case vnl_matlab_print_format_short_e:
00174     width = 8;
00175     precision = 4;
00176     break;
00177   default:/*vnl_matlab_print_format_default:*/ vcl_abort(); break;
00178   }
00179 
00180   switch (format) {
00181   case vnl_matlab_print_format_long:
00182   case vnl_matlab_print_format_short:
00183     conv = 'f';
00184     break;
00185   case vnl_matlab_print_format_long_e:
00186   case vnl_matlab_print_format_short_e:
00187     conv = 'e';
00188     break;
00189   default:/*vnl_matlab_print_format_default:*/ vcl_abort(); break;
00190   }
00191 
00192   float r = vcl_real(v);
00193   float i = vcl_imag(v);
00194 
00195   char fmt[1024];
00196   // Real part
00197   if (r == 0) {
00198     vcl_sprintf(fmt, "%%" "%d" "d ", width);
00199     vcl_sprintf(buf, fmt, 0);
00200 
00201   } else {
00202     vcl_sprintf(fmt, "%%" "%d" "." "%d" "%c ", width, precision, conv);
00203     vcl_sprintf(buf, fmt, r);
00204   }
00205 
00206   buf += vcl_strlen(buf);
00207 
00208   // Imaginary part.  Width is reduced as sign is taken care of separately
00209   if (i == 0) {
00210     vcl_sprintf(fmt, " %%" "%d" "s  ", width-1);
00211     vcl_sprintf(buf, fmt, "");
00212   } else {
00213     char sign = '+';
00214     if (i < 0) {
00215       sign = '-';
00216       i = -i;
00217     }
00218     vcl_sprintf(fmt, "%c%%" "%d.%d%ci ", sign, width-1, precision, conv);
00219     vcl_sprintf(buf, fmt, i);
00220   }
00221 }
00222 
00223 void vnl_matlab_print_scalar(vcl_complex<long double> v,
00224                              char *buf,
00225                              vnl_matlab_print_format format)
00226 {
00227   vnl_matlab_print_scalar(vcl_complex<double>(vcl_real(v), vcl_imag(v)), buf, format); // FIXME
00228 }
00229 
00230 
00231 #include <vcl_iostream.h>
00232 template <class T>
00233 vcl_ostream &vnl_matlab_print_scalar(vcl_ostream &s,
00234                                      T value,
00235                                      vnl_matlab_print_format format)
00236 {
00237   char buf[1024];
00238   vnl_matlab_print_scalar(value, buf, format);
00239   return s << buf;
00240 }
00241 
00242 #define inst(T) template vcl_ostream &vnl_matlab_print_scalar(vcl_ostream &, T, vnl_matlab_print_format)
00243 inst(int);
00244 inst(float);
00245 inst(double);
00246 inst(long double);
00247 inst(vcl_complex<float>);
00248 inst(vcl_complex<double>);
00249 inst(vcl_complex<long double>);
00250