File: probe.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 (856 lines) | stat: -rw-r--r-- 26,425 bytes parent folder | download | duplicates (2)
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
/*
 * Point - find out the z-value of a point inside a triangle/rectangle
 */

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

void CNprobe_dataset();
int  CNpoint_in_region();
static int  point_in_mesh();
static int  point_in_grid();
static int  point_in_tria();
static int  point_in_rect();
static int  point_in_poly();
 
static CNprobeptr make_probe();
static void print_probe();

int CNprobe_dataset_zave();

/*
 * Probe a dataset
 */
void CNprobe_dataset(Dptr, x, y, probehead, probetail, verbose)
CNdatasetptr Dptr;                    /* Dataset to be probed */
double       x, y;                    /* Probe coordinates    */
CNprobeptr   *probehead, *probetail;  /* Results              */
int          verbose;
{
   /* The probe will either work on triangles etc or on a grid array */
   if (Dptr->triahead || Dptr->recthead || Dptr->polyhead) {
      /* Probe the dataset mesh */
      (void) point_in_mesh(x,y,
                           Dptr->triahead, Dptr->triatail,
                           Dptr->recthead, Dptr->recttail,
                           Dptr->polyhead, Dptr->polytail,
                           Dptr->regionhead, Dptr->regiontail,
                           probehead, probetail, 
                           Dptr->data_pr.logx,
                           Dptr->data_pr.logy,
                           Dptr->data_pr.logz,
                           (int) Dptr->ID, 
                           verbose);
   } else if (Dptr->grid && Dptr->datatype==CN_CONTOUR) {
      /* Probe the dataset mesh */
      (void) point_in_grid(x,y,
                           Dptr->grid,
                           probehead, probetail, 
                           Dptr->data_pr.logx,
                           Dptr->data_pr.logy,
                           Dptr->data_pr.logz,
                           (int) Dptr->ID, 
                           verbose);
   }

   if (verbose) CNprint_probe_list(*probehead, *probetail);
}


/*
 * Check to see if a point in the mesh coincides with a region
 */
int CNpoint_in_region(Dptr,x,y,regID,verbose)
CNdatasetptr Dptr;                    /* Dataset to be probed */
double       x, y;                    /* Probe coordinates    */
int          regID;
int          verbose;
{
   CNprobeptr probehead=NULL, probetail=NULL, P;
   int        MATCHFOUND=CN_FALSE;

   /* Probe the mesh */
   (void) CNprobe_dataset(Dptr, x, y, &probehead, &probetail, verbose);

   /* Check the probe-points to see if the regionID coincides */
   for (P=probehead; P!=NULL && !MATCHFOUND; P=P->next) {
      if ((P->T != NULL) && (P->T->region == regID))
         MATCHFOUND = CN_TRUE;
      if ((P->R != NULL) && (P->R->region == regID))
         MATCHFOUND = CN_TRUE;
   }

   /* Delete the list */
   CNdelete_probe_list(&probehead, &probetail);

   /* return */
   return(MATCHFOUND);
}


/*
 * Search thru the mesh for an intersection with a line ax+by+d=0 (const z)
 */
/*ARGSUSED*/
static int point_in_mesh(x,y,
                         triahead,triatail,
                         recthead,recttail,
                         polyhead,polytail,
                         regionhead,regiontail,
                         probehead, probetail, 
                         logx, logy, logz,
                         dataID, verbose)
double       x, y;                    /* Probe coordinates        */
CNtriaptr    triahead, triatail;      /* Triangle  list           */
CNrectptr    recthead, recttail;      /* Rectangle list           */
CNpolyptr    polyhead, polytail;      /* Polygon   list           */
CNregionptr  regionhead, regiontail;  /* Region list              */
CNprobeptr   *probehead, *probetail;  /* Results                  */
short        logx, logy, logz;        /* Log/Linear interpolation */
int          dataID;
int          verbose;
{
   CNpointptr  pointhead=NULL, pointtail=NULL, P;
   CNtriaptr   T;
   CNrectptr   R;
   CNpolyptr   Po;
   CNregionptr reg, Treg=NULL;
   int         probe_count, new_probe_points;
   int         TREG_FOUND;

   /* Count the number of items in the probelist */
   probe_count = CNcount_probes(*probehead, *probetail);

   /* Go thru the triangles first */
   for (T=triahead; T!=NULL; T=T->next) {
      /* Search for point-intersections */
      (void) point_in_tria(T,x,y,&pointhead,&pointtail,
                           logx, logy, logz, verbose);

      /* Find the region */
      TREG_FOUND = CN_FALSE;
      for (reg=regionhead; reg!=NULL; reg=reg->next) 
         if (reg->ID == T->region) {
            TREG_FOUND = CN_TRUE;
            Treg       = reg;
         }
      if (!TREG_FOUND) Treg = NULL;

      /* Place the results in a probe-list */
      for (P=pointhead; P!=NULL; P=P->next) 
         (void) CNinsert_probe(probehead, probetail,
                               P->x,P->y,P->z,
                               T,(CNrectptr)NULL,(CNpolyptr)NULL,Treg,dataID);

      /* Remove everything in the pointlist */
      CNdelete_point_list(&pointhead, &pointtail);
   }

   /* Go thru the rectangles next */
   Treg = NULL;
   for (R=recthead; R!=NULL; R=R->next) {
      /* Search for point-intersections */
      (void) point_in_rect(R,x,y,&pointhead,&pointtail,
                           logx, logy, logz, verbose);

      /* Find the region */
      TREG_FOUND = CN_FALSE;
      for (reg=regionhead; reg!=NULL; reg=reg->next)
         if (reg->ID == R->region) {
            TREG_FOUND = CN_TRUE;
            Treg       = reg;
         }
      if (!TREG_FOUND) Treg = NULL;

      /* Place the results in a probe-list */
      for (P=pointhead; P!=NULL; P=P->next) 
         (void) CNinsert_probe(probehead, probetail,
                               P->x,P->y,P->z,
                               (CNtriaptr)NULL,R,(CNpolyptr)NULL,Treg,dataID);
 
      /* Remove everything in the pointlist */
      CNdelete_point_list(&pointhead, &pointtail);
   }

   /* Go thru the polygons next */
   for (Po=polyhead; Po!=NULL; Po=Po->next) {
      /* Search for point-intersections */
      (void) point_in_poly(Po,x,y,&pointhead,&pointtail,
                           logx, logy, logz, verbose);

      /* Find the region */
      TREG_FOUND = CN_FALSE;
      for (reg=regionhead; reg!=NULL; reg=reg->next) 
         if (reg->ID == Po->region) {
            TREG_FOUND = CN_TRUE;
            Treg       = reg;
         }
      if (!TREG_FOUND) Treg = NULL;

      /* Place the results in a probe-list */
      for (P=pointhead; P!=NULL; P=P->next) 
         (void) CNinsert_probe(probehead, probetail,
                               P->x,P->y,P->z,
                               (CNtriaptr)NULL,(CNrectptr)NULL,Po,Treg,dataID);

      /* Remove everything in the pointlist */
      CNdelete_point_list(&pointhead, &pointtail);
   }


   /* Now count the number of probe-points */
   new_probe_points = CNcount_probes(*probehead, *probetail) - probe_count;
   
   if (verbose) 
      (void) fprintf(stdout,"Found %d probe-points\n",new_probe_points);

   /* Return */
   return(new_probe_points);
}


/*
 * Search thru the grid for an intersection with a line ax+by+d=0 (const z)
 */
/*ARGSUSED*/
static int point_in_grid(x,y,
                         grid,
                         probehead, probetail, 
                         logx, logy, logz,
                         dataID, verbose)
double       x, y;                    /* Probe coordinates        */
CNgrid4Dptr  grid;                    /* A grid array structure   */
CNprobeptr   *probehead, *probetail;  /* Results                  */
short        logx, logy, logz;        /* Log/Linear interpolation */
int          dataID;
int          verbose;
{
   CNrectptr   recthead=NULL, recttail=NULL, R;
   CNnodeptr   nodehead=NULL, nodetail=NULL;
   CNpointptr  pointhead=NULL, pointtail=NULL, P;
   CNpointptr  new_pointhead=NULL, new_pointtail=NULL;
   CNpointptr  pt00, pt01, pt10, pt11;
   CNnodeptr   nd00, nd01, nd10, nd11;
   double      xa, xb, xc, x1, x2, ya, yb, yc, y1, y2, z;
   int         probe_count, new_probe_points;
   int         nx, ny, i, j;

   /* Count the number of items in the probelist */
   probe_count = CNcount_probes(*probehead, *probetail);

   /* Go thru the grid-array and construct rectangles that contain the point */
   nx = grid->nx;
   ny = grid->ny;
   for (i=0; i<nx; i++) {
      /* x-coordinates of rectangle */
      xb    = CNget_1D_double_array_value(grid->xarray,i  ,nx);
      if (i>0)
         xa = CNget_1D_double_array_value(grid->xarray,i-1,nx);
      else
         xa = xb;
      if (i<nx-1)
         xc = CNget_1D_double_array_value(grid->xarray,i+1,nx);
      else
         xc = xb;

      /* Corners */
      x1 = 0.5*(xa + xb);
      x2 = 0.5*(xb + xc);

      /* Filter */
      if ((x > LARGER_OF(x1,x2)) || (x < SMALLER_OF(x1,x2))) continue;

      /* Go thru the ygrid */
      for (j=0; j<ny; j++) {
         /* Find the corners of the rectangle (centered about [i,j]) */
 
         /* y-coordinates of rectangle */
         yb    = CNget_1D_double_array_value(grid->yarray,j  ,ny);
         if (j>0)
            ya = CNget_1D_double_array_value(grid->yarray,j-1,ny);
         else
            ya = yb;
         if (j<ny-1)
            yc = CNget_1D_double_array_value(grid->yarray,j+1,ny);
         else
            yc = yb;
 
         /* corners */
         y1 = 0.5*(ya + yb);
         y2 = 0.5*(yb + yc);

         /* Filter */
         if ((y > LARGER_OF(y1,y2)) || (y < SMALLER_OF(y1,y2))) continue;

         /* z-value */
         z = CNget_1D_double_array_value(grid->zarray, (i+j*nx),nx*ny);

         /* Create the rectangle */
         pt00 = CNinsert_point(&pointhead,&pointtail,x1,y1,z,0);
         nd00 = CNinsert_tailnode(&nodehead,&nodetail,pt00,z,0);
         pt01 = CNinsert_point(&pointhead,&pointtail,x2,y1,z,0);
         nd01 = CNinsert_tailnode(&nodehead,&nodetail,pt01,z,0);
         pt11 = CNinsert_point(&pointhead,&pointtail,x2,y2,z,0);
         nd11 = CNinsert_tailnode(&nodehead,&nodetail,pt11,z,0);
         pt10 = CNinsert_point(&pointhead,&pointtail,x1,y2,z,0);
         nd10 = CNinsert_tailnode(&nodehead,&nodetail,pt10,z,0);
         (void) CNinsert_rect(&recthead,&recttail, nd00,nd01,nd11,nd10,0);
      }
   }

   /* Go thru the rectangles next */
   for (R=recthead; R!=NULL; R=R->next) {
      /* Search for point-intersections */
      (void) point_in_rect(R,x,y,&new_pointhead,&new_pointtail,
                           logx, logy, logz, verbose);

      /* Place the results in a probe-list */
      for (P=new_pointhead; P!=NULL; P=P->next) 
         (void) CNinsert_probe(probehead, probetail,
                               P->x,P->y,P->z,
                               (CNtriaptr)NULL,R,(CNpolyptr)NULL,
                               (CNregionptr)NULL, dataID);
 
      /* Remove everything in the pointlist */
      CNdelete_point_list(&new_pointhead, &new_pointtail);
   }

   /* Remove all the rects nodes and points */
   CNdelete_point_list(&pointhead, &pointtail);
   CNdelete_node_list(&nodehead, &nodetail);
   CNdelete_rect_list(&recthead, &recttail);

   /* Now count the number of probe-points */
   new_probe_points = CNcount_probes(*probehead, *probetail) - probe_count;
   
   if (verbose) 
      (void) fprintf(stdout,"Found %d probe-points\n",new_probe_points);

   /* Return */
   return(new_probe_points);
}

/*
 * Find the z-value of a point inside a triangle
 * Store the result in a pointlist, for compatibilty with the
 * point-rectangle routine.
 */
/*ARGSUSED*/
static int point_in_tria(T,x,y,pointhead,pointtail,
                         logx, logy, logz, verbose)
CNtriaptr  T;
double     x, y;                    /* The x-y coordinates      */
CNpointptr *pointhead, *pointtail;  /* Point list               */
short      logx, logy, logz;        /* Log/Linear interpolation */
int        verbose;
{
   double     xmax, xmin, ymax, ymin, dx, t, z, px, py;
   CNpoint    pt1, pt2, pt3, ptmin, ptmax;
   int        signz;
   int        xlogmode, ylogmode, zlogmode;
   int        intsct;

   /* Check the triangle */
   if (T==NULL) return(0);

   /* Check the boundaries of the triangle first */
   CNget_tria_xmaxmin(T,&xmin,&xmax);
   CNget_tria_ymaxmin(T,&ymin,&ymax);
   if (x < xmin || x > xmax) return(0);
   if (y < ymin || y > ymax) return(0);

   /* Copy the point coordinates */
   pt1 = *(T->n1->coord);   pt1.z = T->n1->t;
   pt2 = *(T->n2->coord);   pt2.z = T->n2->t;
   pt3 = *(T->n3->coord);   pt3.z = T->n3->t;

   /*
    * Convert the numbers to log if necessary then interpolate
    */
   px = x;
   py = y;
   if (logx) {
      xlogmode = CNlogmode3(pt1.x, pt2.x, pt3.x);
      pt1.x = CNlogabs(pt1.x, xlogmode);
      pt2.x = CNlogabs(pt2.x, xlogmode);
      pt3.x = CNlogabs(pt3.x, xlogmode);
      px    = CNlogabs(x    , xlogmode);
   }
   if (logy) {
      ylogmode = CNlogmode3(pt1.y, pt2.y, pt3.y);
      pt1.y = CNlogabs(pt1.y, ylogmode);
      pt2.y = CNlogabs(pt2.y, ylogmode);
      pt3.y = CNlogabs(pt3.y, ylogmode);
      py    = CNlogabs(y    , ylogmode);
   }
   if (logz) {
      signz    = CNsign(pt1.z);
      zlogmode = CNlogmode3(pt1.z, pt2.z, pt3.z);
      pt1.z = CNlogabs(pt1.z, zlogmode);
      pt2.z = CNlogabs(pt2.z, zlogmode);
      pt3.z = CNlogabs(pt3.z, zlogmode);
   }

   /* Now check for an intersection with the segments */
   intsct = CNpoly3_intsct_plane(0.0,1.0,0.0,-py,
                                 &pt1,&pt2,&pt3,&ptmin,&ptmax,
                                 verbose);

   /* This should give me a line parallel to the x-axis */
   if (intsct != 1) return(0);

   /* Reorder ptmin and ptmax in x */
   if (ptmin.x > ptmax.x) {
      pt1   = ptmax;
      ptmax = ptmin;
      ptmin = pt1;
   }

   /* if the point is outside the x-limits of the intersection, return */
   if (px < ptmin.x || x > ptmax.x) return(0);

   /* Now get the intersection */
   dx = ptmax.x - ptmin.x;
   if (fabs(dx) < CN_SMALLER) t = 0.0;
   else                       t = (px - ptmin.x) / dx;
   z = ptmin.z + t*(ptmax.z - ptmin.z);

   /* Reconvert the results */
   if (logz) z = CNinvlogabs(z,signz,zlogmode);

   /* Put the coords inside a point-list */
   (void) CNinsert_point(pointhead, pointtail, x, y, z, 0);

   /* Return */
   return(1);
}

/*
 * Find the z-value of a point inside a rectangle
 * There could be more than 1 point, so store the results in a pointlist
 */
/*ARGSUSED*/
static int point_in_rect(R,x,y,pointhead,pointtail,
                         logx, logy, logz, verbose)
CNrectptr  R;
double     x, y;                    /* The x-y coordinates      */
CNpointptr *pointhead, *pointtail;  /* Point list               */
short      logx, logy, logz;        /* Log/Linear interpolation */
int        verbose;
{
   double     xmax, xmin, ymax, ymin, dx, t, z, px, py;
   CNpoint    pt1, pt2, pt3, pt4, newpt;
   CNpoint    points[10], ptmin, ptmax;
   CNpointptr P;
   int        signz;
   int        xlogmode, ylogmode, zlogmode;
   int        i, npts=0, intsct, FOUND=CN_FALSE, PTFOUND=CN_FALSE;

   /* Check the rectangle */
   if (R==NULL) return(0);

   /* Check the boundaries of the rectangle first */
   CNget_rect_xmaxmin(R,&xmin,&xmax);
   CNget_rect_ymaxmin(R,&ymin,&ymax);
   if (x < xmin || x > xmax) return(0);
   if (y < ymin || y > ymax) return(0);

   /* Copy the point coordinates */
   pt1 = *(R->n1->coord);   pt1.z = R->n1->t;
   pt2 = *(R->n2->coord);   pt2.z = R->n2->t;
   pt3 = *(R->n3->coord);   pt3.z = R->n3->t;
   pt4 = *(R->n4->coord);   pt4.z = R->n4->t;

   /*
    * Convert the numbers to log if necessary then interpolate
    */
   px = x;
   py = y;
   if (logx) {
      xlogmode = CNlogmode4(pt1.x, pt2.x, pt3.x, pt4.x);
      pt1.x = CNlogabs(pt1.x, xlogmode);
      pt2.x = CNlogabs(pt2.x, xlogmode);
      pt3.x = CNlogabs(pt3.x, xlogmode);
      pt4.x = CNlogabs(pt4.x, xlogmode);
      px    = CNlogabs(x    , xlogmode);
   }
   if (logy) {
      ylogmode = CNlogmode4(pt1.y, pt2.y, pt3.y, pt4.y);
      pt1.y = CNlogabs(pt1.y, ylogmode);
      pt2.y = CNlogabs(pt2.y, ylogmode);
      pt3.y = CNlogabs(pt3.y, ylogmode);
      pt4.y = CNlogabs(pt4.y, ylogmode);
      py    = CNlogabs(y    , ylogmode);
   }
   if (logz) {
      signz    = CNsign(pt1.z);
      zlogmode = CNlogmode4(pt1.z, pt2.z, pt3.z, pt4.z);
      pt1.z = CNlogabs(pt1.z, zlogmode);
      pt2.z = CNlogabs(pt2.z, zlogmode);
      pt3.z = CNlogabs(pt3.z, zlogmode);
      pt4.z = CNlogabs(pt4.z, zlogmode);
   }

   /* Now check for an intersection with the segments */
   intsct = CNpoly4_intsct_plane(0.0,1.0,0.0,-py,
                                 &pt1,&pt2,&pt3,&pt4,
                                 points, &npts, 
                                 verbose);

   /* This should give me lines parallel to the x-axis */
   if (intsct) {
      for (i=0; i<npts; i=i+2) {
         /* Skip if the line is too short */
         if (!CNlongline(&(points[i]),&(points[i+1]),1.0e-5)) continue;

         /* skip if the point is outside the x-limits of the intersection */
         if (points[i].x > points[i+1].x) {
            ptmin = points[i+1];
            ptmax = points[i];
         } else {
            ptmin = points[i];
            ptmax = points[i+1];
         }
         if (px < ptmin.x || px > ptmax.x) continue; 

         /* Now get the intersection */
         dx = ptmax.x - ptmin.x;
         if (fabs(dx) < CN_SMALLER) t = 0.0;
         else                       t = (px - ptmin.x) / dx;
         z = ptmin.z + t*(ptmax.z - ptmin.z);

         /* Reconvert the results */
         if (logz) z = CNinvlogabs(z,signz,zlogmode);

         /* Put the coords inside a point-structure for comparison */
         newpt.x = x;
         newpt.y = y;
         newpt.z = z;
         
         /* Find out if the point is already in the list */
         PTFOUND = CN_FALSE;
         for (P=(*pointhead); P!=NULL && !PTFOUND; P=P->next) 
             if (!CNlongline(P,&newpt,1.0e-5)) PTFOUND = CN_TRUE; 

         /* Put the coords inside a point-list */
         if (!PTFOUND) {
            (void) CNinsert_point(pointhead, pointtail, x, y, z, 0);
            FOUND = CN_TRUE;
         }
      }
   }
   return(FOUND);
}

/*
 * Find the z-value of a point inside a polygon 
 * There could be more than 1 point, so store the results in a pointlist
 */
/*ARGSUSED*/
static int point_in_poly(P,x,y,pointhead,pointtail, 
                         logx, logy, logz, verbose)
CNpolyptr  P;
double     x, y;                    /* The x-y coordinates      */
CNpointptr *pointhead, *pointtail;  /* Point list               */
int        verbose;
short      logx, logy, logz;        /* Log/Linear interpolation */
{
   double     xmax, xmin, ymax, ymin;
   CNrectptr  recthead=NULL, recttail=NULL, R;
   CNtriaptr  triahead=NULL, triatail=NULL, T;
   int        npts, FOUND=CN_FALSE;

   /* Check the polygon */
   if (P==NULL) return(0);

   /* Check the boundaries of the polygon first */
   CNget_poly_xmaxmin(P,&xmin,&xmax);
   CNget_poly_ymaxmin(P,&ymin,&ymax);
   if (x < xmin || x > xmax) return(0);
   if (y < ymin || y > ymax) return(0);

   /* Reuse, reuse...  Put the polygon in a triangle or rectangle */
   npts = CNcount_nlists(P->nlisthead, P->nlisttail);
   if ((npts > 1) && (P->nlisthead->N == P->nlisttail->N)) npts--;
   if (npts == 3) {
      /* Put this polygon in a triangle */
      T = CNinsert_tria(&triahead, &triatail,
                        P->nlisthead->N,
                        P->nlisthead->next->N,
                        P->nlisthead->next->next->N,
                        P->region, P->ID);

      /* Intersection check */
      FOUND = point_in_tria(T,x,y,pointhead,pointtail,
                            logx, logy, logz, verbose);

      /* Delete the triangle list */
      CNdelete_tria_list(&triahead, &triatail);

   } else if (npts == 4) {
      /* Put this polygon into a rectangle */
      R = CNinsert_rect(&recthead, &recttail,
                        P->nlisthead->N,
                        P->nlisthead->next->N,
                        P->nlisthead->next->next->N,
                        P->nlisthead->next->next->next->N,
                        P->ID);

      /* Intersection check */
      FOUND = point_in_rect(R,x,y,pointhead,pointtail,
                            logx, logy, logz, verbose);

      /* Delete the rectangle list */
      CNdelete_rect_list(&recthead, &recttail);

   } else {
      FOUND = CN_FALSE;
   }
   return(FOUND);
}


/*
 * Linked list procedures
 */

/*
 * Allocate room for a probe
 */
static CNprobeptr make_probe(x,y,z,T,R,P,reg,ID)
double      x,y,z;
CNtriaptr   T;
CNrectptr   R;
CNpolyptr   P;
CNregionptr reg;
int         ID;
{
   CNprobeptr newptr;
   unsigned int size = sizeof(CNprobe);
 
   if ((newptr = (CNprobeptr)malloc(size))!=NULL) {
      newptr->ID   = ID;
      newptr->x    = x;
      newptr->y    = y;
      newptr->z    = z;
      newptr->T    = T;
      newptr->R    = R;
      newptr->P    = P;
      newptr->reg  = reg;
      newptr->next = NULL;
      newptr->prev = NULL;
   }
   return(newptr);
}

/*
 * Insert a probe at the tail of the current probe list
 */
CNprobeptr CNinsert_probe(probe_listhead, probe_listtail, 
                          x, y, z, T, R, P, reg, ID)
CNprobeptr *probe_listhead, *probe_listtail;
double      x, y, z;
CNtriaptr   T; 
CNrectptr   R; 
CNpolyptr   P;
CNregionptr reg;
int         ID;
{
   CNprobeptr next,A,B;
 
   A = *probe_listtail;
   if ((B=make_probe(x,y,z,T,R,P,reg,ID))!=NULL) {
      if (A==NULL) {
         *probe_listhead = B;
         *probe_listtail = B;
      } else {
         next = A->next;
         B->next = next;
         B->prev = A;
         A->next = B;
         if (next != NULL) next->prev = B;
         if (B->next == NULL) *probe_listtail = B;
      }
   }
   return(B);
}


/*
 * Delete probe at address L
 */
void CNdelete_probe(probe_listhead, probe_listtail, L)
CNprobeptr *probe_listhead, *probe_listtail;
CNprobeptr L;
{
   CNprobeptr prev,next;
 
   prev = L->prev;
   next = L->next;
   if (prev!=NULL) prev->next = next;
   if (next!=NULL) next->prev = prev;
   if (L== *probe_listhead) *probe_listhead = next;
   if (L== *probe_listtail) *probe_listtail = prev;
   free ((char*)L);
   L = NULL;
}
 
 
/*
 * Delete all the probes in the list
 */
void CNdelete_probe_list(probe_listhead, probe_listtail)
CNprobeptr *probe_listhead, *probe_listtail;
{
   CNprobeptr P;
   while ((P = *probe_listhead) != NULL)
      CNdelete_probe(probe_listhead, probe_listtail, P);
}
 
 
/*
 * Print out the list of probes
 */
/*ARGSUSED*/
void CNprint_probe_list(probe_listhead, probe_listtail)
CNprobeptr probe_listhead, probe_listtail;
{
   CNprobeptr P;
 
   (void) fprintf(stdout,"   Found %d intersections :\n",
           CNcount_probes(probe_listhead, probe_listtail));

   for (P=probe_listhead; P!=NULL; P=P->next)
      print_probe(P);
}
 
 
/*
 * print the coordinates of a probe
 */
static void print_probe(pt)
CNprobeptr pt;
{
   (void) fprintf(stdout,"Probe Coordinates : (%g %g %g)",pt->x,pt->y,pt->z);
   if (pt->T != NULL)
   (void) fprintf(stdout,"   Tria ID=%d  Reg ID=%d",pt->T->ID,pt->T->region);
   if (pt->T != NULL && pt->reg != NULL)
   (void) fprintf(stdout,"   Mat Name=\"%s\"",pt->reg->matname);
   if (pt->R != NULL)
   (void) fprintf(stdout,"   Rect ID=%d",pt->R->ID);
   (void) fprintf(stdout,"\n");
}
 
 
/*
 * Count the number of probes in the list
 */
/*ARGSUSED*/
int CNcount_probes(probe_listhead, probe_listtail)
CNprobeptr probe_listhead, probe_listtail;
{
   CNprobeptr P;
   int        count = 0;
 
   for (P=probe_listhead; P!=NULL; P=P->next) count++;
 
   return(count);
}


/*
 * MISC PROBE ROUTINES
 */

/*
 * Determine the average Z-value of a dataset within a specified window
 */
int CNprobe_dataset_zave(Dptr, xmin, xmax, ymin, ymax, zave,
                         npoints)
CNdatasetptr Dptr;                    /* Dataset to be probed */
double       xmin, xmax;              /* x-boundary           */
double       ymin, ymax;              /* y-boundary           */
double       *zave;                   /* average z-value      */
int          *npoints;                /* No of points counted */
{
   double     ave  = 0.0;
   int        npts = 0;
   int        i, j, k;
   double     x, y, z;  
   int        FOUND;
   CNnodeptr  N;

   /* Error check */
   if (Dptr == NULL) {
      (void) fprintf(stderr,"Error! Cannot analyze NULL dataset!\n");
      return (CN_FALSE);
   }

   /* Initialize */
   *zave    = 0.0;
   *npoints = 0;
   FOUND    = CN_FALSE;

   /* Branch according to dataset-type */
   if (Dptr->datatype == CN_CONTOUR) {
      /* This will work for CONTOUR datasets */

      if (Dptr->nodehead != NULL) {

         /* Go thru all the nodes */
         FOUND = CN_TRUE;
         for (N=Dptr->nodehead; N!=NULL; N=N->next) {
            if ((N->coord->x >= xmin && N->coord->x <= xmax) && 
                (N->coord->y >= ymin && N->coord->y <= ymax)) { 
               ave += N->t;
               npts++;
            }
         }
     
         /* Ave z-value */
         if (npts > 0)
            *zave = ave/(double)npts;
         *npoints = npts;

      } else if (Dptr->grid && (Dptr->datatype == CN_CONTOUR)) {

         /* 
          * Sometimes the CONTOUR dataset just contains a grid and
          * no actual points. This often happens when the grid is too
          * large to store as rectangles/triangles
          */

         /* Go thru the grid */
         FOUND = CN_TRUE;
         for (i=0; i<Dptr->grid->nx; i++) {
            for (j=0; j<Dptr->grid->ny; j++) {
               x = CNget_1D_double_array_value(Dptr->grid->xarray,i  ,
                                               Dptr->grid->nx);
               y = CNget_1D_double_array_value(Dptr->grid->yarray,j  , 
                                               Dptr->grid->ny);
               if ((x >= xmin && x <= xmax) && 
                   (y >= ymin && y <= ymax)) { 
                  k = i+j*Dptr->grid->nx;
                  z = CNget_1D_double_array_value(
                                               Dptr->grid->zarray,k,
                                               Dptr->grid->nx*Dptr->grid->ny);
                  ave += z;
                  npts++;
               }
            }
         }

         /* Ave z-value */
         if (npts > 0)
            *zave = ave/(double)npts;
         *npoints = npts;

      }
   }  

   /* Return */
   return (FOUND);
}