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
|
/*
* $Id: gv_quick.c,v 2.0 2004/11/09 12:32:01 bernhard Exp $
*/
/* gv_quick.c
Bill Brown, USACERL
December 1993
*/
/*
Trying some stuff to draw a quick version of a vector file, to represent
it when doing interactive translations.
*/
#include <stdio.h>
#include <stdlib.h>
#include "gstypes.h"
#include "rowcol.h"
/* target number of desired points to represent entire file */
#define TFAST_PTS 800
/* max number of lines desired */
#define MFAST_LNS 400
static geoline *copy_line(geoline *);
static geoline *thin_line(geoline *, float);
/******************************************************************/
static geoline *copy_line(geoline * gln)
{
geoline *newln;
int i, np;
if (NULL == (newln = (geoline *) malloc(sizeof(geoline)))) {
fprintf(stderr, "Can't malloc.\n");
return (NULL);
}
np = newln->npts = gln->npts;
if (2 == (newln->dims = gln->dims)) {
if (NULL == (newln->p2 = (Point2 *) calloc(np, sizeof(Point2)))) {
fprintf(stderr, "Can't calloc.\n"); /* CLEAN UP */
return (NULL);
}
for (i = 0; i < np; i++) {
newln->p2[i][X] = gln->p2[i][X];
newln->p2[i][Y] = gln->p2[i][Y];
}
}
else {
if (NULL == (newln->p3 = (Point3 *) calloc(np, sizeof(Point3)))) {
fprintf(stderr, "Can't calloc.\n"); /* CLEAN UP */
return (NULL);
}
for (i = 0; i < np; i++) {
newln->p3[i][X] = gln->p3[i][X];
newln->p3[i][Y] = gln->p3[i][Y];
newln->p3[i][Z] = gln->p3[i][Z];
}
}
newln->next = NULL;
return (newln);
}
/******************************************************************/
/* for now, just eliminate points at regular interval */
static geoline *thin_line(geoline * gln, float factor)
{
geoline *newln;
int i, nextp, targp;
if (NULL == (newln = (geoline *) malloc(sizeof(geoline)))) {
fprintf(stderr, "Can't malloc.\n");
return (NULL);
}
targp = (int) (gln->npts / factor);
if (targp < 2) {
targp = 2;
}
newln->npts = targp;
if (2 == (newln->dims = gln->dims)) {
if (NULL == (newln->p2 = (Point2 *) calloc(targp, sizeof(Point2)))) {
fprintf(stderr, "Can't calloc.\n"); /* CLEAN UP */
return (NULL);
}
for (i = 0; i < targp; i++) {
if (i == targp - 1) {
nextp = gln->npts - 1; /* avoid rounding error */
}
else {
nextp = (int) ((i * (gln->npts - 1)) / (targp - 1));
}
newln->p2[i][X] = gln->p2[nextp][X];
newln->p2[i][Y] = gln->p2[nextp][Y];
}
}
else {
if (NULL == (newln->p3 = (Point3 *) calloc(targp, sizeof(Point3)))) {
fprintf(stderr, "Can't calloc.\n"); /* CLEAN UP */
return (NULL);
}
for (i = 0; i < targp; i++) {
if (i == targp - 1) {
nextp = gln->npts - 1; /* avoid rounding error */
}
else {
nextp = (int) ((i * (gln->npts - 1)) / (targp - 1));
}
newln->p3[i][X] = gln->p3[nextp][X];
newln->p3[i][Y] = gln->p3[nextp][Y];
newln->p3[i][Z] = gln->p3[nextp][Z];
}
}
newln->next = NULL;
return (newln);
}
/******************************************************************/
float gv_line_length(geoline * gln)
{
int n;
float length = 0.0;
for (n = 0; n < gln->npts - 1; n++) {
if (gln->p2) {
length += GS_P2distance(gln->p2[n + 1], gln->p2[n]);
}
else {
length += GS_distance(gln->p3[n + 1], gln->p3[n]);
}
}
return (length);
}
/******************************************************************/
int gln_num_points(geoline * gln)
{
int np = 0;
geoline *tln;
for (tln = gln; tln; tln = tln->next) {
np += tln->npts;
}
return (np);
}
/******************************************************************/
int gv_num_points(geovect * gv)
{
return (gln_num_points(gv->lines));
}
/******************************************************************/
/* strategy here: if line has more than average number of points, decimate
by eliminating points, otherwise decimate by eliminating shorter lines */
int gv_decimate_lines(geovect * gv)
{
int T_pts, A_ppl, N_s;
float decim_factor, slength[MFAST_LNS], T_slength, A_slength;
geoline *gln, *prev;
/* should check if already exists & free if != gv->lines */
if (TFAST_PTS > (T_pts = gv_num_points(gv))) {
gv->fastlines = gv->lines;
return (1);
}
N_s = 0;
T_slength = 0.0;
decim_factor = T_pts / TFAST_PTS;
A_ppl = T_pts / gv->n_lines; /* (int) Average points per line */
prev = NULL;
for (gln = gv->lines; gln; gln = gln->next) {
if (gln->npts > A_ppl) {
if (prev) {
prev->next = thin_line(gln, decim_factor);
prev = prev->next;
}
else {
prev = gv->fastlines = thin_line(gln, decim_factor);
}
}
else if (N_s < MFAST_LNS) {
T_slength += slength[N_s++] = gv_line_length(gln);
}
}
A_slength = T_slength / N_s;
N_s = 0;
for (gln = gv->lines; gln; gln = gln->next) {
if (gln->npts <= A_ppl) {
if (N_s < MFAST_LNS) {
if (slength[N_s++] > A_slength) {
if (prev) {
prev->next = copy_line(gln);
prev = prev->next;
}
else {
prev = gv->fastlines = copy_line(gln);
}
}
}
}
}
fprintf(stderr, "Decimated lines have %d points.\n",
gln_num_points(gv->fastlines));
return (1);
}
|