-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVelocity_field.cpp
More file actions
160 lines (123 loc) · 4.68 KB
/
Velocity_field.cpp
File metadata and controls
160 lines (123 loc) · 4.68 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
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
#include <vtkVersion.h>
#include <vtkArrowSource.h>
#include <vtkCellArray.h>
#include <vtkGlyph2D.h>
#include <vtkImageData.h>
#include <vtkImageSliceMapper.h>
#include <vtkImageSlice.h>
#include <vtkInteractorStyleImage.h>
#include <vtkPoints.h>
#include <vtkSmartPointer.h>
#include <vtkMath.h>
#include <vtkFloatArray.h>
#include <vtkDoubleArray.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkXMLImageDataWriter.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkFieldData.h>
#include <stdio.h>
#include <string.h>
int getIndex(int z, int y, int x, int* dataDims)
{
return (z*dataDims[1]*dataDims[0]+ y*dataDims[0]+x);
}
int main()
{
float t, limit, accelLeft, accelRight, boundaryVelLeft, boundaryVelRight = 0.;
int ind = 0;
std::cout<< "Enter value of time limit" << std::endl;
std::cin>>limit;
std::cout<< "Enter value of acceleration from Left " << std::endl;
std::cin>>accelLeft;
std::cout<< "Enter value of acceleration from Right " << std::endl;
std::cin>>accelRight;
//// multiplying with -1 since the acceleration is in opposite direction
accelRight = accelRight * ( - 1.);
int dims[3] = {201, 101, 2};
double spacing[3] = {0.01, 0.01, 0.01};
double extent[3] = {0.};
extent[0] = (dims[0] - 1)* spacing[0];
extent[1] = (dims[1] - 1)* spacing[1];
extent[2] = (dims[2] - 1)* spacing[2];
float x, y = 0.;
while ( t < limit )
{
///initialising image data and dimensions
vtkSmartPointer <vtkImageData> imageData = vtkSmartPointer<vtkImageData>::New();
imageData->SetDimensions(dims);
imageData->SetSpacing(spacing);
/*
#if VTK_MAJOR_VERSION <= 5
imageData->SetNumberOfScalarComponents(3);
imageData->SetScalarTypeToDouble();
imageData->AllocateScalars();
#else
imageData->AllocateScalars(VTK_FLOAT,3);
#endif
*/
vtkSmartPointer <vtkFloatArray> scalars = vtkSmartPointer<vtkFloatArray>::New();
int numPoints = dims[0] * dims[1] * dims[2];
scalars->SetNumberOfComponents(3);
scalars->SetNumberOfTuples(numPoints);
scalars->SetName("Scalars");
//float jacobian[4] = {0, 0, 0, 2.};
for (float k = 0.; k < dims[2]; k++) {
for (float j = 0.; j < dims[1]; j++) {
for (float i = 0.; i < dims[0]; i++) {
int index1;
index1 = getIndex(k, j, i, dims);
double interpolator = i / ( dims[0] - 1 );
x = ( accelLeft * ( 1 - interpolator ) + accelRight * interpolator ) * t; ////have to interpolate using acceleration from both sides
y = ( -1 * ( dims[1] - 1 ) / 2 + j ) * 2. / 100;
//std::cout << "velocity " << x <<" " << y << std::endl;
///storing the values in pixel
float tuple[3];
tuple[0] = x;
tuple[1] = y;
tuple[2] = 0.0;
assert(index1 < dims[0] * dims[1] * dims[2]);
scalars->SetTuple(index1, tuple);
/*
//std::cout<< i << ", " << j << " " << y << std::endl;
float* pixel = static_cast<float*>(imageData->GetScalarPointer(i,j,k));
//std::cout<<"Ptr: " << pixel << endl;
pixel[0] = x;
pixel[1] = 0.0;
pixel[2] = 0.0;
*/
}
}
}
imageData->GetPointData()->AddArray(scalars);
std::stringstream index;
index << ind;
std::string filename = "VelocityField_";
std::string filename2 = ".vti";
std::string path = filename + std::string(index.str()) + filename2;
///wrting imagedata to file
vtkSmartPointer <vtkXMLImageDataWriter> writer = vtkSmartPointer<vtkXMLImageDataWriter>::New();
#if VTK_MAJOR_VERSION <= 5
writer->SetInputConnection(imageData->GetProducerPort());
#else
writer->SetInputData(imageData);
#endif
writer->SetFileName(path.c_str());
writer->Write();
boundaryVelLeft = accelLeft * t;
boundaryVelRight = accelRight * t;
extent[0] = extent[0] - ( boundaryVelLeft - boundaryVelRight ) * t;
spacing[0] = extent[0] / (dims[0] - 1);
//std::cout << extent[0] << " " << spacing[0] <<std::endl;
if ( extent[0] <= 0 )
{
std::cerr<<" Wrong acceleration values " <<std::endl;
break;
}
ind++;
t = t + 0.05;
}
return EXIT_SUCCESS;
}