22 std::vector<P> polygons;
25 return std::ranges::any_of(polygons,predicate);
34 constexpr std::optional<std::string> tryAddGetCauseOnError(
const P & polygon)
noexcept {
35 for(
const auto & p : polygons){
37 return "Duplicate polygon: "+p.toString();
38 if(p.crosses(polygon))
39 return "Polygon: "+polygon.toString()+
"\nintersects another polygon:\n"+p.toString();
40 if(p.contains(polygon) || polygon.contains(p))
41 return "Polygons are contained within each other:\n"+polygon.toString()+
"\n and \n"+p.toString();
43 polygons.push_back(polygon);
47 constexpr void addOrThrow(
const P & polygon) {
48 auto error = tryAddGetCauseOnError(polygon);
67 this->polygons = std::vector<P>(std::ranges::begin(polygonsRange),std::ranges::end(polygonsRange));
70 std::ranges::for_each(polygonsRange,[
this](
const P & p){addOrThrow(p);});
73 this->polygons =
filter(polygonsRange,[](
const P & lhs,
const P & rhs){
74 return not( lhs == rhs || lhs.crosses(rhs) || lhs.contains(rhs) || rhs.contains(lhs));
84 addOrThrow(otherPolygon);
95 template<
typename... Args>
101 return std::views::all(polygons);
112 template<
bool checked=false>
114 if constexpr(checked){
115 this->polygons.push_back(polygon);
118 return not tryAddGetCauseOnError(polygon).has_value();
123 auto [position,end] = std::ranges::remove(polygons,polygon);
126 polygons.erase(position,end);
132 auto viewOnPolygonArea = std::views::all(polygons) | std::views::transform([](
const P & p){
return p.area();});
133 return std::accumulate(std::ranges::begin(viewOnPolygonArea),std::ranges::end(viewOnPolygonArea),0.0);
142 Vec2DReal accumulatedWeightedCentroid {0.0,0.0};
144 for(
const auto & polygon: polygons) {
145 accumulatedWeightedCentroid = accumulatedWeightedCentroid + polygon.centroid() * (polygon.area() / totalArea);
147 return accumulatedWeightedCentroid;
151 return anyOf([&point](
const P & p){
return p.contains(point);});
155 return anyOf([&segment](
const P & p){
return p.contains(segment);});
159 return anyOf([&point](
const P & p){
return p.isInside(point);});
163 return anyOf([&point](
const P & p){
return p.isOnBoundary(point);});
167 return std::ranges::all_of(polygons,[&point](
const P & p){
return p.isOutside(point);});
171 return anyOf([&linearFeature](
const P & p){
return p.intersects(linearFeature);});
175 std::unordered_set<Vec2DReal> intersectionSet;
176 std::ranges::for_each(polygons,[&intersectionSet,&linearGeometry](
const P & p){
177 std::ranges::for_each(p.intersections(linearGeometry),[&intersectionSet](
const auto & point){
178 intersectionSet.insert(point);
181 return intersectionSet;
185 return anyOf([&query](
const P & p){
return p.contains(query);});
189 return anyOf([&query](
const P & p){
return p.crosses(query);});
193 return not
contains(query) && not
crosses(query) && anyOf([&query](
const P & p){
return p.touches(query);});
197 return anyOf([&query](
const P & p){
return p.containsInHole(query);});
201 return std::ranges::min(this->
getPolygons() | std::views::transform([&query](
const P & p){
return p.distance(query);}));
205 return std::ranges::min( other.getPolygons() | std::views::transform([
this](
const auto & otherPolygon){return this->distance(otherPolygon);}));
209 auto initialPoint = std::ranges::begin(this->polygons.at(0).getBoundary().getSegments())->p();
215 | std::views::transform([](
const P & p){
return p.getBoundary().getSegments();})
217 for(
const auto s: viewOnAllSegments){
235 for(
const auto & p: other.getPolygons()){
236 auto foundIt = std::ranges::find(this->polygons,p);
237 if(foundIt == std::ranges::end(this->
getPolygons()))
243 constexpr size_t hash() const noexcept {
244 auto polygonHashes = this->
getPolygons() | std::views::transform([](
const auto & p){
return p.hash();});
245 return std::accumulate(std::ranges::begin(polygonHashes),std::ranges::end(polygonHashes),
size_t(0));
249 std::ostringstream oss;
252 for(
const auto & p : this->polygons){
253 if(!first) oss <<
"|";
Exception class for geometry-related errors.
Definition InvalidGeometryException.hpp:10
Implementation of a Multi-Polygon.
Definition MultiPolygon.hpp:20
constexpr Rectangle< numeric_type > aaBB() const noexcept
Definition MultiPolygon.hpp:208
P polygon_type
Definition MultiPolygon.hpp:56
constexpr fishnet::math::DEFAULT_FLOATING_POINT distance(IMultiPolygon auto const &other) const noexcept
Definition MultiPolygon.hpp:204
constexpr util::view_of< P > auto getPolygons() const noexcept
Definition MultiPolygon.hpp:100
constexpr bool contains(IPolygon auto const &query) const noexcept
Definition MultiPolygon.hpp:184
static constexpr GeometryType type
Definition MultiPolygon.hpp:57
MultiPolygon(util::input_range_of< P > auto &&polygonsRange, bool checked=false)
Construct a new Multi Polygon object.
Definition MultiPolygon.hpp:65
constexpr bool removePolygon(const P &polygon) noexcept
Definition MultiPolygon.hpp:122
constexpr bool isInside(IPoint auto const &point) const noexcept
Definition MultiPolygon.hpp:158
constexpr fishnet::math::DEFAULT_FLOATING_POINT distance(IPolygon auto const &query) const noexcept
Definition MultiPolygon.hpp:200
constexpr bool operator==(const MultiPolygon< Q > &other) const noexcept
Definition MultiPolygon.hpp:232
MultiPolygon(const P ¤t, const P &next, Args... remaining)
VarArg constructor of a new Multi Polygon object.
Definition MultiPolygon.hpp:96
constexpr bool isOnBoundary(IPoint auto const &point) const noexcept
Definition MultiPolygon.hpp:162
constexpr std::string toString() const noexcept
Definition MultiPolygon.hpp:248
constexpr Vec2DReal centroid() const noexcept
Computes centroid point of multi-polygon, by weighting the centroid point of each polygon with its ar...
Definition MultiPolygon.hpp:141
constexpr fishnet::math::DEFAULT_FLOATING_POINT area() const noexcept
Definition MultiPolygon.hpp:131
constexpr bool containsInHole(IPolygon auto const &query) const noexcept
Definition MultiPolygon.hpp:196
constexpr bool crosses(IPolygon auto const &query) const noexcept
Definition MultiPolygon.hpp:188
constexpr size_t hash() const noexcept
Definition MultiPolygon.hpp:243
constexpr bool touches(IPolygon auto const &query) const noexcept
Definition MultiPolygon.hpp:192
constexpr std::unordered_set< Vec2DReal > intersections(LinearGeometry auto const &linearGeometry) const noexcept
Definition MultiPolygon.hpp:174
constexpr bool contains(ISegment auto const &segment) const noexcept
Definition MultiPolygon.hpp:154
constexpr bool contains(IPoint auto const &point) const noexcept
Definition MultiPolygon.hpp:150
typename P::numeric_type numeric_type
Definition MultiPolygon.hpp:55
MultiPolygon(const P &polygon, const P &otherPolygon)
Definition MultiPolygon.hpp:82
constexpr bool addPolygon(const P &polygon) noexcept
Add Polygon to the multi-polygon.
Definition MultiPolygon.hpp:113
constexpr bool intersects(LinearGeometry auto const &linearFeature) const noexcept
Definition MultiPolygon.hpp:170
MultiPolygon(const P &polygon)
Definition MultiPolygon.hpp:80
constexpr bool isOutside(IPoint auto const &point) const noexcept
Definition MultiPolygon.hpp:166
Implementation of a rectangle Stores top,right,bottom and left point of the rectangle.
Definition Rectangle.hpp:15
Implementation of a two-dimensional vector.
Definition Vec2D.hpp:16
Interface for a multi-polygon A multi-polygon is a collection of polygons.
Definition IMultiPolygon.hpp:93
Interface for a point.
Definition IPoint.hpp:92
Interface for a polygon A polygon is a two-dimensional shape defined by a boundary and zero or more h...
Definition IPolygon.hpp:98
Interface for a segment A segment is formed between two points p and q, containing only the points on...
Definition ISegment.hpp:105
Definition IGeometry.hpp:13
Definition FunctionalConcepts.hpp:36
Definition CollectionConcepts.hpp:45
Definition BoundingBoxWrapper.hpp:5
GeometryType
Definition GeometryBase.hpp:6
static std::vector< std::ranges::range_value_t< R > > filter(const R &polygons, BinaryFilter binaryCondition, Filter condition=Filter()) noexcept
Filters a range of polygons and returns a list of all polygons that pass the filter(s)
Definition PolygonFilter.hpp:98
double DEFAULT_FLOATING_POINT
Definition NumericConcepts.hpp:12
constexpr size_t size(std::ranges::range auto &&range) noexcept
Definition CollectionConcepts.hpp:50