-
Notifications
You must be signed in to change notification settings - Fork 70
/
renderer.cpp
302 lines (268 loc) · 11.7 KB
/
renderer.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "ksp_plugin/renderer.hpp"
#include "geometry/grassmann.hpp"
#include "geometry/named_quantities.hpp"
#include "physics/apsides.hpp"
#include "physics/body_centred_body_direction_dynamic_frame.hpp"
#include "physics/degrees_of_freedom.hpp"
namespace principia {
namespace ksp_plugin {
namespace internal_renderer {
using base::make_not_null_unique;
using geometry::AngularVelocity;
using geometry::RigidTransformation;
using geometry::Vector;
using geometry::Velocity;
using physics::BodyCentredBodyDirectionDynamicFrame;
using physics::ComputeApsides;
using physics::ComputeNodes;
using physics::DegreesOfFreedom;
Renderer::Renderer(not_null<Celestial const*> const sun,
not_null<std::unique_ptr<NavigationFrame>> plotting_frame)
: sun_(sun),
plotting_frame_(std::move(plotting_frame)) {}
void Renderer::SetPlottingFrame(
not_null<std::unique_ptr<NavigationFrame>> plotting_frame) {
plotting_frame_ = std::move(plotting_frame);
}
not_null<NavigationFrame const*> Renderer::GetPlottingFrame() const {
return target_ ? target_->target_frame.get()
: plotting_frame_.get();
}
void Renderer::SetTargetVessel(
not_null<Vessel*> const vessel,
not_null<Celestial const*> const celestial,
not_null<Ephemeris<Barycentric> const*> const ephemeris) {
CHECK(!vessel->prediction().Empty());
if (!target_ ||
target_->vessel != vessel ||
target_->celestial != celestial) {
target_.emplace(vessel, celestial, ephemeris);
}
}
void Renderer::ClearTargetVessel() {
target_ = std::experimental::nullopt;
}
void Renderer::ClearTargetVesselIf(not_null<Vessel*> const vessel) {
if (target_ && target_->vessel == vessel) {
target_ = std::experimental::nullopt;
}
}
bool Renderer::HasTargetVessel() const {
return static_cast<bool>(target_);
}
Vessel& Renderer::GetTargetVessel() {
CHECK(target_);
return *target_->vessel;
}
Vessel const& Renderer::GetTargetVessel() const {
CHECK(target_);
return *target_->vessel;
}
not_null<std::unique_ptr<DiscreteTrajectory<World>>>
Renderer::RenderBarycentricTrajectoryInWorld(
Instant const& time,
DiscreteTrajectory<Barycentric>::Iterator const& begin,
DiscreteTrajectory<Barycentric>::Iterator const& end,
Position<World> const& sun_world_position,
Rotation<Barycentric, AliceSun> const& planetarium_rotation) const {
auto const trajectory_in_plotting_frame =
RenderBarycentricTrajectoryInPlotting(begin, end);
auto trajectory_in_world =
RenderPlottingTrajectoryInWorld(time,
trajectory_in_plotting_frame->Begin(),
trajectory_in_plotting_frame->End(),
sun_world_position,
planetarium_rotation);
return trajectory_in_world;
}
not_null<std::unique_ptr<DiscreteTrajectory<Navigation>>>
Renderer::RenderBarycentricTrajectoryInPlotting(
DiscreteTrajectory<Barycentric>::Iterator const& begin,
DiscreteTrajectory<Barycentric>::Iterator const& end) const {
CHECK(!target_ || !target_->vessel->prediction().Empty());
auto trajectory = make_not_null_unique<DiscreteTrajectory<Navigation>>();
for (auto it = begin; it != end; ++it) {
if (target_) {
if (it.time() < target_->vessel->prediction().t_min()) {
continue;
} else if (it.time() > target_->vessel->prediction().t_max()) {
break;
}
}
trajectory->Append(
it.time(),
BarycentricToPlotting(it.time())(it.degrees_of_freedom()));
}
return trajectory;
}
not_null<std::unique_ptr<DiscreteTrajectory<World>>>
Renderer::RenderPlottingTrajectoryInWorld(
Instant const& time,
DiscreteTrajectory<Navigation>::Iterator const& begin,
DiscreteTrajectory<Navigation>::Iterator const& end,
Position<World> const& sun_world_position,
Rotation<Barycentric, AliceSun> const& planetarium_rotation) const {
auto trajectory = make_not_null_unique<DiscreteTrajectory<World>>();
// This function does unnatural things.
// - It identifies positions in the plotting frame with those of world using
// the rigid transformation at the current time, instead of transforming each
// position according to the transformation at its time. This hides the fact
// that we are considering an observer fixed in the plotting frame.
// - Instead of applying the full rigid motion and consistently transforming
// the velocities, or even just applying the orthogonal map, it simply
// identifies the coordinates of |World| with those of the plotting frame.
// This is because we are interested in the magnitude of the velocity (the
// speed) in the plotting frame, as well as the coordinates (in frames with a
// physically significant plane, the z coordinate becomes the out-of-plane
// velocity).
// The resulting |DegreesOfFreedom| should be seen as no more than a
// convenient hack to send a plottable position together with a velocity in
// the coordinates we want.
// TODO(phl): This will no longer be needed once we have support for
// projections; instead of these convenient lies we can simply say that the
// camera is fixed in the plotting frame and project there; additional data
// can be gathered from the velocities in the plotting frame as needed and
// sent directly to be shown in markers.
RigidTransformation<Navigation, World> const
from_plotting_frame_to_world_at_current_time =
PlottingToWorld(time, sun_world_position, planetarium_rotation);
for (auto it = begin; it != end; ++it) {
DegreesOfFreedom<Navigation> const& navigation_degrees_of_freedom =
it.degrees_of_freedom();
DegreesOfFreedom<World> const world_degrees_of_freedom = {
from_plotting_frame_to_world_at_current_time(
navigation_degrees_of_freedom.position()),
geometry::Identity<Navigation, World>{}(
navigation_degrees_of_freedom.velocity())};
trajectory->Append(it.time(), world_degrees_of_freedom);
}
return trajectory;
}
RigidMotion<Barycentric, Navigation> Renderer::BarycentricToPlotting(
Instant const& time) const {
return GetPlottingFrame()->ToThisFrameAtTime(time);
}
RigidTransformation<Barycentric, World> Renderer::BarycentricToWorld(
Instant const& time,
Position<World> const& sun_world_position,
Rotation<Barycentric, AliceSun> const& planetarium_rotation) const {
return RigidTransformation<Barycentric, World>(
sun_->current_position(time),
sun_world_position,
BarycentricToWorld(planetarium_rotation));
}
OrthogonalMap<Barycentric, World> Renderer::BarycentricToWorld(
Rotation<Barycentric, AliceSun> const& planetarium_rotation) const {
return OrthogonalMap<WorldSun, World>::Identity() *
BarycentricToWorldSun(planetarium_rotation);
}
OrthogonalMap<Barycentric, WorldSun> Renderer::BarycentricToWorldSun(
Rotation<Barycentric, AliceSun> const& planetarium_rotation) const {
return sun_looking_glass.Inverse().Forget() * planetarium_rotation.Forget();
}
OrthogonalMap<Frenet<Navigation>, World> Renderer::FrenetToWorld(
Instant const& time,
NavigationManœuvre const& manœuvre,
Rotation<Barycentric, AliceSun> const& planetarium_rotation) const {
Instant const initial_time = manœuvre.initial_time();
NavigationFrame const& plotting_frame = *GetPlottingFrame();
return PlottingToWorld(time, planetarium_rotation) *
BarycentricToPlotting(initial_time).orthogonal_map() *
manœuvre.FrenetFrame();
}
OrthogonalMap<Frenet<Navigation>, World> Renderer::FrenetToWorld(
Vessel const& vessel,
Rotation<Barycentric, AliceSun> const& planetarium_rotation) const {
auto const last = vessel.psychohistory().last();
Instant const& time = last.time();
DegreesOfFreedom<Barycentric> const& barycentric_degrees_of_freedom =
last.degrees_of_freedom();
DegreesOfFreedom<Navigation> const plotting_frame_degrees_of_freedom =
BarycentricToPlotting(time)(barycentric_degrees_of_freedom);
Rotation<Frenet<Navigation>, Navigation> const
frenet_frame_to_plotting_frame =
GetPlottingFrame()->FrenetFrame(time,
plotting_frame_degrees_of_freedom);
return PlottingToWorld(time, planetarium_rotation) *
frenet_frame_to_plotting_frame.Forget();
}
OrthogonalMap<Frenet<Navigation>, World> Renderer::FrenetToWorld(
Vessel const& vessel,
NavigationFrame const& navigation_frame,
Rotation<Barycentric, AliceSun> const& planetarium_rotation) const {
auto const vessel_psychohistory_last = vessel.psychohistory().last();
auto const to_navigation =
navigation_frame.ToThisFrameAtTime(vessel_psychohistory_last.time());
auto const from_navigation = to_navigation.orthogonal_map().Inverse();
auto const frenet_frame =
navigation_frame.FrenetFrame(
vessel_psychohistory_last.time(),
to_navigation(
vessel_psychohistory_last.degrees_of_freedom())).Forget();
return BarycentricToWorld(planetarium_rotation) * from_navigation *
frenet_frame;
}
OrthogonalMap<Navigation, Barycentric> Renderer::PlottingToBarycentric(
Instant const& time) const {
return GetPlottingFrame()->FromThisFrameAtTime(time).orthogonal_map();
}
RigidTransformation<Navigation, World> Renderer::PlottingToWorld(
Instant const& time,
Position<World> const& sun_world_position,
Rotation<Barycentric, AliceSun> const& planetarium_rotation) const {
return BarycentricToWorld(time, sun_world_position, planetarium_rotation) *
GetPlottingFrame()->FromThisFrameAtTime(time).rigid_transformation();
}
OrthogonalMap<Navigation, World> Renderer::PlottingToWorld(
Instant const& time,
Rotation<Barycentric, AliceSun> const& planetarium_rotation) const {
return BarycentricToWorld(planetarium_rotation) *
PlottingToBarycentric(time);
}
RigidTransformation<World, Barycentric> Renderer::WorldToBarycentric(
Instant const& time,
Position<World> const& sun_world_position,
Rotation<Barycentric, AliceSun> const& planetarium_rotation) const {
return BarycentricToWorld(time, sun_world_position, planetarium_rotation)
.Inverse();
}
OrthogonalMap<World, Barycentric> Renderer::WorldToBarycentric(
Rotation<Barycentric, AliceSun> const& planetarium_rotation) const {
return BarycentricToWorld(planetarium_rotation).Inverse();
}
RigidTransformation<World, Navigation> Renderer::WorldToPlotting(
Instant const& time,
Position<World> const& sun_world_position,
Rotation<Barycentric, AliceSun> const& planetarium_rotation) const {
return BarycentricToPlotting(time).rigid_transformation() *
WorldToBarycentric(time, sun_world_position, planetarium_rotation);
}
void Renderer::WriteToMessage(
not_null<serialization::Renderer*> message) const {
plotting_frame_->WriteToMessage(message->mutable_plotting_frame());
// No serialization of the |target_|.
}
not_null<std::unique_ptr<Renderer>> Renderer::ReadFromMessage(
serialization::Renderer const& message,
not_null<Celestial const*> sun,
not_null<Ephemeris<Barycentric> const*> const ephemeris) {
return make_not_null_unique<Renderer>(
sun,
NavigationFrame::ReadFromMessage(message.plotting_frame(), ephemeris));
}
Renderer::Target::Target(
not_null<Vessel*> const vessel,
not_null<Celestial const*> const celestial,
not_null<Ephemeris<Barycentric> const*> const ephemeris)
: vessel(vessel),
celestial(celestial),
target_frame(
make_not_null_unique<
BodyCentredBodyDirectionDynamicFrame<Barycentric, Navigation>>(
ephemeris,
[this]() -> auto& { return this->vessel->prediction(); },
celestial->body())) {}
} // namespace internal_renderer
} // namespace ksp_plugin
} // namespace principia