1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestOTDensityMap.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
16 #include "vtkDoubleArray.h"
17 #include "vtkExecutive.h"
18 #include "vtkMathUtilities.h"
19 #include "vtkMultiBlockDataSet.h"
20 #include "vtkNew.h"
21 #include "vtkOTDensityMap.h"
22 #include "vtkTable.h"
23 #include "vtkTestErrorObserver.h"
24 
25 #include <iostream>
26 
27 //----------------------------------------------------------------------------
TestOTDensityMap(int,char * [])28 int TestOTDensityMap(int, char* [])
29 {
30   vtkNew<vtkDoubleArray> arrFirstVariable;
31   arrFirstVariable->SetName("Math");
32 
33   vtkNew<vtkDoubleArray> arrSecondVariable;
34   arrSecondVariable->SetName("French");
35 
36   // Create a two columns table
37   vtkNew<vtkTable> table;
38   table->AddColumn(arrFirstVariable);
39   table->AddColumn(arrSecondVariable);
40 
41   const int numNotes = 20;
42   table->SetNumberOfRows(numNotes);
43 
44   const double MathValue[] = {
45     18, 20, 20, 16, 12, 14, 16, 14, 14, 13, 16, 18, 6, 10, 16, 14, 4, 16, 16, 14
46   };
47 
48   const double FrenchValue[] = {
49     14, 12, 14, 16, 12, 14, 16, 4, 4, 10, 6, 20, 14, 16, 14, 14, 12, 2, 14, 8
50   };
51 
52   for (int i = 0; i < numNotes; ++i)
53   {
54     table->SetValue(i, 0, MathValue[i]);
55     table->SetValue(i, 1, FrenchValue[i]);
56   }
57 
58   // Run Compute Quantiles
59   vtkNew<vtkOTDensityMap> density;
60 
61   vtkNew<vtkTest::ErrorObserver> errorObserver1;
62   // First verify that absence of input does not cause trouble
63   density->GetExecutive()->AddObserver(vtkCommand::ErrorEvent, errorObserver1);
64   density->Update();
65   errorObserver1->CheckErrorMessage("Input port 0 of algorithm vtkOTDensityMap");
66 
67   // Now set the real input table
68   density->SetInputData(table);
69   density->SetNumberOfContours(3);
70   density->SetValue(0, 0.1);
71   density->SetValue(1, 0.5);
72   density->SetValue(2, 0.9);
73   density->SetInputArrayToProcess(0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_ROWS, "Math");
74   density->SetInputArrayToProcess(1, 0, 0, vtkDataObject::FIELD_ASSOCIATION_ROWS, "French");
75   density->Update();
76 
77   vtkMultiBlockDataSet* mbTable = density->GetOutput();
78 
79   if (mbTable->GetNumberOfBlocks() != 3)
80   {
81     cout << "Unexpected Number of Contour Blocks" << std::endl;
82     return EXIT_FAILURE;
83   }
84 
85   unsigned int childsNBlock[] = { 1, 2, 4 };
86 
87   int tablesNRows[] = { 44, 89, 52, 94, 36, 48, 10 };
88 
89   double tablesYValues[] = { 12.80000019073486328125,
90     14.934099197387695312,
91     11.056828498840332031,
92     19.480913162231445312,
93     8.7593898773193359375,
94     15.193044662475585938,
95     19.2800006866455078125 };
96 
97   int nTable = 0;
98   for (unsigned int i = 0; i < mbTable->GetNumberOfBlocks(); i++)
99   {
100     vtkMultiBlockDataSet* childBlock = vtkMultiBlockDataSet::SafeDownCast(mbTable->GetBlock(i));
101     if (!childBlock || childBlock->GetNumberOfBlocks() != childsNBlock[i])
102     {
103       cout << "Unexpected Child Block format" << std::endl;
104       return EXIT_FAILURE;
105     }
106     for (unsigned int j = 0; j < childsNBlock[i]; j++)
107     {
108       vtkTable* childTable = vtkTable::SafeDownCast(childBlock->GetBlock(j));
109       if (!childTable || childTable->GetNumberOfColumns() != 2)
110       {
111         cout << "Unexpected Table format" << std::endl;
112         return EXIT_FAILURE;
113       }
114       if (childTable->GetNumberOfRows() != tablesNRows[nTable])
115       {
116         cout << "Unexpected Number of rows : " << childTable->GetNumberOfRows()
117              << " Expecting : " << tablesNRows[nTable] << std::endl;
118         return EXIT_FAILURE;
119       }
120       vtkVariant tableYValue = childTable->GetValue(0, 1);
121       if (!vtkMathUtilities::FuzzyCompare(tableYValue.ToDouble(), tablesYValues[nTable]))
122       {
123         std::cout << std::setprecision(20) << "Unexpected Table Value:" << tableYValue.ToDouble()
124                   << " Expecting : " << tablesYValues[nTable] << std::endl;
125       }
126       nTable++;
127     }
128   }
129   return EXIT_SUCCESS;
130 }
131