File: r_path.c

package info (click to toggle)
plotutils 2.4.1-11
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 11,676 kB
  • ctags: 6,967
  • sloc: ansic: 76,305; sh: 15,172; cpp: 12,403; yacc: 2,604; makefile: 888; lex: 144
file content (445 lines) | stat: -rw-r--r-- 12,657 bytes parent folder | download | duplicates (3)
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
/* This file contains the internal paint_path() and paint_paths() methods,
   which the public method endpath() is a wrapper around. */

/* This file contains the internal path_is_flushable() method, which is
   invoked after any path segment is added to the segment list, provided
   (0) the segment list has become greater than or equal to the
   `max_unfilled_path_length' Plotter parameter, (1) the path isn't to be
   filled.  In most Plotters, this operation simply returns true. */

/* This file also contains the internal maybe_prepaint_segments() method.
   It is called immediately after any segment is added to a path.  Some
   Plotters, at least under some circumstances, treat endpath() as a no-op.
   Instead, they plot the segments of a path in real time.  */

#include "sys-defines.h"
#include "extern.h"

/* for Cohen-Sutherland clipper, see g_clipper.c */
enum { ACCEPTED = 0x1, CLIPPED_FIRST = 0x2, CLIPPED_SECOND = 0x4 };

/* forward references */
static void _emit_regis_vector ____P((plIntPoint istart, plIntPoint iend, bool skip_null, char *tmpbuf));

void
#ifdef _HAVE_PROTOS
_r_paint_path (S___(Plotter *_plotter))
#else
_r_paint_path (S___(_plotter))
     S___(Plotter *_plotter;)
#endif
{
  char tmpbuf[32];

  if (_plotter->drawstate->pen_type == 0
      && _plotter->drawstate->fill_type == 0)
    /* nothing to draw */
    return;

  switch ((int)_plotter->drawstate->path->type)
    {
    case (int)PATH_SEGMENT_LIST:
      {
	int i;

	/* sanity checks */
	if (_plotter->drawstate->path->num_segments == 0)/* nothing to do */
	  break;
	if (_plotter->drawstate->path->num_segments == 1) /*shouldn't happen */
	  break;

	if (_plotter->drawstate->fill_type)
	  /* fill the path */
	  {
	    bool within_display = true;

	    /* are all juncture points contained within the ReGIS display? */
	    for (i = 0; i < _plotter->drawstate->path->num_segments; i++)
	      {
		double x, y;
		int i_x, i_y;
		
		x = XD(_plotter->drawstate->path->segments[i].p.x,
		       _plotter->drawstate->path->segments[i].p.y);
		y = YD(_plotter->drawstate->path->segments[i].p.x,
		       _plotter->drawstate->path->segments[i].p.y);
		i_x = IROUND(x);
		i_y = IROUND(y);
		if (i_x < REGIS_DEVICE_X_MIN
		    || i_x > REGIS_DEVICE_X_MAX
		    || i_y < REGIS_DEVICE_Y_MIN
		    || i_y > REGIS_DEVICE_Y_MAX)
		  {
		    within_display = false;
		    break;
		  }
	      }

	    if (within_display)
	      /* can fill path using ReGIS primitives */
	      {
		double x, y;
		plIntPoint first, oldpoint, newpoint;

		_r_set_fill_color (S___(_plotter));
		x = XD(_plotter->drawstate->path->segments[0].p.x,
		       _plotter->drawstate->path->segments[0].p.y);
		y = YD(_plotter->drawstate->path->segments[0].p.x,
			     _plotter->drawstate->path->segments[0].p.y);
		first.x = IROUND(x);
		first.y = IROUND(y);
		_regis_move (R___(_plotter) first.x, first.y); /* use P[..] */

		_write_string (_plotter->data, "F(");
		_write_string (_plotter->data, "V");

		oldpoint = first;
		for (i = 1; i < _plotter->drawstate->path->num_segments; i++)
		  {
		    x = XD(_plotter->drawstate->path->segments[i].p.x,
			   _plotter->drawstate->path->segments[i].p.y);
		    y = YD(_plotter->drawstate->path->segments[i].p.x,
			   _plotter->drawstate->path->segments[i].p.y);
		    newpoint.x = IROUND(x);
		    newpoint.y = IROUND(y);
		    /* emit vector; omit it if it has zero-length in the
		       integer device frame (unless it's the 1st vector) */
		    _emit_regis_vector (oldpoint, newpoint, 
					i > 1 ? true : false, tmpbuf);
		    _write_string (_plotter->data, tmpbuf);
		    oldpoint = newpoint;
		  }

		/* if path isn't closed, add a vector to close it (ReGIS
		   behaves unreliably if this isn't done) */
		_emit_regis_vector (newpoint, first, true, tmpbuf);
		_write_string (_plotter->data, tmpbuf);

		/* terminate F(V..) command */
		_write_string (_plotter->data, ")\n");
		_plotter->regis_position_is_unknown = true; /* to us */
	      }
	    else
	      /* path extends beyond ReGIS display, so must clip before
		 filling */
	      {
		/* NOT IMPLEMENTED YET */
	      }
	  }

	if (_plotter->drawstate->pen_type)
	  /* edge the path */
	  {
	    bool attributes_set = false;
	    bool path_in_progress = false;

	    for (i = 1; i < _plotter->drawstate->path->num_segments; i++)
	      {
		plPoint start, end; /* endpoints of seg. (in device coors) */
		plIntPoint istart, iend; /* same, quantized to integer */
		int clipval;
		
		/* nominal starting point and ending point for new line
		   segment, in floating point device coordinates */
		start.x = XD(_plotter->drawstate->path->segments[i-1].p.x,
			     _plotter->drawstate->path->segments[i-1].p.y);
		start.y = YD(_plotter->drawstate->path->segments[i-1].p.x,
			     _plotter->drawstate->path->segments[i-1].p.y);
		end.x = XD(_plotter->drawstate->path->segments[i].p.x,
			   _plotter->drawstate->path->segments[i].p.y);
		end.y = YD(_plotter->drawstate->path->segments[i].p.x,
			   _plotter->drawstate->path->segments[i].p.y);

		/* clip line segment to rectangular clipping region in
		   device frame */
		clipval = _clip_line (&start.x, &start.y, &end.x, &end.y,
				      REGIS_DEVICE_X_MIN_CLIP, 
				      REGIS_DEVICE_X_MAX_CLIP,
				      REGIS_DEVICE_Y_MIN_CLIP, 
				      REGIS_DEVICE_Y_MAX_CLIP);
		if (!(clipval & ACCEPTED)) /* line segment is OOB */
		  {
		    if (path_in_progress) /* terminate it */
		      _write_string (_plotter->data, "\n");
		    path_in_progress = false;
		    continue;	/* drop this line segment */
		  }

		if (clipval & CLIPPED_FIRST) /* must move */
		  {
		    if (path_in_progress) /* terminate it */
		      _write_string (_plotter->data, "\n");
		    path_in_progress = false;
		  }

		/* convert clipped starting point, ending point to integer
		   ReGIS coors */
		istart.x = IROUND(start.x);
		istart.y = IROUND(start.y);
		iend.x = IROUND(end.x);
		iend.y = IROUND(end.y);
		
		if (path_in_progress 
		    && istart.x == iend.x && istart.y == iend.y)
		  /* redundant, so drop this line segment */
		  continue;

		if (attributes_set == false)
		  /* will be drawing something, so sync ReGIS line type and
		     set the ReGIS foreground color to be our pen color;
		     this code gets executed the first time we get here */
		  {
		    _r_set_attributes (S___(_plotter));
		    _r_set_pen_color (S___(_plotter));
		    attributes_set = true;
		  }

		if (path_in_progress == false)
		  {
		    /* if necessary, move graphics cursor to first point of
                       line segment, using P command */
		    _regis_move (R___(_plotter) istart.x, istart.y);
		    
		    /* emit op code for V command, to begin polyline */
		    _write_string (_plotter->data, "V");

		    if (iend.x != istart.x || iend.y != istart.y)
		      /* emit V[] command: ensure initial pixel is painted */
			_write_string (_plotter->data, "[]");

		    path_in_progress = true;
		  }
		
		_emit_regis_vector (istart, iend, true, tmpbuf);
		_write_string (_plotter->data, tmpbuf);
		
		/* update our notion of ReGIS's notion of position */
		  _plotter->regis_pos.x = iend.x;
		  _plotter->regis_pos.y = iend.y;
	      }

	    /* entire path has been drawn */
	    if (path_in_progress == true)
	      _write_string (_plotter->data, "\n");
	  }
      }
      break;
      
    case (int)PATH_CIRCLE:
      {
	double xd, yd, radius_d;
	int i_x, i_y, i_radius;		/* center and radius, quantized */
	plPoint pc;
	double radius;
	
	pc = _plotter->drawstate->path->pc;
	radius = _plotter->drawstate->path->radius;

	/* known to be a circle in device frame, so compute center and
           radius in that frame */
	xd = XD(pc.x, pc.y);
	yd = YD(pc.x, pc.y);
	radius_d = sqrt (XDV(radius,0) * XDV(radius,0)
			 + YDV(radius,0) * YDV(radius,0));
	i_x = IROUND(xd);
	i_y = IROUND(yd);
	i_radius = IROUND(radius_d);
	
	if (i_x - i_radius < REGIS_DEVICE_X_MIN
	    || i_x + i_radius > REGIS_DEVICE_X_MAX
	    || i_y - i_radius < REGIS_DEVICE_Y_MIN
	    || i_y + i_radius > REGIS_DEVICE_Y_MAX)
	  /* circle extends beyond edge of display, so polygonalize and
	     recurse */
	  {
	    plPath *oldpath = _plotter->drawstate->path;
	    
	    _plotter->drawstate->path = _flatten_path (oldpath);
	    _plotter->paint_path (S___(_plotter)); /* recursive invocation */
	    _delete_plPath (_plotter->drawstate->path);
	    _plotter->drawstate->path = oldpath;	    
	  }
	else
	  /* circle contained within display, can draw using ReGIS circle
	     primitive */
	  {
	    if (_plotter->drawstate->fill_type)
	      /* fill the circle */
	      {
		_r_set_fill_color (S___(_plotter));

		_regis_move (R___(_plotter) i_x, i_y); /* use P command */
		if (i_radius > 0)
		  {
		    sprintf (tmpbuf, "F(C[+%d])\n", i_radius);
		    _plotter->regis_position_is_unknown = true; /* to us */
		  }
		else
		  sprintf (tmpbuf, "V[]\n");
		_write_string (_plotter->data, tmpbuf);
	      }
	    
	    if (_plotter->drawstate->pen_type)
	      /* edge the circle */
	      {
		_r_set_attributes (S___(_plotter));
		_r_set_pen_color (S___(_plotter));

		_regis_move (R___(_plotter) i_x, i_y); /* use P command */
		if (i_radius > 0)
		  {
		    sprintf (tmpbuf, "C[+%d]\n", i_radius);
		    _plotter->regis_position_is_unknown = true; /* to us */
		  }
		else
		  sprintf (tmpbuf, "V[]\n");
		_write_string (_plotter->data, tmpbuf);
	      }
	  }
      }
      break;

    default:			/* shouldn't happen */
      break;
    }
}

/* A low-level method for moving the graphics cursor of a ReGIS device to
   agree with the Plotter's notion of what the graphics cursor should be.  */

void
#ifdef _HAVE_PROTOS
_regis_move (R___(Plotter *_plotter) int xx, int yy)
#else
_regis_move (R___(_plotter) xx, yy)
     S___(Plotter *_plotter;)
     int xx, yy;
#endif
{
  char tmpbuf[32];
  plIntPoint newpoint;
      
  /* sanity check */
  if (xx < REGIS_DEVICE_X_MIN || xx > REGIS_DEVICE_X_MAX
      || yy < REGIS_DEVICE_Y_MIN || yy > REGIS_DEVICE_Y_MAX)
    return;

  newpoint.x = xx;
  newpoint.y = yy;

  if (_plotter->regis_position_is_unknown)
    {
      sprintf (tmpbuf, "P[%d,%d]\n", xx, yy);
      _write_string (_plotter->data, tmpbuf);
    }
  else if (xx != _plotter->regis_pos.x || yy != _plotter->regis_pos.y)
    {
      _write_string (_plotter->data, "P");
      _emit_regis_vector (_plotter->regis_pos, newpoint, false, tmpbuf);
      _write_string (_plotter->data, tmpbuf);
      _write_string (_plotter->data, "\n");
    }

  /* update our knowledge of cursor position */
  _plotter->regis_position_is_unknown = false;
  _plotter->regis_pos = newpoint;
}

static void
#ifdef _HAVE_PROTOS
_emit_regis_vector (plIntPoint istart, plIntPoint iend, bool skip_null, char *tmpbuf)
#else
_emit_regis_vector (istart, iend, skip_null, tmpbuf)
     plIntPoint istart, iend;
     bool skip_null;
     char *tmpbuf;
#endif
{
  plIntVector v;
  bool xneg = false, yneg = false;
  char xrelbuf[32], yrelbuf[32], xbuf[32], ybuf[32];
  int xrellen, yrellen, xlen, ylen;
  char *x, *y;

  v.x = iend.x - istart.x;
  v.y = iend.y - istart.y;

  /* trivial case */
  if (v.x == 0 && v.y == 0)
    {
      if (skip_null == false)
	sprintf (tmpbuf, "[]");
      else
	*tmpbuf = '\0';		/* empty string */
      return;
    }

  /* compute length of endpoint in terms of characters, when printed in
     relative and absolute coordinates */

  if (v.x < 0)
    {
      xneg = true;
      v.x = -v.x;
    }
  if (v.y < 0)
    {
      yneg = true;
      v.y = -v.y;
    }

  sprintf (xrelbuf, "%s%d", (xneg ? "-" : "+"), v.x);
  xrellen = strlen (xrelbuf);
  sprintf (yrelbuf, "%s%d", (yneg ? "-" : "+"), v.y);
  yrellen = strlen (yrelbuf);

  sprintf (xbuf, "%d", iend.x);
  xlen = strlen (xbuf);
  sprintf (ybuf, "%d", iend.y);
  ylen = strlen (ybuf);
  
  /* use whichever (relative/absolute) is shorter; prefer relative */
  x = (xrellen <= xlen ? xrelbuf : xbuf);
  y = (yrellen <= ylen ? yrelbuf : ybuf);

  /* draw vector: emit point coordinates */
  if (v.x == 0)
    sprintf (tmpbuf, "[,%s]", y);
  else if (v.y == 0)
    sprintf (tmpbuf, "[%s]", x);
  else
    sprintf (tmpbuf, "[%s,%s]", x, y);
}

bool
#ifdef _HAVE_PROTOS
_r_paint_paths (S___(Plotter *_plotter))
#else
_r_paint_paths (S___(_plotter))
     S___(Plotter *_plotter;)
#endif
{
  return false;
}

bool
#ifdef _HAVE_PROTOS
_r_path_is_flushable (S___(Plotter *_plotter))
#else
_r_path_is_flushable (S___(_plotter))
     S___(Plotter *_plotter;)
#endif
{
  return true;
}

void
#ifdef _HAVE_PROTOS
_r_maybe_prepaint_segments (R___(Plotter *_plotter) int prev_num_segments)
#else
_r_maybe_prepaint_segments (R___(_plotter) prev_num_segments)
     S___(Plotter *_plotter;)
     int prev_num_segments;
#endif
{
}