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
|
//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.4 (Public License)
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
//
// Anti-Grain Geometry - Version 2.4 Release Milano 3 (AggPas 2.4 RM3)
// Pascal Port By: Milan Marusinec alias Milano
// milan@marusinec.sk
// http://www.aggpas.org
// Copyright (c) 2005-2006
//
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
//----------------------------------------------------------------------------
// Contact: mcseem@antigrain.com
// mcseemagg@yahoo.com
// http://www.antigrain.com
//
//----------------------------------------------------------------------------
//
// Smooth polygon generator
//
// [Pascal Port History] -----------------------------------------------------
//
// 24.02.2006-Milano: Unit port establishment
//
{ agg_vcgen_smooth_poly1.pas }
unit
agg_vcgen_smooth_poly1 ;
INTERFACE
{$I agg_mode.inc }
uses
agg_basics ,
agg_vertex_source ,
agg_vertex_sequence ;
{ TYPES DEFINITION }
type
status_e = (
initial ,
ready ,
polygon ,
ctrl_b ,
ctrl_e ,
ctrl1 ,
ctrl2 ,
end_poly ,
stop );
vcgen_smooth_poly1_ptr = ^vcgen_smooth_poly1;
vcgen_smooth_poly1 = object(vertex_source )
m_src_vertices : vertex_sequence;
m_smooth_value : double;
m_closed : unsigned;
m_status : status_e;
m_src_vertex : unsigned;
m_ctrl1_x ,
m_ctrl1_y ,
m_ctrl2_x ,
m_ctrl2_y : double;
constructor Construct;
destructor Destruct; virtual;
procedure smooth_value_(v : double );
function _smooth_value : double;
// Vertex Generator Interface
procedure remove_all; virtual;
procedure add_vertex(x ,y : double; cmd : unsigned ); virtual;
// Vertex Source Interface
procedure rewind(path_id : unsigned ); virtual;
function vertex(x ,y : double_ptr ) : unsigned; virtual;
// Private
procedure calculate(v0 ,v1 ,v2 ,v3 : vertex_dist_ptr );
end;
{ GLOBAL PROCEDURES }
IMPLEMENTATION
{ LOCAL VARIABLES & CONSTANTS }
{ UNIT IMPLEMENTATION }
{ CONSTRUCT }
constructor vcgen_smooth_poly1.Construct;
begin
m_src_vertices.Construct(sizeof(vertex_dist ) ,6 );
m_smooth_value:=0.5;
m_closed:=0;
m_status:=initial;
m_src_vertex:=0;
end;
{ DESTRUCT }
destructor vcgen_smooth_poly1.Destruct;
begin
m_src_vertices.Destruct;
end;
{ SMOOTH_VALUE_ }
procedure vcgen_smooth_poly1.smooth_value_;
begin
m_smooth_value:=v * 0.5;
end;
{ _SMOOTH_VALUE }
function vcgen_smooth_poly1._smooth_value;
begin
result:=m_smooth_value * 2.0;
end;
{ REMOVE_ALL }
procedure vcgen_smooth_poly1.remove_all;
begin
m_src_vertices.remove_all;
m_closed:=0;
m_status:=initial;
end;
{ ADD_VERTEX }
procedure vcgen_smooth_poly1.add_vertex;
var
vd : vertex_dist;
begin
m_status:=initial;
if is_move_to(cmd ) then
begin
vd.x:=x;
vd.y:=y;
vd.dist:=0;
m_src_vertices.modify_last(@vd );
end
else
if is_vertex(cmd ) then
begin
vd.x:=x;
vd.y:=y;
vd.dist:=0;
m_src_vertices.add(@vd );
end
else
m_closed:=get_close_flag(cmd );
end;
{ REWIND }
procedure vcgen_smooth_poly1.rewind;
begin
if m_status = initial then
m_src_vertices.close(boolean(m_closed <> 0 ) );
m_status :=ready;
m_src_vertex:=0;
end;
{ VERTEX }
function vcgen_smooth_poly1.vertex;
var
cmd : unsigned;
label
_next ,_ready ,_polygon ;
begin
cmd:=path_cmd_line_to;
_next:
while not is_stop(cmd ) do
case m_status of
initial :
begin
rewind(0 );
goto _ready;
end;
ready :
_ready:
begin
if m_src_vertices.size < 2 then
begin
cmd:=path_cmd_stop;
goto _next;
end;
if m_src_vertices.size = 2 then
begin
x^:=vertex_dist_ptr(m_src_vertices.array_operator(m_src_vertex ) ).x;
y^:=vertex_dist_ptr(m_src_vertices.array_operator(m_src_vertex ) ).y;
inc(m_src_vertex );
if m_src_vertex = 1 then
begin
result:=path_cmd_move_to;
exit;
end;
if m_src_vertex = 2 then
begin
result:=path_cmd_line_to;
exit;
end;
cmd:=path_cmd_stop;
goto _next;
end;
cmd:=path_cmd_move_to;
m_status:=polygon;
m_src_vertex:=0;
goto _polygon;
end;
polygon :
_polygon:
begin
if m_closed <> 0 then
if m_src_vertex >= m_src_vertices.size then
begin
x^:=vertex_dist_ptr(m_src_vertices.array_operator(0 ) ).x;
y^:=vertex_dist_ptr(m_src_vertices.array_operator(0 ) ).y;
m_status:=end_poly;
result :=path_cmd_curve4;
exit;
end
else
else
if m_src_vertex >= m_src_vertices.size - 1 then
begin
x^:=vertex_dist_ptr(m_src_vertices.array_operator(m_src_vertices.size - 1 ) ).x;
y^:=vertex_dist_ptr(m_src_vertices.array_operator(m_src_vertices.size - 1 ) ).y;
m_status:=end_poly;
result :=path_cmd_curve3;
exit;
end;
calculate(
m_src_vertices.prev(m_src_vertex ) ,
m_src_vertices.curr(m_src_vertex ) ,
m_src_vertices.next(m_src_vertex ) ,
m_src_vertices.next(m_src_vertex + 1 ) );
x^:=vertex_dist_ptr(m_src_vertices.array_operator(m_src_vertex ) ).x;
y^:=vertex_dist_ptr(m_src_vertices.array_operator(m_src_vertex ) ).y;
inc(m_src_vertex );
if m_closed <> 0 then
begin
m_status:=ctrl1;
if m_src_vertex = 1 then
result:=path_cmd_move_to
else
result:=path_cmd_curve4;
exit;
end
else
begin
if m_src_vertex = 1 then
begin
m_status:=ctrl_b;
result :=path_cmd_move_to;
exit;
end;
if m_src_vertex >= m_src_vertices.size - 1 then
begin
m_status:=ctrl_e;
result :=path_cmd_curve3;
exit;
end;
m_status:=ctrl1;
result :=path_cmd_curve4;
exit;
end;
end;
ctrl_b :
begin
x^:=m_ctrl2_x;
y^:=m_ctrl2_y;
m_status:=polygon;
result :=path_cmd_curve3;
exit;
end;
ctrl_e :
begin
x^:=m_ctrl1_x;
y^:=m_ctrl1_y;
m_status:=polygon;
result :=path_cmd_curve3;
exit;
end;
ctrl1 :
begin
x^:=m_ctrl1_x;
y^:=m_ctrl1_y;
m_status:=ctrl2;
result :=path_cmd_curve4;
exit;
end;
ctrl2 :
begin
x^:=m_ctrl2_x;
y^:=m_ctrl2_y;
m_status:=polygon;
result :=path_cmd_curve4;
exit;
end;
end_poly :
begin
m_status:=stop;
result :=path_cmd_end_poly or m_closed;
exit;
end;
stop :
begin
result:=path_cmd_stop;
exit;
end;
end;
result:=cmd;
end;
{ CALCULATE }
procedure vcgen_smooth_poly1.calculate;
var
k1 ,k2 ,xm1 ,ym1 ,xm2 ,ym2 : double;
begin
k1:=v0.dist / (v0.dist + v1.dist );
k2:=v1.dist / (v1.dist + v2.dist );
xm1:=v0.x + (v2.x - v0.x ) * k1;
ym1:=v0.y + (v2.y - v0.y ) * k1;
xm2:=v1.x + (v3.x - v1.x ) * k2;
ym2:=v1.y + (v3.y - v1.y ) * k2;
m_ctrl1_x:=v1.x + m_smooth_value * (v2.x - xm1 );
m_ctrl1_y:=v1.y + m_smooth_value * (v2.y - ym1 );
m_ctrl2_x:=v2.x + m_smooth_value * (v1.x - xm2 );
m_ctrl2_y:=v2.y + m_smooth_value * (v1.y - ym2 );
end;
END.
|