-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplotter.cpp
More file actions
158 lines (139 loc) · 4.07 KB
/
Copy pathplotter.cpp
File metadata and controls
158 lines (139 loc) · 4.07 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
#include "plotter.h"
#include <vector>
#if defined(WIN32) || defined(_WIN32) || defined (__WIN32) || defined(__WIN32__) \
|| defined (_WIN64) || defined(__CYGWIN__) || defined(__MINGW32__)
bool isnan(float f)
{return f != f;}
#endif
Plotter::Plotter(std::map <std::string, DERIVED <Adjustment>, AdjustmentComparator> & newmap) : adjustmentmap(newmap)
{
}
bool Plotter::on_expose_event(GdkEventExpose* event)
{
// This is where we draw on the window
Glib::RefPtr<Gdk::Window> window = get_window();
if(window)
{
Gtk::Allocation allocation = get_allocation();
const int width = allocation.get_width();
const int height = allocation.get_height();
// coordinates for the center of the window
int xc, yc;
xc = width / 2;
yc = height / 2;
Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
// clip to the area indicated by the expose event so that we only redraw
// the portion of the window that needs to be redrawn
cr->rectangle(event->area.x, event->area.y,
event->area.width, event->area.height);
cr->clip();
//draw curve
cr->set_line_width(1.0);
float zoom = adjustmentmap["zoom"]->GetValue();
float camber = adjustmentmap["camber"]->GetValue();
float load = adjustmentmap["load"]->GetValue();
zoom = (1.0-zoom)*4.0+0.1;
load = load * 0.001;
if (UpdateTire())
{
{
//draw lateral line
cr->set_source_rgb(1.0, 0.0, 0.0);
float x0 = -zoom*0.5*20.0;
float xn = zoom*0.5*20.0;
float ymin = -1000.0;
float ymax = 1000.0;
int points = 500;
for (float x = x0; x <= xn; x += (xn-x0)/points)
{
float maxforce;
float yval = tire.Pacejka_Fy(x, load, camber, 1.0, maxforce)/load;
float xval = width*(x-x0)/(xn-x0);
yval /= ymax-ymin;
yval = (yval+1.0)*0.5;
yval = 1.0 - yval;
yval *= height;
if (x == x0)
cr->move_to(xval, yval);
else
cr->line_to(xval, yval);
//std::cout << xval << ", " << yval << std::endl;
}
cr->stroke();
}
{
//draw longitudinal line
cr->set_source_rgb(0.0, 1.0, 0.0);
float x0 = -zoom*0.5;
float xn = zoom*0.5;
float ymin = -1000.0;
float ymax = 1000.0;
int points = 500;
for (float x = x0; x <= xn; x += (xn-x0)/points)
{
float maxforce;
float yval = tire.Pacejka_Fx(x, load, 1.0, maxforce)/load;
float xval = width*(x-x0)/(xn-x0);
yval /= ymax-ymin;
yval = (yval+1.0)*0.5;
yval = 1.0 - yval;
yval *= height;
if (x == x0)
cr->move_to(xval, yval);
else
cr->line_to(xval, yval);
//std::cout << xval << ", " << yval << std::endl;
}
cr->stroke();
}
{
//draw aligning line
cr->set_source_rgb(0.0, 0.0, 1.0);
float x0 = -zoom*0.5*10.0;
float xn = zoom*0.5*10.0;
float ymin = -60.0;
float ymax = 60.0;
int points = 500;
for (float x = x0; x <= xn; x += (xn-x0)/points)
{
float maxforce;
float yval = tire.Pacejka_Mz(0, x, load, camber*(180.0/3.141593), 1.0, maxforce)/load;
float xval = width*(x-x0)/(xn-x0);
yval /= ymax-ymin;
yval = (yval+1.0)*0.5;
yval = 1.0 - yval;
yval *= height;
if (x == x0)
cr->move_to(xval, yval);
else
cr->line_to(xval, yval);
//std::cout << xval << ", " << yval << std::endl;
}
cr->stroke();
}
// draw grid lines
cr->set_line_width(1.0);
cr->set_source_rgb(0.0, 0.0, 0.0);
cr->move_to(xc, 0);
cr->line_to(xc, yc*2);
cr->move_to(0, yc);
cr->line_to(xc*2, yc);
cr->stroke();
}
}
return true;
}
bool Plotter::UpdateTire()
{
std::map <char, std::vector <float> > params;
for (std::map <std::string, DERIVED <Adjustment>, AdjustmentComparator>::const_iterator i = adjustmentmap.begin(); i != adjustmentmap.end(); i++)
{
if (i->first != "camber")
params[i->first[0]].push_back(i->second->GetValue());
}
tire.SetPacejkaParameters(params['b'], params['a'], params['c']);
/*for (int i = 0; i < params['b'].size(); i++)
std::cout << "b" << i << ": " << params['b'][i] << std::endl;*/
tire.CalculateSigmaHatAlphaHat();
return (params['b'][0] != 0);
}