1
0
Fork 0
mirror of https://github.com/gwm17/implot.git synced 2024-10-09 15:47:26 -04:00
implot/implot_internal.h

824 lines
30 KiB
C
Raw Normal View History

2020-08-16 16:38:51 -04:00
// MIT License
// Copyright (c) 2020 Evan Pezent
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
2020-09-02 16:55:35 -04:00
// ImPlot v0.7 WIP
2020-08-16 16:38:51 -04:00
// You may use this file to debug, understand or extend ImPlot features but we
// don't provide any guarantee of forward compatibility!
2020-08-16 16:38:51 -04:00
//-----------------------------------------------------------------------------
// [SECTION] Header Mess
2020-08-16 16:38:51 -04:00
//-----------------------------------------------------------------------------
#pragma once
#ifndef IMGUI_DEFINE_MATH_OPERATORS
#define IMGUI_DEFINE_MATH_OPERATORS
#endif
2020-09-03 00:30:32 -04:00
#include <time.h>
2020-08-16 16:38:51 -04:00
#include "imgui_internal.h"
#ifndef IMPLOT_VERSION
#error Must include implot.h before implot_internal.h
#endif
//-----------------------------------------------------------------------------
// [SECTION] Forward Declarations
2020-08-16 16:38:51 -04:00
//-----------------------------------------------------------------------------
struct ImPlotTick;
struct ImPlotAxis;
struct ImPlotAxisState;
struct ImPlotAxisColor;
struct ImPlotItem;
struct ImPlotState;
struct ImPlotNextPlotData;
//-----------------------------------------------------------------------------
// [SECTION] Context Pointer
2020-08-16 16:38:51 -04:00
//-----------------------------------------------------------------------------
extern ImPlotContext* GImPlot; // Current implicit context pointer
//-----------------------------------------------------------------------------
// [SECTION] Macros and Constants
2020-08-16 16:38:51 -04:00
//-----------------------------------------------------------------------------
// Constants can be changed unless stated otherwise. We may move some of these
// to ImPlotStyleVar_ over time.
// Default plot frame width when requested width is auto (i.e. 0). This is not the plot area width!
#define IMPLOT_DEFAULT_W 400
// Default plot frame height when requested height is auto (i.e. 0). This is not the plot area height!
#define IMPLOT_DEFAULT_H 300
2020-08-16 16:38:51 -04:00
// The maximum number of supported y-axes (DO NOT CHANGE THIS)
#define IMPLOT_Y_AXES 3
// The number of times to subdivided grid divisions (best if a multiple of 1, 2, and 5)
#define IMPLOT_SUB_DIV 10
// Zoom rate for scroll (e.g. 0.1f = 10% plot range every scroll click)
#define IMPLOT_ZOOM_RATE 0.1f
2020-08-16 16:38:51 -04:00
//-----------------------------------------------------------------------------
// [SECTION] Generic Helpers
2020-08-16 16:38:51 -04:00
//-----------------------------------------------------------------------------
// Computes the common (base-10) logarithm
static inline float ImLog10(float x) { return log10f(x); }
static inline double ImLog10(double x) { return log10(x); }
// Returns true if a flag is set
template <typename TSet, typename TFlag>
inline bool ImHasFlag(TSet set, TFlag flag) { return (set & flag) == flag; }
// Flips a flag in a flagset
template <typename TSet, typename TFlag>
inline void ImFlipFlag(TSet& set, TFlag flag) { ImHasFlag(set, flag) ? set &= ~flag : set |= flag; }
// Linearly remaps x from [x0 x1] to [y0 y1].
template <typename T>
inline T ImRemap(T x, T x0, T x1, T y0, T y1) { return y0 + (x - x0) * (y1 - y0) / (x1 - x0); }
// Returns always positive modulo (assumes r != 0)
2020-08-16 16:38:51 -04:00
inline int ImPosMod(int l, int r) { return (l % r + r) % r; }
// Offset calculator helper
template <int Count>
struct ImOffsetCalculator {
ImOffsetCalculator(const int* sizes) {
2020-08-16 16:38:51 -04:00
Offsets[0] = 0;
for (int i = 1; i < Count; ++i)
Offsets[i] = Offsets[i-1] + sizes[i-1];
}
int Offsets[Count];
};
// Character buffer writer helper
struct ImBufferWriter
{
char* Buffer;
size_t Size;
size_t Pos;
ImBufferWriter(char* buffer, size_t size) {
Buffer = buffer;
Size = size;
Pos = 0;
}
2020-08-16 16:38:51 -04:00
void Write(const char* fmt, ...) IM_FMTARGS(2) {
va_list argp;
va_start(argp, fmt);
const int written = ::vsnprintf(&Buffer[Pos], Size - Pos - 1, fmt, argp);
if (written > 0)
Pos += ImMin(size_t(written), Size-Pos-1);
va_end(argp);
}
};
2020-09-02 10:17:18 -04:00
// Fixed size point array
template <int N>
struct ImPlotPointArray {
inline ImPlotPoint& operator[](int i) { return Data[i]; }
inline const ImPlotPoint& operator[](int i) const { return Data[i]; }
ImPlotPoint Data[N];
2020-08-17 17:26:45 -04:00
const int Size = N;
};
//-----------------------------------------------------------------------------
// [SECTION] ImPlot Enums
//-----------------------------------------------------------------------------
2020-09-03 00:30:32 -04:00
typedef int ImPlotScale; // -> enum ImPlotScale_
typedef int ImPlotTimeUnit; // -> enum ImPlotTimeUnit_
2020-09-03 09:00:36 -04:00
typedef int ImPlotTimeUnit; // -> enum ImPlotTimeUnit_
// XY axes scaling combinations
enum ImPlotScale_ {
ImPlotScale_LinLin, // linear x, linear y
ImPlotScale_LogLin, // log x, linear y
ImPlotScale_LinLog, // linear x, log y
ImPlotScale_LogLog // log x, log y
};
2020-09-03 00:30:32 -04:00
enum ImPlotTimeUnit_ {
2020-09-03 09:00:36 -04:00
ImPlotTimeUnit_Us, // microsecond (:29.428 552)
ImPlotTimeUnit_Ms, // millisecond (:29.428)
ImPlotTimeUnit_S, // second (:29)
ImPlotTimeUnit_Min, // minute (7:21pm)
ImPlotTimeUnit_Hr, // hour (7pm)
ImPlotTimeUnit_Day, // day (10/3)
ImPlotTimeUnit_Mo, // month (Oct)
ImPlotTimeUnit_Yr, // year (1991)
2020-09-03 00:30:32 -04:00
ImPlotTimeUnit_COUNT
};
2020-08-16 16:38:51 -04:00
//-----------------------------------------------------------------------------
// [SECTION] ImPlot Structs
//-----------------------------------------------------------------------------
// Storage for colormap modifiers
struct ImPlotColormapMod {
ImPlotColormapMod(const ImVec4* colormap, int colormap_size) {
Colormap = colormap;
ColormapSize = colormap_size;
}
const ImVec4* Colormap;
int ColormapSize;
};
2020-08-17 17:26:45 -04:00
// ImPlotPoint with positive/negative error values
struct ImPlotPointError
2020-08-17 17:26:45 -04:00
{
double X, Y, Neg, Pos;
ImPlotPointError(double x, double y, double neg, double pos) {
X = x; Y = y; Neg = neg; Pos = pos;
}
};
2020-08-16 16:38:51 -04:00
// Tick mark info
struct ImPlotTick
{
double PlotPos;
float PixelPos;
ImVec2 LabelSize;
int BufferOffset;
2020-08-16 16:38:51 -04:00
bool Major;
bool ShowLabel;
2020-08-16 16:38:51 -04:00
ImPlotTick(double value, bool major, bool show_label) {
2020-08-25 22:59:43 -04:00
PlotPos = value;
Major = major;
ShowLabel = show_label;
BufferOffset = -1;
2020-08-16 16:38:51 -04:00
}
};
2020-08-25 22:59:43 -04:00
// Collection of ticks
struct ImPlotTickCollection {
ImVector<ImPlotTick> Ticks;
ImGuiTextBuffer Labels;
float TotalWidth;
float TotalHeight;
float MaxWidth;
float MaxHeight;
int Size;
void AddTick(const ImPlotTick& tick) {
if (tick.ShowLabel) {
TotalWidth += tick.ShowLabel ? tick.LabelSize.x : 0;
TotalHeight += tick.ShowLabel ? tick.LabelSize.y : 0;
MaxWidth = tick.LabelSize.x > MaxWidth ? tick.LabelSize.x : MaxWidth;
MaxHeight = tick.LabelSize.y > MaxHeight ? tick.LabelSize.y : MaxHeight;
}
Ticks.push_back(tick);
Size++;
}
2020-09-01 00:23:48 -04:00
2020-08-25 22:59:43 -04:00
void AddTick(double value, bool major, bool show_label, void (*labeler)(ImPlotTick& tick, ImGuiTextBuffer& buf)) {
ImPlotTick tick(value, major, show_label);
if (labeler)
labeler(tick, Labels);
AddTick(tick);
}
const char* GetLabel(int idx) {
return Labels.Buf.Data + Ticks[idx].BufferOffset;
}
2020-09-01 00:23:48 -04:00
void Reset() {
Ticks.shrink(0);
Labels.Buf.shrink(0);
2020-08-25 22:59:43 -04:00
TotalWidth = TotalHeight = MaxWidth = MaxHeight = 0;
Size = 0;
}
};
2020-08-16 16:38:51 -04:00
// Axis state information that must persist after EndPlot
struct ImPlotAxis
{
ImPlotAxisFlags Flags;
ImPlotAxisFlags PreviousFlags;
ImPlotRange Range;
2020-08-16 16:38:51 -04:00
bool Dragging;
2020-08-19 12:34:52 -04:00
bool HoveredExt;
bool HoveredTot;
2020-08-16 16:38:51 -04:00
ImPlotAxis() {
2020-08-19 12:34:52 -04:00
Flags = PreviousFlags = ImPlotAxisFlags_Default;
Range.Min = 0;
Range.Max = 1;
Dragging = false;
HoveredExt = false;
HoveredTot = false;
2020-08-16 16:38:51 -04:00
}
};
// Axis state information only needed between BeginPlot/EndPlot
struct ImPlotAxisState
{
ImPlotAxis* Axis;
ImGuiCond RangeCond;
bool HasRange;
bool Present;
2020-08-19 12:34:52 -04:00
bool HasLabels;
2020-08-16 16:38:51 -04:00
bool Invert;
bool LockMin;
bool LockMax;
bool Lock;
2020-09-03 00:30:32 -04:00
bool IsTime;
2020-08-16 16:38:51 -04:00
2020-08-19 12:34:52 -04:00
ImPlotAxisState(ImPlotAxis* axis, bool has_range, ImGuiCond range_cond, bool present) {
Axis = axis;
HasRange = has_range;
RangeCond = range_cond;
Present = present;
2020-08-19 12:34:52 -04:00
HasLabels = ImHasFlag(Axis->Flags, ImPlotAxisFlags_TickLabels);
Invert = ImHasFlag(Axis->Flags, ImPlotAxisFlags_Invert);
LockMin = ImHasFlag(Axis->Flags, ImPlotAxisFlags_LockMin) || (HasRange && RangeCond == ImGuiCond_Always);
LockMax = ImHasFlag(Axis->Flags, ImPlotAxisFlags_LockMax) || (HasRange && RangeCond == ImGuiCond_Always);
Lock = !Present || ((LockMin && LockMax) || (HasRange && RangeCond == ImGuiCond_Always));
2020-09-03 00:30:32 -04:00
IsTime = ImHasFlag(Axis->Flags, ImPlotAxisFlags_Time);
}
ImPlotAxisState() { }
2020-08-16 16:38:51 -04:00
};
struct ImPlotAxisColor
{
ImU32 Major, Minor, MajTxt, MinTxt;
ImPlotAxisColor() { Major = Minor = MajTxt = MinTxt = 0; }
2020-08-16 16:38:51 -04:00
};
// State information for Plot items
struct ImPlotItem
{
ImGuiID ID;
ImVec4 Color;
int NameOffset;
bool Show;
2020-09-01 00:58:15 -04:00
bool LegendHovered;
bool SeenThisFrame;
2020-08-16 16:38:51 -04:00
ImPlotItem() {
ID = 0;
Color = ImPlot::NextColormapColor();
NameOffset = -1;
2020-08-16 16:38:51 -04:00
Show = true;
SeenThisFrame = false;
2020-09-01 00:58:15 -04:00
LegendHovered = false;
2020-08-16 16:38:51 -04:00
}
~ImPlotItem() { ID = 0; }
};
// Holds Plot state information that must persist after EndPlot
struct ImPlotState
{
ImPlotFlags Flags;
ImPlotFlags PreviousFlags;
ImPlotAxis XAxis;
ImPlotAxis YAxis[IMPLOT_Y_AXES];
2020-08-16 16:38:51 -04:00
ImPool<ImPlotItem> Items;
ImVec2 SelectStart;
ImVec2 QueryStart;
ImRect QueryRect;
ImRect BB_Legend;
2020-08-16 16:38:51 -04:00
bool Selecting;
bool Querying;
bool Queried;
bool DraggingQuery;
int ColormapIdx;
2020-08-16 16:38:51 -04:00
int CurrentYAxis;
ImPlotState() {
Flags = PreviousFlags = ImPlotFlags_Default;
SelectStart = QueryStart = ImVec2(0,0);
Selecting = Querying = Queried = DraggingQuery = false;
ColormapIdx = CurrentYAxis = 0;
2020-08-16 16:38:51 -04:00
}
};
// Temporary data storage for upcoming plot
struct ImPlotNextPlotData
{
ImGuiCond XRangeCond;
ImGuiCond YRangeCond[IMPLOT_Y_AXES];
2020-08-16 16:38:51 -04:00
ImPlotRange X;
ImPlotRange Y[IMPLOT_Y_AXES];
2020-08-16 16:38:51 -04:00
bool HasXRange;
bool HasYRange[IMPLOT_Y_AXES];
2020-08-16 16:38:51 -04:00
bool ShowDefaultTicksX;
bool ShowDefaultTicksY[IMPLOT_Y_AXES];
2020-08-20 00:50:12 -04:00
bool FitX;
bool FitY[IMPLOT_Y_AXES];
2020-08-16 16:38:51 -04:00
ImPlotNextPlotData() {
HasXRange = false;
ShowDefaultTicksX = true;
2020-08-20 00:50:12 -04:00
FitX = false;
for (int i = 0; i < IMPLOT_Y_AXES; ++i) {
2020-08-16 16:38:51 -04:00
HasYRange[i] = false;
ShowDefaultTicksY[i] = true;
2020-08-20 00:50:12 -04:00
FitY[i] = false;
2020-08-16 16:38:51 -04:00
}
}
};
// Temporary data storage for upcoming item
struct ImPlotItemStyle {
2020-08-30 18:12:36 -04:00
ImVec4 Colors[5]; // ImPlotCol_Line, ImPlotCol_Fill, ImPlotCol_MarkerOutline, ImPlotCol_MarkerFill, ImPlotCol_ErrorBar
2020-09-01 00:23:48 -04:00
float LineWeight;
ImPlotMarker Marker;
float MarkerSize;
float MarkerWeight;
float FillAlpha;
float ErrorBarSize;
float ErrorBarWeight;
float DigitalBitHeight;
float DigitalBitGap;
2020-08-30 18:12:36 -04:00
bool RenderLine;
bool RenderFill;
bool RenderMarkerLine;
bool RenderMarkerFill;
ImPlotItemStyle() {
for (int i = 0; i < 5; ++i)
2020-08-30 18:12:36 -04:00
Colors[i] = IMPLOT_AUTO_COL;
2020-09-02 10:17:18 -04:00
LineWeight = MarkerSize = MarkerWeight = FillAlpha = ErrorBarSize =
2020-08-30 18:12:36 -04:00
ErrorBarSize = ErrorBarWeight = DigitalBitHeight = DigitalBitGap = IMPLOT_AUTO;
Marker = IMPLOT_AUTO;
2020-09-01 00:23:48 -04:00
}
};
2020-08-16 16:38:51 -04:00
// Holds state information that must persist between calls to BeginPlot()/EndPlot()
struct ImPlotContext {
// Plot States
ImPool<ImPlotState> Plots;
ImPlotState* CurrentPlot;
ImPlotItem* CurrentItem;
2020-08-16 16:38:51 -04:00
// Legend
ImVector<int> LegendIndices;
ImGuiTextBuffer LegendLabels;
// Bounding Boxes
ImRect BB_Frame;
ImRect BB_Canvas;
ImRect BB_Plot;
// Axis States
ImPlotAxisColor Col_X;
ImPlotAxisColor Col_Y[IMPLOT_Y_AXES];
2020-08-16 16:38:51 -04:00
ImPlotAxisState X;
ImPlotAxisState Y[IMPLOT_Y_AXES];
2020-08-16 16:38:51 -04:00
// Tick Marks and Labels
2020-08-25 22:59:43 -04:00
ImPlotTickCollection XTicks;
ImPlotTickCollection YTicks[IMPLOT_Y_AXES];
2020-08-19 12:34:52 -04:00
float YAxisReference[IMPLOT_Y_AXES];
2020-08-16 16:38:51 -04:00
// Transformations and Data Extents
ImPlotScale Scales[IMPLOT_Y_AXES];
ImRect PixelRange[IMPLOT_Y_AXES];
2020-08-16 16:38:51 -04:00
double Mx;
double My[IMPLOT_Y_AXES];
2020-08-16 16:38:51 -04:00
double LogDenX;
double LogDenY[IMPLOT_Y_AXES];
2020-08-16 16:38:51 -04:00
ImPlotRange ExtentsX;
ImPlotRange ExtentsY[IMPLOT_Y_AXES];
2020-08-16 16:38:51 -04:00
// Data Fitting Flags
bool FitThisFrame;
bool FitX;
bool FitY[IMPLOT_Y_AXES];
2020-08-16 16:38:51 -04:00
// Hover states
bool Hov_Frame;
bool Hov_Plot;
// Axis Rendering Flags
bool RenderX;
bool RenderY[IMPLOT_Y_AXES];
2020-08-16 16:38:51 -04:00
// Axis Locking Flags
bool LockPlot;
bool ChildWindowMade;
// Style and Colormaps
ImPlotStyle Style;
ImVector<ImGuiColorMod> ColorModifiers;
ImVector<ImGuiStyleMod> StyleModifiers;
const ImVec4* Colormap;
int ColormapSize;
ImVector<ImPlotColormapMod> ColormapModifiers;
2020-08-16 16:38:51 -04:00
2020-09-03 00:30:32 -04:00
// Time
tm Tm;
2020-08-16 16:38:51 -04:00
// Misc
int VisibleItemCount;
int DigitalPlotItemCnt;
int DigitalPlotOffset;
ImPlotNextPlotData NextPlotData;
ImPlotItemStyle NextItemStyle;
2020-08-16 16:38:51 -04:00
ImPlotInputMap InputMap;
ImPlotPoint MousePos[IMPLOT_Y_AXES];
2020-08-16 16:38:51 -04:00
};
struct ImPlotAxisScale
{
ImPlotPoint Min, Max;
2020-08-16 16:38:51 -04:00
ImPlotAxisScale(int y_axis, float tx, float ty, float zoom_rate) {
ImPlotContext& gp = *GImPlot;
Min = ImPlot::PixelsToPlot(gp.BB_Plot.Min - gp.BB_Plot.GetSize() * ImVec2(tx * zoom_rate, ty * zoom_rate), y_axis);
Max = ImPlot::PixelsToPlot(gp.BB_Plot.Max + gp.BB_Plot.GetSize() * ImVec2((1 - tx) * zoom_rate, (1 - ty) * zoom_rate), y_axis);
}
};
//-----------------------------------------------------------------------------
// [SECTION] Internal API
// No guarantee of forward compatibility here!
//-----------------------------------------------------------------------------
namespace ImPlot {
2020-09-01 22:01:00 -04:00
//-----------------------------------------------------------------------------
// [SECTION] Context Utils
//-----------------------------------------------------------------------------
2020-08-16 16:38:51 -04:00
// Initializes an ImPlotContext
void Initialize(ImPlotContext* ctx);
// Resets an ImPlot context for the next call to BeginPlot
void Reset(ImPlotContext* ctx);
2020-09-01 22:01:00 -04:00
//-----------------------------------------------------------------------------
// [SECTION] Plot Utils
//-----------------------------------------------------------------------------
2020-08-16 16:38:51 -04:00
// Gets a plot from the current ImPlotContext
ImPlotState* GetPlot(const char* title);
// Gets the current plot from the current ImPlotContext
ImPlotState* GetCurrentPlot();
2020-08-24 00:45:42 -04:00
// Busts the cache for every plot in the current context
void BustPlotCache();
2020-09-01 22:01:00 -04:00
//-----------------------------------------------------------------------------
// [SECTION] Item Utils
//-----------------------------------------------------------------------------
// Begins a new item. Returns false if the item should not be plotted. Pushes PlotClipRect.
2020-08-30 18:12:36 -04:00
bool BeginItem(const char* label_id, ImPlotCol recolor_from = -1);
// Ends an item (call only if BeginItem returns true). Pops PlotClipRect.
void EndItem();
2020-08-16 16:38:51 -04:00
// Register or get an existing item from the current plot
2020-08-17 21:20:15 -04:00
ImPlotItem* RegisterOrGetItem(const char* label_id);
2020-08-16 16:38:51 -04:00
// Get the ith plot item from the current plot
ImPlotItem* GetItem(int i);
// Get a plot item from the current plot
ImPlotItem* GetItem(const char* label_id);
// Gets a plot item from a specific plot
ImPlotItem* GetItem(const char* plot_title, const char* item_label_id);
// Gets the current item
ImPlotItem* GetCurrentItem();
2020-08-24 00:45:42 -04:00
// Busts the cache for every item for every plot in the current context.
void BustItemCache();
2020-09-01 22:01:00 -04:00
//-----------------------------------------------------------------------------
// [SECTION] Axis Utils
//-----------------------------------------------------------------------------
2020-09-01 22:01:00 -04:00
// Gets the current y-axis for the current plot
inline int GetCurrentYAxis() { return GImPlot->CurrentPlot->CurrentYAxis; }
// Constrains an axis range
void ConstrainAxis(ImPlotAxis& axis);
// Updates axis ticks, lins, and label colors
void UpdateAxisColors(int axis_flag, ImPlotAxisColor* col);
// Updates plot-to-pixel space transformation variables for the current plot.
void UpdateTransformCache();
// Gets the XY scale for the current plot and y-axis
inline ImPlotScale GetCurrentScale() { return GImPlot->Scales[GetCurrentYAxis()]; }
// Returns true if the user has requested data to be fit.
inline bool FitThisFrame() { return GImPlot->FitThisFrame; }
// Extends the current plots axes so that it encompasses point p
void FitPoint(const ImPlotPoint& p);
//-----------------------------------------------------------------------------
// [SECTION] Legend Utils
//-----------------------------------------------------------------------------
2020-08-16 16:38:51 -04:00
// Returns the number of entries in the current legend
int GetLegendCount();
// Gets the ith entry string for the current legend
const char* GetLegendLabel(int i);
2020-09-01 22:01:00 -04:00
//-----------------------------------------------------------------------------
// [SECTION] Tick Utils
//-----------------------------------------------------------------------------
2020-09-03 00:30:32 -04:00
// Label a tick with default formatting.
2020-08-25 22:59:43 -04:00
void LabelTickDefault(ImPlotTick& tick, ImGuiTextBuffer& buffer);
2020-09-03 00:30:32 -04:00
// Label a tick with scientific formating.
2020-08-25 22:59:43 -04:00
void LabelTickScientific(ImPlotTick& tick, ImGuiTextBuffer& buffer);
2020-09-03 00:30:32 -04:00
// Label a tick with time formatting.
2020-09-03 09:00:36 -04:00
void LabelTickTime(ImPlotTick& tick, ImGuiTextBuffer& buffer, ImPlotTimeUnit fmt);
2020-09-01 22:01:00 -04:00
// Populates a list of ImPlotTicks with normal spaced and formatted ticks
2020-08-25 22:59:43 -04:00
void AddTicksDefault(const ImPlotRange& range, int nMajor, int nMinor, ImPlotTickCollection& ticks);
// Populates a list of ImPlotTicks with logarithmic space and formatted ticks
2020-08-25 22:59:43 -04:00
void AddTicksLogarithmic(const ImPlotRange& range, int nMajor, ImPlotTickCollection& ticks);
2020-09-03 00:30:32 -04:00
// Populates a list of ImPlotTicks with time formatted ticks.
void AddTicksTime(const ImPlotRange& range, int nMajor, ImPlotTickCollection& ticks);
2020-08-16 16:38:51 -04:00
// Populates a list of ImPlotTicks with custom spaced and labeled ticks
2020-08-25 22:59:43 -04:00
void AddTicksCustom(const double* values, const char** labels, int n, ImPlotTickCollection& ticks);
2020-09-01 22:01:00 -04:00
//-----------------------------------------------------------------------------
// [SECTION] Styling Utils
//-----------------------------------------------------------------------------
// Get styling data for next item (call between Begin/EndItem)
inline const ImPlotItemStyle& GetItemStyle() { return GImPlot->NextItemStyle; }
// Returns true if a color is set to be automatically determined
inline bool IsColorAuto(const ImVec4& col) { return col.w == -1; }
// Returns true if a style color is set to be automaticaly determined
inline bool IsColorAuto(ImPlotCol idx) { return IsColorAuto(GImPlot->Style.Colors[idx]); }
// Returns the automatically deduced style color
ImVec4 GetAutoColor(ImPlotCol idx);
// Returns the style color whether it is automatic or custom set
inline ImVec4 GetStyleColorVec4(ImPlotCol idx) { return IsColorAuto(idx) ? GetAutoColor(idx) : GImPlot->Style.Colors[idx]; }
inline ImU32 GetStyleColorU32(ImPlotCol idx) { return ImGui::ColorConvertFloat4ToU32(GetStyleColorVec4(idx)); }
// Get built-in colormap data and size
const ImVec4* GetColormap(ImPlotColormap colormap, int* size_out);
// Linearly interpolates a color from the current colormap given t between 0 and 1.
ImVec4 LerpColormap(const ImVec4* colormap, int size, float t);
// Resamples a colormap. #size_out must be greater than 1.
void ResampleColormap(const ImVec4* colormap_in, int size_in, ImVec4* colormap_out, int size_out);
2020-08-25 22:59:43 -04:00
2020-08-16 16:38:51 -04:00
// Draws vertical text. The position is the bottom left of the text rect.
2020-08-24 09:51:03 -04:00
void AddTextVertical(ImDrawList *DrawList, ImVec2 pos, ImU32 col, const char* text_begin, const char* text_end = NULL);
2020-08-16 16:38:51 -04:00
// Calculates the size of vertical text
2020-09-01 22:01:00 -04:00
inline ImVec2 CalcTextSizeVertical(const char *text) { ImVec2 sz = ImGui::CalcTextSize(text); return ImVec2(sz.y, sz.x); }
// Returns white or black text given background color
inline ImU32 CalcTextColor(const ImVec4& bg) { return (bg.x * 0.299 + bg.y * 0.587 + bg.z * 0.114) > 0.729 ? IM_COL32_BLACK : IM_COL32_WHITE; }
//-----------------------------------------------------------------------------
// [SECTION] Math and Misc Utils
//-----------------------------------------------------------------------------
2020-09-01 22:01:00 -04:00
// Rounds x to powers of 2,5 and 10 for generating axis labels (from Graphics Gems 1 Chapter 11.2)
double NiceNum(double x, bool round);
// Returns true if val is NAN or INFINITY
inline bool NanOrInf(double val) { return val == HUGE_VAL || val == -HUGE_VAL || isnan(val); }
2020-08-16 16:38:51 -04:00
// Turns NANs to 0s
inline double ConstrainNan(double val) { return isnan(val) ? 0 : val; }
// Turns infinity to floating point maximums
inline double ConstrainInf(double val) { return val == HUGE_VAL ? DBL_MAX : val == -HUGE_VAL ? - DBL_MAX : val; }
// Turns numbers less than or equal to 0 to 0.001 (sort of arbitrary, is there a better way?)
inline double ConstrainLog(double val) { return val <= 0 ? 0.001f : val; }
// Computes order of magnitude of double.
inline int OrderOfMagnitude(double val) { return val == 0 ? 0 : (int)(floor(log10(fabs(val)))); }
// Returns the precision required for a order of magnitude.
inline int OrderToPrecision(int order) { return order > 0 ? 0 : 1 - order; }
// Returns a floating point precision to use given a value
inline int Precision(double val) { return OrderToPrecision(OrderOfMagnitude(val)); }
2020-08-16 16:38:51 -04:00
// Returns the intersection point of two lines A and B (assumes they are not parallel!)
inline ImVec2 Intersection(const ImVec2& a1, const ImVec2& a2, const ImVec2& b1, const ImVec2& b2) {
2020-09-01 22:01:00 -04:00
float v1 = (a1.x * a2.y - a1.y * a2.x); float v2 = (b1.x * b2.y - b1.y * b2.x);
2020-08-16 16:38:51 -04:00
float v3 = ((a1.x - a2.x) * (b1.y - b2.y) - (a1.y - a2.y) * (b1.x - b2.x));
return ImVec2((v1 * (b1.x - b2.x) - v2 * (a1.x - a2.x)) / v3, (v1 * (b1.y - b2.y) - v2 * (a1.y - a2.y)) / v3);
}
2020-08-16 16:38:51 -04:00
// Fills a buffer with n samples linear interpolated from vmin to vmax
template <typename T>
void FillRange(ImVector<T>& buffer, int n, T vmin, T vmax) {
buffer.resize(n);
T step = (vmax - vmin) / (n - 1);
for (int i = 0; i < n; ++i) {
buffer[i] = vmin + i * step;
}
}
2020-08-16 16:38:51 -04:00
// Offsets and strides a data buffer
template <typename T>
inline T OffsetAndStride(const T* data, int idx, int count, int offset, int stride) {
idx = ImPosMod(offset + idx, count);
return *(const T*)(const void*)((const unsigned char*)data + (size_t)idx * stride);
}
2020-09-03 00:30:32 -04:00
//-----------------------------------------------------------------------------
// [SECTION] Time Utils
//-----------------------------------------------------------------------------
static const int DaysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static const double ImPlotTimeUnitSpans[ImPlotTimeUnit_COUNT] = {0.000001, 0.001, 1, 60, 3600, 86400, 2629800, 31557600};
inline ImPlotTimeUnit GetUnitForRange(double smin, double smax) {
double range = smax - smin;
for (int i = 0; i < ImPlotTimeUnit_COUNT; ++i) {
if (range <= ImPlotTimeUnitSpans[i])
return (ImPlotTimeUnit)i;
}
return ImPlotTimeUnit_Yr;
}
// Returns true if year is leap year (366 days long)
inline bool IsLeapYear(int year) {
if (year % 4 != 0) return false;
if (year % 400 == 0) return true;
if (year % 100 == 0) return false;
return true;
}
// Returns the number of days in a month, accounting for Feb. leap years.
inline int GetDaysInMonth(int year, int month) {
return DaysInMonth[month] + (int)(month == 1 && IsLeapYear(year));
}
inline time_t MkGmTime(const struct tm *ptm) {
time_t secs = 0;
int year = ptm->tm_year + 1900;
for (int y = 1970; y < year; ++y) {
secs += (IsLeapYear(y)? 366: 365) * 86400;
}
for (int m = 0; m < ptm->tm_mon; ++m) {
secs += DaysInMonth[m] * 86400;
if (m == 1 && IsLeapYear(year)) secs += 86400;
}
secs += (ptm->tm_mday - 1) * 86400;
secs += ptm->tm_hour * 3600;
secs += ptm->tm_min * 60;
secs += ptm->tm_sec;
return secs;
}
inline tm* GmTime(const time_t* time, tm* tm)
{
#ifdef _MSC_VER
if (gmtime_s(tm, time) == 0)
return tm;
else
return NULL;
#else
return gmtime_r(time, tm);
#endif
}
inline double AddTime(double t, ImPlotTimeUnit unit, int count) {
switch(unit) {
case ImPlotTimeUnit_Us: return t + count * 0.000001;
case ImPlotTimeUnit_Ms: return t + count * 0.001;
case ImPlotTimeUnit_S: return t + count;
case ImPlotTimeUnit_Min: return t + count * 60;
case ImPlotTimeUnit_Hr: return t + count * 3600;
case ImPlotTimeUnit_Day: return t + count * 86400;
case ImPlotTimeUnit_Yr: count *= 12; // fall-through
case ImPlotTimeUnit_Mo: for (int i = 0; i < count; ++i) {
time_t s = (time_t)t;
GmTime(&s, &GImPlot->Tm);
int days = GetDaysInMonth(GImPlot->Tm.tm_year, GImPlot->Tm.tm_mon);
t = AddTime(t, ImPlotTimeUnit_Day, days);
}
return t;
default: return t;
}
}
inline double FloorTime(double t, ImPlotTimeUnit unit) {
time_t s = (time_t)t;
GmTime(&s, &GImPlot->Tm);
GImPlot->Tm.tm_isdst = -1;
switch (unit) {
case ImPlotTimeUnit_S: return (double)s;
case ImPlotTimeUnit_Ms: return floor(t * 1000) / 1000;
case ImPlotTimeUnit_Us: return floor(t * 1000000) / 1000000;
case ImPlotTimeUnit_Yr: GImPlot->Tm.tm_mon = 0; // fall-through
case ImPlotTimeUnit_Mo: GImPlot->Tm.tm_mday = 1; // fall-through
case ImPlotTimeUnit_Day: GImPlot->Tm.tm_hour = 0; // fall-through
case ImPlotTimeUnit_Hr: GImPlot->Tm.tm_min = 0; // fall-through
case ImPlotTimeUnit_Min: GImPlot->Tm.tm_sec = 0; break;
default: return t;
}
s = MkGmTime(&GImPlot->Tm);
return (double)s;
}
inline double CeilTime(double t, ImPlotTimeUnit unit) {
return AddTime(FloorTime(t, unit), unit, 1);
}
2020-09-03 09:00:36 -04:00
inline void FormatTime(double t, char* buffer, int size, ImPlotTimeUnit unit) {
2020-09-03 00:30:32 -04:00
time_t s = (time_t)t;
int ms = (int)(t * 1000 - floor(t) * 1000);
int us = (int)(t * 1000000 - floor(t) * 1000000);
tm& Tm = GImPlot->Tm;
GmTime(&s, &Tm);
2020-09-03 09:00:36 -04:00
switch(unit) {
case ImPlotTimeUnit_Yr: strftime(buffer, size, "%Y", &Tm); break;
case ImPlotTimeUnit_Mo: strftime(buffer, size, "%b", &Tm); break;
case ImPlotTimeUnit_Day: strftime(buffer, size, "%m/%d", &Tm); break;
case ImPlotTimeUnit_Hr:
2020-09-03 00:30:32 -04:00
if (Tm.tm_hour == 0)
snprintf(buffer, size, "12am");
else if (Tm.tm_hour == 12)
snprintf(buffer, size, "12pm");
else if (Tm.tm_hour < 12)
snprintf(buffer, size, "%uam", Tm.tm_hour);
else if (Tm.tm_hour > 12)
snprintf(buffer, size, "%upm", Tm.tm_hour - 12);
break;
2020-09-03 09:00:36 -04:00
case ImPlotTimeUnit_Min:
2020-09-03 00:30:32 -04:00
if (Tm.tm_hour == 0)
snprintf(buffer, size, "12:%02dam", Tm.tm_min);
else if (Tm.tm_hour == 12)
snprintf(buffer, size, "12%02dpm", Tm.tm_min);
else if (Tm.tm_hour < 12)
snprintf(buffer, size, "%u:%02dam", Tm.tm_hour, Tm.tm_min);
else if (Tm.tm_hour > 12)
snprintf(buffer, size, "%u:%02dpm", Tm.tm_hour - 12, Tm.tm_min);
break;
2020-09-03 09:00:36 -04:00
case ImPlotTimeUnit_S: snprintf(buffer, size, ":%02d", Tm.tm_sec); break;
case ImPlotTimeUnit_Ms: snprintf(buffer, size, ":%02d.%03d", Tm.tm_sec, ms); break;
case ImPlotTimeUnit_Us: snprintf(buffer, size, ":%02d.%06d", Tm.tm_sec, us); break;
2020-09-03 00:30:32 -04:00
default: break;
}
}
//-----------------------------------------------------------------------------
// [SECTION] Internal / Experimental Plotters
// No guarantee of forward compatibility here!
//-----------------------------------------------------------------------------
// Plots axis-aligned, filled rectangles. Every two consecutive points defines opposite corners of a single rectangle.
void PlotRects(const char* label_id, const float* xs, const float* ys, int count, int offset = 0, int stride = sizeof(float));
void PlotRects(const char* label_id, const double* xs, const double* ys, int count, int offset = 0, int stride = sizeof(double));
void PlotRects(const char* label_id, ImPlotPoint (*getter)(void* data, int idx), void* data, int count, int offset = 0);
2020-08-16 16:38:51 -04:00
} // namespace ImPlot