nethercote | 5912c81 | 2004-02-15 15:38:08 +0000 | [diff] [blame] | 1 | /* This file is part of hp2ps, a graph drawer for memory profiles. |
| 2 | Copyright (C) 2002 The University Court of the University of Glasgow. |
| 3 | This program is governed by the license contained in the file LICENSE. */ |
| 4 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 5 | #include <stdio.h> |
| 6 | #include <math.h> |
| 7 | #include "Main.h" |
| 8 | #include "Defines.h" |
| 9 | #include "Dimensions.h" |
| 10 | #include "HpFile.h" |
| 11 | #include "Shade.h" |
| 12 | #include "Utilities.h" |
| 13 | |
| 14 | /* own stuff */ |
| 15 | #include "Curves.h" |
| 16 | |
| 17 | static floatish *g_x; /* x and y values */ |
| 18 | static floatish *g_y; |
| 19 | |
| 20 | static floatish *g_py; /* previous y values */ |
| 21 | |
| 22 | static void Curve PROTO((struct entry *)); /* forward */ |
nethercote | a0b7791 | 2004-06-02 20:43:24 +0000 | [diff] [blame^] | 23 | static void ShadeCurve |
| 24 | PROTO((floatish *x, floatish *y, floatish *py, floatish shade)); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 25 | |
| 26 | void |
| 27 | Curves() |
| 28 | { |
| 29 | intish i; |
| 30 | |
| 31 | for (i = 0; i < nidents; i++) { |
| 32 | Curve(identtable[i]); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | * Draw a curve, and fill the area that is below it and above |
| 38 | * the previous curve. |
| 39 | */ |
| 40 | |
| 41 | static void |
| 42 | Curve(e) |
| 43 | struct entry* e; |
| 44 | { |
| 45 | struct chunk* ch; |
| 46 | int j; |
| 47 | |
| 48 | for (ch = e->chk; ch; ch = ch->next) { |
| 49 | for (j = 0; j < ch->nd; j++) { |
| 50 | g_y[ ch->d[j].bucket ] += ch->d[j].value; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | ShadeCurve(g_x, g_y, g_py, ShadeOf(e->name)); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | static void PlotCurveLeftToRight PROTO((floatish *, floatish *)); /* forward */ |
| 59 | static void PlotCurveRightToLeft PROTO((floatish *, floatish *)); /* forward */ |
| 60 | |
| 61 | static void SaveCurve PROTO((floatish *, floatish *)); /* forward */ |
| 62 | |
| 63 | /* |
| 64 | * Map virtual x coord to physical x coord |
| 65 | */ |
| 66 | |
| 67 | floatish |
| 68 | xpage(x) |
| 69 | floatish x; |
| 70 | { |
| 71 | return (x + graphx0); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | |
| 76 | /* |
| 77 | * Map virtual y coord to physical y coord |
| 78 | */ |
| 79 | |
| 80 | floatish |
| 81 | ypage(y) |
| 82 | floatish y; |
| 83 | { |
| 84 | return (y + graphy0); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | /* |
| 89 | * Fill the region bounded by two splines, using the given |
| 90 | * shade. |
| 91 | */ |
| 92 | |
| 93 | static void |
| 94 | ShadeCurve(x, y, py, shade) |
| 95 | floatish *x; floatish *y; floatish *py; floatish shade; |
| 96 | { |
| 97 | fprintf(psfp, "%f %f moveto\n", xpage(x[0]), ypage(py[0])); |
| 98 | PlotCurveLeftToRight(x, py); |
| 99 | |
| 100 | fprintf(psfp, "%f %f lineto\n", xpage(x[nsamples - 1]), |
| 101 | ypage(y[nsamples - 1])); |
| 102 | PlotCurveRightToLeft(x, y); |
| 103 | |
| 104 | fprintf(psfp, "closepath\n"); |
| 105 | |
| 106 | fprintf(psfp, "gsave\n"); |
| 107 | |
| 108 | SetPSColour(shade); |
| 109 | fprintf(psfp, "fill\n"); |
| 110 | |
| 111 | fprintf(psfp, "grestore\n"); |
| 112 | fprintf(psfp, "stroke\n"); |
| 113 | |
| 114 | SaveCurve(y, py); |
| 115 | } |
| 116 | |
| 117 | static void |
| 118 | PlotCurveLeftToRight(x,y) |
| 119 | floatish *x; floatish *y; |
| 120 | { |
| 121 | intish i; |
| 122 | |
| 123 | for (i = 0; i < nsamples; i++) { |
| 124 | fprintf(psfp, "%f %f lineto\n", xpage(x[i]), ypage(y[i])); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | static void |
| 129 | PlotCurveRightToLeft(x,y) |
| 130 | floatish *x; floatish *y; |
| 131 | { |
| 132 | intish i; |
| 133 | |
| 134 | for (i = nsamples - 1; i >= 0; i-- ) { |
| 135 | fprintf(psfp, "%f %f lineto\n", xpage(x[i]), ypage(y[i])); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Save the curve coordinates stored in y[] in py[]. |
| 141 | */ |
| 142 | |
| 143 | static void |
| 144 | SaveCurve(y, py) |
| 145 | floatish *y; floatish* py; |
| 146 | { |
| 147 | intish i; |
| 148 | |
| 149 | for (i = 0; i < nsamples; i++) { |
| 150 | py[i] = y[i]; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | extern floatish xrange; |
| 155 | |
| 156 | void |
| 157 | CurvesInit() |
| 158 | { |
| 159 | intish i; |
| 160 | |
| 161 | g_x = (floatish*) xmalloc(nsamples * sizeof(floatish)); |
| 162 | g_y = (floatish*) xmalloc(nsamples * sizeof(floatish)); |
| 163 | g_py = (floatish*) xmalloc(nsamples * sizeof(floatish)); |
| 164 | |
| 165 | for (i = 0; i < nsamples; i++) { |
| 166 | g_x[i] = ((samplemap[i] - samplemap[0])/ xrange) * graphwidth; |
| 167 | g_y[i] = g_py[i] = 0.0; |
| 168 | } |
| 169 | } |