VTK生成等值线并标记值
作者:互联网
1 #include <vtkActor.h> 2 #include <vtkDataSetMapper.h> 3 #include <vtkNamedColors.h> 4 #include <vtkNew.h> 5 #include <vtkPoints.h> 6 #include <vtkRenderWindow.h> 7 #include <vtkRenderWindowInteractor.h> 8 #include <vtkRenderer.h> 9 #include <vtkSmartPointer.h> 10 #include <vtkAppendFilter.h> 11 #include <vtkProperty.h> 12 #include <vtkUnstructuredGrid.h> 13 #include <vtkCellDataToPointData.h> 14 #include <vtkFloatArray.h> 15 #include <vtkCellData.h> 16 #include <vtkContourFilter.h> 17 #include <vtkAutoInit.h> 18 #include <vtkStripper.h> 19 #include <vtkCellArrayIterator.h> 20 #include <cstdlib> 21 #include <vtkPointData.h> 22 #include <vtkDoubleArray.h> 23 #include <vtkLabeledDataMapper.h> 24 #include <vtkPolyData.h> 25 #include <vtkTextProperty.h> 26 #include <vtkActor2D.h> 27 28 VTK_MODULE_INIT(vtkInteractionStyle); 29 VTK_MODULE_INIT(vtkRenderingFreeType); 30 VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2); 31 32 int main(int argc, char* argv[]) 33 { 34 vtkNew<vtkUnstructuredGrid> grid; /* 创建一个非结构化网格对象 */ 35 /*=============================网格节点=============================*/ 36 vtkNew<vtkPoints> meshPoints; /* 用于存储网格节点 */ 37 meshPoints->InsertNextPoint(20, 0, 0); 38 meshPoints->InsertNextPoint(17.320508109792, 10.0000000321997, 0); 39 meshPoints->InsertNextPoint(10.0000000084796, 17.3205081212641, 0); 40 meshPoints->InsertNextPoint(0, 20, 0); 41 meshPoints->InsertNextPoint(-10.0000000245946, 17.3205081142845, 0); 42 meshPoints->InsertNextPoint(-17.320508106976, 10.0000000330863, 0); 43 meshPoints->InsertNextPoint(-20, 0, 0); 44 meshPoints->InsertNextPoint(-17.3205081361632, -9.99999998652343, 0); 45 meshPoints->InsertNextPoint(-10.0000000640139, -17.3205080893384, 0); 46 meshPoints->InsertNextPoint(0, -20, 0); 47 meshPoints->InsertNextPoint(9.99999997546481, -17.3205081119126, 0); 48 meshPoints->InsertNextPoint(17.3205080670077, -10.000000025522, 0); 49 meshPoints->InsertNextPoint(-7.15966651257561, -7.12683602128246, 0); 50 meshPoints->InsertNextPoint(-2.63739358105485, -12.9978938464381, 0); 51 meshPoints->InsertNextPoint(-9.10592684278358, -0.737849316405346, 0); 52 meshPoints->InsertNextPoint(6.59947131139743, -9.08284206416132, 0); 53 meshPoints->InsertNextPoint(-0.137734590259206, -2.71811814272003, 0); 54 meshPoints->InsertNextPoint(-7.75933677195531, 5.80947235781205, 0); 55 meshPoints->InsertNextPoint(9.11726442880688, -1.21602962482667, 0); 56 meshPoints->InsertNextPoint(-0.484322809037511, 4.65580556604978, 0); 57 meshPoints->InsertNextPoint(-3.48354780828141, 10.4142340529639, 0); 58 meshPoints->InsertNextPoint(7.28614335171517, 6.31993800133739, 0); 59 meshPoints->InsertNextPoint(3.36376652373578, 13.053010718073, 0); 60 grid->SetPoints(meshPoints); 61 /*=================================================================*/ 62 63 /*=============================设置单元=============================*/ 64 vtkNew<vtkIdList> pointsId; 65 pointsId->SetNumberOfIds(3); 66 pointsId->SetId(0, 9); 67 pointsId->SetId(1, 13); 68 pointsId->SetId(2, 8); 69 grid->InsertNextCell(VTK_TRIANGLE, 3, pointsId->GetPointer(0)); 70 71 pointsId->Initialize(); 72 pointsId->SetNumberOfIds(3); 73 pointsId->SetId(0, 14); 74 pointsId->SetId(1, 12); 75 pointsId->SetId(2, 16); 76 grid->InsertNextCell(VTK_TRIANGLE, 3, pointsId->GetPointer(0)); 77 78 pointsId->Initialize(); 79 pointsId->SetNumberOfIds(3); 80 pointsId->SetId(0, 11); 81 pointsId->SetId(1, 15); 82 pointsId->SetId(2, 10); 83 grid->InsertNextCell(VTK_TRIANGLE, 3, pointsId->GetPointer(0)); 84 85 pointsId->Initialize(); 86 pointsId->SetNumberOfIds(3); 87 pointsId->SetId(0, 15); 88 pointsId->SetId(1, 18); 89 pointsId->SetId(2, 16); 90 grid->InsertNextCell(VTK_TRIANGLE, 3, pointsId->GetPointer(0)); 91 92 pointsId->Initialize(); 93 pointsId->SetNumberOfIds(3); 94 pointsId->SetId(0, 17); 95 pointsId->SetId(1, 19); 96 pointsId->SetId(2, 20); 97 grid->InsertNextCell(VTK_TRIANGLE, 3, pointsId->GetPointer(0)); 98 99 pointsId->Initialize(); 100 pointsId->SetNumberOfIds(3); 101 pointsId->SetId(0, 3); 102 pointsId->SetId(1, 22); 103 pointsId->SetId(2, 2); 104 grid->InsertNextCell(VTK_TRIANGLE, 3, pointsId->GetPointer(0)); 105 106 pointsId->Initialize(); 107 pointsId->SetNumberOfIds(4); 108 pointsId->SetId(0, 8); 109 pointsId->SetId(1, 13); 110 pointsId->SetId(2, 12); 111 pointsId->SetId(3, 7); 112 grid->InsertNextCell(VTK_QUAD, 4, pointsId->GetPointer(0)); 113 114 pointsId->Initialize(); 115 pointsId->SetNumberOfIds(4); 116 pointsId->SetId(0, 12); 117 pointsId->SetId(1, 14); 118 pointsId->SetId(2, 6); 119 pointsId->SetId(3, 7); 120 grid->InsertNextCell(VTK_QUAD, 4, pointsId->GetPointer(0)); 121 122 pointsId->Initialize(); 123 pointsId->SetNumberOfIds(4); 124 pointsId->SetId(0, 13); 125 pointsId->SetId(1, 9); 126 pointsId->SetId(2, 10); 127 pointsId->SetId(3, 15); 128 grid->InsertNextCell(VTK_QUAD, 4, pointsId->GetPointer(0)); 129 130 pointsId->Initialize(); 131 pointsId->SetNumberOfIds(4); 132 pointsId->SetId(0, 6); 133 pointsId->SetId(1, 14); 134 pointsId->SetId(2, 17); 135 pointsId->SetId(3, 5); 136 grid->InsertNextCell(VTK_QUAD, 4, pointsId->GetPointer(0)); 137 138 pointsId->Initialize(); 139 pointsId->SetNumberOfIds(4); 140 pointsId->SetId(0, 16); 141 pointsId->SetId(1, 12); 142 pointsId->SetId(2, 13); 143 pointsId->SetId(3, 15); 144 grid->InsertNextCell(VTK_QUAD, 4, pointsId->GetPointer(0)); 145 146 pointsId->Initialize(); 147 pointsId->SetNumberOfIds(4); 148 pointsId->SetId(0, 14); 149 pointsId->SetId(1, 16); 150 pointsId->SetId(2, 19); 151 pointsId->SetId(3, 17); 152 grid->InsertNextCell(VTK_QUAD, 4, pointsId->GetPointer(0)); 153 154 pointsId->Initialize(); 155 pointsId->SetNumberOfIds(4); 156 pointsId->SetId(0, 17); 157 pointsId->SetId(1, 20); 158 pointsId->SetId(2, 4); 159 pointsId->SetId(3, 5); 160 grid->InsertNextCell(VTK_QUAD, 4, pointsId->GetPointer(0)); 161 162 pointsId->Initialize(); 163 pointsId->SetNumberOfIds(4); 164 pointsId->SetId(0, 16); 165 pointsId->SetId(1, 18); 166 pointsId->SetId(2, 21); 167 pointsId->SetId(3, 19); 168 grid->InsertNextCell(VTK_QUAD, 4, pointsId->GetPointer(0)); 169 170 pointsId->Initialize(); 171 pointsId->SetNumberOfIds(4); 172 pointsId->SetId(0, 15); 173 pointsId->SetId(1, 11); 174 pointsId->SetId(2, 0); 175 pointsId->SetId(3, 18); 176 grid->InsertNextCell(VTK_QUAD, 4, pointsId->GetPointer(0)); 177 178 pointsId->Initialize(); 179 pointsId->SetNumberOfIds(4); 180 pointsId->SetId(0, 20); 181 pointsId->SetId(1, 19); 182 pointsId->SetId(2, 21); 183 pointsId->SetId(3, 22); 184 grid->InsertNextCell(VTK_QUAD, 4, pointsId->GetPointer(0)); 185 186 pointsId->Initialize(); 187 pointsId->SetNumberOfIds(4); 188 pointsId->SetId(0, 22); 189 pointsId->SetId(1, 3); 190 pointsId->SetId(2, 4); 191 pointsId->SetId(3, 20); 192 grid->InsertNextCell(VTK_QUAD, 4, pointsId->GetPointer(0)); 193 194 pointsId->Initialize(); 195 pointsId->SetNumberOfIds(4); 196 pointsId->SetId(0, 21); 197 pointsId->SetId(1, 18); 198 pointsId->SetId(2, 0); 199 pointsId->SetId(3, 1); 200 grid->InsertNextCell(VTK_QUAD, 4, pointsId->GetPointer(0)); 201 202 pointsId->Initialize(); 203 pointsId->SetNumberOfIds(4); 204 pointsId->SetId(0, 2); 205 pointsId->SetId(1, 22); 206 pointsId->SetId(2, 21); 207 pointsId->SetId(3, 1); 208 grid->InsertNextCell(VTK_QUAD, 4, pointsId->GetPointer(0)); 209 /*=================================================================*/ 210 211 /*==============================设置单元颜色========================*/ 212 vtkNew<vtkFloatArray> color; /* 用于设置每个单元的随机颜色 */ 213 for (int i = 0; i < grid->GetNumberOfCells(); ++i) 214 { 215 color->InsertNextValue(std::rand()); 216 } 217 grid->GetCellData()->SetScalars(color); /* 设置单元格的颜色 */ 218 /*=================================================================*/ 219 220 /*==============================单元——>节点========================*/ 221 double rangeMin = grid->GetScalarRange()[0]; 222 double rangeMax = grid->GetScalarRange()[1]; 223 vtkNew<vtkCellDataToPointData> theCellDataToPointData; 224 theCellDataToPointData->SetInputData(grid); 225 theCellDataToPointData->PassCellDataOn(); 226 theCellDataToPointData->Update(); 227 /*=================================================================*/ 228 229 /*============================等值线================================*/ 230 vtkNew<vtkContourFilter> theContourFilter; 231 theContourFilter->SetInputData(theCellDataToPointData->GetOutput()); 232 theContourFilter->GenerateValues(5, rangeMin, rangeMax); 233 theContourFilter->Update(); 234 /*=================================================================*/ 235 236 /*============================等值线值标记===========================*/ 237 // 用于将离散的三角面片拼接为连续的等值面 238 vtkNew<vtkStripper> theStripper; 239 theStripper->SetInputData(theContourFilter->GetOutput()); 240 theStripper->Update(); 241 // 等值线数目 242 vtkIdType lines = theStripper->GetOutput()->GetNumberOfLines(); 243 vtkPoints* points = theStripper->GetOutput()->GetPoints(); 244 vtkCellArray* cells = theStripper->GetOutput()->GetLines(); 245 vtkDataArray* scalars = theStripper->GetOutput()->GetPointData()->GetScalars(); 246 vtkNew<vtkPolyData> labelPolyData; 247 vtkNew<vtkPoints> labelPoints; 248 vtkNew<vtkDoubleArray> labelScalars; 249 labelScalars->SetNumberOfComponents(1); 250 labelScalars->SetName("IsoValues"); 251 252 // 创建单元数组迭代器 253 auto cellIter = vtk::TakeSmartPointer(cells->NewIterator()); 254 for (cellIter->GoToFirstCell(); !cellIter->IsDoneWithTraversal(); cellIter->GoToNextCell()) 255 { 256 vtkIdList* cell = cellIter->GetCurrentCell(); 257 const vtkIdType samplePtIdx = static_cast<vtkIdType>(vtkMath::Random(0, cell->GetNumberOfIds())); 258 vtkIdType midPointId = cell->GetId(samplePtIdx); 259 260 double midPoint[3]; 261 points->GetPoint(midPointId, midPoint); 262 labelPoints->InsertNextPoint(midPoint); 263 labelScalars->InsertNextTuple1(scalars->GetTuple1(midPointId)); 264 } 265 labelPolyData->SetPoints(labelPoints); 266 267 labelPolyData->GetPointData()->SetScalars(labelScalars); 268 /*=================================================================*/ 269 270 vtkNew<vtkDataSetMapper> contourMapper; 271 contourMapper->SetInputData(theContourFilter->GetOutput()); 272 vtkNew<vtkDataSetMapper> mapper; 273 mapper->SetInputData(theCellDataToPointData->GetOutput()); 274 mapper->SetScalarModeToUsePointData(); 275 mapper->SetScalarRange(rangeMin, rangeMax); 276 277 vtkNew<vtkActor> contourActor; 278 contourActor->SetMapper(contourMapper); 279 280 vtkNew<vtkActor> actor; 281 actor->SetMapper(mapper); 282 283 vtkNew<vtkLabeledDataMapper> labelMapper; 284 labelMapper->SetFieldDataName("IsoValues"); 285 labelMapper->SetInputData(labelPolyData); 286 labelMapper->SetLabelModeToLabelScalars(); 287 labelMapper->SetLabelFormat("%6.2f"); 288 labelMapper->GetLabelTextProperty()->SetColor(0, 0, 1); 289 290 vtkNew<vtkActor2D> labelActor; 291 labelActor->SetMapper(labelMapper); 292 293 vtkNew<vtkRenderer> renderer; 294 renderer->AddActor(actor); 295 renderer->AddActor(labelActor); 296 renderer->AddActor(contourActor); 297 298 vtkNew<vtkRenderWindowInteractor> windowInteractor; 299 vtkNew<vtkRenderWindow> renderWindow; 300 301 windowInteractor->SetRenderWindow(renderWindow); 302 renderWindow->AddRenderer(renderer); 303 304 305 renderWindow->Render(); 306 307 windowInteractor->Start(); 308 309 return 0; 310 }
原文地址:https://www.cnblogs.com/liusuanyatong/p/14319699.html
标签:标记,meshPoints,等值线,VTK,pointsId,grid,InsertNextPoint,SetId,include 来源: https://www.cnblogs.com/chanyuantiandao/p/16480519.html