-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.cpp
758 lines (658 loc) · 25.1 KB
/
utility.cpp
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
/*! \file utility.cpp
* \brief The implementation of some utility functions.
* \author Christos Nitsas
* \date 2012
*
* Won't `include` utility.h. In fact, utility.h will `include`
* utility.cpp because it contains function templates (which don't allow
* us to split declaration from definition).
*/
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <string>
#include <assert.h>
#include <unistd.h>
#include "NonDominatedSet.h"
/*!
* \weakgroup ParetoApproximator Everything needed for the Pareto set approximation algorithms.
* @{
*/
// An unnamed namespace containing helper functions. (declarations here)
// (implementations at the end of this file)
namespace {
using pareto_approximator::PointAndSolution;
using pareto_approximator::Facet;
//! Make qconvex's input file. The given points will be the input.
/*!
* \param points The points whose convex hull we need qconvex to compute
* for us.
* \param filename The name of the file we will create. (qconvex's input file)
* \param spaceDimension The dimension of the space that the points live in.
*
* \sa pareto_approximator::computeConvexHull() and
* pareto_approximator::computeConvexHullFacets()
*/
template <class S>
void
writePointsToQconvexInputFile(
std::vector< PointAndSolution<S> > points,
std::string filename, unsigned int spaceDimension);
/*!
* \brief Parse qconvex's output file and return a list of the convex
* hull's facets.
*
* \param filename The name of the file we will parse. (qconvex's output file)
* \param points The points whose convex hull we asked qconvex to compute
* for us. Their order counts - it must be the same as their
* order in qconvex's input file.
* \param spaceDimension The dimension of the space that the points live in.
* \return A list of the convex hull's facets.
*
* \sa pareto_approximator::computeConvexHullFacets()
*/
template <class S>
std::list< Facet<S> >
readFacetsFromQconvexOutputFile(std::string filename,
std::vector< PointAndSolution<S> > points,
unsigned int spaceDimension);
/*!
* \brief Parse qconvex's output file and return a vector of the convex
* hull's extreme points..
*
* \param filename The name of the file we will parse. (qconvex's output file)
* \param points The points (PointAndSolution<S> objects) whose convex hull
* we asked qconvex to compute for us. Their order counts - it
* must be the same as their order in qconvex's input file.
* \return A vector of the convex hull's extreme points (PointAndSolution<S>
* objects).
*
* \sa pareto_approximator::computeConvexHull()
*/
template <class S>
std::list< PointAndSolution<S> >
readExtremePointsFromQconvexOutputFile(
std::string filename,
std::vector< PointAndSolution<S> > points);
//! Normalizes a vector of double. (in place)
/*!
* \param v A vector (as a std::vector<double>).
*
* After the operation the vector will be normalized, i.e. its magnitude
* (in other words length or L2-norm) will be 1.
*/
void
normalizeVector(std::vector<double> & v);
} // namespace
//! The namespace containing everything needed for the Pareto set approximation algorithms.
namespace pareto_approximator {
//! The namespace containing utility functions.
/*!
* (i.e. functions called by class methods e.t.c.)
*/
namespace utility {
//! Compute the facets of the convex hull of the given set of points.
/*!
* \param points A (const reference to a) std::vector of points.
* (PointAndSolution<S> instances)
* \param spaceDimension The dimension of the space that the points live in.
* \return A list containing all the facets (Facet<S> instances) of the
* convex hull.
*
* This function requires that the external program/tool qconvex,
* distributed with qhull (see www.qhull.org) be installed on the system
* (and be on the PATH).
*
* This function currently only works for Unix-like systems, it won't
* work on Windows. It has only been tested on Mac OS X Mountain Lion
* but should work on other Unix-like systems as well.
*
* \sa BaseProblem::doPgen()
*/
template <class S>
std::list< Facet<S> >
computeConvexHullFacets(const std::vector< PointAndSolution<S> > & points,
unsigned int spaceDimension)
{
// The files we'll use to interface with qconvex.
std::string qconvexInputFilename = "qconvex-input.txt";
std::string qconvexOutputFilename = "qconvex-output.txt";
// First make qconvex's input file:
writePointsToQconvexInputFile(points, qconvexInputFilename, spaceDimension);
std::list< Facet<S> > facets;
// Make a subprocess (child) that will exec qconvex to compute
// the convex hull:
pid_t pid = fork();
if (pid < 0) {
// could not fork()
std::cerr << "Failed to fork... Exiting" << std::endl;
exit(-1);
}
else if (pid == 0) {
// child process
// run qconvex (input: qconvex-input.txt, output: qconvex-output.txt)
int rv = execlp("qconvex", "qconvex", "i", "n", "Qt",
"PF0.000000000000000000000001",
"TI", "qconvex-input.txt",
"TO", "qconvex-output.txt", NULL);
// Added option "PFn" where n is a lower bound for the area of
// facets to be printed (facets with area < n will not be printed) to
// avoid degenerate facets with zero area.
// - e.g. "PF0.000000000000000000000001" will not print facets with
// area less than 0.000000000000000000001
// - degenerate facets with zero area might appear due to option "Qt"
// (how come? all the vertices might belong to the same ridge
// of the original non simplicial facet)
if (rv == -1) {
std::cerr << "An error occured while trying to call qconvex... Exiting"
<< std::endl;
exit(-1);
}
// Code will have ended by here.
// - Either via qconvex exiting without error or the exit(-1) in case a
// qconvex error occurs.
// - Won't reach the return statement.
}
else {
// parent process
int childStatus;
waitpid(pid, &childStatus, 0);
if ( not WIFEXITED(childStatus) ) {
if ( WIFSIGNALED(childStatus) ) {
std::cerr << "ERROR: qconvex got a signal that caused it to exit ("
<< WTERMSIG(childStatus) << "). Exiting" << std::endl;
exit(-1);
}
else {
std::cerr << "ERROR: qconvex did not exit normally. Exiting"
<< std::endl;
exit(-1);
}
}
else if ( WEXITSTATUS(childStatus) != 0 ) {
std::cerr << "ERROR: qconvex exited with errorcode: "
<< WEXITSTATUS(childStatus) << std::endl
<< "Exiting" << std::endl;
exit(-1);
}
else {
// parse qconvex's output file (qconvex-output.txt) and make the facets
facets = readFacetsFromQconvexOutputFile(qconvexOutputFilename,
points,
spaceDimension);
// return the facets outside the ifs (to avoid compiler warnings
// about reaching the end of non-void function)
}
}
// Only the parent will get here and only after succesfully reading
// the facets from qconvex's output file. (qconvex will have exited
// without error as well)
return facets;
}
//! Compute the convex hull of the given set of points.
/*!
* \param points A (const reference to a) std::vector of points.
* (PointAndSolution<S> instances)
* \param spaceDimension The dimension of the space that the points live in.
* \return A vector containing all the extreme points (PointAndSolution<S>
* instances) of the convex hull.
*
* We need at least #(spaceDimension+1) points to compute a convex hull.
* If "points" contains less than #(spaceDimension+1) points we will
* this function will just return the given set of points ("points") as
* the result.
*
* This function requires that the external program/tool qconvex,
* distributed with qhull (see www.qhull.org) be installed on the system
* (and be on the PATH).
*
* This function currently only works for Unix-like systems, it won't
* work on Windows. It has only been tested on Mac OS X Mountain Lion
* but should work on other Unix-like systems as well.
*
* \sa BaseProblem::doPgen()
*/
template <class S>
std::list< PointAndSolution<S> >
computeConvexHull(const std::vector< PointAndSolution<S> > & points,
unsigned int spaceDimension)
{
// The files we'll use to interface with qconvex.
std::string qconvexInputFilename = "qconvex-input.txt";
std::string qconvexOutputFilename = "qconvex-output.txt";
std::list< PointAndSolution<S> > extremePoints;
// we need at least #(spaceDimension+1) points to compute a convex hull
if (points.size() <= spaceDimension) {
// no need to continue, all points in "points" are on the lower envelope
extremePoints.assign(points.begin(), points.end());
extremePoints.sort();
return extremePoints;
}
// else
// First make qconvex's input file:
writePointsToQconvexInputFile(points, qconvexInputFilename, spaceDimension);
// Make a subprocess (child) that will exec qconvex to compute
// the convex hull:
pid_t pid = fork();
unsigned int retries = 10;
while ( (pid < 0) && (retries > 0) ) {
// could not fork()
--retries;
std::cerr << "Failed to fork (probably due to low memory)... Will retry in 30 seconds. " << retries << " retries left." << std::endl;
sleep(30);
pid = fork();
}
if (retries == 0) {
std::cerr << "Failed to fork... Exiting" << std::endl;
exit(-1);
}
if (pid == 0) {
// child process
// run qconvex (input: qconvex-input.txt, output: qconvex-output.txt)
int rv = execlp("qconvex", "qconvex", "Fx", "TI", "qconvex-input.txt",
"TO", "qconvex-output.txt", NULL);
// Used option "Fx" which only prints the (indices of the) extreme
// points of the convex hull.
if (rv == -1) {
std::cerr << "An error occured while trying to call qconvex... Exiting"
<< std::endl;
exit(-1);
}
// Code will have ended by here.
// - Either via qconvex exiting without error or the exit(-1) in case a
// qconvex error occurs.
// - Won't reach the return statement.
}
else {
// parent process
int childStatus;
waitpid(pid, &childStatus, 0);
if ( not WIFEXITED(childStatus) ) {
if ( WIFSIGNALED(childStatus) ) {
std::cerr << "ERROR: qconvex got a signal that caused it to exit ("
<< WTERMSIG(childStatus) << "). Exiting" << std::endl;
exit(-1);
}
else {
std::cerr << "ERROR: qconvex did not exit normally. Exiting"
<< std::endl;
exit(-1);
}
}
else if ( WEXITSTATUS(childStatus) != 0 ) {
std::cerr << "ERROR: qconvex exited with errorcode: "
<< WEXITSTATUS(childStatus) << std::endl
<< "Exiting" << std::endl;
exit(-1);
}
else {
// parse qconvex's output file (qconvex-output.txt) and
// get the convex hull's extreme points
extremePoints = readExtremePointsFromQconvexOutputFile(
qconvexOutputFilename,
points);
// return the extreme points outside the ifs (to avoid compiler
// warnings // about reaching the end of non-void function)
}
}
// Only the parent will get here and only after succesfully reading
// the extreme points from qconvex's output file. (qconvex will have
// exited without error as well)
extremePoints.sort();
return extremePoints;
}
/*!
* \brief Filter a sequence of PointAndSolution instances and return
* only the non-dominated ones.
*
* \param first An iterator to the first element in the sequence.
* \param last An iterator to the past-the-end element in the sequence.
*
* We will use a pareto_approximator::NonDominatedSet to discard dominated
* points.
*
* \sa NonDominatedSet
*/
template <class S>
std::vector< PointAndSolution<S> >
filterDominatedPoints(
typename std::vector< PointAndSolution<S> >::const_iterator first,
typename std::vector< PointAndSolution<S> >::const_iterator last)
{
NonDominatedSet< PointAndSolution<S> > filter(first, last);
return std::vector< PointAndSolution<S> >(filter.begin(), filter.end());
}
//! Discard facets not useful for generating new Pareto points.
/*!
* \param facets A (reference to a) list of facets.
*
* Discard facets with all normal vector coefficients non-positive (<= 0).
* Facets with no positive normal vector coefficient are not useful for
* generating new Pareto optimal points.
*
* Only facets with all-positive or mixed (i.e. containing at least some
* positive coefficients) normal vectors can be used to generate new
* Pareto optimal points.
*
* \sa Facet, BaseProblem::doChord() and BaseProblem::doPgen()
*/
template <class S>
void
discardUselessFacets(std::list< Facet<S> > & facets)
{
typename std::list< Facet<S> >::iterator it;
for (it = facets.begin(); it != facets.end(); )
if (it->hasAllNormalVectorElementsNonPositive())
it = facets.erase(it);
else
++it;
}
/*! \brief Choose the Facet instance with the largest local approximation
* error upper bound from sequence of Facet instances.
*
* \param first An iterator to the first element in the sequence.
* \param last An iterator to the past-the-end element in the sequence.
* \return An iterator to the first element in the range that has the
* largest local approximation error upper bound. If no element
* is a non-boundary facet the function returns "last".
*
* The Facet::getLocalApproximationErrorUpperBound() method is used (of
* course) for the facet's local approximation error upper bound.
*
* Boundary facets (i.e. those with isBoundaryFacet() == true) are ignored.
*
* If all the facets in the sequence are boundary facets the iterator
* "last" is returned.
*
* \sa Facet
*/
template <class S>
typename std::list< Facet<S> >::iterator
chooseFacetWithLargestLocalApproximationErrorUpperBound(
typename std::list< Facet<S> >::iterator first,
typename std::list< Facet<S> >::iterator last)
{
typename std::list< Facet<S> >::iterator it, max = last;
for (it = first; it != last; ++it) {
// Is it a non-boundary facet?
if (not it->isBoundaryFacet()) {
// Is it the first non-boundary facet?
// or
// Does it have a larger local approximation error upper bound
// than the one max has?
if ( max == last or it->getLocalApproximationErrorUpperBound() >
max->getLocalApproximationErrorUpperBound() ) {
max = it;
}
// else ignore it
}
// else ignore it
}
return max;
}
/*! \brief Choose a boundary Facet instance with the smallest angle
* from the given sequence of Facet instances.
*
* \param first A const_iterator to the first element in the sequence.
* \param last A const_iterator to the past-the-end element in the sequence.
* \return A const_iterator to the first element in the range that is a
* boundary facet and has the smallest angle. If there are no
* boundary facets the function returns "last".
*
* Non boundary facets (i.e. those with isBoundaryFacet() == false) are
* ignored.
*
* If all the facets in the sequence are non boundary facets the iterator
* "last" is returned.
*
* \sa Facet
*/
template <class S>
typename std::list< Facet<S> >::iterator
chooseBoundaryFacetWithSmallestAngle(
typename std::list< Facet<S> >::iterator first,
typename std::list< Facet<S> >::iterator last)
{
typename std::list< Facet<S> >::iterator it, min = last;
for (it = first; it != last; ++it) {
// Is it a boundary facet?
if (it->isBoundaryFacet()) {
// currently returns the first boundary facet
return it;
}
// else ignore it
}
return min;
}
//! Generate a weight vector (for comb()) using the given facet.
/*!
* \param facet A Facet instance. (Its vertices' weightsUsed attributes
* will be needed if the facet's normal vector is not
* all-positive.)
* \return A std::vector of objective weights (for BaseProblem::comb()).
*
* The resulting weights will be:
* - Either the facet's normal vector. (normalized)
* (if it has no negative elements)
* - Or the mean of the weights used to obtain the facet's vertices.
* (normalized)
*
* \sa BaseProblem, BaseProblem::comb() and
* BaseProblem::generateNewParetoPoint()
*/
template <class S>
std::vector<double>
generateNewWeightVector(const Facet<S> & facet)
{
std::vector<double> weights;
if (facet.hasAllNormalVectorElementsNonNegative()) {
// Use the facet's normal vector (i.e. the facet's slope) as weights.
weights = facet.getNormalVector();
}
else {
// Use the mean of the facet's vertex weights (weightsUsed) as weights.
// - "weights" will be a std::vector<double> of weights W_{i}, where:
// /f$ W_{i} = sum_{j=1}^{j=facet.spaceDimension()} ( w_{ij} ) /f$,
// where w_{ij} is the i'th of the weights used to obtain the j'th
// vertex of "facet".
weights = facet.computeMeanVertexWeights();
}
normalizeVector(weights);
return weights;
}
} // namespace utility
} // namespace pareto_approximator
// An unnamed namespace containing helper functions. (implementations here)
// (declarations at the beginning of this file)
namespace {
//! Make qconvex's input file. The given points will be the input.
/*!
* \param points The points whose convex hull we need qconvex to compute
* for us.
* \param filename The name of the file we will create. (qconvex's input file)
* \param spaceDimension The dimension of the space that the points live in.
*
* \sa pareto_approximator::computeConvexHull() and
* pareto_approximator::computeConvexHullFacets()
*/
template <class S>
void
writePointsToQconvexInputFile(
std::vector< PointAndSolution<S> > points,
std::string filename, unsigned int spaceDimension)
{
// qcif stands for "QConvex's Input File"
std::ofstream qcif(filename.c_str(), std::ios::out | std::ios::trunc);
if (not qcif.is_open()) {
std::cerr << "An error occured while opening file \"" << filename
<< "\" for output... Exiting" << std::endl;
exit(-1);
}
// else
// make qconvex's input file
qcif << spaceDimension << "\n";
qcif << points.size() << "\n";
typename std::vector< PointAndSolution<S> >::iterator pit;
for (pit = points.begin(); pit != points.end(); ++pit)
qcif << pit->point << "\n";
qcif.close();
}
/*!
* \brief Parse qconvex's output file and return a list of the convex
* hull's facets.
*
* \param filename The name of the file we will parse. (qconvex's output file)
* \param points The points whose convex hull we asked qconvex to compute
* for us. Their order counts - it must be the same as their
* order in qconvex's input file.
* \param spaceDimension The dimension of the space that the points live in.
* \return A list of the convex hull's facets.
*
* \sa pareto_approximator::computeConvexHullFacets()
*/
template <class S>
std::list< Facet<S> >
readFacetsFromQconvexOutputFile(std::string filename,
std::vector< PointAndSolution<S> > points,
unsigned int spaceDimension)
{
std::list< Facet<S> > facets;
// qcof stands for "QConvex's Output File"
std::ifstream qcof(filename.c_str());
if (not qcof.is_open()) {
std::cerr << "An error occured while opening file \"" << filename
<< "\" for input... Exiting" << std::endl;
exit(-1);
}
// else
// start parsing qconvex's output file
unsigned int numFacets, i, j, vertexIndex;
qcof >> numFacets;
if (numFacets == 0)
// nothing else to read
return facets;
// first read each facet's vertices
std::vector< typename Facet<S>::VerticesVector > vertexSets(numFacets);
for (i = 0; i != numFacets; ++i) {
for (j = 0; j != spaceDimension; ++j) {
qcof >> vertexIndex;
// Make sure that the vertex index is a valid index.
// - If the vertex index is greater than all valid indices it means
// that qconvex encountered a non-simplicial facet and we had not
// told qconvex to triangulate such facets.
// - We call a facet in a d-dimensional space non-simplicial when it
// consists of more than d vertices.
// - Triangulating a non-simplicial facet with v vertices that lives
// in a d-dimensional space (v > d) is the process of splitting the
// (non-simplicial) facet into a set of coplanar simplicial facets.
// (each of the resulting simplicial facets will have d vertices and
// they will all have the same normal vector)
// - qconvex should triangulate non-simplicial facets if we give it
// the "Qt" option (along with the "i" option) when calling it.
// - Not sure (yet) if qconvex may encounter non-simplicial facets
// in this use case i.e. convex hull of points belonging to a
// Pareto set. (this only applies to the case when the given set of
// points only contains Pareto optimal points)
assert(vertexIndex < points.size());
vertexSets[i].push_back(points[vertexIndex]);
}
}
unsigned int temp;
// consume (read) the next line
// - contains the facet normal vector size + 1
qcof >> temp;
assert(temp == spaceDimension + 1);
unsigned int numOfNormalVectors;
// consume (read) the next line
// - contains the number of normal vectors (number of facets)
qcof >> numOfNormalVectors;
assert(numOfNormalVectors == numFacets);
// now read each facet's normal vector and make the Facet instance
// - qconvex facet's normal vectors face outwards (from the convex hull)
// but ours face inwards so we will reverse each normal vector's sign
double normalVectorElement, facetOffset;
for (i = 0; i != numFacets; ++i) {
std::vector<double> facetNormal(spaceDimension);
for (j = 0; j != spaceDimension; ++j) {
qcof >> normalVectorElement;
facetNormal[j] = - normalVectorElement;
}
// consume the next number (it's the facet offset - we don't need it)
qcof >> facetOffset;
// make the facet and push it onto the list of facets
facets.push_back(Facet<S>(vertexSets[i].begin(), vertexSets[i].end(),
facetNormal.begin(), facetNormal.end()));
}
// We should be at the end of the file. Make sure:
double consume = 0.0;
qcof >> consume;
assert(qcof.eof());
// Close the file and return the list of facets:
qcof.close();
return facets;
}
/*!
* \brief Parse qconvex's output file and return a vector of the convex
* hull's extreme points..
*
* \param filename The name of the file we will parse. (qconvex's output file)
* \param points The points (PointAndSolution<S> objects) whose convex hull
* we asked qconvex to compute for us. Their order counts - it
* must be the same as their order in qconvex's input file.
* \return A vector of the convex hull's extreme points (PointAndSolution<S>
* objects).
*
* \sa pareto_approximator::computeConvexHull()
*/
template <class S>
std::list< PointAndSolution<S> >
readExtremePointsFromQconvexOutputFile(
std::string filename,
std::vector< PointAndSolution<S> > points)
{
std::list< PointAndSolution<S> > extremePoints;
// qcof stands for "QConvex's Output File"
std::ifstream qcof(filename.c_str());
if (not qcof.is_open()) {
std::cerr << "An error occured while opening file \"" << filename
<< "\" for input... Exiting" << std::endl;
exit(-1);
}
// else
// start parsing qconvex's output file
unsigned int numExtremePoints, i, vertexIndex;
qcof >> numExtremePoints;
if (numExtremePoints == 0)
// nothing else to read
return extremePoints;
// else
// read the (indices of the) extreme points
for (i = 0; i != numExtremePoints; ++i) {
qcof >> vertexIndex;
assert(vertexIndex < points.size());
extremePoints.push_back(points[vertexIndex]);
}
// We should be at the end of the file. Make sure:
double consume = 0.0;
qcof >> consume;
assert(qcof.eof());
// Close the file and return the list of facets:
qcof.close();
return extremePoints;
}
//! Normalizes a vector of double. (in place)
/*!
* \param v A vector (as a std::vector<double>).
*
* After the operation the vector will be normalized, i.e. its magnitude
* (in other words length or L2-norm) will be 1.
*/
void
normalizeVector(std::vector<double> & v)
{
double l2Norm = arma::norm(arma::vec(v), 2);
for (unsigned int i = 0; i != v.size(); ++i)
v[i] /= l2Norm;
}
} // namespace
/*! @} */