-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotmini3d.h
More file actions
2077 lines (1839 loc) · 88.6 KB
/
Copy pathplotmini3d.h
File metadata and controls
2077 lines (1839 loc) · 88.6 KB
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
/*
plotmini3d.h -- prototype -- public domain
Basic 3D plotting layered on top of plotmini.h.
Orthographic projection + z-buffer. Non-interactive.
Requires plotmini.h to be included first.
====== USAGE ======
1. In *one* C file:
#define PLOTMINI_IMPLEMENTATION
#define PLOTMINI3D_IMPLEMENTATION
#include "plotmini.h"
#include "plotmini3d.h"
2. Everywhere else just:
#include "plotmini.h"
#include "plotmini3d.h"
*/
#ifndef PLOTMINI3D_H
#define PLOTMINI3D_H
/* Include plotmini.h for its types, but prevent its implementation
from being re-expanded if PLOTMINI_IMPLEMENTATION is already defined. */
#ifdef PLOTMINI_IMPLEMENTATION
#define PLOTMINI3D_TMP_IMPL
#undef PLOTMINI_IMPLEMENTATION
#endif
#include "plotmini.h"
#ifdef PLOTMINI3D_TMP_IMPL
#define PLOTMINI_IMPLEMENTATION
#undef PLOTMINI3D_TMP_IMPL
#endif
#include <math.h>
#ifdef __cplusplus
extern "C" {
#endif
/* ================================================================== */
/* PUBLIC TYPES */
/* ================================================================== */
/* ---- camera / view -------------------------------------------------*/
typedef enum plm3d_projection {
PLM3D_ORTHO = 0,
PLM3D_PERSPECTIVE = 1
} plm3d_projection;
typedef struct plm3d_view {
double azimuth; /* degrees, horizontal rotation (default -45) */
double elevation; /* degrees, tilt above horizontal (default 25) */
double distance; /* camera distance for perspective; 0=ortho */
plm3d_projection projection; /* PLM3D_ORTHO (default) or PLM3D_PERSPECTIVE */
} plm3d_view;
/* ---- 3D scatter ----------------------------------------------------*/
typedef struct plm3d_scatter_style {
plm_color color;
float radius; /* pixel radius */
const char *legend; /* optional legend label */
plm_cmap cmap; /* colormap by Z; PLM_CMAP_NONE = solid */
const float *color_data; /* optional per-point cmap data; NULL=Z */
} plm3d_scatter_style;
typedef struct plm3d_scatter_series {
const float *x_data;
const float *y_data;
const float *z_data;
int count;
plm3d_scatter_style style;
} plm3d_scatter_series;
/* ---- 3D line -------------------------------------------------------*/
typedef struct plm3d_line_style {
plm_color color;
float width; /* pixel width (1.0 = hairline) */
const char *legend; /* optional legend label */
plm_cmap cmap; /* colormap by Z; PLM_CMAP_NONE = solid */
} plm3d_line_style;
typedef struct plm3d_line_series {
const float *x_data;
const float *y_data;
const float *z_data;
int count;
plm3d_line_style style;
} plm3d_line_series;
/* ---- surface (wireframe) -------------------------------------------*/
typedef struct plm3d_surface_style {
plm_color line_color;
float line_width;
plm_cmap cmap; /* colormap for surface fill; PLM_CMAP_NONE = solid fill from line_color */
float light; /* 0 = flat fill, 1 = full Lambertian lighting from camera direction */
const char *legend; /* optional legend label */
int cull_backfaces; /* 0 = show both sides, 1 = skip backfaces */
int wireframe_only; /* 1 = skip fill, draw wireframe only */
float light_azimuth; /* degrees, light horizontal angle; 0,0 = camera direction (default) */
float light_elevation;/* degrees, light altitude above horizontal */
} plm3d_surface_style;
typedef struct plm3d_surface_series {
const float *x_data; /* length nx */
const float *y_data; /* length ny */
const float *z_data; /* length nx*ny, row-major: */
int nx; /* z[i*ny + j] = f(x[i], y[j]) */
int ny;
plm3d_surface_style style;
} plm3d_surface_series;
/* ---- 3D bar --------------------------------------------------------*/
typedef struct plm3d_bar_style {
plm_color fill_color; /* bar fill color; if cmap != NONE, fill_color is ignored */
plm_color stroke_color; /* bar edge color */
float stroke_width; /* bar edge width in pixels */
plm_cmap cmap; /* colormap by Z height; PLM_CMAP_NONE = use fill_color */
const char *legend; /* optional legend label */
float baseline; /* base Z for bars; NaN = auto (0 or z_min) */
} plm3d_bar_style;
typedef struct plm3d_bar_series {
const float *x_data; /* bar centre X positions, length count */
const float *y_data; /* bar centre Y positions, length count */
const float *z_data; /* bar heights, length count */
int count;
float width_x; /* bar half-width in X data units */
float width_y; /* bar half-width in Y data units */
plm3d_bar_style style;
} plm3d_bar_series;
/* ---- 3D stem -------------------------------------------------------*/
typedef struct plm3d_stem_style {
plm_color line_color; /* stem line color */
float line_width; /* stem line width in pixels */
plm_color marker_color; /* marker color (PLM_RGBA(0,0,0,0) = no marker) */
float marker_radius; /* marker radius in pixels */
const char *legend; /* optional legend label */
} plm3d_stem_style;
typedef struct plm3d_stem_series {
const float *x_data;
const float *y_data;
const float *z_data;
int count;
float baseline; /* Z value of stem base; NaN = z_min */
plm3d_stem_style style;
} plm3d_stem_series;
/* ---- 3D histogram (bivariate) --------------------------------------*/
typedef struct plm3d_hist_series {
const float *x_data; /* X samples, length count */
const float *y_data; /* Y samples, length count */
int count; /* number of samples */
int bins_x; /* number of bins in X */
int bins_y; /* number of bins in Y */
plm3d_bar_style style; /* bar style for rendering */
} plm3d_hist_series;
/* ---- 3D plot -------------------------------------------------------*/
typedef struct plm3d_plot {
/* axes */
plm_axis x_axis;
plm_axis y_axis;
plm_axis z_axis;
/* camera */
plm3d_view view;
/* layout (pixels) -- -1 means auto-compute */
int margin_left;
int margin_top;
int margin_right;
int margin_bottom;
/* labels */
const char *title;
const char *x_label;
const char *y_label;
const char *z_label;
/* legend */
plm_legend_pos legend_position; /* PLM_LEGEND_NONE = no legend */
/* series (filled by plm3d_plot_add_*()) */
plm3d_scatter_series *scatters;
int scatter_count;
int scatter_cap;
plm3d_line_series *lines;
int line_count;
int line_cap;
plm3d_surface_series *surfaces;
int surface_count;
int surface_cap;
plm3d_bar_series *bars;
int bar_count;
int bar_cap;
plm3d_stem_series *stems;
int stem_count;
int stem_cap;
plm3d_hist_series *hists;
int hist_count;
int hist_cap;
/* internal state (read-only) */
plm_irect plot_area; /* pixel rect of the 3D data region */
int fb_w; /* framebuffer width at last render */
int fb_h; /* framebuffer height at last render */
} plm3d_plot;
void plm3d_plot_init(plm3d_plot *p);
void plm3d_plot_reset(plm3d_plot *p);
void plm3d_plot_add_scatter(plm3d_plot *p,
const float *x, const float *y, const float *z,
int count, plm3d_scatter_style style);
void plm3d_plot_add_line(plm3d_plot *p,
const float *x, const float *y, const float *z,
int count, plm3d_line_style style);
void plm3d_plot_add_surface(plm3d_plot *p,
const float *x, const float *y, const float *z,
int nx, int ny, plm3d_surface_style style);
void plm3d_plot_add_bar(plm3d_plot *p,
const float *x, const float *y, const float *z,
int count, float width_x, float width_y,
plm3d_bar_style style);
void plm3d_plot_add_stem(plm3d_plot *p,
const float *x, const float *y, const float *z,
int count, plm3d_stem_style style);
void plm3d_plot_add_hist(plm3d_plot *p,
const float *x, const float *y, int count,
int bins_x, int bins_y, plm3d_bar_style style);
void plm3d_colorbar(plm3d_plot *p, plm_fb *fb, plm_cmap cmap,
double vmin, double vmax, const char *label);
/* Render the 3D plot into a framebuffer. The framebuffer must already
be created with plm_fb_create(). */
void plm3d_render(plm3d_plot *p, plm_fb *fb);
/* plm_figure_plot_3d() is declared in plotmini.h; its implementation
lives here so it can access the full plm3d_plot type. */
#ifdef __cplusplus
}
#endif
#endif /* PLOTMINI3D_H */
/* ================================================================== */
/* IMPLEMENTATION */
/* ================================================================== */
#ifdef PLOTMINI3D_IMPLEMENTATION
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/* ------------------------------------------------------------------ */
/* internal helpers */
/* ------------------------------------------------------------------ */
#define PLM3D_ZFAR (-1e30f)
static float plm3d__maxf(float a, float b) { return a > b ? a : b; }
static float plm3d__absf(float a) { return a < 0 ? -a : a; }
/* ---- projection ---------------------------------------------------- */
/* Project a normalised [-0.5, 0.5]^3 point to screen space.
Returns pixel-space (rx, ry) and depth rz.
The caller then scales rx,ry to the actual plot area.
If dist > 0, perspective divide is applied (camera at distance dist). */
static void plm3d__project(double cx, double cy, double cz,
double ca, double sa,
double ce, double se,
double dist,
float *rx, float *ry, float *rz) {
/* Standard orthographic: rotate around Z by azimuth,
then tilt by elevation. z=up in data space. */
*rx = (float)( ca * cx + sa * cy);
*ry = (float)(-sa * se * cx + ca * se * cy + ce * cz);
*rz = (float)( sa * ce * cx - ca * ce * cy + se * cz);
if (dist > 0.0) {
double w = dist / (dist - *rz);
*rx = (float)(*rx * w);
*ry = (float)(*ry * w);
}
}
/* ---- depth-tested pixel ops ---------------------------------------- */
static void plm3d__set_pixel_z(plm_fb *fb, float *zbuf,
int x, int y, float z, plm_color c) {
if (x < 0 || x >= fb->width || y < 0 || y >= fb->height) return;
int idx = y * fb->width + x;
if (z <= zbuf[idx]) return; /* occluded */
zbuf[idx] = z;
if (fb->bpp == 1) {
fb->pixels[y * fb->stride + x] =
(unsigned char)((unsigned int)(c.r * 77 + c.g * 150 + c.b * 29) >> 8);
} else {
unsigned char *p = fb->pixels + y * fb->stride + x * 4;
p[0] = c.r; p[1] = c.g; p[2] = c.b; p[3] = c.a;
}
}
/* ---- depth-tested blend pixel -------------------------------------- */
static void plm3d__blend_pixel_z(plm_fb *fb, float *zbuf,
int x, int y, float z,
plm_color c, unsigned char alpha) {
if (x < 0 || x >= fb->width || y < 0 || y >= fb->height) return;
if (alpha == 0) return;
if (alpha == 255) { plm3d__set_pixel_z(fb, zbuf, x, y, z, c); return; }
int idx = y * fb->width + x;
if (z <= zbuf[idx]) return;
zbuf[idx] = z;
if (fb->bpp == 1) {
unsigned char *dst = fb->pixels + y * fb->stride + x;
unsigned char lum = (unsigned char)((unsigned int)(c.r * 77 + c.g * 150 + c.b * 29) >> 8);
unsigned int inv_a = 255 - alpha;
*dst = (unsigned char)(((unsigned int)lum * alpha + (unsigned int)*dst * inv_a) >> 8);
} else {
unsigned char *dst = fb->pixels + y * fb->stride + x * 4;
unsigned int inv_a = 255 - alpha;
dst[0] = (unsigned char)(((unsigned int)c.r * alpha + (unsigned int)dst[0] * inv_a) >> 8);
dst[1] = (unsigned char)(((unsigned int)c.g * alpha + (unsigned int)dst[1] * inv_a) >> 8);
dst[2] = (unsigned char)(((unsigned int)c.b * alpha + (unsigned int)dst[2] * inv_a) >> 8);
}
}
/* ---- depth-tested line (DDA) --------------------------------------- */
static void plm3d__line_z(plm_fb *fb, float *zbuf,
float x0, float y0, float z0,
float x1, float y1, float z1,
plm_color c) {
float dx = x1 - x0, dy = y1 - y0;
float span = plm3d__maxf(plm3d__absf(dx), plm3d__absf(dy));
int steps = (int)ceilf(span);
if (steps < 1) steps = 1;
float x_inc = dx / (float)steps, y_inc = dy / (float)steps, z_inc = (z1 - z0) / (float)steps;
float x = x0, y = y0, z = z0;
for (int i = 0; i <= steps; i++) {
plm3d__set_pixel_z(fb, zbuf, (int)(x + 0.5f), (int)(y + 0.5f), z, c);
x += x_inc; y += y_inc; z += z_inc;
}
}
/* ---- depth-tested Wu anti-aliased line ------------------------------ */
static void plm3d__wu_line_z(plm_fb *fb, float *zbuf,
float x0, float y0, float z0,
float x1, float y1, float z1,
plm_color c) {
int steep = (fabsf(y1 - y0) > fabsf(x1 - x0));
if (steep) {
{ float t = x0; x0 = y0; y0 = t; }
{ float t = x1; x1 = y1; y1 = t; }
}
if (x0 > x1) {
{ float t = x0; x0 = x1; x1 = t; }
{ float t = y0; y0 = y1; y1 = t; }
{ float t = z0; z0 = z1; z1 = t; }
}
float dx = x1 - x0;
float dy = y1 - y0;
float dz = z1 - z0;
float gradient, zgrad;
if (dx < 1e-6f) { gradient = 1.0f; zgrad = 0.0f; }
else { gradient = dy / dx; zgrad = dz / dx; }
/* first endpoint */
int xpxl1 = (int)x0;
float yend = y0 + gradient * ((float)xpxl1 + 1.0f - x0);
float zend = z0 + zgrad * ((float)xpxl1 + 1.0f - x0);
float xgap = 1.0f - (x0 - (float)xpxl1);
{
int ipart = (int)yend;
float fpart = yend - (float)ipart;
unsigned char a1 = (unsigned char)((1.0f - fpart) * xgap * 255.0f);
unsigned char a2 = (unsigned char)(fpart * xgap * 255.0f);
if (steep) {
plm3d__blend_pixel_z(fb, zbuf, ipart, xpxl1, zend, c, a1);
plm3d__blend_pixel_z(fb, zbuf, ipart + 1, xpxl1, zend, c, a2);
} else {
plm3d__blend_pixel_z(fb, zbuf, xpxl1, ipart, zend, c, a1);
plm3d__blend_pixel_z(fb, zbuf, xpxl1, ipart + 1, zend, c, a2);
}
}
/* second endpoint */
int xpxl2 = (int)x1;
float yend2 = y1 + gradient * ((float)xpxl2 - x1);
float zend2 = z1 + zgrad * ((float)xpxl2 - x1);
float xgap2 = x1 - (float)xpxl2;
if (xgap2 > 0.0f) {
int ipart = (int)yend2;
float fpart = yend2 - (float)ipart;
unsigned char a1 = (unsigned char)((1.0f - fpart) * xgap2 * 255.0f);
unsigned char a2 = (unsigned char)(fpart * xgap2 * 255.0f);
if (steep) {
plm3d__blend_pixel_z(fb, zbuf, ipart, xpxl2, zend2, c, a1);
plm3d__blend_pixel_z(fb, zbuf, ipart + 1, xpxl2, zend2, c, a2);
} else {
plm3d__blend_pixel_z(fb, zbuf, xpxl2, ipart, zend2, c, a1);
plm3d__blend_pixel_z(fb, zbuf, xpxl2, ipart + 1, zend2, c, a2);
}
}
/* main loop */
float intery = yend + gradient;
float interz = zend + zgrad;
for (int x = xpxl1 + 1; x < xpxl2; x++) {
int ipart = (int)intery;
float fpart = intery - (float)ipart;
unsigned char a1 = (unsigned char)((1.0f - fpart) * 255.0f);
unsigned char a2 = (unsigned char)(fpart * 255.0f);
if (steep) {
plm3d__blend_pixel_z(fb, zbuf, ipart, x, interz, c, a1);
plm3d__blend_pixel_z(fb, zbuf, ipart + 1, x, interz, c, a2);
} else {
plm3d__blend_pixel_z(fb, zbuf, x, ipart, interz, c, a1);
plm3d__blend_pixel_z(fb, zbuf, x, ipart + 1, interz, c, a2);
}
intery += gradient;
interz += zgrad;
}
}
/* ---- depth-tested thick line (multiple parallel passes) ------------- */
static void plm3d__thick_line_z(plm_fb *fb, float *zbuf,
float x0, float y0, float z0,
float x1, float y1, float z1,
float width, plm_color c) {
if (width <= 1.0f) {
plm3d__line_z(fb, zbuf, x0, y0, z0, x1, y1, z1, c);
return;
}
/* Offset perpendicular to line direction */
float dx = x1 - x0, dy = y1 - y0;
float len = sqrtf(dx * dx + dy * dy);
if (len < 1e-6f) {
plm3d__line_z(fb, zbuf, x0, y0, z0, x1, y1, z1, c);
return;
}
float nx = -dy / len, ny = dx / len; /* perpendicular */
float half = (width - 1.0f) * 0.5f;
int n = (int)(half + 0.5f);
for (int i = -n; i <= n; i++) {
float off = (float)i;
plm3d__line_z(fb, zbuf,
x0 + nx * off, y0 + ny * off, z0,
x1 + nx * off, y1 + ny * off, z1, c);
}
}
/* ---- depth-tested filled circle ------------------------------------ */
static void plm3d__fill_circle_z(plm_fb *fb, float *zbuf,
float cx, float cy, float cz,
float r, plm_color c) {
int min_x = (int)(cx - r - 1);
int max_x = (int)(cx + r + 1);
int min_y = (int)(cy - r - 1);
int max_y = (int)(cy + r + 1);
float r2 = r * r;
for (int y = min_y; y <= max_y; y++) {
float dy = (float)y - cy;
for (int x = min_x; x <= max_x; x++) {
float dx = (float)x - cx;
if (dx * dx + dy * dy <= r2) {
plm3d__set_pixel_z(fb, zbuf, x, y, cz, c);
}
}
}
}
/* ---- depth-tested filled quad (barycentric triangle rasteriser) ---- */
/* Fill a triangle using barycentric coordinates with z-interpolation. */
static void plm3d__fill_triangle_z(plm_fb *fb, float *zbuf,
float x0, float y0, float z0,
float x1, float y1, float z1,
float x2, float y2, float z2,
plm_color c) {
float fmin_x = x0 < x1 ? (x0 < x2 ? x0 : x2) : (x1 < x2 ? x1 : x2);
float fmin_y = y0 < y1 ? (y0 < y2 ? y0 : y2) : (y1 < y2 ? y1 : y2);
float fmax_x = x0 > x1 ? (x0 > x2 ? x0 : x2) : (x1 > x2 ? x1 : x2);
float fmax_y = y0 > y1 ? (y0 > y2 ? y0 : y2) : (y1 > y2 ? y1 : y2);
int ix0 = (int)fmin_x;
int iy0 = (int)fmin_y;
int ix1 = (int)fmax_x;
int iy1 = (int)fmax_y;
float denom = (y1 - y2) * (x0 - x2) + (x2 - x1) * (y0 - y2);
float eps = -1e-6f;
if (denom > -1e-12f && denom < 1e-12f) return;
for (int y = iy0; y <= iy1; y++) {
for (int x = ix0; x <= ix1; x++) {
float px = (float)x + 0.5f;
float py = (float)y + 0.5f;
float w0 = ((y1 - y2) * (px - x2) + (x2 - x1) * (py - y2)) / denom;
float w1 = ((y2 - y0) * (px - x2) + (x0 - x2) * (py - y2)) / denom;
float w2 = 1.0f - w0 - w1;
if (w0 >= eps && w1 >= eps && w2 >= eps) {
float z = w0 * z0 + w1 * z1 + w2 * z2;
plm3d__set_pixel_z(fb, zbuf, x, y, z, c);
}
}
}
}
/* Fill a convex quad by splitting into two triangles. */
static void plm3d__fill_quad_z(plm_fb *fb, float *zbuf,
const float px[4], const float py[4],
const float pz[4], plm_color c) {
plm3d__fill_triangle_z(fb, zbuf,
px[0], py[0], pz[0],
px[1], py[1], pz[1],
px[2], py[2], pz[2], c);
plm3d__fill_triangle_z(fb, zbuf,
px[0], py[0], pz[0],
px[2], py[2], pz[2],
px[3], py[3], pz[3], c);
}
/* ---- data bounding-box helpers ------------------------------------- */
/* Autorange a single axis from all series data. */
static int plm3d__axis_autorange(plm_axis *ax, int dim,
plm3d_scatter_series *sc, int sc_n,
plm3d_line_series *li, int li_n,
plm3d_surface_series *su, int su_n,
plm3d_bar_series *ba, int ba_n,
plm3d_stem_series *st, int st_n,
plm3d_hist_series *hist, int hist_n) {
double lo = 1e30, hi = -1e30;
int found = 0;
/* helper: walk one series' dimension */
#define CHECK_DATASET(d, n) do { \
if (!(d) || (n) <= 0) break; \
for (int _j = 0; _j < (n); _j++) { \
float v = (d)[_j]; \
if (v < lo) lo = v; \
if (v > hi) hi = v; \
} \
found = 1; \
} while(0)
/* scatters */
for (int i = 0; i < sc_n; i++) {
if (dim == 0) CHECK_DATASET(sc[i].x_data, sc[i].count);
else if (dim == 1) CHECK_DATASET(sc[i].y_data, sc[i].count);
else CHECK_DATASET(sc[i].z_data, sc[i].count);
}
/* lines */
for (int i = 0; i < li_n; i++) {
if (dim == 0) CHECK_DATASET(li[i].x_data, li[i].count);
else if (dim == 1) CHECK_DATASET(li[i].y_data, li[i].count);
else CHECK_DATASET(li[i].z_data, li[i].count);
}
/* surfaces */
for (int i = 0; i < su_n; i++) {
int n = su[i].nx * su[i].ny;
if (dim == 0) CHECK_DATASET(su[i].x_data, su[i].nx);
else if (dim == 1) CHECK_DATASET(su[i].y_data, su[i].ny);
else CHECK_DATASET(su[i].z_data, n);
}
/* bars */
for (int i = 0; i < ba_n; i++) {
if (dim == 0) CHECK_DATASET(ba[i].x_data, ba[i].count);
else if (dim == 1) CHECK_DATASET(ba[i].y_data, ba[i].count);
else {
/* For Z, include 0 (base plane) in range */
CHECK_DATASET(ba[i].z_data, ba[i].count);
if (0.0 < lo) lo = 0.0;
if (0.0 > hi) hi = 0.0;
}
}
/* stems */
for (int i = 0; i < st_n; i++) {
if (dim == 0) CHECK_DATASET(st[i].x_data, st[i].count);
else if (dim == 1) CHECK_DATASET(st[i].y_data, st[i].count);
else {
CHECK_DATASET(st[i].z_data, st[i].count);
/* include baseline */
float bl = plm__isnan(st[i].baseline) ? (float)lo : st[i].baseline;
if (bl < lo) lo = bl;
if (bl > hi) hi = bl;
}
}
/* hists: quick binning pass for Z range; raw data for X/Y axes */
if (dim == 2) {
for (int i = 0; i < hist_n; i++) {
if (hist[i].count < 1 || hist[i].bins_x < 1 || hist[i].bins_y < 1) continue;
double hx_min = 1e30, hx_max = -1e30, hy_min = 1e30, hy_max = -1e30;
for (int j = 0; j < hist[i].count; j++) {
float vx = hist[i].x_data[j], vy = hist[i].y_data[j];
if (plm__isnan(vx) || plm__isnan(vy)) continue;
if (vx < hx_min) hx_min = vx; if (vx > hx_max) hx_max = vx;
if (vy < hy_min) hy_min = vy; if (vy > hy_max) hy_max = vy;
}
if (hx_min >= hx_max || hy_min >= hy_max) continue;
double bw_x = (hx_max - hx_min) / hist[i].bins_x;
double bw_y = (hy_max - hy_min) / hist[i].bins_y;
if (bw_x <= 0.0 || bw_y <= 0.0) continue;
int nbins = hist[i].bins_x * hist[i].bins_y;
int *cnt = (int *)calloc((size_t)nbins, sizeof(int));
if (!cnt) continue;
for (int j = 0; j < hist[i].count; j++) {
float vx = hist[i].x_data[j], vy = hist[i].y_data[j];
if (plm__isnan(vx) || plm__isnan(vy)) continue;
int bx = (int)((vx - hx_min) / bw_x);
int by = (int)((vy - hy_min) / bw_y);
if (bx < 0) bx = 0; if (bx >= hist[i].bins_x) bx = hist[i].bins_x - 1;
if (by < 0) by = 0; if (by >= hist[i].bins_y) by = hist[i].bins_y - 1;
cnt[bx * hist[i].bins_y + by]++;
}
for (int j = 0; j < nbins; j++) {
if (cnt[j] < lo) lo = cnt[j];
if (cnt[j] > hi) hi = cnt[j];
}
free(cnt);
found = 1;
if (0.0 < lo) lo = 0.0; /* include zero baseline */
}
} else {
for (int i = 0; i < hist_n; i++) {
if (dim == 0) CHECK_DATASET(hist[i].x_data, hist[i].count);
else CHECK_DATASET(hist[i].y_data, hist[i].count);
}
}
#undef CHECK_DATASET
if (!found) return 0;
/* add 5% padding */
double pad = (hi - lo) * 0.05;
if (pad <= 0.0) { pad = (lo == 0.0) ? 1.0 : fabs(lo) * 0.05; }
if (pad <= 0.0) pad = 1.0;
ax->min = lo - pad;
ax->max = hi + pad;
return 1;
}
/* ------------------------------------------------------------------ */
/* axis rendering */
/* ------------------------------------------------------------------ */
/* Draw the three origin-frame axis lines with tick marks. */
static void plm3d__draw_axes(plm3d_plot *p, plm_fb *fb, float *zbuf,
double ca, double sa, double ce, double se,
double plot_cx, double plot_cy, double scale) {
plm_color axis_colors[3] = {
{255, 80, 80, 255}, /* X: red-ish */
{80, 200, 80, 255}, /* Y: green-ish */
{80, 80, 255, 255} /* Z: blue-ish */
};
double x_range = p->x_axis.max - p->x_axis.min;
double y_range = p->y_axis.max - p->y_axis.min;
double z_range = p->z_axis.max - p->z_axis.min;
if (x_range <= 0.0) x_range = 1.0;
if (y_range <= 0.0) y_range = 1.0;
if (z_range <= 0.0) z_range = 1.0;
/* Choose the corner of the data cube farthest from the camera.
Test all 8 corners in normalised space; pick the one with
smallest projected depth (rz = farthest). */
double best_rz = 1e30;
int best_cx = 0, best_cy = 0, best_cz = 0;
for (int corner = 0; corner < 8; corner++) {
double tcx = (corner & 1) ? 0.5 : -0.5;
double tcy = (corner & 2) ? 0.5 : -0.5;
double tcz = (corner & 4) ? 0.5 : -0.5;
float rx, ry, rz;
plm3d__project(tcx, tcy, tcz, ca, sa, ce, se, p->view.distance, &rx, &ry, &rz);
if (rz < best_rz) { best_rz = rz; best_cx = corner & 1;
best_cy = (corner >> 1) & 1;
best_cz = (corner >> 2) & 1; }
}
/* Map back to data-space corner */
double ox = best_cx ? p->x_axis.max : p->x_axis.min;
double oy = best_cy ? p->y_axis.max : p->y_axis.min;
double oz = best_cz ? p->z_axis.max : p->z_axis.min;
/* Axis endpoints: from this corner along each axis to the opposite face */
double endpoints[3][3] = {
{best_cx ? p->x_axis.min : p->x_axis.max, oy, oz},
{ox, best_cy ? p->y_axis.min : p->y_axis.max, oz},
{ox, oy, best_cz ? p->z_axis.min : p->z_axis.max}
};
for (int axis = 0; axis < 3; axis++) {
double ex = endpoints[axis][0];
double ey = endpoints[axis][1];
double ez = endpoints[axis][2];
/* Normalize origin and endpoint to [-0.5, 0.5] */
double onx = (ox - p->x_axis.min) / x_range - 0.5;
double ony = (oy - p->y_axis.min) / y_range - 0.5;
double onz = (oz - p->z_axis.min) / z_range - 0.5;
double enx = (ex - p->x_axis.min) / x_range - 0.5;
double eny = (ey - p->y_axis.min) / y_range - 0.5;
double enz = (ez - p->z_axis.min) / z_range - 0.5;
float rx0, ry0, rz0, rx1, ry1, rz1;
plm3d__project(onx, ony, onz, ca, sa, ce, se, p->view.distance, &rx0, &ry0, &rz0);
plm3d__project(enx, eny, enz, ca, sa, ce, se, p->view.distance, &rx1, &ry1, &rz1);
float px0 = (float)(plot_cx + rx0 * scale);
float py0 = (float)(plot_cy - ry0 * scale); /* Y flip */
float px1 = (float)(plot_cx + rx1 * scale);
float py1 = (float)(plot_cy - ry1 * scale);
plm3d__thick_line_z(fb, zbuf, px0, py0, rz0, px1, py1, rz1,
2.0f, axis_colors[axis]);
/* Tick marks along the axis */
double range = (axis == 0) ? x_range :
(axis == 1) ? y_range : z_range;
double lo = (axis == 0) ? p->x_axis.min :
(axis == 1) ? p->y_axis.min : p->z_axis.min;
/* Simple nice-step ticks */
double rough = range / 5.0;
double exp = pow(10.0, floor(log10(rough)));
double mant = rough / exp;
double nice;
if (mant <= 1.5) nice = 1.0;
else if (mant <= 3.0) nice = 2.0;
else if (mant <= 7.0) nice = 5.0;
else nice = 10.0;
double step = nice * exp;
if (step <= 0.0) step = 1.0;
double tick_start = ceil(lo / step) * step;
/* Tick direction in screen space (perpendicular to axis) */
float adx_s = px1 - px0, ady_s = py1 - py0;
float alen_s = sqrtf(adx_s * adx_s + ady_s * ady_s);
if (alen_s < 1.0f) alen_s = 1.0f;
float anx_s = adx_s / alen_s, any_s = ady_s / alen_s;
/* perpendicular: rotate 90 degrees clockwise in screen space */
float tnx_s = -any_s, tny_s = anx_s;
float tick_len_px = 6.0f; /* tick length in screen pixels */
for (double v = tick_start; v <= lo + range; v += step) {
double tx, ty, tz;
tx = (axis == 0) ? v : ox;
ty = (axis == 1) ? v : oy;
tz = (axis == 2) ? v : oz;
/* project tick base to screen */
double tnx0 = (tx - p->x_axis.min) / x_range - 0.5;
double tny0 = (ty - p->y_axis.min) / y_range - 0.5;
double tnz0 = (tz - p->z_axis.min) / z_range - 0.5;
float trx0, try0, trz0;
plm3d__project(tnx0, tny0, tnz0, ca, sa, ce, se, p->view.distance, &trx0, &try0, &trz0);
float tpx0 = (float)(plot_cx + trx0 * scale);
float tpy0 = (float)(plot_cy - try0 * scale);
/* tick tip = base + perpendicular * pixel_length */
float tpx1 = tpx0 + tnx_s * tick_len_px;
float tpy1 = tpy0 + tny_s * tick_len_px;
plm3d__line_z(fb, zbuf, tpx0, tpy0, trz0, tpx1, tpy1, trz0,
axis_colors[axis]);
}
/* ---- grid lines (if axis.grid is set) ---- */
{
plm_axis *ax = (axis == 0) ? &p->x_axis :
(axis == 1) ? &p->y_axis : &p->z_axis;
if (ax->grid) {
plm_color grid_c = PLM_GRID_COLOR;
float grid_lw = 0.6f;
for (double v = tick_start; v <= lo + range; v += step) {
double gx0, gy0, gz0, gx1, gy1, gz1;
if (axis == 0) {
/* X-grid: line at constant X, varying Y, at Z=z_min */
gx0 = gx1 = v;
gy0 = p->y_axis.min; gy1 = p->y_axis.max;
gz0 = gz1 = p->z_axis.min;
} else if (axis == 1) {
/* Y-grid: line at constant Y, varying X, at Z=z_min */
gx0 = p->x_axis.min; gx1 = p->x_axis.max;
gy0 = gy1 = v;
gz0 = gz1 = p->z_axis.min;
} else {
/* Z-grid: line at constant Z, varying X, at Y=y_min */
gx0 = p->x_axis.min; gx1 = p->x_axis.max;
gy0 = gy1 = p->y_axis.min;
gz0 = gz1 = v;
}
double gnx0 = (gx0 - p->x_axis.min) / x_range - 0.5;
double gny0 = (gy0 - p->y_axis.min) / y_range - 0.5;
double gnz0 = (gz0 - p->z_axis.min) / z_range - 0.5;
double gnx1 = (gx1 - p->x_axis.min) / x_range - 0.5;
double gny1 = (gy1 - p->y_axis.min) / y_range - 0.5;
double gnz1 = (gz1 - p->z_axis.min) / z_range - 0.5;
float grx0, gry0, grz0, grx1, gry1, grz1;
plm3d__project(gnx0, gny0, gnz0, ca, sa, ce, se,
p->view.distance, &grx0, &gry0, &grz0);
plm3d__project(gnx1, gny1, gnz1, ca, sa, ce, se,
p->view.distance, &grx1, &gry1, &grz1);
float gpx0 = (float)(plot_cx + grx0 * scale);
float gpy0 = (float)(plot_cy - gry0 * scale);
float gpx1 = (float)(plot_cx + grx1 * scale);
float gpy1 = (float)(plot_cy - gry1 * scale);
plm3d__thick_line_z(fb, zbuf, gpx0, gpy0, grz0,
gpx1, gpy1, grz1, grid_lw, grid_c);
}
}
}
}
}
/* ------------------------------------------------------------------ */
/* series rendering */
/* ------------------------------------------------------------------ */
static void plm3d__render_scatters(plm3d_plot *p, plm_fb *fb, float *zbuf,
double ca, double sa, double ce, double se,
double plot_cx, double plot_cy, double scale,
double x_range, double y_range, double z_range) {
for (int si = 0; si < p->scatter_count; si++) {
plm3d_scatter_series *ss = &p->scatters[si];
int use_cmap = (ss->style.cmap != PLM_CMAP_NONE);
const float *cdata = ss->style.color_data ? ss->style.color_data : ss->z_data;
/* compute range of the colour-mapped variable */
double cmin = p->z_axis.min, crange = z_range;
if (use_cmap && ss->style.color_data) {
cmin = 1e30; double cmax = -1e30;
for (int i = 0; i < ss->count; i++) {
float v = cdata[i];
if (!plm__isnan(v)) {
if (v < cmin) cmin = v;
if (v > cmax) cmax = v;
}
}
if (cmin >= cmax) { cmin -= 0.5; cmax += 0.5; }
crange = cmax - cmin;
if (crange <= 0.0) crange = 1.0;
}
for (int i = 0; i < ss->count; i++) {
double nx = (ss->x_data[i] - p->x_axis.min) / x_range - 0.5;
double ny = (ss->y_data[i] - p->y_axis.min) / y_range - 0.5;
double nz = (ss->z_data[i] - p->z_axis.min) / z_range - 0.5;
if (plm__isnan(ss->x_data[i]) || plm__isnan(ss->y_data[i]) || plm__isnan(ss->z_data[i])) continue;
float rx, ry, rz;
plm3d__project(nx, ny, nz, ca, sa, ce, se, p->view.distance, &rx, &ry, &rz);
float px = (float)(plot_cx + rx * scale);
float py = (float)(plot_cy - ry * scale);
plm_color sc = ss->style.color;
if (use_cmap) {
float cv = (!plm__isnan(cdata[i])) ? cdata[i] : (float)ss->z_data[i];
float t = (float)((cv - cmin) / crange);
if (t < 0.0f) t = 0.0f; if (t > 1.0f) t = 1.0f;
sc = plm_cmap_lookup(t, ss->style.cmap);
}
plm3d__fill_circle_z(fb, zbuf, px, py, rz,
ss->style.radius, sc);
}
}
}
static void plm3d__render_lines(plm3d_plot *p, plm_fb *fb, float *zbuf,
double ca, double sa, double ce, double se,
double plot_cx, double plot_cy, double scale,
double x_range, double y_range, double z_range) {
double z_data_min = p->z_axis.min;
for (int si = 0; si < p->line_count; si++) {
plm3d_line_series *ls = &p->lines[si];
if (ls->count < 2) continue;
int use_cmap = (ls->style.cmap != PLM_CMAP_NONE);
/* Project first point */
double nx0 = (ls->x_data[0] - p->x_axis.min) / x_range - 0.5;
double ny0 = (ls->y_data[0] - p->y_axis.min) / y_range - 0.5;
double nz0 = (ls->z_data[0] - p->z_axis.min) / z_range - 0.5;
float rx0, ry0, rz0;
plm3d__project(nx0, ny0, nz0, ca, sa, ce, se, p->view.distance, &rx0, &ry0, &rz0);
float px0 = (float)(plot_cx + rx0 * scale);
float py0 = (float)(plot_cy - ry0 * scale);
float z0 = ls->z_data[0];
for (int i = 1; i < ls->count; i++) {
double nx1 = (ls->x_data[i] - p->x_axis.min) / x_range - 0.5;
double ny1 = (ls->y_data[i] - p->y_axis.min) / y_range - 0.5;
double nz1 = (ls->z_data[i] - p->z_axis.min) / z_range - 0.5;
float rx1, ry1, rz1;
plm3d__project(nx1, ny1, nz1, ca, sa, ce, se, p->view.distance, &rx1, &ry1, &rz1);
float px1 = (float)(plot_cx + rx1 * scale);
float py1 = (float)(plot_cy - ry1 * scale);
float z1 = ls->z_data[i];
plm_color lc = ls->style.color;
if (use_cmap) {
/* colour segment by midpoint Z */
float zmid = (z0 + z1) * 0.5f;
float t = (float)((zmid - z_data_min) / z_range);
if (t < 0.0f) t = 0.0f; if (t > 1.0f) t = 1.0f;
lc = plm_cmap_lookup(t, ls->style.cmap);
}
if (ls->style.width <= 1.0f)
plm3d__wu_line_z(fb, zbuf, px0, py0, rz0, px1, py1, rz1, lc);
else
plm3d__thick_line_z(fb, zbuf, px0, py0, rz0, px1, py1, rz1,
ls->style.width, lc);
px0 = px1; py0 = py1; rz0 = rz1; z0 = z1;
}
}
}
static void plm3d__render_surfaces(plm3d_plot *p, plm_fb *fb, float *zbuf,
double ca, double sa, double ce, double se,
double plot_cx, double plot_cy, double scale,
double x_range, double y_range, double z_range) {
for (int si = 0; si < p->surface_count; si++) {
plm3d_surface_series *ss = &p->surfaces[si];
int nx = ss->nx, ny = ss->ny;
if (nx < 2 || ny < 2) continue;
/* Pre-project all grid points */
/* We need nx*ny projected points. Use stack for small grids,
heap for large ones. For prototype, allocate on heap. */
float *px = (float *)malloc((size_t)nx * ny * sizeof(float));
float *py = (float *)malloc((size_t)nx * ny * sizeof(float));
float *pz = (float *)malloc((size_t)nx * ny * sizeof(float));
if (!px || !py || !pz) {
free(px); free(py); free(pz);
continue;
}
for (int i = 0; i < nx; i++) {
for (int j = 0; j < ny; j++) {
int idx = i * ny + j;
double nxv = (ss->x_data[i] - p->x_axis.min) / x_range - 0.5;
double nyv = (ss->y_data[j] - p->y_axis.min) / y_range - 0.5;
double nzv = (ss->z_data[idx] - p->z_axis.min) / z_range - 0.5;
float rx, ry, rz;
plm3d__project(nxv, nyv, nzv, ca, sa, ce, se, p->view.distance, &rx, &ry, &rz);
px[idx] = (float)(plot_cx + rx * scale);
py[idx] = (float)(plot_cy - ry * scale);
pz[idx] = rz;
}
}
/* ---- fill quads for proper occlusion (z-buffer) ---- */
if (!ss->style.wireframe_only) {
int use_cmap = (ss->style.cmap != PLM_CMAP_NONE);
float lighting = ss->style.light;
plm_color solid_fill_c;
if (!use_cmap) {
int fr = ss->style.line_color.r + (255 - ss->style.line_color.r) * 2 / 3;
int fg = ss->style.line_color.g + (255 - ss->style.line_color.g) * 2 / 3;
int fb2 = ss->style.line_color.b + (255 - ss->style.line_color.b) * 2 / 3;
solid_fill_c = PLM_RGBA(fr, fg, fb2, 255);
}
double z_data_min = p->z_axis.min;
double z_data_range = z_range;
/* light direction: use custom azimuth/elevation if set, else camera direction */
float lx, ly, lz;
if (ss->style.light_azimuth != 0.0f || ss->style.light_elevation != 0.0f) {
double la = ss->style.light_azimuth * 3.141592653589793 / 180.0;