1 
2 ///////////////////////////////////////////////////////////
3 //                                                       //
4 //                         SAGA                          //
5 //                                                       //
6 //      System for Automated Geoscientific Analyses      //
7 //                                                       //
8 //                     Tool Library                      //
9 //                  Grid_Visualisation                   //
10 //                                                       //
11 //-------------------------------------------------------//
12 //                                                       //
13 //                Grid_Color_Triangle.cpp                //
14 //                                                       //
15 //                 Copyright (C) 2008 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 "Grid_Color_Triangle.h"
52 
53 
54 ///////////////////////////////////////////////////////////
55 //														 //
56 //														 //
57 //														 //
58 ///////////////////////////////////////////////////////////
59 
60 //---------------------------------------------------------
61 #define METHOD_STRING	CSG_String::Format("%s|%s|%s|%s|%s",\
62 	_TL("0 - 1"),\
63 	_TL("Rescale to 0 - 1"),\
64 	_TL("User defined rescale"),\
65 	_TL("Percentiles"),\
66 	_TL("Percentage of standard deviation")\
67 ), 4
68 
69 
70 ///////////////////////////////////////////////////////////
71 //														 //
72 ///////////////////////////////////////////////////////////
73 
74 //---------------------------------------------------------
CGrid_Color_Triangle(void)75 CGrid_Color_Triangle::CGrid_Color_Triangle(void)
76 {
77 	Set_Name		(_TL("Color Triangle Composite"));
78 
79 	Set_Author		("O.Conrad (c) 2008");
80 
81 	Set_Description	(_TW(
82 		"Similar to 'RGB Composite', but the three colors representing intensity of each data set can be chosen by user. "
83 	));
84 
85 	//-----------------------------------------------------
86 	Parameters.Add_Grid  (""      , "A_GRID"   , _TL("A"                               ), _TL(""), PARAMETER_INPUT);
87 	Parameters.Add_Color ("A_GRID", "A_COLOR"  , _TL("Color"                           ), _TL(""), SG_COLOR_RED);
88 	Parameters.Add_Choice("A_GRID", "A_METHOD" , _TL("Value Preparation"               ), _TL(""), METHOD_STRING);
89 	Parameters.Add_Range ("A_GRID", "A_RANGE"  , _TL("Rescale Range"                   ), _TL(""), 0., 1.);
90 	Parameters.Add_Range ("A_GRID", "A_PERCTL" , _TL("Percentiles"                     ), _TL(""), 1., 99., 0., true, 100., true);
91 	Parameters.Add_Double("A_GRID", "A_PERCENT", _TL("Percentage of Standard Deviation"), _TL(""), 150., 0., true);
92 
93 	Parameters.Add_Grid  (""      , "B_GRID"   , _TL("B"                               ), _TL(""), PARAMETER_INPUT);
94 	Parameters.Add_Color ("B_GRID", "B_COLOR"  , _TL("Color"                           ), _TL(""), SG_COLOR_GREEN);
95 	Parameters.Add_Choice("B_GRID", "B_METHOD" , _TL("Value Preparation"               ), _TL(""), METHOD_STRING);
96 	Parameters.Add_Range ("B_GRID", "B_RANGE"  , _TL("Rescale Range"                   ), _TL(""), 0., 1.);
97 	Parameters.Add_Range ("B_GRID", "B_PERCTL" , _TL("Percentiles"                     ), _TL(""), 1., 99., 0., true, 100., true);
98 	Parameters.Add_Double("B_GRID", "B_PERCENT", _TL("Percentage of standard deviation"), _TL(""), 150., 0., true);
99 
100 	Parameters.Add_Grid  (""      , "C_GRID"   , _TL("C"                               ), _TL(""), PARAMETER_INPUT);
101 	Parameters.Add_Color ("C_GRID", "C_COLOR"  , _TL("Color"                           ), _TL(""), SG_COLOR_BLUE);
102 	Parameters.Add_Choice("C_GRID", "C_METHOD" , _TL("Value Preparation"               ), _TL(""), METHOD_STRING);
103 	Parameters.Add_Range ("C_GRID", "C_RANGE"  , _TL("Rescale Range"                   ), _TL(""), 0., 1.);
104 	Parameters.Add_Range ("C_GRID", "C_PERCTL" , _TL("Percentiles"                     ), _TL(""), 1., 99., 0., true, 100., true);
105 	Parameters.Add_Double("C_GRID", "C_PERCENT", _TL("Percentage of Standard Deviation"), _TL(""), 150., 0., true);
106 
107 	//-----------------------------------------------------
108 	Parameters.Add_Grid("",
109 		"GRID"	, _TL("Composite"),
110 		_TL(""),
111 		PARAMETER_OUTPUT, true, SG_DATATYPE_Int
112 	);
113 }
114 
115 
116 ///////////////////////////////////////////////////////////
117 //														 //
118 ///////////////////////////////////////////////////////////
119 
120 //---------------------------------------------------------
121 #define CHK_COLOR(C)	if( C > 255 )	C	= 255;	else if( C < 0 )	C	= 0;
122 
123 //---------------------------------------------------------
On_Execute(void)124 bool CGrid_Color_Triangle::On_Execute(void)
125 {
126 	int			x, y, Color[3], aC[3], bC[3], cC[3];
127 	double		aMin, bMin, cMin, aRange, bRange, cRange, a, b, c;
128 	CSG_Grid	*pA, *pB, *pC, *pRGB;
129 
130 	//-----------------------------------------------------
131 	pA		= _Get_Grid(Parameters("A_GRID")->asGrid(), Parameters("A_METHOD")->asInt(), Parameters("A_RANGE")->asRange(), Parameters("A_PERCTL")->asRange(), Parameters("A_PERCENT")->asDouble(), aMin, aRange);
132 	pB		= _Get_Grid(Parameters("B_GRID")->asGrid(), Parameters("B_METHOD")->asInt(), Parameters("B_RANGE")->asRange(), Parameters("B_PERCTL")->asRange(), Parameters("B_PERCENT")->asDouble(), bMin, bRange);
133 	pC		= _Get_Grid(Parameters("C_GRID")->asGrid(), Parameters("C_METHOD")->asInt(), Parameters("C_RANGE")->asRange(), Parameters("C_PERCTL")->asRange(), Parameters("C_PERCENT")->asDouble(), cMin, cRange);
134 
135 	x		= Parameters("A_COLOR")->asColor();
136 	aC[0]	= SG_GET_R(x);
137 	aC[1]	= SG_GET_G(x);
138 	aC[2]	= SG_GET_B(x);
139 
140 	x		= Parameters("B_COLOR")->asColor();
141 	bC[0]	= SG_GET_R(x);
142 	bC[1]	= SG_GET_G(x);
143 	bC[2]	= SG_GET_B(x);
144 
145 	x		= Parameters("C_COLOR")->asColor();
146 	cC[0]	= SG_GET_R(x);
147 	cC[1]	= SG_GET_G(x);
148 	cC[2]	= SG_GET_B(x);
149 
150 	//-----------------------------------------------------
151 	pRGB	= Parameters("GRID")->asGrid();
152 	pRGB->Create(pRGB->Get_System(), SG_DATATYPE_Int);
153 	DataObject_Set_Colors(pRGB, 11, SG_COLORS_BLACK_WHITE);
154 
155 	CSG_Parameters	Parms;
156 
157 	if( DataObject_Get_Parameters(pRGB, Parms) && Parms("COLORS_TYPE") )
158 	{
159 		Parms("COLORS_TYPE")->Set_Value(3);	// Color Classification Type: RGB
160 
161 		DataObject_Set_Parameters(pRGB, Parms);
162 	}
163 
164 	//-----------------------------------------------------
165 	for(y=0; y<Get_NY() && Set_Progress(y); y++)
166 	{
167 		for(x=0; x<Get_NX(); x++)
168 		{
169 			if( pA->is_NoData(x, y) || pB->is_NoData(x, y) || pC->is_NoData(x, y) )
170 			{
171 				pRGB->Set_NoData(x, y);
172 			}
173 			else
174 			{
175 				a	= aRange * (pA->asDouble(x, y) - aMin);	if( a > 1. )	a	= 1.;	else if( a < 0. )	a	= 0.;
176 				b	= bRange * (pB->asDouble(x, y) - bMin);	if( b > 1. )	b	= 1.;	else if( b < 0. )	b	= 0.;
177 				c	= cRange * (pC->asDouble(x, y) - cMin);	if( c > 1. )	c	= 1.;	else if( c < 0. )	c	= 0.;
178 
179 				Color[0]	= (int)((a * aC[0] + b * bC[0] + c * cC[0]));	CHK_COLOR(Color[0]);
180 				Color[1]	= (int)((a * aC[1] + b * bC[1] + c * cC[1]));	CHK_COLOR(Color[1]);
181 				Color[2]	= (int)((a * aC[2] + b * bC[2] + c * cC[2]));	CHK_COLOR(Color[2]);
182 
183 				pRGB->Set_Value(x, y, SG_GET_RGB(Color[0], Color[1], Color[2]));
184 			}
185 		}
186 	}
187 
188 	return( true );
189 }
190 
191 
192 ///////////////////////////////////////////////////////////
193 //														 //
194 //														 //
195 //														 //
196 ///////////////////////////////////////////////////////////
197 
198 //---------------------------------------------------------
_Get_Grid(CSG_Grid * pGrid,int Method,CSG_Parameter_Range * pRange,CSG_Parameter_Range * pPerctl,double Percent,double & Min,double & Range)199 CSG_Grid * CGrid_Color_Triangle::_Get_Grid(CSG_Grid *pGrid, int Method, CSG_Parameter_Range *pRange, CSG_Parameter_Range *pPerctl, double Percent, double &Min, double &Range)
200 {
201 	if( pGrid )
202 	{
203 		int		x, y;
204 		sLong	n;
205 
206 		switch( Method )
207 		{
208 		default:
209 		case 0:	// 0 - 1
210 			Min		= 0.;
211 			Range	= 1.;
212 			break;
213 
214 		case 1:	// Rescale to 0 - 1
215 			Min		= pGrid->Get_Min();
216 			Range	= pGrid->Get_Range();
217 			Range	= Range > 0. ? 1. / Range : 0.;
218 			break;
219 
220 		case 2:	// User defined rescale
221 			Min		= pRange->Get_Min();
222 			Range	= pRange->Get_Range();
223 			Range	= Range > 0. ? 1. / Range : 0.;
224 			break;
225 
226 		case 3:	// Normalise
227 			n		= (sLong)(pGrid->Get_NCells() * pPerctl->Get_Min() / 100.);
228 			pGrid->Get_Sorted(n < 0 ? 0 : (n >= pGrid->Get_NCells() ? pGrid->Get_NCells() - 1 : n), x, y, false, false);
229 			Min		= pGrid->asDouble(x, y);
230 			n		= (sLong)(pGrid->Get_NCells() * pPerctl->Get_Max() / 100.);
231 			pGrid->Get_Sorted(n < 0 ? 0 : (n >= pGrid->Get_NCells() ? pGrid->Get_NCells() - 1 : n), x, y, false, false);
232 			Range	= pGrid->asDouble(x, y) - Min;
233 			Range	= Range > 0. ? 1. / Range : 0.;
234 			break;
235 
236 		case 4:	// Standard deviation
237 			Range	= sqrt(pGrid->Get_Variance()) * Percent / 100.;
238 			Min		= pGrid->Get_Mean() - Range;
239 			Range	= Range * 2.;
240 			Range	= Range > 0. ? 1. / Range : 0.;
241 			break;
242 		}
243 	}
244 
245 	return( pGrid );
246 }
247 
248 
249 ///////////////////////////////////////////////////////////
250 //														 //
251 //														 //
252 //														 //
253 ///////////////////////////////////////////////////////////
254 
255 //---------------------------------------------------------
256