File: PlanarPolygon.h

package info (click to toggle)
vecgeom 1.2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 23,928 kB
  • sloc: cpp: 88,717; ansic: 6,894; python: 1,035; sh: 582; sql: 538; makefile: 29
file content (627 lines) | stat: -rw-r--r-- 20,452 bytes parent folder | download | duplicates (2)
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
#ifndef VECGEOM_PLANAR_POLYGON_H
#define VECGEOM_PLANAR_POLYGON_H

#include "VecGeom/base/Global.h"
#include "VecGeom/base/SOA3D.h"
#include "VecGeom/base/Vector3D.h"
#include "VecGeom/base/Vector.h"
#include <VecCore/VecCore>
#include <iostream>
#include <limits>

namespace vecgeom {

VECGEOM_DEVICE_FORWARD_DECLARE(class PlanarPolygon;);
VECGEOM_DEVICE_DECLARE_CONV(class, PlanarPolygon);

inline namespace VECGEOM_IMPL_NAMESPACE {

// a class representing a 2D convex or concav polygon
class PlanarPolygon {

  friend struct SExtruImplementation;

protected:
  // we have to work on the "ideal" memory layout/placement for this
  // this is WIP
  SOA3D<Precision> fVertices; // a vector of vertices with links between
                              // note that the z component will hold the slopes between 2 links
                              // We assume a clockwise order of points
  Vector<Precision> fShiftedXJ;
  Vector<Precision> fShiftedYJ;
  Vector<Precision> fLengthSqr;    // the lenghts of each segment
  Vector<Precision> fInvLengthSqr; // the inverse square lengths of each segment
  Vector<Precision> fA;            // the "a"=x coefficient in the plane equation
  Vector<Precision> fB;            // the "b"=y coefficient in the plane equation
  Vector<Precision> fD;            // the "d" coefficient in the plane equation

  bool fIsConvex;  // convexity property to be calculated a construction time
  Precision fMinX; // the extent of the polygon
  Precision fMinY;
  Precision fMaxX;
  Precision fMaxY;

  size_t fNVertices; // the actual number of vertices
  friend class PolygonalShell;

public:
  VECCORE_ATT_HOST_DEVICE
  PlanarPolygon()
      : fVertices(), fShiftedXJ({}), fShiftedYJ({}), fLengthSqr({}), fInvLengthSqr({}), fA({}), fB({}), fD({}),
        fIsConvex(false), fMinX(kInfLength), fMinY(kInfLength), fMaxX(-kInfLength), fMaxY(-kInfLength), fNVertices(0)
  {
  }

  // constructor (not taking ownership of the pointers)
  VECCORE_ATT_HOST_DEVICE
  PlanarPolygon(int nvertices, Precision *x, Precision *y)
      : fVertices(), fShiftedXJ({}), fShiftedYJ({}), fLengthSqr({}), fInvLengthSqr({}), fA({}), fB({}), fD({}),
        fIsConvex(false), fMinX(kInfLength), fMinY(kInfLength), fMaxX(-kInfLength), fMaxY(-kInfLength),
        fNVertices(nvertices)
  {
    Init(nvertices, x, y);
  }

  VECCORE_ATT_HOST_DEVICE
  void Init(int nvertices, Precision *x, Precision *y)
  {
    // allocating more space than nvertices, in order
    // to accomodate an internally vectorized treatment without tails
    // --> the size comes from this formula:
    const size_t kVS                = vecCore::VectorSize<vecgeom::VectorBackend::Real_v>();
    const auto numberOfVectorChunks = (nvertices / kVS + nvertices % kVS);
    // actual buffersize
    const auto bs = numberOfVectorChunks * kVS;
    assert(bs > 0);
    fNVertices = nvertices;
    fVertices.reserve(bs);
    fVertices.resize(nvertices);
    fShiftedXJ.resize(bs, 0);
    fShiftedYJ.resize(bs, 0);
    fLengthSqr.resize(bs, 0);
    fInvLengthSqr.resize(bs, 0);
    fA.resize(bs, 0);
    fB.resize(bs, 0);
    fD.resize(bs, 0);

    int inc = (GetOrientation(x, y, nvertices) > 0) ? -1 : 1;
    size_t i, j;
    // init the vertices (wrapping around periodically)
    for (i = 0; i < (size_t)fNVertices; ++i) {
      const size_t k = (i * inc + fNVertices) % fNVertices;
      fVertices.set(i, x[k], y[k], 0);
      fMinX = vecCore::math::Min(x[k], fMinX);
      fMinY = vecCore::math::Min(y[k], fMinY);
      fMaxX = vecCore::math::Max(x[k], fMaxX);
      fMaxY = vecCore::math::Max(y[k], fMaxY);
    }

    // initialize and cache the slopes as a "hidden" component
    auto slopes      = fVertices.z();
    const auto S     = fNVertices;
    const auto vertx = fVertices.x();
    const auto verty = fVertices.y();
    for (i = 0, j = S - 1; i < S; j = i++) {
      const auto vertxI = vertx[i];
      const auto vertxJ = vertx[j];

      const auto vertyI = verty[i];
      const auto vertyJ = verty[j];

      slopes[i]     = (vertxJ - vertxI) / NonZero(vertyJ - vertyI);
      fShiftedYJ[i] = vertyJ;
      fShiftedXJ[i] = vertxJ;
    }

    for (i = 0; i < (size_t)S; ++i) {
      fLengthSqr[i] = (vertx[i] - fShiftedXJ[i]) * (vertx[i] - fShiftedXJ[i]) +
                      (verty[i] - fShiftedYJ[i]) * (verty[i] - fShiftedYJ[i]);
      fInvLengthSqr[i] = 1. / fLengthSqr[i];
    }

    // init normals
    // this is taken from UnplacedTrapezoid
    // we should make this a standalone function outside any volume class
    for (i = 0; i < (size_t)S; ++i) {
      const auto xi = fVertices.x();
      const auto yi = fVertices.y();

      // arbitary choice of normal for the moment
      auto a = -(fShiftedYJ[i] - yi[i]);
      auto b = +(fShiftedXJ[i] - xi[i]);

      auto norm = 1.0 / std::sqrt(a * a + b * b); // normalization factor, always positive
      a *= norm;
      b *= norm;

      auto d = -(a * xi[i] + b * yi[i]);

      // fix (sign of zero (avoid -0 ))
      if (std::abs(a) < kTolerance) a = 0.;
      if (std::abs(b) < kTolerance) b = 0.;
      if (std::abs(d) < kTolerance) d = 0.;

      //      std::cerr << a << "," << b << "," << d << "\n";

      fA[i] = a;
      fB[i] = b;
      fD[i] = d;
    }

    // fill rest of data buffers periodically (for safe internal vectorized treatment)
    for (i = S; i < bs; ++i) {
      const size_t k = i % fNVertices;
      fVertices.set(i, fVertices.x()[k], fVertices.y()[k], fVertices.z()[k]);
      fShiftedXJ[i]    = fShiftedXJ[k];
      fShiftedYJ[i]    = fShiftedYJ[k];
      fLengthSqr[i]    = fLengthSqr[k];
      fInvLengthSqr[i] = fInvLengthSqr[k];
      fA[i]            = fA[k];
      fB[i]            = fB[k];
      fD[i]            = fD[k];
    }

    // set convexity
    CalcConvexity();

// check orientation
#ifndef VECCORE_CUDA
    if (Area() < 0.) {
      throw std::runtime_error("Polygon not given in clockwise order");
    }
#endif
  }

  VECCORE_ATT_HOST_DEVICE
  Precision GetMinX() const { return fMinX; }

  VECCORE_ATT_HOST_DEVICE
  Precision GetMinY() const { return fMinY; }

  VECCORE_ATT_HOST_DEVICE
  Precision GetMaxX() const { return fMaxX; }

  VECCORE_ATT_HOST_DEVICE
  Precision GetMaxY() const { return fMaxY; }

  VECCORE_ATT_HOST_DEVICE
  SOA3D<Precision> const &GetVertices() const { return fVertices; }

  VECCORE_ATT_HOST_DEVICE
  size_t GetNVertices() const { return fNVertices; }

  // checks if 2D coordinates (x,y) are on the line segment given by index i
  template <typename Real_v, typename InternalReal_v, typename Bool_v>
  VECCORE_ATT_HOST_DEVICE
  Bool_v OnSegment(size_t i, Real_v const &px, Real_v const &py) const
  {
    using vecCore::FromPtr;

    // static assert ( cannot have Real_v == InternalReal_v )

    Bool_v result(false);
    //
    const auto vertx = fVertices.x();
    const auto verty = fVertices.y();
    // const auto slopes = fVertices.z();

    // check if cross close to zero
    const Real_v bx(FromPtr<InternalReal_v>(&vertx[i]));
    const Real_v by(FromPtr<InternalReal_v>(&verty[i]));
    const Real_v ax(FromPtr<InternalReal_v>(&fShiftedXJ[i]));
    const Real_v ay(FromPtr<InternalReal_v>(&fShiftedYJ[i]));
    // const Real_v slope(FromPtr<InternalReal_v>(&slopes[i]));

    const Real_v pymay(py - ay);
    const Real_v pxmax(px - ax);
    const Real_v epsilon(1E-9);

    // optimized crossproduct
    const Real_v cross     = (pymay * (bx - ax) - pxmax * (by - ay));
    const Bool_v collinear = Abs(cross) < epsilon;
    // TODO: can we use the slope?
    // const Bool_v collinear = Abs(pymay - slope * pxmax) < epsilon;

    if (vecCore::MaskFull(!collinear)) {
      return result;
    }
    result |= collinear;

    // can we do this with the slope??
    const auto dotproduct = pxmax * (bx - ax) + pymay * (by - ay);

    // check if length correct (use MakeTolerant templates)
    const Real_v tol(kTolerance);
    result &= (dotproduct >= -tol);
    result &= (dotproduct <= tol + Real_v(FromPtr<InternalReal_v>(&fLengthSqr[i])));
    return result;
  }

  template <typename Real_v, typename Bool_v = vecCore::Mask_v<Real_v>>
  VECCORE_ATT_HOST_DEVICE
  inline Bool_v ContainsConvex(Vector3D<Real_v> const &point) const
  {
    const size_t S = fVertices.size();
    Bool_v result(false);
    Real_v distance = -InfinityLength<Real_v>();
    for (size_t i = 0; i < S; ++i) {
      Real_v dseg = -(fA[i] * point.x() + fB[i] * point.y() + fD[i]);
      vecCore__MaskedAssignFunc(distance, dseg > distance, dseg);
    }
    result = distance < Real_v(0.);
    return result;
  }

  template <typename Real_v, typename Bool_v = vecCore::Mask_v<Real_v>>
  VECCORE_ATT_HOST_DEVICE
  inline Bool_v Contains(Vector3D<Real_v> const &point) const
  {
    const size_t S = fVertices.size();
    Bool_v result(false);
    // implementation based on the point-polygon test after Jordan
    const auto vertx  = fVertices.x();
    const auto verty  = fVertices.y();
    const auto slopes = fVertices.z();
    const auto py     = point.y();
    const auto px     = point.x();
    for (size_t i = 0; i < S; ++i) {
      const auto vertyI       = verty[i];
      const auto vertyJ       = fShiftedYJ[i];
      const Bool_v condition1 = (vertyI > py) ^ (vertyJ > py);

      // early return leads to performance slowdown
      //  if (vecCore::MaskEmpty(condition1))
      //    continue;
      const auto vertxI     = vertx[i];
      const auto condition2 = px < (slopes[i] * (py - vertyI) + vertxI);

      result = (condition1 & condition2) ^ result;
    }
    return result;
  }

  template <typename Real_v, typename Inside_v = int /*vecCore::Index_v<Real_v>*/>
  VECCORE_ATT_HOST_DEVICE
  inline Inside_v InsideConvex(Vector3D<Real_v> const &point) const
  {
    assert(fIsConvex);
    const size_t S  = fVertices.size();
    Inside_v result = Inside_v(vecgeom::kOutside);
    Real_v distance = -InfinityLength<Real_v>();
    for (size_t i = 0; i < S; ++i) {
      Real_v dseg = -(fA[i] * point.x() + fB[i] * point.y() + fD[i]);
      vecCore__MaskedAssignFunc(distance, dseg > distance, dseg);
    }
    vecCore__MaskedAssignFunc(result, distance < Real_v(-kTolerance), Real_v(vecgeom::kInside));
    vecCore__MaskedAssignFunc(result, distance < Real_v(kTolerance), Real_v(vecgeom::kSurface));
    return result;
  }

  // calculate an underestimate of safety for the convex case
  template <typename Real_v>
  VECCORE_ATT_HOST_DEVICE
  Real_v SafetyConvex(Vector3D<Real_v> const &point, bool inside) const
  {
    assert(fIsConvex);
    const size_t S  = fVertices.size();
    Real_v distance = -InfinityLength<Real_v>();
    for (size_t i = 0; i < S; ++i) {
      Real_v dseg = -(fA[i] * point.x() + fB[i] * point.y() + fD[i]);
      vecCore__MaskedAssignFunc(distance, dseg > distance, dseg);
      if (inside) distance *= Real_v(-1.);
    }
    return distance;
  }

  // calculate precise safety sqr to the polygon; return the closest "line" id
  template <typename Real_v>
  VECCORE_ATT_HOST_DEVICE
  Real_v SafetySqr(Vector3D<Real_v> const &point, int &closestid) const
  {
    // implementation based on TGeoPolygone@ROOT
    Real_v safe(1E30);
    int isegmin = -1;

    const auto vertx = fVertices.x();
    const auto verty = fVertices.y();
    const auto S     = fVertices.size();
    for (size_t i = 0; i < S; ++i) {

      // could use the slope information to calc
      const Real_v p1[2] = {vertx[i], verty[i]};
      const Real_v p2[2] = {fShiftedXJ[i], fShiftedYJ[i]};

      const auto dx = p2[0] - p1[0];
      const auto dy = p2[1] - p1[1];
      auto dpx      = point.x() - p1[0];
      auto dpy      = point.y() - p1[1];

      // degenerate edge?
      // const auto lsq = dx * dx + dy * dy;

      // I don't think this is useful -- its a pure static property
      //         if ( ClostToZero(lsq,0)) {
      //            ssq = dpx*dpx + dpy*dpy;
      //            if (ssq < safe) {
      //               safe = ssq;
      //               isegmin = i;
      //            }
      //            continue;
      //         }

      const auto u = (dpx * dx + dpy * dy) * fInvLengthSqr[i];
      if (u > 1) {
        dpx = point.x() - p2[0];
        dpy = point.y() - p2[1];
      } else {
        if (u >= 0) {
          // need to divide by lsq now
          // since this is a static property of the polygon
          // we could actually cache it;
          dpx -= u * dx;
          dpy -= u * dy;
        }
      }
      const auto ssq = dpx * dpx + dpy * dpy;
      if (ssq < safe) {
        safe    = ssq;
        isegmin = i;
      }

      // check if we are done early ( on surface )
      if (Abs(safe) < kTolerance * kTolerance) {
        closestid = isegmin;
        return Real_v(0.);
      }
    }
    closestid = isegmin;
    return safe;
  }

  VECCORE_ATT_HOST_DEVICE
  bool IsConvex() const { return fIsConvex; }

  // check clockwise/counterclockwise condition (returns positive for anti-clockwise)
  // useful function to check orientation of points x,y
  // before calling the PlanarPolygon constructor
  VECCORE_ATT_HOST_DEVICE
  static Precision GetOrientation(Precision *x, Precision *y, size_t N)
  {
    Precision area(0.);
    for (size_t i = 0; i < N; ++i) {
      const Precision p1[2] = {x[i], y[i]};
      const size_t j        = (i + 1) % N;
      const Precision p2[2] = {x[j], y[j]};
      area += (p1[0] * p2[1] - p1[1] * p2[0]);
    }
    return area;
  }

  /* returns area of polygon */
  VECCORE_ATT_HOST_DEVICE
  Precision Area() const
  {
    const auto vertx = fVertices.x();
    const auto verty = fVertices.y();

    const auto kS = fVertices.size();
    Precision area(0.);
    for (size_t i = 0; i < kS; ++i) {
      const Precision p1[2] = {vertx[i], verty[i]};
      const Precision p2[2] = {fShiftedXJ[i], fShiftedYJ[i]};

      area += (p1[0] * p2[1] - p1[1] * p2[0]);
    }
    return 0.5 * area;
  }

private:
  VECCORE_ATT_HOST_DEVICE
  void CalcConvexity()
  {
    // check if we are always turning into the same sense
    // --> check if the sign of the cross product is always the same
    const auto vertx = fVertices.x();
    const auto verty = fVertices.y();

    const auto kS = fNVertices;
    int counter(0);
    for (size_t i = 0; i < kS; ++i) {
      size_t j              = (i + 1) % kS;
      size_t k              = (i + 2) % kS;
      const Precision p1[2] = {vertx[j] - vertx[i], verty[j] - verty[i]};
      const Precision p2[2] = {vertx[k] - vertx[j], verty[k] - verty[j]};
      counter += (p1[0] * p2[1] - p1[1] * p2[0]) < 0 ? -1 : 1;
    }
    fIsConvex = (size_t)std::abs(counter) == kS;
  }
};

// template specialization for scalar case (do internal vectorization)
#define SPECIALIZE
#ifdef SPECIALIZE

template <>
VECCORE_ATT_HOST_DEVICE
inline bool PlanarPolygon::ContainsConvex(Vector3D<Precision> const &point) const
{
  const size_t S     = fVertices.size();
  Precision distance = -InfinityLength<Precision>();
  for (size_t i = 0; i < S; ++i) {
    Precision dseg = -(fA[i] * point.x() + fB[i] * point.y() + fD[i]);
    distance       = vecCore::math::Max(dseg, distance);
  }
  return (distance < 0.);
}

template <>
VECCORE_ATT_HOST_DEVICE
inline bool PlanarPolygon::Contains(Vector3D<Precision> const &point) const
{

  using Real_v = vecgeom::VectorBackend::Real_v;
  using Bool_v = vecCore::Mask_v<Real_v>;
  using vecCore::FromPtr;

  const auto kVectorS = vecCore::VectorSize<Real_v>();

  Bool_v result(false);
  const Real_v px(point.x());
  const Real_v py(point.y());
  const size_t S       = fVertices.size();
  const size_t SVector = S - S % kVectorS;
  size_t i(0);
  const auto vertx  = fVertices.x();
  const auto verty  = fVertices.y();
  const auto slopes = fVertices.z();
  // treat vectorizable part of loop
  for (; i < SVector; i += kVectorS) {
    const Real_v vertyI(FromPtr<Real_v>(&verty[i]));      // init vectors
    const Real_v vertyJ(FromPtr<Real_v>(&fShiftedYJ[i])); // init vectors

    const auto condition1 = (vertyI > py) ^ (vertyJ > py); // xor

    const Real_v vertxI(FromPtr<Real_v>(&vertx[i]));
    const Real_v slope(FromPtr<Real_v>(&slopes[i]));
    const auto condition2 = px < (slope * (py - vertyI) + vertxI);

    result = result ^ (condition1 & condition2);
  }
  // reduction over vector lanes
  bool reduction(false);
  for (size_t j = 0; j < kVectorS; ++j) {
    if (vecCore::MaskLaneAt(result, j)) reduction = !reduction;
  }

  // treat tail
  using Real_s = vecCore::Scalar<Real_v>;
  for (; i < S; ++i) {
    const Real_s vertyI(FromPtr<Real_s>(&verty[i]));                     // init vectors
    const Real_s vertyJ(FromPtr<Real_s>(&fShiftedYJ[i]));                // init vectors
    const bool condition1 = (vertyI > point.y()) ^ (vertyJ > point.y()); // xor
    const Real_s vertxI(FromPtr<Real_s>(&vertx[i]));
    const Real_s slope(FromPtr<Real_s>(&slopes[i]));
    const bool condition2 = point.x() < (slope * (point.y() - vertyI) + vertxI);

    reduction = reduction ^ (condition1 & condition2);
  }
  return reduction;
}

template <>
VECCORE_ATT_HOST_DEVICE
inline Inside_t PlanarPolygon::InsideConvex(Vector3D<Precision> const &point) const
{
  const size_t S = fVertices.size();
  assert(fIsConvex);
  Precision distance = -InfinityLength<Precision>();
  for (size_t i = 0; i < S; ++i) {
    Precision dseg = -(fA[i] * point.x() + fB[i] * point.y() + fD[i]);
    distance       = vecCore::math::Max(dseg, distance);
  }
  if (distance > kTolerance) return vecgeom::kOutside;
  if (distance < -kTolerance) return vecgeom::kInside;
  return vecgeom::kSurface;
}

// template specialization for convex safety
template <>
VECCORE_ATT_HOST_DEVICE
inline Precision PlanarPolygon::SafetyConvex(Vector3D<Precision> const &point, bool inside) const
{
  const size_t S = fVertices.size();
  assert(fIsConvex);
  Precision distance = -InfinityLength<Precision>();
  for (size_t i = 0; i < S; ++i) {
    Precision dseg = -(fA[i] * point.x() + fB[i] * point.y() + fD[i]);
    distance       = vecCore::math::Max(dseg, distance);
  }
  if (inside) distance *= -1.;
  return distance;
}

// template specialization for scalar safety
template <>
VECCORE_ATT_HOST_DEVICE
inline Precision PlanarPolygon::SafetySqr(Vector3D<Precision> const &point, int &closestid) const
{
  using Real_v = vecgeom::VectorBackend::Real_v;
  using vecCore::FromPtr;

  const auto kVectorS = vecCore::VectorSize<Real_v>();
  Precision safe(1E30);
  int isegmin(-1);

  const auto vertx = fVertices.x();
  const auto verty = fVertices.y();
  const auto S     = fVertices.size();
  const Real_v px(point.x());
  const Real_v py(point.y());
  for (size_t i = 0; i < S; i += kVectorS) {
    const Real_v p1[2] = {FromPtr<Real_v>(&vertx[i]), FromPtr<Real_v>(&verty[i])};
    const Real_v p2[2] = {FromPtr<Real_v>(&fShiftedXJ[i]), FromPtr<Real_v>(&fShiftedYJ[i])};

    const auto dx = p2[0] - p1[0];
    const auto dy = p2[1] - p1[1];
    auto dpx      = px - p1[0];
    auto dpy      = py - p1[1];

    // degenerate edge?
    const auto lsq = dx * dx + dy * dy;

    // I don't think this is useful -- its a pure static property
    //         if ( ClostToZero(lsq,0)) {
    //            ssq = dpx*dpx + dpy*dpy;
    //            if (ssq < safe) {
    //               safe = ssq;
    //               isegmin = i;
    //            }
    //            continue;
    //         }

    const auto u     = (dpx * dx + dpy * dy);
    const auto cond1 = (u > lsq);
    const auto cond2 = (!cond1 && (u >= Real_v(0.)));

    if (!vecCore::MaskEmpty(cond1)) {
      vecCore__MaskedAssignFunc(dpx, cond1, px - p2[0]);
      vecCore__MaskedAssignFunc(dpy, cond1, py - p2[1]);
    }
    if (!vecCore::MaskEmpty(cond2)) {
      const auto invlsq = Real_v(1.) / lsq;
      vecCore__MaskedAssignFunc(dpx, cond2, dpx - u * dx * invlsq);
      vecCore__MaskedAssignFunc(dpy, cond2, dpy - u * dy * invlsq);
    }
    const auto ssq = dpx * dpx + dpy * dpy;

// combined reduction is a bit tricky to translate:
// if (ssq < safe) {
//      safe = ssq;
//      isegmin = i;
// }

// a first try is serialized:
#ifndef VECCORE_CUDA
    using std::min;
#endif
    for (size_t j = 0; j < kVectorS; ++j) {
      Precision saftmp = vecCore::LaneAt(ssq, j);
      if (saftmp < safe) {
        safe    = saftmp;
        isegmin = i + j;
      }
    }
    if (Abs(safe) < kTolerance * kTolerance) {
      closestid = isegmin;
      return 0.;
    }
  }
  closestid = isegmin;
  return safe;
}
#endif

} // namespace VECGEOM_IMPL_NAMESPACE
} // end namespace vecgeom

#endif