GEOS 3.15.0dev
CoordinateSequence.h
1/**********************************************************************
2 *
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
5 *
6 * Copyright (C) 2022 ISciences LLC
7 * Copyright (C) 2006 Refractions Research Inc.
8 *
9 * This is free software; you can redistribute and/or modify it under
10 * the terms of the GNU Lesser General Public Licence as published
11 * by the Free Software Foundation.
12 * See the COPYING file for more information.
13 *
14 **********************************************************************/
15
16#pragma once
17
18#include <geos/export.h>
19
20#include <geos/geom/Coordinate.h> // for applyCoordinateFilter
21#include <geos/geom/CoordinateSequenceIterator.h>
22
23#include <cassert>
24#include <vector>
25#include <iostream>
26#include <iosfwd> // ostream
27#include <memory> // for unique_ptr typedef
28
29// Forward declarations
30namespace geos {
31namespace geom {
32class Envelope;
33class CoordinateFilter;
34}
35}
36
37namespace geos {
38namespace geom { // geos::geom
39
56class GEOS_DLL CoordinateSequence {
57
58public:
59
61 enum { X, Y, Z, M };
62
63 using iterator = CoordinateSequenceIterator<CoordinateSequence, Coordinate>;
64 using const_iterator = CoordinateSequenceIterator<const CoordinateSequence, const Coordinate>;
65
66 typedef std::unique_ptr<CoordinateSequence> Ptr;
67
70
75
83 CoordinateSequence(std::size_t size, std::size_t dim = 0);
84
96 CoordinateSequence(std::size_t size, bool hasz, bool hasm, bool initialize = true);
97
103 CoordinateSequence(const std::initializer_list<Coordinate>&);
104
109 CoordinateSequence(const std::initializer_list<CoordinateXY>&);
110
116 CoordinateSequence(const std::initializer_list<CoordinateXYM>&);
117
121 CoordinateSequence(const std::initializer_list<CoordinateXYZM>&);
122
128 static CoordinateSequence XY(std::size_t size) {
129 return CoordinateSequence(size, false, false);
130 }
131
137 static CoordinateSequence XYZ(std::size_t size) {
138 return CoordinateSequence(size, true, false);
139 }
140
146 static CoordinateSequence XYZM(std::size_t size) {
147 return CoordinateSequence(size, true, true);
148 }
149
155 static CoordinateSequence XYM(std::size_t size) {
156 return CoordinateSequence(size, false, true);
157 }
158
162 std::unique_ptr<CoordinateSequence> clone() const;
163
167
174
178 std::size_t getSize() const {
179 return size();
180 }
181
185 size_t size() const
186 {
187 assert(stride() == 2 || stride() == 3 || stride() == 4);
188 switch(stride()) {
189 case 2: return m_vect.size() / 2;
190 case 4: return m_vect.size() / 4;
191 default : return m_vect.size() / 3;
192 }
193 }
194
196 bool isEmpty() const {
197 return m_vect.empty();
198 }
199
206 bool isRing() const;
207
214 std::size_t getDimension() const;
215
216 bool hasZ() const {
217 return m_hasdim ? m_hasz : (m_vect.empty() || !std::isnan(m_vect[2]));
218 }
219
220 bool hasM() const {
221 return m_hasm;
222 }
223
225 bool hasRepeatedPoints() const;
226
229
233 CoordinateType getCoordinateType() const {
234 switch(stride()) {
235 case 4: return CoordinateType::XYZM;
236 case 2: return CoordinateType::XY;
237 default: return hasM() ? CoordinateType::XYM : CoordinateType::XYZ;
238 }
239 }
240
244
248 template<typename T=Coordinate>
249 const T& getAt(std::size_t i) const {
250 static_assert(std::is_base_of<CoordinateXY, T>::value, "Must be a Coordinate class");
251 assert(sizeof(T) <= sizeof(double) * stride());
252 assert(i*stride() < m_vect.size());
253 const T* orig = reinterpret_cast<const T*>(&m_vect[i*stride()]);
254 return *orig;
255 }
256
260 template<typename T=Coordinate>
261 T& getAt(std::size_t i) {
262 static_assert(std::is_base_of<CoordinateXY, T>::value, "Must be a Coordinate class");
263 assert(sizeof(T) <= sizeof(double) * stride());
264 assert(i*stride() < m_vect.size());
265 T* orig = reinterpret_cast<T*>(&m_vect[i*stride()]);
266 return *orig;
267 }
268
272 template<typename T>
273 void getAt(std::size_t i, T& c) const {
274 switch(getCoordinateType()) {
275 case CoordinateType::XY: c = getAt<CoordinateXY>(i); break;
276 case CoordinateType::XYZ: c = getAt<Coordinate>(i); break;
277 case CoordinateType::XYZM: c = getAt<CoordinateXYZM>(i); break;
278 case CoordinateType::XYM: c = getAt<CoordinateXYM>(i); break;
279 default: getAt<Coordinate>(i);
280 }
281 }
282
283 void getAt(std::size_t i, CoordinateXY& c) const {
284 c = getAt<CoordinateXY>(i);
285 }
286
287 // TODO: change to return CoordinateXY
291 const Coordinate& operator[](std::size_t i) const
292 {
293 return getAt(i);
294 }
295
296 // TODO: change to return CoordinateXY
301 operator[](std::size_t i)
302 {
303 return getAt(i);
304 }
305
316 double getOrdinate(std::size_t index, std::size_t ordinateIndex) const;
317
324 double getX(std::size_t index) const
325 {
326 return m_vect[index * stride()];
327 }
328
335 double getY(std::size_t index) const
336 {
337 return m_vect[index * stride() + 1];
338 }
339
347 double getZ(std::size_t index) const
348 {
349 return getOrdinate(index, Z);
350 }
351
359 double getM(std::size_t index) const
360 {
361 return getOrdinate(index, M);
362 }
363
370 void setX(std::size_t index, double x)
371 {
372 m_vect[index * stride()] = x;
373 }
374
381 void setY(std::size_t index, double y)
382 {
383 m_vect[index * stride() + 1] = y;
384 }
385
394 void setZ(std::size_t index, double z)
395 {
396 if (hasZ())
397 setOrdinate(index, Z, z);
398 }
399
408 void setM(std::size_t index, double m)
409 {
410 if (hasM())
411 setOrdinate(index, M, m);
412 }
413
415 template<typename T=Coordinate>
416 const T& back() const
417 {
418 return getAt<T>(size() - 1);
419 }
420
422 template<typename T=Coordinate>
423 T& back()
424 {
425 return getAt<T>(size() - 1);
426 }
427
429 template<typename T=Coordinate>
430 const T& front() const
431 {
432 return *(reinterpret_cast<const T*>(m_vect.data()));
433 }
434
436 template<typename T=Coordinate>
437 T& front()
438 {
439 return *(reinterpret_cast<T*>(m_vect.data()));
440 }
441
443 void toVector(std::vector<Coordinate>& coords) const;
444
445 void toVector(std::vector<CoordinateXY>& coords) const;
446
447
451
453 template<typename T>
454 void setAt(const T& c, std::size_t pos) {
455 switch(getCoordinateType()) {
456 case CoordinateType::XY: setAtImpl<CoordinateXY>(c, pos); break;
457 case CoordinateType::XYZ: setAtImpl<Coordinate>(c, pos); break;
458 case CoordinateType::XYZM: setAtImpl<CoordinateXYZM>(c, pos); break;
459 case CoordinateType::XYM: setAtImpl<CoordinateXYM>(c, pos); break;
460 default: setAtImpl<Coordinate>(c, pos);
461 }
462 }
463
472 void setOrdinate(std::size_t index, std::size_t ordinateIndex, double value);
473
475 void setPoints(const std::vector<Coordinate>& v);
476
478 void setPoints(const std::vector<CoordinateXY>& v);
479
483
488 template<typename T=Coordinate>
489 void add(const T& c) {
490 add(c, size());
491 }
492
499 template<typename T>
500 void add(const T& c, bool allowRepeated)
501 {
502 if(!allowRepeated && !isEmpty()) {
503 const CoordinateXY& last = back<CoordinateXY>();
504 if(last.equals2D(c)) {
505 return;
506 }
507 }
508
509 add(c);
510 }
511
520 template<typename T>
521 void add(const T& c, std::size_t pos)
522 {
523 static_assert(std::is_base_of<CoordinateXY, T>::value, "Must be a Coordinate class");
524
525 // c may be a reference inside m_vect, so we make sure it will not
526 // grow before adding it
527 if (m_vect.size() + stride() <= m_vect.capacity()) {
528 make_space(pos, 1);
529 setAt(c, static_cast<std::size_t>(pos));
530 } else {
531 T tmp{c};
532 make_space(pos, 1);
533 setAt(tmp, static_cast<std::size_t>(pos));
534 }
535 }
536
546 template<typename T>
547 void add(std::size_t i, const T& coord, bool allowRepeated)
548 {
549 // don't add duplicate coordinates
550 if(! allowRepeated) {
551 std::size_t sz = size();
552 if(sz > 0) {
553 if(i > 0) {
554 const CoordinateXY& prev = getAt<CoordinateXY>(i - 1);
555 if(prev.equals2D(coord)) {
556 return;
557 }
558 }
559 if(i < sz) {
560 const CoordinateXY& next = getAt<CoordinateXY>(i);
561 if(next.equals2D(coord)) {
562 return;
563 }
564 }
565 }
566 }
567
568 add(coord, i);
569 }
570
571 void add(double x, double y) {
572 CoordinateXY c(x, y);
573 add(c);
574 }
575
576 void add(const CoordinateSequence& cs);
577
578 void add(const CoordinateSequence& cs, bool allowRepeated);
579
580 void add(const CoordinateSequence& cl, bool allowRepeated, bool forwardDirection);
581
582 void add(const CoordinateSequence& cs, std::size_t from, std::size_t to);
583
584 void add(const CoordinateSequence& cs, std::size_t from, std::size_t to, bool allowRepeated);
585
586 template<typename T, typename... Args>
587 void add(T begin, T end, Args... args) {
588 for (auto it = begin; it != end; ++it) {
589 add(*it, args...);
590 }
591 }
592
593 template<typename T>
594 void add(std::size_t i, T from, T to) {
595 auto npts = static_cast<std::size_t>(std::distance(from, to));
596 make_space(i, npts);
597
598 for (auto it = from; it != to; ++it) {
599 setAt(*it, i);
600 i++;
601 }
602 }
603
607
608 void clear() {
609 m_vect.clear();
610 }
611
612 void reserve(std::size_t capacity) {
613 m_vect.reserve(capacity * stride());
614 }
615
616 void resize(std::size_t capacity) {
617 m_vect.resize(capacity * stride());
618 }
619
620 void pop_back();
621
623 std::string toString() const;
624
626 const CoordinateXY* minCoordinate() const;
627
634
636 //
639 static std::size_t indexOf(const CoordinateXY* coordinate,
640 const CoordinateSequence* cl);
641
647 static bool equals(const CoordinateSequence* cl1,
648 const CoordinateSequence* cl2);
649
655 bool equalsIdentical(const CoordinateSequence& other) const;
656
658 static void scroll(CoordinateSequence* cl, const CoordinateXY* firstCoordinate);
659
678
680 void reverse();
681
682 void sort();
683
685 void swap(std::size_t i, std::size_t j);
686
692 void expandEnvelope(Envelope& env) const;
693
694 void closeRing(bool allowRepeated = false);
695
699
700 template<typename Filter>
701 void apply_rw(const Filter* filter) {
702 switch(getCoordinateType()) {
703 case CoordinateType::XY:
704 for (auto& c : items<CoordinateXY>()) {
705 if (filter->isDone()) break;
706 filter->filter_rw(&c);
707 }
708 break;
709 case CoordinateType::XYZ:
710 for (auto& c : items<Coordinate>()) {
711 if (filter->isDone()) break;
712 filter->filter_rw(&c);
713 }
714 break;
715 case CoordinateType::XYM:
716 for (auto& c : items<CoordinateXYM>()) {
717 if (filter->isDone()) break;
718 filter->filter_rw(&c);
719 }
720 break;
721 case CoordinateType::XYZM:
722 for (auto& c : items<CoordinateXYZM>()) {
723 if (filter->isDone()) break;
724 filter->filter_rw(&c);
725 }
726 break;
727 }
728 m_hasdim = m_hasz = false; // re-check (see http://trac.osgeo.org/geos/ticket/435)
729 }
730
731 template<typename Filter>
732 void apply_ro(Filter* filter) const {
733 switch(getCoordinateType()) {
734 case CoordinateType::XY:
735 for (const auto& c : items<CoordinateXY>()) {
736 if (filter->isDone()) break;
737 filter->filter_ro(&c);
738 }
739 break;
740 case CoordinateType::XYZ:
741 for (const auto& c : items<Coordinate>()) {
742 if (filter->isDone()) break;
743 filter->filter_ro(&c);
744 }
745 break;
746 case CoordinateType::XYM:
747 for (const auto& c : items<CoordinateXYM>()) {
748 if (filter->isDone()) break;
749 filter->filter_ro(&c);
750 }
751 break;
752 case CoordinateType::XYZM:
753 for (const auto& c : items<CoordinateXYZM>()) {
754 if (filter->isDone()) break;
755 filter->filter_ro(&c);
756 }
757 break;
758 }
759 }
760
761 template<typename F>
762 auto applyAt(size_t i, F&& fun) const {
763 switch(getCoordinateType()) {
764 case CoordinateType::XYZ: return fun(getAt<Coordinate>(i));
765 case CoordinateType::XYM: return fun(getAt<CoordinateXYM>(i));
766 case CoordinateType::XYZM: return fun(getAt<CoordinateXYZM>(i));
767 default: return fun(getAt<CoordinateXY>(i));
768 }
769 }
770
771 template<typename F>
772 void forEach(F&& fun) const {
773 switch(getCoordinateType()) {
774 case CoordinateType::XY: for (const auto& c : items<CoordinateXY>()) { fun(c); } break;
775 case CoordinateType::XYZ: for (const auto& c : items<Coordinate>()) { fun(c); } break;
776 case CoordinateType::XYM: for (const auto& c : items<CoordinateXYM>()) { fun(c); } break;
777 case CoordinateType::XYZM: for (const auto& c : items<CoordinateXYZM>()) { fun(c); } break;
778 }
779 }
780
781 template<typename T, typename F>
782 void forEach(F&& fun) const
783 {
784 for (std::size_t i = 0; i < size(); i++) {
785 fun(getAt<T>(i));
786 }
787 }
788
789 template<typename F>
790 void forEachSegment(F&& fun) const {
791 switch(getCoordinateType()) {
792 case CoordinateType::XY: forEachSegmentImpl<CoordinateXY, F>(std::move(fun)); break;
793 case CoordinateType::XYZ: forEachSegmentImpl<Coordinate, F>(std::move(fun)); break;
794 case CoordinateType::XYM: forEachSegmentImpl<CoordinateXYM, F>(std::move(fun)); break;
795 case CoordinateType::XYZM: forEachSegmentImpl<CoordinateXYZM, F>(std::move(fun)); break;
796 }
797 }
798
799 template<typename T, typename F>
800 void forEach(std::size_t from, std::size_t to, F&& fun) const
801 {
802 for (std::size_t i = from; i <= to; i++) {
803 fun(getAt<T>(i));
804 }
805 }
806
807 template<typename T>
808 class Coordinates {
809 public:
810 using SequenceType = typename std::conditional<std::is_const<T>::value, const CoordinateSequence, CoordinateSequence>::type;
811
812 explicit Coordinates(SequenceType* seq) : m_seq(seq) {}
813
814 CoordinateSequenceIterator<SequenceType, T> begin() {
815 return {m_seq};
816 }
817
818 CoordinateSequenceIterator<SequenceType, T> end() {
819 return {m_seq, m_seq->getSize()};
820 }
821
822 CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>
823 begin() const {
824 return CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>{m_seq};
825 }
826
827 CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>
828 end() const {
829 return CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>{m_seq, m_seq->getSize()};
830 }
831
832 CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>
833 cbegin() const {
834 return CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>{m_seq};
835 }
836
837 CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>
838 cend() const {
839 return CoordinateSequenceIterator<const SequenceType, typename std::add_const<T>::type>{m_seq, m_seq->getSize()};
840 }
841
842 private:
843 SequenceType* m_seq;
844 };
845
846 template<typename T>
847 Coordinates<typename std::add_const<T>::type> items() const {
848 return Coordinates<typename std::add_const<T>::type>(this);
849 }
850
851 template<typename T>
852 Coordinates<T> items() {
853 return Coordinates<T>(this);
854 }
855
856
858
859 double* data() {
860 return m_vect.data();
861 }
862
863 const double* data() const {
864 return m_vect.data();
865 }
866
867private:
868 std::vector<double> m_vect; // Vector to store values
869
870 uint8_t m_stride; // Stride of stored values, corresponding to underlying type
871
872 mutable bool m_hasdim; // Has the dimension of this sequence been determined? Or was it created with no
873 // explicit dimensionality, and we're waiting for getDimension() to be called
874 // after some coordinates have been added?
875 mutable bool m_hasz;
876 bool m_hasm;
877
878 void initialize();
879
880 template<typename T1, typename T2>
881 void setAtImpl(const T2& c, std::size_t pos) {
882 auto& orig = getAt<T1>(pos);
883 orig = c;
884 }
885
886 template<typename CoordType, typename F>
887 void forEachSegmentImpl(F&& fun) const {
888 auto p0it = items<CoordType>().cbegin();
889 const auto end = items<CoordType>().end();
890
891 if (p0it == end) {
892 return;
893 }
894
895 auto p1it = std::next(p0it);
896
897 while (p1it != end) {
898 fun(*p0it, *p1it);
899 ++p0it;
900 ++p1it;
901 }
902 }
903
904 void make_space(std::size_t pos, std::size_t n) {
905 m_vect.insert(std::next(m_vect.begin(), static_cast<std::ptrdiff_t>(pos * stride())),
906 m_stride * n,
907 DoubleNotANumber);
908 }
909
910 std::uint8_t stride() const {
911 return m_stride;
912 }
913
914};
915
916GEOS_DLL std::ostream& operator<< (std::ostream& os, const CoordinateSequence& cs);
917
918GEOS_DLL bool operator== (const CoordinateSequence& s1, const CoordinateSequence& s2);
919
920GEOS_DLL bool operator!= (const CoordinateSequence& s1, const CoordinateSequence& s2);
921
922} // namespace geos::geom
923} // namespace geos
924
The internal representation of a list of coordinates inside a Geometry.
Definition CoordinateSequence.h:56
Coordinate is the lightweight class used to store coordinates.
Definition Coordinate.h:220
An Envelope defines a rectangulare region of the 2D coordinate plane.
Definition Envelope.h:59
T & getAt(std::size_t i)
Returns a reference to Coordinate at position i.
Definition CoordinateSequence.h:261
Coordinate & operator[](std::size_t i)
Definition CoordinateSequence.h:301
void setM(std::size_t index, double m)
Definition CoordinateSequence.h:408
void toVector(std::vector< Coordinate > &coords) const
Pushes all Coordinates of this sequence into the provided vector.
double getOrdinate(std::size_t index, std::size_t ordinateIndex) const
const T & front() const
Return first Coordinate in the sequence.
Definition CoordinateSequence.h:430
void setX(std::size_t index, double x)
Definition CoordinateSequence.h:370
T & back()
Return last Coordinate in the sequence.
Definition CoordinateSequence.h:423
void getAt(std::size_t i, T &c) const
Write Coordinate at position i to given Coordinate.
Definition CoordinateSequence.h:273
const T & getAt(std::size_t i) const
Returns a read-only reference to Coordinate at position i.
Definition CoordinateSequence.h:249
T & front()
Return first Coordinate in the sequence.
Definition CoordinateSequence.h:437
double getZ(std::size_t index) const
Definition CoordinateSequence.h:347
double getX(std::size_t index) const
Definition CoordinateSequence.h:324
void setY(std::size_t index, double y)
Definition CoordinateSequence.h:381
double getY(std::size_t index) const
Definition CoordinateSequence.h:335
void setZ(std::size_t index, double z)
Definition CoordinateSequence.h:394
const Coordinate & operator[](std::size_t i) const
Definition CoordinateSequence.h:291
const T & back() const
Return last Coordinate in the sequence.
Definition CoordinateSequence.h:416
double getM(std::size_t index) const
Definition CoordinateSequence.h:359
void add(const T &c, bool allowRepeated)
Definition CoordinateSequence.h:500
void add(std::size_t i, const T &coord, bool allowRepeated)
Inserts the specified coordinate at the specified position in this list.
Definition CoordinateSequence.h:547
void add(const T &c)
Definition CoordinateSequence.h:489
void add(const T &c, std::size_t pos)
Inserts the specified coordinate at the specified position in this sequence. If multiple coordinates ...
Definition CoordinateSequence.h:521
CoordinateSequence(const std::initializer_list< CoordinateXYZM > &)
static CoordinateSequence XYM(std::size_t size)
Definition CoordinateSequence.h:155
CoordinateSequence(const std::initializer_list< CoordinateXY > &)
std::unique_ptr< CoordinateSequence > clone() const
Returns a heap-allocated deep copy of this CoordinateSequence.
CoordinateSequence(std::size_t size, bool hasz, bool hasm, bool initialize=true)
static CoordinateSequence XYZM(std::size_t size)
Definition CoordinateSequence.h:146
static CoordinateSequence XYZ(std::size_t size)
Definition CoordinateSequence.h:137
static CoordinateSequence XY(std::size_t size)
Definition CoordinateSequence.h:128
CoordinateSequence(std::size_t size, std::size_t dim=0)
CoordinateSequence(const std::initializer_list< CoordinateXYM > &)
CoordinateSequence(const std::initializer_list< Coordinate > &)
void setOrdinate(std::size_t index, std::size_t ordinateIndex, double value)
void setPoints(const std::vector< Coordinate > &v)
Substitute Coordinate list with a copy of the given vector.
void setAt(const T &c, std::size_t pos)
Copy Coordinate c to position pos.
Definition CoordinateSequence.h:454
void setPoints(const std::vector< CoordinateXY > &v)
Substitute Coordinate list with a copy of the given vector.
std::size_t getSize() const
Returns the number of Coordinates.
Definition CoordinateSequence.h:178
bool hasRepeatedOrInvalidPoints() const
Returns true if contains any NaN/Inf coordinates.
size_t size() const
Returns the number of Coordinates.
Definition CoordinateSequence.h:185
bool hasRepeatedPoints() const
Returns true if contains any two consecutive points.
CoordinateType getCoordinateType() const
Definition CoordinateSequence.h:233
bool isEmpty() const
Returns true if list contains no coordinates.
Definition CoordinateSequence.h:196
bool isRing() const
Tests whether an a CoordinateSequence forms a ring, by checking length and closure....
std::size_t getDimension() const
bool equalsIdentical(const CoordinateSequence &other) const
Returns true if the two sequences are identical (pointwise equal in all dimensions,...
void reverse()
Reverse Coordinate order in given CoordinateSequence.
static void scroll(CoordinateSequence *cl, const CoordinateXY *firstCoordinate)
Scroll given CoordinateSequence so to start with given Coordinate.
const CoordinateXY * minCoordinate() const
Returns lower-left Coordinate in list.
static int increasingDirection(const CoordinateSequence &pts)
Determines which orientation of the Coordinate array is (overall) increasing.
void expandEnvelope(Envelope &env) const
static CoordinateSequence * atLeastNCoordinatesOrNothing(std::size_t n, CoordinateSequence *c)
Returns either the given CoordinateSequence if its length is greater than the given amount,...
std::string toString() const
Get a string representation of CoordinateSequence.
static std::size_t indexOf(const CoordinateXY *coordinate, const CoordinateSequence *cl)
Return position of a Coordinate.
static bool equals(const CoordinateSequence *cl1, const CoordinateSequence *cl2)
Returns true if the two arrays are identical, both null, or pointwise equal in two dimensions.
void swap(std::size_t i, std::size_t j)
Swap the coordinates at two indices.
Basic namespace for all GEOS functionalities.
Definition geos.h:38