Initial release of Maemo 5 port of gnuplot
[gnuplot] / term / compact.c
1 /*
2  * $Id: compact.c,v 1.6 2004/07/01 17:10:12 broeker Exp $
3  *
4  */
5
6 /* GNUPLOT - compact.c */
7
8 /* contains routines to compress a vector stream without modifying it */
9
10 /*[
11  * Copyright 1986 - 1993, 1998, 2004   Thomas Williams, Colin Kelley
12  *
13  * Permission to use, copy, and distribute this software and its
14  * documentation for any purpose with or without fee is hereby granted,
15  * provided that the above copyright notice appear in all copies and
16  * that both that copyright notice and this permission notice appear
17  * in supporting documentation.
18  *
19  * Permission to modify the software is granted, but not the right to
20  * distribute the complete modified source code.  Modifications are to
21  * be distributed as patches to the released version.  Permission to
22  * distribute binaries produced by compiling modified sources is granted,
23  * provided you
24  *   1. distribute the corresponding source modifications from the
25  *    released version in the form of a patch file along with the binaries,
26  *   2. add special version identification to distinguish your version
27  *    in addition to the base release version number,
28  *   3. provide your name and address as the primary contact for the
29  *    support of your modified version, and
30  *   4. retain our contact information in regard to use of the base
31  *    software.
32  * Permission to distribute the released version of the source code along
33  * with corresponding source modifications in the form of a patch file is
34  * granted with same provisions 2 through 4 for binary distributions.
35  *
36  * This software is provided "as is" without express or implied warranty
37  * to the extent permitted by applicable law.
38 ]*/
39
40 #ifndef COMPACT
41
42 static int compact_slope __PROTO((int xp[], int yp[], int isa_move[], int *sz, double delta));
43
44 /* replaces runs of constant slope in the buffer with single vectors
45    returns the number of points eliminated */
46 static int
47 compact_slope(int xp[], int yp[], int isa_move[], int *sz, double delta)
48 {
49     int dx, dy, old_size, new_index, i, start;
50     float slope, old_slope;
51
52     old_size = *sz;
53     new_index = 0;
54     start = 0;
55     if (xp[1] != xp[0])
56         old_slope = (float) (yp[1] - yp[0]) / (float) (xp[1] - xp[0]);
57     else
58         old_slope = (float) (yp[1] - yp[0]) / (float) (0.00001 + xp[1] - xp[0]);
59
60     for (i = 2; i < old_size; i++) {
61         dx = xp[i] - xp[i - 1];
62         dy = yp[i] - yp[i - 1];
63         if (dx != 0)
64             slope = (float) dy / (float) dx;
65         else
66             slope = (float) dy / ((float) dx + 0.00001);
67         if ((ABS(slope - old_slope) > delta) || (isa_move[i])) {
68             xp[new_index] = xp[start];
69             yp[new_index] = yp[start];
70             isa_move[new_index] = isa_move[start];
71             new_index++;
72             if (start != i - 1) {
73                 xp[new_index] = xp[i - 1];
74                 yp[new_index] = yp[i - 1];
75                 isa_move[new_index] = isa_move[i - 1];
76                 new_index++;
77             }
78             start = i;
79             /* this is the slope for the new run */
80             old_slope = slope;
81         }
82     }
83     /* copy the last point into the new array */
84     xp[new_index] = xp[old_size - 1];
85     yp[new_index] = yp[old_size - 1];
86     isa_move[new_index] = isa_move[old_size - 1];
87     new_index++;
88     *sz = new_index;
89     return (old_size - *sz);
90 }
91
92
93 # if 0 /* currently unused */
94 static int compact_int __PROTO((int xp[], int yp[], int isa_move[], int *size));
95
96 /* compacts the vector list by compressing runs of constant
97    dx&dy into one vector
98    use this if floating point is too expensive!
99    more naive than compact_slope; doesn't compact as much as possible
100    returns the number of points eliminated */
101 static int
102 compact_int(int xp[], int yp[], int isa_move[], int *size)
103 {
104     int dx, dy, old_dx, old_dy, start, index, i, old_size;
105
106     start = index = 0;
107     old_dx = xp[1] - xp[0];
108     old_dy = yp[1] - yp[0];
109     for (i = 2; i < *size; i++) {
110         dx = xp[i] - xp[i - 1];
111         dy = yp[i] - yp[i - 1];
112         if ((ABS(dx - old_dx) + ABS(dy - old_dy) != 0) || (isa_move[i])) {
113             /*  we've reached the end of a run */
114             xp[index] = xp[start];
115             yp[index] = yp[start];
116             isa_move[index] = isa_move[start];
117             index++;
118             if (start != i - 1) {
119                 xp[index] = xp[i - 1];
120                 yp[index] = yp[i - 1];
121                 isa_move[index] = isa_move[i - 1];
122                 index++;
123             }
124             start = i;
125             old_dx = dx;
126             old_dy = dy;
127         }
128     }                           /* end for */
129     /* include the last point */
130     xp[index] = xp[*size - 1];
131     yp[index] = yp[*size - 1];
132     isa_move[index] = isa_move[*size - 1];
133     index++;
134     old_size = *size;
135     *size = index;
136     return (old_size - *size);
137 }
138 # endif /* unused */
139
140 #endif /* !defined(COMPACT) */
141
142 #define COMPACT