1 /**********************************************************
2  * Version $Id$
3  *********************************************************/
4 
5 ///////////////////////////////////////////////////////////
6 //                                                       //
7 //                         SAGA                          //
8 //                                                       //
9 //      System for Automated Geoscientific Analyses      //
10 //                                                       //
11 //                     Tool Library                      //
12 //                     Grid_Calculus                     //
13 //                                                       //
14 //-------------------------------------------------------//
15 //                                                       //
16 //               Grid_Geometric_Figures.cpp              //
17 //                                                       //
18 //                 Copyright (C) 2003 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 Goettingen               //
47 //                Goldschmidtstr. 5                      //
48 //                37077 Goettingen                       //
49 //                Germany                                //
50 //                                                       //
51 ///////////////////////////////////////////////////////////
52 
53 //---------------------------------------------------------
54 
55 
56 ///////////////////////////////////////////////////////////
57 //														 //
58 //														 //
59 //														 //
60 ///////////////////////////////////////////////////////////
61 
62 //---------------------------------------------------------
63 #include "Grid_Geometric_Figures.h"
64 
65 
66 ///////////////////////////////////////////////////////////
67 //														 //
68 //														 //
69 //														 //
70 ///////////////////////////////////////////////////////////
71 
72 //---------------------------------------------------------
CGrid_Geometric_Figures(void)73 CGrid_Geometric_Figures::CGrid_Geometric_Figures(void)
74 {
75 	Set_Name	(_TL("Geometric Figures"));
76 
77 	Set_Author	(SG_T("(c) 2001 by O.Conrad"));
78 
79 	Set_Description(_TW(
80 		"Construct grids from geometric figures (planes, cones).\n"
81 		"(c) 2001 by Olaf Conrad, Goettingen\nemail: oconrad@gwdg.de")
82 	);
83 
84 	//-----------------------------------------------------
85 	Parameters.Add_Grid_List(
86 		NULL, "RESULT"		, _TL("Result"),
87 		_TL(""),
88 		PARAMETER_OUTPUT_OPTIONAL, false
89 	);
90 
91 	Parameters.Add_Value(
92 		NULL, "CELL_COUNT"	, _TL("Cell Count"),
93 		_TL(""),
94 		PARAMETER_TYPE_Int, 100, 2.0, true
95 	);
96 
97 	Parameters.Add_Value(
98 		NULL, "CELL_SIZE"	, _TL("Cell Size"),
99 		_TL(""),
100 		PARAMETER_TYPE_Double, 1.0, 0.0, true
101 	);
102 
103 	Parameters.Add_Choice(
104 		NULL, "FIGURE"		, _TL("Figure"),
105 		_TL(""),
106 		CSG_String::Format(SG_T("%s|%s|%s|"), _TL("Cone (up)"), _TL("Cone (down)"), _TL("Plane")),
107 		0
108 	);
109 
110 	Parameters.Add_Value(
111 		NULL, "PLANE"		, _TL("Direction of Plane [Degree]"),
112 		_TL(""),
113 		PARAMETER_TYPE_Double, 22.5
114 	);
115 }
116 
117 //---------------------------------------------------------
~CGrid_Geometric_Figures(void)118 CGrid_Geometric_Figures::~CGrid_Geometric_Figures(void)
119 {}
120 
121 
122 ///////////////////////////////////////////////////////////
123 //														 //
124 //														 //
125 //														 //
126 ///////////////////////////////////////////////////////////
127 
128 //---------------------------------------------------------
On_Execute(void)129 bool CGrid_Geometric_Figures::On_Execute(void)
130 {
131 	int		NXY;
132 	double	DXY;
133 	CSG_Grid	*pGrid;
134 
135 	//-----------------------------------------------------
136 	NXY		= Parameters("CELL_COUNT")	->asInt();
137 	DXY		= Parameters("CELL_SIZE")	->asDouble();
138 
139 	Parameters("RESULT")->asGridList()->Add_Item(
140 		pGrid	= SG_Create_Grid(SG_DATATYPE_Float, NXY, NXY, DXY)
141 	);
142 
143 	//-----------------------------------------------------
144 	switch( Parameters("FIGURE")->asInt() )
145 	{
146 	case 0:	default:
147 		Create_Cone		(pGrid, true);
148 		break;
149 
150 	case 1:
151 		Create_Cone		(pGrid, false);
152 		break;
153 
154 	case 2:
155 		Create_Plane	(pGrid, Parameters("PLANE")->asDouble());
156 		break;
157 	}
158 
159 	//-----------------------------------------------------
160 	return( true );
161 }
162 
163 
164 ///////////////////////////////////////////////////////////
165 //														 //
166 //														 //
167 //														 //
168 ///////////////////////////////////////////////////////////
169 
170 //---------------------------------------------------------
Create_Cone(CSG_Grid * pGrid,bool bUp)171 void CGrid_Geometric_Figures::Create_Cone(CSG_Grid *pGrid, bool bUp)
172 {
173 	int		x, y;
174 	double	nx_2, ny_2, dx, dy, d;
175 
176 	//-----------------------------------------------------
177 	pGrid->Set_Name(bUp ? _TL("Cone (Up)") : _TL("Cone (Down)"));
178 
179 	nx_2	= pGrid->Get_Cellsize() * (double)pGrid->Get_NX() / 2.0;
180 	ny_2	= pGrid->Get_Cellsize() * (double)pGrid->Get_NY() / 2.0;
181 
182 	//-----------------------------------------------------
183 	for(y=0; y<pGrid->Get_NY() && Set_Progress(y, pGrid->Get_NY()); y++)
184 	{
185 		for(x=0; x<pGrid->Get_NX(); x++)
186 		{
187 			dx	= 0.5 + x * pGrid->Get_Cellsize() - nx_2;
188 			dy	= 0.5 + y * pGrid->Get_Cellsize() - ny_2;
189 			d	= sqrt(dx*dx + dy*dy);
190 
191 			if( d < nx_2 )
192 			{
193 				pGrid->Set_Value(x, y, bUp ? d : -d);
194 			}
195 			else
196 			{
197 				pGrid->Set_NoData(x, y);
198 			}
199 		}
200 	}
201 }
202 
203 //---------------------------------------------------------
Create_Plane(CSG_Grid * pGrid,double Direction)204 void CGrid_Geometric_Figures::Create_Plane(CSG_Grid *pGrid, double Direction)
205 {
206 	int		x, y;
207 	double	xPos, yPos, z, dSin, dCos, Max;
208 
209 	//-----------------------------------------------------
210 	pGrid->Fmt_Name("%s (%.2f %s)", _TL("Plane"), Direction, _TL("Degree"));
211 
212 	Max		= sqrt(2.0) * pGrid->Get_Cellsize() * pGrid->Get_Cellsize() / 4.0;
213 
214 	dSin	= sin(M_DEG_TO_RAD * Direction);
215 	dCos	= cos(M_DEG_TO_RAD * Direction);
216 
217 	//-----------------------------------------------------
218 	for(y=0, yPos=0.5-pGrid->Get_Cellsize()*pGrid->Get_NY()/2.0; y<pGrid->Get_NY() && Set_Progress(y, pGrid->Get_NY()); y++, yPos+=pGrid->Get_Cellsize())
219 	{
220 		for(x=0, xPos=0.5-pGrid->Get_Cellsize()*pGrid->Get_NX()/2.0; x<pGrid->Get_NX(); x++, xPos+=pGrid->Get_Cellsize())
221 		{
222 		//	pGrid->Set_NoData(x, y);
223 
224 		//	z	= dCos * xPos - dSin * yPos;
225 
226 		//	if( z >= -Max && z <= Max )
227 			{
228 		//		z	= dSin * xPos + dCos * yPos;
229 
230 		//		if( z >= -Max && z <= Max )
231 				{
232 					z	= xPos * dSin + yPos * dCos;
233 
234 					pGrid->Set_Value(x, y, z);
235 				}
236 			}
237 		}
238 	}
239 }
240 
241 
242 ///////////////////////////////////////////////////////////
243 //														 //
244 //														 //
245 //														 //
246 ///////////////////////////////////////////////////////////
247 
248 //---------------------------------------------------------
249