1 /**********************************************************
2  * Version $Id$
3  *********************************************************/
4 
5 ///////////////////////////////////////////////////////////
6 //                                                       //
7 //                         SAGA                          //
8 //                                                       //
9 //      System for Automated Geoscientific Analyses      //
10 //                                                       //
11 //                     Tool Library                      //
12 //                      Grid_Tools                       //
13 //                                                       //
14 //-------------------------------------------------------//
15 //                                                       //
16 //                 Grid_Orientation.cpp                  //
17 //                                                       //
18 //                Copyright (C) 2016 by                  //
19 //                     Olaf Conrad                       //
20 //                                                       //
21 //-------------------------------------------------------//
22 //                                                       //
23 // This file is part of 'SAGA - System for Automated     //
24 // Geoscientific Analyses'. SAGA is free software; you   //
25 // can redistribute it and/or modify it under the terms  //
26 // of the GNU General Public License as published by the //
27 // Free Software Foundation, either version 2 of the     //
28 // License, or (at your option) any later version.       //
29 //                                                       //
30 // SAGA is distributed in the hope that it will be       //
31 // useful, but WITHOUT ANY WARRANTY; without even the    //
32 // implied warranty of MERCHANTABILITY or FITNESS FOR A  //
33 // PARTICULAR PURPOSE. See the GNU General Public        //
34 // License for more details.                             //
35 //                                                       //
36 // You should have received a copy of the GNU General    //
37 // Public License along with this program; if not, see   //
38 // <http://www.gnu.org/licenses/>.                       //
39 //                                                       //
40 //-------------------------------------------------------//
41 //                                                       //
42 //    e-mail:     oconrad@saga-gis.org                   //
43 //                                                       //
44 //    contact:    Olaf Conrad                            //
45 //                Institute of Geography                 //
46 //                University of Hamburg                  //
47 //                Germany                                //
48 //                                                       //
49 ///////////////////////////////////////////////////////////
50 
51 //---------------------------------------------------------
52 
53 
54 ///////////////////////////////////////////////////////////
55 //														 //
56 //														 //
57 //														 //
58 ///////////////////////////////////////////////////////////
59 
60 //---------------------------------------------------------
61 #include "Grid_Orientation.h"
62 
63 
64 ///////////////////////////////////////////////////////////
65 //														 //
66 //														 //
67 //														 //
68 ///////////////////////////////////////////////////////////
69 
70 //---------------------------------------------------------
CGrid_Copy(void)71 CGrid_Copy::CGrid_Copy(void)
72 {
73 	//-----------------------------------------------------
74 	Set_Name		(_TL("Copy Grid"));
75 
76 	Set_Author		("O.Conrad (c) 2016");
77 
78 	Set_Description	(_TW(
79 		"Copy a grid. "
80 	));
81 
82 	//-----------------------------------------------------
83 	Parameters.Add_Grid(
84 		""	, "GRID"	, _TL("Grid"),
85 		_TL(""),
86 		PARAMETER_INPUT
87 	);
88 
89 	Parameters.Add_Grid(
90 		""	, "COPY"	, _TL("Copy"),
91 		_TL(""),
92 		PARAMETER_OUTPUT
93 	);
94 }
95 
96 
97 ///////////////////////////////////////////////////////////
98 //														 //
99 ///////////////////////////////////////////////////////////
100 
101 //---------------------------------------------------------
On_Execute(void)102 bool CGrid_Copy::On_Execute(void)
103 {
104 	CSG_Grid	*pGrid	= Parameters("GRID")->asGrid();
105 	CSG_Grid	*pCopy	= Parameters("COPY")->asGrid();
106 
107 	return( pCopy->Create(*pGrid) );
108 }
109 
110 
111 ///////////////////////////////////////////////////////////
112 //														 //
113 //														 //
114 //														 //
115 ///////////////////////////////////////////////////////////
116 
117 //---------------------------------------------------------
CGrid_Invert(void)118 CGrid_Invert::CGrid_Invert(void)
119 {
120 	//-----------------------------------------------------
121 	Set_Name		(_TL("Invert Grid"));
122 
123 	Set_Author		("O.Conrad (c) 2016");
124 
125 	Set_Description	(_TW(
126 		"Invert a grid, i.e. the highest value becomes the lowest and vice versa. "
127 		"If the target is not set, the changes will be stored to the original grid. "
128 	));
129 
130 	//-----------------------------------------------------
131 	Parameters.Add_Grid(
132 		NULL	, "GRID"	, _TL("Grid"),
133 		_TL(""),
134 		PARAMETER_INPUT
135 	);
136 
137 	Parameters.Add_Grid(
138 		NULL	, "INVERSE"	, _TL("Inverse Grid"),
139 		_TL(""),
140 		PARAMETER_OUTPUT_OPTIONAL
141 	);
142 }
143 
144 
145 ///////////////////////////////////////////////////////////
146 //														 //
147 ///////////////////////////////////////////////////////////
148 
149 //---------------------------------------------------------
On_Execute(void)150 bool CGrid_Invert::On_Execute(void)
151 {
152 	CSG_Grid	*pGrid	= Parameters("INVERSE")->asGrid();
153 
154 	if( pGrid == NULL )
155 	{
156 		pGrid	= Parameters("GRID")->asGrid();
157 	}
158 	else if( pGrid != Parameters("GRID")->asGrid() )
159 	{
160 		pGrid->Create(*Parameters("GRID")->asGrid());
161 
162 		pGrid->Fmt_Name("%s [%s]", pGrid->Get_Name(), _TL("Inverse"));
163 	}
164 
165 	//-----------------------------------------------------
166 	double	zMin	= pGrid->Get_Min();
167 	double	zMax	= pGrid->Get_Max();
168 
169 	for(int y=0; y<Get_NY() && SG_UI_Process_Set_Progress(y, Get_NY()); y++)
170 	{
171 		#pragma omp parallel for
172 		for(int x=0; x<Get_NX(); x++)
173 		{
174 			if( !pGrid->is_NoData(x, y) )
175 			{
176 				pGrid->Set_Value(x, y, zMax - (pGrid->asDouble(x, y) - zMin));
177 			}
178 		}
179 	}
180 
181 	//-----------------------------------------------------
182 	if( pGrid == Parameters("GRID")->asGrid() )
183 	{
184 		DataObject_Update(pGrid);
185 	}
186 
187 	return( true );
188 }
189 
190 
191 ///////////////////////////////////////////////////////////
192 //														 //
193 //														 //
194 //														 //
195 ///////////////////////////////////////////////////////////
196 
197 //---------------------------------------------------------
CGrid_Mirror(void)198 CGrid_Mirror::CGrid_Mirror(void)
199 {
200 	//-----------------------------------------------------
201 	Set_Name		(_TL("Mirror Grid"));
202 
203 	Set_Author		("O.Conrad (c) 2016");
204 
205 	Set_Description	(_TW(
206 		"Mirror a grid at its center axes', either vertically, horizontally or both. "
207 		"If the target is not set, the changes will be stored to the original grid. "
208 	));
209 
210 	//-----------------------------------------------------
211 	Parameters.Add_Grid(
212 		NULL	, "GRID"	, _TL("Grid"),
213 		_TL(""),
214 		PARAMETER_INPUT
215 	);
216 
217 	Parameters.Add_Grid(
218 		NULL	, "MIRROR"	, _TL("Mirror Grid"),
219 		_TL(""),
220 		PARAMETER_OUTPUT_OPTIONAL
221 	);
222 
223 	Parameters.Add_Choice(
224 		NULL	, "METHOD"	, _TL("Method"),
225 		_TL(""),
226 		CSG_String::Format("%s|%s|%s|",
227 			_TL("horizontally"),
228 			_TL("vertically"),
229 			_TL("both")
230 		)
231 	);
232 }
233 
234 
235 ///////////////////////////////////////////////////////////
236 //														 //
237 ///////////////////////////////////////////////////////////
238 
239 //---------------------------------------------------------
On_Execute(void)240 bool CGrid_Mirror::On_Execute(void)
241 {
242 	CSG_Grid	*pGrid	= Parameters("MIRROR")->asGrid();
243 
244 	if( pGrid == NULL )
245 	{
246 		pGrid	= Parameters("GRID")->asGrid();
247 	}
248 	else if( pGrid != Parameters("GRID")->asGrid() )
249 	{
250 		pGrid->Create(*Parameters("GRID")->asGrid());
251 
252 		pGrid->Fmt_Name("%s [%s %s]", pGrid->Get_Name(), _TL("mirrored"), Parameters("METHOD")->asString());
253 	}
254 
255 	//-----------------------------------------------------
256 	switch( Parameters("METHOD")->asInt() )
257 	{
258 	//-----------------------------------------------------
259 	case  0:	// vertically
260 		{
261 			for(int xa=0, xb=Get_NX()-1; xa<xb && SG_UI_Process_Set_Progress(xa, Get_NX()/2); xa++, xb--)
262 			{
263 				#pragma omp parallel for
264 				for(int y=0; y<Get_NY(); y++)
265 				{
266 					double	d             = pGrid->asDouble(xa, y);
267 					pGrid->Set_Value(xa, y, pGrid->asDouble(xb, y));
268 					pGrid->Set_Value(xb, y, d);
269 				}
270 			}
271 		}
272 		break;
273 
274 	//-----------------------------------------------------
275 	case  1:	// horizontally
276 		{
277 			for(int ya=0, yb=Get_NY()-1; ya<yb && SG_UI_Process_Set_Progress(ya, Get_NY()/2); ya++, yb--)
278 			{
279 				#pragma omp parallel for
280 				for(int x=0; x<Get_NX(); x++)
281 				{
282 					double	d             = pGrid->asDouble(x, ya);
283 					pGrid->Set_Value(x, ya, pGrid->asDouble(x, yb));
284 					pGrid->Set_Value(x, yb, d);
285 				}
286 			}
287 		}
288 		break;
289 
290 	//-----------------------------------------------------
291 	default:	// both
292 		{
293 			for(int ya=0, yb=Get_NY()-1; ya<=yb && SG_UI_Process_Set_Progress(ya, Get_NY()/2); ya++, yb--)
294 			{
295 				for(int xa=0, xb=Get_NX()-1; xa<=xb; xa++, xb--)
296 				{
297 					if( ya < yb && xa < xb )
298 					{
299 						double	d              = pGrid->asDouble(xa, ya);
300 						pGrid->Set_Value(xa, ya, pGrid->asDouble(xb, yb));
301 						pGrid->Set_Value(xb, yb, d);
302 
303 						d                      = pGrid->asDouble(xa, yb);
304 						pGrid->Set_Value(xa, yb, pGrid->asDouble(xb, ya));
305 						pGrid->Set_Value(xb, ya, d);
306 					}
307 					else if( xa < xb )
308 					{
309 						double	d              = pGrid->asDouble(xa, ya);
310 						pGrid->Set_Value(xa, ya, pGrid->asDouble(xb, ya));
311 						pGrid->Set_Value(xb, ya, d);
312 					}
313 					else if( ya < yb )
314 					{
315 						double	d              = pGrid->asDouble(xa, ya);
316 						pGrid->Set_Value(xa, ya, pGrid->asDouble(xa, yb));
317 						pGrid->Set_Value(xa, yb, d);
318 					}
319 				}
320 			}
321 		}
322 		break;
323 	}
324 
325 	//-----------------------------------------------------
326 	if( pGrid == Parameters("GRID")->asGrid() )
327 	{
328 		DataObject_Update(pGrid);
329 	}
330 
331 	return( true );
332 }
333 
334 
335 ///////////////////////////////////////////////////////////
336 //														 //
337 //														 //
338 //														 //
339 ///////////////////////////////////////////////////////////
340 
341 //---------------------------------------------------------
342