File: graphplot.C

package info (click to toggle)
mixviews 1.20-10.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,928 kB
  • ctags: 5,960
  • sloc: cpp: 32,879; ansic: 2,110; makefile: 445; sh: 17
file content (187 lines) | stat: -rw-r--r-- 5,455 bytes parent folder | download
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// graphplot.C

/******************************************************************************
 *
 *  MiXViews - an X window system based sound & data editor/processor
 *
 *  Copyright (c) 1993, 1994 Regents of the University of California
 *
 *  Author:     Douglas Scott
 *  Date:       December 13, 1994
 *
 *  Permission to use, copy and modify this software and its documentation
 *  for research and/or educational purposes and without fee is hereby granted,
 *  provided that the above copyright notice appear in all copies and that
 *  both that copyright notice and this permission notice appear in
 *  supporting documentation. The author reserves the right to distribute this
 *  software and its documentation.  The University of California and the author
 *  make no representations about the suitability of this software for any 
 *  purpose, and in no event shall University of California be liable for any
 *  damage, loss of data, or profits resulting from its use.
 *  It is provided "as is" without express or implied warranty.
 *
 ******************************************************************************/


#ifdef __GNUG__
#pragma implementation
#endif

#include <InterViews/color.h>
#include <InterViews/painter.h>
#include <InterViews/pattern.h>
#include <InterViews/perspective.h>
#include <InterViews/display.h>
#include <InterViews/world.h>
#include <IV-X11/xdisplay.h>
#include "data.h"
#include "graph.h"
#include "graphplot.h"
#include "localdefs.h"

SimplePlot::SimplePlot(Graph *gr, int points) : GraphPlot(gr, points),
		maxTraces(MaxTraces), nTraces(0), nPoints(0) {
	for(int i = 0; i < maxTraces; i++) {
		xlocs[i] = new Coord[maxPoints];
		ylocs[i] = new Coord[maxPoints];
	}
	maxVertLoc = minVertLoc = 0;
	plotdata = new Data(FloatData, defaultGraphWidth, maxTraces);
	plotdata->ref();
}

SimplePlot::~SimplePlot() {
	Resource::unref(plotdata);
	for(int i = 0; i < maxTraces; i++) {
		delete [] xlocs[i];
		delete [] ylocs[i];
	}
}

void
SimplePlot::load(Data *visible, EnvelopeType etype) {
	nTraces = visible->getEnvelope(plotdata, 0, etype);
}

void
SimplePlot::setPlotLength(int newlen) {
	if(newlen != plotdata->length())
		plotdata->changeLength(newlen);
}

void
SimplePlot::plot() {
	int width = graph->currentWidth();		// number of pixels in graph
	double plotGrain = max(graph->currentHGrain(), 1.0);
	int vlen = plotdata->length();
	// until Coords are floats, this factor must be figured in here
	double scalefactor = graph->vertScaleFactor();
	nPoints = 0;
	for(int i=0; i < vlen && i < maxPoints; i++) {
		int loc;
		if((loc = round(i*plotGrain)) > width)
			break;
		for(int j = 0; j < currentTraces(); j++) {
			int y = round(plotdata->get(i, j) * scalefactor);
			maxVertLoc = (y > maxVertLoc) ? y : maxVertLoc;
			minVertLoc = (y < minVertLoc) ? y : minVertLoc;
			ylocs[j][i] = y;
			xlocs[j][i] = loc;
		}
		nPoints++;
	}
}

boolean
SimplePlot::contains(Coord left, Coord bottom, Coord right, Coord top) {
	return top >= minVertLoc && bottom <= maxVertLoc;
}

void
LinePlot::draw(Canvas *cnv, Painter *out) {
	for(int i = 0; i < currentTraces(); i++)
		out->MultiLine(cnv, xlocs[i], ylocs[i], nPoints);
}

void
BarPlot::plot() {
	SimplePlot::plot();
	int horizOffset = xlocs[0][1] - xlocs[0][0];
	if(nPoints < maxPoints)	// add terminating value for last bar
		xlocs[0][nPoints] = xlocs[0][nPoints-1] + horizOffset;
}

inline boolean trueMax(int x, int y) {
	return (abs(x) > abs(y)) ? x : y;
}

inline boolean trueMin(int x, int y) {
	return (abs(x) < abs(y)) ? x : y;
}


#define sign(x) (x >= 0 ? 1: 0)

void
BarPlot::draw(Canvas *cnv, Painter *out) {
	for(int i = 0; i < nPoints; i++) {
		Coord x0 = xlocs[0][i];
		Coord x1 = xlocs[0][i+1] - 1;
		Coord ytop = ylocs[0][i];
		Coord y0, y1;
		if(currentTraces() > 1) {
			Coord ybottom = ylocs[1][i];
			y0 = trueMax(ytop, ybottom);		// larger of two, ignoring sign
			y1 = (sign((long)ytop) != sign((long)ybottom)) ?
				((y0 == ytop) ? ybottom : ytop) : 0;	// the other one, or 0
		}
		else {
			y0 = ytop;
			y1 = 0;
		}
		out->FillRect(cnv, x0, y0, x1, y1);
	}
}

void
SolidPlot::plot() {
	SimplePlot::plot();
	for(int i = 0; i < currentTraces(); i++) {
		// add the end points for the base of the polygon
		xlocs[i][nPoints] = xlocs[i][nPoints-1];
		ylocs[i][nPoints] = 0;
		xlocs[i][nPoints+1] = xlocs[i][0];
		ylocs[i][nPoints+1] = 0;
	}	
	nPoints += 2;
}

// Draws a filled polygon and then borders it on top and sides with a line.
// To avoid drawing a line aMXINT32 the base, I leave out the final base segment

void
SolidPlot::draw(Canvas *cnv, Painter *out) {
	DisplayRep* rep = graph->GetWorld()->display()->rep();
	int serverDepth = XDefaultDepth(rep->display_, rep->screen_);
	int nSegments = nPoints - 1;
//	for(int i = 0; i < ntraces; i++) {
//  since max val trace hides min val trace, only draw former -- FIX ME
	int i = (currentTraces() > 1) ? 1 : 0;
		if(serverDepth > 1) {
			ColorIntensity gr = 0.5;
			out->SetColors(new Color(gr, gr, gr), nil);		// gray
		}
		else
			out->SetPattern(new Pattern(0x4141));
		out->FillPolygon(cnv, xlocs[i], ylocs[i], nPoints);		// interior
		if(serverDepth > 1) {
			ColorIntensity bl = 0;
			out->SetColors(new Color(bl, bl, bl), nil);			// black
		}
		else
			out->SetPattern(new Pattern(Pattern::solid));
		out->MultiLine(cnv, xlocs[i], ylocs[i], nSegments);		// border
		out->Line(cnv, xlocs[i][0], ylocs[i][0],
			xlocs[i][nSegments], ylocs[i][nSegments]);			// left edge
//	}
}