1 
2 ///////////////////////////////////////////////////////////
3 //                                                       //
4 //                         SAGA                          //
5 //                                                       //
6 //      System for Automated Geoscientific Analyses      //
7 //                                                       //
8 //                     Tool Library                      //
9 //                     grid_spline                       //
10 //                                                       //
11 //-------------------------------------------------------//
12 //                                                       //
13 //              Gridding_Spline_TPS_Local.cpp            //
14 //                                                       //
15 //                 Copyright (C) 2006 by                 //
16 //                      Olaf Conrad                      //
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:     oconrad@saga-gis.org                   //
40 //                                                       //
41 //    contact:    Olaf Conrad                            //
42 //                Institute of Geography                 //
43 //                University of Goettingen               //
44 //                Goldschmidtstr. 5                      //
45 //                37077 Goettingen                       //
46 //                Germany                                //
47 //                                                       //
48 ///////////////////////////////////////////////////////////
49 
50 //---------------------------------------------------------
51 #include "Gridding_Spline_TPS_Local.h"
52 
53 
54 ///////////////////////////////////////////////////////////
55 //														 //
56 //														 //
57 //														 //
58 ///////////////////////////////////////////////////////////
59 
60 //---------------------------------------------------------
CGridding_Spline_TPS_Local(void)61 CGridding_Spline_TPS_Local::CGridding_Spline_TPS_Local(void)
62 {
63 	Set_Name		(_TL("Thin Plate Spline"));
64 
65 	Set_Author		("O.Conrad (c) 2006");
66 
67 	Set_Description	(_TW(
68 		"Creates a 'Thin Plate Spline' function for each grid point "
69 		"based on all of the scattered data points that are within a "
70 		"given distance. The number of points can be limited to a "
71 		"maximum number of closest points."
72 	));
73 
74 	Add_Reference("Donato G., Belongie S.", "2002",
75 		"Approximation Methods for Thin Plate Spline Mappings and Principal Warps",
76 		"In Heyden, A., Sparr, G., Nielsen, M., Johansen, P. (Eds.): Computer Vision - ECCV 2002: 7th European Conference on Computer Vision, Copenhagen, Denmark, May 28-31, 2002, "
77 		"Proceedings, Part III, Lecture Notes in Computer Science., Springer-Verlag Heidelberg; pp.21-31."
78 	);
79 
80 	Add_Reference("Elonen, J.", "2005",
81 		"Thin Plate Spline editor - an example program in C++",
82 		"",
83 		SG_T("http://elonen.iki.fi/code/tpsdemo/index.html"
84 	));
85 
86 	//-----------------------------------------------------
87 	Parameters.Add_Double(
88 		"", "REGULARISATION"	, _TL("Regularisation"),
89 		_TL(""),
90 		0.0001, 0., true
91 	);
92 
93 	//-----------------------------------------------------
94 	m_Search.Create(&Parameters, Parameters.Add_Node("", "NODE_SEARCH", _TL("Search Options"), _TL("")), 16);
95 }
96 
97 
98 ///////////////////////////////////////////////////////////
99 //														 //
100 ///////////////////////////////////////////////////////////
101 
102 //---------------------------------------------------------
On_Parameter_Changed(CSG_Parameters * pParameters,CSG_Parameter * pParameter)103 int CGridding_Spline_TPS_Local::On_Parameter_Changed(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
104 {
105 	if( pParameter->Cmp_Identifier("SHAPES") )
106 	{
107 		m_Search.On_Parameter_Changed(pParameters, pParameter);
108 	}
109 
110 	return( CGridding_Spline_Base::On_Parameter_Changed(pParameters, pParameter) );
111 }
112 
113 //---------------------------------------------------------
On_Parameters_Enable(CSG_Parameters * pParameters,CSG_Parameter * pParameter)114 int CGridding_Spline_TPS_Local::On_Parameters_Enable(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
115 {
116 	m_Search.On_Parameters_Enable(pParameters, pParameter);
117 
118 	return( CGridding_Spline_Base::On_Parameters_Enable(pParameters, pParameter) );
119 }
120 
121 
122 ///////////////////////////////////////////////////////////
123 //														 //
124 ///////////////////////////////////////////////////////////
125 
126 //---------------------------------------------------------
On_Execute(void)127 bool CGridding_Spline_TPS_Local::On_Execute(void)
128 {
129 	double	Regularization	= Parameters("REGULARISATION")->asDouble();
130 
131 	//-----------------------------------------------------
132 	if( m_Search.Do_Use_All(true) )	// global
133 	{
134 		CSG_Thin_Plate_Spline	Spline;
135 
136 		if( !Initialize(Spline.Get_Points()) || !Spline.Create(Regularization, false) )
137 		{
138 			return( false );
139 		}
140 
141 		for(int y=0; y<m_pGrid->Get_NY() && Set_Progress(y, m_pGrid->Get_NY()); y++)
142 		{
143 			double	yWorld	= m_pGrid->Get_YMin() + y * m_pGrid->Get_Cellsize();
144 
145 			#pragma omp parallel for
146 			for(int x=0; x<m_pGrid->Get_NX(); x++)
147 			{
148 				double	xWorld	= m_pGrid->Get_XMin() + x * m_pGrid->Get_Cellsize();
149 
150 				m_pGrid->Set_Value(x, y, Spline.Get_Value(xWorld, yWorld));
151 			}
152 		}
153 	}
154 
155 	//-----------------------------------------------------
156 	else
157 	{
158 		if( !Initialize() || !m_Search.Initialize(Parameters("SHAPES")->asShapes(), Parameters("FIELD")->asInt()) )
159 		{
160 			return( false );
161 		}
162 
163 		for(int y=0; y<m_pGrid->Get_NY() && Set_Progress(y, m_pGrid->Get_NY()); y++)
164 		{
165 			double	yWorld	= m_pGrid->Get_YMin() + y * m_pGrid->Get_Cellsize();
166 
167 			#pragma omp parallel for
168 			for(int x=0; x<m_pGrid->Get_NX(); x++)
169 			{
170 				double	xWorld	= m_pGrid->Get_XMin() + x * m_pGrid->Get_Cellsize();
171 
172 				Set_Value(x, y, xWorld, yWorld, Regularization);
173 			}
174 		}
175 
176 		m_Search.Finalize();
177 	}
178 
179 	//-----------------------------------------------------
180 	return( true );
181 }
182 
183 
184 ///////////////////////////////////////////////////////////
185 //														 //
186 ///////////////////////////////////////////////////////////
187 
188 //---------------------------------------------------------
Set_Value(int x,int y,double xWorld,double yWorld,double Regularization)189 bool CGridding_Spline_TPS_Local::Set_Value(int x, int y, double xWorld, double yWorld, double Regularization)
190 {
191 	CSG_Points_Z	Points;
192 
193 	if( m_Search.Get_Points(xWorld, yWorld, Points) && Points.Get_Count() > 2 )
194 	{
195 		CSG_Thin_Plate_Spline	Spline;
196 
197 		for(int i=0; i<Points.Get_Count(); i++)
198 		{
199 			Spline.Add_Point(Points[i].x, Points[i].y, Points[i].z);
200 		}
201 
202 		if( Spline.Create(Regularization, true) )
203 		{
204 			m_pGrid->Set_Value(x, y, Spline.Get_Value(xWorld, yWorld));
205 
206 			return( true );
207 		}
208 	}
209 
210 	//-----------------------------------------------------
211 	m_pGrid->Set_NoData(x, y);
212 
213 	return( false );
214 }
215 
216 
217 ///////////////////////////////////////////////////////////
218 //														 //
219 //														 //
220 //														 //
221 ///////////////////////////////////////////////////////////
222 
223 //---------------------------------------------------------
224