implot_items.cpp 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692
  1. // MIT License
  2. // Copyright (c) 2023 Evan Pezent
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. // ImPlot v0.16
  19. #define IMGUI_DEFINE_MATH_OPERATORS
  20. #include "implot.h"
  21. #include "implot_internal.h"
  22. //-----------------------------------------------------------------------------
  23. // [SECTION] Macros and Defines
  24. //-----------------------------------------------------------------------------
  25. #define SQRT_1_2 0.70710678118f
  26. #define SQRT_3_2 0.86602540378f
  27. #ifndef IMPLOT_NO_FORCE_INLINE
  28. #ifdef _MSC_VER
  29. #define IMPLOT_INLINE __forceinline
  30. #elif defined(__GNUC__)
  31. #define IMPLOT_INLINE inline __attribute__((__always_inline__))
  32. #elif defined(__CLANG__)
  33. #if __has_attribute(__always_inline__)
  34. #define IMPLOT_INLINE inline __attribute__((__always_inline__))
  35. #else
  36. #define IMPLOT_INLINE inline
  37. #endif
  38. #else
  39. #define IMPLOT_INLINE inline
  40. #endif
  41. #else
  42. #define IMPLOT_INLINE inline
  43. #endif
  44. #if defined __SSE__ || defined __x86_64__ || defined _M_X64
  45. #ifndef IMGUI_ENABLE_SSE
  46. #include <immintrin.h>
  47. #endif
  48. static IMPLOT_INLINE float ImInvSqrt(float x) { return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x))); }
  49. #else
  50. static IMPLOT_INLINE float ImInvSqrt(float x) { return 1.0f / sqrtf(x); }
  51. #endif
  52. #define IMPLOT_NORMALIZE2F_OVER_ZERO(VX,VY) do { float d2 = VX*VX + VY*VY; if (d2 > 0.0f) { float inv_len = ImInvSqrt(d2); VX *= inv_len; VY *= inv_len; } } while (0)
  53. // Support for pre-1.82 versions. Users on 1.82+ can use 0 (default) flags to mean "all corners" but in order to support older versions we are more explicit.
  54. #if (IMGUI_VERSION_NUM < 18102) && !defined(ImDrawFlags_RoundCornersAll)
  55. #define ImDrawFlags_RoundCornersAll ImDrawCornerFlags_All
  56. #endif
  57. //-----------------------------------------------------------------------------
  58. // [SECTION] Template instantiation utility
  59. //-----------------------------------------------------------------------------
  60. // By default, templates are instantiated for `float`, `double`, and for the following integer types, which are defined in imgui.h:
  61. // signed char ImS8; // 8-bit signed integer
  62. // unsigned char ImU8; // 8-bit unsigned integer
  63. // signed short ImS16; // 16-bit signed integer
  64. // unsigned short ImU16; // 16-bit unsigned integer
  65. // signed int ImS32; // 32-bit signed integer == int
  66. // unsigned int ImU32; // 32-bit unsigned integer
  67. // signed long long ImS64; // 64-bit signed integer
  68. // unsigned long long ImU64; // 64-bit unsigned integer
  69. // (note: this list does *not* include `long`, `unsigned long` and `long double`)
  70. //
  71. // You can customize the supported types by defining IMPLOT_CUSTOM_NUMERIC_TYPES at compile time to define your own type list.
  72. // As an example, you could use the compile time define given by the line below in order to support only float and double.
  73. // -DIMPLOT_CUSTOM_NUMERIC_TYPES="(float)(double)"
  74. // In order to support all known C++ types, use:
  75. // -DIMPLOT_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)"
  76. #ifdef IMPLOT_CUSTOM_NUMERIC_TYPES
  77. #define IMPLOT_NUMERIC_TYPES IMPLOT_CUSTOM_NUMERIC_TYPES
  78. #else
  79. #define IMPLOT_NUMERIC_TYPES (ImS8)(ImU8)(ImS16)(ImU16)(ImS32)(ImU32)(ImS64)(ImU64)(float)(double)
  80. #endif
  81. // CALL_INSTANTIATE_FOR_NUMERIC_TYPES will duplicate the template instantion code `INSTANTIATE_MACRO(T)` on supported types.
  82. #define _CAT(x, y) _CAT_(x, y)
  83. #define _CAT_(x,y) x ## y
  84. #define _INSTANTIATE_FOR_NUMERIC_TYPES(chain) _CAT(_INSTANTIATE_FOR_NUMERIC_TYPES_1 chain, _END)
  85. #define _INSTANTIATE_FOR_NUMERIC_TYPES_1(T) INSTANTIATE_MACRO(T) _INSTANTIATE_FOR_NUMERIC_TYPES_2
  86. #define _INSTANTIATE_FOR_NUMERIC_TYPES_2(T) INSTANTIATE_MACRO(T) _INSTANTIATE_FOR_NUMERIC_TYPES_1
  87. #define _INSTANTIATE_FOR_NUMERIC_TYPES_1_END
  88. #define _INSTANTIATE_FOR_NUMERIC_TYPES_2_END
  89. #define CALL_INSTANTIATE_FOR_NUMERIC_TYPES() _INSTANTIATE_FOR_NUMERIC_TYPES(IMPLOT_NUMERIC_TYPES)
  90. namespace ImPlot {
  91. //-----------------------------------------------------------------------------
  92. // [SECTION] Utils
  93. //-----------------------------------------------------------------------------
  94. // Calc maximum index size of ImDrawIdx
  95. template <typename T>
  96. struct MaxIdx { static const unsigned int Value; };
  97. template <> const unsigned int MaxIdx<unsigned short>::Value = 65535;
  98. template <> const unsigned int MaxIdx<unsigned int>::Value = 4294967295;
  99. IMPLOT_INLINE void GetLineRenderProps(const ImDrawList& draw_list, float& half_weight, ImVec2& tex_uv0, ImVec2& tex_uv1) {
  100. const bool aa = ImHasFlag(draw_list.Flags, ImDrawListFlags_AntiAliasedLines) &&
  101. ImHasFlag(draw_list.Flags, ImDrawListFlags_AntiAliasedLinesUseTex);
  102. if (aa) {
  103. ImVec4 tex_uvs = draw_list._Data->TexUvLines[(int)(half_weight*2)];
  104. tex_uv0 = ImVec2(tex_uvs.x, tex_uvs.y);
  105. tex_uv1 = ImVec2(tex_uvs.z, tex_uvs.w);
  106. half_weight += 1;
  107. }
  108. else {
  109. tex_uv0 = tex_uv1 = draw_list._Data->TexUvWhitePixel;
  110. }
  111. }
  112. IMPLOT_INLINE void PrimLine(ImDrawList& draw_list, const ImVec2& P1, const ImVec2& P2, float half_weight, ImU32 col, const ImVec2& tex_uv0, const ImVec2 tex_uv1) {
  113. float dx = P2.x - P1.x;
  114. float dy = P2.y - P1.y;
  115. IMPLOT_NORMALIZE2F_OVER_ZERO(dx, dy);
  116. dx *= half_weight;
  117. dy *= half_weight;
  118. draw_list._VtxWritePtr[0].pos.x = P1.x + dy;
  119. draw_list._VtxWritePtr[0].pos.y = P1.y - dx;
  120. draw_list._VtxWritePtr[0].uv = tex_uv0;
  121. draw_list._VtxWritePtr[0].col = col;
  122. draw_list._VtxWritePtr[1].pos.x = P2.x + dy;
  123. draw_list._VtxWritePtr[1].pos.y = P2.y - dx;
  124. draw_list._VtxWritePtr[1].uv = tex_uv0;
  125. draw_list._VtxWritePtr[1].col = col;
  126. draw_list._VtxWritePtr[2].pos.x = P2.x - dy;
  127. draw_list._VtxWritePtr[2].pos.y = P2.y + dx;
  128. draw_list._VtxWritePtr[2].uv = tex_uv1;
  129. draw_list._VtxWritePtr[2].col = col;
  130. draw_list._VtxWritePtr[3].pos.x = P1.x - dy;
  131. draw_list._VtxWritePtr[3].pos.y = P1.y + dx;
  132. draw_list._VtxWritePtr[3].uv = tex_uv1;
  133. draw_list._VtxWritePtr[3].col = col;
  134. draw_list._VtxWritePtr += 4;
  135. draw_list._IdxWritePtr[0] = (ImDrawIdx)(draw_list._VtxCurrentIdx);
  136. draw_list._IdxWritePtr[1] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 1);
  137. draw_list._IdxWritePtr[2] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 2);
  138. draw_list._IdxWritePtr[3] = (ImDrawIdx)(draw_list._VtxCurrentIdx);
  139. draw_list._IdxWritePtr[4] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 2);
  140. draw_list._IdxWritePtr[5] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 3);
  141. draw_list._IdxWritePtr += 6;
  142. draw_list._VtxCurrentIdx += 4;
  143. }
  144. IMPLOT_INLINE void PrimRectFill(ImDrawList& draw_list, const ImVec2& Pmin, const ImVec2& Pmax, ImU32 col, const ImVec2& uv) {
  145. draw_list._VtxWritePtr[0].pos = Pmin;
  146. draw_list._VtxWritePtr[0].uv = uv;
  147. draw_list._VtxWritePtr[0].col = col;
  148. draw_list._VtxWritePtr[1].pos = Pmax;
  149. draw_list._VtxWritePtr[1].uv = uv;
  150. draw_list._VtxWritePtr[1].col = col;
  151. draw_list._VtxWritePtr[2].pos.x = Pmin.x;
  152. draw_list._VtxWritePtr[2].pos.y = Pmax.y;
  153. draw_list._VtxWritePtr[2].uv = uv;
  154. draw_list._VtxWritePtr[2].col = col;
  155. draw_list._VtxWritePtr[3].pos.x = Pmax.x;
  156. draw_list._VtxWritePtr[3].pos.y = Pmin.y;
  157. draw_list._VtxWritePtr[3].uv = uv;
  158. draw_list._VtxWritePtr[3].col = col;
  159. draw_list._VtxWritePtr += 4;
  160. draw_list._IdxWritePtr[0] = (ImDrawIdx)(draw_list._VtxCurrentIdx);
  161. draw_list._IdxWritePtr[1] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 1);
  162. draw_list._IdxWritePtr[2] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 2);
  163. draw_list._IdxWritePtr[3] = (ImDrawIdx)(draw_list._VtxCurrentIdx);
  164. draw_list._IdxWritePtr[4] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 1);
  165. draw_list._IdxWritePtr[5] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 3);
  166. draw_list._IdxWritePtr += 6;
  167. draw_list._VtxCurrentIdx += 4;
  168. }
  169. IMPLOT_INLINE void PrimRectLine(ImDrawList& draw_list, const ImVec2& Pmin, const ImVec2& Pmax, float weight, ImU32 col, const ImVec2& uv) {
  170. draw_list._VtxWritePtr[0].pos.x = Pmin.x;
  171. draw_list._VtxWritePtr[0].pos.y = Pmin.y;
  172. draw_list._VtxWritePtr[0].uv = uv;
  173. draw_list._VtxWritePtr[0].col = col;
  174. draw_list._VtxWritePtr[1].pos.x = Pmin.x;
  175. draw_list._VtxWritePtr[1].pos.y = Pmax.y;
  176. draw_list._VtxWritePtr[1].uv = uv;
  177. draw_list._VtxWritePtr[1].col = col;
  178. draw_list._VtxWritePtr[2].pos.x = Pmax.x;
  179. draw_list._VtxWritePtr[2].pos.y = Pmax.y;
  180. draw_list._VtxWritePtr[2].uv = uv;
  181. draw_list._VtxWritePtr[2].col = col;
  182. draw_list._VtxWritePtr[3].pos.x = Pmax.x;
  183. draw_list._VtxWritePtr[3].pos.y = Pmin.y;
  184. draw_list._VtxWritePtr[3].uv = uv;
  185. draw_list._VtxWritePtr[3].col = col;
  186. draw_list._VtxWritePtr[4].pos.x = Pmin.x + weight;
  187. draw_list._VtxWritePtr[4].pos.y = Pmin.y + weight;
  188. draw_list._VtxWritePtr[4].uv = uv;
  189. draw_list._VtxWritePtr[4].col = col;
  190. draw_list._VtxWritePtr[5].pos.x = Pmin.x + weight;
  191. draw_list._VtxWritePtr[5].pos.y = Pmax.y - weight;
  192. draw_list._VtxWritePtr[5].uv = uv;
  193. draw_list._VtxWritePtr[5].col = col;
  194. draw_list._VtxWritePtr[6].pos.x = Pmax.x - weight;
  195. draw_list._VtxWritePtr[6].pos.y = Pmax.y - weight;
  196. draw_list._VtxWritePtr[6].uv = uv;
  197. draw_list._VtxWritePtr[6].col = col;
  198. draw_list._VtxWritePtr[7].pos.x = Pmax.x - weight;
  199. draw_list._VtxWritePtr[7].pos.y = Pmin.y + weight;
  200. draw_list._VtxWritePtr[7].uv = uv;
  201. draw_list._VtxWritePtr[7].col = col;
  202. draw_list._VtxWritePtr += 8;
  203. draw_list._IdxWritePtr[0] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 0);
  204. draw_list._IdxWritePtr[1] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 1);
  205. draw_list._IdxWritePtr[2] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 5);
  206. draw_list._IdxWritePtr += 3;
  207. draw_list._IdxWritePtr[0] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 0);
  208. draw_list._IdxWritePtr[1] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 5);
  209. draw_list._IdxWritePtr[2] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 4);
  210. draw_list._IdxWritePtr += 3;
  211. draw_list._IdxWritePtr[0] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 1);
  212. draw_list._IdxWritePtr[1] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 2);
  213. draw_list._IdxWritePtr[2] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 6);
  214. draw_list._IdxWritePtr += 3;
  215. draw_list._IdxWritePtr[0] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 1);
  216. draw_list._IdxWritePtr[1] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 6);
  217. draw_list._IdxWritePtr[2] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 5);
  218. draw_list._IdxWritePtr += 3;
  219. draw_list._IdxWritePtr[0] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 2);
  220. draw_list._IdxWritePtr[1] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 3);
  221. draw_list._IdxWritePtr[2] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 7);
  222. draw_list._IdxWritePtr += 3;
  223. draw_list._IdxWritePtr[0] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 2);
  224. draw_list._IdxWritePtr[1] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 7);
  225. draw_list._IdxWritePtr[2] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 6);
  226. draw_list._IdxWritePtr += 3;
  227. draw_list._IdxWritePtr[0] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 3);
  228. draw_list._IdxWritePtr[1] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 0);
  229. draw_list._IdxWritePtr[2] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 4);
  230. draw_list._IdxWritePtr += 3;
  231. draw_list._IdxWritePtr[0] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 3);
  232. draw_list._IdxWritePtr[1] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 4);
  233. draw_list._IdxWritePtr[2] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 7);
  234. draw_list._IdxWritePtr += 3;
  235. draw_list._VtxCurrentIdx += 8;
  236. }
  237. //-----------------------------------------------------------------------------
  238. // [SECTION] Item Utils
  239. //-----------------------------------------------------------------------------
  240. ImPlotItem* RegisterOrGetItem(const char* label_id, ImPlotItemFlags flags, bool* just_created) {
  241. ImPlotContext& gp = *GImPlot;
  242. ImPlotItemGroup& Items = *gp.CurrentItems;
  243. ImGuiID id = Items.GetItemID(label_id);
  244. if (just_created != nullptr)
  245. *just_created = Items.GetItem(id) == nullptr;
  246. ImPlotItem* item = Items.GetOrAddItem(id);
  247. if (item->SeenThisFrame)
  248. return item;
  249. item->SeenThisFrame = true;
  250. int idx = Items.GetItemIndex(item);
  251. item->ID = id;
  252. if (!ImHasFlag(flags, ImPlotItemFlags_NoLegend) && ImGui::FindRenderedTextEnd(label_id, nullptr) != label_id) {
  253. Items.Legend.Indices.push_back(idx);
  254. item->NameOffset = Items.Legend.Labels.size();
  255. Items.Legend.Labels.append(label_id, label_id + strlen(label_id) + 1);
  256. }
  257. else {
  258. item->Show = true;
  259. }
  260. return item;
  261. }
  262. ImPlotItem* GetItem(const char* label_id) {
  263. ImPlotContext& gp = *GImPlot;
  264. return gp.CurrentItems->GetItem(label_id);
  265. }
  266. bool IsItemHidden(const char* label_id) {
  267. ImPlotItem* item = GetItem(label_id);
  268. return item != nullptr && !item->Show;
  269. }
  270. ImPlotItem* GetCurrentItem() {
  271. ImPlotContext& gp = *GImPlot;
  272. return gp.CurrentItem;
  273. }
  274. void SetNextLineStyle(const ImVec4& col, float weight) {
  275. ImPlotContext& gp = *GImPlot;
  276. gp.NextItemData.Colors[ImPlotCol_Line] = col;
  277. gp.NextItemData.LineWeight = weight;
  278. }
  279. void SetNextFillStyle(const ImVec4& col, float alpha) {
  280. ImPlotContext& gp = *GImPlot;
  281. gp.NextItemData.Colors[ImPlotCol_Fill] = col;
  282. gp.NextItemData.FillAlpha = alpha;
  283. }
  284. void SetNextMarkerStyle(ImPlotMarker marker, float size, const ImVec4& fill, float weight, const ImVec4& outline) {
  285. ImPlotContext& gp = *GImPlot;
  286. gp.NextItemData.Marker = marker;
  287. gp.NextItemData.Colors[ImPlotCol_MarkerFill] = fill;
  288. gp.NextItemData.MarkerSize = size;
  289. gp.NextItemData.Colors[ImPlotCol_MarkerOutline] = outline;
  290. gp.NextItemData.MarkerWeight = weight;
  291. }
  292. void SetNextErrorBarStyle(const ImVec4& col, float size, float weight) {
  293. ImPlotContext& gp = *GImPlot;
  294. gp.NextItemData.Colors[ImPlotCol_ErrorBar] = col;
  295. gp.NextItemData.ErrorBarSize = size;
  296. gp.NextItemData.ErrorBarWeight = weight;
  297. }
  298. ImVec4 GetLastItemColor() {
  299. ImPlotContext& gp = *GImPlot;
  300. if (gp.PreviousItem)
  301. return ImGui::ColorConvertU32ToFloat4(gp.PreviousItem->Color);
  302. return ImVec4();
  303. }
  304. void BustItemCache() {
  305. ImPlotContext& gp = *GImPlot;
  306. for (int p = 0; p < gp.Plots.GetBufSize(); ++p) {
  307. ImPlotPlot& plot = *gp.Plots.GetByIndex(p);
  308. plot.Items.Reset();
  309. }
  310. for (int p = 0; p < gp.Subplots.GetBufSize(); ++p) {
  311. ImPlotSubplot& subplot = *gp.Subplots.GetByIndex(p);
  312. subplot.Items.Reset();
  313. }
  314. }
  315. void BustColorCache(const char* plot_title_id) {
  316. ImPlotContext& gp = *GImPlot;
  317. if (plot_title_id == nullptr) {
  318. BustItemCache();
  319. }
  320. else {
  321. ImGuiID id = ImGui::GetCurrentWindow()->GetID(plot_title_id);
  322. ImPlotPlot* plot = gp.Plots.GetByKey(id);
  323. if (plot != nullptr)
  324. plot->Items.Reset();
  325. else {
  326. ImPlotSubplot* subplot = gp.Subplots.GetByKey(id);
  327. if (subplot != nullptr)
  328. subplot->Items.Reset();
  329. }
  330. }
  331. }
  332. //-----------------------------------------------------------------------------
  333. // [SECTION] BeginItem / EndItem
  334. //-----------------------------------------------------------------------------
  335. static const float ITEM_HIGHLIGHT_LINE_SCALE = 2.0f;
  336. static const float ITEM_HIGHLIGHT_MARK_SCALE = 1.25f;
  337. // Begins a new item. Returns false if the item should not be plotted.
  338. bool BeginItem(const char* label_id, ImPlotItemFlags flags, ImPlotCol recolor_from) {
  339. ImPlotContext& gp = *GImPlot;
  340. IM_ASSERT_USER_ERROR(gp.CurrentPlot != nullptr, "PlotX() needs to be called between BeginPlot() and EndPlot()!");
  341. SetupLock();
  342. bool just_created;
  343. ImPlotItem* item = RegisterOrGetItem(label_id, flags, &just_created);
  344. // set current item
  345. gp.CurrentItem = item;
  346. ImPlotNextItemData& s = gp.NextItemData;
  347. // set/override item color
  348. if (recolor_from != -1) {
  349. if (!IsColorAuto(s.Colors[recolor_from]))
  350. item->Color = ImGui::ColorConvertFloat4ToU32(s.Colors[recolor_from]);
  351. else if (!IsColorAuto(gp.Style.Colors[recolor_from]))
  352. item->Color = ImGui::ColorConvertFloat4ToU32(gp.Style.Colors[recolor_from]);
  353. else if (just_created)
  354. item->Color = NextColormapColorU32();
  355. }
  356. else if (just_created) {
  357. item->Color = NextColormapColorU32();
  358. }
  359. // hide/show item
  360. if (gp.NextItemData.HasHidden) {
  361. if (just_created || gp.NextItemData.HiddenCond == ImGuiCond_Always)
  362. item->Show = !gp.NextItemData.Hidden;
  363. }
  364. if (!item->Show) {
  365. // reset next item data
  366. gp.NextItemData.Reset();
  367. gp.PreviousItem = item;
  368. gp.CurrentItem = nullptr;
  369. return false;
  370. }
  371. else {
  372. ImVec4 item_color = ImGui::ColorConvertU32ToFloat4(item->Color);
  373. // stage next item colors
  374. s.Colors[ImPlotCol_Line] = IsColorAuto(s.Colors[ImPlotCol_Line]) ? ( IsColorAuto(ImPlotCol_Line) ? item_color : gp.Style.Colors[ImPlotCol_Line] ) : s.Colors[ImPlotCol_Line];
  375. s.Colors[ImPlotCol_Fill] = IsColorAuto(s.Colors[ImPlotCol_Fill]) ? ( IsColorAuto(ImPlotCol_Fill) ? item_color : gp.Style.Colors[ImPlotCol_Fill] ) : s.Colors[ImPlotCol_Fill];
  376. s.Colors[ImPlotCol_MarkerOutline] = IsColorAuto(s.Colors[ImPlotCol_MarkerOutline]) ? ( IsColorAuto(ImPlotCol_MarkerOutline) ? s.Colors[ImPlotCol_Line] : gp.Style.Colors[ImPlotCol_MarkerOutline] ) : s.Colors[ImPlotCol_MarkerOutline];
  377. s.Colors[ImPlotCol_MarkerFill] = IsColorAuto(s.Colors[ImPlotCol_MarkerFill]) ? ( IsColorAuto(ImPlotCol_MarkerFill) ? s.Colors[ImPlotCol_Line] : gp.Style.Colors[ImPlotCol_MarkerFill] ) : s.Colors[ImPlotCol_MarkerFill];
  378. s.Colors[ImPlotCol_ErrorBar] = IsColorAuto(s.Colors[ImPlotCol_ErrorBar]) ? ( GetStyleColorVec4(ImPlotCol_ErrorBar) ) : s.Colors[ImPlotCol_ErrorBar];
  379. // stage next item style vars
  380. s.LineWeight = s.LineWeight < 0 ? gp.Style.LineWeight : s.LineWeight;
  381. s.Marker = s.Marker < 0 ? gp.Style.Marker : s.Marker;
  382. s.MarkerSize = s.MarkerSize < 0 ? gp.Style.MarkerSize : s.MarkerSize;
  383. s.MarkerWeight = s.MarkerWeight < 0 ? gp.Style.MarkerWeight : s.MarkerWeight;
  384. s.FillAlpha = s.FillAlpha < 0 ? gp.Style.FillAlpha : s.FillAlpha;
  385. s.ErrorBarSize = s.ErrorBarSize < 0 ? gp.Style.ErrorBarSize : s.ErrorBarSize;
  386. s.ErrorBarWeight = s.ErrorBarWeight < 0 ? gp.Style.ErrorBarWeight : s.ErrorBarWeight;
  387. s.DigitalBitHeight = s.DigitalBitHeight < 0 ? gp.Style.DigitalBitHeight : s.DigitalBitHeight;
  388. s.DigitalBitGap = s.DigitalBitGap < 0 ? gp.Style.DigitalBitGap : s.DigitalBitGap;
  389. // apply alpha modifier(s)
  390. s.Colors[ImPlotCol_Fill].w *= s.FillAlpha;
  391. s.Colors[ImPlotCol_MarkerFill].w *= s.FillAlpha; // TODO: this should be separate, if it at all
  392. // apply highlight mods
  393. if (item->LegendHovered) {
  394. if (!ImHasFlag(gp.CurrentItems->Legend.Flags, ImPlotLegendFlags_NoHighlightItem)) {
  395. s.LineWeight *= ITEM_HIGHLIGHT_LINE_SCALE;
  396. s.MarkerSize *= ITEM_HIGHLIGHT_MARK_SCALE;
  397. s.MarkerWeight *= ITEM_HIGHLIGHT_LINE_SCALE;
  398. // TODO: how to highlight fills?
  399. }
  400. if (!ImHasFlag(gp.CurrentItems->Legend.Flags, ImPlotLegendFlags_NoHighlightAxis)) {
  401. if (gp.CurrentPlot->EnabledAxesX() > 1)
  402. gp.CurrentPlot->Axes[gp.CurrentPlot->CurrentX].ColorHiLi = item->Color;
  403. if (gp.CurrentPlot->EnabledAxesY() > 1)
  404. gp.CurrentPlot->Axes[gp.CurrentPlot->CurrentY].ColorHiLi = item->Color;
  405. }
  406. }
  407. // set render flags
  408. s.RenderLine = s.Colors[ImPlotCol_Line].w > 0 && s.LineWeight > 0;
  409. s.RenderFill = s.Colors[ImPlotCol_Fill].w > 0;
  410. s.RenderMarkerFill = s.Colors[ImPlotCol_MarkerFill].w > 0;
  411. s.RenderMarkerLine = s.Colors[ImPlotCol_MarkerOutline].w > 0 && s.MarkerWeight > 0;
  412. // push rendering clip rect
  413. PushPlotClipRect();
  414. return true;
  415. }
  416. }
  417. // Ends an item (call only if BeginItem returns true)
  418. void EndItem() {
  419. ImPlotContext& gp = *GImPlot;
  420. // pop rendering clip rect
  421. PopPlotClipRect();
  422. // reset next item data
  423. gp.NextItemData.Reset();
  424. // set current item
  425. gp.PreviousItem = gp.CurrentItem;
  426. gp.CurrentItem = nullptr;
  427. }
  428. //-----------------------------------------------------------------------------
  429. // [SECTION] Indexers
  430. //-----------------------------------------------------------------------------
  431. template <typename T>
  432. IMPLOT_INLINE T IndexData(const T* data, int idx, int count, int offset, int stride) {
  433. const int s = ((offset == 0) << 0) | ((stride == sizeof(T)) << 1);
  434. switch (s) {
  435. case 3 : return data[idx];
  436. case 2 : return data[(offset + idx) % count];
  437. case 1 : return *(const T*)(const void*)((const unsigned char*)data + (size_t)((idx) ) * stride);
  438. case 0 : return *(const T*)(const void*)((const unsigned char*)data + (size_t)((offset + idx) % count) * stride);
  439. default: return T(0);
  440. }
  441. }
  442. template <typename T>
  443. struct IndexerIdx {
  444. IndexerIdx(const T* data, int count, int offset = 0, int stride = sizeof(T)) :
  445. Data(data),
  446. Count(count),
  447. Offset(count ? ImPosMod(offset, count) : 0),
  448. Stride(stride)
  449. { }
  450. template <typename I> IMPLOT_INLINE double operator()(I idx) const {
  451. return (double)IndexData(Data, idx, Count, Offset, Stride);
  452. }
  453. const T* Data;
  454. int Count;
  455. int Offset;
  456. int Stride;
  457. };
  458. template <typename _Indexer1, typename _Indexer2>
  459. struct IndexerAdd {
  460. IndexerAdd(const _Indexer1& indexer1, const _Indexer2& indexer2, double scale1 = 1, double scale2 = 1)
  461. : Indexer1(indexer1),
  462. Indexer2(indexer2),
  463. Scale1(scale1),
  464. Scale2(scale2),
  465. Count(ImMin(Indexer1.Count, Indexer2.Count))
  466. { }
  467. template <typename I> IMPLOT_INLINE double operator()(I idx) const {
  468. return Scale1 * Indexer1(idx) + Scale2 * Indexer2(idx);
  469. }
  470. const _Indexer1& Indexer1;
  471. const _Indexer2& Indexer2;
  472. double Scale1;
  473. double Scale2;
  474. int Count;
  475. };
  476. struct IndexerLin {
  477. IndexerLin(double m, double b) : M(m), B(b) { }
  478. template <typename I> IMPLOT_INLINE double operator()(I idx) const {
  479. return M * idx + B;
  480. }
  481. const double M;
  482. const double B;
  483. };
  484. struct IndexerConst {
  485. IndexerConst(double ref) : Ref(ref) { }
  486. template <typename I> IMPLOT_INLINE double operator()(I) const { return Ref; }
  487. const double Ref;
  488. };
  489. //-----------------------------------------------------------------------------
  490. // [SECTION] Getters
  491. //-----------------------------------------------------------------------------
  492. template <typename _IndexerX, typename _IndexerY>
  493. struct GetterXY {
  494. GetterXY(_IndexerX x, _IndexerY y, int count) : IndxerX(x), IndxerY(y), Count(count) { }
  495. template <typename I> IMPLOT_INLINE ImPlotPoint operator()(I idx) const {
  496. return ImPlotPoint(IndxerX(idx),IndxerY(idx));
  497. }
  498. const _IndexerX IndxerX;
  499. const _IndexerY IndxerY;
  500. const int Count;
  501. };
  502. /// Interprets a user's function pointer as ImPlotPoints
  503. struct GetterFuncPtr {
  504. GetterFuncPtr(ImPlotGetter getter, void* data, int count) :
  505. Getter(getter),
  506. Data(data),
  507. Count(count)
  508. { }
  509. template <typename I> IMPLOT_INLINE ImPlotPoint operator()(I idx) const {
  510. return Getter(idx, Data);
  511. }
  512. ImPlotGetter Getter;
  513. void* const Data;
  514. const int Count;
  515. };
  516. template <typename _Getter>
  517. struct GetterOverrideX {
  518. GetterOverrideX(_Getter getter, double x) : Getter(getter), X(x), Count(getter.Count) { }
  519. template <typename I> IMPLOT_INLINE ImPlotPoint operator()(I idx) const {
  520. ImPlotPoint p = Getter(idx);
  521. p.x = X;
  522. return p;
  523. }
  524. const _Getter Getter;
  525. const double X;
  526. const int Count;
  527. };
  528. template <typename _Getter>
  529. struct GetterOverrideY {
  530. GetterOverrideY(_Getter getter, double y) : Getter(getter), Y(y), Count(getter.Count) { }
  531. template <typename I> IMPLOT_INLINE ImPlotPoint operator()(I idx) const {
  532. ImPlotPoint p = Getter(idx);
  533. p.y = Y;
  534. return p;
  535. }
  536. const _Getter Getter;
  537. const double Y;
  538. const int Count;
  539. };
  540. template <typename _Getter>
  541. struct GetterLoop {
  542. GetterLoop(_Getter getter) : Getter(getter), Count(getter.Count + 1) { }
  543. template <typename I> IMPLOT_INLINE ImPlotPoint operator()(I idx) const {
  544. idx = idx % (Count - 1);
  545. return Getter(idx);
  546. }
  547. const _Getter Getter;
  548. const int Count;
  549. };
  550. template <typename T>
  551. struct GetterError {
  552. GetterError(const T* xs, const T* ys, const T* neg, const T* pos, int count, int offset, int stride) :
  553. Xs(xs),
  554. Ys(ys),
  555. Neg(neg),
  556. Pos(pos),
  557. Count(count),
  558. Offset(count ? ImPosMod(offset, count) : 0),
  559. Stride(stride)
  560. { }
  561. template <typename I> IMPLOT_INLINE ImPlotPointError operator()(I idx) const {
  562. return ImPlotPointError((double)IndexData(Xs, idx, Count, Offset, Stride),
  563. (double)IndexData(Ys, idx, Count, Offset, Stride),
  564. (double)IndexData(Neg, idx, Count, Offset, Stride),
  565. (double)IndexData(Pos, idx, Count, Offset, Stride));
  566. }
  567. const T* const Xs;
  568. const T* const Ys;
  569. const T* const Neg;
  570. const T* const Pos;
  571. const int Count;
  572. const int Offset;
  573. const int Stride;
  574. };
  575. //-----------------------------------------------------------------------------
  576. // [SECTION] Fitters
  577. //-----------------------------------------------------------------------------
  578. template <typename _Getter1>
  579. struct Fitter1 {
  580. Fitter1(const _Getter1& getter) : Getter(getter) { }
  581. void Fit(ImPlotAxis& x_axis, ImPlotAxis& y_axis) const {
  582. for (int i = 0; i < Getter.Count; ++i) {
  583. ImPlotPoint p = Getter(i);
  584. x_axis.ExtendFitWith(y_axis, p.x, p.y);
  585. y_axis.ExtendFitWith(x_axis, p.y, p.x);
  586. }
  587. }
  588. const _Getter1& Getter;
  589. };
  590. template <typename _Getter1>
  591. struct FitterX {
  592. FitterX(const _Getter1& getter) : Getter(getter) { }
  593. void Fit(ImPlotAxis& x_axis, ImPlotAxis&) const {
  594. for (int i = 0; i < Getter.Count; ++i) {
  595. ImPlotPoint p = Getter(i);
  596. x_axis.ExtendFit(p.x);
  597. }
  598. }
  599. const _Getter1& Getter;
  600. };
  601. template <typename _Getter1>
  602. struct FitterY {
  603. FitterY(const _Getter1& getter) : Getter(getter) { }
  604. void Fit(ImPlotAxis&, ImPlotAxis& y_axis) const {
  605. for (int i = 0; i < Getter.Count; ++i) {
  606. ImPlotPoint p = Getter(i);
  607. y_axis.ExtendFit(p.y);
  608. }
  609. }
  610. const _Getter1& Getter;
  611. };
  612. template <typename _Getter1, typename _Getter2>
  613. struct Fitter2 {
  614. Fitter2(const _Getter1& getter1, const _Getter2& getter2) : Getter1(getter1), Getter2(getter2) { }
  615. void Fit(ImPlotAxis& x_axis, ImPlotAxis& y_axis) const {
  616. for (int i = 0; i < Getter1.Count; ++i) {
  617. ImPlotPoint p = Getter1(i);
  618. x_axis.ExtendFitWith(y_axis, p.x, p.y);
  619. y_axis.ExtendFitWith(x_axis, p.y, p.x);
  620. }
  621. for (int i = 0; i < Getter2.Count; ++i) {
  622. ImPlotPoint p = Getter2(i);
  623. x_axis.ExtendFitWith(y_axis, p.x, p.y);
  624. y_axis.ExtendFitWith(x_axis, p.y, p.x);
  625. }
  626. }
  627. const _Getter1& Getter1;
  628. const _Getter2& Getter2;
  629. };
  630. template <typename _Getter1, typename _Getter2>
  631. struct FitterBarV {
  632. FitterBarV(const _Getter1& getter1, const _Getter2& getter2, double width) :
  633. Getter1(getter1),
  634. Getter2(getter2),
  635. HalfWidth(width*0.5)
  636. { }
  637. void Fit(ImPlotAxis& x_axis, ImPlotAxis& y_axis) const {
  638. int count = ImMin(Getter1.Count, Getter2.Count);
  639. for (int i = 0; i < count; ++i) {
  640. ImPlotPoint p1 = Getter1(i); p1.x -= HalfWidth;
  641. ImPlotPoint p2 = Getter2(i); p2.x += HalfWidth;
  642. x_axis.ExtendFitWith(y_axis, p1.x, p1.y);
  643. y_axis.ExtendFitWith(x_axis, p1.y, p1.x);
  644. x_axis.ExtendFitWith(y_axis, p2.x, p2.y);
  645. y_axis.ExtendFitWith(x_axis, p2.y, p2.x);
  646. }
  647. }
  648. const _Getter1& Getter1;
  649. const _Getter2& Getter2;
  650. const double HalfWidth;
  651. };
  652. template <typename _Getter1, typename _Getter2>
  653. struct FitterBarH {
  654. FitterBarH(const _Getter1& getter1, const _Getter2& getter2, double height) :
  655. Getter1(getter1),
  656. Getter2(getter2),
  657. HalfHeight(height*0.5)
  658. { }
  659. void Fit(ImPlotAxis& x_axis, ImPlotAxis& y_axis) const {
  660. int count = ImMin(Getter1.Count, Getter2.Count);
  661. for (int i = 0; i < count; ++i) {
  662. ImPlotPoint p1 = Getter1(i); p1.y -= HalfHeight;
  663. ImPlotPoint p2 = Getter2(i); p2.y += HalfHeight;
  664. x_axis.ExtendFitWith(y_axis, p1.x, p1.y);
  665. y_axis.ExtendFitWith(x_axis, p1.y, p1.x);
  666. x_axis.ExtendFitWith(y_axis, p2.x, p2.y);
  667. y_axis.ExtendFitWith(x_axis, p2.y, p2.x);
  668. }
  669. }
  670. const _Getter1& Getter1;
  671. const _Getter2& Getter2;
  672. const double HalfHeight;
  673. };
  674. struct FitterRect {
  675. FitterRect(const ImPlotPoint& pmin, const ImPlotPoint& pmax) :
  676. Pmin(pmin),
  677. Pmax(pmax)
  678. { }
  679. FitterRect(const ImPlotRect& rect) :
  680. FitterRect(rect.Min(), rect.Max())
  681. { }
  682. void Fit(ImPlotAxis& x_axis, ImPlotAxis& y_axis) const {
  683. x_axis.ExtendFitWith(y_axis, Pmin.x, Pmin.y);
  684. y_axis.ExtendFitWith(x_axis, Pmin.y, Pmin.x);
  685. x_axis.ExtendFitWith(y_axis, Pmax.x, Pmax.y);
  686. y_axis.ExtendFitWith(x_axis, Pmax.y, Pmax.x);
  687. }
  688. const ImPlotPoint Pmin;
  689. const ImPlotPoint Pmax;
  690. };
  691. //-----------------------------------------------------------------------------
  692. // [SECTION] Transformers
  693. //-----------------------------------------------------------------------------
  694. struct Transformer1 {
  695. Transformer1(double pixMin, double pltMin, double pltMax, double m, double scaMin, double scaMax, ImPlotTransform fwd, void* data) :
  696. ScaMin(scaMin),
  697. ScaMax(scaMax),
  698. PltMin(pltMin),
  699. PltMax(pltMax),
  700. PixMin(pixMin),
  701. M(m),
  702. TransformFwd(fwd),
  703. TransformData(data)
  704. { }
  705. template <typename T> IMPLOT_INLINE float operator()(T p) const {
  706. if (TransformFwd != nullptr) {
  707. double s = TransformFwd(p, TransformData);
  708. double t = (s - ScaMin) / (ScaMax - ScaMin);
  709. p = PltMin + (PltMax - PltMin) * t;
  710. }
  711. return (float)(PixMin + M * (p - PltMin));
  712. }
  713. double ScaMin, ScaMax, PltMin, PltMax, PixMin, M;
  714. ImPlotTransform TransformFwd;
  715. void* TransformData;
  716. };
  717. struct Transformer2 {
  718. Transformer2(const ImPlotAxis& x_axis, const ImPlotAxis& y_axis) :
  719. Tx(x_axis.PixelMin,
  720. x_axis.Range.Min,
  721. x_axis.Range.Max,
  722. x_axis.ScaleToPixel,
  723. x_axis.ScaleMin,
  724. x_axis.ScaleMax,
  725. x_axis.TransformForward,
  726. x_axis.TransformData),
  727. Ty(y_axis.PixelMin,
  728. y_axis.Range.Min,
  729. y_axis.Range.Max,
  730. y_axis.ScaleToPixel,
  731. y_axis.ScaleMin,
  732. y_axis.ScaleMax,
  733. y_axis.TransformForward,
  734. y_axis.TransformData)
  735. { }
  736. Transformer2(const ImPlotPlot& plot) :
  737. Transformer2(plot.Axes[plot.CurrentX], plot.Axes[plot.CurrentY])
  738. { }
  739. Transformer2() :
  740. Transformer2(*GImPlot->CurrentPlot)
  741. { }
  742. template <typename P> IMPLOT_INLINE ImVec2 operator()(const P& plt) const {
  743. ImVec2 out;
  744. out.x = Tx(plt.x);
  745. out.y = Ty(plt.y);
  746. return out;
  747. }
  748. template <typename T> IMPLOT_INLINE ImVec2 operator()(T x, T y) const {
  749. ImVec2 out;
  750. out.x = Tx(x);
  751. out.y = Ty(y);
  752. return out;
  753. }
  754. Transformer1 Tx;
  755. Transformer1 Ty;
  756. };
  757. //-----------------------------------------------------------------------------
  758. // [SECTION] Renderers
  759. //-----------------------------------------------------------------------------
  760. struct RendererBase {
  761. RendererBase(int prims, int idx_consumed, int vtx_consumed) :
  762. Prims(prims),
  763. IdxConsumed(idx_consumed),
  764. VtxConsumed(vtx_consumed)
  765. { }
  766. const int Prims;
  767. Transformer2 Transformer;
  768. const int IdxConsumed;
  769. const int VtxConsumed;
  770. };
  771. template <class _Getter>
  772. struct RendererLineStrip : RendererBase {
  773. RendererLineStrip(const _Getter& getter, ImU32 col, float weight) :
  774. RendererBase(getter.Count - 1, 6, 4),
  775. Getter(getter),
  776. Col(col),
  777. HalfWeight(ImMax(1.0f,weight)*0.5f)
  778. {
  779. P1 = this->Transformer(Getter(0));
  780. }
  781. void Init(ImDrawList& draw_list) const {
  782. GetLineRenderProps(draw_list, HalfWeight, UV0, UV1);
  783. }
  784. IMPLOT_INLINE bool Render(ImDrawList& draw_list, const ImRect& cull_rect, int prim) const {
  785. ImVec2 P2 = this->Transformer(Getter(prim + 1));
  786. if (!cull_rect.Overlaps(ImRect(ImMin(P1, P2), ImMax(P1, P2)))) {
  787. P1 = P2;
  788. return false;
  789. }
  790. PrimLine(draw_list,P1,P2,HalfWeight,Col,UV0,UV1);
  791. P1 = P2;
  792. return true;
  793. }
  794. const _Getter& Getter;
  795. const ImU32 Col;
  796. mutable float HalfWeight;
  797. mutable ImVec2 P1;
  798. mutable ImVec2 UV0;
  799. mutable ImVec2 UV1;
  800. };
  801. template <class _Getter>
  802. struct RendererLineStripSkip : RendererBase {
  803. RendererLineStripSkip(const _Getter& getter, ImU32 col, float weight) :
  804. RendererBase(getter.Count - 1, 6, 4),
  805. Getter(getter),
  806. Col(col),
  807. HalfWeight(ImMax(1.0f,weight)*0.5f)
  808. {
  809. P1 = this->Transformer(Getter(0));
  810. }
  811. void Init(ImDrawList& draw_list) const {
  812. GetLineRenderProps(draw_list, HalfWeight, UV0, UV1);
  813. }
  814. IMPLOT_INLINE bool Render(ImDrawList& draw_list, const ImRect& cull_rect, int prim) const {
  815. ImVec2 P2 = this->Transformer(Getter(prim + 1));
  816. if (!cull_rect.Overlaps(ImRect(ImMin(P1, P2), ImMax(P1, P2)))) {
  817. if (!ImNan(P2.x) && !ImNan(P2.y))
  818. P1 = P2;
  819. return false;
  820. }
  821. PrimLine(draw_list,P1,P2,HalfWeight,Col,UV0,UV1);
  822. if (!ImNan(P2.x) && !ImNan(P2.y))
  823. P1 = P2;
  824. return true;
  825. }
  826. const _Getter& Getter;
  827. const ImU32 Col;
  828. mutable float HalfWeight;
  829. mutable ImVec2 P1;
  830. mutable ImVec2 UV0;
  831. mutable ImVec2 UV1;
  832. };
  833. template <class _Getter>
  834. struct RendererLineSegments1 : RendererBase {
  835. RendererLineSegments1(const _Getter& getter, ImU32 col, float weight) :
  836. RendererBase(getter.Count / 2, 6, 4),
  837. Getter(getter),
  838. Col(col),
  839. HalfWeight(ImMax(1.0f,weight)*0.5f)
  840. { }
  841. void Init(ImDrawList& draw_list) const {
  842. GetLineRenderProps(draw_list, HalfWeight, UV0, UV1);
  843. }
  844. IMPLOT_INLINE bool Render(ImDrawList& draw_list, const ImRect& cull_rect, int prim) const {
  845. ImVec2 P1 = this->Transformer(Getter(prim*2+0));
  846. ImVec2 P2 = this->Transformer(Getter(prim*2+1));
  847. if (!cull_rect.Overlaps(ImRect(ImMin(P1, P2), ImMax(P1, P2))))
  848. return false;
  849. PrimLine(draw_list,P1,P2,HalfWeight,Col,UV0,UV1);
  850. return true;
  851. }
  852. const _Getter& Getter;
  853. const ImU32 Col;
  854. mutable float HalfWeight;
  855. mutable ImVec2 UV0;
  856. mutable ImVec2 UV1;
  857. };
  858. template <class _Getter1, class _Getter2>
  859. struct RendererLineSegments2 : RendererBase {
  860. RendererLineSegments2(const _Getter1& getter1, const _Getter2& getter2, ImU32 col, float weight) :
  861. RendererBase(ImMin(getter1.Count, getter1.Count), 6, 4),
  862. Getter1(getter1),
  863. Getter2(getter2),
  864. Col(col),
  865. HalfWeight(ImMax(1.0f,weight)*0.5f)
  866. {}
  867. void Init(ImDrawList& draw_list) const {
  868. GetLineRenderProps(draw_list, HalfWeight, UV0, UV1);
  869. }
  870. IMPLOT_INLINE bool Render(ImDrawList& draw_list, const ImRect& cull_rect, int prim) const {
  871. ImVec2 P1 = this->Transformer(Getter1(prim));
  872. ImVec2 P2 = this->Transformer(Getter2(prim));
  873. if (!cull_rect.Overlaps(ImRect(ImMin(P1, P2), ImMax(P1, P2))))
  874. return false;
  875. PrimLine(draw_list,P1,P2,HalfWeight,Col,UV0,UV1);
  876. return true;
  877. }
  878. const _Getter1& Getter1;
  879. const _Getter2& Getter2;
  880. const ImU32 Col;
  881. mutable float HalfWeight;
  882. mutable ImVec2 UV0;
  883. mutable ImVec2 UV1;
  884. };
  885. template <class _Getter1, class _Getter2>
  886. struct RendererBarsFillV : RendererBase {
  887. RendererBarsFillV(const _Getter1& getter1, const _Getter2& getter2, ImU32 col, double width) :
  888. RendererBase(ImMin(getter1.Count, getter1.Count), 6, 4),
  889. Getter1(getter1),
  890. Getter2(getter2),
  891. Col(col),
  892. HalfWidth(width/2)
  893. {}
  894. void Init(ImDrawList& draw_list) const {
  895. UV = draw_list._Data->TexUvWhitePixel;
  896. }
  897. IMPLOT_INLINE bool Render(ImDrawList& draw_list, const ImRect& cull_rect, int prim) const {
  898. ImPlotPoint p1 = Getter1(prim);
  899. ImPlotPoint p2 = Getter2(prim);
  900. p1.x += HalfWidth;
  901. p2.x -= HalfWidth;
  902. ImVec2 P1 = this->Transformer(p1);
  903. ImVec2 P2 = this->Transformer(p2);
  904. float width_px = ImAbs(P1.x-P2.x);
  905. if (width_px < 1.0f) {
  906. P1.x += P1.x > P2.x ? (1-width_px) / 2 : (width_px-1) / 2;
  907. P2.x += P2.x > P1.x ? (1-width_px) / 2 : (width_px-1) / 2;
  908. }
  909. ImVec2 PMin = ImMin(P1, P2);
  910. ImVec2 PMax = ImMax(P1, P2);
  911. if (!cull_rect.Overlaps(ImRect(PMin, PMax)))
  912. return false;
  913. PrimRectFill(draw_list,PMin,PMax,Col,UV);
  914. return true;
  915. }
  916. const _Getter1& Getter1;
  917. const _Getter2& Getter2;
  918. const ImU32 Col;
  919. const double HalfWidth;
  920. mutable ImVec2 UV;
  921. };
  922. template <class _Getter1, class _Getter2>
  923. struct RendererBarsFillH : RendererBase {
  924. RendererBarsFillH(const _Getter1& getter1, const _Getter2& getter2, ImU32 col, double height) :
  925. RendererBase(ImMin(getter1.Count, getter1.Count), 6, 4),
  926. Getter1(getter1),
  927. Getter2(getter2),
  928. Col(col),
  929. HalfHeight(height/2)
  930. {}
  931. void Init(ImDrawList& draw_list) const {
  932. UV = draw_list._Data->TexUvWhitePixel;
  933. }
  934. IMPLOT_INLINE bool Render(ImDrawList& draw_list, const ImRect& cull_rect, int prim) const {
  935. ImPlotPoint p1 = Getter1(prim);
  936. ImPlotPoint p2 = Getter2(prim);
  937. p1.y += HalfHeight;
  938. p2.y -= HalfHeight;
  939. ImVec2 P1 = this->Transformer(p1);
  940. ImVec2 P2 = this->Transformer(p2);
  941. float height_px = ImAbs(P1.y-P2.y);
  942. if (height_px < 1.0f) {
  943. P1.y += P1.y > P2.y ? (1-height_px) / 2 : (height_px-1) / 2;
  944. P2.y += P2.y > P1.y ? (1-height_px) / 2 : (height_px-1) / 2;
  945. }
  946. ImVec2 PMin = ImMin(P1, P2);
  947. ImVec2 PMax = ImMax(P1, P2);
  948. if (!cull_rect.Overlaps(ImRect(PMin, PMax)))
  949. return false;
  950. PrimRectFill(draw_list,PMin,PMax,Col,UV);
  951. return true;
  952. }
  953. const _Getter1& Getter1;
  954. const _Getter2& Getter2;
  955. const ImU32 Col;
  956. const double HalfHeight;
  957. mutable ImVec2 UV;
  958. };
  959. template <class _Getter1, class _Getter2>
  960. struct RendererBarsLineV : RendererBase {
  961. RendererBarsLineV(const _Getter1& getter1, const _Getter2& getter2, ImU32 col, double width, float weight) :
  962. RendererBase(ImMin(getter1.Count, getter1.Count), 24, 8),
  963. Getter1(getter1),
  964. Getter2(getter2),
  965. Col(col),
  966. HalfWidth(width/2),
  967. Weight(weight)
  968. {}
  969. void Init(ImDrawList& draw_list) const {
  970. UV = draw_list._Data->TexUvWhitePixel;
  971. }
  972. IMPLOT_INLINE bool Render(ImDrawList& draw_list, const ImRect& cull_rect, int prim) const {
  973. ImPlotPoint p1 = Getter1(prim);
  974. ImPlotPoint p2 = Getter2(prim);
  975. p1.x += HalfWidth;
  976. p2.x -= HalfWidth;
  977. ImVec2 P1 = this->Transformer(p1);
  978. ImVec2 P2 = this->Transformer(p2);
  979. float width_px = ImAbs(P1.x-P2.x);
  980. if (width_px < 1.0f) {
  981. P1.x += P1.x > P2.x ? (1-width_px) / 2 : (width_px-1) / 2;
  982. P2.x += P2.x > P1.x ? (1-width_px) / 2 : (width_px-1) / 2;
  983. }
  984. ImVec2 PMin = ImMin(P1, P2);
  985. ImVec2 PMax = ImMax(P1, P2);
  986. if (!cull_rect.Overlaps(ImRect(PMin, PMax)))
  987. return false;
  988. PrimRectLine(draw_list,PMin,PMax,Weight,Col,UV);
  989. return true;
  990. }
  991. const _Getter1& Getter1;
  992. const _Getter2& Getter2;
  993. const ImU32 Col;
  994. const double HalfWidth;
  995. const float Weight;
  996. mutable ImVec2 UV;
  997. };
  998. template <class _Getter1, class _Getter2>
  999. struct RendererBarsLineH : RendererBase {
  1000. RendererBarsLineH(const _Getter1& getter1, const _Getter2& getter2, ImU32 col, double height, float weight) :
  1001. RendererBase(ImMin(getter1.Count, getter1.Count), 24, 8),
  1002. Getter1(getter1),
  1003. Getter2(getter2),
  1004. Col(col),
  1005. HalfHeight(height/2),
  1006. Weight(weight)
  1007. {}
  1008. void Init(ImDrawList& draw_list) const {
  1009. UV = draw_list._Data->TexUvWhitePixel;
  1010. }
  1011. IMPLOT_INLINE bool Render(ImDrawList& draw_list, const ImRect& cull_rect, int prim) const {
  1012. ImPlotPoint p1 = Getter1(prim);
  1013. ImPlotPoint p2 = Getter2(prim);
  1014. p1.y += HalfHeight;
  1015. p2.y -= HalfHeight;
  1016. ImVec2 P1 = this->Transformer(p1);
  1017. ImVec2 P2 = this->Transformer(p2);
  1018. float height_px = ImAbs(P1.y-P2.y);
  1019. if (height_px < 1.0f) {
  1020. P1.y += P1.y > P2.y ? (1-height_px) / 2 : (height_px-1) / 2;
  1021. P2.y += P2.y > P1.y ? (1-height_px) / 2 : (height_px-1) / 2;
  1022. }
  1023. ImVec2 PMin = ImMin(P1, P2);
  1024. ImVec2 PMax = ImMax(P1, P2);
  1025. if (!cull_rect.Overlaps(ImRect(PMin, PMax)))
  1026. return false;
  1027. PrimRectLine(draw_list,PMin,PMax,Weight,Col,UV);
  1028. return true;
  1029. }
  1030. const _Getter1& Getter1;
  1031. const _Getter2& Getter2;
  1032. const ImU32 Col;
  1033. const double HalfHeight;
  1034. const float Weight;
  1035. mutable ImVec2 UV;
  1036. };
  1037. template <class _Getter>
  1038. struct RendererStairsPre : RendererBase {
  1039. RendererStairsPre(const _Getter& getter, ImU32 col, float weight) :
  1040. RendererBase(getter.Count - 1, 12, 8),
  1041. Getter(getter),
  1042. Col(col),
  1043. HalfWeight(ImMax(1.0f,weight)*0.5f)
  1044. {
  1045. P1 = this->Transformer(Getter(0));
  1046. }
  1047. void Init(ImDrawList& draw_list) const {
  1048. UV = draw_list._Data->TexUvWhitePixel;
  1049. }
  1050. IMPLOT_INLINE bool Render(ImDrawList& draw_list, const ImRect& cull_rect, int prim) const {
  1051. ImVec2 P2 = this->Transformer(Getter(prim + 1));
  1052. if (!cull_rect.Overlaps(ImRect(ImMin(P1, P2), ImMax(P1, P2)))) {
  1053. P1 = P2;
  1054. return false;
  1055. }
  1056. PrimRectFill(draw_list, ImVec2(P1.x - HalfWeight, P1.y), ImVec2(P1.x + HalfWeight, P2.y), Col, UV);
  1057. PrimRectFill(draw_list, ImVec2(P1.x, P2.y + HalfWeight), ImVec2(P2.x, P2.y - HalfWeight), Col, UV);
  1058. P1 = P2;
  1059. return true;
  1060. }
  1061. const _Getter& Getter;
  1062. const ImU32 Col;
  1063. mutable float HalfWeight;
  1064. mutable ImVec2 P1;
  1065. mutable ImVec2 UV;
  1066. };
  1067. template <class _Getter>
  1068. struct RendererStairsPost : RendererBase {
  1069. RendererStairsPost(const _Getter& getter, ImU32 col, float weight) :
  1070. RendererBase(getter.Count - 1, 12, 8),
  1071. Getter(getter),
  1072. Col(col),
  1073. HalfWeight(ImMax(1.0f,weight) * 0.5f)
  1074. {
  1075. P1 = this->Transformer(Getter(0));
  1076. }
  1077. void Init(ImDrawList& draw_list) const {
  1078. UV = draw_list._Data->TexUvWhitePixel;
  1079. }
  1080. IMPLOT_INLINE bool Render(ImDrawList& draw_list, const ImRect& cull_rect, int prim) const {
  1081. ImVec2 P2 = this->Transformer(Getter(prim + 1));
  1082. if (!cull_rect.Overlaps(ImRect(ImMin(P1, P2), ImMax(P1, P2)))) {
  1083. P1 = P2;
  1084. return false;
  1085. }
  1086. PrimRectFill(draw_list, ImVec2(P1.x, P1.y + HalfWeight), ImVec2(P2.x, P1.y - HalfWeight), Col, UV);
  1087. PrimRectFill(draw_list, ImVec2(P2.x - HalfWeight, P2.y), ImVec2(P2.x + HalfWeight, P1.y), Col, UV);
  1088. P1 = P2;
  1089. return true;
  1090. }
  1091. const _Getter& Getter;
  1092. const ImU32 Col;
  1093. mutable float HalfWeight;
  1094. mutable ImVec2 P1;
  1095. mutable ImVec2 UV;
  1096. };
  1097. template <class _Getter>
  1098. struct RendererStairsPreShaded : RendererBase {
  1099. RendererStairsPreShaded(const _Getter& getter, ImU32 col) :
  1100. RendererBase(getter.Count - 1, 6, 4),
  1101. Getter(getter),
  1102. Col(col)
  1103. {
  1104. P1 = this->Transformer(Getter(0));
  1105. Y0 = this->Transformer(ImPlotPoint(0,0)).y;
  1106. }
  1107. void Init(ImDrawList& draw_list) const {
  1108. UV = draw_list._Data->TexUvWhitePixel;
  1109. }
  1110. IMPLOT_INLINE bool Render(ImDrawList& draw_list, const ImRect& cull_rect, int prim) const {
  1111. ImVec2 P2 = this->Transformer(Getter(prim + 1));
  1112. ImVec2 PMin(ImMin(P1.x, P2.x), ImMin(Y0, P2.y));
  1113. ImVec2 PMax(ImMax(P1.x, P2.x), ImMax(Y0, P2.y));
  1114. if (!cull_rect.Overlaps(ImRect(PMin, PMax))) {
  1115. P1 = P2;
  1116. return false;
  1117. }
  1118. PrimRectFill(draw_list, PMin, PMax, Col, UV);
  1119. P1 = P2;
  1120. return true;
  1121. }
  1122. const _Getter& Getter;
  1123. const ImU32 Col;
  1124. float Y0;
  1125. mutable ImVec2 P1;
  1126. mutable ImVec2 UV;
  1127. };
  1128. template <class _Getter>
  1129. struct RendererStairsPostShaded : RendererBase {
  1130. RendererStairsPostShaded(const _Getter& getter, ImU32 col) :
  1131. RendererBase(getter.Count - 1, 6, 4),
  1132. Getter(getter),
  1133. Col(col)
  1134. {
  1135. P1 = this->Transformer(Getter(0));
  1136. Y0 = this->Transformer(ImPlotPoint(0,0)).y;
  1137. }
  1138. void Init(ImDrawList& draw_list) const {
  1139. UV = draw_list._Data->TexUvWhitePixel;
  1140. }
  1141. IMPLOT_INLINE bool Render(ImDrawList& draw_list, const ImRect& cull_rect, int prim) const {
  1142. ImVec2 P2 = this->Transformer(Getter(prim + 1));
  1143. ImVec2 PMin(ImMin(P1.x, P2.x), ImMin(P1.y, Y0));
  1144. ImVec2 PMax(ImMax(P1.x, P2.x), ImMax(P1.y, Y0));
  1145. if (!cull_rect.Overlaps(ImRect(PMin, PMax))) {
  1146. P1 = P2;
  1147. return false;
  1148. }
  1149. PrimRectFill(draw_list, PMin, PMax, Col, UV);
  1150. P1 = P2;
  1151. return true;
  1152. }
  1153. const _Getter& Getter;
  1154. const ImU32 Col;
  1155. float Y0;
  1156. mutable ImVec2 P1;
  1157. mutable ImVec2 UV;
  1158. };
  1159. template <class _Getter1, class _Getter2>
  1160. struct RendererShaded : RendererBase {
  1161. RendererShaded(const _Getter1& getter1, const _Getter2& getter2, ImU32 col) :
  1162. RendererBase(ImMin(getter1.Count, getter2.Count) - 1, 6, 5),
  1163. Getter1(getter1),
  1164. Getter2(getter2),
  1165. Col(col)
  1166. {
  1167. P11 = this->Transformer(Getter1(0));
  1168. P12 = this->Transformer(Getter2(0));
  1169. }
  1170. void Init(ImDrawList& draw_list) const {
  1171. UV = draw_list._Data->TexUvWhitePixel;
  1172. }
  1173. IMPLOT_INLINE bool Render(ImDrawList& draw_list, const ImRect& cull_rect, int prim) const {
  1174. ImVec2 P21 = this->Transformer(Getter1(prim+1));
  1175. ImVec2 P22 = this->Transformer(Getter2(prim+1));
  1176. ImRect rect(ImMin(ImMin(ImMin(P11,P12),P21),P22), ImMax(ImMax(ImMax(P11,P12),P21),P22));
  1177. if (!cull_rect.Overlaps(rect)) {
  1178. P11 = P21;
  1179. P12 = P22;
  1180. return false;
  1181. }
  1182. const int intersect = (P11.y > P12.y && P22.y > P21.y) || (P12.y > P11.y && P21.y > P22.y);
  1183. ImVec2 intersection = Intersection(P11,P21,P12,P22);
  1184. draw_list._VtxWritePtr[0].pos = P11;
  1185. draw_list._VtxWritePtr[0].uv = UV;
  1186. draw_list._VtxWritePtr[0].col = Col;
  1187. draw_list._VtxWritePtr[1].pos = P21;
  1188. draw_list._VtxWritePtr[1].uv = UV;
  1189. draw_list._VtxWritePtr[1].col = Col;
  1190. draw_list._VtxWritePtr[2].pos = intersection;
  1191. draw_list._VtxWritePtr[2].uv = UV;
  1192. draw_list._VtxWritePtr[2].col = Col;
  1193. draw_list._VtxWritePtr[3].pos = P12;
  1194. draw_list._VtxWritePtr[3].uv = UV;
  1195. draw_list._VtxWritePtr[3].col = Col;
  1196. draw_list._VtxWritePtr[4].pos = P22;
  1197. draw_list._VtxWritePtr[4].uv = UV;
  1198. draw_list._VtxWritePtr[4].col = Col;
  1199. draw_list._VtxWritePtr += 5;
  1200. draw_list._IdxWritePtr[0] = (ImDrawIdx)(draw_list._VtxCurrentIdx);
  1201. draw_list._IdxWritePtr[1] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 1 + intersect);
  1202. draw_list._IdxWritePtr[2] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 3);
  1203. draw_list._IdxWritePtr[3] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 1);
  1204. draw_list._IdxWritePtr[4] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 4);
  1205. draw_list._IdxWritePtr[5] = (ImDrawIdx)(draw_list._VtxCurrentIdx + 3 - intersect);
  1206. draw_list._IdxWritePtr += 6;
  1207. draw_list._VtxCurrentIdx += 5;
  1208. P11 = P21;
  1209. P12 = P22;
  1210. return true;
  1211. }
  1212. const _Getter1& Getter1;
  1213. const _Getter2& Getter2;
  1214. const ImU32 Col;
  1215. mutable ImVec2 P11;
  1216. mutable ImVec2 P12;
  1217. mutable ImVec2 UV;
  1218. };
  1219. struct RectC {
  1220. ImPlotPoint Pos;
  1221. ImPlotPoint HalfSize;
  1222. ImU32 Color;
  1223. };
  1224. template <typename _Getter>
  1225. struct RendererRectC : RendererBase {
  1226. RendererRectC(const _Getter& getter) :
  1227. RendererBase(getter.Count, 6, 4),
  1228. Getter(getter)
  1229. {}
  1230. void Init(ImDrawList& draw_list) const {
  1231. UV = draw_list._Data->TexUvWhitePixel;
  1232. }
  1233. IMPLOT_INLINE bool Render(ImDrawList& draw_list, const ImRect& cull_rect, int prim) const {
  1234. RectC rect = Getter(prim);
  1235. ImVec2 P1 = this->Transformer(rect.Pos.x - rect.HalfSize.x , rect.Pos.y - rect.HalfSize.y);
  1236. ImVec2 P2 = this->Transformer(rect.Pos.x + rect.HalfSize.x , rect.Pos.y + rect.HalfSize.y);
  1237. if ((rect.Color & IM_COL32_A_MASK) == 0 || !cull_rect.Overlaps(ImRect(ImMin(P1, P2), ImMax(P1, P2))))
  1238. return false;
  1239. PrimRectFill(draw_list,P1,P2,rect.Color,UV);
  1240. return true;
  1241. }
  1242. const _Getter& Getter;
  1243. mutable ImVec2 UV;
  1244. };
  1245. //-----------------------------------------------------------------------------
  1246. // [SECTION] RenderPrimitives
  1247. //-----------------------------------------------------------------------------
  1248. /// Renders primitive shapes in bulk as efficiently as possible.
  1249. template <class _Renderer>
  1250. void RenderPrimitivesEx(const _Renderer& renderer, ImDrawList& draw_list, const ImRect& cull_rect) {
  1251. unsigned int prims = renderer.Prims;
  1252. unsigned int prims_culled = 0;
  1253. unsigned int idx = 0;
  1254. renderer.Init(draw_list);
  1255. while (prims) {
  1256. // find how many can be reserved up to end of current draw command's limit
  1257. unsigned int cnt = ImMin(prims, (MaxIdx<ImDrawIdx>::Value - draw_list._VtxCurrentIdx) / renderer.VtxConsumed);
  1258. // make sure at least this many elements can be rendered to avoid situations where at the end of buffer this slow path is not taken all the time
  1259. if (cnt >= ImMin(64u, prims)) {
  1260. if (prims_culled >= cnt)
  1261. prims_culled -= cnt; // reuse previous reservation
  1262. else {
  1263. // add more elements to previous reservation
  1264. draw_list.PrimReserve((cnt - prims_culled) * renderer.IdxConsumed, (cnt - prims_culled) * renderer.VtxConsumed);
  1265. prims_culled = 0;
  1266. }
  1267. }
  1268. else
  1269. {
  1270. if (prims_culled > 0) {
  1271. draw_list.PrimUnreserve(prims_culled * renderer.IdxConsumed, prims_culled * renderer.VtxConsumed);
  1272. prims_culled = 0;
  1273. }
  1274. cnt = ImMin(prims, (MaxIdx<ImDrawIdx>::Value - 0/*draw_list._VtxCurrentIdx*/) / renderer.VtxConsumed);
  1275. // reserve new draw command
  1276. draw_list.PrimReserve(cnt * renderer.IdxConsumed, cnt * renderer.VtxConsumed);
  1277. }
  1278. prims -= cnt;
  1279. for (unsigned int ie = idx + cnt; idx != ie; ++idx) {
  1280. if (!renderer.Render(draw_list, cull_rect, idx))
  1281. prims_culled++;
  1282. }
  1283. }
  1284. if (prims_culled > 0)
  1285. draw_list.PrimUnreserve(prims_culled * renderer.IdxConsumed, prims_culled * renderer.VtxConsumed);
  1286. }
  1287. template <template <class> class _Renderer, class _Getter, typename ...Args>
  1288. void RenderPrimitives1(const _Getter& getter, Args... args) {
  1289. ImDrawList& draw_list = *GetPlotDrawList();
  1290. const ImRect& cull_rect = GetCurrentPlot()->PlotRect;
  1291. RenderPrimitivesEx(_Renderer<_Getter>(getter,args...), draw_list, cull_rect);
  1292. }
  1293. template <template <class,class> class _Renderer, class _Getter1, class _Getter2, typename ...Args>
  1294. void RenderPrimitives2(const _Getter1& getter1, const _Getter2& getter2, Args... args) {
  1295. ImDrawList& draw_list = *GetPlotDrawList();
  1296. const ImRect& cull_rect = GetCurrentPlot()->PlotRect;
  1297. RenderPrimitivesEx(_Renderer<_Getter1,_Getter2>(getter1,getter2,args...), draw_list, cull_rect);
  1298. }
  1299. //-----------------------------------------------------------------------------
  1300. // [SECTION] Markers
  1301. //-----------------------------------------------------------------------------
  1302. template <class _Getter>
  1303. struct RendererMarkersFill : RendererBase {
  1304. RendererMarkersFill(const _Getter& getter, const ImVec2* marker, int count, float size, ImU32 col) :
  1305. RendererBase(getter.Count, (count-2)*3, count),
  1306. Getter(getter),
  1307. Marker(marker),
  1308. Count(count),
  1309. Size(size),
  1310. Col(col)
  1311. { }
  1312. void Init(ImDrawList& draw_list) const {
  1313. UV = draw_list._Data->TexUvWhitePixel;
  1314. }
  1315. IMPLOT_INLINE bool Render(ImDrawList& draw_list, const ImRect& cull_rect, int prim) const {
  1316. ImVec2 p = this->Transformer(Getter(prim));
  1317. if (p.x >= cull_rect.Min.x && p.y >= cull_rect.Min.y && p.x <= cull_rect.Max.x && p.y <= cull_rect.Max.y) {
  1318. for (int i = 0; i < Count; i++) {
  1319. draw_list._VtxWritePtr[0].pos.x = p.x + Marker[i].x * Size;
  1320. draw_list._VtxWritePtr[0].pos.y = p.y + Marker[i].y * Size;
  1321. draw_list._VtxWritePtr[0].uv = UV;
  1322. draw_list._VtxWritePtr[0].col = Col;
  1323. draw_list._VtxWritePtr++;
  1324. }
  1325. for (int i = 2; i < Count; i++) {
  1326. draw_list._IdxWritePtr[0] = (ImDrawIdx)(draw_list._VtxCurrentIdx);
  1327. draw_list._IdxWritePtr[1] = (ImDrawIdx)(draw_list._VtxCurrentIdx + i - 1);
  1328. draw_list._IdxWritePtr[2] = (ImDrawIdx)(draw_list._VtxCurrentIdx + i);
  1329. draw_list._IdxWritePtr += 3;
  1330. }
  1331. draw_list._VtxCurrentIdx += (ImDrawIdx)Count;
  1332. return true;
  1333. }
  1334. return false;
  1335. }
  1336. const _Getter& Getter;
  1337. const ImVec2* Marker;
  1338. const int Count;
  1339. const float Size;
  1340. const ImU32 Col;
  1341. mutable ImVec2 UV;
  1342. };
  1343. template <class _Getter>
  1344. struct RendererMarkersLine : RendererBase {
  1345. RendererMarkersLine(const _Getter& getter, const ImVec2* marker, int count, float size, float weight, ImU32 col) :
  1346. RendererBase(getter.Count, count/2*6, count/2*4),
  1347. Getter(getter),
  1348. Marker(marker),
  1349. Count(count),
  1350. HalfWeight(ImMax(1.0f,weight)*0.5f),
  1351. Size(size),
  1352. Col(col)
  1353. { }
  1354. void Init(ImDrawList& draw_list) const {
  1355. GetLineRenderProps(draw_list, HalfWeight, UV0, UV1);
  1356. }
  1357. IMPLOT_INLINE bool Render(ImDrawList& draw_list, const ImRect& cull_rect, int prim) const {
  1358. ImVec2 p = this->Transformer(Getter(prim));
  1359. if (p.x >= cull_rect.Min.x && p.y >= cull_rect.Min.y && p.x <= cull_rect.Max.x && p.y <= cull_rect.Max.y) {
  1360. for (int i = 0; i < Count; i = i + 2) {
  1361. ImVec2 p1(p.x + Marker[i].x * Size, p.y + Marker[i].y * Size);
  1362. ImVec2 p2(p.x + Marker[i+1].x * Size, p.y + Marker[i+1].y * Size);
  1363. PrimLine(draw_list, p1, p2, HalfWeight, Col, UV0, UV1);
  1364. }
  1365. return true;
  1366. }
  1367. return false;
  1368. }
  1369. const _Getter& Getter;
  1370. const ImVec2* Marker;
  1371. const int Count;
  1372. mutable float HalfWeight;
  1373. const float Size;
  1374. const ImU32 Col;
  1375. mutable ImVec2 UV0;
  1376. mutable ImVec2 UV1;
  1377. };
  1378. 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)};
  1379. 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)};
  1380. static const ImVec2 MARKER_FILL_DIAMOND[4] = {ImVec2(1, 0), ImVec2(0, -1), ImVec2(-1, 0), ImVec2(0, 1)};
  1381. static const ImVec2 MARKER_FILL_UP[3] = {ImVec2(SQRT_3_2,0.5f),ImVec2(0,-1),ImVec2(-SQRT_3_2,0.5f)};
  1382. static const ImVec2 MARKER_FILL_DOWN[3] = {ImVec2(SQRT_3_2,-0.5f),ImVec2(0,1),ImVec2(-SQRT_3_2,-0.5f)};
  1383. static const ImVec2 MARKER_FILL_LEFT[3] = {ImVec2(-1,0), ImVec2(0.5, SQRT_3_2), ImVec2(0.5, -SQRT_3_2)};
  1384. static const ImVec2 MARKER_FILL_RIGHT[3] = {ImVec2(1,0), ImVec2(-0.5, SQRT_3_2), ImVec2(-0.5, -SQRT_3_2)};
  1385. static const ImVec2 MARKER_LINE_CIRCLE[20] = {
  1386. ImVec2(1.0f, 0.0f),
  1387. ImVec2(0.809017f, 0.58778524f),
  1388. ImVec2(0.809017f, 0.58778524f),
  1389. ImVec2(0.30901697f, 0.95105654f),
  1390. ImVec2(0.30901697f, 0.95105654f),
  1391. ImVec2(-0.30901703f, 0.9510565f),
  1392. ImVec2(-0.30901703f, 0.9510565f),
  1393. ImVec2(-0.80901706f, 0.5877852f),
  1394. ImVec2(-0.80901706f, 0.5877852f),
  1395. ImVec2(-1.0f, 0.0f),
  1396. ImVec2(-1.0f, 0.0f),
  1397. ImVec2(-0.80901694f, -0.58778536f),
  1398. ImVec2(-0.80901694f, -0.58778536f),
  1399. ImVec2(-0.3090171f, -0.9510565f),
  1400. ImVec2(-0.3090171f, -0.9510565f),
  1401. ImVec2(0.30901712f, -0.9510565f),
  1402. ImVec2(0.30901712f, -0.9510565f),
  1403. ImVec2(0.80901694f, -0.5877853f),
  1404. ImVec2(0.80901694f, -0.5877853f),
  1405. ImVec2(1.0f, 0.0f)
  1406. };
  1407. 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)};
  1408. 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)};
  1409. 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)};
  1410. 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)};
  1411. 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) };
  1412. 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) };
  1413. 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)};
  1414. static const ImVec2 MARKER_LINE_PLUS[4] = {ImVec2(-1, 0), ImVec2(1, 0), ImVec2(0, -1), ImVec2(0, 1)};
  1415. 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)};
  1416. template <typename _Getter>
  1417. void RenderMarkers(const _Getter& getter, ImPlotMarker marker, float size, bool rend_fill, ImU32 col_fill, bool rend_line, ImU32 col_line, float weight) {
  1418. if (rend_fill) {
  1419. switch (marker) {
  1420. case ImPlotMarker_Circle : RenderPrimitives1<RendererMarkersFill>(getter,MARKER_FILL_CIRCLE,10,size,col_fill); break;
  1421. case ImPlotMarker_Square : RenderPrimitives1<RendererMarkersFill>(getter,MARKER_FILL_SQUARE, 4,size,col_fill); break;
  1422. case ImPlotMarker_Diamond : RenderPrimitives1<RendererMarkersFill>(getter,MARKER_FILL_DIAMOND,4,size,col_fill); break;
  1423. case ImPlotMarker_Up : RenderPrimitives1<RendererMarkersFill>(getter,MARKER_FILL_UP, 3,size,col_fill); break;
  1424. case ImPlotMarker_Down : RenderPrimitives1<RendererMarkersFill>(getter,MARKER_FILL_DOWN, 3,size,col_fill); break;
  1425. case ImPlotMarker_Left : RenderPrimitives1<RendererMarkersFill>(getter,MARKER_FILL_LEFT, 3,size,col_fill); break;
  1426. case ImPlotMarker_Right : RenderPrimitives1<RendererMarkersFill>(getter,MARKER_FILL_RIGHT, 3,size,col_fill); break;
  1427. }
  1428. }
  1429. if (rend_line) {
  1430. switch (marker) {
  1431. case ImPlotMarker_Circle : RenderPrimitives1<RendererMarkersLine>(getter,MARKER_LINE_CIRCLE, 20,size,weight,col_line); break;
  1432. case ImPlotMarker_Square : RenderPrimitives1<RendererMarkersLine>(getter,MARKER_LINE_SQUARE, 8,size,weight,col_line); break;
  1433. case ImPlotMarker_Diamond : RenderPrimitives1<RendererMarkersLine>(getter,MARKER_LINE_DIAMOND, 8,size,weight,col_line); break;
  1434. case ImPlotMarker_Up : RenderPrimitives1<RendererMarkersLine>(getter,MARKER_LINE_UP, 6,size,weight,col_line); break;
  1435. case ImPlotMarker_Down : RenderPrimitives1<RendererMarkersLine>(getter,MARKER_LINE_DOWN, 6,size,weight,col_line); break;
  1436. case ImPlotMarker_Left : RenderPrimitives1<RendererMarkersLine>(getter,MARKER_LINE_LEFT, 6,size,weight,col_line); break;
  1437. case ImPlotMarker_Right : RenderPrimitives1<RendererMarkersLine>(getter,MARKER_LINE_RIGHT, 6,size,weight,col_line); break;
  1438. case ImPlotMarker_Asterisk : RenderPrimitives1<RendererMarkersLine>(getter,MARKER_LINE_ASTERISK,6,size,weight,col_line); break;
  1439. case ImPlotMarker_Plus : RenderPrimitives1<RendererMarkersLine>(getter,MARKER_LINE_PLUS, 4,size,weight,col_line); break;
  1440. case ImPlotMarker_Cross : RenderPrimitives1<RendererMarkersLine>(getter,MARKER_LINE_CROSS, 4,size,weight,col_line); break;
  1441. }
  1442. }
  1443. }
  1444. //-----------------------------------------------------------------------------
  1445. // [SECTION] PlotLine
  1446. //-----------------------------------------------------------------------------
  1447. template <typename _Getter>
  1448. void PlotLineEx(const char* label_id, const _Getter& getter, ImPlotLineFlags flags) {
  1449. if (BeginItemEx(label_id, Fitter1<_Getter>(getter), flags, ImPlotCol_Line)) {
  1450. const ImPlotNextItemData& s = GetItemData();
  1451. if (getter.Count > 1) {
  1452. if (ImHasFlag(flags, ImPlotLineFlags_Shaded) && s.RenderFill) {
  1453. const ImU32 col_fill = ImGui::GetColorU32(s.Colors[ImPlotCol_Fill]);
  1454. GetterOverrideY<_Getter> getter2(getter, 0);
  1455. RenderPrimitives2<RendererShaded>(getter,getter2,col_fill);
  1456. }
  1457. if (s.RenderLine) {
  1458. const ImU32 col_line = ImGui::GetColorU32(s.Colors[ImPlotCol_Line]);
  1459. if (ImHasFlag(flags,ImPlotLineFlags_Segments)) {
  1460. RenderPrimitives1<RendererLineSegments1>(getter,col_line,s.LineWeight);
  1461. }
  1462. else if (ImHasFlag(flags, ImPlotLineFlags_Loop)) {
  1463. if (ImHasFlag(flags, ImPlotLineFlags_SkipNaN))
  1464. RenderPrimitives1<RendererLineStripSkip>(GetterLoop<_Getter>(getter),col_line,s.LineWeight);
  1465. else
  1466. RenderPrimitives1<RendererLineStrip>(GetterLoop<_Getter>(getter),col_line,s.LineWeight);
  1467. }
  1468. else {
  1469. if (ImHasFlag(flags, ImPlotLineFlags_SkipNaN))
  1470. RenderPrimitives1<RendererLineStripSkip>(getter,col_line,s.LineWeight);
  1471. else
  1472. RenderPrimitives1<RendererLineStrip>(getter,col_line,s.LineWeight);
  1473. }
  1474. }
  1475. }
  1476. // render markers
  1477. if (s.Marker != ImPlotMarker_None) {
  1478. if (ImHasFlag(flags, ImPlotLineFlags_NoClip)) {
  1479. PopPlotClipRect();
  1480. PushPlotClipRect(s.MarkerSize);
  1481. }
  1482. const ImU32 col_line = ImGui::GetColorU32(s.Colors[ImPlotCol_MarkerOutline]);
  1483. const ImU32 col_fill = ImGui::GetColorU32(s.Colors[ImPlotCol_MarkerFill]);
  1484. RenderMarkers<_Getter>(getter, s.Marker, s.MarkerSize, s.RenderMarkerFill, col_fill, s.RenderMarkerLine, col_line, s.MarkerWeight);
  1485. }
  1486. EndItem();
  1487. }
  1488. }
  1489. template <typename T>
  1490. void PlotLine(const char* label_id, const T* values, int count, double xscale, double x0, ImPlotLineFlags flags, int offset, int stride) {
  1491. GetterXY<IndexerLin,IndexerIdx<T>> getter(IndexerLin(xscale,x0),IndexerIdx<T>(values,count,offset,stride),count);
  1492. PlotLineEx(label_id, getter, flags);
  1493. }
  1494. template <typename T>
  1495. void PlotLine(const char* label_id, const T* xs, const T* ys, int count, ImPlotLineFlags flags, int offset, int stride) {
  1496. GetterXY<IndexerIdx<T>,IndexerIdx<T>> getter(IndexerIdx<T>(xs,count,offset,stride),IndexerIdx<T>(ys,count,offset,stride),count);
  1497. PlotLineEx(label_id, getter, flags);
  1498. }
  1499. #define INSTANTIATE_MACRO(T) \
  1500. template IMPLOT_API void PlotLine<T> (const char* label_id, const T* values, int count, double xscale, double x0, ImPlotLineFlags flags, int offset, int stride); \
  1501. template IMPLOT_API void PlotLine<T>(const char* label_id, const T* xs, const T* ys, int count, ImPlotLineFlags flags, int offset, int stride);
  1502. CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
  1503. #undef INSTANTIATE_MACRO
  1504. // custom
  1505. void PlotLineG(const char* label_id, ImPlotGetter getter_func, void* data, int count, ImPlotLineFlags flags) {
  1506. GetterFuncPtr getter(getter_func,data, count);
  1507. PlotLineEx(label_id, getter, flags);
  1508. }
  1509. //-----------------------------------------------------------------------------
  1510. // [SECTION] PlotScatter
  1511. //-----------------------------------------------------------------------------
  1512. template <typename Getter>
  1513. void PlotScatterEx(const char* label_id, const Getter& getter, ImPlotScatterFlags flags) {
  1514. if (BeginItemEx(label_id, Fitter1<Getter>(getter), flags, ImPlotCol_MarkerOutline)) {
  1515. const ImPlotNextItemData& s = GetItemData();
  1516. ImPlotMarker marker = s.Marker == ImPlotMarker_None ? ImPlotMarker_Circle: s.Marker;
  1517. if (marker != ImPlotMarker_None) {
  1518. if (ImHasFlag(flags,ImPlotScatterFlags_NoClip)) {
  1519. PopPlotClipRect();
  1520. PushPlotClipRect(s.MarkerSize);
  1521. }
  1522. const ImU32 col_line = ImGui::GetColorU32(s.Colors[ImPlotCol_MarkerOutline]);
  1523. const ImU32 col_fill = ImGui::GetColorU32(s.Colors[ImPlotCol_MarkerFill]);
  1524. RenderMarkers<Getter>(getter, marker, s.MarkerSize, s.RenderMarkerFill, col_fill, s.RenderMarkerLine, col_line, s.MarkerWeight);
  1525. }
  1526. EndItem();
  1527. }
  1528. }
  1529. template <typename T>
  1530. void PlotScatter(const char* label_id, const T* values, int count, double xscale, double x0, ImPlotScatterFlags flags, int offset, int stride) {
  1531. GetterXY<IndexerLin,IndexerIdx<T>> getter(IndexerLin(xscale,x0),IndexerIdx<T>(values,count,offset,stride),count);
  1532. PlotScatterEx(label_id, getter, flags);
  1533. }
  1534. template <typename T>
  1535. void PlotScatter(const char* label_id, const T* xs, const T* ys, int count, ImPlotScatterFlags flags, int offset, int stride) {
  1536. GetterXY<IndexerIdx<T>,IndexerIdx<T>> getter(IndexerIdx<T>(xs,count,offset,stride),IndexerIdx<T>(ys,count,offset,stride),count);
  1537. return PlotScatterEx(label_id, getter, flags);
  1538. }
  1539. #define INSTANTIATE_MACRO(T) \
  1540. template IMPLOT_API void PlotScatter<T>(const char* label_id, const T* values, int count, double xscale, double x0, ImPlotScatterFlags flags, int offset, int stride); \
  1541. template IMPLOT_API void PlotScatter<T>(const char* label_id, const T* xs, const T* ys, int count, ImPlotScatterFlags flags, int offset, int stride);
  1542. CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
  1543. #undef INSTANTIATE_MACRO
  1544. // custom
  1545. void PlotScatterG(const char* label_id, ImPlotGetter getter_func, void* data, int count, ImPlotScatterFlags flags) {
  1546. GetterFuncPtr getter(getter_func,data, count);
  1547. return PlotScatterEx(label_id, getter, flags);
  1548. }
  1549. //-----------------------------------------------------------------------------
  1550. // [SECTION] PlotStairs
  1551. //-----------------------------------------------------------------------------
  1552. template <typename Getter>
  1553. void PlotStairsEx(const char* label_id, const Getter& getter, ImPlotStairsFlags flags) {
  1554. if (BeginItemEx(label_id, Fitter1<Getter>(getter), flags, ImPlotCol_Line)) {
  1555. const ImPlotNextItemData& s = GetItemData();
  1556. if (getter.Count > 1 ) {
  1557. if (s.RenderFill && ImHasFlag(flags,ImPlotStairsFlags_Shaded)) {
  1558. const ImU32 col_fill = ImGui::GetColorU32(s.Colors[ImPlotCol_Fill]);
  1559. if (ImHasFlag(flags, ImPlotStairsFlags_PreStep))
  1560. RenderPrimitives1<RendererStairsPreShaded>(getter,col_fill);
  1561. else
  1562. RenderPrimitives1<RendererStairsPostShaded>(getter,col_fill);
  1563. }
  1564. if (s.RenderLine) {
  1565. const ImU32 col_line = ImGui::GetColorU32(s.Colors[ImPlotCol_Line]);
  1566. if (ImHasFlag(flags, ImPlotStairsFlags_PreStep))
  1567. RenderPrimitives1<RendererStairsPre>(getter,col_line,s.LineWeight);
  1568. else
  1569. RenderPrimitives1<RendererStairsPost>(getter,col_line,s.LineWeight);
  1570. }
  1571. }
  1572. // render markers
  1573. if (s.Marker != ImPlotMarker_None) {
  1574. PopPlotClipRect();
  1575. PushPlotClipRect(s.MarkerSize);
  1576. const ImU32 col_line = ImGui::GetColorU32(s.Colors[ImPlotCol_MarkerOutline]);
  1577. const ImU32 col_fill = ImGui::GetColorU32(s.Colors[ImPlotCol_MarkerFill]);
  1578. RenderMarkers<Getter>(getter, s.Marker, s.MarkerSize, s.RenderMarkerFill, col_fill, s.RenderMarkerLine, col_line, s.MarkerWeight);
  1579. }
  1580. EndItem();
  1581. }
  1582. }
  1583. template <typename T>
  1584. void PlotStairs(const char* label_id, const T* values, int count, double xscale, double x0, ImPlotStairsFlags flags, int offset, int stride) {
  1585. GetterXY<IndexerLin,IndexerIdx<T>> getter(IndexerLin(xscale,x0),IndexerIdx<T>(values,count,offset,stride),count);
  1586. PlotStairsEx(label_id, getter, flags);
  1587. }
  1588. template <typename T>
  1589. void PlotStairs(const char* label_id, const T* xs, const T* ys, int count, ImPlotStairsFlags flags, int offset, int stride) {
  1590. GetterXY<IndexerIdx<T>,IndexerIdx<T>> getter(IndexerIdx<T>(xs,count,offset,stride),IndexerIdx<T>(ys,count,offset,stride),count);
  1591. return PlotStairsEx(label_id, getter, flags);
  1592. }
  1593. #define INSTANTIATE_MACRO(T) \
  1594. template IMPLOT_API void PlotStairs<T> (const char* label_id, const T* values, int count, double xscale, double x0, ImPlotStairsFlags flags, int offset, int stride); \
  1595. template IMPLOT_API void PlotStairs<T>(const char* label_id, const T* xs, const T* ys, int count, ImPlotStairsFlags flags, int offset, int stride);
  1596. CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
  1597. #undef INSTANTIATE_MACRO
  1598. // custom
  1599. void PlotStairsG(const char* label_id, ImPlotGetter getter_func, void* data, int count, ImPlotStairsFlags flags) {
  1600. GetterFuncPtr getter(getter_func,data, count);
  1601. return PlotStairsEx(label_id, getter, flags);
  1602. }
  1603. //-----------------------------------------------------------------------------
  1604. // [SECTION] PlotShaded
  1605. //-----------------------------------------------------------------------------
  1606. template <typename Getter1, typename Getter2>
  1607. void PlotShadedEx(const char* label_id, const Getter1& getter1, const Getter2& getter2, ImPlotShadedFlags flags) {
  1608. if (BeginItemEx(label_id, Fitter2<Getter1,Getter2>(getter1,getter2), flags, ImPlotCol_Fill)) {
  1609. const ImPlotNextItemData& s = GetItemData();
  1610. if (s.RenderFill) {
  1611. const ImU32 col = ImGui::GetColorU32(s.Colors[ImPlotCol_Fill]);
  1612. RenderPrimitives2<RendererShaded>(getter1,getter2,col);
  1613. }
  1614. EndItem();
  1615. }
  1616. }
  1617. template <typename T>
  1618. void PlotShaded(const char* label_id, const T* values, int count, double y_ref, double xscale, double x0, ImPlotShadedFlags flags, int offset, int stride) {
  1619. if (!(y_ref > -DBL_MAX))
  1620. y_ref = GetPlotLimits(IMPLOT_AUTO,IMPLOT_AUTO).Y.Min;
  1621. if (!(y_ref < DBL_MAX))
  1622. y_ref = GetPlotLimits(IMPLOT_AUTO,IMPLOT_AUTO).Y.Max;
  1623. GetterXY<IndexerLin,IndexerIdx<T>> getter1(IndexerLin(xscale,x0),IndexerIdx<T>(values,count,offset,stride),count);
  1624. GetterXY<IndexerLin,IndexerConst> getter2(IndexerLin(xscale,x0),IndexerConst(y_ref),count);
  1625. PlotShadedEx(label_id, getter1, getter2, flags);
  1626. }
  1627. template <typename T>
  1628. void PlotShaded(const char* label_id, const T* xs, const T* ys, int count, double y_ref, ImPlotShadedFlags flags, int offset, int stride) {
  1629. if (y_ref == -HUGE_VAL)
  1630. y_ref = GetPlotLimits(IMPLOT_AUTO,IMPLOT_AUTO).Y.Min;
  1631. if (y_ref == HUGE_VAL)
  1632. y_ref = GetPlotLimits(IMPLOT_AUTO,IMPLOT_AUTO).Y.Max;
  1633. GetterXY<IndexerIdx<T>,IndexerIdx<T>> getter1(IndexerIdx<T>(xs,count,offset,stride),IndexerIdx<T>(ys,count,offset,stride),count);
  1634. GetterXY<IndexerIdx<T>,IndexerConst> getter2(IndexerIdx<T>(xs,count,offset,stride),IndexerConst(y_ref),count);
  1635. PlotShadedEx(label_id, getter1, getter2, flags);
  1636. }
  1637. template <typename T>
  1638. void PlotShaded(const char* label_id, const T* xs, const T* ys1, const T* ys2, int count, ImPlotShadedFlags flags, int offset, int stride) {
  1639. GetterXY<IndexerIdx<T>,IndexerIdx<T>> getter1(IndexerIdx<T>(xs,count,offset,stride),IndexerIdx<T>(ys1,count,offset,stride),count);
  1640. GetterXY<IndexerIdx<T>,IndexerIdx<T>> getter2(IndexerIdx<T>(xs,count,offset,stride),IndexerIdx<T>(ys2,count,offset,stride),count);
  1641. PlotShadedEx(label_id, getter1, getter2, flags);
  1642. }
  1643. #define INSTANTIATE_MACRO(T) \
  1644. template IMPLOT_API void PlotShaded<T>(const char* label_id, const T* values, int count, double y_ref, double xscale, double x0, ImPlotShadedFlags flags, int offset, int stride); \
  1645. template IMPLOT_API void PlotShaded<T>(const char* label_id, const T* xs, const T* ys, int count, double y_ref, ImPlotShadedFlags flags, int offset, int stride); \
  1646. template IMPLOT_API void PlotShaded<T>(const char* label_id, const T* xs, const T* ys1, const T* ys2, int count, ImPlotShadedFlags flags, int offset, int stride);
  1647. CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
  1648. #undef INSTANTIATE_MACRO
  1649. // custom
  1650. void PlotShadedG(const char* label_id, ImPlotGetter getter_func1, void* data1, ImPlotGetter getter_func2, void* data2, int count, ImPlotShadedFlags flags) {
  1651. GetterFuncPtr getter1(getter_func1, data1, count);
  1652. GetterFuncPtr getter2(getter_func2, data2, count);
  1653. PlotShadedEx(label_id, getter1, getter2, flags);
  1654. }
  1655. //-----------------------------------------------------------------------------
  1656. // [SECTION] PlotBars
  1657. //-----------------------------------------------------------------------------
  1658. template <typename Getter1, typename Getter2>
  1659. void PlotBarsVEx(const char* label_id, const Getter1& getter1, const Getter2 getter2, double width, ImPlotBarsFlags flags) {
  1660. if (BeginItemEx(label_id, FitterBarV<Getter1,Getter2>(getter1,getter2,width), flags, ImPlotCol_Fill)) {
  1661. const ImPlotNextItemData& s = GetItemData();
  1662. const ImU32 col_fill = ImGui::GetColorU32(s.Colors[ImPlotCol_Fill]);
  1663. const ImU32 col_line = ImGui::GetColorU32(s.Colors[ImPlotCol_Line]);
  1664. bool rend_fill = s.RenderFill;
  1665. bool rend_line = s.RenderLine;
  1666. if (rend_fill) {
  1667. RenderPrimitives2<RendererBarsFillV>(getter1,getter2,col_fill,width);
  1668. if (rend_line && col_fill == col_line)
  1669. rend_line = false;
  1670. }
  1671. if (rend_line) {
  1672. RenderPrimitives2<RendererBarsLineV>(getter1,getter2,col_line,width,s.LineWeight);
  1673. }
  1674. EndItem();
  1675. }
  1676. }
  1677. template <typename Getter1, typename Getter2>
  1678. void PlotBarsHEx(const char* label_id, const Getter1& getter1, const Getter2& getter2, double height, ImPlotBarsFlags flags) {
  1679. if (BeginItemEx(label_id, FitterBarH<Getter1,Getter2>(getter1,getter2,height), flags, ImPlotCol_Fill)) {
  1680. const ImPlotNextItemData& s = GetItemData();
  1681. const ImU32 col_fill = ImGui::GetColorU32(s.Colors[ImPlotCol_Fill]);
  1682. const ImU32 col_line = ImGui::GetColorU32(s.Colors[ImPlotCol_Line]);
  1683. bool rend_fill = s.RenderFill;
  1684. bool rend_line = s.RenderLine;
  1685. if (rend_fill) {
  1686. RenderPrimitives2<RendererBarsFillH>(getter1,getter2,col_fill,height);
  1687. if (rend_line && col_fill == col_line)
  1688. rend_line = false;
  1689. }
  1690. if (rend_line) {
  1691. RenderPrimitives2<RendererBarsLineH>(getter1,getter2,col_line,height,s.LineWeight);
  1692. }
  1693. EndItem();
  1694. }
  1695. }
  1696. template <typename T>
  1697. void PlotBars(const char* label_id, const T* values, int count, double bar_size, double shift, ImPlotBarsFlags flags, int offset, int stride) {
  1698. if (ImHasFlag(flags, ImPlotBarsFlags_Horizontal)) {
  1699. GetterXY<IndexerIdx<T>,IndexerLin> getter1(IndexerIdx<T>(values,count,offset,stride),IndexerLin(1.0,shift),count);
  1700. GetterXY<IndexerConst,IndexerLin> getter2(IndexerConst(0),IndexerLin(1.0,shift),count);
  1701. PlotBarsHEx(label_id, getter1, getter2, bar_size, flags);
  1702. }
  1703. else {
  1704. GetterXY<IndexerLin,IndexerIdx<T>> getter1(IndexerLin(1.0,shift),IndexerIdx<T>(values,count,offset,stride),count);
  1705. GetterXY<IndexerLin,IndexerConst> getter2(IndexerLin(1.0,shift),IndexerConst(0),count);
  1706. PlotBarsVEx(label_id, getter1, getter2, bar_size, flags);
  1707. }
  1708. }
  1709. template <typename T>
  1710. void PlotBars(const char* label_id, const T* xs, const T* ys, int count, double bar_size, ImPlotBarsFlags flags, int offset, int stride) {
  1711. if (ImHasFlag(flags, ImPlotBarsFlags_Horizontal)) {
  1712. GetterXY<IndexerIdx<T>,IndexerIdx<T>> getter1(IndexerIdx<T>(xs,count,offset,stride),IndexerIdx<T>(ys,count,offset,stride),count);
  1713. GetterXY<IndexerConst, IndexerIdx<T>> getter2(IndexerConst(0),IndexerIdx<T>(ys,count,offset,stride),count);
  1714. PlotBarsHEx(label_id, getter1, getter2, bar_size, flags);
  1715. }
  1716. else {
  1717. GetterXY<IndexerIdx<T>,IndexerIdx<T>> getter1(IndexerIdx<T>(xs,count,offset,stride),IndexerIdx<T>(ys,count,offset,stride),count);
  1718. GetterXY<IndexerIdx<T>,IndexerConst> getter2(IndexerIdx<T>(xs,count,offset,stride),IndexerConst(0),count);
  1719. PlotBarsVEx(label_id, getter1, getter2, bar_size, flags);
  1720. }
  1721. }
  1722. #define INSTANTIATE_MACRO(T) \
  1723. template IMPLOT_API void PlotBars<T>(const char* label_id, const T* values, int count, double bar_size, double shift, ImPlotBarsFlags flags, int offset, int stride); \
  1724. template IMPLOT_API void PlotBars<T>(const char* label_id, const T* xs, const T* ys, int count, double bar_size, ImPlotBarsFlags flags, int offset, int stride);
  1725. CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
  1726. #undef INSTANTIATE_MACRO
  1727. void PlotBarsG(const char* label_id, ImPlotGetter getter_func, void* data, int count, double bar_size, ImPlotBarsFlags flags) {
  1728. if (ImHasFlag(flags, ImPlotBarsFlags_Horizontal)) {
  1729. GetterFuncPtr getter1(getter_func, data, count);
  1730. GetterOverrideX<GetterFuncPtr> getter2(getter1,0);
  1731. PlotBarsHEx(label_id, getter1, getter2, bar_size, flags);
  1732. }
  1733. else {
  1734. GetterFuncPtr getter1(getter_func, data, count);
  1735. GetterOverrideY<GetterFuncPtr> getter2(getter1,0);
  1736. PlotBarsVEx(label_id, getter1, getter2, bar_size, flags);
  1737. }
  1738. }
  1739. //-----------------------------------------------------------------------------
  1740. // [SECTION] PlotBarGroups
  1741. //-----------------------------------------------------------------------------
  1742. template <typename T>
  1743. void PlotBarGroups(const char* const label_ids[], const T* values, int item_count, int group_count, double group_size, double shift, ImPlotBarGroupsFlags flags) {
  1744. const bool horz = ImHasFlag(flags, ImPlotBarGroupsFlags_Horizontal);
  1745. const bool stack = ImHasFlag(flags, ImPlotBarGroupsFlags_Stacked);
  1746. if (stack) {
  1747. SetupLock();
  1748. ImPlotContext& gp = *GImPlot;
  1749. gp.TempDouble1.resize(4*group_count);
  1750. double* temp = gp.TempDouble1.Data;
  1751. double* neg = &temp[0];
  1752. double* pos = &temp[group_count];
  1753. double* curr_min = &temp[group_count*2];
  1754. double* curr_max = &temp[group_count*3];
  1755. for (int g = 0; g < group_count*2; ++g)
  1756. temp[g] = 0;
  1757. if (horz) {
  1758. for (int i = 0; i < item_count; ++i) {
  1759. if (!IsItemHidden(label_ids[i])) {
  1760. for (int g = 0; g < group_count; ++g) {
  1761. double v = (double)values[i*group_count+g];
  1762. if (v > 0) {
  1763. curr_min[g] = pos[g];
  1764. curr_max[g] = curr_min[g] + v;
  1765. pos[g] += v;
  1766. }
  1767. else {
  1768. curr_max[g] = neg[g];
  1769. curr_min[g] = curr_max[g] + v;
  1770. neg[g] += v;
  1771. }
  1772. }
  1773. }
  1774. GetterXY<IndexerIdx<double>,IndexerLin> getter1(IndexerIdx<double>(curr_min,group_count),IndexerLin(1.0,shift),group_count);
  1775. GetterXY<IndexerIdx<double>,IndexerLin> getter2(IndexerIdx<double>(curr_max,group_count),IndexerLin(1.0,shift),group_count);
  1776. PlotBarsHEx(label_ids[i],getter1,getter2,group_size,0);
  1777. }
  1778. }
  1779. else {
  1780. for (int i = 0; i < item_count; ++i) {
  1781. if (!IsItemHidden(label_ids[i])) {
  1782. for (int g = 0; g < group_count; ++g) {
  1783. double v = (double)values[i*group_count+g];
  1784. if (v > 0) {
  1785. curr_min[g] = pos[g];
  1786. curr_max[g] = curr_min[g] + v;
  1787. pos[g] += v;
  1788. }
  1789. else {
  1790. curr_max[g] = neg[g];
  1791. curr_min[g] = curr_max[g] + v;
  1792. neg[g] += v;
  1793. }
  1794. }
  1795. }
  1796. GetterXY<IndexerLin,IndexerIdx<double>> getter1(IndexerLin(1.0,shift),IndexerIdx<double>(curr_min,group_count),group_count);
  1797. GetterXY<IndexerLin,IndexerIdx<double>> getter2(IndexerLin(1.0,shift),IndexerIdx<double>(curr_max,group_count),group_count);
  1798. PlotBarsVEx(label_ids[i],getter1,getter2,group_size,0);
  1799. }
  1800. }
  1801. }
  1802. else {
  1803. const double subsize = group_size / item_count;
  1804. if (horz) {
  1805. for (int i = 0; i < item_count; ++i) {
  1806. const double subshift = (i+0.5)*subsize - group_size/2;
  1807. PlotBars(label_ids[i],&values[i*group_count],group_count,subsize,subshift+shift,ImPlotBarsFlags_Horizontal);
  1808. }
  1809. }
  1810. else {
  1811. for (int i = 0; i < item_count; ++i) {
  1812. const double subshift = (i+0.5)*subsize - group_size/2;
  1813. PlotBars(label_ids[i],&values[i*group_count],group_count,subsize,subshift+shift);
  1814. }
  1815. }
  1816. }
  1817. }
  1818. #define INSTANTIATE_MACRO(T) template IMPLOT_API void PlotBarGroups<T>(const char* const label_ids[], const T* values, int items, int groups, double width, double shift, ImPlotBarGroupsFlags flags);
  1819. CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
  1820. #undef INSTANTIATE_MACRO
  1821. //-----------------------------------------------------------------------------
  1822. // [SECTION] PlotErrorBars
  1823. //-----------------------------------------------------------------------------
  1824. template <typename _GetterPos, typename _GetterNeg>
  1825. void PlotErrorBarsVEx(const char* label_id, const _GetterPos& getter_pos, const _GetterNeg& getter_neg, ImPlotErrorBarsFlags flags) {
  1826. if (BeginItemEx(label_id, Fitter2<_GetterPos,_GetterNeg>(getter_pos, getter_neg), flags, IMPLOT_AUTO)) {
  1827. const ImPlotNextItemData& s = GetItemData();
  1828. ImDrawList& draw_list = *GetPlotDrawList();
  1829. const ImU32 col = ImGui::GetColorU32(s.Colors[ImPlotCol_ErrorBar]);
  1830. const bool rend_whisker = s.ErrorBarSize > 0;
  1831. const float half_whisker = s.ErrorBarSize * 0.5f;
  1832. for (int i = 0; i < getter_pos.Count; ++i) {
  1833. ImVec2 p1 = PlotToPixels(getter_neg(i),IMPLOT_AUTO,IMPLOT_AUTO);
  1834. ImVec2 p2 = PlotToPixels(getter_pos(i),IMPLOT_AUTO,IMPLOT_AUTO);
  1835. draw_list.AddLine(p1,p2,col, s.ErrorBarWeight);
  1836. if (rend_whisker) {
  1837. draw_list.AddLine(p1 - ImVec2(half_whisker, 0), p1 + ImVec2(half_whisker, 0), col, s.ErrorBarWeight);
  1838. draw_list.AddLine(p2 - ImVec2(half_whisker, 0), p2 + ImVec2(half_whisker, 0), col, s.ErrorBarWeight);
  1839. }
  1840. }
  1841. EndItem();
  1842. }
  1843. }
  1844. template <typename _GetterPos, typename _GetterNeg>
  1845. void PlotErrorBarsHEx(const char* label_id, const _GetterPos& getter_pos, const _GetterNeg& getter_neg, ImPlotErrorBarsFlags flags) {
  1846. if (BeginItemEx(label_id, Fitter2<_GetterPos,_GetterNeg>(getter_pos, getter_neg), flags, IMPLOT_AUTO)) {
  1847. const ImPlotNextItemData& s = GetItemData();
  1848. ImDrawList& draw_list = *GetPlotDrawList();
  1849. const ImU32 col = ImGui::GetColorU32(s.Colors[ImPlotCol_ErrorBar]);
  1850. const bool rend_whisker = s.ErrorBarSize > 0;
  1851. const float half_whisker = s.ErrorBarSize * 0.5f;
  1852. for (int i = 0; i < getter_pos.Count; ++i) {
  1853. ImVec2 p1 = PlotToPixels(getter_neg(i),IMPLOT_AUTO,IMPLOT_AUTO);
  1854. ImVec2 p2 = PlotToPixels(getter_pos(i),IMPLOT_AUTO,IMPLOT_AUTO);
  1855. draw_list.AddLine(p1, p2, col, s.ErrorBarWeight);
  1856. if (rend_whisker) {
  1857. draw_list.AddLine(p1 - ImVec2(0, half_whisker), p1 + ImVec2(0, half_whisker), col, s.ErrorBarWeight);
  1858. draw_list.AddLine(p2 - ImVec2(0, half_whisker), p2 + ImVec2(0, half_whisker), col, s.ErrorBarWeight);
  1859. }
  1860. }
  1861. EndItem();
  1862. }
  1863. }
  1864. template <typename T>
  1865. void PlotErrorBars(const char* label_id, const T* xs, const T* ys, const T* err, int count, ImPlotErrorBarsFlags flags, int offset, int stride) {
  1866. PlotErrorBars(label_id, xs, ys, err, err, count, flags, offset, stride);
  1867. }
  1868. template <typename T>
  1869. void PlotErrorBars(const char* label_id, const T* xs, const T* ys, const T* neg, const T* pos, int count, ImPlotErrorBarsFlags flags, int offset, int stride) {
  1870. IndexerIdx<T> indexer_x(xs, count,offset,stride);
  1871. IndexerIdx<T> indexer_y(ys, count,offset,stride);
  1872. IndexerIdx<T> indexer_n(neg,count,offset,stride);
  1873. IndexerIdx<T> indexer_p(pos,count,offset,stride);
  1874. GetterError<T> getter(xs, ys, neg, pos, count, offset, stride);
  1875. if (ImHasFlag(flags, ImPlotErrorBarsFlags_Horizontal)) {
  1876. IndexerAdd<IndexerIdx<T>,IndexerIdx<T>> indexer_xp(indexer_x, indexer_p, 1, 1);
  1877. IndexerAdd<IndexerIdx<T>,IndexerIdx<T>> indexer_xn(indexer_x, indexer_n, 1, -1);
  1878. GetterXY<IndexerAdd<IndexerIdx<T>,IndexerIdx<T>>,IndexerIdx<T>> getter_p(indexer_xp, indexer_y, count);
  1879. GetterXY<IndexerAdd<IndexerIdx<T>,IndexerIdx<T>>,IndexerIdx<T>> getter_n(indexer_xn, indexer_y, count);
  1880. PlotErrorBarsHEx(label_id, getter_p, getter_n, flags);
  1881. }
  1882. else {
  1883. IndexerAdd<IndexerIdx<T>,IndexerIdx<T>> indexer_yp(indexer_y, indexer_p, 1, 1);
  1884. IndexerAdd<IndexerIdx<T>,IndexerIdx<T>> indexer_yn(indexer_y, indexer_n, 1, -1);
  1885. GetterXY<IndexerIdx<T>,IndexerAdd<IndexerIdx<T>,IndexerIdx<T>>> getter_p(indexer_x, indexer_yp, count);
  1886. GetterXY<IndexerIdx<T>,IndexerAdd<IndexerIdx<T>,IndexerIdx<T>>> getter_n(indexer_x, indexer_yn, count);
  1887. PlotErrorBarsVEx(label_id, getter_p, getter_n, flags);
  1888. }
  1889. }
  1890. #define INSTANTIATE_MACRO(T) \
  1891. template IMPLOT_API void PlotErrorBars<T>(const char* label_id, const T* xs, const T* ys, const T* err, int count, ImPlotErrorBarsFlags flags, int offset, int stride); \
  1892. template IMPLOT_API void PlotErrorBars<T>(const char* label_id, const T* xs, const T* ys, const T* neg, const T* pos, int count, ImPlotErrorBarsFlags flags, int offset, int stride);
  1893. CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
  1894. #undef INSTANTIATE_MACRO
  1895. //-----------------------------------------------------------------------------
  1896. // [SECTION] PlotStems
  1897. //-----------------------------------------------------------------------------
  1898. template <typename _GetterM, typename _GetterB>
  1899. void PlotStemsEx(const char* label_id, const _GetterM& get_mark, const _GetterB& get_base, ImPlotStemsFlags flags) {
  1900. if (BeginItemEx(label_id, Fitter2<_GetterM,_GetterB>(get_mark,get_base), flags, ImPlotCol_Line)) {
  1901. const ImPlotNextItemData& s = GetItemData();
  1902. // render stems
  1903. if (s.RenderLine) {
  1904. const ImU32 col_line = ImGui::GetColorU32(s.Colors[ImPlotCol_Line]);
  1905. RenderPrimitives2<RendererLineSegments2>(get_mark, get_base, col_line, s.LineWeight);
  1906. }
  1907. // render markers
  1908. if (s.Marker != ImPlotMarker_None) {
  1909. PopPlotClipRect();
  1910. PushPlotClipRect(s.MarkerSize);
  1911. const ImU32 col_line = ImGui::GetColorU32(s.Colors[ImPlotCol_MarkerOutline]);
  1912. const ImU32 col_fill = ImGui::GetColorU32(s.Colors[ImPlotCol_MarkerFill]);
  1913. RenderMarkers<_GetterM>(get_mark, s.Marker, s.MarkerSize, s.RenderMarkerFill, col_fill, s.RenderMarkerLine, col_line, s.MarkerWeight);
  1914. }
  1915. EndItem();
  1916. }
  1917. }
  1918. template <typename T>
  1919. void PlotStems(const char* label_id, const T* values, int count, double ref, double scale, double start, ImPlotStemsFlags flags, int offset, int stride) {
  1920. if (ImHasFlag(flags, ImPlotStemsFlags_Horizontal)) {
  1921. GetterXY<IndexerIdx<T>,IndexerLin> get_mark(IndexerIdx<T>(values,count,offset,stride),IndexerLin(scale,start),count);
  1922. GetterXY<IndexerConst,IndexerLin> get_base(IndexerConst(ref),IndexerLin(scale,start),count);
  1923. PlotStemsEx(label_id, get_mark, get_base, flags);
  1924. }
  1925. else {
  1926. GetterXY<IndexerLin,IndexerIdx<T>> get_mark(IndexerLin(scale,start),IndexerIdx<T>(values,count,offset,stride),count);
  1927. GetterXY<IndexerLin,IndexerConst> get_base(IndexerLin(scale,start),IndexerConst(ref),count);
  1928. PlotStemsEx(label_id, get_mark, get_base, flags);
  1929. }
  1930. }
  1931. template <typename T>
  1932. void PlotStems(const char* label_id, const T* xs, const T* ys, int count, double ref, ImPlotStemsFlags flags, int offset, int stride) {
  1933. if (ImHasFlag(flags, ImPlotStemsFlags_Horizontal)) {
  1934. GetterXY<IndexerIdx<T>,IndexerIdx<T>> get_mark(IndexerIdx<T>(xs,count,offset,stride),IndexerIdx<T>(ys,count,offset,stride),count);
  1935. GetterXY<IndexerConst,IndexerIdx<T>> get_base(IndexerConst(ref),IndexerIdx<T>(ys,count,offset,stride),count);
  1936. PlotStemsEx(label_id, get_mark, get_base, flags);
  1937. }
  1938. else {
  1939. GetterXY<IndexerIdx<T>,IndexerIdx<T>> get_mark(IndexerIdx<T>(xs,count,offset,stride),IndexerIdx<T>(ys,count,offset,stride),count);
  1940. GetterXY<IndexerIdx<T>,IndexerConst> get_base(IndexerIdx<T>(xs,count,offset,stride),IndexerConst(ref),count);
  1941. PlotStemsEx(label_id, get_mark, get_base, flags);
  1942. }
  1943. }
  1944. #define INSTANTIATE_MACRO(T) \
  1945. template IMPLOT_API void PlotStems<T>(const char* label_id, const T* values, int count, double ref, double scale, double start, ImPlotStemsFlags flags, int offset, int stride); \
  1946. template IMPLOT_API void PlotStems<T>(const char* label_id, const T* xs, const T* ys, int count, double ref, ImPlotStemsFlags flags, int offset, int stride);
  1947. CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
  1948. #undef INSTANTIATE_MACRO
  1949. //-----------------------------------------------------------------------------
  1950. // [SECTION] PlotInfLines
  1951. //-----------------------------------------------------------------------------
  1952. template <typename T>
  1953. void PlotInfLines(const char* label_id, const T* values, int count, ImPlotInfLinesFlags flags, int offset, int stride) {
  1954. const ImPlotRect lims = GetPlotLimits(IMPLOT_AUTO,IMPLOT_AUTO);
  1955. if (ImHasFlag(flags, ImPlotInfLinesFlags_Horizontal)) {
  1956. GetterXY<IndexerConst,IndexerIdx<T>> get_min(IndexerConst(lims.X.Min),IndexerIdx<T>(values,count,offset,stride),count);
  1957. GetterXY<IndexerConst,IndexerIdx<T>> get_max(IndexerConst(lims.X.Max),IndexerIdx<T>(values,count,offset,stride),count);
  1958. if (BeginItemEx(label_id, FitterY<GetterXY<IndexerConst,IndexerIdx<T>>>(get_min), flags, ImPlotCol_Line)) {
  1959. const ImPlotNextItemData& s = GetItemData();
  1960. const ImU32 col_line = ImGui::GetColorU32(s.Colors[ImPlotCol_Line]);
  1961. if (s.RenderLine)
  1962. RenderPrimitives2<RendererLineSegments2>(get_min, get_max, col_line, s.LineWeight);
  1963. EndItem();
  1964. }
  1965. }
  1966. else {
  1967. GetterXY<IndexerIdx<T>,IndexerConst> get_min(IndexerIdx<T>(values,count,offset,stride),IndexerConst(lims.Y.Min),count);
  1968. GetterXY<IndexerIdx<T>,IndexerConst> get_max(IndexerIdx<T>(values,count,offset,stride),IndexerConst(lims.Y.Max),count);
  1969. if (BeginItemEx(label_id, FitterX<GetterXY<IndexerIdx<T>,IndexerConst>>(get_min), flags, ImPlotCol_Line)) {
  1970. const ImPlotNextItemData& s = GetItemData();
  1971. const ImU32 col_line = ImGui::GetColorU32(s.Colors[ImPlotCol_Line]);
  1972. if (s.RenderLine)
  1973. RenderPrimitives2<RendererLineSegments2>(get_min, get_max, col_line, s.LineWeight);
  1974. EndItem();
  1975. }
  1976. }
  1977. }
  1978. #define INSTANTIATE_MACRO(T) template IMPLOT_API void PlotInfLines<T>(const char* label_id, const T* xs, int count, ImPlotInfLinesFlags flags, int offset, int stride);
  1979. CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
  1980. #undef INSTANTIATE_MACRO
  1981. //-----------------------------------------------------------------------------
  1982. // [SECTION] PlotPieChart
  1983. //-----------------------------------------------------------------------------
  1984. IMPLOT_INLINE void RenderPieSlice(ImDrawList& draw_list, const ImPlotPoint& center, double radius, double a0, double a1, ImU32 col) {
  1985. const float resolution = 50 / (2 * IM_PI);
  1986. ImVec2 buffer[52];
  1987. buffer[0] = PlotToPixels(center,IMPLOT_AUTO,IMPLOT_AUTO);
  1988. int n = ImMax(3, (int)((a1 - a0) * resolution));
  1989. double da = (a1 - a0) / (n - 1);
  1990. int i = 0;
  1991. for (; i < n; ++i) {
  1992. double a = a0 + i * da;
  1993. buffer[i + 1] = PlotToPixels(center.x + radius * cos(a), center.y + radius * sin(a),IMPLOT_AUTO,IMPLOT_AUTO);
  1994. }
  1995. buffer[i+1] = buffer[0];
  1996. // fill
  1997. draw_list.AddConvexPolyFilled(buffer, n + 1, col);
  1998. // border (for AA)
  1999. draw_list.AddPolyline(buffer, n + 2, col, 0, 2.0f);
  2000. }
  2001. template <typename T>
  2002. void PlotPieChart(const char* const label_ids[], const T* values, int count, double x, double y, double radius, const char* fmt, double angle0, ImPlotPieChartFlags flags) {
  2003. IM_ASSERT_USER_ERROR(GImPlot->CurrentPlot != nullptr, "PlotPieChart() needs to be called between BeginPlot() and EndPlot()!");
  2004. ImDrawList & draw_list = *GetPlotDrawList();
  2005. double sum = 0;
  2006. for (int i = 0; i < count; ++i)
  2007. sum += (double)values[i];
  2008. const bool normalize = ImHasFlag(flags,ImPlotPieChartFlags_Normalize) || sum > 1.0;
  2009. ImPlotPoint center(x,y);
  2010. PushPlotClipRect();
  2011. double a0 = angle0 * 2 * IM_PI / 360.0;
  2012. double a1 = angle0 * 2 * IM_PI / 360.0;
  2013. ImPlotPoint Pmin = ImPlotPoint(x-radius,y-radius);
  2014. ImPlotPoint Pmax = ImPlotPoint(x+radius,y+radius);
  2015. for (int i = 0; i < count; ++i) {
  2016. double percent = normalize ? (double)values[i] / sum : (double)values[i];
  2017. a1 = a0 + 2 * IM_PI * percent;
  2018. if (BeginItemEx(label_ids[i], FitterRect(Pmin,Pmax))) {
  2019. ImU32 col = GetCurrentItem()->Color;
  2020. if (percent < 0.5) {
  2021. RenderPieSlice(draw_list, center, radius, a0, a1, col);
  2022. }
  2023. else {
  2024. RenderPieSlice(draw_list, center, radius, a0, a0 + (a1 - a0) * 0.5, col);
  2025. RenderPieSlice(draw_list, center, radius, a0 + (a1 - a0) * 0.5, a1, col);
  2026. }
  2027. EndItem();
  2028. }
  2029. a0 = a1;
  2030. }
  2031. if (fmt != nullptr) {
  2032. a0 = angle0 * 2 * IM_PI / 360.0;
  2033. a1 = angle0 * 2 * IM_PI / 360.0;
  2034. char buffer[32];
  2035. for (int i = 0; i < count; ++i) {
  2036. ImPlotItem* item = GetItem(label_ids[i]);
  2037. double percent = normalize ? (double)values[i] / sum : (double)values[i];
  2038. a1 = a0 + 2 * IM_PI * percent;
  2039. if (item->Show) {
  2040. ImFormatString(buffer, 32, fmt, (double)values[i]);
  2041. ImVec2 size = ImGui::CalcTextSize(buffer);
  2042. double angle = a0 + (a1 - a0) * 0.5;
  2043. ImVec2 pos = PlotToPixels(center.x + 0.5 * radius * cos(angle), center.y + 0.5 * radius * sin(angle),IMPLOT_AUTO,IMPLOT_AUTO);
  2044. ImU32 col = CalcTextColor(ImGui::ColorConvertU32ToFloat4(item->Color));
  2045. draw_list.AddText(pos - size * 0.5f, col, buffer);
  2046. }
  2047. a0 = a1;
  2048. }
  2049. }
  2050. PopPlotClipRect();
  2051. }
  2052. #define INSTANTIATE_MACRO(T) template IMPLOT_API void PlotPieChart<T>(const char* const label_ids[], const T* values, int count, double x, double y, double radius, const char* fmt, double angle0, ImPlotPieChartFlags flags);
  2053. CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
  2054. #undef INSTANTIATE_MACRO
  2055. //-----------------------------------------------------------------------------
  2056. // [SECTION] PlotHeatmap
  2057. //-----------------------------------------------------------------------------
  2058. template <typename T>
  2059. struct GetterHeatmapRowMaj {
  2060. GetterHeatmapRowMaj(const T* values, int rows, int cols, double scale_min, double scale_max, double width, double height, double xref, double yref, double ydir) :
  2061. Values(values),
  2062. Count(rows*cols),
  2063. Rows(rows),
  2064. Cols(cols),
  2065. ScaleMin(scale_min),
  2066. ScaleMax(scale_max),
  2067. Width(width),
  2068. Height(height),
  2069. XRef(xref),
  2070. YRef(yref),
  2071. YDir(ydir),
  2072. HalfSize(Width*0.5, Height*0.5)
  2073. { }
  2074. template <typename I> IMPLOT_INLINE RectC operator()(I idx) const {
  2075. double val = (double)Values[idx];
  2076. const int r = idx / Cols;
  2077. const int c = idx % Cols;
  2078. const ImPlotPoint p(XRef + HalfSize.x + c*Width, YRef + YDir * (HalfSize.y + r*Height));
  2079. RectC rect;
  2080. rect.Pos = p;
  2081. rect.HalfSize = HalfSize;
  2082. const float t = ImClamp((float)ImRemap01(val, ScaleMin, ScaleMax),0.0f,1.0f);
  2083. ImPlotContext& gp = *GImPlot;
  2084. rect.Color = gp.ColormapData.LerpTable(gp.Style.Colormap, t);
  2085. return rect;
  2086. }
  2087. const T* const Values;
  2088. const int Count, Rows, Cols;
  2089. const double ScaleMin, ScaleMax, Width, Height, XRef, YRef, YDir;
  2090. const ImPlotPoint HalfSize;
  2091. };
  2092. template <typename T>
  2093. struct GetterHeatmapColMaj {
  2094. GetterHeatmapColMaj(const T* values, int rows, int cols, double scale_min, double scale_max, double width, double height, double xref, double yref, double ydir) :
  2095. Values(values),
  2096. Count(rows*cols),
  2097. Rows(rows),
  2098. Cols(cols),
  2099. ScaleMin(scale_min),
  2100. ScaleMax(scale_max),
  2101. Width(width),
  2102. Height(height),
  2103. XRef(xref),
  2104. YRef(yref),
  2105. YDir(ydir),
  2106. HalfSize(Width*0.5, Height*0.5)
  2107. { }
  2108. template <typename I> IMPLOT_INLINE RectC operator()(I idx) const {
  2109. double val = (double)Values[idx];
  2110. const int r = idx % Cols;
  2111. const int c = idx / Cols;
  2112. const ImPlotPoint p(XRef + HalfSize.x + c*Width, YRef + YDir * (HalfSize.y + r*Height));
  2113. RectC rect;
  2114. rect.Pos = p;
  2115. rect.HalfSize = HalfSize;
  2116. const float t = ImClamp((float)ImRemap01(val, ScaleMin, ScaleMax),0.0f,1.0f);
  2117. ImPlotContext& gp = *GImPlot;
  2118. rect.Color = gp.ColormapData.LerpTable(gp.Style.Colormap, t);
  2119. return rect;
  2120. }
  2121. const T* const Values;
  2122. const int Count, Rows, Cols;
  2123. const double ScaleMin, ScaleMax, Width, Height, XRef, YRef, YDir;
  2124. const ImPlotPoint HalfSize;
  2125. };
  2126. template <typename T>
  2127. void RenderHeatmap(ImDrawList& draw_list, const T* values, int rows, int cols, double scale_min, double scale_max, const char* fmt, const ImPlotPoint& bounds_min, const ImPlotPoint& bounds_max, bool reverse_y, bool col_maj) {
  2128. ImPlotContext& gp = *GImPlot;
  2129. Transformer2 transformer;
  2130. if (scale_min == 0 && scale_max == 0) {
  2131. T temp_min, temp_max;
  2132. ImMinMaxArray(values,rows*cols,&temp_min,&temp_max);
  2133. scale_min = (double)temp_min;
  2134. scale_max = (double)temp_max;
  2135. }
  2136. if (scale_min == scale_max) {
  2137. ImVec2 a = transformer(bounds_min);
  2138. ImVec2 b = transformer(bounds_max);
  2139. ImU32 col = GetColormapColorU32(0,gp.Style.Colormap);
  2140. draw_list.AddRectFilled(a, b, col);
  2141. return;
  2142. }
  2143. const double yref = reverse_y ? bounds_max.y : bounds_min.y;
  2144. const double ydir = reverse_y ? -1 : 1;
  2145. if (col_maj) {
  2146. GetterHeatmapColMaj<T> getter(values, rows, cols, scale_min, scale_max, (bounds_max.x - bounds_min.x) / cols, (bounds_max.y - bounds_min.y) / rows, bounds_min.x, yref, ydir);
  2147. RenderPrimitives1<RendererRectC>(getter);
  2148. }
  2149. else {
  2150. GetterHeatmapRowMaj<T> getter(values, rows, cols, scale_min, scale_max, (bounds_max.x - bounds_min.x) / cols, (bounds_max.y - bounds_min.y) / rows, bounds_min.x, yref, ydir);
  2151. RenderPrimitives1<RendererRectC>(getter);
  2152. }
  2153. // labels
  2154. if (fmt != nullptr) {
  2155. const double w = (bounds_max.x - bounds_min.x) / cols;
  2156. const double h = (bounds_max.y - bounds_min.y) / rows;
  2157. const ImPlotPoint half_size(w*0.5,h*0.5);
  2158. int i = 0;
  2159. if (col_maj) {
  2160. for (int c = 0; c < cols; ++c) {
  2161. for (int r = 0; r < rows; ++r) {
  2162. ImPlotPoint p;
  2163. p.x = bounds_min.x + 0.5*w + c*w;
  2164. p.y = yref + ydir * (0.5*h + r*h);
  2165. ImVec2 px = transformer(p);
  2166. char buff[32];
  2167. ImFormatString(buff, 32, fmt, values[i]);
  2168. ImVec2 size = ImGui::CalcTextSize(buff);
  2169. double t = ImClamp(ImRemap01((double)values[i], scale_min, scale_max),0.0,1.0);
  2170. ImVec4 color = SampleColormap((float)t);
  2171. ImU32 col = CalcTextColor(color);
  2172. draw_list.AddText(px - size * 0.5f, col, buff);
  2173. i++;
  2174. }
  2175. }
  2176. }
  2177. else {
  2178. for (int r = 0; r < rows; ++r) {
  2179. for (int c = 0; c < cols; ++c) {
  2180. ImPlotPoint p;
  2181. p.x = bounds_min.x + 0.5*w + c*w;
  2182. p.y = yref + ydir * (0.5*h + r*h);
  2183. ImVec2 px = transformer(p);
  2184. char buff[32];
  2185. ImFormatString(buff, 32, fmt, values[i]);
  2186. ImVec2 size = ImGui::CalcTextSize(buff);
  2187. double t = ImClamp(ImRemap01((double)values[i], scale_min, scale_max),0.0,1.0);
  2188. ImVec4 color = SampleColormap((float)t);
  2189. ImU32 col = CalcTextColor(color);
  2190. draw_list.AddText(px - size * 0.5f, col, buff);
  2191. i++;
  2192. }
  2193. }
  2194. }
  2195. }
  2196. }
  2197. template <typename T>
  2198. void PlotHeatmap(const char* label_id, const T* values, int rows, int cols, double scale_min, double scale_max, const char* fmt, const ImPlotPoint& bounds_min, const ImPlotPoint& bounds_max, ImPlotHeatmapFlags flags) {
  2199. if (BeginItemEx(label_id, FitterRect(bounds_min, bounds_max))) {
  2200. ImDrawList& draw_list = *GetPlotDrawList();
  2201. const bool col_maj = ImHasFlag(flags, ImPlotHeatmapFlags_ColMajor);
  2202. RenderHeatmap(draw_list, values, rows, cols, scale_min, scale_max, fmt, bounds_min, bounds_max, true, col_maj);
  2203. EndItem();
  2204. }
  2205. }
  2206. #define INSTANTIATE_MACRO(T) template IMPLOT_API void PlotHeatmap<T>(const char* label_id, const T* values, int rows, int cols, double scale_min, double scale_max, const char* fmt, const ImPlotPoint& bounds_min, const ImPlotPoint& bounds_max, ImPlotHeatmapFlags flags);
  2207. CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
  2208. #undef INSTANTIATE_MACRO
  2209. //-----------------------------------------------------------------------------
  2210. // [SECTION] PlotHistogram
  2211. //-----------------------------------------------------------------------------
  2212. template <typename T>
  2213. double PlotHistogram(const char* label_id, const T* values, int count, int bins, double bar_scale, ImPlotRange range, ImPlotHistogramFlags flags) {
  2214. const bool cumulative = ImHasFlag(flags, ImPlotHistogramFlags_Cumulative);
  2215. const bool density = ImHasFlag(flags, ImPlotHistogramFlags_Density);
  2216. const bool outliers = !ImHasFlag(flags, ImPlotHistogramFlags_NoOutliers);
  2217. if (count <= 0 || bins == 0)
  2218. return 0;
  2219. if (range.Min == 0 && range.Max == 0) {
  2220. T Min, Max;
  2221. ImMinMaxArray(values, count, &Min, &Max);
  2222. range.Min = (double)Min;
  2223. range.Max = (double)Max;
  2224. }
  2225. double width;
  2226. if (bins < 0)
  2227. CalculateBins(values, count, bins, range, bins, width);
  2228. else
  2229. width = range.Size() / bins;
  2230. ImPlotContext& gp = *GImPlot;
  2231. ImVector<double>& bin_centers = gp.TempDouble1;
  2232. ImVector<double>& bin_counts = gp.TempDouble2;
  2233. bin_centers.resize(bins);
  2234. bin_counts.resize(bins);
  2235. int below = 0;
  2236. for (int b = 0; b < bins; ++b) {
  2237. bin_centers[b] = range.Min + b * width + width * 0.5;
  2238. bin_counts[b] = 0;
  2239. }
  2240. int counted = 0;
  2241. double max_count = 0;
  2242. for (int i = 0; i < count; ++i) {
  2243. double val = (double)values[i];
  2244. if (range.Contains(val)) {
  2245. const int b = ImClamp((int)((val - range.Min) / width), 0, bins - 1);
  2246. bin_counts[b] += 1.0;
  2247. if (bin_counts[b] > max_count)
  2248. max_count = bin_counts[b];
  2249. counted++;
  2250. }
  2251. else if (val < range.Min) {
  2252. below++;
  2253. }
  2254. }
  2255. if (cumulative && density) {
  2256. if (outliers)
  2257. bin_counts[0] += below;
  2258. for (int b = 1; b < bins; ++b)
  2259. bin_counts[b] += bin_counts[b-1];
  2260. double scale = 1.0 / (outliers ? count : counted);
  2261. for (int b = 0; b < bins; ++b)
  2262. bin_counts[b] *= scale;
  2263. max_count = bin_counts[bins-1];
  2264. }
  2265. else if (cumulative) {
  2266. if (outliers)
  2267. bin_counts[0] += below;
  2268. for (int b = 1; b < bins; ++b)
  2269. bin_counts[b] += bin_counts[b-1];
  2270. max_count = bin_counts[bins-1];
  2271. }
  2272. else if (density) {
  2273. double scale = 1.0 / ((outliers ? count : counted) * width);
  2274. for (int b = 0; b < bins; ++b)
  2275. bin_counts[b] *= scale;
  2276. max_count *= scale;
  2277. }
  2278. if (ImHasFlag(flags, ImPlotHistogramFlags_Horizontal))
  2279. PlotBars(label_id, &bin_counts.Data[0], &bin_centers.Data[0], bins, bar_scale*width, ImPlotBarsFlags_Horizontal);
  2280. else
  2281. PlotBars(label_id, &bin_centers.Data[0], &bin_counts.Data[0], bins, bar_scale*width);
  2282. return max_count;
  2283. }
  2284. #define INSTANTIATE_MACRO(T) template IMPLOT_API double PlotHistogram<T>(const char* label_id, const T* values, int count, int bins, double bar_scale, ImPlotRange range, ImPlotHistogramFlags flags);
  2285. CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
  2286. #undef INSTANTIATE_MACRO
  2287. //-----------------------------------------------------------------------------
  2288. // [SECTION] PlotHistogram2D
  2289. //-----------------------------------------------------------------------------
  2290. template <typename T>
  2291. double PlotHistogram2D(const char* label_id, const T* xs, const T* ys, int count, int x_bins, int y_bins, ImPlotRect range, ImPlotHistogramFlags flags) {
  2292. // const bool cumulative = ImHasFlag(flags, ImPlotHistogramFlags_Cumulative); NOT SUPPORTED
  2293. const bool density = ImHasFlag(flags, ImPlotHistogramFlags_Density);
  2294. const bool outliers = !ImHasFlag(flags, ImPlotHistogramFlags_NoOutliers);
  2295. const bool col_maj = ImHasFlag(flags, ImPlotHistogramFlags_ColMajor);
  2296. if (count <= 0 || x_bins == 0 || y_bins == 0)
  2297. return 0;
  2298. if (range.X.Min == 0 && range.X.Max == 0) {
  2299. T Min, Max;
  2300. ImMinMaxArray(xs, count, &Min, &Max);
  2301. range.X.Min = (double)Min;
  2302. range.X.Max = (double)Max;
  2303. }
  2304. if (range.Y.Min == 0 && range.Y.Max == 0) {
  2305. T Min, Max;
  2306. ImMinMaxArray(ys, count, &Min, &Max);
  2307. range.Y.Min = (double)Min;
  2308. range.Y.Max = (double)Max;
  2309. }
  2310. double width, height;
  2311. if (x_bins < 0)
  2312. CalculateBins(xs, count, x_bins, range.X, x_bins, width);
  2313. else
  2314. width = range.X.Size() / x_bins;
  2315. if (y_bins < 0)
  2316. CalculateBins(ys, count, y_bins, range.Y, y_bins, height);
  2317. else
  2318. height = range.Y.Size() / y_bins;
  2319. const int bins = x_bins * y_bins;
  2320. ImPlotContext& gp = *GImPlot;
  2321. ImVector<double>& bin_counts = gp.TempDouble1;
  2322. bin_counts.resize(bins);
  2323. for (int b = 0; b < bins; ++b)
  2324. bin_counts[b] = 0;
  2325. int counted = 0;
  2326. double max_count = 0;
  2327. for (int i = 0; i < count; ++i) {
  2328. if (range.Contains((double)xs[i], (double)ys[i])) {
  2329. const int xb = ImClamp( (int)((double)(xs[i] - range.X.Min) / width) , 0, x_bins - 1);
  2330. const int yb = ImClamp( (int)((double)(ys[i] - range.Y.Min) / height) , 0, y_bins - 1);
  2331. const int b = yb * x_bins + xb;
  2332. bin_counts[b] += 1.0;
  2333. if (bin_counts[b] > max_count)
  2334. max_count = bin_counts[b];
  2335. counted++;
  2336. }
  2337. }
  2338. if (density) {
  2339. double scale = 1.0 / ((outliers ? count : counted) * width * height);
  2340. for (int b = 0; b < bins; ++b)
  2341. bin_counts[b] *= scale;
  2342. max_count *= scale;
  2343. }
  2344. if (BeginItemEx(label_id, FitterRect(range))) {
  2345. ImDrawList& draw_list = *GetPlotDrawList();
  2346. RenderHeatmap(draw_list, &bin_counts.Data[0], y_bins, x_bins, 0, max_count, nullptr, range.Min(), range.Max(), false, col_maj);
  2347. EndItem();
  2348. }
  2349. return max_count;
  2350. }
  2351. #define INSTANTIATE_MACRO(T) template IMPLOT_API double PlotHistogram2D<T>(const char* label_id, const T* xs, const T* ys, int count, int x_bins, int y_bins, ImPlotRect range, ImPlotHistogramFlags flags);
  2352. CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
  2353. #undef INSTANTIATE_MACRO
  2354. //-----------------------------------------------------------------------------
  2355. // [SECTION] PlotDigital
  2356. //-----------------------------------------------------------------------------
  2357. // TODO: Make this behave like all the other plot types (.e. not fixed in y axis)
  2358. template <typename Getter>
  2359. void PlotDigitalEx(const char* label_id, Getter getter, ImPlotDigitalFlags flags) {
  2360. if (BeginItem(label_id, flags, ImPlotCol_Fill)) {
  2361. ImPlotContext& gp = *GImPlot;
  2362. ImDrawList& draw_list = *GetPlotDrawList();
  2363. const ImPlotNextItemData& s = GetItemData();
  2364. if (getter.Count > 1 && s.RenderFill) {
  2365. ImPlotPlot& plot = *gp.CurrentPlot;
  2366. ImPlotAxis& x_axis = plot.Axes[plot.CurrentX];
  2367. ImPlotAxis& y_axis = plot.Axes[plot.CurrentY];
  2368. int pixYMax = 0;
  2369. ImPlotPoint itemData1 = getter(0);
  2370. for (int i = 0; i < getter.Count; ++i) {
  2371. ImPlotPoint itemData2 = getter(i);
  2372. if (ImNanOrInf(itemData1.y)) {
  2373. itemData1 = itemData2;
  2374. continue;
  2375. }
  2376. if (ImNanOrInf(itemData2.y)) itemData2.y = ImConstrainNan(ImConstrainInf(itemData2.y));
  2377. int pixY_0 = (int)(s.LineWeight);
  2378. itemData1.y = ImMax(0.0, itemData1.y);
  2379. float pixY_1_float = s.DigitalBitHeight * (float)itemData1.y;
  2380. int pixY_1 = (int)(pixY_1_float); //allow only positive values
  2381. int pixY_chPosOffset = (int)(ImMax(s.DigitalBitHeight, pixY_1_float) + s.DigitalBitGap);
  2382. pixYMax = ImMax(pixYMax, pixY_chPosOffset);
  2383. ImVec2 pMin = PlotToPixels(itemData1,IMPLOT_AUTO,IMPLOT_AUTO);
  2384. ImVec2 pMax = PlotToPixels(itemData2,IMPLOT_AUTO,IMPLOT_AUTO);
  2385. int pixY_Offset = 0; //20 pixel from bottom due to mouse cursor label
  2386. pMin.y = (y_axis.PixelMin) + ((-gp.DigitalPlotOffset) - pixY_Offset);
  2387. pMax.y = (y_axis.PixelMin) + ((-gp.DigitalPlotOffset) - pixY_0 - pixY_1 - pixY_Offset);
  2388. //plot only one rectangle for same digital state
  2389. while (((i+2) < getter.Count) && (itemData1.y == itemData2.y)) {
  2390. const int in = (i + 1);
  2391. itemData2 = getter(in);
  2392. if (ImNanOrInf(itemData2.y)) break;
  2393. pMax.x = PlotToPixels(itemData2,IMPLOT_AUTO,IMPLOT_AUTO).x;
  2394. i++;
  2395. }
  2396. //do not extend plot outside plot range
  2397. if (pMin.x < x_axis.PixelMin) pMin.x = x_axis.PixelMin;
  2398. if (pMax.x < x_axis.PixelMin) pMax.x = x_axis.PixelMin;
  2399. if (pMin.x > x_axis.PixelMax) pMin.x = x_axis.PixelMax - 1; //fix issue related to https://github.com/ocornut/imgui/issues/3976
  2400. if (pMax.x > x_axis.PixelMax) pMax.x = x_axis.PixelMax - 1; //fix issue related to https://github.com/ocornut/imgui/issues/3976
  2401. //plot a rectangle that extends up to x2 with y1 height
  2402. if ((pMax.x > pMin.x) && (gp.CurrentPlot->PlotRect.Contains(pMin) || gp.CurrentPlot->PlotRect.Contains(pMax))) {
  2403. // ImVec4 colAlpha = item->Color;
  2404. // colAlpha.w = item->Highlight ? 1.0f : 0.9f;
  2405. draw_list.AddRectFilled(pMin, pMax, ImGui::GetColorU32(s.Colors[ImPlotCol_Fill]));
  2406. }
  2407. itemData1 = itemData2;
  2408. }
  2409. gp.DigitalPlotItemCnt++;
  2410. gp.DigitalPlotOffset += pixYMax;
  2411. }
  2412. EndItem();
  2413. }
  2414. }
  2415. template <typename T>
  2416. void PlotDigital(const char* label_id, const T* xs, const T* ys, int count, ImPlotDigitalFlags flags, int offset, int stride) {
  2417. GetterXY<IndexerIdx<T>,IndexerIdx<T>> getter(IndexerIdx<T>(xs,count,offset,stride),IndexerIdx<T>(ys,count,offset,stride),count);
  2418. return PlotDigitalEx(label_id, getter, flags);
  2419. }
  2420. #define INSTANTIATE_MACRO(T) template IMPLOT_API void PlotDigital<T>(const char* label_id, const T* xs, const T* ys, int count, ImPlotDigitalFlags flags, int offset, int stride);
  2421. CALL_INSTANTIATE_FOR_NUMERIC_TYPES()
  2422. #undef INSTANTIATE_MACRO
  2423. // custom
  2424. void PlotDigitalG(const char* label_id, ImPlotGetter getter_func, void* data, int count, ImPlotDigitalFlags flags) {
  2425. GetterFuncPtr getter(getter_func,data,count);
  2426. return PlotDigitalEx(label_id, getter, flags);
  2427. }
  2428. //-----------------------------------------------------------------------------
  2429. // [SECTION] PlotImage
  2430. //-----------------------------------------------------------------------------
  2431. void PlotImage(const char* label_id, ImTextureID user_texture_id, const ImPlotPoint& bmin, const ImPlotPoint& bmax, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& tint_col, ImPlotImageFlags) {
  2432. if (BeginItemEx(label_id, FitterRect(bmin,bmax))) {
  2433. ImU32 tint_col32 = ImGui::ColorConvertFloat4ToU32(tint_col);
  2434. GetCurrentItem()->Color = tint_col32;
  2435. ImDrawList& draw_list = *GetPlotDrawList();
  2436. ImVec2 p1 = PlotToPixels(bmin.x, bmax.y,IMPLOT_AUTO,IMPLOT_AUTO);
  2437. ImVec2 p2 = PlotToPixels(bmax.x, bmin.y,IMPLOT_AUTO,IMPLOT_AUTO);
  2438. PushPlotClipRect();
  2439. draw_list.AddImage(user_texture_id, p1, p2, uv0, uv1, tint_col32);
  2440. PopPlotClipRect();
  2441. EndItem();
  2442. }
  2443. }
  2444. //-----------------------------------------------------------------------------
  2445. // [SECTION] PlotText
  2446. //-----------------------------------------------------------------------------
  2447. void PlotText(const char* text, double x, double y, const ImVec2& pixel_offset, ImPlotTextFlags flags) {
  2448. IM_ASSERT_USER_ERROR(GImPlot->CurrentPlot != nullptr, "PlotText() needs to be called between BeginPlot() and EndPlot()!");
  2449. SetupLock();
  2450. ImDrawList & draw_list = *GetPlotDrawList();
  2451. PushPlotClipRect();
  2452. ImU32 colTxt = GetStyleColorU32(ImPlotCol_InlayText);
  2453. if (ImHasFlag(flags,ImPlotTextFlags_Vertical)) {
  2454. ImVec2 siz = CalcTextSizeVertical(text) * 0.5f;
  2455. ImVec2 ctr = siz * 0.5f;
  2456. ImVec2 pos = PlotToPixels(ImPlotPoint(x,y),IMPLOT_AUTO,IMPLOT_AUTO) + ImVec2(-ctr.x, ctr.y) + pixel_offset;
  2457. if (FitThisFrame() && !ImHasFlag(flags, ImPlotItemFlags_NoFit)) {
  2458. FitPoint(PixelsToPlot(pos));
  2459. FitPoint(PixelsToPlot(pos.x + siz.x, pos.y - siz.y));
  2460. }
  2461. AddTextVertical(&draw_list, pos, colTxt, text);
  2462. }
  2463. else {
  2464. ImVec2 siz = ImGui::CalcTextSize(text);
  2465. ImVec2 pos = PlotToPixels(ImPlotPoint(x,y),IMPLOT_AUTO,IMPLOT_AUTO) - siz * 0.5f + pixel_offset;
  2466. if (FitThisFrame() && !ImHasFlag(flags, ImPlotItemFlags_NoFit)) {
  2467. FitPoint(PixelsToPlot(pos));
  2468. FitPoint(PixelsToPlot(pos+siz));
  2469. }
  2470. draw_list.AddText(pos, colTxt, text);
  2471. }
  2472. PopPlotClipRect();
  2473. }
  2474. //-----------------------------------------------------------------------------
  2475. // [SECTION] PlotDummy
  2476. //-----------------------------------------------------------------------------
  2477. void PlotDummy(const char* label_id, ImPlotDummyFlags flags) {
  2478. if (BeginItem(label_id, flags, ImPlotCol_Line))
  2479. EndItem();
  2480. }
  2481. } // namespace ImPlot