This repository was archived by the owner on Oct 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSimulator.cs
883 lines (816 loc) · 31.6 KB
/
Simulator.cs
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
/*
* Simulator.cs
* RVO2 Library C#
*
* SPDX-FileCopyrightText: 2008 University of North Carolina at Chapel Hill
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Please send all bug reports to <geom@cs.unc.edu>.
*
* The authors may be contacted via:
*
* Jur van den Berg, Stephen J. Guy, Jamie Snape, Ming C. Lin, Dinesh Manocha
* Dept. of Computer Science
* 201 S. Columbia St.
* Frederick P. Brooks, Jr. Computer Science Bldg.
* Chapel Hill, N.C. 27599-3175
* United States of America
*
* <http://gamma.cs.unc.edu/RVO2/>
*/
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using UnityEngine;
using UnityEngine.Pool;
namespace RVO
{
/**
* <summary>Defines the simulation.</summary>
*/
public class Simulator
{
private static int mAgentIdCounter = 0;
internal ConcurrentDictionary<int, Agent> agents_;
private ConcurrentStack<int> removeList_;
internal IList<Obstacle> obstacles_;
internal KdTree kdTree_;
internal float timeStep_;
ManualResetEvent[] buildTreeDoneEvent;
private int m_MaxThreadCount = 8;
private int m_ParallelAgentCount = 500;//ÿ¸öÏ̸߳üÐÂAgent¸öÊý
CacheArray<Agent> m_AgentsCache;
private static readonly Simulator instance_ = new();
private Agent defaultAgent_;
private float globalTime_;
private ManualResetEvent[] doneEvents;
public static Simulator Instance
{
get
{
return instance_;
}
}
/**
* <summary>Adds a new agent with default properties to the simulation.
* </summary>
*
* <returns>The number of the agent, or -1 when the agent defaults have
* not been set.</returns>
*
* <param name="position">The two-dimensional starting position of this
* agent.</param>
*/
public int addAgent(Vector2 position)
{
if (defaultAgent_ == null)
{
return -1;
}
var id = addAgent(position, defaultAgent_.neighborDist_, defaultAgent_.maxNeighbors_, defaultAgent_.timeHorizon_, defaultAgent_.timeHorizonObst_,
defaultAgent_.radius_, defaultAgent_.maxSpeed_, defaultAgent_.velocity_);
return id;
}
private int newAgentId()
{
return ++mAgentIdCounter;
}
/**
* <summary>Adds a new agent to the simulation.</summary>
*
* <returns>The number of the agent.</returns>
*
* <param name="position">The two-dimensional starting position of this
* agent.</param>
* <param name="neighborDist">The maximum distance (center point to
* center point) to other agents this agent takes into account in the
* navigation. The larger this number, the longer the running time of
* the simulation. If the number is too low, the simulation will not be
* safe. Must be non-negative.</param>
* <param name="maxNeighbors">The maximum number of other agents this
* agent takes into account in the navigation. The larger this number,
* the longer the running time of the simulation. If the number is too
* low, the simulation will not be safe.</param>
* <param name="timeHorizon">The minimal amount of time for which this
* agent's velocities that are computed by the simulation are safe with
* respect to other agents. The larger this number, the sooner this
* agent will respond to the presence of other agents, but the less
* freedom this agent has in choosing its velocities. Must be positive.
* </param>
* <param name="timeHorizonObst">The minimal amount of time for which
* this agent's velocities that are computed by the simulation are safe
* with respect to obstacles. The larger this number, the sooner this
* agent will respond to the presence of obstacles, but the less freedom
* this agent has in choosing its velocities. Must be positive.</param>
* <param name="radius">The radius of this agent. Must be non-negative.
* </param>
* <param name="maxSpeed">The maximum speed of this agent. Must be
* non-negative.</param>
* <param name="velocity">The initial two-dimensional linear velocity of
* this agent.</param>
*/
public int addAgent(Vector2 position, float neighborDist, int maxNeighbors, float timeHorizon, float timeHorizonObst, float radius, float maxSpeed, Vector2 velocity)
{
Agent agent = new();
agent.id_ = newAgentId();
agent.removed_ = false;
agent.maxNeighbors_ = maxNeighbors;
agent.maxSpeed_ = maxSpeed;
agent.neighborDist_ = neighborDist;
agent.position_ = position;
agent.radius_ = radius;
agent.timeHorizon_ = timeHorizon;
agent.timeHorizonObst_ = timeHorizonObst;
agent.velocity_ = velocity;
agents_.TryAdd(agent.id_, agent);
return agent.id_;
}
/**
* <summary>Adds a new obstacle to the simulation.</summary>
*
* <returns>The number of the first vertex of the obstacle, or -1 when
* the number of vertices is less than two.</returns>
*
* <param name="vertices">List of the vertices of the polygonal obstacle
* in counterclockwise order.</param>
*
* <remarks>To add a "negative" obstacle, e.g. a bounding polygon around
* the environment, the vertices should be listed in clockwise order.
* </remarks>
*/
public int addObstacle(IList<Vector2> vertices)
{
if (vertices.Count < 2)
{
return -1;
}
int obstacleNo = obstacles_.Count;
for (int i = 0; i < vertices.Count; ++i)
{
Obstacle obstacle = new();
obstacle.point_ = vertices[i];
if (i != 0)
{
obstacle.previous_ = obstacles_[obstacles_.Count - 1];
obstacle.previous_.next_ = obstacle;
}
if (i == vertices.Count - 1)
{
obstacle.next_ = obstacles_[obstacleNo];
obstacle.next_.previous_ = obstacle;
}
obstacle.direction_ = (vertices[(i == vertices.Count - 1 ? 0 : i + 1)] - vertices[i]).normalized;
if (vertices.Count == 2)
{
obstacle.convex_ = true;
}
else
{
obstacle.convex_ = (RVOMath.leftOf(vertices[(i == 0 ? vertices.Count - 1 : i - 1)], vertices[i], vertices[(i == vertices.Count - 1 ? 0 : i + 1)]) >= 0.0f);
}
obstacle.id_ = obstacles_.Count;
obstacles_.Add(obstacle);
}
return obstacleNo;
}
/**
* <summary>Clears the simulation.</summary>
*/
public void Clear()
{
mAgentIdCounter = 0;
m_AgentsCache.Release();
agents_.Clear();
removeList_.Clear();
defaultAgent_ = null;
kdTree_ = null;
obstacles_.Clear();
globalTime_ = 0.0f;
timeStep_ = 0.1f;
doneEvents = null;
buildTreeDoneEvent = null;
}
/**
* <summary>Performs a simulation step and updates the two-dimensional
* position and two-dimensional velocity of each agent.</summary>
*
* <returns>The global time after the simulation step.</returns>
*/
public float doStep()
{
int agentNum;
if ((agentNum = getNumAgents()) > 0)
{
if (agentNum != m_AgentsCache.Count)
{
m_AgentsCache.Resize(agentNum);
int index = 0;
foreach (var item in agents_.Values)
{
m_AgentsCache[index++] = item;
}
}
buildTreeDoneEvent[0].Reset();
U3D.Threading.Tasks.Task.Run(() =>
{
kdTree_.buildAgentTree(m_AgentsCache);
buildTreeDoneEvent[0].Set();
});
WaitHandle.WaitAll(buildTreeDoneEvent);
int taskCount = Mathf.Clamp(Mathf.CeilToInt(agentNum / (float)m_ParallelAgentCount), 1, m_MaxThreadCount);
int preTaskNum = Mathf.CeilToInt(agentNum / (float)taskCount);
for (int i = 0; i < taskCount; i++)
{
doneEvents[i].Reset();
int startIdx = i * preTaskNum;
int endIdx = Mathf.Min((i + 1) * preTaskNum, agentNum);
int currentIdx = i;
U3D.Threading.Tasks.Task.Run(() =>
{
for (int idx = startIdx; idx < endIdx; idx++)
{
var agent = m_AgentsCache[idx];
if (agent.removed_)
{
continue;
}
agent.computeNeighbors();
agent.computeNewVelocity();
agent.update();
}
doneEvents[currentIdx].Set();
});
}
for (int i = taskCount; i < doneEvents.Length; i++)
{
doneEvents[i].Set();
}
WaitHandle.WaitAll(doneEvents);
}
while (removeList_.TryPop(out int key))
{
agents_.TryRemove(key, out _);
}
globalTime_ += timeStep_;
return globalTime_;
}
/**
* <summary>Returns the specified agent neighbor of the specified agent.
* </summary>
*
* <returns>The number of the neighboring agent.</returns>
*
* <param name="agentNo">The number of the agent whose agent neighbor is
* to be retrieved.</param>
* <param name="neighborNo">The number of the agent neighbor to be
* retrieved.</param>
*/
public int getAgentAgentNeighbor(int agentNo, int neighborNo)
{
return agents_[agentNo].agentNeighbors_[neighborNo].Value.id_;
}
/**
* <summary>Returns the maximum neighbor count of a specified agent.
* </summary>
*
* <returns>The present maximum neighbor count of the agent.</returns>
*
* <param name="agentNo">The number of the agent whose maximum neighbor
* count is to be retrieved.</param>
*/
public int getAgentMaxNeighbors(int agentNo)
{
return agents_[agentNo].maxNeighbors_;
}
/**
* <summary>Returns the maximum speed of a specified agent.</summary>
*
* <returns>The present maximum speed of the agent.</returns>
*
* <param name="agentNo">The number of the agent whose maximum speed is
* to be retrieved.</param>
*/
public float getAgentMaxSpeed(int agentNo)
{
return agents_[agentNo].maxSpeed_;
}
/**
* <summary>Returns the maximum neighbor distance of a specified agent.
* </summary>
*
* <returns>The present maximum neighbor distance of the agent.
* </returns>
*
* <param name="agentNo">The number of the agent whose maximum neighbor
* distance is to be retrieved.</param>
*/
public float getAgentNeighborDist(int agentNo)
{
return agents_[agentNo].neighborDist_;
}
/**
* <summary>Returns the count of agent neighbors taken into account to
* compute the current velocity for the specified agent.</summary>
*
* <returns>The count of agent neighbors taken into account to compute
* the current velocity for the specified agent.</returns>
*
* <param name="agentNo">The number of the agent whose count of agent
* neighbors is to be retrieved.</param>
*/
public int getAgentNumAgentNeighbors(int agentNo)
{
return agents_[agentNo].agentNeighbors_.Count;
}
/**
* <summary>Returns the count of obstacle neighbors taken into account
* to compute the current velocity for the specified agent.</summary>
*
* <returns>The count of obstacle neighbors taken into account to
* compute the current velocity for the specified agent.</returns>
*
* <param name="agentNo">The number of the agent whose count of obstacle
* neighbors is to be retrieved.</param>
*/
public int getAgentNumObstacleNeighbors(int agentNo)
{
return agents_[agentNo].obstacleNeighbors_.Count;
}
/**
* <summary>Returns the specified obstacle neighbor of the specified
* agent.</summary>
*
* <returns>The number of the first vertex of the neighboring obstacle
* edge.</returns>
*
* <param name="agentNo">The number of the agent whose obstacle neighbor
* is to be retrieved.</param>
* <param name="neighborNo">The number of the obstacle neighbor to be
* retrieved.</param>
*/
public int getAgentObstacleNeighbor(int agentNo, int neighborNo)
{
return agents_[agentNo].obstacleNeighbors_[neighborNo].Value.id_;
}
/**
* <summary>Returns the ORCA constraints of the specified agent.
* </summary>
*
* <returns>A list of lines representing the ORCA constraints.</returns>
*
* <param name="agentNo">The number of the agent whose ORCA constraints
* are to be retrieved.</param>
*
* <remarks>The halfplane to the left of each line is the region of
* permissible velocities with respect to that ORCA constraint.
* </remarks>
*/
public IList<Line> getAgentOrcaLines(int agentNo)
{
return agents_[agentNo].orcaLines_;
}
/**
* <summary>Returns the two-dimensional position of a specified agent.
* </summary>
*
* <returns>The present two-dimensional position of the (center of the)
* agent.</returns>
*
* <param name="agentNo">The number of the agent whose two-dimensional
* position is to be retrieved.</param>
*/
public Vector2 getAgentPosition(int agentNo)
{
return agents_[agentNo].position_;
}
/**
* <summary>Returns the two-dimensional preferred velocity of a
* specified agent.</summary>
*
* <returns>The present two-dimensional preferred velocity of the agent.
* </returns>
*
* <param name="agentNo">The number of the agent whose two-dimensional
* preferred velocity is to be retrieved.</param>
*/
public Vector2 getAgentPrefVelocity(int agentNo)
{
return agents_[agentNo].prefVelocity_;
}
/**
* <summary>Returns the radius of a specified agent.</summary>
*
* <returns>The present radius of the agent.</returns>
*
* <param name="agentNo">The number of the agent whose radius is to be
* retrieved.</param>
*/
public float getAgentRadius(int agentNo)
{
return agents_[agentNo].radius_;
}
/**
* <summary>Returns the time horizon of a specified agent.</summary>
*
* <returns>The present time horizon of the agent.</returns>
*
* <param name="agentNo">The number of the agent whose time horizon is
* to be retrieved.</param>
*/
public float getAgentTimeHorizon(int agentNo)
{
return agents_[agentNo].timeHorizon_;
}
/**
* <summary>Returns the time horizon with respect to obstacles of a
* specified agent.</summary>
*
* <returns>The present time horizon with respect to obstacles of the
* agent.</returns>
*
* <param name="agentNo">The number of the agent whose time horizon with
* respect to obstacles is to be retrieved.</param>
*/
public float getAgentTimeHorizonObst(int agentNo)
{
return agents_[agentNo].timeHorizonObst_;
}
/**
* <summary>Returns the two-dimensional linear velocity of a specified
* agent.</summary>
*
* <returns>The present two-dimensional linear velocity of the agent.
* </returns>
*
* <param name="agentNo">The number of the agent whose two-dimensional
* linear velocity is to be retrieved.</param>
*/
public Vector2 getAgentVelocity(int agentNo)
{
return agents_[agentNo].velocity_;
}
/**
* <summary>Returns the global time of the simulation.</summary>
*
* <returns>The present global time of the simulation (zero initially).
* </returns>
*/
public float getGlobalTime()
{
return globalTime_;
}
/**
* <summary>Returns the count of agents in the simulation.</summary>
*
* <returns>The count of agents in the simulation.</returns>
*/
public int getNumAgents()
{
return agents_.Count;
}
/**
* <summary>Returns the count of obstacle vertices in the simulation.
* </summary>
*
* <returns>The count of obstacle vertices in the simulation.</returns>
*/
public int getNumObstacleVertices()
{
return obstacles_.Count;
}
/**
* <summary>Returns the count of workers.</summary>
*
* <returns>The count of workers.</returns>
*/
//public int GetNumWorkers()
//{
// return numWorkers_;
//}
/**
* <summary>Returns the two-dimensional position of a specified obstacle
* vertex.</summary>
*
* <returns>The two-dimensional position of the specified obstacle
* vertex.</returns>
*
* <param name="vertexNo">The number of the obstacle vertex to be
* retrieved.</param>
*/
public Vector2 getObstacleVertex(int vertexNo)
{
return obstacles_[vertexNo].point_;
}
/**
* <summary>Returns the number of the obstacle vertex succeeding the
* specified obstacle vertex in its polygon.</summary>
*
* <returns>The number of the obstacle vertex succeeding the specified
* obstacle vertex in its polygon.</returns>
*
* <param name="vertexNo">The number of the obstacle vertex whose
* successor is to be retrieved.</param>
*/
public int getNextObstacleVertexNo(int vertexNo)
{
return obstacles_[vertexNo].next_.id_;
}
/**
* <summary>Returns the number of the obstacle vertex preceding the
* specified obstacle vertex in its polygon.</summary>
*
* <returns>The number of the obstacle vertex preceding the specified
* obstacle vertex in its polygon.</returns>
*
* <param name="vertexNo">The number of the obstacle vertex whose
* predecessor is to be retrieved.</param>
*/
public int getPrevObstacleVertexNo(int vertexNo)
{
return obstacles_[vertexNo].previous_.id_;
}
/**
* <summary>Returns the time step of the simulation.</summary>
*
* <returns>The present time step of the simulation.</returns>
*/
public float getTimeStep()
{
return timeStep_;
}
/**
* <summary>Processes the obstacles that have been added so that they
* are accounted for in the simulation.</summary>
*
* <remarks>Obstacles added to the simulation after this function has
* been called are not accounted for in the simulation.</remarks>
*/
public void processObstacles()
{
kdTree_.buildObstacleTree();
}
/**
* <summary>Performs a visibility query between the two specified points
* with respect to the obstacles.</summary>
*
* <returns>A boolean specifying whether the two points are mutually
* visible. Returns true when the obstacles have not been processed.
* </returns>
*
* <param name="point1">The first point of the query.</param>
* <param name="point2">The second point of the query.</param>
* <param name="radius">The minimal distance between the line connecting
* the two points and the obstacles in order for the points to be
* mutually visible (optional). Must be non-negative.</param>
*/
public bool queryVisibility(Vector2 point1, Vector2 point2, float radius)
{
return kdTree_.queryVisibility(point1, point2, radius);
}
/**
* <summary>Sets the default properties for any new agent that is added.
* </summary>
*
* <param name="neighborDist">The default maximum distance (center point
* to center point) to other agents a new agent takes into account in
* the navigation. The larger this number, the longer he running time of
* the simulation. If the number is too low, the simulation will not be
* safe. Must be non-negative.</param>
* <param name="maxNeighbors">The default maximum number of other agents
* a new agent takes into account in the navigation. The larger this
* number, the longer the running time of the simulation. If the number
* is too low, the simulation will not be safe.</param>
* <param name="timeHorizon">The default minimal amount of time for
* which a new agent's velocities that are computed by the simulation
* are safe with respect to other agents. The larger this number, the
* sooner an agent will respond to the presence of other agents, but the
* less freedom the agent has in choosing its velocities. Must be
* positive.</param>
* <param name="timeHorizonObst">The default minimal amount of time for
* which a new agent's velocities that are computed by the simulation
* are safe with respect to obstacles. The larger this number, the
* sooner an agent will respond to the presence of obstacles, but the
* less freedom the agent has in choosing its velocities. Must be
* positive.</param>
* <param name="radius">The default radius of a new agent. Must be
* non-negative.</param>
* <param name="maxSpeed">The default maximum speed of a new agent. Must
* be non-negative.</param>
* <param name="velocity">The default initial two-dimensional linear
* velocity of a new agent.</param>
*/
public void setAgentDefaults(float neighborDist, int maxNeighbors, float timeHorizon, float timeHorizonObst, float radius, float maxSpeed, Vector2 velocity)
{
if (defaultAgent_ == null)
{
defaultAgent_ = new Agent();
}
defaultAgent_.maxNeighbors_ = maxNeighbors;
defaultAgent_.maxSpeed_ = maxSpeed;
defaultAgent_.neighborDist_ = neighborDist;
defaultAgent_.radius_ = radius;
defaultAgent_.timeHorizon_ = timeHorizon;
defaultAgent_.timeHorizonObst_ = timeHorizonObst;
defaultAgent_.velocity_ = velocity;
}
/**
* <summary>Sets the maximum neighbor count of a specified agent.
* </summary>
*
* <param name="agentNo">The number of the agent whose maximum neighbor
* count is to be modified.</param>
* <param name="maxNeighbors">The replacement maximum neighbor count.
* </param>
*/
public void setAgentMaxNeighbors(int agentNo, int maxNeighbors)
{
agents_[agentNo].maxNeighbors_ = maxNeighbors;
}
/**
* <summary>Sets the maximum speed of a specified agent.</summary>
*
* <param name="agentNo">The number of the agent whose maximum speed is
* to be modified.</param>
* <param name="maxSpeed">The replacement maximum speed. Must be
* non-negative.</param>
*/
public void setAgentMaxSpeed(int agentNo, float maxSpeed)
{
agents_[agentNo].maxSpeed_ = maxSpeed;
}
/**
* <summary>Sets the maximum neighbor distance of a specified agent.
* </summary>
*
* <param name="agentNo">The number of the agent whose maximum neighbor
* distance is to be modified.</param>
* <param name="neighborDist">The replacement maximum neighbor distance.
* Must be non-negative.</param>
*/
public void setAgentNeighborDist(int agentNo, float neighborDist)
{
agents_[agentNo].neighborDist_ = neighborDist;
}
/**
* <summary>Sets the two-dimensional position of a specified agent.
* </summary>
*
* <param name="agentNo">The number of the agent whose two-dimensional
* position is to be modified.</param>
* <param name="position">The replacement of the two-dimensional
* position.</param>
*/
public void setAgentPosition(int agentNo, Vector2 position)
{
var agent = agents_[agentNo];
agent.position_ = position;
}
/**
* <summary>Sets the two-dimensional preferred velocity of a specified
* agent.</summary>
*
* <param name="agentNo">The number of the agent whose two-dimensional
* preferred velocity is to be modified.</param>
* <param name="prefVelocity">The replacement of the two-dimensional
* preferred velocity.</param>
*/
public void setAgentPrefVelocity(int agentNo, Vector2 prefVelocity)
{
var agent = agents_[agentNo];
agent.prefVelocity_ = prefVelocity;
}
/**
* <summary>Sets the radius of a specified agent.</summary>
*
* <param name="agentNo">The number of the agent whose radius is to be
* modified.</param>
* <param name="radius">The replacement radius. Must be non-negative.
* </param>
*/
public void setAgentRadius(int agentNo, float radius)
{
agents_[agentNo].radius_ = radius;
}
/**
* <summary>Sets the time horizon of a specified agent with respect to
* other agents.</summary>
*
* <param name="agentNo">The number of the agent whose time horizon is
* to be modified.</param>
* <param name="timeHorizon">The replacement time horizon with respect
* to other agents. Must be positive.</param>
*/
public void setAgentTimeHorizon(int agentNo, float timeHorizon)
{
agents_[agentNo].timeHorizon_ = timeHorizon;
}
/**
* <summary>Sets the time horizon of a specified agent with respect to
* obstacles.</summary>
*
* <param name="agentNo">The number of the agent whose time horizon with
* respect to obstacles is to be modified.</param>
* <param name="timeHorizonObst">The replacement time horizon with
* respect to obstacles. Must be positive.</param>
*/
public void setAgentTimeHorizonObst(int agentNo, float timeHorizonObst)
{
agents_[agentNo].timeHorizonObst_ = timeHorizonObst;
}
/**
* <summary>Sets the two-dimensional linear velocity of a specified
* agent.</summary>
*
* <param name="agentNo">The number of the agent whose two-dimensional
* linear velocity is to be modified.</param>
* <param name="velocity">The replacement two-dimensional linear
* velocity.</param>
*/
public void setAgentVelocity(int agentNo, Vector2 velocity)
{
var agent = agents_[agentNo];
agent.velocity_ = velocity;
}
/**
* <summary>Sets the global time of the simulation.</summary>
*
* <param name="globalTime">The global time of the simulation.</param>
*/
public void setGlobalTime(float globalTime)
{
globalTime_ = globalTime;
}
/**
* <summary>Sets the number of workers.</summary>
*
* <param name="numWorkers">The number of workers.</param>
*/
public void SetParallelWork(int threadCount, int parallelCount)
{
this.m_ParallelAgentCount = Mathf.Max(1, parallelCount);
this.m_MaxThreadCount = Mathf.Max(1, threadCount);
doneEvents = new ManualResetEvent[m_MaxThreadCount];
for (int i = 0; i < doneEvents.Length; i++)
{
doneEvents[i] = new ManualResetEvent(false);
}
}
/**
* <summary>Sets the time step of the simulation.</summary>
*
* <param name="timeStep">The time step of the simulation. Must be
* positive.</param>
*/
public void setTimeStep(float timeStep)
{
timeStep_ = timeStep;
}
internal void setAgentWeight(int agentNo, float v)
{
var agent = agents_[agentNo];
agent.weight_ = v;
}
internal float getAgentWeight(int agentNo)
{
return agents_[agentNo].weight_;
}
internal void removeAgent(int agentNo)
{
var agent = agents_[agentNo];
agent.removed_ = true;
removeList_.Push(agentNo);
}
/**
* <summary>Constructs and initializes a simulation.</summary>
*/
private Simulator()
{
mAgentIdCounter = 0;
m_AgentsCache = CacheArray<Agent>.Acquire(0, 100);
agents_ = new ConcurrentDictionary<int, Agent>();
removeList_ = new ConcurrentStack<int>();
defaultAgent_ = null;
kdTree_ = new KdTree();
obstacles_ = new List<Obstacle>();
globalTime_ = 0.0f;
timeStep_ = 0.1f;
SetParallelWork(4, 500);
buildTreeDoneEvent = new ManualResetEvent[] { new ManualResetEvent(false) };
}
}
}