Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
CameraConfigurable.cs
Go to the documentation of this file.
1 using System;
7 using UnityEngine;
8 using Random = UnityEngine.Random;
9 
10 namespace droid.Runtime.Prototyping.Configurables.Experimental {
14  [AddComponentMenu(ConfigurableComponentMenuPath._ComponentMenuPath
15  + "Camera"
16  + ConfigurableComponentMenuPath._Postfix)]
17  [RequireComponent(typeof(Camera))]
22  string _fov_str;
23 
27  string _focal_str;
28 
32  string _sensor_width_str;
33 
36  string _sensor_height_str;
37 
40  string _lens_shift_y_str;
41 
44  string _lens_shift_x_str;
45 
46  string _gate_fit_str;
47 
48  [SerializeField] Camera _camera;
49  [SerializeField] SynchroniseCameraProperties _syncer;
50  [SerializeField] Space1 _fov_space = new Space1 {_Min_Value = 60f, _Max_Value = 90f};
51  [SerializeField] Space1 _focal_space = new Space1 {_Min_Value = 2f, _Max_Value = 3f};
52 
53  [SerializeField]
54  Space2 _sensor_size_space =
55  new Space2(2) {_Min_Values = new Vector2(2.5f, 2.5f), _Max_Values = new Vector2(5, 5)};
56 
57  [SerializeField]
58  Space2 _lens_shift_space =
59  new Space2(3) {_Min_Values = new Vector2(-0.1f, -0.1f), _Max_Values = new Vector2(0.1f, 0.1f)};
60 
61  [SerializeField] Space1 _gate_fit_space = new Space1(0) {_Min_Value = 0f, _Max_Value = 4f};
62 
66  protected override void PreSetup() {
67  this._fov_str = this.Identifier + "Fov";
68  this._focal_str = this.Identifier + "Focal";
69  this._sensor_width_str = this.Identifier + "SensorWidth";
70  this._sensor_height_str = this.Identifier + "SensorHeight";
71  this._lens_shift_x_str = this.Identifier + "LensShiftX";
72  this._lens_shift_y_str = this.Identifier + "LensShiftY";
73  this._gate_fit_str = this.Identifier + "GateFit";
74  if (!this._camera) {
75  this._camera = this.GetComponent<Camera>();
76  }
77 
78  if (!this._syncer) {
79  this._syncer = this.GetComponent<SynchroniseCameraProperties>();
80  }
81  }
82 
86  protected override void RegisterComponent() {
87  if (!this._camera.usePhysicalProperties) {
88  this.ParentEnvironment =
89  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._fov_str);
90  } else {
91  this.ParentEnvironment =
92  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._focal_str);
93  this.ParentEnvironment =
94  NeodroidUtilities.RegisterComponent(this.ParentEnvironment,
95  (Configurable)this,
96  this._sensor_width_str);
97  this.ParentEnvironment =
98  NeodroidUtilities.RegisterComponent(this.ParentEnvironment,
99  (Configurable)this,
100  this._sensor_height_str);
101  this.ParentEnvironment =
102  NeodroidUtilities.RegisterComponent(this.ParentEnvironment,
103  (Configurable)this,
104  this._lens_shift_x_str);
105  this.ParentEnvironment =
106  NeodroidUtilities.RegisterComponent(this.ParentEnvironment,
107  (Configurable)this,
108  this._lens_shift_y_str);
109  this.ParentEnvironment =
110  NeodroidUtilities.RegisterComponent(this.ParentEnvironment,
111  (Configurable)this,
112  this._gate_fit_str);
113  }
114  }
115 
119  protected override void UnRegisterComponent() {
120  if (!this._camera.usePhysicalProperties) {
121  this.ParentEnvironment?.UnRegister(this, this._fov_str);
122  } else {
123  this.ParentEnvironment?.UnRegister(this, this._focal_str);
124  this.ParentEnvironment?.UnRegister(this, this._sensor_width_str);
125  this.ParentEnvironment?.UnRegister(this, this._sensor_height_str);
126  this.ParentEnvironment?.UnRegister(this, this._lens_shift_x_str);
127  this.ParentEnvironment?.UnRegister(this, this._lens_shift_y_str);
128  this.ParentEnvironment?.UnRegister(this, this._gate_fit_str);
129  }
130  }
131 
132  public override ISpace ConfigurableValueSpace { get; }
133 
137  public override void ApplyConfiguration(IConfigurableConfiguration configuration) {
138  #if NEODROID_DEBUG
139  if (this.Debugging) {
140  DebugPrinting.ApplyPrint(this.Debugging, configuration, this.Identifier);
141  }
142  #endif
143 
144  if (configuration.ConfigurableName == this._fov_str) {
145  this._camera.fieldOfView = configuration.ConfigurableValue;
146  } else if (configuration.ConfigurableName == this._focal_str) {
147  this._camera.focalLength = configuration.ConfigurableValue;
148  } else if (configuration.ConfigurableName == this._sensor_width_str) {
149  var a = this._camera.sensorSize;
150  a.x = configuration.ConfigurableValue;
151  this._camera.sensorSize = a;
152  } else if (configuration.ConfigurableName == this._sensor_height_str) {
153  var a = this._camera.sensorSize;
154  a.y = configuration.ConfigurableValue;
155  this._camera.sensorSize = a;
156  } else if (configuration.ConfigurableName == this._lens_shift_x_str) {
157  var a = this._camera.lensShift;
158  a.x = configuration.ConfigurableValue;
159  this._camera.lensShift = a;
160  } else if (configuration.ConfigurableName == this._lens_shift_y_str) {
161  var a = this._camera.lensShift;
162  a.y = configuration.ConfigurableValue;
163  this._camera.lensShift = a;
164  } else if (configuration.ConfigurableName == this._gate_fit_str) {
165  Enum.TryParse(((int)configuration.ConfigurableValue).ToString(),
166  out Camera.GateFitMode gate_fit_mode);
167  this._camera.gateFit = gate_fit_mode;
168  }
169 
170  if (this._syncer) {
171  this._syncer.Sync_Cameras();
172  }
173  }
174 
180  public override Configuration[] SampleConfigurations() {
181  if (!this._camera.usePhysicalProperties) {
182  return new[] {new Configuration(this._fov_str, this._fov_space.Sample())};
183  }
184 
185  var r = Random.Range(0, 6);
186  switch (r) {
187  case 0:
188  return new[] {new Configuration(this._focal_str, this._focal_space.Sample())};
189  case 1:
190  return new[] {new Configuration(this._sensor_width_str, this._sensor_size_space.Sample().x)};
191 
192  case 2:
193  return new[] {new Configuration(this._sensor_height_str, this._sensor_size_space.Sample().y)};
194 
195  case 3:
196  return new[] {new Configuration(this._lens_shift_x_str, this._lens_shift_space.Sample().x)};
197 
198  case 4:
199  return new[] {new Configuration(this._lens_shift_y_str, this._lens_shift_space.Sample().y)};
200 
201  case 5:
202  return new[] {new Configuration(this._gate_fit_str, this._gate_fit_space.Sample())};
203  default:
204  throw new IndexOutOfRangeException();
205  }
206  }
207  }
208 }
override void ApplyConfiguration(IConfigurableConfiguration configuration)