-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.cpp
More file actions
287 lines (238 loc) · 6.87 KB
/
Copy pathtools.cpp
File metadata and controls
287 lines (238 loc) · 6.87 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
/*
Definition of the MCP tools exposed by the application.
*/
#include "config.h"
extern "C" {
#include "mcp.h"
}
#include "tools.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
vec3 edgeColor;
vec3 backgroundColor;
float thickness;
int geometryType;
int symmetry;
int animationOn;
int setColor(const char* name,vec3* color)
{
color->x=1.0 ; color->y=1.0; color->z=1.0;
if (strcmp(name,"red")==0)
{
color->x=1.0 ; color->y=0.0;color->z=0.0;
return 0;
}
if (strcmp(name,"green")==0)
{
color->x=0.0 ; color->y=1.0;color->z=0.0;
return 0;
}
if (strcmp(name,"blue")==0)
{
color->x=0.0 ; color->y=0.0;color->z=1.0;
return 0;
}
if (strcmp(name,"white")==0)
{
color->x=1.0 ; color->y=1.0;color->z=1.0;
return 0;
}
if (strcmp(name,"black")==0)
{
color->x=0.0 ; color->y=0.0;color->z=0.0;
return 0;
}
if (strcmp(name,"gray")==0)
{
color->x=0.5 ; color->y=0.5;color->z=0.5;
return 0;
}
return 1;
}
void setDefaults()
{
(void)setColor("black",&edgeColor);
(void)setColor("black",&backgroundColor);
thickness=0.01;
geometryType=0;
symmetry=0;
animationOn=1;
}
static cJSON *tool_reset(cJSON *args)
{
setDefaults();
return create_result_text("reset to defaults");
}
static cJSON *tool_set_animation(cJSON *args)
{
const cJSON *status = cJSON_GetObjectItemCaseSensitive(args, "on");
const int error = cJSON_IsBool(status) ? 0 : 1;
const cJSON_bool animaStatus = cJSON_IsBool(status) && cJSON_IsTrue(status);
cJSON *res;
if (!error && animaStatus)
{
animationOn=1;
res=create_result_text("animation started");
}
else if (!error && !animaStatus)
{
animationOn=0;
res=create_result_text("animation stopped");
}
else
{
res=create_result_text("unknown value");
}
return res;
}
static cJSON *tool_set_geometry(cJSON *args)
{
const cJSON *status = cJSON_GetObjectItemCaseSensitive(args, "geometry");
const char *geom = (cJSON_IsString(status) && status->valuestring) ? status->valuestring : "";
cJSON *res;
if (strcmp(geom,"disk")==0)
{
geometryType=0;
res=create_result_text("geometry changed to disk");
}
else if (strcmp(geom,"plane")==0)
{
geometryType=1;
res=create_result_text("geometry changed to plane");
}
else
{
res=create_result_text("unknown value");
}
return res;
}
static cJSON *tool_set_symmetry(cJSON *args)
{
const cJSON *status = cJSON_GetObjectItemCaseSensitive(args, "symmetry");
const int sym = (cJSON_IsNumber(status) && status->valueint) ? status->valueint : 0;
cJSON *res;
if ((sym >= 0) && (sym < 3))
{
symmetry=sym;
res=create_result_text("symmetry changed");
}
else
{
res=create_result_text("unknown value");
}
return res;
}
static cJSON *tool_set_edge_color(cJSON *args)
{
const cJSON *color = cJSON_GetObjectItemCaseSensitive(args, "color");
const char *col = (cJSON_IsString(color) && color->valuestring) ? color->valuestring : "";
int err = setColor(col,&edgeColor);
cJSON *res;
if (err)
{
res=create_result_text("unknown color");
}
else
{
res=create_result_text("color changed");
}
return res;
}
static cJSON *tool_set_background_color(cJSON *args)
{
const cJSON *color = cJSON_GetObjectItemCaseSensitive(args, "color");
const char *col = (cJSON_IsString(color) && color->valuestring) ? color->valuestring : "";
int err = setColor(col,&backgroundColor);
cJSON *res;
if (err)
{
res=create_result_text("unknown color");
}
else
{
res=create_result_text("color changed");
}
return res;
}
static cJSON *tool_add(cJSON *args)
{
const cJSON *a = cJSON_GetObjectItemCaseSensitive(args, "a");
const cJSON *b = cJSON_GetObjectItemCaseSensitive(args, "b");
double ad = cJSON_IsNumber(a) ? a->valuedouble : 0.0;
double bd = cJSON_IsNumber(b) ? b->valuedouble : 0.0;
double sum = ad + bd;
char buf[64];
snprintf(buf, sizeof(buf), "%.17g", sum);
cJSON *res = create_result_text(buf);
return res;
}
cJSON *handle_tools_call(cJSON *id, cJSON *params)
{
if (!cJSON_IsObject(params))
return err(id, MCP_INVALID_PARAMS, "Invalid params");
const cJSON *name = cJSON_GetObjectItemCaseSensitive(params, "name");
const cJSON *arguments = cJSON_GetObjectItemCaseSensitive(params, "arguments");
if (!cJSON_IsString(name) || !name->valuestring)
return err(id, MCP_METHOD_NOT_FOUND, "Missing tool name");
if (!cJSON_IsObject(arguments))
return err(id, MCP_INVALID_PARAMS, "Missing arguments");
cJSON *result = NULL;
if (strcmp(name->valuestring, "edgeColor") == 0)
{
result = tool_set_edge_color((cJSON *)arguments);
}
else if (strcmp(name->valuestring, "backgroundColor") == 0)
{
result = tool_set_background_color((cJSON *)arguments);
}
else if (strcmp(name->valuestring, "animationOn") == 0)
{
result = tool_set_animation((cJSON *)arguments);
}
else if (strcmp(name->valuestring, "geometryType") == 0)
{
result = tool_set_geometry((cJSON *)arguments);
}
else if (strcmp(name->valuestring, "symmetryType") == 0)
{
result = tool_set_symmetry((cJSON *)arguments);
}
else if (strcmp(name->valuestring, "reset") == 0)
{
result = tool_reset((cJSON *)arguments);
}
//else if (strcmp(name->valuestring, "add") == 0)
//{
// result = tool_add((cJSON *)arguments);
//}
else
{
return err(id, MCP_METHOD_NOT_FOUND, "Unknown tool");
}
return ok(id, result);
}
void define_tools()
{
setDefaults();
struct tool *tool = add_tool("edgeColor", "Set color of edges");
add_argument(tool,
"color", TYPE_STR, "Color name (red, green, blue, white,black,gray)");
tool = add_tool("backgroundColor", "Set color of background");
add_argument(tool,
"color", TYPE_STR, "Color name (red, green, blue, white,black,gray)");
tool = add_tool("animationOn", "start or stop animation");
add_argument(tool,
"on", TYPE_BOOL, "Animation status");
tool = add_tool("geometryType", "Type of geometry");
add_argument(tool,
"geometry", TYPE_STR, "Type of geometry (disk or plane)");
tool = add_tool("symmetryType", "Set the symmetry type");
add_argument(tool,
"symmetry", TYPE_INT, "Type of symmetry (0,1,2)");
tool = add_tool("reset", "Reset defaukt settings");
//struct tool *addTool = add_tool("add", "Add two numbers");
// Add arguments in reverse order (linked list)
//add_argument(addTool, "b", TYPE_FLOAT, "Second number");
//add_argument(addTool, "a", TYPE_FLOAT, "First number");
}