#include #include #include #include namespace { using namespace aethera; using namespace aethera::render_2d; template std::unique_ptr build_object(Args&&... args) { typename Object::Builder builder(std::forward(args)...); auto result = builder.build(); if (!result) std::terminate(); return std::move(result).value(); } template std::unique_ptr build_scene(Renderables*... renderables) { typename Scene::Builder builder; (builder.add_renderable(renderables), ...); auto result = builder.build(); if (!result) std::terminate(); return std::move(result).value(); } bool contains_color(Image_View view) { for (int y = 0; y < view.height; ++y) { const auto* row = reinterpret_cast(view.data + static_cast(y) * view.stride); for (int x = 0; x < view.width; ++x) if (row[x] != 0) return true; } return false; } template std::unique_ptr render_frame(Scene* scene) { static std::uint64_t sequence{1}; auto frame = std::make_unique(Frame_Identity{sequence++, 0}); EXPECT_EQ(scene->render(frame.get()), Render_Scene_2D::Render_Result::completed); return frame; } } TEST(spectrum_data, publishes_samples_and_interpolates_power) { using Frequency = Impl; using Power = Impl; using Object = Impl; using Scene_Object = Impl; auto frequency = build_object(); auto power = build_object(); auto spectrum = build_object(frequency.get(), power.get()); const auto missing = spectrum->power_at(25.0); ASSERT_FALSE(missing.has_value()); EXPECT_EQ(missing.error(), Spectrum::Power_At_Result::no_samples); spectrum->set<&Spectrum::Prop::frequency_range>(Axis_Range{0.0, 100.0}); const double samples[]{-100.0, -50.0, 0.0}; spectrum->pending_buffer() = Spectrum_Frame{std::vector(std::begin(samples), std::end(samples))}; spectrum->mark_dirty(); spectrum->advance(); EXPECT_EQ(spectrum->current_buffer().samples.size(), 3u); const auto value = spectrum->power_at(25.0); ASSERT_TRUE(value.has_value()); EXPECT_DOUBLE_EQ(*value, -75.0); const auto outside = spectrum->power_at(101.0); ASSERT_FALSE(outside.has_value()); EXPECT_EQ(outside.error(), Spectrum::Power_At_Result::frequency_out_of_range); } TEST(spectrum_markers, supports_selection_frequency_changes_and_known_results) { using Frequency = Impl; using Power = Impl; using Object = Impl; using Scene_Object = Impl; auto frequency = build_object(); auto power = build_object(); auto spectrum = build_object(frequency.get(), power.get()); spectrum->add_custom_marker(10.0); spectrum->add_custom_line_marker(20.0); spectrum->advance(); EXPECT_EQ(spectrum->selectable_line_marker_count(), 2u); spectrum->set_selected_marker_index(1); spectrum->advance(); EXPECT_EQ(spectrum->selected_marker_index(), 1); EXPECT_EQ(spectrum->set_current_marker_frequency(25.0), Spectrum::Set_Current_Marker_Frequency_Result::updated); spectrum->advance(); const auto frequency_value = spectrum->marker_frequency(1); ASSERT_TRUE(frequency_value.has_value()); EXPECT_DOUBLE_EQ(*frequency_value, 25.0); const auto invalid = spectrum->marker_frequency(2); ASSERT_FALSE(invalid.has_value()); EXPECT_EQ(invalid.error(), Spectrum::Marker_Frequency_Result::index_out_of_range); spectrum->remove_selected_marker(); spectrum->advance(); EXPECT_EQ(spectrum->selectable_line_marker_count(), 1u); EXPECT_EQ(spectrum->selected_marker_index(), -1); } TEST(render_scene_2d, composites_axes_and_spectrum_into_final_frame) { using Frequency = Impl; using Power = Impl; using Spectrum_Object = Impl; using Scene_Object = Impl; initialize_runtime(2); auto frequency = build_object(); auto power = build_object(); auto spectrum = build_object(frequency.get(), power.get()); auto scene = build_scene(spectrum.get()); const Size canvas{160, 120}; frequency->set<&Abs_Axis::Prop::position>(Point_F{20.0, 100.0}); frequency->set<&Abs_Axis::Prop::pixel_length>(120.0); frequency->set<&Numeric_Axis::Prop::coordinate_range>(Axis_Range{0.0, 100.0}); power->set<&Abs_Axis::Prop::position>(Point_F{20.0, 100.0}); power->set<&Abs_Axis::Prop::pixel_length>(-80.0); power->set<&Abs_Axis::Prop::orientation>(Axis_Orientation::vertical); power->set<&Numeric_Axis::Prop::coordinate_range>(Axis_Range{-100.0, 0.0}); spectrum->set<&Spectrum::Prop::frequency_range>(Axis_Range{0.0, 100.0}); spectrum->set<&Spectrum::Prop::partition_mode>(Spectrum_Partition_Mode::fixed); spectrum->set<&Spectrum::Prop::partition_count>(3u); spectrum->set<&Spectrum::Prop::sweep_region_visible>(true); spectrum->set<&Spectrum::Prop::max_hold_visible>(true); const double samples[]{-90.0, -65.0, -20.0, -45.0, -75.0}; spectrum->pending_buffer() = Spectrum_Frame{std::vector(std::begin(samples), std::end(samples))}; spectrum->mark_dirty(); scene->set<&Render_Scene_2D::Prop::viewport>(canvas); scene->set<&Render_Scene_2D::Prop::background>(Color::transparent()); scene->activate_view(); const auto prepare_graph = scene->pending_dependency_graph(); EXPECT_TRUE(prepare_graph.depends_on(spectrum.get(), scene.get())); EXPECT_TRUE(prepare_graph.depends_on(spectrum.get(), frequency.get())); EXPECT_TRUE(prepare_graph.depends_on(spectrum.get(), power.get())); const auto paint_graph = scene->pending_dependency_graph(); EXPECT_TRUE(paint_graph.depends_on(frequency.get(), spectrum.get())); EXPECT_TRUE(paint_graph.depends_on(power.get(), spectrum.get())); const auto cache_graph = scene->pending_dependency_graph(); EXPECT_TRUE(cache_graph.depends_on(spectrum.get(), scene.get())); EXPECT_TRUE(cache_graph.depends_on(spectrum.get(), frequency.get())); EXPECT_TRUE(cache_graph.depends_on(spectrum.get(), power.get())); auto output_frame = render_frame(scene.get()); EXPECT_GT(spectrum->read_state().rendered_point_count, 0u); const auto& render_state = spectrum->read_state(); EXPECT_EQ(render_state.prepare_task_count, 4u); EXPECT_EQ(render_state.paint_task_count, 1u); const Image_View frame = output_frame->image(); ASSERT_FALSE(frame.empty()); EXPECT_TRUE(contains_color(frame)); EXPECT_FALSE(spectrum->dirty()); EXPECT_FALSE(spectrum->dirty()); EXPECT_FALSE(frequency->dirty()); EXPECT_FALSE(power->dirty()); output_frame = render_frame(scene.get()); EXPECT_TRUE(contains_color(output_frame->image())); frequency->set<&Numeric_Axis::Prop::precision>(3); EXPECT_FALSE(spectrum->dirty()); output_frame = render_frame(scene.get()); EXPECT_TRUE(spectrum->read_state().paint_executed); EXPECT_TRUE(frequency->read_state().paint_executed); EXPECT_TRUE(power->read_state().paint_executed); EXPECT_TRUE(contains_color(output_frame->image())); frequency->set<&Numeric_Axis::Prop::coordinate_range>(Axis_Range{0.0, 120.0}); EXPECT_TRUE(spectrum->dirty()); output_frame = render_frame(scene.get()); EXPECT_FALSE(spectrum->dirty()); EXPECT_TRUE(spectrum->read_state().prepare_executed); spectrum->set<&Spectrum::Prop::partition_count>(2u); spectrum->pending_buffer() = Spectrum_Frame{std::vector(std::begin(samples), std::end(samples))}; spectrum->mark_dirty(); output_frame = render_frame(scene.get()); const auto& rebuilt_state = spectrum->read_state(); EXPECT_TRUE(rebuilt_state.prepare_graph_rebuilt); EXPECT_EQ(rebuilt_state.prepare_task_count, 3u); EXPECT_TRUE(contains_color(output_frame->image())); const Size resized_canvas{200, 140}; scene->set<&Render_Scene_2D::Prop::viewport>(resized_canvas); EXPECT_TRUE(spectrum->dirty()); EXPECT_FALSE(frequency->dirty()); EXPECT_FALSE(power->dirty()); output_frame = render_frame(scene.get()); EXPECT_EQ(scene->read_prop().viewport, resized_canvas); EXPECT_FALSE(frequency->read_state().prepare_executed); EXPECT_TRUE(frequency->read_state().paint_executed); const Image_View resized_frame = output_frame->image(); EXPECT_EQ(resized_frame.width, resized_canvas.width); EXPECT_EQ(resized_frame.height, resized_canvas.height); EXPECT_TRUE(contains_color(resized_frame)); }