File: box.c

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (359 lines) | stat: -rw-r--r-- 6,638 bytes parent folder | download | duplicates (3)
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
/*!
   \file box.c

   \brief Vector library - bounding box related fns

   Higher level functions for reading/writing/manipulating vectors.

   (C) 2001-2008 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.

   \author Radim Blazek

   \date 2001-2008
 */

#include <stdlib.h>
#include <grass/gis.h>
#include <grass/Vect.h>

/*!
   \brief Tests for point in box

   \param x,y,z coordinates
   \param Box boundary box

   \return 1 point is in box
   \return 0 point is not in box
 */
int Vect_point_in_box(double x, double y, double z, BOUND_BOX * Box)
{

    if (x >= Box->W && x <= Box->E &&
	y >= Box->S && y <= Box->N && z >= Box->B && z <= Box->T) {
	return 1;
    }

    return 0;
}

/*!
   \brief Tests for overlap of two boxes

   \param A boundary box A
   \param B boundary box B

   \return 1 boxes overlap
   \return 0 boxes do not overlap
 */
int Vect_box_overlap(BOUND_BOX * A, BOUND_BOX * B)
{

    if (A->E < B->W || A->W > B->E ||
	A->N < B->S || A->S > B->N || A->T < B->B || A->B > B->T) {
	return 0;
    }

    return 1;
}

/*!
   \brief Copy box B to box A

   \param A boundary A
   \param B boundary B

   \return 1
 */
int Vect_box_copy(BOUND_BOX * A, BOUND_BOX * B)
{

    A->N = B->N;
    A->S = B->S;
    A->E = B->E;
    A->W = B->W;
    A->T = B->T;
    A->B = B->B;

    return 1;
}

/*!
   \brief Extend box A by box B

   \param A boundary A
   \param B boundary B

   \return 1
 */
int Vect_box_extend(BOUND_BOX * A, BOUND_BOX * B)
{

    if (B->N > A->N)
	A->N = B->N;
    if (B->S < A->S)
	A->S = B->S;
    if (B->E > A->E)
	A->E = B->E;
    if (B->W < A->W)
	A->W = B->W;
    if (B->T > A->T)
	A->T = B->T;
    if (B->B < A->B)
	A->B = B->B;

    return 1;
}


/*!
 * \brief Clip coordinates to box, if necessary, lines extending outside of a box.
 *
 * A line represented by the coordinates <b>x, y</b> and <b>c_x, c_y</b> is clipped to
 * the window defined by <b>s</b> (south), <b>n</b> (north), <b>w</b>
 * (west), and <b>e</b> (east). Note that the following constraints must be
 * true:
 * w <e
 * s <n
 * The <b>x</b> and <b>c_x</b> are values to be compared to <b>w</b> and
 * <b>e.</b> The <b>y</b> and <b>c_y</b> are values to be compared to
 * <b>s</b> and <b>n.</b>
 * The <b>x</b> and <b>c_x</b> values returned lie between <b>w</b> and 
 * <b>e.</b> The <b>y</b> and <b>c_y</b> values returned lie between 
 * <b>s</b> and <b>n.</b>
 *
 *  \param x, y coordinates (w, e)
 *  \param c_x,c_y coordinates (s, n)
 *  \param Box boundary box
 *
 *  \return 1 if any clipping occured
 *  \return 0 otherwise
 */

int
Vect_box_clip(double *x, double *y, double *c_x, double *c_y, BOUND_BOX * Box)
{
    int mod;

    mod = 0;

    if (*x < Box->W) {
	if (*c_x != *x)
	    *y = *y + (Box->W - *x) / (*c_x - *x) * (*c_y - *y);
	*x = Box->W;
	mod = 1;
    }
    if (*x > Box->E) {
	if (*c_x != *x)
	    *y = *y + (Box->E - *x) / (*c_x - *x) * (*c_y - *y);
	*x = Box->E;
	mod = 1;
    }
    if (*c_x < Box->W) {
	if (*c_x != *x)
	    *c_y = *c_y + (Box->W - *c_x) / (*x - *c_x) * (*y - *c_y);
	*c_x = Box->W;
	mod = 1;
    }
    if (*c_x > Box->E) {
	if (*c_x != *x)
	    *c_y = *c_y + (Box->E - *c_x) / (*x - *c_x) * (*y - *c_y);
	*c_x = Box->E;
	mod = 1;
    }
    if (*y < Box->S) {
	if (*c_y != *y)
	    *x = *x + (Box->S - *y) / (*c_y - *y) * (*c_x - *x);
	*y = Box->S;
	mod = 1;
    }
    if (*y > Box->N) {
	if (*c_y != *y)
	    *x = *x + (Box->N - *y) / (*c_y - *y) * (*c_x - *x);
	*y = Box->N;
	mod = 1;
    }
    if (*c_y < Box->S) {
	if (*c_y != *y)
	    *c_x = *c_x + (Box->S - *c_y) / (*y - *c_y) * (*x - *c_x);
	*c_y = Box->S;
	mod = 1;
    }
    if (*c_y > Box->N) {
	if (*c_y != *y)
	    *c_x = *c_x + (Box->N - *c_y) / (*y - *c_y) * (*x - *c_x);
	*c_y = Box->N;
	mod = 1;
    }

    return (mod);
}



/*!
   \brief Get boundary box of line

   \param Map vector map
   \param line line id
   \param[out] Box bounding box

   \return 1 on success
   \return 0 line is dead
 */
int Vect_get_line_box(struct Map_info *Map, int line, BOUND_BOX * Box)
{
    struct Plus_head *Plus;
    P_LINE *Line;

    Plus = &(Map->plus);
    Line = Plus->Line[line];

    if (Line == NULL) {		/* dead */
	Box->N = 0;
	Box->S = 0;
	Box->E = 0;
	Box->W = 0;
	Box->T = 0;
	Box->B = 0;
	return 0;
    }
    else {
	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;
}

/*!
   \brief Get boundary box of area

   \param Map vector map
   \param area area id
   \param[out] Box bounding box

   \return 1 on success
   \return 0 area is dead
 */
int Vect_get_area_box(struct Map_info *Map, int area, BOUND_BOX * Box)
{
    struct Plus_head *Plus;
    P_AREA *Area;

    Plus = &(Map->plus);
    Area = Plus->Area[area];

    if (Area == NULL) {		/* dead */
	Box->N = 0;
	Box->S = 0;
	Box->E = 0;
	Box->W = 0;
	Box->T = 0;
	Box->B = 0;
	return 0;
    }
    else {
	Box->N = Area->N;
	Box->S = Area->S;
	Box->E = Area->E;
	Box->W = Area->W;
	Box->T = Area->T;
	Box->B = Area->B;
    }

    return 1;
}

/*!
   \brief Get boundary box of isle

   \param Map vector map
   \param isle isle id
   \param[out] Box bounding box

   \return 1 on success
   \return 0 isle is dead
 */
int Vect_get_isle_box(struct Map_info *Map, int isle, BOUND_BOX * Box)
{
    struct Plus_head *Plus;
    P_ISLE *Isle;

    Plus = &(Map->plus);
    Isle = Plus->Isle[isle];

    if (Isle == NULL) {		/* dead */
	Box->N = 0;
	Box->S = 0;
	Box->E = 0;
	Box->W = 0;
	Box->T = 0;
	Box->B = 0;
	return 0;
    }
    else {
	Box->N = Isle->N;
	Box->S = Isle->S;
	Box->E = Isle->E;
	Box->W = Isle->W;
	Box->T = Isle->T;
	Box->B = Isle->B;
    }

    return 1;
}

/*!
   \brief Get boundary box of map

   \param Map vector map
   \param[out] Box bouding box

   \return 1 on success, 0 on error
 */
int Vect_get_map_box(struct Map_info *Map, BOUND_BOX * Box)
{
    struct Plus_head *Plus;

    Plus = &(Map->plus);

    Box->N = Plus->box.N;
    Box->S = Plus->box.S;
    Box->E = Plus->box.E;
    Box->W = Plus->box.W;
    Box->T = Plus->box.T;
    Box->B = Plus->box.B;

    return 1;
}


/*!
   \brief Copy region Window to Box

   \param Window region structure (raster-based)
   \param[out] Box boundary box (vector-based)

   \return 1 on success, 0 on error
 */
int Vect_region_box(struct Cell_head *Window, BOUND_BOX * Box)
{

    Box->N = Window->north;
    Box->S = Window->south;
    Box->E = Window->east;
    Box->W = Window->west;
    Box->T = PORT_DOUBLE_MAX;
    Box->B = -PORT_DOUBLE_MAX;

    return 1;
}