| ²é¿´: 1284 | »Ø¸´: 4 | ||
lxb9721гæ (ÕýʽдÊÖ)
|
[ÇóÖú]
Çó¸ßÊÖ°ïæÐÞ¸ÄÒ»¶Îc++³ÌÐò
|
|
ÏÂÃæÊÇÒ»¶Î½¨Á¢K-DÊ÷µÄc++³ÌÐò£¬ÆäÖгÌÐòÖеÄÊý¾ÝÒªÐÞ¸ÄΪÓöÁÈëtxtÎļþÖеÄÊý¾ÝÀ´±íʾ£¬ÇÒtxtÎļþÖеÄÊý¾ÝΪÈýÁУ¬ÒÔ¿Õ¸ñ·Ö¸î¡£ main.cpp #include "3dtree.h" #include #include void main() { time_t tt = time(NULL); //½¨Á¢Ê÷ int nodes = 100000; kdTree t(nodes); //´æ´¢Êý¾Ý t.store(1.0,1.0,1.0,0); t.store(1.0,2.0,1.0,1); t.store(2.0,4.0,5.0,2); t.store(3.0,1.0,2.0,3); t.store(4.0,6.0,2.0,4); t.store(1.0,5.0,8.0,5); //µ÷ÕûÊ÷ t.treeBalance(); //ѰÕÒ×î½üÁÙµã nNearestNodes nNN(3); nNN.setDistandIndx(4.3); nNN.setSearchPnt(2.0,4.0,5.0); cout << endl << "searching ..." << endl; t.locateNodes(&nNN,1); if(nNN.found) for(int i = 1; i <= nNN.found; i++) { cout < < "id of the nearest point: " << nNN.index->id < < endl < < "the dis: " < < nNN.dist2 << endl; cout < < "the coordinates of the point:"; cout < < nNN.index->pos[0] < < " " < < nNN.index->pos[1] < < " " < < nNN.index->pos[2] < < endl < < endl; } else cout << "Nothing found!" << endl << endl; cout<<"run time:"< system("pause" ;} 3dtree.cpp #include "3dtree.h" kdTree::kdTree(const int nodes) { storedKDNodes = 0; maxNumOfNodes = nodes; kdNodes = new kdNode[maxNumOfNodes + 1]; if(!kdNodes) { cout<<"³õʼ»¯kdÊ÷ʱÄÚ´æÒç³ö£¡"< } boundrayMin[0] = boundrayMin[1] = boundrayMin[2] = 1e8f; boundrayMax[0] = boundrayMax[1] = boundrayMax[2] = -1e8f; } kdTree::~kdTree() { delete [] kdNodes; } void kdTree::treeBalance() { if(storedKDNodes > 1) { kdNode **pa1 = new kdNode*[storedKDNodes + 1]; //×éÖ¯ºÃÊ÷ºóµÄÖ¸Õë kdNode **pa2 = new kdNode*[storedKDNodes + 1]; //ÔÊ¼ÔªËØµÄÖ¸Õë for(int i =0; i <= storedKDNodes; i++) pa2 = &kdNodes; balancePartition(pa1, pa2, 1, 1, storedKDNodes); delete []pa2; //ÖØÐÂÅÅÁÐÊ÷ //__w64 int d, j = 1; // According to the warning given when 'int ' is used int d, j = 1; //jλÖÃÔªËØÒÑ¾×ªÒÆ×ß int foo = 1; //fooNodes´æ´¢µÄÔªËØµÄ×î³õλÖà kdNode fooNodes = kdNodes[j]; for( int i = 1; i <= storedKDNodes; i++) { d = pa1[j] - kdNodes; pa1[j] = NULL; if(d != foo) kdNodes[j] = kdNodes[d]; else { kdNodes[j] = fooNodes; if(i < storedKDNodes) { for(; foo <= storedKDNodes; foo++) if(NULL != pa1[foo]) break; fooNodes = kdNodes[foo]; j = foo; } continue; } j = d; } delete []pa1; } halfStoredKDNodes = storedKDNodes/2 - 1; } void kdTree::locateNodes(nNearestNodes * const nNN,const int index)const { const kdNode *p = &kdNodes[index]; double dist1; if(index < halfStoredKDNodes) { dist1 = nNN->pos[p->plane] - p->pos[p->plane]; if(0.0 < dist1) { locateNodes(nNN, 2 * index + 1); if(nNN->dist2[0] > dist1 * dist1) locateNodes(nNN, 2 * index); } else { locateNodes(nNN, 2 * index); if(nNN->dist2[0] > dist1 * dist1) locateNodes(nNN, 2 * index + 1); }//if }//if // ¼ÆËã¾àÀë dist1 = p->pos[0] - nNN->pos[0]; double dist2 = dist1 * dist1; dist1 = p->pos[1] - nNN->pos[1]; dist2 += dist1 * dist1; dist1 = p->pos[2] - nNN->pos[2]; dist2 += dist1 * dist1; if(nNN->dist2[0] > dist2) { if(nNN->found < nNN->max) { nNN->found++; nNN->dist2[nNN->found] = dist2; nNN->index[nNN->found] = p; } else { int j, parent; if(0 == nNN->got_Heap)//½¨Á¢´ó¶¥¶Ñ { double dst2; const kdNode *nd; int halfFound = nNN->found >> 1; for(int k = halfFound; k >= 1; k--) { parent = k; nd = nNN->index[k]; dst2 = nNN->dist2[k]; while(parent <= halfFound) { j = parent + parent; if(j < nNN->found && nNN->dist2[j] < nNN->dist2[j + 1]) j ++; if(dst2 >= nNN->dist2[j]) break; nNN->dist2[parent] = nNN->dist2[j]; nNN->index[parent] = nNN->index[j]; parent = j; }//while nNN->dist2[parent] = dst2; nNN->index[parent] = nd; }//for nNN->got_Heap = 1; }//if //²åÈë parent = 1; //if() j = 2; while(j <= nNN->found) { if(j < nNN->found && nNN->dist2[j] < nNN->dist2[j + 1]) j++; if(dist2 > nNN->dist2[j]) break; nNN->dist2[parent] = nNN->dist2[j]; nNN->index[parent] = nNN->index[j]; parent = j; j += j; }//while if((parent != 1)||(dist2 < nNN->dist2[parent])) { nNN->index[parent] = p; nNN->dist2[parent] = dist2; } nNN->dist2[0] = nNN->dist2[1];//?????? }//else }//if } #define swap(kdN,a,b){ kdNode* tmp = kdN[a]; kdN[a] = kdN; kdN = tmp;} void kdTree::medianPartition(kdNode** pOrig,const int start,const int end,const int median,const int axis) { int left = start; int right = end; while(right > left) { const TYPE v = pOrig[right]->pos[axis]; int i = left - 1; int j = right; for(; ![]() { while(pOrig[++i]->pos[axis] < v); while(pOrig[--j]->pos[axis] > v && j > left); if(i >= j) break; swap(pOrig, i, j); } swap(pOrig, i, right); if(i >= median) right = i - 1; if(i <= median) left = i + 1; } } void kdTree::balancePartition(kdNode** pBalanced,kdNode** pOriginal,const int index,const int start,const int end) { //¼ÆËãmedian£¬ÕâÊÇÔõô¼ÆËãµÄÄØ£¿£¿£¿ int median = 1; while((4 * median) <= (end - start + 1)) median += median; //median*=2; if((3 * median) <= (end - start +1)) { median += median; median += start - 1; } else median = end - median + 1; // ѰÕÒ·Ö¸îÊý¾ÝµÄÖá int axis = 2; if((boundrayMax[0] - boundrayMin[0]) > (boundrayMax[1] - boundrayMin[1])&& (boundrayMax[0] - boundrayMin[0]) > (boundrayMax[2] - boundrayMin[2])) axis = 0; else if((boundrayMax[1] - boundrayMin[1]) > (boundrayMax[2] - boundrayMin[2])) axis = 1; // °´median·Ö¸î½Úµã medianPartition(pOriginal, start, end, median, axis); pBalanced[index] = pOriginal[median]; pBalanced[index]->plane = axis; // µü´úƽºâ×óÓÒ×ÓÊ÷ if(median > start) { if(start < median - 1) { const float tmp = boundrayMax[axis]; boundrayMax[axis] = pBalanced[index]->pos[axis]; balancePartition(pBalanced, pOriginal, 2 * index, start, median - 1); boundrayMax[axis] = tmp; } else pBalanced[2 * index] = pOriginal[start]; } if(median < end) { if(median + 1 < end) { const float tmp = boundrayMin[axis]; boundrayMin[axis] = pBalanced[index]->pos[axis]; balancePartition(pBalanced, pOriginal, 2 * index + 1, median + 1, end); boundrayMin[axis] = tmp; } else pBalanced[2 * index + 1] = pOriginal[end]; } } [ Last edited by lxb9721 on 2012-7-2 at 20:27 ] |
» ²ÂÄãϲ»¶
081700£¬311£¬Çóµ÷¼Á
ÒѾÓÐ15È˻ظ´
Ò»Ö¾Ô¸±±¾©»¯¹¤085600 310·ÖÇóµ÷¼Á
ÒѾÓÐ18È˻ظ´
085600²ÄÁÏÓ뻯¹¤301·ÖÇóµ÷¼ÁԺУ
ÒѾÓÐ4È˻ظ´
²ÄÁÏÓ뻯¹¤371Çóµ÷¼Á
ÒѾÓÐ14È˻ظ´
336²ÄÁÏÓ뻯¹¤085600Çóµ÷¼Á
ÒѾÓÐ7È˻ظ´
²ÄÁÏ334Çóµ÷¼Á
ÒѾÓÐ18È˻ظ´
331Çóµ÷¼Á
ÒѾÓÐ8È˻ظ´
332Çóµ÷¼Á
ÒѾÓÐ17È˻ظ´
Ò»Ö¾Ô¸ÄϾ©º½¿Õº½Ìì´óѧ ²ÄÁÏÓ뻯¹¤329·ÖÇóµ÷¼Á
ÒѾÓÐ4È˻ظ´
²ÄÁÏר˶322
ÒѾÓÐ7È˻ظ´
» ±¾Ö÷ÌâÏà¹Ø¼ÛÖµÌùÍÆ¼ö£¬¶ÔÄúͬÑùÓаïÖú:
ÇóÖú¸ßÊÖ°ïæ·ÒëһС¶ÎÓ¢ÎÄ£¡£¡£¡
ÒѾÓÐ1È˻ظ´
ÇëÇó¸ßÊÖ°ïæ·ÒëÒ»¶ÎµÂÎÄ(²»ÊǺܳ¤£¬²»³¬¹ý150¸ö´Ê)
ÒѾÓÐ9È˻ظ´
Çó¸ßÊÖ°ïæ·ÒëÒ»¶ÎÓ¢ÎÄ£¬ÒªÍ¨Ë³£¬²»Òª¹È¸è·ÒëÖ®ÀàµÄ£¡
ÒѾÓÐ3È˻ظ´
ÇóÖúͼƬÐ޸쬏ßÊÖ°ïæ
ÒѾÓÐ4È˻ظ´
Çó¸ßÊÖ°ïæ¸ÄÒ»ÏÂÓ¢ÎÄÕªÒª
ÒѾÓÐ12È˻ظ´
¡¾ÇóÖú¡¿Çó½«cÓïÑÔ¸ÄдΪC++£¬Çó½Ì¸ßÈ˰¡£¡
ÒѾÓÐ6È˻ظ´
¡¾ÇóÖú¡¿¸ßÊÖ°ï¿´¿´Õâ¶ÎC++´úÂë
ÒѾÓÐ4È˻ظ´
¡¾ÇóÖú¡¿Çó¸ßÊÖ°ïæ¸ÄдÊý¾Ýʵʱ¶¯Ì¬ÏÔʾ³ÌÐò~
ÒѾÓÐ8È˻ظ´
¡¾ÇóÖú¡¿Çóc++±àÒ»¸ö¼òµ¥¼ÆËãÆ÷µÄÔ´´úÂë
ÒѾÓÐ6È˻ظ´
¡¾½»Á÷¡¿VC++, C#, VB´ðÒÉרÌù
ÒѾÓÐ145È˻ظ´
libralibra
ÖÁ×ðľ³æ (ÖøÃûдÊÖ)
æôÆï½«¾ü
- ³ÌÐòÇ¿Ìû: 40
- Ó¦Öú: 817 (²©ºó)
- ½ð±Ò: 12914.1
- ºì»¨: 64
- Ìû×Ó: 2238
- ÔÚÏß: 287.3Сʱ
- ³æºÅ: 696514
- ×¢²á: 2009-02-05
- רҵ: ¼ÆËã»úÈí¼þ

2Â¥2012-07-02 20:49:24
c55719747
гæ (³õÈëÎÄ̳)
- Ó¦Öú: 0 (Ó×¶ùÔ°)
- ½ð±Ò: 8.5
- Ìû×Ó: 8
- ÔÚÏß: 3.2Сʱ
- ³æºÅ: 1869363
- ×¢²á: 2012-06-25
- רҵ: ¼ÆËã»úÈí¼þ
3Â¥2012-07-02 21:03:23
¡ï ¡ï ¡ï ¡ï ¡ï ¡ï ¡ï ¡ï ¡ï ¡ï
¸Ðл²ÎÓ룬ӦÖúÖ¸Êý +1
lxb9721: ½ð±Ò+10, ¡ï¡ï¡ïºÜÓаïÖú, http://emuch.net/bbs/viewthread.php?tid=4698994 2012-07-05 20:38:46
¸Ðл²ÎÓ룬ӦÖúÖ¸Êý +1
lxb9721: ½ð±Ò+10, ¡ï¡ï¡ïºÜÓаïÖú, http://emuch.net/bbs/viewthread.php?tid=4698994 2012-07-05 20:38:46
|
±¾ÌûÄÚÈݱ»ÆÁ±Î |
4Â¥2012-07-02 21:59:18
ÆÐÌáÉ«
ľ³æ (ÕýʽдÊÖ)
- Ó¦Öú: 13 (СѧÉú)
- ½ð±Ò: 1592.9
- ºì»¨: 1
- Ìû×Ó: 581
- ÔÚÏß: 67.4Сʱ
- ³æºÅ: 1735005
- ×¢²á: 2012-04-03
- ÐÔ±ð: GG
- רҵ: ×ÔÈ»ÓïÑÔÀí½âÓë»úÆ÷·Òë
¡¾´ð°¸¡¿Ó¦Öú»ØÌû
¡ï ¡ï ¡ï ¡ï ¡ï
¸Ðл²ÎÓ룬ӦÖúÖ¸Êý +1
lxb9721: ½ð±Ò+5, ¡ï¡ï¡ïºÜÓаïÖú, http://emuch.net/bbs/viewthread.php?tid=4698994 2012-07-05 20:38:58
¸Ðл²ÎÓ룬ӦÖúÖ¸Êý +1
lxb9721: ½ð±Ò+5, ¡ï¡ï¡ïºÜÓаïÖú, http://emuch.net/bbs/viewthread.php?tid=4698994 2012-07-05 20:38:58
| Ì«³¤ÁË£¬Ö±½Ó°ÑÔ´Îļþ¸ãÉÏÀ´£¬ÓпոøÄã¿´¿´ |

5Â¥2012-07-03 20:46:23














;
»Ø¸´´ËÂ¥