This file is indexed.

/usr/include/CGAL/Polytope_distance_d.h is in libcgal-dev 4.7-4.

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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
// Copyright (c) 1997-2001  ETH Zurich (Switzerland).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
// You can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL$
// $Id$
// 
//
// Author(s)     : Sven Schoenherr <sven@inf.ethz.ch>

#ifndef CGAL_POLYTOPE_DISTANCE_D_H
#define CGAL_POLYTOPE_DISTANCE_D_H

// includes
// --------
#include <CGAL/Optimisation/basic.h>
#include <CGAL/function_objects.h>

#include <CGAL/QP_options.h>
#include <CGAL/QP_solver/QP_solver.h>
#include <CGAL/QP_models.h>
#include <CGAL/QP_solver/functors.h>
#include <CGAL/QP_solver/QP_full_filtered_pricing.h>
#include <CGAL/QP_solver/QP_full_exact_pricing.h>

namespace CGAL {

// To determine the polytope distance, we set up the following QP:
// 
// We let v-w be nonnegative vectors such that
// v-w = p-q, where p and q are the points that realize the
// optimal distance. That way, we obtain a nonnegative QP
// 
//
//min (v-w)^T(w-v)
//     v-w - \sum x_i p_i + \sum y_j q_j    = 0
//           \sum x_i                       = 1
//                           \sum y_j       = 1
//     v, w, x, y >= 0

namespace PD_detail {
  // The functors necessary to realize access to A
  // 
  // A has the form
  //    I  -I    -P    Q
  //    0   0     1    0
  //    0   0     0    1
  // where  I and -I are blocks of size d*d, and the 0's and 1's are
  // rows vectors of 0's and 1's 
  //
  // we have one functor for a fixed column, and one functor for the
  // whole matrix

  // functor for a fixed column of A
  template <class NT, class Iterator>
  class A_column : public std::unary_function <int, NT>
  {
  public:
    typedef NT result_type;
    A_column()
    {}
  
    A_column (int j, int d, bool in_p, Iterator it)
      : j_ (j), d_ (d), in_p_ (in_p), it_ (it), nt_0_ (0), nt_1_ (1)
    {}

    result_type operator() (int i) const
    {
      if (j_ < d_) {
	// column for v
	return (i == j_ ? nt_1_ : nt_0_);
      }
      if (j_ < 2*d_) {
	// column for w
	return (i == j_-d_ ? -nt_1_ : nt_0_);
      }
      // point column
      if (in_p_) {
	// column for P
	if (i < d_) return -(*(it_ + i));
	if (i == d_) return *(it_ + d_); // homogenizing coordinate
	return nt_0_;
      } else {
	// column for Q
	if (i < d_) return (*(it_ + i));
	if (i == d_+1) return *(it_ + d_); // homogenizing coordinate
	return nt_0_;
      }
      // never get here
    }
    
  private:
    int j_;                  // column number
    int d_;                  // dimension
    bool in_p_;              // point in P ?
    Iterator it_;            // the iterator through the column's point
    NT nt_0_;
    NT nt_1_; 
  };
  
  // functor for matrix A
  template <class NT, class Access_coordinate_begin_d,
	    class Point_iterator >
  class A_matrix : public std::unary_function
  <int, boost::transform_iterator <A_column
    <NT, typename Access_coordinate_begin_d::Coordinate_iterator>, 
				   boost::counting_iterator<int> > >
  { 
    typedef PD_detail::A_column
    <NT, typename Access_coordinate_begin_d::Coordinate_iterator> A_column;
  public:
    typedef  boost::transform_iterator
    <A_column, boost::counting_iterator<int> > result_type;
    
    A_matrix ()
    {}

    A_matrix (int d, 
	      Access_coordinate_begin_d da_coord,
	      Point_iterator P, 
	      int r, 
	      Point_iterator Q) 
      : d_ (d), da_coord_ (da_coord), P_ (P), r_ (r), Q_ (Q)
    {}

    result_type operator () (int j) const
    { 
      if (j < 2*d_) {
	// column of v or w
	return result_type
	  (0, A_column (j, d_, false  /*dummy*/, 
			da_coord_ (*P_) /*dummy*/));
      }
      if (j < 2*d_+r_) {
	// column of P
	return result_type
	  (0, A_column (j , d_, true, da_coord_ (*(P_+(j-2*d_)))));
      }
      // column of Q
      return result_type
	(0, A_column (j, d_, false, da_coord_ (*(Q_+(j-2*d_-r_))))); 
    }

  private:
    int d_;                  // dimension
    Access_coordinate_begin_d da_coord_; // data accessor
    Point_iterator P_;       // point set P
    int r_;                  // size of point set P
    Point_iterator Q_;       // point set Q
  };

  // The functor necessary to realize access to b
  // b has the form
  // 0
  // 0
  // 1  (row d)
  // 1  (row d+1)

  template <class NT>
  class B_vector : public std::unary_function<int, NT>
  {
  public:
    typedef NT result_type;
    B_vector()
    {}

    B_vector (int d)
      : d_ (d), nt_0_ (0), nt_1_ (1)
    {}

    result_type operator() (int i) const
    {
      return ( (i == d_ || i == d_+1) ? nt_1_ : nt_0_);
    }

  private:
    int d_;
    NT nt_0_;
    NT nt_1_;
  };


  // The functors necessary to realize access to D
  // 
  // D has the form
  //   I  -I    0
  //  -I   I    0
  //   0   0    0
  // where all I and -I are blocks of size d*d
  //
  // we have one functor for a fixed row, and one functor for the
  // whole matrix

  // functor for a fixed row of D; note that we have to return 2D in
  // order to please the QP_solver
  template <class NT> 
  class D_row : public std::unary_function <int, NT> 
  {
  public:
    typedef NT result_type;
    D_row () 
    {}
    D_row (int i, int d) 
      : i_ (i), d_ (d), nt_0_ (0), nt_2_ (2)
    {}
    
    result_type operator () (int j) const
    {
      if (j < d_) {
	//  I ( 1 iff i = j)
	// -I (-1 iff i = j + d)
	//  0
	if (i_ == j) return nt_2_;
	if (i_ == j + d_) return -nt_2_;
	return nt_0_;
      }
      if (j < 2*d_) {
	// -I (-1 iff i = j - d)
	//  I ( 1 iff i = j)
	//  0
	if (i_ == j - d_) return -nt_2_;
	if (i_ == j) return nt_2_;
	return nt_0_;
      }
      // 0
      // 0
      // 0
      return nt_0_;
    }
  private:
    int i_; // row number
    int d_; // dimension
    NT nt_0_;
    NT nt_2_;
  };

  // functor for matrix D
  template <class NT>
  class D_matrix : public std::unary_function
  <int, boost::transform_iterator<D_row<NT>,
				  boost::counting_iterator<int> > >
  { 
  public:
    typedef boost::transform_iterator<D_row<NT>,
	    boost::counting_iterator<int> > result_type; 
    D_matrix ()
    {}
    D_matrix (int d)
      :  d_ (d)
    {}

    result_type operator()(int i) const 
    {
      return result_type (0, D_row<NT>(i, d_));
    }

  private:
    int d_; // dimension
  };
}

// Class interfaces
// ================
template < class Traits_ >
class Polytope_distance_d {
public:
  // self
  typedef  Traits_                    Traits;
  typedef  Polytope_distance_d<Traits>
  Self;

  // types from the traits class
  typedef  typename Traits::Point_d   Point;

  typedef  typename Traits::Rep_tag   Rep_tag;

  typedef  typename Traits::RT        RT;
  typedef  typename Traits::FT        FT;

  typedef  typename Traits::Access_dimension_d
  Access_dimension_d;
  typedef  typename Traits::Access_coordinates_begin_d
  Access_coordinates_begin_d;

  typedef  typename Traits::Construct_point_d
  Construct_point_d;

  typedef  typename Traits::ET        ET;
  typedef  typename Traits::NT        NT; 

private:
  // private types
  typedef  std::vector<Point>         Point_vector;
  typedef  std::vector<ET>            ET_vector;
    
  typedef  QP_access_by_index
  <typename std::vector<Point>::const_iterator, int> Point_by_index;
    
  typedef  std::vector<NT>            NT_vector;
  typedef  std::vector<NT_vector>     NT_matrix;
  
  typedef  std::vector<int>           Index_vector;
public:    
  // public types
  typedef  typename Point_vector::const_iterator
  Point_iterator;
    
  typedef typename Index_vector::const_iterator IVCI;
  typedef CGAL::Join_input_iterator_1< IVCI, Point_by_index >
  Support_point_iterator;

  typedef typename Index_vector::const_iterator
  Support_point_index_iterator;
    
  typedef  typename ET_vector::const_iterator
  Coordinate_iterator;

private:
  
  // QP solver iterator types
  typedef PD_detail::A_matrix <NT, Access_coordinates_begin_d,
			       Point_iterator> A_matrix;
  typedef boost::transform_iterator<
    A_matrix, boost::counting_iterator<int> > A_iterator;

  typedef PD_detail::B_vector <NT> B_vector;
  typedef boost::transform_iterator<
    B_vector, boost::counting_iterator<int> > B_iterator;

  typedef CGAL::Const_oneset_iterator<CGAL::Comparison_result> R_iterator;  
  typedef CGAL::Const_oneset_iterator<NT> C_iterator; 

  typedef PD_detail::D_matrix <NT> D_matrix;
  typedef boost::transform_iterator <
    D_matrix, boost::counting_iterator<int> > D_iterator;

  // Program type
  typedef CGAL::Nonnegative_quadratic_program_from_iterators
      <A_iterator, B_iterator, R_iterator, D_iterator, C_iterator> QP;

  // Tags
  typedef QP_solver_impl::QP_tags <Tag_false, Tag_true> QP_tags;

  // Solver types
  typedef  CGAL::QP_solver <QP, ET, QP_tags > Solver;

  typedef  typename Solver::Pricing_strategy Pricing_strategy;

public:    

  // creation
  Polytope_distance_d( const Traits&  traits  = Traits())
    : nt_0(0), nt_1(1),
      tco( traits), da_coord (tco.access_coordinates_begin_d_object()),
      d( -1), solver(0) 
  {}
    
  template < class InputIterator1, class InputIterator2 >
  Polytope_distance_d( InputIterator1 p_first,
		       InputIterator1 p_last,
		       InputIterator2 q_first,
		       InputIterator2 q_last,
		       const Traits&  traits = Traits())
    : nt_0(0), nt_1(1), 
      tco( traits), da_coord (tco.access_coordinates_begin_d_object()),
      solver(0)
  {
    set( p_first, p_last, q_first, q_last);
  }

  ~Polytope_distance_d() {    
    if (solver)
      delete solver;
  }

  // access to point sets
  int  ambient_dimension( ) const { return d; }
    
  int  number_of_points( ) const { return static_cast<int>(p_points.size()+q_points.size());}
    
  int  number_of_points_p( ) const { return static_cast<int>(p_points.size()); }
  int  number_of_points_q( ) const { return static_cast<int>(q_points.size()); }
    
  Point_iterator  points_p_begin( ) const { return p_points.begin(); }
  Point_iterator  points_p_end  ( ) const { return p_points.end  (); }
    
  Point_iterator  points_q_begin( ) const { return q_points.begin(); }
  Point_iterator  points_q_end  ( ) const { return q_points.end  (); }
    
  // access to support points
  int
  number_of_support_points( ) const
  { return is_finite() ? 
      static_cast<int>(p_support_indices.size()+q_support_indices.size()) : 0; }
    
  int  number_of_support_points_p() const { return static_cast<int>(p_support_indices.size());}
  int  number_of_support_points_q() const { return static_cast<int>(q_support_indices.size());}
    
  Support_point_iterator
  support_points_p_begin() const
  { return Support_point_iterator(
				  p_support_indices.begin(),
				  Point_by_index( p_points.begin())); }
    
  Support_point_iterator
  support_points_p_end() const
  { return Support_point_iterator(
				  is_finite() ? p_support_indices.end()
				  : p_support_indices.begin(),
				  Point_by_index( p_points.begin())); }
    
  Support_point_iterator
  support_points_q_begin() const
  { return Support_point_iterator(
				  q_support_indices.begin(),
				  Point_by_index( q_points.begin())); }
    
  Support_point_iterator
  support_points_q_end() const
  { return Support_point_iterator(
				  is_finite() ? q_support_indices.end()
				  : q_support_indices.begin(),
				  Point_by_index( q_points.begin())); }

  Support_point_index_iterator
  support_points_p_indices_begin() const
  { return p_support_indices.begin(); }
    
  Support_point_index_iterator
  support_points_p_indices_end() const
  { return p_support_indices.end(); }

  Support_point_index_iterator
  support_points_q_indices_begin() const
  { return q_support_indices.begin(); }

  Support_point_index_iterator
  support_points_q_indices_end() const
  { return q_support_indices.end(); }
    
  // access to realizing points (rational representation)
  Coordinate_iterator
  realizing_point_p_coordinates_begin( ) const { return p_coords.begin(); }
    
  Coordinate_iterator
  realizing_point_p_coordinates_end  ( ) const { return p_coords.end  (); }
    
  Coordinate_iterator
  realizing_point_q_coordinates_begin( ) const { return q_coords.begin(); }
    
  Coordinate_iterator
  realizing_point_q_coordinates_end  ( ) const { return q_coords.end  (); }
    
  // access to squared distance (rational representation)
  ET  squared_distance_numerator  ( ) const
  { return solver->solution_numerator(); }
    
  ET  squared_distance_denominator( ) const
  { return solver->solution_denominator(); }
    
  // access to realizing points and squared distance
  // NOTE: an implicit conversion from ET to RT must be available!
  Point
  realizing_point_p( ) const
  { CGAL_optimisation_precondition( is_finite());
  return tco.construct_point_d_object()
    ( ambient_dimension(),
      realizing_point_p_coordinates_begin(),
      realizing_point_p_coordinates_end  ()); }
    
  Point
  realizing_point_q( ) const
  { CGAL_optimisation_precondition( is_finite());
  return tco.construct_point_d_object()
    ( ambient_dimension(),
      realizing_point_q_coordinates_begin(),
      realizing_point_q_coordinates_end  ()); }
    
  FT
  squared_distance( ) const
  { 
    return FT( squared_distance_numerator  ()) /
      FT( squared_distance_denominator()); }
    
  bool  is_finite( ) const
  { return ( number_of_points_p() > 0) && ( number_of_points_q() > 0); }
    
  bool  is_zero( ) const
  { return CGAL_NTS is_zero( squared_distance_numerator()); }
    
  bool  is_degenerate( ) const { return ( ! is_finite()); }
    
  // modifiers
  template < class InputIterator1, class InputIterator2 >
  void
  set( InputIterator1 p_first, InputIterator1 p_last,
       InputIterator2 q_first, InputIterator2 q_last)
  { 
    p_points.clear();
    q_points.clear();
    std::copy( p_first, p_last, std::back_inserter( p_points));
    std::copy( q_first, q_last, std::back_inserter( q_points));
    set_dimension();
    CGAL_optimisation_precondition_msg
      (check_dimension( p_points.begin(), p_points.end())
       && check_dimension( q_points.begin(), q_points.end()),
       "Not all points have the same dimension.");

    compute_distance(); 
  }
    
  template < class InputIterator >
  void
  set_p( InputIterator p_first, InputIterator p_last)
  { 
    p_points.clear();
    std::copy( p_first, p_last, std::back_inserter( p_points));
    set_dimension();
    CGAL_optimisation_precondition_msg
      (check_dimension( p_points.begin(), p_points.end()),
       "Not all points have the same dimension.");

    compute_distance(); 
  }
    
  template < class InputIterator >
  void
  set_q( InputIterator q_first, InputIterator q_last)
  {
    q_points.clear();
    std::copy( q_first, q_last, std::back_inserter( q_points));
    set_dimension();
    CGAL_optimisation_precondition_msg
      (check_dimension( q_points.begin(), q_points.end()),
       "Not all points have the same dimension.");
  
    compute_distance(); 
  }
    
  void
  insert_p( const Point& p)
  { 
    CGAL_optimisation_precondition
      ( ( ! is_finite()) ||
	( tco.access_dimension_d_object()( p) == d));
    p_points.push_back( p);
    set_dimension(); // it might no longer be -1 
    compute_distance(); 
  }
    
  void
  insert_q( const Point& q)
  { 
    CGAL_optimisation_precondition
      ( ( ! is_finite()) ||
	( tco.access_dimension_d_object()( q) == d));
    q_points.push_back( q); 
    set_dimension(); // it might no longer be -1 
    compute_distance(); 
  }
    
  template < class InputIterator1, class InputIterator2 >
  void
  insert( InputIterator1 p_first, InputIterator1 p_last,
	  InputIterator2 q_first, InputIterator2 q_last)
  { 
    CGAL_optimisation_precondition_code(int old_r = static_cast<int>(p_points.size()));
    CGAL_optimisation_precondition_code(int old_s = static_cast<int>(q_points.size()));
    p_points.insert( p_points.end(), p_first, p_last);
    q_points.insert( q_points.end(), q_first, q_last);
    set_dimension();
    CGAL_optimisation_precondition_msg
      (check_dimension( p_points.begin()+old_r, p_points.end())
       && check_dimension( q_points.begin()+old_s, q_points.end()),
       "Not all points have the same dimension.");
    compute_distance(); 
  }
    
  template < class InputIterator >
  void
  insert_p( InputIterator p_first, InputIterator p_last)
  { 
    CGAL_optimisation_precondition_code(int old_r = static_cast<int>(p_points.size()));
    p_points.insert( p_points.end(), p_first, p_last);
    set_dimension();
    CGAL_optimisation_precondition_msg
      (check_dimension( p_points.begin()+old_r, p_points.end()),
       "Not all points have the same dimension.");
    compute_distance(); 
  }
    
  template < class InputIterator >
  void
  insert_q( InputIterator q_first, InputIterator q_last)
  { 
    CGAL_optimisation_precondition_code( int old_s = static_cast<int>(q_points.size()));
    q_points.insert( q_points.end(), q_first, q_last);
    set_dimension();
    CGAL_optimisation_precondition_msg
      (check_dimension( q_points.begin()+old_s, q_points.end()),
       "Not all points have the same dimension.");
    compute_distance(); 
  }
    
  void
  clear( )
  { 
    p_points.clear();
    q_points.clear();
    compute_distance(); 
  }
    
  // validity check
  bool  is_valid( bool verbose = false, int level = 0) const;
    
  // traits class access
  const Traits&  traits( ) const { return tco; }
    

private:
  NT nt_0;
  NT nt_1;

  Traits                     tco;        // traits class object
  Access_coordinates_begin_d da_coord;   // data accessor object
    
  Point_vector             p_points;  // points of P
  Point_vector             q_points;  // points of Q
  int                      d;         // dimension of input points
    
  ET_vector                p_coords;          // realizing point of P
  ET_vector                q_coords;          // realizing point of Q
    
  Solver                   *solver;    // quadratic programming solver
    
  Index_vector             p_support_indices;
  Index_vector             q_support_indices;
    
private:    
  // set dimension of input points
  void
  set_dimension( )
  { 
    d = ( p_points.size() > 0 ?
	  tco.access_dimension_d_object()( p_points[ 0]) :
	  q_points.size() > 0 ?
	  tco.access_dimension_d_object()( q_points[ 0]) :
	  -1); 
  }
    
  // check dimension of input points
  template < class InputIterator >
  bool
  check_dimension( InputIterator first, InputIterator last)
  { return ( std::find_if
	     ( first, last,
	       CGAL::compose1_1
	       ( std::bind2nd(std::not_equal_to<int>(), d),
		 tco.access_dimension_d_object()))
	     == last); }
    
  // compute (squared) distance
  void
  compute_distance( )
  {
    // clear support points
    p_support_indices.clear();
    q_support_indices.clear();
    if ( ( p_points.size() == 0) || ( q_points.size() == 0)) return;
        
    // construct program
    int n = 2 * d + static_cast<int>(p_points.size() + q_points.size());
    int m = d + 2;
    CGAL_optimisation_precondition (p_points.size() > 0);
    QP qp (n, m, 
	   A_iterator 
	   (boost::counting_iterator<int>(0), 
	    A_matrix (d, da_coord, p_points.begin(), static_cast<int>(p_points.size()), 
			 q_points.begin())),
	   B_iterator (boost::counting_iterator<int>(0), B_vector (d)), 
	   R_iterator (CGAL::EQUAL), 
	   D_iterator (boost::counting_iterator<int>(0), D_matrix (d)),
	   C_iterator (nt_0));

    delete solver;
    Quadratic_program_options options;
    options.set_pricing_strategy(pricing_strategy(NT()));
    solver = new Solver(qp, options);
    CGAL_optimisation_assertion(solver->status() == QP_OPTIMAL);
    // compute support and realizing points
    ET  et_0 = 0;
    int r = static_cast<int>(p_points.size());
    p_coords.resize( ambient_dimension()+1);
    q_coords.resize( ambient_dimension()+1);
    std::fill( p_coords.begin(), p_coords.end(), et_0);
    std::fill( q_coords.begin(), q_coords.end(), et_0);
    for (int i = 0; i < solver->number_of_basic_original_variables(); ++i) {
      ET  value = solver->basic_original_variables_numerator_begin()[ i];
      int index = solver->basic_original_variable_indices_begin()[ i];
      if (index < 2*d) continue; // v or w variable
      if (index < 2*d + r) {
	// a point of p
	for ( int j = 0; j < d; ++j) {
	  p_coords[ j]
	    += value * ET(da_coord (p_points[ index-2*d ])[ j]);
	}
	p_support_indices.push_back( index-2*d);
      } else {
	// a point of q
	for ( int j = 0; j < d; ++j) {
	  q_coords[ j]
	    += value * ET(da_coord (q_points[ index-2*d-r])[ j]);
	}
	q_support_indices.push_back( index-2*d-r);
      }
    }
    p_coords[ d] = q_coords[ d] = solver->variables_common_denominator();
  }
    
  template < class NT >
  Quadratic_program_pricing_strategy pricing_strategy( NT) {
    return QP_PARTIAL_FILTERED_DANTZIG;
  }
  
  Quadratic_program_pricing_strategy pricing_strategy( ET) {
    return QP_PARTIAL_DANTZIG;
  }
    
};

// Function declarations
// =====================
// output operator
template < class Traits_ >
std::ostream&
operator << ( std::ostream& os,
              const Polytope_distance_d<Traits_>& poly_dist);


// ============================================================================

// Class implementation
// ====================

// validity check
template < class Traits_ >
bool
Polytope_distance_d<Traits_>::
is_valid( bool verbose, int level) const
{
  using namespace std;

  CGAL::Verbose_ostream verr( verbose);
  verr << "CGAL::Polytope_distance_d<Traits>::" << endl;
  verr << "is_valid( true, " << level << "):" << endl;
  verr << "  |P+Q| = " << number_of_points_p()
       <<          '+' << number_of_points_q()
       <<   ", |S| = " << number_of_support_points_p()
       <<          '+' << number_of_support_points_q() << endl;

  if ( is_finite()) {

    // compute normal vector
    ET_vector  normal( d), diff( d);
    ET  et_0 = 0, den = p_coords[d];
    CGAL_optimisation_assertion (den > et_0);
    CGAL_optimisation_assertion (den == q_coords[d]);
    int i, j;
    for ( j = 0; j < d; ++j) normal[ j] = p_coords[ j] - q_coords[ j];

    // check correctness of computed squared distance
    verr << "  checking squared_distance..." << flush;
    ET sqr_dist_num (0);
    for ( j = 0; j < d; ++j)
      sqr_dist_num += normal[ j] * normal[ j];
    ET sqr_dist_den = 
      p_coords[ d] * p_coords[ d];
    if (sqr_dist_num * squared_distance_denominator() !=
	sqr_dist_den * squared_distance_numerator())
      return CGAL::_optimisation_is_valid_fail
	( verr, "realizing points don't have correct squared distance");


    // check P
    // -------
    verr << "  checking P..." << flush;
        
    // check point set
    for ( i = 0; i < number_of_points_p(); ++i) {
      for ( j = 0; j < d; ++j) {
	// compute (a positive multiple of) p^* - p_i
	diff[ j] = 
	  ET(da_coord(p_points[ i])[d]) * p_coords[ j] - 
	  den * ET(da_coord( p_points[ i])[ j]);
      }
      if ( std::inner_product( diff.begin(), diff.end(),
			       normal.begin(), et_0) > et_0)
	return CGAL::_optimisation_is_valid_fail
	  ( verr, "polytope P is not separated by its hyperplane");
    }
        
    verr << "passed." << endl;

    // check Q
    // -------
    verr << "  checking Q..." << flush;
        
    // check point set
    for ( i = 0; i < number_of_points_q(); ++i) {
      for ( j = 0; j < d; ++j) {
	// compute (a positive multiple of) q^* - q_i
	diff[ j] = 
	  ET(da_coord(q_points[ i])[d]) * q_coords[ j] - 
	  den * ET(da_coord( q_points[ i])[ j]);
      }
      if ( std::inner_product( diff.begin(), diff.end(),
			       normal.begin(), et_0) < et_0)
	return CGAL::_optimisation_is_valid_fail
	  ( verr, "polytope Q is not separated by its hyperplane");
    }
        
    verr << "passed." << endl;
  }

  verr << "  object is valid!" << endl;
  return( true);
}

// output operator
template < class Traits_ >
std::ostream&
operator << ( std::ostream& os,
              const Polytope_distance_d<Traits_>& poly_dist)
{
  using namespace std;

  typedef  typename Polytope_distance_d<Traits_>::Point  Point;
  typedef  ostream_iterator<Point>       Os_it;
  typedef  typename Traits_::ET          ET;
  typedef  ostream_iterator<ET>          Et_it;

  switch ( CGAL::get_mode( os)) {

  case CGAL::IO::PRETTY:
    os << "CGAL::Polytope_distance_d( |P+Q| = "
       << poly_dist.number_of_points_p() << '+'
       << poly_dist.number_of_points_q() << ", |S| = "
       << poly_dist.number_of_support_points_p() << '+'
       << poly_dist.number_of_support_points_q() << endl;
    os << "  P = {" << endl;
    os << "    ";
    copy( poly_dist.points_p_begin(), poly_dist.points_p_end(),
	  Os_it( os, ",\n    "));
    os << "}" << endl;
    os << "  Q = {" << endl;
    os << "    ";
    copy( poly_dist.points_q_begin(), poly_dist.points_q_end(),
	  Os_it( os, ",\n    "));
    os << "}" << endl;
    os << "  S_P = {" << endl;
    os << "    ";
    copy( poly_dist.support_points_p_begin(),
	  poly_dist.support_points_p_end(),
	  Os_it( os, ",\n    "));
    os << "}" << endl;
    os << "  S_Q = {" << endl;
    os << "    ";
    copy( poly_dist.support_points_q_begin(),
	  poly_dist.support_points_q_end(),
	  Os_it( os, ",\n    "));
    os << "}" << endl;
    os << "  p = ( ";
    copy( poly_dist.realizing_point_p_coordinates_begin(),
	  poly_dist.realizing_point_p_coordinates_end(),
	  Et_it( os, " "));
    os << ")" << endl;
    os << "  q = ( ";
    copy( poly_dist.realizing_point_q_coordinates_begin(),
	  poly_dist.realizing_point_q_coordinates_end(),
	  Et_it( os, " "));
    os << ")" << endl;
    os << "  squared distance = "
       << poly_dist.squared_distance_numerator() << " / "
       << poly_dist.squared_distance_denominator() << endl;
    break;

  case CGAL::IO::ASCII:
    os << poly_dist.number_of_points_p() << endl;
    copy( poly_dist.points_p_begin(),
	  poly_dist.points_p_end(),
	  Os_it( os, "\n"));
    os << poly_dist.number_of_points_q() << endl;
    copy( poly_dist.points_q_begin(),
	  poly_dist.points_q_end(),
	  Os_it( os, "\n"));
    break;

  case CGAL::IO::BINARY:
    os << poly_dist.number_of_points_p() << endl;
    copy( poly_dist.points_p_begin(),
	  poly_dist.points_p_end(),
	  Os_it( os));
    os << poly_dist.number_of_points_q() << endl;
    copy( poly_dist.points_q_begin(),
	  poly_dist.points_q_end(),
	  Os_it( os));
    break;

  default:
    CGAL_optimisation_assertion_msg( false,
				     "CGAL::get_mode( os) invalid!");
    break; }

  return( os);
}


} //namespace CGAL

#endif // CGAL_POLYTOPE_DISTANCE_D_H

// ===== EOF ==================================================================