Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mockingbirdnest/Principia
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 6a7e2698470b
Choose a base ref
...
head repository: mockingbirdnest/Principia
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: f3d4abd8470d
Choose a head ref
  • 9 commits
  • 6 files changed
  • 1 contributor

Commits on Nov 9, 2019

  1. Copy the full SHA
    fb76482 View commit details
  2. draw things

    eggrobin committed Nov 9, 2019
    Copy the full SHA
    0bb16fa View commit details
  3. hide instead of forgetting

    eggrobin committed Nov 9, 2019
    Copy the full SHA
    7d9e2a3 View commit details
  4. Copy the full SHA
    5f5e195 View commit details
  5. follow KSP fading

    eggrobin committed Nov 9, 2019
    Copy the full SHA
    63ead11 View commit details
  6. Copy the full SHA
    a5800ee View commit details

Commits on Nov 16, 2019

  1. after pleroy's review

    eggrobin committed Nov 16, 2019
    Copy the full SHA
    fccbd8b View commit details
  2. lint

    eggrobin committed Nov 16, 2019
    Copy the full SHA
    6417ba9 View commit details
  3. Merge pull request #2370 from eggrobin/celestial-trajectory-plotting

    Celestial trajectory plotting
    eggrobin authored Nov 16, 2019
    Copy the full SHA
    f3d4abd View commit details
Showing with 245 additions and 34 deletions.
  1. +92 −11 ksp_plugin/interface_planetarium.cpp
  2. +17 −8 ksp_plugin/planetarium.cpp
  3. +12 −0 ksp_plugin/planetarium.hpp
  4. +5 −2 ksp_plugin_adapter/gl_lines.cs
  5. +74 −12 ksp_plugin_adapter/ksp_plugin_adapter.cs
  6. +45 −1 serialization/journal.proto
103 changes: 92 additions & 11 deletions ksp_plugin/interface_planetarium.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

#include "ksp_plugin/interface.hpp"

#include <algorithm>

#include "geometry/affine_map.hpp"
#include "geometry/grassmann.hpp"
#include "geometry/named_quantities.hpp"
@@ -189,11 +191,10 @@ Iterator* principia__PlanetariumPlotPsychohistory(
Planetarium const* const planetarium,
Plugin const* const plugin,
int const method,
char const* const vessel_guid) {
journal::Method<journal::PlanetariumPlotPsychohistory> m({planetarium,
plugin,
method,
vessel_guid});
char const* const vessel_guid,
double const max_history_length) {
journal::Method<journal::PlanetariumPlotPsychohistory> m(
{planetarium, plugin, method, vessel_guid, max_history_length});
CHECK_NOTNULL(plugin);
CHECK_NOTNULL(planetarium);

@@ -203,12 +204,92 @@ Iterator* principia__PlanetariumPlotPsychohistory(
return m.Return(new TypedIterator<RP2Lines<Length, Camera>>({}));
} else {
auto const& psychohistory = plugin->GetVessel(vessel_guid)->psychohistory();
auto const rp2_lines = PlotMethodN(*planetarium,
method,
psychohistory.begin(),
psychohistory.end(),
plugin->CurrentTime(),
/*reverse=*/true);
auto const rp2_lines =
PlotMethodN(*planetarium,
method,
psychohistory.LowerBound(plugin->CurrentTime() -
max_history_length * Second),
psychohistory.end(),
plugin->CurrentTime(),
/*reverse=*/true);
return m.Return(new TypedIterator<RP2Lines<Length, Camera>>(rp2_lines));
}
}

// Returns an iterator for the rendered past trajectory of the celestial with
// the given index; the trajectory goes back as far as the history of the vessel
// with the given GUID, or, if no vessel is provided, up to |max_history_length|
// seconds before the present time.
Iterator* principia__PlanetariumPlotCelestialTrajectoryForPsychohistory(
Planetarium const* const planetarium,
Plugin const* const plugin,
int const celestial_index,
char const* const vessel_guid,
double const max_history_length) {
journal::Method<journal::PlanetariumPlotCelestialTrajectoryForPsychohistory>
m({planetarium,
plugin,
celestial_index,
vessel_guid,
max_history_length});
CHECK_NOTNULL(plugin);
CHECK_NOTNULL(planetarium);

// Do not plot the past when there is a target vessel as it is misleading.
if (plugin->renderer().HasTargetVessel()) {
return m.Return(new TypedIterator<RP2Lines<Length, Camera>>({}));
} else {
auto const& celestial_trajectory =
plugin->GetCelestial(celestial_index).trajectory();
Instant const first_time = std::max(
plugin->CurrentTime() - max_history_length * Second,
vessel_guid == nullptr
? celestial_trajectory.t_min()
: plugin->GetVessel(vessel_guid)->psychohistory().t_min());
auto const rp2_lines =
planetarium->PlotMethod2(celestial_trajectory,
first_time,
/*last_time=*/plugin->CurrentTime(),
/*now=*/plugin->CurrentTime(),
/*reverse=*/true);
return m.Return(new TypedIterator<RP2Lines<Length, Camera>>(rp2_lines));
}
}

// Returns an iterator for the rendered future trajectory of the celestial with
// the given index; the trajectory goes as far as the furthest of the final time
// of the prediction or that of the flight plan.
Iterator*
principia__PlanetariumPlotCelestialTrajectoryForPredictionOrFlightPlan(
Planetarium const* const planetarium,
Plugin const* const plugin,
int const celestial_index,
char const* const vessel_guid) {
journal::Method<
journal::PlanetariumPlotCelestialTrajectoryForPredictionOrFlightPlan>
m({planetarium, plugin, celestial_index, vessel_guid});
CHECK_NOTNULL(plugin);
CHECK_NOTNULL(planetarium);

// Do not plot the past when there is a target vessel as it is misleading.
if (plugin->renderer().HasTargetVessel()) {
return m.Return(new TypedIterator<RP2Lines<Length, Camera>>({}));
} else {
auto const& vessel = *plugin->GetVessel(vessel_guid);
Instant const prediction_final_time = vessel.prediction().t_max();
Instant const final_time =
vessel.has_flight_plan()
? std::max(vessel.flight_plan().actual_final_time(),
prediction_final_time)
: prediction_final_time;
auto const& celestial_trajectory =
plugin->GetCelestial(celestial_index).trajectory();
auto const rp2_lines =
planetarium->PlotMethod2(celestial_trajectory,
/*first_time=*/plugin->CurrentTime(),
/*last_time=*/final_time,
/*now=*/plugin->CurrentTime(),
/*reverse=*/false);
return m.Return(new TypedIterator<RP2Lines<Length, Camera>>(rp2_lines));
}
}
25 changes: 17 additions & 8 deletions ksp_plugin/planetarium.cpp
Original file line number Diff line number Diff line change
@@ -138,21 +138,30 @@ RP2Lines<Length, Camera> Planetarium::PlotMethod2(
DiscreteTrajectory<Barycentric>::Iterator const& end,
Instant const& now,
bool const reverse) const {
RP2Lines<Length, Camera> lines;
if (begin == end) {
return lines;
return {};
}
auto last = end;
--last;

double const tan²_angular_resolution =
Pow<2>(parameters_.tan_angular_resolution_);
auto const plottable_spheres = ComputePlottableSpheres(now);
auto const& trajectory = *begin.trajectory();
auto const begin_time = std::max(begin->time, plotting_frame_->t_min());
auto const last_time = std::min(last->time, plotting_frame_->t_max());
auto const final_time = reverse ? begin_time : last_time;
auto previous_time = reverse ? last_time : begin_time;
return PlotMethod2(trajectory, begin_time, last_time, now, reverse);
}

RP2Lines<Length, Camera> Planetarium::PlotMethod2(
Trajectory<Barycentric> const& trajectory,
Instant const& first_time,
Instant const& last_time,
Instant const& now,
bool const reverse) const {
RP2Lines<Length, Camera> lines;
auto const plottable_spheres = ComputePlottableSpheres(now);
double const tan²_angular_resolution =
Pow<2>(parameters_.tan_angular_resolution_);
auto const final_time = reverse ? first_time : last_time;
auto previous_time = reverse ? last_time : first_time;

Sign const direction = reverse ? Sign(-1) : Sign(1);
if (direction * (final_time - previous_time) <= Time{}) {
return lines;
12 changes: 12 additions & 0 deletions ksp_plugin/planetarium.hpp
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ using physics::DegreesOfFreedom;
using physics::DiscreteTrajectory;
using physics::Ephemeris;
using physics::RigidMotion;
using physics::Trajectory;
using quantities::Angle;
using quantities::Length;

@@ -82,12 +83,23 @@ class Planetarium {
Instant const& now,
bool reverse) const;

// A method that plots the cubic Hermite spline interpolating the trajectory,
// using an adaptive step size to keep the error between the straight segments
// and the actual spline below and close to the angular resolution.
RP2Lines<Length, Camera> PlotMethod2(
DiscreteTrajectory<Barycentric>::Iterator const& begin,
DiscreteTrajectory<Barycentric>::Iterator const& end,
Instant const& now,
bool reverse) const;

// The same method, operating on the |Trajectory| interface.
RP2Lines<Length, Camera> PlotMethod2(
Trajectory<Barycentric> const& trajectory,
Instant const& first_time,
Instant const& last_time,
Instant const& now,
bool reverse) const;

private:
// Computes the coordinates of the spheres that represent the |ephemeris_|
// bodies. These coordinates are in the |plotting_frame_| at time |now|.
7 changes: 5 additions & 2 deletions ksp_plugin_adapter/gl_lines.cs
Original file line number Diff line number Diff line change
@@ -109,8 +109,11 @@ public static void PlotRP2Lines(DisposableIterator rp2_lines_iterator,
rp2_line_iterator.IteratorGetRP2LineXY());
if (previous_rp2_point.HasValue) {
if (style == Style.Faded) {
colour.a = 1 - (float)(4 * index) / (float)(5 * size);
UnityEngine.GL.Color(colour);
var faded_colour = colour;
// Fade from the opacity of |colour| (when index = 0) down to 1/4
// of that opacity.
faded_colour.a *= 1 - (float)(4 * index) / (float)(5 * size);
UnityEngine.GL.Color(faded_colour);
}
if (style != Style.Dashed || index % 2 == 1) {
UnityEngine.GL.Vertex3((float)previous_rp2_point.Value.x,
86 changes: 74 additions & 12 deletions ksp_plugin_adapter/ksp_plugin_adapter.cs
Original file line number Diff line number Diff line change
@@ -862,8 +862,6 @@ private void FixedUpdate() {
(FlightGlobals.currentMainBody
?? FlightGlobals.GetHomeBody()).flightGlobalsIndex);

plugin_.ForgetAllHistoriesBefore(plugin_.CurrentTime() -
main_window_.history_length);
// TODO(egg): Set the degrees of freedom of the origin of |World| (by
// toying with Krakensbane and FloatingOrigin) here.

@@ -1677,6 +1675,16 @@ private void RotateGalaxyCube() {
}
}

private void RemoveStockTrajectoriesIfNeeded(CelestialBody celestial) {
if (celestial.orbitDriver == null) {
return;
}
celestial.orbitDriver.Renderer.drawMode =
main_window_.display_patched_conics
? OrbitRenderer.DrawMode.REDRAW_AND_RECALCULATE
: OrbitRenderer.DrawMode.OFF;
}

private void RemoveStockTrajectoriesIfNeeded(Vessel vessel) {
if (vessel.patchedConicRenderer != null) {
vessel.patchedConicRenderer.relativityMode =
@@ -1776,6 +1784,7 @@ private void RenderTrajectories() {
c => c.MapObject?.uiNode != null)) {
celestial.MapObject.uiNode.OnClick -= OnCelestialNodeClick;
celestial.MapObject.uiNode.OnClick += OnCelestialNodeClick;
RemoveStockTrajectoriesIfNeeded(celestial);
}
foreach (var vessel in FlightGlobals.Vessels.Where(
v => v.mapObject?.uiNode != null)) {
@@ -1785,24 +1794,28 @@ private void RenderTrajectories() {
vessel.mapObject.uiNode.OnClick += OnVesselNodeClick;
RemoveStockTrajectoriesIfNeeded(vessel);
}
Vessel main_vessel = PredictedVessel();
if (main_vessel == null) {
return;
string main_vessel_guid = PredictedVessel()?.id.ToString();
if (main_vessel_guid != null && !plugin_.HasVessel(main_vessel_guid)) {
main_vessel_guid = null;
}
string main_vessel_guid = main_vessel.id.ToString();
bool ready_to_draw_active_vessel_trajectory =
MapView.MapIsEnabled &&
plugin_.HasVessel(main_vessel_guid);
if (ready_to_draw_active_vessel_trajectory) {
if (MapView.MapIsEnabled) {
XYZ sun_world_position = (XYZ)Planetarium.fetch.Sun.position;
using (DisposablePlanetarium planetarium =
GLLines.NewPlanetarium(plugin_, sun_world_position)) {
GLLines.Draw(() => {
PlotCelestialTrajectories(planetarium, main_vessel_guid);

// Vessel trajectories.
if (main_vessel_guid == null) {
return;
}
// Main vessel psychohistory and prediction.
using (DisposableIterator rp2_lines_iterator =
planetarium.PlanetariumPlotPsychohistory(
plugin_,
чебышёв_plotting_method_,
main_vessel_guid)) {
main_vessel_guid,
main_window_.history_length)) {
GLLines.PlotRP2Lines(rp2_lines_iterator,
XKCDColors.Lime,
GLLines.Style.Faded);
@@ -1816,6 +1829,7 @@ private void RenderTrajectories() {
XKCDColors.Fuchsia,
GLLines.Style.Solid);
}
// Target psychohistory and prediction.
string target_id =
FlightGlobals.fetch.VesselTarget?.GetVessel()?.id.ToString();
if (FlightGlobals.ActiveVessel != null &&
@@ -1825,7 +1839,8 @@ private void RenderTrajectories() {
planetarium.PlanetariumPlotPsychohistory(
plugin_,
чебышёв_plotting_method_,
target_id)) {
target_id,
main_window_.history_length)) {
GLLines.PlotRP2Lines(rp2_lines_iterator,
XKCDColors.Goldenrod,
GLLines.Style.Faded);
@@ -1840,6 +1855,7 @@ private void RenderTrajectories() {
GLLines.Style.Solid);
}
}
// Main vessel flight plan.
if (plugin_.FlightPlanExists(main_vessel_guid)) {
int number_of_anomalous_manœuvres =
plugin_.FlightPlanNumberOfAnomalousManoeuvres(main_vessel_guid);
@@ -1903,6 +1919,52 @@ private void RenderTrajectories() {
}
}

private void PlotCelestialTrajectories(DisposablePlanetarium planetarium,
string main_vessel_guid) {
foreach (CelestialBody celestial in FlightGlobals.Bodies) {
if (plotting_frame_selector_.FixedBodies().Contains(celestial)) {
continue;
}
var colour = celestial.MapObject?.uiNode?.VisualIconData.color ??
XKCDColors.SunshineYellow;
if (colour.a != 1) {
// When zoomed into a planetary system, the trajectory of the
// planet is hidden in stock (because KSP then draws most things
// in the reference frame centred on that planet).
// Here we still want to display the trajectory of the primary,
// e.g., if we are drawing the trajectories of the Jovian system
// in the heliocentric frame.
foreach (CelestialBody child in celestial.orbitingBodies) {
colour.a = Math.Max(
child.MapObject?.uiNode?.VisualIconData.color.a ?? 1,
colour.a);
}
}
if (colour.a == 0) {
continue;
}
using (DisposableIterator rp2_lines_iterator =
planetarium.PlanetariumPlotCelestialTrajectoryForPsychohistory(
plugin_,
celestial.flightGlobalsIndex,
main_vessel_guid,
main_window_.history_length)) {
GLLines.PlotRP2Lines(rp2_lines_iterator,
colour,
GLLines.Style.Faded);
}
if (main_vessel_guid != null) {
using (DisposableIterator rp2_lines_iterator =
planetarium.PlanetariumPlotCelestialTrajectoryForPredictionOrFlightPlan(
plugin_, celestial.flightGlobalsIndex, main_vessel_guid)) {
GLLines.PlotRP2Lines(rp2_lines_iterator,
colour,
GLLines.Style.Solid);
}
}
}
}

private void RenderPredictionMarkers(string vessel_guid,
XYZ sun_world_position) {
if (plotting_frame_selector_.target_override) {
Loading