Boost.X3: does not compile with std::optional<std::string> - boost-spirit-x3

The following parser does not compile when I use std::optional<std::string> for values of a -lexeme[+alpha] rule: something breaks in the attribute management.
The grammar works fine if instead of std::string I use int as a base type, it also works if I use twice std::string (i.e., no std::optional). However it does not work with boost::optional either. In the real grammar there is a difference between the absence of the string, and an empty string: I want to use std::optional (or its Boost predecessor).
The full example is available on Coliru, but here are the relevant bits:
struct pair_t
{
std::string first;
std::optional<std::string> second;
};
BOOST_FUSION_ADAPT_STRUCT(pair_t, first, second)
const auto pair_rule = lexeme[+alpha] >> -lexeme[+alpha] >> eoi;
for (std::string i: {"ab", "ab cd"})
{
auto res = pair_t{};
auto first = i.cbegin();
auto last = i.cend();
auto r = x3::phrase_parse(first, last, pair_rule, space, res);
if (r && first == last)
std::cout << i << ": " << res << '\n';
else
std::cout << i << ": failed\n";
}
My compiler reports:
clang++-mp-5.0 -std=c++17 -isystem /opt/local/include/ x3-optional.cc && ./a.out
In file included from x3-optional.cc:8:
In file included from /opt/local/include/boost/spirit/home/x3.hpp:14:
In file included from /opt/local/include/boost/spirit/home/x3/auxiliary.hpp:11:
In file included from /opt/local/include/boost/spirit/home/x3/auxiliary/any_parser.hpp:17:
/opt/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:180:9: error: no matching function
for call to 'move_to'
detail::move_to(std::move(src), dest
^~~~~~~~~~~~~~~
/opt/local/include/boost/spirit/home/x3/char/char_parser.hpp:31:29: note: in instantiation of
function template specialization 'boost::spirit::x3::traits::move_to<const char &,
std::__1::basic_string<char> >' requested here
x3::traits::move_to(*first, attr);
^

Related

corefine_and_compute_difference CGAL error: precondition violation

Problem description
I read the mesh from the file "blank.off" and load it into the a surface_mesh variable blank. One file named "hepoints49.txt" stores point clouds. I use function CGAL::advancing_front_surface_reconstruction() to convert this point cloud to surface_mesh sv, and then use function corefine_and_compute_difference(blank,sv,res) to perform the Boolean subtraction between blank and sv.But the program throws an exception and terminates. The following is displayed on the terminal:
Using context 4 . 3 GL
load sv...
Using context 4 . 3 GL
start difference...
CGAL error: precondition violation!
Expression : CGAL::is_valid_polygon_mesh(tm)
File : D:\dev\vcpkg\installed\x64-windows\include\CGAL/Polygon_mesh_processing/orientation.h
Line : 190
Could you please help me solve this problem?
code
#include<iostream>
#include<io.h>
#include<fstream>
#include<algorithm>
#include<array>
#include<CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include<CGAL/Advancing_front_surface_reconstruction.h>
#include<CGAL/Surface_mesh.h>
#include<CGAL/disable_warnings.h>
#include<CGAL/draw_surface_mesh.h>
#include<ctime>
#include<string>
#include<CGAL/polygon_mesh_processing/corefinement.h>
#include<CGAL/polygon_mesh_processing/remesh.h>
#include<CGAL/boost/graph/selection.h>
#include<CGAL/polygon_mesh_processing/repair_self_intersections.h>
using std::cin;
using std::cout;
using std::endl;
using std::string;
namespace PMP = CGAL::Polygon_mesh_processing;
typedef std::array<std::size_t, 3> Facet;
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef Kernel::Point_3 Point_3;
typedef CGAL::Surface_mesh<Point_3> Mesh;
struct Construct {
Mesh& mesh;
template <typename PointIterator>
Construct(Mesh& mesh, PointIterator b, PointIterator e):mesh(mesh) {
for (; b != e; ++b) {
boost::graph_traits<Mesh>::vertex_descriptor v;
v = add_vertex(mesh);
mesh.point(v) = *b;
}
}
Construct& operator=(const Facet f) {
typedef boost::graph_traits<Mesh>::vertex_descriptor vertex_descriptor;
typedef boost::graph_traits<Mesh>::vertices_size_type size_type;
mesh.add_face(vertex_descriptor(static_cast<size_type>(f[0])),
vertex_descriptor(static_cast<size_type>(f[1])),
vertex_descriptor(static_cast<size_type>(f[2])));
return *this;
}
Construct& operator*() { return *this; }
Construct& operator++() { return *this; }
Construct& operator++(int) { return *this; }
};
int main() {
//load blank
Mesh blank, sv,res;
std::ifstream fin("blank.off");
fin>>blank;
fin.close();
CGAL::draw(blank);
//load sv
string filename = "hepoints49.txt" ;
std::cout << "load sv..."<< std::endl;
fin.open(filename);
std::vector<Point_3> points;
std::vector<Facet> facets;
std::copy(std::istream_iterator<Point_3>(fin),
std::istream_iterator<Point_3>(),
std::back_inserter(points));//load points
fin.close();
Construct construct(sv, points.begin(), points.end());
CGAL::advancing_front_surface_reconstruction(points.begin(), points.end(), construct);//convert sv to surface_mesh
CGAL::draw(sv);
std::cout << "start difference..." << std::endl;
bool valid_difference = PMP::corefine_and_compute_difference(blank,sv,res);
if (valid_difference) {
std::cout << "difference was successfully computed. " << std::endl;
CGAL::draw(res);
}
else {
std::cout << "difference could not be completed. Skip. " << endl << endl;
}
//CGAL::draw(res);
return 0;
}
Runtime environment
CGAL version: 5.3
IDE: VS2017
Solution Configuration: Debug x64
I tried to run this program in Release mode, of course there is no exception thrown. But the result I got turned out to be the opposite of what I want.
Files
Files that appearing in the code are provided below:
https://github.com/wenzaifou/for-stack-overflow-question3.git
Github link is provided because the file is relatively large.
The way the mesh is constructed from advancing front output does not filter out isolated vertices, which causes the exception to be raised. Adding a call to CGAL::Polygon_mesh_processing::remove_isolated_vertices(sv) will solve the problem.
Then you might encounter the issue that your meshes are not outward oriented (meaning then represent an infinite portion of space). Adding the following calls will solve the problem:
if (!CGAL::Polygon_mesh_processing::is_outward_oriented(blank))
CGAL::Polygon_mesh_processing::reverse_face_orientations(blank);
if (!CGAL::Polygon_mesh_processing::is_outward_oriented(sv))
CGAL::Polygon_mesh_processing::reverse_face_orientations(sv);
Doc refs here and there.

GDAL VSIS3 and GetRasterBand

I'm trying to access some Landsat data from S3 without making local copies of the files. As a test I wanted to run a simple GetRasterBand on the file but I'm not sure how to go about treating a VSILFILE as a GDALDataset without downloading the file.
GDAL API guide states VSILFILE "cannot be used with any functions other than the "VSI*L" family of functions. They aren't "real" FILE objects."
Snip of my code :
VSILFILE *poVs3Dataset;
//GDALDataset *poDataset;
GDALRasterBand *poBand;
char * path = (char *)"/vsis3/landsat-pds/c1/L8/139/045/LC08_L1TP_139045_20170304_20170316_01_T1/LC08_L1TP_139045_20170304_20170316_01_T1_B1.TIF";
GDALAllRegister();
VSIInstallS3FileHandler();
CPLSetConfigOption( "AWS_ACCESS_KEY_ID", "XXX" );
CPLSetConfigOption( "AWS_SECRET_ACCESS_KEY", "XXX" );
poVs3Dataset = VSIFOpenL(path, "r");
poBand = poVs3Dataset->GetRasterBand( 1 );
Which ultimately and understandably fails
g++ -g -L/usr/local/lib -lgdal stats.cpp
error: ‘VSILFILE’ has no member named ‘GetRasterBand’
Are there any good C++ examples out there I could work through?
Thanks!
Setting my environment variables prior to calling the executable seemed to help:
$> env AWS_ACCESS_KEY_ID=xxx AWS_SECRET_ACCESS_KEY=xxx ./a.out /vsis3/landsat-pds/c1/L8/139/045/LC08_L1TP_139045_20170304_20170316_01_T1/LC08_L1TP_139045_20170304_20170316_01_T1_B1.TIF
This is now my test code to prove to myself I can access with both GDALOpenEx & VSIFOpenExL which works in case it helps someone else:
VSILFILE *fpL;
GDALDataset *poDataset;
GDALRasterBand *poBand;
char * path = (char *)"/vsis3/landsat-pds/c1/L8/139/045/LC08_L1TP_139045_20170304_20170316_01_T1/LC08_L1TP_139045_20170304_20170316_01_T1_B1.TIF";
VSIStatBufL sStat;
const char* const apszAllowedDrivers[] = { "GTiff", NULL };
GDALAllRegister();
poDataset = reinterpret_cast<GDALDataset*>(GDALOpenEx(path, GDAL_OF_READONLY | GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR, NULL, NULL, NULL));
if( poDataset == NULL )
{
std::cout << "Couldn't open " << std::endl;
}
poBand = poDataset->GetRasterBand( 1 );
int nXSize = poBand->GetXSize();
int nYSize = poBand->GetYSize();
std::cout << "nXSize : " << nXSize << std::endl;
std::cout << "nYSize : " << nYSize << std::endl;
fpL = VSIFOpenExL(path, "rb", 1);
if( fpL != NULL )
{
....
works!!!

wcout function does not print a french character

I am using the wcin in order to store a single character in a wchar_t. Then I try to print it with a wcout call and the french character 'é' : but I can't see it at my console.
My compiler is g++ 4.5.4 and my OS is Ubuntu 12.10 64 bits.
Here is my attempt (wideChars.cpp) :
#include <iostream>
int main(){
using namespace std;
wchar_t aChar;
cout << "Enter your char : ";
wcin >> aChar;
wcout << L"You entered " << aChar << L" .\n";
return 0;
}
When I lauch the programm :
$ ./wideChars
Enter your char : é
You entered .
So, what's wrong with this code ?
First, add some error checking. Test what does wcin.good() return after the input and what does wcout.good() return after the "You entered" print? I suspect one of those will return false.
What are your LANG and LC_* environment variables set to?
Then try to fix this by adding this at the top of your main(): wcin.imbue(std::locale("")); wcout.imbue(std::locale(""));
I do not have my Ubuntu at hand right now, so I am flying blind here and mostly guessing.
UPDATE
If the above suggestion does not help then try to construct locale like this and imbue() this locale instead.
std::locale loc (
std::locale (),
new std::codecvt_byname<wchar_t, char, std::mbstate_t>("")));
UPDATE 2
Here is what works for me. The key is to set the C locale as well. IMHO, this is a bug in GNU C++ standard library implementation. Unless I am mistaken, setting std::locale::global(""); should also set the C library locale.
#include <iostream>
#include <locale>
#include <clocale>
#define DUMP(x) do { std::wcerr << #x ": " << x << "\n"; } while (0)
int main(){
using namespace std;
std::locale loc ("");
std::locale::global (loc);
DUMP(std::setlocale(LC_ALL, NULL));
DUMP(std::setlocale(LC_ALL, ""));
wcin.imbue (loc);
DUMP (wcin.good());
wchar_t aChar = 0;
wcin >> aChar;
DUMP (wcin.good());
DUMP ((int)aChar);
wcout << L"You entered " << aChar << L" .\n";
return 0;
}
UPDATE 3
I am confused, now I cannot reproduce it again and setting std::locale::global(loc); seems to do the right thing wrt/ the C locale as well.

QtCreator with MinGW : How to make compiler optimization

I am using QtCreator on windows, and I would like to know how to make my compiler optimize the output.
My understanding is that MinGW is a port of GCC. So, I should be able to use arguments such as -O2. However, in the "Projects" bar, the only things I see are :
Build step for qmake (probably not here, qmake is about the .pro files / MOC / Qt stuff ...)
Build step for mingw32-make (probably)
Clean steps (probably not)
So, I tried to add -O2 in the "Make arguments" box, but unfortunately, I get an error "invalid option --O"
For anyone interested, I was trying to make an implementation of the Ackermann function because I read that :
The Ackermann function, due to its definition in terms of extremely
deep recursion, can be used as a benchmark of a compiler's ability to
optimize recursion
The code (which doesn't really use Qt) :
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <ctime>
using namespace std;
int nbRecursion;
int nbRecursions9;
int Ackermann(int m, int n){
nbRecursion++;
if(nbRecursion % 1000000 == 0){
qDebug() << nbRecursions9 << nbRecursion;
}
if(nbRecursion == 1000000000){
nbRecursion = 0;
nbRecursions9++;
}
if(m==0){
return n+1;
}
if(m>0 && n>0){
return Ackermann(m-1,Ackermann(m, n-1));
}
if(m>0 && n==0){
return Ackermann(m-1,1);
}
qDebug() << "Bug at " << m << ", " << n;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
nbRecursion = 0;
nbRecursions9 = 0;
int m = 3;
int n = 13;
clock_t begin = clock();
Ackermann(m,n);
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
qDebug() << "There are " << CLOCKS_PER_SEC << " CLOCKS_PER_SEC";
qDebug() << "There were " << nbRecursions9 << nbRecursion << " recursions in " << elapsed_secs << " seconds";
double timeX = 1000000000.0*((elapsed_secs)/(double)nbRecursion);
if(nbRecursions9>0){
timeX += elapsed_secs/(double)nbRecursions9;
}
qDebug() << "Time for a function call : " << timeX << " nanoseconds";
return a.exec();
}
-O2 is used by default when you do a release build. Only debug builds don't use optimization. Regardless, if you want to use specific compiler options, you do so in the project file (*.pro) itself, by appending your options to the QMAKE_CFLAGS_RELEASE (for C files) and QMAKE_CXXFLAGS_RELEASE (for C++ files). For example:
QMAKE_CFLAGS_RELEASE += -O3 -march=i686 -mtune=generic -fomit-frame-pointer
QMAKE_CXXFLAGS_RELEASE += -O3 -march=i686 -mtune=generic -fomit-frame-pointer
If you really want to use some specific options always, regardless of whether it's a debug or release build, then append to QMAKE_CFLAGS and QMAKE_CXXFLAGS instead. But usually, you'll only want optimization options in your release builds, not the debug ones.

How to overload operator>> for bool

I want to parse scalar as bool.
This example works:
#include <yaml.h>
#include <iostream>
#include <sstream>
#include <string>
void operator>> (const YAML::Node & node, bool & b)
{
std::string tmp;
node >> tmp;
std::cout << tmp << std::endl;
b = (tmp == "1") || (tmp == "yes");
}
int main()
{
bool b1, b2;
std::stringstream ss("key: да\notherkey: no");
YAML::Parser parser(ss);
YAML::Node doc;
parser.GetNextDocument(doc);
doc["key"] >> b1;
doc["otherkey"] >> b2;
std::cout << b1 << std::endl;
std::cout << b2 << std::endl;
return 0;
}
But in more complicated case template operator is used:
YAML::operator>><bool> (node=..., value=#0x63f6e8) at /usr/include/yaml-cpp/nodeimpl.h:24
And I get 'YAML::InvalidScalar' if string in not 'yes' or 'no'.
yaml-cpp already reads bools by default, as specified by the YAML spec.
y, yes, true, on
produce true, and
n, no, false, off
produce false. If you want to extend or change this behavior (for example, so that "да" also produces true), as you found out, overloading operator >> in the YAML namespace works.
The reason it needs to be in the YAML namespace (but only for "more complicated examples" - meaning where you don't directly call operator >> with a bool argument) is the way C++ lookup works.
See this answer to my old question for a great explanation.