summaryrefslogtreecommitdiffstats
path: root/src/bltGrMarkerPolygon.C
blob: e8c0cb4e30b8f0dada660ae23e571e4ebb715a50 (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
/*
 * Smithsonian Astrophysical Observatory, Cambridge, MA, USA
 * This code has been modified under the terms listed below and is made
 * available under the same terms.
 */

/*
 *	Copyright 1993-2004 George A Howlett.
 *
 *	Permission is hereby granted, free of charge, to any person obtaining
 *	a copy of this software and associated documentation files (the
 *	"Software"), to deal in the Software without restriction, including
 *	without limitation the rights to use, copy, modify, merge, publish,
 *	distribute, sublicense, and/or sell copies of the Software, and to
 *	permit persons to whom the Software is furnished to do so, subject to
 *	the following conditions:
 *
 *	The above copyright notice and this permission notice shall be
 *	included in all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *	LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *	OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *	WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

extern "C" {
#include "bltGraph.h"
};

#include "bltGrMarkerPolygon.h"

using namespace Blt;

// OptionSpecs

static Tk_OptionSpec optionSpecs[] = {
  {TK_OPTION_CUSTOM, "-bindtags", "bindTags", "BindTags", 
   "Polygon all", -1, Tk_Offset(PolygonMarkerOptions, tags), 
   TK_OPTION_NULL_OK, &listObjOption, 0},
  {TK_OPTION_CUSTOM, "-cap", "cap", "Cap", 
   "butt", -1, Tk_Offset(PolygonMarkerOptions, capStyle),
   0, &capStyleObjOption, 0},
  {TK_OPTION_CUSTOM, "-coords", "coords", "Coords",
   NULL, -1, Tk_Offset(PolygonMarkerOptions, worldPts), 
   TK_OPTION_NULL_OK, &coordsObjOption, 0},
  {TK_OPTION_CUSTOM, "-dashes", "dashes", "Dashes",
   NULL, -1, Tk_Offset(PolygonMarkerOptions, dashes), 
   TK_OPTION_NULL_OK, &dashesObjOption, 0},
  {TK_OPTION_STRING, "-element", "element", "Element", 
   NULL, -1, Tk_Offset(PolygonMarkerOptions, elemName),
   TK_OPTION_NULL_OK, NULL, 0},
  {TK_OPTION_COLOR, "-fill", "fill", "Fill", 
   NULL, -1, Tk_Offset(PolygonMarkerOptions, fill),
   TK_OPTION_NULL_OK, NULL, 0},
  {TK_OPTION_COLOR, "-fillbg", "fillbg", "FillBg", 
   NULL, -1, Tk_Offset(PolygonMarkerOptions, fillBg),
   TK_OPTION_NULL_OK, NULL, 0},
  {TK_OPTION_CUSTOM, "-join", "join", "Join", 
   "miter", -1, Tk_Offset(PolygonMarkerOptions, joinStyle),
   0, &joinStyleObjOption, 0},
  {TK_OPTION_PIXELS, "-linewidth", "lineWidth", "LineWidth",
   "1", -1, Tk_Offset(PolygonMarkerOptions, lineWidth), 0, NULL, 0},
  {TK_OPTION_BOOLEAN, "-hide", "hide", "Hide", 
   "no", -1, Tk_Offset(PolygonMarkerOptions, hide), 0, NULL, 0},
  {TK_OPTION_CUSTOM, "-mapx", "mapX", "MapX",
   "x", -1, Tk_Offset(PolygonMarkerOptions, axes.x), 0, &xAxisObjOption, 0},
  {TK_OPTION_CUSTOM, "-mapy", "mapY", "MapY", 
   "y", -1, Tk_Offset(PolygonMarkerOptions, axes.y), 0, &yAxisObjOption, 0},
  {TK_OPTION_COLOR, "-outline", "outline", "Outline", 
   STD_NORMAL_FOREGROUND, -1, Tk_Offset(PolygonMarkerOptions, outline), 
   TK_OPTION_NULL_OK, NULL, 0},
  {TK_OPTION_COLOR, "-outlinebg", "outlinebg", "OutlineBg", 
   NULL, -1, Tk_Offset(PolygonMarkerOptions, outlineBg), 
   TK_OPTION_NULL_OK, NULL, 0},
  {TK_OPTION_STRING_TABLE, "-state", "state", "State", 
   "normal", -1, Tk_Offset(PolygonMarkerOptions, state),
   0, &stateObjOption, 0},
  {TK_OPTION_BITMAP, "-stipple", "stipple", "Stipple",
   NULL, -1, Tk_Offset(PolygonMarkerOptions, stipple),
   TK_OPTION_NULL_OK, NULL, 0},
  {TK_OPTION_BOOLEAN, "-under", "under", "Under",
   "no", -1, Tk_Offset(PolygonMarkerOptions, drawUnder), 0, NULL, 0},
  {TK_OPTION_PIXELS, "-xoffset", "xOffset", "XOffset",
   "0", -1, Tk_Offset(PolygonMarkerOptions, xOffset), 0, NULL, 0},
  {TK_OPTION_BOOLEAN, "-xor", "xor", "Xor",
   "no", -1, Tk_Offset(PolygonMarkerOptions, xorr), 0, NULL, 0},
  {TK_OPTION_PIXELS, "-yoffset", "yOffset", "YOffset",
   "0", -1, Tk_Offset(PolygonMarkerOptions, yOffset), 0, NULL, 0},
  {TK_OPTION_END, NULL, NULL, NULL, NULL, -1, 0, 0, NULL, 0}
};

PolygonMarker::PolygonMarker(Graph* graphPtr, const char* name, 
			     Tcl_HashEntry* hPtr) 
  : Marker(graphPtr, name, hPtr)
{
  obj.classId = CID_MARKER_POLYGON;
  obj.className = dupstr("PolygonMarker");
  ops_ = (PolygonMarkerOptions*)calloc(1, sizeof(PolygonMarkerOptions));
  optionTable_ = Tk_CreateOptionTable(graphPtr->interp, optionSpecs);

  screenPts =NULL;
  outlineGC =NULL;
  fillGC =NULL;
  fillPts =NULL;
  nFillPts =0;
  outlinePts =NULL;
  nOutlinePts =0;
  xorState =0;
}

PolygonMarker::~PolygonMarker()
{
  Graph* graphPtr = obj.graphPtr;

  if (fillGC)
    Tk_FreeGC(graphPtr->display, fillGC);
  if (outlineGC)
    Blt_FreePrivateGC(graphPtr->display, outlineGC);
  if (fillPts)
    free(fillPts);
  if (outlinePts)
    free(outlinePts);
  if (screenPts)
    free(screenPts);
}

int PolygonMarker::configure()
{
  Graph* graphPtr = obj.graphPtr;
  PolygonMarkerOptions* ops = (PolygonMarkerOptions*)ops_;

  GC newGC;
  XGCValues gcValues;
  unsigned long gcMask;
  Drawable drawable;

  drawable = Tk_WindowId(graphPtr->tkwin);
  gcMask = (GCLineWidth | GCLineStyle);
  if (ops->outline) {
    gcMask |= GCForeground;
    gcValues.foreground = ops->outline->pixel;
  }
  if (ops->outlineBg) {
    gcMask |= GCBackground;
    gcValues.background = ops->outlineBg->pixel;
  }
  gcMask |= (GCCapStyle | GCJoinStyle);
  gcValues.cap_style = ops->capStyle;
  gcValues.join_style = ops->joinStyle;
  gcValues.line_style = LineSolid;
  gcValues.dash_offset = 0;
  gcValues.line_width = LineWidth(ops->lineWidth);
  if (LineIsDashed(ops->dashes)) {
    gcValues.line_style = (ops->outlineBg == NULL)
      ? LineOnOffDash : LineDoubleDash;
  }
  if (ops->xorr) {
    unsigned long pixel;
    gcValues.function = GXxor;

    gcMask |= GCFunction;
    pixel = Tk_3DBorderColor(graphPtr->plotBg)->pixel;
    if (gcMask & GCBackground) {
      gcValues.background ^= pixel;
    }
    gcValues.foreground ^= pixel;
    if (drawable != None) {
      draw(drawable);
    }
  }
  newGC = Blt_GetPrivateGC(graphPtr->tkwin, gcMask, &gcValues);
  if (LineIsDashed(ops->dashes)) {
    Blt_SetDashes(graphPtr->display, newGC, &ops->dashes);
  }
  if (outlineGC) {
    Blt_FreePrivateGC(graphPtr->display, outlineGC);
  }
  outlineGC = newGC;

  gcMask = 0;
  if (ops->fill) {
    gcMask |= GCForeground;
    gcValues.foreground = ops->fill->pixel;
  }
  if (ops->fillBg) {
    gcMask |= GCBackground;
    gcValues.background = ops->fillBg->pixel;
  }
  if (ops->stipple != None) {
    gcValues.stipple = ops->stipple;
    gcValues.fill_style = (ops->fillBg)
      ? FillOpaqueStippled : FillStippled;
    gcMask |= (GCStipple | GCFillStyle);
  }
  newGC = Tk_GetGC(graphPtr->tkwin, gcMask, &gcValues);
  if (fillGC) {
    Tk_FreeGC(graphPtr->display, fillGC);
  }
  fillGC = newGC;

  if ((gcMask == 0) && !(graphPtr->flags & RESET_AXES) && (ops->xorr)) {
    if (drawable != None) {
      map();
      draw(drawable);
    }
    return TCL_OK;
  }

  return TCL_OK;
}

void PolygonMarker::draw(Drawable drawable)
{
  Graph* graphPtr = obj.graphPtr;
  PolygonMarkerOptions* ops = (PolygonMarkerOptions*)ops_;

  // fill region
  if ((nFillPts > 0) && (ops->fill)) {
    XPoint* points = (XPoint*)malloc(nFillPts * sizeof(XPoint));
    if (!points)
      return;

    XPoint* dp = points;
    Point2d *sp, *send;
    for (sp = fillPts, send = sp + nFillPts; sp < send; sp++) {
      dp->x = (short int)sp->x;
      dp->y = (short int)sp->y;
      dp++;
    }

    XFillPolygon(graphPtr->display, drawable, fillGC, points, 
		 nFillPts, Complex, CoordModeOrigin);
    free(points);
  }

  // outline
  if ((nOutlinePts > 0) && (ops->lineWidth > 0) && 
      (ops->outline)) {
    Blt_Draw2DSegments(graphPtr->display, drawable, outlineGC,
		       outlinePts, nOutlinePts);
  }
}

void PolygonMarker::map()
{
  Graph* graphPtr = obj.graphPtr;
  PolygonMarkerOptions* ops = (PolygonMarkerOptions*)ops_;

  if (outlinePts) {
    free(outlinePts);
    outlinePts = NULL;
    nOutlinePts = 0;
  }

  if (fillPts) {
    free(fillPts);
    fillPts = NULL;
    nFillPts = 0;
  }

  if (screenPts) {
    free(screenPts);
    screenPts = NULL;
  }

  if (!ops->worldPts || ops->worldPts->num < 3)
    return;

  // Allocate and fill a temporary array to hold the screen coordinates of
  // the polygon.

  int nScreenPts = ops->worldPts->num + 1;
  Point2d* lscreenPts = (Point2d*)malloc((nScreenPts + 1) * sizeof(Point2d));
  {
    Point2d *sp, *dp, *send;

    dp = lscreenPts;
    for (sp = ops->worldPts->points, send = sp + ops->worldPts->num; 
	 sp < send; sp++) {
      *dp = mapPoint(sp, &ops->axes);
      dp->x += ops->xOffset;
      dp->y += ops->yOffset;
      dp++;
    }
    *dp = lscreenPts[0];
  }
  Region2d extents;
  Blt_GraphExtents(graphPtr, &extents);
  clipped_ = TRUE;
  if (ops->fill) {
    Point2d* lfillPts = (Point2d*)malloc(sizeof(Point2d) * nScreenPts * 3);
    int n = 
      Blt_PolyRectClip(&extents, lscreenPts, ops->worldPts->num,lfillPts);
    if (n < 3)
      free(lfillPts);
    else {
      nFillPts = n;
      fillPts = lfillPts;
      clipped_ = FALSE;
    }
  }
  if ((ops->outline) && (ops->lineWidth > 0)) { 
    Segment2d *segPtr;
    Point2d *sp, *send;

    /* 
     * Generate line segments representing the polygon outline.  The
     * resulting outline may or may not be closed from viewport clipping.
     */
    Segment2d* loutlinePts = (Segment2d*)malloc(nScreenPts * sizeof(Segment2d));
    if (!loutlinePts)
      return;

    // Note that this assumes that the point array contains an extra point
    // that closes the polygon.
    segPtr = loutlinePts;
    for (sp = lscreenPts, send = sp + (nScreenPts - 1); sp < send; sp++) {
      segPtr->p = sp[0];
      segPtr->q = sp[1];
      if (Blt_LineRectClip(&extents, &segPtr->p, &segPtr->q)) {
	segPtr++;
      }
    }
    nOutlinePts = segPtr - loutlinePts;
    outlinePts = loutlinePts;
    if (nOutlinePts > 0) {
      clipped_ = FALSE;
    }
  }

  screenPts = lscreenPts;
}

int PolygonMarker::pointIn(Point2d *samplePtr)
{
  PolygonMarkerOptions* ops = (PolygonMarkerOptions*)ops_;

  if (ops->worldPts && 
      (ops->worldPts->num >= 3) && 
      (screenPts))
    return Blt_PointInPolygon(samplePtr, screenPts, 
			      ops->worldPts->num + 1);

  return FALSE;
}

int PolygonMarker::regionIn(Region2d *extsPtr, int enclosed)
{
  PolygonMarkerOptions* ops = (PolygonMarkerOptions*)ops_;
    
  if (ops->worldPts && 
      (ops->worldPts->num >= 3) && 
      (screenPts))
    return Blt_RegionInPolygon(extsPtr, screenPts, 
			       ops->worldPts->num, enclosed);

  return FALSE;
}

void PolygonMarker::postscript(Blt_Ps ps)
{
  Graph* graphPtr = obj.graphPtr;
  PolygonMarkerOptions* ops = (PolygonMarkerOptions*)ops_;

  if (ops->fill) {
    Blt_Ps_Polyline(ps, fillPts, nFillPts);
    if (ops->fillBg) {
      Blt_Ps_XSetBackground(ps, ops->fillBg);
      Blt_Ps_Append(ps, "gsave fill grestore\n");
    }
    Blt_Ps_XSetForeground(ps, ops->fill);
    if (ops->stipple != None) {
      Blt_Ps_XSetStipple(ps, graphPtr->display, ops->stipple);
    } else {
      Blt_Ps_Append(ps, "fill\n");
    }
  }

  if ((ops->lineWidth > 0) && (ops->outline)) {

    Blt_Ps_XSetLineAttributes(ps, ops->outline, ops->lineWidth, &ops->dashes,
			      ops->capStyle, ops->joinStyle);

    if ((ops->outlineBg) && (LineIsDashed(ops->dashes))) {
      Blt_Ps_Append(ps, "/DashesProc {\ngsave\n    ");
      Blt_Ps_XSetBackground(ps, ops->outlineBg);
      Blt_Ps_Append(ps, "    ");
      Blt_Ps_XSetDashes(ps, (Blt_Dashes *)NULL);
      Blt_Ps_Append(ps, "stroke\n  grestore\n} def\n");
    } else {
      Blt_Ps_Append(ps, "/DashesProc {} def\n");
    }
    Blt_Ps_Draw2DSegments(ps, outlinePts, nOutlinePts);
  }
}