Skip to content

Commit 2c0de3a

Browse files
authored
Feature: Delay Iteration (Merge PR #366 from scene-randomizer-support)
Ported over the ability to delay the current iteration
2 parents b3a5f03 + 237ba27 commit 2c0de3a

File tree

5 files changed

+133
-2
lines changed

5 files changed

+133
-2
lines changed

com.unity.perception/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Users can now choose the base folder location to store their generated datasets.
2121

2222
Added a `projection` field in the capture.sensor metadata. Values are either "perspective" or "orthographic".
2323

24+
Users can now delay the current iteration for one frame from within randomizers by calling the `DelayIteration` function of the active scenario.
25+
2426
### Changed
2527

2628
Changed the JSON serialization key of Normal Sampler's standard deviation property from "standardDeviation" to "stddev". Scneario JSON configurations that were generated using previous versions will need to be manually updated to reflect this change.

com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ private set
4848
public TextAsset configuration;
4949

5050
private bool m_ShouldRestartIteration;
51+
private bool m_ShouldDelayIteration;
52+
5153
private const int k_MaxIterationStartCount = 100;
5254

5355
/// <summary>
@@ -359,13 +361,22 @@ void IterationLoop()
359361
do
360362
{
361363
m_ShouldRestartIteration = false;
364+
m_ShouldDelayIteration = false;
362365
iterationStartCount++;
363366
foreach (var randomizer in activeRandomizers)
364367
{
365368
randomizer.IterationStart();
366369
if (m_ShouldRestartIteration)
367370
break;
371+
372+
if (m_ShouldDelayIteration)
373+
{
374+
Debug.Log($"Iteration was delayed by {randomizer.GetType().Name}");
375+
break;
376+
}
368377
}
378+
if (m_ShouldDelayIteration)
379+
break;
369380
} while (m_ShouldRestartIteration && iterationStartCount < k_MaxIterationStartCount);
370381

371382
if (m_ShouldRestartIteration)
@@ -381,7 +392,9 @@ void IterationLoop()
381392
randomizer.Update();
382393

383394
// Iterate scenario frame count
384-
currentIterationFrame++;
395+
if (!m_ShouldDelayIteration)
396+
currentIterationFrame++;
397+
385398
framesSinceInitialization++;
386399
}
387400

@@ -504,5 +517,18 @@ public void RestartIteration()
504517
{
505518
m_ShouldRestartIteration = true;
506519
}
520+
521+
/// <summary>
522+
/// Delays the current iteration by one frame.
523+
/// This results in <see cref="OnIterationStart" /> being called again for the same iteration.
524+
/// </summary>
525+
/// <remarks>
526+
/// Must be called from within the <see cref="OnIterationStart"/> function of a class
527+
/// inheriting from <see cref="Randomizer" />.
528+
/// </remarks>
529+
public void DelayIteration()
530+
{
531+
m_ShouldDelayIteration = true;
532+
}
507533
}
508534
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using UnityEngine;
3+
using UnityEngine.Perception.Randomization.Randomizers;
4+
5+
namespace RandomizationTests.ScenarioTests
6+
{
7+
/// <summary>
8+
/// Delays the scenario every Nth iteration where N is given by <see cref="m_IterationDelay" />.
9+
/// Does not delay the very first iteration i.e. iteration 0.
10+
/// </summary>
11+
/// <remarks>
12+
/// With <see cref="m_IterationDelay" /> set to 2, the iterations 2, 4, 6, ..., etc. will be delayed once.
13+
/// </remarks>
14+
[Serializable]
15+
[AddRandomizerMenu("Perception Tests/Example Delay Randomizer")]
16+
public class ExampleDelayRandomizer : Randomizer
17+
{
18+
int m_IterationDelay = 2;
19+
bool m_DelayedThisIteration = false;
20+
21+
public ExampleDelayRandomizer(int iterationDelay = 2)
22+
{
23+
m_IterationDelay = Math.Max(2, iterationDelay);
24+
}
25+
26+
protected override void OnIterationStart()
27+
{
28+
if (m_DelayedThisIteration)
29+
{
30+
m_DelayedThisIteration = false;
31+
return;
32+
}
33+
34+
var currentIteration = scenario.currentIteration;
35+
if (currentIteration > 0 && ((currentIteration) % m_IterationDelay) == 0)
36+
{
37+
Debug.Log($"Delaying iteration {currentIteration} once.");
38+
m_DelayedThisIteration = true;
39+
scenario.DelayIteration();
40+
}
41+
}
42+
}
43+
}

com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/ExampleDelayRandomizer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/ScenarioTests.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using System;
22
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
35
using NUnit.Framework;
46
using UnityEngine;
57
using UnityEngine.Perception.Randomization.Randomizers.SampleRandomizers;
68
using UnityEngine.Perception.Randomization.Samplers;
79
using UnityEngine.Perception.GroundTruth;
10+
using UnityEngine.Perception.Randomization.Randomizers;
811
using UnityEngine.Perception.Randomization.Scenarios;
912
using UnityEngine.TestTools;
1013
using Object = UnityEngine.Object;
@@ -30,11 +33,20 @@ public void TearDown()
3033
}
3134

3235
// TODO: update this function once the perception camera doesn't skip the first frame
33-
IEnumerator CreateNewScenario(int totalIterations, int framesPerIteration)
36+
IEnumerator CreateNewScenario(int totalIterations, int framesPerIteration, Randomizer[] randomizers = null)
3437
{
3538
m_Scenario = m_TestObject.AddComponent<TestFixedLengthScenario>();
3639
m_Scenario.constants.totalIterations = totalIterations;
3740
m_Scenario.constants.framesPerIteration = framesPerIteration;
41+
42+
if (randomizers != null)
43+
{
44+
foreach (var rnd in randomizers)
45+
{
46+
m_Scenario.AddRandomizer(rnd);
47+
}
48+
}
49+
3850
yield return null; // Skip first frame
3951
yield return null; // Skip first Update() frame
4052
}
@@ -142,6 +154,43 @@ public IEnumerator GeneratedRandomSeedsChangeWithScenarioIteration()
142154
Assert.AreNotEqual(seeds[i], SamplerState.NextRandomState());
143155
}
144156

157+
158+
[UnityTest]
159+
public IEnumerator IterationCorrectlyDelays()
160+
{
161+
yield return CreateNewScenario(5, 1, new Randomizer[]
162+
{
163+
// Delays every other iteration
164+
new ExampleDelayRandomizer(2)
165+
});
166+
167+
// State: currentIteration = 0
168+
Assert.AreEqual(0, m_Scenario.currentIteration);
169+
yield return null;
170+
// State: currentIteration = 1
171+
Assert.AreEqual(1, m_Scenario.currentIteration);
172+
yield return null;
173+
// State: currentIteration = 2
174+
// Action: ExampleDelayRandomizer will delay the iteration
175+
Assert.AreEqual(2, m_Scenario.currentIteration);
176+
yield return null;
177+
// State: currentIteration = 2
178+
Assert.AreEqual(2, m_Scenario.currentIteration);
179+
yield return null;
180+
// State: currentIteration = 3;
181+
Assert.AreEqual(3, m_Scenario.currentIteration);
182+
yield return null;
183+
// State: currentIteration = 4
184+
// Action: ExampleDelayRandomizer will delay the iteration
185+
Assert.AreEqual(4, m_Scenario.currentIteration);
186+
yield return null;
187+
// State: currentIteration = 4
188+
Assert.AreEqual(4, m_Scenario.currentIteration);
189+
yield return null;
190+
// State: currentIteration = 5
191+
Assert.AreEqual(5, m_Scenario.currentIteration);
192+
}
193+
145194
PerceptionCamera SetupPerceptionCamera()
146195
{
147196
m_TestObject.SetActive(false);

0 commit comments

Comments
 (0)