VTK Users Guide 中C++例程之multiLineText
本人将《The VTK User’s Guide 11th Edition 》中的TCL编写的例程进行了C++的转换,使用的平台是VS2015,VTK版本是8.0.1。之所以贴出来,以求方便大家使用C++来进行VTK的研究。
VTK/Examples/Annotation/Tcl/multiLineText.tcl 之 转成C++
1 #include "vtkAutoInit.h" 2 #include "vtkTextProperty.h" 3 #include "vtkTextMapper.h" 4 #include "vtkActor2D.h" 5 #include "vtkPoints.h" 6 #include "vtkCellArray.h" 7 #include "vtkPolyData.h" 8 #include "vtkCoordinate.h" 9 #include "vtkPolyDataMapper2D.h" 10 #include "vtkProperty2D.h" 11 #include "vtkRenderer.h" 12 #include "vtkRenderWindow.h" 13 #include "vtkRenderWindowInteractor.h" 14 #include "vtkCamera.h" 15 16 int main() 17 { 18 VTK_MODULE_INIT(vtkRenderingOpenGL2); 19 VTK_MODULE_INIT(vtkRenderingFreeType); 20 VTK_MODULE_INIT(vtkInteractionStyle); 21 22 // Create the text mappers and the associated Actor2Ds. 23 24 //The font and text properties (except justification) are the same for each 25 //single line mapper.Let's create a common text property object 26 vtkTextProperty *singleLineTextProp = vtkTextProperty::New(); 27 singleLineTextProp->SetFontSize(14); 28 singleLineTextProp->SetFontFamilyToArial(); 29 singleLineTextProp->BoldOff(); 30 singleLineTextProp->ItalicOff(); 31 singleLineTextProp->ShadowOff(); 32 33 //The font and text properties(except justification) are the same for each 34 //multi line mapper.Let's create a common text property object 35 vtkTextProperty *multiLineTextProp = vtkTextProperty::New(); 36 multiLineTextProp->ShallowCopy(singleLineTextProp); 37 multiLineTextProp->BoldOn(); 38 multiLineTextProp->ItalicOn(); 39 multiLineTextProp->ShadowOn(); 40 multiLineTextProp->SetLineSpacing(0.8); 41 42 //The text is on a single line and bottom-justified. 43 vtkTextMapper *singleLineTextB = vtkTextMapper::New(); 44 singleLineTextB->SetInput("Single line (bottom)"); 45 singleLineTextB->GetTextProperty()->ShallowCopy(singleLineTextProp); 46 singleLineTextB->GetTextProperty()->SetVerticalJustificationToBottom(); 47 singleLineTextB->GetTextProperty()->SetColor(1, 0, 0); 48 vtkActor2D *singleLineTextActorB = vtkActor2D::New(); 49 singleLineTextActorB->SetMapper(singleLineTextB); 50 singleLineTextActorB->GetPositionCoordinate()->SetCoordinateSystemToNormalizedDisplay(); 51 singleLineTextActorB->GetPositionCoordinate()->SetValue(0.05, 0.85); 52 53 //The text is on a single line and center-justified (vertical justification). 54 vtkTextMapper *singleLineTextC = vtkTextMapper::New(); 55 singleLineTextC->SetInput("Single line (centered)"); 56 singleLineTextC->GetTextProperty()->ShallowCopy(singleLineTextProp); 57 singleLineTextC->GetTextProperty()->SetVerticalJustificationToCentered(); 58 singleLineTextC->GetTextProperty()->SetColor(0, 1, 0); 59 vtkActor2D *singleLineTextActorC = vtkActor2D::New(); 60 singleLineTextActorC->SetMapper(singleLineTextC); 61 singleLineTextActorC->GetPositionCoordinate()->SetCoordinateSystemToNormalizedDisplay(); 62 singleLineTextActorC->GetPositionCoordinate()->SetValue(0.05, 0.75); 63 64 //The text is on a single line and top-justified. 65 vtkTextMapper *singleLineTextT = vtkTextMapper::New(); 66 singleLineTextT->SetInput("Single line (top)"); 67 singleLineTextT->GetTextProperty()->ShallowCopy(singleLineTextProp); 68 singleLineTextT->GetTextProperty()->SetVerticalJustificationToTop(); 69 singleLineTextT->GetTextProperty()->SetColor(0, 0, 1); 70 vtkActor2D *singleLineTextActorT = vtkActor2D::New(); 71 singleLineTextActorT->SetMapper(singleLineTextT); 72 singleLineTextActorT->GetPositionCoordinate()->SetCoordinateSystemToNormalizedDisplay(); 73 singleLineTextActorT->GetPositionCoordinate()->SetValue(0.05, 0.65); 74 75 //The text is on multiple lines and left- and top-justified. 76 vtkTextMapper *textMapperL = vtkTextMapper::New(); 77 textMapperL->SetInput("This is\nmulti-line\ntext output\n(left-top)"); 78 textMapperL->GetTextProperty()->ShallowCopy(multiLineTextProp); 79 textMapperL->GetTextProperty()->SetJustificationToLeft(); 80 textMapperL->GetTextProperty()->SetVerticalJustificationToTop(); 81 textMapperL->GetTextProperty()->SetColor(1, 0, 0); 82 vtkActor2D *textActorL = vtkActor2D::New(); 83 textActorL->SetMapper(textMapperL); 84 textActorL->GetPositionCoordinate()->SetCoordinateSystemToNormalizedDisplay(); 85 textActorL->GetPositionCoordinate()->SetValue(0.05, 0.5); 86 87 // The text is on multiple lines and center-justified (both horizontal and 88 // vertical). 89 vtkTextMapper *textMapperC = vtkTextMapper::New(); 90 textMapperC->SetInput("This is\nmulti-line\ntext output\n(centered)"); 91 textMapperC->GetTextProperty()->ShallowCopy(multiLineTextProp); 92 textMapperC->GetTextProperty()->SetJustificationToCentered(); 93 textMapperC->GetTextProperty()->SetVerticalJustificationToCentered(); 94 textMapperC->GetTextProperty()->SetColor(0, 1, 0); 95 vtkActor2D *textActorC = vtkActor2D::New(); 96 textActorC->SetMapper(textMapperC); 97 textActorC->GetPositionCoordinate()->SetCoordinateSystemToNormalizedDisplay(); 98 textActorC->GetPositionCoordinate()->SetValue(0.5, 0.5); 99 100 //The text is on multiple lines and right- and bottom-justified. 101 vtkTextMapper *textMapperR = vtkTextMapper::New(); 102 textMapperR->SetInput("This is\nmulti-line\ntext output\n(right-bottom)"); 103 textMapperR->GetTextProperty()->ShallowCopy(multiLineTextProp); 104 textMapperR->GetTextProperty()->SetJustificationToRight(); 105 textMapperR->GetTextProperty()->SetVerticalJustificationToBottom(); 106 textMapperR->GetTextProperty()->SetColor(0, 0, 1); 107 vtkActor2D *textActorR = vtkActor2D::New(); 108 textActorR->SetMapper(textMapperR); 109 textActorR->GetPositionCoordinate()->SetCoordinateSystemToNormalizedDisplay(); 110 textActorR->GetPositionCoordinate()->SetValue(0.95, 0.5); 111 112 //Draw the grid to demonstrate the placement of the text. 113 114 //Set up the necessary points. 115 vtkPoints *Pts = vtkPoints::New(); 116 Pts->InsertNextPoint(0.05, 0.0, 0.0); 117 Pts->InsertNextPoint(0.05, 1.0, 0.0); 118 Pts->InsertNextPoint(0.5, 0.0, 0.0); 119 Pts->InsertNextPoint(0.5, 1.0, 0.0); 120 Pts->InsertNextPoint(0.95, 0.0, 0.0); 121 Pts->InsertNextPoint(0.95, 1.0, 0.0); 122 Pts->InsertNextPoint(0.0, 0.5, 0.0); 123 Pts->InsertNextPoint(1.0, 0.5, 0.0); 124 Pts->InsertNextPoint(0.00, 0.85, 0.0); 125 Pts->InsertNextPoint(0.50, 0.85, 0.0); 126 Pts->InsertNextPoint(0.00, 0.75, 0.0); 127 Pts->InsertNextPoint(0.50, 0.75, 0.0); 128 Pts->InsertNextPoint(0.00, 0.65, 0.0); 129 Pts->InsertNextPoint(0.50, 0.65, 0.0); 130 131 //Set up the lines that use these points. 132 int i; 133 static vtkIdType m_pts[7][2] = { { 0, 1 },{ 2, 3 },{ 4, 5 },{ 6, 7 },{ 8, 9 }, 134 { 10, 11 } ,{ 12, 13 } }; 135 vtkCellArray *Lines = vtkCellArray::New(); 136 for (i = 0; i < 7; i++) Lines->InsertNextCell(2, m_pts[i]); 137 138 //Create a grid that uses these points and lines. 139 vtkPolyData *Grid = vtkPolyData::New(); 140 Grid->SetPoints(Pts); 141 Grid->SetLines(Lines); 142 143 //Set up the coordinate system. 144 vtkCoordinate *normCoords = vtkCoordinate::New(); 145 normCoords->SetCoordinateSystemToNormalizedViewport(); 146 147 //Set up the mapper and actor (2D) for the grid. 148 vtkPolyDataMapper2D *mapper = vtkPolyDataMapper2D::New(); 149 mapper->SetInputData(Grid); 150 mapper->SetTransformCoordinate(normCoords); 151 vtkActor2D *gridActor = vtkActor2D::New(); 152 gridActor->SetMapper(mapper); 153 gridActor->GetProperty()->SetColor(0.1, 0.1, 0.1); 154 155 //Create the Renderer, RenderWindow, and RenderWindowInteractor 156 vtkRenderer *ren1 = vtkRenderer::New(); 157 vtkRenderWindow *renWin = vtkRenderWindow::New(); 158 renWin->AddRenderer(ren1); 159 vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New(); 160 iren->SetRenderWindow(renWin); 161 162 //Add the actors to the renderer; set the background and size; zoom in 163 // closer to the image; render 164 ren1->AddActor2D(textActorL); 165 ren1->AddActor2D(textActorC); 166 ren1->AddActor2D(textActorR); 167 ren1->AddActor2D(singleLineTextActorB); 168 ren1->AddActor2D(singleLineTextActorC); 169 ren1->AddActor2D(singleLineTextActorT); 170 ren1->AddActor2D(gridActor); 171 172 ren1->SetBackground(1, 1, 1); 173 renWin->SetSize(500, 300); 174 ren1->GetActiveCamera()->Zoom(1.5); 175 renWin->Render(); 176 iren->Start(); 177 178 singleLineTextProp->Delete(); 179 multiLineTextProp->Delete(); 180 singleLineTextB->Delete(); 181 singleLineTextActorB->Delete(); 182 singleLineTextC->Delete(); 183 singleLineTextActorC->Delete(); 184 singleLineTextT->Delete(); 185 singleLineTextActorT->Delete(); 186 textMapperL->Delete(); 187 textActorL->Delete(); 188 textMapperC->Delete(); 189 textActorC->Delete(); 190 textMapperR->Delete(); 191 textActorR->Delete(); 192 Pts->Delete(); 193 Lines->Delete(); 194 Grid->Delete(); 195 normCoords->Delete(); 196 mapper->Delete(); 197 gridActor->Delete(); 198 ren1->Delete(); 199 renWin->Delete(); 200 iren->Delete(); 201 202 singleLineTextProp = NULL; 203 multiLineTextProp = NULL; 204 singleLineTextB = NULL; 205 singleLineTextActorB = NULL; 206 singleLineTextC = NULL; 207 singleLineTextActorC = NULL; 208 singleLineTextT = NULL; 209 singleLineTextActorT = NULL; 210 textMapperL = NULL; 211 textActorL = NULL; 212 textMapperC = NULL; 213 textActorC = NULL; 214 textMapperR = NULL; 215 textActorR = NULL; 216 Pts = NULL; 217 Lines = NULL; 218 Grid = NULL; 219 normCoords = NULL; 220 mapper = NULL; 221 gridActor = NULL; 222 ren1 = NULL; 223 renWin = NULL; 224 iren = NULL; 225 226 return 0; 227 }
不要迷恋哥,哥只是一段代码。