Merge branch 'master' into pygromacs
[alexxy/gromacs.git] / src / gromacs / gmxana / gmx_xpm2ps.cpp
similarity index 94%
rename from src/gromacs/gmxana/gmx_xpm2ps.c
rename to src/gromacs/gmxana/gmx_xpm2ps.cpp
index 83f5523ed7a4d93194ab5b028daad3cbb7944caf..06c2677a89503a4c5a3646b0ceb8fc554edca072 100644 (file)
  */
 #include "gmxpre.h"
 
-#include <math.h>
-#include <stdio.h>
-#include <stdlib.h>
+#include <cmath>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+
+#include <algorithm>
 
 #include "gromacs/commandline/pargs.h"
 #include "gromacs/fileio/matio.h"
@@ -51,6 +54,7 @@
 #include "gromacs/utility/cstringutil.h"
 #include "gromacs/utility/fatalerror.h"
 #include "gromacs/utility/futil.h"
+#include "gromacs/utility/gmxassert.h"
 #include "gromacs/utility/smalloc.h"
 
 #define FUDGE 1.2
@@ -200,7 +204,7 @@ gmx_bool diff_maps(int nmap1, t_mapping *map1, int nmap2, t_mapping *map2)
             {
                 bDiff = TRUE;
             }
-            if (strcmp(map1[i].desc, map2[i].desc) != 0)
+            if (std::strcmp(map1[i].desc, map2[i].desc) != 0)
             {
                 bDiff = TRUE;
             }
@@ -232,7 +236,7 @@ void leg_discrete(t_psdata ps, real x0, real y0, char *label,
     ps_rgb(ps, BLACK);
     ps_strfont(ps, font, fontsize);
     yhh = y0+fontsize+3*DDD;
-    if ((int)strlen(label) > 0)
+    if (std::strlen(label) > 0)
     {
         ps_ctext(ps, x0, yhh, label, eXLeft);
     }
@@ -264,12 +268,14 @@ void leg_continuous(t_psdata ps, real x0, real x, real y0, char *label,
     {
         x = 8*fontsize;
     }
-    boxxh = (real)x/(real)(nmap-mapoffset);
+    boxxh = x/(nmap-mapoffset);
     if (boxxh > fontsize)
     {
         boxxh = fontsize;
     }
 
+    GMX_RELEASE_ASSERT(map != NULL, "NULL map array provided to leg_continuous()");
+
     /* LANDSCAPE */
     xx0 = x0-((nmap-mapoffset)*boxxh)/2.0;
 
@@ -284,7 +290,7 @@ void leg_continuous(t_psdata ps, real x0, real x, real y0, char *label,
 
     yhh = y0+boxyh+3*DDD;
     ps_ctext(ps, xx0+boxxh/2, yhh, map[0].desc, eXCenter);
-    if ((int)strlen(label) > 0)
+    if (std::strlen(label) > 0)
     {
         ps_ctext(ps, x0, yhh, label, eXCenter);
     }
@@ -353,14 +359,15 @@ static void draw_boxes(t_psdata ps, real x0, real y0, real w,
     real     xxx;
     char   **xtick, **ytick;
     real     xx, yy, dy, xx00, yy00, offset_x, offset_y;
-    int      i, j, x, y, ntx, nty, strlength;
+    int      i, j, x, y, ntx, nty;
+    size_t   strlength;
 
     /* Only necessary when there will be no y-labels */
     strlength = 0;
 
     /* Draw the box */
     ps_rgb(ps, BLACK);
-    ps_linewidth(ps, psr->boxlinewidth);
+    ps_linewidth(ps, static_cast<int>(psr->boxlinewidth));
     yy00 = y0;
     for (i = 0; (i < nmat); i++)
     {
@@ -370,7 +377,7 @@ static void draw_boxes(t_psdata ps, real x0, real y0, real w,
     }
 
     /* Draw the ticks on the axes */
-    ps_linewidth(ps, psr->ticklinewidth);
+    ps_linewidth(ps, static_cast<int>(psr->ticklinewidth));
     xx00 = x0-1;
     yy00 = y0-1;
     for (i = 0; (i < nmat); i++)
@@ -446,7 +453,7 @@ static void draw_boxes(t_psdata ps, real x0, real y0, real w,
                 (psr->Y.first && (y == 0)))
             {
                 /* Major ticks */
-                strlength = max(strlength, (int)strlen(ytick[y]));
+                strlength = std::max(strlength, std::strlen(ytick[y]));
                 ps_line (ps, xx00, yy, xx00-psr->Y.majorticklen, yy);
                 ps_ctext(ps, xx00-psr->Y.majorticklen-DDD,
                          yy-psr->Y.tickfontsize/3.0, ytick[y], eXRight);
@@ -509,7 +516,7 @@ static void draw_zerolines(t_psdata out, real x0, real y0, real w,
 
     xx00 = x0-1.5;
     yy00 = y0-1.5;
-    ps_linewidth(out, psr->zerolinewidth);
+    ps_linewidth(out, static_cast<int>(psr->zerolinewidth));
     for (i = 0; (i < nmat); i++)
     {
         dy = box_height(&(mat[i]), psr);
@@ -522,8 +529,8 @@ static void draw_zerolines(t_psdata out, real x0, real y0, real w,
                 xx = xx00+(x+0.7)*psr->xboxsize;
                 /* draw lines whenever tick label almost zero (e.g. next trajectory) */
                 if (x != 0 && x < mat[i].nx-1 &&
-                    fabs(mat[i].axis_x[x]) <
-                    0.1*fabs(mat[i].axis_x[x+1]-mat[i].axis_x[x]) )
+                    std::abs(mat[i].axis_x[x]) <
+                    0.1*std::abs(mat[i].axis_x[x+1]-mat[i].axis_x[x]) )
                 {
                     ps_line (out, xx, yy00, xx, yy00+dy+2);
                 }
@@ -537,8 +544,8 @@ static void draw_zerolines(t_psdata out, real x0, real y0, real w,
                 yy = yy00+(y+0.7)*psr->yboxsize;
                 /* draw lines whenever tick label almost zero (e.g. next trajectory) */
                 if (y != 0 && y < mat[i].ny-1 &&
-                    fabs(mat[i].axis_y[y]) <
-                    0.1*fabs(mat[i].axis_y[y+1]-mat[i].axis_y[y]) )
+                    std::abs(mat[i].axis_y[y]) <
+                    0.1*std::abs(mat[i].axis_y[y+1]-mat[i].axis_y[y]) )
                 {
                     ps_line (out, xx00, yy, xx00+w+2, yy);
                 }
@@ -561,9 +568,9 @@ static void box_dim(int nmat, t_matrix mat[], t_matrix *mat2, t_psrec *psr,
     ww = 0;
     for (i = 0; (i < nmat); i++)
     {
-        ww       = max(ww, mat[i].nx*psr->xboxsize);
+        ww       = std::max(ww, mat[i].nx*psr->xboxsize);
         hh      += box_height(&(mat[i]), psr);
-        maxytick = max(maxytick, mat[i].nx);
+        maxytick = std::max(maxytick, mat[i].nx);
     }
     if (bFrame)
     {
@@ -574,7 +581,7 @@ static void box_dim(int nmat, t_matrix mat[], t_matrix *mat2, t_psrec *psr,
         if (psr->Y.major > 0)
         {
             dww += psr->Y.majorticklen + DDD +
-                psr->Y.tickfontsize*(log(maxytick)/log(10.0));
+                psr->Y.tickfontsize*(std::log(static_cast<real>(maxytick))/std::log(10.0));
         }
         else if (psr->Y.minor > 0)
         {
@@ -626,7 +633,7 @@ int add_maps(t_mapping **newmap,
     int         nmap, j, k;
     t_mapping  *map;
 
-    nsymbols = strlen(mapper);
+    nsymbols = std::strlen(mapper);
     nmap     = nmap1+nmap2;
     if (nmap > nsymbols*nsymbols)
     {
@@ -669,8 +676,7 @@ void xpm_mat(const char *outf, int nmat, t_matrix *mat, t_matrix *mat2,
              gmx_bool bDiag, gmx_bool bFirstDiag)
 {
     FILE      *out;
-    char       buf[100];
-    int        i, j, k, x, y, col;
+    int        i, x, y, col;
     int        nmap;
     t_mapping *map = NULL;
 
@@ -710,11 +716,11 @@ void xpm_mat(const char *outf, int nmat, t_matrix *mat, t_matrix *mat2,
             sfree(mat[i].map);
             mat[i].nmap = nmap;
             mat[i].map  = map;
-            if (strcmp(mat[i].title, mat2[i].title) != 0)
+            if (std::strcmp(mat[i].title, mat2[i].title) != 0)
             {
                 sprintf(mat[i].title+strlen(mat[i].title), " / %s", mat2[i].title);
             }
-            if (strcmp(mat[i].legend, mat2[i].legend) != 0)
+            if (std::strcmp(mat[i].legend, mat2[i].legend) != 0)
             {
                 sprintf(mat[i].legend+strlen(mat[i].legend), " / %s", mat2[i].legend);
             }
@@ -728,22 +734,22 @@ static void tick_spacing(int n, real axis[], real offset, char axisnm,
                          real *major, real *minor)
 {
     real     space;
-    gmx_bool bTryAgain, bFive;
+    gmx_bool bTryAgain;
     int      i, j, t, f = 0, ten;
 #define NFACT 4
     real     major_fact[NFACT] = {5, 4, 2, 1};
     real     minor_fact[NFACT] = {5, 4, 4, 5};
 
     /* start with interval between 10 matrix points: */
-    space = max(10*axis[1]-axis[0], axis[min(10, n-1)]-axis[0]);
+    space = std::max(10*axis[1]-axis[0], axis[std::min(10, n-1)]-axis[0]);
     /* get power of 10 */
-    ten       = (int)ceil(log(space)/log(10))-1;
+    ten       = static_cast<int>(std::ceil(std::log(space)/std::log(10.0))-1);
     bTryAgain = TRUE;
     for (t = ten+2; t > ten-3 && bTryAgain; t--)
     {
         for (f = 0; f < NFACT && bTryAgain; f++)
         {
-            space = pow(10, t) * major_fact[f];
+            space = std::pow(static_cast<real>(10.0), static_cast<real>(t)) * major_fact[f];
             /* count how many ticks we would get: */
             i = 0;
             for (j = 0; j < n; j++)
@@ -754,17 +760,17 @@ static void tick_spacing(int n, real axis[], real offset, char axisnm,
                 }
             }
             /* do we have a reasonable number of ticks ? */
-            bTryAgain = (i > min(10, n-1)) || (i < 5);
+            bTryAgain = (i > std::min(10, n-1)) || (i < 5);
         }
     }
     if (bTryAgain)
     {
-        space = max(10*axis[1]-axis[0], axis[min(10, n-1)]-axis[0]);
+        space = std::max(10*axis[1]-axis[0], axis[std::min(10, n-1)]-axis[0]);
         fprintf(stderr, "Auto tick spacing failed for %c-axis, guessing %g\n",
                 axisnm, space);
     }
     *major = space;
-    *minor = space / minor_fact[f-1];
+    *minor = space / minor_fact[(f > 0) ? f-1 : 0];
     fprintf(stderr, "Auto tick spacing for %c-axis: major %g, minor %g\n",
             axisnm, *major, *minor);
 }
@@ -780,7 +786,7 @@ void ps_mat(const char *outf, int nmat, t_matrix mat[], t_matrix mat2[],
     t_psdata      out;
     t_psrec       psrec, *psr;
     int           W, H;
-    int           i, j, x, y, col, leg = 0;
+    int           i, x, y, col, leg = 0;
     real          x0, y0, xx;
     real          w, h, dw, dh;
     int           nmap1 = 0, nmap2 = 0, leg_nmap;
@@ -868,7 +874,7 @@ void ps_mat(const char *outf, int nmat, t_matrix mat[], t_matrix mat2[],
     }
     if ( (mat[0].legend[0] == 0) && psr->legend)
     {
-        strcpy(mat[0].legend, psr->leglabel);
+        std::strcpy(mat[0].legend, psr->leglabel);
     }
 
     bTitle          = bTitle     && mat[nmat-1].title[0];
@@ -881,21 +887,21 @@ void ps_mat(const char *outf, int nmat, t_matrix mat[], t_matrix mat2[],
     box_dim(nmat, mat, mat2, psr, elegend, bFrame, &w, &h, &dw, &dh);
 
     /* Set up bounding box */
-    W = w+dw;
-    H = h+dh;
+    W = static_cast<int>(w+dw);
+    H = static_cast<int>(h+dh);
 
     /* Start box at */
     x0 = dw;
     y0 = dh;
-    x  = W+psr->xoffs;
-    y  = H+psr->yoffs;
+    x  = static_cast<int>(W+psr->xoffs);
+    y  = static_cast<int>(H+psr->yoffs);
     if (bFrame)
     {
         x += 5*DDD;
         y += 4*DDD;
     }
     out = ps_open(outf, 0, 0, x, y);
-    ps_linewidth(out, psr->linewidth);
+    ps_linewidth(out, static_cast<int>(psr->linewidth));
     ps_init_rgb_box(out, psr->xboxsize, psr->yboxsize);
     ps_init_rgb_nbox(out, psr->xboxsize, psr->yboxsize);
     ps_translate(out, psr->xoffs, psr->yoffs);
@@ -913,9 +919,9 @@ void ps_mat(const char *outf, int nmat, t_matrix mat[], t_matrix mat2[],
             /* Print title, if any */
             ps_rgb(out, BLACK);
             ps_strfont(out, psr->titfont, psr->titfontsize);
-            if (!mat2 || (strcmp(mat[i].title, mat2[i].title) == 0))
+            if (!mat2 || (std::strcmp(mat[i].title, mat2[i].title) == 0))
             {
-                strcpy(buf, mat[i].title);
+                std::strcpy(buf, mat[i].title);
             }
             else
             {
@@ -993,7 +999,7 @@ void ps_mat(const char *outf, int nmat, t_matrix mat[], t_matrix mat2[],
     if (elegend != elNone)
     {
         ps_comment(out, "Now it's legend time!");
-        ps_linewidth(out, psr->linewidth);
+        ps_linewidth(out, static_cast<int>(psr->linewidth));
         if (mat2 == NULL || elegend != elSecond)
         {
             bDiscrete = mat[0].bDiscrete;
@@ -1134,7 +1140,7 @@ void zero_lines(int nmat, t_matrix *mat, t_matrix *mat2)
             }
             for (x = 0; x < mats[i].nx-1; x++)
             {
-                if (fabs(mats[i].axis_x[x+1]) < 1e-5)
+                if (std::abs(mats[i].axis_x[x+1]) < 1e-5)
                 {
                     for (y = 0; y < mats[i].ny; y++)
                     {
@@ -1144,7 +1150,7 @@ void zero_lines(int nmat, t_matrix *mat, t_matrix *mat2)
             }
             for (y = 0; y < mats[i].ny-1; y++)
             {
-                if (fabs(mats[i].axis_y[y+1]) < 1e-5)
+                if (std::abs(mats[i].axis_y[y+1]) < 1e-5)
                 {
                     for (x = 0; x < mats[i].nx; x++)
                     {
@@ -1161,7 +1167,6 @@ void write_combined_matrix(int ecombine, const char *fn,
                            real *cmin, real *cmax)
 {
     int        i, j, k, nlevels;
-    t_mapping *map = NULL;
     FILE      *out;
     real     **rmat1, **rmat2;
     real       rhi, rlo;
@@ -1198,8 +1203,8 @@ void write_combined_matrix(int ecombine, const char *fn,
                     default:
                         gmx_fatal(FARGS, "No such combination rule %d for matrices", ecombine);
                 }
-                rlo = min(rlo, rmat1[i][j]);
-                rhi = max(rhi, rmat1[i][j]);
+                rlo = std::min(rlo, rmat1[i][j]);
+                rhi = std::max(rhi, rmat1[i][j]);
             }
         }
         if (cmin)
@@ -1210,7 +1215,7 @@ void write_combined_matrix(int ecombine, const char *fn,
         {
             rhi = *cmax;
         }
-        nlevels = max(mat1[k].nmap, mat2[k].nmap);
+        nlevels = std::max(mat1[k].nmap, mat2[k].nmap);
         if (rhi == rlo)
         {
             fprintf(stderr,
@@ -1339,25 +1344,25 @@ void rainbow_map(gmx_bool bBlue, int nmap, t_mapping map[])
         if (c <= 0.25) /* 0-0.25 */
         {
             r = 0;
-            g = pow(4*c, 0.666);
+            g = std::pow(4.0*c, 2.0/3.0);
             b = 1;
         }
         else if (c <= 0.5) /* 0.25-0.5 */
         {
             r = 0;
             g = 1;
-            b = pow(2-4*c, 0.666);
+            b = std::pow(2.0-4.0*c, 2.0/3.0);
         }
         else if (c <= 0.75) /* 0.5-0.75 */
         {
-            r = pow(4*c-2, 0.666);
+            r = std::pow(4.0*c-2.0, 2.0/3.0);
             g = 1;
             b = 0;
         }
         else /* 0.75-1 */
         {
             r = 1;
-            g = pow(4-4*c, 0.666);
+            g = std::pow(4.0-4.0*c, 2.0/3.0);
             b = 0;
         }
         map[i].rgb.r = r;
@@ -1426,7 +1431,7 @@ int gmx_xpm2ps(int argc, char *argv[])
     int             i, nmat, nmat2, etitle, elegend, ediag, erainbow, ecombine;
     t_matrix       *mat = NULL, *mat2 = NULL;
     gmx_bool        bTitle, bTitleOnce, bDiag, bFirstDiag, bGrad;
-    static gmx_bool bFrame = TRUE, bZeroLine = FALSE, bYonce = FALSE, bAdd = FALSE;
+    static gmx_bool bFrame = TRUE, bZeroLine = FALSE, bYonce = FALSE;
     static real     size   = 400, boxx = 0, boxy = 0, cmin = 0, cmax = 0;
     static rvec     grad   = {0, 0, 0};
     enum                    {
@@ -1547,7 +1552,7 @@ int gmx_xpm2ps(int argc, char *argv[])
         if (nmat != nmat2)
         {
             fprintf(stderr, "Different number of matrices, using the smallest number.\n");
-            nmat = nmat2 = min(nmat, nmat2);
+            nmat = nmat2 = std::min(nmat, nmat2);
         }
     }
     else
@@ -1568,10 +1573,10 @@ int gmx_xpm2ps(int argc, char *argv[])
     {
         for (i = 0; (i < nmat); i++)
         {
-            strcpy(mat[i].label_y, mat[i].title);
+            std::strcpy(mat[i].label_y, mat[i].title);
             if (mat2)
             {
-                strcpy(mat2[i].label_y, mat2[i].title);
+                std::strcpy(mat2[i].label_y, mat2[i].title);
             }
         }
     }