1 /**********************************************************
2  * Version $Id$
3  *********************************************************/
4 // diverses.cc
5 
6 //#include "../stdafx.h"
7 #include <iostream>
8 #include <sstream>
9 #include <math.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <time.h>
13 
14 #include "grid_bsl.h"
15 #include "diverses.h"
16 
17 #include <vector>
18 #include <algorithm>
19 
20 using namespace std;
21 
22 #define false 0
23 #define true 1
24 
25 #ifdef _DEBUG
26 #define new DEBUG_NEW
27 #endif
28 
29 using namespace std;
30 
31 
32 /*int Rand(const I_Vec& I, const GridWerte& G, int abstand)
33 {
34 	IVecWerte W((GridWerte &) G);
35 	return Rand(I, W, abstand);
36 }
37 */
38 /*
39 int Rand(const I_Vec& I, const IVecWerte& W, int abstand)
40 {
41 	return Rand(I.X(), I.Y(), W(), abstand);
42 
43 }
44 */
Rand(int x,int y,const GridWerte & W,int abstand)45 bool Rand(int x, int y, const GridWerte& W, int abstand )
46 {
47 	return (x == abstand || x == W.xanz-1-abstand
48 		|| y == abstand || y == W.yanz-1-abstand);
49 }
50 
51 /*
52 int innerhalb(const I_Vec& I, const GridWerte& W)
53 {
54 	int x = I.X();
55 	int y = I.Y();
56 
57 	return (x >= 0 && x < W.xanz && y >= 0 && y < W.yanz);
58 }
59 
60 int innerhalb(const I_Vec& I, const IVecWerte& W)
61 {
62 	int x = I.X();
63 	int y = I.Y();
64 
65 	return (x >= 0 && x < W().xanz && y >= 0 && y < W().yanz);
66 }
67 */
innerhalb(int x,int y,const GridWerte & W)68 int innerhalb(int x, int y, const GridWerte& W)
69 {
70 	return (x >= 0 && x < W.xanz && y >= 0 && y < W.yanz);
71 }
72 
73 
operator <<(ostream & o,const C_Vec2 & V)74 ostream& operator << (ostream& o, const C_Vec2& V)
75 {
76 	o << "(" << V.X() << ", " << V.Y() << ")";
77 	return o;
78 }
79 
normieren(C_Vec2 & G)80 void normieren(C_Vec2& G)
81 	// Normiert einen Vektor
82 {
83 	double r = G.Length();
84 	G /= r;
85 }
86 
87 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
88 //    min3 max3
89 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
90 
91 
max3(double x,double y,double z)92 double max3(double x, double y, double z)
93 {
94 	return( x > y ? (x > z ? x : z) : (y > z ? y : z) );
95 }
96 
min3(double x,double y,double z)97 double min3(double x, double y, double z)
98 {
99 	return( x < y ? (x < z ? x : z) : (y < z ? y : z) );
100 }
101 
102 
103 static int
NachbarBesetzt(int y0,int y1,int x0,int x1,int y,int x,GridWerte & G)104 NachbarBesetzt(int y0, int y1, int x0, int x1, int y, int x,
105 			   GridWerte& G)
106 {
107 	for (int i=y0; i<=y1; i++)
108 		for (int j=x0; j<=x1; j++)
109 			if (i == -1 || i == 1 || j == -1 || j == 1)
110 			{
111 				if (innerhalb(x+j, y+i, G))
112 					if (G(x+j,y+i) != 0.0)
113 						return true;
114 			}
115 	return false;
116 }
117 
118 
119 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
120 //  DeleteNotKonvex
121 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
122 
123 
124 
125 
126 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
127 //     LinRand
128 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
129 
130 void
LinRand(GridWerte & G,GridWerte & Erg)131 LinRand( GridWerte& G, GridWerte& Erg)
132 {
133 	Erg = G;
134 	Erg.xanz += 2;
135 	Erg.yanz += 2;
136 	Erg.xll -= Erg.dxy;
137 	Erg.yll -= Erg.dxy;
138 	Erg.getMem();
139 
140 	int i;
141 	int j;
142 	long yyy = G.yanz;
143 	long xxx = G.xanz;
144 
145 //	for (i=0; i<G.yanz; i++)
146 //		for (j=0; j<G.xanz; j++)
147 	for (i=0; i<yyy; i++)
148 		for (j=0; j<xxx; j++)
149 			Erg.Set_Value(j+1,i+1, G(j,i));
150 
151 	// linke  Kante
152 	j = 0;
153 	for (i=1; i<=G.yanz; i++)
154 		Erg.Set_Value(j,i, 2.0*G(j,i-1)-G(j+1,i-1));
155 
156 	// rechte Kante
157 	j = G.xanz-1;
158 
159 	for (i=1; i<=G.yanz; i++)
160 		Erg.Set_Value(j+2,i, 2.0*G(j,i-1)-G(j-1,i-1));
161 
162 	// untere Kante
163 	i = 0;
164 //	for (j=1; j<=G.xanz; j++)
165 	for (j=1; j<=xxx; j++)
166 		Erg.Set_Value(j,i, 2.0*G(j-1,i)-G(j-1,i+1));
167 
168 	// obere Kante
169 	i = G.yanz-1;
170 //	for (j=1; j<=G.xanz; j++)
171 	for (j=1; j<=xxx; j++)
172 		Erg.Set_Value(j,i+2, 2.0*G(j-1,i)-G(j-1,i-1));
173 
174 	double m1, m2;
175 	// Ecke ul
176 	m1 = 2*Erg(0,1)-Erg(0,2);
177 	m2 = 2*Erg(1,0)-Erg(2,0);
178 	Erg.Set_Value(0,0, (m1+m2)/2, true);
179 
180 	// Ecke ur
181 	m1 = 2*Erg(Erg.xanz-1,1)-Erg(Erg.xanz-1,2);
182 	m2 = 2*Erg(Erg.xanz-2,0)-Erg(Erg.xanz-3,0);
183 	Erg.Set_Value(Erg.xanz-1,0, (m1+m2)/2, true);
184 
185 	// Ecke ol
186 	m1 = 2*Erg(0,Erg.yanz-2)-Erg(0,Erg.yanz-3);
187 	m2 = 2*Erg(1,Erg.yanz-1)-Erg(2,Erg.yanz-1);
188 	Erg.Set_Value(0,Erg.yanz-1, (m1+m2)/2, true);
189 
190 	// Ecke or
191 	m1 = 2*Erg(Erg.xanz-1,Erg.yanz-2)-Erg(Erg.xanz-1,Erg.yanz-3);
192 	m2 = 2*Erg(Erg.xanz-2,Erg.yanz-1)-Erg(Erg.xanz-3,Erg.yanz-1);
193 
194 	Erg.Set_Value(Erg.xanz-1,Erg.yanz-1, (m1+m2)/2, true);
195 
196 	Erg.calcMinMax();
197 }
198 
199 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
200 //        RandHinzu
201 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
202 
203 
copyGrid(GridWerte & Dest,GridWerte & Source,bool newmem)204 void copyGrid(GridWerte &Dest,  GridWerte &Source, bool newmem)
205 {
206 	if (newmem)
207 	{
208 		Dest = Source;
209 		Dest.getMem();
210 	}
211 
212 	for (int i=0; i<Source.yanz; i++)
213 	{
214 		for (int j=0; j<Source.xanz; j++)
215 		{
216 			double wert= Source(j,i);
217 
218 			Dest.Set_Value(j,i, wert);
219 		}
220 	}
221 
222 
223 }
224 
225 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
226 //        Expositions-Abweichung
227 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
228 
calcExpoAbweichung(GridWerte & Erg,GridWerte & Expo)229 void calcExpoAbweichung(GridWerte& Erg,  GridWerte& Expo)
230 {
231 	int		i;
232 
233 	double w[9];
234 	// Standard-Expositionen zur mittleren Rasterzelle
235 	w[0] = double( 45.0/180.0*M_PI);  // -1 -1
236 	w[1] = double(  0.0/180.0*M_PI);  //  0 -1
237 	w[2] = double(315.0/180.0*M_PI);  //  1 -1
238 
239 	w[3] = double( 90.0/180.0*M_PI);  // -1  0
240 	w[4] = double(  0.0/180.0*M_PI);  //  0  0 egal, wird nicht benutzt
241 	w[5] = double(270.0/180.0*M_PI);  //  1  0
242 
243 	w[6] = double(135.0/180.0*M_PI);  // -1 +1
244 	w[7] = double(180.0/180.0*M_PI);  //  0 +1
245 	w[8] = double(225.0/180.0*M_PI);  //  1 +1
246 
247 	Erg = Expo;
248 	Erg.getMem();
249 	for (i=0; i<Erg.yanz; i++)
250 	{
251 		for (int j=0; j<Erg.xanz; j++)
252 			Erg.Set_Value(j,i, 0.0f);
253 	}
254 
255 	int zaehler;
256 	double winkel;
257 	for (i=0; i<Erg.yanz; i++)
258 	{
259 		for (int j=0; j<Erg.xanz; j++)
260 		{
261 			zaehler = 0;
262 			for (int ii=-1; ii<=1; ii++)
263 			{
264 				for (int jj=-1; jj<=1; jj++)
265 				{
266 					if ((ii != 0 || j != 0) && innerhalb(j+jj,i+ii, Erg))
267 					{
268 						winkel = fabs(w[(ii+1)*3+jj+1]-Expo(j+jj,i+ii));
269 						if (winkel > M_PI)
270 							winkel = 2*M_PI-winkel;
271 						Erg.Add_Value(j,i, winkel/M_PI);
272 						zaehler++;
273 					}
274 				}
275 			}
276 			if (zaehler != 0)
277 				Erg.Mul_Value(j,i, 1.0 / zaehler);
278 		}
279 	}
280 
281 }
282 
calcMittelwert(GridWerte & H,int x,int y)283 static double calcMittelwert( GridWerte& H, int x, int y)
284 {
285 	if (x != H.xanz-1 && y != H.yanz-1)
286 		return (H(x,y) + H(x,y+1) + H(x+1,y) + H(x+1,y+1))/4.0f;
287 	else if (x == H.xanz-1)
288 	{
289 		return (H(x,y) + H(x,y+1))/2.0f;
290 	}
291 	else
292 	{
293 		return (H(x,y) + H(x+1,y))/2.0f;
294 	}
295 }
296 
297 
298 
299 
300 
301