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
|
/*
* $Id: plus_line.c,v 1.11 2003/09/11 16:12:48 markus Exp $
*
****************************************************************************
*
* MODULE: Vector library
*
* AUTHOR(S): Original author CERL, probably Dave Gerdes.
* Update to GRASS 5.7 Radim Blazek.
*
* PURPOSE: Lower level functions for reading/writing/manipulating vectors.
*
* COPYRIGHT: (C) 2001 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
* for details.
*
*****************************************************************************/
#include <stdlib.h>
#include "Vect.h"
/* dig_add_line ()
** Add new line to plus structure.
**
**
** Returns -1 on error
** number of line
*/
int
dig_add_line (struct Plus_head *plus, int type, struct line_pnts *Points, long offset){
int lineid, node, lp;
P_LINE *line;
BOUND_BOX box;
/* First look if we have space in array of pointers to lines
* and reallocate if necessary */
if ( plus->n_lines >= plus->alloc_lines ) { /* array is full */
if ( dig_alloc_lines(plus,1000) == -1 )
return -1;
}
/* allocate line structure */
lineid = plus->n_lines + 1;
plus->Line[lineid] = dig_alloc_line();
line = plus->Line[lineid];
/* Add nodes */
G_debug ( 3, "Register node: type = %d, %f,%f", type, Points->x[0], Points->y[0]);
node = dig_find_node ( plus, Points->x[0], Points->y[0], Points->z[0]);
G_debug ( 3, "node = %d", node);
if ( node == 0 ) {
node = dig_add_node ( plus, Points->x[0], Points->y[0], Points->z[0] );
G_debug ( 3, "Add new node: %d", node);
} else {
G_debug ( 3, "Old node found: %d", node);
}
line->N1 = node;
dig_node_add_line (plus, node, lineid, Points, type );
if ( plus->do_uplist ) dig_node_add_updated ( plus, node );
if ( type & GV_LINES ) {
lp = Points->n_points - 1;
G_debug ( 3, "Register node %f,%f", Points->x[lp], Points->y[lp]);
node = dig_find_node ( plus, Points->x[lp], Points->y[lp], Points->z[lp]);
G_debug ( 3, "node = %d", node);
if ( node == 0 ) {
node = dig_add_node ( plus, Points->x[lp], Points->y[lp], Points->z[lp]);
G_debug ( 3, "Add new node: %d", node);
} else {
G_debug ( 3, "Old node found: %d", node);
}
line->N2 = node;
dig_node_add_line (plus, node, -lineid, Points, type );
if ( plus->do_uplist ) dig_node_add_updated ( plus, node );
} else {
line->N2 = 0;
}
line->type = type;
line->offset = offset;
line->left = 0;
line->right = 0;
line->N = 0;
line->S = 0;
line->E = 0;
line->W = 0;
plus->n_lines++;
switch ( type ) {
case GV_POINT:
plus->n_plines++;
break;
case GV_LINE:
plus->n_llines++;
break;
case GV_BOUNDARY:
plus->n_blines++;
break;
case GV_CENTROID:
plus->n_clines++;
break;
case GV_FACE:
plus->n_flines++;
break;
case GV_KERNEL:
plus->n_klines++;
break;
}
dig_line_box ( Points, &box );
dig_line_set_box (plus, lineid, &box);
dig_spidx_add_line ( plus, lineid, &box );
if ( plus->do_uplist ) dig_line_add_updated ( plus, lineid );
return ( lineid );
}
/* dig_del_line ()
** Delete line from topology. Doesn't update area/isle references
** (dig_del_area/isle() must be run before the line is deleted if the
** line is part of such structure).
** Updateis info about line in nodes. If this line is last in node then node is deleted.
**
** Returns -1 on error
** 0 OK
*/
int
dig_del_line (struct Plus_head *plus, int line)
{
int i, mv;
P_LINE *Line;
P_NODE *Node;
/* TODO: free structures */
G_debug (3, "dig_del_line() line = %d", line);
Line = plus->Line[line];
dig_spidx_del_line ( plus, line );
/* Delete from nodes (and nodes) */
Node = plus->Node[Line->N1];
mv = 0;
for ( i = 0; i < Node->n_lines; i++ ) {
if ( mv ) {
Node->lines[i-1] = Node->lines[i];
Node->angles[i-1] = Node->angles[i];
} else {
if ( abs(Node->lines[i]) == line ) mv = 1;
}
}
Node->n_lines--;
if ( Node->n_lines == 0 ) {
G_debug (3, " node %d has 0 lines -> delete", Line->N1);
dig_spidx_del_node ( plus, Line->N1 );
plus->Node[Line->N1] = NULL;
} else {
if ( plus->do_uplist ) dig_node_add_updated ( plus, Line->N1 );
}
if ( Line->type & GV_LINES ) {
Node = plus->Node[Line->N2];
mv = 0;
for ( i = 0; i < Node->n_lines; i++ ) {
if ( mv ) {
Node->lines[i-1] = Node->lines[i];
Node->angles[i-1] = Node->angles[i];
} else {
if ( abs(Node->lines[i]) == line ) mv = 1;
}
}
Node->n_lines--;
if ( Node->n_lines == 0 ) {
G_debug (3, " node %d has 0 lines -> delete", Line->N2);
dig_spidx_del_node ( plus, Line->N2 );
plus->Node[Line->N2] = NULL;
} else {
if ( plus->do_uplist ) dig_node_add_updated ( plus, Line->N2 );
}
}
/* Delete line */
plus->Line[line] = NULL;
return 0;
}
/* dig_line_get_area ()
** Get area number on line side
**
** Returns area number
** 0 no area
** -1 error
*/
plus_t
dig_line_get_area (struct Plus_head *plus, plus_t line, int side) {
P_LINE *Line;
Line = plus->Line[line];
if ( side == GV_LEFT ) {
G_debug ( 3, "dig_line_get_area(): line = %d, side = %d (left), area = %d",
line, side, Line->left );
return (Line->left );
}
if ( side == GV_RIGHT ) {
G_debug ( 3, "dig_line_get_area(): line = %d, side = %d (right), area = %d",
line, side, Line->right );
return (Line->right);
}
return (-1);
}
/* dig_line_set_area ()
** Set area number on line side
**
*/
int
dig_line_set_area (struct Plus_head *plus, plus_t line, int side, plus_t area ) {
P_LINE *Line;
Line = plus->Line[line];
if ( side == GV_LEFT ) { Line->left = area; }
else if ( side == GV_RIGHT ) { Line->right = area; }
return (1);
}
/* dig_line_set_box ()
** Set line bound box
**
*/
int
dig_line_set_box (struct Plus_head *plus, plus_t line, BOUND_BOX *Box ) {
P_LINE *Line;
Line = plus->Line[line];
Line->N = Box->N;
Line->S = Box->S;
Line->E = Box->E;
Line->W = Box->W;
Line->T = Box->T;
Line->B = Box->B;
return (1);
}
/* dig_line_get_box ()
** Get line bound box saved in topo
**
*/
int
dig_line_get_box (struct Plus_head *plus, plus_t line, BOUND_BOX *Box ) {
P_LINE *Line;
Line = plus->Line[line];
Box->N = Line->N;
Box->S = Line->S;
Box->E = Line->E;
Box->W = Line->W;
Box->T = Line->T;
Box->B = Line->B;
return (1);
}
|