-
Notifications
You must be signed in to change notification settings - Fork 0
/
ref.cpp
568 lines (507 loc) · 15.2 KB
/
ref.cpp
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
#include <SFML/Graphics.hpp>
#include "../SFML_Functions/SFML_Functions.hpp"
#include <algorithm>
#include <iostream>
#include <fstream>
float (*cross)(const Point&, const Point&) = SFUTIL::cross<float>;
float (*dot)(Point, Point) = SFUTIL::dot<float>;
bool (*getIntPt)(const Point&, const Point&, const Point&, const Point&, Point*) = SFUTIL::lineSegsIntersect<float>;
typedef unsigned int NODEID;
typedef Point Vec2;
Vec2 (*normalize)(const Vec2&) = SFUTIL::getUnitVec<float>;
uint next(uint i, uint sz)
{
return i == sz - 1 ? 0 : i + 1;
}
uint last(uint i, uint sz)
{
return i == 0 ? sz - 1 : i - 1;
}
float pseudoAngle(Point &P)
{
float p = P.x / (fabsf(P.x) + fabsf(P.y));
return (P.y > 0.f ? 3.f - p : 1.f + p);//0-4
}
struct Node
{
Node(float x, float y, float _r) : pos(x,y), r(_r) {}
Point pos;
float r;
//NODEID id;
std::vector<NODEID> edges;//connected to... (not OWNED)
};//ie a circle
std::vector<Node> nodes;
//sort ccw from -> 0
struct AngleSort
{
AngleSort(Point &p):base(p){}
Point &base;
bool operator()(NODEID a, NODEID b)
{
Point A = nodes[a].pos - base;
Point B = nodes[b].pos - base;
return pseudoAngle(A) < pseudoAngle(B);
}
};
//undirected graph
void link(NODEID A, NODEID B)
{
nodes[A].edges.push_back(B);
nodes[B].edges.push_back(A);
}
NODEID nextNode(NODEID curNodeId, NODEID lastNodeId)
{
Node &curNode = nodes[curNodeId];
for(uint i = 0; i < curNode.edges.size(); i++)
{
if(curNode.edges[i] == lastNodeId)
{
return curNode.edges[next(i,curNode.edges.size())];
}
}
throw;
return -1;
}
//outer traverse...
//assumes starts at endnode...
void traverseGraph(NODEID start)
{
//go ccw... so first to next...
NODEID curId = start;
std::cout<<"At node "<<curId<<std::endl;
if(!nodes[start].edges.size()) return;
curId = nodes[start].edges[0];
NODEID lastId = start;
std::cout<<"At node "<<curId<<std::endl;
//while(curId != start)
do
{
NODEID nextId = nextNode(curId,lastId);
lastId = curId;
curId = nextId;
std::cout<<"At node "<<curId<<std::endl;
} while (curId != start);
}
sf::VertexArray lines;
std::vector<Point> points;
//I should step through this code to see where it messes up...
void drawOutline()
{
lines.setPrimitiveType(sf::LineStrip);
//start at 0 and go around...
NODEID curId = 0;
for(curId = 0; curId != nodes.size(); curId++)
if(nodes[curId].edges.size() == 1) break;
// NODEID curId = 0;
// NODEID lastId = 1;
NODEID lastId = nodes[curId].edges[0];
while(true)
{
//start with left
Node &curNode = nodes[curId];
Node &lastNode = nodes[lastId];
Vec2 Vl = lastNode.pos - curNode.pos;
Vec2 vl_n(-Vl.y, Vl.x);
vl_n = normalize(vl_n);
NODEID nextId = nextNode(curId,lastId);
Node &nextNode = nodes[nextId];
Vec2 Vn = curNode.pos - nextNode.pos;
Vec2 vn_n(-Vn.y, Vn.x);//.normalize()
vn_n = normalize(vn_n);
bool toLeft = cross(Vl,Vn) > 0;
if(curNode.edges.size() == 1)//end node// draw endpts...
{
points.push_back(curNode.pos + vl_n * curNode.r);
points.push_back(curNode.pos + vn_n * curNode.r);
//switch
lastId = curId;
curId = nextId;
if(curId == 0) break;
continue;
}
if(toLeft)//not a miter.. add 2 points
{
points.push_back(curNode.pos + vl_n * curNode.r);
points.push_back(curNode.pos + vn_n * curNode.r);
}
else
{
//a miter, just add the 1 point
Point pl1 = curNode.pos + vl_n * curNode.r;
Point pl2 = lastNode.pos + vl_n * lastNode.r;
Point pn1 = curNode.pos + vn_n * curNode.r;
Point pn2 = nextNode.pos + vn_n * nextNode.r;
Point intpt;
getIntPt(pl1,pl2,pn1,pn2,&intpt);
points.push_back(intpt);
}
lastId = curId;
curId = nextId;
if(curId == 0) break;
}
}
/*
void traverseGraph(NODEID start)
{
//go ccw... so first to next...
NODEID curId = start;
std::cout<<"At node "<<curId<<std::endl;
if(!nodes[start].edges.size()) return;
curId = nodes[start].edges[0];
NODEID lastId = start;
std::cout<<"At node "<<curId<<std::endl;
//while(curId != start)
do
{
NODEID nextId = nextNode(curId,lastId);
lastId = curId;
curId = nextId;
std::cout<<"At node "<<curId<<std::endl;
} while (curId != start);
}*/
void drawOutlinePoints()
{
lines.setPrimitiveType(sf::Lines);
//for each node...
//for each edge from that node...
//there are 2 points
for(uint i = 0; i < nodes.size(); i++)
{
Node &n = nodes[i];
//for each edge...
for(uint k = 0; k < n.edges.size(); k++)
{
//if(k!=1 || n.edges.size()!=3 ) continue;
Node &e = nodes[n.edges[k]];
Node &e_next = nodes[n.edges[next(k,n.edges.size())]];
Node &e_last = nodes[n.edges[last(k,n.edges.size())]];
Vec2 v0 = e.pos - n.pos;
Vec2 vp = e_last.pos - n.pos;
Vec2 va = e_next.pos - n.pos;
Vec2 v0n(-v0.y, v0.x);
v0n = normalize(v0n);
if(n.edges.size() == 1)//no miter just end point...
{
points.push_back(n.pos - v0n * n.r);
points.push_back(n.pos + v0n * n.r);
continue;
}
Vec2 vpn(-vp.y, vp.x);
vpn = normalize(vpn);
Vec2 van(-va.y, va.x);
van = normalize(van);
//is it a miter point or a tan point?
bool lastToLeft = cross(vp,v0) > 0.f;
bool nextToLeft = cross(v0,va) > 0.f;
if(!lastToLeft)//just regular...
{
//lines.append({n.pos,sf::Color::Blue});
//lines.append({e_last.pos,sf::Color::Blue});
//lines.append({n.pos,sf::Color::Black});
//lines.append({e.pos,sf::Color::Black});
lines.append({n.pos,sf::Color::Black});
lines.append({n.pos - v0n * n.r,sf::Color::Black});
points.push_back(n.pos - v0n * n.r);
}
else//miter
{//BLOODY>>>> THINKING REQUIRED :(
lines.append({n.pos,sf::Color::Black});
lines.append({e.pos,sf::Color::Black});
lines.append({n.pos,sf::Color::Black});
lines.append({e_last.pos,sf::Color::Black});
/*
Vec2 vl = n.pos - e_last.pos;
vl = normalize(vl);
Vec2 vln(-vl.y,vl.x);
Vec2 tangent = normalize(v0n + vpn);
Vec2 miter(-tangent.y, tangent.x);
float l = (n.r + e_last.r) / dot(miter,v0n);//normal
miter *= l/2;*/
Point pe1 = n.pos - v0n * n.r;
Point pe2 = e.pos - v0n * e.r;
Point pp1 = n.pos + vpn * n.r;
Point pp2 = e_last.pos + vpn * e_last.r;
Point intpt;
getIntPt(pe1,pe2,pp1,pp2,&intpt);
lines.append({n.pos,sf::Color::Blue});
lines.append({intpt,sf::Color::Blue});
points.push_back(intpt);
}
if(!nextToLeft)//just regular...
{
//lines.append({n.pos,sf::Color::Green});
//lines.append({e_next.pos,sf::Color::Green});
//lines.append({n.pos,sf::Color::Yellow});
//lines.append({e.pos,sf::Color::Yellow});
lines.append({n.pos,sf::Color::Yellow});
lines.append({n.pos + v0n * n.r,sf::Color::Yellow});
points.push_back(n.pos + v0n * n.r);
}
else//miter
{
Vec2 tangent = normalize(van + v0n);
Vec2 miter(-tangent.y, tangent.x);
float l = (n.r + e_next.r) / dot(miter,van);
miter *= l/2;
//points.push_back(n.pos - miter);
}
}
}
}
//////////////////visualizations...////////////////////
std::vector<sf::CircleShape> nodeCircles;
void drawNodes()
{
sf::CircleShape C;
C.setFillColor(sf::Color(255,0,0,64));
for(auto &n : nodes)
{
C.setRadius(n.r);
C.setOrigin(n.r,n.r);
C.setPosition(n.pos);
nodeCircles.push_back(C);
}
}
std::vector<sf::CircleShape> pointCircles;
void drawPoints()
{
sf::CircleShape C;
C.setFillColor(sf::Color(0,255,0,64));
C.setOrigin(5,5);
C.setRadius(5);
for(auto &p : points)
{
C.setPosition(p);
pointCircles.push_back(C);
}
}
///////////////////////
std::vector<sf::CircleShape> circles;
void genCircles(sf::FloatRect &&bounds)
{
srand(time(NULL));
sf::CircleShape circle;
circle.setFillColor(sf::Color(255,0,0,128));
//try and create a ground? left and right
/*
{
float x = -10;
float y = 10;//rand() % (int)bounds.height + bounds.top;
float r = rand() % 15 + 5;
circle.setOrigin(r,r);
circle.setRadius(r);
circle.setPosition(x,y);
circles.push_back(circle);
x = 110;
y = 10;//rand() % (int)bounds.height + bounds.top;
r = rand() % 15 + 5;
circle.setOrigin(r,r);
circle.setRadius(r);
circle.setPosition(x,y);
circles.push_back(circle);
}*/
for(int i = 0; i < 25; i++)
{
float x = rand() % (int)bounds.width + bounds.left;
float y = rand() % (int)bounds.height + bounds.top;
float r = rand() % 15 + 5;
circle.setOrigin(r,r);
circle.setRadius(r);
circle.setPosition(x,y);
bool collided = false;
for(auto &c : circles)
{
float r2 = c.getRadius() + r;
float dx = c.getPosition().x - x;
float dy = c.getPosition().y - y;
if(r2 * r2 > (dx * dx + dy * dy))
{
collided = true;
break;
}
}
if(!collided)
circles.push_back(circle);
}
}
struct Edge
{
unsigned int a, b;
float l2;
};
std::vector<Edge> edges;
std::vector<Edge> closeEdges;
sf::VertexArray edgeLines;
void genLines()
{
for(uint i = 0; i < circles.size()-1; i++)
{
for(uint j = i+1; j < circles.size(); j++)
{
//create
sf::Vector2f d = circles[i].getPosition() - circles[j].getPosition();
float l2 = d.x * d.x + d.y * d.y;
//append
edges.push_back({i,j,l2});
}
}
//sort...
auto edgeSort = [](const Edge &E1, const Edge & E2) -> bool
{
return E1.l2 < E2.l2;
};
std::sort(edges.begin(),edges.end(),edgeSort);
//try Kruskal's algorithm here...
//no understand it.. copy pasta
std::vector<uint> id;
id.resize(edges.size());
for(uint i = 0; i<id.size(); ++i) id[i] = i;
auto root = [&](uint x) -> uint
{
while (id[x] != x)
{
id[x] = id[id[x]];
x = id[x];
}
return x;
};
auto union1 = [&](uint x, uint y)
{
uint p = root(x);
uint q = root(y);
id[p] = id[q];
};
std::vector<bool> visited;
std::vector<bool> addedEdge(edges.size(),false);
uint numVisited = 0;
visited.resize(circles.size(), false);
for(uint i = 0; i < edges.size(); ++i)
{
if(root(edges[i].a) != root(edges[i].b))
{
union1(edges[i].a,edges[i].b);
if(!visited[edges[i].a])
{
visited[edges[i].a] = true;
numVisited++;
}
if(!visited[edges[i].b])
{
visited[edges[i].b] = true;
numVisited++;
}
closeEdges.push_back(edges[i]);
addedEdge[i]=true;
if(numVisited == edges.size())
break;
}
}
//need to use (delauney type) edge list... then can just choose random edges from there...
for(uint i = 0; i<addedEdge.size(); ++i)
{
if(addedEdge[i] == false) {closeEdges.push_back(edges[i]);
break;}
}
std::cout<<"Ids are now:";
for(auto &i : id) std::cout<<" "<<i;
std::cout<<std::endl;
//show
edgeLines.setPrimitiveType(sf::Lines);
for(auto &e : closeEdges)
{
edgeLines.append({circles[e.a].getPosition(),sf::Color::White});
edgeLines.append({circles[e.b].getPosition(),sf::Color::White});
}
}
int main()
{
//create it from our graph....
/*
sf::RenderTexture target;
target.create(640,640);//width height
sf::View view;
view.setSize(120,120);
view.setCenter(50,50);
target.setView(view);*/
genCircles({0,0,100,100});
genLines();
//convert over
for(auto &c : circles)
nodes.emplace_back(c.getPosition().x,c.getPosition().y,c.getRadius());
for(auto &e : closeEdges)
link(e.a,e.b);
/*
nodes.emplace_back(160,190,20);//0
nodes.emplace_back(180,140,15);//1
nodes.emplace_back(120, 60,40);//2
nodes.emplace_back(190, 60,10);//3
nodes.emplace_back( 60,130,20);//4
nodes.emplace_back( 40,100,10);//5
link(0,1);
link(1,2);
link(2,3);
link(2,4);
link(4,5);*/
//link(1,3);
for(auto &n : nodes)
{
if(n.edges.size()>2)
std::sort(n.edges.begin(),n.edges.end(),AngleSort(n.pos));
}
//std::sort(edges.begin(),edges.end(),AS);
//print graph...
for(uint i = 0; i<nodes.size(); i++)
{
std::cout<<"Node: "<<i<<" e: ";
for(uint k = 0; k<nodes[i].edges.size(); k++)
std::cout<<nodes[i].edges[k]<<" ";
std::cout<<std::endl;
}
drawNodes();
drawOutline();
// drawOutlinePoints();
drawPoints();
traverseGraph(4);
sf::VertexArray edgeLines;
edgeLines.setPrimitiveType(sf::Lines);
for(auto &n : nodes)
for(auto &e : n.edges)
{
edgeLines.append({n.pos, sf::Color::Green});
edgeLines.append({nodes[e].pos, sf::Color::Green});
}
for(auto &p : points)
lines.append({p,sf::Color::Black});
lines.append(lines[0]);
sf::RenderWindow window(sf::VideoMode(640,640),"Graph");
sf::Event event;
sf::View view;
view.setCenter(50,50);
view.setSize(120,120);
window.setView(view);
CameraController camera(window,view);
//view.rotate(33);
while (window.isOpen())
{
window.clear(sf::Color(64,64,64));
window.draw(lines);
window.draw(edgeLines);
for(auto &c : pointCircles) window.draw(c);
for(auto &c : nodeCircles) window.draw(c);
window.display();
window.waitEvent(event);
switch (event.type)
{
case sf::Event::Closed:
case sf::Event::KeyPressed:
window.close();
break;
default:
break;
}
camera.handleEvent(event);
}
std::ofstream ofile("points");
for(auto &p : points) ofile << "{" << p.x << "," << p.y << "}," << std::endl;
return 0;
}