Skip to content

Fix label clipping during rotation by adding ViewScaleFactor and reducing plot area #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions implot3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ void ComputeBoxCorners(ImPlot3DPoint* corners, const ImPlot3DPoint& range_min, c
// Convert a position in the plot's NDC to pixels
ImVec2 NDCToPixels(const ImPlot3DPlot& plot, const ImPlot3DPoint& point) {
ImVec2 center = plot.PlotRect.GetCenter();
ImPlot3DPoint point_pix = plot.GetBoxZoom() * (plot.Rotation * point);
ImPlot3DPoint point_pix = plot.GetViewScale() * (plot.Rotation * point);
point_pix.y *= -1.0f; // Invert y-axis
point_pix.x += center.x;
point_pix.y += center.y;
Expand Down Expand Up @@ -1887,7 +1887,7 @@ ImPlot3DRay PixelsToNDCRay(const ImVec2& pix) {
SetupLock();

// Calculate zoom factor and plot center
float zoom = plot.GetBoxZoom();
float zoom = plot.GetViewScale();
ImVec2 center = plot.PlotRect.GetCenter();

// Undo screen transformations to get back to NDC space
Expand Down Expand Up @@ -2038,7 +2038,7 @@ void HandleInput(ImPlot3DPlot& plot) {
ImPlot3DPoint delta_pixels(delta.x, -delta.y, 0.0f);

// Convert delta to NDC space
float zoom = plot.GetBoxZoom();
float zoom = plot.GetViewScale();
ImPlot3DPoint delta_NDC = plot.Rotation.Inverse() * (delta_pixels / zoom);

// Convert delta to plot space
Expand Down Expand Up @@ -2294,7 +2294,7 @@ void SetupLock() {
for (int i = 0; i < 3; i++) {
ImPlot3DAxis& axis = plot.Axes[i];
if (axis.ShowDefaultTicks) {
float pixels = plot.GetBoxZoom() * plot.BoxScale[i];
float pixels = plot.GetViewScale() * plot.BoxScale[i];
axis.Locator(axis.Ticker, axis.Range, pixels, axis.Formatter, axis.FormatterData);
}
}
Expand Down Expand Up @@ -2363,6 +2363,9 @@ static const ImPlot3DStyleVarInfo GPlot3DStyleVarInfo[] = {
{ImGuiDataType_Float, 2, (ImU32)offsetof(ImPlot3DStyle, LegendPadding)}, // ImPlot3DStyleVar_LegendPadding
{ImGuiDataType_Float, 2, (ImU32)offsetof(ImPlot3DStyle, LegendInnerPadding)}, // ImPlot3DStyleVar_LegendInnerPadding
{ImGuiDataType_Float, 2, (ImU32)offsetof(ImPlot3DStyle, LegendSpacing)}, // ImPlot3DStyleVar_LegendSpacing

// View style
{ImGuiDataType_Float, 1, (ImU32)offsetof(ImPlot3DStyle, ViewScaleFactor)}, // ImPlot3DStyleVar_ViewScaleFactor
};

static const ImPlot3DStyleVarInfo* GetPlotStyleVarInfo(ImPlot3DStyleVar idx) {
Expand Down Expand Up @@ -3391,7 +3394,9 @@ void ImPlot3DPlot::SetRange(const ImPlot3DPoint& min, const ImPlot3DPoint& max)
Axes[2].SetRange(min.z, max.z);
}

float ImPlot3DPlot::GetBoxZoom() const { return ImMin(PlotRect.GetWidth(), PlotRect.GetHeight()) / 1.8f; }
float ImPlot3DPlot::GetViewScale() const {
return ImMin(PlotRect.GetWidth(), PlotRect.GetHeight()) / 2.2f * ImPlot3D::GImPlot3D->Style.ViewScaleFactor;
}

//-----------------------------------------------------------------------------
// [SECTION] ImPlot3DStyle
Expand All @@ -3413,6 +3418,8 @@ ImPlot3DStyle::ImPlot3DStyle() {
LegendPadding = ImVec2(10, 10);
LegendInnerPadding = ImVec2(5, 5);
LegendSpacing = ImVec2(5, 0);
// View style
ViewScaleFactor = 1.0f;
// Colors
ImPlot3D::StyleColorsAuto(this);
Colormap = ImPlot3DColormap_Deep;
Expand Down Expand Up @@ -3689,7 +3696,7 @@ void ImPlot3D::ShowMetricsWindow(bool* p_popen) {
ImGui::BulletText("Animation: Time=%.4f RotationEnd=[%.2f,%.2f,%.2f,%.2f]", plot.AnimationTime, plot.RotationAnimationEnd.x,
plot.RotationAnimationEnd.y, plot.Rotation.z, plot.RotationAnimationEnd.w);
ImGui::BulletText("BoxScale: [%.2f,%.2f,%.2f]", plot.BoxScale.x, plot.BoxScale.y, plot.BoxScale.z);
ImGui::BulletText("BoxZoom: %.2f", plot.GetBoxZoom());
ImGui::BulletText("ViewScale: %.2f", plot.GetViewScale());

ImGui::TreePop();
}
Expand Down
4 changes: 4 additions & 0 deletions implot3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ enum ImPlot3DStyleVar_ {
ImPlot3DStyleVar_LegendPadding, // ImVec2, legend padding from plot edges
ImPlot3DStyleVar_LegendInnerPadding, // ImVec2, legend inner padding from legend edges
ImPlot3DStyleVar_LegendSpacing, // ImVec2, spacing between legend entries
// View style
ImPlot3DStyleVar_ViewScaleFactor, // float, scale factor for 3D view/plot box
ImPlot3DStyleVar_COUNT
};

Expand Down Expand Up @@ -818,6 +820,8 @@ struct ImPlot3DStyle {
ImVec2 LegendPadding; // Legend padding from plot edges
ImVec2 LegendInnerPadding; // Legend inner padding from legend edges
ImVec2 LegendSpacing; // Spacing between legend entries
// View style
float ViewScaleFactor; // Scale factor for 3D view/plot box
// Colors
ImVec4 Colors[ImPlot3DCol_COUNT];
inline ImVec4 GetColor(ImPlot3DCol idx) const { return Colors[idx]; }
Expand Down
2 changes: 1 addition & 1 deletion implot3d_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ struct ImPlot3DPlot {
ImPlot3DPoint RangeMax() const;
ImPlot3DPoint RangeCenter() const;
void SetRange(const ImPlot3DPoint& min, const ImPlot3DPoint& max);
float GetBoxZoom() const;
float GetViewScale() const;
};

struct ImPlot3DContext {
Expand Down