aboutsummaryrefslogtreecommitdiff
path: root/src/graph/view3D/surface3d.c
blob: b89bb6d6034ca3cd657846b43a529ff8da12adae (plain)
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
/*
  Copyright (c) 1991-2002, The Numerical Algorithms Group Ltd.
  All rights reserved.
  Copyright (C) 2007-2008, Gabriel Dos Reis.
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:

      - Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.

      - Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in
        the documentation and/or other materials provided with the
        distribution.

      - Neither the name of The Numerical Algorithms Group Ltd. nor the
        names of its contributors may be used to endorse or promote products
        derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#define _SURFACE3D_C
#include "axiom-c-macros.h"

#include <string.h>
#include <math.h>
#include <stdlib.h>

#include "header.h"
#include "draw.h"
#include "mode.h"   /* for #define components */
#include "sockio.h"
#include "com.h"

#include "XSpadFill.H1"
#include "XShade.H1"
#include "util.H1"
#include "Gfun.H1"
#include "all_3d.H1"


  /**** useful defines ****/

#define precisionFactor 1024

/* depthChecker turns on the extensive depth checking mechanisms
   for the depth sort algorithm. Without it, the hidden surface
   removal is just a sort by z which works remarkably well, but,
   is insufficient and, at times, may end up being incorrect */
#define depthChecker


pointInfo ptIA, ptIB, ptIC; /* global to this file */

/************************************
 * void drawLineComponent(p,dFlag)  *
 ************************************/

void
drawLineComponent (poly * p, int dFlag)
{
  int i, hue;
  int *anIndex;
  RGB col_rgb;

  /* If the polygon is clipped against the hither plane (clipPz) then
     it is not drawn...or...if the clipStuff is set to true, and
     the polygon is clipped against the user defined clip volume, it
     is also not drawn. */
  if (!((p->partialClipPz) || (viewData.clipStuff && (p->partialClip)))) {
    /* This routine will eventually only be skipped if
       p->totalClip is true and another routine would
       handle the partialClip. this routine would handle
       only those polygons without any clipped points */
    for (i=0, anIndex=p->indexPtr; i<p->numpts; i++,anIndex++) {
      quadMesh[i].x = refPt3D(viewData,*anIndex)->px;
      quadMesh[i].y = refPt3D(viewData,*anIndex)->py;
    }

    if (dFlag==Xoption) {
      if (mono || viewport->monoOn)
        GSetForeground(opaqueGC, (float)foregroundColor, dFlag);
      else {
        hue = hueValue(p->color);
        GSetForeground(opaqueGC, (float)XSolidColor(hue,2), dFlag);
      }
    } else
      GSetForeground(opaqueGC, psBlack, dFlag);

    if (dFlag==PSoption && !mono && !viewport->monoOn) {
      hue = getHue(p->color);
      col_rgb = hlsTOrgb((float)hue,0.5,0.8);
      PSDrawColor(col_rgb.r,col_rgb.g,col_rgb.b,quadMesh,p->numpts);
    } else {
      GDrawLines(opaqueGC, viewport->viewWindow, quadMesh, p->numpts,
                 CoordModeOrigin, dFlag);
    }
    if (dFlag == Xoption)
      XMapWindow(dsply, viewport->viewWindow);
  }
}



/**************************************************
 * void drawOpaquePolygon(p,aGC,anotherGC)        *
 **************************************************/

void
drawOpaquePolygon (poly *p,GC aGC,GC anotherGC,int dFlag)
{

  int *anIndex, i, hue, isNaN = 0;
  RGB col_rgb;

  if (mono || viewport->monoOn) {
    GSetForeground(anotherGC, (float)foregroundColor, dFlag);
  } else {
    hue = hueValue(p->color);
    GSetForeground(anotherGC, (float)XSolidColor(hue,2), dFlag);
  }

  /* If the polygon is clipped against the hither plane (clipPz) then
     it is not drawn, or if the clipStuff is set to true, and
     the polygon is clipped against the user defined clip volume, it
     is also not drawn. */

  if (!((p->partialClipPz) || (viewData.clipStuff && (p->partialClip)))) {

    /* This routine should eventually only be skipped if
       p->totalClip is true and another routine would
       handle the partialClip. This routine would handle
       only those polygons without any clipped points. */

    for (i=0, anIndex=p->indexPtr; i<p->numpts; i++,anIndex++) {
      quadMesh[i].x = refPt3D(viewData,*anIndex)->px;
      quadMesh[i].y = refPt3D(viewData,*anIndex)->py;
      if (eqNANQ(quadMesh[i].x) || eqNANQ(quadMesh[i].y)) isNaN = 1;
    }

    quadMesh[i].x =refPt3D(viewData,*(p->indexPtr))->px;
    quadMesh[i].y =refPt3D(viewData,*(p->indexPtr))->py;
    if (eqNANQ(quadMesh[i].x) || eqNANQ(quadMesh[i].y)) isNaN = 1;

    if (dFlag==PSoption && !mono && !viewport->monoOn && !isNaN) {
      GSetForeground(GC9991, (float)backgroundColor, PSoption);
      PSFillPolygon(GC9991, quadMesh, p->numpts+1);
      hue = getHue(p->color);
      col_rgb = hlsTOrgb((float)hue,0.5,0.8);
      if (viewport->diagonals)
        PSDrawColor(col_rgb.r,col_rgb.g,col_rgb.b,quadMesh,p->numpts+1);
      else
        PSDrawColor(col_rgb.r,col_rgb.g,col_rgb.b,quadMesh,p->numpts);
    } else {
      if (mono || viewport->monoOn) {
        GSetForeground(anotherGC, (float)foregroundColor, dFlag);
      } else {
        hue = hueValue(p->color);
        GSetForeground(anotherGC, (float)XSolidColor(hue,2), dFlag);
      }
      GSetForeground(aGC,(float)backgroundColor,dFlag);
      if (!isNaN) {
        XFillPolygon(dsply, viewport->viewWindow, aGC, quadMesh, p->numpts,
                     Convex,CoordModeOrigin);
        if (viewport->diagonals)
          GDrawLines(anotherGC,viewport->viewWindow,quadMesh,p->numpts+1,
                     CoordModeOrigin, dFlag);
        else
          GDrawLines(anotherGC,viewport->viewWindow,quadMesh,p->numpts,
                     CoordModeOrigin, dFlag);
      }
    }
    if (dFlag == Xoption) XMapWindow(dsply,viewport->viewWindow);
  }   /* if not totally clipped */
}



/*************************************
 * poly *copyPolygons(polygonList)   *
 *                                   *
 * copies the given list of polygons *
 * into a newly allocated list       *
 *************************************/

poly *
copyPolygons (poly *polygonList)
{

  int i;
  poly *aPoly,*retval,*prev;

  prev = retval = aPoly = (poly *)saymem("surface.c",1,sizeof(poly));
  aPoly->indexPtr = (int *)saymem("surface.c",
                                  polygonList->numpts,sizeof(int));
  aPoly->num = polygonList->num;
  aPoly->sortNum = polygonList->sortNum;
  aPoly->split = polygonList->split;
  aPoly->numpts = polygonList->numpts;
  for (i=0; i<aPoly->numpts; i++)
    *((aPoly->indexPtr) + i) = *((polygonList->indexPtr) + i);
  aPoly->N[0] = polygonList->N[0];
  aPoly->N[1] = polygonList->N[1];
  aPoly->N[2] = polygonList->N[2];
  aPoly->planeConst = polygonList->planeConst;
  aPoly->color = polygonList->color;
  aPoly->moved = no;
  aPoly->pxmin = polygonList->pxmin;
  aPoly->pxmax = polygonList->pxmax;
  aPoly->pymin = polygonList->pymin;
  aPoly->pymax = polygonList->pymax;
  aPoly->pzmin = polygonList->pzmin;
  aPoly->pzmax = polygonList->pzmax;
  aPoly->xmin = polygonList->xmin;
  aPoly->xmax = polygonList->xmax;
  aPoly->ymin = polygonList->ymin;
  aPoly->ymax = polygonList->ymax;
  aPoly->zmin = polygonList->zmin;
  aPoly->zmax = polygonList->zmax;
  aPoly->normalFacingOut = polygonList->normalFacingOut;
  aPoly->primitiveType = polygonList->primitiveType;
  for (polygonList = polygonList->next;
       polygonList != NIL(poly);
       polygonList = polygonList->next) {
    prev->next = aPoly = (poly *)saymem("surface.c",1,sizeof(poly));
    aPoly->indexPtr = (int *)saymem("surface.c",
                                    polygonList->numpts,sizeof(int));
    aPoly->num = polygonList->num;
    aPoly->sortNum = polygonList->sortNum;
    aPoly->numpts = polygonList->numpts;
    aPoly->split = polygonList->split;
    for (i=0; i<aPoly->numpts; i++)
      *((aPoly->indexPtr) + i) = *((polygonList->indexPtr) + i);
    aPoly->N[0] = polygonList->N[0];
    aPoly->N[1] = polygonList->N[1];
    aPoly->N[2] = polygonList->N[2];
    aPoly->planeConst = polygonList->planeConst;
    aPoly->color = polygonList->color;
    aPoly->moved = no;
    aPoly->pxmin = polygonList->pxmin;
    aPoly->pxmax = polygonList->pxmax;
    aPoly->pymin = polygonList->pymin;
    aPoly->pymax = polygonList->pymax;
    aPoly->pzmin = polygonList->pzmin;
    aPoly->pzmax = polygonList->pzmax;
    aPoly->xmin = polygonList->xmin;
    aPoly->xmax = polygonList->xmax;
    aPoly->ymin = polygonList->ymin;
    aPoly->ymax = polygonList->ymax;
    aPoly->zmin = polygonList->zmin;
    aPoly->zmax = polygonList->zmax;
    aPoly->normalFacingOut = polygonList->normalFacingOut;
    aPoly->primitiveType = polygonList->primitiveType;
    prev = aPoly;
  }
  aPoly->next = 0;
  return(retval);
}


/******************************
 * void minMaxPolygons(aPoly) *
 *                            *
 * sets up the xmin,          *
 * etc, for each polygon      *
 * for sorting and            *
 * extent checking.           *
 ******************************/

void
minMaxPolygons (poly *aPoly)
{

  int *anIndex;
  int i;

  for (; aPoly != NIL(poly); aPoly = aPoly->next) {
    anIndex = aPoly->indexPtr;
    aPoly->pxmin = aPoly->pxmax = refPt3D(viewData,*anIndex)->px;
    aPoly->pymin = aPoly->pymax = refPt3D(viewData,*anIndex)->py;
    aPoly->pzmin = aPoly->pzmax = refPt3D(viewData,*anIndex)->pz;
    aPoly->xmin = aPoly->xmax = refPt3D(viewData,*anIndex)->x;
    aPoly->ymin = aPoly->ymax = refPt3D(viewData,*anIndex)->y;
    aPoly->zmin = aPoly->zmax = refPt3D(viewData,*anIndex)->z;
    for (i=1,anIndex++; i<aPoly->numpts; i++,anIndex++) {
      if (refPt3D(viewData,*anIndex)->px < aPoly->pxmin)
        aPoly->pxmin = refPt3D(viewData,*anIndex)->px;
      else if (refPt3D(viewData,*anIndex)->px > aPoly->pxmax)
        aPoly->pxmax = refPt3D(viewData,*anIndex)->px;
      if (refPt3D(viewData,*anIndex)->py < aPoly->pymin)
        aPoly->pymin = refPt3D(viewData,*anIndex)->py;
      else if (refPt3D(viewData,*anIndex)->py > aPoly->pymax)
        aPoly->pymax = refPt3D(viewData,*anIndex)->py;
      if (refPt3D(viewData,*anIndex)->pz < aPoly->pzmin)
        aPoly->pzmin = refPt3D(viewData,*anIndex)->pz;
      else if (refPt3D(viewData,*anIndex)->pz > aPoly->pzmax)
        aPoly->pzmax = refPt3D(viewData,*anIndex)->pz;

      if (refPt3D(viewData,*anIndex)->x < aPoly->xmin)
        aPoly->xmin = refPt3D(viewData,*anIndex)->x;
      else if (refPt3D(viewData,*anIndex)->x > aPoly->xmax)
        aPoly->xmax = refPt3D(viewData,*anIndex)->x;
      if (refPt3D(viewData,*anIndex)->y < aPoly->ymin)
        aPoly->ymin = refPt3D(viewData,*anIndex)->y;
      else if (refPt3D(viewData,*anIndex)->y > aPoly->ymax)
        aPoly->ymax = refPt3D(viewData,*anIndex)->y;
      if (refPt3D(viewData,*anIndex)->z < aPoly->zmin)
        aPoly->zmin = refPt3D(viewData,*anIndex)->z;
      else if (refPt3D(viewData,*anIndex)->z > aPoly->zmax)
        aPoly->zmax = refPt3D(viewData,*anIndex)->z;
    }
  }
}  /* minMaxPolygons */



/***********************************
 * int polyCompare (p1,p2) *
 *                                 *
 * returns -1 if p1 < p2           *
 *          0 if p1 = p2           *
 *          1 if p1 > p2           *
 * note that this is the reverse   *
 * of what the msort requested.    *
 * this is so that the list will   *
 * be sorted from max to min.      *
 ***********************************/

int
polyCompare (poly *p1,poly *p2)
{

  if (p1->pzmax > p2->pzmax) return(-1);
  else if (p1->pzmax < p2->pzmax) return(1);
  else return(0);
}

/***********************
 * void calcEyePoint() *
 *                     *
 * sets the global     *
 * variable eyePoint[] *
 * to where the viewer *
 * is pointed towards  *
 ***********************/

void
calcEyePoint (void)
{

  eyePoint[0] = sinPhi * (sinTheta);
  eyePoint[1] = sinPhi * (-cosTheta);
  eyePoint[2] = cosPhi;

}



/*
 void drawPolygons()
 A general routine for displaying a list of polygons 
 with the proper hidden surfaces removed. Assumes the 
 list of polygons is in viewData.polygons. Needs a 
 routine to split intersecting polygons in object space.               *
 */


/**************************************
 * void drawRenderedPolygon(p,dFlag)  *
 *                                    *
 * calculate the color for the        *
 * polygon p and draw it              *
 **************************************/

void
drawRenderedPolygon (poly *p,int dFlag)
{

  int        i,hue,shade, isNaN = 0;
  float      whichSide,H[3],P[3],LN,HN,diff,spec,tempLight,lumens,E[3],N[3];
  int        *anIndex, *indx;
  RGB        col_rgb;

  if (!((p->partialClipPz) || (viewData.clipStuff && (p->partialClip)))) {
      /* This routine should eventually only be skipped if
         p->totalClip is true and another routine would
         handle the partialClip. This routine would handle
         only those polygons without any clipped points. */

    for (i=0, anIndex=p->indexPtr; i<p->numpts; i++,anIndex++) {
      quadMesh[i].x = refPt3D(viewData,*anIndex)->px;
      quadMesh[i].y = refPt3D(viewData,*anIndex)->py;
      if (eqNANQ(quadMesh[i].x) || eqNANQ(quadMesh[i].y)) isNaN = 1;
    }
    quadMesh[i].x = refPt3D(viewData,*(p->indexPtr))->px;
    quadMesh[i].y = refPt3D(viewData,*(p->indexPtr))->py;
    if (eqNANQ(quadMesh[i].x) || eqNANQ(quadMesh[i].y)) isNaN = 1;

    if (!isNaN) {
      /* calculate polygon illumination */
      indx = p->indexPtr;
      P[0] = (refPt3D(viewData,*(indx))->wx +
              refPt3D(viewData,*(indx+1))->wx +
              refPt3D(viewData,*(indx+2))->wx);
      P[1] = (refPt3D(viewData,*(indx))->wy +
              refPt3D(viewData,*(indx+1))->wy +
              refPt3D(viewData,*(indx+2))->wy);
      P[2] = (refPt3D(viewData,*(indx))->wz +
              refPt3D(viewData,*(indx+1))->wz +
              refPt3D(viewData,*(indx+2))->wz);
      normalizeVector(P);

      N[0] = p->N[0];  N[1] = p->N[1];  N[2] = p->N[2];
      normalizeVector(eyePoint);
      E[0] = 4.0*eyePoint[0] - P[0];
      E[1] = 4.0*eyePoint[1] - P[1];
      E[2] = 4.0*eyePoint[2] - P[2];
      normalizeVector(E);
      diff = 0.0;        spec = 0.0;
      LN = N[0]*viewport->lightVector[0] +
           N[1]*viewport->lightVector[1] +
           N[2]*viewport->lightVector[2];
      if (LN < 0.0) LN = -LN;
      diff = LN*Cdiff;

      if (LN > 0.0) {
        H[0] = E[0] + viewport->lightVector[0];
        H[1] = E[1] + viewport->lightVector[1];
        H[2] = E[2] + viewport->lightVector[2];
        normalizeVector(H);
        HN = dotProduct(N,H,3);
        if (HN < 0.0) HN = -HN;
        spec = pow((double)absolute(HN),coeff);
        if (spec > 1.0) spec = 1.0;
      }

      lumens = ((Camb + 0.15) + diff + spec*Cspec);
      if (lumens > 1.0) lumens = 1.0;
      if (lumens < 0.0) lumens = 0.0;
      if (dFlag==PSoption && !mono && !viewport->monoOn) {
        hue = getHue(p->color);
        col_rgb = hlsTOrgb((float)hue,lumens,0.8);
        /* NTSC color to grey =  .299 red + .587 green + .114 blue */
        maxGreyShade = (int) psShadeMax;
        whichSide = (.299*col_rgb.r + .587*col_rgb.g + .114*col_rgb.b) *
                    (maxGreyShade-1);
      }
      else {
        if (mono || viewport->monoOn) {
          hue = getHue(p->color);
          col_rgb = hlsTOrgb((float)hue,lumens,0.8);
          whichSide = (.299*col_rgb.r + .587*col_rgb.g + .114*col_rgb.b) *
                      (maxGreyShade-1);
        } else
          whichSide = lumens*(totalShades-1);
      }

      tempLight = lightIntensity;
      if (lightIntensity < Camb) lightIntensity = Camb;

      shade = floor(lightIntensity * absolute(whichSide));
      lightIntensity = tempLight;

      if (shade < totalShades) {
        /* shade < totalShades is (temporarily) necessary here
           because, currently, parameterizations for things like
           the sphere would produce triangular shaped polygons
           close to the poles which get triangularized leaving a
           triangle with coincidental points. the normal for this
           would be undefined (since coincidental points would create
           a zero vector) and the shade would be large, hence,
           the conditional.  */

        if (mono || viewport->monoOn) {
          if (dFlag == Xoption) {
            XChangeShade(dsply,maxGreyShade-shade-1);
            XShadePolygon(dsply,viewport->viewWindow,quadMesh,p->numpts+1,
                          Convex,CoordModeOrigin);
          }
          else if (dFlag == PSoption) { /* renderGC has number 9991 
                                     (see main.c, header.h) */
            GSetForeground(GC9991,
                           1.0-(float)(maxGreyShade-shade-1)*psShadeMul,PSoption);
            PSFillPolygon(GC9991, quadMesh, p->numpts+1);
          }
        } else { /* not mono */
          if (dFlag == Xoption) {
            hue = hueValue(p->color);
            XSpadFillPolygon(dsply, viewport->viewWindow, quadMesh,
                             p->numpts+1, Convex,CoordModeOrigin, hue, shade);
          }
          else if (dFlag == PSoption)  /* draws it out in monochrome  */
            PSColorPolygon(col_rgb.r,col_rgb.g,col_rgb.b,quadMesh,p->numpts+1);
        }        /* if mono-else */

        if (viewData.outlineRenderOn) {
          if (viewport->diagonals) {
            if (dFlag == PSoption) {
              GSetForeground(renderGC,psBlack, dFlag);
              GDrawLines(renderGC,viewport->viewWindow,quadMesh,p->numpts+1,
                         CoordModeOrigin,dFlag);
            } else
              GDrawLines(renderGC,viewport->viewWindow,quadMesh,p->numpts+1,
                         CoordModeOrigin,dFlag);
          } else {
            if (dFlag == PSoption) {
              GSetForeground(renderGC,psBlack,PSoption);
              GDrawLines(renderGC,viewport->viewWindow,quadMesh,p->numpts,
                         CoordModeOrigin,PSoption);
            } else
              GDrawLines(renderGC,viewport->viewWindow,quadMesh,p->numpts,
                         CoordModeOrigin,dFlag);
          }
        }
      }
    } /* if not NaN */
    if (dFlag == Xoption) XMapWindow(dsply,viewport->viewWindow);
  }  /* if not clipped */

} /* drawRenderedPolygon */


void 
freePointResevoir(void)
{

  viewTriple *v;

  while (splitPoints != NIL(viewTriple)) {
    v = splitPoints;
    splitPoints = splitPoints->next;
    free(v);
  }

} /* freePointResevoir */

/***********************************
 * void freeListOfPolygons(pList); *
 *                                 *
 * frees up a list of polygons.    *
 ***********************************/

void
freeListOfPolygons (poly *pList)
{

  poly *nextP;

  for (; pList != NIL(poly); pList=nextP) {
    nextP=pList->next;
    free(pList->indexPtr);
    free(pList);
  }
} /* freeListOfPolygons() */



void
drawPolygons(int dFlag)
{

  poly *p,*head;
  poly *tempQuick=NULL;
  int quickFirst=yes;

  if (recalc) {

    /* To get around multiple X Expose events the server tends
       to send upon startup, leave negation of firstTime to the end. */
    rotated = no;
    zoomed = no;
    translated = no;
    switchedPerspective = no;
    changedEyeDistance = no;
    redoSmooth = yes;

    if (keepDrawingViewport()) {
      if (!firstTime) {
        strcpy(control->message,"          Creating Polygons         ");
        writeControlMessage();
        freeListOfPolygons(quickList);
        freePointResevoir();
      }
      strcpy(control->message,"         Collecting Polygons        ");
      writeControlMessage();
      quickList = copyPolygons(viewData.polygons);

      if (keepDrawingViewport()) {
        /* to get normal facing outside info    */
        strcpy(control->message,"         Projecting Polygons        ");
        writeControlMessage();
        projectAllPolys(quickList);
        if (keepDrawingViewport()) {
          strcpy(control->message,"       Setting Extreme Values       ");
          writeControlMessage();
          minMaxPolygons(quickList);
          if (keepDrawingViewport()) {
            strcpy(control->message,"          Sorting Polygons          ");
            writeControlMessage();
            quickList = msort(quickList,0,viewData.numPolygons,polyCompare);
            if (keepDrawingViewport()) {
              calcEyePoint();
              head = p = quickList;

              clearControlMessage();
              strcpy(control->message,viewport->title);
              writeControlMessage();

              if (viewData.scaleDown) {
                if (keepDrawingViewport()) {
                  for (p=quickList;
                       keepDrawingViewport() && (p != NIL(poly));
                       p=p->next) {
                    switch (p->primitiveType) {
                    case pointComponent:
                      if (dFlag==Xoption) {
                        if (mono || viewport->monoOn)
                          GSetForeground(componentGC,
                                         (float)foregroundColor, dFlag);
                        else
                          GSetForeground(componentGC,
                                         (float)meshOutline, dFlag);
                      } else {
                        GSetForeground(componentGC, psBlack, dFlag);
                        GFillArc(componentGC, viewport->viewWindow,
                                 (int)refPt3D(viewData,*(p->indexPtr))->px,
                                 (int)refPt3D(viewData,*(p->indexPtr))->py,
                                 viewData.pointSize,viewData.pointSize,0,
                                 360*64, dFlag);
                      }
                      break;
                    case lineComponent:
                      drawLineComponent(p,dFlag);
                      break;
                    default:
                      if (viewData.style == opaqueMesh) {
                        GSetForeground(globGC,(float)backgroundColor,dFlag);
                        drawOpaquePolygon(p,globGC,opaqueGC,dFlag);
                      } else {
                        drawRenderedPolygon(p,dFlag);
                      }
                    } /* switch */
                  }
                }
              }

              if (!quickFirst) {
                /* append the rest of the polygons onto the list */
                tempQuick->next = head;
                /* but do not continue the drawing... */
                if (head != NIL(poly)) head->doNotStopDraw = no;
              }  /* if !quickFirst */
              finishedList = (p==NIL(poly));
            }      /*      for various      */
          }        /*         steps         */
        }          /*           of          */
      }    /* keepDrawingViewport() */
    }              /*          ***          */
    /* May want to have a flag somewhere to stop the drawing yet
       continue the freeing */
    if (firstTime) firstTime = no;
  } else {  /* if recalc else if not recalc just draw stuff in list */
    if (keepDrawingViewport()) {
      for (p=quickList;
           keepDrawingViewport() && p != NIL(poly) &&
             (viewData.scaleDown || p->doNotStopDraw);  p=p->next) {
        projectAPoly(p);
        switch (p->primitiveType) {
        case pointComponent:
          if (dFlag==Xoption) {
            if (mono || viewport->monoOn)
              GSetForeground(componentGC,(float)foregroundColor, dFlag);
            else
              GSetForeground(componentGC,(float)meshOutline, dFlag);
          } else
            GSetForeground(componentGC,psBlack, dFlag);
          GFillArc(componentGC, viewport->viewWindow,
                   (int)refPt3D(viewData,*(p->indexPtr))->px,
                   (int)refPt3D(viewData,*(p->indexPtr))->py,
                   viewData.pointSize,viewData.pointSize,0,360*64,dFlag);
          break;
        case lineComponent:
          drawLineComponent(p,dFlag);
          break;
        default:
          if (viewData.style == opaqueMesh) {
            GSetForeground(globGC,(float)backgroundColor,dFlag);
            drawOpaquePolygon(p,globGC,opaqueGC,dFlag);
          } else
            drawRenderedPolygon(p,dFlag);
        } /* switch */
      }
    }
  }
} /* drawPolygons */







/**************************
 * int lessThan(x,y)      *
 * int greaterThan(x,y)   *
 * int equal(x,y)         *
 *                        *
 * Compares two floating  *
 * point numbers for      *
 * precision of up to one *
 * place in a thousand.   *
 * returns                *
 *    1 if true           *
 *    o otherwise         *
 **************************/

int
lessThan (float x,float y)
{
  int xI,yI;

  xI = x*precisionFactor;
  yI = y*precisionFactor;
  return(xI<yI);
}

int
greaterThan (float x,float y)
{
  int xI,yI;

  xI = x*precisionFactor;
  yI = y*precisionFactor;
  return(xI>yI);
}

int
isNaN (float v)
{
  return (v != v);
}


int
isNaNPoint (float x,float y,float z)
{
  return (isNaN(x) || isNaN(y) || isNaN(z));
}

int
equal (float x,float y)
{
  int xI,yI;

  xI = x*precisionFactor;
  yI = y*precisionFactor;
  return(xI==yI);
}