-
Notifications
You must be signed in to change notification settings - Fork 0
/
simCamera.cpp
208 lines (162 loc) · 6.24 KB
/
simCamera.cpp
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
#include "simCamera.hpp"
#include "normalRandom.hpp"
#include <stdio.h>
#include <math.h>
#include <iostream>
SimCamera::SimCamera(){}
SimCamera::SimCamera(SimScene simScene)
{
camPositionNoiseMean(0,0) = 0.0;
camPositionNoiseMean(1,0) = 0.0;
camPositionNoiseMean(2,0) = 0.0;
camPositionNoiseVariance(0,0) = 1.0;
camPositionNoiseVariance(1,0) = 1.0;
camPositionNoiseVariance(2,0) = 1.0;
camAccelerationNoiseMean(0,0) = 0.0;
camAccelerationNoiseMean(1,0) = 0.0;
camAccelerationNoiseMean(2,0) = 0.0;
camAccelerationNoiseVariance(0,0) = 0.0000000000000001;
camAccelerationNoiseVariance(1,0) = 0.0000000000000001;
camAccelerationNoiseVariance(2,0) = 0.0000000000000001;
camMeasurementNoiseMean(0,0) = 0.0;
camMeasurementNoiseMean(1,0) = 0.0;
camMeasurementNoiseMean(2,0) = 0.0;
camMeasurementNoiseVariance(0,0) = 0.0000000000000001;
camMeasurementNoiseVariance(1,0) = 0.0000000000000001;
camMeasurementNoiseVariance(2,0) = 0.0000000000000001;
camDirection(0,0) = 1.0;
camDirection(1,0) = 0.0;
camDirection(2,0) = 0.0;
//GOOD SET
// defaultInverseDepth = 0.005;
// defaultTimeStep = 0.01;
defaultInverseDepth = 0.005;
defaultTimeStep = 0.01;
reset();
initializeMap(simScene);
}
void SimCamera::initializeMap(SimScene simScene)
{
for (int i=0; i<simScene.landmarks.size(); i++)
{
Landmark landmark(camPosition,
simScene.landmarks.at(i) - camPosition,
defaultInverseDepth);
map.push_back(landmark);
}
}
double SimCamera::angleV2V(Eigen::Vector3d v1, Eigen::Vector3d v2)
{
v1.normalize();
v2.normalize();
double angle = acos(v1.dot(v2));
if (v2(1,0) < v1(1,0))
{
angle *= -1.0;
}
/*
printf("angle = %f in radians\n", angle);
printf("angle = %f in degrees\n", angle * 180.0/3.1415927);
*/
return angle;
}
void SimCamera::reset()
{
currTime = 0.0;
camInitialVelocity(0,0) = 0.0;
camInitialVelocity(1,0) = 0.0;
camInitialVelocity(2,0) = 0.0;
camInitialPosition(0,0) = -1.0;
camInitialPosition(1,0) = 100.0;
camInitialPosition(2,0) = 0.0;
camPosition = camInitialPosition;
camVelocity = camInitialVelocity;
camAcceleration(0,0) = 0.0;
camAcceleration(1,0) = -9.81;
camAcceleration(2,0) = 0.0;
}
void SimCamera::timeStep()
{
currTime += defaultTimeStep;
camVelocity = (camAcceleration * currTime) + camInitialVelocity;
camPosition =
(0.5 * camAcceleration * (currTime * currTime)) +
(camInitialVelocity * currTime) +
camInitialPosition;
//std::cout << "Noiseless camera position:" << std::endl;
//std::cout << camPosition << std::endl;
//std::cout << "acceleration before noise:" << std::endl;
//std::cout << camAcceleration << std::endl;
addNoise2Acceleration();
//std::cout << "acceleration after noise:" << std::endl;
//std::cout << camAcceleration << std::endl;
camVelocity = (camAcceleration * currTime) + camInitialVelocity;
camPosition =
(0.5 * camAcceleration * (currTime * currTime)) +
(camInitialVelocity * currTime) +
camInitialPosition;
//std::cout << "Noisy camera position:" << std::endl;
//std::cout << camPosition << std::endl;
//Reset acceleration
camAcceleration(0,0) = 0.0;
camAcceleration(1,0) = -9.81;
camAcceleration(2,0) = 0.0;
}
void SimCamera::addNoise2Acceleration()
{
double noiseX = generateNoise(camAccelerationNoiseMean(0,0), camAccelerationNoiseVariance(0,0));
double noiseY = generateNoise(camAccelerationNoiseMean(1,0), camAccelerationNoiseVariance(1,0));
double noiseZ = generateNoise(camAccelerationNoiseMean(2,0), camAccelerationNoiseVariance(2,0));
//Only add noise to Y value for 2d simulation
camAcceleration(1,0) = camAcceleration(1,0) + noiseY;
printf("ACTUAL ACCELERATION = %.10f\n", camAcceleration(1,0));
}
void SimCamera::addNoise2Position()
{
double noiseX = generateNoise(camPositionNoiseMean(0,0), camPositionNoiseVariance(0,0));
double noiseY = generateNoise(camPositionNoiseMean(1,0), camPositionNoiseVariance(1,0));
double noiseZ = generateNoise(camPositionNoiseMean(2,0), camPositionNoiseVariance(2,0));
//printf("Perfect Position = %f\n", camPosition(1,0));
//Only put noise on Y value for 2d simulation
camPosition(1,0) = camPosition(1,0) + noiseY;
//printf("Noisy Position = %f\n", camPosition(1,0));
}
Eigen::VectorXd SimCamera::measure(SimScene simScene)
{
//Assume all landmarks observed and 1D observations
Eigen::VectorXd observations(simScene.landmarks.size());
for (int i=0; i<simScene.landmarks.size(); i++)
{
double slope = (simScene.landmarks.at(i)(1,0) - camPosition(1,0)) /
(simScene.landmarks.at(i)(0,0) - camPosition(0,0));
double yIntercept = camPosition(1,0) - (slope * camPosition(0,0));
Eigen::Vector3d imagePoint(0.0, yIntercept, 0.0);
imagePoint = imagePoint - camPosition;
/*
printf("Imagepoint %d\n", i);
std::cout << imagePoint << std::endl;
*/
addNoise2Measurement(imagePoint);
/*
printf("Imagepoint after noise\n");
std::cout << imagePoint << std::endl;
*/
observations(i,0) = imagePoint(1,0); //Observation = y value of projection
}
//printf("Observations:\n");
//std::cout << observations << std::endl;
return observations;
}
void SimCamera::addNoise2Measurement(Eigen::Vector3d& imagePoint)
{
double noiseX = generateNoise(camMeasurementNoiseMean(0,0), camMeasurementNoiseVariance(0,0));
double noiseY = generateNoise(camMeasurementNoiseMean(1,0), camMeasurementNoiseVariance(1,0));
double noiseZ = generateNoise(camMeasurementNoiseMean(2,0), camMeasurementNoiseVariance(2,0));
//Only add noise to y dimension for 2d example
imagePoint(1,0) = imagePoint(1,0) + noiseY;
}
Eigen::Vector3d SimCamera::position()
{
printf("Simulated Camera Position: (%f, %f)\n", camPosition(0,0), camPosition(1,0));
return camPosition;
}