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
#include "ScanGridProvider.h"

ScanGridProvider::ScanGridProvider()
:
  cameraID(CameraInfo::Top)
{
  DEBUG_REQUEST_REGISTER("Vision:ScanGridProvider:draw_verticals", "draw vertical scanlines", false);
  DEBUG_REQUEST_REGISTER("Vision:ScanGridProvider:draw_horizontals", "draw horizontal scanlines", false);
  DEBUG_REQUEST_REGISTER("Vision:ScanGridProvider:draw_vertical_min_y", "", false);
  DEBUG_REQUEST_REGISTER("Vision:ScanGridProvider:draw_v_scan_pattern", "", false);
  getDebugParameterList().add(&params);
}

ScanGridProvider::~ScanGridProvider()
{
  getDebugParameterList().remove(&params);
}

void ScanGridProvider::execute(CameraInfo::CameraID id)
{
  this->cameraID = id;

  if(!getCameraMatrix().valid) {
      return;
  }
  getScanGrid().reset();

  const int width = getImage().width();
  const int height = getImage().height();

  // calculate horizon
  int horizon_offset = 2;
  int max_horizon =
      std::max((int) (getArtificialHorizon().begin().y + horizon_offset), 0);
  int min_horizon =
      std::max((int) (getArtificialHorizon().end().y + horizon_offset), 0);

  if (min_horizon > max_horizon) {
    std::swap(min_horizon, max_horizon);
  }

  if (max_horizon >= height) {
    // camera doesn't point to the ground
    return;
  }

  Vector2i pointInImage;

  // project max_scan_distance_mm on the image plain
  // to obtain the upper limit of vertical scanlines
  Vector3d cameraMatrixOffset = Vector3d(getCameraMatrix().translation.x,
                                         getCameraMatrix().translation.y,
                                         0);
  Vector3d farthestPoint = cameraMatrixOffset +
      (RotationMatrix::getRotationZ(getCameraMatrix().rotation.getZAngle()) *
       Vector3d(params.max_scan_distance_mm, 0, 0));

  if(!CameraGeometry::relativePointToImage(getCameraMatrix(), getCameraInfo(),
                                           farthestPoint, pointInImage))
  {
    // projection failed
    return;
  }

  // should be under the horizon and not higher than top of the image
  int min_scan_y = std::max(pointInImage.y, max_horizon);
  // should not be lower than the bottom of the image
  if(min_scan_y >= height) {
    return;
  }

  DEBUG_REQUEST("Vision:ScanGridProvider:draw_vertical_min_y",
    LINE_PX(ColorClasses::black, 0, min_scan_y, width-1, min_scan_y);
  );

  double fieldWidthCovered = calculateMaxFieldWidth(min_scan_y);
  if (fieldWidthCovered <= 0) {
    return;
  }
  // calculate the number of vertical scanlines at min_scan_y,
  // so the gap at min_scan_y is horizontal_gap_mm
  int numberOfVerticals = std::min(
        (int) (fieldWidthCovered / params.horizontal_gap_mm),
        params.max_vertical_scanlines);

  // calculate vertical scan pattern by creating points
  // on the field and projecting them back onto the image
  calculate_vertical_scan_pattern(min_scan_y);
  if(getScanGrid().vScanPattern.empty()) {
    return;
  }

  double minGap = width / (double) numberOfVerticals;

  double focalLength = getCameraInfo().getFocalLength();
  double cameraHeight = getCameraMatrix().translation.z;

  int max_scan_y;
  double distance;
  std::vector<size_t> line_start_increasing_length;
  std::vector<double> v_gaps;

  double gap_modifier = params.vertical_gap_mm / params.horizontal_gap_mm;

  int vScan_idx = static_cast<int>(getScanGrid().vScanPattern.size()) - 1;
  
  for(double gap = minGap; gap < width; gap *= 2)
  {
    // distance to an object with a size of "gap" in the image,
    // if it would have a size of "horizontal_gap_mm" on the field
    distance = params.horizontal_gap_mm * focalLength / gap;

    // determine the start of the vertical scanline
    if(distance < cameraHeight) {
      max_scan_y = height-1;
    } else {
      // project distance onto the ground plane
      distance = std::sqrt(distance * distance - cameraHeight * cameraHeight);
      // project point in the distance onto the image,
      // to obtain the start of the scanline
      Vector3d pointOnField = cameraMatrixOffset +
          (RotationMatrix::getRotationZ(getCameraMatrix().rotation.getZAngle())
           * Vector3d(distance, 0, 0));

      bool projectionSucces = CameraGeometry::relativePointToImage(
            getCameraMatrix(), getCameraInfo(),
            pointOnField,
            pointInImage);
      if(!projectionSucces) {
        break;
      }
      max_scan_y = std::min(pointInImage.y, height-1);
      if (max_scan_y <= min_scan_y) {
        continue;
      }
    }
    // determine start in vertical scan pattern,
    // iterate backwards because most scanlines are short
    size_t bottom_idx = 0;
    for(; vScan_idx >= 0; vScan_idx--)
    {
      int y = getScanGrid().vScanPattern[static_cast<size_t>(vScan_idx)];
      if(y > max_scan_y) {
        bottom_idx = std::min(getScanGrid().vScanPattern.size()-1, static_cast<size_t>(vScan_idx)+1);
        break;
      }
    }

    line_start_increasing_length.push_back(bottom_idx);

    // vertical gap sizes
    v_gaps.push_back(gap_modifier * gap);

    if(max_scan_y >= height-1) {
      // bottom of the image reached
      break;
    }
  }

  if(line_start_increasing_length.empty()) {
    return;
  }

  // set all lines to the same lenghts
  if(params.uniform_vertical_lengths) {
    std::fill(line_start_increasing_length.begin(), line_start_increasing_length.end(), line_start_increasing_length.back());
  }

  // fill the image with vertical scanlines
  getScanGrid().vertical.resize(numberOfVerticals);

  std::vector<size_t>::iterator line_start_itr =
      line_start_increasing_length.begin();

  int frequency;
  for(int i=1; i<=numberOfVerticals; i*=2) {
    frequency = i*2;

    for(int j=i-1; j<numberOfVerticals; j+=frequency) {
      int x = (int) (j*minGap);
      ScanGrid::VScanLine scanline;
      scanline.x = x;
      scanline.bottom = *line_start_itr;
      scanline.top = getScanGrid().vScanPattern.size()-1;

      getScanGrid().vertical[j] = scanline;

      //if(next(line) == linesIncreasingLength.end()) {
      //  getScanGrid().longverticals.push_back(j);
      //}
    }
    if(next(line_start_itr) != line_start_increasing_length.end()){
      ++line_start_itr;
    }
  }

  // calculate approximate number of horizontals with the current v_gaps
  line_start_itr = line_start_increasing_length.begin();
  double last_y = min_scan_y;
  double n_horizontals = 0;
  for(double v_gap : v_gaps) {
    double y = getScanGrid().vScanPattern[*line_start_itr];
    n_horizontals += (y - last_y) / v_gap;
    last_y = y;

    ++line_start_itr;
  }

  // adjust v_gaps if too many horizontals
  if (n_horizontals > params.max_horizontal_scanlines) {
    gap_modifier = n_horizontals / params.max_horizontal_scanlines;
    for(double& v_gap : v_gaps) {
      v_gap *= gap_modifier;
    }
  }

  getScanGrid().horizontal.reserve(params.max_horizontal_scanlines);

  gap_modifier = params.h_field_scan_rate_mm / params.horizontal_gap_mm;

  // fill the image with horizontal scanlines
  size_t i = 0;
  double y = min_scan_y;
  double h_skip = gap_modifier * minGap;
  while(y < height) {
    ScanGrid::HScanLine horizontal;
    horizontal.left_x = 0;
    horizontal.right_x = width-1;
    horizontal.y = (int) y;
    horizontal.skip = std::max((int) h_skip, params.min_horizontal_gap_px);

    getScanGrid().horizontal.push_back(horizontal);

    double v_gap = v_gaps[i];
    size_t h_line_start = line_start_increasing_length[i];

    if(y + v_gap > getScanGrid().vScanPattern[h_line_start]) {
      if(++i >= v_gaps.size()) {
        break;
      }
      // adjust gaps
      v_gap = v_gaps[i];
      h_line_start = line_start_increasing_length[i];
      h_skip = gap_modifier * v_gap;
    }
    y += v_gap;
  }

  DEBUG_REQUEST("Vision:ScanGridProvider:draw_verticals",
    for(const ScanGrid::VScanLine& scanline: getScanGrid().vertical)
    {
      LINE_PX(ColorClasses::blue,
              scanline.x, getScanGrid().vScanPattern.at(scanline.bottom),
              scanline.x, getScanGrid().vScanPattern.at(scanline.top));
      for(size_t ii=scanline.bottom; ii<=scanline.top; ++ii)
      {
        POINT_PX(ColorClasses::red, scanline.x, getScanGrid().vScanPattern.at(ii));

        //Test
        const int xx = scanline.x;
        const int yy = getScanGrid().vScanPattern.at(ii);
        getImage().getY(xx, yy);
      }
    }
  );

  DEBUG_REQUEST("Vision:ScanGridProvider:draw_horizontals",
    for(const ScanGrid::HScanLine& scanline: getScanGrid().horizontal)
    {
      LINE_PX(ColorClasses::blue, scanline.left_x, scanline.y, scanline.right_x, scanline.y);
      for(int x=scanline.left_x; x<=scanline.right_x; x+=scanline.skip)
      {
        POINT_PX(ColorClasses::red, x, scanline.y);

        //Test
        getImage().getY(x, scanline.y);
      }
    }
  );

}//end execute


double ScanGridProvider::calculateMaxFieldWidth(int y)
{
  // project the left and right image corner onto the field
  // and caluclate the distance between thems
  Vector2d projectedLeftCorner;
  if(!CameraGeometry::imagePixelToFieldCoord(
    getCameraMatrix(), getCameraInfo(),
    0,
    y,
    0.0,
    projectedLeftCorner))
  {
    return -1.;
  }
  Vector2d projectedRightCorner;
  if(!CameraGeometry::imagePixelToFieldCoord(
    getCameraMatrix(), getCameraInfo(),
    getImage().width()-1,
    y,
    0.0,
    projectedRightCorner))
  {
    return -1.;
  }
  return (projectedLeftCorner - projectedRightCorner).abs();
}

bool ScanGridProvider::calculate_vertical_scan_pattern(int min_scan_y)
{
  std::vector<int>& scan_pattern = getScanGrid().vScanPattern;
  //std::vector<int> scan_pattern;

  const double width = getImage().width();
  const double height = getImage().height();

  // get depicted field point at the bottom of the image
  Vector2d field_point;
  if(!CameraGeometry::imagePixelToFieldCoord(
    getCameraMatrix(), getCameraInfo(),
    width/2,
    height,
    0.0,
    field_point))
  {
    return false;
  }
  Vector3d field_point_3d(field_point.x, field_point.y, 0);

  // camera direction
  Vector3d direction_3d = RotationMatrix::getRotationZ(getCameraMatrix().rotation.getZAngle()) *
                          Vector3d(params.v_field_scan_rate_mm, 0, 0);

  // create scan points starting from the bottom of the image
  int y = getImage().height()-1;
  Vector2i image_point;
  bool skip_to_small = false;
  while(y > min_scan_y)
  {
    DEBUG_REQUEST("Vision:ScanGridProvider:draw_v_scan_pattern",
      POINT_PX(ColorClasses::red, getImage().width()/2, y);
    );
    scan_pattern.push_back(y);

    if(!skip_to_small) {
      field_point_3d += direction_3d;

      bool projectionSucces = CameraGeometry::relativePointToImage(
            getCameraMatrix(), getCameraInfo(),
            field_point_3d,
            image_point);
      if(!projectionSucces) {
        return false;
      }

      if(y - image_point.y < params.min_vertical_gap_px) {
        y -= params.min_vertical_gap_px;
        skip_to_small = true;
      } else {
        y = image_point.y;
      }
    } else {
      y -= params.min_vertical_gap_px;
    }
  }
  return true;
}