This file is indexed.

/usr/include/pcl-1.7/pcl/registration/bfgs.h is in libpcl-dev 1.7.2-14build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
#ifndef PCL_FOR_EIGEN_BFGS_H
#define PCL_FOR_EIGEN_BFGS_H

#if defined __GNUC__
#  pragma GCC system_header 
#endif

#include <pcl/registration/eigen.h>

namespace Eigen
{
  template< typename _Scalar >
  class PolynomialSolver<_Scalar,2> : public PolynomialSolverBase<_Scalar,2>
  {
    public:
      typedef PolynomialSolverBase<_Scalar,2>    PS_Base;
      EIGEN_POLYNOMIAL_SOLVER_BASE_INHERITED_TYPES( PS_Base )
        
    public:

      virtual ~PolynomialSolver () {}

      template< typename OtherPolynomial >
      inline PolynomialSolver( const OtherPolynomial& poly, bool& hasRealRoot )
      {
        compute( poly, hasRealRoot );
      }
      
      /** Computes the complex roots of a new polynomial. */
      template< typename OtherPolynomial >
      void compute( const OtherPolynomial& poly, bool& hasRealRoot)
      {
        const Scalar ZERO(0);
        Scalar a2(2 * poly[2]);
        assert( ZERO != poly[poly.size()-1] );
        Scalar discriminant ((poly[1] * poly[1]) - (4 * poly[0] * poly[2]));
        if (ZERO < discriminant)
        {
          Scalar discriminant_root (std::sqrt (discriminant));
          m_roots[0] = (-poly[1] - discriminant_root) / (a2) ;
          m_roots[1] = (-poly[1] + discriminant_root) / (a2) ;
          hasRealRoot = true;
        }
        else {
          if (ZERO == discriminant)
          {
            m_roots.resize (1);
            m_roots[0] = -poly[1] / a2;
            hasRealRoot = true;
          }
          else
          {
            Scalar discriminant_root (std::sqrt (-discriminant));
            m_roots[0] = RootType (-poly[1] / a2, -discriminant_root / a2);
            m_roots[1] = RootType (-poly[1] / a2,  discriminant_root / a2);
            hasRealRoot = false;
          }
        }
      }
      
      template< typename OtherPolynomial >
      void compute( const OtherPolynomial& poly)
      {
        bool hasRealRoot;
        compute(poly, hasRealRoot);
      }

    protected:
      using                   PS_Base::m_roots;
  };
}

template<typename _Scalar, int NX=Eigen::Dynamic>
struct BFGSDummyFunctor
{
  typedef _Scalar Scalar;
  enum { InputsAtCompileTime = NX };
  typedef Eigen::Matrix<Scalar,InputsAtCompileTime,1> VectorType;

  const int m_inputs;

  BFGSDummyFunctor() : m_inputs(InputsAtCompileTime) {}
  BFGSDummyFunctor(int inputs) : m_inputs(inputs) {}

  virtual ~BFGSDummyFunctor() {}
  int inputs() const { return m_inputs; }

  virtual double operator() (const VectorType &x) = 0;
  virtual void  df(const VectorType &x, VectorType &df) = 0;
  virtual void fdf(const VectorType &x, Scalar &f, VectorType &df) = 0;
};

namespace BFGSSpace {
  enum Status {
    NegativeGradientEpsilon = -3,
    NotStarted = -2,
    Running = -1,
    Success = 0,
    NoProgress = 1
  };
}

/**
 * BFGS stands for Broyden–Fletcher–Goldfarb–Shanno (BFGS) method for solving 
 * unconstrained nonlinear optimization problems. 
 * For further details please visit: http://en.wikipedia.org/wiki/BFGS_method
 * The method provided here is almost similar to the one provided by GSL.
 * It reproduces Fletcher's original algorithm in Practical Methods of Optimization
 * algorithms : 2.6.2 and 2.6.4 and uses the same politics in GSL with cubic 
 * interpolation whenever it is possible else falls to quadratic interpolation for 
 * alpha parameter.
 */
template<typename FunctorType>
class BFGS
{
public:
  typedef typename FunctorType::Scalar Scalar;
  typedef typename FunctorType::VectorType FVectorType;

  BFGS(FunctorType &_functor) 
      : pnorm(0), g0norm(0), iter(-1), functor(_functor) {  }

  typedef Eigen::DenseIndex Index;

  struct Parameters {
    Parameters()
    : max_iters(400)
      , bracket_iters(100)
      , section_iters(100)
      , rho(0.01)
      , sigma(0.01)
      , tau1(9)
      , tau2(0.05)
      , tau3(0.5)
      , step_size(1)
      , order(3) {}
    Index max_iters;   // maximum number of function evaluation
    Index bracket_iters;
    Index section_iters;
    Scalar rho;
    Scalar sigma;
    Scalar tau1;
    Scalar tau2;
    Scalar tau3;
    Scalar step_size;
    Index order;
  };

  BFGSSpace::Status minimize(FVectorType &x);
  BFGSSpace::Status minimizeInit(FVectorType &x);
  BFGSSpace::Status minimizeOneStep(FVectorType &x);
  BFGSSpace::Status testGradient(Scalar epsilon);
  void resetParameters(void) { parameters = Parameters(); }
  
  Parameters parameters;
  Scalar f;
  FVectorType gradient;
private:
  
  BFGS& operator=(const BFGS&);
  BFGSSpace::Status lineSearch (Scalar rho, Scalar sigma, 
                                Scalar tau1, Scalar tau2, Scalar tau3,
                                int order, Scalar alpha1, Scalar &alpha_new);
  Scalar interpolate (Scalar a, Scalar fa, Scalar fpa,
                      Scalar b, Scalar fb, Scalar fpb, Scalar xmin, Scalar xmax,
                      int order);  
  void checkExtremum (const Eigen::Matrix<Scalar, 4, 1>& coefficients, Scalar x, Scalar& xmin, Scalar& fmin);
  void moveTo (Scalar alpha);
  Scalar slope ();
  Scalar applyF (Scalar alpha);
  Scalar applyDF (Scalar alpha);
  void applyFDF (Scalar alpha, Scalar &f, Scalar &df);
  void updatePosition (Scalar alpha, FVectorType& x, Scalar& f, FVectorType& g);
  void changeDirection ();
  
  Scalar delta_f, fp0;
  FVectorType x0, dx0, dg0, g0, dx, p;
  Scalar pnorm, g0norm;

  Scalar f_alpha;
  Scalar df_alpha;
  FVectorType x_alpha;
  FVectorType g_alpha;
  
  // cache "keys"
  Scalar f_cache_key;
  Scalar df_cache_key;
  Scalar x_cache_key;
  Scalar g_cache_key;

  Index iter;
  FunctorType &functor;
};


template<typename FunctorType> void
BFGS<FunctorType>::checkExtremum(const Eigen::Matrix<Scalar, 4, 1>& coefficients, Scalar x, Scalar& xmin, Scalar& fmin)
{
  Scalar y = Eigen::poly_eval(coefficients, x);
  if(y < fmin) { xmin = x; fmin = y; }
}

template<typename FunctorType> void
BFGS<FunctorType>::moveTo(Scalar alpha)
{
  x_alpha = x0 + alpha * p;
  x_cache_key = alpha;
}

template<typename FunctorType> typename BFGS<FunctorType>::Scalar
BFGS<FunctorType>::slope()
{
  return (g_alpha.dot (p));
}

template<typename FunctorType> typename BFGS<FunctorType>::Scalar
BFGS<FunctorType>::applyF(Scalar alpha)
{
  if (alpha == f_cache_key) return f_alpha;
  moveTo (alpha);
  f_alpha = functor (x_alpha);
  f_cache_key = alpha;
  return (f_alpha);
}

template<typename FunctorType> typename BFGS<FunctorType>::Scalar
BFGS<FunctorType>::applyDF(Scalar alpha)
{
  if (alpha == df_cache_key) return df_alpha;
  moveTo (alpha);
  if(alpha != g_cache_key)
  {
    functor.df (x_alpha, g_alpha);
    g_cache_key = alpha;
  }
  df_alpha = slope ();
  df_cache_key = alpha;
  return (df_alpha);
}

template<typename FunctorType> void
BFGS<FunctorType>::applyFDF(Scalar alpha, Scalar& f, Scalar& df)
{
  if(alpha == f_cache_key && alpha == df_cache_key)
  {
    f = f_alpha;
    df = df_alpha;
    return;
  }

  if(alpha == f_cache_key || alpha == df_cache_key)
  {
    f = applyF (alpha);
    df = applyDF (alpha);
    return;
  }

  moveTo (alpha);
  functor.fdf (x_alpha, f_alpha, g_alpha);
  f_cache_key = alpha;
  g_cache_key = alpha;
  df_alpha = slope ();
  df_cache_key = alpha;
  f = f_alpha;
  df = df_alpha;
}

template<typename FunctorType> void
BFGS<FunctorType>::updatePosition (Scalar alpha, FVectorType &x, Scalar &f, FVectorType &g)
{
  { 
    Scalar f_alpha, df_alpha; 
    applyFDF (alpha, f_alpha, df_alpha); 
  } ;

  f = f_alpha;
  x = x_alpha;
  g = g_alpha;
}  

template<typename FunctorType> void
BFGS<FunctorType>::changeDirection ()
{
  x_alpha = x0;
  x_cache_key = 0.0;
  f_cache_key = 0.0;
  g_alpha = g0;
  g_cache_key = 0.0;
  df_alpha = slope ();
  df_cache_key = 0.0;
}

template<typename FunctorType> BFGSSpace::Status
BFGS<FunctorType>::minimize(FVectorType  &x)
{
  BFGSSpace::Status status = minimizeInit(x);
  do {
    status = minimizeOneStep(x);
    iter++;
  } while (status==BFGSSpace::Success && iter < parameters.max_iters);
  return status;
}

template<typename FunctorType> BFGSSpace::Status
BFGS<FunctorType>::minimizeInit(FVectorType  &x)
{
  iter = 0;
  delta_f = 0;
  dx.setZero ();
  functor.fdf(x, f, gradient);
  x0 = x;
  g0 = gradient;
  g0norm = g0.norm ();
  p = gradient * -1/g0norm;
  pnorm = p.norm ();
  fp0 = -g0norm;

  {
    x_alpha = x0; x_cache_key = 0;
    
    f_alpha = f; f_cache_key = 0;
    
    g_alpha = g0; g_cache_key = 0;
    
    df_alpha = slope (); df_cache_key = 0;
  }

  return BFGSSpace::NotStarted;
}

template<typename FunctorType> BFGSSpace::Status
BFGS<FunctorType>::minimizeOneStep(FVectorType  &x)
{
  Scalar alpha = 0.0, alpha1;
  Scalar f0 = f;
  if (pnorm == 0.0 || g0norm == 0.0 || fp0 == 0)
  {
    dx.setZero ();
    return BFGSSpace::NoProgress;
  }

  if (delta_f < 0)
  {
    Scalar del = std::max (-delta_f, 10 * std::numeric_limits<Scalar>::epsilon() * fabs(f0));
    alpha1 = std::min (1.0, 2.0 * del / (-fp0));
  }
  else
    alpha1 = fabs(parameters.step_size);

  BFGSSpace::Status status = lineSearch(parameters.rho, parameters.sigma, 
                                        parameters.tau1, parameters.tau2, parameters.tau3, 
                                        parameters.order, alpha1, alpha);

  if(status != BFGSSpace::Success)
    return status;

  updatePosition(alpha, x, f, gradient);

  delta_f = f - f0;

  /* Choose a new direction for the next step */
  {
    /* This is the BFGS update: */
    /* p' = g1 - A dx - B dg */
    /* A = - (1+ dg.dg/dx.dg) B + dg.g/dx.dg */
    /* B = dx.g/dx.dg */

    Scalar dxg, dgg, dxdg, dgnorm, A, B;

    /* dx0 = x - x0 */
    dx0 = x - x0;
    dx = dx0; /* keep a copy */

    /* dg0 = g - g0 */
    dg0 = gradient - g0;
    dxg = dx0.dot (gradient);
    dgg = dg0.dot (gradient);
    dxdg = dx0.dot (dg0);
    dgnorm = dg0.norm ();

    if (dxdg != 0)
    {
      B = dxg / dxdg;
      A = -(1.0 + dgnorm * dgnorm / dxdg) * B + dgg / dxdg;
    }
    else
    {
      B = 0;
      A = 0;
    }

    p = -A * dx0;
    p+= gradient;
    p+= -B * dg0 ;
  }

  g0 = gradient;
  x0 = x;
  g0norm = g0.norm ();
  pnorm = p.norm ();
  
  Scalar dir = ((p.dot (gradient)) > 0) ? -1.0 : 1.0;
  p*= dir / pnorm;
  pnorm = p.norm ();
  fp0 = p.dot (g0);

  changeDirection();
  return BFGSSpace::Success;
}

template<typename FunctorType> typename BFGSSpace::Status 
BFGS<FunctorType>::testGradient(Scalar epsilon)
{
  if(epsilon < 0)
    return BFGSSpace::NegativeGradientEpsilon;
  else
  {
    if(gradient.norm () < epsilon)
      return BFGSSpace::Success;
    else
      return BFGSSpace::Running;
  }
}

template<typename FunctorType> typename BFGS<FunctorType>::Scalar 
BFGS<FunctorType>::interpolate (Scalar a, Scalar fa, Scalar fpa,
                                Scalar b, Scalar fb, Scalar fpb, 
                                Scalar xmin, Scalar xmax,
                                int order)
{
  /* Map [a,b] to [0,1] */
  Scalar y, alpha, ymin, ymax, fmin;

  ymin = (xmin - a) / (b - a);
  ymax = (xmax - a) / (b - a);
  
  // Ensure ymin <= ymax
  if (ymin > ymax) { Scalar tmp = ymin; ymin = ymax; ymax = tmp; };

  if (order > 2 && !(fpb != fpb) && fpb != std::numeric_limits<Scalar>::infinity ()) 
  {
    fpa = fpa * (b - a);
    fpb = fpb * (b - a);

    Scalar eta = 3 * (fb - fa) - 2 * fpa - fpb;
    Scalar xi = fpa + fpb - 2 * (fb - fa);
    Scalar c0 = fa, c1 = fpa, c2 = eta, c3 = xi;
    Scalar y0, y1;
    Eigen::Matrix<Scalar, 4, 1> coefficients;
    coefficients << c0, c1, c2, c3;
    
    y = ymin; 
    // Evaluate the cubic polyinomial at ymin;
    fmin = Eigen::poly_eval (coefficients, ymin);
    checkExtremum (coefficients, ymax, y, fmin);
    {
      // Solve quadratic polynomial for the derivate
      Eigen::Matrix<Scalar, 3, 1> coefficients2;
      coefficients2 << c1, 2 * c2, 3 * c3;
      bool real_roots;
      Eigen::PolynomialSolver<Scalar, 2> solver (coefficients2, real_roots);
      if(real_roots)
      {
        if ((solver.roots ()).size () == 2)  /* found 2 roots */
        {
          y0 = std::real (solver.roots () [0]);
          y1 = std::real (solver.roots () [1]);
          if(y0 > y1) { Scalar tmp (y0); y0 = y1; y1 = tmp; }
          if (y0 > ymin && y0 < ymax) 
            checkExtremum (coefficients, y0, y, fmin);
          if (y1 > ymin && y1 < ymax) 
            checkExtremum (coefficients, y1, y, fmin);
        }
        else if ((solver.roots ()).size () == 1)  /* found 1 root */
        {
          y0 = std::real (solver.roots () [0]);
          if (y0 > ymin && y0 < ymax) 
            checkExtremum (coefficients, y0, y, fmin);
        }
      }
    }
  } 
  else 
  {
    fpa = fpa * (b - a);
    Scalar fl = fa + ymin*(fpa + ymin*(fb - fa -fpa));
    Scalar fh = fa + ymax*(fpa + ymax*(fb - fa -fpa));
    Scalar c = 2 * (fb - fa - fpa);       /* curvature */
    y = ymin; fmin = fl;
    
    if (fh < fmin) { y = ymax; fmin = fh; } 
    
    if (c > a)  /* positive curvature required for a minimum */
    {
      Scalar z = -fpa / c;      /* location of minimum */
      if (z > ymin && z < ymax) {
        Scalar f = fa + z*(fpa + z*(fb - fa -fpa));
        if (f < fmin) { y = z; fmin = f; };
      }
    }
  }
  
  alpha = a + y * (b - a);
  return alpha;
}

template<typename FunctorType> BFGSSpace::Status 
BFGS<FunctorType>::lineSearch(Scalar rho, Scalar sigma, 
                              Scalar tau1, Scalar tau2, Scalar tau3,
                              int order, Scalar alpha1, Scalar &alpha_new)
{
  Scalar f0, fp0, falpha, falpha_prev, fpalpha, fpalpha_prev, delta, alpha_next;
  Scalar alpha = alpha1, alpha_prev = 0.0;
  Scalar a, b, fa, fb, fpa, fpb;
  Index i = 0;

  applyFDF (0.0, f0, fp0);

  falpha_prev = f0;
  fpalpha_prev = fp0;

  /* Avoid uninitialized variables morning */
  a = 0.0; b = alpha;
  fa = f0; fb = 0.0;
  fpa = fp0; fpb = 0.0;

  /* Begin bracketing */  

  while (i++ < parameters.bracket_iters)
  {
    falpha = applyF (alpha);

    if (falpha > f0 + alpha * rho * fp0 || falpha >= falpha_prev)
    {
      a = alpha_prev; fa = falpha_prev; fpa = fpalpha_prev;
      b = alpha; fb = falpha; fpb = std::numeric_limits<Scalar>::quiet_NaN ();
      break;
    } 

    fpalpha = applyDF (alpha);

    /* Fletcher's sigma test */
    if (fabs (fpalpha) <= -sigma * fp0)
    {
      alpha_new = alpha;
      return BFGSSpace::Success;
    }

    if (fpalpha >= 0)
    {
      a = alpha; fa = falpha; fpa = fpalpha;
      b = alpha_prev; fb = falpha_prev; fpb = fpalpha_prev;
      break;                /* goto sectioning */
    }

    delta = alpha - alpha_prev;

    {
      Scalar lower = alpha + delta;
      Scalar upper = alpha + tau1 * delta;

      alpha_next = interpolate (alpha_prev, falpha_prev, fpalpha_prev,
                                alpha, falpha, fpalpha, lower, upper, order);

    }

    alpha_prev = alpha;
    falpha_prev = falpha;
    fpalpha_prev = fpalpha;
    alpha = alpha_next;
  }
  /*  Sectioning of bracket [a,b] */
  while (i++ < parameters.section_iters)
  {
    delta = b - a;
      
    {
      Scalar lower = a + tau2 * delta;
      Scalar upper = b - tau3 * delta;
        
      alpha = interpolate (a, fa, fpa, b, fb, fpb, lower, upper, order);
    }
    falpha = applyF (alpha);
    if ((a-alpha)*fpa <= std::numeric_limits<Scalar>::epsilon ()) {
      /* roundoff prevents progress */
      return BFGSSpace::NoProgress;
    };

    if (falpha > f0 + rho * alpha * fp0 || falpha >= fa)
    {
      /*  a_next = a; */
      b = alpha; fb = falpha; fpb = std::numeric_limits<Scalar>::quiet_NaN ();
    }
    else
    {
      fpalpha = applyDF (alpha);
          
      if (fabs(fpalpha) <= -sigma * fp0)
      {
        alpha_new = alpha;
        return BFGSSpace::Success;  /* terminate */
      }
          
      if ( ((b-a) >= 0 && fpalpha >= 0) || ((b-a) <=0 && fpalpha <= 0))
      {
        b = a; fb = fa; fpb = fpa;
        a = alpha; fa = falpha; fpa = fpalpha;
      }
      else
      {
        a = alpha; fa = falpha; fpa = fpalpha;
      }
    }
  }
  return BFGSSpace::Success;
}
#endif // PCL_FOR_EIGEN_BFGS_H