00001 #include "vtkDijkstraImageData.h"
00002
00003 #include "vtkObjectFactory.h"
00004 #include "vtkIntArray.h"
00005 #include "vtkPointData.h"
00006 #include "vtkPriorityQueue.h"
00007 #include "vtkIdList.h"
00008 #include "vtkImageData.h"
00009 #include "vtkFloatArray.h"
00010 #include <math.h>
00011 #include <stdlib.h>
00012
00013 vtkCxxSetObjectMacro(vtkDijkstraImageData,BoundaryScalars,vtkDataArray);
00014
00015
00016 vtkDijkstraImageData* vtkDijkstraImageData::New()
00017 {
00018
00019
00020
00021
00022
00023 vtkObject* ret = vtkObjectFactory::CreateInstance("vtkDijkstraImageData");
00024 if(ret)
00025 {
00026 return (vtkDijkstraImageData*)ret;
00027 }
00028
00029 return new vtkDijkstraImageData;
00030 }
00031
00032
00033
00034
00035
00036
00037 vtkDijkstraImageData::vtkDijkstraImageData()
00038 {
00039 this->SourceID = 0;
00040 this->SinkID = 0;
00041 this->NumberOfInputPoints = 0;
00042 this->PathPointer = -1;
00043 this->StopWhenEndReached = 1;
00044
00045 this->ShortestPathIdList = NULL;
00046 this->Parent = NULL;
00047 this->Visited = NULL;
00048 this->PQ = NULL;
00049 this->BoundaryScalars = NULL;
00050
00051 this->UseInverseDistance = 0;
00052 this->UseInverseSquaredDistance = 0;
00053 this->UseInverseExponentialDistance = 1;
00054 this->UseSquaredDistance = 0;
00055
00056 this->puntos = NULL;
00057 this->lineas = NULL;
00058
00059
00060 this->logger = NULL;
00061
00062
00063
00064
00065 }
00066
00067
00068
00069 vtkDijkstraImageData::~vtkDijkstraImageData()
00070 {
00071
00072
00073 if (this->ShortestPathIdList)
00074 this->ShortestPathIdList->Delete();
00075 if (this->Parent)
00076 this->Parent->Delete();
00077 if(this->BoundaryScalars)
00078 this->BoundaryScalars->Delete();
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 }
00094
00095
00096 unsigned long vtkDijkstraImageData::GetMTime()
00097 {
00098 unsigned long mTime=this->MTime.GetMTime();
00099
00100 return mTime;
00101 }
00102
00103
00104 void vtkDijkstraImageData::SetInput(vtkImageData* data){
00105 this->vtkProcessObject::SetNthInput(0, data);
00106 }
00107
00108 vtkImageData* vtkDijkstraImageData::GetInput(){
00109 return (vtkImageData *)(this->Inputs[0]);
00110 }
00111
00112
00113 void vtkDijkstraImageData::init(vtkImageData *inData)
00114 {
00115
00116 if (this->ShortestPathIdList)
00117 this->ShortestPathIdList->Delete();
00118
00119 if (this->Parent)
00120 this->Parent->Delete();
00121 if (this->Visited)
00122 this->Visited->Delete();
00123 if (this->PQ)
00124 this->PQ->Delete();
00125
00126 this->ShortestPathIdList = vtkIdList::New();
00127 this->Parent = vtkIntArray::New();
00128 this->Visited = vtkIntArray::New();
00129 this->PQ = vtkPriorityQueue::New();
00130
00131
00132 CreateGraph(inData);
00133
00134
00135 int numPoints = inData->GetNumberOfPoints();
00136
00137 this->Parent->SetNumberOfComponents(1);
00138 this->Parent->SetNumberOfTuples(numPoints);
00139 this->Visited->SetNumberOfComponents(1);
00140 this->Visited->SetNumberOfTuples(numPoints);
00141
00142 }
00143
00144 void vtkDijkstraImageData::DeleteGraph()
00145 {
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 }
00161
00162
00163 void vtkDijkstraImageData::CreateGraph(vtkImageData *inData) {
00164
00165
00166
00167
00168 int numPoints = inData->GetNumberOfPoints();
00169 this->SetNumberOfInputPoints(numPoints);
00170
00171
00172 int *dim = inData->GetDimensions();
00173 vtkDataArray *scalars = inData->GetPointData()->GetScalars();
00174 vtkIdList *graphNodes = vtkIdList::New();
00175 int graphSize = 0;
00176
00177 for(int k = 0; k <dim[2]; k++) {
00178 this->UpdateProgress ((float) k / (2 * ((float) dim[2] - 1)));
00179 for(int j = 0; j <dim[1]; j++) {
00180 for(int i = 0; i <dim[0]; i++) {
00181
00182 int id = k*(dim[1]*dim[0]) + j*dim[0] + i;
00183 float maskValue = scalars->GetTuple1(id);
00184
00185
00186 if(maskValue > 0) {
00187
00188 graphNodes->InsertNextId(id);
00189 graphSize++;
00190 }
00191 }
00192 }
00193 }
00194
00195 this->SetNumberOfGraphNodes(graphSize);
00196
00197
00198
00199 PQ->Allocate(graphSize);
00200 for(int i=0; i<graphSize;i++) {
00201 PQ->Insert(VTK_LARGE_FLOAT,graphNodes->GetId(i));
00202 }
00203
00204 graphNodes->Delete();
00205
00206 }
00207
00208
00209
00210 void vtkDijkstraImageData::RunDijkstra(vtkDataArray *scalars,int startv, int endv)
00211 {
00212
00213 int i, u, v;
00214
00215 printf("tipo de ponderacion de pesos : linear %i, squared %i, exponential %i\n",this->UseInverseDistance,this->UseInverseSquaredDistance,this->UseInverseExponentialDistance);
00216
00217
00218
00219
00220
00221
00222
00223
00224 InitSingleSource(startv);
00225
00226
00227 this->Visited->SetValue(startv, 1);
00228
00229
00230 int initialSize = PQ->GetNumberOfItems();
00231 int size = initialSize;
00232 int stop = 0;
00233
00234
00235
00236
00237
00238
00239
00240 while ((PQ->GetNumberOfItems() > 0) && !stop)
00241 {
00242
00243 this->UpdateProgress (0.5 + (float) (initialSize - size) / ((float) 2 * initialSize));
00244
00245
00246 vtkFloatingPointType u_weight;
00247
00248
00249
00250
00251
00252
00253 #if (VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION == 0)
00254 u = PQ->Pop(u_weight,0);
00255 #else
00256 u = PQ->Pop(0, u_weight);
00257 #endif
00258
00259
00260
00261
00262
00263
00264
00265 this->Visited->SetValue(u, 1);
00266
00267
00268
00269
00270
00271
00272 if (u == endv && StopWhenEndReached)
00273 stop = 1;
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283 vtkIdList *list = vtkIdList::New();
00284 this->FindNeighbors(list,u,scalars);
00285
00286
00287
00288
00289
00290
00291 for (i = 0; i < list->GetNumberOfIds(); i++)
00292 {
00293
00294
00295
00296
00297 v = list->GetId(i);
00298
00299
00300
00301
00302
00303
00304
00305
00306 if (this->Visited->GetValue(v) != 1)
00307 {
00308
00309
00310
00311
00312
00313
00314 float w = EdgeCost(scalars, u, v);
00315
00316
00317
00318
00319 float v_weight = this->PQ->GetPriority(v);
00320 if(v == endv) {
00321
00322
00323
00324 }
00325
00326
00327
00328
00329
00330
00331
00332
00333 if (v_weight > (u_weight + w))
00334 {
00335
00336 this->PQ->DeleteId(v);
00337
00338
00339 this->PQ->Insert((u_weight + w),v);
00340
00341
00342
00343
00344
00345 this->Parent->SetValue(v, u);
00346
00347 }
00348 }
00349 else{
00350
00351 }
00352 }
00353
00354
00355
00356
00357 list->Delete();
00358 size--;
00359 }
00360 this->PQ->Delete();
00361 this->Visited->Delete();
00362 }
00363
00364
00365 void vtkDijkstraImageData::InitSingleSource(int startv)
00366 {
00367 for (int v = 0; v < this->GetNumberOfInputPoints(); v++)
00368 {
00369 this->Parent->SetValue(v, -1);
00370 this->Visited->SetValue(v, 0);
00371 }
00372 PQ->DeleteId(startv);
00373
00374 PQ->Insert(0,startv);
00375
00376 }
00377
00378
00379
00380 void vtkDijkstraImageData::FindNeighbors(vtkIdList *list,int id, vtkDataArray *scalars) {
00381
00382
00383 vtkImageData *input = this->GetInput();
00384
00385 int *dim = input->GetDimensions();
00386 int numPts = dim[0] * dim[1] * dim[2];
00387
00388
00389 for(int vk = -1; vk<2; vk++) {
00390 for(int vj = -1; vj<2; vj++) {
00391 for(int vi = -1; vi<2; vi++) {
00392
00393 int tmpID = id + (vk * dim[1]*dim[0]) + (vj * dim[0]) + vi;
00394
00395 if( tmpID >= 0 && tmpID < numPts && tmpID != 0) {
00396 float mask = scalars->GetTuple1(tmpID);
00397
00398 if(mask > 0 && tmpID != id) {
00399 list->InsertUniqueId(tmpID);
00400
00401
00402 }
00403 }
00404 }
00405 }
00406 }
00407
00408 }
00409
00410
00411
00412 float vtkDijkstraImageData::fuerzaAtraccion(int u, float w)
00413 {
00414
00415 double coordsSource[3];
00416 double coordsSink[3];
00417 double coordsPoint[3];
00418
00419 this->GetInput()->GetPoint(this->GetSourceID(), coordsSource);
00420 this->GetInput()->GetPoint(this->GetSinkID(), coordsSink);
00421
00422
00423
00424 this->GetInput()->GetPoint(u, coordsPoint);
00425
00426
00427
00428
00429 float r1 = sqrt((coordsSource[0]-coordsPoint[0])*(coordsSource[0]-coordsPoint[0]) +
00430 (coordsSource[1]-coordsPoint[1])*(coordsSource[1]-coordsPoint[1]) +
00431 (coordsSource[2]-coordsPoint[2])*(coordsSource[2]-coordsPoint[2]));
00432
00433
00434 float r2 = sqrt((coordsSink[0]-coordsPoint[0])*(coordsSink[0]-coordsPoint[0]) +
00435 (coordsSink[1]-coordsPoint[1])*(coordsSink[1]-coordsPoint[1]) +
00436 (coordsSink[2]-coordsPoint[2])*(coordsSink[2]-coordsPoint[2]));
00437
00438 float d = sqrt((coordsSink[0]-coordsSource[0])*(coordsSink[0]-coordsSource[0]) +
00439 (coordsSink[1]-coordsSource[1])*(coordsSink[1]-coordsSource[1]) +
00440 (coordsSink[2]-coordsSource[2])*(coordsSink[2]-coordsSource[2]));
00441
00442
00443
00444 if (r2 < d && r1 > d){
00445 return w/2;
00446 } else if ( r2 > d && r1 < d){
00447 return w/2;
00448 } else if (r1 < d && r2 < d){
00449 return w*w;
00450 } else return w/2;
00451
00452
00453
00454 }
00455
00456
00457
00458
00459 float vtkDijkstraImageData::EdgeCost(vtkDataArray *scalars, int u, int v)
00460 {
00461
00462 float w;
00463
00464 float dist2 = scalars->GetTuple1(v);
00465 float dist = sqrt(dist2);
00466 if(this->UseInverseDistance)
00467 w = (1.0/dist);
00468 else if(this->UseInverseSquaredDistance)
00469 w = (1.0/(dist2));
00470 else if(this->UseInverseExponentialDistance)
00471 w = (1.0/exp(dist));
00472 else if(this->UseSquaredDistance)
00473 w = dist2;
00474
00475
00476
00477 return w;
00478 }
00479
00480
00481 void vtkDijkstraImageData::BuildShortestPath(int start,int end)
00482 {
00483
00484 int p = end;
00485 int next = 0;
00486 int i = 0;
00487 double punto[3];
00488
00489 this->puntos = vtkPoints::New();
00490 this->lineas = vtkCellArray::New();
00491
00492
00493
00494
00495
00496
00497 while (p != start && p > 0)
00498 {
00499 printPointData(p);
00500
00501 this->GetInput()->GetPoint(p,punto);
00502 this->puntos->InsertPoint(i,punto);
00503
00504 next = this->Parent->GetValue(p);
00505
00506 this->GetInput()->GetPoint(next,punto);
00507 this->puntos->InsertPoint(i+1,punto);
00508
00509 this->lineas->InsertNextCell(2);
00510 this->lineas->InsertCellPoint(i);
00511 this->lineas->InsertCellPoint(i+1);
00512
00513 this->ShortestPathIdList->InsertNextId(p);
00514 p = next;
00515 i++;
00516 }
00517
00518 this->ShortestPathIdList->InsertNextId(p);
00519 printPointData(p);
00520
00521
00522
00523
00524 this->GetOutput()->SetPoints (this->puntos);
00525 this->GetOutput()->SetLines(this->lineas);
00526 this->GetOutput()->Modified();
00527
00528
00529 }
00530
00531 vtkIdList* vtkDijkstraImageData::GetShortestPathIdList()
00532 {
00533 return this->ShortestPathIdList;
00534 }
00535
00536
00537
00538 void vtkDijkstraImageData::printPointData(int pointID)
00539 {
00540 double coords[3];
00541
00542 this->GetInput()->GetPoint(pointID, coords);
00543 double scalar = this->GetInput()->GetPointData()->GetScalars()->GetTuple1(pointID);
00544
00545 printf("Punto ID: %i ",pointID);
00546 printf("Coords[%.0f, %.0f, %.0f] ", coords[0], coords[1], coords[2]);
00547 printf("Scalar: %.2f\n", scalar);
00548
00549 }
00550
00551
00552
00553
00554
00555
00556 void vtkDijkstraImageData::InitTraversePath(){
00557 this->PathPointer = -1;
00558 }
00559
00560
00561 int vtkDijkstraImageData::GetNumberOfPathNodes(){
00562 return this->ShortestPathIdList->GetNumberOfIds();
00563 }
00564
00565
00566 int vtkDijkstraImageData::GetNextPathNode(){
00567 this->PathPointer = this->PathPointer + 1;
00568
00569 if(this->PathPointer < this->GetNumberOfPathNodes()) {
00570
00571 return this->ShortestPathIdList->GetId(this->PathPointer);
00572 } else {
00573 return -1;
00574 }
00575 }
00576
00577
00578
00579 int vtkDijkstraImageData::findClosestPointInGraph(vtkDataArray *scalars,int id,int dim0,int dim1, int dim2) {
00580
00581
00582 int kFactor = dim0 * dim1;
00583 int jFactor = dim0;
00584
00585 int numPoints = kFactor * dim2;
00586 vtkIdList* Q = vtkIdList::New();
00587 Q->InsertNextId(id);
00588
00589 int pointer = 0;
00590 int foundID = -1;
00591
00592 while(Q->GetNumberOfIds() != 0) {
00593
00594 int current = Q->GetId(pointer);
00595 pointer = pointer + 1;
00596
00597
00598
00599 if(scalars->GetTuple1(current) >0) {
00600
00601 return current;
00602 } else {
00603
00604 scalars->SetTuple1(current,-1);
00605
00606
00607 if (current + kFactor <numPoints) {
00608 if(scalars->GetTuple1(current + kFactor) != -1){
00609
00610 Q->InsertNextId(current+kFactor);
00611 }
00612 }
00613
00614 if (current - kFactor >= 0){
00615 if(scalars->GetTuple1(current - kFactor) != -1){
00616
00617 Q->InsertNextId(current-kFactor);
00618 }
00619 }
00620
00621 if (current + jFactor < numPoints) {
00622 if(scalars->GetTuple1(current + jFactor) != -1){
00623
00624 Q->InsertNextId(current + jFactor);
00625 }
00626 }
00627
00628 if (current - jFactor >= 0) {
00629 if(scalars->GetTuple1(current - jFactor) != -1){
00630
00631 Q->InsertNextId(current - jFactor);
00632 }
00633 }
00634
00635 if (current+1 <numPoints){
00636 if(scalars->GetTuple1(current + 1) != -1){
00637
00638 Q->InsertNextId(current + 1);
00639 }
00640 }
00641
00642 if (current -1 >= 0) {
00643 if(scalars->GetTuple1(current - 1) != -1){
00644
00645 Q->InsertNextId(current - 1);
00646 }
00647 }
00648 }
00649 }
00650 Q->Delete();
00651 return foundID;
00652 }
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662 void vtkDijkstraImageData::Execute()
00663 {
00664
00665 vtkImageData *inData = this->GetInput();
00666 vtkPolyData *outData = this->GetOutput();
00667
00668
00669
00670
00671
00672 if (inData->GetNumberOfScalarComponents() > 3)
00673 {
00674 vtkErrorMacro("This filter can handle upto 3 components");
00675 return;
00676 }
00677
00678
00679
00680 this->init(inData);
00681
00682
00683 int *dim = inData->GetDimensions();
00684 vtkDataArray *scalars = inData->GetPointData()->GetScalars();
00685
00686
00687 printf("source ID %i value is %f\n",this->GetSourceID(),scalars->GetTuple1(this->GetSourceID()));
00688 if(scalars->GetTuple1(this->GetSourceID()) == 0) {
00689
00690 vtkFloatArray *copyScalars = vtkFloatArray::New();
00691 copyScalars->DeepCopy(inData->GetPointData()->GetScalars());
00692
00693 this->SetSourceID(this->findClosestPointInGraph(copyScalars,this->GetSourceID(),dim[0],dim[1],dim[2]));
00694
00695 copyScalars->Delete();
00696 printf("NEW source ID %i value is %f\n",this->GetSourceID(),scalars->GetTuple1(this->GetSourceID()));
00697
00698 }
00699
00700 printf("sink ID %i value is %f\n",this->GetSinkID(),scalars->GetTuple1(this->GetSinkID()));
00701 if(scalars->GetTuple1(this->GetSinkID()) == 0) {
00702 vtkFloatArray *copyScalars2 = vtkFloatArray::New();
00703 copyScalars2->DeepCopy(inData->GetPointData()->GetScalars());
00704 this->SetSinkID(this->findClosestPointInGraph(copyScalars2,this->GetSinkID(),dim[0],dim[1],dim[2]));
00705 copyScalars2->Delete();
00706
00707 printf("NEW sink ID %i value is %f\n",this->GetSinkID(),scalars->GetTuple1(this->GetSinkID()));
00708 }
00709
00710
00711 this->RunDijkstra(scalars,this->GetSourceID(),this->GetSinkID());
00712
00713 this->BuildShortestPath(this->GetSourceID(),this->GetSinkID());
00714
00715
00716
00717
00718 }
00719
00720
00721
00722 void vtkDijkstraImageData::PrintSelf(ostream& os, vtkIndent indent)
00723 {
00724 Superclass::PrintSelf(os,indent);
00725
00726 os << indent << "Source ID: ( "
00727 << this->GetSourceID() << " )\n";
00728
00729 os << indent << "Sink ID: ( "
00730 << this->GetSinkID() << " )\n";
00731 }
00732
00733
00734