divide boost segment into N equal segments using boost geometry - c++

I created a line segment using boost geometry model. I am wondering if there is a way in boost to divide the segment into N equal line segments.
Please do not consider this question as duplicated version of this.
(EDIT)
because is over-dated and I have already tried the answer provided for this question.

The simplest thing that comes to mind:
Live On Coliru
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/io/io.hpp>
#include <boost/geometry/arithmetic/arithmetic.hpp>
#include <boost/geometry/geometries/segment.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <iostream>
template <typename Segment, typename Point = typename boost::geometry::point_type<Segment>::type>
auto split_into(unsigned N, Segment const& s) {
using namespace boost::geometry;
assert(N>1);
auto step = s.second;
subtract_point(step, s.first);
divide_value(step, N-1);
std::vector<Point> segments;
std::generate_n(back_inserter(segments), N, [accu=s.first, step] () mutable { Point p = accu; return add_point(accu, step), p; });
return model::linestring<Point> {segments.begin(), segments.end()};
}
using namespace boost::geometry;
using Point = model::d2::point_xy<double>;
int main() {
model::segment<Point> const line { {1,3}, {11,33} };
std::cout << wkt(line) << "\n";
std::cout << wkt(split_into(5, line)) << "\n";
}
Prints
LINESTRING(1 3,11 33)
LINESTRING(1 3,3.5 10.5,6 18,8.5 25.5,11 33)
Fancy Range Version
One that might scale better for large N:
Live On Coliru
#include <boost/range/adaptors.hpp>
#include <boost/range/irange.hpp>
template <typename Segment, typename Point = typename boost::geometry::point_type<Segment>::type>
auto split_into(unsigned N, Segment const& s) {
using namespace boost::geometry;
using namespace boost::adaptors;
assert(N>1);
auto step = s.second;
subtract_point(step, s.first);
divide_value(step, N-1);
auto gen = boost::irange(0u, N) | transformed([=](int i) { auto p = step; multiply_value(p, i); add_point(p, s.first); return p; });
return model::linestring<Point> { boost::begin(gen), boost::end(gen) };
}
Prints
LINESTRING(1 3,11 33)
LINESTRING(1 3,3.5 10.5,6 18,8.5 25.5,11 33)

If you need to divide AB segment to N equal pieces, then coordinates of starting point for i-th segment (and the end of previous segment) might be calculated using parametric equation of line.
Pseudocode:
for i = 0.. N-1
t = i / N
Start[i].X = A.X * (1 - t) + B.X * t
Start[i].Y = A.Y * (1 - t) + B.Y * t

Related

How can I read a 2-columns CSV file into a map with ranges?

I'm given a CSV file with two elements per line:
1,2
12,40
11,7
...
which I want to read into a std::map<int, int>.
How can I do that, using anything from Ranges library and Range-v3?
At the moment this is where I've got (with the help of this answer):
#include <boost/hof/lift.hpp>
#include <iostream>
#include <range/v3/istream_range.hpp>
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/istream.hpp>
#include <range/v3/view/join.hpp>
#include <range/v3/view/chunk.hpp>
#include <range/v3/view/transform.hpp>
#include <range/v3/view/split.hpp>
#include <string>
using ranges::istream;
using ranges::to;
using namespace ranges::views;
constexpr auto splitAtComma = [](auto const& r) { return r | split(','); };
constexpr auto rngToString = [](auto const& r) { return r | to<std::string>; };
constexpr auto strToInt = BOOST_HOF_LIFT(std::stoi);
constexpr auto parseCoords = transform(splitAtComma)
| join
| transform(rngToString)
| transform(strToInt)
| chunk(2);
int main() {
auto lines = istream<std::string>(std::cin);
auto coords = lines | parseCoords;
std::cout << coords << std::endl;
}
which, invoked like this
./main <<END
1,2
12,40
11,7
END
gives this output
[[1,2],[12,40],[11,7]]
The point is that now I don't know how convert that range-of-ranges into a map. Also, I have the feeling that I'm in a dead-end street, because each element of a std::map<int, int> comes from 2 ints, but in the range-of-ranges above [1,2], [12,40], and [11,7] are just ranges of ints, not encoding at compile tiime that there's 2 ints only.
There is no need to use join here because r | split(',') already gives you a range with two elements. Prefer std::from_chars over std::stoi.
You can do this only using the standard library's <ranges>
#include <ranges>
#include <charconv>
#include <fmt/ranges.h>
#include <sstream>
auto toInt = [](auto r) {
int i = 0;
std::from_chars(std::to_address(r.begin()), std::to_address(r.end()), i);
return i;
};
auto parseCoords =
std::views::transform(
[](auto r) { return std::move(r) | std::views::split(','); })
| std::views::transform(
[](auto r) { return std::pair{toInt(r.front()), toInt(*++r.begin())}; });
int main() {
auto in = std::istringstream{R"(
1,2
12,40
11,7
)"};
auto coords = std::views::istream<std::string>(in)
| parseCoords;
fmt::print("{}\n", coords);
}
Demo
Note that the std::move(r) in views::transform is necessary because we need to construct an owning_view to avoid the dangling issue.

Extract longest connected subvector fullfilling a condition

We have as input a vector, as an example
std::vector<std::int32_t> allnumbers{1,2,-3,4,5,-6,7,8,9};
We have a boolean condition, as an example output numbers have to be larger 3.
What we want is as output the longest subvector fullfilling the condition. All elements of the output have to have been connected in the input.
std::vector<std::int32_t> allnumbers{4,5,7,8,9};
Is wrong, as 5 and 7 have not been adjacent before (-6 between them).
std::vector<std::int32_t> allnumbers{4,5};
Is wrong, as it is not the longest subvector.
std::vector<std::int32_t> allnumbers{7,8,9};
Is finally correct.
How to write the algorithm elegantly with C++17 standard, possibly without using the Boost library? By elegantly I mean few lines of code with good readability. Utilizing prefarably as much as possible. Performance or memory consumption is here less of an issue. I think the "brute force" solution I post below already has here enough performance. One time iterate through input and only few iterators to keep track during exectuion.
The following is a working "brute force" solution:
#include <functional>
#include <iostream>
#include <stdint.h>
#include <vector>
std::vector<std::int32_t> longestConnectedVectorFullfillingPredicate(
std::function<bool(const std::int32_t)> predicate,
std::vector<std::int32_t> &inputVector)
{
auto currentIt = inputVector.begin();
auto endIt = inputVector.end();
auto beginLongestConnectedSubvector = endIt;
auto endLongestConnectedSubvector = endIt;
auto longestConnectedSubvectorLength = 0;
while (currentIt != endIt)
{
const auto currentBeginConnectedSubvector = std::find_if(
currentIt, endIt, [predicate](const std::int32_t &value) { return predicate(value); });
const auto currentEndConnectedSubvector = std::find_if(
currentBeginConnectedSubvector, endIt, [predicate](const std::int32_t &value) {
return !predicate(value);
});
const auto currentConnectedSubvectorLength =
std::distance(currentBeginConnectedSubvector, currentEndConnectedSubvector);
if (currentConnectedSubvectorLength > longestConnectedSubvectorLength)
{
longestConnectedSubvectorLength = currentConnectedSubvectorLength;
beginLongestConnectedSubvector = currentBeginConnectedSubvector;
endLongestConnectedSubvector = currentEndConnectedSubvector;
}
currentIt = currentEndConnectedSubvector;
}
return std::vector<std::int32_t>(beginLongestConnectedSubvector, endLongestConnectedSubvector);
}
int main()
{
const auto largerThree = [](std::int32_t value) { return value > 3; };
std::vector<std::int32_t> allnumbers{1, 2, -3, 4, 5, -6, 7, 8, 9};
auto result = longestConnectedVectorFullfillingPredicate(largerThree, allnumbers);
for (auto res : result)
{
std::cout << res << std::endl;
}
return 0;
}
This is quite a few lines... Would like to shorten it without loosing much readability.
You might like this, which does the following
include needed headers, define useful namespace aliases, and a couple of useful function objects
pipes allnumbers into group_by which puts all adjacent numbers greater than 3 in a range/chunk (all other numbers remain in singleton range each)
pipes that into filter which says goodby to the singletons with the number not greater than 3
then finds the longest range by using max_element to which an appropriate lambda is passed
#include <boost/hana/functional/on.hpp>
#include <boost/hana/functional/partial.hpp>
#include <boost/hana/functional/reverse_partial.hpp>
#include <boost/range/numeric.hpp>
#include <functional>
#include <iostream>
#include <range/v3/algorithm/max_element.hpp>
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/filter.hpp>
#include <range/v3/view/group_by.hpp>
#include <range/v3/view/transform.hpp>
#include <vector>
using boost::hana::on;
using boost::hana::reverse_partial;
using namespace ranges::views;
using namespace ranges;
auto both = [](bool x, bool y){ return x && y; };
auto greater_than_3 = reverse_partial(std::greater<>{}, 3);
int main() {
std::vector<int> allnumbers{1,2,-3,4,5,-6,7,8,9};
auto chunks
= allnumbers
| group_by(both ^on^ greater_than_3)
| filter([](auto v){ return greater_than_3(v.front()); })
| transform(to_vector)
| to_vector;
auto result = *max_element(
chunks,
std::less<>{} ^on^ std::mem_fn(&decltype(allnumbers)::size));
for (auto i : result) {
std::cout << i << ',';
}
}

Range v3. Difference between two ranges

I am trying to get the difference between two ranges but have little luck.
Something like
vector<int> l{ 1,5,6 };
auto diff = views::ints(1,10) - view::all( l );
==> Range<int> { 2,3,4,7,8,9 }
By the way. Are there any good writings on range-v3? Something to make me wiser?
Thanks.
You are looking for the set_difference algorithm, or its lazy view version:
#include <range/v3/view/iota.hpp>
#include <range/v3/view/set_algorithm.hpp>
#include <iostream>
#include <vector>
int main() {
std::vector<int> l{ 1,5,6 };
auto diff = ranges::view::set_difference(ranges::view::ints(1,10), l);
std::cout << diff << '\n'; // [2,3,4,7,8,9]
}

How to multiply a vector and scalar in C++?

I would like to multiply a vector with a scalar. This vector was created using the accepted answer to this question of mine namely:
std::vector<int> n(N + 1);
std::iota(begin(n), end(n), 0);
and I would like to multiply this vector, n, with a scalar (specifically of type double, if it is relevant here) called npi.
I have seen this answer to a previous question here, but it wasn't all that helpful. The way I attempted to implement it was by adding:
std::transform(n.begin(), n.end(), n.begin(),
std::bind1st(std::multiplies<T>(),pin));
to my C++ program. This returned the compile error:
error: ‘T’ was not declared in this scope
std::bind1st(std::multiplies<T>(),pin));
I would like to call the vector created by multiplying this vector with a scalar npi, so please do not give me code that will call this new vector n (i.e., overwriting my existing n vector).
EDIT:
If it will placate whomever voted to close this question, here is my full program:
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cmath>
#include <utility>
#include <unistd.h>
#include <algorithm>
#include <numeric>
/*#include <armadillo>*/
using namespace std;
/*using namespace arma;*/
double N = 1000.0;
double x0 = 0;
double x1 = 100;
double pin = M_PI / double(N);
int main() {
std::vector<int> n(N + 1);
std::iota(begin(n), end(n), 0);
std::transform(n.begin(), n.end(), n.begin(),
std::bind1st(std::multiplies<T>(),pin));
for(double i: n)
{
std::cout << i << '\n' << std::scientific;
}
}
For vector<int> output, one way is:
auto npi = n;
for( auto& i: npi )
i *= pin;
If npi should be vector<double> (not clear from the question) then replace the first line with:
std::vector<double> npi( n.begin(), n.end() );
You need to replace T by the type contained in the vector, in this case int. However you can probably simplify your code by using a lambda function here instead:
#include <algorithm> // for std::transform
#include <cmath> // for M_PI
#include <iostream> // for std::cout etc
#include <numeric> // for std::iota
#include <vector> // for awesome
int main() {
std::vector<int> vec1(10);
std::iota(vec1.begin(), vec1.end(), 0);
int N = 42;
std::vector<double> vec2(vec1.size()); // vec2 needs to be as big or bigger than vec1
std::transform(vec1.begin(), vec1.end(), vec2.begin(),
[N](int i) { return i * M_PI / N; });
for (auto a : vec1)
std::cout << a << " ";
std::cout << std::endl;
for (auto a : vec2)
std::cout << a << " ";
std::cout << std::endl;
}
Here's an online example: http://melpon.org/wandbox/permlink/XrNxDND0steJmym8
If I have understood you correctly you need the following
std::vector<double> v;
v.reserve(n.size());
std::transform(n.begin(), n.end(), std::back_inserter( v ),
std::bind1st(std::multiplies<double>(), pin));
You can pass the scalar in the capture clause of the Lambda function and do the multiplication inside the lambda function itself
#include <algorithm>
#include <vector>
std::vector<int> foo;
std::vector<int> bar;
auto npi=4.0;
std::transform (foo.begin(), foo.end(), bar.begin(), foo.begin(), [&npi](auto& c){return c * npi;}

Correct way to compare two pointees to use in std algorithms

I am playing with boost::range and boost::lambda with following example to compare two numbers and get the element out which has same number.
#include <iostream>
#include <boost/optional.hpp>
#include <boost/range/algorithm/find_if.hpp>
#include <boost/typeof/typeof.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/utility/compare_pointees.hpp>
template <class Range, class Predicate>
boost::optional<typename boost::range_value<Range>::type>
search_for(const Range& r, Predicate pred)
{
BOOST_AUTO (it, boost::find_if(r, pred));
if (it == boost::end(r))
return boost::none;
return *it;
}
int main()
{
int a = 1;
int b = 2;
int c = 3;
int d = 3;
std::vector<int*> m = {&a, &b, &c};
if (boost::optional<int*> number =
search_for(m, boost::equal_pointees(???, &d))) {
std::cout << "found:" << (*number.get()) << std::endl;
}
else {
std::cout << "not found" << std::endl;
}
}
What should I use for ??? above in search_for function?
I believe it could be very simple but don't know how to do it. I can use the boost::bind or std::bind2d, etc to compare but was thinking if there is any elegant way to do it. Also, this code sample could be restructured to much simpler one but I am just learning.
With boost::lambda, it looks like this:
namespace ll = boost::lambda;
search_for(m, *ll::_1 == d)
Which is far less complicated than taking a pointer to d just so you can use equal_pointees.