summaryrefslogtreecommitdiff
path: root/cad/plugins/QuteMol/qutemol/src/OctaMapSamp.h
blob: f0c2f897d758edada2e72eb320d2fc7766dbc88d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343

class OctaMapSamp{
public:
  
  int TotTexSizeX(){
    return size;
  }

  int TotTexSizeY(){
    return size;
  }
  
  static vector<int> map;     // mappa 2d di indici a dir
  static vector<Point3f> dir;    // direzioni (uniche!)
  static vector<Point3f> dirrot; // direzioni ruotate (uniche!)
  
  vector<float> sum;          // somme (uniche!)
  vector<float> div;          // divisioni (uniche!)

  static int size;

  void Shade(){
    //col.resize(dir.size());
    /*for (int i=0; i<dir.size(); i++) {
      col[i]=dir[i]*Point3d(0.6,0.8,0);
      if (col[i]<0) col[i]*=-1;
      if (col[i]>1) col[i]=1;
    }*/
  }
  
  static inline int nsamp(){
    return dir.size();
  }

  
  // duplicates those texels that are already douplicated
  void DuplicateTexels(vector<Byte> &t, int s, int tx, int ty){
    int e=size-1; // the end
    // four corners
    int k0=(tx+  (ty  )*s)*3;
    int k1=(tx+e+(ty  )*s)*3;
    int k2=(tx+e+(ty+e)*s)*3;
    int k3=(tx+  (ty+e)*s)*3;
    t[k0  ]=t[k1  ]=t[k2  ]=t[k3  ];
    t[k0+1]=t[k1+1]=t[k2+1]=t[k3+1];
    t[k0+2]=t[k1+2]=t[k2+2]=t[k3+2];
    
    // sides 
    for (int i=1; i<size/2; i++) {
      int k0a=(tx    + (ty +i  )*s)*3;
      int k0b=(tx    + (ty +e-i)*s)*3;
      
      int k1a=(tx+e  + (ty +i  )*s)*3;
      int k1b=(tx+e  + (ty +e-i)*s)*3;
      
      int k2a=(tx+i  + (ty     )*s)*3;
      int k2b=(tx+e-i+ (ty     )*s)*3;
      
      int k3a=(tx+i  + (ty +e  )*s)*3;
      int k3b=(tx+e-i+ (ty +e  )*s)*3;

      t[k0a+0]=t[k0b+0]; t[k1a+0]=t[k1b+0]; t[k2a+0]=t[k2b+0]; t[k3a+0]=t[k3b+0];
      t[k0a+1]=t[k0b+1]; t[k1a+1]=t[k1b+1]; t[k2a+1]=t[k2b+1]; t[k3a+1]=t[k3b+1];
      t[k0a+2]=t[k0b+2]; t[k1a+2]=t[k1b+2]; t[k2a+2]=t[k2b+2]; t[k3a+2]=t[k3b+2];
    }
  }
  

  void FillTexture(vector<Byte> &t, int s, int tx, int ty,
                   float cr,float cg,float cb){
    for (int y=0; y<size; y++) {
    for (int x=0; x<size; x++) 
    {
      int k=(x+tx+(y+ty)*s)*3;
      //Byte shade=(int)( col[ Index( x , y ) ] );
      /*t[k++]= shade;
      t[k++]= shade;
      t[k++]= shade;*/
      
      Point3f p=dir[ Index( x , y ) ];

      Point3f q=(p+Point3f(1,1,1))/2.0*255.0;
        t[k++]= (Byte)q[0];
        t[k++]= (Byte)q[1];
        t[k++]= (Byte)q[2];

      /*int shade=(int)(p*Point3d(0.8,0.6,0)*255.0);
      if (shade<0) shade=-shade/2;
      if (shade>255) shade=255;
      t[k++]= shade;
      t[k++]= shade;
      t[k++]= shade;*/
        

/*      float i=255.0*float(map[Index( x , y )])/(size*size*size);
      t[k++]= (int)i;
      t[k++]= (int)i;
      t[k++]= (int)i;
      

      int r=0, g=0, b=0;
      if (y<size) r=255;
      if (x<size) b=255; if (x>=size*2) g=255; 
      k=(x+(y)*s)*3;
      t[k++]= t[k++]/2+r/2;
      t[k++]= t[k++]/2+g/2;
      t[k++]= t[k++]/2+b/2;
      */

      /*
      int r=0, g=0, b=0;
      if (y<size) r=255;
      if (x<size) b=255; if (x>=size*2) g=255; 
      t[k++]= r;
      t[k++]= g;
      t[k++]= b;*/
//      printf("%4d ",map[Index( x , y )]);
      //printf("%3d ",shade);

    }
    //printf("\n");
    }

  }
  
  inline static int Index(int x, int y){
    return x+y*size;
  }
  
  inline static float sign(float x){
    return (x<0)?-1:+1;
  }
  inline static float Abs(float x){
    return (x<0)?-x:+x;
  }  
  
  void Zero(){
    int n=dir.size();
    sum.resize(n,0);
    div.resize(n,0);
  }
  
  // smoothing of an octamap!
  void Smooth(vector<Byte> &t, int s, int tx, int ty){
    vector<int> oldvalue(size*size*6);
    // copy old values
    for (int y=0; y<size*2; y++) 
    for (int x=0; x<size*3; x++) 
    {
      int k=(x+tx+(y+ty)*s)*3;
      int i= Index( x , y );
      oldvalue[i]=t[k];
    }
    
    int dy=size, dx=1;
    
    int e=size-1;
    // smooth old values
    for (int y=0; y<size; y++) 
    for (int x=0; x<size; x++) 
    {
/*      int i= Index( x , y );
      int sum=oldvalue[i];
      
      if (y!=0)  sum+=oldvalue[i-dy];
      else       sum+=oldvalue[ Index( e-x , 1 ) ];
        
      if (x!=0)  sum+=oldvalue[i-dx];
      else       sum+=oldvalue[ Index( 1 , e-y ) ];
        
      if (y!=e)  sum+=oldvalue[i+dy];
      else       sum+=oldvalue[ Index( e-x ,e-1 ) ];
      
      if (x!=e)  sum+=oldvalue[i+dx];
      else       sum+=oldvalue[ Index( e-1 , e-y ) ];
      
      sum=(sum+3)/5;
*/      
      int i= Index( x , y );
      const int TH=2;
      int sum=oldvalue[i];
      int ddiv=1;
      int w;
      
      if (y!=0)  w=oldvalue[i-dy];
      else       w=oldvalue[ Index( e-x , 1 ) ];
      if(w>TH) {sum+=w; ddiv++; }
        
      if (x!=0)  w=oldvalue[i-dx];
      else       w=oldvalue[ Index( 1 , e-y ) ];
      if(w>TH) {sum+=w; ddiv++; }
        
      if (y!=e)  w=oldvalue[i+dy];
      else       w=oldvalue[ Index( e-x ,e-1 ) ];
      if(w>TH) {sum+=w; ddiv++; }
      
      if (x!=e)  w=oldvalue[i+dx];
      else       w=oldvalue[ Index( e-1 , e-y ) ];
      if(w>TH) {sum+=w; ddiv++; }
      
      sum=(sum+ddiv/2)/ddiv;


      int k=(x+tx+(y+ty)*s)*3;
      t[k]=t[k+1]=t[k+2]=sum;
    }
  }

  // smoothing of an octamap!
  void Smooth(vector<int> &t, int s, int tx, int ty){
    vector<int> oldvalue(size*size*6);
    // copy old values
    for (int y=0; y<size*2; y++) 
    for (int x=0; x<size*3; x++) 
    {
      int k=(x+tx+(y+ty)*s);
      int i= Index( x , y );
      oldvalue[i]=t[k];
    }
    
    int dy=size, dx=1;
    
    int e=size-1;
    // smooth old values
    for (int y=0; y<size; y++) 
    for (int x=0; x<size; x++) 
    {
      int i= Index( x , y );
      const int TH=5;
      int sum=oldvalue[i];
      int ddiv=1;
      int w;
      
      if (y!=0)  w=oldvalue[i-dy];
      else       w=oldvalue[ Index( e-x , 1 ) ];
      if(w>TH) {sum+=w; ddiv++; }
        
      if (x!=0)  w=oldvalue[i-dx];
      else       w=oldvalue[ Index( 1 , e-y ) ];
      if(w>TH) {sum+=w; ddiv++; }
        
      if (y!=e)  w=oldvalue[i+dy];
      else       w=oldvalue[ Index( e-x ,e-1 ) ];
      if(w>TH) {sum+=w; ddiv++; }
      
      if (x!=e)  w=oldvalue[i+dx];
      else       w=oldvalue[ Index( e-1 , e-y ) ];
      if(w>TH) {sum+=w; ddiv++; }
      
      sum=(sum+ddiv/2)/ddiv;
      
      int k=(x+tx+(y+ty)*s);
      t[k]=sum;
    }
  }
  
  static void SetSize(int _size){
    size=_size;
    initMap();
    ComputeWeight();
  }

  static Point3f getDir(float x, float y){
    float fs=float(size)-1;
    Point3f p(x*2/fs-1,y*2/fs-1,0);
    float ax=Abs(p[0]), ay=Abs(p[1]), az=+1;
    if (ax+ay>1.0) {
      p=Point3f( sign(p[0])*(1-ay),
                 sign(p[1])*(1-ax), 0);
      az=-1;
    }
    p[2]=(1-ax-ay)*az;
    p.Normalize();
    return p;
  }
  
  static void initMap(){
    dir.resize(size*size);
    
    for (int y=0; y<size; y++) 
    for (int x=0; x<size; x++) 
      dir[Index(x,y)]=getDir(x,y);
  }

  static vector<float> weight;
  
  static float Area(Point3f a, Point3f b, Point3f c){
    return Abs( ((b-a)^(c-a)).Norm()*0.5 );
  }
  
  static void ComputeWeight(){
    weight.resize(size*size);
    
    for (int y=0,k=0; y<size; y++)
    for (int x=0; x<size; x++,k++){
      float h=0.5;
      
      Point3f p00=getDir(x-h,y-h);
      Point3f p01=getDir(x-h,y+0);
      Point3f p02=getDir(x-h,y+h);
      Point3f p10=getDir(x+0,y-h);
      Point3f p11=getDir(x+0,y+0);
      Point3f p12=getDir(x+0,y+h);
      Point3f p20=getDir(x+h,y-h);
      Point3f p21=getDir(x+h,y+0);
      Point3f p22=getDir(x+h,y+h);
      
      float tota=0; int c=0; int e=size-1;

      if ( (x!=0) && (y!=0) ){
        tota+=Area( p00, p10, p01 );
        tota+=Area( p10, p11, p01 );
        c++;
      }
      if ( (x!=0) && (y!=e) ){
        tota+=Area( p01, p11, p12 );
        tota+=Area( p01, p12, p02 );
        c++;
      }
      if ( (x!=e) && (y!=0) ){
        tota+=Area( p10, p20, p21 );
        tota+=Area( p21, p11, p10 );
        c++;
      }
      if ( (x!=e) && (y!=e) ){
        tota+=Area( p11, p21, p12 );
        tota+=Area( p21, p22, p12 );
        c++;
      }
      weight[k]=1.0/(tota*4/c);
      //printf("%8.6f%c",weight[k], (x%size==size-1)?'\n':' ' );
    }
  }
  
  void FillTexture(vector<Byte> &texture, const vector<int> &sumtable, 
                   int texsize, float div, int tx, int ty );

  OctaMapSamp(){ }

};