Skip to content

Commit fd9f163

Browse files
committed
Add configurable water surface grid size
Enables users to define the water surface visualization grid dimensions and center the grids position (via the SetWaterGridExtent() API). Handy for adjusting the visible water area to match the simulation domain. Default to 100m x 100m centered at origin if not specified.
1 parent 61421da commit fd9f163

8 files changed

Lines changed: 166 additions & 77 deletions

File tree

include/hydroc/gui/guihelper.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ class UI {
4848
*/
4949
virtual void SetWaveModel(std::shared_ptr<WaveBase> wave);
5050

51+
/**@brief Set the water grid extent for visualization.
52+
*
53+
* Configures the dimensions and position of the water surface mesh.
54+
* Call after Init() and before SetWaveModel().
55+
*
56+
* @param width Grid extent in X direction [m] (default: 100m)
57+
* @param length Grid extent in Y direction [m] (default: 100m)
58+
* @param center_x Grid center X coordinate [m] (default: 0)
59+
* @param center_y Grid center Y coordinate [m] (default: 0)
60+
*/
61+
virtual void SetWaterGridExtent(double width, double length,
62+
double center_x = 0.0, double center_y = 0.0);
63+
5164
/**@brief return the internal system.
5265
*
5366
* Should be called after init.
@@ -75,6 +88,8 @@ class GUI : public UI {
7588
void SetCamera(double x, double y, double z, double dirx, double diry, double dirz) override;
7689
bool IsRunning(double timestep) override;
7790
void SetWaveModel(std::shared_ptr<WaveBase> wave) override;
91+
void SetWaterGridExtent(double width, double length,
92+
double center_x = 0.0, double center_y = 0.0) override;
7893

7994
private:
8095
std::shared_ptr<hydroc::gui::GUIImpl> pImpl;

src/gui/guihelper.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ bool UI::IsRunning(double timestep) {
2929
return true;
3030
}
3131

32-
void UI::SetWaveModel(std::shared_ptr<WaveBase> wave) {
32+
void UI::SetWaveModel(std::shared_ptr<WaveBase> /*wave*/) {
3333
// Default (headless) UI does not render waves.
34-
(void)wave;
34+
}
35+
36+
void UI::SetWaterGridExtent(double /*width*/, double /*length*/,
37+
double /*center_x*/, double /*center_y*/) {
38+
// Default (headless) UI does not render water surface.
3539
}
3640

3741
// -----------------------------------------------------------------------------
@@ -64,3 +68,7 @@ bool GUI::IsRunning(double timestep) {
6468
void GUI::SetWaveModel(std::shared_ptr<WaveBase> wave) {
6569
pImpl->SetWaveModel(wave);
6670
}
71+
72+
void GUI::SetWaterGridExtent(double width, double length, double center_x, double center_y) {
73+
pImpl->SetWaterGridExtent(width, length, center_x, center_y);
74+
}

src/gui/guihelperVSG.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,21 @@ void GUIImplVSG::SetWaveModel(std::shared_ptr<WaveBase> wave) {
114114
// Water surface will be created/updated on first render via EnsureWaterSurface().
115115
}
116116

117+
void GUIImplVSG::SetWaterGridExtent(double width, double length, double center_x, double center_y) {
118+
if (!viewer_settings_) {
119+
return;
120+
}
121+
122+
viewer_settings_->grid_width = width;
123+
viewer_settings_->grid_length = length;
124+
viewer_settings_->grid_center_x = center_x;
125+
viewer_settings_->grid_center_y = center_y;
126+
viewer_settings_->grid_extent_changed = true;
127+
128+
std::cout << "[WaterSurface] Grid extent: " << width << " x " << length << " m"
129+
<< ", center: (" << center_x << ", " << center_y << ")" << std::endl;
130+
}
131+
117132
void GUIImplVSG::EnsureWaterSurface() {
118133
// Require both system and visual system to be valid.
119134
if (!system_ || !pVis) {
@@ -141,7 +156,7 @@ void GUIImplVSG::EnsureWaterSurface() {
141156
// Always create animated water surface (supports waves + radiation viz).
142157
// Static plane fallback removed - animated water handles all cases.
143158
int resolution = (viewer_settings_) ? viewer_settings_->grid_resolution : 0;
144-
animated_water_->Initialize(pVis.get(), resolution);
159+
animated_water_->Initialize(pVis.get(), resolution, viewer_settings_.get());
145160

146161
if (animated_water_->IsInitialized()) {
147162
std::cout << "[WaterSurface] wave_ptr=" << (wave_model_ ? "ok" : "null");
@@ -173,10 +188,12 @@ bool GUIImplVSG::IsRunning(double timestep) {
173188
// Handle visibility toggle.
174189
animated_water_->SetVisible(viewer_settings_->show_water);
175190

176-
// Handle resolution change (requires mesh rebuild).
177-
if (viewer_settings_->resolution_changed && animated_water_->IsInitialized()) {
178-
animated_water_->Reinitialize(viewer_settings_->grid_resolution);
191+
// Handle resolution or extent change (requires mesh rebuild).
192+
if ((viewer_settings_->resolution_changed || viewer_settings_->grid_extent_changed) &&
193+
animated_water_->IsInitialized()) {
194+
animated_water_->Reinitialize(viewer_settings_->grid_resolution, viewer_settings_.get());
179195
viewer_settings_->resolution_changed = false;
196+
viewer_settings_->grid_extent_changed = false;
180197
}
181198
}
182199

src/gui/guihelper_impl.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ class GUIImpl {
4242
hydroc::cli::LogWarning(
4343
"Warning: GUI deactivated. HydroChrono was built without run-time visualization support.");
4444
}
45-
virtual void SetCamera(double x, double y, double z, double dirx, double diry, double dirz) {}
46-
virtual bool IsRunning(double timestep) { return true; }
47-
virtual void SetWaveModel(std::shared_ptr<WaveBase> wave) { (void)wave; }
45+
virtual void SetCamera(double /*x*/, double /*y*/, double /*z*/,
46+
double /*dirx*/, double /*diry*/, double /*dirz*/) {}
47+
virtual bool IsRunning(double /*timestep*/) { return true; }
48+
virtual void SetWaveModel(std::shared_ptr<WaveBase> /*wave*/) {}
49+
virtual void SetWaterGridExtent(double /*width*/, double /*length*/,
50+
double /*center_x*/, double /*center_y*/) {}
4851
};
4952

5053
#ifdef HYDROCHRONO_HAVE_IRRLICHT
@@ -82,6 +85,7 @@ class GUIImplVSG : public GUIImpl {
8285
virtual void SetCamera(double x, double y, double z, double dirx, double diry, double dirz) override;
8386
virtual bool IsRunning(double timestep) override;
8487
virtual void SetWaveModel(std::shared_ptr<WaveBase> wave) override;
88+
virtual void SetWaterGridExtent(double width, double length, double center_x, double center_y) override;
8589

8690
private:
8791
/// Ensure water surface is created (animated if wave model set, static otherwise).

src/gui/vsg_gui_component.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ HydroChronoGuiComponent::HydroChronoGuiComponent(chrono::vsg3d::ChVisualSystemVS
1212
ViewerSettings* settings)
1313
: vsys_(vsys), pressed_(button_pressed), settings_(settings) {}
1414

15-
void HydroChronoGuiComponent::render(vsg::CommandBuffer& cb) {
15+
void HydroChronoGuiComponent::render(vsg::CommandBuffer& /*cb*/) {
1616
// Play/Pause button (top center, no background).
1717
{
1818
ImGuiWindowFlags window_flags = 0;

src/gui/vsg_gui_component.h

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,41 @@
1212
namespace hydroc {
1313
namespace gui {
1414

15-
// =============================================================================
16-
// ViewerSettings: Runtime-adjustable water visualization settings
17-
// =============================================================================
18-
// Allows tuning water appearance without recompiling.
19-
// Owned by GUIImplVSG, passed by pointer to GUI component and water surface.
15+
/// Runtime-adjustable water visualization settings.
16+
///
17+
/// Allows tuning water appearance without recompiling. Owned by GUIImplVSG
18+
/// and passed by pointer to GUI components and the water surface renderer.
2019
struct ViewerSettings {
20+
// --- Display Options ---
2121
bool show_water = true; ///< Toggle water surface visibility
22-
float wave_visual_scale = 1.0f; ///< Multiplier for wave elevation display
23-
int grid_resolution = 64; ///< Vertices per side (32/64/96/128)
24-
int update_hz = 30; ///< Max update rate in Hz (10-60)
22+
bool show_water_grid = false; ///< Show wireframe overlay on water surface
2523
bool show_water_status = false; ///< Show status line in overlay
2624

27-
// Allowed discrete values for grid resolution.
25+
// --- Wave Rendering ---
26+
float wave_visual_scale = 1.0f; ///< Multiplier for wave elevation display
27+
int update_hz = 30; ///< Maximum update rate [Hz], range 10-60
28+
29+
// --- Grid Resolution ---
30+
int grid_resolution = 64; ///< Vertices per side (32, 64, 96, or 128)
31+
bool resolution_changed = false; ///< Triggers mesh rebuild when resolution changes
32+
2833
static constexpr int kResolutionOptions[] = {32, 64, 96, 128};
2934
static constexpr int kResolutionCount = 4;
3035

31-
// Track if resolution changed (requires mesh rebuild).
32-
bool resolution_changed = false;
36+
// --- Grid Extent ---
37+
// Values <= 0 use defaults (100m x 100m centered at origin).
38+
double grid_width = 0.0; ///< Grid extent in X direction [m]
39+
double grid_length = 0.0; ///< Grid extent in Y direction [m]
40+
double grid_center_x = 0.0; ///< Grid center X coordinate [m]
41+
double grid_center_y = 0.0; ///< Grid center Y coordinate [m]
42+
bool grid_extent_changed = false; ///< Triggers mesh rebuild when extent changes
3343

34-
// =========================================================================
35-
// Radiation Visualization (Tier 0 - Approximate, Visual Only)
36-
// =========================================================================
37-
// Simple visualization of radiated waves from moving bodies.
38-
// NOT physically accurate - purely for visual feedback.
39-
//
40-
// Most parameters are auto-derived from physics:
41-
// - Wavelength derived from wave speed using deep water dispersion
42-
// - Amplitude scales with body velocity and size
43-
// - Only "Visual Scale" is user-adjustable (for inspection)
44+
// --- Radiation Visualization (Approximate) ---
45+
// Visual-only representation of radiated waves from moving bodies.
46+
// NOT physically accurate; wavelength derived from dispersion relation,
47+
// amplitude scales with body velocity and size.
4448
bool show_radiation_viz = false; ///< Enable radiated wave visualization
45-
float radiation_visual_scale = 1.0f; ///< Visual amplification (range 0.1-5x, for inspection only)
46-
47-
// Water surface grid overlay for better visibility.
48-
bool show_water_grid = false; ///< Show wireframe grid on water surface
49+
float radiation_visual_scale = 1.0f; ///< Visual amplification [0.1x - 5x]
4950
};
5051

5152
/// ImGui component for HydroChrono visualization overlay.

0 commit comments

Comments
 (0)