-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomDataAndGetters.nim
57 lines (49 loc) · 2.21 KB
/
CustomDataAndGetters.nim
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
import std/[math]
import imgui
import implot
import utils
#// Example for Custom Data and Getters section.
type
Vector2f = object
x, y:cfloat
proc Spiral(data: pointer, idx: cint , point: ptr ImPlotPoint) : pointer {.cdecl.} =
var
r = 0.9f #// outer radius
a = 0f #// inner radius
b = 0.05f #// increment per rev
n = (r - a) / b #// number of revolutions
let th = (2 * n * 3.14).cdouble; #// angle
let Th = (th * idx.cdouble / (1000 - 1)).cfloat
point.x = (0.5f + (a + b*Th / (2.0f * 3.14))*cos(Th)).cfloat
point.y = (0.5f + (a + b*Th / (2.0f * 3.14))*sin(Th)).cfloat
#-----------------------------------
# demo_Custom_CustomDataAndGetters()
#-----------------------------------
proc demo_Custom_CustomDataAndGetters*() =
igBulletText("You can plot custom structs using the stride feature.")
igBulletText("Most plotters can also be passed a function pointer for getting data.")
igIndent()
igBulletText("You can optionally pass user data to be given to your getter function.")
igBulletText("C++ lambdas can be passed as function pointers as well!")
igUnindent()
var vec2_data = [ Vector2f(x: 0,y: 0), Vector2f(x:1,y: 1) ]
if ipBeginPlot("##Custom Data"):
defer: ipEndPlot()
#// custom structs using stride example:
ipPlotLine("Vector2f", vec2_data[0].x.addr, vec2_data[0].y.addr
, 2
, 0.ImPlotLineFlags, 0, #[sizeof(Vector2f).int32]# (sizeof(cfloat) * 2).int32 )
#// custom getter example 1:
ipPlotLineG("Spiral", Spiral, nullptr, 1000)
#// custom getter example 2:
var
data1 = WaveData(X: 0.001, Amp: 0.2, Freq: 2, Offset: 0.75)
data2 = WaveData(X: 0.001, Amp: 0.2, Freq: 4, Offset: 0.25)
ipPlotLineG("Waves", SineWave2, data1.addr, 1000)
ipPlotLineG("Waves", SawWave, data2.addr, 1000)
ipPushStyleVar(ImPlotStyleVar.FillAlpha, 0.25f)
ipPlotShadedG("Waves", SineWave2, data1.addr, SawWave, data2.addr, 1000)
ipPopStyleVar()
#// you can also pass C++ lambdas:
#// auto lamda = [](void* data, int idx) { ... return ImPlotPoint(x,y); }
#// ipPlotLine("My Lambda", lambda, data, 1000)