File: count_decimation.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (127 lines) | stat: -rw-r--r-- 3,799 bytes parent folder | download | duplicates (4)
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
/****************************************************************************
 *
 * MODULE:       v.decimate
 * AUTHOR(S):    Vaclav Petras
 * PURPOSE:      Reduce the number of points in a vector map
 * COPYRIGHT:    (C) 2015 by the GRASS Development Team
 *
 *               This program is free software under the GNU General Public
 *               License (>=v2). Read the COPYING file that comes with GRASS
 *               for details.
 *
 *****************************************************************************/

/* TODO: change int */
/* TODO: revise names */

#include "count_decimation.h"

#include <grass/gis.h>

#include <stdlib.h>

void count_decimation_init(struct CountDecimationControl *control, int *skip,
                           int *preserve, int *offset, int *limit)
{
    control->skip_every = 0;
    control->preserve_every = 0;
    /* counter used by both but that's ok, skip and preserve are exclusive */
    control->every_counter = 0;
    control->n_count_filtered = 0;
    control->offset_n = 0;
    control->offset_n_counter = 0;
    control->limit_n = 0;
    control->limit_n_counter = 0;
    if (skip)
        control->skip_every = *skip;
    if (preserve)
        control->preserve_every = *preserve;
    if (offset)
        control->offset_n = *offset;
    if (limit)
        control->limit_n = *limit;
}

int count_decimation_is_valid(struct CountDecimationControl *control)
{
    if (control->skip_every == 1)
        return FALSE;
    if (control->skip_every && control->preserve_every > 1)
        return FALSE;
    return TRUE;
}

int count_decimation_is_noop(struct CountDecimationControl *control)
{
    if (control->skip_every < 2 && control->preserve_every < 2 &&
        !control->offset_n && !control->limit_n)
        return TRUE;
    return FALSE;
}

void count_decimation_init_from_str(struct CountDecimationControl *control,
                                    const char *skip, const char *preserve,
                                    const char *offset, const char *limit)
{
    control->skip_every = 0;
    control->preserve_every = 0;
    control->every_counter = 0;
    control->n_count_filtered = 0;
    control->offset_n = 0;
    control->offset_n_counter = 0;
    control->limit_n = 0;
    control->limit_n_counter = 0;
    /* TODO: atoi is probably not appropriate */
    if (skip)
        control->skip_every = atoi(skip);
    if (preserve)
        control->preserve_every = atoi(preserve);
    if (offset)
        control->offset_n = atoi(offset);
    if (limit)
        control->limit_n = atoi(limit);
}

/* TODO: eliminate noop cases */
int count_decimation_is_out(struct CountDecimationControl *control)
{
    if (control->offset_n) {
        if (control->offset_n_counter < control->offset_n) {
            control->offset_n_counter++;
            return TRUE;
        }
        else {
            control->offset_n = 0; /* disable offset check */
        }
    }
    if (control->skip_every) {
        control->every_counter++;
        if (control->every_counter == control->skip_every) {
            control->n_count_filtered++;
            control->every_counter = 0;
            return TRUE;
        }
    }
    else if (control->preserve_every) {
        control->every_counter++;
        if (control->every_counter == control->preserve_every) {
            control->every_counter = 0;
        }
        else {
            control->n_count_filtered++;
            return TRUE;
        }
    }
    return FALSE;
}

int count_decimation_is_end(struct CountDecimationControl *control)
{
    if (control->limit_n) {
        control->limit_n_counter++;
        /* this matches the last successfully imported point */
        if (control->limit_n_counter == control->limit_n)
            return TRUE;
    }
    return FALSE;
}