1 
2 ///////////////////////////////////////////////////////////
3 //                                                       //
4 //                         SAGA                          //
5 //                                                       //
6 //      System for Automated Geoscientific Analyses      //
7 //                                                       //
8 //                     Tool Library                      //
9 //                     Grid_Calculus                     //
10 //                                                       //
11 //-------------------------------------------------------//
12 //                                                       //
13 //                   Grid_Plotter.cpp                    //
14 //                                                       //
15 //                 Copyright (C) 2003 by                 //
16 //                    Andre Ringeler                     //
17 //                                                       //
18 //-------------------------------------------------------//
19 //                                                       //
20 // This file is part of 'SAGA - System for Automated     //
21 // Geoscientific Analyses'. SAGA is free software; you   //
22 // can redistribute it and/or modify it under the terms  //
23 // of the GNU General Public License as published by the //
24 // Free Software Foundation, either version 2 of the     //
25 // License, or (at your option) any later version.       //
26 //                                                       //
27 // SAGA is distributed in the hope that it will be       //
28 // useful, but WITHOUT ANY WARRANTY; without even the    //
29 // implied warranty of MERCHANTABILITY or FITNESS FOR A  //
30 // PARTICULAR PURPOSE. See the GNU General Public        //
31 // License for more details.                             //
32 //                                                       //
33 // You should have received a copy of the GNU General    //
34 // Public License along with this program; if not, see   //
35 // <http://www.gnu.org/licenses/>.                       //
36 //                                                       //
37 //-------------------------------------------------------//
38 //                                                       //
39 //    e-mail:     aringel@gwdg.de                        //
40 //                                                       //
41 //    contact:    Andre Ringeler                         //
42 //                Institute of Geography                 //
43 //                University of Goettingen               //
44 //                Goldschmidtstr. 5                      //
45 //                37077 Goettingen                       //
46 //                Germany                                //
47 //                                                       //
48 ///////////////////////////////////////////////////////////
49 
50 //---------------------------------------------------------
51 #include "Grid_Plotter.h"
52 
53 
54 ///////////////////////////////////////////////////////////
55 //														 //
56 //														 //
57 //														 //
58 ///////////////////////////////////////////////////////////
59 
60 //---------------------------------------------------------
CGrid_Plotter(void)61 CGrid_Plotter::CGrid_Plotter(void)
62 {
63 	Set_Name	(_TL("Function Plotter"));
64 
65 	Set_Author	("A.Ringeler (c) 2003");
66 
67 	CSG_String	s(_TW(
68 		"Generate a grid based on a functional expression. "
69 		"The function interpreter uses an formula expression "
70 		"parser that offers the following operators:\n"
71 	));
72 
73 	s	+= CSG_Formula::Get_Help_Operators(true);
74 
75 	Set_Description(s);
76 
77 	//-----------------------------------------------------
78 	Parameters.Add_String("",
79 		"FORMULA"	, _TL("Formula"),
80 		_TL(""),
81 		"sin(x*x + y*y)"
82 	);
83 
84 	Parameters.Add_Range("FORMULA",
85 		"X_RANGE"	, _TL("X Range"),
86 		_TL(""),
87 		0.0, 10.0
88 	);
89 
90 	Parameters.Add_Range("FORMULA",
91 		"Y_RANGE"	, _TL("Y Range"),
92 		_TL(""),
93 		0., 10.
94 	);
95 
96 	//-----------------------------------------------------
97 	m_Grid_Target.Create(&Parameters, false, "", "TARGET_");
98 
99 	m_Grid_Target.Add_Grid("FUNCTION", _TL("Function"), false);
100 }
101 
102 
103 ///////////////////////////////////////////////////////////
104 //														 //
105 ///////////////////////////////////////////////////////////
106 
107 //---------------------------------------------------------
On_Parameters_Enable(CSG_Parameters * pParameters,CSG_Parameter * pParameter)108 int CGrid_Plotter::On_Parameters_Enable(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
109 {
110 	m_Grid_Target.On_Parameters_Enable(pParameters, pParameter);
111 
112 	return( CSG_Tool::On_Parameters_Enable(pParameters, pParameter) );
113 }
114 
115 
116 ///////////////////////////////////////////////////////////
117 //														 //
118 ///////////////////////////////////////////////////////////
119 
120 //---------------------------------------------------------
On_Execute(void)121 bool CGrid_Plotter::On_Execute(void)
122 {
123 	CSG_Formula	Formula;
124 
125 	if( !Formula.Set_Formula(Parameters("FORMULA")->asString()) )
126 	{
127 		CSG_String	Message;
128 
129 		if( !Formula.Get_Error(Message) )
130 		{
131 			Message	= _TL("unknown errror parsing formula");
132 		}
133 
134 		Error_Set(Message);
135 
136 		return( false );
137 	}
138 
139 	//-----------------------------------------------------
140 	CSG_Grid	*pFunction	= m_Grid_Target.Get_Grid("FUNCTION");
141 
142 	if( !pFunction )
143 	{
144 		Error_Set(_TL("could not create target grid"));
145 
146 		return( false );
147 	}
148 
149 	//-----------------------------------------------------
150 	double xMin		= Parameters("X_RANGE.MIN")->asDouble();
151 	double xRange	= Parameters("X_RANGE.MAX")->asDouble() - xMin;
152 
153 	double yMin		= Parameters("Y_RANGE.MIN")->asDouble();
154 	double yRange	= Parameters("Y_RANGE.MAX")->asDouble() - yMin;
155 
156 	//-----------------------------------------------------
157 	for(int y=0; y<pFunction->Get_NY() && Set_Progress(y); y++)
158 	{
159 		Formula.Set_Variable('y', yMin + yRange * (y / (double)pFunction->Get_NY()));
160 
161 		#pragma omp parallel for
162 		for(int x=0; x<pFunction->Get_NX(); x++)
163 		{
164 			pFunction->Set_Value(x, y, Formula.Get_Value(xMin + xRange * (x / (double)pFunction->Get_NX())));
165 		}
166 	}
167 
168 	//-----------------------------------------------------
169 	return( true );
170 }
171 
172 
173 ///////////////////////////////////////////////////////////
174 //														 //
175 //														 //
176 //														 //
177 ///////////////////////////////////////////////////////////
178 
179 //---------------------------------------------------------
180