-
Notifications
You must be signed in to change notification settings - Fork 20
/
implot3d.cpp
3119 lines (2653 loc) · 121 KB
/
implot3d.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//--------------------------------------------------
// ImPlot3D v0.1
// implot3d.cpp
// Date: 2024-11-16
// Author: Breno Cunha Queiroz (brenocq.com)
//
// Acknowledgments:
// ImPlot3D is heavily inspired by ImPlot
// (https://github.com/epezent/implot) by Evan Pezent,
// and follows a similar code style and structure to
// maintain consistency with ImPlot's API.
//--------------------------------------------------
// Table of Contents:
// [SECTION] Includes
// [SECTION] Macros
// [SECTION] Context
// [SECTION] Text Utils
// [SECTION] Legend Utils
// [SECTION] Mouse Position Utils
// [SECTION] Plot Box Utils
// [SECTION] Formatter
// [SECTION] Locator
// [SECTION] Context Menus
// [SECTION] Begin/End Plot
// [SECTION] Setup
// [SECTION] Plot Utils
// [SECTION] Setup Utils
// [SECTION] Miscellaneous
// [SECTION] Styles
// [SECTION] Colormaps
// [SECTION] Context Utils
// [SECTION] Style Utils
// [SECTION] ImPlot3DPoint
// [SECTION] ImPlot3DBox
// [SECTION] ImPlot3DRange
// [SECTION] ImPlot3DQuat
// [SECTION] ImDrawList3D
// [SECTION] ImPlot3DAxis
// [SECTION] ImPlot3DPlot
// [SECTION] ImPlot3DStyle
//-----------------------------------------------------------------------------
// [SECTION] Includes
//-----------------------------------------------------------------------------
#ifndef IMGUI_DEFINE_MATH_OPERATORS
#define IMGUI_DEFINE_MATH_OPERATORS
#endif
// We define this to avoid accidentally using the deprecated API
#ifndef IMPLOT_DISABLE_OBSOLETE_FUNCTIONS
#define IMPLOT_DISABLE_OBSOLETE_FUNCTIONS
#endif
#include "implot3d.h"
#include "implot3d_internal.h"
#ifndef IMGUI_DISABLE
//-----------------------------------------------------------------------------
// [SECTION] Macros
//-----------------------------------------------------------------------------
#define IMPLOT3D_CHECK_CTX() IM_ASSERT_USER_ERROR(GImPlot3D != nullptr, "No current context. Did you call ImPlot3D::CreateContext() or ImPlot3D::SetCurrentContext()?")
#define IMPLOT3D_CHECK_PLOT() IM_ASSERT_USER_ERROR(GImPlot3D->CurrentPlot != nullptr, "No active plot. Did you call ImPlot3D::BeginPlot()?")
//-----------------------------------------------------------------------------
// [SECTION] Context
//-----------------------------------------------------------------------------
namespace ImPlot3D {
// Global ImPlot3D context
#ifndef GImPlot3D
ImPlot3DContext* GImPlot3D = nullptr;
#endif
static ImPlot3DQuat init_rotation = ImPlot3DQuat(-0.513269f, -0.212596f, -0.318184f, 0.76819f);
ImPlot3DContext* CreateContext() {
ImPlot3DContext* ctx = IM_NEW(ImPlot3DContext)();
if (GImPlot3D == nullptr)
SetCurrentContext(ctx);
InitializeContext(ctx);
return ctx;
}
void DestroyContext(ImPlot3DContext* ctx) {
if (ctx == nullptr)
ctx = GImPlot3D;
if (GImPlot3D == ctx)
SetCurrentContext(nullptr);
IM_DELETE(ctx);
}
ImPlot3DContext* GetCurrentContext() { return GImPlot3D; }
void SetCurrentContext(ImPlot3DContext* ctx) { GImPlot3D = ctx; }
//-----------------------------------------------------------------------------
// [SECTION] Text Utils
//-----------------------------------------------------------------------------
void AddTextRotated(ImDrawList* draw_list, ImVec2 pos, float angle, ImU32 col, const char* text_begin, const char* text_end) {
if (!text_end)
text_end = text_begin + strlen(text_begin);
ImGuiContext& g = *GImGui;
ImFont* font = g.Font;
// Align to be pixel perfect
pos = ImFloor(pos);
const float scale = g.FontSize / font->FontSize;
// Measure the size of the text in unrotated coordinates
ImVec2 text_size = font->CalcTextSizeA(g.FontSize, FLT_MAX, 0.0f, text_begin, text_end, nullptr);
// Precompute sine and cosine of the angle (note: angle should be positive for rotation in ImGui)
float cos_a = cosf(-angle);
float sin_a = sinf(-angle);
const char* s = text_begin;
int chars_total = (int)(text_end - s);
int chars_rendered = 0;
const int vtx_count_max = chars_total * 4;
const int idx_count_max = chars_total * 6;
draw_list->PrimReserve(idx_count_max, vtx_count_max);
// Adjust pen position to center the text
ImVec2 pen = ImVec2(-text_size.x * 0.5f, -text_size.y * 0.5f);
while (s < text_end) {
unsigned int c = (unsigned int)*s;
if (c < 0x80) {
s += 1;
} else {
s += ImTextCharFromUtf8(&c, s, text_end);
if (c == 0) // Malformed UTF-8?
break;
}
const ImFontGlyph* glyph = font->FindGlyph((ImWchar)c);
if (glyph == nullptr) {
continue;
}
// Glyph dimensions and positions
ImVec2 glyph_offset = ImVec2(glyph->X0, glyph->Y0) * scale;
ImVec2 glyph_size = ImVec2(glyph->X1 - glyph->X0, glyph->Y1 - glyph->Y0) * scale;
// Corners of the glyph quad in unrotated space
ImVec2 corners[4];
corners[0] = pen + glyph_offset;
corners[1] = pen + glyph_offset + ImVec2(glyph_size.x, 0);
corners[2] = pen + glyph_offset + glyph_size;
corners[3] = pen + glyph_offset + ImVec2(0, glyph_size.y);
// Rotate and translate the corners
for (int i = 0; i < 4; i++) {
float x = corners[i].x;
float y = corners[i].y;
corners[i].x = x * cos_a - y * sin_a + pos.x;
corners[i].y = x * sin_a + y * cos_a + pos.y;
}
// Texture coordinates
ImVec2 uv0 = ImVec2(glyph->U0, glyph->V0);
ImVec2 uv1 = ImVec2(glyph->U1, glyph->V1);
// Render the glyph quad
draw_list->PrimQuadUV(corners[0], corners[1], corners[2], corners[3],
uv0, ImVec2(glyph->U1, glyph->V0),
uv1, ImVec2(glyph->U0, glyph->V1),
col);
// Advance the pen position
pen.x += glyph->AdvanceX * scale;
chars_rendered++;
}
// Return unused vertices
int chars_skipped = chars_total - chars_rendered;
draw_list->PrimUnreserve(chars_skipped * 6, chars_skipped * 4);
}
void AddTextCentered(ImDrawList* draw_list, ImVec2 top_center, ImU32 col, const char* text_begin) {
const char* text_end = ImGui::FindRenderedTextEnd(text_begin);
ImVec2 text_size = ImGui::CalcTextSize(text_begin, text_end, true);
draw_list->AddText(ImVec2(top_center.x - text_size.x * 0.5f, top_center.y), col, text_begin, text_end);
}
//-----------------------------------------------------------------------------
// [SECTION] Legend Utils
//-----------------------------------------------------------------------------
ImVec2 GetLocationPos(const ImRect& outer_rect, const ImVec2& inner_size, ImPlot3DLocation loc, const ImVec2& pad) {
ImVec2 pos;
// Legend x coordinate
if (ImPlot3D::ImHasFlag(loc, ImPlot3DLocation_West) && !ImPlot3D::ImHasFlag(loc, ImPlot3DLocation_East))
pos.x = outer_rect.Min.x + pad.x;
else if (!ImPlot3D::ImHasFlag(loc, ImPlot3DLocation_West) && ImPlot3D::ImHasFlag(loc, ImPlot3DLocation_East))
pos.x = outer_rect.Max.x - pad.x - inner_size.x;
else
pos.x = outer_rect.GetCenter().x - inner_size.x * 0.5f;
// Legend y coordinate
if (ImPlot3D::ImHasFlag(loc, ImPlot3DLocation_North) && !ImPlot3D::ImHasFlag(loc, ImPlot3DLocation_South))
pos.y = outer_rect.Min.y + pad.y;
else if (!ImPlot3D::ImHasFlag(loc, ImPlot3DLocation_North) && ImPlot3D::ImHasFlag(loc, ImPlot3DLocation_South))
pos.y = outer_rect.Max.y - pad.y - inner_size.y;
else
pos.y = outer_rect.GetCenter().y - inner_size.y * 0.5f;
pos.x = IM_ROUND(pos.x);
pos.y = IM_ROUND(pos.y);
return pos;
}
ImVec2 CalcLegendSize(ImPlot3DItemGroup& items, const ImVec2& pad, const ImVec2& spacing, bool vertical) {
const int nItems = items.GetLegendCount();
const float txt_ht = ImGui::GetTextLineHeight();
const float icon_size = txt_ht;
// Get label max width
float max_label_width = 0;
float sum_label_width = 0;
for (int i = 0; i < nItems; i++) {
const char* label = items.GetLegendLabel(i);
const float label_width = ImGui::CalcTextSize(label, nullptr, true).x;
max_label_width = label_width > max_label_width ? label_width : max_label_width;
sum_label_width += label_width;
}
// Compute legend size
const ImVec2 legend_size = vertical ? ImVec2(pad.x * 2 + icon_size + max_label_width, pad.y * 2 + nItems * txt_ht + (nItems - 1) * spacing.y) : ImVec2(pad.x * 2 + icon_size * nItems + sum_label_width + (nItems - 1) * spacing.x, pad.y * 2 + txt_ht);
return legend_size;
}
void ShowLegendEntries(ImPlot3DItemGroup& items, const ImRect& legend_bb, bool hovered, const ImVec2& pad, const ImVec2& spacing, bool vertical, ImDrawList& draw_list) {
const float txt_ht = ImGui::GetTextLineHeight();
const float icon_size = txt_ht;
const float icon_shrink = 2;
ImU32 col_txt = GetStyleColorU32(ImPlot3DCol_LegendText);
ImU32 col_txt_dis = ImAlphaU32(col_txt, 0.25f);
float sum_label_width = 0;
const int num_items = items.GetLegendCount();
if (num_items == 0)
return;
ImPlot3DContext& gp = *GImPlot3D;
// Render legend items
for (int i = 0; i < num_items; i++) {
const int idx = i;
ImPlot3DItem* item = items.GetLegendItem(idx);
const char* label = items.GetLegendLabel(idx);
const float label_width = ImGui::CalcTextSize(label, nullptr, true).x;
const ImVec2 top_left = vertical ? legend_bb.Min + pad + ImVec2(0, i * (txt_ht + spacing.y)) : legend_bb.Min + pad + ImVec2(i * (icon_size + spacing.x) + sum_label_width, 0);
sum_label_width += label_width;
ImRect icon_bb;
icon_bb.Min = top_left + ImVec2(icon_shrink, icon_shrink);
icon_bb.Max = top_left + ImVec2(icon_size - icon_shrink, icon_size - icon_shrink);
ImRect label_bb;
label_bb.Min = top_left;
label_bb.Max = top_left + ImVec2(label_width + icon_size, icon_size);
ImU32 col_txt_hl;
ImU32 col_item = ImAlphaU32(item->Color, 1);
ImRect button_bb(icon_bb.Min, label_bb.Max);
ImGui::KeepAliveID(item->ID);
bool item_hov = false;
bool item_hld = false;
bool item_clk = ImPlot3D::ImHasFlag(items.Legend.Flags, ImPlot3DLegendFlags_NoButtons)
? false
: ImGui::ButtonBehavior(button_bb, item->ID, &item_hov, &item_hld);
if (item_clk)
item->Show = !item->Show;
const bool hovering = item_hov && !ImPlot3D::ImHasFlag(items.Legend.Flags, ImPlot3DLegendFlags_NoHighlightItem);
if (hovering) {
item->LegendHovered = true;
col_txt_hl = ImPlot3D::ImMixU32(col_txt, col_item, 64);
} else {
item->LegendHovered = false;
col_txt_hl = ImGui::GetColorU32(col_txt);
}
ImU32 col_icon;
if (item_hld)
col_icon = item->Show ? ImAlphaU32(col_item, 0.5f) : ImGui::GetColorU32(ImGuiCol_TextDisabled, 0.5f);
else if (item_hov)
col_icon = item->Show ? ImAlphaU32(col_item, 0.75f) : ImGui::GetColorU32(ImGuiCol_TextDisabled, 0.75f);
else
col_icon = item->Show ? col_item : col_txt_dis;
draw_list.AddRectFilled(icon_bb.Min, icon_bb.Max, col_icon);
const char* text_display_end = ImGui::FindRenderedTextEnd(label, nullptr);
if (label != text_display_end)
draw_list.AddText(top_left + ImVec2(icon_size, 0), item->Show ? col_txt_hl : col_txt_dis, label, text_display_end);
}
}
void RenderLegend() {
ImPlot3DContext& gp = *GImPlot3D;
ImPlot3DPlot& plot = *gp.CurrentPlot;
if (ImPlot3D::ImHasFlag(plot.Flags, ImPlot3DFlags_NoLegend) || plot.Items.GetLegendCount() == 0)
return;
ImGuiContext& g = *GImGui;
ImGuiWindow* window = g.CurrentWindow;
ImDrawList* draw_list = window->DrawList;
const ImGuiIO& IO = ImGui::GetIO();
ImPlot3DLegend& legend = plot.Items.Legend;
const bool legend_horz = ImPlot3D::ImHasFlag(legend.Flags, ImPlot3DLegendFlags_Horizontal);
const ImVec2 legend_size = CalcLegendSize(plot.Items, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, !legend_horz);
const ImVec2 legend_pos = GetLocationPos(plot.PlotRect,
legend_size,
legend.Location,
gp.Style.LegendPadding);
legend.Rect = ImRect(legend_pos, legend_pos + legend_size);
// Test hover
legend.Hovered = legend.Rect.Contains(IO.MousePos);
// Render background
ImU32 col_bg = GetStyleColorU32(ImPlot3DCol_LegendBg);
ImU32 col_bd = GetStyleColorU32(ImPlot3DCol_LegendBorder);
draw_list->AddRectFilled(legend.Rect.Min, legend.Rect.Max, col_bg);
draw_list->AddRect(legend.Rect.Min, legend.Rect.Max, col_bd);
// Render legends
ShowLegendEntries(plot.Items, legend.Rect, legend.Hovered, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, !legend_horz, *draw_list);
}
//-----------------------------------------------------------------------------
// [SECTION] Mouse Position Utils
//-----------------------------------------------------------------------------
void RenderMousePos() {
ImPlot3DContext& gp = *GImPlot3D;
ImPlot3DPlot& plot = *gp.CurrentPlot;
if (ImPlot3D::ImHasFlag(plot.Flags, ImPlot3DFlags_NoMouseText))
return;
ImVec2 mouse_pos = ImGui::GetMousePos();
ImPlot3DPoint mouse_plot_pos = PixelsToPlotPlane(mouse_pos, ImPlane3D_YZ, true);
if (mouse_plot_pos.IsNaN())
mouse_plot_pos = PixelsToPlotPlane(mouse_pos, ImPlane3D_XZ, true);
if (mouse_plot_pos.IsNaN())
mouse_plot_pos = PixelsToPlotPlane(mouse_pos, ImPlane3D_XY, true);
char buff[IMPLOT3D_LABEL_MAX_SIZE];
if (!mouse_plot_pos.IsNaN()) {
ImGuiTextBuffer builder;
builder.append("(");
for (int i = 0; i < 3; i++) {
ImPlot3DAxis& axis = plot.Axes[i];
if (i > 0)
builder.append(", ");
axis.Formatter(mouse_plot_pos[i], buff, IMPLOT3D_LABEL_MAX_SIZE, axis.FormatterData);
builder.append(buff);
}
builder.append(")");
const ImVec2 size = ImGui::CalcTextSize(builder.c_str());
// TODO custom location/padding
const ImVec2 pos = GetLocationPos(plot.PlotRect, size, ImPlot3DLocation_SouthEast, ImVec2(10, 10));
ImDrawList& draw_list = *ImGui::GetWindowDrawList();
draw_list.AddText(pos, GetStyleColorU32(ImPlot3DCol_InlayText), builder.c_str());
}
}
//-----------------------------------------------------------------------------
// [SECTION] Plot Box Utils
//-----------------------------------------------------------------------------
// Faces of the box (defined by 4 corner indices)
static const int faces[6][4] = {
{0, 3, 7, 4}, // X-min face
{0, 4, 5, 1}, // Y-min face
{0, 1, 2, 3}, // Z-min face
{1, 2, 6, 5}, // X-max face
{3, 7, 6, 2}, // Y-max face
{4, 5, 6, 7}, // Z-max face
};
// Edges of the box (defined by 2 corner indices)
static const int edges[12][2] = {
// Bottom face edges
{0, 1},
{1, 2},
{2, 3},
{3, 0},
// Top face edges
{4, 5},
{5, 6},
{6, 7},
{7, 4},
// Vertical edges
{0, 4},
{1, 5},
{2, 6},
{3, 7},
};
// Face edges (4 edge indices for each face)
static const int face_edges[6][4] = {
{3, 11, 8, 7}, // X-min face
{0, 8, 4, 9}, // Y-min face
{0, 1, 2, 3}, // Z-min face
{1, 9, 5, 10}, // X-max face
{2, 10, 6, 11}, // Y-max face
{4, 5, 6, 7}, // Z-max face
};
// Lookup table for axis_corners based on active_faces (3D plot)
static const int axis_corners_lookup_3d[8][3][2] = {
// Index 0: active_faces = {0, 0, 0}
{{3, 2}, {1, 2}, {1, 5}},
// Index 1: active_faces = {0, 0, 1}
{{7, 6}, {5, 6}, {1, 5}},
// Index 2: active_faces = {0, 1, 0}
{{0, 1}, {1, 2}, {2, 6}},
// Index 3: active_faces = {0, 1, 1}
{{4, 5}, {5, 6}, {2, 6}},
// Index 4: active_faces = {1, 0, 0}
{{3, 2}, {0, 3}, {0, 4}},
// Index 5: active_faces = {1, 0, 1}
{{7, 6}, {4, 7}, {0, 4}},
// Index 6: active_faces = {1, 1, 0}
{{0, 1}, {0, 3}, {3, 7}},
// Index 7: active_faces = {1, 1, 1}
{{4, 5}, {4, 7}, {3, 7}},
};
int GetMouseOverPlane(const ImPlot3DPlot& plot, const bool* active_faces, const ImVec2* corners_pix, int* plane_out = nullptr) {
ImGuiIO& io = ImGui::GetIO();
ImVec2 mouse_pos = io.MousePos;
if (plane_out)
*plane_out = -1;
// Check each active face
for (int a = 0; a < 3; a++) {
int face_idx = a + 3 * active_faces[a];
ImVec2 p0 = corners_pix[faces[face_idx][0]];
ImVec2 p1 = corners_pix[faces[face_idx][1]];
ImVec2 p2 = corners_pix[faces[face_idx][2]];
ImVec2 p3 = corners_pix[faces[face_idx][3]];
// Check if the mouse is inside the face's quad (using a triangle check)
if (ImTriangleContainsPoint(p0, p1, p2, mouse_pos) || ImTriangleContainsPoint(p2, p3, p0, mouse_pos)) {
if (plane_out)
*plane_out = a;
return a; // Return the plane index: 0 -> YZ, 1 -> XZ, 2 -> XY
}
}
return -1; // Not over any active plane
}
int GetMouseOverAxis(const ImPlot3DPlot& plot, const bool* active_faces, const ImVec2* corners_pix, const int plane_2d, int* edge_out = nullptr) {
const float axis_proximity_threshold = 15.0f; // Distance in pixels to consider the mouse "close" to an axis
ImGuiIO& io = ImGui::GetIO();
ImVec2 mouse_pos = io.MousePos;
if (edge_out)
*edge_out = -1;
bool visible_edges[12];
for (int i = 0; i < 12; i++)
visible_edges[i] = false;
for (int a = 0; a < 3; a++) {
int face_idx = a + 3 * active_faces[a];
if (plane_2d != -1 && a != plane_2d)
continue;
for (size_t i = 0; i < 4; i++)
visible_edges[face_edges[face_idx][i]] = true;
}
// Check each edge for proximity to the mouse
for (int edge = 0; edge < 12; edge++) {
if (!visible_edges[edge])
continue;
ImVec2 p0 = corners_pix[edges[edge][0]];
ImVec2 p1 = corners_pix[edges[edge][1]];
// Check distance to the edge
ImVec2 closest_point = ImLineClosestPoint(p0, p1, mouse_pos);
float dist = ImLengthSqr(mouse_pos - closest_point);
if (dist <= axis_proximity_threshold) {
if (edge_out)
*edge_out = edge;
// Determine which axis the edge belongs to
if (edge == 0 || edge == 2 || edge == 4 || edge == 6)
return 0; // X-axis
else if (edge == 1 || edge == 3 || edge == 5 || edge == 7)
return 1; // Y-axis
else
return 2; // Z-axis
}
}
return -1; // Not over any axis
}
void RenderPlotBackground(ImDrawList* draw_list, const ImPlot3DPlot& plot, const ImVec2* corners_pix, const bool* active_faces, const int plane_2d) {
const ImVec4 col_bg = GetStyleColorVec4(ImPlot3DCol_PlotBg);
const ImVec4 col_bg_hov = col_bg + ImVec4(0.03f, 0.03f, 0.03f, 0.0f);
int hovered_plane = -1;
if (!plot.Held) {
// If the mouse is not held, highlight plane hovering when mouse over it
hovered_plane = GetMouseOverPlane(plot, active_faces, corners_pix);
if (GetMouseOverAxis(plot, active_faces, corners_pix, plane_2d) != -1)
hovered_plane = -1;
} else {
// If the mouse is held, highlight the held plane
hovered_plane = plot.HeldPlaneIdx;
}
for (int a = 0; a < 3; a++) {
int idx[4]; // Corner indices
for (int i = 0; i < 4; i++)
idx[i] = faces[a + 3 * active_faces[a]][i];
const ImU32 col = ImGui::ColorConvertFloat4ToU32((hovered_plane == a) ? col_bg_hov : col_bg);
draw_list->AddQuadFilled(corners_pix[idx[0]], corners_pix[idx[1]], corners_pix[idx[2]], corners_pix[idx[3]], col);
}
}
void RenderPlotBorder(ImDrawList* draw_list, const ImPlot3DPlot& plot, const ImVec2* corners_pix, const bool* active_faces, const int plane_2d) {
ImGuiIO& io = ImGui::GetIO();
int hovered_edge = -1;
if (!plot.Held)
GetMouseOverAxis(plot, active_faces, corners_pix, plane_2d, &hovered_edge);
else
hovered_edge = plot.HeldEdgeIdx;
bool render_edge[12];
for (int i = 0; i < 12; i++)
render_edge[i] = false;
for (int a = 0; a < 3; a++) {
int face_idx = a + 3 * active_faces[a];
if (plane_2d != -1 && a != plane_2d)
continue;
for (size_t i = 0; i < 4; i++)
render_edge[face_edges[face_idx][i]] = true;
}
ImU32 col_bd = GetStyleColorU32(ImPlot3DCol_PlotBorder);
for (int i = 0; i < 12; i++) {
if (render_edge[i]) {
int idx0 = edges[i][0];
int idx1 = edges[i][1];
float thickness = i == hovered_edge ? 3.0f : 1.0f;
draw_list->AddLine(corners_pix[idx0], corners_pix[idx1], col_bd, thickness);
}
}
}
void RenderGrid(ImDrawList* draw_list, const ImPlot3DPlot& plot, const ImPlot3DPoint* corners, const bool* active_faces, const int plane_2d) {
ImVec4 col_grid = GetStyleColorVec4(ImPlot3DCol_AxisGrid);
ImU32 col_grid_minor = ImGui::GetColorU32(col_grid * ImVec4(1, 1, 1, 0.3f));
ImU32 col_grid_major = ImGui::GetColorU32(col_grid * ImVec4(1, 1, 1, 0.6f));
for (int face = 0; face < 3; face++) {
if (plane_2d != -1 && face != plane_2d)
continue;
int face_idx = face + 3 * active_faces[face];
const ImPlot3DAxis& axis_u = plot.Axes[(face + 1) % 3];
const ImPlot3DAxis& axis_v = plot.Axes[(face + 2) % 3];
// Get the two axes (u and v) that define the face plane
int idx0 = faces[face_idx][0];
int idx1 = faces[face_idx][1];
int idx2 = faces[face_idx][2];
int idx3 = faces[face_idx][3];
// Corners of the face in plot space
ImPlot3DPoint p0 = corners[idx0];
ImPlot3DPoint p1 = corners[idx1];
ImPlot3DPoint p2 = corners[idx2];
ImPlot3DPoint p3 = corners[idx3];
// Vectors along the edges
ImPlot3DPoint u_vec = p1 - p0;
ImPlot3DPoint v_vec = p3 - p0;
// Render grid lines along u axis (axis_u)
if (!ImPlot3D::ImHasFlag(axis_u.Flags, ImPlot3DAxisFlags_NoGridLines))
for (int t = 0; t < axis_u.Ticker.TickCount(); ++t) {
const ImPlot3DTick& tick = axis_u.Ticker.Ticks[t];
// Compute position along u
float t_u = (tick.PlotPos - axis_u.Range.Min) / (axis_u.Range.Max - axis_u.Range.Min);
ImPlot3DPoint p_start = p0 + u_vec * t_u;
ImPlot3DPoint p_end = p3 + u_vec * t_u;
// Convert to pixel coordinates
ImVec2 p_start_pix = PlotToPixels(p_start);
ImVec2 p_end_pix = PlotToPixels(p_end);
// Get color
ImU32 col_line = tick.Major ? col_grid_major : col_grid_minor;
// Draw the grid line
draw_list->AddLine(p_start_pix, p_end_pix, col_line);
}
// Render grid lines along v axis (axis_v)
if (!ImPlot3D::ImHasFlag(axis_v.Flags, ImPlot3DAxisFlags_NoGridLines))
for (int t = 0; t < axis_v.Ticker.TickCount(); ++t) {
const ImPlot3DTick& tick = axis_v.Ticker.Ticks[t];
// Compute position along v
float t_v = (tick.PlotPos - axis_v.Range.Min) / (axis_v.Range.Max - axis_v.Range.Min);
ImPlot3DPoint p_start = p0 + v_vec * t_v;
ImPlot3DPoint p_end = p1 + v_vec * t_v;
// Convert to pixel coordinates
ImVec2 p_start_pix = PlotToPixels(p_start);
ImVec2 p_end_pix = PlotToPixels(p_end);
// Get color
ImU32 col_line = tick.Major ? col_grid_major : col_grid_minor;
// Draw the grid line
draw_list->AddLine(p_start_pix, p_end_pix, col_line);
}
}
}
void RenderTickMarks(ImDrawList* draw_list, const ImPlot3DPlot& plot, const ImPlot3DPoint* corners, const ImVec2* corners_pix, const int axis_corners[3][2], const int plane_2d) {
ImU32 col_tick = GetStyleColorU32(ImPlot3DCol_AxisTick);
auto DeterminePlaneForAxis = [&](int axis_idx) {
if (plane_2d != -1)
return plane_2d;
// If no plane chosen (-1), use:
// X or Y axis -> XY plane (2)
// Z axis -> YZ plane (0)
if (axis_idx == 2)
return 1; // Z-axis use XZ plane
else
return 2; // X or Y-axis use XY plane
};
for (int a = 0; a < 3; a++) {
const ImPlot3DAxis& axis = plot.Axes[a];
if (ImPlot3D::ImHasFlag(axis.Flags, ImPlot3DAxisFlags_NoTickMarks))
continue;
int idx0 = axis_corners[a][0];
int idx1 = axis_corners[a][1];
if (idx0 == idx1) // axis not visible or invalid
continue;
ImPlot3DPoint axis_start = corners[idx0];
ImPlot3DPoint axis_end = corners[idx1];
ImPlot3DPoint axis_dir = axis_end - axis_start;
float axis_len = axis_dir.Length();
if (axis_len < 1e-12f)
continue;
axis_dir /= axis_len;
// Draw axis line
ImVec2 axis_start_pix = corners_pix[idx0];
ImVec2 axis_end_pix = corners_pix[idx1];
draw_list->AddLine(axis_start_pix, axis_end_pix, col_tick);
// Choose plane
int chosen_plane = DeterminePlaneForAxis(a);
// Project axis_dir onto chosen plane
ImPlot3DPoint proj_dir = axis_dir;
if (chosen_plane == 0) {
// YZ plane: zero out x
proj_dir.x = 0.0f;
} else if (chosen_plane == 1) {
// XZ plane: zero out y
proj_dir.y = 0.0f;
} else if (chosen_plane == 2) {
// XY plane: zero out z
proj_dir.z = 0.0f;
}
float proj_len = proj_dir.Length();
if (proj_len < 1e-12f) {
// Axis is parallel to plane normal or something degenerate, skip ticks
continue;
}
proj_dir /= proj_len;
// Rotate 90 degrees in chosen plane
ImPlot3DPoint tick_dir;
if (chosen_plane == 0) {
// YZ plane
// proj_dir=(0,py,pz), rotate 90°: (py,pz) -> (-pz,py)
tick_dir = ImPlot3DPoint(0, -proj_dir.z, proj_dir.y);
} else if (chosen_plane == 1) {
// XZ plane (plane=1)
// proj_dir=(px,0,pz), rotate 90°: (px,pz) -> (-pz,px)
tick_dir = ImPlot3DPoint(-proj_dir.z, 0, proj_dir.x);
} else {
// XY plane
// proj_dir=(px,py,0), rotate by 90°: (px,py) -> (-py,px)
tick_dir = ImPlot3DPoint(-proj_dir.y, proj_dir.x, 0);
}
tick_dir.Normalize();
// Tick lengths in NDC units
const float major_size_ndc = 0.06f;
const float minor_size_ndc = 0.03f;
for (int t = 0; t < axis.Ticker.TickCount(); ++t) {
const ImPlot3DTick& tick = axis.Ticker.Ticks[t];
float v = (tick.PlotPos - axis.Range.Min) / (axis.Range.Max - axis.Range.Min);
ImPlot3DPoint tick_pos_ndc = PlotToNDC(axis_start + axis_dir * (v * axis_len));
// Half tick on each side of the axis line
float size_tick_ndc = tick.Major ? major_size_ndc : minor_size_ndc;
ImPlot3DPoint half_tick_ndc = tick_dir * (size_tick_ndc * 0.5f);
ImPlot3DPoint T1_ndc = tick_pos_ndc - half_tick_ndc;
ImPlot3DPoint T2_ndc = tick_pos_ndc + half_tick_ndc;
ImVec2 T1_screen = NDCToPixels(T1_ndc);
ImVec2 T2_screen = NDCToPixels(T2_ndc);
draw_list->AddLine(T1_screen, T2_screen, col_tick);
}
}
}
void RenderTickLabels(ImDrawList* draw_list, const ImPlot3DPlot& plot, const ImPlot3DPoint* corners, const ImVec2* corners_pix, const int axis_corners[3][2]) {
ImVec2 box_center_pix = PlotToPixels(plot.RangeCenter());
ImU32 col_tick_txt = GetStyleColorU32(ImPlot3DCol_AxisText);
for (int a = 0; a < 3; a++) {
const ImPlot3DAxis& axis = plot.Axes[a];
if (ImPlot3D::ImHasFlag(axis.Flags, ImPlot3DAxisFlags_NoTickLabels))
continue;
// Corner indices for this axis
int idx0 = axis_corners[a][0];
int idx1 = axis_corners[a][1];
// If normal to the 2D plot, ignore the ticks
if (idx0 == idx1)
continue;
// Start and end points of the axis in plot space
ImPlot3DPoint axis_start = corners[idx0];
ImPlot3DPoint axis_end = corners[idx1];
// Direction vector along the axis
ImPlot3DPoint axis_dir = axis_end - axis_start;
// Convert axis start and end to screen space
ImVec2 axis_start_pix = corners_pix[idx0];
ImVec2 axis_end_pix = corners_pix[idx1];
// Screen space axis direction
ImVec2 axis_screen_dir = axis_end_pix - axis_start_pix;
float axis_length = ImSqrt(ImLengthSqr(axis_screen_dir));
if (axis_length != 0.0f)
axis_screen_dir /= axis_length;
else
axis_screen_dir = ImVec2(1.0f, 0.0f); // Default direction if length is zero
// Perpendicular direction in screen space
ImVec2 offset_dir_pix = ImVec2(-axis_screen_dir.y, axis_screen_dir.x);
// Make sure direction points away from cube center
ImVec2 box_center_pix = PlotToPixels(plot.RangeCenter());
ImVec2 axis_center_pix = (axis_start_pix + axis_end_pix) * 0.5f;
ImVec2 center_to_axis_pix = axis_center_pix - box_center_pix;
center_to_axis_pix /= ImSqrt(ImLengthSqr(center_to_axis_pix));
if (ImDot(offset_dir_pix, center_to_axis_pix) < 0.0f)
offset_dir_pix = -offset_dir_pix;
// Adjust the offset magnitude
float offset_magnitude = 20.0f; // TODO Calculate based on label size
ImVec2 offset_pix = offset_dir_pix * offset_magnitude;
// Compute angle perpendicular to axis in screen space
float angle = atan2f(-axis_screen_dir.y, axis_screen_dir.x) + IM_PI * 0.5f;
// Normalize angle to be between -π and π
if (angle > IM_PI)
angle -= 2 * IM_PI;
if (angle < -IM_PI)
angle += 2 * IM_PI;
// Adjust angle to keep labels upright
if (angle > IM_PI * 0.5f)
angle -= IM_PI;
if (angle < -IM_PI * 0.5f)
angle += IM_PI;
// Loop over ticks
for (int t = 0; t < axis.Ticker.TickCount(); ++t) {
const ImPlot3DTick& tick = axis.Ticker.Ticks[t];
if (!tick.ShowLabel)
continue;
// Compute position along the axis
float t_axis = (tick.PlotPos - axis.Range.Min) / (axis.Range.Max - axis.Range.Min);
ImPlot3DPoint tick_pos = axis_start + axis_dir * t_axis;
// Convert to pixel coordinates
ImVec2 tick_pos_pix = PlotToPixels(tick_pos);
// Get the tick label text
const char* label = axis.Ticker.GetText(tick);
// Adjust label position by offset
ImVec2 label_pos_pix = tick_pos_pix + offset_pix;
// Render the tick label
AddTextRotated(draw_list, label_pos_pix, angle, col_tick_txt, label);
}
}
}
void RenderAxisLabels(ImDrawList* draw_list, const ImPlot3DPlot& plot, const ImPlot3DPoint* corners, const ImVec2* corners_pix, const int axis_corners[3][2]) {
for (int a = 0; a < 3; a++) {
const ImPlot3DAxis& axis = plot.Axes[a];
if (!axis.HasLabel())
continue;
const char* label = axis.GetLabel();
// Corner indices
int idx0 = axis_corners[a][0];
int idx1 = axis_corners[a][1];
// If normal to the 2D plot, ignore axis label
if (idx0 == idx1)
continue;
// Position at the end of the axis
ImPlot3DPoint label_pos = (PlotToNDC(corners[idx0]) + PlotToNDC(corners[idx1])) * 0.5f;
ImPlot3DPoint center_dir = label_pos.Normalized();
// Add offset
label_pos += center_dir * 0.3f;
// Convert to pixel coordinates
ImVec2 label_pos_pix = NDCToPixels(label_pos);
// Adjust label position and angle
ImU32 col_ax_txt = GetStyleColorU32(ImPlot3DCol_AxisText);
// Compute text angle
ImVec2 screen_delta = corners_pix[idx1] - corners_pix[idx0];
float angle = atan2f(-screen_delta.y, screen_delta.x);
if (angle > IM_PI * 0.5f)
angle -= IM_PI;
if (angle < -IM_PI * 0.5f)
angle += IM_PI;
AddTextRotated(draw_list, label_pos_pix, angle, col_ax_txt, label);
}
}
// Function to compute active faces based on the rotation
// If the plot is close to 2D, plane_2d is set to the plane index (0 -> YZ, 1 -> XZ, 2 -> XY)
// plane_2d is set to -1 otherwise
void ComputeActiveFaces(bool* active_faces, const ImPlot3DQuat& rotation, const ImPlot3DAxis* axes, int* plane_2d = nullptr) {
if (plane_2d)
*plane_2d = -1;
ImPlot3DPoint rot_face_n[3] = {
rotation * ImPlot3DPoint(1.0f, 0.0f, 0.0f),
rotation * ImPlot3DPoint(0.0f, 1.0f, 0.0f),
rotation * ImPlot3DPoint(0.0f, 0.0f, 1.0f),
};
int num_deg = 0; // Check number of planes that are degenerate (seen as a line)
for (int i = 0; i < 3; ++i) {
// Determine the active face based on the Z component
if (fabs(rot_face_n[i].z) < 0.025) {
// If aligned with the plane, choose the min face for bottom/left
active_faces[i] = rot_face_n[i].x + rot_face_n[i].y < 0.0f;
num_deg++;
} else {
// Otherwise, determine based on the Z component
bool is_inverted = ImHasFlag(axes[i].Flags, ImPlot3DAxisFlags_Invert);
active_faces[i] = is_inverted ? (rot_face_n[i].z > 0.0f) : (rot_face_n[i].z < 0.0f);
// Set this plane as possible 2d plane
if (plane_2d)
*plane_2d = i;
}
}
// Only return 2d plane if there are exactly 2 degenerate planes
if (num_deg != 2 && plane_2d)
*plane_2d = -1;
}
// Function to compute the box corners in plot space
void ComputeBoxCorners(ImPlot3DPoint* corners, const ImPlot3DPoint& range_min, const ImPlot3DPoint& range_max) {
corners[0] = ImPlot3DPoint(range_min.x, range_min.y, range_min.z); // 0
corners[1] = ImPlot3DPoint(range_max.x, range_min.y, range_min.z); // 1
corners[2] = ImPlot3DPoint(range_max.x, range_max.y, range_min.z); // 2
corners[3] = ImPlot3DPoint(range_min.x, range_max.y, range_min.z); // 3
corners[4] = ImPlot3DPoint(range_min.x, range_min.y, range_max.z); // 4
corners[5] = ImPlot3DPoint(range_max.x, range_min.y, range_max.z); // 5
corners[6] = ImPlot3DPoint(range_max.x, range_max.y, range_max.z); // 6
corners[7] = ImPlot3DPoint(range_min.x, range_max.y, range_max.z); // 7
}
// Function to compute the box corners in pixel space
void ComputeBoxCornersPix(ImVec2* corners_pix, const ImPlot3DPoint* corners) {
for (int i = 0; i < 8; i++) {
corners_pix[i] = PlotToPixels(corners[i]);
}
}
void RenderPlotBox(ImDrawList* draw_list, const ImPlot3DPlot& plot) {
// Get plot parameters
const ImRect& plot_area = plot.PlotRect;
const ImPlot3DQuat& rotation = plot.Rotation;
ImPlot3DPoint range_min = plot.RangeMin();
ImPlot3DPoint range_max = plot.RangeMax();
ImPlot3DPoint range_center = plot.RangeCenter();
// Compute active faces
bool active_faces[3];
int plane_2d = -1;
ComputeActiveFaces(active_faces, rotation, plot.Axes, &plane_2d);
bool is_2d = plane_2d != -1;
// Compute box corners in plot space
ImPlot3DPoint corners[8];
ComputeBoxCorners(corners, range_min, range_max);
// Compute box corners in pixel space
ImVec2 corners_pix[8];
ComputeBoxCornersPix(corners_pix, corners);
// Compute axes start and end corners (given current rotation)
int axis_corners[3][2];
if (is_2d) {
int face = plane_2d + 3 * active_faces[plane_2d]; // Face of the 2D plot
int common_edges[2] = {-1, -1}; // Edges shared by the 3 faces
// Find the common edges between the 3 faces
for (int i = 0; i < 4; i++) {
int edge = face_edges[face][i];
for (int j = 0; j < 2; j++) {
int axis = (plane_2d + 1 + j) % 3;
int face_idx = axis + active_faces[axis] * 3;
for (int k = 0; k < 4; k++) {
if (face_edges[face_idx][k] == edge) {
common_edges[j] = edge;
break;
}
}
}
}
// Get corners from 2 edges (origin is the corner in common)
int origin_corner = -1;
int x_corner = -1;
int y_corner = -1;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
if (edges[common_edges[0]][i] == edges[common_edges[1]][j]) {
origin_corner = edges[common_edges[0]][i];
x_corner = edges[common_edges[0]][!i];
y_corner = edges[common_edges[1]][!j];
}
// Swap x and y if they are flipped
ImVec2 x_vec = corners_pix[x_corner] - corners_pix[origin_corner];
ImVec2 y_vec = corners_pix[y_corner] - corners_pix[origin_corner];
if (y_vec.x > x_vec.x)
ImSwap(x_corner, y_corner);
// Check which 3d axis the 2d axis refers to
ImPlot3DPoint origin_3d = corners[origin_corner];
ImPlot3DPoint x_3d = (corners[x_corner] - origin_3d).Normalized();
ImPlot3DPoint y_3d = (corners[y_corner] - origin_3d).Normalized();
int x_axis = -1;
bool x_inverted = false;
int y_axis = -1;
bool y_inverted = false;
for (int i = 0; i < 2; i++) {
int axis_i = (plane_2d + 1 + i) % 3;
if (y_axis != -1 || (ImAbs(x_3d[axis_i]) > 1e-8f && x_axis == -1)) {
x_axis = axis_i;
x_inverted = x_3d[axis_i] < 0.0f;
} else {
y_axis = axis_i;
y_inverted = y_3d[axis_i] < 0.0f;