-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.cpp
More file actions
282 lines (243 loc) · 8.78 KB
/
Copy pathutils.cpp
File metadata and controls
282 lines (243 loc) · 8.78 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
#include "utils.h"
#include "raylib.h"
#include "rlgl.h"
#define PAR_MALLOC(T, N) ((T *)RL_MALLOC(N * sizeof(T)))
#define PAR_CALLOC(T, N) ((T *)RL_CALLOC(N * sizeof(T), 1))
#define PAR_REALLOC(T, BUF, N) ((T *)RL_REALLOC(BUF, sizeof(T) * (N)))
#define PAR_FREE RL_FREE
#include "raylib/src/external/par_shapes.h"
#ifndef DEFAULT_MESH_VERTEX_BUFFERS
# define DEFAULT_MESH_VERTEX_BUFFERS 7
#endif
#include "imgui.h"
Matrix convert(const sva::PTransformd & pt)
{
auto mat = sva::conversions::toHomogeneous(pt.cast<float>());
Matrix out;
out.m0 = mat(0, 0);
out.m1 = mat(1, 0);
out.m2 = mat(2, 0);
out.m3 = mat(3, 0);
out.m4 = mat(0, 1);
out.m5 = mat(1, 1);
out.m6 = mat(2, 1);
out.m7 = mat(3, 1);
out.m8 = mat(0, 2);
out.m9 = mat(1, 2);
out.m10 = mat(2, 2);
out.m11 = mat(3, 2);
out.m12 = mat(0, 3);
out.m13 = mat(1, 3);
out.m14 = mat(2, 3);
out.m15 = mat(3, 3);
return out;
}
Vector3 translation(const sva::PTransformd & pt)
{
const auto & t = pt.translation().cast<float>();
return {t.x(), t.y(), t.z()};
}
Vector3 intersection(Ray ray, Vector3 normal, Vector3 point)
{
float n_d = Vector3DotProduct(normal, ray.direction);
if(n_d == 0.0f)
{
printf("NO INTERSECTION\n");
return point;
}
float t = (Vector3DotProduct(normal, point) - Vector3DotProduct(normal, ray.position)) / n_d;
return Vector3Add(ray.position, Vector3Scale(ray.direction, t));
}
void DrawGridXY(int slices, float spacing)
{
int halfSlices = slices / 2;
if(rlCheckBufferLimit(slices * 4)) rlglDraw();
rlBegin(RL_LINES);
for(int i = -halfSlices; i <= halfSlices; i++)
{
if(i == 0)
{
rlColor3f(0.5f, 0.5f, 0.5f);
rlColor3f(0.5f, 0.5f, 0.5f);
rlColor3f(0.5f, 0.5f, 0.5f);
rlColor3f(0.5f, 0.5f, 0.5f);
}
else
{
rlColor3f(0.75f, 0.75f, 0.75f);
rlColor3f(0.75f, 0.75f, 0.75f);
rlColor3f(0.75f, 0.75f, 0.75f);
rlColor3f(0.75f, 0.75f, 0.75f);
}
rlVertex3f((float)i * spacing, (float)-halfSlices * spacing, 0.0f);
rlVertex3f((float)i * spacing, (float)halfSlices * spacing, 0.0f);
rlVertex3f((float)-halfSlices * spacing, (float)i * spacing, 0.0f);
rlVertex3f((float)halfSlices * spacing, (float)i * spacing, 0.0f);
}
rlEnd();
}
void DrawCylinderEx(Vector3 position,
Vector3 normal,
float radiusTop,
float radiusBottom,
float height,
int sides,
Color color)
{
if(sides < 3) sides = 3;
normal = Vector3Normalize(normal);
int numVertex = sides * 6;
if(rlCheckBufferLimit(numVertex)) rlglDraw();
rlPushMatrix();
{
rlLoadIdentity();
rlTranslatef(position.x, position.y, position.z);
auto vec = Vector3CrossProduct({0, 1, 0}, normal);
rlRotatef(RAD2DEG * acosf(Vector3DotProduct({0, 1, 0}, normal)), vec.x, vec.y, vec.z);
rlBegin(RL_TRIANGLES);
{
rlColor4ub(color.r, color.g, color.b, color.a);
if(radiusTop > 0)
{
// Draw Body
for(int i = 0; i < 360; i += 360 / sides)
{
rlVertex3f(sinf(DEG2RAD * i) * radiusBottom, 0, cosf(DEG2RAD * i) * radiusBottom); // Bottom Left
rlVertex3f(sinf(DEG2RAD * (i + 360 / sides)) * radiusBottom, 0,
cosf(DEG2RAD * (i + 360 / sides)) * radiusBottom); // Bottom Right
rlVertex3f(sinf(DEG2RAD * (i + 360 / sides)) * radiusTop, height,
cosf(DEG2RAD * (i + 360 / sides)) * radiusTop); // Top Right
rlVertex3f(sinf(DEG2RAD * i) * radiusTop, height, cosf(DEG2RAD * i) * radiusTop); // Top Left
rlVertex3f(sinf(DEG2RAD * i) * radiusBottom, 0, cosf(DEG2RAD * i) * radiusBottom); // Bottom Left
rlVertex3f(sinf(DEG2RAD * (i + 360 / sides)) * radiusTop, height,
cosf(DEG2RAD * (i + 360 / sides)) * radiusTop); // Top Right
}
// Draw Cap
for(int i = 0; i < 360; i += 360 / sides)
{
rlVertex3f(0, height, 0);
rlVertex3f(sinf(DEG2RAD * i) * radiusTop, height, cosf(DEG2RAD * i) * radiusTop);
rlVertex3f(sinf(DEG2RAD * (i + 360 / sides)) * radiusTop, height,
cosf(DEG2RAD * (i + 360 / sides)) * radiusTop);
}
}
else
{
// Draw Cone
for(int i = 0; i < 360; i += 360 / sides)
{
rlVertex3f(0, height, 0);
rlVertex3f(sinf(DEG2RAD * i) * radiusBottom, 0, cosf(DEG2RAD * i) * radiusBottom);
rlVertex3f(sinf(DEG2RAD * (i + 360 / sides)) * radiusBottom, 0,
cosf(DEG2RAD * (i + 360 / sides)) * radiusBottom);
}
}
// Draw Base
for(int i = 0; i < 360; i += 360 / sides)
{
rlVertex3f(0, 0, 0);
rlVertex3f(sinf(DEG2RAD * (i + 360 / sides)) * radiusBottom, 0,
cosf(DEG2RAD * (i + 360 / sides)) * radiusBottom);
rlVertex3f(sinf(DEG2RAD * i) * radiusBottom, 0, cosf(DEG2RAD * i) * radiusBottom);
}
}
rlEnd();
}
rlPopMatrix();
}
void DrawArrow(Vector3 p0, Vector3 p1, float shaft_diam, float head_diam, float head_len, Color color)
{
Vector3 normal = Vector3Subtract(p1, p0);
float height = Vector3Length(normal);
if(height == 0.0f)
{
return;
}
normal = Vector3Scale(normal, 1 / height);
if(head_len >= height)
{
head_len = height;
}
float shaft_len = height - head_len;
if(shaft_len != 0)
{
DrawCylinderEx(p0, normal, shaft_diam / 2, shaft_diam / 2, shaft_len, 8, color);
}
DrawCylinderEx(Vector3Add(p0, Vector3Scale(normal, shaft_len)), normal, 0, head_diam / 2, head_len, 8, color);
}
void DrawFrame(const sva::PTransformd & pose)
{
auto px = translation(sva::PTransformd(Eigen::Vector3d{0.15, 0, 0}) * pose);
auto py = translation(sva::PTransformd(Eigen::Vector3d{0, 0.15, 0}) * pose);
auto pz = translation(sva::PTransformd(Eigen::Vector3d{0, 0, 0.15}) * pose);
auto p0 = translation(pose);
DrawArrow(p0, px, 0.15 * 0.15, 0.15 * 0.15, 0.5 * 0.15, RED);
DrawArrow(p0, py, 0.15 * 0.15, 0.15 * 0.15, 0.5 * 0.15, GREEN);
DrawArrow(p0, pz, 0.15 * 0.15, 0.15 * 0.15, 0.5 * 0.15, BLUE);
}
void Combo(const char * label,
const std::vector<std::string> & values,
const std::string & current,
std::function<void(const std::string &)> callback)
{
if(ImGui::BeginCombo(label, current.c_str()))
{
for(const auto & v : values)
{
bool active = v == current;
if(ImGui::Selectable(v.c_str(), active))
{
callback(v);
}
if(active)
{
ImGui::SetItemDefaultFocus();
}
}
ImGui::EndCombo();
}
}
void Combo(const char * label, const std::vector<std::string> & values, std::string & current)
{
Combo(label, values, current, [¤t](const std::string & v) { current = v; });
}
Mesh GenMeshCylinderROS(float radius, float height, int slices)
{
Mesh mesh = {};
mesh.vboId = (unsigned int *)RL_CALLOC(DEFAULT_MESH_VERTEX_BUFFERS, sizeof(unsigned int));
// Generate the cylinder body
par_shapes_mesh * cylinder = par_shapes_create_cylinder(slices, 8);
par_shapes_scale(cylinder, radius, radius, height);
par_shapes_translate(cylinder, 0, 0, -height / 2);
// Generate an orientable disk shape (top cap)
float cap_center[3] = {0, 0, height / 2};
float cap_normal[3] = {0, 0, 1};
par_shapes_mesh * capTop = par_shapes_create_disk(radius, slices, cap_center, cap_normal);
capTop->tcoords = PAR_MALLOC(float, 2 * capTop->npoints);
for(int i = 0; i < 2 * capTop->npoints; i++) capTop->tcoords[i] = 0.0f;
par_shapes_merge(cylinder, capTop);
// Generate an orientable disk shape (bottom cap)
float rot_axis[3] = {1, 0, 0};
par_shapes_rotate(capTop, M_PI, rot_axis);
par_shapes_merge_and_free(cylinder, capTop);
mesh.vertices = (float *)RL_MALLOC(cylinder->ntriangles * 3 * 3 * sizeof(float));
mesh.texcoords = (float *)RL_MALLOC(cylinder->ntriangles * 3 * 2 * sizeof(float));
mesh.normals = (float *)RL_MALLOC(cylinder->ntriangles * 3 * 3 * sizeof(float));
mesh.vertexCount = cylinder->ntriangles * 3;
mesh.triangleCount = cylinder->ntriangles;
for(int k = 0; k < mesh.vertexCount; k++)
{
mesh.vertices[k * 3] = cylinder->points[cylinder->triangles[k] * 3];
mesh.vertices[k * 3 + 1] = cylinder->points[cylinder->triangles[k] * 3 + 1];
mesh.vertices[k * 3 + 2] = cylinder->points[cylinder->triangles[k] * 3 + 2];
mesh.normals[k * 3] = cylinder->normals[cylinder->triangles[k] * 3];
mesh.normals[k * 3 + 1] = cylinder->normals[cylinder->triangles[k] * 3 + 1];
mesh.normals[k * 3 + 2] = cylinder->normals[cylinder->triangles[k] * 3 + 2];
mesh.texcoords[k * 2] = cylinder->tcoords[cylinder->triangles[k] * 2];
mesh.texcoords[k * 2 + 1] = cylinder->tcoords[cylinder->triangles[k] * 2 + 1];
}
par_shapes_free_mesh(cylinder);
// Upload vertex data to GPU (static mesh)
rlLoadMesh(&mesh, false);
return mesh;
}