------------------------------------------------------------------------------
| 546 | |
| 547 | //------------------------------------------------------------------------------ |
| 548 | bool vtkLabeledContourMapper::PrepareRender(vtkRenderer* ren, vtkActor* act) |
| 549 | { |
| 550 | if (!this->Internal->SetViewInfo(ren, act)) |
| 551 | { |
| 552 | return false; |
| 553 | } |
| 554 | |
| 555 | // Already checked that these exist in CheckInputs() |
| 556 | vtkPolyData* input = this->GetInput(); |
| 557 | vtkCellArray* lines = input->GetLines(); |
| 558 | vtkDataArray* scalars = input->GetPointData()->GetScalars(); |
| 559 | vtkTextRenderer* tren = vtkTextRenderer::GetInstance(); |
| 560 | if (!tren) |
| 561 | { |
| 562 | vtkErrorMacro(<< "Text renderer unavailable."); |
| 563 | return false; |
| 564 | } |
| 565 | |
| 566 | // Maps scalar values to text properties: |
| 567 | typedef std::map<double, vtkTextProperty*> LabelPropertyMapType; |
| 568 | LabelPropertyMapType labelMap; |
| 569 | |
| 570 | // Initialize with the user-requested mapping, if it exists. |
| 571 | if (this->TextPropertyMapping != nullptr) |
| 572 | { |
| 573 | vtkDoubleArray::Iterator valIt = this->TextPropertyMapping->Begin(); |
| 574 | vtkDoubleArray::Iterator valItEnd = this->TextPropertyMapping->End(); |
| 575 | TextPropLoop tprops(this->TextProperties); |
| 576 | for (; valIt != valItEnd; ++valIt) |
| 577 | { |
| 578 | labelMap.insert(std::make_pair(*valIt, tprops.Next())); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | // Create the list of metrics, but no text property information yet. |
| 583 | vtkIdType numPts; |
| 584 | const vtkIdType* ids; |
| 585 | for (lines->InitTraversal(); lines->GetNextCell(numPts, ids);) |
| 586 | { |
| 587 | this->Internal->LabelMetrics.emplace_back(); |
| 588 | LabelMetric& metric = this->Internal->LabelMetrics.back(); |
| 589 | if (!(metric.Valid = (numPts > 0))) |
| 590 | { |
| 591 | // Mark as invalid and skip if there are no points. |
| 592 | continue; |
| 593 | } |
| 594 | metric.Value = scalars->GetComponent(ids[0], 0); |
| 595 | metric.Value = std::fabs(metric.Value) > 1e-6 ? metric.Value : 0.0; |
| 596 | std::ostringstream str; |
| 597 | str << metric.Value; |
| 598 | metric.Text = str.str(); |
| 599 | |
| 600 | // Beware future maintainers: The following line of code has been carefully |
| 601 | // crafted to reach a zen-like harmony of compatibility between various |
| 602 | // compilers that have differing syntactic requirements for creating a |
| 603 | // pair containing a nullptr: |
| 604 | // - Pedantically strict C++11 compilers (e.g. MSVC 2012) will not compile: |
| 605 | // std::make_pair<double, X*>(someDouble, nullptr); |
no test coverage detected