implot3d.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. //--------------------------------------------------
  2. // ImPlot3D v0.1
  3. // implot3d.h
  4. // Date: 2024-11-16
  5. // Author: Breno Cunha Queiroz (brenocq.com)
  6. //
  7. // Acknowledgments:
  8. // ImPlot3D is heavily inspired by ImPlot
  9. // (https://github.com/epezent/implot) by Evan Pezent,
  10. // and follows a similar code style and structure to
  11. // maintain consistency with ImPlot's API.
  12. //--------------------------------------------------
  13. // Table of Contents:
  14. // [SECTION] Macros and Defines
  15. // [SECTION] Forward declarations and basic types
  16. // [SECTION] Flags & Enumerations
  17. // [SECTION] Context
  18. // [SECTION] Begin/End Plot
  19. // [SECTION] Setup
  20. // [SECTION] Plot Items
  21. // [SECTION] Plot Utils
  22. // [SECTION] Miscellaneous
  23. // [SECTION] Styles
  24. // [SECTION] Demo
  25. // [SECTION] Debugging
  26. // [SECTION] ImPlot3DPoint
  27. // [SECTION] ImPlot3DRay
  28. // [SECTION] ImPlot3DPlane
  29. // [SECTION] ImPlot3DBox
  30. // [SECTION] ImPlot3DQuat
  31. // [SECTION] ImPlot3DStyle
  32. // [SECTION] Callbacks
  33. // [SECTION] Meshes
  34. #pragma once
  35. #include "imgui.h"
  36. #ifndef IMGUI_DISABLE
  37. //-----------------------------------------------------------------------------
  38. // [SECTION] Macros and Defines
  39. //-----------------------------------------------------------------------------
  40. #ifndef IMPLOT3D_API
  41. #define IMPLOT3D_API
  42. #endif
  43. #define IMPLOT3D_VERSION "0.1" // ImPlot3D version
  44. #define IMPLOT3D_AUTO -1 // Deduce variable automatically
  45. #define IMPLOT3D_AUTO_COL ImVec4(0, 0, 0, -1) // Deduce color automatically
  46. #define IMPLOT3D_TMP template <typename T> IMPLOT3D_API
  47. //-----------------------------------------------------------------------------
  48. // [SECTION] Forward declarations and basic types
  49. //-----------------------------------------------------------------------------
  50. // Forward declarations
  51. struct ImPlot3DContext;
  52. struct ImPlot3DStyle;
  53. struct ImPlot3DPoint;
  54. struct ImPlot3DRay;
  55. struct ImPlot3DPlane;
  56. struct ImPlot3DBox;
  57. struct ImPlot3DRange;
  58. struct ImPlot3DQuat;
  59. // Enums
  60. typedef int ImPlot3DCond; // -> ImPlot3DCond_ // Enum: Condition for flags
  61. typedef int ImPlot3DCol; // -> ImPlot3DCol_ // Enum: Styling colors
  62. typedef int ImPlot3DStyleVar; // -> ImPlot3DStyleVar_ // Enum: Style variables
  63. typedef int ImPlot3DMarker; // -> ImPlot3DMarker_ // Enum: Marker styles
  64. typedef int ImPlot3DLocation; // -> ImPlot3DLocation_ // Enum: Locations
  65. typedef int ImAxis3D; // -> ImAxis3D_ // Enum: Axis indices
  66. typedef int ImPlane3D; // -> ImPlane3D_ // Enum: Plane indices
  67. typedef int ImPlot3DColormap; // -> ImPlot3DColormap_ // Enum: Colormaps
  68. // Flags
  69. typedef int ImPlot3DFlags; // -> ImPlot3DFlags_ // Flags: for BeginPlot()
  70. typedef int ImPlot3DItemFlags; // -> ImPlot3DItemFlags_ // Flags: Item flags
  71. typedef int ImPlot3DScatterFlags; // -> ImPlot3DScatterFlags_ // Flags: Scatter plot flags
  72. typedef int ImPlot3DLineFlags; // -> ImPlot3DLineFlags_ // Flags: Line plot flags
  73. typedef int ImPlot3DTriangleFlags; // -> ImPlot3DTriangleFlags_ // Flags: Triangle plot flags
  74. typedef int ImPlot3DQuadFlags; // -> ImPlot3DQuadFlags_ // Flags: QuadFplot flags
  75. typedef int ImPlot3DSurfaceFlags; // -> ImPlot3DSurfaceFlags_ // Flags: Surface plot flags
  76. typedef int ImPlot3DMeshFlags; // -> ImPlot3DMeshFlags_ // Flags: Mesh plot flags
  77. typedef int ImPlot3DLegendFlags; // -> ImPlot3DLegendFlags_ // Flags: Legend flags
  78. typedef int ImPlot3DAxisFlags; // -> ImPlot3DAxisFlags_ // Flags: Axis flags
  79. //-----------------------------------------------------------------------------
  80. // [SECTION] Flags & Enumerations
  81. //-----------------------------------------------------------------------------
  82. // Flags for ImPlot3D::BeginPlot()
  83. enum ImPlot3DFlags_ {
  84. ImPlot3DFlags_None = 0, // Default
  85. ImPlot3DFlags_NoTitle = 1 << 0, // Hide plot title
  86. ImPlot3DFlags_NoLegend = 1 << 1, // Hide plot legend
  87. ImPlot3DFlags_NoMouseText = 1 << 2, // Hide mouse position in plot coordinates
  88. ImPlot3DFlags_NoClip = 1 << 3, // Disable 3D box clipping
  89. ImPlot3DFlags_CanvasOnly = ImPlot3DFlags_NoTitle | ImPlot3DFlags_NoLegend | ImPlot3DFlags_NoMouseText,
  90. };
  91. // Represents a condition for SetupAxisLimits etc. (same as ImGuiCond, but we only support a subset of those enums)
  92. enum ImPlot3DCond_ {
  93. ImPlot3DCond_None = ImGuiCond_None, // No condition (always set the variable), same as _Always
  94. ImPlot3DCond_Always = ImGuiCond_Always, // No condition (always set the variable)
  95. ImPlot3DCond_Once = ImGuiCond_Once, // Set the variable once per runtime session (only the first call will succeed)
  96. };
  97. enum ImPlot3DCol_ {
  98. // Item colors
  99. ImPlot3DCol_Line = 0, // Line color
  100. ImPlot3DCol_Fill, // Fill color
  101. ImPlot3DCol_MarkerOutline, // Marker outline color
  102. ImPlot3DCol_MarkerFill, // Marker fill color
  103. // Plot colors
  104. ImPlot3DCol_TitleText, // Title color
  105. ImPlot3DCol_InlayText, // Color for texts appearing inside of plots
  106. ImPlot3DCol_FrameBg, // Frame background color
  107. ImPlot3DCol_PlotBg, // Plot area background color
  108. ImPlot3DCol_PlotBorder, // Plot area border color
  109. // Legend colors
  110. ImPlot3DCol_LegendBg, // Legend background color
  111. ImPlot3DCol_LegendBorder, // Legend border color
  112. ImPlot3DCol_LegendText, // Legend text color
  113. // Axis colors
  114. ImPlot3DCol_AxisText, // Axis label and tick lables color
  115. ImPlot3DCol_AxisGrid, // Axis grid color
  116. ImPlot3DCol_AxisTick, // Axis tick color (defaults to AxisGrid)
  117. ImPlot3DCol_COUNT,
  118. };
  119. // Plot styling variables
  120. enum ImPlot3DStyleVar_ {
  121. // Item style
  122. ImPlot3DStyleVar_LineWeight, // float, plot item line weight in pixels
  123. ImPlot3DStyleVar_Marker, // int, marker specification
  124. ImPlot3DStyleVar_MarkerSize, // float, marker size in pixels (roughly the marker's "radius")
  125. ImPlot3DStyleVar_MarkerWeight, // float, plot outline weight of markers in pixels
  126. ImPlot3DStyleVar_FillAlpha, // float, alpha modifier applied to all plot item fills
  127. // Plot style
  128. ImPlot3DStyleVar_PlotDefaultSize, // ImVec2, default size used when ImVec2(0,0) is passed to BeginPlot
  129. ImPlot3DStyleVar_PlotMinSize, // ImVec2, minimum size plot frame can be when shrunk
  130. ImPlot3DStyleVar_PlotPadding, // ImVec2, padding between widget frame and plot area, labels, or outside legends (i.e. main padding)
  131. ImPlot3DStyleVar_LabelPadding, // ImVec2, padding between axes labels, tick labels, and plot edge
  132. // Legend style
  133. ImPlot3DStyleVar_LegendPadding, // ImVec2, legend padding from plot edges
  134. ImPlot3DStyleVar_LegendInnerPadding, // ImVec2, legend inner padding from legend edges
  135. ImPlot3DStyleVar_LegendSpacing, // ImVec2, spacing between legend entries
  136. ImPlot3DStyleVar_COUNT
  137. };
  138. enum ImPlot3DMarker_ {
  139. ImPlot3DMarker_None = -1, // No marker
  140. ImPlot3DMarker_Circle, // Circle marker (default)
  141. ImPlot3DMarker_Square, // Square maker
  142. ImPlot3DMarker_Diamond, // Diamond marker
  143. ImPlot3DMarker_Up, // Upward-pointing triangle marker
  144. ImPlot3DMarker_Down, // Downward-pointing triangle marker
  145. ImPlot3DMarker_Left, // Leftward-pointing triangle marker
  146. ImPlot3DMarker_Right, // Rightward-pointing triangle marker
  147. ImPlot3DMarker_Cross, // Cross marker (not fillable)
  148. ImPlot3DMarker_Plus, // Plus marker (not fillable)
  149. ImPlot3DMarker_Asterisk, // Asterisk marker (not fillable)
  150. ImPlot3DMarker_COUNT
  151. };
  152. // Flags for items
  153. enum ImPlot3DItemFlags_ {
  154. ImPlot3DItemFlags_None = 0, // Default
  155. ImPlot3DItemFlags_NoLegend = 1 << 0, // The item won't have a legend entry displayed
  156. ImPlot3DItemFlags_NoFit = 1 << 1, // The item won't be considered for plot fits
  157. };
  158. // Flags for PlotScatter
  159. enum ImPlot3DScatterFlags_ {
  160. ImPlot3DScatterFlags_None = 0, // Default
  161. ImPlot3DScatterFlags_NoLegend = ImPlot3DItemFlags_NoLegend,
  162. ImPlot3DScatterFlags_NoFit = ImPlot3DItemFlags_NoFit,
  163. };
  164. // Flags for PlotLine
  165. enum ImPlot3DLineFlags_ {
  166. ImPlot3DLineFlags_None = 0, // Default
  167. ImPlot3DLineFlags_NoLegend = ImPlot3DItemFlags_NoLegend,
  168. ImPlot3DLineFlags_NoFit = ImPlot3DItemFlags_NoFit,
  169. ImPlot3DLineFlags_Segments = 1 << 10, // A line segment will be rendered from every two consecutive points
  170. ImPlot3DLineFlags_Loop = 1 << 11, // The last and first point will be connected to form a closed loop
  171. ImPlot3DLineFlags_SkipNaN = 1 << 12, // NaNs values will be skipped instead of rendered as missing data
  172. };
  173. // Flags for PlotTriangle
  174. enum ImPlot3DTriangleFlags_ {
  175. ImPlot3DTriangleFlags_None = 0, // Default
  176. ImPlot3DTriangleFlags_NoLegend = ImPlot3DItemFlags_NoLegend,
  177. ImPlot3DTriangleFlags_NoFit = ImPlot3DItemFlags_NoFit,
  178. };
  179. // Flags for PlotQuad
  180. enum ImPlot3DQuadFlags_ {
  181. ImPlot3DQuadFlags_None = 0, // Default
  182. ImPlot3DQuadFlags_NoLegend = ImPlot3DItemFlags_NoLegend,
  183. ImPlot3DQuadFlags_NoFit = ImPlot3DItemFlags_NoFit,
  184. };
  185. // Flags for PlotSurface
  186. enum ImPlot3DSurfaceFlags_ {
  187. ImPlot3DSurfaceFlags_None = 0, // Default
  188. ImPlot3DSurfaceFlags_NoLegend = ImPlot3DItemFlags_NoLegend,
  189. ImPlot3DSurfaceFlags_NoFit = ImPlot3DItemFlags_NoFit,
  190. };
  191. // Flags for PlotMesh
  192. enum ImPlot3DMeshFlags_ {
  193. ImPlot3DMeshFlags_None = 0, // Default
  194. ImPlot3DMeshFlags_NoLegend = ImPlot3DItemFlags_NoLegend,
  195. ImPlot3DMeshFlags_NoFit = ImPlot3DItemFlags_NoFit,
  196. };
  197. // Flags for legends
  198. enum ImPlot3DLegendFlags_ {
  199. ImPlot3DLegendFlags_None = 0, // Default
  200. ImPlot3DLegendFlags_NoButtons = 1 << 0, // Legend icons will not function as hide/show buttons
  201. ImPlot3DLegendFlags_NoHighlightItem = 1 << 1, // Plot items will not be highlighted when their legend entry is hovered
  202. ImPlot3DLegendFlags_Horizontal = 1 << 2, // Legend entries will be displayed horizontally
  203. };
  204. // Used to position legend on a plot
  205. enum ImPlot3DLocation_ {
  206. ImPlot3DLocation_Center = 0, // Center-center
  207. ImPlot3DLocation_North = 1 << 0, // Top-center
  208. ImPlot3DLocation_South = 1 << 1, // Bottom-center
  209. ImPlot3DLocation_West = 1 << 2, // Center-left
  210. ImPlot3DLocation_East = 1 << 3, // Center-right
  211. ImPlot3DLocation_NorthWest = ImPlot3DLocation_North | ImPlot3DLocation_West, // Top-left
  212. ImPlot3DLocation_NorthEast = ImPlot3DLocation_North | ImPlot3DLocation_East, // Top-right
  213. ImPlot3DLocation_SouthWest = ImPlot3DLocation_South | ImPlot3DLocation_West, // Bottom-left
  214. ImPlot3DLocation_SouthEast = ImPlot3DLocation_South | ImPlot3DLocation_East // Bottom-right
  215. };
  216. // Flags for axis
  217. enum ImPlot3DAxisFlags_ {
  218. ImPlot3DAxisFlags_None = 0, // Default
  219. ImPlot3DAxisFlags_NoLabel = 1 << 0, // No axis label will be displayed
  220. ImPlot3DAxisFlags_NoGridLines = 1 << 1, // No grid lines will be displayed
  221. ImPlot3DAxisFlags_NoTickMarks = 1 << 2, // No tick marks will be displayed
  222. ImPlot3DAxisFlags_NoTickLabels = 1 << 3, // No tick labels will be displayed
  223. ImPlot3DAxisFlags_LockMin = 1 << 4, // The axis minimum value will be locked when panning/zooming
  224. ImPlot3DAxisFlags_LockMax = 1 << 5, // The axis maximum value will be locked when panning/zooming
  225. ImPlot3DAxisFlags_AutoFit = 1 << 6, // Axis will be auto-fitting to data extents
  226. ImPlot3DAxisFlags_Lock = ImPlot3DAxisFlags_LockMin | ImPlot3DAxisFlags_LockMax,
  227. ImPlot3DAxisFlags_NoDecorations = ImPlot3DAxisFlags_NoLabel | ImPlot3DAxisFlags_NoGridLines | ImPlot3DAxisFlags_NoTickLabels,
  228. };
  229. // Axis indices
  230. enum ImAxis3D_ {
  231. ImAxis3D_X = 0,
  232. ImAxis3D_Y,
  233. ImAxis3D_Z,
  234. ImAxis3D_COUNT,
  235. };
  236. // Plane indices
  237. enum ImPlane3D_ {
  238. ImPlane3D_YZ = 0,
  239. ImPlane3D_XZ,
  240. ImPlane3D_XY,
  241. ImPlane3D_COUNT,
  242. };
  243. // Colormaps
  244. enum ImPlot3DColormap_ {
  245. ImPlot3DColormap_Deep = 0, // Same as seaborn "deep"
  246. ImPlot3DColormap_Dark = 1, // Same as matplotlib "Set1"
  247. ImPlot3DColormap_Pastel = 2, // Same as matplotlib "Pastel1"
  248. ImPlot3DColormap_Paired = 3, // Same as matplotlib "Paired"
  249. ImPlot3DColormap_Viridis = 4, // Same as matplotlib "viridis"
  250. ImPlot3DColormap_Plasma = 5, // Same as matplotlib "plasma"
  251. ImPlot3DColormap_Hot = 6, // Same as matplotlib/MATLAB "hot"
  252. ImPlot3DColormap_Cool = 7, // Same as matplotlib/MATLAB "cool"
  253. ImPlot3DColormap_Pink = 8, // Same as matplotlib/MATLAB "pink"
  254. ImPlot3DColormap_Jet = 9, // Same as matplotlib/MATLAB "jet"
  255. ImPlot3DColormap_Twilight = 10, // Same as matplotlib "twilight"
  256. ImPlot3DColormap_RdBu = 11, // Same as matplotlib "RdBu"
  257. ImPlot3DColormap_BrBG = 12, // Same as matplotlib "BrGB"
  258. ImPlot3DColormap_PiYG = 13, // Same as matplotlib "PiYG"
  259. ImPlot3DColormap_Spectral = 14, // Same as matplotlib "Spectral"
  260. ImPlot3DColormap_Greys = 15, // White/black
  261. };
  262. namespace ImPlot3D {
  263. //-----------------------------------------------------------------------------
  264. // [SECTION] Context
  265. //-----------------------------------------------------------------------------
  266. IMPLOT3D_API ImPlot3DContext* CreateContext();
  267. IMPLOT3D_API void DestroyContext(ImPlot3DContext* ctx = nullptr);
  268. IMPLOT3D_API ImPlot3DContext* GetCurrentContext();
  269. IMPLOT3D_API void SetCurrentContext(ImPlot3DContext* ctx);
  270. //-----------------------------------------------------------------------------
  271. // [SECTION] Begin/End Plot
  272. //-----------------------------------------------------------------------------
  273. // Starts a 3D plotting context. If this function returns true, EndPlot() MUST
  274. // be called! You are encouraged to use the following convention:
  275. //
  276. // if (ImPlot3D::BeginPlot(...)) {
  277. // ImPlot3D::PlotLine(...);
  278. // ...
  279. // ImPlot3D::EndPlot();
  280. // }
  281. //
  282. // Important notes:
  283. // - #title_id must be unique to the current ImGui ID scope. If you need to avoid ID
  284. // collisions or don't want to display a title in the plot, use double hashes
  285. // (e.g. "MyPlot##HiddenIdText" or "##NoTitle").
  286. // - #size is the **frame** size of the plot widget, not the plot area.
  287. IMPLOT3D_API bool BeginPlot(const char* title_id, const ImVec2& size = ImVec2(-1, 0), ImPlot3DFlags flags = 0);
  288. IMPLOT3D_API void EndPlot(); // Only call if BeginPlot() returns true!
  289. //-----------------------------------------------------------------------------
  290. // [SECTION] Setup
  291. //-----------------------------------------------------------------------------
  292. // The following API allows you to setup and customize various aspects of the
  293. // current plot. The functions should be called immediately after BeginPlot()
  294. // and before any other API calls. Typical usage is as follows:
  295. // if (ImPlot3D::BeginPlot(...)) { 1) Begin a new plot
  296. // ImPlot3D::SetupAxis(ImAxis3D_X, "My X-Axis"); 2) Make Setup calls
  297. // ImPlot3D::SetupAxis(ImAxis3D_Y, "My Y-Axis");
  298. // ImPlot3D::SetupLegend(ImPlotLocation_North);
  299. // ...
  300. // ImPlot3D::SetupFinish(); 3) [Optional] Explicitly finish setup
  301. // ImPlot3D::PlotLine(...); 4) Plot items
  302. // ...
  303. // ImPlot3D::EndPlot(); 5) End the plot
  304. // }
  305. //
  306. // Important notes:
  307. //
  308. // - Always call Setup code at the top of your BeginPlot conditional statement.
  309. // - Setup is locked once you start plotting or explicitly call SetupFinish.
  310. // Do NOT call Setup code after you begin plotting or after you make
  311. // any non-Setup API calls (e.g. utils like PlotToPixels also lock Setup).
  312. // - Calling SetupFinish is OPTIONAL, but probably good practice. If you do not
  313. // call it yourself, then the first subsequent plotting or utility function will
  314. // call it for you.
  315. // Enables an axis or sets the label and/or flags for an existing axis. Leave #label = nullptr for no label
  316. IMPLOT3D_API void SetupAxis(ImAxis3D axis, const char* label = nullptr, ImPlot3DAxisFlags flags = 0);
  317. IMPLOT3D_API void SetupAxisLimits(ImAxis3D axis, double v_min, double v_max, ImPlot3DCond cond = ImPlot3DCond_Once);
  318. // Sets the label and/or flags for primary X/Y/Z axes (shorthand for three calls to SetupAxis)
  319. IMPLOT3D_API void SetupAxes(const char* x_label, const char* y_label, const char* z_label, ImPlot3DAxisFlags x_flags = 0, ImPlot3DAxisFlags y_flags = 0, ImPlot3DAxisFlags z_flags = 0);
  320. // Sets the X/Y/Z axes range limits. If ImPlotCond_Always is used, the axes limits will be locked (shorthand for two calls to SetupAxisLimits)
  321. IMPLOT3D_API void SetupAxesLimits(double x_min, double x_max, double y_min, double y_max, double z_min, double z_max, ImPlot3DCond cond = ImPlot3DCond_Once);
  322. IMPLOT3D_API void SetupLegend(ImPlot3DLocation location, ImPlot3DLegendFlags flags = 0);
  323. //-----------------------------------------------------------------------------
  324. // [SECTION] Plot Items
  325. //-----------------------------------------------------------------------------
  326. IMPLOT3D_TMP void PlotScatter(const char* label_id, const T* xs, const T* ys, const T* zs, int count, ImPlot3DScatterFlags flags = 0, int offset = 0, int stride = sizeof(T));
  327. IMPLOT3D_TMP void PlotLine(const char* label_id, const T* xs, const T* ys, const T* zs, int count, ImPlot3DLineFlags flags = 0, int offset = 0, int stride = sizeof(T));
  328. IMPLOT3D_TMP void PlotTriangle(const char* label_id, const T* xs, const T* ys, const T* zs, int count, ImPlot3DTriangleFlags flags = 0, int offset = 0, int stride = sizeof(T));
  329. IMPLOT3D_TMP void PlotQuad(const char* label_id, const T* xs, const T* ys, const T* zs, int count, ImPlot3DQuadFlags flags = 0, int offset = 0, int stride = sizeof(T));
  330. IMPLOT3D_TMP void PlotSurface(const char* label_id, const T* xs, const T* ys, const T* zs, int x_count, int y_count, ImPlot3DSurfaceFlags flags = 0, int offset = 0, int stride = sizeof(T));
  331. IMPLOT3D_API void PlotMesh(const char* label_id, const ImPlot3DPoint* vtx, const unsigned int* idx, int vtx_count, int idx_count, ImPlot3DMeshFlags flags = 0);
  332. // Plots a centered text label at point x,y,z. It is possible to set the text angle in radians and offset in pixels
  333. IMPLOT3D_API void PlotText(const char* text, float x, float y, float z, float angle = 0.0f, const ImVec2& pix_offset = ImVec2(0, 0));
  334. //-----------------------------------------------------------------------------
  335. // [SECTION] Plot Utils
  336. //-----------------------------------------------------------------------------
  337. // Convert a position in the current plot's coordinate system to pixels
  338. IMPLOT3D_API ImVec2 PlotToPixels(const ImPlot3DPoint& point);
  339. IMPLOT3D_API ImVec2 PlotToPixels(double x, double y, double z);
  340. // Convert a pixel coordinate to a ray in the current plot's coordinate system
  341. IMPLOT3D_API ImPlot3DRay PixelsToPlotRay(const ImVec2& pix);
  342. IMPLOT3D_API ImPlot3DRay PixelsToPlotRay(double x, double y);
  343. // Convert a pixel coordinate to a point in an axis plane in the current plot's coordinate system
  344. IMPLOT3D_API ImPlot3DPoint PixelsToPlotPlane(const ImVec2& pix, ImPlane3D plane, bool mask = true);
  345. IMPLOT3D_API ImPlot3DPoint PixelsToPlotPlane(double x, double y, ImPlane3D plane, bool mask = true);
  346. IMPLOT3D_API ImVec2 GetPlotPos(); // Get the current plot position (top-left) in pixels
  347. IMPLOT3D_API ImVec2 GetPlotSize(); // Get the current plot size in pixels
  348. //-----------------------------------------------------------------------------
  349. // [SECTION] Miscellaneous
  350. //-----------------------------------------------------------------------------
  351. IMPLOT3D_API ImDrawList* GetPlotDrawList();
  352. //-----------------------------------------------------------------------------
  353. // [SECTION] Styles
  354. //-----------------------------------------------------------------------------
  355. // Get current style
  356. IMPLOT3D_API ImPlot3DStyle& GetStyle();
  357. // Set color styles
  358. IMPLOT3D_API void StyleColorsAuto(ImPlot3DStyle* dst = nullptr); // Set colors with ImGui style
  359. IMPLOT3D_API void StyleColorsDark(ImPlot3DStyle* dst = nullptr); // Set colors with dark style
  360. IMPLOT3D_API void StyleColorsLight(ImPlot3DStyle* dst = nullptr); // Set colors with light style
  361. IMPLOT3D_API void StyleColorsClassic(ImPlot3DStyle* dst = nullptr); // Set colors with classic style
  362. // Temporarily modify a style color. Don't forget to call PopStyleColor!
  363. IMPLOT3D_API void PushStyleColor(ImPlot3DCol idx, ImU32 col);
  364. IMPLOT3D_API void PushStyleColor(ImPlot3DCol idx, const ImVec4& col);
  365. // Undo temporary style color modification(s). Undo multiple pushes at once by increasing count
  366. IMPLOT3D_API void PopStyleColor(int count = 1);
  367. // Temporarily modify a style variable of float type. Don't forget to call PopStyleVar!
  368. IMPLOT3D_API void PushStyleVar(ImPlot3DStyleVar idx, float val);
  369. // Temporarily modify a style variable of int type. Don't forget to call PopStyleVar!
  370. IMPLOT3D_API void PushStyleVar(ImPlot3DStyleVar idx, int val);
  371. // Temporarily modify a style variable of ImVec2 type. Don't forget to call PopStyleVar!
  372. IMPLOT3D_API void PushStyleVar(ImPlot3DStyleVar idx, const ImVec2& val);
  373. // Undo temporary style variable modification(s). Undo multiple pushes at once by increasing count
  374. IMPLOT3D_API void PopStyleVar(int count = 1);
  375. // Set the line color and weight for the next item only
  376. IMPLOT3D_API void SetNextLineStyle(const ImVec4& col = IMPLOT3D_AUTO_COL, float weight = IMPLOT3D_AUTO);
  377. // Set the fill color for the next item only
  378. IMPLOT3D_API void SetNextFillStyle(const ImVec4& col = IMPLOT3D_AUTO_COL, float alpha_mod = IMPLOT3D_AUTO);
  379. // Set the marker style for the next item only
  380. IMPLOT3D_API void SetNextMarkerStyle(ImPlot3DMarker marker = IMPLOT3D_AUTO, float size = IMPLOT3D_AUTO, const ImVec4& fill = IMPLOT3D_AUTO_COL, float weight = IMPLOT3D_AUTO, const ImVec4& outline = IMPLOT3D_AUTO_COL);
  381. // Get color
  382. IMPLOT3D_API ImVec4 GetStyleColorVec4(ImPlot3DCol idx);
  383. IMPLOT3D_API ImU32 GetStyleColorU32(ImPlot3DCol idx);
  384. //-----------------------------------------------------------------------------
  385. // [SECTION] Colormaps
  386. //-----------------------------------------------------------------------------
  387. // Item styling is based on colormaps when the relevant ImPlot3DCol_XXX is set to
  388. // IMPLOT3D_AUTO_COL (default). Several built-in colormaps are available. You can
  389. // add and then push/pop your own colormaps as well. To permanently set a colormap,
  390. // modify the Colormap index member of your ImPlot3DStyle.
  391. // Colormap data will be ignored and a custom color will be used if you have done one of the following:
  392. // 1) Modified an item style color in your ImPlot3DStyle to anything other than IMPLOT3D_AUTO_COL.
  393. // 3) Set the next item style with a SetNextXXXStyle function.
  394. // Add a new colormap. The color data will be copied. The colormap can be used by pushing either the returned index or the
  395. // string name with PushColormap. The colormap name must be unique and the size must be greater than 1. You will receive
  396. // an assert otherwise! By default colormaps are considered to be qualitative (i.e. discrete). If you want to create a
  397. // continuous colormap, set #qual=false. This will treat the colors you provide as keys, and ImPlot3D will build a linearly
  398. // interpolated lookup table. The memory footprint of this table will be exactly ((size-1)*255+1)*4 bytes.
  399. IMPLOT3D_API ImPlot3DColormap AddColormap(const char* name, const ImVec4* cols, int size, bool qual = true);
  400. IMPLOT3D_API ImPlot3DColormap AddColormap(const char* name, const ImU32* cols, int size, bool qual = true);
  401. // Returns the number of available colormaps (i.e. the built-in + user-added count)
  402. IMPLOT3D_API int GetColormapCount();
  403. // Returns a null terminated string name for a colormap given an index. Returns nullptr if index is invalid
  404. IMPLOT3D_API const char* GetColormapName(ImPlot3DColormap cmap);
  405. // Returns an index number for a colormap given a valid string name. Returns -1 if name is invalid
  406. IMPLOT3D_API ImPlot3DColormap GetColormapIndex(const char* name);
  407. // Temporarily switch to one of the built-in (i.e. ImPlot3DColormap_XXX) or user-added colormaps (i.e. a return value of AddColormap). Don't forget to call PopColormap!
  408. IMPLOT3D_API void PushColormap(ImPlot3DColormap cmap);
  409. // Push a colormap by string name. Use built-in names such as "Default", "Deep", "Jet", etc. or a string you provided to AddColormap. Don't forget to call PopColormap!
  410. IMPLOT3D_API void PushColormap(const char* name);
  411. // Undo temporary colormap modification(s). Undo multiple pushes at once by increasing count
  412. IMPLOT3D_API void PopColormap(int count = 1);
  413. // Returns the next color from the current colormap and advances the colormap for the current plot
  414. // Can also be used with no return value to skip colors if desired. You need to call it between Begin/EndPlot!
  415. IMPLOT3D_API ImVec4 NextColormapColor();
  416. // Returns the size of a colormap
  417. IMPLOT3D_API int GetColormapSize(ImPlot3DColormap cmap = IMPLOT3D_AUTO);
  418. // Returns a color from a colormap given an index >= 0 (modulo will be performed)
  419. IMPLOT3D_API ImVec4 GetColormapColor(int idx, ImPlot3DColormap cmap = IMPLOT3D_AUTO);
  420. // Sample a color from the current colormap given t between 0 and 1
  421. IMPLOT3D_API ImVec4 SampleColormap(float t, ImPlot3DColormap cmap = IMPLOT3D_AUTO);
  422. //-----------------------------------------------------------------------------
  423. // [SECTION] Demo
  424. //-----------------------------------------------------------------------------
  425. // Add implot3d_demo.cpp to your sources to use methods in this section
  426. // Shows the ImPlot3D demo window
  427. IMPLOT3D_API void ShowDemoWindow(bool* p_open = nullptr);
  428. // Shows ImPlot3D style editor block (not a window)
  429. IMPLOT3D_API void ShowStyleEditor(ImPlot3DStyle* ref = nullptr);
  430. } // namespace ImPlot3D
  431. //-----------------------------------------------------------------------------
  432. // [SECTION] ImPlot3DPoint
  433. //-----------------------------------------------------------------------------
  434. // ImPlot3DPoint: 3D vector to store points in 3D
  435. struct ImPlot3DPoint {
  436. float x, y, z;
  437. constexpr ImPlot3DPoint() : x(0.0f), y(0.0f), z(0.0f) {}
  438. constexpr ImPlot3DPoint(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {}
  439. // Accessors
  440. float& operator[](size_t idx) {
  441. IM_ASSERT(idx == 0 || idx == 1 || idx == 2);
  442. return ((float*)(void*)(char*)this)[idx];
  443. }
  444. float operator[](size_t idx) const {
  445. IM_ASSERT(idx == 0 || idx == 1 || idx == 2);
  446. return ((const float*)(const void*)(const char*)this)[idx];
  447. }
  448. // Binary operators
  449. ImPlot3DPoint operator*(float rhs) const;
  450. ImPlot3DPoint operator/(float rhs) const;
  451. ImPlot3DPoint operator+(const ImPlot3DPoint& rhs) const;
  452. ImPlot3DPoint operator-(const ImPlot3DPoint& rhs) const;
  453. ImPlot3DPoint operator*(const ImPlot3DPoint& rhs) const;
  454. ImPlot3DPoint operator/(const ImPlot3DPoint& rhs) const;
  455. // Unary operator
  456. ImPlot3DPoint operator-() const;
  457. // Compound assignment operators
  458. ImPlot3DPoint& operator*=(float rhs);
  459. ImPlot3DPoint& operator/=(float rhs);
  460. ImPlot3DPoint& operator+=(const ImPlot3DPoint& rhs);
  461. ImPlot3DPoint& operator-=(const ImPlot3DPoint& rhs);
  462. ImPlot3DPoint& operator*=(const ImPlot3DPoint& rhs);
  463. ImPlot3DPoint& operator/=(const ImPlot3DPoint& rhs);
  464. // Comparison operators
  465. bool operator==(const ImPlot3DPoint& rhs) const;
  466. bool operator!=(const ImPlot3DPoint& rhs) const;
  467. // Dot product
  468. float Dot(const ImPlot3DPoint& rhs) const;
  469. // Cross product
  470. ImPlot3DPoint Cross(const ImPlot3DPoint& rhs) const;
  471. // Get vector length
  472. float Length() const;
  473. // Get vector squared length
  474. float LengthSquared() const;
  475. // Normalize to unit length
  476. void Normalize();
  477. // Return vector normalized to unit length
  478. ImPlot3DPoint Normalized() const;
  479. // Friend binary operators to allow commutative behavior
  480. friend ImPlot3DPoint operator*(float lhs, const ImPlot3DPoint& rhs);
  481. // Check if the point is NaN
  482. bool IsNaN() const;
  483. #ifdef IMPLOT3D_POINT_CLASS_EXTRA
  484. IMPLOT3D_POINT_CLASS_EXTRA // Define additional constructors and implicit cast operators in imconfig.h to convert back and forth between your math types and ImPlot3DPoint
  485. #endif
  486. };
  487. //-----------------------------------------------------------------------------
  488. // [SECTION] ImPlot3DRay
  489. //-----------------------------------------------------------------------------
  490. struct ImPlot3DRay {
  491. ImPlot3DPoint Origin;
  492. ImPlot3DPoint Direction;
  493. };
  494. //-----------------------------------------------------------------------------
  495. // [SECTION] ImPlot3DPlane
  496. //-----------------------------------------------------------------------------
  497. struct ImPlot3DPlane {
  498. ImPlot3DPoint Point;
  499. ImPlot3DPoint Normal;
  500. };
  501. //-----------------------------------------------------------------------------
  502. // [SECTION] ImPlot3DBox
  503. //-----------------------------------------------------------------------------
  504. struct ImPlot3DBox {
  505. ImPlot3DPoint Min;
  506. ImPlot3DPoint Max;
  507. // Default constructor
  508. constexpr ImPlot3DBox() : Min(ImPlot3DPoint()), Max(ImPlot3DPoint()) {}
  509. // Constructor with two points
  510. constexpr ImPlot3DBox(const ImPlot3DPoint& min, const ImPlot3DPoint& max) : Min(min), Max(max) {}
  511. // Method to expand the box to include a point
  512. void Expand(const ImPlot3DPoint& point);
  513. // Method to check if a point is inside the box
  514. bool Contains(const ImPlot3DPoint& point) const;
  515. // Method to clip a line segment against the box
  516. bool ClipLineSegment(const ImPlot3DPoint& p0, const ImPlot3DPoint& p1, ImPlot3DPoint& p0_clipped, ImPlot3DPoint& p1_clipped) const;
  517. };
  518. //-----------------------------------------------------------------------------
  519. // [SECTION] ImPlot3DRange
  520. //-----------------------------------------------------------------------------
  521. struct ImPlot3DRange {
  522. float Min;
  523. float Max;
  524. constexpr ImPlot3DRange() : Min(0.0f), Max(0.0f) {}
  525. constexpr ImPlot3DRange(float min, float max) : Min(min), Max(max) {}
  526. void Expand(float value);
  527. bool Contains(float value) const;
  528. float Size() const { return Max - Min; }
  529. };
  530. //-----------------------------------------------------------------------------
  531. // [SECTION] ImPlot3DQuat
  532. //-----------------------------------------------------------------------------
  533. struct ImPlot3DQuat {
  534. float x, y, z, w;
  535. // Constructors
  536. constexpr ImPlot3DQuat() : x(0.0f), y(0.0f), z(0.0f), w(1.0f) {}
  537. constexpr ImPlot3DQuat(float _x, float _y, float _z, float _w) : x(_x), y(_y), z(_z), w(_w) {}
  538. ImPlot3DQuat(float _angle, const ImPlot3DPoint& _axis);
  539. // Set quaternion from two vectors
  540. static ImPlot3DQuat FromTwoVectors(const ImPlot3DPoint& v0, const ImPlot3DPoint& v1);
  541. // Get quaternion length
  542. float Length() const;
  543. // Get normalized quaternion
  544. ImPlot3DQuat Normalized() const;
  545. // Conjugate of the quaternion
  546. ImPlot3DQuat Conjugate() const;
  547. // Inverse of the quaternion
  548. ImPlot3DQuat Inverse() const;
  549. // Binary operators
  550. ImPlot3DQuat operator*(const ImPlot3DQuat& rhs) const;
  551. // Normalize the quaternion in place
  552. ImPlot3DQuat& Normalize();
  553. // Rotate a 3D point using the quaternion
  554. ImPlot3DPoint operator*(const ImPlot3DPoint& point) const;
  555. // Comparison operators
  556. bool operator==(const ImPlot3DQuat& rhs) const;
  557. bool operator!=(const ImPlot3DQuat& rhs) const;
  558. // Interpolate between two quaternions
  559. static ImPlot3DQuat Slerp(const ImPlot3DQuat& q1, const ImPlot3DQuat& q2, float t);
  560. // Get quaternion dot product
  561. float Dot(const ImPlot3DQuat& rhs) const;
  562. #ifdef IMPLOT3D_QUAT_CLASS_EXTRA
  563. IMPLOT3D_QUAT_CLASS_EXTRA // Define additional constructors and implicit cast operators in imconfig.h to convert back and forth between your math types and ImPlot3DQuat
  564. #endif
  565. };
  566. //-----------------------------------------------------------------------------
  567. // [SECTION] ImPlot3DStyle
  568. //-----------------------------------------------------------------------------
  569. struct ImPlot3DStyle {
  570. // Item style
  571. float LineWeight; // Line weight in pixels
  572. int Marker; // Default marker type (ImPlot3DMarker_None)
  573. float MarkerSize; // Marker size in pixels (roughly the marker's "radius")
  574. float MarkerWeight; // Marker outline weight in pixels
  575. float FillAlpha; // Alpha modifier applied to plot fills
  576. // Plot style
  577. ImVec2 PlotDefaultSize;
  578. ImVec2 PlotMinSize;
  579. ImVec2 PlotPadding;
  580. ImVec2 LabelPadding;
  581. // Legend style
  582. ImVec2 LegendPadding; // Legend padding from plot edges
  583. ImVec2 LegendInnerPadding; // Legend inner padding from legend edges
  584. ImVec2 LegendSpacing; // Spacing between legend entries
  585. // Colors
  586. ImVec4 Colors[ImPlot3DCol_COUNT];
  587. // Colormap
  588. ImPlot3DColormap Colormap; // The current colormap. Set this to either an ImPlot3DColormap_ enum or an index returned by AddColormap
  589. // Constructor
  590. IMPLOT3D_API ImPlot3DStyle();
  591. };
  592. //-----------------------------------------------------------------------------
  593. // [SECTION] Callbacks
  594. //-----------------------------------------------------------------------------
  595. // Callback signature for axis tick label formatter
  596. typedef int (*ImPlot3DFormatter)(float value, char* buff, int size, void* user_data);
  597. //-----------------------------------------------------------------------------
  598. // [SECTION] Meshes
  599. //-----------------------------------------------------------------------------
  600. namespace ImPlot3D {
  601. // Cube
  602. constexpr int CUBE_VTX_COUNT = 8; // Number of cube vertices
  603. constexpr int CUBE_IDX_COUNT = 36; // Number of cube indices (12 triangles)
  604. extern ImPlot3DPoint cube_vtx[CUBE_VTX_COUNT]; // Cube vertices
  605. extern unsigned int cube_idx[CUBE_IDX_COUNT]; // Cube indices
  606. // Sphere
  607. constexpr int SPHERE_VTX_COUNT = 162; // Number of sphere vertices for 128 triangles
  608. constexpr int SPHERE_IDX_COUNT = 960; // Number of sphere indices (128 triangles)
  609. extern ImPlot3DPoint sphere_vtx[SPHERE_VTX_COUNT]; // Sphere vertices
  610. extern unsigned int sphere_idx[SPHERE_IDX_COUNT]; // Sphere indices
  611. // Duck (Rubber Duck by Poly by Google [CC-BY] via Poly Pizza)
  612. constexpr int DUCK_VTX_COUNT = 254; // Number of duck vertices
  613. constexpr int DUCK_IDX_COUNT = 1428; // Number of duck indices
  614. extern ImPlot3DPoint duck_vtx[DUCK_VTX_COUNT]; // Duck vertices
  615. extern unsigned int duck_idx[DUCK_IDX_COUNT]; // Duck indices
  616. } // namespace ImPlot3D
  617. #endif // #ifndef IMGUI_DISABLE