00001
00012 #include "Brep.h"
00013
00014
00015 using namespace jbxl;
00016
00017
00019
00020
00021
00027 BREP_SOLID::BREP_SOLID()
00028 {
00029 freed = false;
00030
00031 facetno = 0;
00032 vertexno = 0;
00033 vertexid = 0;
00034 vcount = 0;
00035
00036 counter = NULL;
00037 octree = new OctreeNode(NULL, this);
00038 rbound.set(HUGE_VALF, -HUGE_VALF, HUGE_VALF, -HUGE_VALF, HUGE_VALF, -HUGE_VALF);
00039 }
00040
00041
00042
00048 BREP_SOLID::~BREP_SOLID()
00049 {
00050 if (!freed) FreeData();
00051 }
00052
00053
00054
00060 void BREP_SOLID::FreeData()
00061 {
00062 if (freed) return;
00063
00064 BREP_SHELL* pbsh;
00065 BREP_SHELL_LIST::iterator ishell = shells.begin();
00066
00067 CVCounter* cnt = NULL;
00068 if (counter!=NULL) {
00069 cnt = counter->GetUsableCounter();
00070 if (cnt!=NULL) cnt->SetMax(100);
00071 }
00072
00073 int shn = (int)shells.size();
00074 while (ishell!=shells.end()) {
00075 if (cnt!=NULL) cnt->MakeChildCounter(100/shn);
00076 pbsh = *ishell;
00077 ishell = shells.erase(ishell);
00078 delete pbsh;
00079 if (cnt!=NULL) cnt->DeleteChildCounter();
00080 }
00081
00082 contours.clear();
00083 wings.clear();
00084 shortage_wings.clear();
00085 surplus_contours.clear();
00086
00087 delete octree;
00088
00089 freed = true;
00090 }
00091
00092
00093
00097 void BREP_SOLID::CloseData()
00098 {
00099 BREP_SHELL_LIST::iterator ishell;
00100 for (ishell=shells.begin(); ishell!=shells.end(); ishell++) (*ishell)->CloseData();
00101 for (ishell=shells.begin(); ishell!=shells.end(); ishell++) rbound.fusion((*ishell)->rbound);
00102
00103 octree->ComputeVerticesNormal();
00104 }
00105
00106
00107
00113 void BREP_SOLID::ConnectShell(BREP_SHELL* shell)
00114 {
00115 if (shell!=NULL) {
00116 shell->solid = this;
00117 shells.push_back(shell);
00118 }
00119 }
00120
00121
00122
00128 void BREP_SOLID::DisconnectShell(BREP_SHELL* shell)
00129 {
00130 if (shell!=NULL) {
00131 BREP_SHELL_LIST::iterator ishell;
00132 ishell = std::find(shells.begin(), shells.end(), shell);
00133 if (ishell!=shells.end()) shells.erase(ishell);
00134 shell->solid = NULL;
00135 }
00136 }
00137
00138
00139
00140
00142
00143
00144
00145 BREP_SHELL::BREP_SHELL(BREP_SOLID* pr_solid)
00146 {
00147 solid = pr_solid;
00148 rbound.set(HUGE_VALF, -HUGE_VALF, HUGE_VALF, -HUGE_VALF, HUGE_VALF, -HUGE_VALF);
00149
00150 if (solid!=NULL) solid->ConnectShell(this);
00151 }
00152
00153
00154
00155 BREP_SHELL::~BREP_SHELL()
00156 {
00157 CVCounter* counter = NULL;
00158 if (solid!=NULL) {
00159 if (solid->counter!=NULL) {
00160 counter = solid->counter->GetUsableCounter();
00161 if (counter!=NULL) counter->SetMax(100);
00162 }
00163 solid->DisconnectShell(this);
00164 }
00165
00166
00167 BREP_FACET* pbfc;
00168 BREP_FACET_LIST::iterator ifacet = facets.begin();
00169
00170 int cnt = 0;
00171 int itv = Max(1, (int)facets.size()/100);
00172 while (ifacet!=facets.end()) {
00173 pbfc = *ifacet;
00174 ifacet = facets.erase(ifacet);
00175 pbfc->shell = NULL;
00176 delete(pbfc);
00177 cnt++;
00178
00179
00180 if (counter!=NULL && cnt%itv==0) counter->StepIt();
00181 }
00182 }
00183
00184
00185
00186 void BREP_SHELL::CloseData()
00187 {
00188 BREP_FACET_LIST::iterator ifacet;
00189 for (ifacet=facets.begin(); ifacet!=facets.end(); ifacet++) (*ifacet)->CloseData();
00190 for (ifacet=facets.begin(); ifacet!=facets.end(); ifacet++) rbound.fusion((*ifacet)->rbound);
00191 }
00192
00193
00194
00195
00196 void BREP_SHELL::ConnectFacet(BREP_FACET* facet)
00197 {
00198 if (facet!=NULL) {
00199 facet->shell = this;
00200 facets.push_back(facet);
00201 }
00202 }
00203
00204
00205
00206
00207 void BREP_SHELL::DisconnectFacet(BREP_FACET* facet)
00208 {
00209 if (facet!=NULL) {
00210 BREP_FACET_LIST::iterator ifacet;
00211 ifacet = std::find(facets.begin(), facets.end(), facet);
00212 if (ifacet!=facets.end()) facets.erase(ifacet);
00213
00214 facet->shell = NULL;
00215 }
00216 }
00217
00218
00219
00220
00222
00223
00224
00225 BREP_FACET::BREP_FACET(BREP_SHELL* pr_shell)
00226 {
00227 shell = pr_shell;
00228 if (shell!=NULL) shell->ConnectFacet(this);
00229
00230 deletable = false;
00231 notdelete = false;
00232 tolerance = Facet_Tolerance;
00233
00234 rbound.set(HUGE_VALF, -HUGE_VALF, HUGE_VALF, -HUGE_VALF, HUGE_VALF, -HUGE_VALF);
00235 }
00236
00237
00238
00239 BREP_FACET::~BREP_FACET()
00240 {
00241 if (shell!=NULL) shell->DisconnectFacet(this);
00242
00243 BREP_CONTOUR* bpcn;
00244 BREP_CONTOUR_LIST::iterator icon = outer_contours.begin();
00245 while (icon!=outer_contours.end()) {
00246 bpcn = *icon;
00247 icon = outer_contours.erase(icon);
00248 bpcn->facet = NULL;
00249 delete (bpcn);
00250 }
00251 }
00252
00253
00254
00255 void BREP_FACET::CloseData()
00256 {
00257 BREP_CONTOUR_LIST::iterator icon;
00258 for (icon=outer_contours.begin(); icon!=outer_contours.end(); icon++) (*icon)->CloseData();
00259
00260 ComputePlaneEquation();
00261 }
00262
00263
00264
00265
00266 void BREP_FACET::ConnectContour(BREP_CONTOUR* contour)
00267 {
00268 if (contour!=NULL) {
00269 contour->facet = this;
00270 outer_contours.push_back(contour);
00271 }
00272 }
00273
00274
00275
00276 void BREP_FACET::DisconnectContour(BREP_CONTOUR* contour)
00277 {
00278 if (contour!=NULL) {
00279 BREP_CONTOUR_LIST::iterator icon;
00280 icon = std::find(outer_contours.begin(), outer_contours.end(), contour);
00281 if (icon!=outer_contours.end()) outer_contours.erase(icon);
00282
00283 contour->facet = NULL;
00284 }
00285 }
00286
00287
00288
00296 void BREP_FACET::ComputePlaneEquation()
00297 {
00298 normal = outer_contours.front()->normal;
00299
00300 double emax = 0.0;
00301 double dmin = HUGE_VALF;
00302 double dmax = -HUGE_VALF;
00303
00304 BREP_CONTOUR_LIST::iterator icon;
00305 for (icon=outer_contours.begin(); icon!=outer_contours.end(); icon++) {
00306 (*icon)->CloseFacet(this, emax, dmin, dmax);
00307 }
00308
00309 d = (dmin + dmax)/2.0;
00310 tolerance = Max((dmax - dmin)/2.0, Facet_Tolerance);
00311 tolerance = Max(emax, tolerance);
00312
00313 double extent = tolerance + emax;
00314 rbound.xmin -= extent;
00315 rbound.ymin -= extent;
00316 rbound.zmin -= extent;
00317 rbound.xmax += extent;
00318 rbound.ymax += extent;
00319 rbound.zmax += extent;
00320 }
00321
00322
00323
00324
00326
00327
00328 BREP_CONTOUR::BREP_CONTOUR(BREP_FACET* pr_facet)
00329 {
00330 facet = pr_facet;
00331 if (facet!=NULL) facet->ConnectContour(this);
00332
00333 wing = NULL;
00334 dup_edge = 0;
00335 collision = false;
00336 hasCollisionVector = false;
00337
00338 rbound.set(HUGE_VALF, -HUGE_VALF, HUGE_VALF, -HUGE_VALF, HUGE_VALF, -HUGE_VALF);
00339 }
00340
00341
00342
00343
00344 BREP_CONTOUR::~BREP_CONTOUR()
00345 {
00346 if (facet!=NULL) facet->DisconnectContour(this);
00347 DestroyWings();
00348 }
00349
00350
00351
00352 void BREP_CONTOUR::CloseData()
00353 {
00354 ComputeNormal();
00355 if (!hasCollisionVector) ComputeDirectRS();
00356 }
00357
00358
00359
00365 void BREP_CONTOUR::ConnectWing(BREP_WING* new_wing)
00366 {
00367 if (new_wing==NULL) return;
00368
00369 new_wing->contour = this;
00370
00371 if (wing==NULL) {
00372 new_wing->next = new_wing->prev = new_wing;
00373 wing = new_wing;
00374 }
00375 else {
00376 new_wing->next = wing;
00377 new_wing->prev = wing->prev;
00378 new_wing->prev->next = new_wing->next->prev = new_wing;
00379 }
00380 }
00381
00382
00383
00384
00385 void BREP_CONTOUR::DisconnectWing(BREP_WING* dis_wing)
00386 {
00387 if (wing==dis_wing) {
00388 if (dis_wing->next==dis_wing) {
00389 wing = NULL;
00390 }
00391 else {
00392 wing = dis_wing->next;
00393 }
00394 }
00395
00396 if (dis_wing->next) dis_wing->next->prev = dis_wing->prev;
00397 if (dis_wing->prev) dis_wing->prev->next = dis_wing->next;
00398
00399 dis_wing->contour = NULL;
00400 dis_wing->next = dis_wing->prev = NULL;
00401 }
00402
00403
00404
00411 BREP_WING* BREP_CONTOUR::CreateWing(BREP_VERTEX* vertex1, BREP_VERTEX* vertex2)
00412 {
00413
00414
00415 if (wing!=NULL && GetWingOtherSide(wing->prev)->vertex!=vertex1) {
00416 DEBUG_MODE PRINT_MESG("CreateWing: Irregular order of Wings!!\n");
00417 return NULL;
00418 }
00419
00420
00421
00422 BREP_WING* wing = CreateWingWithoutContour(vertex1, vertex2);
00423 if (wing==NULL) return NULL;
00424
00425
00426 ConnectWing(wing);
00427 if (GetWingOtherSide(wing)->contour!=NULL) {
00428 wing->edge->complete = true;
00429 }
00430
00431
00432 (wing->vertex)->wing_list.push_back(wing);
00433
00434 return wing;
00435 }
00436
00437
00438
00444 void BREP_CONTOUR::DestroyWings()
00445 {
00446 BREP_WING* first = wing;
00447 if (first==NULL) return;
00448
00449 BREP_WING* prev;
00450 for (BREP_WING* swing=first->prev; swing!=first; swing=prev) {
00451 prev = swing->prev;
00452 DestroyWing(swing);
00453 }
00454 DestroyWing(first);
00455 }
00456
00457
00458
00459
00460 void BREP_CONTOUR::CloseFacet(BREP_FACET* facet, double& emax, double& dmin, double& dmax)
00461 {
00462 if (wing==NULL) return;
00463
00464 BREP_WING* next = wing;
00465 do {
00466 BREP_WING* swing = next;
00467
00468 double d = -(facet->normal * swing->vertex->point);
00469 if (d < dmin) dmin = d;
00470 if (d > dmax) dmax = d;
00471 if (swing->vertex->tolerance>emax) emax = swing->vertex->tolerance;
00472 facet->rbound.fusion(swing->vertex->point);
00473
00474 next = swing->next;
00475 } while (next!=wing);
00476 }
00477
00478
00479
00485 void BREP_CONTOUR::ComputeNormal()
00486 {
00487 normal = Vector<double>(0.0, 0.0, 0.0);
00488
00489 Vector<double> next = wing->vertex->point;
00490 for (BREP_WING* swing=wing; swing; swing=(swing->next==wing ? NULL:swing->next)) {
00491 Vector<double> cur = next;
00492 next = swing->next->vertex->point;
00493 normal.x += (cur.y - next.y) * (cur.z + next.z);
00494 normal.y += (cur.z - next.z) * (cur.x + next.x);
00495 normal.z += (cur.x - next.x) * (cur.y + next.y);
00496 }
00497 normal.normalize();
00498 }
00499
00500
00501
00507 void BREP_CONTOUR::ComputeDirectRS()
00508 {
00509 TVector<double> point = Vertex2TVector(wing->next->vertex);
00510
00511 directR = point - Vertex2TVector(wing->vertex);
00512 directS = Vertex2TVector(wing->next->next->vertex) - point;
00513 directRS = directR^directS;
00514 directR.norm();
00515 directS.norm();
00516 directRS.norm();
00517
00518 hasCollisionVector = true;
00519 }
00520
00521
00522
00523 BREP_CONTOUR* CreateContour(BREP_FACET* facet)
00524 {
00525 BREP_CONTOUR* contour = new BREP_CONTOUR(facet);
00526 return contour;
00527 }
00528
00529
00530
00531
00533
00534
00535
00536 BREP_WING::BREP_WING(BREP_VERTEX* vx)
00537 {
00538 vertex = vx;
00539 prev = NULL;
00540 next = NULL;
00541 edge = NULL;
00542 contour = NULL;
00543 }
00544
00545
00546
00547
00549
00550
00551
00552 BREP_EDGE::BREP_EDGE(BREP_VERTEX* vertex1, BREP_VERTEX* vertex2)
00553 {
00554 wing1 = new BREP_WING(vertex1);
00555 wing2 = new BREP_WING(vertex2);
00556 wing1->edge = wing2->edge = this;
00557
00558 center = (vertex1->point + vertex2->point)/2.;
00559 edge_list = NULL;
00560 complete = false;
00561 tolerance = Edge_Tolerance;
00562 }
00563
00564
00565
00566 BREP_EDGE::~BREP_EDGE()
00567 {
00568
00569 if (wing1->contour || wing2->contour) {
00570 DEBUG_MODE PRINT_MESG("~BREP_EDGE: This Edge is still used!!\n");
00571 }
00572
00573 delete wing1;
00574 delete wing2;
00575
00576 if (edge_list!=NULL) {
00577 BREP_EDGE_LIST::iterator iedge = std::find(edge_list->begin(), edge_list->end(), this);
00578 if (iedge!=edge_list->end()) {
00579 edge_list->erase(iedge);
00580 }
00581 if (edge_list->size()==1) {
00582 (*(edge_list->begin()))->edge_list = NULL;
00583 delete(edge_list);
00584 }
00585 }
00586 }
00587
00588
00589
00590 void BREP_EDGE::CloseData()
00591 {
00592
00593 double e = (wing1->vertex)->tolerance;
00594 e = Max(e, (wing2->vertex)->tolerance);
00595 tolerance = Max(e, tolerance);
00596 }
00597
00598
00599
00600
00602
00603
00604
00605 BREP_VERTEX::BREP_VERTEX()
00606 {
00607
00608
00609 index = -1;
00610 calc_normal = true;
00611 tolerance = Abs_Vertex_Tolerance;
00612
00613 forbidden_list = NULL;
00614 distance2 = HUGE_VALF;
00615 }
00616
00617
00618
00619 BREP_VERTEX::~BREP_VERTEX()
00620 {
00621
00622 if (!wing_list.empty()) {
00623 DEBUG_MODE PRINT_MESG("~BREP_VERTEX: List of Wing is not empty!!\n");
00624 }
00625
00626 if (forbidden_list!=NULL) {
00627 forbidden_list->clear();
00628 delete(forbidden_list);
00629 }
00630 }
00631
00632
00633
00634
00635 void BREP_VERTEX::DisconnectWing(BREP_WING* wing)
00636 {
00637 BREP_WING_LIST::iterator iwing;
00638
00639 iwing = std::find(wing_list.begin(), wing_list.end(), wing);
00640 if (iwing==wing_list.end()) {
00641
00642 DEBUG_MODE PRINT_MESG("DisconnectWing: This Wing does not connect with Vertex!!\n");
00643 return;
00644 }
00645
00646 wing_list.erase(iwing);
00647 }
00648
00649
00650
00657 void BREP_VERTEX::ComputeNormal()
00658 {
00659 normal = Vector<double>(0.0, 0.0, 0.0);
00660
00661 BREP_WING_LIST::iterator iwing;
00662 for (iwing=wing_list.begin(); iwing!=wing_list.end(); iwing++) {
00663 BREP_WING* wing = *iwing;
00664 normal = normal + (wing->contour->facet)->normal;
00665 }
00666 normal.normalize();
00667 }
00668
00669
00670
00671 void BREP_VERTEX::ComputeTolerance()
00672 {
00673 double max = Xabs(point.x);
00674 max = Max(max, Xabs(point.y));
00675 max = Max(max, Xabs(point.z));
00676
00677 tolerance = Vertex_Tolerance * max;
00678 tolerance = Max(tolerance, Abs_Vertex_Tolerance);
00679 }
00680
00681
00682
00683 void BREP_VERTEX::CloseData()
00684 {
00685 ComputeTolerance();
00686 }
00687
00688
00689
00690
00692
00693
00694
00695 OctreeNode::OctreeNode(BREP_VERTEX* new_vertex, BREP_SOLID* sld)
00696 {
00697 solid = sld;
00698 vertex = new_vertex;
00699
00700 if (solid!=NULL && vertex!=NULL) {
00701 vertex->index = solid->vertexid;
00702 solid->vertexid++;
00703 }
00704
00705 for (int i=0; i<8; i++) child[i] = NULL;
00706 }
00707
00708
00709
00710 OctreeNode::~OctreeNode()
00711 {
00712 for (int i=0; i<8; i++) {
00713 if (child[i]!=NULL) delete child[i];
00714 }
00715
00716 if (vertex!=NULL) {
00717 delete vertex;
00718 if (solid!=NULL) solid->vertexno--;
00719 }
00720 }
00721
00722
00723
00730 OctreeNode* OctreeNode::AddWithUnique(BREP_VERTEX* new_vertex)
00731 {
00732 OctreeNode* o = NULL;
00733 OctreeNode* p = this;
00734 int cmp = -1;
00735
00736 if (new_vertex==NULL) return NULL;
00737
00738 if (p->vertex==NULL) {
00739 p->vertex = new_vertex;
00740 if (solid!=NULL) {
00741 p->vertex->index = solid->vertexid;
00742 solid->vertexid++;
00743 }
00744 return p;
00745 }
00746
00747 while (p!=NULL) {
00748 cmp = CompareVertex(p->vertex, new_vertex);
00749 if (cmp==8) return p;
00750 o = p;
00751 p = p->child[cmp];
00752 }
00753
00754 if (cmp>=0 && o!=NULL) {
00755 p = new OctreeNode(new_vertex, solid);
00756 o->child[cmp] = p;
00757 }
00758 return p;
00759 }
00760
00761
00762
00769 OctreeNode* OctreeNode::AddWithDuplicates(BREP_VERTEX* new_vertex)
00770 {
00771 OctreeNode* o = NULL;
00772 OctreeNode* p = this;
00773 int cmp = -1;
00774
00775 if (new_vertex==NULL) return this;
00776
00777 if (p->vertex==NULL) {
00778 p->vertex = new_vertex;
00779 if (solid!=NULL) {
00780 p->vertex->index = solid->vertexid;
00781 solid->vertexid++;
00782 }
00783 return p;
00784 }
00785
00786 while (p!=NULL) {
00787 cmp = CompareVertex(p->vertex, new_vertex);
00788 o = p;
00789 p = p->child[cmp%8];
00790 }
00791
00792 if (cmp>=0 && o!=NULL) {
00793 p = new OctreeNode(new_vertex, solid);
00794 o->child[cmp%8] = p;
00795 }
00796 return p;
00797 }
00798
00799
00800
00806 OctreeNode* OctreeNode::FindSubtree(BREP_VERTEX* element)
00807 {
00808 OctreeNode* p = this;
00809 while(p!=NULL){
00810 int cmp = CompareVertex(p->vertex, element);
00811 if (cmp==8) return p;
00812 if (cmp<0) return NULL;
00813 p = p->child[cmp];
00814 }
00815 return NULL;
00816 }
00817
00818
00819
00820
00821 void OctreeNode::ComputeVerticesNormal(void)
00822 {
00823 for (int i=0; i<8; i++) {
00824 if (child[i]!=NULL) {
00825 child[i]->ComputeVerticesNormal();
00826 }
00827 }
00828 if (vertex!=NULL && vertex->calc_normal) vertex->ComputeNormal();
00829 }
00830
00831
00832
00833
00835
00836
00837
00845 DllExport void jbxl::ConnectWingToVertex(BREP_WING* wing)
00846 {
00847 if (wing!=NULL) {
00848 (wing->vertex)->wing_list.push_back(wing);
00849 }
00850 }
00851
00852
00853
00861 DllExport BREP_WING* jbxl::CreateWingWithoutContour(BREP_VERTEX* vertex1, BREP_VERTEX* vertex2)
00862 {
00863 BREP_WING* wing = NULL;
00864
00865
00866 BREP_EDGE* edge = FindEdge(vertex1, vertex2);
00867 if (edge==NULL) {
00868 edge = CreateEdge(vertex1, vertex2);
00869 if (edge==NULL) return NULL;
00870 wing = edge->wing1;
00871 }
00872 else{
00873 if (edge->wing1->vertex==vertex1) wing = edge->wing1;
00874 else if (edge->wing2->vertex==vertex1) wing = edge->wing2;
00875
00876
00877 if (edge->edge_list!=NULL && wing!=NULL && wing->contour!=NULL) {
00878 BREP_EDGE_LIST::iterator iedge=edge->edge_list->begin();
00879 while (iedge!=edge->edge_list->end() && wing->contour!=NULL) {
00880 if ((*iedge)->wing1->vertex==vertex1) wing = (*iedge)->wing1;
00881 else if ((*iedge)->wing2->vertex==vertex1) wing = (*iedge)->wing2;
00882 iedge++;
00883 }
00884 }
00885
00886
00887 if (wing!=NULL && wing->contour!=NULL){
00888
00889 BREP_EDGE* new_edge = CreateEdge(vertex1, vertex2);
00890 if (new_edge==NULL) return NULL;
00891
00892 if (edge->edge_list==NULL) {
00893 edge->edge_list = new BREP_EDGE_LIST();
00894 edge->edge_list->push_back(edge);
00895 }
00896 edge->edge_list->push_back(new_edge);
00897 new_edge->edge_list = edge->edge_list;
00898 wing = new_edge->wing1;
00899 }
00900 }
00901
00902 return wing;
00903 }
00904
00905
00906
00910 DllExport void jbxl::DestroyWing(BREP_WING* wing)
00911 {
00912 wing->vertex->DisconnectWing(wing);
00913 wing->contour->DisconnectWing(wing);
00914
00915 if (!wing->edge->wing1->contour && !wing->edge->wing2->contour) {
00916 delete wing->edge;
00917 }
00918 else {
00919 wing->edge->complete = false;
00920 }
00921 }
00922
00923
00924
00935 DllExport BREP_EDGE* jbxl::FindEdge(BREP_VERTEX* vertex1, BREP_VERTEX* vertex2)
00936 {
00937 BREP_WING_LIST list;
00938 BREP_WING_LIST::iterator iwing;
00939
00940 list = vertex2->wing_list;
00941 for (iwing=list.begin(); iwing!=list.end(); iwing++){
00942 BREP_WING* wing = *iwing;
00943 if (GetWingOtherSide(wing)->vertex==vertex1) return wing->edge;
00944 }
00945
00946 list = vertex1->wing_list;
00947 for (iwing=list.begin(); iwing!=list.end(); iwing++) {
00948 BREP_WING* wing = *iwing;
00949 if (GetWingOtherSide(wing)->vertex==vertex2) return wing->edge;
00950 }
00951
00952 return NULL;
00953 }
00954
00955
00956
00960 DllExport BREP_VERTEX** jbxl::GetOctreeVertices(OctreeNode* octree, long int* vertexno)
00961 {
00962 BREP_VERTEX** vindex = NULL;
00963
00964 long int num = OctreeGetter(octree, NULL, 0);
00965
00966 if (num>0) {
00967 vindex = (BREP_VERTEX**)malloc(num*sizeof(BREP_VERTEX*));
00968 }
00969 if (vindex!=NULL) {
00970 OctreeGetter(octree, vindex, 0);
00971 }
00972 if (vertexno!=NULL) *vertexno = num;
00973
00974 return vindex;
00975 }
00976
00977
00978
00979 long int jbxl::OctreeGetter(OctreeNode* p, BREP_VERTEX** vtx, long int counter)
00980 {
00981 if (p->vertex!=NULL) {
00982 if (vtx!=NULL && p->vertex->index>=0) vtx[p->vertex->index] = p->vertex;
00983 counter++;
00984 }
00985
00986 for (int i=0; i<8; i++) {
00987 if (p->child[i]!=NULL) {
00988 counter = OctreeGetter(p->child[i], vtx, counter);
00989 }
00990 }
00991
00992 return counter;
00993 }
00994
00995
00996
01019 DllExport BREP_VERTEX* jbxl::AddVertex2Octree(BREP_VERTEX* vert, OctreeNode* octree, bool dupli)
01020 {
01021 OctreeNode* node;
01022
01023 if (dupli) node = octree->AddWithDuplicates(vert);
01024 else node = octree->AddWithUnique(vert);
01025 if (node==NULL) return NULL;
01026
01027 if (node->vertex==vert) {
01028 octree->solid->vertexno++;
01029 }
01030 return node->vertex;
01031 }
01032
01033
01034
01037 DllExport BREP_EDGE* jbxl::CreateEdge(BREP_VERTEX* v1, BREP_VERTEX* v2)
01038 {
01039 if (v1==v2) return NULL;
01040
01041 BREP_EDGE* edge = new BREP_EDGE(v1, v2);
01042 edge->CloseData();
01043
01044 return edge;
01045 }
01046
01047
01048
01057 DllExport int jbxl::CompareVertex(BREP_VERTEX* v1, BREP_VERTEX* v2)
01058 {
01059 double tolerance = v1->tolerance + v2->tolerance;
01060 double dist2 = (v1->point.x - v2->point.x)*(v1->point.x - v2->point.x) +
01061 (v1->point.y - v2->point.y)*(v1->point.y - v2->point.y) +
01062 (v1->point.z - v2->point.z)*(v1->point.z - v2->point.z);
01063 if (dist2<=tolerance*tolerance && v1->uvmap==v2->uvmap) return 8;
01064
01065 int code = 0;
01066 if (v1->point.x > v2->point.x) code += 1;
01067 if (v1->point.y > v2->point.y) code += 2;
01068 if (v1->point.z > v2->point.z) code += 4;
01069 return code;
01070 }
01071
01072
01073
01079 DllExport BREP_WING* jbxl::GetWingOtherSide(BREP_WING* wing)
01080 {
01081 if (wing==wing->edge->wing1) return wing->edge->wing2;
01082
01083 return wing->edge->wing1;
01084 }
01085
01086
01087
01088 DllExport TVector<double> jbxl::Vertex2TVector(BREP_VERTEX* v)
01089 {
01090 TVector<double> tv;
01091
01092 tv.x = v->point.x;
01093 tv.y = v->point.y;
01094 tv.z = v->point.z;
01095 tv.n = sqrt(tv.x*tv.x + tv.y*tv.y + tv.z*tv.z);
01096 tv.t = v->tolerance;
01097
01098 return tv;
01099 }
01100