Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

open-axiom repository from github

24005 views
1
/*
2
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
3
All rights reserved.
4
Copyright (C) 2007-2008, Gabriel Dos Reis.
5
All rights reserved.
6
7
Redistribution and use in source and binary forms, with or without
8
modification, are permitted provided that the following conditions are
9
met:
10
11
- Redistributions of source code must retain the above copyright
12
notice, this list of conditions and the following disclaimer.
13
14
- Redistributions in binary form must reproduce the above copyright
15
notice, this list of conditions and the following disclaimer in
16
the documentation and/or other materials provided with the
17
distribution.
18
19
- Neither the name of The Numerical Algorithms Group Ltd. nor the
20
names of its contributors may be used to endorse or promote products
21
derived from this software without specific prior written permission.
22
23
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
27
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
*/
35
36
#define _SPOON2D_C
37
38
#include <unistd.h>
39
#include <stdlib.h>
40
#include <string.h>
41
42
#include "open-axiom.h"
43
#include "viewAlone.h"
44
45
46
#include "util.H1"
47
#include "cfuns.h"
48
#include "all_alone.H1"
49
50
using namespace OpenAxiom;
51
52
/* #define huhDEBUG */
53
54
/* #define spoonDEBUG */
55
56
void
57
spoonView2D(void)
58
{
59
60
int i,code,pipe0[2],pipe1[2],there;
61
char envAXIOM[100],runView[100];
62
63
sprintf(errorStr,"%s","creating pipes");
64
check(pipe(pipe0));
65
check(pipe(pipe1));
66
switch(fork()) {
67
68
case -1:
69
fprintf(stderr,
70
"Cannot create a new process - probably have too many things running already.\n");
71
exit(-1);
72
73
case 0:
74
/************
75
* Child *
76
************/
77
printf("(spoon2D child) mapping of pipes to standard I/O for view2D\n");
78
sprintf(errorStr,"%s","(viewAlone) mapping of pipes to standard I/O for view2D");
79
check(dup2(pipe0[0],0));
80
check(dup2(pipe1[1],1));
81
close(pipe0[0]);
82
close(pipe0[1]);
83
close(pipe1[0]);
84
close(pipe1[1]);
85
printf("(spoon2D child) start the TwoDimensionalViewport process\n");
86
sprintf(errorStr,"%s","(viewAlone) execution of the TwoDimensionalViewport process");
87
sprintf(envAXIOM,"%s",oa_getenv("AXIOM"));
88
sprintf(runView,"%s%s",envAXIOM,"/lib/view2D");
89
check(execl(runView,runView, (char*)NULL));
90
fprintf(stderr,"Could not execute view2D! Check that view2D is on your path variable.\n");
91
exit(-1);
92
93
default:
94
/*************
95
* Parent *
96
*************/
97
98
viewP.viewType = view2DType;
99
100
/* set up pipes to child process */
101
close(pipe0[0]);
102
close(pipe1[1]);
103
viewP.viewIn = pipe1[0];
104
viewP.viewOut = pipe0[1];
105
printf("(spoon2D parent) pipes created\n");
106
if (viewP.viewIn <0) {
107
fprintf(stderr," Could not connect from Viewport manager to viewport process. Try again.\n");
108
return;
109
} else {
110
code = read(viewP.viewIn,&ack,intSize);
111
if (code < 0) {
112
fprintf(stderr," Could not connect from Viewport manager to viewport process. Try again.\n");
113
return;
114
}
115
}
116
printf("(spoon2D parent) making View2D data\n");
117
makeView2DFromFileData(&doView2D);
118
119
/* tell child it is to be a stand alone program */
120
i = yes;
121
fprintf(stderr," Transmitting data to viewport...\n");
122
write(viewP.viewOut,&i,intSize);
123
124
write(viewP.viewOut,&doView2D,sizeof(view2DStruct));
125
i = strlen(doView2D.title)+1;
126
write(viewP.viewOut,&i,intSize); /* tell the length of the title to child */
127
write(viewP.viewOut,doView2D.title,i); /* tell the title to the child, child */
128
for (i=0; i<maxGraphs; i++) {
129
there = graphArray[i].key;
130
write(viewP.viewOut,&there,intSize);
131
sendGraphToView2D(i,there,&viewP);
132
}; /* for i in graphs */
133
134
fprintf(stderr," Done.\n");
135
136
/*** get acknowledge from viewport */
137
138
code = read(viewP.viewIn,&(viewP.viewWindow),sizeof(Window));
139
openaxiom_sleep(1); /* wait a second...*/
140
exit(0);
141
142
} /* switch */
143
144
} /* forkView2D() */
145
146
147
148
/*void sendGraphToView2D(i,there,viewP) */
149
void
150
sendGraphToView2D(int i,int there,viewManager *viewP)
151
{
152
graphStruct *gPtr;
153
pointListStruct *llPtr;
154
pointStruct *p;
155
int j,k;
156
printf("(spoon2D sendGraphToView2D) i=%d there=%d\n",i,there);
157
if (there) {
158
159
gPtr = &(graphArray[i]);
160
printf("(spoon2D sendGraphToView2D) graph %d is there\n",i);
161
write(viewP->viewOut,&(gPtr->xmin),floatSize);
162
write(viewP->viewOut,&(gPtr->xmax),floatSize);
163
write(viewP->viewOut,&(gPtr->ymin),floatSize);
164
write(viewP->viewOut,&(gPtr->ymax),floatSize);
165
write(viewP->viewOut,&(gPtr->xNorm),floatSize);
166
write(viewP->viewOut,&(gPtr->yNorm),floatSize);
167
write(viewP->viewOut,&(gPtr->spadUnitX),floatSize);
168
write(viewP->viewOut,&(gPtr->spadUnitY),floatSize);
169
write(viewP->viewOut,&(gPtr->unitX),floatSize);
170
write(viewP->viewOut,&(gPtr->unitY),floatSize);
171
write(viewP->viewOut,&(gPtr->originX),floatSize);
172
write(viewP->viewOut,&(gPtr->originY),floatSize);
173
write(viewP->viewOut,&(gPtr->numberOfLists),intSize);
174
175
llPtr = gPtr->listOfListsOfPoints;
176
for (j=0; j<(gPtr->numberOfLists); j++) {
177
write(viewP->viewOut,&(llPtr->numberOfPoints),intSize);
178
p = llPtr->listOfPoints;
179
for (k=0; k<(llPtr->numberOfPoints); k++) {
180
write(viewP->viewOut,&(p->x),floatSize);
181
write(viewP->viewOut,&(p->y),floatSize);
182
write(viewP->viewOut,&(p->hue),floatSize);
183
write(viewP->viewOut,&(p->shade),floatSize);
184
p++;
185
} /* for k in list of points */
186
write(viewP->viewOut,&(llPtr->pointColor),intSize);
187
write(viewP->viewOut,&(llPtr->lineColor),intSize);
188
write(viewP->viewOut,&(llPtr->pointSize),intSize);
189
llPtr++;
190
} /* for j in list of lists of points */
191
192
/* a state is defined for a graph if it is there */
193
write(viewP->viewOut,&(graphStateArray[i].scaleX),floatSize);
194
write(viewP->viewOut,&(graphStateArray[i].scaleY),floatSize);
195
write(viewP->viewOut,&(graphStateArray[i].deltaX),floatSize);
196
write(viewP->viewOut,&(graphStateArray[i].deltaY),floatSize);
197
write(viewP->viewOut,&(graphStateArray[i].pointsOn),intSize);
198
write(viewP->viewOut,&(graphStateArray[i].connectOn),intSize);
199
write(viewP->viewOut,&(graphStateArray[i].splineOn),intSize);
200
write(viewP->viewOut,&(graphStateArray[i].axesOn),intSize);
201
write(viewP->viewOut,&(graphStateArray[i].axesColor),intSize);
202
write(viewP->viewOut,&(graphStateArray[i].unitsOn),intSize);
203
write(viewP->viewOut,&(graphStateArray[i].unitsColor),intSize);
204
write(viewP->viewOut,&(graphStateArray[i].showing),intSize);
205
206
} /* if graph is there */
207
208
}
209
210
211
void
212
makeView2DFromFileData(view2DStruct *doView2D)
213
{
214
215
int i,j,k;
216
char title[256];
217
FILE *graphFile;
218
char graphFilename[256];
219
pointListStruct *aList;
220
pointStruct *aPoint;
221
printf("(spoon2D makeView2DFromFileData)\n");
222
fgets(title,256,viewFile);
223
printf("(spoon2D) title=%s\n",title);
224
if (!(doView2D->title =
225
(char *)malloc((strlen(title)+1) * sizeof(char)))) {
226
fprintf(stderr,
227
"Ran out of memory (malloc) trying to get the title.\n");
228
exit(-1);
229
}
230
sprintf(doView2D->title,"%s",title);
231
/* put in a null terminator over the newline that the fgets reads */
232
doView2D->title[strlen(doView2D->title)-1] = '\0';
233
fscanf(viewFile,"%d %d %d %d\n",
234
&(doView2D->vX),
235
&(doView2D->vY),
236
&(doView2D->vW),
237
&(doView2D->vH));
238
printf("(spoon2D) X=%d Y=%d W=%d H=%d \n",
239
doView2D->vX,doView2D->vY,doView2D->vW,doView2D->vH);
240
for (i=0; i<maxGraphs; i++) {
241
fscanf(viewFile,"%d\n",
242
&(graphArray[i].key));
243
printf("(spoon2D) i=%d key=%d\n",
244
i,graphArray[i].key);
245
fscanf(viewFile,"%g %g\n",
246
&(graphStateArray[i].scaleX),
247
&(graphStateArray[i].scaleY));
248
printf("(spoon2D) scaleX=%g scaleY=%g\n",
249
graphStateArray[i].scaleX,graphStateArray[i].scaleY);
250
fscanf(viewFile,"%g %g\n",
251
&(graphStateArray[i].deltaX),
252
&(graphStateArray[i].deltaY));
253
printf("(spoon2D) deltaX=%g deltaY=%g\n",
254
graphStateArray[i].deltaX,graphStateArray[i].deltaY);
255
fscanf(viewFile,"%g %g\n",
256
&(graphStateArray[i].centerX),
257
&(graphStateArray[i].centerY));
258
printf("(spoon2D) centerX=%g centerY=%g\n",
259
graphStateArray[i].centerX,graphStateArray[i].centerY);
260
fscanf(viewFile,"%d %d %d %d %d %d %d\n",
261
&(graphStateArray[i].pointsOn),
262
&(graphStateArray[i].connectOn),
263
&(graphStateArray[i].splineOn),
264
&(graphStateArray[i].axesOn),
265
&(graphStateArray[i].axesColor),
266
&(graphStateArray[i].unitsOn),
267
&(graphStateArray[i].unitsColor));
268
printf("(spoon2D) pointsOn=%d connectOn=%d splineOn=%d axesOn=%d axesColor=%d unitsOn=%d unitsColor=%d\n",
269
graphStateArray[i].pointsOn,graphStateArray[i].connectOn,
270
graphStateArray[i].splineOn,graphStateArray[i].axesOn,
271
graphStateArray[i].axesColor,graphStateArray[i].unitsOn,
272
graphStateArray[i].unitsColor);
273
fscanf(viewFile,"%d %d\n",
274
&(graphStateArray[i].showing),
275
&(graphStateArray[i].selected));
276
printf("(spoon2D) showing=%d selected=%d\n",
277
graphStateArray[i].showing,graphStateArray[i].selected);
278
}
279
fclose(viewFile);
280
for (i=0; i<maxGraphs; i++) {
281
if (graphArray[i].key) {
282
/** OPEN FILE FOR GRAPHS **/
283
sprintf(graphFilename,"%s%s%d",pathname,"/graph",i);
284
if ((graphFile = fopen(graphFilename,"r")) == NULL) {
285
fprintf(stderr," Error: Cannot find the file %s\n",graphFilename);
286
perror("fopen");
287
return;
288
} else {
289
printf("(spoon2d) \n\nGRAPH%i\n",i);
290
fscanf(graphFile,"%g %g %g %g\n",
291
&(graphArray[i].xmin),
292
&(graphArray[i].ymin),
293
&(graphArray[i].xmax),
294
&(graphArray[i].ymax));
295
printf("(spoon2d) xmin=%g ymin=%g xmax=%g ymax=%g\n",
296
graphArray[i].xmin,graphArray[i].ymin,
297
graphArray[i].xmax,graphArray[i].ymax);
298
fscanf(graphFile,"%g %g\n",
299
&(graphArray[i].xNorm),
300
&(graphArray[i].yNorm));
301
printf("(spoon2d) xNorm=%g yNorm=%g\n",
302
graphArray[i].xNorm,graphArray[i].yNorm);
303
fscanf(graphFile,"%g %g\n",
304
&(graphArray[i].originX),
305
&(graphArray[i].originY));
306
printf("(spoon2d) originX=%g originY=%g\n",
307
graphArray[i].originX,graphArray[i].originY);
308
fscanf(graphFile,"%g %g\n",
309
&(graphArray[i].spadUnitX),
310
&(graphArray[i].spadUnitY));
311
printf("(spoon2d) spadUnitX=%g spadUnitY=%g\n",
312
graphArray[i].spadUnitX,graphArray[i].spadUnitY);
313
fscanf(graphFile,"%g %g\n",
314
&(graphArray[i].unitX),
315
&(graphArray[i].unitY));
316
printf("(spoon2d) unitX=%g unitY=%g\n",
317
graphArray[i].unitX,graphArray[i].unitY);
318
fscanf(graphFile,"%d\n",
319
&(graphArray[i].numberOfLists));
320
printf("(spoon2d) numberOfLists=%d\n",
321
graphArray[i].numberOfLists);
322
if (!(aList =
323
(pointListStruct *)malloc(graphArray[i].numberOfLists *
324
sizeof(pointListStruct)))) {
325
fprintf(stderr,"viewAlone: Fatal Error>> Out of memory trying to receive a graph.\n");
326
exit(-1);
327
}
328
graphArray[i].listOfListsOfPoints = aList;
329
for (j=0;
330
j<graphArray[i].numberOfLists;
331
j++, aList++) {
332
printf("(spoon2d) list number %d\n",j);
333
fscanf(graphFile,"%d\n",&(aList->numberOfPoints));
334
printf("(spoon2d) number of points %d\n",
335
aList->numberOfPoints);
336
fscanf(graphFile,"%d %d %d\n",
337
&(aList->pointColor),
338
&(aList->lineColor),
339
&(aList->pointSize));
340
printf("(spoon2d) pointColor=%d lineColor=%d pointSize=%d\n",
341
aList->pointColor,aList->lineColor,aList->pointSize);
342
if (!(aPoint = (pointStruct *)malloc(aList->numberOfPoints *
343
sizeof(pointStruct)))) {
344
fprintf(stderr,"viewAlone: Fatal Error>> Out of memory trying to receive a graph.\n");
345
exit(-1);
346
}
347
aList->listOfPoints = aPoint; /** point to current point list **/
348
for (k=0;
349
k<aList->numberOfPoints;
350
k++,aPoint++)
351
{ fscanf(graphFile,"%g %g %g %g\n",
352
&(aPoint->x),
353
&(aPoint->y),
354
&(aPoint->hue),
355
&(aPoint->shade));
356
printf("(spoon2d)k=%d x=%g y=%g hue=%g shade=%g\n",
357
k,aPoint->x,aPoint->y,aPoint->hue,aPoint->shade);
358
}
359
} /* for j, aList */
360
fclose(graphFile);
361
} /* else, opened up a file */
362
} /* if graph.key */
363
} /* for i */
364
} /* makeView2DFromFileData */
365
366