File: trans_polar.dpr

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (324 lines) | stat: -rw-r--r-- 6,604 bytes parent folder | download | duplicates (6)
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
//
// AggPas 2.4 RM3 Demo application
// Note: Press F1 key on run to see more info about this demo
//
// Paths: src;src\ctrl;src\svg;src\util;src\platform\win;expat-wrap
//
program
 trans_polar_ ;

uses
 agg_basics ,
 agg_platform_support ,
 agg_color ,
 agg_pixfmt ,
 agg_pixfmt_rgb ,

 agg_ctrl ,
 agg_slider_ctrl ,
 agg_cbox_ctrl ,

 agg_renderer_base ,
 agg_renderer_scanline ,
 agg_rasterizer_scanline_aa ,
 agg_scanline_u ,
 agg_render_scanlines ,

 agg_conv_transform ,
 agg_conv_segmentator ,
 agg_vertex_source ,
 agg_trans_affine ;

{$I agg_mode.inc }

const
 flip_y = true;

type
 transformed_control = object(ctrl )
   m_ctrl     : ctrl_ptr;
   m_pipeline : vertex_source_ptr;

   constructor Construct(ctrl : ctrl_ptr; pl : vertex_source_ptr );

   function  num_paths : unsigned; virtual;
   procedure rewind(path_id : unsigned ); virtual;
   function  vertex(x ,y : double_ptr ) : unsigned; virtual;
   function  _color(i : unsigned ) : aggclr_ptr; virtual;

  end;

 trans_polar_ptr = ^trans_polar; 
 trans_polar = object(trans_affine )
   m_base_angle ,
   m_base_scale ,

   m_base_x ,
   m_base_y ,

   m_translation_x ,
   m_translation_y ,

   m_spiral : double;

   constructor Construct;

   procedure base_scale (v : double );
   procedure full_circle(v : double );
   procedure base_offset(dx ,dy : double );
   procedure translation(dx ,dy : double );
   procedure spiral     (v : double );

  end;

 the_application = object(platform_support )
   m_slider1       ,
   m_slider_spiral ,
   m_slider_base_y : slider_ctrl;

   constructor Construct(format_ : pix_format_e; flip_y_ : boolean );
   destructor  Destruct;

   procedure on_draw; virtual;

   procedure on_key(x ,y : int; key ,flags : unsigned ); virtual;

  end;

{ CONSTRUCT }
constructor transformed_control.Construct;
begin
 m_ctrl    :=ctrl;
 m_pipeline:=pl;

end;

{ NUM_PATHS }
function transformed_control.num_paths;
begin
 result:=m_ctrl.num_paths;

end;

{ REWIND }
procedure transformed_control.rewind;
begin
 m_pipeline.rewind(path_id );

end;

{ VERTEX }
function transformed_control.vertex;
begin
 result:=m_pipeline.vertex(x ,y );

end;

{ _COLOR }
function transformed_control._color;
begin
 result:=m_ctrl._color(i );

end;

{ trans_polar_transform }
procedure trans_polar_transform(this : trans_polar_ptr; x ,y : double_ptr );
var
 x1 ,y1 : double;

begin
 x1:=(x^ + this.m_base_x ) * this.m_base_angle;
 y1:=(y^ + this.m_base_y ) * this.m_base_scale + (x^ * this.m_spiral );

 x^:=Cos(x1 ) * y1 + this.m_translation_x;
 y^:=Sin(x1 ) * y1 + this.m_translation_y;

end;

{ CONSTRUCT }
constructor trans_polar.Construct;
begin
 inherited Construct;

 m_base_angle:=1.0;
 m_base_scale:=1.0;

 m_base_x:=0.0;
 m_base_y:=0.0;

 m_translation_x:=0.0;
 m_translation_y:=0.0;

 m_spiral:=0.0;

 transform:=@trans_polar_transform;

end;

{ BASE_SCALE }
procedure trans_polar.base_scale;
begin
 m_base_scale:=v;

end;

{ FULL_CIRCLE }
procedure trans_polar.full_circle;
begin
 m_base_angle:=2.0 * pi / v;

end;

{ BASE_OFFSET }
procedure trans_polar.base_offset;
begin
 m_base_x:=dx;
 m_base_y:=dy;

end;

{ TRANSLATION }
procedure trans_polar.translation;
begin
 m_translation_x:=dx;
 m_translation_y:=dy;

end;

{ SPIRAL }
procedure trans_polar.spiral;
begin
 m_spiral:=v;

end;

{ CONSTRUCT }
constructor the_application.Construct;
begin
 inherited Construct(format_ ,flip_y_ );

 m_slider1.Construct      (10 ,10 ,600 - 10 ,17 ,not flip_y_ );
 m_slider_spiral.Construct(10 ,10 + 20 ,600 - 10 ,17 + 20 ,not flip_y_ );
 m_slider_base_y.Construct(10 ,10 + 40 ,600 - 10 ,17 + 40 ,not flip_y_ );

 add_ctrl(@m_slider1 );

 m_slider1.range_    (0.0 ,100.0 );
 m_slider1.num_steps_(5 );
 m_slider1.value_    (32.0);
 m_slider1.label_    ('Some Value=%1.0f' );

 add_ctrl(@m_slider_spiral );

 m_slider_spiral.label_('Spiral=%.3f' );
 m_slider_spiral.range_(-0.1 ,0.1 );
 m_slider_spiral.value_(0.0 );

 add_ctrl(@m_slider_base_y );

 m_slider_base_y.label_('Base Y=%.3f' );
 m_slider_base_y.range_(50.0 ,200.0 );
 m_slider_base_y.value_(120.0 );

end;

{ DESTRUCT }
destructor the_application.Destruct;
begin
 inherited Destruct;

 m_slider1.Destruct;
 m_slider_spiral.Destruct;
 m_slider_base_y.Destruct;

end;

{ ON_DRAW }
procedure the_application.on_draw;
var
 pixf : pixel_formats;

 rb  : renderer_base;
 ren : renderer_scanline_aa_solid;
 ras : rasterizer_scanline_aa;
 sl  : scanline_u8;

 rgba  : aggclr;
 trans : trans_polar;
 segm  : conv_segmentator;

 pipeline : conv_transform;

 ctrl : transformed_control;

begin
// Initialize structures
 pixfmt_bgr24(pixf ,rbuf_window );

 rb.Construct (@pixf );
 ren.Construct(@rb );

 rgba.ConstrDbl(1 ,1 ,1 );
 rb.clear      (@rgba );

 ras.Construct;
 sl.Construct;

// Render the controls
 render_ctrl(@ras ,@sl ,@ren ,@m_slider1 );
 render_ctrl(@ras ,@sl ,@ren ,@m_slider_spiral );
 render_ctrl(@ras ,@sl ,@ren ,@m_slider_base_y );

// Render
 trans.Construct;
 trans.full_circle(-600 );
 trans.base_scale (-1.0 );
 trans.base_offset(0.0 ,m_slider_base_y._value );
 trans.translation(_width / 2.0 ,_height / 2.0 + 30.0 );
 trans.spiral     (-m_slider_spiral._value );

 segm.Construct    (@m_slider1 );
 pipeline.Construct(@segm ,@trans );
 ctrl.Construct    (@m_slider1 ,@pipeline );

 render_ctrl(@ras ,@sl ,@ren ,@ctrl );

// Free AGG resources
 ras.Destruct;
 sl.Destruct;

end;

{ ON_KEY }
procedure the_application.on_key;
begin
 if key = key_f1 then
  message_(
   'Another example of non-linear transformations requested by one of my friends. '#13 +
   'Here we render a standard AGG control in its original form (the slider in the '#13 +
   'bottom) and after the transformation. The transformer itself is not a part of '#13 +
   'AGG and just demonstrates how to write custom transformers (class trans_polar).'#13 +
   'Note that because the transformer is non-linear, we need to use conv_segmentator '#13 +
   'first. Don''t worry much about the transformed_control class, it''s just an adaptor  '#13 +
   'used to render the controls with additional transformations.'#13#13 +
   'How to play with:'#13#13 +
   'Try to drag the value of the slider at the bottom and watch how it''s being '#13 +
   'synchronized in the polar coordinates. Also change two other parameters '#13 +
   '(Spiral and Base Y) and the size of the window.' +
   #13#13'Note: F2 key saves current "screenshot" file in this demo''s directory.  ' );

end;

VAR
 app : the_application;

BEGIN
 app.Construct(pix_format_bgr24 ,flip_y );
 app.caption_ ('AGG Example. Polar Transformer (F1-Help)' );

 if app.init(600 ,400 ,window_resize ) then
  app.run;

 app.Destruct;

END.