-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpolygon_integrals.cpp
484 lines (436 loc) · 9.15 KB
/
polygon_integrals.cpp
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <cmath>
# include <ctime>
using namespace std;
# include "polygon_integrals.hpp"
//****************************************************************************80
int i4_max ( int i1, int i2 )
//****************************************************************************80
//
// Purpose:
//
// I4_MAX returns the maximum of two I4's.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 13 October 1998
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int I1, I2, are two integers to be compared.
//
// Output, int I4_MAX, the larger of I1 and I2.
//
{
int value;
if ( i2 < i1 )
{
value = i1;
}
else
{
value = i2;
}
return value;
}
//****************************************************************************80
int i4_min ( int i1, int i2 )
//****************************************************************************80
//
// Purpose:
//
// I4_MIN returns the minimum of two I4's.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 13 October 1998
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int I1, I2, two integers to be compared.
//
// Output, int I4_MIN, the smaller of I1 and I2.
//
{
int value;
if ( i1 < i2 )
{
value = i1;
}
else
{
value = i2;
}
return value;
}
//****************************************************************************80
double moment ( int n, double x[], double y[], int p, int q )
//****************************************************************************80
//
// Purpose:
//
// MOMENT computes an unnormalized moment of a polygon.
//
// Discussion:
//
// Nu(P,Q) = Integral ( x, y in polygon ) x^p y^q dx dy
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 03 October 2012
//
// Author:
//
// John Burkardt
//
// Reference:
//
// Carsten Steger,
// On the calculation of arbitrary moments of polygons,
// Technical Report FGBV-96-05,
// Forschungsgruppe Bildverstehen, Informatik IX,
// Technische Universitaet Muenchen, October 1996.
//
// Parameters:
//
// Input, int N, the number of vertices of the polygon.
//
// Input, double X[N], Y[N], the vertex coordinates.
//
// Input, int P, Q, the indices of the moment.
//
// Output, double MOMENT, the unnormalized moment Nu(P,Q).
//
{
int i;
int k;
int l;
double nu_pq;
double s_pq;
double xi;
double xj;
double yi;
double yj;
nu_pq = 0.0;
xj = x[n-1];
yj = y[n-1];
for ( i = 0; i < n; i++ )
{
xi = x[i];
yi = y[i];
s_pq = 0.0;
for ( k = 0; k <= p; k++ )
{
for ( l = 0; l <= q; l++ )
{
s_pq = s_pq
+ r8_choose ( k + l, l ) * r8_choose ( p + q - k - l, q - l )
* pow ( xi, k ) * pow ( xj, p - k )
* pow ( yi, l ) * pow ( yj, q - l );
}
}
nu_pq = nu_pq + ( xj * yi - xi * yj ) * s_pq;
xj = xi;
yj = yi;
}
nu_pq = nu_pq / ( double ) ( p + q + 2 ) / ( double ) ( p + q + 1 )
/ r8_choose ( p + q, p );
return nu_pq;
}
//****************************************************************************80
double moment_central ( int n, double x[], double y[], int p, int q )
//****************************************************************************80
//
// Purpose:
//
// MOMENT_CENTRAL computes central moments of a polygon.
//
// Discussion:
//
// The central moment Mu(P,Q) is defined by
//
// Mu(P,Q) = Integral ( polygon ) (x-Alpha(1,0))^p (y-Alpha(0,1))^q dx dy
// / Area ( polygon )
//
// where
//
// Alpha(1,0) = Integral ( polygon ) x dx dy / Area ( polygon )
// Alpha(0,1) = Integral ( polygon ) y dx dy / Area ( polygon )
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 03 October 2012
//
// Author:
//
// John Burkardt
//
// Reference:
//
// Carsten Steger,
// On the calculation of arbitrary moments of polygons,
// Technical Report FGBV-96-05,
// Forschungsgruppe Bildverstehen, Informatik IX,
// Technische Universitaet Muenchen, October 1996.
//
// Parameters:
//
// Input, int N, the number of vertices of the polygon.
//
// Input, double X[N], Y[N], the vertex coordinates.
//
// Input, int P, Q, the indices of the moment.
//
// Output, double MOMENT_CENTRAL, the unnormalized moment Mu(P,Q).
//
{
double alpha_01;
double alpha_10;
double alpha_ij;
int i;
int j;
double mu_pq;
alpha_10 = moment_normalized ( n, x, y, 1, 0 );
alpha_01 = moment_normalized ( n, x, y, 0, 1 );
mu_pq = 0.0;
for ( i = 0; i <= p; i++ )
{
for ( j = 0; j <= q; j++ )
{
alpha_ij = moment_normalized ( n, x, y, i, j );
mu_pq = mu_pq + r8_mop ( p + q - i - j )
* r8_choose ( p, i ) * r8_choose ( q, j )
* pow ( alpha_10, p - i ) * pow ( alpha_01, q - j ) * alpha_ij;
}
}
return mu_pq;
}
//****************************************************************************80
double moment_normalized ( int n, double x[], double y[], int p, int q )
//****************************************************************************80
//
// Purpose:
//
// MOMENT_NORMALIZED computes a normalized moment of a polygon.
//
// Discussion:
//
// Alpha(P,Q) = Integral ( x, y in polygon ) x^p y^q dx dy / Area ( polygon )
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 03 October 2012
//
// Author:
//
// John Burkardt
//
// Reference:
//
// Carsten Steger,
// On the calculation of arbitrary moments of polygons,
// Technical Report FGBV-96-05,
// Forschungsgruppe Bildverstehen, Informatik IX,
// Technische Universitaet Muenchen, October 1996.
//
// Parameters:
//
// Input, int N, the number of vertices of the polygon.
//
// Input, double X[N], Y[N], the vertex coordinates.
//
// Input, int P, Q, the indices of the moment.
//
// Output, double MOMENT_NORMALIZED, the normalized moment Alpha(P,Q).
//
{
double alpha_pq;
double nu_00;
double nu_pq;
nu_pq = moment ( n, x, y, p, q );
nu_00 = moment ( n, x, y, 0, 0 );
alpha_pq = nu_pq / nu_00;
return alpha_pq;
}
//****************************************************************************80
double r8_choose ( int n, int k )
//****************************************************************************80
//
// Purpose:
//
// R8_CHOOSE computes the binomial coefficient C(N,K) as an R8.
//
// Discussion:
//
// The value is calculated in such a way as to avoid overflow and
// roundoff. The calculation is done in R8 arithmetic.
//
// The formula used is:
//
// C(N,K) = N! / ( K! * (N-K)! )
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 29 July 2011
//
// Author:
//
// John Burkardt
//
// Reference:
//
// ML Wolfson, HV Wright,
// Algorithm 160:
// Combinatorial of M Things Taken N at a Time,
// Communications of the ACM,
// Volume 6, Number 4, April 1963, page 161.
//
// Parameters:
//
// Input, int N, K, the values of N and K.
//
// Output, double R8_CHOOSE, the number of combinations of N
// things taken K at a time.
//
{
int i;
int mn;
int mx;
double value;
mn = i4_min ( k, n - k );
if ( mn < 0 )
{
value = 0.0;
}
else if ( mn == 0 )
{
value = 1.0;
}
else
{
mx = i4_max ( k, n - k );
value = ( double ) ( mx + 1 );
for ( i = 2; i <= mn; i++ )
{
value = ( value * ( double ) ( mx + i ) ) / ( double ) i;
}
}
return value;
}
//****************************************************************************80
double r8_mop ( int i )
//****************************************************************************80
//
// Purpose:
//
// R8_MOP returns the I-th power of -1 as an R8 value.
//
// Discussion:
//
// An R8 is an double value.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 16 November 2007
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int I, the power of -1.
//
// Output, double R8_MOP, the I-th power of -1.
//
{
double value;
if ( ( i % 2 ) == 0 )
{
value = 1.0;
}
else
{
value = -1.0;
}
return value;
}
//****************************************************************************80
void timestamp ( )
//****************************************************************************80
//
// Purpose:
//
// TIMESTAMP prints the current YMDHMS date as a time stamp.
//
// Example:
//
// 31 May 2001 09:45:54 AM
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 08 July 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// None
//
{
# define TIME_SIZE 40
static char time_buffer[TIME_SIZE];
const struct std::tm *tm_ptr;
size_t len;
std::time_t now;
now = std::time ( NULL );
tm_ptr = std::localtime ( &now );
len = std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr );
std::cout << time_buffer << "\n";
return;
# undef TIME_SIZE
}