1 /*****************************************************************************
2 FILE : $Source: /projects/higgs1/SNNS/CVS/SNNS/xgui/sources/d3_graph.c,v $
3 SHORTNAME : graph.c
4 SNNS VERSION : 4.2
5
6 PURPOSE : routines for polygons and colors
7 NOTES :
8
9 AUTHOR : Ralf Huebner
10 DATE : 1.12.1991
11
12 CHANGED BY : Sven Doering
13 RCS VERSION : $Revision: 2.8 $
14 LAST CHANGE : $Date: 1998/03/13 16:31:46 $
15
16 Copyright (c) 1990-1995 SNNS Group, IPVR, Univ. Stuttgart, FRG
17 Copyright (c) 1996-1998 SNNS Group, WSI, Univ. Tuebingen, FRG
18
19 ******************************************************************************/
20 #include <config.h>
21
22 #include <stdio.h>
23 #ifdef HAVE_VALUES_H
24 #include <values.h>
25 #endif
26 #include <math.h>
27
28 #include <X11/Xlib.h>
29 #include <X11/X.h>
30 #include <X11/Xutil.h>
31 #include <X11/Xos.h>
32 #include <X11/Intrinsic.h>
33
34 #include "ui.h"
35
36 #include "ui_mainP.h"
37 #include "ui_event.h"
38 #include "ui_color.h"
39 #include "d3_global.h"
40 #include "d3_zgraph.h"
41 #include "d3_point.h"
42
43 #include "d3_graph.ph"
44
45
46
47
48 /*****************************************************************************
49 FUNCTION : incrementalize_y
50
51 PURPOSE : increment y
52 RETURNS :
53 NOTES :
54
55 ******************************************************************************/
56
57
incrementalize_y(register float * p1,register float * p2,register float * p,register float * dp,int y,register int mask)58 static void incrementalize_y (register float *p1, register float *p2, register float *p, register float *dp, int y, register int mask)
59 {
60 float dy, frac;
61
62 dy = *(*(vector *)p2+1) - *(*(vector *)p1+1);
63 if (dy==0.0) dy = 1.0;
64 frac = y + 0.5 - *(*(vector *)p1+1);
65
66 for (; mask!=0; mask>>=1, p1++, p2++, p++, dp++)
67 if (mask&1) {
68 *dp = (*p2-*p1) / dy;
69 *p = *p1 + *dp * frac;
70 }
71 }
72
73
74
75 /*****************************************************************************
76 FUNCTION : incrementalize_x
77
78 PURPOSE : increment x
79 RETURNS :
80 NOTES :
81
82 ******************************************************************************/
83
84
85
incrementalize_x(register float * p1,register float * p2,register float * p,register float * dp,int x,register int mask)86 static void incrementalize_x (register float *p1, register float *p2, register float *p, register float *dp, int x, register int mask)
87 {
88 float dx, frac;
89
90 dx = **(vector *)p2 - **(vector *)p1;
91 if (dx==0.0) dx = 1.0;
92 frac = x + 0.5 - **(vector *)p1;
93
94 for (; mask!=0; mask>>=1, p1++, p2++, p++, dp++)
95 if (mask&1) {
96 *dp = (*p2-*p1) / dx;
97 *p = *p1 + *dp * frac;
98 }
99 }
100
101
102
103 /*****************************************************************************
104 FUNCTION : increment
105
106 PURPOSE : increment a masked value
107 RETURNS :
108 NOTES :
109
110 ******************************************************************************/
111
112
increment(register float * p,register float * dp,register int mask)113 static void increment (register float *p, register float *dp, register int mask)
114 {
115 for (; mask!=0; mask>>=1, p++, dp++)
116 if (mask&1)
117 *p += *dp;
118 }
119
120
121
122
123 /*****************************************************************************
124 FUNCTION : put_pixel
125
126 PURPOSE : draw a z-buffered pixel
127 RETURNS :
128 NOTES :
129
130 ******************************************************************************/
131
132
133
put_pixel(int px,int py,vector point)134 static void put_pixel (int px, int py, vector point)
135 {
136 float zp;
137
138 d3_readZbuffer (px, py, &zp);
139 if (point[2] < zp) {
140 d3_putColPixel (px, py);
141 d3_writeZbuffer (px, py, point[2]);
142 }
143 }
144
145
146
147
148 /*****************************************************************************
149 FUNCTION : scanline
150
151 PURPOSE : scan a horizontal line in a polygon
152 RETURNS :
153 NOTES :
154
155 ******************************************************************************/
156
157
158
scanline(int y,vector l,vector r,int mask)159 static void scanline (int y, vector l, vector r, int mask)
160 {
161 int x, lx, rx;
162 vector p, dp;
163
164 mask &= ~POLY_MASK(0);
165 lx = ceil ((*l) - 0.5);
166 if (lx<d3_clipWindow.x0) lx = d3_clipWindow.x0;
167 rx = floor ((*r) - 0.5);
168 if (rx>d3_clipWindow.x1) rx = d3_clipWindow.x1;
169 if (lx>rx) return;
170 incrementalize_x (l, r, p, dp, lx, mask);
171 for (x=lx; x<=rx; x++) {
172 put_pixel (x, y, p);
173 increment (p, dp, mask);
174 }
175 }
176
177
178
179
180 /*****************************************************************************
181 FUNCTION : d3_drawPoly
182
183 PURPOSE : draw a zbuffered polygon
184 RETURNS :
185 NOTES :
186
187 ******************************************************************************/
188
189
190
d3_drawPoly(register d3_polygon_type * p)191 void d3_drawPoly (register d3_polygon_type *p)
192 {
193 register int i, li, ri, y, ly, ry, top, rem, mask;
194 float ymin;
195 vector l, r, dl, dr;
196
197 p->mask = POLY_MASK(0) | POLY_MASK(1) | POLY_MASK(2);
198
199 ymin = MAXFLOAT;
200 for (i=0; i<p->n; i++) { /* find top vertex */
201 if (p->vert[i][1] < ymin) {
202 ymin = (p->vert[i])[1];
203 top = i;
204 }
205 }
206 li = ri = top; /* left and right vertex indices */
207 rem = p->n; /* number of vertices remaining */
208 y = ceil (ymin - 0.5); /* current scanline */
209 ly = ry = y-1; /* lower end of left & right edges */
210 mask = p->mask & ~POLY_MASK(1); /* stop interpolating screen y */
211
212 while (rem > 0) {
213 while (ly<=y && rem>0) {
214 rem--;
215 i = li-1;
216 if (i<0) i = p->n-1;
217 incrementalize_y (p->vert[li], p->vert[i], l, dl, y, mask);
218 ly = floor (p->vert[i][1] + 0.5);
219 li = i;
220 }
221 while (ry<=y && rem>0) {
222 rem--;
223 i = ri+1;
224 if (i>=p->n) i = 0;
225 incrementalize_y (p->vert[ri], p->vert[i], r, dr, y, mask);
226 ry = floor (p->vert[i][1] + 0.5);
227 ri = i;
228 }
229
230
231 while (y<ly && y<ry) {
232 if (y>=d3_clipWindow.y0 && y<=d3_clipWindow.y1)
233 if (*l <= *r)
234 scanline (y, l, r, mask);
235 else
236 scanline (y, r, l, mask);
237 y++;
238 increment (l, dl, mask);
239 increment (r, dr, mask);
240 }
241 }
242 }
243
244
245
246
247
248 /*****************************************************************************
249 FUNCTION : d3_setClipWindow
250
251 PURPOSE : sets the clipping window for the polygon routines
252 RETURNS :
253 NOTES :
254
255 ******************************************************************************/
256
257
258
d3_setClipWindow(int x0,int y0,int x1,int y1)259 void d3_setClipWindow (int x0, int y0, int x1, int y1)
260 {
261 d3_clipWindow.x0 = x0;
262 d3_clipWindow.x1 = x1;
263 d3_clipWindow.y0 = y0;
264 d3_clipWindow.y1 = y1;
265 }
266
267
268
269
270 /*****************************************************************************
271 FUNCTION : d3_drawLine
272
273 PURPOSE : draw a line
274 RETURNS :
275 NOTES :
276
277 ******************************************************************************/
278
279
280
281
d3_drawLine(int x1,int y1,int x2,int y2)282 void d3_drawLine (int x1, int y1, int x2, int y2)
283 {
284 XDrawLine(d3_display, d3_window, d3_gc, x1, y1, x2 ,y2);
285 }
286
287
288
289
290 /*****************************************************************************
291 FUNCTION : d3_intens_to_grayval
292
293 PURPOSE : converts the polygon light intensity in a palette gray value
294 RETURNS : palette index
295 NOTES :
296
297 ******************************************************************************/
298
299
300
d3_intens_to_grayval(float intens)301 int d3_intens_to_grayval (float intens)
302 {
303 return (floor ((1.0 - intens) * (float) MAXCOLSTEPS));
304 }
305
306
307
308
309
310 /*****************************************************************************
311 FUNCTION : d3_value_to_color
312
313 PURPOSE : converts the polygon light intensity in a palette color value
314 RETURNS : palette index
315 NOTES :
316 UPDATE : 3.3.93 shading for activation
317
318 ******************************************************************************/
319
320
d3_value_to_color(float value)321 int d3_value_to_color (float value)
322 {
323 return ((int) ((1.0 + value) * ((float) MAXCOLSTEPS - 3.0)));
324 }
325
326
327
328
329 /*****************************************************************************
330 FUNCTION : d3_setColor
331
332 PURPOSE : sets the drawing color
333 RETURNS :
334 NOTES :
335
336 ******************************************************************************/
337
338
d3_setColor(long unsigned int color)339 void d3_setColor (long unsigned int color)
340 {
341 XSetForeground(d3_display, d3_gc,
342 ui_col_rangePixels[color]);
343 }
344
345
346
347 /*****************************************************************************
348 FUNCTION : d3_setBlackColor
349
350 PURPOSE : sets the drawing color to black
351 RETURNS :
352 NOTES :
353
354 ******************************************************************************/
355
356
357
d3_setBlackColor(void)358 void d3_setBlackColor (void)
359 {
360 XSetForeground(d3_display, d3_gc,
361 BlackPixel (d3_display, d3_screen));
362 }
363
364
365
366 /*****************************************************************************
367 FUNCTION : d3_setLinkColor
368
369 PURPOSE : calculates the link color from a value
370 RETURNS :
371 NOTES :
372
373 ******************************************************************************/
374
375
376
d3_setLinkColor(float * weight)377 void d3_setLinkColor (float *weight)
378 {
379 int procent_value;
380 float quotient;
381
382 if (*weight >= 0.0) {
383 if (d3_state.link_scale != 0.0)
384 {
385 quotient = fabs((*weight) / d3_state.link_scale);
386 if (quotient < 1.0)
387 procent_value = (int)(100.0 * quotient);
388 else
389 procent_value = 100;
390 }
391 else
392 procent_value = 100;
393 XSetForeground (d3_display, d3_gc, ui_col_rangePixels[MAXCOLSTEPS +
394 procent_value * MAXCOLSTEPS / 100]);
395 } else {
396 if (d3_state.link_scale != 0.0)
397 {
398 quotient = fabs((*weight) / d3_state.link_scale);
399 if (quotient < 1.0)
400 procent_value = (int)(100.0 * quotient);
401 else
402 procent_value = 100;
403 }
404 else
405 procent_value = 100;
406 XSetForeground (d3_display, d3_gc, ui_col_rangePixels[MAXCOLSTEPS -
407 procent_value * MAXCOLSTEPS / 100]);
408 }
409 }
410
411
412
413
414 /*****************************************************************************
415 FUNCTION : d3_clearDisplay
416
417 PURPOSE : clears the display window
418 RETURNS :
419 NOTES :
420
421 ******************************************************************************/
422
423
d3_clearDisplay(void)424 void d3_clearDisplay (void)
425 {
426 XClearWindow (d3_display, d3_window);
427 }
428
429
430
431 /*****************************************************************************
432 FUNCTION : d3_getRootSizes
433
434 PURPOSE : gets the dimensions of the root window
435 RETURNS : width and height
436 NOTES :
437
438 ******************************************************************************/
439
440
d3_getRootSizes(unsigned int * xsize,unsigned int * ysize)441 void d3_getRootSizes (unsigned int *xsize, unsigned int *ysize)
442 {
443 Window root;
444 int x, y;
445 unsigned int width, height, border, depth;
446 Status status;
447
448 status = XGetGeometry (ui_display, DefaultRootWindow (ui_display), &root,
449 &x, &y, &width, &height, &border, &depth);
450 *xsize = width;
451 *ysize = height;
452 }
453
454
455
456
457 /* end of file */
458 /* lines: 512 */
459