/[ascend]/trunk/tcltk98/generic/interface/plot.c
ViewVC logotype

Contents of /trunk/tcltk98/generic/interface/plot.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 17 - (show annotations) (download) (as text)
Tue Dec 7 17:37:58 2004 UTC (19 years, 6 months ago) by aw0a
File MIME type: text/x-csrc
File size: 10541 byte(s)
moved interface directory one level deeper in tree
1 /*
2 * General Plot Functions
3 * Interface pointer functions for Ascend
4 * Version: $Revision: 1.4 $
5 * Version control file: $RCSfile: plot.c,v $
6 * Date last modified: $Date: 1998/02/18 21:11:53 $
7 * Last modified by: $Author: ballan $
8 * Part of Ascend
9 *
10 * This file is part of the Ascend Programming System.
11 *
12 * Copyright (C) 1990 Thomas Guthrie Epperly, Karl Michael Westerberg
13 * Copyright (C) 1994 Kirk Andre Abbott, Benjamin Andrew Allan
14 *
15 * The Ascend Programming System is free software; you can redistribute
16 * it and/or modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of the
18 * License, or (at your option) any later version.
19 *
20 * ASCEND is distributed in hope that it will be
21 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with the program; if not, write to the Free Software Foundation,
27 * Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named
28 * COPYING.
29 *
30 * This module defines the plot generation auxillaries for Ascend.
31 *
32 */
33
34 #include "utilities/ascConfig.h"
35 #include "general/list.h"
36 #include "compiler/instance_enum.h"
37 #include "compiler/fractions.h"
38 #include "compiler/compiler.h"
39 #include "compiler/symtab.h"
40 #include "compiler/dimen.h"
41 #include "compiler/atomvalue.h"
42 #include "compiler/instance_name.h"
43 #include "compiler/parentchild.h"
44 #include "compiler/instquery.h"
45 #include "compiler/child.h"
46 #include "compiler/type_desc.h"
47 #include "compiler/module.h"
48 #include "compiler/library.h"
49 #include "interface/plot.h"
50
51 /*
52 * ASSUMPTION ASSUMPTION ASSUMPTION:
53 * The points are to be displayed in the order that ASCEND
54 * internally sorts arrays, i.e. increasing integer subscript value.
55 *
56 * By making these assumptions, we remove the strict requirement
57 * that the curves have points indexed 1..n. We might, after all,
58 * want to index from 0.
59 */
60 enum PlotTypes g_plot_type;
61
62 static symchar *g_strings[16];
63 #define PLOT_POINT_SYM g_strings[0]
64 #define PLOT_POINT_INT g_strings[1]
65 #define PLOT_TITLE g_strings[2]
66 #define PLOT_XLABEL g_strings[3]
67 #define PLOT_YLABEL g_strings[4]
68 #define PLOT_XLOG g_strings[5]
69 #define PLOT_YLOG g_strings[6]
70 #define PLOT_XLO g_strings[7]
71 #define PLOT_YLO g_strings[8]
72 #define PLOT_XHI g_strings[9]
73 #define PLOT_YHI g_strings[10]
74 #define PLOT_CURVE g_strings[11]
75 #define PLOT_LEGEND g_strings[12]
76 #define PLOT_POINT g_strings[13]
77 #define PLOT_XPOINT g_strings[14]
78 #define PLOT_YPOINT g_strings[15]
79
80 static void init_gstrings(void)
81 {
82 PLOT_POINT_SYM = AddSymbol("plt_plot_symbol");
83 PLOT_POINT_INT = AddSymbol("plt_plot_integer");
84 PLOT_TITLE = AddSymbolL("title",5);
85 PLOT_XLABEL = AddSymbolL("XLabel",6);
86 PLOT_YLABEL = AddSymbolL("YLabel",6);
87 PLOT_XLOG = AddSymbolL("Xlog",4);
88 PLOT_YLOG = AddSymbolL("Ylog",4);
89 PLOT_XLO = AddSymbolL("Xlow",4);
90 PLOT_YLO = AddSymbolL("Ylow",4);
91 PLOT_XHI = AddSymbolL("Xhigh",5);
92 PLOT_YHI = AddSymbolL("Yhigh",5);
93 PLOT_CURVE = AddSymbolL("curve",5);
94 PLOT_LEGEND = AddSymbolL("legend",6);
95 PLOT_POINT = AddSymbolL("pnt",3);
96 PLOT_XPOINT = AddSymbolL("x",1);
97 PLOT_YPOINT = AddSymbolL("y",1);
98 }
99
100 /*
101 * returns TRUE if the type of inst is more refined than the type of
102 * plt_point_int or plt_point_sym
103 */
104 boolean plot_allowed(struct Instance *inst) {
105 struct TypeDescription *type, *plt_type_s, *plt_type_i;
106 if (inst==NULL) {
107 return 0;
108 }
109 init_gstrings(); /* set up symchars for the child queries */
110 plt_type_s = FindType(PLOT_POINT_SYM); /* look for plt_point_sym */
111 plt_type_i = FindType(PLOT_POINT_INT); /* look for plt_point_int */
112 if (plt_type_i == NULL && plt_type_s == NULL) {
113 return 0; /* no plots => fail */
114 }
115 type = InstanceTypeDesc(inst);
116 if (type==NULL) {
117 return 0; /* atom children have not type */
118 }
119 /* type is more refined than symbol plot */
120 if ((plt_type_s && type == MoreRefined(type,plt_type_s))||
121 (plt_type_i && type == MoreRefined(type,plt_type_i))) {
122 return 1;
123 }
124 return 0;
125 }
126
127
128 /*
129 * A very similar function such as the one below is defined in BrowserProc.c.
130 * We need to do some reorganization. kaa.
131 */
132 static unsigned long ChildNumberbyChar(struct Instance *i, symchar *name)
133 {
134 struct InstanceName rec;
135 unsigned long c = 0;
136 unsigned long nch = 0;
137 long index;
138
139 if(i==NULL||name==NULL) {
140 FPRINTF(stderr,"Null Instance or name in ChildbyNameChar\n");
141 return 0;
142 }
143 assert(AscFindSymbol(name)!=NULL);
144 nch = NumberChildren(i);
145 if(!nch) return 0;
146 do {
147 c++;
148 rec = ChildName(i,c);
149 switch (InstanceNameType(rec)){
150 case StrName:
151 if (InstanceNameStr(rec) == name) {
152 return c;
153 }
154 break;
155 case IntArrayIndex:
156 index = atol(SCP(name));
157 if (index==InstanceIntIndex(rec)) {
158 return c;
159 }
160 break;
161 case StrArrayIndex:
162 if (InstanceStrIndex(rec) == name) {
163 return c;
164 }
165 break;
166 }
167 } while(c < nch);
168 return 0; /*NOTREACHED*/
169 }
170
171 static
172 void do_plot_legends(FILE *fp, struct Instance *i, symchar *label)
173 {
174 struct Instance *str_inst;
175 unsigned long ndx;
176 ndx = ChildNumberbyChar(i,label);
177 if (ndx) {
178 str_inst = InstanceChild(i,ndx);
179 if (AtomAssigned(str_inst)) {
180 switch(g_plot_type) {
181 case PLAIN_PLOT:
182 FPRINTF(fp,"\n\n");
183 break;
184 case GNU_PLOT:
185 FPRINTF(fp,"\n#\"%s\"\n",SCP(GetSymbolAtomValue(str_inst)));
186 break;
187 case XGRAPH_PLOT:
188 FPRINTF(fp,"\n\"%s\n",SCP(GetSymbolAtomValue(str_inst)));
189 break;
190 default:
191 break;
192 }
193 } else {
194 FPRINTF(fp,"\n\n");
195 }
196 }
197 }
198
199 static
200 void do_plot_labels(FILE *fp, struct Instance *i, symchar *label,
201 char *labelstring)
202 {
203 struct Instance *inst;
204 unsigned long ndx;
205 assert(AscFindSymbol(label)!=NULL);
206 ndx = ChildNumberbyChar(i,label);
207 if (ndx) {
208 inst = InstanceChild(i,ndx);
209 if (AtomAssigned(inst)) {
210 switch (InstanceKind(inst)) {
211 case REAL_INST:
212 case REAL_ATOM_INST:
213 case REAL_CONSTANT_INST:
214 FPRINTF(fp,"%s %.18g\n",labelstring, RealAtomValue(inst));
215 break;
216 case INTEGER_INST:
217 case INTEGER_ATOM_INST:
218 case INTEGER_CONSTANT_INST:
219 FPRINTF(fp,"%s %ld\n",labelstring, GetIntegerAtomValue(inst));
220 break;
221 case SYMBOL_INST:
222 case SYMBOL_ATOM_INST:
223 case SYMBOL_CONSTANT_INST:
224 FPRINTF(fp,"%s %s\n",labelstring, SCP(GetSymbolAtomValue(inst)));
225 break;
226 case BOOLEAN_INST:
227 case BOOLEAN_ATOM_INST:
228 case BOOLEAN_CONSTANT_INST:
229 FPRINTF(fp,"%s %d\n",labelstring,
230 (GetBooleanAtomValue(inst)!=0)?1:0);
231 break;
232 default:
233 break;
234 }
235 }
236 }
237 }
238
239 static void write_point(FILE *fp, struct Instance *point, boolean drawline)
240 {
241 /* ? draw line from previous point
242 * Writes a given point (instance of plt_point) to fp
243 * if values of point are well defined.
244 */
245 unsigned long ndx;
246 struct Instance *ix, *iy;
247 double x,y;
248
249 ndx = ChildNumberbyChar(point,PLOT_XPOINT); /* do x */
250 ix = InstanceChild(point,ndx);
251 ndx = ChildNumberbyChar(point,PLOT_YPOINT); /* do y */
252 iy = InstanceChild(point,ndx);
253
254 if (ix != NULL && iy != NULL && AtomAssigned(ix) && AtomAssigned(iy)) {
255 x = RealAtomValue(ix);
256 y = RealAtomValue(iy);
257 FPRINTF(fp,drawline ? "%g %g\n" : "move %g %g\n",x,y);
258 }
259 }
260
261 static void write_curve(FILE *fp,
262 struct Instance *curve,
263 unsigned long curve_number)
264 {
265 /*
266 * Writes a given curve to the given file with descriptor fp.
267 */
268
269 struct Instance *point_array_inst, *a_point;
270 unsigned long ndx;
271 unsigned long npoints;
272 unsigned long c;
273
274 (void)curve_number; /* stopping gcc whine about unused parameter */
275
276 /* do plot legend */
277 do_plot_legends(fp,curve,PLOT_LEGEND);
278
279 ndx = ChildNumberbyChar(curve,PLOT_POINT);
280 if (ndx) {
281 point_array_inst = InstanceChild(curve,ndx);
282 npoints = NumberChildren(point_array_inst);
283 for (c=1;c<=npoints;c++) {
284 a_point = InstanceChild(point_array_inst,c);
285 write_point(fp,a_point,1);
286 }
287 }
288 }
289
290 void plot_prepare_file(struct Instance *plot_inst, char *plotfilename)
291 {
292 struct Instance *curve_array_inst, *a_curve;
293 unsigned long ndx;
294 unsigned long ncurves;
295 unsigned long c;
296 enum inst_t kind;
297 FILE *fp;
298
299 if (plot_inst==NULL) {
300 return;
301 }
302 fp=fopen(plotfilename,"w");
303 if( fp == NULL ) {
304 FPRINTF(stderr,"ERROR: plot_prepare_file: Cannot open %s for writing.\n",
305 plotfilename);
306 return;
307 }
308 init_gstrings(); /* set up symchars for the child queries */
309 /* do the plot labels */
310 switch(g_plot_type) {
311 case PLAIN_PLOT:
312 break;
313 case GNU_PLOT:
314 FPRINTF(fp,"#Plot data for gnuplot generated by ASCEND\n");
315 FPRINTF(fp,"#Remove # marks for legends etc.\n\n");
316 do_plot_labels(fp,plot_inst,PLOT_TITLE, "#set title ");
317 do_plot_labels(fp,plot_inst,PLOT_XLABEL, "#set xlabel ");
318 do_plot_labels(fp,plot_inst,PLOT_YLABEL, "#set ylabel ");
319 break;
320 case XGRAPH_PLOT:
321 FPRINTF(fp,"#Plot data for Xgraph generated by ASCEND\n\n");
322 do_plot_labels(fp,plot_inst,PLOT_TITLE, "TitleText: ");
323 do_plot_labels(fp,plot_inst,PLOT_XLABEL, "XUnitText: ");
324 do_plot_labels(fp,plot_inst,PLOT_YLABEL, "YUnitText: ");
325 do_plot_labels(fp,plot_inst,PLOT_YLOG, "LogY: ");
326 do_plot_labels(fp,plot_inst,PLOT_XLOG, "LogX: ");
327 do_plot_labels(fp,plot_inst,PLOT_YLO, "YLowLimit: ");
328 do_plot_labels(fp,plot_inst,PLOT_XLO, "XLowLimit: ");
329 do_plot_labels(fp,plot_inst,PLOT_YHI, "YHighLimit: ");
330 do_plot_labels(fp,plot_inst,PLOT_XHI, "XHighLimit: ");
331 break;
332 default:
333 break;
334 }
335
336 ndx = ChildNumberbyChar(plot_inst,PLOT_CURVE); /*search for curve_array*/
337 if (ndx) {
338 curve_array_inst = InstanceChild(plot_inst,ndx);
339 kind = InstanceKind(curve_array_inst);
340 ncurves = NumberChildren(curve_array_inst); /*get no. of curves*/
341 for (c=1;c<=ncurves;c++) { /*get true curve number*/
342 if (kind==ARRAY_INT_INST || kind==ARRAY_ENUM_INST) {
343 a_curve = InstanceChild(curve_array_inst,c);
344 write_curve(fp,a_curve,c);
345 }
346 }
347 }
348 fclose(fp);
349 }

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22