39 #include "geometry.hh"
46 CDartVertex* CExtractionImage::createFaceForExtract3d()
50 CDartVertex* first = (CDartVertex *) FMap->addMapDart();
51 FMap->linkAlpha0(first, FMap->addMapDart());
55 actu = (CDartVertex *) FMap->addMapDart();
56 FMap->linkAlpha0(actu, FMap->addMapDart());
57 FMap->linkAlpha1(actu,FMap->alpha0(prev));
60 actu = (CDartVertex *) FMap->addMapDart();
61 FMap->linkAlpha0(actu, FMap->addMapDart());
62 FMap->linkAlpha1(actu,FMap->alpha0(prev));
65 actu = (CDartVertex *) FMap->addMapDart();
66 FMap->linkAlpha0(actu, FMap->addMapDart());
67 FMap->linkAlpha1(actu,FMap->alpha0(prev));
69 FMap->linkAlpha1(FMap->alpha0(actu),first);
76 CDartVertex* CExtractionImage::createFaceForExtract3d(
const CVertex & AVertex )
78 CDartVertex* first = createFaceForExtract3d();
80 FMap->addAttribute(first, ORBIT_VERTEX,
81 new CAttributeVertex(AVertex.getX()*
STEP3D_X,
90 CDartVertex* CExtractionImage::createCubeForExtract3d(
const CVertex & AVertex )
92 CDartVertex* d1 = createFaceForExtract3d();
93 CDartVertex* d2 = createFaceForExtract3d();
94 CDartVertex* d3 = createFaceForExtract3d();
95 CDartVertex* d4 = createFaceForExtract3d();
96 CDartVertex* d5 = createFaceForExtract3d();
97 CDartVertex* last = createFaceForExtract3d(AVertex);
99 FMap->linkAlpha2(FMap->alpha101(d1),d2); FMap->linkAlpha2(FMap->alpha1010(d1), FMap->alpha0(d2));
100 FMap->linkAlpha2(FMap->alpha1(d1),FMap->alpha01(d3)); FMap->linkAlpha2(FMap->alpha10(d1),FMap->alpha010(d3));
101 FMap->linkAlpha2(FMap->alpha1(d2),FMap->alpha1010(d3)); FMap->linkAlpha2(FMap->alpha10(d2),FMap->alpha101(d3));
102 FMap->linkAlpha2(d4,FMap->alpha01(d1)); FMap->linkAlpha2(FMap->alpha0(d4),FMap->alpha010(d1));
103 FMap->linkAlpha2(FMap->alpha01(d4),FMap->alpha01(d2)); FMap->linkAlpha2(FMap->alpha010(d4),FMap->alpha010(d2));
104 FMap->linkAlpha2(FMap->alpha1(d5),d3); FMap->linkAlpha2(FMap->alpha10(d5),FMap->alpha0(d3));
105 FMap->linkAlpha2(FMap->alpha01(d5),FMap->alpha10(d4)); FMap->linkAlpha2(FMap->alpha010(d5),FMap->alpha1(d4));
106 FMap->linkAlpha2(FMap->alpha101(d5),d1); FMap->linkAlpha2(FMap->alpha1010(d5),FMap->alpha0(d1));
108 FMap->linkAlpha2(last, d5); FMap->linkAlpha2(FMap->alpha0(last), FMap->alpha0(d5));
109 FMap->linkAlpha2(FMap->alpha01(last),FMap->alpha101(d4)); FMap->linkAlpha2(FMap->alpha010(last), FMap->alpha1010(d4));
110 FMap->linkAlpha2(FMap->alpha101(last), FMap->alpha101(d2)); FMap->linkAlpha2(FMap->alpha1010(last), FMap->alpha1010(d2));
111 FMap->linkAlpha2(FMap->alpha1(last), FMap->alpha1(d3)); FMap->linkAlpha2(FMap->alpha10(last), FMap->alpha10(d3));
118 CDart* CExtractionImage::createBorder3d(
unsigned int columns,
unsigned int rows)
125 CDart* first = createFaceForExtract3d(CVertex(0,1,1));
127 prev = FMap->alpha101(first);
130 for (
unsigned int i=0;i<=columns;++i)
132 actu = createFaceForExtract3d(CVertex(i,0,1));
133 FMap->linkAlpha2(prev,actu); FMap->linkAlpha2(FMap->alpha0(prev),FMap->alpha0(actu));
135 prev = FMap->alpha101(actu);
139 FMap->linkAlpha2(prev, first); FMap->linkAlpha2(FMap->alpha0(prev), FMap->alpha0(first));
142 prev = FMap->alpha010(first);
143 prev2 = FMap->alpha101201(first);
144 for (
unsigned int j=0;j<=rows;++j)
146 for (
unsigned int i=0;i<=columns;++i)
148 actu = createFaceForExtract3d(CVertex(i,j,0));
150 FMap->linkAlpha2(actu,prev); FMap->linkAlpha2(FMap->alpha0(actu),FMap->alpha0(prev));
152 FMap->linkAlpha2(FMap->alpha1(actu),prev2); FMap->linkAlpha2(FMap->alpha10(actu),FMap->alpha0(prev2));
154 prev = FMap->alpha101(actu);
155 prev2 = FMap->alpha0121(prev2);
158 if (j==0) prev2 = FMap->alpha21(prev2);
162 FMap->linkAlpha2(prev,FMap->alpha10(first)); FMap->linkAlpha2(FMap->alpha0(prev),FMap->alpha1(first));
165 prev = FMap->alpha10121(first);
166 for (
unsigned int i=0;i<=columns;++i)
168 FMap->linkAlpha2(prev,prev2); FMap->linkAlpha2(FMap->alpha0(prev),FMap->alpha0(prev2));
170 prev = FMap->alpha0121(prev);
171 prev2 = FMap->alpha0121(prev2);
174 return FMap->alpha101(first);
179 CDart* CExtractionImage::computeUpFromLast3d(CDart* ADart)
181 CDart* res = FMap->alpha012(ADart);
183 while(!FMap->isFree3(res))
184 res = FMap->alpha32(res);
186 return FMap->alpha010(res);
191 CDart* CExtractionImage::computeBehindFromLast3d(CDart* ADart)
193 CDart* res = FMap->alpha2(ADart);
195 while(!FMap->isFree3(res))
196 res = FMap->alpha32(res);
198 return FMap->alpha101(res);
202 void CExtractionImage::mergeImage3dVolumes(CDart* ADart)
210 if (FMap->alpha3(ADart)!=FMap->alpha2(ADart))
212 tmp = FMap->alpha32(ADart); tmp2=FMap->alpha2(ADart);
213 FMap->unsew2(ADart); FMap->unsew2(FMap->alpha3(ADart));
214 FMap->sew2(tmp, tmp2);
217 ADart = FMap->alpha01(ADart);
223 ADart = FMap->alpha01(ADart);
225 FMap->delMapDart(FMap->alpha30(tmp));
226 FMap->delMapDart(FMap->alpha3(tmp));
227 FMap->delMapDart(FMap->alpha0(tmp));
228 FMap->delMapDart(tmp);
234 void CExtractionImage::mergeImage3dFaces(CDart* ADart)
238 if (FMap->alpha2(ADart)!=FMap->alpha1(ADart))
240 tmp=FMap->alpha1(ADart); tmp2=FMap->alpha21(ADart);
242 FMap->unsew1(ADart); FMap->unsew1(FMap->alpha2(ADart));
244 FMap->sew1(tmp,tmp2);
247 if (FMap->alpha02(ADart)!=FMap->alpha01(ADart))
249 tmp=FMap->alpha01(ADart); tmp2=FMap->alpha021(ADart);
251 FMap->unsew1(FMap->alpha0(ADart));
252 FMap->unsew1(FMap->alpha02(ADart));
254 FMap->sew1(tmp,tmp2);
257 FMap->delMapDart(FMap->alpha302(ADart));
258 FMap->delMapDart(FMap->alpha32(ADart));
259 FMap->delMapDart(FMap->alpha30(ADart));
260 FMap->delMapDart(FMap->alpha3(ADart));
261 FMap->delMapDart(FMap->alpha02(ADart));
262 FMap->delMapDart(FMap->alpha2(ADart));
263 FMap->delMapDart(FMap->alpha0(ADart));
264 FMap->delMapDart(ADart);
269 void CExtractionImage::mergeImage3dEdges(CDart* ADart)
274 tmp = FMap->alpha0(ADart);
275 tmp2 = FMap->alpha10(ADart);
277 FMap->topoUnsew0(ADart);
278 FMap->topoUnsew0(FMap->alpha1(ADart));
280 FMap->topoSew0(tmp,tmp2);
282 CStaticCoverage123 it(FMap,ADart);
284 FMap->delMapDart(it++);
289 bool CExtractionImage::isDegre2Vertex3d(CDart* ADart)
291 CDynamicCoverage23 it(FMap,ADart);
294 if (FMap->alpha2(*it)==FMap->alpha1(*it))
return false;
295 if (FMap->alpha12(ADart)!=FMap->alpha21(ADart))
return false;
303 CDart * CExtractionImage::isRealDegre2Vertex3d(CDart* ADart,
int AMark)
305 CDart * other = NULL;
306 int mark = FMap->getNewMark();
309 CDynamicCoverage123 it(FMap,ADart);
310 while (nb<3 && it.cont())
312 if (!FMap->isMarked(*it,mark))
314 if (!FMap->isMarked(*it,AMark))
320 CDynamicCoverage023 it2(FMap,*it);
322 FMap->setMark(it2++,mark);
327 FMap->unmarkAll(mark);
328 FMap->freeMark(mark);
330 if (nb!=2) other=NULL;
337 bool CExtractionImage::areFacesColinear(CDart* ADart)
339 CVertex vector1 = FMap->faceNormalVector(ADart);
340 CVertex vector2 = FMap->faceNormalVector(FMap->alpha2(ADart));
342 return CGeometry::areColinear(vector1, vector2);
347 bool CExtractionImage::isDegre2Edge3d(CDart* ADart)
349 return (FMap->alpha23(ADart)==FMap->alpha32(ADart));
354 void CExtractionImage::sew3Cube(CDart* ADart, CDart* TheLastDart)
356 FMap->topoSew3(FMap->alpha012(ADart),FMap->alpha1(computeUpFromLast3d(TheLastDart)));
358 FMap->topoSew3(FMap->alpha1012(ADart),computeBehindFromLast3d(TheLastDart));
360 FMap->topoSew3(FMap->alpha21012(ADart),FMap->alpha101(TheLastDart));
365 bool CExtractionImage::faceMergingWillDisconnect(CDart* ADart)
367 if (FMap->alpha1(ADart)==FMap->alpha2(ADart) ||
368 FMap->alpha01(ADart)==FMap->alpha02(ADart))
return false;
370 CDynamicCoverage01 it(FMap,ADart);
373 if (it++==FMap->alpha2(ADart))
return true;
380 bool CExtractionImage::extract3dImage(CImage3d * AImage,
int ALevel,
382 bool AKeepFictiveEdges,
383 bool AShiftFictiveEdges)
385 assert(AImage != NULL);
387 if (! AImage->isOk())
390 #ifdef DEBUG_EXTRACT_IMAGE
391 cout<<
"Avant de créer le bord supérieur : "<<endl;
394 CDart * last = createBorder3d(AImage->getWidth(),AImage->getHeight());
396 #ifdef DEBUG_EXTRACT_IMAGE
397 cout<<
"Ok on FMap->alpha créé le bord supérieur."<<endl;
422 int fictiveMark = FMap->getNewMark();
426 CVertex CurrentPointel(1,1,1);
430 for (
unsigned int j=0;j<=AImage->getHeight();++j)
432 for (
unsigned int i=0;i<=AImage->getWidth();++i)
434 #ifdef DEBUG_EXTRACT_IMAGE
435 cout<<
"On traite le voxel ("<<i<<
", "<<j<<
", "<<k<<
")"<<endl;
438 tmp = createCubeForExtract3d(CurrentPointel);
443 leftFace = FMap->alpha0(last);
444 upFace = FMap->alpha3012(last);
445 behindFace = FMap->alpha3201(last);
447 vertex1 = FMap->alpha23(leftFace);
448 vertex2 = FMap->alpha323(upFace);
449 vertex3 = FMap->alpha323(behindFace);
452 if (AImage->sameVoxelActuLeft(i,j))
454 if (vertex2==FMap->alpha13(leftFace)) vertex2=NULL;
456 mergeImage3dVolumes(leftFace);
460 if (AImage->sameVoxelActuBehind(i,j))
462 if (vertex1==FMap->alpha1(behindFace)) vertex1=NULL;
464 mergeImage3dVolumes(behindFace);
468 if (AImage->sameVoxelActuUp(i,j))
470 if (vertex3==FMap->alpha1(upFace)) vertex3=NULL;
472 mergeImage3dVolumes(upFace);
481 leftFace2 = FMap->alpha1(leftFace);
483 if ( isDegre2Edge3d(leftFace) &&
484 areFacesColinear(leftFace) )
486 mergeImage3dFaces(leftFace);
491 if ( isDegre2Edge3d(leftFace2) &&
492 areFacesColinear(leftFace2) )
494 if ( AKeepFictiveEdges &&
496 FMap->isMarked(FMap->alpha1(leftFace2),fictiveMark) )
498 FMap->markOrbit(leftFace2,ORBIT_023,fictiveMark);
499 mergeImage3dEdges(leftFace2);
504 if ( leftFace != NULL ||
505 !AKeepFictiveEdges ||
506 !faceMergingWillDisconnect(leftFace2) )
508 mergeImage3dFaces(leftFace2);
514 if (AKeepFictiveEdges)
515 FMap->markOrbit(leftFace2,ORBIT_023,fictiveMark);
520 if (behindFace!=NULL)
522 behindFace2 = FMap->alpha1(behindFace);
524 if ( isDegre2Edge3d(behindFace) &&
525 areFacesColinear(behindFace) )
527 mergeImage3dFaces(behindFace);
532 if ( isDegre2Edge3d(behindFace2) &&
533 areFacesColinear(behindFace2) )
535 if ( behindFace == NULL &&
537 FMap->isMarked(FMap->alpha1(behindFace2),fictiveMark) )
539 FMap->markOrbit(behindFace2,ORBIT_023,fictiveMark);
540 mergeImage3dEdges(behindFace2);
545 if ( behindFace != NULL ||
546 !AKeepFictiveEdges ||
547 !faceMergingWillDisconnect(behindFace2) )
549 mergeImage3dFaces(behindFace2);
555 if (AKeepFictiveEdges)
556 FMap->markOrbit(behindFace2,ORBIT_023,fictiveMark);
563 upFace2 = FMap->alpha1(upFace);
565 if ( isDegre2Edge3d(upFace) &&
566 areFacesColinear(upFace) )
568 mergeImage3dFaces(upFace);
573 if ( isDegre2Edge3d(upFace2) &&
574 areFacesColinear(upFace2) )
576 if ( upFace == NULL &&
578 FMap->isMarked(FMap->alpha1(upFace2),fictiveMark) )
580 FMap->markOrbit(upFace2,ORBIT_023,fictiveMark);
581 mergeImage3dEdges(upFace2);
586 if ( upFace != NULL ||
587 !AKeepFictiveEdges ||
588 !faceMergingWillDisconnect(upFace2) )
590 mergeImage3dFaces(upFace2);
596 if (AKeepFictiveEdges)
597 FMap->markOrbit(upFace2,ORBIT_023,fictiveMark);
606 if (vertex1!=NULL && vertex2==NULL && vertex3==NULL)
608 else if (vertex2!=NULL && vertex1==NULL && vertex3==NULL)
610 else if (vertex3!=NULL && vertex1==NULL && vertex2==NULL)
615 if (AKeepFictiveEdges && AShiftFictiveEdges)
617 vertex2=isRealDegre2Vertex3d(vertex,fictiveMark);
618 if ( vertex2!=NULL &&
619 areEdgesAlign(vertex,vertex2) )
621 FMap->shiftAllFictiveEdges(vertex,fictiveMark);
622 mergeImage3dEdges(vertex);
627 if ( isDegre2Vertex3d(vertex) &&
628 areEdgesAlign(vertex,FMap->alpha1(vertex)) )
630 mergeImage3dEdges(vertex);
638 last = FMap->alpha101(tmp);
646 if ( i == AImage->getWidth()-1 )
648 CurrentPointel.setX(0);
650 if ( j == AImage->getHeight()-1 )
652 CurrentPointel.setY(0);
653 CurrentPointel.setZ(CurrentPointel.getZ()+1);
656 CurrentPointel.setY(CurrentPointel.getY()+1);
659 CurrentPointel.setX(CurrentPointel.getX()+1);
663 std::cout <<
"Ok pour l'extraction de la plaque num "
664 << AImage->getPlaneActu() << std::endl;
667 while(AImage->readNextPlane());
669 if (ADestroyBorder && ALevel > 0)
671 CStaticCoverage012 it(FMap,last);
673 FMap->delMapDart(it++);
676 FMap->unmarkAll(fictiveMark);
677 FMap->freeMark(fictiveMark);
684 bool CExtractionImage::extract3dImage(
const std::string & AFilename,
690 bool KeepFictiveEdges,
691 bool ShiftFictiveEdges )
693 #ifdef MODULE_EXTRACTION_IMAGE_WITHOUT_MAGICK
696 CImage3dMagick image(AFilename,FirstPlane,NbPlaneToRead,Lg);
697 return extract3dImage(& image, Level,
698 DestroyBorder, KeepFictiveEdges, ShiftFictiveEdges);
699 #endif // MODULE_EXTRACTION_IMAGE_WITHOUT_MAGICK
703 bool CExtractionImage::extract3dImageCEA(
const std::string & AFilename,
708 bool KeepFictiveEdges,
709 bool ShiftFictiveEdges )
711 CImage3dCEA image(AFilename,FirstPlane,NbPlaneToRead);
713 return extract3dImage(& image, Level,
714 DestroyBorder, KeepFictiveEdges, ShiftFictiveEdges);
718 bool CExtractionImage::extractOneRegionVoxels(CImage3d * AImage,
719 int ARed,
int AGreen,
720 int ABlue,
int AAlpha )
722 assert(AImage != NULL);
724 if (! AImage->isOk())
727 #ifdef DEBUG_EXTRACT_IMAGE
728 std::cout<<
"Début extractOneRegionVoxels:"<<std::flush;
731 int toKeep = FMap->getNewMark();
734 FMap->negateMaskMark(toKeep);
736 #ifdef DEBUG_EXTRACT_IMAGE
737 cout<<
"\nAvant de créer le bord supérieur : "<<endl;
740 CDart * last = createBorder3d(AImage->getWidth(),AImage->getHeight());
742 #ifdef DEBUG_EXTRACT_IMAGE
743 std::cout<<
"Ok on a créé le bord supérieur."<<std::endl;
747 #ifdef DEBUG_EXTRACT_IMAGE
748 cout<<
"Ok on FMap->alpha créé le bord supérieur."<<endl;
753 unsigned int nbpoints = 0;
762 CVertex CurrentPointel(1,1,1);
766 for (
unsigned int j=0;j<=AImage->getHeight();++j)
768 for (
unsigned int i=0;i<=AImage->getWidth();++i)
770 #ifdef DEBUG_EXTRACT_IMAGE
771 cout<<
"On traite le voxel ("<<i<<
", "<<j<<
", "<<k<<
")"<<endl;
774 tmp = createCubeForExtract3d(CurrentPointel);
778 if ( i<AImage->getWidth() && j<AImage->getHeight() &&
779 !AImage->lastPlane() &&
780 AImage->sameVoxel(i,j,
true,ARed, AGreen, ABlue, AAlpha) )
782 FMap->markOrbit(tmp, ORBIT_VOLUME, toKeep);
788 leftFace = FMap->alpha0(last);
789 upFace = FMap->alpha3012(last);
790 behindFace = FMap->alpha3201(last);
793 if (AImage->sameVoxelActuLeft(i,j))
795 mergeImage3dVolumes(leftFace);
799 if (AImage->sameVoxelActuBehind(i,j))
801 mergeImage3dVolumes(behindFace);
805 if (AImage->sameVoxelActuUp(i,j))
807 mergeImage3dVolumes(upFace);
813 last = FMap->alpha101(tmp);
820 if ( i == AImage->getWidth()-1 )
822 CurrentPointel.setX(0);
824 if ( j == AImage->getHeight()-1 )
826 CurrentPointel.setY(0);
827 CurrentPointel.setZ(CurrentPointel.getZ()+1);
830 CurrentPointel.setY(CurrentPointel.getY()+1);
833 CurrentPointel.setX(CurrentPointel.getX()+1);
837 #ifdef DEBUG_EXTRACT_IMAGE
838 std::cout <<
"Ok pour l'extraction de la plaque num "
839 << AImage->getPlaneActu() << std::endl;
844 while(AImage->readNextPlane());
847 CStaticCoverage012 it(FMap,last);
849 FMap->delMapDart(it++);
851 #ifdef DEBUG_EXTRACT_IMAGE
852 std::cout <<
"Ok pour la destruction du bord." << std::endl;
857 CDynamicCoverageAll itAll(FMap);
858 while( itAll.cont() )
861 if ( !FMap->isMarked(tmp, toKeep) )
863 if ( !FMap->isFree3(tmp) )
865 if ( FMap->isMarked(FMap->alpha3(tmp), toKeep) )
873 #ifdef DEBUG_EXTRACT_IMAGE
874 std::cout <<
"Ok pour la décousure par alpha3." << std::endl;
878 while( itAll.cont() )
882 if ( !FMap->isMarked(tmp, toKeep) )
883 FMap->delMapDart(tmp);
886 FMap->negateMaskMark(toKeep);
888 assert( FMap->isWholeMapUnmarked(toKeep) );
890 FMap->freeMark(toKeep);
893 #ifdef DEBUG_EXTRACT_IMAGE
894 std::cout <<
"Ok pour l'extraction d'une région : terminée. ";
895 std::cout<<nbpoints<<
" voxels."<<std::endl;
902 bool CExtractionImage::extractOneRegionVoxels(
const std::string & AFilename,
906 int ARed,
int AGreen,
907 int ABlue,
int AAlpha )
909 #ifdef MODULE_EXTRACTION_IMAGE_WITHOUT_MAGICK
912 CImage3dMagick image(AFilename,FirstPlane,NbPlaneToRead,Lg);
913 return extractOneRegionVoxels(& image, ARed, AGreen, ABlue, AAlpha);
914 #endif // MODULE_EXTRACTION_IMAGE_WITHOUT_MAGICK