File: cdline.c

package info (click to toggle)
gwyddion 2.47-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 38,180 kB
  • ctags: 37,276
  • sloc: ansic: 317,531; sh: 5,122; xml: 3,583; python: 3,067; makefile: 2,694; cpp: 2,550; pascal: 418; perl: 154; ruby: 130; sed: 17
file content (914 lines) | stat: -rw-r--r-- 24,923 bytes parent folder | download
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
/*
 *  @(#) $Id: cdline.c 18119 2016-01-19 11:01:32Z yeti-dn $
 *  Copyright (C) 2004 David Necas (Yeti), Petr Klapetek.
 *  E-mail: yeti@gwyddion.net, klapetek@gwyddion.net.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include <string.h>
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwymath.h>
#include <libprocess/cdline.h>
#include <libgwyddion/gwydebugobjects.h>
#include "gwyprocessinternal.h"
#include <libgwyddion/gwynlfit.h>
#include <libprocess/gwycaldata.h>

typedef gdouble (*GwyCDLineFitFunc)(gdouble x,
                                    gint n_param,
                                    const gdouble *param,
                                    gpointer user_data,
                                    gboolean *fres);

typedef void (*GwyCDLineCDFunc)(const gdouble *x,
                                const gdouble *y,
                                gint n_dat,
                                gdouble *param,
                                gdouble *err,
                                gpointer user_data,
                                gboolean *fres);

typedef struct {
    const char *name;
    gint power_x;
    gint power_y;
    double default_init;
} GwyCDLineParam;

struct _GwyCDLineBuiltin {
    const gchar *function_name;
    const gchar *group_name;
    const gchar *function_definition;
    GwyCDLineFitFunc function;
    GwyCDLineCDFunc function_fit;
    gint nparams;
    const GwyCDLineParam *param;
};

static GwyCDLine*
gwy_cdline_new_static(const GwyCDLineBuiltin *data);

G_DEFINE_TYPE(GwyCDLine, gwy_cdline, GWY_TYPE_RESOURCE)

static void
get_linestatpars(const gdouble *y, gint ndat,
                 gint from, gint to,
                 gdouble *avg, gdouble *sigma)
{
    gint i, n;

    from = CLAMP(from, 0, ndat);
    to = CLAMP(to, 0, ndat);

    if (from > to)
        GWY_SWAP(gint, from, to);

    *avg = 0;
    *sigma = 0;

    n = to - from;
    if (n <= 0)
        return;

    for (i = from; i < to; i++)
        *avg += y[i];
    *avg /= n;

    for (i = from; i < to; i++)
        *sigma += (y[i] - *avg)*(y[i] - *avg);
    *sigma = sqrt(*sigma/n);
}

static void
get_linestatpars2(const gdouble *y, gint ndat,
                  gint from1, gint to1,
                  gint from2, gint to2,
                  gdouble *avg, gdouble *sigma)
{
    gint i, n;

    from1 = CLAMP(from1, 0, ndat);
    to1 = CLAMP(to1, 0, ndat);

    from2 = CLAMP(from2, 0, ndat);
    to2 = CLAMP(to2, 0, ndat);

    if (from1 > to1)
        GWY_SWAP(gint, from1, to1);
    if (from2 > to2)
        GWY_SWAP(gint, from2, to2);
    if (from1 > from2) {
        GWY_SWAP(gint, from1, from2);
        GWY_SWAP(gint, to1, to2);
    }
    /* Merge overlapping intervals to one. */
    if (from2 <= to1) {
        get_linestatpars(y, ndat, from1, MAX(to1, to2), avg, sigma);
        return;
    }

    if (to1 == from1 && from1 == 0)
        to1++;
    if (to2 == from2 && to2 == ndat)
        from2--;

    *avg = 0;
    *sigma = 0;

    n = to1 - from1 + to2 - from2;
    if (n <= 0)
        return;

    for (i = from1; i < to1; i++)
        *avg += y[i];
    for (i = from2; i < to2; i++)
        *avg += y[i];
    *avg /= n;

    for (i = from1; i < to1; i++)
        *sigma += (y[i] - *avg)*(y[i] - *avg);
    for (i = from2; i < to2; i++)
        *sigma += (y[i] - *avg)*(y[i] - *avg);
    *sigma = sqrt(*sigma/n);
}

static void
cd_uedgeheight(const gdouble *x,
               const gdouble *y,
               gint n_dat,
               gdouble *param,
               gdouble *err,
               G_GNUC_UNUSED gpointer user_data,
               gboolean *fres)
{
    gint i;
    gint nstep;
    gdouble max, val;
    gint imax, iwidth;

    nstep = n_dat/20;
    iwidth = n_dat/8;
    if (nstep < 1)
        nstep = 1;

    max = -G_MAXDOUBLE;
    imax = nstep/2;
    for (i = nstep; i < (n_dat - 2 * nstep); i++) {
        val = ((y[i + nstep] - y[i])/(x[i + nstep] - x[i]));
        if (max < val) {
            max = val;
            imax = i + nstep/2;
            param[1] = (x[i + nstep] + x[i])/2.0;
        }
    }

    get_linestatpars(y, n_dat, 0, imax - iwidth/2, param + 2, err + 2);
    get_linestatpars(y, n_dat, imax + iwidth/2, n_dat, param + 3, err + 3);

    param[0] = param[3] - param[2];
    err[0] = hypot(err[2], err[3]);
    err[1] = -1;

    *fres = TRUE;
}

static void
cd_ledgeheight(const gdouble *x,
               const gdouble *y,
               gint n_dat,
               gdouble *param,
               gdouble *err,
               G_GNUC_UNUSED gpointer user_data,
               gboolean *fres)
{
    gint i;
    gint nstep;
    gdouble min, val;
    gint imin, iwidth;

    nstep = n_dat/20;
    iwidth = n_dat/8;
    if (nstep < 1)
        nstep = 1;

    min = G_MAXDOUBLE;
    imin = nstep/2;
    for (i = nstep; i < (n_dat - 2 * nstep); i++) {
        val = ((y[i + nstep] - y[i])/(x[i + nstep] - x[i]));
        if (min > val) {
            min = val;
            imin = i + nstep/2;
            param[1] = (x[i + nstep] + x[i])/2.0;
        }
    }

    get_linestatpars(y, n_dat, 0, imin - iwidth/2, param + 2, err + 2);
    get_linestatpars(y, n_dat, imin + iwidth/2, n_dat, param + 3, err + 3);

    param[0] = param[2] - param[3];

    err[0] = hypot(err[2], err[3]);
    err[1] = -1;
    *fres = TRUE;
}

static gdouble
func_edgeheight(gdouble x,
                G_GNUC_UNUSED gint n_param,
                const gdouble *param,
                G_GNUC_UNUSED gpointer user_data,
                G_GNUC_UNUSED gboolean *fres)
{
    if (x < param[1])
        return param[2];
    else
        return param[3];
}


static void
cd_rstepheight(const gdouble *x,
               const gdouble *y,
               gint n_dat,
               gdouble *param,
               gdouble *err,
               G_GNUC_UNUSED gpointer user_data,
               gboolean *fres)
{
    gint i;
    gint nstep;
    gdouble max, min, val;
    gint imax, imin, iwidth;

    nstep = n_dat/20;
    if (nstep < 1)
        nstep = 1;

    max = -G_MAXDOUBLE;
    min = G_MAXDOUBLE;

    imax = imin = nstep/2;
    for (i = nstep; i < (n_dat - 2 * nstep); i++) {
        val = ((y[i + nstep] - y[i])/(x[i + nstep] - x[i]));
        if (min > val) {
            min = val;
            imin = i + nstep/2;
            param[3] = (x[i + nstep] + x[i])/2.0;
        }
    }

    for (i = imin; i < (n_dat - 2 * nstep); i++) {
        val = ((y[i + nstep] - y[i])/(x[i + nstep] - x[i]));
        if (max < val) {
            max = val;
            imax = i + nstep/2;
            param[4] = (x[i + nstep] + x[i])/2.0;
        }
    }
    iwidth = imax - imin;

    /*FIXME modidfied now (imin+iwidth/3, imax-iwidth/3) */
    get_linestatpars(y, n_dat, imin + iwidth/3, imax - iwidth/3,
                     param+2, err+2);
    /* XXX: The +1s correspond to sharp inequalities in original code. */
    get_linestatpars2(y, n_dat,
                      imin - iwidth + 1, imin - iwidth/3,
                      imax + iwidth/3 + 1, imax + iwidth,
                      param+1, err+1);
    param[0] = param[2] - param[1];

    err[0] = hypot(err[2], err[1]);
    err[3] = err[4] = -1;
    *fres = TRUE;
}

static void
cd_stepheight(const gdouble *x,
              const gdouble *y,
              gint n_dat,
              gdouble *param,
              gdouble *err,
              G_GNUC_UNUSED gpointer user_data,
              gboolean *fres)
{
    gint i;
    gint nstep;
    gdouble max, min, val;
    gint imax, imin, iwidth;

    nstep = n_dat/20;
    if (nstep < 1)
        nstep = 1;

    max = -G_MAXDOUBLE;
    min = G_MAXDOUBLE;
    imax = imin = nstep/2;

    for (i = nstep; i < (n_dat - 2 * nstep); i++) {
        val = ((y[i + nstep] - y[i])/(x[i + nstep] - x[i]));
        if (max < val) {
            max = val;
            imax = i + nstep/2;
            param[3] = (x[i + nstep] + x[i])/2.0;
        }
    }

    for (i = imax; i < (n_dat - 2 * nstep); i++) {
        val = ((y[i + nstep] - y[i])/(x[i + nstep] - x[i]));
        if (min > val) {
            min = val;
            imin = i + nstep/2;
            param[4] = (x[i + nstep] + x[i])/2.0;
        }
    }
    iwidth = imin - imax;

    /*FIXME: modified now (imax+iwidth/3, imin-iwidth/3) */
   // if (!cdata) {
    get_linestatpars(y, n_dat, imax + iwidth/3, imin - iwidth/3, param + 2,
                     err + 2);
    /* XXX: The +1s correspond to sharp inequalities in original code. */
    get_linestatpars2(y, n_dat,
                      imax - iwidth + 1, imax - iwidth/3,
                      imin + iwidth/3 + 1, imin + iwidth,
                      param+1, err+1);

    err[3] = err[4] = -1;
    /* } else {
        param[1] = 0;
        param[2] = 0;
        suma = sumb = 0;
        for (i = 0; i < n_dat; i++) {
            if ((i < (imax - iwidth/3) && i > (imax - iwidth))
                || (i > (imin + iwidth/3) && i < (imin + iwidth))) {
                param[1] += y[i]/cdata->zunc[i]/cdata->zunc[i];
                suma += (1/cdata->zunc[i]/cdata->zunc[i]);
            }
            else if (i>(imax + iwidth/3) && i<=(imin - iwidth/3))
            {
                param[2] += y[i]/cdata->zunc[i]/cdata->zunc[i];
                sumb += (1/cdata->zunc[i]/cdata->zunc[i]);
            }
        }
        param[1]/=suma;
        param[2]/=sumb;

        err[1] = 0;
        err[2] = 0;
        na = nb = 0;
        for (i = 0; i < n_dat; i++) {
            if ((i < (imax - iwidth/3) && i > (imax - iwidth))
                || (i > (imin + iwidth/3) && i < (imin + iwidth))) {
                err[1] += cdata->zunc[i]*cdata->zunc[i];
                na++;
            }
            else if (i>(imax + iwidth/3) && i<=(imin - iwidth/3))
            {
                printf("%g\n", cdata->zunc[i]);
                err[2] += cdata->zunc[i]*cdata->zunc[i];
                nb++;
            }
        }
        err[1] = sqrt(err[1]/na);
        err[2] = sqrt(err[2]/nb);
        err[3] = cdata->xunc[imax];
        err[4] = cdata->xunc[imin];
    }
    */

    param[0] = param[2] - param[1];
    err[0] = hypot(err[2], err[1]);

    *fres = TRUE;
}

static gdouble
func_stepheight(gdouble x,
                G_GNUC_UNUSED gint n_param,
                const gdouble *param,
                G_GNUC_UNUSED gpointer user_data,
                G_GNUC_UNUSED gboolean *fres)
{
    if (x > param[3] && x < param[4])
        return param[2];
    else
        return param[1];
}

static gdouble
    func_circle_down(gdouble x,
                G_GNUC_UNUSED gint n_param,
                const gdouble *param,
                G_GNUC_UNUSED gpointer user_data,
                G_GNUC_UNUSED gboolean *fres)
{
  if ( (param[0]*param[0] - (x-param[1])*(x-param[1])) > 0.0)
     return param[2] - sqrt(param[0]*param[0] - (x-param[1])*(x-param[1]));
  else
     return param[2];
}

static void
cd_circle_down(const gdouble *x,
               const gdouble *y,
               gint n_dat,
               gdouble *param,
               gdouble *err,
               G_GNUC_UNUSED gpointer user_data,
               gboolean *fres)
{
  gdouble x1, x2, x3, y11, y2, y3, rx, ry, res; /* y1 already used somewhere... */
  gint i, m;
  GwyNLFitter *fitter;
  GwyNLFitFunc ff;
  gdouble par_init[3];
  gdouble par_res[3];
  gboolean par_fix[3];

  m = n_dat/2;
  if (x[0] != x[n_dat-1]){
     x1 = x[0]; y11 = y[0];
     x2 = x[m]; y2 = y[m];
     x3 = x[n_dat-1]; y3 = y[n_dat-1];
  }
  else{
     x1 = x[m]; y11 = y[m];
     x2 = x[n_dat-1]; y2 = y[n_dat-1];
     x3 = x[0]; y3 = y[0];
  }
  ry = ( (x1-x3)*((x2-x1)*(x2-x1)+(y2-y11)*(y2-y11)) + (x2-x1)*((x3-x1)*(x3-x1)+(y3-y11)*(y3-y11)) )/
      ( 2*((x2-x1)*(y3-y11)-(x3-x1)*(y2-y11)) );
  rx = ( (x3-x1)*(x3-x1)+(y3-y11)*(y3-y11)+2*ry*(y3-y11) )/(-2*(x3-x1));
  par_init[0] = sqrt(rx*rx+ry*ry); /*r*/
  par_init[1] = x1-rx; /*x0*/
  par_init[2] = y11+ry; /*y0*/
  for (i=0;i<3;i++){
      par_fix[i] = FALSE;
      par_res[i] = par_init[i];
  }
  ff = func_circle_down;
  fitter = gwy_math_nlfit_new(ff, gwy_math_nlfit_derive);
  res = gwy_math_nlfit_fit_full(fitter, n_dat, x, y, NULL, 3, par_res, par_fix, NULL, NULL);
  if (res > 0.0){
    if (par_res[0] < 0.0)
       par_res[0] *= -1;
    for (i=0;i<3;i++){
      param[i] = par_res[i];
      err[i] = gwy_math_nlfit_get_sigma(fitter, i);
    }
    *fres = TRUE;
  }
  else
    *fres = FALSE;
}

static gdouble
func_circle_up(gdouble x,
               G_GNUC_UNUSED gint n_param,
               const gdouble *param,
               G_GNUC_UNUSED gpointer user_data,
               G_GNUC_UNUSED gboolean *fres)
{
  if ( (param[0]*param[0] - (x-param[1])*(x-param[1])) > 0.0)
    return param[2] + sqrt(param[0]*param[0] - (x-param[1])*(x-param[1]));
  else
    return param[2];
}

static void
cd_circle_up(const gdouble *x,
             const gdouble *y,
             gint n_dat,
             gdouble *param,
             gdouble *err,
             G_GNUC_UNUSED gpointer user_data,
             gboolean *fres)
{
  gdouble x1, x2, x3, y11, y2, y3, rx, ry, res;
  gint i, m;
  GwyNLFitter *fitter;
  GwyNLFitFunc ff;
  gdouble par_init[3];
  gdouble par_res[3];
  gboolean par_fix[3];

  m = n_dat/2;
  if (x[0] != x[n_dat-1]){
      x1 = x[0]; y11 = y[0];
      x2 = x[m]; y2 = y[m];
      x3 = x[n_dat-1]; y3 = y[n_dat-1];
  }
  else{
    x1 = x[m]; y11 = y[m];
    x2 = x[n_dat-1]; y2 = y[n_dat-1];
    x3 = x[0]; y3 = y[0];
  }

  ry = ( (x1-x3)*((x2-x1)*(x2-x1)+(y2-y11)*(y2-y11)) + (x2-x1)*((x3-x1)*(x3-x1)+(y3-y11)*(y3-y11)) )/
        ( 2*((x2-x1)*(y3-y11)-(x3-x1)*(y2-y11)) );
  rx = ( (x3-x1)*(x3-x1)+(y3-y11)*(y3-y11)+2*ry*(y3-y11) )/(-2*(x3-x1));
  par_init[0] = sqrt(rx*rx+ry*ry); /*r*/
  par_init[1] = x1-rx; /*x0*/
  par_init[2] = y11+ry; /*y0*/
  for (i=0;i<3;i++){
    par_fix[i] = FALSE;
    par_res[i] = par_init[i];
  }
  ff = func_circle_up;
  fitter = gwy_math_nlfit_new(ff, gwy_math_nlfit_derive);
  res = gwy_math_nlfit_fit_full(fitter, n_dat, x, y, NULL, 3, par_res, par_fix, NULL, NULL);
  if (res > 0.0){
    for (i=0;i<3;i++){
      param[i] = par_res[i];
      err[i] = gwy_math_nlfit_get_sigma(fitter, i);
    }
    *fres = TRUE;
  }
  else
    *fres = FALSE;
}

/************************** cdlines ****************************/

static const GwyCDLineParam stepheight_pars[] = {
   { "h",             0, 1, 1, },
   { "y<sub>1</sub>", 0, 1, 2, },
   { "y<sub>2</sub>", 0, 1, 2, },
   { "x<sub>1</sub>", 1, 0, 3, },
   { "x<sub>2</sub>", 1, 0, 4, },
};

static const GwyCDLineParam edgeheight_pars[] = {
   { "h",             0, 1, 1, },
   { "x",             1, 0, 2, },
   { "y<sub>1</sub>", 0, 1, 2, },
   { "y<sub>2</sub>", 0, 1, 2, },
};

static const GwyCDLineParam circle_pars[] = {
  { "r",             0, 1, 1, },
  { "x<sub>0</sub>", 1, 0, 2, },
  { "y<sub>0</sub>", 0, 1, 2, },
};

static const GwyCDLineBuiltin cdlines[] = {
    {
        N_("Edge height (right)"),
        N_("Edge"),
        "cd_step.png",
        &func_edgeheight,
        &cd_uedgeheight,
        G_N_ELEMENTS(edgeheight_pars),
        edgeheight_pars
    },
    {
        N_("Edge height (left)"),
        N_("Edge"),
        "cd_rstep.png",
        &func_edgeheight,
        &cd_ledgeheight,
        G_N_ELEMENTS(edgeheight_pars),
        edgeheight_pars
    },
    {
        N_("Step height (positive)"),  /* ISO 5436 */
        N_("Line"),
        "cd_line.png",
        &func_stepheight,
        &cd_stepheight,
        G_N_ELEMENTS(stepheight_pars),
        stepheight_pars
    },
    {
        N_("Step height (negative)"),
        N_("Line"),
        "cd_rline.png",
        &func_stepheight,
        &cd_rstepheight,
        G_N_ELEMENTS(stepheight_pars),
        stepheight_pars
    },
    {
        N_("Circle (down)"),
        N_("Circle"),
        "circle_down.png",
        &func_circle_down,
        &cd_circle_down,
        G_N_ELEMENTS(circle_pars),
        circle_pars
    },
    {
        N_("Circle (up)"),
        N_("Circle"),
        "circle_up.png",
        &func_circle_up,
        &cd_circle_up,
        G_N_ELEMENTS(circle_pars),
        circle_pars
    },
};

/**
 * gwy_cdline_get_name:
 * @cdline: A critical dimension evaluator.
 *
 * Return cdline name (its unique identifier).
 *
 * Returns: The cdline name.
 **/
const gchar*
gwy_cdline_get_name(GwyCDLine* cdline)
{
    g_return_val_if_fail(GWY_IS_CDLINE(cdline), "");
    return cdline->builtin->function_name;
}

/**
 * gwy_cdline_get_definition:
 * @cdline: A critical dimension evaluator.
 *
 * Gets the name of the image file with critical dimension evaluator
 * description.
 *
 * Returns: The cdline function definition.
 **/
const gchar*
gwy_cdline_get_definition(GwyCDLine* cdline)
{
    g_return_val_if_fail(GWY_IS_CDLINE(cdline), "");
    return cdline->builtin->function_definition;
}

/**
 * gwy_cdline_get_param_name:
 * @cdline: A NL evaluator function cdline.
 * @param: A parameter number.
 *
 * Returns the name of a critical dimension evaluator parameter.
 *
 * The name may contain Pango markup.
 *
 * Returns: The name of parameter @param.
 **/
const gchar*
gwy_cdline_get_param_name(GwyCDLine* cdline,
                                     gint param)
{
    const GwyCDLineParam *par;

    g_return_val_if_fail(GWY_IS_CDLINE(cdline), "");
    g_return_val_if_fail(param >= 0 && param < cdline->builtin->nparams, NULL);
    par = cdline->builtin->param + param;

    return par->name;
}

/**
 * gwy_cdline_get_param_default:
 * @cdline: A NL evaluator function cdline.
 * @param: A parameter number.
 *
 * Returns a constant default parameter value.
 *
 * Returns: The default parameter value, unrelated to the actual data fitted.
 *          It is worthless.
 **/
gdouble
gwy_cdline_get_param_default(GwyCDLine* cdline,
                             gint param)
{
    const GwyCDLineParam *par;

    g_return_val_if_fail(GWY_IS_CDLINE(cdline), 0.0);
    g_return_val_if_fail(param >= 0 && param < cdline->builtin->nparams,
                         G_MAXDOUBLE);
    par = cdline->builtin->param + param;

    return par->default_init;
}

/**
 * gwy_cdline_get_param_units:
 * @cdline: A critical dimension evaluator.
 * @param: A parameter number.
 * @siunit_x: SI unit of abscissa.
 * @siunit_y: SI unit of ordinate.
 *
 * Derives the SI unit of a critical dimension parameter from the units of
 * abscissa and ordinate.
 *
 * Returns: A newly created #GwySIUnit with the units of the parameter @param.
 *          If the units of @param are not representable as #GwySIUnit,
 *          the result is unitless (i.e. it will be presented as a mere
 *          number).
 *
 * Since: 2.5
 **/
GwySIUnit*
gwy_cdline_get_param_units(GwyCDLine *cdline,
                           gint param,
                           GwySIUnit *siunit_x,
                           GwySIUnit *siunit_y)
{
    const GwyCDLineParam *par;

    g_return_val_if_fail(GWY_IS_CDLINE(cdline), NULL);
    g_return_val_if_fail(param >= 0 && param < cdline->builtin->nparams, NULL);
    par = cdline->builtin->param + param;

    return gwy_si_unit_power_multiply(siunit_x, par->power_x,
                                      siunit_y, par->power_y,
                                      NULL);
}

/**
 * gwy_cdline_get_nparams:
 * @cdline: A critical dimension evaluator.
 *
 * Return the number of parameters of @cdline.
 *
 * Returns: The number of function parameters.
 **/
gint
gwy_cdline_get_nparams(GwyCDLine* cdline)
{
    g_return_val_if_fail(GWY_IS_CDLINE(cdline), 0);
    return cdline->builtin->nparams;
}

/**
 * gwy_cdline_get_value:
 * @cdline: A critical dimension evaluator.
 * @x: The point to compute value at.
 * @params: Evaluator parameter values.
 * @fres: Set to %TRUE if succeeds, %FALSE on failure.
 *
 * Calculates critical dimension function value in a single point with given
 * parameters.
 *
 * Returns: The function value.
 **/
gdouble
gwy_cdline_get_value(GwyCDLine *cdline,
                     gdouble x,
                     const gdouble *params,
                     gboolean *fres)
{
    return cdline->builtin->function(x, cdline->builtin->nparams, params,
                                     NULL, fres);
}

/**
 * gwy_cdline_fit:
 * @cdline: A critical dimension evaluator.
 * @n_dat: The number of data points (number of items in @x and @y).
 * @x: Abscissa points.
 * @y: Ordinate points.
 * @n_param: The number of parameters.  This argument is ignored as the
 *           evaluator knows how many parameters it has, it is safe to pass 0.
 * @params: Array to store fitted parameter values to.
 * @err: Array to store parameter errros to, may be %NULL.
 * @fixed_param: Which parameters should be treated as fixed.  It is ignored,
 *               pass %NULL.
 * @user_data: Ignored, pass %NULL.
 *
 * Performs a critical dimension evaulation (fit).
 **/
void
gwy_cdline_fit(GwyCDLine* cdline,
               gint n_dat,
               const gdouble *x,
               const gdouble *y,
               G_GNUC_UNUSED gint n_param,
               gdouble *params,
               gdouble *err,
               G_GNUC_UNUSED const gboolean *fixed_param,
               gpointer user_data)
{
    gboolean fres;

    g_return_if_fail(GWY_IS_CDLINE(cdline));
    fres = TRUE;
    cdline->builtin->function_fit(x, y, n_dat, params, err, user_data, &fres);
}

/**
 * gwy_cdline_fit_with_caldata:
 * @cdline: A critical dimension evaluator.
 * @n_dat: The number of data points (number of items in @x and @y).
 * @x: Abscissa points.
 * @y: Ordinate points.
 * @params: Array to store fitted parameter values to.
 * @err: Array to store parameter errros to, may be %NULL.
 * @cdata: Curve calibration data, may be %NULL.
 *
 * Performs a critical dimension evaulation (fit), allowing user to pass uncertainties.
 **/
void
gwy_cdline_fit_with_caldata (GwyCDLine* cdline,
                             gint n_dat,
                             const gdouble *x,
                             const gdouble *y,
                             gdouble *params,
                             gdouble *err,
                             GwyCurveCalibrationData *cdata)
{
    gboolean fres;

    g_return_if_fail(GWY_IS_CDLINE(cdline));
    fres = TRUE;
    cdline->builtin->function_fit(x, y, n_dat, params, err, cdata, &fres);
}

static void
gwy_cdline_class_init(GwyCDLineClass *klass)
{
    GwyResourceClass *parent_class, *res_class = GWY_RESOURCE_CLASS(klass);

    parent_class = GWY_RESOURCE_CLASS(gwy_cdline_parent_class);
    res_class->item_type = *gwy_resource_class_get_item_type(parent_class);

    res_class->item_type.type = G_TYPE_FROM_CLASS(klass);

    res_class->name = "cdlines";
    res_class->inventory = gwy_inventory_new(&res_class->item_type);
    gwy_inventory_forget_order(res_class->inventory);
}

static void
gwy_cdline_init(GwyCDLine *cdline)
{
    gwy_debug_objects_creation(G_OBJECT(cdline));
}

static GwyCDLine*
gwy_cdline_new_static(const GwyCDLineBuiltin *data)
{
    GwyCDLine *cdline;

    cdline = g_object_new(GWY_TYPE_CDLINE, "is-const", TRUE, NULL);
    cdline->builtin = data;
    g_string_assign(GWY_RESOURCE(cdline)->name, data->function_name);

    return cdline;
}

void
_gwy_cdline_class_setup_presets(void)
{
    GwyResourceClass *klass;
    GwyCDLine *cdline;
    guint i;

    /* Force class instantiation, this function is called before it's first
     * referenced. */
    klass = g_type_class_ref(GWY_TYPE_CDLINE);

    for (i = 0; i < G_N_ELEMENTS(cdlines); i++) {
        cdline = gwy_cdline_new_static(cdlines + i);
        gwy_inventory_insert_item(klass->inventory, cdline);
        g_object_unref(cdline);
    }
    gwy_inventory_restore_order(klass->inventory);

    /* The cdlines added a reference so we can safely unref it again */
    g_type_class_unref(klass);
}

/**
 * gwy_cdlines:
 *
 * Gets inventory with all the critical dimension evaluators.
 *
 * Returns: Critical dimension evaluator inventory.
 **/
GwyInventory*
gwy_cdlines(void)
{
    return GWY_RESOURCE_CLASS(g_type_class_peek(GWY_TYPE_CDLINE))->inventory;
}

/************************** Documentation ****************************/

/**
 * SECTION:cdline
 * @title: cdline
 * @short_description: Critical dimension
 **/

/* vim: set cin et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */