1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 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
|
/*=========================================================================
Program: Visualization Toolkit
Module: TestPolyhedron5.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 "vtkClipDataSet.h"
#include "vtkContourFilter.h"
#include "vtkDoubleArray.h"
#include "vtkNew.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkUnstructuredGrid.h"
#include "vtkXMLPolyDataWriter.h"
/* This is the layout of a cube with points on each edge
In the test below we're going to test all combinations of
edge points being present. As there are 12 edge points, the
number of combinations is 2^12, i.e. 4096 cases. This can be
calculated in ~15 seconds
Point indices: Face indices:
7--14--6 *------*
| | | |
19 18 | 2 |
| | | |
7--19--3--10--2--18--6 *------*------*------*
| | | | | | | |
15 11 9 13 | 3 | 4 | 1 |
| | | | | | | |
4--16--0---8--1--17--5 *------*------*------*
| | | |
16 17 | 0 |
| | | |
4--12--5 *------*
| | | |
15 13 | 5 |
| | | |
7--14--6 *------*
*/
#define CORNERS 8
#define EDGES 12
#define FACES 6
#define NPOINTS (CORNERS + EDGES + FACES)
const int Faces[FACES][8] = {
{ 0, 8, 1, 17, 5, 12, 4, 16 },
{ 1, 9, 2, 18, 6, 13, 5, 17 },
{ 2, 10, 3, 19, 7, 14, 6, 18 },
{ 3, 11, 0, 16, 4, 15, 7, 19 },
{ 0, 8, 1, 9, 2, 10, 3, 11 },
{ 4, 12, 5, 13, 6, 14, 7, 15 },
};
const double Points[CORNERS + EDGES + FACES][3] = {
// first the corner points
// lower plane
{ 0, 0, 0 },
{ 0, 2, 0 },
{ 2, 2, 0 },
{ 2, 0, 0 },
// upper plane
{ 0, 0, 2 },
{ 0, 2, 2 },
{ 2, 2, 2 },
{ 2, 0, 2 },
// then the edge points
// lower plane
{ 0, 1, 0 },
{ 1, 2, 0 },
{ 2, 1, 0 },
{ 1, 0, 0 },
// upper plane
{ 0, 1, 2 },
{ 1, 2, 2 },
{ 2, 1, 2 },
{ 1, 0, 2 },
// intermediate plane
// make the polyhedron concave by offsetting
// these points towards the cube center
{ 0.25, 0.25, 1 },
{ 0.25, 1.75, 1 },
{ 1.75, 1.75, 1 },
{ 1.75, 0.25, 1 },
// face centers (not used for now)
{ 1, 1, 0 },
{ 0, 1, 1 },
{ 1, 2, 1 },
{ 2, 1, 1 },
{ 1, 0, 1 },
{ 1, 1, 2 },
};
void BuildCaseGrid(int aCase, vtkUnstructuredGrid* grid, vtkIdList* faceStream)
{
faceStream->Reset();
for (int i = 0; i < FACES; ++i)
{
const int* face = Faces[i];
int nFacePoints = 0;
for (int j = 0; j < 8; ++j)
{
int idx = 1 << face[j];
if ((aCase & idx) != 0)
{
++nFacePoints;
}
}
faceStream->InsertNextId(nFacePoints);
for (int j = 0; j < 8; ++j)
{
int idx = 1 << face[j];
if ((aCase & idx) != 0)
{
faceStream->InsertNextId(face[j]);
}
}
}
grid->InsertNextCell(VTK_POLYHEDRON, FACES, faceStream->GetPointer(0));
}
void BuildPoints(vtkPoints* pts)
{
for (int i = 0; i < NPOINTS; ++i)
{
const double* pt = Points[i];
pts->InsertNextPoint(pt);
}
}
int TestPolyhedronCombinatorialContouring(int vtkNotUsed(argc), char* vtkNotUsed(argv)[])
{
vtkNew<vtkPoints> pts;
BuildPoints(pts);
vtkNew<vtkUnstructuredGrid> g;
g->Allocate(1);
vtkNew<vtkIdList> ptIds;
vtkNew<vtkXMLPolyDataWriter> pw;
vtkNew<vtkDoubleArray> data;
data->SetName("AirVolumeFraction");
data->Allocate(NPOINTS);
data->SetNumberOfTuples(NPOINTS);
// assign 0 to even points and 1 to odd points, then contour at 0.5
// TODO: also vary the data systematically?
// (although that would give rise to sum(i=1..20, binomial(20,i)*2^(6+i)) = O(10^7) cases
// and 4,096 cases run in about 15 seconds)
// FOR NOW: don't vary the data
for (int i = 0; i < NPOINTS; ++i)
{
data->SetTuple1(i, i % 2);
}
g->GetPointData()->AddArray(data);
// there are 8 constant points (the corner points)
int corners = (1 << CORNERS) - 1;
// there are 12 variable points (the edge points)
int nCases = 1 << EDGES;
for (int i = 1; i < nCases; ++i)
{
int aCase = corners + (i << CORNERS);
g->Reset();
g->SetPoints(pts);
BuildCaseGrid(aCase, g, ptIds);
vtkNew<vtkContourFilter> cf;
cf->SetInputArrayToProcess(
0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_POINTS, "AirVolumeFraction");
cf->SetValue(0, 0.5);
cf->SetInputData(g);
cf->Update();
vtkPolyData* result = cf->GetOutput();
if (!result || result->GetNumberOfCells() < 1)
{
cerr << "Case " << aCase << " has no contour" << endl;
return EXIT_FAILURE;
}
vtkNew<vtkClipDataSet> clipLess, clipMore;
clipLess->SetInsideOut(0);
clipMore->SetInsideOut(1);
clipLess->SetInputData(g);
clipMore->SetInputData(g);
clipLess->SetInputArrayToProcess(
0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_POINTS, "AirVolumeFraction");
clipLess->SetValue(0.5);
clipMore->SetInputArrayToProcess(
0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_POINTS, "AirVolumeFraction");
clipMore->SetValue(0.5);
clipLess->Update();
vtkUnstructuredGrid* less = clipLess->GetOutput();
if (!less || less->GetNumberOfCells() < 1)
{
cerr << "Case " << aCase << " has no 'less' clip result" << endl;
return EXIT_FAILURE;
}
clipMore->Update();
vtkUnstructuredGrid* more = clipMore->GetOutput();
if (!more || more->GetNumberOfCells() < 1)
{
cerr << "Case " << aCase << " has no 'more' clip result" << endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
|