vtkGenericEdgeTable.cxx 22.8 KB
Newer Older
Will Schroeder's avatar
Will Schroeder committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkGenericEdgeTable.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkGenericEdgeTable.h"
#include "vtkObjectFactory.h"

18
#include <vector>
19
#include <cassert>
Will Schroeder's avatar
Will Schroeder committed
20 21 22

vtkStandardNewMacro(vtkGenericEdgeTable);

23 24 25
static int PRIME_NUMBERS[] = {1, 3, 7, 13, 31, 61, 127,  251,  509,  1021,
                              2039, 4093};

26 27 28 29 30 31 32
// Description:
// Constructor with a scalar field of `size' doubles.
// \pre positive_number_of_components: size>0
vtkGenericEdgeTable::PointEntry::PointEntry(int size)
{
  assert("pre: positive_number_of_components" && size>0);
  this->Reference = -10;
33

34 35 36
  this->Coord[0]  = -100;
  this->Coord[1]  = -100;
  this->Coord[2]  = -100;
37 38
  this->Scalar = new double[size];
  this->numberOfComponents = size;
39 40
}

Will Schroeder's avatar
Will Schroeder committed
41 42 43
class vtkEdgeTablePoints
{
public:
44 45
  typedef std::vector<vtkGenericEdgeTable::PointEntry> VectorPointTableType;
  typedef std::vector<VectorPointTableType> PointTableType;
Will Schroeder's avatar
Will Schroeder committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

  void Resize(vtkIdType size);
  void LoadFactor();
  void DumpPoints();

  PointTableType PointVector;
  vtkIdType Modulo;
};

//-----------------------------------------------------------------------------
void vtkEdgeTablePoints::Resize(vtkIdType newSize)
{
  vtkIdType size = PointVector.size();

  if( size <= newSize )
    {
    PointVector.resize(newSize);
63
    int index = static_cast<int>(log(static_cast<double>(newSize))/log(2.));
Will Schroeder's avatar
Will Schroeder committed
64
    this->Modulo = PRIME_NUMBERS[index];
65 66
    //cout << "this->Modulo:" << newSize << "," << index << ","
    //     << this->Modulo << endl;
Will Schroeder's avatar
Will Schroeder committed
67 68
    }

69
  assert(static_cast<unsigned>(size) < PointVector.size() ); // valid size
70 71
  // For now you are not supposed to use this method
  assert( 0 );
Will Schroeder's avatar
Will Schroeder committed
72
}
73

Will Schroeder's avatar
Will Schroeder committed
74 75 76 77 78 79 80
//-----------------------------------------------------------------------------
void vtkEdgeTablePoints::LoadFactor()
{
  vtkIdType numEntries = 0;
  vtkIdType numBins = 0;

  vtkIdType size = PointVector.size();
81
  cerr << "EdgeTablePoints:\n";
Will Schroeder's avatar
Will Schroeder committed
82 83 84 85
  for(int i=0; i<size; i++)
    {
    numEntries += PointVector[i].size();
    if( PointVector[i].size() ) numBins++;
86
    cerr << PointVector[i].size() << ",";
Will Schroeder's avatar
Will Schroeder committed
87
    }
88 89
  cerr << "\n";
  cout << size << "," << numEntries << "," << numBins << "," << Modulo
Will Schroeder's avatar
Will Schroeder committed
90 91 92 93 94 95 96 97 98 99
            << "\n";
}

//-----------------------------------------------------------------------------
void vtkEdgeTablePoints::DumpPoints()
{
  vtkIdType size = PointVector.size();
  for(int i=0; i<size; i++)
    {
    VectorPointTableType v = PointVector[i];
100
    for (VectorPointTableType::iterator it = v.begin(); it!=v.end(); ++it)
Will Schroeder's avatar
Will Schroeder committed
101 102 103 104 105 106 107 108 109 110 111 112 113
      {
      //PointEntry
      cout << "PointEntry: " << it->PointId << " " << it->Reference << ":(" <<
      it->Coord[0] << "," << it->Coord[1] << "," << it->Coord[2] << ")"
           << endl;
      }
    }
}

//-----------------------------------------------------------------------------
class vtkEdgeTableEdge
{
public:
114 115
  typedef std::vector<vtkGenericEdgeTable::EdgeEntry> VectorEdgeTableType;
  typedef std::vector<VectorEdgeTableType> EdgeTableType;
Will Schroeder's avatar
Will Schroeder committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

  void Resize(vtkIdType size);
  void LoadFactor();
  void DumpEdges();

  EdgeTableType Vector;
  vtkIdType Modulo;
};

//-----------------------------------------------------------------------------
void vtkEdgeTableEdge::Resize(vtkIdType newSize)
{
  vtkIdType size = Vector.size();

  if( size <= newSize )
    {
    Vector.resize(newSize);
133
    int index = static_cast<int>(log(static_cast<double>(newSize))/log(2.));
Will Schroeder's avatar
Will Schroeder committed
134 135 136 137
    this->Modulo = PRIME_NUMBERS[index];
    cout << "this->Modulo:" << index << ":" << this->Modulo << endl;
    }

138 139
  // For now you are not supposed to use this method
  assert(0);
Will Schroeder's avatar
Will Schroeder committed
140 141 142 143 144 145 146 147 148
}

//-----------------------------------------------------------------------------
void vtkEdgeTableEdge::LoadFactor()
{
  vtkIdType numEntry = 0;
  vtkIdType numBins = 0;

  vtkIdType size = Vector.size();
149
  cerr << "EdgeTableEdge:\n";
Will Schroeder's avatar
Will Schroeder committed
150 151 152 153 154 155
  for(int i=0; i<size; i++)
    {
    VectorEdgeTableType v = Vector[i];
    numEntry += v.size();
    if(v.size()) numBins++;
    }
156 157
  cerr << "\n";
  cerr << size << "," << numEntry << "," << numBins << "," << Modulo
Will Schroeder's avatar
Will Schroeder committed
158 159 160 161 162 163 164 165 166 167
            << "\n";
}

//-----------------------------------------------------------------------------
void vtkEdgeTableEdge::DumpEdges()
{
  vtkIdType size = Vector.size();
  for(int i=0; i<size; i++)
    {
    VectorEdgeTableType v = Vector[i];
168
    for (VectorEdgeTableType::iterator it = v.begin(); it!=v.end(); ++it)
Will Schroeder's avatar
Will Schroeder committed
169 170
      {
      //EdgeEntry
171
      cout << "EdgeEntry: (" << it->E1 << "," << it->E2 << ") " <<
Will Schroeder's avatar
Will Schroeder committed
172 173 174 175 176 177
      it->Reference << "," << it->ToSplit << "," << it->PtId <<  endl;
      }
    }
}

//-----------------------------------------------------------------------------
178
static inline void OrderEdge(vtkIdType &e1, vtkIdType &e2)
Will Schroeder's avatar
Will Schroeder committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192
{
  vtkIdType temp1 = e1;
  vtkIdType temp2 = e2;
  e1 = temp1 < temp2 ? temp1 : temp2;
  e2 = temp1 > temp2 ? temp1 : temp2;
}

//-----------------------------------------------------------------------------
// Instantiate object based on maximum point id.
vtkGenericEdgeTable::vtkGenericEdgeTable()
{
  this->EdgeTable = new vtkEdgeTableEdge;
  this->HashPoints = new vtkEdgeTablePoints;

193 194
  // Default to only one component
  this->NumberOfComponents = 1;
195

196 197
  // The whole problem is here to find the proper size for a descent hash table
  // Since we do not allow check our size as we go the hash table
Bill Lorensen's avatar
Bill Lorensen committed
198
  // Should be big enough from the beginning otherwise we'll lose the
199
  // constant time access
200 201
  // But on the other hand we do not want it to be too big for mem consumption
  // A compromise of 4093 was found fo be working in a lot of case
Will Schroeder's avatar
Will Schroeder committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
#if 1
  this->EdgeTable->Vector.resize(4093);
  this->EdgeTable->Modulo = 4093;
  this->HashPoints->PointVector.resize(4093);  //127
  this->HashPoints->Modulo = 4093;
#else
  this->EdgeTable->Vector.resize(509);
  this->EdgeTable->Modulo = 509;
  this->HashPoints->PointVector.resize(127);
  this->HashPoints->Modulo = 127;
#endif

  this->LastPointId = 0;
}

//-----------------------------------------------------------------------------
vtkGenericEdgeTable::~vtkGenericEdgeTable()
{
  delete this->EdgeTable;
  delete this->HashPoints;
}

//-----------------------------------------------------------------------------
// We assume the edge is not being split:
void vtkGenericEdgeTable::InsertEdge(vtkIdType e1, vtkIdType e2,
                                     vtkIdType cellId, int ref )
{
  vtkIdType ptId;
  this->InsertEdge(e1, e2, cellId, ref, 0, ptId);
}

//-----------------------------------------------------------------------------
// the edge is being split and we want the new ptId
void vtkGenericEdgeTable::InsertEdge(vtkIdType e1, vtkIdType e2,
                                     vtkIdType cellId, int ref,
                                     vtkIdType &ptId )
{
  this->InsertEdge(e1, e2, cellId, ref, 1, ptId );
}

//-----------------------------------------------------------------------------
void vtkGenericEdgeTable::InsertEdge(vtkIdType e1, vtkIdType e2,
                                     vtkIdType cellId, int ref, int toSplit,
                                     vtkIdType &ptId )
{
  if( e1 == e2 )
    {
    vtkErrorMacro( << "Not an edge:" << e1 << "," << e2 );
    }
  assert("pre: not degenerated edge" && e1!=e2);
252

Will Schroeder's avatar
Will Schroeder committed
253 254 255 256 257
  //reorder so that e1 < e2;
  OrderEdge(e1, e2);

  vtkIdType pos = this->HashFunction(e1, e2);

Kyle Lutz's avatar
Kyle Lutz committed
258
  //Be careful with reference the equal is not overloaded
Will Schroeder's avatar
Will Schroeder committed
259
  vtkEdgeTableEdge::VectorEdgeTableType &vect = this->EdgeTable->Vector[pos];
260

Will Schroeder's avatar
Will Schroeder committed
261
  //Need to check size again
262
  //Here we just puch_back at the end,
Will Schroeder's avatar
Will Schroeder committed
263
  //the vector should not be very long and should not contain any empty spot.
264
  EdgeEntry newEntry;
Will Schroeder's avatar
Will Schroeder committed
265 266 267 268 269 270
  newEntry.E1 = e1;
  newEntry.E2 = e2;
  newEntry.Reference = ref;
  newEntry.ToSplit = toSplit;
  newEntry.CellId = cellId;

271
  if(newEntry.ToSplit)
Will Schroeder's avatar
Will Schroeder committed
272 273 274 275 276 277 278 279
    {
    newEntry.PtId = ptId = this->LastPointId++;
    }
  else
    {
    newEntry.PtId = ptId = -1;
    }

280 281
//  vtkDebugMacro( << "Inserting Edge:" << e1 << "," << e2 << ": ref="  <<
//    newEntry.Reference << ", cellId=" << cellId << ", split=" << toSplit <<
282
//    ", ptId=" << newEntry.PtId );
Will Schroeder's avatar
Will Schroeder committed
283 284 285 286 287 288 289 290 291 292 293 294

  vect.push_back( newEntry );
}

//-----------------------------------------------------------------------------
// Try to remove an edge, in fact decrement the ref count
int vtkGenericEdgeTable::RemoveEdge(vtkIdType e1, vtkIdType e2)
{
  int ref = 0;
  //reorder so that e1 < e2;
  OrderEdge(e1, e2);

295
  //vtkDebugMacro( << "RemoveEdge:" << e1 << "," << e2 );
Will Schroeder's avatar
Will Schroeder committed
296 297

  vtkIdType pos = this->HashFunction(e1, e2);
298 299
  assert("check: valid range po" &&
         static_cast<unsigned>(pos) < this->EdgeTable->Vector.size() );
Will Schroeder's avatar
Will Schroeder committed
300 301 302

  //Need to check size first
  vtkEdgeTableEdge::VectorEdgeTableType &vect = this->EdgeTable->Vector[pos];
303

304
  int found = 0;
305

Will Schroeder's avatar
Will Schroeder committed
306
  vtkEdgeTableEdge::VectorEdgeTableType::iterator it;
307
  for(it = vect.begin(); it != vect.end(); )
Will Schroeder's avatar
Will Schroeder committed
308 309 310 311 312
  {
    if( it->E1 == e1 && it->E2 == e2 )
      {
      if( --it->Reference == 0 )
        {
313
        // Ok this edge is about to be physically removed, remove also the point
Will Schroeder's avatar
Will Schroeder committed
314 315 316 317 318 319 320 321
        // is contained, if one:
        if( it->ToSplit )
          {
          assert("check: positive id" && it->PtId >= 0 );

          this->RemovePoint( it->PtId );
          }
        }
322
      found = 1;
Will Schroeder's avatar
Will Schroeder committed
323 324 325
      ref = it->Reference;
      }

326 327 328
    if( it->E1 == e1 && it->E2 == e2 && it->Reference == 0 )
      {
      it = vect.erase( it );
Will Schroeder's avatar
Will Schroeder committed
329
      }
330 331 332 333 334
    else
      {
      ++it;
      }
  }
Will Schroeder's avatar
Will Schroeder committed
335 336 337 338

  if( !found )
    {
    //We did not find any corresponding entry to erase, warn user
339
    vtkErrorMacro( << "No entry were found in the hash table for edge:" << e1
Will Schroeder's avatar
Will Schroeder committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
      << "," << e2  );
    assert("check: not found" &&  0 );
    }

  return ref;
}

//-----------------------------------------------------------------------------
int vtkGenericEdgeTable::CheckEdge(vtkIdType e1, vtkIdType e2, vtkIdType &ptId)
{
  //int index;
  EdgeEntry ent;
  //reorder so that e1 < e2;
  OrderEdge(e1, e2);

  //vtkDebugMacro( << "Checking edge:" << e1 << "," << e2  );

  vtkIdType pos = this->HashFunction(e1, e2);
358

359
  if( static_cast<unsigned>(pos) >= this->EdgeTable->Vector.size() )
Will Schroeder's avatar
Will Schroeder committed
360 361 362 363
    {
    vtkDebugMacro( << "No entry were found in the hash table" );
    return -1;
    }
364

365 366
  assert("check: valid range pos" &&
         static_cast<unsigned>(pos)<this->EdgeTable->Vector.size() );
Will Schroeder's avatar
Will Schroeder committed
367 368
  //Need to check size first
  vtkEdgeTableEdge::VectorEdgeTableType &vect = this->EdgeTable->Vector[pos];
369

370
#if defined(USE_CONST_ITERATOR)
Will Schroeder's avatar
Will Schroeder committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
  vtkEdgeTableEdge::VectorEdgeTableType::const_iterator it;
  for(it = vect.begin(); it != vect.end(); ++it)
    {
    if( (it->E1 == e1) && (it->E2 == e2))
      {
      ptId = it->PtId;
      break;
      }
    }

  if( it == vect.end())
    {
    //We did not find any corresponding entry, warn user
    vtkDebugMacro( << "No entry were found in the hash table" );
    return -1;
    }

  return it->ToSplit;
#else
390
  int vectsize = static_cast<int>(vect.size());
Will Schroeder's avatar
Will Schroeder committed
391
  int index;
392
  for (index=0; index<vectsize; index++)
Will Schroeder's avatar
Will Schroeder committed
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
    {
    ent = vect[index];
    if( ent.E1 == e1 && ent.E2 == e2 )
      {
      ptId = ent.PtId;
      break;
      }
    }

  if( index == vectsize )
    {
    //We did not find any corresponding entry, warn user
    vtkDebugMacro( << "No entry were found in the hash table" );
    return -1;
    }

  return ent.ToSplit;
#endif

}

//-----------------------------------------------------------------------------
415
int vtkGenericEdgeTable::IncrementEdgeReferenceCount(vtkIdType e1,
416
                                                     vtkIdType e2,
417
                                                     vtkIdType cellId )
Will Schroeder's avatar
Will Schroeder committed
418 419 420 421 422 423 424 425
{
  int index;
  //reorder so that e1 < e2;
  OrderEdge(e1, e2);

  //vtkDebugMacro( << "IncrementEdgeReferenceCount:" << e1 << "," << e2  );

  vtkIdType pos = this->HashFunction(e1, e2);
426 427
  assert("check: valid range pos" &&
         static_cast<unsigned>(pos) < this->EdgeTable->Vector.size());
Will Schroeder's avatar
Will Schroeder committed
428 429 430

  //Need to check size first
  vtkEdgeTableEdge::VectorEdgeTableType &vect = this->EdgeTable->Vector[pos];
431

432
  int vectsize = static_cast<int>(vect.size());
433
  for (index=0; index<vectsize; index++)
Will Schroeder's avatar
Will Schroeder committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
    {
    EdgeEntry &ent = vect[index];
    if( (ent.E1 == e1) && (ent.E2 == e2))
      {
      if( ent.CellId == cellId )
        {
        ent.Reference++;
        //vtkDebugMacro( << "Incrementing: (" << e1 << "," << e2 << ") " << ent.Reference );
        }
      else
        {
        // If cellIds are different it means we pass from one cell to another
        // therefore the first time we should not increment the ref count
        // as it has already been taken into account.
        ent.CellId = cellId;
        }
      return -1;
      }
    }

    //We did not find any corresponding entry, warn user
    vtkErrorMacro( << "No entry were found in the hash table" );
    return -1;
}

//-----------------------------------------------------------------------------
int vtkGenericEdgeTable::CheckEdgeReferenceCount(vtkIdType e1, vtkIdType e2)
{
  int index;
  OrderEdge(e1, e2);   //reorder so that e1 < e2;

  //vtkDebugMacro( << "CheckEdgeReferenceCount:" << e1 << "," << e2  );

  vtkIdType pos = this->HashFunction(e1, e2);
468 469
  assert("check: valid range pos" &&
         static_cast<unsigned>(pos) < this->EdgeTable->Vector.size());
Will Schroeder's avatar
Will Schroeder committed
470 471 472

  //Need to check size first
  vtkEdgeTableEdge::VectorEdgeTableType &vect = this->EdgeTable->Vector[pos];
473 474

  int vectsize = static_cast<int>(vect.size());
475
  for (index=0; index<vectsize; index++)
Will Schroeder's avatar
Will Schroeder committed
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
    {
    EdgeEntry &ent = vect[index];
    if( (ent.E1 == e1) && (ent.E2 == e2))
      {
      assert("check: positive reference" &&  ent.Reference >= 0 );
      return ent.Reference;
      }
    }

  //We did not find any corresponding entry, warn user
  vtkErrorMacro( << "No entry were found in the hash table" );
  return -1;
}

//-----------------------------------------------------------------------------
vtkIdType vtkGenericEdgeTable::HashFunction(vtkIdType e1, vtkIdType e2)
{
493
  return (e1 + e2) % this->EdgeTable->Modulo;
Will Schroeder's avatar
Will Schroeder committed
494 495 496 497 498 499 500 501 502 503 504 505 506 507
}

//-----------------------------------------------------------------------------
void vtkGenericEdgeTable::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
}

//-----------------------------------------------------------------------------
void vtkGenericEdgeTable::Initialize(vtkIdType start)
{
  if(this->LastPointId)
    {
    //if different from zero then raise problem:
508
    vtkDebugMacro( << "You are not supposed to initialize during algorithm" );
Will Schroeder's avatar
Will Schroeder committed
509 510 511 512 513 514
    return;
    }

  this->LastPointId = start;
}

515
//-----------------------------------------------------------------------------
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
// Description:
// Return the total number of components for the point-centered attributes.
// \post positive_result: result>0
int vtkGenericEdgeTable::GetNumberOfComponents()
{
  return this->NumberOfComponents;
}

//-----------------------------------------------------------------------------
// Description:
// Set the total number of components for the point-centered attributes.
void vtkGenericEdgeTable::SetNumberOfComponents(int count)
{
  assert("pre: positive_count" && count>0);
  this->NumberOfComponents=count;
}

Will Schroeder's avatar
Will Schroeder committed
533 534 535 536 537 538 539 540 541 542 543 544
//-----------------------------------------------------------------------------
vtkIdType vtkGenericEdgeTable::HashFunction(vtkIdType ptId)
{
  return ptId % this->HashPoints->Modulo;
}
//-----------------------------------------------------------------------------
// Check if point ptId exist in the hash table
int vtkGenericEdgeTable::CheckPoint(vtkIdType ptId)
{
  int index;
  vtkIdType pos = this->HashFunction(ptId);

545
  if( static_cast<unsigned>(pos) >= this->HashPoints->PointVector.size() )
Will Schroeder's avatar
Will Schroeder committed
546 547 548
    {
    return 0;
    }
549

Will Schroeder's avatar
Will Schroeder committed
550
  assert("check: valid range pos" &&
551
         static_cast<unsigned>(pos)<this->HashPoints->PointVector.size() );
552

Kyle Lutz's avatar
Kyle Lutz committed
553
  //Be careful with reference the equal is not overloaded
554
  vtkEdgeTablePoints::VectorPointTableType &vect =
Will Schroeder's avatar
Will Schroeder committed
555 556
    this->HashPoints->PointVector[pos];

557
  int vectsize = static_cast<int>(vect.size());
558
  for (index=0; index<vectsize; index++)
Will Schroeder's avatar
Will Schroeder committed
559 560 561 562 563 564 565 566 567 568 569 570
    {
    if( vect[index].PointId == ptId )
      {
      return 1;
      }
    }

  if( index == vectsize )
    {
    //We did not find any corresponding entry
    return 0;
    }
571

Will Schroeder's avatar
Will Schroeder committed
572 573 574 575 576 577 578 579
  vtkErrorMacro( << "Error, impossible case" );
  return -1;
}

//-----------------------------------------------------------------------------
// Find point coordinate and scalar associated of point ptId
//
int vtkGenericEdgeTable::CheckPoint(vtkIdType ptId, double point[3],
580
                                    double *scalar)
Will Schroeder's avatar
Will Schroeder committed
581
{
582
  // pre: sizeof(scalar)==GetNumberOfComponents()
Will Schroeder's avatar
Will Schroeder committed
583 584 585
  int index;
  vtkIdType pos = this->HashFunction(ptId);
  assert("check: valid range pos" &&
586
         static_cast<unsigned>(pos) < this->HashPoints->PointVector.size() );
Will Schroeder's avatar
Will Schroeder committed
587

Kyle Lutz's avatar
Kyle Lutz committed
588
  // Be careful with reference the equal is not overloaded
589
  vtkEdgeTablePoints::VectorPointTableType &vect =
Will Schroeder's avatar
Will Schroeder committed
590 591 592 593
    this->HashPoints->PointVector[pos];

  //Need to check size again

594 595
  int vectsize = static_cast<int>(vect.size());
  for (index=0; index<vectsize; index++)
Will Schroeder's avatar
Will Schroeder committed
596 597 598 599
    {
    PointEntry &ent = vect[index];
    if( ent.PointId == ptId )
      {
600 601
      memcpy(point,ent.Coord,sizeof(double)*3);
      memcpy(scalar,ent.Scalar,sizeof(double)*this->NumberOfComponents);
Will Schroeder's avatar
Will Schroeder committed
602 603 604 605 606 607 608 609 610 611 612 613
      return 1;
      }
    }

  if( index == vectsize )
    {
    //We did not find any corresponding entry, warn user
    vtkErrorMacro( << "No entry were found in the hash table:" << ptId );
    return 0;
    }

  assert("check: TODO" && 0 );
Will Schroeder's avatar
Will Schroeder committed
614
  return 1;
Will Schroeder's avatar
Will Schroeder committed
615 616 617 618 619 620 621 622 623
}

//-----------------------------------------------------------------------------
void vtkGenericEdgeTable::InsertPoint(vtkIdType ptId, double point[3])
{
  vtkIdType pos = this->HashFunction(ptId);

  //Need to check size first
  //this->HashPoints->Resize( pos );
624
  assert("check: valid range pos" &&
625
         static_cast<unsigned>(pos) < this->HashPoints->PointVector.size() );
Will Schroeder's avatar
Will Schroeder committed
626

Kyle Lutz's avatar
Kyle Lutz committed
627
  //Be careful with reference the equal is not overloaded
628
  vtkEdgeTablePoints::VectorPointTableType &vect =
Will Schroeder's avatar
Will Schroeder committed
629 630 631
    this->HashPoints->PointVector[pos];

  //Need to check size again
632
  //Here we just puch_back at the end,
Will Schroeder's avatar
Will Schroeder committed
633
  //the vector should not be very long and should not contain any empty spot.
634
  PointEntry newEntry(this->NumberOfComponents); // = vect.back();
Will Schroeder's avatar
Will Schroeder committed
635
  newEntry.PointId = ptId;
636
  memcpy(newEntry.Coord,point,sizeof(double)*3);
Will Schroeder's avatar
Will Schroeder committed
637 638
  newEntry.Reference = 1;

639
//  vtkDebugMacro( << "Inserting Point:" << ptId << ":"
Will Schroeder's avatar
Will Schroeder committed
640 641 642 643 644 645 646
//    << point[0] << "," << point[1] << "," << point[2] );
  vect.push_back( newEntry );
}

//-----------------------------------------------------------------------------
void vtkGenericEdgeTable::RemovePoint(vtkIdType ptId)
{
647
  int found = 0;
Will Schroeder's avatar
Will Schroeder committed
648 649 650
  vtkIdType pos = this->HashFunction(ptId);

  //Need to check size first
651
  assert("check: valid range pos" &&
652
         static_cast<unsigned>(pos) < this->HashPoints->PointVector.size() );
Will Schroeder's avatar
Will Schroeder committed
653

Kyle Lutz's avatar
Kyle Lutz committed
654
  //Be careful with reference the equal is not overloaded
655
  vtkEdgeTablePoints::VectorPointTableType &vect =
Will Schroeder's avatar
Will Schroeder committed
656 657 658 659
    this->HashPoints->PointVector[pos];

  //vtkDebugMacro( << "Remove Point:" << ptId << ":" << vect.size() );

660
  vtkEdgeTablePoints::VectorPointTableType::iterator it;
661
  for (it= vect.begin(); it!= vect.end(); )
Will Schroeder's avatar
Will Schroeder committed
662
    {
663 664
    PointEntry &ent = *it;

Will Schroeder's avatar
Will Schroeder committed
665 666
    if( ent.PointId == ptId )
      {
667
      --ent.Reference;
668
      found = 1;
Will Schroeder's avatar
Will Schroeder committed
669
      }
670 671 672 673 674 675 676 677 678

    if( ent.PointId == ptId && ent.Reference == 0 )
      {
      it = vect.erase(it);
      }
    else
      {
      ++it;
      }
Will Schroeder's avatar
Will Schroeder committed
679 680 681 682 683 684 685 686 687 688 689
    }

  if( !found )
    {
    //We did not find any corresponding entry, warn user
    vtkErrorMacro( << "No entry were found in the hash table" );
    }
}

//-----------------------------------------------------------------------------
void vtkGenericEdgeTable::InsertPointAndScalar(vtkIdType ptId, double pt[3],
690
                                               double *s)
Will Schroeder's avatar
Will Schroeder committed
691
{
692
  // sizeof(s)=this->NumberOfComponents
Will Schroeder's avatar
Will Schroeder committed
693 694 695 696
  vtkIdType pos = this->HashFunction(ptId);

  //Need to check size first
  //this->HashPoints->Resize( pos );
697
  if( !(static_cast<unsigned>(pos) < this->HashPoints->PointVector.size() ))
Will Schroeder's avatar
Will Schroeder committed
698 699 700 701 702
    {
    int kk = 2;
    kk++;
    }

Kyle Lutz's avatar
Kyle Lutz committed
703
  //Be careful with reference the equal is not overloaded
Will Schroeder's avatar
Will Schroeder committed
704 705 706 707 708
  vtkEdgeTablePoints::VectorPointTableType &vect = this->HashPoints->PointVector[pos];

  //Please keep the following:
#if 0
  //Need to check size again
709
  //Here we just puch_back at the end,
Will Schroeder's avatar
Will Schroeder committed
710
  //the vector should not be very long and should not contain any empty spot.
711
  for (index=0; index<vect.size(); index++)
Will Schroeder's avatar
Will Schroeder committed
712 713 714 715 716 717 718 719 720 721 722 723
    {
    PointEntry ent = vect[index];
    if( ent.PointId == ptId )
      {
      //The point was already in the table:
      assert("check: same id" &&  ent.PointId == ptId );
      assert("check: same x" && ent.Coord[0] == pt[0]);
      assert("check: same y" && ent.Coord[1] == pt[1]);
      assert("check: same z" && ent.Coord[2] == pt[2]);
      assert("check: same x scalar" && ent.Scalar[0] == s[0]);
      assert("check: same y scalar" && ent.Scalar[1] == s[1]);
      assert("check: same z scalar" && ent.Scalar[2] == s[2]);
724

Will Schroeder's avatar
Will Schroeder committed
725
      vtkErrorMacro( << "The point was already in the table" ); //FIXME
726

Will Schroeder's avatar
Will Schroeder committed
727 728 729 730 731 732
      return;
      }
    }
#endif

  //We did not find any matching point thus insert it
733

734
  PointEntry newEntry(this->NumberOfComponents); // = vect.back();
Will Schroeder's avatar
Will Schroeder committed
735
  newEntry.PointId = ptId;
736 737
  memcpy(newEntry.Coord,pt,sizeof(double)*3);
  memcpy(newEntry.Scalar,s,sizeof(double)*this->NumberOfComponents);
Will Schroeder's avatar
Will Schroeder committed
738 739
  newEntry.Reference = 1;

740 741
//  vtkDebugMacro( << "InsertPointAndScalar:" << ptId << ":"
//    << pt[0] << "," << pt[1] << "," << pt[2] << "->"
742
//    << s[0] << "," << s[1] << "," << s[2] );
Will Schroeder's avatar
Will Schroeder committed
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757

  vect.push_back( newEntry );
}

//-----------------------------------------------------------------------------
void vtkGenericEdgeTable::DumpTable()
{
  this->EdgeTable->DumpEdges();
  this->HashPoints->DumpPoints();
}

//-----------------------------------------------------------------------------
void vtkGenericEdgeTable::IncrementPointReferenceCount(vtkIdType ptId )
{
  unsigned int index;
758
  int found = 0;
Will Schroeder's avatar
Will Schroeder committed
759 760 761 762
  vtkIdType pos = this->HashFunction(ptId);

  //Need to check size first
  assert("check: valid range pos" &&
763
         static_cast<unsigned>(pos) < this->HashPoints->PointVector.size() );
Will Schroeder's avatar
Will Schroeder committed
764

Kyle Lutz's avatar
Kyle Lutz committed
765
  //Be careful with reference the equal is not overloaded
Will Schroeder's avatar
Will Schroeder committed
766 767 768 769
  vtkEdgeTablePoints::VectorPointTableType &vect = this->HashPoints->PointVector[pos];

  //vtkDebugMacro(<< "IncrementPointReferenceCount:" << ptId << ":" << vect.size() );

770
  for (index=0; index<vect.size(); index++)
Will Schroeder's avatar
Will Schroeder committed
771 772 773 774 775
    {
    PointEntry &ent = vect[index];
    if( ent.PointId == ptId )
      {
      ent.Reference++;
776
      found = 1;
Will Schroeder's avatar
Will Schroeder committed
777 778 779 780 781 782 783 784 785 786 787 788 789 790
      }
    }

  if( !found )
    {
    //We did not find any corresponding entry, warn user
    vtkErrorMacro( << "No entry were found in the hash table" );
    }
}

//-----------------------------------------------------------------------------
void vtkGenericEdgeTable::LoadFactor()
{
  vtkDebugMacro( << "------ Begin LoadFactor ------- " );
791

Will Schroeder's avatar
Will Schroeder committed
792
  this->EdgeTable->LoadFactor();
793
  this->HashPoints->LoadFactor();
Will Schroeder's avatar
Will Schroeder committed
794
}