From 8b288efe244d4188685cff029105f81ddd756bc1 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Mon, 10 Jan 2022 03:04:54 +0100 Subject: [PATCH 01/33] encouraging eggsperiment with meshes --- journal/profiles.hpp | 1 + ksp_plugin/interface.hpp | 7 ++ ksp_plugin/interface_planetarium.cpp | 74 +++++++++++++- ksp_plugin/planetarium.cpp | 101 ++++++++++++++++++- ksp_plugin/planetarium.hpp | 17 +++- ksp_plugin/plugin.cpp | 6 +- ksp_plugin/plugin.hpp | 3 +- ksp_plugin_adapter/gl_lines.cs | 4 +- ksp_plugin_adapter/ksp_plugin_adapter.cs | 58 ++++++++--- ksp_plugin_adapter/ksp_plugin_adapter.csproj | 6 +- ksp_plugin_adapter/main_window.cs | 15 +++ serialization/journal.proto | 24 +++++ 12 files changed, 292 insertions(+), 24 deletions(-) diff --git a/journal/profiles.hpp b/journal/profiles.hpp index 6bca553708..b1b37d6109 100644 --- a/journal/profiles.hpp +++ b/journal/profiles.hpp @@ -39,6 +39,7 @@ using interface::Origin; using interface::QP; using interface::QPRW; using interface::Status; +using interface::UnityVector3; using interface::WXYZ; using interface::XY; using interface::XYZ; diff --git a/ksp_plugin/interface.hpp b/ksp_plugin/interface.hpp index 92da81d73f..8e1f6a43a0 100644 --- a/ksp_plugin/interface.hpp +++ b/ksp_plugin/interface.hpp @@ -73,6 +73,13 @@ std::unique_ptr TakeOwnership(T** pointer); template std::unique_ptr TakeOwnershipArray(T** pointer); +extern "C" +struct UnityVector3 { + float x; + float y; + float z; +}; + #include "ksp_plugin/interface.generated.h" extern "C" PRINCIPIA_DLL diff --git a/ksp_plugin/interface_planetarium.cpp b/ksp_plugin/interface_planetarium.cpp index 79cb5c33db..5b9dbc41f5 100644 --- a/ksp_plugin/interface_planetarium.cpp +++ b/ksp_plugin/interface_planetarium.cpp @@ -95,7 +95,10 @@ Planetarium* __cdecl principia__PlanetariumCreate( world_to_plotting_affine_map * camera_to_world_affine_map, focal * Metre); - return m.Return(plugin->NewPlanetarium(parameters, perspective).release()); + return m.Return(plugin->NewPlanetarium( + parameters, + perspective, + world_to_plotting_affine_map.Inverse()).release()); } void __cdecl principia__PlanetariumDelete( @@ -281,5 +284,74 @@ principia__PlanetariumPlotCelestialTrajectoryForPredictionOrFlightPlan( } } +// 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. +void __cdecl principia__PlanetariumGetCelestialVertices( + Planetarium const* const planetarium, + Plugin const* const plugin, + int const celestial_index, + char const* const vessel_guid, + double const inverse_scale_factor, + XYZ const offset, + UnityVector3* const vertices, + int const vertices_size, + double* const minimal_distance_from_camera, + int* const vertex_count) { + journal::Method m( + {planetarium, + plugin, + celestial_index, + vessel_guid, + inverse_scale_factor, + offset, + vertices, + vertices_size}, + {minimal_distance_from_camera, vertex_count}); + CHECK_NOTNULL(plugin); + CHECK_NOTNULL(planetarium); + *vertex_count = 0; + + // Do not plot the past when there is a target vessel as it is misleading. + if (plugin->renderer().HasTargetVessel()) { + *minimal_distance_from_camera = std::numeric_limits::infinity(); + return m.Return(); + } 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(); + // No need to request reanimation here because the current time of the + // plugin is necessarily covered. + Length minimal_distance; + planetarium->PlotMethod3( + celestial_trajectory, + /*first_time=*/plugin->CurrentTime(), + /*last_time=*/final_time, + /*now=*/plugin->CurrentTime(), + /*reverse=*/false, + [inverse_scale_factor, offset = FromXYZ(offset), vertices, vertex_count]( + Position const& q) { + auto const scaled_coords = + ((q - World::origin) / Metre).coordinates() * + inverse_scale_factor + + offset; + vertices[*vertex_count] = {static_cast(scaled_coords.x), + static_cast(scaled_coords.y), + static_cast(scaled_coords.z)}; + ++*vertex_count; + }, + vertices_size, + &minimal_distance); + *minimal_distance_from_camera = minimal_distance / Metre; + return m.Return(); + } +} + } // namespace interface } // namespace principia diff --git a/ksp_plugin/planetarium.cpp b/ksp_plugin/planetarium.cpp index 2d0b026f38..ac15bad149 100644 --- a/ksp_plugin/planetarium.cpp +++ b/ksp_plugin/planetarium.cpp @@ -43,11 +43,13 @@ Planetarium::Planetarium( Parameters const& parameters, Perspective perspective, not_null const*> const ephemeris, - not_null const plotting_frame) + not_null const plotting_frame, + RigidTransformation const& plotting_to_world) : parameters_(parameters), perspective_(std::move(perspective)), ephemeris_(ephemeris), - plotting_frame_(plotting_frame) {} + plotting_frame_(plotting_frame), + plotting_to_world_(plotting_to_world) {} RP2Lines Planetarium::PlotMethod0( DiscreteTrajectory const& trajectory, @@ -268,6 +270,101 @@ RP2Lines Planetarium::PlotMethod2( return lines; } +void Planetarium::PlotMethod3( + Trajectory const& trajectory, + Instant const& first_time, + Instant const& last_time, + Instant const& now, + bool reverse, + std::function const&)> add_point, + int const max_steps, + Length* minimal_distance) const { + 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; + + if (minimal_distance != nullptr) { + *minimal_distance = Infinity; + } + + Sign const direction = reverse ? Sign::Negative() : Sign::Positive(); + if (direction * (final_time - previous_time) <= Time{}) { + return; + } + RigidMotion to_plotting_frame_at_t = + plotting_frame_->ToThisFrameAtTime(previous_time); + DegreesOfFreedom const initial_degrees_of_freedom = + to_plotting_frame_at_t( + trajectory.EvaluateDegreesOfFreedom(previous_time)); + Position previous_position = + initial_degrees_of_freedom.position(); + Velocity previous_velocity = + initial_degrees_of_freedom.velocity(); + Time Δt = final_time - previous_time; + + Instant t; + double estimated_tan²_error; + std::optional> + degrees_of_freedom_in_barycentric; + Position position; + Square minimal_squared_distance = Infinity>; + + std::optional> last_endpoint; + + int steps_accepted = 0; + + goto estimate_tan²_error; + + while (steps_accepted < max_steps && + direction * (previous_time - final_time) < Time{}) { + do { + // One square root because we have squared errors, another one because the + // errors are quadratic in time (in other words, two square roots because + // the squared errors are quartic in time). + // A safety factor prevents catastrophic retries. + Δt *= 0.9 * Sqrt(Sqrt(tan²_angular_resolution / estimated_tan²_error)); + estimate_tan²_error: + t = previous_time + Δt; + if (direction * (t - final_time) > Time{}) { + t = final_time; + Δt = t - previous_time; + } + Position const extrapolated_position = + previous_position + previous_velocity * Δt; + to_plotting_frame_at_t = plotting_frame_->ToThisFrameAtTime(t); + degrees_of_freedom_in_barycentric = + trajectory.EvaluateDegreesOfFreedom(t); + position = to_plotting_frame_at_t.rigid_transformation()( + degrees_of_freedom_in_barycentric->position()); + + // The quadratic term of the error between the linear interpolation and + // the actual function is maximized halfway through the segment, so it is + // 1/2 (Δt/2)² f″(t-Δt) = (1/2 Δt² f″(t-Δt)) / 4; the squared error is + // thus (1/2 Δt² f″(t-Δt))² / 16. + estimated_tan²_error = + perspective_.Tan²AngularDistance(extrapolated_position, position) / + 16; + } while (estimated_tan²_error > tan²_angular_resolution); + ++steps_accepted; + + previous_time = t; + previous_position = position; + previous_velocity = + to_plotting_frame_at_t(*degrees_of_freedom_in_barycentric).velocity(); + + add_point(plotting_to_world_(position)); + if (minimal_distance != nullptr) { + minimal_squared_distance = + std::min(minimal_squared_distance, + perspective_.SquaredDistanceFromCamera(position)); + } + } + if (minimal_distance != nullptr) { + *minimal_distance = Sqrt(minimal_squared_distance); + } +} + std::vector> Planetarium::ComputePlottableSpheres( Instant const& now) const { RigidMotion const rigid_motion_at_now = diff --git a/ksp_plugin/planetarium.hpp b/ksp_plugin/planetarium.hpp index 93868a0b54..3fcaccdfc1 100644 --- a/ksp_plugin/planetarium.hpp +++ b/ksp_plugin/planetarium.hpp @@ -25,6 +25,8 @@ using geometry::Displacement; using geometry::Instant; using geometry::OrthogonalMap; using geometry::Perspective; +using geometry::Position; +using geometry::RigidTransformation; using geometry::RP2Lines; using geometry::RP2Point; using geometry::Segment; @@ -65,7 +67,8 @@ class Planetarium { Planetarium(Parameters const& parameters, Perspective perspective, not_null const*> ephemeris, - not_null plotting_frame); + not_null plotting_frame, + RigidTransformation const& plotting_to_world); // A no-op method that just returns all the points in the trajectory defined // by |begin| and |end|. @@ -104,6 +107,16 @@ class Planetarium { bool reverse, Length* minimal_distance = nullptr) const; + void PlotMethod3( + Trajectory const& trajectory, + Instant const& first_time, + Instant const& last_time, + Instant const& now, + bool reverse, + std::function const&)> add_point, + int max_steps, + Length* minimal_distance = nullptr) const; + private: // Computes the coordinates of the spheres that represent the |ephemeris_| // bodies. These coordinates are in the |plotting_frame_| at time |now|. @@ -118,6 +131,8 @@ class Planetarium { DiscreteTrajectory::iterator end) const; Parameters const parameters_; + // TODO(egg): Consider distinguishing this copy of World. + RigidTransformation const plotting_to_world_; Perspective const perspective_; not_null const*> const ephemeris_; not_null const plotting_frame_; diff --git a/ksp_plugin/plugin.cpp b/ksp_plugin/plugin.cpp index 24b83ca669..1a577ddd2d 100644 --- a/ksp_plugin/plugin.cpp +++ b/ksp_plugin/plugin.cpp @@ -1076,12 +1076,14 @@ void Plugin::ClearOrbitAnalysersOfVesselsOtherThan(Vessel const& vessel) { not_null> Plugin::NewPlanetarium( Planetarium::Parameters const& parameters, - Perspective const& perspective) + Perspective const& perspective, + RigidTransformation const& plotting_to_world) const { return make_not_null_unique(parameters, perspective, ephemeris_.get(), - renderer_->GetPlottingFrame()); + renderer_->GetPlottingFrame(), + plotting_to_world); } not_null> diff --git a/ksp_plugin/plugin.hpp b/ksp_plugin/plugin.hpp index 9b0a08a8e7..227938837f 100644 --- a/ksp_plugin/plugin.hpp +++ b/ksp_plugin/plugin.hpp @@ -390,7 +390,8 @@ class Plugin { virtual not_null> NewPlanetarium( Planetarium::Parameters const& parameters, - Perspective const& perspective) const; + Perspective const& perspective, + geometry::RigidTransformation const& plotting_to_world) const; virtual not_null> NewBarycentricRotatingNavigationFrame(Index primary_index, diff --git a/ksp_plugin_adapter/gl_lines.cs b/ksp_plugin_adapter/gl_lines.cs index 88323600d5..55679e19ba 100644 --- a/ksp_plugin_adapter/gl_lines.cs +++ b/ksp_plugin_adapter/gl_lines.cs @@ -14,7 +14,9 @@ public static void Draw(Action line_vertices) { try { UnityEngine.GL.PushMatrix(); line_material.SetPass(0); - UnityEngine.GL.LoadPixelMatrix(); + if (!MainWindow.use_meshes) { + UnityEngine.GL.LoadPixelMatrix(); + } UnityEngine.GL.Begin(UnityEngine.GL.LINES); line_vertices(); diff --git a/ksp_plugin_adapter/ksp_plugin_adapter.cs b/ksp_plugin_adapter/ksp_plugin_adapter.cs index 9cc6207b43..51d196d938 100644 --- a/ksp_plugin_adapter/ksp_plugin_adapter.cs +++ b/ksp_plugin_adapter/ksp_plugin_adapter.cs @@ -2161,7 +2161,7 @@ private void RenderTrajectories() { GLLines.PlotRP2Lines(rp2_lines_iterator, target_history_colour, target_history_style); - } + }// TODO COMMENT ALL PAST CELESTIAL PLOTTING, ADD MESH PATH using (DisposableIterator rp2_lines_iterator = planetarium.PlanetariumPlotPrediction( plugin_, @@ -2249,6 +2249,7 @@ private void PlotCelestialTrajectories(DisposablePlanetarium planetarium, double tan_angular_resolution = Math.Min( Math.Tan(vertical_fov * degree / 2) / (camera.pixelHeight / 2), Math.Tan(horizontal_fov * degree / 2) / (camera.pixelWidth / 2)); + MainWindow.trace = $"{UnityEngine.Camera.current.name}, {PlanetariumCamera.Camera.name} {ScaledSpace.LocalToScaledSpace(Vector3d.zero)}"; PlotSubtreeTrajectories(planetarium, main_vessel_guid, Planetarium.fetch.Sun, tan_angular_resolution); @@ -2265,7 +2266,7 @@ private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, double min_distance_from_camera = (root.position - camera_world_position).magnitude; if (!plotting_frame_selector_.FixesBody(root)) { - using (DisposableIterator rp2_lines_iterator = + /*using (DisposableIterator rp2_lines_iterator = planetarium.PlanetariumPlotCelestialTrajectoryForPsychohistory( plugin_, root.flightGlobalsIndex, @@ -2275,18 +2276,49 @@ private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, min_distance_from_camera = Math.Min(min_distance_from_camera, min_past_distance); GLLines.PlotRP2Lines(rp2_lines_iterator, colour, GLLines.Style.Faded); - } + }*/ if (main_vessel_guid != null) { - using (DisposableIterator rp2_lines_iterator = - planetarium. - PlanetariumPlotCelestialTrajectoryForPredictionOrFlightPlan( - plugin_, - root.flightGlobalsIndex, - main_vessel_guid, - out double min_future_distance)) { - min_distance_from_camera = - Math.Min(min_distance_from_camera, min_future_distance); - GLLines.PlotRP2Lines(rp2_lines_iterator, colour, GLLines.Style.Solid); + if (MainWindow.use_meshes) { + unsafe { + var vertices = new UnityEngine.Vector3[10_000]; + int vertex_count; + fixed (UnityEngine.Vector3* vertices_data = vertices) { + planetarium.PlanetariumGetCelestialVertices( + plugin_, + root.flightGlobalsIndex, + main_vessel_guid, + ScaledSpace.InverseScaleFactor, + (XYZ)ScaledSpace.LocalToScaledSpace(Vector3d.zero), + (IntPtr)vertices_data, + vertices.Length, + out double min_future_distance, + out vertex_count); + } + var mesh = new UnityEngine.Mesh(); + mesh.vertices = vertices; + var indices = new int[vertex_count]; + var colours = new UnityEngine.Color[vertex_count]; + for (int i = 0; i < vertex_count; ++i) { + indices[i] = i; + colours[i] = colour; + } + mesh.colors = colours; + MainWindow.trace += $"{vertex_count} vertices for {root.name}"; + mesh.SetIndices(indices, UnityEngine.MeshTopology.LineStrip, submesh: 0); + UnityEngine.Graphics.DrawMeshNow(mesh, UnityEngine.Vector3.zero, UnityEngine.Quaternion.identity); + } + } else { + using (DisposableIterator rp2_lines_iterator = + planetarium. + PlanetariumPlotCelestialTrajectoryForPredictionOrFlightPlan( + plugin_, + root.flightGlobalsIndex, + main_vessel_guid, + out double min_future_distance)) { + min_distance_from_camera = + Math.Min(min_distance_from_camera, min_future_distance); + GLLines.PlotRP2Lines(rp2_lines_iterator, colour, GLLines.Style.Solid); + } } } } diff --git a/ksp_plugin_adapter/ksp_plugin_adapter.csproj b/ksp_plugin_adapter/ksp_plugin_adapter.csproj index 223c5df529..810fa69fc5 100644 --- a/ksp_plugin_adapter/ksp_plugin_adapter.csproj +++ b/ksp_plugin_adapter/ksp_plugin_adapter.csproj @@ -20,7 +20,7 @@ TRACE;DEBUG;KSP_VERSION_1_12_2 prompt 4 - false + true false v4.7.2 @@ -32,7 +32,7 @@ prompt 4 true - false + true false v4.7.2 @@ -44,7 +44,7 @@ prompt 4 true - false + true false v3.5 diff --git a/ksp_plugin_adapter/main_window.cs b/ksp_plugin_adapter/main_window.cs index 44e8a50197..2b1284dcda 100644 --- a/ksp_plugin_adapter/main_window.cs +++ b/ksp_plugin_adapter/main_window.cs @@ -275,7 +275,22 @@ private void RenderKSPFeatures() { } } + public static bool use_meshes = false; + public static string trace = ""; + public static int x,y,z; + private void RenderLoggingSettings() { + use_meshes = UnityEngine.GUILayout.Toggle(use_meshes, "Use meshes"); + UnityEngine.GUILayout.Label(trace); + if (int.TryParse(UnityEngine.GUILayout.TextArea(x.ToString()), out int nx)) { + x = nx; + } + if (int.TryParse(UnityEngine.GUILayout.TextArea(y.ToString()), out int ny)) { + y = ny; + } + if (int.TryParse(UnityEngine.GUILayout.TextArea(z.ToString()), out int nz)) { + z = nz; + } using (new UnityEngine.GUILayout.HorizontalScope()) { UnityEngine.GUILayout.Label( text : L10N.CacheFormat( diff --git a/serialization/journal.proto b/serialization/journal.proto index 99f9ce20df..a037d50cb9 100644 --- a/serialization/journal.proto +++ b/serialization/journal.proto @@ -1857,6 +1857,30 @@ message PlanetariumPlotCelestialTrajectoryForPredictionOrFlightPlan { optional Return return = 3; } +message PlanetariumGetCelestialVertices { + extend Method { + optional PlanetariumGetCelestialVertices extension = 5999; + } + message In { + required fixed64 planetarium = 1 [(pointer_to) = "Planetarium const", + (disposable) = "DisposablePlanetarium", + (is_subject) = true]; + required fixed64 plugin = 2 [(pointer_to) = "Plugin const"]; + required int32 celestial_index = 3; + required string vessel_guid = 4; + required double inverse_scale_factor = 5; + required XYZ offset = 6; + required fixed64 vertices = 7 [(pointer_to) = "UnityVector3"]; + required int32 vertices_size = 8; + } + message Out { + required double minimal_distance_from_camera = 1; + required int32 vertex_count = 2; + } + optional In in = 1; + optional Out out = 2; +} + message PlanetariumPlotFlightPlanSegment { extend Method { optional PlanetariumPlotFlightPlanSegment extension = 5137; From 535b15b869896a47228907cd2151952952bf0fa2 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Wed, 12 Jan 2022 01:04:46 +0100 Subject: [PATCH 02/33] different scheduling and now or not --- ksp_plugin_adapter/gl_lines.cs | 2 +- ksp_plugin_adapter/ksp_plugin_adapter.cs | 9 +++++++-- ksp_plugin_adapter/main_window.cs | 20 +++++++++----------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/ksp_plugin_adapter/gl_lines.cs b/ksp_plugin_adapter/gl_lines.cs index 55679e19ba..712717b7ad 100644 --- a/ksp_plugin_adapter/gl_lines.cs +++ b/ksp_plugin_adapter/gl_lines.cs @@ -150,7 +150,7 @@ private static XY ToScreen(XY rp2_point) { private static UnityEngine.Material line_material_; - private static UnityEngine.Material line_material { + public static UnityEngine.Material line_material { get { if (line_material_ == null) { line_material_ = new UnityEngine.Material( diff --git a/ksp_plugin_adapter/ksp_plugin_adapter.cs b/ksp_plugin_adapter/ksp_plugin_adapter.cs index 51d196d938..831cd6d047 100644 --- a/ksp_plugin_adapter/ksp_plugin_adapter.cs +++ b/ksp_plugin_adapter/ksp_plugin_adapter.cs @@ -870,7 +870,8 @@ private void LateUpdate() { if (map_renderer_ == null) { map_renderer_ = PlanetariumCamera.Camera.gameObject. AddComponent(); - map_renderer_.post_render = RenderTrajectories; + map_renderer_.pre_cull = () => {if (MainWindow.pre_cull) RenderTrajectories();}; + map_renderer_.post_render = () => {if (!MainWindow.pre_cull) RenderTrajectories();}; } if (galaxy_cube_rotator_ == null) { @@ -2305,7 +2306,11 @@ private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, mesh.colors = colours; MainWindow.trace += $"{vertex_count} vertices for {root.name}"; mesh.SetIndices(indices, UnityEngine.MeshTopology.LineStrip, submesh: 0); - UnityEngine.Graphics.DrawMeshNow(mesh, UnityEngine.Vector3.zero, UnityEngine.Quaternion.identity); + if (MainWindow.now) { + UnityEngine.Graphics.DrawMeshNow(mesh, UnityEngine.Vector3.zero, UnityEngine.Quaternion.identity); + } else { + UnityEngine.Graphics.DrawMesh(mesh, UnityEngine.Vector3.zero, UnityEngine.Quaternion.identity, GLLines.line_material, MainWindow.layer, PlanetariumCamera.Camera); + } } } else { using (DisposableIterator rp2_lines_iterator = diff --git a/ksp_plugin_adapter/main_window.cs b/ksp_plugin_adapter/main_window.cs index 2b1284dcda..86170bdcfc 100644 --- a/ksp_plugin_adapter/main_window.cs +++ b/ksp_plugin_adapter/main_window.cs @@ -274,23 +274,21 @@ private void RenderKSPFeatures() { selecting_target_celestial_ = false; } } - + public static bool use_meshes = false; + public static bool pre_cull = false; + public static bool now = true; + public static int layer = 9; public static string trace = ""; - public static int x,y,z; private void RenderLoggingSettings() { use_meshes = UnityEngine.GUILayout.Toggle(use_meshes, "Use meshes"); - UnityEngine.GUILayout.Label(trace); - if (int.TryParse(UnityEngine.GUILayout.TextArea(x.ToString()), out int nx)) { - x = nx; - } - if (int.TryParse(UnityEngine.GUILayout.TextArea(y.ToString()), out int ny)) { - y = ny; - } - if (int.TryParse(UnityEngine.GUILayout.TextArea(z.ToString()), out int nz)) { - z = nz; + pre_cull = UnityEngine.GUILayout.Toggle(pre_cull, "Pre-cull"); + now = UnityEngine.GUILayout.Toggle(now, "Now"); + if (int.TryParse(UnityEngine.GUILayout.TextArea(layer.ToString()), out int result)) { + layer = result; } + UnityEngine.GUILayout.Label(trace); using (new UnityEngine.GUILayout.HorizontalScope()) { UnityEngine.GUILayout.Label( text : L10N.CacheFormat( From 570998e8127e6f0505e90de68f04a2b19f80f699 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Wed, 12 Jan 2022 01:59:42 +0100 Subject: [PATCH 03/33] fix colours --- ksp_plugin_adapter/ksp_plugin_adapter.cs | 6 ++++-- ksp_plugin_adapter/main_window.cs | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/ksp_plugin_adapter/ksp_plugin_adapter.cs b/ksp_plugin_adapter/ksp_plugin_adapter.cs index 831cd6d047..48cc497d41 100644 --- a/ksp_plugin_adapter/ksp_plugin_adapter.cs +++ b/ksp_plugin_adapter/ksp_plugin_adapter.cs @@ -2298,10 +2298,12 @@ private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, var mesh = new UnityEngine.Mesh(); mesh.vertices = vertices; var indices = new int[vertex_count]; - var colours = new UnityEngine.Color[vertex_count]; + var colours = new UnityEngine.Color[vertices.Length]; + for (int i = 0; i < colours.Length; ++i) { + colours[i] = colour; + } for (int i = 0; i < vertex_count; ++i) { indices[i] = i; - colours[i] = colour; } mesh.colors = colours; MainWindow.trace += $"{vertex_count} vertices for {root.name}"; diff --git a/ksp_plugin_adapter/main_window.cs b/ksp_plugin_adapter/main_window.cs index 86170bdcfc..99c4b99858 100644 --- a/ksp_plugin_adapter/main_window.cs +++ b/ksp_plugin_adapter/main_window.cs @@ -277,14 +277,14 @@ private void RenderKSPFeatures() { public static bool use_meshes = false; public static bool pre_cull = false; - public static bool now = true; + public static bool now = false; public static int layer = 9; public static string trace = ""; private void RenderLoggingSettings() { use_meshes = UnityEngine.GUILayout.Toggle(use_meshes, "Use meshes"); - pre_cull = UnityEngine.GUILayout.Toggle(pre_cull, "Pre-cull"); - now = UnityEngine.GUILayout.Toggle(now, "Now"); + now = UnityEngine.GUILayout.Toggle(now, "Now post-render"); + pre_cull = !now; if (int.TryParse(UnityEngine.GUILayout.TextArea(layer.ToString()), out int result)) { layer = result; } From 5978f223a7a95979c4d1b1f15e22f3c766be9972 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Wed, 12 Jan 2022 19:45:23 +0100 Subject: [PATCH 04/33] =?UTF-8?q?don=E2=80=99t=20leak?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ksp_plugin_adapter/ksp_plugin_adapter.cs | 21 +++++++++++++++++---- ksp_plugin_adapter/main_window.cs | 2 ++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/ksp_plugin_adapter/ksp_plugin_adapter.cs b/ksp_plugin_adapter/ksp_plugin_adapter.cs index 48cc497d41..21e18e8b2a 100644 --- a/ksp_plugin_adapter/ksp_plugin_adapter.cs +++ b/ksp_plugin_adapter/ksp_plugin_adapter.cs @@ -2256,6 +2256,8 @@ private void PlotCelestialTrajectories(DisposablePlanetarium planetarium, tan_angular_resolution); } + private Dictionary celestial_trajectories_ = new Dictionary(); + private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, string main_vessel_guid, CelestialBody root, @@ -2281,6 +2283,15 @@ private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, if (main_vessel_guid != null) { if (MainWindow.use_meshes) { unsafe { + UnityEngine.Mesh mesh; + if (MainWindow.reuse_meshes) { + if (!celestial_trajectories_.TryGetValue(root, out mesh)) { + mesh = new UnityEngine.Mesh(); + celestial_trajectories_[root] = mesh; + } + } else { + mesh = new UnityEngine.Mesh(); + } var vertices = new UnityEngine.Vector3[10_000]; int vertex_count; fixed (UnityEngine.Vector3* vertices_data = vertices) { @@ -2295,19 +2306,21 @@ private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, out double min_future_distance, out vertex_count); } - var mesh = new UnityEngine.Mesh(); mesh.vertices = vertices; var indices = new int[vertex_count]; + for (int i = 0; i < vertex_count; ++i) { + indices[i] = i; + } var colours = new UnityEngine.Color[vertices.Length]; for (int i = 0; i < colours.Length; ++i) { colours[i] = colour; } - for (int i = 0; i < vertex_count; ++i) { - indices[i] = i; - } mesh.colors = colours; MainWindow.trace += $"{vertex_count} vertices for {root.name}"; mesh.SetIndices(indices, UnityEngine.MeshTopology.LineStrip, submesh: 0); + if (MainWindow.reuse_meshes) { + mesh.RecalculateBounds(); + } if (MainWindow.now) { UnityEngine.Graphics.DrawMeshNow(mesh, UnityEngine.Vector3.zero, UnityEngine.Quaternion.identity); } else { diff --git a/ksp_plugin_adapter/main_window.cs b/ksp_plugin_adapter/main_window.cs index 99c4b99858..910b9c55ba 100644 --- a/ksp_plugin_adapter/main_window.cs +++ b/ksp_plugin_adapter/main_window.cs @@ -276,6 +276,7 @@ private void RenderKSPFeatures() { } public static bool use_meshes = false; + public static bool reuse_meshes = false; public static bool pre_cull = false; public static bool now = false; public static int layer = 9; @@ -283,6 +284,7 @@ private void RenderKSPFeatures() { private void RenderLoggingSettings() { use_meshes = UnityEngine.GUILayout.Toggle(use_meshes, "Use meshes"); + reuse_meshes = UnityEngine.GUILayout.Toggle(reuse_meshes, "Reuse meshes"); now = UnityEngine.GUILayout.Toggle(now, "Now post-render"); pre_cull = !now; if (int.TryParse(UnityEngine.GUILayout.TextArea(layer.ToString()), out int result)) { From 63e2cb6298b6b9021dbc55012339652d861498a7 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Thu, 13 Jan 2022 23:20:38 +0100 Subject: [PATCH 05/33] start refactoring --- ksp_plugin/interface.hpp | 8 +------- ksp_plugin/interface_planetarium.cpp | 6 +++--- ksp_plugin/planetarium.cpp | 20 +++++++++++++++--- ksp_plugin/planetarium.hpp | 26 ++++++++++++++++++++++-- ksp_plugin_adapter/ksp_plugin_adapter.cs | 2 +- serialization/journal.proto | 10 ++++----- 6 files changed, 51 insertions(+), 21 deletions(-) diff --git a/ksp_plugin/interface.hpp b/ksp_plugin/interface.hpp index 8e1f6a43a0..fd5696016e 100644 --- a/ksp_plugin/interface.hpp +++ b/ksp_plugin/interface.hpp @@ -56,6 +56,7 @@ using ksp_plugin::PileUp; using ksp_plugin::PileUpFuture; using ksp_plugin::Planetarium; using ksp_plugin::Plugin; +using ksp_plugin::ScaledSpacePoint; using ksp_plugin::Vessel; using ksp_plugin::World; using physics::DegreesOfFreedom; @@ -73,13 +74,6 @@ std::unique_ptr TakeOwnership(T** pointer); template std::unique_ptr TakeOwnershipArray(T** pointer); -extern "C" -struct UnityVector3 { - float x; - float y; - float z; -}; - #include "ksp_plugin/interface.generated.h" extern "C" PRINCIPIA_DLL diff --git a/ksp_plugin/interface_planetarium.cpp b/ksp_plugin/interface_planetarium.cpp index 5b9dbc41f5..6a12887524 100644 --- a/ksp_plugin/interface_planetarium.cpp +++ b/ksp_plugin/interface_planetarium.cpp @@ -242,7 +242,7 @@ Iterator* __cdecl principia__PlanetariumPlotCelestialTrajectoryForPsychohistory( // 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* __cdecl -principia__PlanetariumPlotCelestialTrajectoryForPredictionOrFlightPlan( +principia__PlanetariumGetCelestialPastTrajectoryVertices( Planetarium const* const planetarium, Plugin const* const plugin, int const celestial_index, @@ -287,7 +287,7 @@ principia__PlanetariumPlotCelestialTrajectoryForPredictionOrFlightPlan( // 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. -void __cdecl principia__PlanetariumGetCelestialVertices( +void __cdecl principia__PlanetariumGetCelestialFutureTrajectoryVertices( Planetarium const* const planetarium, Plugin const* const plugin, int const celestial_index, @@ -298,7 +298,7 @@ void __cdecl principia__PlanetariumGetCelestialVertices( int const vertices_size, double* const minimal_distance_from_camera, int* const vertex_count) { - journal::Method m( + journal::Method m( {planetarium, plugin, celestial_index, diff --git a/ksp_plugin/planetarium.cpp b/ksp_plugin/planetarium.cpp index ac15bad149..2d6be4b290 100644 --- a/ksp_plugin/planetarium.cpp +++ b/ksp_plugin/planetarium.cpp @@ -15,6 +15,7 @@ namespace ksp_plugin { namespace internal_planetarium { using geometry::Position; +using geometry::R3Element; using geometry::RP2Line; using geometry::Sign; using geometry::Velocity; @@ -44,12 +45,16 @@ Planetarium::Planetarium( Perspective perspective, not_null const*> const ephemeris, not_null const plotting_frame, - RigidTransformation const& plotting_to_world) + RigidTransformation const& plotting_to_world, + Inverse const& inverse_scale_factor, + Position const& scaled_space_origin) : parameters_(parameters), perspective_(std::move(perspective)), ephemeris_(ephemeris), plotting_frame_(plotting_frame), - plotting_to_world_(plotting_to_world) {} + plotting_to_world_(plotting_to_world), + inverse_scale_factor_(inverse_scale_factor), + scaled_space_origin_(scaled_space_origin) {} RP2Lines Planetarium::PlotMethod0( DiscreteTrajectory const& trajectory, @@ -276,7 +281,7 @@ void Planetarium::PlotMethod3( Instant const& last_time, Instant const& now, bool reverse, - std::function const&)> add_point, + std::function add_point, int const max_steps, Length* minimal_distance) const { double const tan²_angular_resolution = @@ -440,6 +445,15 @@ Segments Planetarium::ComputePlottableSegments( return all_segments; } +ScaledSpacePoint Planetarium::WorldToScaledSpace( + Position const& position) { + R3Element const scaled_space_coordinates = + ((position - scaled_space_origin_) * inverse_scale_factor_).coordinates(); + return ScaledSpacePoint{static_cast(scaled_space_coordinates.x), + static_cast(scaled_space_coordinates.y), + static_cast(scaled_space_coordinates.z)}; +} + } // namespace internal_planetarium } // namespace ksp_plugin } // namespace principia diff --git a/ksp_plugin/planetarium.hpp b/ksp_plugin/planetarium.hpp index 3fcaccdfc1..63a2eb803f 100644 --- a/ksp_plugin/planetarium.hpp +++ b/ksp_plugin/planetarium.hpp @@ -39,6 +39,19 @@ using physics::RigidMotion; using physics::Trajectory; using quantities::Angle; using quantities::Length; +using quantities::Inverse; + +// Corresponds to a UnityEngine.Vector3 representing a position in KSP’s +// ScaledSpace. +extern "C" +struct ScaledSpacePoint { + float x; + float y; + float z; +}; + +static_assert(std::is_pod::value, + "NavigationFrameParameters is used for interfacing"); // A planetarium is an ephemeris together with a perspective. In this setting // it is possible to draw trajectories in the projective plane. @@ -64,11 +77,15 @@ class Planetarium { // TODO(phl): All this Navigation is weird. Should it be named Plotting? // In particular Navigation vs. NavigationFrame is a mess. + // |inverse_scale_factor| and |scaled_space_origin| define the + // transformation from World to ScaledSpace. Planetarium(Parameters const& parameters, Perspective perspective, not_null const*> ephemeris, not_null plotting_frame, - RigidTransformation const& plotting_to_world); + RigidTransformation const& plotting_to_world, + Inverse const& inverse_scale_factor, + Position const& scaled_space_origin); // A no-op method that just returns all the points in the trajectory defined // by |begin| and |end|. @@ -113,7 +130,7 @@ class Planetarium { Instant const& last_time, Instant const& now, bool reverse, - std::function const&)> add_point, + std::function add_point, int max_steps, Length* minimal_distance = nullptr) const; @@ -130,17 +147,22 @@ class Planetarium { DiscreteTrajectory::iterator begin, DiscreteTrajectory::iterator end) const; + ScaledSpacePoint WorldToScaledSpace(Position const& position); + Parameters const parameters_; // TODO(egg): Consider distinguishing this copy of World. RigidTransformation const plotting_to_world_; Perspective const perspective_; not_null const*> const ephemeris_; not_null const plotting_frame_; + Inverse const inverse_scale_factor_; + Position const scaled_space_origin_; }; } // namespace internal_planetarium using internal_planetarium::Planetarium; +using internal_planetarium::ScaledSpacePoint; } // namespace ksp_plugin } // namespace principia diff --git a/ksp_plugin_adapter/ksp_plugin_adapter.cs b/ksp_plugin_adapter/ksp_plugin_adapter.cs index 21e18e8b2a..bee3267b97 100644 --- a/ksp_plugin_adapter/ksp_plugin_adapter.cs +++ b/ksp_plugin_adapter/ksp_plugin_adapter.cs @@ -2295,7 +2295,7 @@ private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, var vertices = new UnityEngine.Vector3[10_000]; int vertex_count; fixed (UnityEngine.Vector3* vertices_data = vertices) { - planetarium.PlanetariumGetCelestialVertices( + planetarium.PlanetariumGetCelestialFutureTrajectoryVertices( plugin_, root.flightGlobalsIndex, main_vessel_guid, diff --git a/serialization/journal.proto b/serialization/journal.proto index a037d50cb9..00294c9ab8 100644 --- a/serialization/journal.proto +++ b/serialization/journal.proto @@ -1781,6 +1781,8 @@ message PlanetariumCreate { required XYZ xyz_camera_position_in_world = 6; required double focal = 7; required double field_of_view = 8; + required double inverse_scale_factor = 9; + required XYZ offset = 10; } message Return { required fixed64 result = 1 [(pointer_to) = "Planetarium", @@ -1857,9 +1859,9 @@ message PlanetariumPlotCelestialTrajectoryForPredictionOrFlightPlan { optional Return return = 3; } -message PlanetariumGetCelestialVertices { +message PlanetariumGetCelestialFutureTrajectoryVertices { extend Method { - optional PlanetariumGetCelestialVertices extension = 5999; + optional PlanetariumGetCelestialFutureTrajectoryVertices extension = 5999; } message In { required fixed64 planetarium = 1 [(pointer_to) = "Planetarium const", @@ -1868,9 +1870,7 @@ message PlanetariumGetCelestialVertices { required fixed64 plugin = 2 [(pointer_to) = "Plugin const"]; required int32 celestial_index = 3; required string vessel_guid = 4; - required double inverse_scale_factor = 5; - required XYZ offset = 6; - required fixed64 vertices = 7 [(pointer_to) = "UnityVector3"]; + required fixed64 vertices = 7 [(pointer_to) = "ScaledSpacePoint"]; required int32 vertices_size = 8; } message Out { From aaa51138b03284888f639355c18c1235ff8f11c1 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Mon, 17 Jan 2022 18:42:27 +0100 Subject: [PATCH 06/33] Transform to scaled space in the planetarium --- journal/profiles.hpp | 2 +- ksp_plugin/interface_planetarium.cpp | 32 ++++++++++-------------- ksp_plugin/planetarium.cpp | 4 +-- ksp_plugin/planetarium.hpp | 2 +- ksp_plugin/plugin.cpp | 8 ++++-- ksp_plugin/plugin.hpp | 5 +++- ksp_plugin_adapter/gl_lines.cs | 3 ++- ksp_plugin_adapter/ksp_plugin_adapter.cs | 2 -- serialization/journal.proto | 2 +- 9 files changed, 30 insertions(+), 30 deletions(-) diff --git a/journal/profiles.hpp b/journal/profiles.hpp index b1b37d6109..9caa99d9e9 100644 --- a/journal/profiles.hpp +++ b/journal/profiles.hpp @@ -39,7 +39,6 @@ using interface::Origin; using interface::QP; using interface::QPRW; using interface::Status; -using interface::UnityVector3; using interface::WXYZ; using interface::XY; using interface::XYZ; @@ -48,6 +47,7 @@ using ksp_plugin::PileUpFuture; using ksp_plugin::Planetarium; using ksp_plugin::Plugin; using ksp_plugin::Vessel; +using ksp_plugin::ScaledSpacePoint; namespace journal { diff --git a/ksp_plugin/interface_planetarium.cpp b/ksp_plugin/interface_planetarium.cpp index 6a12887524..e73bfa1785 100644 --- a/ksp_plugin/interface_planetarium.cpp +++ b/ksp_plugin/interface_planetarium.cpp @@ -51,7 +51,9 @@ Planetarium* __cdecl principia__PlanetariumCreate( XYZ const xyz_opengl_camera_z_in_world, XYZ const xyz_camera_position_in_world, double const focal, - double const field_of_view) { + double const field_of_view, + double const inverse_scale_factor, + XYZ const scaled_space_origin) { journal::Method m({plugin, sun_world_position, xyz_opengl_camera_x_in_world, @@ -59,7 +61,9 @@ Planetarium* __cdecl principia__PlanetariumCreate( xyz_opengl_camera_z_in_world, xyz_camera_position_in_world, focal, - field_of_view}); + field_of_view, + inverse_scale_factor, + scaled_space_origin}); Renderer const& renderer = CHECK_NOTNULL(plugin)->renderer(); Multivector const opengl_camera_x_in_world( @@ -98,7 +102,9 @@ Planetarium* __cdecl principia__PlanetariumCreate( return m.Return(plugin->NewPlanetarium( parameters, perspective, - world_to_plotting_affine_map.Inverse()).release()); + world_to_plotting_affine_map.Inverse(), + inverse_scale_factor * (1 / Metre), + FromXYZ>(scaled_space_origin)).release()); } void __cdecl principia__PlanetariumDelete( @@ -242,7 +248,7 @@ Iterator* __cdecl principia__PlanetariumPlotCelestialTrajectoryForPsychohistory( // 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* __cdecl -principia__PlanetariumGetCelestialPastTrajectoryVertices( +principia__PlanetariumPlotCelestialTrajectoryForPredictionOrFlightPlan( Planetarium const* const planetarium, Plugin const* const plugin, int const celestial_index, @@ -292,9 +298,7 @@ void __cdecl principia__PlanetariumGetCelestialFutureTrajectoryVertices( Plugin const* const plugin, int const celestial_index, char const* const vessel_guid, - double const inverse_scale_factor, - XYZ const offset, - UnityVector3* const vertices, + ScaledSpacePoint* const vertices, int const vertices_size, double* const minimal_distance_from_camera, int* const vertex_count) { @@ -303,8 +307,6 @@ void __cdecl principia__PlanetariumGetCelestialFutureTrajectoryVertices( plugin, celestial_index, vessel_guid, - inverse_scale_factor, - offset, vertices, vertices_size}, {minimal_distance_from_camera, vertex_count}); @@ -335,16 +337,8 @@ void __cdecl principia__PlanetariumGetCelestialFutureTrajectoryVertices( /*last_time=*/final_time, /*now=*/plugin->CurrentTime(), /*reverse=*/false, - [inverse_scale_factor, offset = FromXYZ(offset), vertices, vertex_count]( - Position const& q) { - auto const scaled_coords = - ((q - World::origin) / Metre).coordinates() * - inverse_scale_factor + - offset; - vertices[*vertex_count] = {static_cast(scaled_coords.x), - static_cast(scaled_coords.y), - static_cast(scaled_coords.z)}; - ++*vertex_count; + [vertices, vertex_count](ScaledSpacePoint const& vertex) { + vertices[(*vertex_count)++] = vertex; }, vertices_size, &minimal_distance); diff --git a/ksp_plugin/planetarium.cpp b/ksp_plugin/planetarium.cpp index 2d6be4b290..c454cbc5b3 100644 --- a/ksp_plugin/planetarium.cpp +++ b/ksp_plugin/planetarium.cpp @@ -358,7 +358,7 @@ void Planetarium::PlotMethod3( previous_velocity = to_plotting_frame_at_t(*degrees_of_freedom_in_barycentric).velocity(); - add_point(plotting_to_world_(position)); + add_point(WorldToScaledSpace(plotting_to_world_(position))); if (minimal_distance != nullptr) { minimal_squared_distance = std::min(minimal_squared_distance, @@ -446,7 +446,7 @@ Segments Planetarium::ComputePlottableSegments( } ScaledSpacePoint Planetarium::WorldToScaledSpace( - Position const& position) { + Position const& position) const { R3Element const scaled_space_coordinates = ((position - scaled_space_origin_) * inverse_scale_factor_).coordinates(); return ScaledSpacePoint{static_cast(scaled_space_coordinates.x), diff --git a/ksp_plugin/planetarium.hpp b/ksp_plugin/planetarium.hpp index 63a2eb803f..3db3d1800a 100644 --- a/ksp_plugin/planetarium.hpp +++ b/ksp_plugin/planetarium.hpp @@ -147,7 +147,7 @@ class Planetarium { DiscreteTrajectory::iterator begin, DiscreteTrajectory::iterator end) const; - ScaledSpacePoint WorldToScaledSpace(Position const& position); + ScaledSpacePoint WorldToScaledSpace(Position const& position) const; Parameters const parameters_; // TODO(egg): Consider distinguishing this copy of World. diff --git a/ksp_plugin/plugin.cpp b/ksp_plugin/plugin.cpp index 1a577ddd2d..237c702dfa 100644 --- a/ksp_plugin/plugin.cpp +++ b/ksp_plugin/plugin.cpp @@ -1077,13 +1077,17 @@ void Plugin::ClearOrbitAnalysersOfVesselsOtherThan(Vessel const& vessel) { not_null> Plugin::NewPlanetarium( Planetarium::Parameters const& parameters, Perspective const& perspective, - RigidTransformation const& plotting_to_world) + RigidTransformation const& plotting_to_world, + Inverse inverse_scale_factor, + Position scaled_space_origin) const { return make_not_null_unique(parameters, perspective, ephemeris_.get(), renderer_->GetPlottingFrame(), - plotting_to_world); + plotting_to_world, + inverse_scale_factor, + scaled_space_origin); } not_null> diff --git a/ksp_plugin/plugin.hpp b/ksp_plugin/plugin.hpp index 227938837f..45ef72ce89 100644 --- a/ksp_plugin/plugin.hpp +++ b/ksp_plugin/plugin.hpp @@ -86,6 +86,7 @@ using physics::RotatingBody; using physics::Trajectory; using quantities::Angle; using quantities::Force; +using quantities::Inverse; using quantities::Length; using quantities::Mass; using quantities::Time; @@ -391,7 +392,9 @@ class Plugin { virtual not_null> NewPlanetarium( Planetarium::Parameters const& parameters, Perspective const& perspective, - geometry::RigidTransformation const& plotting_to_world) const; + geometry::RigidTransformation const& plotting_to_world, + Inverse inverse_scale_factor, + Position scaled_space_origin) const; virtual not_null> NewBarycentricRotatingNavigationFrame(Index primary_index, diff --git a/ksp_plugin_adapter/gl_lines.cs b/ksp_plugin_adapter/gl_lines.cs index 712717b7ad..834c159886 100644 --- a/ksp_plugin_adapter/gl_lines.cs +++ b/ksp_plugin_adapter/gl_lines.cs @@ -74,7 +74,8 @@ public static DisposablePlanetarium NewPlanetarium(IntPtr plugin, (XYZ)(Vector3d)opengl_camera_z_in_world, (XYZ)(Vector3d)camera_position_in_world, focal:1, - field_of_view); + field_of_view, ScaledSpace.InverseScaleFactor, + (XYZ)ScaledSpace.LocalToScaledSpace(Vector3d.zero)); } public static void PlotRP2Lines(DisposableIterator rp2_lines_iterator, diff --git a/ksp_plugin_adapter/ksp_plugin_adapter.cs b/ksp_plugin_adapter/ksp_plugin_adapter.cs index bee3267b97..1e0fcdfc8d 100644 --- a/ksp_plugin_adapter/ksp_plugin_adapter.cs +++ b/ksp_plugin_adapter/ksp_plugin_adapter.cs @@ -2299,8 +2299,6 @@ private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, plugin_, root.flightGlobalsIndex, main_vessel_guid, - ScaledSpace.InverseScaleFactor, - (XYZ)ScaledSpace.LocalToScaledSpace(Vector3d.zero), (IntPtr)vertices_data, vertices.Length, out double min_future_distance, diff --git a/serialization/journal.proto b/serialization/journal.proto index 00294c9ab8..ff0689dcce 100644 --- a/serialization/journal.proto +++ b/serialization/journal.proto @@ -1782,7 +1782,7 @@ message PlanetariumCreate { required double focal = 7; required double field_of_view = 8; required double inverse_scale_factor = 9; - required XYZ offset = 10; + required XYZ scaled_space_origin = 10; } message Return { required fixed64 result = 1 [(pointer_to) = "Planetarium", From 73de9ad1335b69a84bc7d4fe25b7d65a6fceadb7 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Sat, 22 Jan 2022 05:08:44 +0100 Subject: [PATCH 07/33] PlotMethod3 versions of everything: C++ side --- ksp_plugin/interface_planetarium.cpp | 171 ++++++++++++++++++++++++++- serialization/journal.proto | 88 +++++++++++++- 2 files changed, 255 insertions(+), 4 deletions(-) diff --git a/ksp_plugin/interface_planetarium.cpp b/ksp_plugin/interface_planetarium.cpp index e73bfa1785..165bbcc329 100644 --- a/ksp_plugin/interface_planetarium.cpp +++ b/ksp_plugin/interface_planetarium.cpp @@ -290,8 +290,174 @@ principia__PlanetariumPlotCelestialTrajectoryForPredictionOrFlightPlan( } } -// 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 +// PLOT METHOD 3 + +void __cdecl principia__PlanetariumGetFlightPlanSegmentVertices( + Planetarium const* const planetarium, + Plugin const* const plugin, + char const* const vessel_guid, + int const index, + ScaledSpacePoint* const vertices, + int const vertices_size, + int* const vertex_count) { + journal::Method m( + {planetarium, plugin, vessel_guid, index, vertices, vertices_size}, + {vertex_count}); + CHECK_NOTNULL(plugin); + CHECK_NOTNULL(planetarium); + Vessel const& vessel = *plugin->GetVessel(vessel_guid); + CHECK(vessel.has_flight_plan()) << vessel_guid; + auto const segment = vessel.flight_plan().GetSegment(index); + if (segment->empty()) { + return m.Return(); + } + // TODO(egg): this is ugly; we should centralize rendering. + // If this is a burn and we cannot render the beginning of the burn, we + // render none of it, otherwise we try to render the Frenet trihedron at the + // start and we fail. + if (index % 2 == 0 || + segment->front().time >= plugin->renderer().GetPlottingFrame()->t_min()) { + planetarium->PlotMethod3( + *segment, + segment->front().time, + segment->back().time, + plugin->CurrentTime(), + /*reverse=*/false, + [vertices, vertex_count](ScaledSpacePoint const& vertex) { + vertices[(*vertex_count)++] = vertex; + }, + vertices_size); + } + return m.Return(); +} + +void __cdecl principia__PlanetariumGetPredictionVertices( + Planetarium const* const planetarium, + Plugin const* const plugin, + char const* const vessel_guid, + ScaledSpacePoint* const vertices, + int const vertices_size, + int* const vertex_count) { + journal::Method m( + {planetarium, plugin, vessel_guid, vertices, vertices_size}, + {vertex_count}); + CHECK_NOTNULL(plugin); + CHECK_NOTNULL(planetarium); + auto const prediction = plugin->GetVessel(vessel_guid)->prediction(); + planetarium->PlotMethod3( + *prediction, + prediction->front().time, + prediction->back().time, + plugin->CurrentTime(), + /*reverse=*/false, + [vertices, vertex_count](ScaledSpacePoint const& vertex) { + vertices[(*vertex_count)++] = vertex; + }, + vertices_size); + return m.Return(); +} + +void __cdecl principia__PlanetariumGetPsychohistoryVertices( + Planetarium const* const planetarium, + Plugin const* const plugin, + char const* const vessel_guid, + double const max_history_length, + ScaledSpacePoint* const vertices, + int const vertices_size, + int* const vertex_count) { + journal::Method m( + {planetarium, + plugin, + vessel_guid, + max_history_length, + vertices, + vertices_size}, + {vertex_count}); + CHECK_NOTNULL(plugin); + CHECK_NOTNULL(planetarium); + + // Do not plot the psychohistory when there is a target vessel as it is + // misleading. + if (plugin->renderer().HasTargetVessel()) { + return m.Return(); + } else { + auto const vessel = plugin->GetVessel(vessel_guid); + auto const& trajectory = vessel->trajectory(); + planetarium->PlotMethod3( + trajectory, + trajectory + .lower_bound(plugin->CurrentTime() - max_history_length * Second) + ->time, + trajectory.back().time, + plugin->CurrentTime(), + /*reverse=*/true, + [vertices, vertex_count](ScaledSpacePoint const& vertex) { + vertices[(*vertex_count)++] = vertex; + }, + vertices_size); + return m.Return(); + } +} + +// TODO(egg): comment for the rendered past trajectory of the celestial with +// the given index; the trajectory goes back |max_history_length| seconds before +// the present time (or to the earliest time available if the relevant |t_min| +// is more recent). +void __cdecl principia__PlanetariumGetCelestialPastTrajectoryVertices( + Planetarium const* const planetarium, + Plugin const* const plugin, + int const celestial_index, + double const max_history_length, + ScaledSpacePoint* const vertices, + int const vertices_size, + double* const minimal_distance_from_camera, + int* const vertex_count) { + journal::Method m( + {planetarium, + plugin, + celestial_index, + max_history_length, + vertices, + vertices_size}, + {minimal_distance_from_camera, vertex_count}); + 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()) { + *minimal_distance_from_camera = std::numeric_limits::infinity(); + return m.Return(); + } else { + auto const& celestial_trajectory = + plugin->GetCelestial(celestial_index).trajectory(); + Instant const desired_first_time = + plugin->CurrentTime() - max_history_length * Second; + + // Since we would want to plot starting from |desired_first_time|, ask the + // reanimator to reconstruct the past. That may take a while, during which + // time the history will be shorter than desired. + plugin->RequestReanimation(desired_first_time); + + Instant const first_time = + std::max(desired_first_time, celestial_trajectory.t_min()); + Length minimal_distance; + planetarium->PlotMethod3( + celestial_trajectory, + first_time, + /*last_time=*/plugin->CurrentTime(), + /*now=*/plugin->CurrentTime(), + /*reverse=*/true, + [vertices, vertex_count](ScaledSpacePoint const& vertex) { + vertices[(*vertex_count)++] = vertex; + }, + vertices_size, + &minimal_distance); + *minimal_distance_from_camera = minimal_distance / Metre; + return m.Return(); + } +} + +// TODO(egg): comment; the trajectory goes as far as the furthest of the final time // of the prediction or that of the flight plan. void __cdecl principia__PlanetariumGetCelestialFutureTrajectoryVertices( Planetarium const* const planetarium, @@ -315,6 +481,7 @@ void __cdecl principia__PlanetariumGetCelestialFutureTrajectoryVertices( *vertex_count = 0; // Do not plot the past when there is a target vessel as it is misleading. + // TODO(egg): This is the future, not the past! if (plugin->renderer().HasTargetVessel()) { *minimal_distance_from_camera = std::numeric_limits::infinity(); return m.Return(); diff --git a/serialization/journal.proto b/serialization/journal.proto index ff0689dcce..cc2654a15b 100644 --- a/serialization/journal.proto +++ b/serialization/journal.proto @@ -1870,8 +1870,8 @@ message PlanetariumGetCelestialFutureTrajectoryVertices { required fixed64 plugin = 2 [(pointer_to) = "Plugin const"]; required int32 celestial_index = 3; required string vessel_guid = 4; - required fixed64 vertices = 7 [(pointer_to) = "ScaledSpacePoint"]; - required int32 vertices_size = 8; + required fixed64 vertices = 5 [(pointer_to) = "ScaledSpacePoint"]; + required int32 vertices_size = 6; } message Out { required double minimal_distance_from_camera = 1; @@ -1881,6 +1881,90 @@ message PlanetariumGetCelestialFutureTrajectoryVertices { optional Out out = 2; } +message PlanetariumGetCelestialPastTrajectoryVertices { + extend Method { + optional PlanetariumGetCelestialPastTrajectoryVertices extension = 5998; + } + message In { + required fixed64 planetarium = 1 [(pointer_to) = "Planetarium const", + (disposable) = "DisposablePlanetarium", + (is_subject) = true]; + required fixed64 plugin = 2 [(pointer_to) = "Plugin const"]; + required int32 celestial_index = 3; + required double max_history_length = 4; + required fixed64 vertices = 5 [(pointer_to) = "ScaledSpacePoint"]; + required int32 vertices_size = 6; + } + message Out { + required double minimal_distance_from_camera = 1; + required int32 vertex_count = 2; + } + optional In in = 1; + optional Out out = 2; +} + +message PlanetariumGetFlightPlanSegmentVertices { + extend Method { + optional PlanetariumGetFlightPlanSegmentVertices extension = 5997; + } + message In { + required fixed64 planetarium = 1 [(pointer_to) = "Planetarium const", + (disposable) = "DisposablePlanetarium", + (is_subject) = true]; + required fixed64 plugin = 2 [(pointer_to) = "Plugin const"]; + required string vessel_guid = 4; + required int32 index = 5; + required fixed64 vertices = 6 [(pointer_to) = "ScaledSpacePoint"]; + required int32 vertices_size = 7; + } + message Out { + required int32 vertex_count = 1; + } + optional In in = 1; + optional Out out = 2; +} + +message PlanetariumGetPredictionVertices { + extend Method { + optional PlanetariumGetPredictionVertices extension = 5996; + } + message In { + required fixed64 planetarium = 1 [(pointer_to) = "Planetarium const", + (disposable) = "DisposablePlanetarium", + (is_subject) = true]; + required fixed64 plugin = 2 [(pointer_to) = "Plugin const"]; + required string vessel_guid = 4; + required fixed64 vertices = 5 [(pointer_to) = "ScaledSpacePoint"]; + required int32 vertices_size = 6; + } + message Out { + required int32 vertex_count = 1; + } + optional In in = 1; + optional Out out = 2; +} + +message PlanetariumGetPsychohistoryVertices { + extend Method { + optional PlanetariumGetPsychohistoryVertices extension = 5995; + } + message In { + required fixed64 planetarium = 1 [(pointer_to) = "Planetarium const", + (disposable) = "DisposablePlanetarium", + (is_subject) = true]; + required fixed64 plugin = 2 [(pointer_to) = "Plugin const"]; + required string vessel_guid = 4; + required double max_history_length = 5; + required fixed64 vertices = 6 [(pointer_to) = "ScaledSpacePoint"]; + required int32 vertices_size = 7; + } + message Out { + required int32 vertex_count = 1; + } + optional In in = 1; + optional Out out = 2; +} + message PlanetariumPlotFlightPlanSegment { extend Method { optional PlanetariumPlotFlightPlanSegment extension = 5137; From 5cbb127396c4779469252082cad56f9d5671808b Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Sat, 22 Jan 2022 13:05:39 +0100 Subject: [PATCH 08/33] plotter --- ksp_plugin_adapter/gl_lines.cs | 4 +- ksp_plugin_adapter/ksp_plugin_adapter.cs | 84 +++------- ksp_plugin_adapter/ksp_plugin_adapter.csproj | 1 + ksp_plugin_adapter/plotter.cs | 158 +++++++++++++++++++ 4 files changed, 181 insertions(+), 66 deletions(-) create mode 100644 ksp_plugin_adapter/plotter.cs diff --git a/ksp_plugin_adapter/gl_lines.cs b/ksp_plugin_adapter/gl_lines.cs index 834c159886..370806cf80 100644 --- a/ksp_plugin_adapter/gl_lines.cs +++ b/ksp_plugin_adapter/gl_lines.cs @@ -14,9 +14,7 @@ public static void Draw(Action line_vertices) { try { UnityEngine.GL.PushMatrix(); line_material.SetPass(0); - if (!MainWindow.use_meshes) { - UnityEngine.GL.LoadPixelMatrix(); - } + UnityEngine.GL.LoadPixelMatrix(); UnityEngine.GL.Begin(UnityEngine.GL.LINES); line_vertices(); diff --git a/ksp_plugin_adapter/ksp_plugin_adapter.cs b/ksp_plugin_adapter/ksp_plugin_adapter.cs index 1e0fcdfc8d..d75dfbbce3 100644 --- a/ksp_plugin_adapter/ksp_plugin_adapter.cs +++ b/ksp_plugin_adapter/ksp_plugin_adapter.cs @@ -122,6 +122,8 @@ internal IntPtr Plugin() { private UnityEngine.Color target_prediction_colour = XKCDColors.LightMauve; private GLLines.Style target_prediction_style = GLLines.Style.Solid; + private Plotter plotter_; + private readonly List vessel_futures_ = new List(); // The RSAS is the component of the stock KSP autopilot that deals with @@ -262,9 +264,9 @@ private readonly Dictionary [KSPField(isPersistant = true)] private readonly OrbitAnalyser orbit_analyser_; [KSPField(isPersistant = true)] - internal ReferenceFrameSelector plotting_frame_selector_; + internal readonly ReferenceFrameSelector plotting_frame_selector_; [KSPField(isPersistant = true)] - internal MainWindow main_window_; + private MainWindow main_window_; public event Action LockClearing; public event Action WindowsDisposal; @@ -331,6 +333,7 @@ private readonly Dictionary plotting_frame_selector_ = new ReferenceFrameSelector(this, UpdateRenderingFrame, L10N.CacheFormat("#Principia_PlottingFrame")); + plotter_ = new Plotter(this); main_window_ = new MainWindow(this, flight_planner_, orbit_analyser_, @@ -2122,7 +2125,10 @@ private void RenderTrajectories() { XYZ sun_world_position = (XYZ)Planetarium.fetch.Sun.position; using (DisposablePlanetarium planetarium = GLLines.NewPlanetarium(plugin_, sun_world_position)) { - GLLines.Draw(() => { + if (MainWindow.use_meshes) { + plotter_.Plot(planetarium, main_vessel_guid, + main_window_.history_length); + } else GLLines.Draw(() => { PlotCelestialTrajectories(planetarium, main_vessel_guid); // Vessel trajectories. @@ -2256,8 +2262,6 @@ private void PlotCelestialTrajectories(DisposablePlanetarium planetarium, tan_angular_resolution); } - private Dictionary celestial_trajectories_ = new Dictionary(); - private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, string main_vessel_guid, CelestialBody root, @@ -2269,7 +2273,7 @@ private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, double min_distance_from_camera = (root.position - camera_world_position).magnitude; if (!plotting_frame_selector_.FixesBody(root)) { - /*using (DisposableIterator rp2_lines_iterator = + using (DisposableIterator rp2_lines_iterator = planetarium.PlanetariumPlotCelestialTrajectoryForPsychohistory( plugin_, root.flightGlobalsIndex, @@ -2279,64 +2283,18 @@ private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, min_distance_from_camera = Math.Min(min_distance_from_camera, min_past_distance); GLLines.PlotRP2Lines(rp2_lines_iterator, colour, GLLines.Style.Faded); - }*/ + } if (main_vessel_guid != null) { - if (MainWindow.use_meshes) { - unsafe { - UnityEngine.Mesh mesh; - if (MainWindow.reuse_meshes) { - if (!celestial_trajectories_.TryGetValue(root, out mesh)) { - mesh = new UnityEngine.Mesh(); - celestial_trajectories_[root] = mesh; - } - } else { - mesh = new UnityEngine.Mesh(); - } - var vertices = new UnityEngine.Vector3[10_000]; - int vertex_count; - fixed (UnityEngine.Vector3* vertices_data = vertices) { - planetarium.PlanetariumGetCelestialFutureTrajectoryVertices( - plugin_, - root.flightGlobalsIndex, - main_vessel_guid, - (IntPtr)vertices_data, - vertices.Length, - out double min_future_distance, - out vertex_count); - } - mesh.vertices = vertices; - var indices = new int[vertex_count]; - for (int i = 0; i < vertex_count; ++i) { - indices[i] = i; - } - var colours = new UnityEngine.Color[vertices.Length]; - for (int i = 0; i < colours.Length; ++i) { - colours[i] = colour; - } - mesh.colors = colours; - MainWindow.trace += $"{vertex_count} vertices for {root.name}"; - mesh.SetIndices(indices, UnityEngine.MeshTopology.LineStrip, submesh: 0); - if (MainWindow.reuse_meshes) { - mesh.RecalculateBounds(); - } - if (MainWindow.now) { - UnityEngine.Graphics.DrawMeshNow(mesh, UnityEngine.Vector3.zero, UnityEngine.Quaternion.identity); - } else { - UnityEngine.Graphics.DrawMesh(mesh, UnityEngine.Vector3.zero, UnityEngine.Quaternion.identity, GLLines.line_material, MainWindow.layer, PlanetariumCamera.Camera); - } - } - } else { - using (DisposableIterator rp2_lines_iterator = - planetarium. - PlanetariumPlotCelestialTrajectoryForPredictionOrFlightPlan( - plugin_, - root.flightGlobalsIndex, - main_vessel_guid, - out double min_future_distance)) { - min_distance_from_camera = - Math.Min(min_distance_from_camera, min_future_distance); - GLLines.PlotRP2Lines(rp2_lines_iterator, colour, GLLines.Style.Solid); - } + using (DisposableIterator rp2_lines_iterator = + planetarium. + PlanetariumPlotCelestialTrajectoryForPredictionOrFlightPlan( + plugin_, + root.flightGlobalsIndex, + main_vessel_guid, + out double min_future_distance)) { + min_distance_from_camera = + Math.Min(min_distance_from_camera, min_future_distance); + GLLines.PlotRP2Lines(rp2_lines_iterator, colour, GLLines.Style.Solid); } } } diff --git a/ksp_plugin_adapter/ksp_plugin_adapter.csproj b/ksp_plugin_adapter/ksp_plugin_adapter.csproj index 810fa69fc5..260e113fe5 100644 --- a/ksp_plugin_adapter/ksp_plugin_adapter.csproj +++ b/ksp_plugin_adapter/ksp_plugin_adapter.csproj @@ -131,6 +131,7 @@ + diff --git a/ksp_plugin_adapter/plotter.cs b/ksp_plugin_adapter/plotter.cs new file mode 100644 index 0000000000..f0c74990df --- /dev/null +++ b/ksp_plugin_adapter/plotter.cs @@ -0,0 +1,158 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace principia { +namespace ksp_plugin_adapter { + +class Plotter { + public Plotter(PrincipiaPluginAdapter adapter) { + adapter_ = adapter; + } + + public void Plot(DisposablePlanetarium planetarium, + string main_vessel_guid, + double history_length) { + PlotCelestialTrajectories(planetarium, main_vessel_guid, history_length); + } + + private void PlotCelestialTrajectories(DisposablePlanetarium planetarium, + string main_vessel_guid, + double history_length) { + const double degree = Math.PI / 180; + UnityEngine.Camera camera = PlanetariumCamera.Camera; + float vertical_fov = camera.fieldOfView; + float horizontal_fov = + UnityEngine.Camera.VerticalToHorizontalFieldOfView( + vertical_fov, camera.aspect); + // The angle subtended by the pixel closest to the centre of the viewport. + double tan_angular_resolution = Math.Min( + Math.Tan(vertical_fov * degree / 2) / (camera.pixelHeight / 2), + Math.Tan(horizontal_fov * degree / 2) / (camera.pixelWidth / 2)); + PlotSubtreeTrajectories(planetarium, main_vessel_guid, history_length, + Planetarium.fetch.Sun, tan_angular_resolution); + } + + private unsafe void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, + string main_vessel_guid, + double history_length, + CelestialBody root, + double tan_angular_resolution) { + CelestialTrajectories trajectories; + if (!celestial_trajectories_.TryGetValue(root, out trajectories)) { + trajectories = celestial_trajectories_[root] = + new CelestialTrajectories(); + } + var colour = root.orbitDriver?.Renderer?.orbitColor ?? + XKCDColors.SunshineYellow; + var camera_world_position = ScaledSpace.ScaledToLocalSpace( + PlanetariumCamera.fetch.transform.position); + double min_distance_from_camera = + (root.position - camera_world_position).magnitude; + if (!adapter_.plotting_frame_selector_.FixesBody(root)) { + UnityEngine.Mesh past_mesh = MainWindow.reuse_meshes ? trajectories.past : new UnityEngine.Mesh(); + fixed (UnityEngine.Vector3* vertices_data = vertices_) { + planetarium.PlanetariumGetCelestialPastTrajectoryVertices( + adapter_.Plugin(), + root.flightGlobalsIndex, + history_length, + (IntPtr)vertices_data, + vertices_.Length, + out double min_past_distance, + out int vertex_count); + min_distance_from_camera = + Math.Min(min_distance_from_camera, min_past_distance); + DrawLineMesh(past_mesh, vertices_, vertex_count, colour, + GLLines.Style.Faded); + } + if (main_vessel_guid != null) { + UnityEngine.Mesh future_mesh = MainWindow.reuse_meshes ? trajectories.future : new UnityEngine.Mesh(); + fixed (UnityEngine.Vector3* vertices_data = vertices_) { + planetarium.PlanetariumGetCelestialFutureTrajectoryVertices( + adapter_.Plugin(), + root.flightGlobalsIndex, + main_vessel_guid, + (IntPtr)vertices_data, + vertices_.Length, + out double min_future_distance, + out int vertex_count); + DrawLineMesh(future_mesh, vertices_, vertex_count, colour, + GLLines.Style.Solid); + } + } + } + foreach (CelestialBody child in root.orbitingBodies) { + // Plot the trajectory of an orbiting body if it could be separated from + // that of its parent by a pixel of empty space, instead of merely making + // the line wider. + if (child.orbit.ApR / min_distance_from_camera > + 2 * tan_angular_resolution) { + PlotSubtreeTrajectories(planetarium, main_vessel_guid, history_length, + child, tan_angular_resolution); + } + } + } + + private static void DrawLineMesh(UnityEngine.Mesh mesh, + UnityEngine.Vector3[] vertices, + int vertex_count, + UnityEngine.Color colour, + GLLines.Style style) { + mesh.vertices = vertices; + var indices = new int[vertex_count]; + for (int i = 0; i < vertex_count; ++i) { + indices[i] = i; + } + var colours = new UnityEngine.Color[vertices.Length]; + if (style == GLLines.Style.Faded) { + for (int i = 0; i < colours.Length; ++i) { + var faded_colour = colour; + // Fade from the opacity of |colour| (when i = 0) down to 1/4 of that + // opacity. + faded_colour.a *= 1 - (float)(4 * i) / (float)(5 * colours.Length); + colours[i] = faded_colour; + } + } else { + for (int i = 0; i < colours.Length; ++i) { + colours[i] = colour; + } + } + mesh.colors = colours; + mesh.SetIndices( + indices, + style == GLLines.Style.Dashed ? UnityEngine.MeshTopology.Lines + : UnityEngine.MeshTopology.LineStrip, + submesh: 0); + if (MainWindow.reuse_meshes) { + mesh.RecalculateBounds(); + } + if (MainWindow.now) { + UnityEngine.Graphics.DrawMeshNow(mesh, UnityEngine.Vector3.zero, UnityEngine.Quaternion.identity); + } else { + UnityEngine.Graphics.DrawMesh( + mesh, + UnityEngine.Vector3.zero, + UnityEngine.Quaternion.identity, + GLLines.line_material, + MainWindow.layer, + PlanetariumCamera.Camera); + } + } + + private readonly PrincipiaPluginAdapter adapter_; + + private UnityEngine.Vector3[] vertices_ = new UnityEngine.Vector3[10_000]; + + private class CelestialTrajectories { + public UnityEngine.Mesh future = new UnityEngine.Mesh(); + public UnityEngine.Mesh past = new UnityEngine.Mesh(); + } + + private Dictionary celestial_trajectories_ = + new Dictionary(); +} + +} // namespace ksp_plugin_adapter +} // namespace principia From f4a3a61022b8a96e42be367aa228eb8c776f9e87 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Sat, 22 Jan 2022 17:01:11 +0100 Subject: [PATCH 09/33] vessels --- ksp_plugin_adapter/ksp_plugin_adapter.cs | 27 +++---- ksp_plugin_adapter/plotter.cs | 94 +++++++++++++++++++++++- 2 files changed, 106 insertions(+), 15 deletions(-) diff --git a/ksp_plugin_adapter/ksp_plugin_adapter.cs b/ksp_plugin_adapter/ksp_plugin_adapter.cs index d75dfbbce3..2f13ce9280 100644 --- a/ksp_plugin_adapter/ksp_plugin_adapter.cs +++ b/ksp_plugin_adapter/ksp_plugin_adapter.cs @@ -109,18 +109,19 @@ internal IntPtr Plugin() { private bool navball_changed_ = true; private FlightGlobals.SpeedDisplayModes? previous_display_mode_; - private UnityEngine.Color history_colour = XKCDColors.Lime; - private GLLines.Style history_style = GLLines.Style.Faded; - private UnityEngine.Color prediction_colour = XKCDColors.Fuchsia; - private GLLines.Style prediction_style = GLLines.Style.Solid; - private UnityEngine.Color flight_plan_colour = XKCDColors.PeriwinkleBlue; - private GLLines.Style flight_plan_style = GLLines.Style.Dashed; - private UnityEngine.Color burn_colour = XKCDColors.Pink; - private GLLines.Style burn_style = GLLines.Style.Solid; - private UnityEngine.Color target_history_colour = XKCDColors.Goldenrod; - private GLLines.Style target_history_style = GLLines.Style.Faded; - private UnityEngine.Color target_prediction_colour = XKCDColors.LightMauve; - private GLLines.Style target_prediction_style = GLLines.Style.Solid; + // TODO(egg): move all of that to the plotter once the RP2 lines are gone. + internal UnityEngine.Color history_colour = XKCDColors.Lime; + internal GLLines.Style history_style = GLLines.Style.Faded; + internal UnityEngine.Color prediction_colour = XKCDColors.Fuchsia; + internal GLLines.Style prediction_style = GLLines.Style.Solid; + internal UnityEngine.Color flight_plan_colour = XKCDColors.PeriwinkleBlue; + internal GLLines.Style flight_plan_style = GLLines.Style.Dashed; + internal UnityEngine.Color burn_colour = XKCDColors.Pink; + internal GLLines.Style burn_style = GLLines.Style.Solid; + internal UnityEngine.Color target_history_colour = XKCDColors.Goldenrod; + internal GLLines.Style target_history_style = GLLines.Style.Faded; + internal UnityEngine.Color target_prediction_colour = XKCDColors.LightMauve; + internal GLLines.Style target_prediction_style = GLLines.Style.Solid; private Plotter plotter_; @@ -2168,7 +2169,7 @@ private void RenderTrajectories() { GLLines.PlotRP2Lines(rp2_lines_iterator, target_history_colour, target_history_style); - }// TODO COMMENT ALL PAST CELESTIAL PLOTTING, ADD MESH PATH + } using (DisposableIterator rp2_lines_iterator = planetarium.PlanetariumPlotPrediction( plugin_, diff --git a/ksp_plugin_adapter/plotter.cs b/ksp_plugin_adapter/plotter.cs index f0c74990df..305cb63f03 100644 --- a/ksp_plugin_adapter/plotter.cs +++ b/ksp_plugin_adapter/plotter.cs @@ -16,6 +16,94 @@ public void Plot(DisposablePlanetarium planetarium, string main_vessel_guid, double history_length) { PlotCelestialTrajectories(planetarium, main_vessel_guid, history_length); + if (main_vessel_guid == null) { + return; + } + PlotVesselTrajectories(planetarium, main_vessel_guid, history_length); + } + + private void PlotVesselTrajectories(DisposablePlanetarium planetarium, + string main_vessel_guid, + double history_length) { + // Main vessel psychohistory and prediction. + using (DisposableIterator rp2_lines_iterator = + planetarium.PlanetariumPlotPsychohistory( + Plugin, + main_vessel_guid, + history_length)) { + GLLines.PlotRP2Lines(rp2_lines_iterator, + adapter_.history_colour, + adapter_.history_style); + } + using (DisposableIterator rp2_lines_iterator = + planetarium.PlanetariumPlotPrediction( + Plugin, + main_vessel_guid)) { + GLLines.PlotRP2Lines(rp2_lines_iterator, + adapter_.prediction_colour, + adapter_.prediction_style); + } + // Target psychohistory and prediction. + string target_id = FlightGlobals.fetch.VesselTarget?.GetVessel()?.id. + ToString(); + if (FlightGlobals.ActiveVessel != null && + !adapter_.plotting_frame_selector_.target_frame_selected && + target_id != null && + Plugin.HasVessel(target_id)) { + fixed (UnityEngine.Vector3* vertices_data = vertices_) { + planetarium.PlanetariumGetPsychohistoryVertices( + Plugin, + target_id, + history_length, + vertices_,)) { + GLLines.PlotRP2Lines(rp2_lines_iterator, + adapter_.target_history_colour, + adapter_.target_history_style); + }// TODO COMMENT ALL PAST CELESTIAL PLOTTING, ADD MESH PATH + using (DisposableIterator rp2_lines_iterator = + planetarium.PlanetariumPlotPrediction( + Plugin, + target_id)) { + GLLines.PlotRP2Lines(rp2_lines_iterator, + adapter_.target_prediction_colour, + adapter_.target_prediction_style); + } + } + // Main vessel flight plan. + if (Plugin.FlightPlanExists(main_vessel_guid)) { + int number_of_anomalous_manœuvres = + Plugin.FlightPlanNumberOfAnomalousManoeuvres(main_vessel_guid); + int number_of_manœuvres = + Plugin.FlightPlanNumberOfManoeuvres(main_vessel_guid); + int number_of_segments = + Plugin.FlightPlanNumberOfSegments(main_vessel_guid); + for (int i = 0; i < number_of_segments; ++i) { + bool is_burn = i % 2 == 1; + using (DisposableIterator rp2_lines_iterator = + planetarium.PlanetariumPlotFlightPlanSegment( + Plugin, + main_vessel_guid, + i)) { + GLLines.PlotRP2Lines(rp2_lines_iterator, + is_burn + ? adapter_.burn_colour + : adapter_.flight_plan_colour, + is_burn + ? adapter_.burn_style + : adapter_.flight_plan_style); + } + if (is_burn) { + int manœuvre_index = i / 2; + if (manœuvre_index < + number_of_manœuvres - number_of_anomalous_manœuvres) { + NavigationManoeuvreFrenetTrihedron manœuvre = + Plugin.FlightPlanGetManoeuvreFrenetTrihedron( + main_vessel_guid, + manœuvre_index); + } + } + } + } } private void PlotCelestialTrajectories(DisposablePlanetarium planetarium, @@ -55,7 +143,7 @@ private unsafe void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, UnityEngine.Mesh past_mesh = MainWindow.reuse_meshes ? trajectories.past : new UnityEngine.Mesh(); fixed (UnityEngine.Vector3* vertices_data = vertices_) { planetarium.PlanetariumGetCelestialPastTrajectoryVertices( - adapter_.Plugin(), + Plugin, root.flightGlobalsIndex, history_length, (IntPtr)vertices_data, @@ -71,7 +159,7 @@ private unsafe void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, UnityEngine.Mesh future_mesh = MainWindow.reuse_meshes ? trajectories.future : new UnityEngine.Mesh(); fixed (UnityEngine.Vector3* vertices_data = vertices_) { planetarium.PlanetariumGetCelestialFutureTrajectoryVertices( - adapter_.Plugin(), + Plugin, root.flightGlobalsIndex, main_vessel_guid, (IntPtr)vertices_data, @@ -143,6 +231,8 @@ private static void DrawLineMesh(UnityEngine.Mesh mesh, private readonly PrincipiaPluginAdapter adapter_; + private IntPtr Plugin => adapter_.Plugin(); + private UnityEngine.Vector3[] vertices_ = new UnityEngine.Vector3[10_000]; private class CelestialTrajectories { From 9e74ad67df6fb84227f08002dfc55e1a97fbe2bb Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Sat, 22 Jan 2022 23:25:51 +0100 Subject: [PATCH 10/33] more vessel --- ksp_plugin_adapter/ksp_plugin_adapter.cs | 97 ++++++----- ksp_plugin_adapter/plotter.cs | 201 +++++++++++------------ 2 files changed, 147 insertions(+), 151 deletions(-) diff --git a/ksp_plugin_adapter/ksp_plugin_adapter.cs b/ksp_plugin_adapter/ksp_plugin_adapter.cs index 2f13ce9280..687b1e41e1 100644 --- a/ksp_plugin_adapter/ksp_plugin_adapter.cs +++ b/ksp_plugin_adapter/ksp_plugin_adapter.cs @@ -2129,54 +2129,59 @@ private void RenderTrajectories() { if (MainWindow.use_meshes) { plotter_.Plot(planetarium, main_vessel_guid, main_window_.history_length); - } else GLLines.Draw(() => { - PlotCelestialTrajectories(planetarium, main_vessel_guid); + } + GLLines.Draw(() => { + if (!MainWindow.use_meshes) { + 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_, - main_vessel_guid, - main_window_.history_length)) { - GLLines.PlotRP2Lines(rp2_lines_iterator, - history_colour, - history_style); - } - using (DisposableIterator rp2_lines_iterator = - planetarium.PlanetariumPlotPrediction( - plugin_, - main_vessel_guid)) { - GLLines.PlotRP2Lines(rp2_lines_iterator, - prediction_colour, - prediction_style); - } - // Target psychohistory and prediction. - string target_id = FlightGlobals.fetch.VesselTarget?.GetVessel()?.id. - ToString(); - if (FlightGlobals.ActiveVessel != null && - !plotting_frame_selector_.target_frame_selected && - target_id != null && - plugin_.HasVessel(target_id)) { + if (!MainWindow.use_meshes) { + // Main vessel psychohistory and prediction. using (DisposableIterator rp2_lines_iterator = planetarium.PlanetariumPlotPsychohistory( plugin_, - target_id, + main_vessel_guid, main_window_.history_length)) { GLLines.PlotRP2Lines(rp2_lines_iterator, - target_history_colour, - target_history_style); + history_colour, + history_style); } using (DisposableIterator rp2_lines_iterator = planetarium.PlanetariumPlotPrediction( plugin_, - target_id)) { + main_vessel_guid)) { GLLines.PlotRP2Lines(rp2_lines_iterator, - target_prediction_colour, - target_prediction_style); + prediction_colour, + prediction_style); + } + // Target psychohistory and prediction. + string target_id = FlightGlobals.fetch.VesselTarget?.GetVessel()?.id. + ToString(); + if (FlightGlobals.ActiveVessel != null && + !plotting_frame_selector_.target_frame_selected && + target_id != null && + plugin_.HasVessel(target_id)) { + using (DisposableIterator rp2_lines_iterator = + planetarium.PlanetariumPlotPsychohistory( + plugin_, + target_id, + main_window_.history_length)) { + GLLines.PlotRP2Lines(rp2_lines_iterator, + target_history_colour, + target_history_style); + } + using (DisposableIterator rp2_lines_iterator = + planetarium.PlanetariumPlotPrediction( + plugin_, + target_id)) { + GLLines.PlotRP2Lines(rp2_lines_iterator, + target_prediction_colour, + target_prediction_style); + } } } // Main vessel flight plan. @@ -2199,18 +2204,20 @@ private void RenderTrajectories() { } Vector3d position_at_start = (Vector3d)rendered_segments. IteratorGetDiscreteTrajectoryXYZ(); - using (DisposableIterator rp2_lines_iterator = - planetarium.PlanetariumPlotFlightPlanSegment( - plugin_, - main_vessel_guid, - i)) { - GLLines.PlotRP2Lines(rp2_lines_iterator, - is_burn - ? burn_colour - : flight_plan_colour, - is_burn - ? burn_style - : flight_plan_style); + if (!MainWindow.use_meshes) { + using (DisposableIterator rp2_lines_iterator = + planetarium.PlanetariumPlotFlightPlanSegment( + plugin_, + main_vessel_guid, + i)) { + GLLines.PlotRP2Lines(rp2_lines_iterator, + is_burn + ? burn_colour + : flight_plan_colour, + is_burn + ? burn_style + : flight_plan_style); + } } if (is_burn) { int manœuvre_index = i / 2; diff --git a/ksp_plugin_adapter/plotter.cs b/ksp_plugin_adapter/plotter.cs index 305cb63f03..3bed416b6a 100644 --- a/ksp_plugin_adapter/plotter.cs +++ b/ksp_plugin_adapter/plotter.cs @@ -12,37 +12,39 @@ public Plotter(PrincipiaPluginAdapter adapter) { adapter_ = adapter; } - public void Plot(DisposablePlanetarium planetarium, - string main_vessel_guid, - double history_length) { - PlotCelestialTrajectories(planetarium, main_vessel_guid, history_length); - if (main_vessel_guid == null) { - return; + public unsafe void Plot(DisposablePlanetarium planetarium, + string main_vessel_guid, + double history_length) { + fixed (UnityEngine.Vector3* vertices_data = vertices_) { + vertices_data_ = (IntPtr)vertices_data; + PlotCelestialTrajectories(planetarium, main_vessel_guid, history_length); + if (main_vessel_guid == null) { + return; + } + PlotVesselTrajectories(planetarium, main_vessel_guid, history_length); + vertices_data_ = IntPtr.Zero; } - PlotVesselTrajectories(planetarium, main_vessel_guid, history_length); } private void PlotVesselTrajectories(DisposablePlanetarium planetarium, string main_vessel_guid, double history_length) { - // Main vessel psychohistory and prediction. - using (DisposableIterator rp2_lines_iterator = - planetarium.PlanetariumPlotPsychohistory( - Plugin, - main_vessel_guid, - history_length)) { - GLLines.PlotRP2Lines(rp2_lines_iterator, - adapter_.history_colour, - adapter_.history_style); - } - using (DisposableIterator rp2_lines_iterator = - planetarium.PlanetariumPlotPrediction( - Plugin, - main_vessel_guid)) { - GLLines.PlotRP2Lines(rp2_lines_iterator, - adapter_.prediction_colour, - adapter_.prediction_style); - } + planetarium.PlanetariumGetPsychohistoryVertices( + Plugin, + main_vessel_guid, + history_length, + vertices_data_, + vertices_.Length, + out vertex_count_); + DrawLineMesh(psychohistory_, adapter_.history_colour, adapter_.history_style); + planetarium.PlanetariumGetPredictionVertices( + Plugin, + main_vessel_guid, + vertices_data_, + vertices_.Length, + out vertex_count_); + DrawLineMesh(prediction_, adapter_.prediction_colour, adapter_.prediction_style); + // Target psychohistory and prediction. string target_id = FlightGlobals.fetch.VesselTarget?.GetVessel()?.id. ToString(); @@ -50,24 +52,15 @@ private void PlotVesselTrajectories(DisposablePlanetarium planetarium, !adapter_.plotting_frame_selector_.target_frame_selected && target_id != null && Plugin.HasVessel(target_id)) { - fixed (UnityEngine.Vector3* vertices_data = vertices_) { - planetarium.PlanetariumGetPsychohistoryVertices( - Plugin, - target_id, - history_length, - vertices_,)) { - GLLines.PlotRP2Lines(rp2_lines_iterator, - adapter_.target_history_colour, - adapter_.target_history_style); - }// TODO COMMENT ALL PAST CELESTIAL PLOTTING, ADD MESH PATH - using (DisposableIterator rp2_lines_iterator = - planetarium.PlanetariumPlotPrediction( - Plugin, - target_id)) { - GLLines.PlotRP2Lines(rp2_lines_iterator, - adapter_.target_prediction_colour, - adapter_.target_prediction_style); - } + planetarium.PlanetariumGetPsychohistoryVertices( + Plugin, + target_id, + history_length, + vertices_data_, + vertices_.Length, + out vertex_count_); + DrawLineMesh(target_psychohistory_, adapter_.target_history_colour, + adapter_.target_history_style); } // Main vessel flight plan. if (Plugin.FlightPlanExists(main_vessel_guid)) { @@ -77,31 +70,23 @@ private void PlotVesselTrajectories(DisposablePlanetarium planetarium, Plugin.FlightPlanNumberOfManoeuvres(main_vessel_guid); int number_of_segments = Plugin.FlightPlanNumberOfSegments(main_vessel_guid); + for (int i = flight_plan_segments_.Count; i < number_of_segments; ++i) { + flight_plan_segments_.Add(new UnityEngine.Mesh()); + } for (int i = 0; i < number_of_segments; ++i) { bool is_burn = i % 2 == 1; - using (DisposableIterator rp2_lines_iterator = - planetarium.PlanetariumPlotFlightPlanSegment( - Plugin, - main_vessel_guid, - i)) { - GLLines.PlotRP2Lines(rp2_lines_iterator, - is_burn - ? adapter_.burn_colour - : adapter_.flight_plan_colour, - is_burn - ? adapter_.burn_style - : adapter_.flight_plan_style); - } - if (is_burn) { - int manœuvre_index = i / 2; - if (manœuvre_index < - number_of_manœuvres - number_of_anomalous_manœuvres) { - NavigationManoeuvreFrenetTrihedron manœuvre = - Plugin.FlightPlanGetManoeuvreFrenetTrihedron( - main_vessel_guid, - manœuvre_index); - } - } + planetarium.PlanetariumGetFlightPlanSegmentVertices( + Plugin, + main_vessel_guid, + i, + vertices_data_, + vertices_.Length, + out vertex_count_); + DrawLineMesh(flight_plan_segments_[i], + is_burn ? adapter_.burn_colour + : adapter_.flight_plan_colour, + is_burn ? adapter_.burn_style + : adapter_.flight_plan_style); } } } @@ -123,11 +108,11 @@ private void PlotCelestialTrajectories(DisposablePlanetarium planetarium, Planetarium.fetch.Sun, tan_angular_resolution); } - private unsafe void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, - string main_vessel_guid, - double history_length, - CelestialBody root, - double tan_angular_resolution) { + private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, + string main_vessel_guid, + double history_length, + CelestialBody root, + double tan_angular_resolution) { CelestialTrajectories trajectories; if (!celestial_trajectories_.TryGetValue(root, out trajectories)) { trajectories = celestial_trajectories_[root] = @@ -140,35 +125,27 @@ private unsafe void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, double min_distance_from_camera = (root.position - camera_world_position).magnitude; if (!adapter_.plotting_frame_selector_.FixesBody(root)) { - UnityEngine.Mesh past_mesh = MainWindow.reuse_meshes ? trajectories.past : new UnityEngine.Mesh(); - fixed (UnityEngine.Vector3* vertices_data = vertices_) { - planetarium.PlanetariumGetCelestialPastTrajectoryVertices( + planetarium.PlanetariumGetCelestialPastTrajectoryVertices( + Plugin, + root.flightGlobalsIndex, + history_length, + vertices_data_, + vertices_.Length, + out double min_past_distance, + out vertex_count_); + min_distance_from_camera = + Math.Min(min_distance_from_camera, min_past_distance); + DrawLineMesh(trajectories.past, colour, GLLines.Style.Faded); + if (main_vessel_guid != null) { + planetarium.PlanetariumGetCelestialFutureTrajectoryVertices( Plugin, root.flightGlobalsIndex, - history_length, - (IntPtr)vertices_data, + main_vessel_guid, + vertices_data_, vertices_.Length, - out double min_past_distance, - out int vertex_count); - min_distance_from_camera = - Math.Min(min_distance_from_camera, min_past_distance); - DrawLineMesh(past_mesh, vertices_, vertex_count, colour, - GLLines.Style.Faded); - } - if (main_vessel_guid != null) { - UnityEngine.Mesh future_mesh = MainWindow.reuse_meshes ? trajectories.future : new UnityEngine.Mesh(); - fixed (UnityEngine.Vector3* vertices_data = vertices_) { - planetarium.PlanetariumGetCelestialFutureTrajectoryVertices( - Plugin, - root.flightGlobalsIndex, - main_vessel_guid, - (IntPtr)vertices_data, - vertices_.Length, - out double min_future_distance, - out int vertex_count); - DrawLineMesh(future_mesh, vertices_, vertex_count, colour, - GLLines.Style.Solid); - } + out double min_future_distance, + out vertex_count_); + DrawLineMesh(trajectories.future, colour, GLLines.Style.Solid); } } foreach (CelestialBody child in root.orbitingBodies) { @@ -183,17 +160,19 @@ private unsafe void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, } } - private static void DrawLineMesh(UnityEngine.Mesh mesh, - UnityEngine.Vector3[] vertices, - int vertex_count, - UnityEngine.Color colour, - GLLines.Style style) { - mesh.vertices = vertices; - var indices = new int[vertex_count]; - for (int i = 0; i < vertex_count; ++i) { + private void DrawLineMesh(UnityEngine.Mesh mesh, + UnityEngine.Color colour, + GLLines.Style style) { + filled_vertices_ |= vertex_count_ == vertices_.Length; + if (!MainWindow.reuse_meshes) { + mesh = new UnityEngine.Mesh(); + } + mesh.vertices = vertices_; + var indices = new int[vertex_count_]; + for (int i = 0; i < vertex_count_; ++i) { indices[i] = i; } - var colours = new UnityEngine.Color[vertices.Length]; + var colours = new UnityEngine.Color[vertices_.Length]; if (style == GLLines.Style.Faded) { for (int i = 0; i < colours.Length; ++i) { var faded_colour = colour; @@ -234,6 +213,11 @@ private static void DrawLineMesh(UnityEngine.Mesh mesh, private IntPtr Plugin => adapter_.Plugin(); private UnityEngine.Vector3[] vertices_ = new UnityEngine.Vector3[10_000]; + // A pointer to vertices_; vertices_ should be pinned and should not be + // reassigned while this is non-null. + private IntPtr vertices_data_ = IntPtr.Zero; + private int vertex_count_; + private bool filled_vertices_ = false; private class CelestialTrajectories { public UnityEngine.Mesh future = new UnityEngine.Mesh(); @@ -242,6 +226,11 @@ private class CelestialTrajectories { private Dictionary celestial_trajectories_ = new Dictionary(); + private UnityEngine.Mesh psychohistory_ = new UnityEngine.Mesh(); + private UnityEngine.Mesh prediction_ = new UnityEngine.Mesh(); + private List flight_plan_segments_ = new List(); + private UnityEngine.Mesh target_psychohistory_ = new UnityEngine.Mesh(); + private UnityEngine.Mesh target_prediction_ = new UnityEngine.Mesh(); } } // namespace ksp_plugin_adapter From 8ae323d55f7f58d6c49c342eec6ca4cedf931c32 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Sun, 23 Jan 2022 02:53:52 +0100 Subject: [PATCH 11/33] timing --- ksp_plugin_adapter/ksp_plugin_adapter.cs | 22 ++++++++++++++++------ ksp_plugin_adapter/main_window.cs | 5 ++--- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/ksp_plugin_adapter/ksp_plugin_adapter.cs b/ksp_plugin_adapter/ksp_plugin_adapter.cs index 687b1e41e1..dbfa547305 100644 --- a/ksp_plugin_adapter/ksp_plugin_adapter.cs +++ b/ksp_plugin_adapter/ksp_plugin_adapter.cs @@ -874,8 +874,8 @@ private void LateUpdate() { if (map_renderer_ == null) { map_renderer_ = PlanetariumCamera.Camera.gameObject. AddComponent(); - map_renderer_.pre_cull = () => {if (MainWindow.pre_cull) RenderTrajectories();}; - map_renderer_.post_render = () => {if (!MainWindow.pre_cull) RenderTrajectories();}; + map_renderer_.pre_cull = () => { if (MainWindow.pre_cull) RenderTrajectoriesWithMeshes(); }; + map_renderer_.post_render = () => { RenderTrajectories(); if (!MainWindow.pre_cull) RenderTrajectoriesWithMeshes(); }; } if (galaxy_cube_rotator_ == null) { @@ -2103,6 +2103,20 @@ private void HandleMapViewClicks() { } } + private void RenderTrajectoriesWithMeshes() { + if (!MainWindow.use_meshes) { + return; + } + string main_vessel_guid = PredictedVessel()?.id.ToString(); + if (MapView.MapIsEnabled) { + XYZ sun_world_position = (XYZ)Planetarium.fetch.Sun.position; + using (DisposablePlanetarium planetarium = + GLLines.NewPlanetarium(plugin_, sun_world_position)) { + plotter_.Plot(planetarium, main_vessel_guid, main_window_.history_length); + } + } + } + private void RenderTrajectories() { if (!PluginRunning()) { return; @@ -2126,10 +2140,6 @@ private void RenderTrajectories() { XYZ sun_world_position = (XYZ)Planetarium.fetch.Sun.position; using (DisposablePlanetarium planetarium = GLLines.NewPlanetarium(plugin_, sun_world_position)) { - if (MainWindow.use_meshes) { - plotter_.Plot(planetarium, main_vessel_guid, - main_window_.history_length); - } GLLines.Draw(() => { if (!MainWindow.use_meshes) { PlotCelestialTrajectories(planetarium, main_vessel_guid); diff --git a/ksp_plugin_adapter/main_window.cs b/ksp_plugin_adapter/main_window.cs index 910b9c55ba..fa0a490820 100644 --- a/ksp_plugin_adapter/main_window.cs +++ b/ksp_plugin_adapter/main_window.cs @@ -276,8 +276,8 @@ private void RenderKSPFeatures() { } public static bool use_meshes = false; - public static bool reuse_meshes = false; - public static bool pre_cull = false; + public static bool reuse_meshes = true; + public static bool pre_cull => !now; public static bool now = false; public static int layer = 9; public static string trace = ""; @@ -286,7 +286,6 @@ private void RenderLoggingSettings() { use_meshes = UnityEngine.GUILayout.Toggle(use_meshes, "Use meshes"); reuse_meshes = UnityEngine.GUILayout.Toggle(reuse_meshes, "Reuse meshes"); now = UnityEngine.GUILayout.Toggle(now, "Now post-render"); - pre_cull = !now; if (int.TryParse(UnityEngine.GUILayout.TextArea(layer.ToString()), out int result)) { layer = result; } From 6ce46fb1de3d4845c6765543d15cfb43f9f119d4 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Sun, 23 Jan 2022 03:55:44 +0100 Subject: [PATCH 12/33] wrong origin --- ksp_plugin_adapter/gl_lines.cs | 19 +++++++++++-------- ksp_plugin_adapter/ksp_plugin_adapter.cs | 3 +++ ksp_plugin_adapter/plotter.cs | 1 + 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/ksp_plugin_adapter/gl_lines.cs b/ksp_plugin_adapter/gl_lines.cs index 370806cf80..8b29fc72ef 100644 --- a/ksp_plugin_adapter/gl_lines.cs +++ b/ksp_plugin_adapter/gl_lines.cs @@ -66,14 +66,17 @@ public static DisposablePlanetarium NewPlanetarium(IntPtr plugin, double m11 = camera.projectionMatrix[1, 1]; double field_of_view = Math.Atan2(Math.Sqrt(m00 * m00 + m11 * m11), m00 * m11); - return plugin.PlanetariumCreate(sun_world_position, - (XYZ)(Vector3d)opengl_camera_x_in_world, - (XYZ)(Vector3d)opengl_camera_y_in_world, - (XYZ)(Vector3d)opengl_camera_z_in_world, - (XYZ)(Vector3d)camera_position_in_world, - focal:1, - field_of_view, ScaledSpace.InverseScaleFactor, - (XYZ)ScaledSpace.LocalToScaledSpace(Vector3d.zero)); + return plugin.PlanetariumCreate( + sun_world_position, + (XYZ)(Vector3d)opengl_camera_x_in_world, + (XYZ)(Vector3d)opengl_camera_y_in_world, + (XYZ)(Vector3d)opengl_camera_z_in_world, + (XYZ)(Vector3d)camera_position_in_world, + focal:1, + field_of_view, + ScaledSpace.InverseScaleFactor, + scaled_space_origin: (XYZ)ScaledSpace.ScaledToLocalSpace( + Vector3d.zero)); } public static void PlotRP2Lines(DisposableIterator rp2_lines_iterator, diff --git a/ksp_plugin_adapter/ksp_plugin_adapter.cs b/ksp_plugin_adapter/ksp_plugin_adapter.cs index dbfa547305..0108f6cfca 100644 --- a/ksp_plugin_adapter/ksp_plugin_adapter.cs +++ b/ksp_plugin_adapter/ksp_plugin_adapter.cs @@ -2104,6 +2104,9 @@ private void HandleMapViewClicks() { } private void RenderTrajectoriesWithMeshes() { + if (!PluginRunning()) { + return; + } if (!MainWindow.use_meshes) { return; } diff --git a/ksp_plugin_adapter/plotter.cs b/ksp_plugin_adapter/plotter.cs index 3bed416b6a..39c189eaef 100644 --- a/ksp_plugin_adapter/plotter.cs +++ b/ksp_plugin_adapter/plotter.cs @@ -104,6 +104,7 @@ private void PlotCelestialTrajectories(DisposablePlanetarium planetarium, double tan_angular_resolution = Math.Min( Math.Tan(vertical_fov * degree / 2) / (camera.pixelHeight / 2), Math.Tan(horizontal_fov * degree / 2) / (camera.pixelWidth / 2)); + MainWindow.trace = $"{UnityEngine.Camera.current.name}, {PlanetariumCamera.Camera.name} {ScaledSpace.LocalToScaledSpace(Vector3d.zero)}"; PlotSubtreeTrajectories(planetarium, main_vessel_guid, history_length, Planetarium.fetch.Sun, tan_angular_resolution); } From d1ba73dd67f0f0acd621c076721a72df03db1205 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Sun, 23 Jan 2022 13:41:33 +0100 Subject: [PATCH 13/33] initialize --- ksp_plugin/interface_planetarium.cpp | 6 ++++++ ksp_plugin_adapter/plotter.cs | 14 ++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/ksp_plugin/interface_planetarium.cpp b/ksp_plugin/interface_planetarium.cpp index 165bbcc329..fd14503a5b 100644 --- a/ksp_plugin/interface_planetarium.cpp +++ b/ksp_plugin/interface_planetarium.cpp @@ -305,6 +305,8 @@ void __cdecl principia__PlanetariumGetFlightPlanSegmentVertices( {vertex_count}); CHECK_NOTNULL(plugin); CHECK_NOTNULL(planetarium); + *vertex_count = 0; + Vessel const& vessel = *plugin->GetVessel(vessel_guid); CHECK(vessel.has_flight_plan()) << vessel_guid; auto const segment = vessel.flight_plan().GetSegment(index); @@ -343,6 +345,8 @@ void __cdecl principia__PlanetariumGetPredictionVertices( {vertex_count}); CHECK_NOTNULL(plugin); CHECK_NOTNULL(planetarium); + *vertex_count = 0; + auto const prediction = plugin->GetVessel(vessel_guid)->prediction(); planetarium->PlotMethod3( *prediction, @@ -375,6 +379,7 @@ void __cdecl principia__PlanetariumGetPsychohistoryVertices( {vertex_count}); CHECK_NOTNULL(plugin); CHECK_NOTNULL(planetarium); + *vertex_count = 0; // Do not plot the psychohistory when there is a target vessel as it is // misleading. @@ -422,6 +427,7 @@ void __cdecl principia__PlanetariumGetCelestialPastTrajectoryVertices( {minimal_distance_from_camera, vertex_count}); CHECK_NOTNULL(plugin); CHECK_NOTNULL(planetarium); + *vertex_count = 0; // Do not plot the past when there is a target vessel as it is misleading. if (plugin->renderer().HasTargetVessel()) { diff --git a/ksp_plugin_adapter/plotter.cs b/ksp_plugin_adapter/plotter.cs index 39c189eaef..377837ca7a 100644 --- a/ksp_plugin_adapter/plotter.cs +++ b/ksp_plugin_adapter/plotter.cs @@ -61,13 +61,17 @@ private void PlotVesselTrajectories(DisposablePlanetarium planetarium, out vertex_count_); DrawLineMesh(target_psychohistory_, adapter_.target_history_colour, adapter_.target_history_style); + planetarium.PlanetariumGetPredictionVertices( + Plugin, + target_id, + vertices_data_, + vertices_.Length, + out vertex_count_); + DrawLineMesh(target_prediction_, adapter_.target_prediction_colour, + adapter_.target_prediction_style); } // Main vessel flight plan. if (Plugin.FlightPlanExists(main_vessel_guid)) { - int number_of_anomalous_manœuvres = - Plugin.FlightPlanNumberOfAnomalousManoeuvres(main_vessel_guid); - int number_of_manœuvres = - Plugin.FlightPlanNumberOfManoeuvres(main_vessel_guid); int number_of_segments = Plugin.FlightPlanNumberOfSegments(main_vessel_guid); for (int i = flight_plan_segments_.Count; i < number_of_segments; ++i) { @@ -146,6 +150,8 @@ private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, vertices_.Length, out double min_future_distance, out vertex_count_); + min_distance_from_camera = + Math.Min(min_distance_from_camera, min_future_distance); DrawLineMesh(trajectories.future, colour, GLLines.Style.Solid); } } From 925972082572ed2925b662592d7f3db7b4b5e425 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Sun, 23 Jan 2022 13:41:58 +0100 Subject: [PATCH 14/33] last_time --- ksp_plugin/interface_planetarium.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ksp_plugin/interface_planetarium.cpp b/ksp_plugin/interface_planetarium.cpp index fd14503a5b..d884153063 100644 --- a/ksp_plugin/interface_planetarium.cpp +++ b/ksp_plugin/interface_planetarium.cpp @@ -393,8 +393,8 @@ void __cdecl principia__PlanetariumGetPsychohistoryVertices( trajectory .lower_bound(plugin->CurrentTime() - max_history_length * Second) ->time, - trajectory.back().time, - plugin->CurrentTime(), + /*last_time=*/plugin->CurrentTime(), + /*now=*/plugin->CurrentTime(), /*reverse=*/true, [vertices, vertex_count](ScaledSpacePoint const& vertex) { vertices[(*vertex_count)++] = vertex; From 19be0871e010201e543124bb875f44505282defd Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Sun, 23 Jan 2022 15:20:57 +0100 Subject: [PATCH 15/33] emit the first point --- ksp_plugin/planetarium.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ksp_plugin/planetarium.cpp b/ksp_plugin/planetarium.cpp index c454cbc5b3..a91a8146fb 100644 --- a/ksp_plugin/planetarium.cpp +++ b/ksp_plugin/planetarium.cpp @@ -308,6 +308,9 @@ void Planetarium::PlotMethod3( initial_degrees_of_freedom.velocity(); Time Δt = final_time - previous_time; + add_point(WorldToScaledSpace(plotting_to_world_(previous_position))); + int points_added = 1; + Instant t; double estimated_tan²_error; std::optional> @@ -315,13 +318,9 @@ void Planetarium::PlotMethod3( Position position; Square minimal_squared_distance = Infinity>; - std::optional> last_endpoint; - - int steps_accepted = 0; - goto estimate_tan²_error; - while (steps_accepted < max_steps && + while (points_added < max_steps && direction * (previous_time - final_time) < Time{}) { do { // One square root because we have squared errors, another one because the @@ -351,7 +350,6 @@ void Planetarium::PlotMethod3( perspective_.Tan²AngularDistance(extrapolated_position, position) / 16; } while (estimated_tan²_error > tan²_angular_resolution); - ++steps_accepted; previous_time = t; previous_position = position; @@ -359,6 +357,8 @@ void Planetarium::PlotMethod3( to_plotting_frame_at_t(*degrees_of_freedom_in_barycentric).velocity(); add_point(WorldToScaledSpace(plotting_to_world_(position))); + ++points_added; + if (minimal_distance != nullptr) { minimal_squared_distance = std::min(minimal_squared_distance, From ef9a72a4ba46ec70479ec7de743967d93dd8f867 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Sun, 23 Jan 2022 15:22:45 +0100 Subject: [PATCH 16/33] naming --- ksp_plugin/planetarium.cpp | 2 +- ksp_plugin/planetarium.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ksp_plugin/planetarium.cpp b/ksp_plugin/planetarium.cpp index a91a8146fb..8309544620 100644 --- a/ksp_plugin/planetarium.cpp +++ b/ksp_plugin/planetarium.cpp @@ -282,7 +282,7 @@ void Planetarium::PlotMethod3( Instant const& now, bool reverse, std::function add_point, - int const max_steps, + int const max_points, Length* minimal_distance) const { double const tan²_angular_resolution = Pow<2>(parameters_.tan_angular_resolution_); diff --git a/ksp_plugin/planetarium.hpp b/ksp_plugin/planetarium.hpp index 3db3d1800a..22ca346417 100644 --- a/ksp_plugin/planetarium.hpp +++ b/ksp_plugin/planetarium.hpp @@ -131,7 +131,7 @@ class Planetarium { Instant const& now, bool reverse, std::function add_point, - int max_steps, + int max_points, Length* minimal_distance = nullptr) const; private: From 2a136d11e5367ead4d966a0b558c79384fd9481b Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Sun, 23 Jan 2022 16:08:39 +0100 Subject: [PATCH 17/33] fix fading --- ksp_plugin/planetarium.cpp | 2 +- ksp_plugin_adapter/plotter.cs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ksp_plugin/planetarium.cpp b/ksp_plugin/planetarium.cpp index 8309544620..f1264022b1 100644 --- a/ksp_plugin/planetarium.cpp +++ b/ksp_plugin/planetarium.cpp @@ -320,7 +320,7 @@ void Planetarium::PlotMethod3( goto estimate_tan²_error; - while (points_added < max_steps && + while (points_added < max_points && direction * (previous_time - final_time) < Time{}) { do { // One square root because we have squared errors, another one because the diff --git a/ksp_plugin_adapter/plotter.cs b/ksp_plugin_adapter/plotter.cs index 377837ca7a..2d9564487b 100644 --- a/ksp_plugin_adapter/plotter.cs +++ b/ksp_plugin_adapter/plotter.cs @@ -181,15 +181,15 @@ private void DrawLineMesh(UnityEngine.Mesh mesh, } var colours = new UnityEngine.Color[vertices_.Length]; if (style == GLLines.Style.Faded) { - for (int i = 0; i < colours.Length; ++i) { + for (int i = 0; i < vertex_count_; ++i) { var faded_colour = colour; // Fade from the opacity of |colour| (when i = 0) down to 1/4 of that // opacity. - faded_colour.a *= 1 - (float)(4 * i) / (float)(5 * colours.Length); + faded_colour.a *= 1 - (float)(4 * i) / (float)(5 * vertex_count_); colours[i] = faded_colour; } } else { - for (int i = 0; i < colours.Length; ++i) { + for (int i = 0; i < vertex_count_; ++i) { colours[i] = colour; } } From 0dd64645d7b7408d254b157cc3e54377a0249cd6 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Sun, 23 Jan 2022 18:12:44 +0100 Subject: [PATCH 18/33] forever pinned --- ksp_plugin_adapter/plotter.cs | 72 +++++++++++++++++------------------ 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/ksp_plugin_adapter/plotter.cs b/ksp_plugin_adapter/plotter.cs index 2d9564487b..f3294ee589 100644 --- a/ksp_plugin_adapter/plotter.cs +++ b/ksp_plugin_adapter/plotter.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Runtime.InteropServices; namespace principia { namespace ksp_plugin_adapter { @@ -12,18 +10,14 @@ public Plotter(PrincipiaPluginAdapter adapter) { adapter_ = adapter; } - public unsafe void Plot(DisposablePlanetarium planetarium, - string main_vessel_guid, - double history_length) { - fixed (UnityEngine.Vector3* vertices_data = vertices_) { - vertices_data_ = (IntPtr)vertices_data; - PlotCelestialTrajectories(planetarium, main_vessel_guid, history_length); - if (main_vessel_guid == null) { - return; - } - PlotVesselTrajectories(planetarium, main_vessel_guid, history_length); - vertices_data_ = IntPtr.Zero; + public void Plot(DisposablePlanetarium planetarium, + string main_vessel_guid, + double history_length) { + PlotCelestialTrajectories(planetarium, main_vessel_guid, history_length); + if (main_vessel_guid == null) { + return; } + PlotVesselTrajectories(planetarium, main_vessel_guid, history_length); } private void PlotVesselTrajectories(DisposablePlanetarium planetarium, @@ -33,15 +27,15 @@ private void PlotVesselTrajectories(DisposablePlanetarium planetarium, Plugin, main_vessel_guid, history_length, - vertices_data_, - vertices_.Length, + VertexBuffer.data, + VertexBuffer.size, out vertex_count_); DrawLineMesh(psychohistory_, adapter_.history_colour, adapter_.history_style); planetarium.PlanetariumGetPredictionVertices( Plugin, main_vessel_guid, - vertices_data_, - vertices_.Length, + VertexBuffer.data, + VertexBuffer.size, out vertex_count_); DrawLineMesh(prediction_, adapter_.prediction_colour, adapter_.prediction_style); @@ -56,16 +50,16 @@ private void PlotVesselTrajectories(DisposablePlanetarium planetarium, Plugin, target_id, history_length, - vertices_data_, - vertices_.Length, + VertexBuffer.data, + VertexBuffer.size, out vertex_count_); DrawLineMesh(target_psychohistory_, adapter_.target_history_colour, adapter_.target_history_style); planetarium.PlanetariumGetPredictionVertices( Plugin, target_id, - vertices_data_, - vertices_.Length, + VertexBuffer.data, + VertexBuffer.size, out vertex_count_); DrawLineMesh(target_prediction_, adapter_.target_prediction_colour, adapter_.target_prediction_style); @@ -83,8 +77,8 @@ private void PlotVesselTrajectories(DisposablePlanetarium planetarium, Plugin, main_vessel_guid, i, - vertices_data_, - vertices_.Length, + VertexBuffer.data, + VertexBuffer.size, out vertex_count_); DrawLineMesh(flight_plan_segments_[i], is_burn ? adapter_.burn_colour @@ -134,8 +128,8 @@ private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, Plugin, root.flightGlobalsIndex, history_length, - vertices_data_, - vertices_.Length, + VertexBuffer.data, + VertexBuffer.size, out double min_past_distance, out vertex_count_); min_distance_from_camera = @@ -146,8 +140,8 @@ private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, Plugin, root.flightGlobalsIndex, main_vessel_guid, - vertices_data_, - vertices_.Length, + VertexBuffer.data, + VertexBuffer.size, out double min_future_distance, out vertex_count_); min_distance_from_camera = @@ -170,16 +164,15 @@ private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, private void DrawLineMesh(UnityEngine.Mesh mesh, UnityEngine.Color colour, GLLines.Style style) { - filled_vertices_ |= vertex_count_ == vertices_.Length; if (!MainWindow.reuse_meshes) { mesh = new UnityEngine.Mesh(); } - mesh.vertices = vertices_; + mesh.vertices = VertexBuffer.get; var indices = new int[vertex_count_]; for (int i = 0; i < vertex_count_; ++i) { indices[i] = i; } - var colours = new UnityEngine.Color[vertices_.Length]; + var colours = new UnityEngine.Color[VertexBuffer.size]; if (style == GLLines.Style.Faded) { for (int i = 0; i < vertex_count_; ++i) { var faded_colour = colour; @@ -219,12 +212,19 @@ private void DrawLineMesh(UnityEngine.Mesh mesh, private IntPtr Plugin => adapter_.Plugin(); - private UnityEngine.Vector3[] vertices_ = new UnityEngine.Vector3[10_000]; - // A pointer to vertices_; vertices_ should be pinned and should not be - // reassigned while this is non-null. - private IntPtr vertices_data_ = IntPtr.Zero; + private static class VertexBuffer { + public static IntPtr data => handle_.AddrOfPinnedObject(); + public static int size => vertices_.Length; + + public static UnityEngine.Vector3[] get => vertices_; + + private static UnityEngine.Vector3[] vertices_ = + new UnityEngine.Vector3[10_000]; + private static GCHandle handle_ = + GCHandle.Alloc(vertices_, GCHandleType.Pinned); + } + private int vertex_count_; - private bool filled_vertices_ = false; private class CelestialTrajectories { public UnityEngine.Mesh future = new UnityEngine.Mesh(); From 4533134316581e7f9778a42b7f01ae526338971f Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Sun, 23 Jan 2022 18:46:19 +0100 Subject: [PATCH 19/33] dynamic --- ksp_plugin_adapter/ksp_plugin_adapter.cs | 3 ++- ksp_plugin_adapter/main_window.cs | 5 ++++ ksp_plugin_adapter/plotter.cs | 33 ++++++++++++++++++------ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/ksp_plugin_adapter/ksp_plugin_adapter.cs b/ksp_plugin_adapter/ksp_plugin_adapter.cs index 0108f6cfca..04501b6473 100644 --- a/ksp_plugin_adapter/ksp_plugin_adapter.cs +++ b/ksp_plugin_adapter/ksp_plugin_adapter.cs @@ -123,7 +123,8 @@ internal IntPtr Plugin() { internal UnityEngine.Color target_prediction_colour = XKCDColors.LightMauve; internal GLLines.Style target_prediction_style = GLLines.Style.Solid; - private Plotter plotter_; + // TODO(egg): private. + internal Plotter plotter_; private readonly List vessel_futures_ = new List(); diff --git a/ksp_plugin_adapter/main_window.cs b/ksp_plugin_adapter/main_window.cs index fa0a490820..f043530e52 100644 --- a/ksp_plugin_adapter/main_window.cs +++ b/ksp_plugin_adapter/main_window.cs @@ -279,6 +279,7 @@ private void RenderKSPFeatures() { public static bool reuse_meshes = true; public static bool pre_cull => !now; public static bool now = false; + public static bool dynamic = false; public static int layer = 9; public static string trace = ""; @@ -286,6 +287,10 @@ private void RenderLoggingSettings() { use_meshes = UnityEngine.GUILayout.Toggle(use_meshes, "Use meshes"); reuse_meshes = UnityEngine.GUILayout.Toggle(reuse_meshes, "Reuse meshes"); now = UnityEngine.GUILayout.Toggle(now, "Now post-render"); + dynamic = UnityEngine.GUILayout.Toggle(dynamic, "Dynamic"); + if (UnityEngine.GUILayout.Button("Remake meshes")) { + adapter_.plotter_.RemakeAllMeshes(); + } if (int.TryParse(UnityEngine.GUILayout.TextArea(layer.ToString()), out int result)) { layer = result; } diff --git a/ksp_plugin_adapter/plotter.cs b/ksp_plugin_adapter/plotter.cs index f3294ee589..3cafbd83b9 100644 --- a/ksp_plugin_adapter/plotter.cs +++ b/ksp_plugin_adapter/plotter.cs @@ -69,7 +69,7 @@ private void PlotVesselTrajectories(DisposablePlanetarium planetarium, int number_of_segments = Plugin.FlightPlanNumberOfSegments(main_vessel_guid); for (int i = flight_plan_segments_.Count; i < number_of_segments; ++i) { - flight_plan_segments_.Add(new UnityEngine.Mesh()); + flight_plan_segments_.Add(MakeDynamicMesh()); } for (int i = 0; i < number_of_segments; ++i) { bool is_burn = i % 2 == 1; @@ -165,7 +165,7 @@ private void DrawLineMesh(UnityEngine.Mesh mesh, UnityEngine.Color colour, GLLines.Style style) { if (!MainWindow.reuse_meshes) { - mesh = new UnityEngine.Mesh(); + mesh = MakeDynamicMesh(); } mesh.vertices = VertexBuffer.get; var indices = new int[vertex_count_]; @@ -208,6 +208,23 @@ private void DrawLineMesh(UnityEngine.Mesh mesh, } } + private static UnityEngine.Mesh MakeDynamicMesh() { + var result = new UnityEngine.Mesh(); + if (MainWindow.dynamic) { + result.MarkDynamic(); + } + return result; + } + + public void RemakeAllMeshes() { + celestial_trajectories_ = new Dictionary(); + psychohistory_ = MakeDynamicMesh(); + prediction_ = MakeDynamicMesh(); + flight_plan_segments_ = new List(); + target_psychohistory_ = MakeDynamicMesh(); + target_prediction_ = MakeDynamicMesh(); + } + private readonly PrincipiaPluginAdapter adapter_; private IntPtr Plugin => adapter_.Plugin(); @@ -227,17 +244,17 @@ private static class VertexBuffer { private int vertex_count_; private class CelestialTrajectories { - public UnityEngine.Mesh future = new UnityEngine.Mesh(); - public UnityEngine.Mesh past = new UnityEngine.Mesh(); + public UnityEngine.Mesh future = MakeDynamicMesh(); + public UnityEngine.Mesh past = MakeDynamicMesh(); } private Dictionary celestial_trajectories_ = new Dictionary(); - private UnityEngine.Mesh psychohistory_ = new UnityEngine.Mesh(); - private UnityEngine.Mesh prediction_ = new UnityEngine.Mesh(); + private UnityEngine.Mesh psychohistory_ = MakeDynamicMesh(); + private UnityEngine.Mesh prediction_ = MakeDynamicMesh(); private List flight_plan_segments_ = new List(); - private UnityEngine.Mesh target_psychohistory_ = new UnityEngine.Mesh(); - private UnityEngine.Mesh target_prediction_ = new UnityEngine.Mesh(); + private UnityEngine.Mesh target_psychohistory_ = MakeDynamicMesh(); + private UnityEngine.Mesh target_prediction_ = MakeDynamicMesh(); } } // namespace ksp_plugin_adapter From d6bfb90802fd33b47a61e71f6839a6397e3c8f13 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Sun, 23 Jan 2022 18:48:36 +0100 Subject: [PATCH 20/33] unconditionally --- ksp_plugin_adapter/ksp_plugin_adapter.cs | 3 +-- ksp_plugin_adapter/main_window.cs | 5 ----- ksp_plugin_adapter/plotter.cs | 13 +------------ 3 files changed, 2 insertions(+), 19 deletions(-) diff --git a/ksp_plugin_adapter/ksp_plugin_adapter.cs b/ksp_plugin_adapter/ksp_plugin_adapter.cs index 04501b6473..0108f6cfca 100644 --- a/ksp_plugin_adapter/ksp_plugin_adapter.cs +++ b/ksp_plugin_adapter/ksp_plugin_adapter.cs @@ -123,8 +123,7 @@ internal IntPtr Plugin() { internal UnityEngine.Color target_prediction_colour = XKCDColors.LightMauve; internal GLLines.Style target_prediction_style = GLLines.Style.Solid; - // TODO(egg): private. - internal Plotter plotter_; + private Plotter plotter_; private readonly List vessel_futures_ = new List(); diff --git a/ksp_plugin_adapter/main_window.cs b/ksp_plugin_adapter/main_window.cs index f043530e52..fa0a490820 100644 --- a/ksp_plugin_adapter/main_window.cs +++ b/ksp_plugin_adapter/main_window.cs @@ -279,7 +279,6 @@ private void RenderKSPFeatures() { public static bool reuse_meshes = true; public static bool pre_cull => !now; public static bool now = false; - public static bool dynamic = false; public static int layer = 9; public static string trace = ""; @@ -287,10 +286,6 @@ private void RenderLoggingSettings() { use_meshes = UnityEngine.GUILayout.Toggle(use_meshes, "Use meshes"); reuse_meshes = UnityEngine.GUILayout.Toggle(reuse_meshes, "Reuse meshes"); now = UnityEngine.GUILayout.Toggle(now, "Now post-render"); - dynamic = UnityEngine.GUILayout.Toggle(dynamic, "Dynamic"); - if (UnityEngine.GUILayout.Button("Remake meshes")) { - adapter_.plotter_.RemakeAllMeshes(); - } if (int.TryParse(UnityEngine.GUILayout.TextArea(layer.ToString()), out int result)) { layer = result; } diff --git a/ksp_plugin_adapter/plotter.cs b/ksp_plugin_adapter/plotter.cs index 3cafbd83b9..80a435e465 100644 --- a/ksp_plugin_adapter/plotter.cs +++ b/ksp_plugin_adapter/plotter.cs @@ -210,21 +210,10 @@ private void DrawLineMesh(UnityEngine.Mesh mesh, private static UnityEngine.Mesh MakeDynamicMesh() { var result = new UnityEngine.Mesh(); - if (MainWindow.dynamic) { - result.MarkDynamic(); - } + result.MarkDynamic(); return result; } - public void RemakeAllMeshes() { - celestial_trajectories_ = new Dictionary(); - psychohistory_ = MakeDynamicMesh(); - prediction_ = MakeDynamicMesh(); - flight_plan_segments_ = new List(); - target_psychohistory_ = MakeDynamicMesh(); - target_prediction_ = MakeDynamicMesh(); - } - private readonly PrincipiaPluginAdapter adapter_; private IntPtr Plugin => adapter_.Plugin(); From ed1238de210f2f60e9296a4ad2104a7aeb9cdd92 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Sun, 23 Jan 2022 19:07:12 +0100 Subject: [PATCH 21/33] drop the RP2 paths --- ksp_plugin_adapter/gl_lines.cs | 66 --------- ksp_plugin_adapter/ksp_plugin_adapter.cs | 168 +++-------------------- ksp_plugin_adapter/main_window.cs | 14 -- ksp_plugin_adapter/plotter.cs | 26 ++-- 4 files changed, 25 insertions(+), 249 deletions(-) diff --git a/ksp_plugin_adapter/gl_lines.cs b/ksp_plugin_adapter/gl_lines.cs index 8b29fc72ef..f392d62b2f 100644 --- a/ksp_plugin_adapter/gl_lines.cs +++ b/ksp_plugin_adapter/gl_lines.cs @@ -79,77 +79,11 @@ public static DisposablePlanetarium NewPlanetarium(IntPtr plugin, Vector3d.zero)); } - public static void PlotRP2Lines(DisposableIterator rp2_lines_iterator, - UnityEngine.Color colour, - Style style) { - UnityEngine.GL.Color(colour); - - // First evaluate the total size of the lines. - int size = 0; - for (; - !rp2_lines_iterator.IteratorAtEnd(); - rp2_lines_iterator.IteratorIncrement()) { - using (DisposableIterator rp2_line_iterator = - rp2_lines_iterator.IteratorGetRP2LinesIterator()) { - size += rp2_line_iterator.IteratorSize(); - } - } - - // Reset the iterator and do the actual plotting. - rp2_lines_iterator.IteratorReset(); - int index = 0; - for (; - !rp2_lines_iterator.IteratorAtEnd(); - rp2_lines_iterator.IteratorIncrement()) { - using (DisposableIterator rp2_line_iterator = - rp2_lines_iterator.IteratorGetRP2LinesIterator()) { - XY? previous_rp2_point = null; - for (; - !rp2_line_iterator.IteratorAtEnd(); - rp2_line_iterator.IteratorIncrement()) { - XY current_rp2_point = ToScreen( - rp2_line_iterator.IteratorGetRP2LineXY()); - if (previous_rp2_point.HasValue) { - if (style == Style.Faded) { - 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, - (float)previous_rp2_point.Value.y, - 0); - UnityEngine.GL.Vertex3((float)current_rp2_point.x, - (float)current_rp2_point.y, - 0); - } - } - previous_rp2_point = current_rp2_point; - ++index; - } - } - } - } - private static UnityEngine.Vector3 WorldToMapScreen(Vector3d world) { return PlanetariumCamera.Camera.WorldToScreenPoint( ScaledSpace.LocalToScaledSpace(world)); } - private static XY ToScreen(XY rp2_point) { - UnityEngine.Camera camera = PlanetariumCamera.Camera; - return new XY{ - x = (rp2_point.x * camera.projectionMatrix[0, 0] + 1.0) * - 0.5 * - camera.pixelWidth, - y = (rp2_point.y * camera.projectionMatrix[1, 1] + 1.0) * - 0.5 * - camera.pixelHeight - }; - } - private static UnityEngine.Material line_material_; public static UnityEngine.Material line_material { diff --git a/ksp_plugin_adapter/ksp_plugin_adapter.cs b/ksp_plugin_adapter/ksp_plugin_adapter.cs index 0108f6cfca..cbffd2a7aa 100644 --- a/ksp_plugin_adapter/ksp_plugin_adapter.cs +++ b/ksp_plugin_adapter/ksp_plugin_adapter.cs @@ -18,7 +18,7 @@ public partial class PrincipiaPluginAdapter : ScenarioModule, // From https://forum.kerbalspaceprogram.com/index.php?/topic/84273--/, // edited 2017-03-09. Where the name of the layer is not CamelCase, the // actual name is commented. - private enum UnityLayers { + public enum UnityLayers { TransparentFX = 1, IgnoreRaycast = 2, // Ignore Raycast Water = 3, @@ -874,8 +874,8 @@ private void LateUpdate() { if (map_renderer_ == null) { map_renderer_ = PlanetariumCamera.Camera.gameObject. AddComponent(); - map_renderer_.pre_cull = () => { if (MainWindow.pre_cull) RenderTrajectoriesWithMeshes(); }; - map_renderer_.post_render = () => { RenderTrajectories(); if (!MainWindow.pre_cull) RenderTrajectoriesWithMeshes(); }; + map_renderer_.pre_cull = () => { RenderTrajectories(); }; + map_renderer_.post_render = () => { RenderManœuvreMarkers(); }; } if (galaxy_cube_rotator_ == null) { @@ -2103,23 +2103,6 @@ private void HandleMapViewClicks() { } } - private void RenderTrajectoriesWithMeshes() { - if (!PluginRunning()) { - return; - } - if (!MainWindow.use_meshes) { - return; - } - string main_vessel_guid = PredictedVessel()?.id.ToString(); - if (MapView.MapIsEnabled) { - XYZ sun_world_position = (XYZ)Planetarium.fetch.Sun.position; - using (DisposablePlanetarium planetarium = - GLLines.NewPlanetarium(plugin_, sun_world_position)) { - plotter_.Plot(planetarium, main_vessel_guid, main_window_.history_length); - } - } - } - private void RenderTrajectories() { if (!PluginRunning()) { return; @@ -2143,61 +2126,24 @@ private void RenderTrajectories() { XYZ sun_world_position = (XYZ)Planetarium.fetch.Sun.position; using (DisposablePlanetarium planetarium = GLLines.NewPlanetarium(plugin_, sun_world_position)) { - GLLines.Draw(() => { - if (!MainWindow.use_meshes) { - PlotCelestialTrajectories(planetarium, main_vessel_guid); - } + plotter_.Plot(planetarium, main_vessel_guid, main_window_.history_length); + } + } + } - // Vessel trajectories. + private void RenderManœuvreMarkers() { + if (!PluginRunning()) { + return; + } + string main_vessel_guid = PredictedVessel()?.id.ToString(); + if (MapView.MapIsEnabled) { + XYZ sun_world_position = (XYZ)Planetarium.fetch.Sun.position; + using (DisposablePlanetarium planetarium = + GLLines.NewPlanetarium(plugin_, sun_world_position)) { + GLLines.Draw(() => { if (main_vessel_guid == null) { return; } - if (!MainWindow.use_meshes) { - // Main vessel psychohistory and prediction. - using (DisposableIterator rp2_lines_iterator = - planetarium.PlanetariumPlotPsychohistory( - plugin_, - main_vessel_guid, - main_window_.history_length)) { - GLLines.PlotRP2Lines(rp2_lines_iterator, - history_colour, - history_style); - } - using (DisposableIterator rp2_lines_iterator = - planetarium.PlanetariumPlotPrediction( - plugin_, - main_vessel_guid)) { - GLLines.PlotRP2Lines(rp2_lines_iterator, - prediction_colour, - prediction_style); - } - // Target psychohistory and prediction. - string target_id = FlightGlobals.fetch.VesselTarget?.GetVessel()?.id. - ToString(); - if (FlightGlobals.ActiveVessel != null && - !plotting_frame_selector_.target_frame_selected && - target_id != null && - plugin_.HasVessel(target_id)) { - using (DisposableIterator rp2_lines_iterator = - planetarium.PlanetariumPlotPsychohistory( - plugin_, - target_id, - main_window_.history_length)) { - GLLines.PlotRP2Lines(rp2_lines_iterator, - target_history_colour, - target_history_style); - } - using (DisposableIterator rp2_lines_iterator = - planetarium.PlanetariumPlotPrediction( - plugin_, - target_id)) { - GLLines.PlotRP2Lines(rp2_lines_iterator, - target_prediction_colour, - target_prediction_style); - } - } - } - // Main vessel flight plan. if (plugin_.FlightPlanExists(main_vessel_guid)) { int number_of_anomalous_manœuvres = plugin_.FlightPlanNumberOfAnomalousManoeuvres(main_vessel_guid); @@ -2217,21 +2163,6 @@ private void RenderTrajectories() { } Vector3d position_at_start = (Vector3d)rendered_segments. IteratorGetDiscreteTrajectoryXYZ(); - if (!MainWindow.use_meshes) { - using (DisposableIterator rp2_lines_iterator = - planetarium.PlanetariumPlotFlightPlanSegment( - plugin_, - main_vessel_guid, - i)) { - GLLines.PlotRP2Lines(rp2_lines_iterator, - is_burn - ? burn_colour - : flight_plan_colour, - is_burn - ? burn_style - : flight_plan_style); - } - } if (is_burn) { int manœuvre_index = i / 2; if (manœuvre_index < @@ -2265,71 +2196,6 @@ private void RenderTrajectories() { } } - private void PlotCelestialTrajectories(DisposablePlanetarium planetarium, - string main_vessel_guid) { - const double degree = Math.PI / 180; - UnityEngine.Camera camera = PlanetariumCamera.Camera; - float vertical_fov = camera.fieldOfView; - float horizontal_fov = - UnityEngine.Camera.VerticalToHorizontalFieldOfView( - vertical_fov, camera.aspect); - // The angle subtended by the pixel closest to the centre of the viewport. - double tan_angular_resolution = Math.Min( - Math.Tan(vertical_fov * degree / 2) / (camera.pixelHeight / 2), - Math.Tan(horizontal_fov * degree / 2) / (camera.pixelWidth / 2)); - MainWindow.trace = $"{UnityEngine.Camera.current.name}, {PlanetariumCamera.Camera.name} {ScaledSpace.LocalToScaledSpace(Vector3d.zero)}"; - PlotSubtreeTrajectories(planetarium, main_vessel_guid, - Planetarium.fetch.Sun, - tan_angular_resolution); - } - - private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, - string main_vessel_guid, - CelestialBody root, - double tan_angular_resolution) { - var colour = root.orbitDriver?.Renderer?.orbitColor ?? - XKCDColors.SunshineYellow; - var camera_world_position = ScaledSpace.ScaledToLocalSpace( - PlanetariumCamera.fetch.transform.position); - double min_distance_from_camera = - (root.position - camera_world_position).magnitude; - if (!plotting_frame_selector_.FixesBody(root)) { - using (DisposableIterator rp2_lines_iterator = - planetarium.PlanetariumPlotCelestialTrajectoryForPsychohistory( - plugin_, - root.flightGlobalsIndex, - main_vessel_guid, - main_window_.history_length, - out double min_past_distance)) { - min_distance_from_camera = - Math.Min(min_distance_from_camera, min_past_distance); - GLLines.PlotRP2Lines(rp2_lines_iterator, colour, GLLines.Style.Faded); - } - if (main_vessel_guid != null) { - using (DisposableIterator rp2_lines_iterator = - planetarium. - PlanetariumPlotCelestialTrajectoryForPredictionOrFlightPlan( - plugin_, - root.flightGlobalsIndex, - main_vessel_guid, - out double min_future_distance)) { - min_distance_from_camera = - Math.Min(min_distance_from_camera, min_future_distance); - GLLines.PlotRP2Lines(rp2_lines_iterator, colour, GLLines.Style.Solid); - } - } - } - foreach (CelestialBody child in root.orbitingBodies) { - // Plot the trajectory of an orbiting body if it could be separated from - // that of its parent by a pixel of empty space, instead of merely making - // the line wider. - if (child.orbit.ApR / min_distance_from_camera > 2 * tan_angular_resolution) { - PlotSubtreeTrajectories(planetarium, main_vessel_guid, child, - tan_angular_resolution); - } - } - } - private void RenderPredictionMarkers(string vessel_guid, XYZ sun_world_position) { if (plotting_frame_selector_.target_frame_selected) { diff --git a/ksp_plugin_adapter/main_window.cs b/ksp_plugin_adapter/main_window.cs index fa0a490820..44e8a50197 100644 --- a/ksp_plugin_adapter/main_window.cs +++ b/ksp_plugin_adapter/main_window.cs @@ -274,22 +274,8 @@ private void RenderKSPFeatures() { selecting_target_celestial_ = false; } } - - public static bool use_meshes = false; - public static bool reuse_meshes = true; - public static bool pre_cull => !now; - public static bool now = false; - public static int layer = 9; - public static string trace = ""; private void RenderLoggingSettings() { - use_meshes = UnityEngine.GUILayout.Toggle(use_meshes, "Use meshes"); - reuse_meshes = UnityEngine.GUILayout.Toggle(reuse_meshes, "Reuse meshes"); - now = UnityEngine.GUILayout.Toggle(now, "Now post-render"); - if (int.TryParse(UnityEngine.GUILayout.TextArea(layer.ToString()), out int result)) { - layer = result; - } - UnityEngine.GUILayout.Label(trace); using (new UnityEngine.GUILayout.HorizontalScope()) { UnityEngine.GUILayout.Label( text : L10N.CacheFormat( diff --git a/ksp_plugin_adapter/plotter.cs b/ksp_plugin_adapter/plotter.cs index 80a435e465..6fc0462754 100644 --- a/ksp_plugin_adapter/plotter.cs +++ b/ksp_plugin_adapter/plotter.cs @@ -102,7 +102,6 @@ private void PlotCelestialTrajectories(DisposablePlanetarium planetarium, double tan_angular_resolution = Math.Min( Math.Tan(vertical_fov * degree / 2) / (camera.pixelHeight / 2), Math.Tan(horizontal_fov * degree / 2) / (camera.pixelWidth / 2)); - MainWindow.trace = $"{UnityEngine.Camera.current.name}, {PlanetariumCamera.Camera.name} {ScaledSpace.LocalToScaledSpace(Vector3d.zero)}"; PlotSubtreeTrajectories(planetarium, main_vessel_guid, history_length, Planetarium.fetch.Sun, tan_angular_resolution); } @@ -164,9 +163,6 @@ private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, private void DrawLineMesh(UnityEngine.Mesh mesh, UnityEngine.Color colour, GLLines.Style style) { - if (!MainWindow.reuse_meshes) { - mesh = MakeDynamicMesh(); - } mesh.vertices = VertexBuffer.get; var indices = new int[vertex_count_]; for (int i = 0; i < vertex_count_; ++i) { @@ -192,20 +188,14 @@ private void DrawLineMesh(UnityEngine.Mesh mesh, style == GLLines.Style.Dashed ? UnityEngine.MeshTopology.Lines : UnityEngine.MeshTopology.LineStrip, submesh: 0); - if (MainWindow.reuse_meshes) { - mesh.RecalculateBounds(); - } - if (MainWindow.now) { - UnityEngine.Graphics.DrawMeshNow(mesh, UnityEngine.Vector3.zero, UnityEngine.Quaternion.identity); - } else { - UnityEngine.Graphics.DrawMesh( - mesh, - UnityEngine.Vector3.zero, - UnityEngine.Quaternion.identity, - GLLines.line_material, - MainWindow.layer, - PlanetariumCamera.Camera); - } + mesh.RecalculateBounds(); + UnityEngine.Graphics.DrawMesh( + mesh, + UnityEngine.Vector3.zero, + UnityEngine.Quaternion.identity, + GLLines.line_material, + (int)PrincipiaPluginAdapter.UnityLayers.Atmosphere, + PlanetariumCamera.Camera); } private static UnityEngine.Mesh MakeDynamicMesh() { From 9a7ac4da47c3e21051d0748b428ac33f385363c9 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Sun, 23 Jan 2022 19:08:19 +0100 Subject: [PATCH 22/33] advanced graphics programming --- ksp_plugin_adapter/plotter.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ksp_plugin_adapter/plotter.cs b/ksp_plugin_adapter/plotter.cs index 6fc0462754..0e6331d1f0 100644 --- a/ksp_plugin_adapter/plotter.cs +++ b/ksp_plugin_adapter/plotter.cs @@ -189,6 +189,9 @@ private void DrawLineMesh(UnityEngine.Mesh mesh, : UnityEngine.MeshTopology.LineStrip, submesh: 0); mesh.RecalculateBounds(); + // If the lines are drawn in layer 31 (Vectors), which sounds more + // appropriate, they vanish when zoomed out. Layer 9 works; pay no + // attention to its name. UnityEngine.Graphics.DrawMesh( mesh, UnityEngine.Vector3.zero, From 69cf2fb20123dbd40fa14cdd8be508fad59d3223 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Sun, 23 Jan 2022 19:39:06 +0100 Subject: [PATCH 23/33] in place --- ksp_plugin/interface_planetarium.cpp | 205 ++------------------------- ksp_plugin_adapter/plotter.cs | 18 +-- serialization/journal.proto | 133 ++--------------- 3 files changed, 29 insertions(+), 327 deletions(-) diff --git a/ksp_plugin/interface_planetarium.cpp b/ksp_plugin/interface_planetarium.cpp index ea14c524aa..d1f9ecaa9b 100644 --- a/ksp_plugin/interface_planetarium.cpp +++ b/ksp_plugin/interface_planetarium.cpp @@ -116,192 +116,7 @@ void __cdecl principia__PlanetariumDelete( return m.Return(); } -Iterator* __cdecl principia__PlanetariumPlotFlightPlanSegment( - Planetarium const* const planetarium, - Plugin const* const plugin, - char const* const vessel_guid, - int const index) { - journal::Method m({planetarium, - plugin, - vessel_guid, - index}); - CHECK_NOTNULL(plugin); - CHECK_NOTNULL(planetarium); - Vessel const& vessel = *plugin->GetVessel(vessel_guid); - CHECK(vessel.has_flight_plan()) << vessel_guid; - auto const segment = vessel.flight_plan().GetSegment(index); - RP2Lines rp2_lines; - // TODO(egg): this is ugly; we should centralize rendering. - // If this is a burn and we cannot render the beginning of the burn, we - // render none of it, otherwise we try to render the Frenet trihedron at the - // start and we fail. - if (index % 2 == 0 || - segment->empty() || - segment->front().time >= plugin->renderer().GetPlottingFrame()->t_min()) { - rp2_lines = planetarium->PlotMethod2(*segment, - segment->begin(), segment->end(), - plugin->CurrentTime(), - /*reverse=*/false); - } - return m.Return(new TypedIterator>(rp2_lines)); -} - -Iterator* __cdecl principia__PlanetariumPlotPrediction( - Planetarium const* const planetarium, - Plugin const* const plugin, - char const* const vessel_guid) { - journal::Method m({planetarium, - plugin, - vessel_guid}); - CHECK_NOTNULL(plugin); - CHECK_NOTNULL(planetarium); - auto const prediction = plugin->GetVessel(vessel_guid)->prediction(); - auto const rp2_lines = planetarium->PlotMethod2(*prediction, - prediction->begin(), - prediction->end(), - plugin->CurrentTime(), - /*reverse=*/false); - return m.Return(new TypedIterator>(rp2_lines)); -} - -Iterator* __cdecl principia__PlanetariumPlotPsychohistory( - Planetarium const* const planetarium, - Plugin const* const plugin, - char const* const vessel_guid, - double const max_history_length) { - journal::Method m( - {planetarium, plugin, vessel_guid, max_history_length}); - CHECK_NOTNULL(plugin); - CHECK_NOTNULL(planetarium); - - // Do not plot the psychohistory when there is a target vessel as it is - // misleading. - if (plugin->renderer().HasTargetVessel()) { - return m.Return(new TypedIterator>({})); - } else { - auto const vessel = plugin->GetVessel(vessel_guid); - auto const& trajectory = vessel->trajectory(); - auto const& psychohistory = vessel->psychohistory(); - - Instant const desired_first_time = - plugin->CurrentTime() - max_history_length * Second; - - // Since we would want to plot starting from |desired_first_time|, ask the - // reanimator to reconstruct the past. That may take a while, during which - // time the history will be shorter than desired. - vessel->RequestReanimation(desired_first_time); - - auto const rp2_lines = - planetarium->PlotMethod2( - trajectory, - trajectory.lower_bound(desired_first_time), - psychohistory->end(), - plugin->CurrentTime(), - /*reverse=*/true); - return m.Return(new TypedIterator>(rp2_lines)); - } -} - -// Returns an iterator for the rendered past trajectory of the celestial with -// the given index; the trajectory goes back |max_history_length| seconds before -// the present time (or to the earliest time available if the relevant |t_min| -// is more recent). -Iterator* __cdecl principia__PlanetariumPlotCelestialTrajectoryForPsychohistory( - Planetarium const* const planetarium, - Plugin const* const plugin, - int const celestial_index, - char const* const vessel_guid, - double const max_history_length, - double* const minimal_distance_from_camera) { - journal::Method - m({planetarium, - plugin, - celestial_index, - vessel_guid, - max_history_length}, - {minimal_distance_from_camera}); - 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()) { - *minimal_distance_from_camera = std::numeric_limits::infinity(); - return m.Return(new TypedIterator>({})); - } else { - auto const& celestial_trajectory = - plugin->GetCelestial(celestial_index).trajectory(); - Instant const desired_first_time = - plugin->CurrentTime() - max_history_length * Second; - - // Since we would want to plot starting from |desired_first_time|, ask the - // reanimator to reconstruct the past. That may take a while, during which - // time the history will be shorter than desired. - plugin->RequestReanimation(desired_first_time); - - Instant const first_time = - std::max(desired_first_time, celestial_trajectory.t_min()); - Length minimal_distance; - auto const rp2_lines = - planetarium->PlotMethod2(celestial_trajectory, - first_time, - /*last_time=*/plugin->CurrentTime(), - /*now=*/plugin->CurrentTime(), - /*reverse=*/true, - &minimal_distance); - *minimal_distance_from_camera = minimal_distance / Metre; - return m.Return(new TypedIterator>(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* __cdecl -principia__PlanetariumPlotCelestialTrajectoryForPredictionOrFlightPlan( - Planetarium const* const planetarium, - Plugin const* const plugin, - int const celestial_index, - char const* const vessel_guid, - double* const minimal_distance_from_camera) { - journal::Method< - journal::PlanetariumPlotCelestialTrajectoryForPredictionOrFlightPlan> - m({planetarium, plugin, celestial_index, vessel_guid}, - {minimal_distance_from_camera}); - 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()) { - *minimal_distance_from_camera = std::numeric_limits::infinity(); - return m.Return(new TypedIterator>({})); - } 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(); - // No need to request reanimation here because the current time of the - // plugin is necessarily covered. - Length minimal_distance; - auto const rp2_lines = - planetarium->PlotMethod2(celestial_trajectory, - /*first_time=*/plugin->CurrentTime(), - /*last_time=*/final_time, - /*now=*/plugin->CurrentTime(), - /*reverse=*/false, - &minimal_distance); - *minimal_distance_from_camera = minimal_distance / Metre; - return m.Return(new TypedIterator>(rp2_lines)); - } -} - -// PLOT METHOD 3 - -void __cdecl principia__PlanetariumGetFlightPlanSegmentVertices( +void __cdecl principia__PlanetariumPlotFlightPlanSegment( Planetarium const* const planetarium, Plugin const* const plugin, char const* const vessel_guid, @@ -309,7 +124,7 @@ void __cdecl principia__PlanetariumGetFlightPlanSegmentVertices( ScaledSpacePoint* const vertices, int const vertices_size, int* const vertex_count) { - journal::Method m( + journal::Method m( {planetarium, plugin, vessel_guid, index, vertices, vertices_size}, {vertex_count}); CHECK_NOTNULL(plugin); @@ -342,14 +157,14 @@ void __cdecl principia__PlanetariumGetFlightPlanSegmentVertices( return m.Return(); } -void __cdecl principia__PlanetariumGetPredictionVertices( +void __cdecl principia__PlanetariumPlotPrediction( Planetarium const* const planetarium, Plugin const* const plugin, char const* const vessel_guid, ScaledSpacePoint* const vertices, int const vertices_size, int* const vertex_count) { - journal::Method m( + journal::Method m( {planetarium, plugin, vessel_guid, vertices, vertices_size}, {vertex_count}); CHECK_NOTNULL(plugin); @@ -370,7 +185,7 @@ void __cdecl principia__PlanetariumGetPredictionVertices( return m.Return(); } -void __cdecl principia__PlanetariumGetPsychohistoryVertices( +void __cdecl principia__PlanetariumPlotPsychohistory( Planetarium const* const planetarium, Plugin const* const plugin, char const* const vessel_guid, @@ -378,7 +193,7 @@ void __cdecl principia__PlanetariumGetPsychohistoryVertices( ScaledSpacePoint* const vertices, int const vertices_size, int* const vertex_count) { - journal::Method m( + journal::Method m( {planetarium, plugin, vessel_guid, @@ -417,7 +232,7 @@ void __cdecl principia__PlanetariumGetPsychohistoryVertices( // the given index; the trajectory goes back |max_history_length| seconds before // the present time (or to the earliest time available if the relevant |t_min| // is more recent). -void __cdecl principia__PlanetariumGetCelestialPastTrajectoryVertices( +void __cdecl principia__PlanetariumPlotCelestialPastTrajectory( Planetarium const* const planetarium, Plugin const* const plugin, int const celestial_index, @@ -426,7 +241,7 @@ void __cdecl principia__PlanetariumGetCelestialPastTrajectoryVertices( int const vertices_size, double* const minimal_distance_from_camera, int* const vertex_count) { - journal::Method m( + journal::Method m( {planetarium, plugin, celestial_index, @@ -474,7 +289,7 @@ void __cdecl principia__PlanetariumGetCelestialPastTrajectoryVertices( // TODO(egg): comment; the trajectory goes as far as the furthest of the final time // of the prediction or that of the flight plan. -void __cdecl principia__PlanetariumGetCelestialFutureTrajectoryVertices( +void __cdecl principia__PlanetariumPlotCelestialFutureTrajectory( Planetarium const* const planetarium, Plugin const* const plugin, int const celestial_index, @@ -483,7 +298,7 @@ void __cdecl principia__PlanetariumGetCelestialFutureTrajectoryVertices( int const vertices_size, double* const minimal_distance_from_camera, int* const vertex_count) { - journal::Method m( + journal::Method m( {planetarium, plugin, celestial_index, diff --git a/ksp_plugin_adapter/plotter.cs b/ksp_plugin_adapter/plotter.cs index 0e6331d1f0..aff1090dee 100644 --- a/ksp_plugin_adapter/plotter.cs +++ b/ksp_plugin_adapter/plotter.cs @@ -23,7 +23,7 @@ public void Plot(DisposablePlanetarium planetarium, private void PlotVesselTrajectories(DisposablePlanetarium planetarium, string main_vessel_guid, double history_length) { - planetarium.PlanetariumGetPsychohistoryVertices( + planetarium.PlanetariumPlotPsychohistory( Plugin, main_vessel_guid, history_length, @@ -31,7 +31,7 @@ private void PlotVesselTrajectories(DisposablePlanetarium planetarium, VertexBuffer.size, out vertex_count_); DrawLineMesh(psychohistory_, adapter_.history_colour, adapter_.history_style); - planetarium.PlanetariumGetPredictionVertices( + planetarium.PlanetariumPlotPrediction( Plugin, main_vessel_guid, VertexBuffer.data, @@ -46,7 +46,7 @@ private void PlotVesselTrajectories(DisposablePlanetarium planetarium, !adapter_.plotting_frame_selector_.target_frame_selected && target_id != null && Plugin.HasVessel(target_id)) { - planetarium.PlanetariumGetPsychohistoryVertices( + planetarium.PlanetariumPlotPsychohistory( Plugin, target_id, history_length, @@ -55,7 +55,7 @@ private void PlotVesselTrajectories(DisposablePlanetarium planetarium, out vertex_count_); DrawLineMesh(target_psychohistory_, adapter_.target_history_colour, adapter_.target_history_style); - planetarium.PlanetariumGetPredictionVertices( + planetarium.PlanetariumPlotPrediction( Plugin, target_id, VertexBuffer.data, @@ -73,7 +73,7 @@ private void PlotVesselTrajectories(DisposablePlanetarium planetarium, } for (int i = 0; i < number_of_segments; ++i) { bool is_burn = i % 2 == 1; - planetarium.PlanetariumGetFlightPlanSegmentVertices( + planetarium.PlanetariumPlotFlightPlanSegment( Plugin, main_vessel_guid, i, @@ -123,7 +123,7 @@ private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, double min_distance_from_camera = (root.position - camera_world_position).magnitude; if (!adapter_.plotting_frame_selector_.FixesBody(root)) { - planetarium.PlanetariumGetCelestialPastTrajectoryVertices( + planetarium.PlanetariumPlotCelestialPastTrajectory( Plugin, root.flightGlobalsIndex, history_length, @@ -135,7 +135,7 @@ private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, Math.Min(min_distance_from_camera, min_past_distance); DrawLineMesh(trajectories.past, colour, GLLines.Style.Faded); if (main_vessel_guid != null) { - planetarium.PlanetariumGetCelestialFutureTrajectoryVertices( + planetarium.PlanetariumPlotCelestialFutureTrajectory( Plugin, root.flightGlobalsIndex, main_vessel_guid, @@ -163,7 +163,7 @@ private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, private void DrawLineMesh(UnityEngine.Mesh mesh, UnityEngine.Color colour, GLLines.Style style) { - mesh.vertices = VertexBuffer.get; + mesh.vertices = VertexBuffer.vertices; var indices = new int[vertex_count_]; for (int i = 0; i < vertex_count_; ++i) { indices[i] = i; @@ -215,7 +215,7 @@ private static class VertexBuffer { public static IntPtr data => handle_.AddrOfPinnedObject(); public static int size => vertices_.Length; - public static UnityEngine.Vector3[] get => vertices_; + public static UnityEngine.Vector3[] vertices => vertices_; private static UnityEngine.Vector3[] vertices_ = new UnityEngine.Vector3[10_000]; diff --git a/serialization/journal.proto b/serialization/journal.proto index cc2654a15b..b681717209 100644 --- a/serialization/journal.proto +++ b/serialization/journal.proto @@ -1808,60 +1808,9 @@ message PlanetariumDelete { optional Out out = 2; } -message PlanetariumPlotCelestialTrajectoryForPsychohistory { +message PlanetariumPlotCelestialFutureTrajectory { extend Method { - optional PlanetariumPlotCelestialTrajectoryForPsychohistory extension = 5161; - } - message In { - required fixed64 planetarium = 1 [(pointer_to) = "Planetarium const", - (disposable) = "DisposablePlanetarium", - (is_subject) = true]; - required fixed64 plugin = 2 [(pointer_to) = "Plugin const"]; - required int32 celestial_index = 3; - optional string vessel_guid = 4; - required double max_history_length = 5; - } - message Out { - required double minimal_distance_from_camera = 1; - } - message Return { - required fixed64 rp2_lines = 1 [(pointer_to) = "Iterator", - (disposable) = "DisposableIterator", - (is_produced) = true]; - } - optional In in = 1; - optional Out out = 2; - optional Return return = 3; -} - -message PlanetariumPlotCelestialTrajectoryForPredictionOrFlightPlan { - extend Method { - optional PlanetariumPlotCelestialTrajectoryForPredictionOrFlightPlan extension = 5162; - } - message In { - required fixed64 planetarium = 1 [(pointer_to) = "Planetarium const", - (disposable) = "DisposablePlanetarium", - (is_subject) = true]; - required fixed64 plugin = 2 [(pointer_to) = "Plugin const"]; - required int32 celestial_index = 3; - required string vessel_guid = 4; - } - message Out { - required double minimal_distance_from_camera = 1; - } - message Return { - required fixed64 rp2_lines = 1 [(pointer_to) = "Iterator", - (disposable) = "DisposableIterator", - (is_produced) = true]; - } - optional In in = 1; - optional Out out = 2; - optional Return return = 3; -} - -message PlanetariumGetCelestialFutureTrajectoryVertices { - extend Method { - optional PlanetariumGetCelestialFutureTrajectoryVertices extension = 5999; + optional PlanetariumPlotCelestialFutureTrajectory extension = 5161; } message In { required fixed64 planetarium = 1 [(pointer_to) = "Planetarium const", @@ -1881,9 +1830,9 @@ message PlanetariumGetCelestialFutureTrajectoryVertices { optional Out out = 2; } -message PlanetariumGetCelestialPastTrajectoryVertices { +message PlanetariumPlotCelestialPastTrajectory { extend Method { - optional PlanetariumGetCelestialPastTrajectoryVertices extension = 5998; + optional PlanetariumPlotCelestialPastTrajectory extension = 5162; } message In { required fixed64 planetarium = 1 [(pointer_to) = "Planetarium const", @@ -1903,9 +1852,9 @@ message PlanetariumGetCelestialPastTrajectoryVertices { optional Out out = 2; } -message PlanetariumGetFlightPlanSegmentVertices { +message PlanetariumPlotFlightPlanSegment { extend Method { - optional PlanetariumGetFlightPlanSegmentVertices extension = 5997; + optional PlanetariumPlotFlightPlanSegment extension = 5137; } message In { required fixed64 planetarium = 1 [(pointer_to) = "Planetarium const", @@ -1924,9 +1873,9 @@ message PlanetariumGetFlightPlanSegmentVertices { optional Out out = 2; } -message PlanetariumGetPredictionVertices { +message PlanetariumPlotPrediction { extend Method { - optional PlanetariumGetPredictionVertices extension = 5996; + optional PlanetariumPlotPrediction extension = 5136; } message In { required fixed64 planetarium = 1 [(pointer_to) = "Planetarium const", @@ -1944,9 +1893,9 @@ message PlanetariumGetPredictionVertices { optional Out out = 2; } -message PlanetariumGetPsychohistoryVertices { +message PlanetariumPlotPsychohistory { extend Method { - optional PlanetariumGetPsychohistoryVertices extension = 5995; + optional PlanetariumPlotPsychohistory extension = 5134; } message In { required fixed64 planetarium = 1 [(pointer_to) = "Planetarium const", @@ -1965,68 +1914,6 @@ message PlanetariumGetPsychohistoryVertices { optional Out out = 2; } -message PlanetariumPlotFlightPlanSegment { - extend Method { - optional PlanetariumPlotFlightPlanSegment extension = 5137; - } - message In { - required fixed64 planetarium = 1 [(pointer_to) = "Planetarium const", - (disposable) = "DisposablePlanetarium", - (is_subject) = true]; - required fixed64 plugin = 2 [(pointer_to) = "Plugin const"]; - required string vessel_guid = 4; - required int32 index = 5; - } - message Return { - required fixed64 rp2_lines = 1 [(pointer_to) = "Iterator", - (disposable) = "DisposableIterator", - (is_produced) = true]; - } - optional In in = 1; - optional Return return = 3; -} - -message PlanetariumPlotPrediction { - extend Method { - optional PlanetariumPlotPrediction extension = 5136; - } - message In { - required fixed64 planetarium = 1 [(pointer_to) = "Planetarium const", - (disposable) = "DisposablePlanetarium", - (is_subject) = true]; - required fixed64 plugin = 2 [(pointer_to) = "Plugin const"]; - required string vessel_guid = 4; - } - message Return { - required fixed64 rp2_lines = 1 [(pointer_to) = "Iterator", - (disposable) = "DisposableIterator", - (is_produced) = true]; - } - optional In in = 1; - optional Return return = 3; -} - -message PlanetariumPlotPsychohistory { - extend Method { - optional PlanetariumPlotPsychohistory extension = 5134; - } - message In { - required fixed64 planetarium = 1 [(pointer_to) = "Planetarium const", - (disposable) = "DisposablePlanetarium", - (is_subject) = true]; - required fixed64 plugin = 2 [(pointer_to) = "Plugin const"]; - required string vessel_guid = 4; - required double max_history_length = 5; - } - message Return { - required fixed64 rp2_lines = 1 [(pointer_to) = "Iterator", - (disposable) = "DisposableIterator", - (is_produced) = true]; - } - optional In in = 1; - optional Return return = 3; -} - message PrepareToReportCollisions { extend Method { optional PrepareToReportCollisions extension = 5118; From 1344dcfd21acae1a3fd9e851a5ddc7cd04f3050a Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Mon, 24 Jan 2022 03:26:20 +0100 Subject: [PATCH 24/33] make the tests compile --- ksp_plugin/interface_planetarium.cpp | 14 ++++--- ksp_plugin/planetarium.hpp | 2 + ksp_plugin/plugin.hpp | 3 +- .../interface_planetarium_test.cpp | 6 ++- ksp_plugin_test/mock_planetarium.hpp | 9 ++++- ksp_plugin_test/mock_plugin.hpp | 5 ++- ksp_plugin_test/planetarium_test.cpp | 37 +++++++++++++++---- 7 files changed, 58 insertions(+), 18 deletions(-) diff --git a/ksp_plugin/interface_planetarium.cpp b/ksp_plugin/interface_planetarium.cpp index d1f9ecaa9b..3431bba074 100644 --- a/ksp_plugin/interface_planetarium.cpp +++ b/ksp_plugin/interface_planetarium.cpp @@ -228,10 +228,10 @@ void __cdecl principia__PlanetariumPlotPsychohistory( } } -// TODO(egg): comment for the rendered past trajectory of the celestial with -// the given index; the trajectory goes back |max_history_length| seconds before -// the present time (or to the earliest time available if the relevant |t_min| -// is more recent). +// Fills the array of size |vertices_size| at |vertices| with vertices for the +// rendered past trajectory of the celestial with the given index; the +// trajectory goes back |max_history_length| seconds before the present time (or +// to the earliest time available if the relevant |t_min| is more recent). void __cdecl principia__PlanetariumPlotCelestialPastTrajectory( Planetarium const* const planetarium, Plugin const* const plugin, @@ -287,8 +287,10 @@ void __cdecl principia__PlanetariumPlotCelestialPastTrajectory( } } -// TODO(egg): comment; the trajectory goes as far as the furthest of the final time -// of the prediction or that of the flight plan. +// Fills the array of size |vertices_size| at |vertices| with vertices 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. void __cdecl principia__PlanetariumPlotCelestialFutureTrajectory( Planetarium const* const planetarium, Plugin const* const plugin, diff --git a/ksp_plugin/planetarium.hpp b/ksp_plugin/planetarium.hpp index 22ca346417..63bf6a43d1 100644 --- a/ksp_plugin/planetarium.hpp +++ b/ksp_plugin/planetarium.hpp @@ -124,6 +124,8 @@ class Planetarium { bool reverse, Length* minimal_distance = nullptr) const; + // A method similar to PlotMethod2, but which produces a three-dimensional + // trajectory in scaled space instead of projecting and hiding. void PlotMethod3( Trajectory const& trajectory, Instant const& first_time, diff --git a/ksp_plugin/plugin.hpp b/ksp_plugin/plugin.hpp index 45ef72ce89..3fa30c2d66 100644 --- a/ksp_plugin/plugin.hpp +++ b/ksp_plugin/plugin.hpp @@ -66,6 +66,7 @@ using geometry::Point; using geometry::Perspective; using geometry::Position; using geometry::Rotation; +using geometry::RigidTransformation; using geometry::Vector; using geometry::Velocity; using integrators::FixedStepSizeIntegrator; @@ -392,7 +393,7 @@ class Plugin { virtual not_null> NewPlanetarium( Planetarium::Parameters const& parameters, Perspective const& perspective, - geometry::RigidTransformation const& plotting_to_world, + RigidTransformation const& plotting_to_world, Inverse inverse_scale_factor, Position scaled_space_origin) const; diff --git a/ksp_plugin_test/interface_planetarium_test.cpp b/ksp_plugin_test/interface_planetarium_test.cpp index c1d05b46b5..367e64da36 100644 --- a/ksp_plugin_test/interface_planetarium_test.cpp +++ b/ksp_plugin_test/interface_planetarium_test.cpp @@ -62,7 +62,7 @@ TEST_F(InterfacePlanetariumTest, ConstructionDestruction) { Permutation( Permutation::CoordinatePermutation::YXZ) .Forget()))); - EXPECT_CALL(*plugin_, NewPlanetarium(_, _)) + EXPECT_CALL(*plugin_, NewPlanetarium(_, _, _, _, _)) .WillOnce(Return(ByMove(std::make_unique()))); Planetarium const* planetarium = principia__PlanetariumCreate(plugin_.get(), @@ -72,7 +72,9 @@ TEST_F(InterfacePlanetariumTest, ConstructionDestruction) { {0, 0, 1}, {1, 2, 3}, 10, - 90); + 90, + 1.0 / 6000, + {4, 5, 6}); principia__PlanetariumDelete(&planetarium); EXPECT_THAT(planetarium, IsNull()); } diff --git a/ksp_plugin_test/mock_planetarium.hpp b/ksp_plugin_test/mock_planetarium.hpp index 1332f97af8..67cb70cc5d 100644 --- a/ksp_plugin_test/mock_planetarium.hpp +++ b/ksp_plugin_test/mock_planetarium.hpp @@ -30,7 +30,14 @@ class MockPlanetarium : public Planetarium { .Forget()), 1 * Metre), make_not_null const*>(), - make_not_null()) {} + make_not_null(), + RigidTransformation( + Navigation::origin, + World::origin, + Signature::CentralInversion() + .Forget()), + /*inverse_scale_factor=*/1 / (6000 * Metre), + /*scaled_space_origin=*/World::origin) {} }; } // namespace internal_planetarium diff --git a/ksp_plugin_test/mock_plugin.hpp b/ksp_plugin_test/mock_plugin.hpp index b7d088ce1a..24e4922ec4 100644 --- a/ksp_plugin_test/mock_plugin.hpp +++ b/ksp_plugin_test/mock_plugin.hpp @@ -100,7 +100,10 @@ class MockPlugin : public Plugin { MOCK_METHOD(not_null>, NewPlanetarium, (Planetarium::Parameters const& parameters, - (Perspective const& perspective)), + (Perspective const& perspective), + (RigidTransformation const& plotting_to_world), + Inverse inverse_scale_factor, + Position scaled_space_origin), (const, override)); MOCK_METHOD(not_null>, NewBodyCentredNonRotatingNavigationFrame, diff --git a/ksp_plugin_test/planetarium_test.cpp b/ksp_plugin_test/planetarium_test.cpp index e2efc01ac5..ca4ad08ab8 100644 --- a/ksp_plugin_test/planetarium_test.cpp +++ b/ksp_plugin_test/planetarium_test.cpp @@ -107,6 +107,10 @@ class PlanetariumTest : public ::testing::Test { Sign::Positive(), DeduceSignReversingOrientation{}).Forget()), /*focal=*/5 * Metre), + plotting_to_world_(Navigation::origin, + World::origin, + Signature::CentralInversion() + .Forget()), // A body of radius 1 m located at the origin. body_(MassiveBody::Parameters(1 * Kilogram), RotatingBody::Parameters( @@ -134,6 +138,7 @@ class PlanetariumTest : public ::testing::Test { Instant const t0_; Perspective const perspective_; MockDynamicFrame plotting_frame_; + RigidTransformation plotting_to_world_; RotatingBody const body_; std::vector> const bodies_; MockContinuousTrajectory continuous_trajectory_; @@ -155,8 +160,13 @@ TEST_F(PlanetariumTest, PlotMethod0) { /*sphere_radius_multiplier=*/1, /*angular_resolution=*/0 * Degree, /*field_of_view=*/90 * Degree); - Planetarium planetarium( - parameters, perspective_, &ephemeris_, &plotting_frame_); + Planetarium planetarium(parameters, + perspective_, + &ephemeris_, + &plotting_frame_, + plotting_to_world_, + /*inverse_scale_factor=*/1 / (6000 * Metre), + /*scaled_space_origin=*/World::origin); auto const rp2_lines = planetarium.PlotMethod0(discrete_trajectory, discrete_trajectory.begin(), @@ -201,8 +211,13 @@ TEST_F(PlanetariumTest, PlotMethod1) { /*sphere_radius_multiplier=*/1, /*angular_resolution=*/0.4 * ArcMinute, /*field_of_view=*/90 * Degree); - Planetarium planetarium( - parameters, perspective_, &ephemeris_, &plotting_frame_); + Planetarium planetarium(parameters, + perspective_, + &ephemeris_, + &plotting_frame_, + plotting_to_world_, + /*inverse_scale_factor=*/1 / (6000 * Metre), + /*scaled_space_origin=*/World::origin); auto const rp2_lines = planetarium.PlotMethod1(discrete_trajectory, discrete_trajectory.begin(), @@ -237,8 +252,13 @@ TEST_F(PlanetariumTest, PlotMethod2) { /*sphere_radius_multiplier=*/1, /*angular_resolution=*/0.4 * ArcMinute, /*field_of_view=*/90 * Degree); - Planetarium planetarium( - parameters, perspective_, &ephemeris_, &plotting_frame_); + Planetarium planetarium(parameters, + perspective_, + &ephemeris_, + &plotting_frame_, + plotting_to_world_, + /*inverse_scale_factor=*/1 / (6000 * Metre), + /*scaled_space_origin=*/World::origin); auto const rp2_lines = planetarium.PlotMethod2(discrete_trajectory, discrete_trajectory.begin(), @@ -293,7 +313,10 @@ TEST_F(PlanetariumTest, RealSolarSystem) { Perspective(rigid_transformation, /*focal=*/1 * Metre), ephemeris.get(), - plotting_frame.get()); + plotting_frame.get(), + plotting_to_world_, + /*inverse_scale_factor=*/1 / (6000 * Metre), + /*scaled_space_origin=*/World::origin); auto const rp2_lines = planetarium.PlotMethod2(discrete_trajectory, discrete_trajectory.begin(), From ae021b455c8930f64229fcc34d1f098527e8c9e2 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Mon, 24 Jan 2022 04:48:11 +0100 Subject: [PATCH 25/33] limit the cheesy world --- ksp_plugin/interface_planetarium.cpp | 18 +++++++++++------ ksp_plugin/planetarium.cpp | 22 +++++---------------- ksp_plugin/planetarium.hpp | 28 ++++++++++++++------------- ksp_plugin/plugin.cpp | 9 +++------ ksp_plugin/plugin.hpp | 5 ++--- ksp_plugin_test/mock_planetarium.hpp | 13 +++++-------- ksp_plugin_test/mock_plugin.hpp | 5 ++--- ksp_plugin_test/planetarium_test.cpp | 29 ++++++++++++---------------- 8 files changed, 56 insertions(+), 73 deletions(-) diff --git a/ksp_plugin/interface_planetarium.cpp b/ksp_plugin/interface_planetarium.cpp index 3431bba074..f8d01a28be 100644 --- a/ksp_plugin/interface_planetarium.cpp +++ b/ksp_plugin/interface_planetarium.cpp @@ -100,12 +100,18 @@ Planetarium* __cdecl principia__PlanetariumCreate( world_to_plotting_affine_map * camera_to_world_affine_map, focal * Metre); - return m.Return(plugin->NewPlanetarium( - parameters, - perspective, - world_to_plotting_affine_map.Inverse(), - inverse_scale_factor * (1 / Metre), - FromXYZ>(scaled_space_origin)).release()); + return m.Return( + plugin->NewPlanetarium( + parameters, + perspective, + [plotting_to_world = world_to_plotting_affine_map.Inverse(), + scaled_space_origin = FromXYZ>(scaled_space_origin), + inverse_scale_factor = inverse_scale_factor * (1 / Metre)]( + Position const& plotted_point) { + return ScaledSpacePoint::FromCoordinates( + ((plotting_to_world(plotted_point) - scaled_space_origin) * + inverse_scale_factor).coordinates()); + }).release()); } void __cdecl principia__PlanetariumDelete( diff --git a/ksp_plugin/planetarium.cpp b/ksp_plugin/planetarium.cpp index f1264022b1..5b47cc7ce9 100644 --- a/ksp_plugin/planetarium.cpp +++ b/ksp_plugin/planetarium.cpp @@ -45,16 +45,13 @@ Planetarium::Planetarium( Perspective perspective, not_null const*> const ephemeris, not_null const plotting_frame, - RigidTransformation const& plotting_to_world, - Inverse const& inverse_scale_factor, - Position const& scaled_space_origin) + std::function const&)> + plotting_to_scaled_space) : parameters_(parameters), perspective_(std::move(perspective)), ephemeris_(ephemeris), plotting_frame_(plotting_frame), - plotting_to_world_(plotting_to_world), - inverse_scale_factor_(inverse_scale_factor), - scaled_space_origin_(scaled_space_origin) {} + plotting_to_scaled_space_(std::move(plotting_to_scaled_space)) {} RP2Lines Planetarium::PlotMethod0( DiscreteTrajectory const& trajectory, @@ -308,7 +305,7 @@ void Planetarium::PlotMethod3( initial_degrees_of_freedom.velocity(); Time Δt = final_time - previous_time; - add_point(WorldToScaledSpace(plotting_to_world_(previous_position))); + add_point(plotting_to_scaled_space_(previous_position)); int points_added = 1; Instant t; @@ -356,7 +353,7 @@ void Planetarium::PlotMethod3( previous_velocity = to_plotting_frame_at_t(*degrees_of_freedom_in_barycentric).velocity(); - add_point(WorldToScaledSpace(plotting_to_world_(position))); + add_point(plotting_to_scaled_space_(position)); ++points_added; if (minimal_distance != nullptr) { @@ -445,15 +442,6 @@ Segments Planetarium::ComputePlottableSegments( return all_segments; } -ScaledSpacePoint Planetarium::WorldToScaledSpace( - Position const& position) const { - R3Element const scaled_space_coordinates = - ((position - scaled_space_origin_) * inverse_scale_factor_).coordinates(); - return ScaledSpacePoint{static_cast(scaled_space_coordinates.x), - static_cast(scaled_space_coordinates.y), - static_cast(scaled_space_coordinates.z)}; -} - } // namespace internal_planetarium } // namespace ksp_plugin } // namespace principia diff --git a/ksp_plugin/planetarium.hpp b/ksp_plugin/planetarium.hpp index 63bf6a43d1..1b7199fde0 100644 --- a/ksp_plugin/planetarium.hpp +++ b/ksp_plugin/planetarium.hpp @@ -26,7 +26,7 @@ using geometry::Instant; using geometry::OrthogonalMap; using geometry::Perspective; using geometry::Position; -using geometry::RigidTransformation; +using geometry::R3Element; using geometry::RP2Lines; using geometry::RP2Point; using geometry::Segment; @@ -39,12 +39,13 @@ using physics::RigidMotion; using physics::Trajectory; using quantities::Angle; using quantities::Length; -using quantities::Inverse; // Corresponds to a UnityEngine.Vector3 representing a position in KSP’s // ScaledSpace. -extern "C" -struct ScaledSpacePoint { +extern "C" struct ScaledSpacePoint { + static inline ScaledSpacePoint FromCoordinates( + R3Element const& coordinates); + float x; float y; float z; @@ -83,9 +84,8 @@ class Planetarium { Perspective perspective, not_null const*> ephemeris, not_null plotting_frame, - RigidTransformation const& plotting_to_world, - Inverse const& inverse_scale_factor, - Position const& scaled_space_origin); + std::function const&)> + plotting_to_scaled_space); // A no-op method that just returns all the points in the trajectory defined // by |begin| and |end|. @@ -149,18 +149,20 @@ class Planetarium { DiscreteTrajectory::iterator begin, DiscreteTrajectory::iterator end) const; - ScaledSpacePoint WorldToScaledSpace(Position const& position) const; - Parameters const parameters_; - // TODO(egg): Consider distinguishing this copy of World. - RigidTransformation const plotting_to_world_; + std::function const&)> plotting_to_scaled_space_; Perspective const perspective_; not_null const*> const ephemeris_; not_null const plotting_frame_; - Inverse const inverse_scale_factor_; - Position const scaled_space_origin_; }; +inline ScaledSpacePoint ScaledSpacePoint::FromCoordinates( + R3Element const& coordinates) { + return ScaledSpacePoint{static_cast(coordinates.x), + static_cast(coordinates.y), + static_cast(coordinates.z)}; +} + } // namespace internal_planetarium using internal_planetarium::Planetarium; diff --git a/ksp_plugin/plugin.cpp b/ksp_plugin/plugin.cpp index 59d1e253f0..33c36a7e35 100644 --- a/ksp_plugin/plugin.cpp +++ b/ksp_plugin/plugin.cpp @@ -1086,17 +1086,14 @@ void Plugin::ClearOrbitAnalysersOfVesselsOtherThan(Vessel const& vessel) { not_null> Plugin::NewPlanetarium( Planetarium::Parameters const& parameters, Perspective const& perspective, - RigidTransformation const& plotting_to_world, - Inverse inverse_scale_factor, - Position scaled_space_origin) + std::function const&)> + plotting_to_scaled_space) const { return make_not_null_unique(parameters, perspective, ephemeris_.get(), renderer_->GetPlottingFrame(), - plotting_to_world, - inverse_scale_factor, - scaled_space_origin); + std::move(plotting_to_scaled_space)); } not_null> diff --git a/ksp_plugin/plugin.hpp b/ksp_plugin/plugin.hpp index 3fa30c2d66..230497cdce 100644 --- a/ksp_plugin/plugin.hpp +++ b/ksp_plugin/plugin.hpp @@ -393,9 +393,8 @@ class Plugin { virtual not_null> NewPlanetarium( Planetarium::Parameters const& parameters, Perspective const& perspective, - RigidTransformation const& plotting_to_world, - Inverse inverse_scale_factor, - Position scaled_space_origin) const; + std::function const&)> + plotting_to_scaled_space) const; virtual not_null> NewBarycentricRotatingNavigationFrame(Index primary_index, diff --git a/ksp_plugin_test/mock_planetarium.hpp b/ksp_plugin_test/mock_planetarium.hpp index 67cb70cc5d..20c160214e 100644 --- a/ksp_plugin_test/mock_planetarium.hpp +++ b/ksp_plugin_test/mock_planetarium.hpp @@ -30,14 +30,11 @@ class MockPlanetarium : public Planetarium { .Forget()), 1 * Metre), make_not_null const*>(), - make_not_null(), - RigidTransformation( - Navigation::origin, - World::origin, - Signature::CentralInversion() - .Forget()), - /*inverse_scale_factor=*/1 / (6000 * Metre), - /*scaled_space_origin=*/World::origin) {} + [](Position const& plotted_point) { + return ScaledSpacePoint::FromCoordinates( + ((plotted_point - Navigation::origin) * + (1 / (6000 * Metre))).coordinates()); + }) {} }; } // namespace internal_planetarium diff --git a/ksp_plugin_test/mock_plugin.hpp b/ksp_plugin_test/mock_plugin.hpp index 24e4922ec4..ddea647ea1 100644 --- a/ksp_plugin_test/mock_plugin.hpp +++ b/ksp_plugin_test/mock_plugin.hpp @@ -101,9 +101,8 @@ class MockPlugin : public Plugin { NewPlanetarium, (Planetarium::Parameters const& parameters, (Perspective const& perspective), - (RigidTransformation const& plotting_to_world), - Inverse inverse_scale_factor, - Position scaled_space_origin), + std::function const&)> + plotting_to_scaled_space), (const, override)); MOCK_METHOD(not_null>, NewBodyCentredNonRotatingNavigationFrame, diff --git a/ksp_plugin_test/planetarium_test.cpp b/ksp_plugin_test/planetarium_test.cpp index ca4ad08ab8..3ab034fc27 100644 --- a/ksp_plugin_test/planetarium_test.cpp +++ b/ksp_plugin_test/planetarium_test.cpp @@ -107,10 +107,12 @@ class PlanetariumTest : public ::testing::Test { Sign::Positive(), DeduceSignReversingOrientation{}).Forget()), /*focal=*/5 * Metre), - plotting_to_world_(Navigation::origin, - World::origin, - Signature::CentralInversion() - .Forget()), + plotting_to_scaled_space_( + [](Position const& plotted_point) { + return ScaledSpacePoint::FromCoordinates( + ((plotted_point - Navigation::origin) * + (1 / (6000 * Metre))).coordinates()); + }) // A body of radius 1 m located at the origin. body_(MassiveBody::Parameters(1 * Kilogram), RotatingBody::Parameters( @@ -138,7 +140,8 @@ class PlanetariumTest : public ::testing::Test { Instant const t0_; Perspective const perspective_; MockDynamicFrame plotting_frame_; - RigidTransformation plotting_to_world_; + std::function const&)> + plotting_to_scaled_space_; RotatingBody const body_; std::vector> const bodies_; MockContinuousTrajectory continuous_trajectory_; @@ -164,9 +167,7 @@ TEST_F(PlanetariumTest, PlotMethod0) { perspective_, &ephemeris_, &plotting_frame_, - plotting_to_world_, - /*inverse_scale_factor=*/1 / (6000 * Metre), - /*scaled_space_origin=*/World::origin); + plotting_to_scaled_space_); auto const rp2_lines = planetarium.PlotMethod0(discrete_trajectory, discrete_trajectory.begin(), @@ -215,9 +216,7 @@ TEST_F(PlanetariumTest, PlotMethod1) { perspective_, &ephemeris_, &plotting_frame_, - plotting_to_world_, - /*inverse_scale_factor=*/1 / (6000 * Metre), - /*scaled_space_origin=*/World::origin); + plotting_to_scaled_space_); auto const rp2_lines = planetarium.PlotMethod1(discrete_trajectory, discrete_trajectory.begin(), @@ -256,9 +255,7 @@ TEST_F(PlanetariumTest, PlotMethod2) { perspective_, &ephemeris_, &plotting_frame_, - plotting_to_world_, - /*inverse_scale_factor=*/1 / (6000 * Metre), - /*scaled_space_origin=*/World::origin); + plotting_to_scaled_space_); auto const rp2_lines = planetarium.PlotMethod2(discrete_trajectory, discrete_trajectory.begin(), @@ -314,9 +311,7 @@ TEST_F(PlanetariumTest, RealSolarSystem) { /*focal=*/1 * Metre), ephemeris.get(), plotting_frame.get(), - plotting_to_world_, - /*inverse_scale_factor=*/1 / (6000 * Metre), - /*scaled_space_origin=*/World::origin); + plotting_to_scaled_space_); auto const rp2_lines = planetarium.PlotMethod2(discrete_trajectory, discrete_trajectory.begin(), From f42939119b19ea02f31c75d916c7a142d165ef79 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Mon, 24 Jan 2022 05:42:48 +0100 Subject: [PATCH 26/33] make it build more --- ksp_plugin_test/interface_planetarium_test.cpp | 2 +- ksp_plugin_test/mock_planetarium.hpp | 1 + ksp_plugin_test/planetarium_test.cpp | 3 ++- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ksp_plugin_test/interface_planetarium_test.cpp b/ksp_plugin_test/interface_planetarium_test.cpp index 367e64da36..56ae1c168f 100644 --- a/ksp_plugin_test/interface_planetarium_test.cpp +++ b/ksp_plugin_test/interface_planetarium_test.cpp @@ -62,7 +62,7 @@ TEST_F(InterfacePlanetariumTest, ConstructionDestruction) { Permutation( Permutation::CoordinatePermutation::YXZ) .Forget()))); - EXPECT_CALL(*plugin_, NewPlanetarium(_, _, _, _, _)) + EXPECT_CALL(*plugin_, NewPlanetarium(_, _, _)) .WillOnce(Return(ByMove(std::make_unique()))); Planetarium const* planetarium = principia__PlanetariumCreate(plugin_.get(), diff --git a/ksp_plugin_test/mock_planetarium.hpp b/ksp_plugin_test/mock_planetarium.hpp index 20c160214e..1ef3888cdd 100644 --- a/ksp_plugin_test/mock_planetarium.hpp +++ b/ksp_plugin_test/mock_planetarium.hpp @@ -30,6 +30,7 @@ class MockPlanetarium : public Planetarium { .Forget()), 1 * Metre), make_not_null const*>(), + make_not_null(), [](Position const& plotted_point) { return ScaledSpacePoint::FromCoordinates( ((plotted_point - Navigation::origin) * diff --git a/ksp_plugin_test/planetarium_test.cpp b/ksp_plugin_test/planetarium_test.cpp index 3ab034fc27..7dc189e133 100644 --- a/ksp_plugin_test/planetarium_test.cpp +++ b/ksp_plugin_test/planetarium_test.cpp @@ -49,6 +49,7 @@ using geometry::Instant; using geometry::LinearMap; using geometry::OrthogonalMap; using geometry::Perspective; +using geometry::Position; using geometry::RigidTransformation; using geometry::Rotation; using geometry::Sign; @@ -112,7 +113,7 @@ class PlanetariumTest : public ::testing::Test { return ScaledSpacePoint::FromCoordinates( ((plotted_point - Navigation::origin) * (1 / (6000 * Metre))).coordinates()); - }) + }), // A body of radius 1 m located at the origin. body_(MassiveBody::Parameters(1 * Kilogram), RotatingBody::Parameters( From 34ca9673885cb1794b18d9cc6cb2d920af5f2ba1 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Mon, 24 Jan 2022 06:32:33 +0100 Subject: [PATCH 27/33] make the benchmarks build --- benchmarks/planetarium_plot_methods.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/benchmarks/planetarium_plot_methods.cpp b/benchmarks/planetarium_plot_methods.cpp index 8f6275b9cc..a4ba49bc3b 100644 --- a/benchmarks/planetarium_plot_methods.cpp +++ b/benchmarks/planetarium_plot_methods.cpp @@ -36,6 +36,8 @@ using ksp_plugin::Barycentric; using ksp_plugin::Navigation; using ksp_plugin::NavigationFrame; using ksp_plugin::Planetarium; +using ksp_plugin::ScaledSpacePoint; +using ksp_plugin::World; using physics::BodyCentredNonRotatingDynamicFrame; using physics::DegreesOfFreedom; using physics::DiscreteTrajectory; @@ -171,7 +173,12 @@ class Satellites { return Planetarium(parameters, perspective, ephemeris_.get(), - earth_centred_inertial_.get()); + earth_centred_inertial_.get(), + [](Position const& plotted_point) { + return ScaledSpacePoint::FromCoordinates( + ((plotted_point - World::origin) * + (1 / (6000 * Metre))).coordinates()); + }); } private: From 438483b8371c69ca59974237ed70fec9b1b41c0e Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Mon, 24 Jan 2022 06:33:14 +0100 Subject: [PATCH 28/33] navigation --- benchmarks/planetarium_plot_methods.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmarks/planetarium_plot_methods.cpp b/benchmarks/planetarium_plot_methods.cpp index a4ba49bc3b..e71c7e312c 100644 --- a/benchmarks/planetarium_plot_methods.cpp +++ b/benchmarks/planetarium_plot_methods.cpp @@ -176,7 +176,7 @@ class Satellites { earth_centred_inertial_.get(), [](Position const& plotted_point) { return ScaledSpacePoint::FromCoordinates( - ((plotted_point - World::origin) * + ((plotted_point - Navigation::origin) * (1 / (6000 * Metre))).coordinates()); }); } From 6b959f46b06bd762e690deb2a156e61e4d0f8ffe Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Mon, 24 Jan 2022 13:37:17 +0100 Subject: [PATCH 29/33] lint --- ksp_plugin/planetarium.hpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ksp_plugin/planetarium.hpp b/ksp_plugin/planetarium.hpp index 1b7199fde0..d235eb9fc2 100644 --- a/ksp_plugin/planetarium.hpp +++ b/ksp_plugin/planetarium.hpp @@ -78,8 +78,6 @@ class Planetarium { // TODO(phl): All this Navigation is weird. Should it be named Plotting? // In particular Navigation vs. NavigationFrame is a mess. - // |inverse_scale_factor| and |scaled_space_origin| define the - // transformation from World to ScaledSpace. Planetarium(Parameters const& parameters, Perspective perspective, not_null const*> ephemeris, @@ -150,7 +148,8 @@ class Planetarium { DiscreteTrajectory::iterator end) const; Parameters const parameters_; - std::function const&)> plotting_to_scaled_space_; + std::function const&)> + plotting_to_scaled_space_; Perspective const perspective_; not_null const*> const ephemeris_; not_null const plotting_frame_; From d4aae0f364cc2838deedb9bf37a44dca060bf14c Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Wed, 26 Jan 2022 03:02:36 +0100 Subject: [PATCH 30/33] =?UTF-8?q?pleroy=E2=80=99s=20comments=20on=20the=20?= =?UTF-8?q?C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ksp_plugin_adapter/ksp_plugin_adapter.cs | 4 +- ksp_plugin_adapter/plotter.cs | 163 +++++++++++++---------- 2 files changed, 94 insertions(+), 73 deletions(-) diff --git a/ksp_plugin_adapter/ksp_plugin_adapter.cs b/ksp_plugin_adapter/ksp_plugin_adapter.cs index cbffd2a7aa..948e1d7ea6 100644 --- a/ksp_plugin_adapter/ksp_plugin_adapter.cs +++ b/ksp_plugin_adapter/ksp_plugin_adapter.cs @@ -109,7 +109,7 @@ internal IntPtr Plugin() { private bool navball_changed_ = true; private FlightGlobals.SpeedDisplayModes? previous_display_mode_; - // TODO(egg): move all of that to the plotter once the RP2 lines are gone. + // TODO(egg): move all of that to the plotter. internal UnityEngine.Color history_colour = XKCDColors.Lime; internal GLLines.Style history_style = GLLines.Style.Faded; internal UnityEngine.Color prediction_colour = XKCDColors.Fuchsia; @@ -2126,7 +2126,7 @@ private void RenderTrajectories() { XYZ sun_world_position = (XYZ)Planetarium.fetch.Sun.position; using (DisposablePlanetarium planetarium = GLLines.NewPlanetarium(plugin_, sun_world_position)) { - plotter_.Plot(planetarium, main_vessel_guid, main_window_.history_length); + plotter_.PlotTrajectories(planetarium, main_vessel_guid, main_window_.history_length); } } } diff --git a/ksp_plugin_adapter/plotter.cs b/ksp_plugin_adapter/plotter.cs index aff1090dee..08b5e1a21e 100644 --- a/ksp_plugin_adapter/plotter.cs +++ b/ksp_plugin_adapter/plotter.cs @@ -10,9 +10,9 @@ public Plotter(PrincipiaPluginAdapter adapter) { adapter_ = adapter; } - public void Plot(DisposablePlanetarium planetarium, - string main_vessel_guid, - double history_length) { + public void PlotTrajectories(DisposablePlanetarium planetarium, + string main_vessel_guid, + double history_length) { PlotCelestialTrajectories(planetarium, main_vessel_guid, history_length); if (main_vessel_guid == null) { return; @@ -23,53 +23,66 @@ public void Plot(DisposablePlanetarium planetarium, private void PlotVesselTrajectories(DisposablePlanetarium planetarium, string main_vessel_guid, double history_length) { - planetarium.PlanetariumPlotPsychohistory( - Plugin, - main_vessel_guid, - history_length, - VertexBuffer.data, - VertexBuffer.size, - out vertex_count_); - DrawLineMesh(psychohistory_, adapter_.history_colour, adapter_.history_style); - planetarium.PlanetariumPlotPrediction( - Plugin, - main_vessel_guid, - VertexBuffer.data, - VertexBuffer.size, - out vertex_count_); - DrawLineMesh(prediction_, adapter_.prediction_colour, adapter_.prediction_style); - - // Target psychohistory and prediction. - string target_id = FlightGlobals.fetch.VesselTarget?.GetVessel()?.id. - ToString(); - if (FlightGlobals.ActiveVessel != null && - !adapter_.plotting_frame_selector_.target_frame_selected && - target_id != null && - Plugin.HasVessel(target_id)) { + { planetarium.PlanetariumPlotPsychohistory( Plugin, - target_id, + main_vessel_guid, history_length, VertexBuffer.data, VertexBuffer.size, - out vertex_count_); - DrawLineMesh(target_psychohistory_, adapter_.target_history_colour, - adapter_.target_history_style); + out int vertex_count); + DrawLineMesh(psychohistory_mesh_, vertex_count, adapter_.history_colour, + adapter_.history_style); + } + { planetarium.PlanetariumPlotPrediction( Plugin, - target_id, + main_vessel_guid, VertexBuffer.data, VertexBuffer.size, - out vertex_count_); - DrawLineMesh(target_prediction_, adapter_.target_prediction_colour, - adapter_.target_prediction_style); + out int vertex_count); + DrawLineMesh(prediction_mesh_, vertex_count, adapter_.prediction_colour, + adapter_.prediction_style); } + + // Target psychohistory and prediction. + string target_id = FlightGlobals.fetch.VesselTarget?.GetVessel()?.id. + ToString(); + if (FlightGlobals.ActiveVessel != null && + !adapter_.plotting_frame_selector_.target_frame_selected && + target_id != null && + Plugin.HasVessel(target_id)) { + { + planetarium.PlanetariumPlotPsychohistory( + Plugin, + target_id, + history_length, + VertexBuffer.data, + VertexBuffer.size, + out int vertex_count); + DrawLineMesh(target_psychohistory_mesh_, vertex_count, + adapter_.target_history_colour, + adapter_.target_history_style); + } + { + planetarium.PlanetariumPlotPrediction( + Plugin, + target_id, + VertexBuffer.data, + VertexBuffer.size, + out int vertex_count); + DrawLineMesh(target_prediction_mesh_, vertex_count, + adapter_.target_prediction_colour, + adapter_.target_prediction_style); + } + } + // Main vessel flight plan. if (Plugin.FlightPlanExists(main_vessel_guid)) { int number_of_segments = Plugin.FlightPlanNumberOfSegments(main_vessel_guid); - for (int i = flight_plan_segments_.Count; i < number_of_segments; ++i) { - flight_plan_segments_.Add(MakeDynamicMesh()); + for (int i = flight_plan_segment_meshes_.Count; i < number_of_segments; ++i) { + flight_plan_segment_meshes_.Add(MakeDynamicMesh()); } for (int i = 0; i < number_of_segments; ++i) { bool is_burn = i % 2 == 1; @@ -79,8 +92,9 @@ private void PlotVesselTrajectories(DisposablePlanetarium planetarium, i, VertexBuffer.data, VertexBuffer.size, - out vertex_count_); - DrawLineMesh(flight_plan_segments_[i], + out int vertex_count); + DrawLineMesh(flight_plan_segment_meshes_[i], + vertex_count, is_burn ? adapter_.burn_colour : adapter_.flight_plan_colour, is_burn ? adapter_.burn_style @@ -106,14 +120,15 @@ private void PlotCelestialTrajectories(DisposablePlanetarium planetarium, Planetarium.fetch.Sun, tan_angular_resolution); } + // Plots the trajectories of |root| and its natural satellites. private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, - string main_vessel_guid, - double history_length, - CelestialBody root, - double tan_angular_resolution) { + string main_vessel_guid, + double history_length, + CelestialBody root, + double tan_angular_resolution) { CelestialTrajectories trajectories; - if (!celestial_trajectories_.TryGetValue(root, out trajectories)) { - trajectories = celestial_trajectories_[root] = + if (!celestial_trajectory_meshes_.TryGetValue(root, out trajectories)) { + trajectories = celestial_trajectory_meshes_[root] = new CelestialTrajectories(); } var colour = root.orbitDriver?.Renderer?.orbitColor ?? @@ -123,17 +138,21 @@ private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, double min_distance_from_camera = (root.position - camera_world_position).magnitude; if (!adapter_.plotting_frame_selector_.FixesBody(root)) { - planetarium.PlanetariumPlotCelestialPastTrajectory( - Plugin, - root.flightGlobalsIndex, - history_length, - VertexBuffer.data, - VertexBuffer.size, - out double min_past_distance, - out vertex_count_); - min_distance_from_camera = - Math.Min(min_distance_from_camera, min_past_distance); - DrawLineMesh(trajectories.past, colour, GLLines.Style.Faded); + { + planetarium.PlanetariumPlotCelestialPastTrajectory( + Plugin, + root.flightGlobalsIndex, + history_length, + VertexBuffer.data, + VertexBuffer.size, + out double min_past_distance, + out int vertex_count); + min_distance_from_camera = + Math.Min(min_distance_from_camera, min_past_distance); + DrawLineMesh(trajectories.past, vertex_count, colour, + GLLines.Style.Faded); + } + if (main_vessel_guid != null) { planetarium.PlanetariumPlotCelestialFutureTrajectory( Plugin, @@ -142,10 +161,11 @@ private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, VertexBuffer.data, VertexBuffer.size, out double min_future_distance, - out vertex_count_); + out int vertex_count); min_distance_from_camera = Math.Min(min_distance_from_camera, min_future_distance); - DrawLineMesh(trajectories.future, colour, GLLines.Style.Solid); + DrawLineMesh(trajectories.future, vertex_count, colour, + GLLines.Style.Solid); } } foreach (CelestialBody child in root.orbitingBodies) { @@ -161,24 +181,25 @@ private void PlotSubtreeTrajectories(DisposablePlanetarium planetarium, } private void DrawLineMesh(UnityEngine.Mesh mesh, + int vertex_count, UnityEngine.Color colour, GLLines.Style style) { mesh.vertices = VertexBuffer.vertices; - var indices = new int[vertex_count_]; - for (int i = 0; i < vertex_count_; ++i) { + var indices = new int[vertex_count]; + for (int i = 0; i < vertex_count; ++i) { indices[i] = i; } var colours = new UnityEngine.Color[VertexBuffer.size]; if (style == GLLines.Style.Faded) { - for (int i = 0; i < vertex_count_; ++i) { + for (int i = 0; i < vertex_count; ++i) { var faded_colour = colour; - // Fade from the opacity of |colour| (when i = 0) down to 1/4 of that + // Fade from the opacity of |colour| (when i = 0) down to 20% of that // opacity. - faded_colour.a *= 1 - (float)(4 * i) / (float)(5 * vertex_count_); + faded_colour.a *= 1 - 0.8f * (i / (float)vertex_count); colours[i] = faded_colour; } } else { - for (int i = 0; i < vertex_count_; ++i) { + for (int i = 0; i < vertex_count; ++i) { colours[i] = colour; } } @@ -223,20 +244,20 @@ private static class VertexBuffer { GCHandle.Alloc(vertices_, GCHandleType.Pinned); } - private int vertex_count_; - private class CelestialTrajectories { public UnityEngine.Mesh future = MakeDynamicMesh(); public UnityEngine.Mesh past = MakeDynamicMesh(); } - private Dictionary celestial_trajectories_ = + private Dictionary + celestial_trajectory_meshes_ = new Dictionary(); - private UnityEngine.Mesh psychohistory_ = MakeDynamicMesh(); - private UnityEngine.Mesh prediction_ = MakeDynamicMesh(); - private List flight_plan_segments_ = new List(); - private UnityEngine.Mesh target_psychohistory_ = MakeDynamicMesh(); - private UnityEngine.Mesh target_prediction_ = MakeDynamicMesh(); + private UnityEngine.Mesh psychohistory_mesh_ = MakeDynamicMesh(); + private UnityEngine.Mesh prediction_mesh_ = MakeDynamicMesh(); + private List flight_plan_segment_meshes_ = + new List(); + private UnityEngine.Mesh target_psychohistory_mesh_ = MakeDynamicMesh(); + private UnityEngine.Mesh target_prediction_mesh_ = MakeDynamicMesh(); } } // namespace ksp_plugin_adapter From e919037d222b6123d335c0f00ed78f279da295a7 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Wed, 26 Jan 2022 04:39:04 +0100 Subject: [PATCH 31/33] C++ comments --- benchmarks/planetarium_plot_methods.cpp | 7 ++-- ksp_plugin/interface_planetarium.cpp | 55 +++++++++++++++---------- ksp_plugin/planetarium.cpp | 27 +++++++++--- ksp_plugin/planetarium.hpp | 35 ++++++++++------ ksp_plugin/plugin.hpp | 2 - ksp_plugin_test/mock_planetarium.hpp | 3 +- ksp_plugin_test/planetarium_test.cpp | 3 +- 7 files changed, 86 insertions(+), 46 deletions(-) diff --git a/benchmarks/planetarium_plot_methods.cpp b/benchmarks/planetarium_plot_methods.cpp index e71c7e312c..c302960fad 100644 --- a/benchmarks/planetarium_plot_methods.cpp +++ b/benchmarks/planetarium_plot_methods.cpp @@ -170,14 +170,15 @@ class Satellites { /*sphere_radius_multiplier=*/1, /*angular_resolution=*/0.4 * ArcMinute, /*field_of_view=*/90 * Degree); + constexpr auto inverse_scale_factor = (1 / (6000 * Metre); return Planetarium(parameters, perspective, ephemeris_.get(), earth_centred_inertial_.get(), [](Position const& plotted_point) { - return ScaledSpacePoint::FromCoordinates( - ((plotted_point - Navigation::origin) * - (1 / (6000 * Metre))).coordinates()); + return ScaledSpacePoint::FromCoordinates( + ((plotted_point - Navigation::origin) * + inverse_scale_factor).coordinates()); }); } diff --git a/ksp_plugin/interface_planetarium.cpp b/ksp_plugin/interface_planetarium.cpp index f8d01a28be..0e62b61e4b 100644 --- a/ksp_plugin/interface_planetarium.cpp +++ b/ksp_plugin/interface_planetarium.cpp @@ -100,18 +100,20 @@ Planetarium* __cdecl principia__PlanetariumCreate( world_to_plotting_affine_map * camera_to_world_affine_map, focal * Metre); + auto const plotting_to_scaled_space = + [plotting_to_world = world_to_plotting_affine_map.Inverse(), + scaled_space_origin = FromXYZ>(scaled_space_origin), + inverse_scale_factor = inverse_scale_factor * (1 / Metre)]( + Position const& plotted_point) { + return ScaledSpacePoint::FromCoordinates( + ((plotting_to_world(plotted_point) - scaled_space_origin) * + inverse_scale_factor).coordinates()); + }; return m.Return( plugin->NewPlanetarium( parameters, perspective, - [plotting_to_world = world_to_plotting_affine_map.Inverse(), - scaled_space_origin = FromXYZ>(scaled_space_origin), - inverse_scale_factor = inverse_scale_factor * (1 / Metre)]( - Position const& plotted_point) { - return ScaledSpacePoint::FromCoordinates( - ((plotting_to_world(plotted_point) - scaled_space_origin) * - inverse_scale_factor).coordinates()); - }).release()); + plotting_to_scaled_space).release()); } void __cdecl principia__PlanetariumDelete( @@ -122,6 +124,9 @@ void __cdecl principia__PlanetariumDelete( return m.Return(); } +// Fills the array of size |vertices_size| at |vertices| with vertices for the +// rendering of the segment with the given index in the flight plan of the +// vessel with the given GUID. void __cdecl principia__PlanetariumPlotFlightPlanSegment( Planetarium const* const planetarium, Plugin const* const plugin, @@ -140,19 +145,15 @@ void __cdecl principia__PlanetariumPlotFlightPlanSegment( Vessel const& vessel = *plugin->GetVessel(vessel_guid); CHECK(vessel.has_flight_plan()) << vessel_guid; auto const segment = vessel.flight_plan().GetSegment(index); - if (segment->empty()) { - return m.Return(); - } // TODO(egg): this is ugly; we should centralize rendering. // If this is a burn and we cannot render the beginning of the burn, we // render none of it, otherwise we try to render the Frenet trihedron at the // start and we fail. if (index % 2 == 0 || + segment->empty() || segment->front().time >= plugin->renderer().GetPlottingFrame()->t_min()) { planetarium->PlotMethod3( - *segment, - segment->front().time, - segment->back().time, + *segment, segment->begin(), segment->end(), plugin->CurrentTime(), /*reverse=*/false, [vertices, vertex_count](ScaledSpacePoint const& vertex) { @@ -163,6 +164,8 @@ void __cdecl principia__PlanetariumPlotFlightPlanSegment( return m.Return(); } +// Fills the array of size |vertices_size| at |vertices| with vertices for the +// rendered prediction of the vessel with the given GUID. void __cdecl principia__PlanetariumPlotPrediction( Planetarium const* const planetarium, Plugin const* const plugin, @@ -179,9 +182,7 @@ void __cdecl principia__PlanetariumPlotPrediction( auto const prediction = plugin->GetVessel(vessel_guid)->prediction(); planetarium->PlotMethod3( - *prediction, - prediction->front().time, - prediction->back().time, + *prediction, prediction->begin(), prediction->end(), plugin->CurrentTime(), /*reverse=*/false, [vertices, vertex_count](ScaledSpacePoint const& vertex) { @@ -191,6 +192,10 @@ void __cdecl principia__PlanetariumPlotPrediction( return m.Return(); } +// Fills the array of size |vertices_size| at |vertices| with vertices for the +// rendered past trajectory of the vessel with the given GUID; the +// trajectory goes back |max_history_length| seconds before the present time (or +// to the earliest time available if the relevant |t_min| is more recent). void __cdecl principia__PlanetariumPlotPsychohistory( Planetarium const* const planetarium, Plugin const* const plugin, @@ -218,12 +223,20 @@ void __cdecl principia__PlanetariumPlotPsychohistory( } else { auto const vessel = plugin->GetVessel(vessel_guid); auto const& trajectory = vessel->trajectory(); + auto const& psychohistory = vessel->psychohistory(); + + Instant const desired_first_time = + plugin->CurrentTime() - max_history_length * Second; + + // Since we would want to plot starting from |desired_first_time|, ask the + // reanimator to reconstruct the past. That may take a while, during which + // time the history will be shorter than desired. + vessel->RequestReanimation(desired_first_time); + planetarium->PlotMethod3( trajectory, - trajectory - .lower_bound(plugin->CurrentTime() - max_history_length * Second) - ->time, - /*last_time=*/plugin->CurrentTime(), + trajectory.lower_bound(desired_first_time), + psychohistory->end(), /*now=*/plugin->CurrentTime(), /*reverse=*/true, [vertices, vertex_count](ScaledSpacePoint const& vertex) { diff --git a/ksp_plugin/planetarium.cpp b/ksp_plugin/planetarium.cpp index 5b47cc7ce9..caa7bb3d4b 100644 --- a/ksp_plugin/planetarium.cpp +++ b/ksp_plugin/planetarium.cpp @@ -45,8 +45,7 @@ Planetarium::Planetarium( Perspective perspective, not_null const*> const ephemeris, not_null const plotting_frame, - std::function const&)> - plotting_to_scaled_space) + PlottingToScaledSpaceConversion plotting_to_scaled_space) : parameters_(parameters), perspective_(std::move(perspective)), ephemeris_(ephemeris), @@ -272,15 +271,33 @@ RP2Lines Planetarium::PlotMethod2( return lines; } +void Planetarium::PlotMethod3( + Trajectory const& trajectory, + DiscreteTrajectory::iterator begin, + DiscreteTrajectory::iterator end, + Instant const& now, + bool const reverse, + std::function const& add_point, + int max_points) const { + if (begin == end) { + return {}; + } + auto last = std::prev(end); + auto const begin_time = std::max(begin->time, plotting_frame_->t_min()); + auto const last_time = std::min(last->time, plotting_frame_->t_max()); + PlotMethod3( + trajectory, begin_time, last_time, now, reverse, add_point, max_points); +} + void Planetarium::PlotMethod3( Trajectory const& trajectory, Instant const& first_time, Instant const& last_time, Instant const& now, - bool reverse, - std::function add_point, + bool const reverse, + std::function const& add_point, int const max_points, - Length* minimal_distance) const { + Length* const minimal_distance) const { double const tan²_angular_resolution = Pow<2>(parameters_.tan_angular_resolution_); auto const final_time = reverse ? first_time : last_time; diff --git a/ksp_plugin/planetarium.hpp b/ksp_plugin/planetarium.hpp index d235eb9fc2..ee2187fdaa 100644 --- a/ksp_plugin/planetarium.hpp +++ b/ksp_plugin/planetarium.hpp @@ -76,14 +76,16 @@ class Planetarium { friend class Planetarium; }; + using PlottingToScaledSpaceConversion = + std::function const&)>; + // TODO(phl): All this Navigation is weird. Should it be named Plotting? // In particular Navigation vs. NavigationFrame is a mess. Planetarium(Parameters const& parameters, Perspective perspective, not_null const*> ephemeris, not_null plotting_frame, - std::function const&)> - plotting_to_scaled_space); + PlottingToScaledSpaceConversion plotting_to_scaled_space); // A no-op method that just returns all the points in the trajectory defined // by |begin| and |end|. @@ -124,15 +126,23 @@ class Planetarium { // A method similar to PlotMethod2, but which produces a three-dimensional // trajectory in scaled space instead of projecting and hiding. - void PlotMethod3( - Trajectory const& trajectory, - Instant const& first_time, - Instant const& last_time, - Instant const& now, - bool reverse, - std::function add_point, - int max_points, - Length* minimal_distance = nullptr) const; + void PlotMethod3(Trajectory const& trajectory, + DiscreteTrajectory::iterator begin, + DiscreteTrajectory::iterator end, + Instant const& now, + bool reverse, + std::function const& add_point, + int max_points) const; + + // The same method, operating on the |Trajectory| interface. + void PlotMethod3(Trajectory const& trajectory, + Instant const& first_time, + Instant const& last_time, + Instant const& now, + bool reverse, + std::function const& add_point, + int max_points, + Length* minimal_distance = nullptr) const; private: // Computes the coordinates of the spheres that represent the |ephemeris_| @@ -148,8 +158,7 @@ class Planetarium { DiscreteTrajectory::iterator end) const; Parameters const parameters_; - std::function const&)> - plotting_to_scaled_space_; + PlottingToScaledSpaceConversion plotting_to_scaled_space_; Perspective const perspective_; not_null const*> const ephemeris_; not_null const plotting_frame_; diff --git a/ksp_plugin/plugin.hpp b/ksp_plugin/plugin.hpp index 230497cdce..055c89c499 100644 --- a/ksp_plugin/plugin.hpp +++ b/ksp_plugin/plugin.hpp @@ -66,7 +66,6 @@ using geometry::Point; using geometry::Perspective; using geometry::Position; using geometry::Rotation; -using geometry::RigidTransformation; using geometry::Vector; using geometry::Velocity; using integrators::FixedStepSizeIntegrator; @@ -87,7 +86,6 @@ using physics::RotatingBody; using physics::Trajectory; using quantities::Angle; using quantities::Force; -using quantities::Inverse; using quantities::Length; using quantities::Mass; using quantities::Time; diff --git a/ksp_plugin_test/mock_planetarium.hpp b/ksp_plugin_test/mock_planetarium.hpp index 1ef3888cdd..9f6710f26f 100644 --- a/ksp_plugin_test/mock_planetarium.hpp +++ b/ksp_plugin_test/mock_planetarium.hpp @@ -32,9 +32,10 @@ class MockPlanetarium : public Planetarium { make_not_null const*>(), make_not_null(), [](Position const& plotted_point) { + constexpr auto inverse_scale_factor = 1 / (6000 * Metre); return ScaledSpacePoint::FromCoordinates( ((plotted_point - Navigation::origin) * - (1 / (6000 * Metre))).coordinates()); + inverse_scale_factor).coordinates()); }) {} }; diff --git a/ksp_plugin_test/planetarium_test.cpp b/ksp_plugin_test/planetarium_test.cpp index 7dc189e133..fc26266555 100644 --- a/ksp_plugin_test/planetarium_test.cpp +++ b/ksp_plugin_test/planetarium_test.cpp @@ -110,9 +110,10 @@ class PlanetariumTest : public ::testing::Test { /*focal=*/5 * Metre), plotting_to_scaled_space_( [](Position const& plotted_point) { + constexpr auto inverse_scale_factor = 1 / (6000 * Metre); return ScaledSpacePoint::FromCoordinates( ((plotted_point - Navigation::origin) * - (1 / (6000 * Metre))).coordinates()); + inverse_scale_factor)).coordinates()); }), // A body of radius 1 m located at the origin. body_(MassiveBody::Parameters(1 * Kilogram), From 2ae9512b3e3b6bc0fbbab53d5efb6e2bbfc8e54f Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Wed, 26 Jan 2022 14:24:52 +0100 Subject: [PATCH 32/33] make it build --- benchmarks/planetarium_plot_methods.cpp | 8 ++++---- ksp_plugin/planetarium.cpp | 2 +- ksp_plugin_test/planetarium_test.cpp | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/benchmarks/planetarium_plot_methods.cpp b/benchmarks/planetarium_plot_methods.cpp index c302960fad..39574d8497 100644 --- a/benchmarks/planetarium_plot_methods.cpp +++ b/benchmarks/planetarium_plot_methods.cpp @@ -170,15 +170,15 @@ class Satellites { /*sphere_radius_multiplier=*/1, /*angular_resolution=*/0.4 * ArcMinute, /*field_of_view=*/90 * Degree); - constexpr auto inverse_scale_factor = (1 / (6000 * Metre); return Planetarium(parameters, perspective, ephemeris_.get(), earth_centred_inertial_.get(), [](Position const& plotted_point) { - return ScaledSpacePoint::FromCoordinates( - ((plotted_point - Navigation::origin) * - inverse_scale_factor).coordinates()); + constexpr auto inverse_scale_factor = 1 / (6000 * Metre); + return ScaledSpacePoint::FromCoordinates( + ((plotted_point - Navigation::origin) * + inverse_scale_factor).coordinates()); }); } diff --git a/ksp_plugin/planetarium.cpp b/ksp_plugin/planetarium.cpp index caa7bb3d4b..44a73b3e45 100644 --- a/ksp_plugin/planetarium.cpp +++ b/ksp_plugin/planetarium.cpp @@ -280,7 +280,7 @@ void Planetarium::PlotMethod3( std::function const& add_point, int max_points) const { if (begin == end) { - return {}; + return; } auto last = std::prev(end); auto const begin_time = std::max(begin->time, plotting_frame_->t_min()); diff --git a/ksp_plugin_test/planetarium_test.cpp b/ksp_plugin_test/planetarium_test.cpp index fc26266555..ef1999a710 100644 --- a/ksp_plugin_test/planetarium_test.cpp +++ b/ksp_plugin_test/planetarium_test.cpp @@ -113,7 +113,7 @@ class PlanetariumTest : public ::testing::Test { constexpr auto inverse_scale_factor = 1 / (6000 * Metre); return ScaledSpacePoint::FromCoordinates( ((plotted_point - Navigation::origin) * - inverse_scale_factor)).coordinates()); + inverse_scale_factor).coordinates()); }), // A body of radius 1 m located at the origin. body_(MassiveBody::Parameters(1 * Kilogram), From 43840adf04b05c7e2264391974c92f7440953749 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Wed, 26 Jan 2022 15:00:38 +0100 Subject: [PATCH 33/33] lint --- ksp_plugin/planetarium.hpp | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/ksp_plugin/planetarium.hpp b/ksp_plugin/planetarium.hpp index ee2187fdaa..57f067f2f7 100644 --- a/ksp_plugin/planetarium.hpp +++ b/ksp_plugin/planetarium.hpp @@ -126,23 +126,25 @@ class Planetarium { // A method similar to PlotMethod2, but which produces a three-dimensional // trajectory in scaled space instead of projecting and hiding. - void PlotMethod3(Trajectory const& trajectory, - DiscreteTrajectory::iterator begin, - DiscreteTrajectory::iterator end, - Instant const& now, - bool reverse, - std::function const& add_point, - int max_points) const; + void PlotMethod3( + Trajectory const& trajectory, + DiscreteTrajectory::iterator begin, + DiscreteTrajectory::iterator end, + Instant const& now, + bool reverse, + std::function const& add_point, + int max_points) const; // The same method, operating on the |Trajectory| interface. - void PlotMethod3(Trajectory const& trajectory, - Instant const& first_time, - Instant const& last_time, - Instant const& now, - bool reverse, - std::function const& add_point, - int max_points, - Length* minimal_distance = nullptr) const; + void PlotMethod3( + Trajectory const& trajectory, + Instant const& first_time, + Instant const& last_time, + Instant const& now, + bool reverse, + std::function const& add_point, + int max_points, + Length* minimal_distance = nullptr) const; private: // Computes the coordinates of the spheres that represent the |ephemeris_|