1 /**********************************************************
2 * Version $Id$
3 *********************************************************/
4
5 ///////////////////////////////////////////////////////////
6 // //
7 // SAGA //
8 // //
9 // System for Automated Geoscientific Analyses //
10 // //
11 // Application Programming Interface //
12 // //
13 // Library: SAGA_API //
14 // //
15 //-------------------------------------------------------//
16 // //
17 // mat_grid_radius.cpp //
18 // //
19 // Copyright (C) 2005 by Olaf Conrad //
20 // //
21 //-------------------------------------------------------//
22 // //
23 // This file is part of 'SAGA - System for Automated //
24 // Geoscientific Analyses'. //
25 // //
26 // This library is free software; you can redistribute //
27 // it and/or modify it under the terms of the GNU Lesser //
28 // General Public License as published by the Free //
29 // Software Foundation, either version 2.1 of the //
30 // License, or (at your option) any later version. //
31 // //
32 // This library is distributed in the hope that it will //
33 // be useful, but WITHOUT ANY WARRANTY; without even the //
34 // implied warranty of MERCHANTABILITY or FITNESS FOR A //
35 // PARTICULAR PURPOSE. See the GNU Lesser General Public //
36 // License for more details. //
37 // //
38 // You should have received a copy of the GNU Lesser //
39 // General Public License along with this program; if //
40 // not, see <http://www.gnu.org/licenses/>. //
41 // //
42 //-------------------------------------------------------//
43 // //
44 // contact: Olaf Conrad //
45 // Institute of Geography //
46 // University of Goettingen //
47 // Goldschmidtstr. 5 //
48 // 37077 Goettingen //
49 // Germany //
50 // //
51 // e-mail: oconrad@saga-gis.org //
52 // //
53 ///////////////////////////////////////////////////////////
54
55 //---------------------------------------------------------
56
57
58 ///////////////////////////////////////////////////////////
59 // //
60 // Grid Radius //
61 // //
62 ///////////////////////////////////////////////////////////
63
64 //---------------------------------------------------------
65 #include "mat_tools.h"
66 #include "grid.h"
67
68
69 ///////////////////////////////////////////////////////////
70 // //
71 // //
72 // //
73 ///////////////////////////////////////////////////////////
74
75 //---------------------------------------------------------
CSG_Grid_Radius(int maxRadius)76 CSG_Grid_Radius::CSG_Grid_Radius(int maxRadius)
77 {
78 m_maxRadius = 0;
79 m_nPoints = 0;
80 m_Points = NULL;
81 m_nPoints_R = NULL;
82 m_Points_R = NULL;
83
84 Create(maxRadius);
85 }
86
87 //---------------------------------------------------------
~CSG_Grid_Radius(void)88 CSG_Grid_Radius::~CSG_Grid_Radius(void)
89 {
90 Destroy();
91 }
92
93
94 ///////////////////////////////////////////////////////////
95 // //
96 // //
97 // //
98 ///////////////////////////////////////////////////////////
99
100 //---------------------------------------------------------
Create(int maxRadius)101 bool CSG_Grid_Radius::Create(int maxRadius)
102 {
103 Destroy();
104
105 //-----------------------------------------------------
106 if( maxRadius > 0 && maxRadius != m_maxRadius )
107 {
108 int x, y, i, n;
109 double d;
110
111 m_maxRadius = maxRadius;
112
113 m_nPoints_R = (int *)SG_Calloc(m_maxRadius + 1, sizeof(int));
114
115 for(y=-m_maxRadius; y<=m_maxRadius; y++)
116 {
117 for(x=-m_maxRadius; x<=m_maxRadius; x++)
118 {
119 if( (d = M_GET_LENGTH(x, y)) <= m_maxRadius )
120 {
121 m_nPoints++;
122 m_nPoints_R[(int)d]++;
123 }
124 }
125 }
126
127 //-------------------------------------------------
128 if( m_nPoints > 0 )
129 {
130 m_Points = (TSG_Grid_Radius *)SG_Calloc(m_nPoints , sizeof(TSG_Grid_Radius ));
131 m_Points_R = (TSG_Grid_Radius **)SG_Calloc(m_maxRadius + 1, sizeof(TSG_Grid_Radius *));
132
133 for(i=0, n=0; i<=m_maxRadius; i++)
134 {
135 m_Points_R [i] = m_Points + n;
136 n += m_nPoints_R[i];
137 m_nPoints_R[i] = 0;
138 }
139
140 //---------------------------------------------
141 for(y=-m_maxRadius; y<=m_maxRadius; y++)
142 {
143 for(x=-m_maxRadius; x<=m_maxRadius; x++)
144 {
145 if( (d = M_GET_LENGTH(x, y)) <= m_maxRadius )
146 {
147 i = (int)d;
148 n = m_nPoints_R[i]++;
149
150 m_Points_R[i][n].x = x;
151 m_Points_R[i][n].y = y;
152 m_Points_R[i][n].d = d;
153 }
154 }
155 }
156
157 return( true );
158 }
159 }
160
161 //-----------------------------------------------------
162 Destroy();
163
164 return( false );
165 }
166
167 //---------------------------------------------------------
Destroy(void)168 void CSG_Grid_Radius::Destroy(void)
169 {
170 if( m_Points )
171 {
172 SG_Free(m_Points);
173 }
174
175 if( m_nPoints_R )
176 {
177 SG_Free(m_nPoints_R);
178 }
179
180 if( m_Points_R )
181 {
182 SG_Free(m_Points_R);
183 }
184
185 m_maxRadius = 0;
186 m_nPoints = 0;
187 m_Points = NULL;
188 m_nPoints_R = NULL;
189 m_Points_R = NULL;
190 }
191
192
193 ///////////////////////////////////////////////////////////
194 // //
195 // //
196 // //
197 ///////////////////////////////////////////////////////////
198
199 //---------------------------------------------------------
200