implot3d_items.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  1. //--------------------------------------------------
  2. // ImPlot3D v0.1
  3. // implot3d_items.cpp
  4. // Date: 2024-11-26
  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] Includes
  15. // [SECTION] Macros & Defines
  16. // [SECTION] Template instantiation utility
  17. // [SECTION] Item Utils
  18. // [SECTION] Draw Utils
  19. // [SECTION] Renderers
  20. // [SECTION] Indexers
  21. // [SECTION] Getters
  22. // [SECTION] RenderPrimitives
  23. // [SECTION] Markers
  24. // [SECTION] PlotScatter
  25. // [SECTION] PlotLine
  26. // [SECTION] PlotTriangle
  27. // [SECTION] PlotText
  28. //-----------------------------------------------------------------------------
  29. // [SECTION] Includes
  30. //-----------------------------------------------------------------------------
  31. #ifndef IMGUI_DEFINE_MATH_OPERATORS
  32. #define IMGUI_DEFINE_MATH_OPERATORS
  33. #endif
  34. #include "implot3d.h"
  35. #include "implot3d_internal.h"
  36. #ifndef IMGUI_DISABLE
  37. //-----------------------------------------------------------------------------
  38. // [SECTION] Macros & Defines
  39. //-----------------------------------------------------------------------------
  40. #define SQRT_1_2 0.70710678118f
  41. #define SQRT_3_2 0.86602540378f
  42. // clang-format off
  43. #ifndef IMPLOT3D_NO_FORCE_INLINE
  44. #ifdef _MSC_VER
  45. #define IMPLOT3D_INLINE __forceinline
  46. #elif defined(__GNUC__)
  47. #define IMPLOT3D_INLINE inline __attribute__((__always_inline__))
  48. #elif defined(__CLANG__)
  49. #if __has_attribute(__always_inline__)
  50. #define IMPLOT3D_INLINE inline __attribute__((__always_inline__))
  51. #else
  52. #define IMPLOT3D_INLINE inline
  53. #endif
  54. #else
  55. #define IMPLOT3D_INLINE inline
  56. #endif
  57. #else
  58. #define IMPLOT3D_INLINE inline
  59. #endif
  60. // clang-format on
  61. #define IMPLOT3D_NORMALIZE2F(VX, VY) \
  62. do { \
  63. float d2 = VX * VX + VY * VY; \
  64. if (d2 > 0.0f) { \
  65. float inv_len = ImRsqrt(d2); \
  66. VX *= inv_len; \
  67. VY *= inv_len; \
  68. } \
  69. } while (0)
  70. IMPLOT3D_INLINE void GetLineRenderProps(const ImDrawList3D& draw_list_3d, float& half_weight, ImVec2& tex_uv0, ImVec2& tex_uv1) {
  71. const bool aa = ImPlot3D::ImHasFlag(draw_list_3d._Flags, ImDrawListFlags_AntiAliasedLines) &&
  72. ImPlot3D::ImHasFlag(draw_list_3d._Flags, ImDrawListFlags_AntiAliasedLinesUseTex);
  73. if (aa) {
  74. ImVec4 tex_uvs = draw_list_3d._SharedData->TexUvLines[(int)(half_weight * 2)];
  75. tex_uv0 = ImVec2(tex_uvs.x, tex_uvs.y);
  76. tex_uv1 = ImVec2(tex_uvs.z, tex_uvs.w);
  77. half_weight += 1;
  78. } else {
  79. tex_uv0 = tex_uv1 = draw_list_3d._SharedData->TexUvWhitePixel;
  80. }
  81. }
  82. //-----------------------------------------------------------------------------
  83. // [SECTION] Template instantiation utility
  84. //-----------------------------------------------------------------------------
  85. // By default, templates are instantiated for `float`, `double`, and for the following integer types, which are defined in imgui.h:
  86. // signed char ImS8; // 8-bit signed integer
  87. // unsigned char ImU8; // 8-bit unsigned integer
  88. // signed short ImS16; // 16-bit signed integer
  89. // unsigned short ImU16; // 16-bit unsigned integer
  90. // signed int ImS32; // 32-bit signed integer == int
  91. // unsigned int ImU32; // 32-bit unsigned integer
  92. // signed long long ImS64; // 64-bit signed integer
  93. // unsigned long long ImU64; // 64-bit unsigned integer
  94. // (note: this list does *not* include `long`, `unsigned long` and `long double`)
  95. //
  96. // You can customize the supported types by defining IMPLOT3D_CUSTOM_NUMERIC_TYPES at compile time to define your own type list.
  97. // As an example, you could use the compile time define given by the line below in order to support only float and double.
  98. // -DIMPLOT3D_CUSTOM_NUMERIC_TYPES="(float)(double)"
  99. // In order to support all known C++ types, use:
  100. // -DIMPLOT3D_CUSTOM_NUMERIC_TYPES="(signed char)(unsigned char)(signed short)(unsigned short)(signed int)(unsigned int)(signed long)(unsigned long)(signed long long)(unsigned long long)(float)(double)(long double)"
  101. #ifdef IMPLOT3D_CUSTOM_NUMERIC_TYPES
  102. #define IMPLOT3D_NUMERIC_TYPES IMPLOT3D_CUSTOM_NUMERIC_TYPES
  103. #else
  104. #define IMPLOT3D_NUMERIC_TYPES (ImS8)(ImU8)(ImS16)(ImU16)(ImS32)(ImU32)(ImS64)(ImU64)(float)(double)
  105. #endif
  106. // CALL_INSTANTIATE_FOR_NUMERIC_TYPES will duplicate the template instantiation code `INSTANTIATE_MACRO(T)` on supported types.
  107. #define _CAT(x, y) _CAT_(x, y)
  108. #define _CAT_(x, y) x##y
  109. #define _INSTANTIATE_FOR_NUMERIC_TYPES(chain) _CAT(_INSTANTIATE_FOR_NUMERIC_TYPES_1 chain, _END)
  110. #define _INSTANTIATE_FOR_NUMERIC_TYPES_1(T) INSTANTIATE_MACRO(T) _INSTANTIATE_FOR_NUMERIC_TYPES_2
  111. #define _INSTANTIATE_FOR_NUMERIC_TYPES_2(T) INSTANTIATE_MACRO(T) _INSTANTIATE_FOR_NUMERIC_TYPES_1
  112. #define _INSTANTIATE_FOR_NUMERIC_TYPES_1_END
  113. #define _INSTANTIATE_FOR_NUMERIC_TYPES_2_END
  114. #define CALL_INSTANTIATE_FOR_NUMERIC_TYPES() _INSTANTIATE_FOR_NUMERIC_TYPES(IMPLOT3D_NUMERIC_TYPES)
  115. //-----------------------------------------------------------------------------
  116. // [SECTION] Item Utils
  117. //-----------------------------------------------------------------------------
  118. namespace ImPlot3D {
  119. static const float ITEM_HIGHLIGHT_LINE_SCALE = 2.0f;
  120. static const float ITEM_HIGHLIGHT_MARK_SCALE = 1.25f;
  121. bool BeginItem(const char* label_id, ImPlot3DItemFlags flags, ImPlot3DCol recolor_from) {
  122. ImPlot3DContext& gp = *GImPlot3D;
  123. IM_ASSERT_USER_ERROR(gp.CurrentPlot != nullptr, "PlotX() needs to be called between BeginPlot() and EndPlot()!");
  124. // Lock setup
  125. SetupLock();
  126. ImPlot3DStyle& style = gp.Style;
  127. ImPlot3DNextItemData& n = gp.NextItemData;
  128. // Register item
  129. bool just_created;
  130. ImPlot3DItem* item = RegisterOrGetItem(label_id, flags, &just_created);
  131. // Set/override item color
  132. if (recolor_from != -1) {
  133. if (!IsColorAuto(n.Colors[recolor_from]))
  134. item->Color = ImGui::ColorConvertFloat4ToU32(n.Colors[recolor_from]);
  135. else if (!IsColorAuto(gp.Style.Colors[recolor_from]))
  136. item->Color = ImGui::ColorConvertFloat4ToU32(gp.Style.Colors[recolor_from]);
  137. else if (just_created)
  138. item->Color = NextColormapColorU32();
  139. } else if (just_created) {
  140. item->Color = NextColormapColorU32();
  141. }
  142. // Set next item color
  143. ImVec4 item_color = ImGui::ColorConvertU32ToFloat4(item->Color);
  144. n.IsAutoLine = IsColorAuto(n.Colors[ImPlot3DCol_Line]) && IsColorAuto(ImPlot3DCol_Line);
  145. n.IsAutoFill = IsColorAuto(n.Colors[ImPlot3DCol_Fill]) && IsColorAuto(ImPlot3DCol_Fill);
  146. n.Colors[ImPlot3DCol_Line] = IsColorAuto(n.Colors[ImPlot3DCol_Line]) ? (IsColorAuto(ImPlot3DCol_Line) ? item_color : gp.Style.Colors[ImPlot3DCol_Line]) : n.Colors[ImPlot3DCol_Line];
  147. n.Colors[ImPlot3DCol_Fill] = IsColorAuto(n.Colors[ImPlot3DCol_Fill]) ? (IsColorAuto(ImPlot3DCol_Fill) ? item_color : gp.Style.Colors[ImPlot3DCol_Fill]) : n.Colors[ImPlot3DCol_Fill];
  148. n.Colors[ImPlot3DCol_MarkerOutline] = IsColorAuto(n.Colors[ImPlot3DCol_MarkerOutline]) ? (IsColorAuto(ImPlot3DCol_MarkerOutline) ? n.Colors[ImPlot3DCol_Line] : gp.Style.Colors[ImPlot3DCol_MarkerOutline]) : n.Colors[ImPlot3DCol_MarkerOutline];
  149. n.Colors[ImPlot3DCol_MarkerFill] = IsColorAuto(n.Colors[ImPlot3DCol_MarkerFill]) ? (IsColorAuto(ImPlot3DCol_MarkerFill) ? n.Colors[ImPlot3DCol_Line] : gp.Style.Colors[ImPlot3DCol_MarkerFill]) : n.Colors[ImPlot3DCol_MarkerFill];
  150. // Set size & weight
  151. n.LineWeight = n.LineWeight < 0.0f ? style.LineWeight : n.LineWeight;
  152. n.Marker = n.Marker < 0 ? style.Marker : n.Marker;
  153. n.MarkerSize = n.MarkerSize < 0.0f ? style.MarkerSize : n.MarkerSize;
  154. n.MarkerWeight = n.MarkerWeight < 0.0f ? style.MarkerWeight : n.MarkerWeight;
  155. n.FillAlpha = n.FillAlpha < 0 ? gp.Style.FillAlpha : n.FillAlpha;
  156. // Apply alpha modifiers
  157. n.Colors[ImPlot3DCol_Fill].w *= n.FillAlpha;
  158. n.Colors[ImPlot3DCol_MarkerFill].w *= n.FillAlpha;
  159. // Set render flags
  160. n.RenderLine = n.Colors[ImPlot3DCol_Line].w > 0 && n.LineWeight > 0;
  161. n.RenderFill = n.Colors[ImPlot3DCol_Fill].w > 0;
  162. n.RenderMarkerFill = n.Colors[ImPlot3DCol_MarkerFill].w > 0;
  163. n.RenderMarkerLine = n.Colors[ImPlot3DCol_MarkerOutline].w > 0 && n.MarkerWeight > 0;
  164. // Don't render if item is hidden
  165. if (!item->Show) {
  166. EndItem();
  167. return false;
  168. } else {
  169. // Legend hover highlight
  170. if (item->LegendHovered) {
  171. if (!ImHasFlag(gp.CurrentItems->Legend.Flags, ImPlot3DLegendFlags_NoHighlightItem)) {
  172. n.LineWeight *= ITEM_HIGHLIGHT_LINE_SCALE;
  173. n.MarkerSize *= ITEM_HIGHLIGHT_MARK_SCALE;
  174. n.MarkerWeight *= ITEM_HIGHLIGHT_LINE_SCALE;
  175. }
  176. }
  177. }
  178. return true;
  179. }
  180. template <typename _Getter>
  181. bool BeginItemEx(const char* label_id, const _Getter& getter, ImPlot3DItemFlags flags = 0, ImPlot3DCol recolor_from = IMPLOT3D_AUTO) {
  182. if (BeginItem(label_id, flags, recolor_from)) {
  183. ImPlot3DContext& gp = *GImPlot3D;
  184. ImPlot3DPlot& plot = *gp.CurrentPlot;
  185. if (plot.FitThisFrame && !ImHasFlag(flags, ImPlot3DItemFlags_NoFit)) {
  186. for (int i = 0; i < getter.Count; i++)
  187. plot.ExtendFit(getter(i));
  188. }
  189. return true;
  190. }
  191. return false;
  192. }
  193. void EndItem() {
  194. ImPlot3DContext& gp = *GImPlot3D;
  195. gp.NextItemData.Reset();
  196. }
  197. ImPlot3DItem* RegisterOrGetItem(const char* label_id, ImPlot3DItemFlags flags, bool* just_created) {
  198. ImPlot3DContext& gp = *GImPlot3D;
  199. ImPlot3DItemGroup& Items = *gp.CurrentItems;
  200. ImGuiID id = Items.GetItemID(label_id);
  201. if (just_created != nullptr)
  202. *just_created = Items.GetItem(id) == nullptr;
  203. ImPlot3DItem* item = Items.GetOrAddItem(id);
  204. // Avoid re-adding the same item to the legend (the legend is reset every frame)
  205. if (item->SeenThisFrame)
  206. return item;
  207. item->SeenThisFrame = true;
  208. // Add item to the legend
  209. int idx = Items.GetItemIndex(item);
  210. item->ID = id;
  211. if (!ImHasFlag(flags, ImPlot3DItemFlags_NoLegend) && ImGui::FindRenderedTextEnd(label_id, nullptr) != label_id) {
  212. Items.Legend.Indices.push_back(idx);
  213. item->NameOffset = Items.Legend.Labels.size();
  214. Items.Legend.Labels.append(label_id, label_id + strlen(label_id) + 1);
  215. }
  216. return item;
  217. }
  218. void BustItemCache() {
  219. ImPlot3DContext& gp = *GImPlot3D;
  220. for (int p = 0; p < gp.Plots.GetBufSize(); ++p) {
  221. ImPlot3DPlot& plot = *gp.Plots.GetByIndex(p);
  222. plot.Items.Reset();
  223. }
  224. }
  225. void SetNextLineStyle(const ImVec4& col, float weight) {
  226. ImPlot3DContext& gp = *GImPlot3D;
  227. ImPlot3DNextItemData& n = gp.NextItemData;
  228. n.Colors[ImPlot3DCol_Line] = col;
  229. n.LineWeight = weight;
  230. }
  231. void SetNextFillStyle(const ImVec4& col, float alpha) {
  232. ImPlot3DContext& gp = *GImPlot3D;
  233. ImPlot3DNextItemData& n = gp.NextItemData;
  234. gp.NextItemData.Colors[ImPlot3DCol_Fill] = col;
  235. gp.NextItemData.FillAlpha = alpha;
  236. }
  237. void SetNextMarkerStyle(ImPlot3DMarker marker, float size, const ImVec4& fill, float weight, const ImVec4& outline) {
  238. ImPlot3DContext& gp = *GImPlot3D;
  239. ImPlot3DNextItemData& n = gp.NextItemData;
  240. n.Marker = marker;
  241. n.Colors[ImPlot3DCol_MarkerFill] = fill;
  242. n.MarkerSize = size;
  243. n.Colors[ImPlot3DCol_MarkerOutline] = outline;
  244. n.MarkerWeight = weight;
  245. }
  246. //-----------------------------------------------------------------------------
  247. // [SECTION] Draw Utils
  248. //-----------------------------------------------------------------------------
  249. IMPLOT3D_INLINE void PrimLine(ImDrawList3D& draw_list_3d, const ImVec2& P1, const ImVec2& P2, float half_weight, ImU32 col, const ImVec2& tex_uv0, const ImVec2 tex_uv1, float z) {
  250. float dx = P2.x - P1.x;
  251. float dy = P2.y - P1.y;
  252. IMPLOT3D_NORMALIZE2F(dx, dy);
  253. dx *= half_weight;
  254. dy *= half_weight;
  255. draw_list_3d._VtxWritePtr[0].pos.x = P1.x + dy;
  256. draw_list_3d._VtxWritePtr[0].pos.y = P1.y - dx;
  257. draw_list_3d._VtxWritePtr[0].uv = tex_uv0;
  258. draw_list_3d._VtxWritePtr[0].col = col;
  259. draw_list_3d._VtxWritePtr[1].pos.x = P2.x + dy;
  260. draw_list_3d._VtxWritePtr[1].pos.y = P2.y - dx;
  261. draw_list_3d._VtxWritePtr[1].uv = tex_uv0;
  262. draw_list_3d._VtxWritePtr[1].col = col;
  263. draw_list_3d._VtxWritePtr[2].pos.x = P2.x - dy;
  264. draw_list_3d._VtxWritePtr[2].pos.y = P2.y + dx;
  265. draw_list_3d._VtxWritePtr[2].uv = tex_uv1;
  266. draw_list_3d._VtxWritePtr[2].col = col;
  267. draw_list_3d._VtxWritePtr[3].pos.x = P1.x - dy;
  268. draw_list_3d._VtxWritePtr[3].pos.y = P1.y + dx;
  269. draw_list_3d._VtxWritePtr[3].uv = tex_uv1;
  270. draw_list_3d._VtxWritePtr[3].col = col;
  271. draw_list_3d._VtxWritePtr += 4;
  272. draw_list_3d._IdxWritePtr[0] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx);
  273. draw_list_3d._IdxWritePtr[1] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx + 1);
  274. draw_list_3d._IdxWritePtr[2] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx + 2);
  275. draw_list_3d._IdxWritePtr[3] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx);
  276. draw_list_3d._IdxWritePtr[4] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx + 2);
  277. draw_list_3d._IdxWritePtr[5] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx + 3);
  278. draw_list_3d._IdxWritePtr += 6;
  279. draw_list_3d._VtxCurrentIdx += 4;
  280. draw_list_3d._ZWritePtr[0] = z;
  281. draw_list_3d._ZWritePtr[1] = z;
  282. draw_list_3d._ZWritePtr += 2;
  283. }
  284. //-----------------------------------------------------------------------------
  285. // [SECTION] Renderers
  286. //-----------------------------------------------------------------------------
  287. float GetPointDepth(ImPlot3DPoint p) {
  288. ImPlot3DContext& gp = *GImPlot3D;
  289. ImPlot3DPlot& plot = *gp.CurrentPlot;
  290. ImPlot3DPoint p_rot = plot.Rotation * p;
  291. return p_rot.z;
  292. }
  293. struct RendererBase {
  294. RendererBase(int prims, int idx_consumed, int vtx_consumed) : Prims(prims),
  295. IdxConsumed(idx_consumed),
  296. VtxConsumed(vtx_consumed) {}
  297. const unsigned int Prims; // Number of primitives to render
  298. const unsigned int IdxConsumed; // Number of indices consumed per primitive
  299. const unsigned int VtxConsumed; // Number of vertices consumed per primitive
  300. };
  301. template <class _Getter>
  302. struct RendererMarkersFill : RendererBase {
  303. RendererMarkersFill(const _Getter& getter, const ImVec2* marker, int count, float size, ImU32 col) : RendererBase(getter.Count, (count - 2) * 3, count),
  304. Getter(getter),
  305. Marker(marker),
  306. Count(count),
  307. Size(size),
  308. Col(col) {}
  309. void Init(ImDrawList3D& draw_list_3d) const {
  310. UV = draw_list_3d._SharedData->TexUvWhitePixel;
  311. }
  312. IMPLOT3D_INLINE bool Render(ImDrawList3D& draw_list_3d, const ImPlot3DBox& cull_box, int prim) const {
  313. ImPlot3DPoint p_plot = Getter(prim);
  314. if (!cull_box.Contains(p_plot))
  315. return false;
  316. ImVec2 p = PlotToPixels(p_plot);
  317. // 3 vertices per triangle
  318. for (int i = 0; i < Count; i++) {
  319. draw_list_3d._VtxWritePtr[0].pos.x = p.x + Marker[i].x * Size;
  320. draw_list_3d._VtxWritePtr[0].pos.y = p.y + Marker[i].y * Size;
  321. draw_list_3d._VtxWritePtr[0].uv = UV;
  322. draw_list_3d._VtxWritePtr[0].col = Col;
  323. draw_list_3d._VtxWritePtr++;
  324. }
  325. // 3 indices per triangle
  326. for (int i = 2; i < Count; i++) {
  327. // Indices
  328. draw_list_3d._IdxWritePtr[0] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx);
  329. draw_list_3d._IdxWritePtr[1] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx + i - 1);
  330. draw_list_3d._IdxWritePtr[2] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx + i);
  331. draw_list_3d._IdxWritePtr += 3;
  332. // Z
  333. draw_list_3d._ZWritePtr[0] = GetPointDepth(p_plot);
  334. draw_list_3d._ZWritePtr++;
  335. }
  336. // Update vertex count
  337. draw_list_3d._VtxCurrentIdx += (ImDrawIdx)Count;
  338. return true;
  339. }
  340. const _Getter& Getter;
  341. const ImVec2* Marker;
  342. const int Count;
  343. const float Size;
  344. const ImU32 Col;
  345. mutable ImVec2 UV;
  346. };
  347. template <class _Getter>
  348. struct RendererMarkersLine : RendererBase {
  349. RendererMarkersLine(const _Getter& getter, const ImVec2* marker, int count, float size, float weight, ImU32 col) : RendererBase(getter.Count, count / 2 * 6, count / 2 * 4), Getter(getter), Marker(marker), Count(count), HalfWeight(ImMax(1.0f, weight) * 0.5f), Size(size), Col(col) {}
  350. void Init(ImDrawList3D& draw_list_3d) const {
  351. GetLineRenderProps(draw_list_3d, HalfWeight, UV0, UV1);
  352. }
  353. IMPLOT3D_INLINE bool Render(ImDrawList3D& draw_list_3d, const ImPlot3DBox& cull_box, int prim) const {
  354. ImPlot3DPoint p_plot = Getter(prim);
  355. if (!cull_box.Contains(p_plot))
  356. return false;
  357. ImVec2 p = PlotToPixels(p_plot);
  358. for (int i = 0; i < Count; i = i + 2) {
  359. ImVec2 p1(p.x + Marker[i].x * Size, p.y + Marker[i].y * Size);
  360. ImVec2 p2(p.x + Marker[i + 1].x * Size, p.y + Marker[i + 1].y * Size);
  361. PrimLine(draw_list_3d, p1, p2, HalfWeight, Col, UV0, UV1, GetPointDepth(p_plot));
  362. }
  363. return true;
  364. }
  365. const _Getter& Getter;
  366. const ImVec2* Marker;
  367. const int Count;
  368. mutable float HalfWeight;
  369. const float Size;
  370. const ImU32 Col;
  371. mutable ImVec2 UV0;
  372. mutable ImVec2 UV1;
  373. };
  374. template <class _Getter>
  375. struct RendererLineStrip : RendererBase {
  376. RendererLineStrip(const _Getter& getter, ImU32 col, float weight)
  377. : RendererBase(getter.Count - 1, 6, 4),
  378. Getter(getter),
  379. Col(col),
  380. HalfWeight(ImMax(1.0f, weight) * 0.5f) {
  381. // Initialize the first point in plot coordinates
  382. P1_plot = Getter(0);
  383. }
  384. void Init(ImDrawList3D& draw_list_3d) const {
  385. GetLineRenderProps(draw_list_3d, HalfWeight, UV0, UV1);
  386. }
  387. IMPLOT3D_INLINE bool Render(ImDrawList3D& draw_list_3d, const ImPlot3DBox& cull_box, int prim) const {
  388. ImPlot3DPoint P2_plot = Getter(prim + 1);
  389. // Clip the line segment to the culling box using Liang-Barsky algorithm
  390. ImPlot3DPoint P1_clipped, P2_clipped;
  391. bool visible = cull_box.ClipLineSegment(P1_plot, P2_plot, P1_clipped, P2_clipped);
  392. if (visible) {
  393. // Convert clipped points to pixel coordinates
  394. ImVec2 P1_screen = PlotToPixels(P1_clipped);
  395. ImVec2 P2_screen = PlotToPixels(P2_clipped);
  396. // Render the line segment
  397. PrimLine(draw_list_3d, P1_screen, P2_screen, HalfWeight, Col, UV0, UV1, GetPointDepth((P1_plot + P2_plot) * 0.5f));
  398. }
  399. // Update for next segment
  400. P1_plot = P2_plot;
  401. return visible;
  402. }
  403. const _Getter& Getter;
  404. const ImU32 Col;
  405. mutable float HalfWeight;
  406. mutable ImPlot3DPoint P1_plot;
  407. mutable ImVec2 UV0;
  408. mutable ImVec2 UV1;
  409. };
  410. template <class _Getter>
  411. struct RendererLineStripSkip : RendererBase {
  412. RendererLineStripSkip(const _Getter& getter, ImU32 col, float weight)
  413. : RendererBase(getter.Count - 1, 6, 4),
  414. Getter(getter),
  415. Col(col),
  416. HalfWeight(ImMax(1.0f, weight) * 0.5f) {
  417. // Initialize the first point in plot coordinates
  418. P1_plot = Getter(0);
  419. }
  420. void Init(ImDrawList3D& draw_list_3d) const {
  421. GetLineRenderProps(draw_list_3d, HalfWeight, UV0, UV1);
  422. }
  423. IMPLOT3D_INLINE bool Render(ImDrawList3D& draw_list_3d, const ImPlot3DBox& cull_box, int prim) const {
  424. // Get the next point in plot coordinates
  425. ImPlot3DPoint P2_plot = Getter(prim + 1);
  426. bool visible = false;
  427. // Check for NaNs in P1_plot and P2_plot
  428. if (!ImNan(P1_plot.x) && !ImNan(P1_plot.y) && !ImNan(P1_plot.z) &&
  429. !ImNan(P2_plot.x) && !ImNan(P2_plot.y) && !ImNan(P2_plot.z)) {
  430. // Clip the line segment to the culling box
  431. ImPlot3DPoint P1_clipped, P2_clipped;
  432. visible = cull_box.ClipLineSegment(P1_plot, P2_plot, P1_clipped, P2_clipped);
  433. if (visible) {
  434. // Convert clipped points to pixel coordinates
  435. ImVec2 P1_screen = PlotToPixels(P1_clipped);
  436. ImVec2 P2_screen = PlotToPixels(P2_clipped);
  437. // Render the line segment
  438. PrimLine(draw_list_3d, P1_screen, P2_screen, HalfWeight, Col, UV0, UV1, GetPointDepth((P1_plot + P2_plot) * 0.5f));
  439. }
  440. }
  441. // Update P1_plot if P2_plot is valid
  442. if (!ImNan(P2_plot.x) && !ImNan(P2_plot.y) && !ImNan(P2_plot.z))
  443. P1_plot = P2_plot;
  444. return visible;
  445. }
  446. const _Getter& Getter;
  447. const ImU32 Col;
  448. mutable float HalfWeight;
  449. mutable ImPlot3DPoint P1_plot;
  450. mutable ImVec2 UV0;
  451. mutable ImVec2 UV1;
  452. };
  453. template <class _Getter>
  454. struct RendererLineSegments : RendererBase {
  455. RendererLineSegments(const _Getter& getter, ImU32 col, float weight)
  456. : RendererBase(getter.Count / 2, 6, 4),
  457. Getter(getter),
  458. Col(col),
  459. HalfWeight(ImMax(1.0f, weight) * 0.5f) {}
  460. void Init(ImDrawList3D& draw_list_3d) const {
  461. GetLineRenderProps(draw_list_3d, HalfWeight, UV0, UV1);
  462. }
  463. IMPLOT3D_INLINE bool Render(ImDrawList3D& draw_list_3d, const ImPlot3DBox& cull_box, int prim) const {
  464. // Get the segment's endpoints in plot coordinates
  465. ImPlot3DPoint P1_plot = Getter(prim * 2 + 0);
  466. ImPlot3DPoint P2_plot = Getter(prim * 2 + 1);
  467. // Check for NaNs in P1_plot and P2_plot
  468. if (!ImNan(P1_plot.x) && !ImNan(P1_plot.y) && !ImNan(P1_plot.z) &&
  469. !ImNan(P2_plot.x) && !ImNan(P2_plot.y) && !ImNan(P2_plot.z)) {
  470. // Clip the line segment to the culling box
  471. ImPlot3DPoint P1_clipped, P2_clipped;
  472. bool visible = cull_box.ClipLineSegment(P1_plot, P2_plot, P1_clipped, P2_clipped);
  473. if (visible) {
  474. // Convert clipped points to pixel coordinates
  475. ImVec2 P1_screen = PlotToPixels(P1_clipped);
  476. ImVec2 P2_screen = PlotToPixels(P2_clipped);
  477. // Render the line segment
  478. PrimLine(draw_list_3d, P1_screen, P2_screen, HalfWeight, Col, UV0, UV1, GetPointDepth((P1_plot + P2_plot) * 0.5f));
  479. }
  480. return visible;
  481. }
  482. return false;
  483. }
  484. const _Getter& Getter;
  485. const ImU32 Col;
  486. mutable float HalfWeight;
  487. mutable ImVec2 UV0;
  488. mutable ImVec2 UV1;
  489. };
  490. template <class _Getter>
  491. struct RendererTriangleFill : RendererBase {
  492. RendererTriangleFill(const _Getter& getter, ImU32 col) : RendererBase(getter.Count / 3, 3, 3),
  493. Getter(getter),
  494. Col(col) {}
  495. void Init(ImDrawList3D& draw_list_3d) const {
  496. UV = draw_list_3d._SharedData->TexUvWhitePixel;
  497. }
  498. IMPLOT3D_INLINE bool Render(ImDrawList3D& draw_list_3d, const ImPlot3DBox& cull_box, int prim) const {
  499. ImPlot3DPoint p_plot[3];
  500. p_plot[0] = Getter(3 * prim);
  501. p_plot[1] = Getter(3 * prim + 1);
  502. p_plot[2] = Getter(3 * prim + 2);
  503. // Check if the triangle is outside the culling box
  504. if (!cull_box.Contains(p_plot[0]) && !cull_box.Contains(p_plot[0]) && !cull_box.Contains(p_plot[1]))
  505. return false;
  506. // Project the triangle vertices to screen space
  507. ImVec2 p[3];
  508. p[0] = PlotToPixels(p_plot[0]);
  509. p[1] = PlotToPixels(p_plot[1]);
  510. p[2] = PlotToPixels(p_plot[2]);
  511. // 3 vertices per triangle
  512. draw_list_3d._VtxWritePtr[0].pos.x = p[0].x;
  513. draw_list_3d._VtxWritePtr[0].pos.y = p[0].y;
  514. draw_list_3d._VtxWritePtr[0].uv = UV;
  515. draw_list_3d._VtxWritePtr[0].col = Col;
  516. draw_list_3d._VtxWritePtr[1].pos.x = p[1].x;
  517. draw_list_3d._VtxWritePtr[1].pos.y = p[1].y;
  518. draw_list_3d._VtxWritePtr[1].uv = UV;
  519. draw_list_3d._VtxWritePtr[1].col = Col;
  520. draw_list_3d._VtxWritePtr[2].pos.x = p[2].x;
  521. draw_list_3d._VtxWritePtr[2].pos.y = p[2].y;
  522. draw_list_3d._VtxWritePtr[2].uv = UV;
  523. draw_list_3d._VtxWritePtr[2].col = Col;
  524. draw_list_3d._VtxWritePtr += 3;
  525. // 3 indices per triangle
  526. draw_list_3d._IdxWritePtr[0] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx);
  527. draw_list_3d._IdxWritePtr[1] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx + 1);
  528. draw_list_3d._IdxWritePtr[2] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx + 2);
  529. draw_list_3d._IdxWritePtr += 3;
  530. // 1 Z per vertex
  531. draw_list_3d._ZWritePtr[0] = GetPointDepth((p_plot[0] + p_plot[1] + p_plot[2]) / 3);
  532. draw_list_3d._ZWritePtr++;
  533. // Update vertex count
  534. draw_list_3d._VtxCurrentIdx += 3;
  535. return true;
  536. }
  537. const _Getter& Getter;
  538. mutable ImVec2 UV;
  539. const ImU32 Col;
  540. };
  541. template <class _Getter>
  542. struct RendererQuadFill : RendererBase {
  543. RendererQuadFill(const _Getter& getter, ImU32 col) : RendererBase(getter.Count / 4, 6, 4),
  544. Getter(getter),
  545. Col(col) {}
  546. void Init(ImDrawList3D& draw_list_3d) const {
  547. UV = draw_list_3d._SharedData->TexUvWhitePixel;
  548. }
  549. IMPLOT3D_INLINE bool Render(ImDrawList3D& draw_list_3d, const ImPlot3DBox& cull_box, int prim) const {
  550. ImPlot3DPoint p_plot[4];
  551. p_plot[0] = Getter(4 * prim);
  552. p_plot[1] = Getter(4 * prim + 1);
  553. p_plot[2] = Getter(4 * prim + 2);
  554. p_plot[3] = Getter(4 * prim + 3);
  555. // Check if the quad is outside the culling box
  556. if (!cull_box.Contains(p_plot[0]) && !cull_box.Contains(p_plot[1]) &&
  557. !cull_box.Contains(p_plot[2]) && !cull_box.Contains(p_plot[3]))
  558. return false;
  559. // Project the quad vertices to screen space
  560. ImVec2 p[4];
  561. p[0] = PlotToPixels(p_plot[0]);
  562. p[1] = PlotToPixels(p_plot[1]);
  563. p[2] = PlotToPixels(p_plot[2]);
  564. p[3] = PlotToPixels(p_plot[3]);
  565. // Add vertices for two triangles
  566. draw_list_3d._VtxWritePtr[0].pos.x = p[0].x;
  567. draw_list_3d._VtxWritePtr[0].pos.y = p[0].y;
  568. draw_list_3d._VtxWritePtr[0].uv = UV;
  569. draw_list_3d._VtxWritePtr[0].col = Col;
  570. draw_list_3d._VtxWritePtr[1].pos.x = p[1].x;
  571. draw_list_3d._VtxWritePtr[1].pos.y = p[1].y;
  572. draw_list_3d._VtxWritePtr[1].uv = UV;
  573. draw_list_3d._VtxWritePtr[1].col = Col;
  574. draw_list_3d._VtxWritePtr[2].pos.x = p[2].x;
  575. draw_list_3d._VtxWritePtr[2].pos.y = p[2].y;
  576. draw_list_3d._VtxWritePtr[2].uv = UV;
  577. draw_list_3d._VtxWritePtr[2].col = Col;
  578. draw_list_3d._VtxWritePtr[3].pos.x = p[3].x;
  579. draw_list_3d._VtxWritePtr[3].pos.y = p[3].y;
  580. draw_list_3d._VtxWritePtr[3].uv = UV;
  581. draw_list_3d._VtxWritePtr[3].col = Col;
  582. draw_list_3d._VtxWritePtr += 4;
  583. // Add indices for two triangles
  584. draw_list_3d._IdxWritePtr[0] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx);
  585. draw_list_3d._IdxWritePtr[1] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx + 1);
  586. draw_list_3d._IdxWritePtr[2] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx + 2);
  587. draw_list_3d._IdxWritePtr[3] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx);
  588. draw_list_3d._IdxWritePtr[4] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx + 2);
  589. draw_list_3d._IdxWritePtr[5] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx + 3);
  590. draw_list_3d._IdxWritePtr += 6;
  591. // Add depth values for the two triangles
  592. draw_list_3d._ZWritePtr[0] = GetPointDepth((p_plot[0] + p_plot[1] + p_plot[2]) / 3.0f);
  593. draw_list_3d._ZWritePtr[1] = GetPointDepth((p_plot[0] + p_plot[2] + p_plot[3]) / 3.0f);
  594. draw_list_3d._ZWritePtr += 2;
  595. // Update vertex count
  596. draw_list_3d._VtxCurrentIdx += 4;
  597. return true;
  598. }
  599. const _Getter& Getter;
  600. mutable ImVec2 UV;
  601. const ImU32 Col;
  602. };
  603. template <class _Getter>
  604. struct RendererSurfaceFill : RendererBase {
  605. RendererSurfaceFill(const _Getter& getter, int x_count, int y_count, ImU32 col) : RendererBase((x_count - 1) * (y_count - 1), 6, 4),
  606. Getter(getter),
  607. XCount(x_count),
  608. YCount(y_count),
  609. Col(col) {}
  610. void Init(ImDrawList3D& draw_list_3d) const {
  611. UV = draw_list_3d._SharedData->TexUvWhitePixel;
  612. // Compute min and max values for the colormap (if not solid fill)
  613. const ImPlot3DNextItemData& n = GetItemData();
  614. if (n.IsAutoFill) {
  615. Min = FLT_MAX;
  616. Max = -FLT_MAX;
  617. for (int i = 0; i < Getter.Count; i++) {
  618. float z = Getter(i).z;
  619. Min = ImMin(Min, z);
  620. Max = ImMax(Max, z);
  621. }
  622. }
  623. }
  624. IMPLOT3D_INLINE bool Render(ImDrawList3D& draw_list_3d, const ImPlot3DBox& cull_box, int prim) const {
  625. int x = prim % (XCount - 1);
  626. int y = prim / (XCount - 1);
  627. ImPlot3DPoint p_plot[4];
  628. p_plot[0] = Getter(x + y * XCount);
  629. p_plot[1] = Getter(x + 1 + y * XCount);
  630. p_plot[2] = Getter(x + 1 + (y + 1) * XCount);
  631. p_plot[3] = Getter(x + (y + 1) * XCount);
  632. // Check if the quad is outside the culling box
  633. if (!cull_box.Contains(p_plot[0]) && !cull_box.Contains(p_plot[1]) &&
  634. !cull_box.Contains(p_plot[2]) && !cull_box.Contains(p_plot[3]))
  635. return false;
  636. // Compute colors
  637. ImU32 cols[4] = {Col, Col, Col, Col};
  638. const ImPlot3DNextItemData& n = GetItemData();
  639. if (n.IsAutoFill) {
  640. float alpha = GImPlot3D->NextItemData.FillAlpha;
  641. for (int i = 0; i < 4; i++) {
  642. ImVec4 col = SampleColormap(ImClamp(ImRemap01(p_plot[i].z, Min, Max), 0.0f, 1.0f));
  643. col.w *= alpha;
  644. cols[i] = ImGui::ColorConvertFloat4ToU32(col);
  645. }
  646. }
  647. // Project the quad vertices to screen space
  648. ImVec2 p[4];
  649. p[0] = PlotToPixels(p_plot[0]);
  650. p[1] = PlotToPixels(p_plot[1]);
  651. p[2] = PlotToPixels(p_plot[2]);
  652. p[3] = PlotToPixels(p_plot[3]);
  653. // Add vertices for two triangles
  654. draw_list_3d._VtxWritePtr[0].pos.x = p[0].x;
  655. draw_list_3d._VtxWritePtr[0].pos.y = p[0].y;
  656. draw_list_3d._VtxWritePtr[0].uv = UV;
  657. draw_list_3d._VtxWritePtr[0].col = cols[0];
  658. draw_list_3d._VtxWritePtr[1].pos.x = p[1].x;
  659. draw_list_3d._VtxWritePtr[1].pos.y = p[1].y;
  660. draw_list_3d._VtxWritePtr[1].uv = UV;
  661. draw_list_3d._VtxWritePtr[1].col = cols[1];
  662. draw_list_3d._VtxWritePtr[2].pos.x = p[2].x;
  663. draw_list_3d._VtxWritePtr[2].pos.y = p[2].y;
  664. draw_list_3d._VtxWritePtr[2].uv = UV;
  665. draw_list_3d._VtxWritePtr[2].col = cols[2];
  666. draw_list_3d._VtxWritePtr[3].pos.x = p[3].x;
  667. draw_list_3d._VtxWritePtr[3].pos.y = p[3].y;
  668. draw_list_3d._VtxWritePtr[3].uv = UV;
  669. draw_list_3d._VtxWritePtr[3].col = cols[3];
  670. draw_list_3d._VtxWritePtr += 4;
  671. // Add indices for two triangles
  672. draw_list_3d._IdxWritePtr[0] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx);
  673. draw_list_3d._IdxWritePtr[1] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx + 1);
  674. draw_list_3d._IdxWritePtr[2] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx + 2);
  675. draw_list_3d._IdxWritePtr[3] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx);
  676. draw_list_3d._IdxWritePtr[4] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx + 2);
  677. draw_list_3d._IdxWritePtr[5] = (ImDrawIdx)(draw_list_3d._VtxCurrentIdx + 3);
  678. draw_list_3d._IdxWritePtr += 6;
  679. // Add depth values for the two triangles
  680. draw_list_3d._ZWritePtr[0] = GetPointDepth((p_plot[0] + p_plot[1] + p_plot[2]) / 3.0f);
  681. draw_list_3d._ZWritePtr[1] = GetPointDepth((p_plot[0] + p_plot[2] + p_plot[3]) / 3.0f);
  682. draw_list_3d._ZWritePtr += 2;
  683. // Update vertex count
  684. draw_list_3d._VtxCurrentIdx += 4;
  685. return true;
  686. }
  687. const _Getter& Getter;
  688. mutable ImVec2 UV;
  689. mutable float Min; // Minimum value for the colormap
  690. mutable float Max; // Minimum value for the colormap
  691. const int XCount;
  692. const int YCount;
  693. const ImU32 Col;
  694. };
  695. //-----------------------------------------------------------------------------
  696. // [SECTION] Indexers
  697. //-----------------------------------------------------------------------------
  698. template <typename T>
  699. IMPLOT3D_INLINE T IndexData(const T* data, int idx, int count, int offset, int stride) {
  700. const int s = ((offset == 0) << 0) | ((stride == sizeof(T)) << 1);
  701. switch (s) {
  702. case 3: return data[idx];
  703. case 2: return data[(offset + idx) % count];
  704. case 1: return *(const T*)(const void*)((const unsigned char*)data + (size_t)((idx)) * stride);
  705. case 0: return *(const T*)(const void*)((const unsigned char*)data + (size_t)((offset + idx) % count) * stride);
  706. default: return T(0);
  707. }
  708. }
  709. template <typename T>
  710. struct IndexerIdx {
  711. IndexerIdx(const T* data, int count, int offset = 0, int stride = sizeof(T)) : Data(data),
  712. Count(count),
  713. Offset(offset),
  714. Stride(stride) {}
  715. template <typename I> IMPLOT3D_INLINE double operator()(I idx) const {
  716. return (double)IndexData(Data, idx, Count, Offset, Stride);
  717. }
  718. const T* Data;
  719. int Count;
  720. int Offset;
  721. int Stride;
  722. };
  723. //-----------------------------------------------------------------------------
  724. // [SECTION] Getters
  725. //-----------------------------------------------------------------------------
  726. template <typename _IndexerX, typename _IndexerY, typename _IndexerZ>
  727. struct GetterXYZ {
  728. GetterXYZ(_IndexerX x, _IndexerY y, _IndexerZ z, int count) : IndexerX(x), IndexerY(y), IndexerZ(z), Count(count) {}
  729. template <typename I> IMPLOT3D_INLINE ImPlot3DPoint operator()(I idx) const {
  730. return ImPlot3DPoint(IndexerX(idx), IndexerY(idx), IndexerZ(idx));
  731. }
  732. const _IndexerX IndexerX;
  733. const _IndexerY IndexerY;
  734. const _IndexerZ IndexerZ;
  735. const int Count;
  736. };
  737. template <typename _Getter>
  738. struct GetterLoop {
  739. GetterLoop(_Getter getter) : Getter(getter), Count(getter.Count + 1) {}
  740. template <typename I> IMPLOT3D_INLINE ImPlot3DPoint operator()(I idx) const {
  741. idx = idx % (Count - 1);
  742. return Getter(idx);
  743. }
  744. const _Getter Getter;
  745. const int Count;
  746. };
  747. template <typename _Getter>
  748. struct GetterTriangleLines {
  749. GetterTriangleLines(_Getter getter) : Getter(getter), Count(getter.Count * 2) {}
  750. template <typename I> IMPLOT3D_INLINE ImPlot3DPoint operator()(I idx) const {
  751. idx = ((idx % 6 + 1) / 2) % 3 + idx / 6 * 3;
  752. return Getter(idx);
  753. }
  754. const _Getter Getter;
  755. const int Count;
  756. };
  757. template <typename _Getter>
  758. struct GetterQuadLines {
  759. GetterQuadLines(_Getter getter) : Getter(getter), Count(getter.Count * 2) {}
  760. template <typename I> IMPLOT3D_INLINE ImPlot3DPoint operator()(I idx) const {
  761. idx = ((idx % 8 + 1) / 2) % 4 + idx / 8 * 4;
  762. return Getter(idx);
  763. }
  764. const _Getter Getter;
  765. const int Count;
  766. };
  767. template <typename _Getter>
  768. struct GetterSurfaceLines {
  769. GetterSurfaceLines(_Getter getter, int x_count, int y_count)
  770. : Getter(getter), XCount(x_count), YCount(y_count) {
  771. int horizontal_segments = (XCount - 1) * YCount;
  772. int vertical_segments = (YCount - 1) * XCount;
  773. int segments = horizontal_segments + vertical_segments;
  774. Count = segments * 2; // Each segment has 2 endpoints
  775. }
  776. template <typename I> IMPLOT3D_INLINE ImPlot3DPoint operator()(I idx) const {
  777. // idx is an endpoint index
  778. int endpoint_i = (int)(idx % 2);
  779. int segment_i = (int)(idx / 2);
  780. int horizontal_segments = (XCount - 1) * YCount;
  781. int px, py;
  782. if (segment_i < horizontal_segments) {
  783. // Horizontal segment
  784. int row = segment_i / (XCount - 1);
  785. int col = segment_i % (XCount - 1);
  786. // Endpoint 0 is (col, row), endpoint 1 is (col+1, row)
  787. px = endpoint_i == 0 ? col : col + 1;
  788. py = row;
  789. } else {
  790. // Vertical segment
  791. int seg_v = segment_i - horizontal_segments;
  792. int col = seg_v / (YCount - 1);
  793. int row = seg_v % (YCount - 1);
  794. // Endpoint 0 is (col, row), endpoint 1 is (col, row+1)
  795. px = col;
  796. py = row + endpoint_i;
  797. }
  798. return Getter(py * XCount + px);
  799. }
  800. const _Getter Getter;
  801. int Count;
  802. const int XCount;
  803. const int YCount;
  804. };
  805. struct Getter3DPoints {
  806. Getter3DPoints(const ImPlot3DPoint* points, int count) : Points(points), Count(count) {}
  807. template <typename I> IMPLOT3D_INLINE ImPlot3DPoint operator()(I idx) const {
  808. return Points[idx];
  809. }
  810. const ImPlot3DPoint* Points;
  811. const int Count;
  812. };
  813. struct GetterMeshTriangles {
  814. GetterMeshTriangles(const ImPlot3DPoint* vtx, const unsigned int* idx, int idx_count)
  815. : Vtx(vtx), Idx(idx), IdxCount(idx_count), TriCount(idx_count / 3), Count(idx_count) {}
  816. template <typename I>
  817. IMPLOT3D_INLINE ImPlot3DPoint operator()(I i) const {
  818. unsigned int vi = Idx[i];
  819. return Vtx[vi];
  820. }
  821. const ImPlot3DPoint* Vtx;
  822. const unsigned int* Idx;
  823. int IdxCount;
  824. int TriCount;
  825. int Count;
  826. };
  827. //-----------------------------------------------------------------------------
  828. // [SECTION] RenderPrimitives
  829. //-----------------------------------------------------------------------------
  830. /// Renders primitive shapes
  831. template <template <class> class _Renderer, class _Getter, typename... Args>
  832. void RenderPrimitives(const _Getter& getter, Args... args) {
  833. _Renderer<_Getter> renderer(getter, args...);
  834. ImPlot3DPlot& plot = *GetCurrentPlot();
  835. ImDrawList3D& draw_list_3d = plot.DrawList;
  836. ImPlot3DBox cull_box;
  837. if (ImHasFlag(plot.Flags, ImPlot3DFlags_NoClip)) {
  838. cull_box.Min = ImPlot3DPoint(-HUGE_VAL, -HUGE_VAL, -HUGE_VAL);
  839. cull_box.Max = ImPlot3DPoint(HUGE_VAL, HUGE_VAL, HUGE_VAL);
  840. } else {
  841. cull_box.Min = plot.RangeMin();
  842. cull_box.Max = plot.RangeMax();
  843. }
  844. // Find how many can be reserved up to end of current draw command's limit
  845. unsigned int prims_to_render = ImMin(renderer.Prims, (ImDrawList3D::MaxIdx() - draw_list_3d._VtxCurrentIdx) / renderer.VtxConsumed);
  846. // Reserve vertices and indices to render the primitives
  847. draw_list_3d.PrimReserve(prims_to_render * renderer.IdxConsumed, prims_to_render * renderer.VtxConsumed);
  848. // Initialize renderer
  849. renderer.Init(draw_list_3d);
  850. // Render primitives
  851. int num_culled = 0;
  852. for (unsigned int i = 0; i < prims_to_render; i++)
  853. if (!renderer.Render(draw_list_3d, cull_box, i))
  854. num_culled++;
  855. // Unreserve unused vertices and indices
  856. draw_list_3d.PrimUnreserve(num_culled * renderer.IdxConsumed, num_culled * renderer.VtxConsumed);
  857. }
  858. //-----------------------------------------------------------------------------
  859. // [SECTION] Markers
  860. //-----------------------------------------------------------------------------
  861. static const ImVec2 MARKER_FILL_CIRCLE[10] = {ImVec2(1.0f, 0.0f), ImVec2(0.809017f, 0.58778524f), ImVec2(0.30901697f, 0.95105654f), ImVec2(-0.30901703f, 0.9510565f), ImVec2(-0.80901706f, 0.5877852f), ImVec2(-1.0f, 0.0f), ImVec2(-0.80901694f, -0.58778536f), ImVec2(-0.3090171f, -0.9510565f), ImVec2(0.30901712f, -0.9510565f), ImVec2(0.80901694f, -0.5877853f)};
  862. static const ImVec2 MARKER_FILL_SQUARE[4] = {ImVec2(SQRT_1_2, SQRT_1_2), ImVec2(SQRT_1_2, -SQRT_1_2), ImVec2(-SQRT_1_2, -SQRT_1_2), ImVec2(-SQRT_1_2, SQRT_1_2)};
  863. static const ImVec2 MARKER_FILL_DIAMOND[4] = {ImVec2(1, 0), ImVec2(0, -1), ImVec2(-1, 0), ImVec2(0, 1)};
  864. static const ImVec2 MARKER_FILL_UP[3] = {ImVec2(SQRT_3_2, 0.5f), ImVec2(0, -1), ImVec2(-SQRT_3_2, 0.5f)};
  865. static const ImVec2 MARKER_FILL_DOWN[3] = {ImVec2(SQRT_3_2, -0.5f), ImVec2(0, 1), ImVec2(-SQRT_3_2, -0.5f)};
  866. static const ImVec2 MARKER_FILL_LEFT[3] = {ImVec2(-1, 0), ImVec2(0.5, SQRT_3_2), ImVec2(0.5, -SQRT_3_2)};
  867. static const ImVec2 MARKER_FILL_RIGHT[3] = {ImVec2(1, 0), ImVec2(-0.5, SQRT_3_2), ImVec2(-0.5, -SQRT_3_2)};
  868. static const ImVec2 MARKER_LINE_CIRCLE[20] = {
  869. ImVec2(1.0f, 0.0f),
  870. ImVec2(0.809017f, 0.58778524f),
  871. ImVec2(0.809017f, 0.58778524f),
  872. ImVec2(0.30901697f, 0.95105654f),
  873. ImVec2(0.30901697f, 0.95105654f),
  874. ImVec2(-0.30901703f, 0.9510565f),
  875. ImVec2(-0.30901703f, 0.9510565f),
  876. ImVec2(-0.80901706f, 0.5877852f),
  877. ImVec2(-0.80901706f, 0.5877852f),
  878. ImVec2(-1.0f, 0.0f),
  879. ImVec2(-1.0f, 0.0f),
  880. ImVec2(-0.80901694f, -0.58778536f),
  881. ImVec2(-0.80901694f, -0.58778536f),
  882. ImVec2(-0.3090171f, -0.9510565f),
  883. ImVec2(-0.3090171f, -0.9510565f),
  884. ImVec2(0.30901712f, -0.9510565f),
  885. ImVec2(0.30901712f, -0.9510565f),
  886. ImVec2(0.80901694f, -0.5877853f),
  887. ImVec2(0.80901694f, -0.5877853f),
  888. ImVec2(1.0f, 0.0f)};
  889. static const ImVec2 MARKER_LINE_SQUARE[8] = {ImVec2(SQRT_1_2, SQRT_1_2), ImVec2(SQRT_1_2, -SQRT_1_2), ImVec2(SQRT_1_2, -SQRT_1_2), ImVec2(-SQRT_1_2, -SQRT_1_2), ImVec2(-SQRT_1_2, -SQRT_1_2), ImVec2(-SQRT_1_2, SQRT_1_2), ImVec2(-SQRT_1_2, SQRT_1_2), ImVec2(SQRT_1_2, SQRT_1_2)};
  890. static const ImVec2 MARKER_LINE_DIAMOND[8] = {ImVec2(1, 0), ImVec2(0, -1), ImVec2(0, -1), ImVec2(-1, 0), ImVec2(-1, 0), ImVec2(0, 1), ImVec2(0, 1), ImVec2(1, 0)};
  891. static const ImVec2 MARKER_LINE_UP[6] = {ImVec2(SQRT_3_2, 0.5f), ImVec2(0, -1), ImVec2(0, -1), ImVec2(-SQRT_3_2, 0.5f), ImVec2(-SQRT_3_2, 0.5f), ImVec2(SQRT_3_2, 0.5f)};
  892. static const ImVec2 MARKER_LINE_DOWN[6] = {ImVec2(SQRT_3_2, -0.5f), ImVec2(0, 1), ImVec2(0, 1), ImVec2(-SQRT_3_2, -0.5f), ImVec2(-SQRT_3_2, -0.5f), ImVec2(SQRT_3_2, -0.5f)};
  893. static const ImVec2 MARKER_LINE_LEFT[6] = {ImVec2(-1, 0), ImVec2(0.5, SQRT_3_2), ImVec2(0.5, SQRT_3_2), ImVec2(0.5, -SQRT_3_2), ImVec2(0.5, -SQRT_3_2), ImVec2(-1, 0)};
  894. static const ImVec2 MARKER_LINE_RIGHT[6] = {ImVec2(1, 0), ImVec2(-0.5, SQRT_3_2), ImVec2(-0.5, SQRT_3_2), ImVec2(-0.5, -SQRT_3_2), ImVec2(-0.5, -SQRT_3_2), ImVec2(1, 0)};
  895. static const ImVec2 MARKER_LINE_ASTERISK[6] = {ImVec2(-SQRT_3_2, -0.5f), ImVec2(SQRT_3_2, 0.5f), ImVec2(-SQRT_3_2, 0.5f), ImVec2(SQRT_3_2, -0.5f), ImVec2(0, -1), ImVec2(0, 1)};
  896. static const ImVec2 MARKER_LINE_PLUS[4] = {ImVec2(-1, 0), ImVec2(1, 0), ImVec2(0, -1), ImVec2(0, 1)};
  897. static const ImVec2 MARKER_LINE_CROSS[4] = {ImVec2(-SQRT_1_2, -SQRT_1_2), ImVec2(SQRT_1_2, SQRT_1_2), ImVec2(SQRT_1_2, -SQRT_1_2), ImVec2(-SQRT_1_2, SQRT_1_2)};
  898. template <typename _Getter>
  899. void RenderMarkers(const _Getter& getter, ImPlot3DMarker marker, float size, bool rend_fill, ImU32 col_fill, bool rend_line, ImU32 col_line, float weight) {
  900. if (rend_fill) {
  901. switch (marker) {
  902. case ImPlot3DMarker_Circle: RenderPrimitives<RendererMarkersFill>(getter, MARKER_FILL_CIRCLE, 10, size, col_fill); break;
  903. case ImPlot3DMarker_Square: RenderPrimitives<RendererMarkersFill>(getter, MARKER_FILL_SQUARE, 4, size, col_fill); break;
  904. case ImPlot3DMarker_Diamond: RenderPrimitives<RendererMarkersFill>(getter, MARKER_FILL_DIAMOND, 4, size, col_fill); break;
  905. case ImPlot3DMarker_Up: RenderPrimitives<RendererMarkersFill>(getter, MARKER_FILL_UP, 3, size, col_fill); break;
  906. case ImPlot3DMarker_Down: RenderPrimitives<RendererMarkersFill>(getter, MARKER_FILL_DOWN, 3, size, col_fill); break;
  907. case ImPlot3DMarker_Left: RenderPrimitives<RendererMarkersFill>(getter, MARKER_FILL_LEFT, 3, size, col_fill); break;
  908. case ImPlot3DMarker_Right: RenderPrimitives<RendererMarkersFill>(getter, MARKER_FILL_RIGHT, 3, size, col_fill); break;
  909. }
  910. }
  911. if (rend_line) {
  912. switch (marker) {
  913. case ImPlot3DMarker_Circle: RenderPrimitives<RendererMarkersLine>(getter, MARKER_LINE_CIRCLE, 20, size, weight, col_line); break;
  914. case ImPlot3DMarker_Square: RenderPrimitives<RendererMarkersLine>(getter, MARKER_LINE_SQUARE, 8, size, weight, col_line); break;
  915. case ImPlot3DMarker_Diamond: RenderPrimitives<RendererMarkersLine>(getter, MARKER_LINE_DIAMOND, 8, size, weight, col_line); break;
  916. case ImPlot3DMarker_Up: RenderPrimitives<RendererMarkersLine>(getter, MARKER_LINE_UP, 6, size, weight, col_line); break;
  917. case ImPlot3DMarker_Down: RenderPrimitives<RendererMarkersLine>(getter, MARKER_LINE_DOWN, 6, size, weight, col_line); break;
  918. case ImPlot3DMarker_Left: RenderPrimitives<RendererMarkersLine>(getter, MARKER_LINE_LEFT, 6, size, weight, col_line); break;
  919. case ImPlot3DMarker_Right: RenderPrimitives<RendererMarkersLine>(getter, MARKER_LINE_RIGHT, 6, size, weight, col_line); break;
  920. case ImPlot3DMarker_Asterisk: RenderPrimitives<RendererMarkersLine>(getter, MARKER_LINE_ASTERISK, 6, size, weight, col_line); break;
  921. case ImPlot3DMarker_Plus: RenderPrimitives<RendererMarkersLine>(getter, MARKER_LINE_PLUS, 4, size, weight, col_line); break;
  922. case ImPlot3DMarker_Cross: RenderPrimitives<RendererMarkersLine>(getter, MARKER_LINE_CROSS, 4, size, weight, col_line); break;
  923. }
  924. }
  925. }
  926. //-----------------------------------------------------------------------------
  927. // [SECTION] PlotScatter
  928. //-----------------------------------------------------------------------------
  929. template <typename Getter>
  930. void PlotScatterEx(const char* label_id, const Getter& getter, ImPlot3DScatterFlags flags) {
  931. if (BeginItemEx(label_id, getter, flags, ImPlot3DCol_MarkerOutline)) {
  932. const ImPlot3DNextItemData& n = GetItemData();
  933. ImPlot3DMarker marker = n.Marker == ImPlot3DMarker_None ? ImPlot3DMarker_Circle : n.Marker;
  934. const ImU32 col_line = ImGui::GetColorU32(n.Colors[ImPlot3DCol_MarkerOutline]);
  935. const ImU32 col_fill = ImGui::GetColorU32(n.Colors[ImPlot3DCol_MarkerFill]);
  936. if (marker != ImPlot3DMarker_None)
  937. RenderMarkers<Getter>(getter, marker, n.MarkerSize, n.RenderMarkerFill, col_fill, n.RenderMarkerLine, col_line, n.MarkerWeight);
  938. EndItem();
  939. }
  940. }
  941. template <typename T>
  942. void PlotScatter(const char* label_id, const T* xs, const T* ys, const T* zs, int count, ImPlot3DScatterFlags flags, int offset, int stride) {
  943. if (count < 1)
  944. return;
  945. GetterXYZ<IndexerIdx<T>, IndexerIdx<T>, IndexerIdx<T>> getter(IndexerIdx<T>(xs, count, offset, stride), IndexerIdx<T>(ys, count, offset, stride), IndexerIdx<T>(zs, count, offset, stride), count);
  946. return PlotScatterEx(label_id, getter, flags);
  947. }
  948. #define INSTANTIATE_MACRO(T) \
  949. template IMPLOT3D_API void PlotScatter<T>(const char* label_id, const T* xs, const T* ys, const T* zs, int count, ImPlot3DScatterFlags flags, int offset, int stride);
  950. CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
  951. #undef INSTANTIATE_MACRO
  952. //-----------------------------------------------------------------------------
  953. // [SECTION] PlotLine
  954. //-----------------------------------------------------------------------------
  955. template <typename _Getter>
  956. void PlotLineEx(const char* label_id, const _Getter& getter, ImPlot3DLineFlags flags) {
  957. if (BeginItemEx(label_id, getter, flags, ImPlot3DCol_Line)) {
  958. const ImPlot3DNextItemData& n = GetItemData();
  959. if (getter.Count >= 2 && n.RenderLine) {
  960. const ImU32 col_line = ImGui::GetColorU32(n.Colors[ImPlot3DCol_Line]);
  961. if (ImHasFlag(flags, ImPlot3DLineFlags_Segments)) {
  962. RenderPrimitives<RendererLineSegments>(getter, col_line, n.LineWeight);
  963. } else if (ImHasFlag(flags, ImPlot3DLineFlags_Loop)) {
  964. if (ImHasFlag(flags, ImPlot3DLineFlags_SkipNaN))
  965. RenderPrimitives<RendererLineStripSkip>(GetterLoop<_Getter>(getter), col_line, n.LineWeight);
  966. else
  967. RenderPrimitives<RendererLineStrip>(GetterLoop<_Getter>(getter), col_line, n.LineWeight);
  968. } else {
  969. if (ImHasFlag(flags, ImPlot3DLineFlags_SkipNaN))
  970. RenderPrimitives<RendererLineStripSkip>(getter, col_line, n.LineWeight);
  971. else
  972. RenderPrimitives<RendererLineStrip>(getter, col_line, n.LineWeight);
  973. }
  974. }
  975. // Render markers
  976. if (n.Marker != ImPlot3DMarker_None) {
  977. const ImU32 col_line = ImGui::GetColorU32(n.Colors[ImPlot3DCol_MarkerOutline]);
  978. const ImU32 col_fill = ImGui::GetColorU32(n.Colors[ImPlot3DCol_MarkerFill]);
  979. RenderMarkers<_Getter>(getter, n.Marker, n.MarkerSize, n.RenderMarkerFill, col_fill, n.RenderMarkerLine, col_line, n.MarkerWeight);
  980. }
  981. EndItem();
  982. }
  983. }
  984. IMPLOT3D_TMP void PlotLine(const char* label_id, const T* xs, const T* ys, const T* zs, int count, ImPlot3DLineFlags flags, int offset, int stride) {
  985. if (count < 2)
  986. return;
  987. GetterXYZ<IndexerIdx<T>, IndexerIdx<T>, IndexerIdx<T>> getter(IndexerIdx<T>(xs, count, offset, stride), IndexerIdx<T>(ys, count, offset, stride), IndexerIdx<T>(zs, count, offset, stride), count);
  988. return PlotLineEx(label_id, getter, flags);
  989. }
  990. #define INSTANTIATE_MACRO(T) \
  991. template IMPLOT3D_API void PlotLine<T>(const char* label_id, const T* xs, const T* ys, const T* zs, int count, ImPlot3DLineFlags flags, int offset, int stride);
  992. CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
  993. #undef INSTANTIATE_MACRO
  994. //-----------------------------------------------------------------------------
  995. // [SECTION] PlotTriangle
  996. //-----------------------------------------------------------------------------
  997. template <typename _Getter>
  998. void PlotTriangleEx(const char* label_id, const _Getter& getter, ImPlot3DTriangleFlags flags) {
  999. if (BeginItemEx(label_id, getter, flags, ImPlot3DCol_Fill)) {
  1000. const ImPlot3DNextItemData& n = GetItemData();
  1001. // Render fill
  1002. if (getter.Count >= 3 && n.RenderFill) {
  1003. const ImU32 col_fill = ImGui::GetColorU32(n.Colors[ImPlot3DCol_Fill]);
  1004. RenderPrimitives<RendererTriangleFill>(getter, col_fill);
  1005. }
  1006. // Render lines
  1007. if (getter.Count >= 2 && n.RenderLine) {
  1008. const ImU32 col_line = ImGui::GetColorU32(n.Colors[ImPlot3DCol_Line]);
  1009. RenderPrimitives<RendererLineSegments>(GetterTriangleLines<_Getter>(getter), col_line, n.LineWeight);
  1010. }
  1011. // Render markers
  1012. if (n.Marker != ImPlot3DMarker_None) {
  1013. const ImU32 col_line = ImGui::GetColorU32(n.Colors[ImPlot3DCol_MarkerOutline]);
  1014. const ImU32 col_fill = ImGui::GetColorU32(n.Colors[ImPlot3DCol_MarkerFill]);
  1015. RenderMarkers<_Getter>(getter, n.Marker, n.MarkerSize, n.RenderMarkerFill, col_fill, n.RenderMarkerLine, col_line, n.MarkerWeight);
  1016. }
  1017. EndItem();
  1018. }
  1019. }
  1020. IMPLOT3D_TMP void PlotTriangle(const char* label_id, const T* xs, const T* ys, const T* zs, int count, ImPlot3DTriangleFlags flags, int offset, int stride) {
  1021. if (count < 3)
  1022. return;
  1023. GetterXYZ<IndexerIdx<T>, IndexerIdx<T>, IndexerIdx<T>> getter(IndexerIdx<T>(xs, count, offset, stride), IndexerIdx<T>(ys, count, offset, stride), IndexerIdx<T>(zs, count, offset, stride), count);
  1024. return PlotTriangleEx(label_id, getter, flags);
  1025. }
  1026. #define INSTANTIATE_MACRO(T) \
  1027. template IMPLOT3D_API void PlotTriangle<T>(const char* label_id, const T* xs, const T* ys, const T* zs, int count, ImPlot3DTriangleFlags flags, int offset, int stride);
  1028. CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
  1029. #undef INSTANTIATE_MACRO
  1030. //-----------------------------------------------------------------------------
  1031. // [SECTION] PlotQuad
  1032. //-----------------------------------------------------------------------------
  1033. template <typename _Getter>
  1034. void PlotQuadEx(const char* label_id, const _Getter& getter, ImPlot3DQuadFlags flags) {
  1035. if (BeginItemEx(label_id, getter, flags, ImPlot3DCol_Fill)) {
  1036. const ImPlot3DNextItemData& n = GetItemData();
  1037. // Render fill
  1038. if (getter.Count >= 4 && n.RenderFill) {
  1039. const ImU32 col_fill = ImGui::GetColorU32(n.Colors[ImPlot3DCol_Fill]);
  1040. RenderPrimitives<RendererQuadFill>(getter, col_fill);
  1041. }
  1042. // Render lines
  1043. if (getter.Count >= 2 && n.RenderLine) {
  1044. const ImU32 col_line = ImGui::GetColorU32(n.Colors[ImPlot3DCol_Line]);
  1045. RenderPrimitives<RendererLineSegments>(GetterQuadLines<_Getter>(getter), col_line, n.LineWeight);
  1046. }
  1047. // Render markers
  1048. if (n.Marker != ImPlot3DMarker_None) {
  1049. const ImU32 col_line = ImGui::GetColorU32(n.Colors[ImPlot3DCol_MarkerOutline]);
  1050. const ImU32 col_fill = ImGui::GetColorU32(n.Colors[ImPlot3DCol_MarkerFill]);
  1051. RenderMarkers<_Getter>(getter, n.Marker, n.MarkerSize, n.RenderMarkerFill, col_fill, n.RenderMarkerLine, col_line, n.MarkerWeight);
  1052. }
  1053. EndItem();
  1054. }
  1055. }
  1056. IMPLOT3D_TMP void PlotQuad(const char* label_id, const T* xs, const T* ys, const T* zs, int count, ImPlot3DQuadFlags flags, int offset, int stride) {
  1057. if (count < 3)
  1058. return;
  1059. GetterXYZ<IndexerIdx<T>, IndexerIdx<T>, IndexerIdx<T>> getter(IndexerIdx<T>(xs, count, offset, stride), IndexerIdx<T>(ys, count, offset, stride), IndexerIdx<T>(zs, count, offset, stride), count);
  1060. return PlotQuadEx(label_id, getter, flags);
  1061. }
  1062. #define INSTANTIATE_MACRO(T) \
  1063. template IMPLOT3D_API void PlotQuad<T>(const char* label_id, const T* xs, const T* ys, const T* zs, int count, ImPlot3DQuadFlags flags, int offset, int stride);
  1064. CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
  1065. #undef INSTANTIATE_MACRO
  1066. //-----------------------------------------------------------------------------
  1067. // [SECTION] PlotSurface
  1068. //-----------------------------------------------------------------------------
  1069. template <typename _Getter>
  1070. void PlotSurfaceEx(const char* label_id, const _Getter& getter, int x_count, int y_count, ImPlot3DSurfaceFlags flags) {
  1071. if (BeginItemEx(label_id, getter, flags, ImPlot3DCol_Fill)) {
  1072. const ImPlot3DNextItemData& n = GetItemData();
  1073. // Render fill
  1074. if (getter.Count >= 4 && n.RenderFill) {
  1075. const ImU32 col_fill = ImGui::GetColorU32(n.Colors[ImPlot3DCol_Fill]);
  1076. RenderPrimitives<RendererSurfaceFill>(getter, x_count, y_count, col_fill);
  1077. }
  1078. // Render lines
  1079. if (getter.Count >= 2 && n.RenderLine) {
  1080. const ImU32 col_line = ImGui::GetColorU32(n.Colors[ImPlot3DCol_Line]);
  1081. RenderPrimitives<RendererLineSegments>(GetterSurfaceLines<_Getter>(getter, x_count, y_count), col_line, n.LineWeight);
  1082. }
  1083. // Render markers
  1084. if (n.Marker != ImPlot3DMarker_None) {
  1085. const ImU32 col_line = ImGui::GetColorU32(n.Colors[ImPlot3DCol_MarkerOutline]);
  1086. const ImU32 col_fill = ImGui::GetColorU32(n.Colors[ImPlot3DCol_MarkerFill]);
  1087. RenderMarkers<_Getter>(getter, n.Marker, n.MarkerSize, n.RenderMarkerFill, col_fill, n.RenderMarkerLine, col_line, n.MarkerWeight);
  1088. }
  1089. EndItem();
  1090. }
  1091. }
  1092. 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, int offset, int stride) {
  1093. int count = x_count * y_count;
  1094. if (count < 4)
  1095. return;
  1096. GetterXYZ<IndexerIdx<T>, IndexerIdx<T>, IndexerIdx<T>> getter(IndexerIdx<T>(xs, count, offset, stride), IndexerIdx<T>(ys, count, offset, stride), IndexerIdx<T>(zs, count, offset, stride), count);
  1097. return PlotSurfaceEx(label_id, getter, x_count, y_count, flags);
  1098. }
  1099. #define INSTANTIATE_MACRO(T) \
  1100. template IMPLOT3D_API void PlotSurface<T>(const char* label_id, const T* xs, const T* ys, const T* zs, int x_count, int y_count, ImPlot3DSurfaceFlags flags, int offset, int stride);
  1101. CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
  1102. #undef INSTANTIATE_MACRO
  1103. void PlotMesh(const char* label_id, const ImPlot3DPoint* vtx, const unsigned int* idx, int vtx_count, int idx_count, ImPlot3DMeshFlags flags) {
  1104. Getter3DPoints getter(vtx, vtx_count); // Get vertices
  1105. GetterMeshTriangles getter_triangles(vtx, idx, idx_count); // Get triangle vertices
  1106. if (BeginItemEx(label_id, getter, flags, ImPlot3DCol_Fill)) {
  1107. const ImPlot3DNextItemData& n = GetItemData();
  1108. // Render fill
  1109. if (getter.Count >= 3 && n.RenderFill) {
  1110. const ImU32 col_fill = ImGui::GetColorU32(n.Colors[ImPlot3DCol_Fill]);
  1111. RenderPrimitives<RendererTriangleFill>(getter_triangles, col_fill);
  1112. }
  1113. // Render lines
  1114. if (getter.Count >= 2 && n.RenderLine && !n.IsAutoLine) {
  1115. const ImU32 col_line = ImGui::GetColorU32(n.Colors[ImPlot3DCol_Line]);
  1116. RenderPrimitives<RendererLineSegments>(GetterTriangleLines<GetterMeshTriangles>(getter_triangles), col_line, n.LineWeight);
  1117. }
  1118. // Render markers
  1119. if (n.Marker != ImPlot3DMarker_None) {
  1120. const ImU32 col_line = ImGui::GetColorU32(n.Colors[ImPlot3DCol_MarkerOutline]);
  1121. const ImU32 col_fill = ImGui::GetColorU32(n.Colors[ImPlot3DCol_MarkerFill]);
  1122. RenderMarkers(getter, n.Marker, n.MarkerSize, n.RenderMarkerFill, col_fill, n.RenderMarkerLine, col_line, n.MarkerWeight);
  1123. }
  1124. EndItem();
  1125. }
  1126. }
  1127. //-----------------------------------------------------------------------------
  1128. // [SECTION] PlotText
  1129. //-----------------------------------------------------------------------------
  1130. void PlotText(const char* text, float x, float y, float z, float angle, const ImVec2& pix_offset) {
  1131. ImPlot3DContext& gp = *GImPlot3D;
  1132. IM_ASSERT_USER_ERROR(gp.CurrentPlot != nullptr, "PlotText() needs to be called between BeginPlot() and EndPlot()!");
  1133. SetupLock();
  1134. ImPlot3DPlot& plot = *gp.CurrentPlot;
  1135. ImPlot3DBox cull_box;
  1136. if (ImHasFlag(plot.Flags, ImPlot3DFlags_NoClip)) {
  1137. cull_box.Min = ImPlot3DPoint(-HUGE_VAL, -HUGE_VAL, -HUGE_VAL);
  1138. cull_box.Max = ImPlot3DPoint(HUGE_VAL, HUGE_VAL, HUGE_VAL);
  1139. } else {
  1140. cull_box.Min = plot.RangeMin();
  1141. cull_box.Max = plot.RangeMax();
  1142. }
  1143. if (!cull_box.Contains(ImPlot3DPoint(x, y, z)))
  1144. return;
  1145. ImVec2 p = PlotToPixels(ImPlot3DPoint(x, y, z));
  1146. p.x += pix_offset.x;
  1147. p.y += pix_offset.y;
  1148. AddTextRotated(GetPlotDrawList(), p, angle, GetStyleColorU32(ImPlot3DCol_InlayText), text);
  1149. }
  1150. } // namespace ImPlot3D
  1151. #endif // #ifndef IMGUI_DISABLE