File: feedback.c

package info (click to toggle)
viewmol 2.3-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 9,424 kB
  • ctags: 2,239
  • sloc: ansic: 29,098; sh: 909; makefile: 513; python: 238
file content (579 lines) | stat: -rw-r--r-- 25,506 bytes parent folder | download
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
/*******************************************************************************
*                                                                              *
*                                   Viewmol                                    *
*                                                                              *
*                             F E E D B A C K . C                              *
*                                                                              *
*                 Copyright (c) Joerg-R. Hill, December 2000                   *
*                                                                              *
********************************************************************************
*
* $Id: feedback.c,v 1.4 2000/12/10 15:07:06 jrh Exp $
* $Log: feedback.c,v $
* Revision 1.4  2000/12/10 15:07:06  jrh
* Release 2.3
*
* Revision 1.3  1999/05/24 01:25:26  jrh
* Release 2.2.1
*
* Revision 1.2  1999/02/07 21:49:06  jrh
* Release 2.2
*
* Revision 1.1  1998/01/26 00:34:59  jrh
* Initial revision
*
*/  
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "viewmol.h"

/* OpenGL's GL_3D_COLOR feedback vertex format. */
typedef struct _Feedback3Dcolor {
  GLfloat x;
  GLfloat y;
  GLfloat z;
  GLfloat red;
  GLfloat green;
  GLfloat blue;
  GLfloat alpha;
} Feedback3Dcolor;

typedef struct _Feedback3DindexedColor {
  GLfloat x;
  GLfloat y;
  GLfloat z;
  GLfloat color;
} Feedback3DindexedColor;

typedef struct _DepthIndex {
  GLfloat *ptr;
  GLfloat depth;
} DepthIndex;

void (*setRGBColor)(FILE *, GLfloat, GLfloat, GLfloat);
void (*moveto)(FILE *, int, GLfloat, GLfloat);
void (*lineto)(FILE *, int, GLfloat, GLfloat);
void (*triangle)(FILE *, GLfloat, GLfloat, GLfloat, GLfloat, GLfloat, GLfloat);
void (*triangleColor)(FILE *, GLfloat, GLfloat, GLfloat);
void (*point)(FILE *, int, GLfloat, GLfloat);
void (*lineStipple)(FILE *, GLushort);
void (*showString)(FILE *, int, GLfloat, GLfloat);

void feedbackString(char *, double, double, double, double, GLuint);
void feedbackLineStipple(GLint, GLushort);
void feedbackLineWidth(GLfloat);
void feedbackDisable(GLenum);
double visibility(GLfloat *);

extern void PostscriptSetRGBColor(FILE *, GLfloat, GLfloat, GLfloat);
extern void PostscriptMoveto(FILE *, int, GLfloat, GLfloat);
extern void PostscriptLineto(FILE *, int, GLfloat, GLfloat);
extern void PostscriptTriangle(FILE *, GLfloat, GLfloat, GLfloat, GLfloat, GLfloat, GLfloat);
extern void PostscriptTriangleColor(FILE *, GLfloat, GLfloat, GLfloat);
extern void PostscriptPoint(FILE *, int, GLfloat, GLfloat);
extern void PostscriptLineStipple(FILE *, GLushort);
extern void PostscriptShowString(FILE *, int, GLfloat, GLfloat);

extern void hpglSetRGBColor(FILE *, GLfloat, GLfloat, GLfloat);
extern void hpglMoveto(FILE *, int, GLfloat, GLfloat);
extern void hpglLineto(FILE *, int, GLfloat, GLfloat);
extern void hpglTriangle(FILE *, GLfloat, GLfloat, GLfloat, GLfloat, GLfloat, GLfloat);
extern void hpglTriangleColor(FILE *, GLfloat, GLfloat, GLfloat);
extern void hpglPoint(FILE *, int, GLfloat, GLfloat);
extern void hpglLineStipple(FILE *, GLushort);
extern void hpglShowString(FILE *, int, GLfloat, GLfloat);

extern void *getmem(size_t, size_t);
extern void fremem(void **);
extern void getRGBColor(Widget, Pixel, GLfloat *, GLfloat *, GLfloat *);
extern void vectorProduct(double *, double *, double *);
extern double scalarProduct(double *, double *);

extern struct WINDOW windows[];
extern int rgbMode;
extern char *text, *textPointer;

static GLfloat lastred, lastgreen, lastblue;

GLfloat *writePrimitives(FILE *file, GLfloat *loc, int window, int format, int portrait)
{
  int token;
  int nvertices, i;
  GLfloat red, green, blue, alpha=0.0, dr, dg, db;
  int smooth, triOffset;
  Feedback3Dcolor *vertexC=(Feedback3Dcolor *)0;
  Feedback3DindexedColor *vertexI=(Feedback3DindexedColor *)0;

  token=*loc;
  loc++;
  switch (token)
  {
    case GL_LINE_RESET_TOKEN:
    case GL_LINE_TOKEN:         if (rgbMode)
                                {
                                  vertexC=(Feedback3Dcolor *)loc;
                                  if (vertexC[1].alpha != 1.0)
                                    (*setRGBColor)(file, vertexC[1].alpha, vertexC[1].alpha, vertexC[1].alpha);
                                  else
                                    (*setRGBColor)(file, vertexC[1].red, vertexC[1].green, vertexC[1].blue);
                                  (*moveto)(file, portrait, vertexC[0].x, vertexC[0].y);
                                  (*lineto)(file, portrait, vertexC[1].x, vertexC[1].y);
                                  loc+=14; /* Each vertex element in the feedback buffer is 7 GLfloats. */
                                }
                                else
                                {
                                  vertexI=(Feedback3DindexedColor *)loc;
                                  getRGBColor(windows[window].widget, (Pixel)vertexI[1].color, &red, &green, &blue);
                                  (*setRGBColor)(file, red, green, blue);
                                  (*moveto)(file, portrait, vertexI[0].x, vertexI[0].y);
                                  (*lineto)(file, portrait, vertexI[1].x, vertexI[1].y);
                                  loc+=8;  /* Each vertex element in the feedback buffer is 4 GLfloats. */
                                }
                                if (format == POSTSCRIPT) fprintf(file, "stroke\n");
                                break;
    case GL_POLYGON_TOKEN:      nvertices=*loc;
                                loc++;
                                if (nvertices > 0)
                                {
                                  if (rgbMode)
                                  {
                                    vertexC=(Feedback3Dcolor *)loc;
                                    red=vertexC[0].red;
                                    green=vertexC[0].green;
                                    blue=vertexC[0].blue;
                                    alpha=vertexC[0].alpha;
                                  }
                                  else
                                  {
                                    vertexI=(Feedback3DindexedColor *)loc;
                                    getRGBColor(windows[window].widget, (Pixel)vertexI[0].color, &red, &green, &blue);
                                  }
                                  smooth=FALSE;
                                  if (alpha == 1.0)
                                  {
                                    for (i=1; i<nvertices; i++)
                                    {
                                      if (rgbMode)
                                      {
                                        dr=vertexC[i].red;
                                        dg=vertexC[i].green;
                                        db=vertexC[i].blue;
                                      }
                                      else
                                        getRGBColor(windows[window].widget, (Pixel)vertexI[i].color, &dr, &dg, &db);
                                      if (red != dr || green != dg || blue != db)
                                      {
                                        smooth=TRUE;
                                        break;
                                      }
                                    }
                                  }
                                  else
                                    red=green=blue=alpha;
                                  if (smooth)
                                  {
                                    /* Smooth shaded polygon; varying colors at vertices. */
                                    /* Break polygon into "nvertices-2" triangle fans. */
                                    for (i=0; i<nvertices-2; i++)
                                    {
                                      if (rgbMode)
                                      {
                                        triOffset=i*7;
                                        (*triangle)(file, vertexC[0].x, vertexC[i+1].x, vertexC[i+2].x,
                                                    vertexC[0].y, vertexC[i+1].y, vertexC[i+2].y);
                                        (*triangleColor)(file, vertexC[0].red, vertexC[0].green, vertexC[0].blue);
                                        (*triangleColor)(file, vertexC[i+1].red, vertexC[i+1].green, vertexC[i+1].blue);
                                        (*triangleColor)(file, vertexC[i+2].red, vertexC[i+2].green, vertexC[i+2].blue);
                                      }
                                      else
                                      {
                                        triOffset=i*4;
                                        (*triangle)(file, vertexI[0].x, vertexI[i+1].x, vertexI[i+2].x,
                                                    vertexI[0].y, vertexI[i+1].y, vertexI[i+2].y);
                                        getRGBColor(windows[window].widget, (Pixel)vertexI[0].color, &red, &green, &blue);
                                        (*triangleColor)(file, red, green, blue);
                                        getRGBColor(windows[window].widget, (Pixel)vertexI[i+1].color, &red, &green, &blue);
                                        (*triangleColor)(file, red, green, blue);
                                        getRGBColor(windows[window].widget, (Pixel)vertexI[i+2].color, &red, &green, &blue);
                                        (*triangleColor)(file, red, green, blue);
                                      }
                                    }
                                    lastred=lastgreen=lastblue=(-1.);
                                  }
                                  else
                                  {
                                    /* Flat shaded polygon; all vertex colors the same. */
                                    if (format == POSTSCRIPT)
                                      fprintf(file, "newpath\n");
                                    (*setRGBColor)(file, red, green, blue);

                                    /* Draw a filled polygon. */
                                    if (rgbMode)
                                      (*moveto)(file, portrait, vertexC[0].x, vertexC[0].y);
                                    else
                                      (*moveto)(file, portrait, vertexI[0].x, vertexI[0].y);
                                    for (i=1; i<nvertices; i++)
                                    {
                                      if (rgbMode)
                                        (*lineto)(file, portrait, vertexC[i].x, vertexC[i].y);
                                      else
                                        (*lineto)(file, portrait, vertexI[i].x, vertexI[i].y);
                                    }
                                    if (format == POSTSCRIPT)
                                      fprintf(file, "closepath fill\n");
                                  }
                                }
                                if (rgbMode)
                                  loc+=nvertices*7;  /* Each vertex element in the
                                                        feedback buffer is 7 GLfloats. */
                                else
                                  loc+=nvertices*4;  /* Each vertex element in the
                                                          feedback buffer is 4 GLfloats. */
                                break;
    case GL_POINT_TOKEN:        if (rgbMode)
                                {
                                  vertexC=(Feedback3Dcolor *)loc;
                                  if (vertexC[0].alpha != 1.0)
                                    (*setRGBColor)(file, vertexC[0].alpha, vertexC[0].alpha, vertexC[0].alpha);
                                  else
                                    (*setRGBColor)(file, vertexC[0].red, vertexC[0].green, vertexC[0].blue);
                                  (*point)(file, portrait, vertexC[0].x, vertexC[0].y);
                                  loc+=7;           /* Each vertex element in the feedback
                                                       buffer is 7 GLfloats. */
                                }
                                else
                                {
                                  vertexI=(Feedback3DindexedColor *)loc;
                                  getRGBColor(windows[window].widget, (Pixel)vertexI[0].color, &red, &green, &blue);
                                  (*setRGBColor)(file, red, green, blue);
                                    (*point)(file, portrait, vertexI[0].x, vertexI[0].y);
                                  loc+=4;           /* Each vertex element in the feedback
                                                       buffer is 4 GLfloats. */
                                }
                                break;
    case GL_BITMAP_TOKEN:
    case GL_COPY_PIXEL_TOKEN:
    case GL_DRAW_PIXEL_TOKEN:   if (rgbMode)
                                {
                                  vertexC=(Feedback3Dcolor *)loc;
                                  if (vertexC[0].alpha != 1.0)
                                    (*setRGBColor)(file, vertexC[0].alpha, vertexC[0].alpha, vertexC[0].alpha);
                                  else
                                    (*setRGBColor)(file, vertexC[0].red, vertexC[0].green, vertexC[0].blue);
                                  (*showString)(file, portrait, vertexC[0].x, vertexC[0].y);
                                  loc+=7;
                                }
                                else
                                {
                                  vertexI=(Feedback3DindexedColor *)loc;
                                  getRGBColor(windows[window].widget, (Pixel)vertexI[0].color, &red, &green, &blue);
                                  (*setRGBColor)(file, red, green, blue);
                                  (*showString)(file, portrait, vertexI[0].x, vertexI[0].y);
                                  loc+=4;
                                }
                                break;
    case GL_PASS_THROUGH_TOKEN: i=((int)(*loc) & 0xffff0000);
                                switch (i)
                                {
                                  case LINE_STIPPLE: (*lineStipple)(file, (GLushort)(*loc) & 0xffff);
                                                     break;
                                  case LINE_WIDTH:   if (format == POSTSCRIPT)
                                                       fprintf(file, "%d setlinewidth\n", (int)(*loc) & 0xffff);
                                                     break;
                                }
                                loc+=1;
                                break;
    default:                    /* If we arrive here OpenGL has been changed. */
                                break;
  }
  return(loc);
}

static int compare(const void *a, const void *b)
{
  DepthIndex *p1=(DepthIndex *)a;
  DepthIndex *p2=(DepthIndex *)b;
  GLfloat diff=p2->depth-p1->depth;

  if (diff > 0.0)
    return(1);
  else if (diff < 0.0)
    return(-1);
  else
    return(0);
}

void writeFeedback(FILE * file, GLint size, GLfloat *buffer, int window,
                   int format, int smooth, int portrait)
{
  GLfloat *loc, *end;
  Feedback3Dcolor *vertexC;
  Feedback3DindexedColor *vertexI;
  GLfloat depthSum, visible, addDepth=0.0, textDepth=0.0;
  DepthIndex *prims;
  int token, nprimitives, item, checkVisibility=TRUE;
  int nvertices;
  register int i;

  switch (format)
  {
    case HPGL:       setRGBColor=hpglSetRGBColor;
                     moveto=hpglMoveto;
                     lineto=hpglLineto;
                     triangle=hpglTriangle;
                     triangleColor=hpglTriangleColor;
                     point=hpglPoint;
                     lineStipple=hpglLineStipple;
                     showString=hpglShowString;
                     break;
    case POSTSCRIPT: setRGBColor=PostscriptSetRGBColor;
                     moveto=PostscriptMoveto;
                     lineto=PostscriptLineto;
                     triangle=PostscriptTriangle;
                     triangleColor=PostscriptTriangleColor;
                     point=PostscriptPoint;
                     lineStipple=PostscriptLineStipple;
                     showString=PostscriptShowString;
                     break;
  }

  end=buffer+size;
  textPointer=text;
  lastred=lastgreen=lastblue=(-1.);

  /* Count how many primitives there are. */
  nprimitives=0;
  loc=buffer;
  if (smooth)
  {
    while (loc < end)
    {
      token = *loc;
      loc++;
      switch (token)
      {
        case GL_LINE_TOKEN:
        case GL_LINE_RESET_TOKEN:   if (rgbMode) loc+=14;
                                    else         loc+=8;
                                    nprimitives++;
                                    break;
        case GL_POLYGON_TOKEN:      nvertices=*loc;
                                    loc++;
                                    if (rgbMode) loc+=(7*nvertices);
                                    else         loc+=(4*nvertices);
                                    nprimitives++;
                                    break;
        case GL_POINT_TOKEN:        if (rgbMode) loc+=7;
                                    else         loc+=4;
                                    nprimitives++;
                                    break;
        case GL_BITMAP_TOKEN:
        case GL_COPY_PIXEL_TOKEN:
        case GL_DRAW_PIXEL_TOKEN:   if (rgbMode)
                                      loc+=7;
                                    else
                                      loc+=4;
                                    nprimitives++;
                                    break;
        case GL_PASS_THROUGH_TOKEN: loc+=1;
                                    nprimitives++;
                                    break;
        default:                    /* If we arrive here OpenGL has been changed. */
                                      break;
      }
    }

    /* Allocate an array of pointers that will point back at
       primitives in the feedback buffer.  There will be one
       entry per primitive.  This array is also where we keep the
       primitive's average depth.  There is one entry per
       primitive  in the feedback buffer. */
    prims=(DepthIndex *)getmem(nprimitives, sizeof(DepthIndex));

    item=0;
    loc=buffer;
    while (loc < end)
    {
      prims[item].ptr=loc;  /* Save this primitive's location. */
      token=*loc;
      loc++;
      visible=(-1.0);
      switch (token)
      {
        case GL_LINE_TOKEN:
        case GL_LINE_RESET_TOKEN:   if (rgbMode)
                                    {
                                      vertexC=(Feedback3Dcolor *)loc;
                                      depthSum=vertexC[0].z+vertexC[1].z;
                                      loc+=14;
                                    }
                                    else
                                    {
                                      vertexI=(Feedback3DindexedColor *)loc;
                                      depthSum=vertexI[0].z+vertexI[1].z;
                                      loc+=8;
                                    }
                                    prims[item].depth=depthSum*0.5+addDepth;
                                    break;
        case GL_POLYGON_TOKEN:      nvertices=*loc;
                                    loc++;
                                    if (checkVisibility) visible=visibility(loc);
                                    if (rgbMode)
                                    {
                                      vertexC=(Feedback3Dcolor *)loc;
                                      depthSum=vertexC[0].z;
                                      for (i=1; i<nvertices; i++)
                                        depthSum+=vertexC[i].z;
                                      loc+=(7*nvertices);
                                    }
                                    else
                                    {
                                      vertexI=(Feedback3DindexedColor *)loc;
                                      depthSum=vertexI[0].z;
                                      for (i=1; i<nvertices; i++)
                                        depthSum+=vertexI[i].z;
                                      loc+=(4*nvertices);
                                    }
                                    prims[item].depth=depthSum/nvertices+addDepth;
                                    break;
        case GL_POINT_TOKEN:        if (rgbMode)
                                    {
                                      vertexC=(Feedback3Dcolor *)loc;
                                      prims[item].depth=vertexC[0].z+addDepth;
                                      loc+=7;
                                    }
                                    else
                                    {
                                      vertexI=(Feedback3DindexedColor *)loc;
                                      prims[item].depth=vertexI[0].z+addDepth;
                                      loc+=4;
                                    }
                                    break;
        case GL_BITMAP_TOKEN:
        case GL_COPY_PIXEL_TOKEN:
        case GL_DRAW_PIXEL_TOKEN:   if (rgbMode)
                                    {
                                      vertexC=(Feedback3Dcolor *)loc;
                                      prims[item].depth=textDepth+addDepth;
                                      textDepth-=1.0;
                                      loc+=7;
                                    }
                                    else
                                    {
                                      vertexI=(Feedback3DindexedColor *)loc;
                                      prims[item].depth=textDepth+addDepth;
                                      textDepth-=1.0;
                                      loc+=4;
                                    }
                                    break;
        case GL_PASS_THROUGH_TOKEN: i=(int)(*loc) & 0xffff0000; 
                                    switch (i)
                                    {
                                      case ADD_DEPTH:  addDepth=(GLfloat)((int)(*loc) & 0xffff);
                                                        break;
                                      case VISIBILITY: checkVisibility=!checkVisibility;
                                                       break;
                                    }
                                    loc+=1;
                                    break;
        default:                    /* If we arrive here OpenGL has been changed. */
                                    break;
      }
      if (token == GL_POLYGON_TOKEN)
      {
        if (visible < 0.0) item++;
      }
      else
        item++;
    }
    nprimitives=item;

    /* Sort the primitives back to front. */
    qsort(prims, nprimitives, sizeof(DepthIndex), compare);

    /* Understand that sorting by a primitives average depth
       doesn't allow us to disambiguate some cases like self
       intersecting polygons.  Handling these cases would require
       breaking up the primitives.  That's too involved for this
       example.  Sorting by depth is good enough for lots of
       applications. */

    /* Emit the Encapsulated PostScript for the primitives in
       back to front order. */
    for (item=0; item<nprimitives; item++) {
      (void)writePrimitives(file, prims[item].ptr, window, format, portrait);
    }

    fremem((void *)&prims);
  } /* end of if (smooth) */
  else
  {
    while (loc < end)
      loc=writePrimitives(file, loc, window, format, portrait);
  }
}

void feedbackString(char *s, double x, double y, double z, double offset, GLuint dummy)
{
  GLubyte bitmap[] = {0};

  glRasterPos3d(x, y, z);
  glBitmap(1, 1, 1, 1, 1, 1, bitmap);
  if (offset == 0.0)
    strcpy(textPointer, "l");
  else
    strcpy(textPointer, "r");
  textPointer++;
  while (*s == ' ') s++;
  strcat(textPointer, s);
  textPointer+=strlen(s)+1;
}

void feedbackLineStipple(GLint dummy, GLushort pattern)
{
  glPassThrough((GLfloat)(LINE_STIPPLE + pattern));
}

void feedbackLineWidth(GLfloat width)
{
  glPassThrough((GLfloat)(LINE_WIDTH + width));
}

void feedbackDisable(GLenum what)
{
  if (what == GL_LINE_STIPPLE)
  {
    glPassThrough((GLfloat)(LINE_STIPPLE + 0xffff));
  }
}

double visibility(GLfloat *loc)
{
  Feedback3Dcolor *polygonC;
  Feedback3DindexedColor *polygonI;
  double v1[3], v2[3], n[3];

  if (rgbMode)
  {
    polygonC=(Feedback3Dcolor *)loc;
    v1[0]=polygonC[1].x-polygonC[0].x;
    v1[1]=polygonC[1].y-polygonC[0].y;
    v1[2]=polygonC[1].z-polygonC[0].z;
    v2[0]=polygonC[2].x-polygonC[1].x;
    v2[1]=polygonC[2].y-polygonC[1].y;
    v2[2]=polygonC[2].z-polygonC[1].z;
  }
  else
  {
    polygonI=(Feedback3DindexedColor *)loc;
    v1[0]=polygonI[1].x-polygonI[0].x;
    v1[1]=polygonI[1].y-polygonI[0].y;
    v1[2]=polygonI[1].z-polygonI[0].z;
    v2[0]=polygonI[2].x-polygonI[1].x;
    v2[1]=polygonI[2].y-polygonI[1].y;
    v2[2]=polygonI[2].z-polygonI[1].z;
  }
  vectorProduct(v1, v2, n);
  return(-n[2]/sqrt(scalarProduct(n, n)));
}