implot3d_internal.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. //--------------------------------------------------
  2. // ImPlot3D v0.1
  3. // implot3d_internal.h
  4. // Date: 2024-11-17
  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] Constants
  15. // [SECTION] Generic Helpers
  16. // [SECTION] Forward Declarations
  17. // [SECTION] Callbacks
  18. // [SECTION] Structs
  19. // [SECTION] Context Pointer
  20. // [SECTION] Context Utils
  21. // [SECTION] Style Utils
  22. // [SECTION] Item Utils
  23. // [SECTION] Plot Utils
  24. // [SECTION] Setup Utils
  25. // [SECTION] Formatter
  26. // [SECTION] Locator
  27. #pragma once
  28. #ifndef IMPLOT3D_VERSION
  29. #include "implot3d.h"
  30. #endif
  31. #ifndef IMGUI_DISABLE
  32. #include "imgui_internal.h"
  33. //-----------------------------------------------------------------------------
  34. // [SECTION] Constants
  35. //-----------------------------------------------------------------------------
  36. // Default label format for axis labels
  37. #define IMPLOT3D_LABEL_FORMAT "%g"
  38. // Max character size for tick labels
  39. #define IMPLOT3D_LABEL_MAX_SIZE 32
  40. //-----------------------------------------------------------------------------
  41. // [SECTION] Generic Helpers
  42. //-----------------------------------------------------------------------------
  43. namespace ImPlot3D {
  44. // Computes the common (base-10) logarithm
  45. static inline float ImLog10(float x) { return log10f(x); }
  46. // Returns true if flag is set
  47. template <typename TSet, typename TFlag>
  48. static inline bool ImHasFlag(TSet set, TFlag flag) { return (set & flag) == flag; }
  49. // Flips a flag in a flagset
  50. template <typename TSet, typename TFlag>
  51. static inline void ImFlipFlag(TSet& set, TFlag flag) { ImHasFlag(set, flag) ? set &= ~flag : set |= flag; }
  52. template <typename T>
  53. static inline T ImRemap01(T x, T x0, T x1) { return (x - x0) / (x1 - x0); }
  54. // Returns true if val is NAN
  55. static inline bool ImNan(float val) { return isnan(val); }
  56. // Returns true if val is NAN or INFINITY
  57. static inline bool ImNanOrInf(float val) { return !(val >= -FLT_MAX && val <= FLT_MAX) || ImNan(val); }
  58. // Turns NANs to 0s
  59. static inline double ImConstrainNan(float val) { return ImNan(val) ? 0 : val; }
  60. // Turns infinity to floating point maximums
  61. static inline double ImConstrainInf(double val) { return val >= FLT_MAX ? FLT_MAX : val <= -FLT_MAX ? -FLT_MAX
  62. : val; }
  63. // True if two numbers are approximately equal using units in the last place.
  64. static inline bool ImAlmostEqual(double v1, double v2, int ulp = 2) { return ImAbs(v1 - v2) < FLT_EPSILON * ImAbs(v1 + v2) * ulp || ImAbs(v1 - v2) < FLT_MIN; }
  65. // Set alpha channel of 32-bit color from float in range [0.0 1.0]
  66. static inline ImU32 ImAlphaU32(ImU32 col, float alpha) {
  67. return col & ~((ImU32)((1.0f - alpha) * 255) << IM_COL32_A_SHIFT);
  68. }
  69. // Mix color a and b by factor s in [0 256]
  70. static inline ImU32 ImMixU32(ImU32 a, ImU32 b, ImU32 s) {
  71. #ifdef IMPLOT3D_MIX64
  72. const ImU32 af = 256 - s;
  73. const ImU32 bf = s;
  74. const ImU64 al = (a & 0x00ff00ff) | (((ImU64)(a & 0xff00ff00)) << 24);
  75. const ImU64 bl = (b & 0x00ff00ff) | (((ImU64)(b & 0xff00ff00)) << 24);
  76. const ImU64 mix = (al * af + bl * bf);
  77. return ((mix >> 32) & 0xff00ff00) | ((mix & 0xff00ff00) >> 8);
  78. #else
  79. const ImU32 af = 256 - s;
  80. const ImU32 bf = s;
  81. const ImU32 al = (a & 0x00ff00ff);
  82. const ImU32 ah = (a & 0xff00ff00) >> 8;
  83. const ImU32 bl = (b & 0x00ff00ff);
  84. const ImU32 bh = (b & 0xff00ff00) >> 8;
  85. const ImU32 ml = (al * af + bl * bf);
  86. const ImU32 mh = (ah * af + bh * bf);
  87. return (mh & 0xff00ff00) | ((ml & 0xff00ff00) >> 8);
  88. #endif
  89. }
  90. } // namespace ImPlot3D
  91. //-----------------------------------------------------------------------------
  92. // [SECTION] Forward Declarations
  93. //-----------------------------------------------------------------------------
  94. struct ImPlot3DTicker;
  95. //------------------------------------------------------------------------------
  96. // [SECTION] Callbacks
  97. //------------------------------------------------------------------------------
  98. typedef void (*ImPlot3DLocator)(ImPlot3DTicker& ticker, const ImPlot3DRange& range, ImPlot3DFormatter formatter, void* formatter_data);
  99. //-----------------------------------------------------------------------------
  100. // [SECTION] Structs
  101. //-----------------------------------------------------------------------------
  102. struct ImDrawList3D {
  103. ImVector<ImDrawIdx> IdxBuffer; // Index buffer
  104. ImVector<ImDrawVert> VtxBuffer; // Vertex buffer
  105. ImVector<float> ZBuffer; // Z buffer. Depth value for each triangle
  106. unsigned int _VtxCurrentIdx; // [Internal] current vertex index
  107. ImDrawVert* _VtxWritePtr; // [Internal] point within VtxBuffer.Data after each add command (to avoid using the ImVector<> operators too much)
  108. ImDrawIdx* _IdxWritePtr; // [Internal] point within IdxBuffer.Data after each add command (to avoid using the ImVector<> operators too much)
  109. float* _ZWritePtr; // [Internal] point within ZBuffer.Data after each add command (to avoid using the ImVector<> operators too much)
  110. ImDrawListFlags _Flags; // [Internal] draw list flags
  111. ImDrawListSharedData* _SharedData; // [Internal] shared draw list data
  112. ImDrawList3D() {
  113. memset(this, 0, sizeof(*this));
  114. }
  115. void PrimReserve(int idx_count, int vtx_count);
  116. void PrimUnreserve(int idx_count, int vtx_count);
  117. void SortedMoveToImGuiDrawList();
  118. constexpr static unsigned int MaxIdx() { return sizeof(ImDrawIdx) == 2 ? 65535 : 4294967295; }
  119. };
  120. struct ImPlot3DNextItemData {
  121. ImVec4 Colors[4]; // ImPlot3DCol_Line, ImPlot3DCol_Fill, ImPlot3DCol_MarkerOutline, ImPlot3DCol_MarkerFill,
  122. float LineWeight;
  123. ImPlot3DMarker Marker;
  124. float MarkerSize;
  125. float MarkerWeight;
  126. float FillAlpha;
  127. bool RenderLine;
  128. bool RenderFill;
  129. bool RenderMarkerLine;
  130. bool RenderMarkerFill;
  131. bool IsAutoFill;
  132. bool IsAutoLine;
  133. bool Hidden;
  134. ImPlot3DNextItemData() { Reset(); }
  135. void Reset() {
  136. for (int i = 0; i < 4; i++)
  137. Colors[i] = IMPLOT3D_AUTO_COL;
  138. LineWeight = IMPLOT3D_AUTO;
  139. Marker = IMPLOT3D_AUTO;
  140. MarkerSize = IMPLOT3D_AUTO;
  141. MarkerWeight = IMPLOT3D_AUTO;
  142. FillAlpha = IMPLOT3D_AUTO;
  143. RenderLine = false;
  144. RenderFill = false;
  145. RenderMarkerLine = true;
  146. RenderMarkerFill = true;
  147. IsAutoFill = true;
  148. IsAutoLine = true;
  149. Hidden = false;
  150. }
  151. };
  152. // Colormap data storage
  153. struct ImPlot3DColormapData {
  154. ImVector<ImU32> Keys;
  155. ImVector<int> KeyCounts;
  156. ImVector<int> KeyOffsets;
  157. ImVector<ImU32> Tables;
  158. ImVector<int> TableSizes;
  159. ImVector<int> TableOffsets;
  160. ImGuiTextBuffer Text;
  161. ImVector<int> TextOffsets;
  162. ImVector<bool> Quals;
  163. ImGuiStorage Map;
  164. int Count;
  165. ImPlot3DColormapData() { Count = 0; }
  166. int Append(const char* name, const ImU32* keys, int count, bool qual) {
  167. if (GetIndex(name) != -1)
  168. return -1;
  169. KeyOffsets.push_back(Keys.size());
  170. KeyCounts.push_back(count);
  171. Keys.reserve(Keys.size() + count);
  172. for (int i = 0; i < count; ++i)
  173. Keys.push_back(keys[i]);
  174. TextOffsets.push_back(Text.size());
  175. Text.append(name, name + strlen(name) + 1);
  176. Quals.push_back(qual);
  177. ImGuiID id = ImHashStr(name);
  178. int idx = Count++;
  179. Map.SetInt(id, idx);
  180. _AppendTable(idx);
  181. return idx;
  182. }
  183. void _AppendTable(ImPlot3DColormap cmap) {
  184. int key_count = GetKeyCount(cmap);
  185. const ImU32* keys = GetKeys(cmap);
  186. int off = Tables.size();
  187. TableOffsets.push_back(off);
  188. if (IsQual(cmap)) {
  189. Tables.reserve(key_count);
  190. for (int i = 0; i < key_count; ++i)
  191. Tables.push_back(keys[i]);
  192. TableSizes.push_back(key_count);
  193. } else {
  194. int max_size = 255 * (key_count - 1) + 1;
  195. Tables.reserve(off + max_size);
  196. // ImU32 last = keys[0];
  197. // Tables.push_back(last);
  198. // int n = 1;
  199. for (int i = 0; i < key_count - 1; ++i) {
  200. for (int s = 0; s < 255; ++s) {
  201. ImU32 a = keys[i];
  202. ImU32 b = keys[i + 1];
  203. ImU32 c = ImPlot3D::ImMixU32(a, b, s);
  204. // if (c != last) {
  205. Tables.push_back(c);
  206. // last = c;
  207. // n++;
  208. // }
  209. }
  210. }
  211. ImU32 c = keys[key_count - 1];
  212. // if (c != last) {
  213. Tables.push_back(c);
  214. // n++;
  215. // }
  216. // TableSizes.push_back(n);
  217. TableSizes.push_back(max_size);
  218. }
  219. }
  220. void RebuildTables() {
  221. Tables.resize(0);
  222. TableSizes.resize(0);
  223. TableOffsets.resize(0);
  224. for (int i = 0; i < Count; ++i)
  225. _AppendTable(i);
  226. }
  227. inline bool IsQual(ImPlot3DColormap cmap) const { return Quals[cmap]; }
  228. inline const char* GetName(ImPlot3DColormap cmap) const { return cmap < Count ? Text.Buf.Data + TextOffsets[cmap] : nullptr; }
  229. inline ImPlot3DColormap GetIndex(const char* name) const {
  230. ImGuiID key = ImHashStr(name);
  231. return Map.GetInt(key, -1);
  232. }
  233. inline const ImU32* GetKeys(ImPlot3DColormap cmap) const { return &Keys[KeyOffsets[cmap]]; }
  234. inline int GetKeyCount(ImPlot3DColormap cmap) const { return KeyCounts[cmap]; }
  235. inline ImU32 GetKeyColor(ImPlot3DColormap cmap, int idx) const { return Keys[KeyOffsets[cmap] + idx]; }
  236. inline void SetKeyColor(ImPlot3DColormap cmap, int idx, ImU32 value) {
  237. Keys[KeyOffsets[cmap] + idx] = value;
  238. RebuildTables();
  239. }
  240. inline const ImU32* GetTable(ImPlot3DColormap cmap) const { return &Tables[TableOffsets[cmap]]; }
  241. inline int GetTableSize(ImPlot3DColormap cmap) const { return TableSizes[cmap]; }
  242. inline ImU32 GetTableColor(ImPlot3DColormap cmap, int idx) const { return Tables[TableOffsets[cmap] + idx]; }
  243. inline ImU32 LerpTable(ImPlot3DColormap cmap, float t) const {
  244. int off = TableOffsets[cmap];
  245. int siz = TableSizes[cmap];
  246. int idx = Quals[cmap] ? ImClamp((int)(siz * t), 0, siz - 1) : (int)((siz - 1) * t + 0.5f);
  247. return Tables[off + idx];
  248. }
  249. };
  250. // State information for plot items
  251. struct ImPlot3DItem {
  252. ImGuiID ID;
  253. ImU32 Color;
  254. int NameOffset;
  255. bool Show;
  256. bool LegendHovered;
  257. bool SeenThisFrame;
  258. ImPlot3DItem() {
  259. ID = 0;
  260. Color = IM_COL32_WHITE;
  261. NameOffset = -1;
  262. Show = true;
  263. LegendHovered = false;
  264. SeenThisFrame = false;
  265. }
  266. ~ImPlot3DItem() { ID = 0; }
  267. };
  268. // Holds legend state
  269. struct ImPlot3DLegend {
  270. ImPlot3DLegendFlags Flags;
  271. ImPlot3DLegendFlags PreviousFlags;
  272. ImPlot3DLocation Location;
  273. ImPlot3DLocation PreviousLocation;
  274. ImVector<int> Indices;
  275. ImGuiTextBuffer Labels;
  276. ImRect Rect;
  277. bool Hovered;
  278. bool Held;
  279. ImPlot3DLegend() {
  280. PreviousFlags = Flags = ImPlot3DLegendFlags_None;
  281. Hovered = Held = false;
  282. PreviousLocation = Location = ImPlot3DLocation_NorthWest;
  283. }
  284. void Reset() {
  285. Indices.shrink(0);
  286. Labels.Buf.shrink(0);
  287. }
  288. };
  289. // Holds items
  290. struct ImPlot3DItemGroup {
  291. ImPool<ImPlot3DItem> ItemPool;
  292. ImPlot3DLegend Legend;
  293. int ColormapIdx;
  294. ImPlot3DItemGroup() {
  295. ColormapIdx = 0;
  296. }
  297. int GetItemCount() const { return ItemPool.GetBufSize(); }
  298. ImGuiID GetItemID(const char* label_id) { return ImGui::GetID(label_id); }
  299. ImPlot3DItem* GetItem(ImGuiID id) { return ItemPool.GetByKey(id); }
  300. ImPlot3DItem* GetItem(const char* label_id) { return GetItem(GetItemID(label_id)); }
  301. ImPlot3DItem* GetOrAddItem(ImGuiID id) { return ItemPool.GetOrAddByKey(id); }
  302. ImPlot3DItem* GetItemByIndex(int i) { return ItemPool.GetByIndex(i); }
  303. int GetItemIndex(ImPlot3DItem* item) { return ItemPool.GetIndex(item); }
  304. int GetLegendCount() const { return Legend.Indices.size(); }
  305. ImPlot3DItem* GetLegendItem(int i) { return ItemPool.GetByIndex(Legend.Indices[i]); }
  306. const char* GetLegendLabel(int i) { return Legend.Labels.Buf.Data + GetLegendItem(i)->NameOffset; }
  307. void Reset() {
  308. ItemPool.Clear();
  309. Legend.Reset();
  310. ColormapIdx = 0;
  311. }
  312. };
  313. // Tick mark info
  314. struct ImPlot3DTick {
  315. float PlotPos;
  316. bool Major;
  317. bool ShowLabel;
  318. ImVec2 LabelSize;
  319. int TextOffset;
  320. int Idx;
  321. ImPlot3DTick(double value, bool major, bool show_label) {
  322. PlotPos = value;
  323. Major = major;
  324. ShowLabel = show_label;
  325. TextOffset = -1;
  326. }
  327. };
  328. // Collection of ticks
  329. struct ImPlot3DTicker {
  330. ImVector<ImPlot3DTick> Ticks;
  331. ImGuiTextBuffer TextBuffer;
  332. ImPlot3DTicker() {
  333. Reset();
  334. }
  335. ImPlot3DTick& AddTick(double value, bool major, bool show_label, const char* label) {
  336. ImPlot3DTick tick(value, major, show_label);
  337. if (show_label && label != nullptr) {
  338. tick.TextOffset = TextBuffer.size();
  339. TextBuffer.append(label, label + strlen(label) + 1);
  340. tick.LabelSize = ImGui::CalcTextSize(TextBuffer.Buf.Data + tick.TextOffset);
  341. }
  342. return AddTick(tick);
  343. }
  344. ImPlot3DTick& AddTick(double value, bool major, bool show_label, ImPlot3DFormatter formatter, void* data) {
  345. ImPlot3DTick tick(value, major, show_label);
  346. if (show_label && formatter != nullptr) {
  347. char buff[IMPLOT3D_LABEL_MAX_SIZE];
  348. tick.TextOffset = TextBuffer.size();
  349. formatter(tick.PlotPos, buff, sizeof(buff), data);
  350. TextBuffer.append(buff, buff + strlen(buff) + 1);
  351. tick.LabelSize = ImGui::CalcTextSize(TextBuffer.Buf.Data + tick.TextOffset);
  352. }
  353. return AddTick(tick);
  354. }
  355. inline ImPlot3DTick& AddTick(ImPlot3DTick tick) {
  356. tick.Idx = Ticks.size();
  357. Ticks.push_back(tick);
  358. return Ticks.back();
  359. }
  360. const char* GetText(int idx) const {
  361. return TextBuffer.Buf.Data + Ticks[idx].TextOffset;
  362. }
  363. const char* GetText(const ImPlot3DTick& tick) const {
  364. return GetText(tick.Idx);
  365. }
  366. void Reset() {
  367. Ticks.shrink(0);
  368. TextBuffer.Buf.shrink(0);
  369. }
  370. int TickCount() const {
  371. return Ticks.Size;
  372. }
  373. };
  374. // Holds axis information
  375. struct ImPlot3DAxis {
  376. ImPlot3DAxisFlags Flags;
  377. ImPlot3DAxisFlags PreviousFlags;
  378. ImPlot3DRange Range;
  379. ImPlot3DCond RangeCond;
  380. ImGuiTextBuffer Label;
  381. // Ticks
  382. ImPlot3DTicker Ticker;
  383. ImPlot3DFormatter Formatter;
  384. void* FormatterData;
  385. ImPlot3DLocator Locator;
  386. // Fit data
  387. bool FitThisFrame;
  388. ImPlot3DRange FitExtents;
  389. // User input
  390. bool Held;
  391. // Constructor
  392. ImPlot3DAxis() {
  393. PreviousFlags = Flags = ImPlot3DAxisFlags_None;
  394. // Range
  395. Range.Min = 0.0f;
  396. Range.Max = 1.0f;
  397. RangeCond = ImPlot3DCond_None;
  398. // Ticks
  399. Formatter = nullptr;
  400. FormatterData = nullptr;
  401. Locator = nullptr;
  402. // Fit data
  403. FitThisFrame = true;
  404. FitExtents.Min = HUGE_VAL;
  405. FitExtents.Max = -HUGE_VAL;
  406. // User input
  407. Held = false;
  408. }
  409. inline void SetRange(double v1, double v2) {
  410. Range.Min = ImMin(v1, v2);
  411. Range.Max = ImMax(v1, v2);
  412. }
  413. inline bool SetMin(double _min, bool force = false) {
  414. if (!force && IsLockedMin())
  415. return false;
  416. _min = ImPlot3D::ImConstrainNan(ImPlot3D::ImConstrainInf(_min));
  417. if (_min >= Range.Max)
  418. return false;
  419. Range.Min = _min;
  420. return true;
  421. }
  422. inline bool SetMax(double _max, bool force = false) {
  423. if (!force && IsLockedMax())
  424. return false;
  425. _max = ImPlot3D::ImConstrainNan(ImPlot3D::ImConstrainInf(_max));
  426. if (_max <= Range.Min)
  427. return false;
  428. Range.Max = _max;
  429. return true;
  430. }
  431. inline bool IsRangeLocked() const { return RangeCond == ImPlot3DCond_Always; }
  432. inline bool IsLockedMin() const { return IsRangeLocked() || ImPlot3D::ImHasFlag(Flags, ImPlot3DAxisFlags_LockMin); }
  433. inline bool IsLockedMax() const { return IsRangeLocked() || ImPlot3D::ImHasFlag(Flags, ImPlot3DAxisFlags_LockMax); }
  434. inline bool IsLocked() const { return IsLockedMin() && IsLockedMax(); }
  435. inline void SetLabel(const char* label) {
  436. Label.Buf.shrink(0);
  437. if (label && ImGui::FindRenderedTextEnd(label, nullptr) != label)
  438. Label.append(label, label + strlen(label) + 1);
  439. }
  440. inline const char* GetLabel() const { return Label.Buf.Data; }
  441. bool HasLabel() const;
  442. bool HasGridLines() const;
  443. bool HasTickLabels() const;
  444. bool HasTickMarks() const;
  445. bool IsAutoFitting() const;
  446. void ExtendFit(float value);
  447. void ApplyFit();
  448. float PlotToNDC(float value) const;
  449. float NDCToPlot(float value) const;
  450. };
  451. // Holds plot state information that must persist after EndPlot
  452. struct ImPlot3DPlot {
  453. ImGuiID ID;
  454. ImPlot3DFlags Flags;
  455. ImPlot3DFlags PreviousFlags;
  456. ImGuiTextBuffer Title;
  457. bool JustCreated;
  458. bool Initialized;
  459. // Bounding rectangles
  460. ImRect FrameRect; // Outermost bounding rectangle that encapsulates whole the plot/title/padding/etc
  461. ImRect CanvasRect; // Frame rectangle reduced by padding
  462. ImRect PlotRect; // Bounding rectangle for the actual plot area
  463. // Rotation & Axes
  464. ImPlot3DQuat Rotation;
  465. ImPlot3DAxis Axes[3];
  466. // Animation
  467. float AnimationTime; // Remaining animation time
  468. ImPlot3DQuat RotationAnimationEnd; // End rotation for animation
  469. // User input
  470. bool SetupLocked;
  471. bool Hovered;
  472. bool Held;
  473. int HeldEdgeIdx; // Index of the edge being held
  474. int HeldPlaneIdx; // Index of the plane being held
  475. // Fit data
  476. bool FitThisFrame;
  477. // Items
  478. ImPlot3DItemGroup Items;
  479. ImPlot3DItem* CurrentItem;
  480. // 3D draw list
  481. ImDrawList3D DrawList;
  482. // Misc
  483. bool ContextClick; // True if context button was clicked (to distinguish from double click)
  484. bool OpenContextThisFrame;
  485. ImPlot3DPlot() {
  486. PreviousFlags = Flags = ImPlot3DFlags_None;
  487. JustCreated = true;
  488. Initialized = false;
  489. Rotation = ImPlot3DQuat(0.0f, 0.0f, 0.0f, 1.0f);
  490. for (int i = 0; i < 3; i++)
  491. Axes[i] = ImPlot3DAxis();
  492. AnimationTime = 0.0f;
  493. RotationAnimationEnd = Rotation;
  494. SetupLocked = false;
  495. Hovered = Held = false;
  496. HeldEdgeIdx = -1;
  497. HeldPlaneIdx = -1;
  498. FitThisFrame = true;
  499. CurrentItem = nullptr;
  500. ContextClick = false;
  501. OpenContextThisFrame = false;
  502. }
  503. inline void SetTitle(const char* title) {
  504. Title.Buf.shrink(0);
  505. if (title && ImGui::FindRenderedTextEnd(title, nullptr) != title)
  506. Title.append(title, title + strlen(title) + 1);
  507. }
  508. inline bool HasTitle() const { return !Title.empty() && !ImPlot3D::ImHasFlag(Flags, ImPlot3DFlags_NoTitle); }
  509. inline const char* GetTitle() const { return Title.Buf.Data; }
  510. void ExtendFit(const ImPlot3DPoint& point);
  511. ImPlot3DPoint RangeMin() const;
  512. ImPlot3DPoint RangeMax() const;
  513. ImPlot3DPoint RangeCenter() const;
  514. void SetRange(const ImPlot3DPoint& min, const ImPlot3DPoint& max);
  515. };
  516. struct ImPlot3DContext {
  517. ImPool<ImPlot3DPlot> Plots;
  518. ImPlot3DPlot* CurrentPlot;
  519. ImPlot3DItemGroup* CurrentItems;
  520. ImPlot3DNextItemData NextItemData;
  521. ImPlot3DStyle Style;
  522. ImVector<ImGuiColorMod> ColorModifiers;
  523. ImVector<ImGuiStyleMod> StyleModifiers;
  524. ImVector<ImPlot3DColormap> ColormapModifiers;
  525. ImPlot3DColormapData ColormapData;
  526. };
  527. //-----------------------------------------------------------------------------
  528. // [SECTION] Context Pointer
  529. //-----------------------------------------------------------------------------
  530. namespace ImPlot3D {
  531. #ifndef GImPlot3D
  532. extern IMPLOT3D_API ImPlot3DContext* GImPlot3D; // Current context pointer
  533. #endif
  534. //-----------------------------------------------------------------------------
  535. // [SECTION] Context Utils
  536. //-----------------------------------------------------------------------------
  537. IMPLOT3D_API void InitializeContext(ImPlot3DContext* ctx); // Initialize ImPlot3DContext
  538. IMPLOT3D_API void ResetContext(ImPlot3DContext* ctx); // Reset ImPlot3DContext
  539. //-----------------------------------------------------------------------------
  540. // [SECTION] Style Utils
  541. //-----------------------------------------------------------------------------
  542. IMPLOT3D_API bool IsColorAuto(const ImVec4& col);
  543. IMPLOT3D_API bool IsColorAuto(ImPlot3DCol idx);
  544. IMPLOT3D_API ImVec4 GetAutoColor(ImPlot3DCol idx);
  545. IMPLOT3D_API const char* GetStyleColorName(ImPlot3DCol idx);
  546. // Get styling data for next item (call between BeginItem/EndItem)
  547. IMPLOT3D_API const ImPlot3DNextItemData& GetItemData();
  548. // Returns a color from the Color map given an index >= 0 (modulo will be performed)
  549. IMPLOT3D_API ImU32 GetColormapColorU32(int idx, ImPlot3DColormap cmap);
  550. // Returns the next unused colormap color and advances the colormap. Can be used to skip colors if desired
  551. IMPLOT3D_API ImU32 NextColormapColorU32();
  552. //-----------------------------------------------------------------------------
  553. // [SECTION] Item Utils
  554. //-----------------------------------------------------------------------------
  555. IMPLOT3D_API bool BeginItem(const char* label_id, ImPlot3DItemFlags flags = 0, ImPlot3DCol recolor_from = IMPLOT3D_AUTO);
  556. IMPLOT3D_API void EndItem();
  557. // Register or get an existing item from the current plot
  558. IMPLOT3D_API ImPlot3DItem* RegisterOrGetItem(const char* label_id, ImPlot3DItemFlags flags, bool* just_created = nullptr);
  559. // Busts the cache for every item for every plot in the current context
  560. IMPLOT3D_API void BustItemCache();
  561. // TODO move to another place
  562. IMPLOT3D_API void AddTextRotated(ImDrawList* draw_list, ImVec2 pos, float angle, ImU32 col, const char* text_begin, const char* text_end = nullptr);
  563. //-----------------------------------------------------------------------------
  564. // [SECTION] Plot Utils
  565. //-----------------------------------------------------------------------------
  566. // Gets the current plot from the current ImPlot3DContext
  567. IMPLOT3D_API ImPlot3DPlot* GetCurrentPlot();
  568. // Busts the cache for every plot in the current context
  569. IMPLOT3D_API void BustPlotCache();
  570. IMPLOT3D_API ImVec2 GetFramePos(); // Get the current frame position (top-left) in pixels
  571. IMPLOT3D_API ImVec2 GetFrameSize(); // Get the current frame size in pixels
  572. // Convert a position in the current plot's coordinate system to the current plot's normalized device coordinate system (NDC)
  573. // When the cube aspect ratio is [1,1,1], the NDC varies from [-0.5, 0.5] in each axis
  574. IMPLOT3D_API ImPlot3DPoint PlotToNDC(const ImPlot3DPoint& point);
  575. IMPLOT3D_API ImPlot3DPoint NDCToPlot(const ImPlot3DPoint& point);
  576. // Convert a position in the current plot's NDC to pixels
  577. IMPLOT3D_API ImVec2 NDCToPixels(const ImPlot3DPoint& point);
  578. // Convert a pixel coordinate to a ray in the NDC
  579. IMPLOT3D_API ImPlot3DRay PixelsToNDCRay(const ImVec2& pix);
  580. // Convert a ray in the NDC to a ray in the current plot's coordinate system
  581. IMPLOT3D_API ImPlot3DRay NDCRayToPlotRay(const ImPlot3DRay& ray);
  582. //-----------------------------------------------------------------------------
  583. // [SECTION] Setup Utils
  584. //-----------------------------------------------------------------------------
  585. IMPLOT3D_API void SetupLock();
  586. //-----------------------------------------------------------------------------
  587. // [SECTION] Formatter
  588. //-----------------------------------------------------------------------------
  589. int Formatter_Default(float value, char* buff, int size, void* data);
  590. //------------------------------------------------------------------------------
  591. // [SECTION] Locator
  592. //------------------------------------------------------------------------------
  593. void Locator_Default(ImPlot3DTicker& ticker, const ImPlot3DRange& range, ImPlot3DFormatter formatter, void* formatter_data);
  594. } // namespace ImPlot3D
  595. #endif // #ifndef IMGUI_DISABLE