-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexample_vectorfield.cpp
More file actions
132 lines (108 loc) · 3.77 KB
/
example_vectorfield.cpp
File metadata and controls
132 lines (108 loc) · 3.77 KB
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
#include "vtkActor.h"
#include "vtkArrowSource.h"
#include "vtkFloatArray.h"
#include <vtkDataArray.h>
#include "vtkDataSetMapper.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include <vtkGlyph3D.h>
#include "vtkPolyData.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSmartPointer.h"
#include "vtkStructuredGrid.h"
#include "vtkStructuredPoints.h"
#include "vtkUnsignedCharArray.h"
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
int main()
{
/* Arrow */
vtkSmartPointer<vtkArrowSource> arrow =
vtkSmartPointer<vtkArrowSource>::New();
/* Glyph - object to visualize vector field */
vtkSmartPointer<vtkPoints> P =
vtkSmartPointer<vtkPoints>::New();
P->InsertNextPoint(0.0, 0.0, 0.0);
P->InsertNextPoint(0.0, 0.0, 2.0);
P->InsertNextPoint(0.0, 2.0, 0.0);
P->InsertNextPoint(0.0, 2.0, 2.0);
P->InsertNextPoint(2.0, 0.0, 0.0);
P->InsertNextPoint(2.0, 0.0, 2.0);
P->InsertNextPoint(2.0, 2.0, 0.0);
P->InsertNextPoint(2.0, 2.0, 2.0);
P->InsertNextPoint(1.0, 1.0, 1.0);
P->InsertNextPoint(1.0, 1.0, 1.0);
P->InsertNextPoint(1.0, 1.0, 1.0);
vtkSmartPointer<vtkFloatArray> directions =
vtkSmartPointer<vtkFloatArray>::New();
directions->SetNumberOfComponents(3);
directions->InsertNextTuple3(-1.0, -1.0, -1.0);
directions->InsertNextTuple3(-1.0, -1.0, 1.0);
directions->InsertNextTuple3(-1.0, 1.0, -1.0);
directions->InsertNextTuple3(-1.0, 1.0, 1.0);
directions->InsertNextTuple3(1.0, -1.0, -1.0);
directions->InsertNextTuple3(1.0, -1.0, 1.0);
directions->InsertNextTuple3(1.0, 1.0, -1.0);
directions->InsertNextTuple3(1.0, 1.0, 1.0);
directions->InsertNextTuple3(-1.0, 0.01, 0.01);
directions->InsertNextTuple3(0.01, 2.0, 0.01);
directions->InsertNextTuple3(0.01, 0.01, 2.5);
vtkSmartPointer<vtkUnsignedCharArray> colors =
vtkSmartPointer<vtkUnsignedCharArray>::New();
colors->SetNumberOfComponents(3);
unsigned char r[3] = { 255,0,0 };
unsigned char g[3] = { 0,255,0 };
unsigned char b[3] = { 0,0,255 };
colors->InsertNextTypedTuple(r);
colors->InsertNextTypedTuple(g);
colors->InsertNextTypedTuple(b);
colors->InsertNextTypedTuple(r);
colors->InsertNextTypedTuple(g);
colors->InsertNextTypedTuple(b);
colors->InsertNextTypedTuple(r);
colors->InsertNextTypedTuple(g);
colors->InsertNextTypedTuple(b);
colors->InsertNextTypedTuple(r);
colors->InsertNextTypedTuple(g);
vtkSmartPointer<vtkPolyData> polydata =
vtkSmartPointer<vtkPolyData>::New();
polydata->SetPoints(P);
polydata->GetPointData()->SetScalars(colors);
polydata->GetPointData()->SetVectors(directions);
/* Glyph setup */
vtkSmartPointer<vtkGlyph3D> glyph =
vtkSmartPointer<vtkGlyph3D>::New();
glyph->SetSourceConnection(arrow->GetOutputPort());
glyph->SetInputData(polydata);
//glyph->ScalingOff();
//glyph->SetScaleMode(VTK_SCALE_BY_VECTORCOMPONENTS);
//glyph->SetScaling(1.1);
//glyph->SetColorModeToColorByScalar();
glyph->SetColorModeToColorByVector();
//glyph->SetVectorModeToUseVectors();
glyph->OrientOn();
glyph->Update();
vtkSmartPointer<vtkDataSetMapper> mapper =
vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInputConnection(glyph->GetOutputPort());
mapper->ScalarVisibilityOn();
mapper->SetScalarRange(1.0, 2.5);
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> interactor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(renderWindow);
renderWindow->Render();
interactor->Start();
return 0;
}