File: misc.c

package info (click to toggle)
plotmtv 1.4.1-4
  • links: PTS
  • area: main
  • in suites: slink
  • size: 4,024 kB
  • ctags: 5,006
  • sloc: ansic: 51,179; makefile: 1,976; fortran: 1,277; sh: 510; csh: 439
file content (440 lines) | stat: -rw-r--r-- 12,098 bytes parent folder | download | duplicates (6)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
 * misc.c - miscellaneous useful utilities 
 */

#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "CNplot.h"

static void swap();

static int plotset_has_filledcontour();
static int plotset_has_grid();
static int plotset_has_mesh4Dquant();

/*
 * Find out if there is a color contour dataset in the plotset
 */
int CNplotset_has_colored_contours(Pptr)
CNplotsetptr Pptr;
{
   int FOUND = CN_FALSE;

   /* Error Checking */
   if (Pptr == NULL) {
      (void) fprintf(stderr,
             "CNplotset_has_colored_contours() : Error! Null plotset!\n");
      return(0);
   }

   FOUND = plotset_has_filledcontour(Pptr) ||
           plotset_has_grid(Pptr) ||
           plotset_has_mesh4Dquant(Pptr);

   /* Return status */
   return(FOUND);
}

/*
 * Find out if there is a filled contour dataset in the plotset
 */
static int plotset_has_filledcontour(Pptr)
CNplotsetptr Pptr;
{
   CNdslistptr DS;
   int FOUND = CN_FALSE;

   /* Error Checking */
   if (Pptr == NULL) {
      (void) fprintf(stderr,
                     "CNplotset_has_filledcontour() : Error! Null plotset!\n");
      return(0);
   }

   /* Go thru the list */
   for (DS=Pptr->datahead; DS!=NULL && !FOUND; DS=DS->next)
      if ( ((DS->Dptr->datatype == CN_CONTOUR) || 
            (DS->Dptr->datatype == CN_POLYGON)) && 
           (DS->Dptr->data_pr.contstyle == CN_FILLCONT))
         FOUND = CN_TRUE;

   /* Return status */
   return(FOUND);
}

/*
 * Find out if there is a grid dataset in the plotset
 */
static int plotset_has_grid(Pptr)
CNplotsetptr Pptr;
{
   CNdslistptr DS;
   int FOUND = CN_FALSE;

   /* Error Checking */
   if (Pptr == NULL) {
      (void) fprintf(stderr,
                     "CNplotset_has_grid() : Error! Null plotset!\n");
      return(0);
   }

   /* Go thru the list */
   for (DS=Pptr->datahead; DS!=NULL && !FOUND; DS=DS->next)
      if (DS->Dptr->grid && DS->Dptr->datatype==CN_GRID4D) FOUND = CN_TRUE;

   /* Return status */
   return(FOUND);
}

/*
 * Find out if there is a quant-mesh4D dataset in the plotset
 */
static int plotset_has_mesh4Dquant(Pptr)
CNplotsetptr Pptr;
{
   CNdslistptr DS;
   int FOUND = CN_FALSE;

   /* Error Checking */
   if (Pptr == NULL) {
      (void) fprintf(stderr,
                     "CNplotset_has_mesh4Dquant() : Error! Null plotset!\n");
      return(0);
   }

   /* Go thru the list */
   for (DS=Pptr->datahead; DS!=NULL && !FOUND; DS=DS->next)
      if (DS->Dptr->mesh4D && DS->Dptr->datatype==CN_MESH4D_C) FOUND = CN_TRUE;

   /* Return status */
   return(FOUND);
}

/*
 * Find out if there is a line-label dataset in the plotset
 */
int CNplotset_has_linelabels(Pptr)
CNplotsetptr Pptr;
{
   CNdslistptr DS;
   CNcurveptr  C;
   int FOUND = CN_FALSE;

   /* Error Checking */
   if (Pptr == NULL) {
      (void) fprintf(stderr,
                     "CNplotset_has_linelabels() : Error! Null plotset!\n");
      return(0);
   }

   /* Go thru the list */
   for (DS=Pptr->datahead; DS!=NULL && !FOUND; DS=DS->next) {

      /* Plot only the 2D/3D plot labels */
      if ((DS->Dptr->datatype==CN_PLOT2D) ||
          (DS->Dptr->datatype==CN_PROBAB) ||
          (DS->Dptr->datatype==CN_BARCHART) ||
          (DS->Dptr->datatype==CN_PLOT3D)){

        /* Curve set */
        for (C=DS->Dptr->curvehead; C!=NULL && !FOUND; C=C->next) {
          if((C->curv_pr.linelabel!=NULL)&&CNstrlen(C->curv_pr.linelabel)!=0)
          FOUND = CN_TRUE;
        }
      }
   }

   /* Return status */
   return(FOUND);
}


/*
 * Find out if there is a contour in the data list
 */
int CNplotset_has_contour(Pptr)
CNplotsetptr Pptr;
{
   CNdslistptr DS;
   int FOUND = CN_FALSE;

   /* Error Checking */
   if (Pptr == NULL) {
      (void) fprintf(stderr,"CNplotset_has_contour() : Error! Null plotset!\n");
      return(0);
   }

   /* Go thru the list */
   for (DS=Pptr->datahead; DS!=NULL && !FOUND; DS=DS->next)
      if (DS->Dptr->datatype == CN_CONTOUR)
         FOUND = CN_TRUE;

   /* Return status */
   return(FOUND);
}


/*
 * Go through the data sets in the plotset and find the
 * largest boundary that will fit around all the datasets
 */
void
CNset_plotset_boundaries(Pptr, xmin, xmax, ymin, ymax, zmin, zmax)
CNplotsetptr Pptr;
double       *xmin, *xmax, *ymin, *ymax, *zmin, *zmax;
{
   CNdslistptr D;

   /* check to see if there is any data */
   if (Pptr == NULL) {
      (void) fprintf(stderr,"   ***Error! No data in the plotset\n");
      return;  
   }

   /* Initialize the boundary data */
   *xmin =  CN_LARGE; 
   *xmax = -CN_LARGE;
   *ymin =  CN_LARGE;
   *ymax = -CN_LARGE;
   *zmin =  CN_LARGE;
   *zmax = -CN_LARGE;

   /* Go thru the data sets and compare boundaries */
   for (D=Pptr->datahead; D!=NULL; D=D->next) {
      if (D->Dptr->bxmin < *xmin) *xmin = D->Dptr->bxmin;
      if (D->Dptr->bxmax > *xmax) *xmax = D->Dptr->bxmax;
      if (D->Dptr->bymin < *ymin) *ymin = D->Dptr->bymin;
      if (D->Dptr->bymax > *ymax) *ymax = D->Dptr->bymax;
      if (D->Dptr->datatype != CN_PLOT2D) {
         if (D->Dptr->bzmin < *zmin) *zmin = D->Dptr->bzmin;
         if (D->Dptr->bzmax > *zmax) *zmax = D->Dptr->bzmax;
      }
   }
   if (*zmin > *zmax) {
      *zmin = Pptr->datahead->Dptr->bzmin;
      *zmax = Pptr->datahead->Dptr->bzmax;
   }
}


/*
 * Go through the data sets in the plotset and find the
 * largest viewport that will fit around all the datasets
 */
void
CNset_plotset_viewport(Pptr, xmin, xmax, ymin, ymax, zmin, zmax)
CNplotsetptr Pptr;
double       *xmin, *xmax, *ymin, *ymax, *zmin, *zmax;
{
   CNdslistptr D;
   CNdatasetptr DP;

   /* check to see if there is any data */
   if (Pptr == NULL) {
      (void) fprintf(stderr,"   ***Error! No data in the plotset\n");
      return;  
   }

   /* Initialize the viewport data */
   *xmin = Pptr->datahead->Dptr->plot_pr.vxmin;
   *xmax = Pptr->datahead->Dptr->plot_pr.vxmax;
   *ymin = Pptr->datahead->Dptr->plot_pr.vymin;
   *ymax = Pptr->datahead->Dptr->plot_pr.vymax;
   *zmin = Pptr->datahead->Dptr->plot_pr.vzmin;
   *zmax = Pptr->datahead->Dptr->plot_pr.vzmax;

   /* Go thru the data sets and compare boundaries */
   for (D=Pptr->datahead->next; D!=NULL; D=D->next) {
      DP = D->Dptr;
      if (DP->plot_pr.vxmin < *xmin) *xmin = DP->plot_pr.vxmin;
      if (DP->plot_pr.vxmax > *xmax) *xmax = DP->plot_pr.vxmax;
      if (DP->plot_pr.vymin < *ymin) *ymin = DP->plot_pr.vymin;
      if (DP->plot_pr.vymax > *ymax) *ymax = DP->plot_pr.vymax;
      if (DP->plot_pr.vzmin < *zmin) *zmin = DP->plot_pr.vzmin;
      if (DP->plot_pr.vzmax > *zmax) *zmax = DP->plot_pr.vzmax;
   }
}

/*
 * Reset the viewport for the plotset
 */
void CNreset_plotset_viewport(WP)
CNplotsetptr WP;
{
   double xmin, xmax, ymin, ymax, zmin, zmax;

   /* Error Checking */
   if (WP==NULL) {
      (void) fprintf(stderr,
                     "CNreset_plotset_viewport() : Error - Null plotset!\n");
      return;
   }

   /*
    * Reset the plot boundaries
    * The values returned are the bounding-box values of all the
    * datasets contained within the plotset
    */
   CNset_plotset_viewport(WP, &xmin, &xmax, &ymin, &ymax, &zmin, &zmax);
   if ((WP->plot_pr.flag1 & CNvxmin) == 0) WP->plot_pr.vxmin = xmin;
   if ((WP->plot_pr.flag1 & CNvxmax) == 0) WP->plot_pr.vxmax = xmax;
   if ((WP->plot_pr.flag1 & CNvymin) == 0) WP->plot_pr.vymin = ymin;
   if ((WP->plot_pr.flag1 & CNvymax) == 0) WP->plot_pr.vymax = ymax;
   if ((WP->plot_pr.flag1 & CNvzmin) == 0) WP->plot_pr.vzmin = zmin;
   if ((WP->plot_pr.flag1 & CNvzmax) == 0) WP->plot_pr.vzmax = zmax;

   /* Save the current viewport */
   WP->plot_pr.prev_vxmin = WP->plot_pr.vxmin;
   WP->plot_pr.prev_vxmax = WP->plot_pr.vxmax;
   WP->plot_pr.prev_vymin = WP->plot_pr.vymin;
   WP->plot_pr.prev_vymax = WP->plot_pr.vymax;
   WP->plot_pr.prev_vzmin = WP->plot_pr.vzmin;
   WP->plot_pr.prev_vzmax = WP->plot_pr.vzmax;

   /* Reset the 3D window */
   CNreinitialize_view(WP->view_pr,xmin,xmax,ymin,ymax,zmin,zmax);
}

/*
 * Make a copy of a dataset
 */
CNdatasetptr CNcopy_dataset(Dptr,dataID)
CNdatasetptr Dptr;
int          *dataID;
{
   CNdatasetptr Dnew=NULL;
   CNcurveptr   C, Cnew;
   CNpointptr   P;

   /* Error Checking */
   if (Dptr==NULL) {
      (void) fprintf(stderr,
                     "CNcopy_dataset() : Error - Null dataset!\n");
      return(NULL);
   }
   
   /* This only works for curve datasets */
   if (!(Dptr->datatype == CN_PLOT2D || Dptr->datatype == CN_PLOT3D)) {
      (void) fprintf(stdout,
                     "Sorry - can only copy curve datasets for now.\n");
      return(NULL);
   }

   /* Check curve list */
   if (Dptr->curvehead == NULL) {
      (void) fprintf(stderr,
                     "CNcopy_dataset() : Error - No curves in dataset!\n");
      return(NULL);
   }
  
   /* Create a new dataset */
   Dnew = CNmake_dataset(Dptr->filename,
                         Dptr->label,
                         (int) Dptr->datatype,
                         Dptr->bxmin, Dptr->bxmax,
                         Dptr->bymin, Dptr->bymax,
                         Dptr->bzmin, Dptr->bzmax,
                         Dptr->plot_pr.vxmin, Dptr->plot_pr.vxmax,
                         Dptr->plot_pr.vymin, Dptr->plot_pr.vymax,
                         Dptr->plot_pr.vzmin, Dptr->plot_pr.vzmax,
                         *dataID);
   if (Dnew == NULL) return(NULL);

   /* Increment the dataID */
   (*dataID)++;

   /* Copy the curves */
   for (C=Dptr->curvehead; C!=NULL; C=C->next) {
      /* Allocate a curve data structure */
      Cnew = CNinsert_curve(&(Dnew->curvehead), &(Dnew->curvetail), C->ID);

      /* Copy the properties */
      CNset_curve_property(&(Cnew->curv_pr), &(C->curv_pr));

      /* Copy the points */
      for (P=C->pointhead; P!=NULL; P=P->next)
         (void) CNinsert_point(&(Cnew->pointhead),&(Cnew->pointtail),
                               P->x,P->y,P->z,(int)P->ID);
   }

   /* Copy the properties */
   CNset_plotset_property(&(Dnew->plot_pr), &(Dptr->plot_pr));
   CNset_dataset_property(&(Dnew->data_pr), &(Dptr->data_pr));

   /* Return */
   return(Dnew);
}

/*
 * Transform the data in a dataset
 */
int CNtransform_dataset(Dptr, xshift, yshift, zshift,
                            xmult,  ymult,  zmult)
CNdatasetptr Dptr;
double xshift, yshift, zshift, xmult,  ymult,  zmult;
{
   CNcurveptr C;
   CNpointptr P;
   int        status = 0;

   /* 
    * x' = xmult * x + xshift
    */

   /* Error Checking */
   if (Dptr==NULL) {
      (void) fprintf(stderr,
                     "CNtransform_dataset() : Error - Null dataset!\n");
      return(0);
   }
   
   /* Modify each curve in the dataset */
   if (Dptr->datatype == CN_PLOT2D || Dptr->datatype == CN_PLOT3D) {
      status = 1;
      for (C=Dptr->curvehead; C!=NULL; C=C->next) {
         for (P=C->pointhead; P!=NULL; P=P->next) {
            P->x = xmult*P->x + xshift; 
            P->y = ymult*P->y + yshift; 
            P->z = zmult*P->z + zshift; 
         }
      }
   }

   /* Reset the boundary */
   Dptr->bxmin = xmult*Dptr->bxmin + xshift; 
   Dptr->bymin = ymult*Dptr->bymin + yshift; 
   Dptr->bzmin = zmult*Dptr->bzmin + zshift; 
   Dptr->bxmax = xmult*Dptr->bxmax + xshift; 
   Dptr->bymax = ymult*Dptr->bymax + yshift; 
   Dptr->bzmax = zmult*Dptr->bzmax + zshift; 
   swap(&(Dptr->bxmin), &(Dptr->bxmax));
   swap(&(Dptr->bymin), &(Dptr->bymax));
   swap(&(Dptr->bzmin), &(Dptr->bzmax));

   /* Reset the viewport */
   Dptr->plot_pr.vxmin = xmult*Dptr->plot_pr.vxmin + xshift; 
   Dptr->plot_pr.vymin = ymult*Dptr->plot_pr.vymin + yshift; 
   Dptr->plot_pr.vzmin = zmult*Dptr->plot_pr.vzmin + zshift; 
   Dptr->plot_pr.vxmax = xmult*Dptr->plot_pr.vxmax + xshift; 
   Dptr->plot_pr.vymax = ymult*Dptr->plot_pr.vymax + yshift; 
   Dptr->plot_pr.vzmax = zmult*Dptr->plot_pr.vzmax + zshift; 
   swap(&(Dptr->plot_pr.vxmin), &(Dptr->plot_pr.vxmax));
   swap(&(Dptr->plot_pr.vymin), &(Dptr->plot_pr.vymax));
   swap(&(Dptr->plot_pr.vzmin), &(Dptr->plot_pr.vzmax));

   return(status);
}

static void swap(a,b)
double *a, *b;
{
   double tmp;

   if (*a > *b) {
      tmp = *a;
      *a = *b;
      *b = tmp;
   } 
}