Debian Codegraph @

src/OctaMapSamp.h

raw symbols SCIP
1
2 class OctaMapSamp{
3 public:
4
5 int TotTexSizeX(){
6 return size;
7 }
8
9 int TotTexSizeY(){
10 return size;
11 }
12
13 static vector<int> map; // mappa 2d di indici a dir
14 static vector<Point3f> dir; // direzioni (uniche!)
15 static vector<Point3f> dirrot; // direzioni ruotate (uniche!)
16
17 vector<float> sum; // somme (uniche!)
18 vector<float> div; // divisioni (uniche!)
19
20 static int size;
21
22 void Shade(){
23 //col.resize(dir.size());
24 /*for (int i=0; i<dir.size(); i++) {
25 col[i]=dir[i]*Point3d(0.6,0.8,0);
26 if (col[i]<0) col[i]*=-1;
27 if (col[i]>1) col[i]=1;
28 }*/
29 }
30
31 static inline int nsamp(){
32 return dir.size();
33 }
34
35
36 // duplicates those texels that are already douplicated
37 void DuplicateTexels(vector<Byte> &t, int s, int tx, int ty){
38 int e=size-1; // the end
39 // four corners
40 int k0=(tx+ (ty )*s)*3;
41 int k1=(tx+e+(ty )*s)*3;
42 int k2=(tx+e+(ty+e)*s)*3;
43 int k3=(tx+ (ty+e)*s)*3;
44 t[k0 ]=t[k1 ]=t[k2 ]=t[k3 ];
45 t[k0+1]=t[k1+1]=t[k2+1]=t[k3+1];
46 t[k0+2]=t[k1+2]=t[k2+2]=t[k3+2];
47
48 // sides
49 for (int i=1; i<size/2; i++) {
50 int k0a=(tx + (ty +i )*s)*3;
51 int k0b=(tx + (ty +e-i)*s)*3;
52
53 int k1a=(tx+e + (ty +i )*s)*3;
54 int k1b=(tx+e + (ty +e-i)*s)*3;
55
56 int k2a=(tx+i + (ty )*s)*3;
57 int k2b=(tx+e-i+ (ty )*s)*3;
58
59 int k3a=(tx+i + (ty +e )*s)*3;
60 int k3b=(tx+e-i+ (ty +e )*s)*3;
61
62 t[k0a+0]=t[k0b+0]; t[k1a+0]=t[k1b+0]; t[k2a+0]=t[k2b+0]; t[k3a+0]=t[k3b+0];
63 t[k0a+1]=t[k0b+1]; t[k1a+1]=t[k1b+1]; t[k2a+1]=t[k2b+1]; t[k3a+1]=t[k3b+1];
64 t[k0a+2]=t[k0b+2]; t[k1a+2]=t[k1b+2]; t[k2a+2]=t[k2b+2]; t[k3a+2]=t[k3b+2];
65 }
66 }
67
68
69 void FillTexture(vector<Byte> &t, int s, int tx, int ty,
70 float cr,float cg,float cb){
71 for (int y=0; y<size; y++) {
72 for (int x=0; x<size; x++)
73 {
74 int k=(x+tx+(y+ty)*s)*3;
75 //Byte shade=(int)( col[ Index( x , y ) ] );
76 /*t[k++]= shade;
77 t[k++]= shade;
78 t[k++]= shade;*/
79
80 Point3f p=dir[ Index( x , y ) ];
81
82 Point3f q=(p+Point3f(1,1,1))/2.0*255.0;
83 t[k++]= (Byte)q[0];
84 t[k++]= (Byte)q[1];
85 t[k++]= (Byte)q[2];
86
87 /*int shade=(int)(p*Point3d(0.8,0.6,0)*255.0);
88 if (shade<0) shade=-shade/2;
89 if (shade>255) shade=255;
90 t[k++]= shade;
91 t[k++]= shade;
92 t[k++]= shade;*/
93
94
95 /* float i=255.0*float(map[Index( x , y )])/(size*size*size);
96 t[k++]= (int)i;
97 t[k++]= (int)i;
98 t[k++]= (int)i;
99
100
101 int r=0, g=0, b=0;
102 if (y<size) r=255;
103 if (x<size) b=255; if (x>=size*2) g=255;
104 k=(x+(y)*s)*3;
105 t[k++]= t[k++]/2+r/2;
106 t[k++]= t[k++]/2+g/2;
107 t[k++]= t[k++]/2+b/2;
108 */
109
110 /*
111 int r=0, g=0, b=0;
112 if (y<size) r=255;
113 if (x<size) b=255; if (x>=size*2) g=255;
114 t[k++]= r;
115 t[k++]= g;
116 t[k++]= b;*/
117 // printf("%4d ",map[Index( x , y )]);
118 //printf("%3d ",shade);
119
120 }
121 //printf("\n");
122 }
123
124 }
125
126 inline static int Index(int x, int y){
127 return x+y*size;
128 }
129
130 inline static float sign(float x){
131 return (x<0)?-1:+1;
132 }
133 inline static float Abs(float x){
134 return (x<0)?-x:+x;
135 }
136
137 void Zero(){
138 int n=dir.size();
139 sum.resize(n,0);
140 div.resize(n,0);
141 }
142
143 // smoothing of an octamap!
144 void Smooth(vector<Byte> &t, int s, int tx, int ty){
145 vector<int> oldvalue(size*size*6);
146 // copy old values
147 for (int y=0; y<size*2; y++)
148 for (int x=0; x<size*3; x++)
149 {
150 int k=(x+tx+(y+ty)*s)*3;
151 int i= Index( x , y );
152 oldvalue[i]=t[k];
153 }
154
155 int dy=size, dx=1;
156
157 int e=size-1;
158 // smooth old values
159 for (int y=0; y<size; y++)
160 for (int x=0; x<size; x++)
161 {
162 /* int i= Index( x , y );
163 int sum=oldvalue[i];
164
165 if (y!=0) sum+=oldvalue[i-dy];
166 else sum+=oldvalue[ Index( e-x , 1 ) ];
167
168 if (x!=0) sum+=oldvalue[i-dx];
169 else sum+=oldvalue[ Index( 1 , e-y ) ];
170
171 if (y!=e) sum+=oldvalue[i+dy];
172 else sum+=oldvalue[ Index( e-x ,e-1 ) ];
173
174 if (x!=e) sum+=oldvalue[i+dx];
175 else sum+=oldvalue[ Index( e-1 , e-y ) ];
176
177 sum=(sum+3)/5;
178 */
179 int i= Index( x , y );
180 const int TH=2;
181 int sum=oldvalue[i];
182 int ddiv=1;
183 int w;
184
185 if (y!=0) w=oldvalue[i-dy];
186 else w=oldvalue[ Index( e-x , 1 ) ];
187 if(w>TH) {sum+=w; ddiv++; }
188
189 if (x!=0) w=oldvalue[i-dx];
190 else w=oldvalue[ Index( 1 , e-y ) ];
191 if(w>TH) {sum+=w; ddiv++; }
192
193 if (y!=e) w=oldvalue[i+dy];
194 else w=oldvalue[ Index( e-x ,e-1 ) ];
195 if(w>TH) {sum+=w; ddiv++; }
196
197 if (x!=e) w=oldvalue[i+dx];
198 else w=oldvalue[ Index( e-1 , e-y ) ];
199 if(w>TH) {sum+=w; ddiv++; }
200
201 sum=(sum+ddiv/2)/ddiv;
202
203
204 int k=(x+tx+(y+ty)*s)*3;
205 t[k]=t[k+1]=t[k+2]=sum;
206 }
207 }
208
209 // smoothing of an octamap!
210 void Smooth(vector<int> &t, int s, int tx, int ty){
211 vector<int> oldvalue(size*size*6);
212 // copy old values
213 for (int y=0; y<size*2; y++)
214 for (int x=0; x<size*3; x++)
215 {
216 int k=(x+tx+(y+ty)*s);
217 int i= Index( x , y );
218 oldvalue[i]=t[k];
219 }
220
221 int dy=size, dx=1;
222
223 int e=size-1;
224 // smooth old values
225 for (int y=0; y<size; y++)
226 for (int x=0; x<size; x++)
227 {
228 int i= Index( x , y );
229 const int TH=5;
230 int sum=oldvalue[i];
231 int ddiv=1;
232 int w;
233
234 if (y!=0) w=oldvalue[i-dy];
235 else w=oldvalue[ Index( e-x , 1 ) ];
236 if(w>TH) {sum+=w; ddiv++; }
237
238 if (x!=0) w=oldvalue[i-dx];
239 else w=oldvalue[ Index( 1 , e-y ) ];
240 if(w>TH) {sum+=w; ddiv++; }
241
242 if (y!=e) w=oldvalue[i+dy];
243 else w=oldvalue[ Index( e-x ,e-1 ) ];
244 if(w>TH) {sum+=w; ddiv++; }
245
246 if (x!=e) w=oldvalue[i+dx];
247 else w=oldvalue[ Index( e-1 , e-y ) ];
248 if(w>TH) {sum+=w; ddiv++; }
249
250 sum=(sum+ddiv/2)/ddiv;
251
252 int k=(x+tx+(y+ty)*s);
253 t[k]=sum;
254 }
255 }
256
257 static void SetSize(int _size){
258 size=_size;
259 initMap();
260 ComputeWeight();
261 }
262
263 static Point3f getDir(float x, float y){
264 float fs=float(size)-1;
265 Point3f p(x*2/fs-1,y*2/fs-1,0);
266 float ax=Abs(p[0]), ay=Abs(p[1]), az=+1;
267 if (ax+ay>1.0) {
268 p=Point3f( sign(p[0])*(1-ay),
269 sign(p[1])*(1-ax), 0);
270 az=-1;
271 }
272 p[2]=(1-ax-ay)*az;
273 p.Normalize();
274 return p;
275 }
276
277 static void initMap(){
278 dir.resize(size*size);
279
280 for (int y=0; y<size; y++)
281 for (int x=0; x<size; x++)
282 dir[Index(x,y)]=getDir(x,y);
283 }
284
285 static vector<float> weight;
286
287 static float Area(Point3f a, Point3f b, Point3f c){
288 return Abs( ((b-a)^(c-a)).Norm()*0.5 );
289 }
290
291 static void ComputeWeight(){
292 weight.resize(size*size);
293
294 for (int y=0,k=0; y<size; y++)
295 for (int x=0; x<size; x++,k++){
296 float h=0.5;
297
298 Point3f p00=getDir(x-h,y-h);
299 Point3f p01=getDir(x-h,y+0);
300 Point3f p02=getDir(x-h,y+h);
301 Point3f p10=getDir(x+0,y-h);
302 Point3f p11=getDir(x+0,y+0);
303 Point3f p12=getDir(x+0,y+h);
304 Point3f p20=getDir(x+h,y-h);
305 Point3f p21=getDir(x+h,y+0);
306 Point3f p22=getDir(x+h,y+h);
307
308 float tota=0; int c=0; int e=size-1;
309
310 if ( (x!=0) && (y!=0) ){
311 tota+=Area( p00, p10, p01 );
312 tota+=Area( p10, p11, p01 );
313 c++;
314 }
315 if ( (x!=0) && (y!=e) ){
316 tota+=Area( p01, p11, p12 );
317 tota+=Area( p01, p12, p02 );
318 c++;
319 }
320 if ( (x!=e) && (y!=0) ){
321 tota+=Area( p10, p20, p21 );
322 tota+=Area( p21, p11, p10 );
323 c++;
324 }
325 if ( (x!=e) && (y!=e) ){
326 tota+=Area( p11, p21, p12 );
327 tota+=Area( p21, p22, p12 );
328 c++;
329 }
330 weight[k]=1.0/(tota*4/c);
331 //printf("%8.6f%c",weight[k], (x%size==size-1)?'\n':' ' );
332 }
333 }
334
335 void FillTexture(vector<Byte> &texture, const vector<int> &sumtable,
336 int texsize, float div, int tx, int ty );
337
338 OctaMapSamp(){ }
339
340 };
341
342
343