ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/REPOS_ERICCA/magic/lib/template/src/tpl_octree.h
Revision: 584
Committed: Thu Oct 30 21:06:47 2014 UTC (10 years, 6 months ago) by francois
Content type: text/plain
File size: 48194 byte(s)
Log Message:
resolution de la mausaise definition d'un octree initialise par une fonction carte de taille

File Contents

# User Rev Content
1 5 //------------------------------------------------------------
2     //------------------------------------------------------------
3     // MAGiC
4 francois 276 // Jean Christophe Cuilli�re et Vincent FRANCOIS
5     // D�partement de G�nie M�canique - UQTR
6 5 //------------------------------------------------------------
7 francois 276 // Le projet MAGIC est un projet de recherche du d�partement
8     // de g�nie m�canique de l'Universit� du Qu�bec �
9     // Trois Rivi�res
10     // Les librairies ne peuvent �tre utilis�es sans l'accord
11 5 // des auteurs (contact : francois@uqtr.ca)
12     //------------------------------------------------------------
13     //------------------------------------------------------------
14     //
15     // tpl_octree.h
16     //
17     //------------------------------------------------------------
18     //------------------------------------------------------------
19     // COPYRIGHT 2000
20 francois 276 // Version du 02/03/2006 � 11H24
21 5 //------------------------------------------------------------
22     //------------------------------------------------------------
23     #ifndef _TPLOCTREE_
24     #define _TPLOCTREE_
25    
26    
27 francois 481 #include "ot_boite_3d.h"
28 5 #include "tpl_liste_entite.h"
29     #include "tpl_map_entite.h"
30 francois 65 #include <math.h>
31 5
32 francois 65 #define ID 0
33     #define IF 1
34     #define IR 2
35     #define IB 3
36     #define IL 4
37     #define IU 5
38     #define IDF 6
39     #define ILD 7
40     #define ILF 8
41     #define IRD 9
42     #define IRF 10
43     #define IDB 11
44     #define IRB 12
45     #define ILB 13
46     #define IUF 14
47     #define ILU 15
48     #define IRU 16
49     #define IUB 17
50    
51 francois 102 using namespace std;
52 francois 65
53     class OUTIL_OCTREE
54     {
55     public:
56     OUTIL_OCTREE()
57     {
58     };
59     ~OUTIL_OCTREE()
60     {
61     };
62    
63     unsigned long determine_keycode(int nx,int ny,int nz)
64     {
65     vector<int> bits;
66     int ok=0,i=0;
67     do
68     {
69     int nnz=nz>>i;
70     int nny=ny>>i;
71     int nnx=nx>>i;
72     int bit1= nnz & 1;
73     int bit2= nny & 1;
74     int bit3= nnx & 1;
75     if ((nnz==0) && (nny==0) && (nnx==0)) ok=1;
76     bits.insert(bits.end(),bit1);
77     bits.insert(bits.end(),bit2);
78     bits.insert(bits.end(),bit3);
79     i++;
80     }
81     while (ok==0);
82     unsigned long keycode=0;
83     for (int i=0;i<bits.size();i++)
84     keycode=keycode+bits[i]*(1<<i);
85     return keycode;
86     }
87    
88    
89     bool adjacent(int direction,int octant)
90     {
91     bool tabadj[18][8]=
92     {
93     {true, true, true, true, false, false, false, false},
94     {true, true, false, false, true, true, false, false},
95     {false, true, false, true, false, true, false, true},
96     {false, false, true, true, false, false, true, true},
97     {true, false, true, false, true, false, true, false},
98     {false, false, false, false, true, true, true, true},
99     {true, true, false, false, false, false, false, false},
100     {true, false, true, false, false, false, false, false},
101     {true, false, false, false, true, false, false, false},
102     {false, true, false, true, false, false, false, false},
103     {false, true, false, false, false, true, false, false},
104     {false, false, true, true, false, false, false, false},
105     {false, false, false, true, false, false, false, true},
106     {false, false, true, false, false, false, true, false},
107     {false, false, false, false, true, true, false, false},
108     {false, false, false, false, true, false, true, false},
109     {false, false, false, false, false, true, false, true},
110     {false, false, false, false, false, false, true, true},
111    
112     };
113    
114     return tabadj[direction][octant];
115     }
116    
117    
118     int reflect(int direction,int octant)
119     {
120     int tabreflect[18][8]=
121     {
122     {4, 5, 6, 7, 0, 1, 2, 3},
123     {2, 3, 0, 1, 6, 7, 4, 5},
124     {1, 0, 3, 2, 5, 4, 7, 6},
125     {2, 3, 0, 1, 6, 7, 4, 5},
126     {1, 0, 3, 2, 5, 4, 7, 6},
127     {4, 5, 6, 7, 0, 1, 2, 3},
128     {6, 7, 4, 5, 2, 3, 0, 1},
129     {5, 4, 7, 6, 1, 0, 3, 2},
130     {3, 2, 1, 0, 7, 6, 5, 4},
131     {5, 4, 7, 6, 1, 0, 3, 2},
132     {3, 2, 1, 0, 7, 6, 5, 4},
133     {6, 7, 4, 5, 2, 3, 0, 1},
134     {3, 2, 1, 0, 7, 6, 5, 4},
135     {3, 2, 1, 0, 7, 6, 5, 4},
136     {6, 7, 4, 5, 2, 3, 0, 1},
137     {5, 4, 7, 6, 1, 0, 3, 2},
138     {5, 4, 7, 6, 1, 0, 3, 2},
139     {6, 7, 4, 5, 2, 3, 0, 1}
140    
141     };
142    
143     return tabreflect[direction][octant];
144     }
145    
146    
147    
148     int commonface(int direction,int octant)
149     {
150     int tabcommonface[12][8]=
151     {
152     {-1, -1, 0, 0, 1, 1, -1, -1},
153     {-1, 0, -1, 0, 4, -1, 4, -1},
154     {-1, 1, 4, -1, -1, 1, 4, -1},
155     {0, -1, 0, -1, -1, 2, -1, 2},
156     {1, -1, -1, 2, 1, -1, -1, 2},
157     {0, 0, -1, -1, -1, -1, 3, 3},
158     {-1, 2, 3, -1, -1, 2, 3, -1},
159     {4, -1, -1, 3, 4, -1, -1, 3},
160     {1, 1, -1, -1, -1, -1, 5, 5},
161     {4, -1, 4, -1, -1, 5, -1, 5},
162     {-1, 2, -1, 2, 5, -1, 5, -1},
163     {-1, -1, 3, 3, 5, 5, -1, -1},
164     } ;
165     return tabcommonface[direction-6][octant];
166    
167     }
168    
169     int filsvoisin(int direction,int numvoisin)
170     {
171     int tab[6][4]=
172     {
173     {4, 5, 6, 7},
174     {2, 3, 6, 7},
175     {0, 2, 4, 6},
176     {0, 1, 4, 5},
177     {1, 3, 5, 7},
178     {0, 1, 2, 3}
179     };
180     return tab[direction][numvoisin];
181     }
182    
183     int sommetface(int numface,int numsommet)
184     {
185     int tab[6][4]=
186     {
187     {0,1,2,3},
188     {0,1,5,4},
189     {1,2,6,5},
190     {3,2,6,7},
191     {0,3,7,4},
192     {4,5,6,7},
193     };
194     return tab[numface][numsommet];
195     }
196    
197     int sommetarete(int numarete,int numsommet)
198     {
199     int tab[12][2]=
200     {
201     {0,1},
202     {0,3},
203     {0,4},
204     {1,2},
205     {1,5},
206     {3,2},
207     {2,6},
208     {3,7},
209     {4,5},
210     {4,7},
211     {5,6},
212     {7,6},
213     };
214     return tab[numarete-6][numsommet];
215     }
216    
217    
218    
219    
220    
221     int areteface(int numface,int numarete)
222     {
223     int tab[6][4]=
224     {
225     {6,9,11,7},
226     {6,10,14,8},
227     {9,12,16,10},
228     {11,12,17,13},
229     {7,13,15,8},
230     {14,16,17,15},
231     };
232     return tab[numface][numarete];
233     }
234    
235     int faceadjacente(int numface,int numarete)
236     {
237     int tab[6][4]=
238     {
239     {1,2,3,4},
240     {0,2,5,4},
241     {0,3,5,1},
242     {0,2,5,4},
243     {0,3,5,1},
244     {1,2,3,4},
245     };
246     return tab[numface][numarete];
247     }
248    
249    
250    
251     };
252    
253    
254 5 class CELLULE_OCTREE_BASE
255     {
256     public:
257     CELLULE_OCTREE_BASE()
258     {
259     static unsigned long idmax=0;
260     id=idmax;
261     idmax++;
262     };
263    
264     virtual ~CELLULE_OCTREE_BASE() {};
265     virtual BOITE_3D get_boite(void)=0;
266     virtual CELLULE_OCTREE_BASE* get_fils(int num)=0;
267     virtual int get_feuille(void)=0;
268 francois 65 virtual int get_niveau(void)=0;
269     virtual int get_sontype(void)=0;
270 5 virtual unsigned long get_id() {return id;};
271     unsigned long id;
272     };
273    
274    
275     class OCTREE_BASE
276     {
277     public:
278     OCTREE_BASE() {};
279     virtual ~OCTREE_BASE() {};
280     virtual int get_nb_cellule(void)=0;
281     virtual CELLULE_OCTREE_BASE* get_cellule(int num)=0;
282 francois 65 virtual int get_niveau_max(void)=0;
283 5 };
284    
285    
286     template <class A,class CONDITION>
287     class TPL_CELLULE_OCTREE:public CELLULE_OCTREE_BASE
288     {
289     public:
290 francois 65 TPL_CELLULE_OCTREE(TPL_CELLULE_OCTREE<A,CONDITION> *cell,double xmin,double ymin,double zmin,double xmax,double ymax,double zmax,int niv,int st):CELLULE_OCTREE_BASE(),boite(xmin,ymin,zmin,xmax,ymax,zmax),niveau(niv),pere(cell),sontype(st)
291 5 {
292     };
293     virtual ~TPL_CELLULE_OCTREE() {};
294     BOITE_3D get_boite(void) {return boite;};
295 francois 65 int get_niveau(void) {return niveau;};
296 5 BOITE_3D boite;
297     int feuille;
298     TPL_LISTE_ENTITE<CONDITION> lst_entite_CONDITION;
299     TPL_LISTE_ENTITE<A> lst_entite_A;
300     virtual TPL_CELLULE_OCTREE<A,CONDITION>* get_fils(int num) {return fils[num];};
301 francois 65 virtual TPL_CELLULE_OCTREE<A,CONDITION>* get_pere(void) {return pere;};
302 5 virtual int get_feuille(void) {return feuille;};
303 francois 276
304     virtual void vide(void)
305     {
306     lst_entite_A.vide();
307     }
308    
309 francois 65 int get_sontype(void)
310     {
311     return sontype;
312     }
313 5 int get_nb_entite(void)
314     {
315     return lst_entite_A.get_nb();
316     };
317     A get_entite(int num)
318     {
319     return lst_entite_A.get(num);
320     };
321 francois 65 TPL_CELLULE_OCTREE< A ,CONDITION> *fils[8];
322     TPL_CELLULE_OCTREE< A ,CONDITION> *pere;
323 5
324 francois 65
325     int niveau;
326     int sontype;
327 5 };
328    
329     template <class A,class CONDITION,class B>
330     class TPL_CELLULE_INFO:public CELLULE_OCTREE_BASE
331     {
332     public:
333     TPL_CELLULE_INFO(double xmin,double ymin,double zmin,double xmax,double ymax,double zmax):CELLULE_OCTREE_BASE(),boite(xmin,ymin,zmin,xmax,ymax,zmax)
334     {
335     };
336     virtual ~TPL_CELLULE_INFO() {};
337     BOITE_3D get_boite(void) {return boite;};
338     BOITE_3D boite;
339     int feuille;
340     TPL_CELLULE_INFO< A ,CONDITION,B> *fils[8];
341     TPL_LISTE_ENTITE<CONDITION> lst_entite_CONDITION;
342     TPL_LISTE_ENTITE<A> lst_entite_A;
343     B tab[8];
344     virtual TPL_CELLULE_INFO<A,CONDITION,B>* get_fils(int num) {return fils[num];};
345     virtual int get_feuille(void) {return feuille;};
346     int get_nb_entite(void)
347     {
348     return lst_entite_A.get_nb();
349     };
350     A get_entite(int num)
351     {
352     return lst_entite_A.get(num);
353     };
354    
355     B get_info(int num) {return tab[num];};
356     void change_info(int num,B val) {tab[num]=val;};
357    
358     };
359    
360    
361    
362     template <class A,class CONDITION>
363     class TPL_OCTREE:public OCTREE_BASE
364     {
365     public:
366 francois 65 TPL_OCTREE():niveaumax(0) {};
367 5 ~TPL_OCTREE()
368     {
369     for (int i1=0;i1<lst_entite_cellule.get_nb();i1++)
370     {
371     TPL_CELLULE_OCTREE<A ,CONDITION>* cellule=lst_entite_cellule.get(i1);
372     delete cellule;
373     }
374     };
375    
376     virtual int get_nb_cellule(void) {return lst_entite_cellule.get_nb();};
377 francois 65 virtual int get_nb_feuille(void) {return lst_entite_feuille.get_nb();};
378 5
379     virtual BOITE_3D get_boite_de_base(void) {return lst_entite_cellule.get(0)->get_boite();};
380    
381     virtual TPL_CELLULE_OCTREE<A,CONDITION>* get_cellule(int num) {return lst_entite_cellule.get(num); };
382 francois 65 virtual TPL_CELLULE_OCTREE<A,CONDITION>* get_feuille(int num) {return lst_entite_feuille.get(num); };
383 5
384    
385     virtual void initialiser(TPL_LISTE_ENTITE<CONDITION>* lst_entite,int nombre,double xmin,double ymin,double zmin,double xmax,double ymax,double zmax)
386     {
387 francois 65 TPL_CELLULE_OCTREE<A ,CONDITION>* root_cellule=new TPL_CELLULE_OCTREE<A ,CONDITION>(NULL,xmin,ymin,zmin,xmax,ymax,zmax,0,-1);
388 5 lst_entite_cellule.ajouter(root_cellule);
389     for (int i=0;i<lst_entite->get_nb();i++)
390     {
391     CONDITION cond=lst_entite->get(i);
392     BOITE_3D boite_cond=cond->get_boite_3D();
393     if (boite_cond*root_cellule->boite) root_cellule->lst_entite_CONDITION.ajouter(cond);
394     }
395     if (root_cellule->lst_entite_CONDITION.get_nb()>nombre)
396     {
397     root_cellule->feuille=0;
398     double dx=xmax-xmin;
399     double dy=ymax-ymin;
400     double dz=zmax-zmin;
401 francois 65 root_cellule->fils[0]=cree_fils(root_cellule,xmin,ymin,zmin,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre,1,0);
402     root_cellule->fils[1]=cree_fils(root_cellule,xmin+dx/2.,ymin,zmin,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre,1,1);
403     root_cellule->fils[2]=cree_fils(root_cellule,xmin,ymin+dy/2.,zmin,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre,1,2);
404     root_cellule->fils[3]=cree_fils(root_cellule,xmin+dx/2.,ymin+dy/2.,zmin,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre,1,3);
405     root_cellule->fils[4]=cree_fils(root_cellule,xmin,ymin,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre,1,4);
406     root_cellule->fils[5]=cree_fils(root_cellule,xmin+dx/2.,ymin,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre,1,5);
407     root_cellule->fils[6]=cree_fils(root_cellule,xmin,ymin+dy/2.,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre,1,6);
408     root_cellule->fils[7]=cree_fils(root_cellule,xmin+dx/2.,ymin+dy/2.,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre,1,7);
409 5 }
410     else root_cellule->feuille=1;
411     for (int j=0;j<lst_entite_cellule.get_nb();j++)
412     {
413     TPL_CELLULE_OCTREE<A ,CONDITION>* cellule=lst_entite_cellule.get(j);
414     cellule->lst_entite_CONDITION.vide();
415     }
416     }
417    
418     virtual void initialiser(TPL_MAP_ENTITE<CONDITION>* lst_entite,int nombre,double xmin,double ymin,double zmin,double xmax,double ymax,double zmax)
419     {
420 francois 65 TPL_CELLULE_OCTREE<A ,CONDITION>* root_cellule=new TPL_CELLULE_OCTREE<A ,CONDITION>(NULL,xmin,ymin,zmin,xmax,ymax,zmax,0,-1);
421 5 lst_entite_cellule.ajouter(root_cellule);
422 francois 276 typename TPL_MAP_ENTITE<CONDITION>::ITERATEUR it;
423     for (CONDITION cond=lst_entite->get_premier(it);cond!=NULL;cond=lst_entite->get_suivant(it))
424 5 {
425     BOITE_3D boite_cond=cond->get_boite_3D();
426     if (boite_cond*root_cellule->boite) root_cellule->lst_entite_CONDITION.ajouter(cond);
427     }
428     if (root_cellule->lst_entite_CONDITION.get_nb()>nombre)
429     {
430     root_cellule->feuille=0;
431     double dx=xmax-xmin;
432     double dy=ymax-ymin;
433     double dz=zmax-zmin;
434 francois 65 root_cellule->fils[0]=cree_fils(root_cellule,xmin,ymin,zmin,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre,1,0);
435     root_cellule->fils[1]=cree_fils(root_cellule,xmin+dx/2.,ymin,zmin,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre,1,1);
436     root_cellule->fils[2]=cree_fils(root_cellule,xmin,ymin+dy/2.,zmin,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre,1,2);
437     root_cellule->fils[3]=cree_fils(root_cellule,xmin+dx/2.,ymin+dy/2.,zmin,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre,1,3);
438     root_cellule->fils[4]=cree_fils(root_cellule,xmin,ymin,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre,1,4);
439     root_cellule->fils[5]=cree_fils(root_cellule,xmin+dx/2.,ymin,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre,1,5);
440     root_cellule->fils[6]=cree_fils(root_cellule,xmin,ymin+dy/2.,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre,1,6);
441     root_cellule->fils[7]=cree_fils(root_cellule,xmin+dx/2.,ymin+dy/2.,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre,1,7);
442 5 }
443     else root_cellule->feuille=1;
444     for (int j=0;j<lst_entite_cellule.get_nb();j++)
445     {
446     TPL_CELLULE_OCTREE<A ,CONDITION>* cellule=lst_entite_cellule.get(j);
447     cellule->lst_entite_CONDITION.vide();
448     }
449     }
450    
451     virtual void initialiser(OCTREE_BASE* oc)
452     {
453     BOITE_3D boite=oc->get_cellule(0)->get_boite();
454     double xmin=boite.get_xmin();
455     double xmax=boite.get_xmax();
456     double ymin=boite.get_ymin();
457     double ymax=boite.get_ymax();
458     double zmin=boite.get_zmin();
459     double zmax=boite.get_zmax();
460 francois 65 TPL_CELLULE_OCTREE<A ,CONDITION>* cellule=new TPL_CELLULE_OCTREE<A ,CONDITION>(NULL,xmin,ymin,zmin,xmax,ymax,zmax,0,oc->get_cellule(0)->get_sontype());
461 5 lst_entite_cellule.ajouter(cellule);
462 francois 65 niveaumax=oc->get_niveau_max();
463     cellule->feuille=oc->get_cellule(0)->get_feuille();
464 5 if (oc->get_cellule(0)->get_feuille()==0)
465     {
466 francois 65 cellule->fils[0]=cree_fils(cellule,oc->get_cellule(0)->get_fils(0),0);
467     cellule->fils[1]=cree_fils(cellule,oc->get_cellule(0)->get_fils(1),1);
468     cellule->fils[2]=cree_fils(cellule,oc->get_cellule(0)->get_fils(2),2);
469     cellule->fils[3]=cree_fils(cellule,oc->get_cellule(0)->get_fils(3),3);
470     cellule->fils[4]=cree_fils(cellule,oc->get_cellule(0)->get_fils(4),4);
471     cellule->fils[5]=cree_fils(cellule,oc->get_cellule(0)->get_fils(5),5);
472     cellule->fils[6]=cree_fils(cellule,oc->get_cellule(0)->get_fils(6),6);
473     cellule->fils[7]=cree_fils(cellule,oc->get_cellule(0)->get_fils(7),7);
474 5 }
475 francois 65 else {cellule->feuille=1;lst_entite_feuille.ajouter(cellule);}
476 5 };
477    
478    
479 francois 65 TPL_CELLULE_OCTREE<A,CONDITION>* cree_fils( TPL_CELLULE_OCTREE<A ,CONDITION>* cell,CELLULE_OCTREE_BASE *cellule_base,int st)
480 5 {
481     BOITE_3D boite=cellule_base->get_boite();
482     double xmin=boite.get_xmin();
483     double xmax=boite.get_xmax();
484     double ymin=boite.get_ymin();
485     double ymax=boite.get_ymax();
486     double zmin=boite.get_zmin();
487     double zmax=boite.get_zmax();
488 francois 65 TPL_CELLULE_OCTREE<A ,CONDITION>* cellule=new TPL_CELLULE_OCTREE<A ,CONDITION>(cell,xmin,ymin,zmin,xmax,ymax,zmax,cellule_base->get_niveau(),st);
489 5 lst_entite_cellule.ajouter(cellule);
490 francois 65 if (niveaumax<cellule_base->get_niveau()) niveaumax=cellule_base->get_niveau();
491 5 if (cellule_base->get_feuille()==1)
492     {
493 francois 65 {cellule->feuille=1;lst_entite_feuille.ajouter(cellule);}
494 5 }
495     else
496     {
497 francois 65 cellule->fils[0]=cree_fils(cellule,cellule_base->get_fils(0),0);
498     cellule->fils[1]=cree_fils(cellule,cellule_base->get_fils(1),1);
499     cellule->fils[2]=cree_fils(cellule,cellule_base->get_fils(2),2);
500     cellule->fils[3]=cree_fils(cellule,cellule_base->get_fils(3),3);
501     cellule->fils[4]=cree_fils(cellule,cellule_base->get_fils(4),4);
502     cellule->fils[5]=cree_fils(cellule,cellule_base->get_fils(5),5);
503     cellule->fils[6]=cree_fils(cellule,cellule_base->get_fils(6),6);
504     cellule->fils[7]=cree_fils(cellule,cellule_base->get_fils(7),7);
505 5 cellule->feuille=0;
506     }
507     return cellule;
508     };
509    
510 francois 65 virtual TPL_CELLULE_OCTREE<A ,CONDITION>* cree_fils(TPL_CELLULE_OCTREE<A ,CONDITION>* cell,double xmin,double ymin,double zmin,double dx,double dy,double dz,TPL_LISTE_ENTITE<CONDITION>* lst_entite,int nombre,int niv,int st)
511 5 {
512 francois 65 TPL_CELLULE_OCTREE<A ,CONDITION>* cellule=new TPL_CELLULE_OCTREE<A ,CONDITION>(cell,xmin,ymin,zmin,xmin+dx,ymin+dy,zmin+dz,niv,st);
513 5 lst_entite_cellule.ajouter(cellule);
514 francois 65 if (niveaumax<niv) niveaumax=niv;
515     for (int i=0;i<lst_entite->get_nb();i++)
516 5 {
517     CONDITION cond=lst_entite->get(i);
518     BOITE_3D boite_cond=cond->get_boite_3D();
519     if (boite_cond*cellule->boite) cellule->lst_entite_CONDITION.ajouter(cond);
520     }
521     if (cellule->lst_entite_CONDITION.get_nb()>nombre)
522     {
523     cellule->feuille=0;
524 francois 65 cellule->fils[0]=cree_fils(cellule,xmin,ymin,zmin,dx/2.,dy/2.,dz/2.,&(cellule->lst_entite_CONDITION),nombre,niv+1,0);
525     cellule->fils[1]=cree_fils(cellule,xmin+dx/2.,ymin,zmin,dx/2.,dy/2.,dz/2.,&(cellule->lst_entite_CONDITION),nombre,niv+1,1);
526     cellule->fils[2]=cree_fils(cellule,xmin,ymin+dy/2.,zmin,dx/2.,dy/2.,dz/2.,&(cellule->lst_entite_CONDITION),nombre,niv+1,2);
527     cellule->fils[3]=cree_fils(cellule,xmin+dx/2.,ymin+dy/2.,zmin,dx/2.,dy/2.,dz/2.,&(cellule->lst_entite_CONDITION),nombre,niv+1,3);
528     cellule->fils[4]=cree_fils(cellule,xmin,ymin,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(cellule->lst_entite_CONDITION),nombre,niv+1,4);
529     cellule->fils[5]=cree_fils(cellule,xmin+dx/2.,ymin,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(cellule->lst_entite_CONDITION),nombre,niv+1,5);
530     cellule->fils[6]=cree_fils(cellule,xmin,ymin+dy/2.,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(cellule->lst_entite_CONDITION),nombre,niv+1,6);
531     cellule->fils[7]=cree_fils(cellule,xmin+dx/2.,ymin+dy/2.,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(cellule->lst_entite_CONDITION),nombre,niv+1,7);
532 5 }
533 francois 65 else {cellule->feuille=1;lst_entite_feuille.ajouter(cellule);}
534 5 return cellule;
535     }
536    
537    
538     virtual void rechercher(BOITE_3D& boite,TPL_MAP_ENTITE<A>& liste_entite_trouve,TPL_CELLULE_OCTREE<A ,CONDITION>* cellule)
539     {
540     if (boite*cellule->boite)
541     if (cellule->feuille==1)
542     {
543     for (int i=0;i<cellule->lst_entite_A.get_nb();i++)
544     liste_entite_trouve.ajouter(cellule->lst_entite_A.get(i));
545     }
546     else
547     {
548     rechercher(boite,liste_entite_trouve,cellule->fils[0]);
549     rechercher(boite,liste_entite_trouve,cellule->fils[1]);
550     rechercher(boite,liste_entite_trouve,cellule->fils[2]);
551     rechercher(boite,liste_entite_trouve,cellule->fils[3]);
552     rechercher(boite,liste_entite_trouve,cellule->fils[4]);
553     rechercher(boite,liste_entite_trouve,cellule->fils[5]);
554     rechercher(boite,liste_entite_trouve,cellule->fils[6]);
555     rechercher(boite,liste_entite_trouve,cellule->fils[7]);
556     }
557     };
558    
559    
560     virtual void get_cellule(BOITE_3D& boite,TPL_CELLULE_OCTREE<A ,CONDITION>* cellule,TPL_MAP_ENTITE<TPL_CELLULE_OCTREE<A ,CONDITION> * > &liste_entite)
561     {
562     if (boite*cellule->boite)
563     if (cellule->feuille==1)
564     {
565     liste_entite.ajouter(cellule);
566     }
567     else
568     {
569     get_cellule(boite,cellule->fils[0],liste_entite);
570     get_cellule(boite,cellule->fils[1],liste_entite);
571     get_cellule(boite,cellule->fils[2],liste_entite);
572     get_cellule(boite,cellule->fils[3],liste_entite);
573     get_cellule(boite,cellule->fils[4],liste_entite);
574     get_cellule(boite,cellule->fils[5],liste_entite);
575     get_cellule(boite,cellule->fils[6],liste_entite);
576     get_cellule(boite,cellule->fils[7],liste_entite);
577     }
578     };
579    
580 francois 65 virtual TPL_CELLULE_OCTREE<A,CONDITION> *get_cellule_adjacent(TPL_CELLULE_OCTREE<A,CONDITION> * cellule,int direction)
581     {
582     TPL_CELLULE_OCTREE<A,CONDITION> *cellule2;
583     OUTIL_OCTREE outil;
584     if (cellule->pere==NULL) cellule2=NULL;
585     else if (outil.adjacent(direction,cellule->get_sontype())) cellule2=get_cellule_adjacent(cellule->pere,direction);
586     else if (outil.commonface(direction,cellule->get_sontype())!=-1) cellule2=get_cellule_voisin(cellule->pere,outil.commonface(direction,cellule->get_sontype()));
587     else cellule2=cellule->pere;
588     if (cellule2!=NULL)
589     if (cellule2->get_feuille()==0)
590     return cellule2->get_fils(outil.reflect(direction,cellule->get_sontype()));
591     return cellule2;
592     }
593    
594     virtual TPL_CELLULE_OCTREE<A,CONDITION> *get_cellule_voisin(TPL_CELLULE_OCTREE<A,CONDITION> * cellule,int direction)
595     {
596     TPL_CELLULE_OCTREE<A,CONDITION> *cellule2;
597     OUTIL_OCTREE outil;
598     if ((!(cellule->pere==NULL)) && (outil.adjacent(direction,cellule->get_sontype())))
599     cellule2=get_cellule_voisin(cellule->pere,direction);
600     else
601     cellule2=cellule->pere;
602     if (cellule2!=NULL)
603     if (cellule2->feuille==0)
604     return cellule2->get_fils(outil.reflect(direction,cellule->get_sontype()));
605     return cellule2;
606     }
607    
608    
609    
610    
611     virtual void get_feuille_voisins(TPL_CELLULE_OCTREE<A,CONDITION> * cellule,int direction,vector<TPL_CELLULE_OCTREE<A,CONDITION> *> *lst)
612     {
613     OUTIL_OCTREE outil;
614     lst->clear();
615     TPL_CELLULE_OCTREE<A,CONDITION> *cellule2=get_cellule_voisin(cellule,direction);
616     if (cellule2!=NULL) lst->insert(lst->end(),cellule2);
617     int i=0;
618     while (i<lst->size())
619     {
620     TPL_CELLULE_OCTREE<A,CONDITION> *cell=(*lst)[i];
621     if (cell->feuille==0)
622     {
623     lst->insert(lst->end(),cell->get_fils(outil.filsvoisin(direction,0)));
624     lst->insert(lst->end(),cell->get_fils(outil.filsvoisin(direction,1)));
625     lst->insert(lst->end(),cell->get_fils(outil.filsvoisin(direction,2)));
626     lst->insert(lst->end(),cell->get_fils(outil.filsvoisin(direction,3)));
627     }
628     i++;
629     }
630 francois 102 typename vector<TPL_CELLULE_OCTREE<A,CONDITION> * >::iterator j=lst->end();
631 francois 65 while (j!=lst->begin())
632     {
633     j--;
634     TPL_CELLULE_OCTREE<A,CONDITION> *cell=*j;
635     if (cell->feuille==0)
636     lst->erase(j);
637    
638     }
639     }
640    
641 5 virtual TPL_CELLULE_OCTREE<A,CONDITION> *get_cellule(double x,double y, double z)
642     {
643     BOITE_3D boite(x,y,z,x,y,z);
644     TPL_CELLULE_OCTREE<A ,CONDITION>* cellule=lst_entite_cellule.get(0);
645     TPL_MAP_ENTITE<TPL_CELLULE_OCTREE<A ,CONDITION> * > liste_cellule;
646     get_cellule(boite,cellule,liste_cellule);
647     if (liste_cellule.get_nb()==1) return liste_cellule.get(0);
648     return NULL;
649     };
650    
651    
652 francois 65 virtual unsigned long get_cellule_keycode(int numcellule,int numsommet)
653     {
654     TPL_CELLULE_OCTREE<A ,CONDITION>* cellule=lst_entite_cellule.get(numcellule);
655     return get_keycode(cellule,numsommet);
656     }
657    
658     virtual unsigned long get_feuille_keycode(int numcellule,int numsommet)
659     {
660     TPL_CELLULE_OCTREE<A ,CONDITION>* cellule=lst_entite_feuille.get(numcellule);
661     return get_keycode(cellule,numsommet);
662     }
663    
664     virtual unsigned long get_keycode(TPL_CELLULE_OCTREE<A ,CONDITION>* cellule,int numsommet)
665     {
666     TPL_CELLULE_OCTREE<A ,CONDITION>* root=lst_entite_cellule.get(0);
667    
668     double x=cellule->boite.get_xmin();
669     double y=cellule->boite.get_ymin();
670     double z=cellule->boite.get_zmin();
671     double xx=cellule->boite.get_xmax();
672     double yy=cellule->boite.get_ymax();
673     double zz=cellule->boite.get_zmax();
674     double somx=x,somy=y,somz=z;
675     if (numsommet==1) somx=xx;
676     if (numsommet==2) {somx=xx;somy=yy;}
677     if (numsommet==3) somy=yy;
678     if (numsommet==4) somz=zz;
679     if (numsommet==5) {somx=xx;somz=zz;}
680     if (numsommet==6) {somx=xx;somy=yy;somz=zz;}
681     if (numsommet==7) {somy=yy;somz=zz;}
682     double nx=0.1+(somx-root->boite.get_xmin())/(root->boite.get_xmax()-root->boite.get_xmin())*(1<<get_niveau_max());
683     double ny=0.1+(somy-root->boite.get_ymin())/(root->boite.get_ymax()-root->boite.get_ymin())*(1<<get_niveau_max());
684     double nz=0.1+(somz-root->boite.get_zmin())/(root->boite.get_zmax()-root->boite.get_zmin())*(1<<get_niveau_max());
685     OUTIL_OCTREE outil;
686     return outil.determine_keycode(nx,ny,nz);
687     }
688    
689     virtual unsigned long get_keycode(double x,double y,double z)
690     {
691     TPL_CELLULE_OCTREE<A ,CONDITION>* root=lst_entite_cellule.get(0);
692     double nx=0.1+(x-root->boite.get_xmin())/(root->boite.get_xmax()-root->boite.get_xmin())*(1<<get_niveau_max());
693     double ny=0.1+(y-root->boite.get_ymin())/(root->boite.get_ymax()-root->boite.get_ymin())*(1<<get_niveau_max());
694     double nz=0.1+(z-root->boite.get_zmin())/(root->boite.get_zmax()-root->boite.get_zmin())*(1<<get_niveau_max());
695     OUTIL_OCTREE outil;
696     return outil.determine_keycode(nx,ny,nz);
697     }
698    
699    
700    
701     virtual void get_xyzsommet_cellule(int numcellule,int numsommet,double &x,double &y,double &z)
702     {
703     TPL_CELLULE_OCTREE<A ,CONDITION>* cellule=lst_entite_cellule.get(numcellule);
704     x=cellule->boite.get_xmin();
705     y=cellule->boite.get_ymin();
706     z=cellule->boite.get_zmin();
707     double xx=cellule->boite.get_xmax();
708     double yy=cellule->boite.get_ymax();
709     double zz=cellule->boite.get_zmax();
710     if (numsommet==1) x=xx;
711     if (numsommet==2) {x=xx;y=yy;}
712     if (numsommet==3) y=yy;
713     if (numsommet==4) z=zz;
714     if (numsommet==5) {x=xx;z=zz;}
715     if (numsommet==6) {x=xx;y=yy;z=zz;}
716     if (numsommet==7) {y=yy;z=zz;}
717     }
718    
719     virtual void get_xyzsommet_feuille(int numcellule,int numsommet,double &x,double &y,double &z)
720     {
721     TPL_CELLULE_OCTREE<A ,CONDITION>* cellule=lst_entite_feuille.get(numcellule);
722     x=cellule->boite.get_xmin();
723     y=cellule->boite.get_ymin();
724     z=cellule->boite.get_zmin();
725     double xx=cellule->boite.get_xmax();
726     double yy=cellule->boite.get_ymax();
727     double zz=cellule->boite.get_zmax();
728     if (numsommet==1) x=xx;
729     if (numsommet==2) {x=xx;y=yy;}
730     if (numsommet==3) y=yy;
731     if (numsommet==4) z=zz;
732     if (numsommet==5) {x=xx;z=zz;}
733     if (numsommet==6) {x=xx;y=yy;z=zz;}
734     if (numsommet==7) {y=yy;z=zz;}
735     }
736    
737    
738    
739     virtual void equilibre(int niveauequilibre=1)
740     {
741     int ok=0;
742     do
743     {
744     int nbfeuilleavant=get_nb_feuille();
745     vector< TPL_CELLULE_OCTREE<A,CONDITION> *> lstraf;
746     for (int i=0;i<lst_entite_feuille.get_nb();i++)
747     {
748     TPL_CELLULE_OCTREE<A,CONDITION> *feuille=lst_entite_feuille.get(i);
749     int niveaumaxvoisin=0;
750     for (int j=0;j<6;j++)
751     {
752     vector< TPL_CELLULE_OCTREE<A,CONDITION> *> lstvoi;
753     get_feuille_voisins(feuille,j,&lstvoi);
754     int nb_feuille=lstvoi.size();
755     for (int k=0;k<nb_feuille;k++)
756     {
757     int niveau=lstvoi[k]->get_niveau();
758     if (niveau>niveaumaxvoisin) niveaumaxvoisin=niveau;
759     }
760    
761     }
762     if (niveaumaxvoisin-feuille->get_niveau()>niveauequilibre) {lstraf.insert(lstraf.end(),feuille);feuille->feuille=niveaumaxvoisin-niveauequilibre;}
763     }
764     int nbcelluleraffiner=lstraf.size();
765     for (int i=0;i<nbcelluleraffiner;i++)
766     {
767     TPL_CELLULE_OCTREE<A,CONDITION> *cell=lstraf[i];
768     lst_entite_feuille.supprimer(cell);
769     int niveauatteindre=cell->feuille;
770     int niveau=cell->niveau;
771     cell->feuille=0;
772     double xmin=cell->boite.get_xmin();
773     double ymin=cell->boite.get_ymin();
774     double zmin=cell->boite.get_zmin();
775     double dx=cell->boite.get_xmax()-cell->boite.get_xmin();
776     double dy=cell->boite.get_ymax()-cell->boite.get_ymin();
777     double dz=cell->boite.get_zmax()-cell->boite.get_zmin();
778    
779     cell->fils[0]=cree_fils(cell,xmin,ymin,zmin,dx/2.,dy/2.,dz/2.,niveau+1,niveauatteindre,0);
780     cell->fils[1]=cree_fils(cell,xmin+dx/2.,ymin,zmin,dx/2.,dy/2.,dz/2.,niveau+1,niveauatteindre,1);
781     cell->fils[2]=cree_fils(cell,xmin,ymin+dy/2.,zmin,dx/2.,dy/2.,dz/2.,niveau+1,niveauatteindre,2);
782     cell->fils[3]=cree_fils(cell,xmin+dx/2.,ymin+dy/2.,zmin,dx/2.,dy/2.,dz/2.,niveau+1,niveauatteindre,3);
783     cell->fils[4]=cree_fils(cell,xmin,ymin,zmin+dz/2.,dx/2.,dy/2.,dz/2.,niveau+1,niveauatteindre,4);
784     cell->fils[5]=cree_fils(cell,xmin+dx/2.,ymin,zmin+dz/2.,dx/2.,dy/2.,dz/2.,niveau+1,niveauatteindre,5);
785     cell->fils[6]=cree_fils(cell,xmin,ymin+dy/2.,zmin+dz/2.,dx/2.,dy/2.,dz/2.,niveau+1,niveauatteindre,6);
786     cell->fils[7]=cree_fils(cell,xmin+dx/2.,ymin+dy/2.,zmin+dz/2.,dx/2.,dy/2.,dz/2.,niveau+1,niveauatteindre,7);
787     }
788     int nbfeuilleapres=get_nb_feuille();
789     if (nbfeuilleapres==nbfeuilleavant) ok=1;
790     }
791     while (ok==0);
792    
793    
794     }
795    
796     virtual TPL_CELLULE_OCTREE<A ,CONDITION>* cree_fils(TPL_CELLULE_OCTREE<A ,CONDITION>* cell,double xmin,double ymin,double zmin,double dx,double dy,double dz,int niv,int niveauatteindre,int st)
797     {
798     TPL_CELLULE_OCTREE<A ,CONDITION>* cellule=new TPL_CELLULE_OCTREE<A ,CONDITION>(cell,xmin,ymin,zmin,xmin+dx,ymin+dy,zmin+dz,niv,st);
799     lst_entite_cellule.ajouter(cellule);
800     if (niveaumax<niv) niveaumax=niv;
801     if (cellule->get_niveau()!=niveauatteindre)
802     {
803     cellule->feuille=0;
804     cellule->fils[0]=cree_fils(cellule,xmin,ymin,zmin,dx/2.,dy/2.,dz/2.,niv+1,niveauatteindre,0);
805     cellule->fils[1]=cree_fils(cellule,xmin+dx/2.,ymin,zmin,dx/2.,dy/2.,dz/2.,niv+1,niveauatteindre,1);
806     cellule->fils[2]=cree_fils(cellule,xmin,ymin+dy/2.,zmin,dx/2.,dy/2.,dz/2.,niv+1,niveauatteindre,2);
807     cellule->fils[3]=cree_fils(cellule,xmin+dx/2.,ymin+dy/2.,zmin,dx/2.,dy/2.,dz/2.,niv+1,niveauatteindre,3);
808     cellule->fils[4]=cree_fils(cellule,xmin,ymin,zmin+dz/2.,dx/2.,dy/2.,dz/2.,niv+1,niveauatteindre,4);
809     cellule->fils[5]=cree_fils(cellule,xmin+dx/2.,ymin,zmin+dz/2.,dx/2.,dy/2.,dz/2.,niv+1,niveauatteindre,5);
810     cellule->fils[6]=cree_fils(cellule,xmin,ymin+dy/2.,zmin+dz/2.,dx/2.,dy/2.,dz/2.,niv+1,niveauatteindre,6);
811     cellule->fils[7]=cree_fils(cellule,xmin+dx/2.,ymin+dy/2.,zmin+dz/2.,dx/2.,dy/2.,dz/2.,niv+1,niveauatteindre,7);
812     }
813     else {cellule->feuille=1;lst_entite_feuille.ajouter(cellule);}
814     return cellule;
815     }
816    
817    
818 5 virtual void rechercher(double xcentre,double ycentre,double zcentre,double rayon_recherche,TPL_MAP_ENTITE<A>& liste_entite_trouve)
819     {
820     BOITE_3D boite(xcentre-rayon_recherche,ycentre-rayon_recherche,zcentre-rayon_recherche,xcentre+rayon_recherche,ycentre+rayon_recherche,zcentre+rayon_recherche);
821     TPL_CELLULE_OCTREE<A ,CONDITION>* cellule=lst_entite_cellule.get(0);
822     rechercher(boite,liste_entite_trouve,cellule);
823     };
824    
825    
826    
827     virtual void inserer(BOITE_3D& boite,A a,TPL_CELLULE_OCTREE<A ,CONDITION>* cellule)
828     {
829     if (boite*cellule->boite)
830     if (cellule->feuille==1)
831     {
832     cellule->lst_entite_A.ajouter(a);
833     }
834     else
835     {
836     inserer(boite,a,cellule->fils[0]);
837     inserer(boite,a,cellule->fils[1]);
838     inserer(boite,a,cellule->fils[2]);
839     inserer(boite,a,cellule->fils[3]);
840     inserer(boite,a,cellule->fils[4]);
841     inserer(boite,a,cellule->fils[5]);
842     inserer(boite,a,cellule->fils[6]);
843     inserer(boite,a,cellule->fils[7]);
844     }
845     };
846    
847     virtual void supprimer(BOITE_3D& boite,A a,TPL_CELLULE_OCTREE<A ,CONDITION>* cellule)
848     {
849     if (boite*cellule->boite)
850     if (cellule->feuille==1)
851     {
852     cellule->lst_entite_A.supprimer(a);
853     }
854     else
855     {
856     supprimer(boite,a,cellule->fils[0]);
857     supprimer(boite,a,cellule->fils[1]);
858     supprimer(boite,a,cellule->fils[2]);
859     supprimer(boite,a,cellule->fils[3]);
860     supprimer(boite,a,cellule->fils[4]);
861     supprimer(boite,a,cellule->fils[5]);
862     supprimer(boite,a,cellule->fils[6]);
863     supprimer(boite,a,cellule->fils[7]);
864     }
865     };
866    
867     virtual void inserer(A a)
868     {
869     BOITE_3D boite=a->get_boite_3D();
870     TPL_CELLULE_OCTREE<A ,CONDITION>* cellule=lst_entite_cellule.get(0);
871     inserer(boite,a,cellule);
872     }
873    
874     virtual void supprimer(A a)
875     {
876     BOITE_3D boite=a->get_boite_3D();
877     TPL_CELLULE_OCTREE<A ,CONDITION>* cellule=lst_entite_cellule.get(0);
878     supprimer(boite,a,cellule);
879     }
880    
881 francois 65 virtual int get_niveau_max(void)
882     {
883     return niveaumax;
884     }
885 5
886 francois 65
887    
888 francois 276 virtual void vide(void)
889     {
890     for (int i=0;i<lst_entite_feuille.get_nb();i++)
891     {
892     TPL_CELLULE_OCTREE<A ,CONDITION>* cellule=lst_entite_feuille.get(i);
893     cellule->vide();
894     }
895     }
896 francois 65
897 francois 507
898     virtual double get_dimension_caracteristique()
899     {
900     TPL_CELLULE_OCTREE<A ,CONDITION>* cellule=lst_entite_cellule.get(0);
901     double x=cellule->boite.get_xmin();
902     double y=cellule->boite.get_ymin();
903     double z=cellule->boite.get_zmin();
904     double xx=cellule->boite.get_xmax();
905     double yy=cellule->boite.get_ymax();
906     double zz=cellule->boite.get_zmax();
907     return sqrt((xx-x)*(xx-x)+(yy-y)*(yy-y)+(zz-z)*(zz-z)) ;
908     }
909    
910    
911    
912 francois 65 protected:
913 5 TPL_LISTE_ENTITE<TPL_CELLULE_OCTREE<A ,CONDITION>* > lst_entite_cellule;
914 francois 65 TPL_LISTE_ENTITE<TPL_CELLULE_OCTREE<A ,CONDITION>* > lst_entite_feuille;
915     int niveaumax;
916 5
917     };
918    
919 francois 65 template <class A,class B>
920     class TPL_OCTREE_FCT:public TPL_OCTREE<A,A>
921     {
922     private:
923     double coef;
924 5
925 francois 65 public:
926     TPL_OCTREE_FCT():TPL_OCTREE<A,A>(),coef(1.) {};
927     ~TPL_OCTREE_FCT() {}
928 5
929 francois 65
930 francois 102 virtual void change_coefficent_multiplicateur(double val) {coef=val;};
931 francois 65
932     virtual void initialiser(B &fonction,double xmin,double ymin,double zmin,double xmax,double ymax,double zmax)
933     {
934     TPL_CELLULE_OCTREE<A ,A>* root_cellule=new TPL_CELLULE_OCTREE<A ,A>(NULL,xmin,ymin,zmin,xmax,ymax,zmax,0,-1);
935 francois 102 TPL_OCTREE<A,A>::lst_entite_cellule.ajouter(root_cellule);
936 francois 65 double dx=root_cellule->boite.get_xmax()-root_cellule->boite.get_xmin();
937     double dy=root_cellule->boite.get_ymax()-root_cellule->boite.get_ymin();
938     double dz=root_cellule->boite.get_zmax()-root_cellule->boite.get_zmin();
939     double d=dx;
940 francois 584 if (dy>d) d=dy;
941     if (dz>d) d=dz;
942 francois 65 double xyz[3];
943     root_cellule->boite.get_centre(xyz);
944     double ecart[9];
945     fonction.evaluer(xyz,ecart);
946     double dcible=coef/sqrt(ecart[0]);
947     if (dcible<d)
948     {
949     root_cellule->feuille=0;
950     root_cellule->fils[0]=cree_fils(root_cellule,xmin,ymin,zmin,dx/2.,dy/2.,dz/2.,fonction,1,0);
951     root_cellule->fils[1]=cree_fils(root_cellule,xmin+dx/2.,ymin,zmin,dx/2.,dy/2.,dz/2.,fonction,1,1);
952     root_cellule->fils[2]=cree_fils(root_cellule,xmin,ymin+dy/2.,zmin,dx/2.,dy/2.,dz/2.,fonction,1,2);
953     root_cellule->fils[3]=cree_fils(root_cellule,xmin+dx/2.,ymin+dy/2.,zmin,dx/2.,dy/2.,dz/2.,fonction,1,3);
954     root_cellule->fils[4]=cree_fils(root_cellule,xmin,ymin,zmin+dz/2.,dx/2.,dy/2.,dz/2.,fonction,1,4);
955     root_cellule->fils[5]=cree_fils(root_cellule,xmin+dx/2.,ymin,zmin+dz/2.,dx/2.,dy/2.,dz/2.,fonction,1,5);
956     root_cellule->fils[6]=cree_fils(root_cellule,xmin,ymin+dy/2.,zmin+dz/2.,dx/2.,dy/2.,dz/2.,fonction,1,6);
957     root_cellule->fils[7]=cree_fils(root_cellule,xmin+dx/2.,ymin+dy/2.,zmin+dz/2.,dx/2.,dy/2.,dz/2.,fonction,1,7);
958     }
959 francois 102 else {root_cellule->feuille=1;TPL_OCTREE<A,A>::lst_entite_feuille.ajouter(root_cellule);}
960 francois 65 }
961    
962     virtual TPL_CELLULE_OCTREE<A ,A>* cree_fils(TPL_CELLULE_OCTREE<A ,A>* cell,double xmin,double ymin,double zmin,double dx,double dy,double dz,B &fonction,int niv,int st)
963     {
964     TPL_CELLULE_OCTREE<A ,A>* cellule=new TPL_CELLULE_OCTREE<A ,A>(cell,xmin,ymin,zmin,xmin+dx,ymin+dy,zmin+dz,niv,st);
965 francois 102 TPL_OCTREE<A,A>::lst_entite_cellule.ajouter(cellule);
966     if (TPL_OCTREE<A,A>::niveaumax<niv) TPL_OCTREE<A,A>::niveaumax=niv;
967 francois 65 double ddx=cellule->boite.get_xmax()-cellule->boite.get_xmin();
968     double ddy=cellule->boite.get_ymax()-cellule->boite.get_ymin();
969     double ddz=cellule->boite.get_zmax()-cellule->boite.get_zmin();
970     double d=dx;
971 francois 584 if (ddy>d) d=ddy;
972     if (ddz>d) d=ddz;
973 francois 65 double xyz[3];
974     cellule->boite.get_centre(xyz);
975     double ecart[9];
976     fonction.evaluer(xyz,ecart);
977     double dcible=coef/sqrt(ecart[0]);
978     if (dcible<d)
979     {
980     cellule->feuille=0;
981     cellule->fils[0]=cree_fils(cellule,xmin,ymin,zmin,dx/2.,dy/2.,dz/2.,fonction,niv+1,0);
982     cellule->fils[1]=cree_fils(cellule,xmin+dx/2.,ymin,zmin,dx/2.,dy/2.,dz/2.,fonction,niv+1,1);
983     cellule->fils[2]=cree_fils(cellule,xmin,ymin+dy/2.,zmin,dx/2.,dy/2.,dz/2.,fonction,niv+1,2);
984     cellule->fils[3]=cree_fils(cellule,xmin+dx/2.,ymin+dy/2.,zmin,dx/2.,dy/2.,dz/2.,fonction,niv+1,3);
985     cellule->fils[4]=cree_fils(cellule,xmin,ymin,zmin+dz/2.,dx/2.,dy/2.,dz/2.,fonction,niv+1,4);
986     cellule->fils[5]=cree_fils(cellule,xmin+dx/2.,ymin,zmin+dz/2.,dx/2.,dy/2.,dz/2.,fonction,niv+1,5);
987     cellule->fils[6]=cree_fils(cellule,xmin,ymin+dy/2.,zmin+dz/2.,dx/2.,dy/2.,dz/2.,fonction,niv+1,6);
988     cellule->fils[7]=cree_fils(cellule,xmin+dx/2.,ymin+dy/2.,zmin+dz/2.,dx/2.,dy/2.,dz/2.,fonction,niv+1,7);
989     }
990 francois 102 else {cellule->feuille=1;TPL_OCTREE<A,A>::lst_entite_feuille.ajouter(cellule);}
991 francois 65 return cellule;
992     }
993     };
994    
995    
996    
997 5 template <class A,class CONDITION,class B>
998     class TPL_OCTREE_INFO:public OCTREE_BASE
999     {
1000     public:
1001     TPL_OCTREE_INFO() {};
1002     ~TPL_OCTREE_INFO()
1003     {
1004     for (int i1=0;i1<lst_entite_cellule.get_nb();i1++)
1005     {
1006     TPL_CELLULE_INFO<A ,CONDITION,B>* cellule=lst_entite_cellule.get(i1);
1007     delete cellule;
1008     }
1009     };
1010    
1011     virtual int get_nb_cellule(void) {return lst_entite_cellule.get_nb();};
1012    
1013     virtual BOITE_3D get_boite_de_base(void) {return lst_entite_cellule.get(0)->get_boite();};
1014    
1015     virtual TPL_CELLULE_INFO<A,CONDITION,B>* get_cellule(int num) {return lst_entite_cellule.get(num); };
1016    
1017    
1018     virtual void initialiser(TPL_LISTE_ENTITE<CONDITION>* lst_entite,int nombre,double xmin,double ymin,double zmin,double xmax,double ymax,double zmax)
1019     {
1020     TPL_CELLULE_INFO<A ,CONDITION,B>* root_cellule=new TPL_CELLULE_INFO<A ,CONDITION,B>(xmin,ymin,zmin,xmax,ymax,zmax);
1021     lst_entite_cellule.ajouter(root_cellule);
1022     for (int i=0;i<lst_entite->get_nb();i++)
1023     {
1024     CONDITION cond=lst_entite->get(i);
1025     BOITE_3D boite_cond=cond->get_boite_3D();
1026     if (boite_cond*root_cellule->boite) root_cellule->lst_entite_CONDITION.ajouter(cond);
1027     }
1028     if (root_cellule->lst_entite_CONDITION.get_nb()>nombre)
1029     {
1030     root_cellule->feuille=0;
1031     double dx=xmax-xmin;
1032     double dy=ymax-ymin;
1033     double dz=zmax-zmin;
1034     root_cellule->fils[0]=cree_fils(xmin,ymin,zmin,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre);
1035     root_cellule->fils[1]=cree_fils(xmin+dx/2.,ymin,zmin,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre);
1036     root_cellule->fils[2]=cree_fils(xmin,ymin+dy/2.,zmin,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre);
1037     root_cellule->fils[3]=cree_fils(xmin+dx/2.,ymin+dy/2.,zmin,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre);
1038     root_cellule->fils[4]=cree_fils(xmin,ymin,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre);
1039     root_cellule->fils[5]=cree_fils(xmin+dx/2.,ymin,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre);
1040     root_cellule->fils[6]=cree_fils(xmin,ymin+dy/2.,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre);
1041     root_cellule->fils[7]=cree_fils(xmin+dx/2.,ymin+dy/2.,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre);
1042     }
1043     else root_cellule->feuille=1;
1044     for (int j=0;j<lst_entite_cellule.get_nb();j++)
1045     {
1046     TPL_CELLULE_INFO<A ,CONDITION,B>* cellule=lst_entite_cellule.get(j);
1047     cellule->lst_entite_CONDITION.vide();
1048     }
1049     }
1050    
1051     virtual void initialiser(TPL_MAP_ENTITE<CONDITION>* lst_entite,int nombre,double xmin,double ymin,double zmin,double xmax,double ymax,double zmax)
1052     {
1053     TPL_CELLULE_INFO<A ,CONDITION,B>* root_cellule=new TPL_CELLULE_INFO<A ,CONDITION,B>(xmin,ymin,zmin,xmax,ymax,zmax);
1054     lst_entite_cellule.ajouter(root_cellule);
1055     for (int i=0;i<lst_entite->get_nb();i++)
1056     {
1057     CONDITION cond=lst_entite->get(i);
1058     BOITE_3D boite_cond=cond->get_boite_3D();
1059     if (boite_cond*root_cellule->boite) root_cellule->lst_entite_CONDITION.ajouter(cond);
1060     }
1061     if (root_cellule->lst_entite_CONDITION.get_nb()>nombre)
1062     {
1063     root_cellule->feuille=0;
1064     double dx=xmax-xmin;
1065     double dy=ymax-ymin;
1066     double dz=zmax-zmin;
1067     root_cellule->fils[0]=cree_fils(xmin,ymin,zmin,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre);
1068     root_cellule->fils[1]=cree_fils(xmin+dx/2.,ymin,zmin,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre);
1069     root_cellule->fils[2]=cree_fils(xmin,ymin+dy/2.,zmin,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre);
1070     root_cellule->fils[3]=cree_fils(xmin+dx/2.,ymin+dy/2.,zmin,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre);
1071     root_cellule->fils[4]=cree_fils(xmin,ymin,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre);
1072     root_cellule->fils[5]=cree_fils(xmin+dx/2.,ymin,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre);
1073     root_cellule->fils[6]=cree_fils(xmin,ymin+dy/2.,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre);
1074     root_cellule->fils[7]=cree_fils(xmin+dx/2.,ymin+dy/2.,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(root_cellule->lst_entite_CONDITION),nombre);
1075     }
1076     else root_cellule->feuille=1;
1077     for (int j=0;j<lst_entite_cellule.get_nb();j++)
1078     {
1079     TPL_CELLULE_INFO<A ,CONDITION,B>* cellule=lst_entite_cellule.get(j);
1080     cellule->lst_entite_CONDITION.vide();
1081     }
1082     }
1083    
1084     virtual void initialiser(OCTREE_BASE* oc)
1085     {
1086     BOITE_3D boite=oc->get_cellule(0)->get_boite();
1087     double xmin=boite.get_xmin();
1088     double xmax=boite.get_xmax();
1089     double ymin=boite.get_ymin();
1090     double ymax=boite.get_ymax();
1091     double zmin=boite.get_zmin();
1092     double zmax=boite.get_zmax();
1093     TPL_CELLULE_INFO<A ,CONDITION,B>* cellule=new TPL_CELLULE_INFO<A ,CONDITION,B>(xmin,ymin,zmin,xmax,ymax,zmax);
1094     lst_entite_cellule.ajouter(cellule);
1095     if (oc->get_cellule(0)->get_feuille()==0)
1096     {
1097     cellule->fils[0]=cree_fils(oc->get_cellule(0)->get_fils(0));
1098     cellule->fils[1]=cree_fils(oc->get_cellule(0)->get_fils(1));
1099     cellule->fils[2]=cree_fils(oc->get_cellule(0)->get_fils(2));
1100     cellule->fils[3]=cree_fils(oc->get_cellule(0)->get_fils(3));
1101     cellule->fils[4]=cree_fils(oc->get_cellule(0)->get_fils(4));
1102     cellule->fils[5]=cree_fils(oc->get_cellule(0)->get_fils(5));
1103     cellule->fils[6]=cree_fils(oc->get_cellule(0)->get_fils(6));
1104     cellule->fils[7]=cree_fils(oc->get_cellule(0)->get_fils(7));
1105     }
1106     cellule->feuille=oc->get_cellule(0)->get_feuille();
1107     };
1108    
1109    
1110     TPL_CELLULE_INFO<A,CONDITION,B>* cree_fils(CELLULE_OCTREE_BASE *cellule_base)
1111     {
1112     BOITE_3D boite=cellule_base->get_boite();
1113     double xmin=boite.get_xmin();
1114     double xmax=boite.get_xmax();
1115     double ymin=boite.get_ymin();
1116     double ymax=boite.get_ymax();
1117     double zmin=boite.get_zmin();
1118     double zmax=boite.get_zmax();
1119     TPL_CELLULE_INFO<A ,CONDITION,B>* cellule=new TPL_CELLULE_INFO<A ,CONDITION,B>(xmin,ymin,zmin,xmax,ymax,zmax);
1120     lst_entite_cellule.ajouter(cellule);
1121     if (cellule_base->get_feuille()==1)
1122     {
1123     cellule->feuille=1;
1124     }
1125     else
1126     {
1127     cellule->fils[0]=cree_fils(cellule_base->get_fils(0));
1128     cellule->fils[1]=cree_fils(cellule_base->get_fils(1));
1129     cellule->fils[2]=cree_fils(cellule_base->get_fils(2));
1130     cellule->fils[3]=cree_fils(cellule_base->get_fils(3));
1131     cellule->fils[4]=cree_fils(cellule_base->get_fils(4));
1132     cellule->fils[5]=cree_fils(cellule_base->get_fils(5));
1133     cellule->fils[6]=cree_fils(cellule_base->get_fils(6));
1134     cellule->fils[7]=cree_fils(cellule_base->get_fils(7));
1135     cellule->feuille=0;
1136     }
1137     return cellule;
1138     };
1139    
1140     virtual TPL_CELLULE_INFO<A ,CONDITION,B>* cree_fils(double xmin,double ymin,double zmin,double dx,double dy,double dz,TPL_LISTE_ENTITE<CONDITION>* lst_entite,int nombre)
1141     {
1142     TPL_CELLULE_INFO<A ,CONDITION,B>* cellule=new TPL_CELLULE_INFO<A ,CONDITION,B>(xmin,ymin,zmin,xmin+dx,ymin+dy,zmin+dz);
1143     lst_entite_cellule.ajouter(cellule);
1144     for (int i=0;i<lst_entite->get_nb();i++)
1145     {
1146     CONDITION cond=lst_entite->get(i);
1147     BOITE_3D boite_cond=cond->get_boite_3D();
1148     if (boite_cond*cellule->boite) cellule->lst_entite_CONDITION.ajouter(cond);
1149     }
1150     if (cellule->lst_entite_CONDITION.get_nb()>nombre)
1151     {
1152     cellule->feuille=0;
1153     cellule->fils[0]=cree_fils(xmin,ymin,zmin,dx/2.,dy/2.,dz/2.,&(cellule->lst_entite_CONDITION),nombre);
1154     cellule->fils[1]=cree_fils(xmin+dx/2.,ymin,zmin,dx/2.,dy/2.,dz/2.,&(cellule->lst_entite_CONDITION),nombre);
1155     cellule->fils[2]=cree_fils(xmin,ymin+dy/2.,zmin,dx/2.,dy/2.,dz/2.,&(cellule->lst_entite_CONDITION),nombre);
1156     cellule->fils[3]=cree_fils(xmin+dx/2.,ymin+dy/2.,zmin,dx/2.,dy/2.,dz/2.,&(cellule->lst_entite_CONDITION),nombre);
1157     cellule->fils[4]=cree_fils(xmin,ymin,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(cellule->lst_entite_CONDITION),nombre);
1158     cellule->fils[5]=cree_fils(xmin+dx/2.,ymin,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(cellule->lst_entite_CONDITION),nombre);
1159     cellule->fils[6]=cree_fils(xmin,ymin+dy/2.,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(cellule->lst_entite_CONDITION),nombre);
1160     cellule->fils[7]=cree_fils(xmin+dx/2.,ymin+dy/2.,zmin+dz/2.,dx/2.,dy/2.,dz/2.,&(cellule->lst_entite_CONDITION),nombre);
1161     }
1162     else cellule->feuille=1;
1163     return cellule;
1164     }
1165    
1166    
1167     virtual void rechercher(BOITE_3D& boite,TPL_MAP_ENTITE<A>& liste_entite_trouve,TPL_CELLULE_INFO<A ,CONDITION,B>* cellule)
1168     {
1169     if (boite*cellule->boite)
1170     if (cellule->feuille==1)
1171     {
1172     for (int i=0;i<cellule->lst_entite_A.get_nb();i++)
1173     liste_entite_trouve.ajouter(cellule->lst_entite_A.get(i));
1174     }
1175     else
1176     {
1177     rechercher(boite,liste_entite_trouve,cellule->fils[0]);
1178     rechercher(boite,liste_entite_trouve,cellule->fils[1]);
1179     rechercher(boite,liste_entite_trouve,cellule->fils[2]);
1180     rechercher(boite,liste_entite_trouve,cellule->fils[3]);
1181     rechercher(boite,liste_entite_trouve,cellule->fils[4]);
1182     rechercher(boite,liste_entite_trouve,cellule->fils[5]);
1183     rechercher(boite,liste_entite_trouve,cellule->fils[6]);
1184     rechercher(boite,liste_entite_trouve,cellule->fils[7]);
1185     }
1186     };
1187    
1188    
1189     virtual void get_cellule(BOITE_3D& boite,TPL_CELLULE_INFO<A ,CONDITION,B>* cellule,TPL_MAP_ENTITE<TPL_CELLULE_INFO<A ,CONDITION,B> * > &liste_entite)
1190     {
1191     if (boite*cellule->boite)
1192     if (cellule->feuille==1)
1193     {
1194     liste_entite.ajouter(cellule);
1195     }
1196     else
1197     {
1198     get_cellule(boite,cellule->fils[0],liste_entite);
1199     get_cellule(boite,cellule->fils[1],liste_entite);
1200     get_cellule(boite,cellule->fils[2],liste_entite);
1201     get_cellule(boite,cellule->fils[3],liste_entite);
1202     get_cellule(boite,cellule->fils[4],liste_entite);
1203     get_cellule(boite,cellule->fils[5],liste_entite);
1204     get_cellule(boite,cellule->fils[6],liste_entite);
1205     get_cellule(boite,cellule->fils[7],liste_entite);
1206     }
1207     };
1208    
1209     virtual TPL_CELLULE_INFO<A,CONDITION,B> *get_cellule(double x,double y, double z)
1210     {
1211     BOITE_3D boite(x,y,z,x,y,z);
1212     TPL_CELLULE_INFO<A ,CONDITION,B>* cellule=lst_entite_cellule.get(0);
1213     TPL_MAP_ENTITE<TPL_CELLULE_INFO<A ,CONDITION,B> * > liste_cellule;
1214     get_cellule(boite,cellule,liste_cellule);
1215     if (liste_cellule.get_nb()==1) return liste_cellule.get(0);
1216     return NULL;
1217     };
1218    
1219    
1220     virtual void rechercher(double xcentre,double ycentre,double zcentre,double rayon_recherche,TPL_MAP_ENTITE<A>& liste_entite_trouve)
1221     {
1222     BOITE_3D boite(xcentre-rayon_recherche,ycentre-rayon_recherche,zcentre-rayon_recherche,xcentre+rayon_recherche,ycentre+rayon_recherche,zcentre+rayon_recherche);
1223     TPL_CELLULE_INFO<A ,CONDITION,B>* cellule=lst_entite_cellule.get(0);
1224     rechercher(boite,liste_entite_trouve,cellule);
1225     };
1226    
1227    
1228    
1229     virtual void inserer(BOITE_3D& boite,A a,TPL_CELLULE_INFO<A ,CONDITION,B>* cellule)
1230     {
1231     if (boite*cellule->boite)
1232     if (cellule->feuille==1)
1233     {
1234     cellule->lst_entite_A.ajouter(a);
1235     }
1236     else
1237     {
1238     inserer(boite,a,cellule->fils[0]);
1239     inserer(boite,a,cellule->fils[1]);
1240     inserer(boite,a,cellule->fils[2]);
1241     inserer(boite,a,cellule->fils[3]);
1242     inserer(boite,a,cellule->fils[4]);
1243     inserer(boite,a,cellule->fils[5]);
1244     inserer(boite,a,cellule->fils[6]);
1245     inserer(boite,a,cellule->fils[7]);
1246     }
1247     };
1248    
1249     virtual void supprimer(BOITE_3D& boite,A a,TPL_CELLULE_INFO<A ,CONDITION,B>* cellule)
1250     {
1251     if (boite*cellule->boite)
1252     if (cellule->feuille==1)
1253     {
1254     cellule->lst_entite_A.supprimer(a);
1255     }
1256     else
1257     {
1258     supprimer(boite,a,cellule->fils[0]);
1259     supprimer(boite,a,cellule->fils[1]);
1260     supprimer(boite,a,cellule->fils[2]);
1261     supprimer(boite,a,cellule->fils[3]);
1262     supprimer(boite,a,cellule->fils[4]);
1263     supprimer(boite,a,cellule->fils[5]);
1264     supprimer(boite,a,cellule->fils[6]);
1265     supprimer(boite,a,cellule->fils[7]);
1266     }
1267     };
1268    
1269     virtual void inserer(A a)
1270     {
1271     BOITE_3D boite=a->get_boite_3D();
1272     TPL_CELLULE_INFO<A ,CONDITION,B>* cellule=lst_entite_cellule.get(0);
1273     inserer(boite,a,cellule);
1274     }
1275    
1276     virtual void supprimer(A a)
1277     {
1278     BOITE_3D boite=a->get_boite_3D();
1279     TPL_CELLULE_INFO<A ,CONDITION,B>* cellule=lst_entite_cellule.get(0);
1280     supprimer(boite,a,cellule);
1281     }
1282    
1283    
1284     private:
1285     TPL_LISTE_ENTITE<TPL_CELLULE_INFO<A ,CONDITION,B>* > lst_entite_cellule;
1286    
1287     };
1288    
1289    
1290    
1291    
1292     #endif