QhullPoints.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (c) 2009-2011 C.B. Barber. All rights reserved.
00004 ** $Id: //main/2011/qhull/src/libqhullcpp/QhullPoints.h#4 $$Change: 1348 $
00005 ** $DateTime: 2011/03/25 23:54:58 $$Author: bbarber $
00006 **
00007 ****************************************************************************/
00008 
00009 #ifndef QHULLPOINTS_H
00010 #define QHULLPOINTS_H
00011 
00012 #include "QhullPoint.h"
00013 extern "C" {
00014     #include "libqhull/qhull_a.h"
00015 }
00016 
00017 #include <ostream>
00018 
00019 namespace orgQhull {
00020 
00021 #//Types
00022 
00023     // with const_iterator and iterator
00024     class QhullPoints;
00026     class QhullPointsIterator;
00027 
00028 class QhullPoints {
00029 
00030     // QhullPoints consists of pointers into an array of coordinates.
00031 
00032 private:
00033 #//Field
00034     coordT             *point_first;
00035     coordT             *point_end;  // end>=first.  Trailing coordinates ignored
00036     int                 point_dimension;  // >= 0
00037 
00038 public:
00039 #//Subtypes
00040     class               const_iterator;
00041     class               iterator;
00042     typedef QhullPoints::const_iterator ConstIterator;
00043     typedef QhullPoints::iterator Iterator;
00044 
00045 #//Construct
00046                         QhullPoints() : point_first(0), point_end(0), point_dimension(0) {};
00047                         QhullPoints(int pointDimension) : point_first(0), point_end(0), point_dimension(pointDimension) { QHULL_ASSERT(pointDimension>=0); }
00048                         QhullPoints(int pointDimension, int coordinateCount2, coordT *c) : point_first(c), point_end(c+coordinateCount2), point_dimension(pointDimension) { QHULL_ASSERT(pointDimension>=0 && coordinateCount2>=0 ); }
00049                         //Copy constructor copies pointers but not contents.  Needed for return by value and parameter passing.
00050                         QhullPoints(const QhullPoints &other)  : point_first(other.point_first), point_end(other.point_end), point_dimension(other.point_dimension) {}
00051                        ~QhullPoints() {}
00052 
00053 //disabled since p= p2 is ambiguous (coord* vs coord)
00054 private:
00055     QhullPoints        &operator=(const QhullPoints &other) { point_first= other.point_first; point_end= other.point_end; point_dimension= other.point_dimension; return *this; }
00056 public:
00057 
00058 #//Conversion
00059     const coordT       *constData() const { return coordinates(); }
00060     // See coordinates()
00061     coordT             *data() { return coordinates(); }
00062     const coordT       *data() const { return coordinates(); }
00063 #ifndef QHULL_NO_STL
00064     std::vector<QhullPoint> toStdVector() const;
00065 #endif //QHULL_NO_STL
00066 #ifdef QHULL_USES_QT
00067     QList<QhullPoint>   toQList() const;
00068 #endif //QHULL_USES_QT
00069 
00070 #//GetSet
00071     coordT             *coordinates() const { return point_first; }
00072     int                 coordinateCount() const { return (int)(point_end-point_first); } // WARN64
00073     int                 count() const { return (int)size(); } // WARN64
00074     void                defineAs(int pointDimension, int coordinatesCount, coordT *c) { QHULL_ASSERT(pointDimension>=0 && coordinatesCount>=0 && c!=0); point_first= c; point_end= c+coordinatesCount; point_dimension= pointDimension; }
00075     void                defineAs(int coordinatesCount, coordT *c) { QHULL_ASSERT((coordinatesCount>=0 && c!=0) || (c==0 && coordinatesCount==0)); point_first= c; point_end= c+coordinatesCount; }
00076     void                defineAs(const QhullPoints &other) { point_first= other.point_first; point_end= other.point_end; point_dimension= other.point_dimension; }
00077     int                 dimension() const { return point_dimension; }
00078     bool                empty() const { return point_end==point_first; }
00079     coordT             *extraCoordinates() const { return extraCoordinatesCount() ? (point_end-extraCoordinatesCount()) : 0; }
00080     int                 extraCoordinatesCount() const { return point_dimension>0 ? (int)((point_end-point_first)%(size_t)point_dimension) : 0; }  // WARN64
00081     bool                includesCoordinates(const coordT *c) const { return c>=point_first && c<point_end; }
00082     bool                isEmpty() const { return empty(); }
00083     bool                operator==(const QhullPoints &other) const;
00084     bool                operator!=(const QhullPoints &other) const { return !operator==(other); }
00085     void                setDimension(int pointDimension) { QHULL_ASSERT(pointDimension>=0); point_dimension= pointDimension; }
00086     size_t              size() const { return (point_dimension ? (point_end-point_first)/point_dimension : 0); }
00087 
00088 #//ElementAccess -- can not return references to QhullPoint
00089     QhullPoint          at(int idx) const { coordT *p= point_first+idx*point_dimension; QHULL_ASSERT(p<point_end); return QhullPoint(point_dimension, p); }
00090     QhullPoint          back() const { return last(); }
00091     QhullPoint          first() const { return QhullPoint(point_dimension, point_first); }
00092     QhullPoint          front() const { return first(); }
00093     QhullPoint          last() const { return QhullPoint(point_dimension, point_end - point_dimension); }
00095     QhullPoints         mid(int idx, int length= -1) const;
00096     QhullPoint          operator[](int idx) const { return at(idx); }
00097     QhullPoint          value(int idx) const;
00098     // Non-const since copy is an alias
00099     QhullPoint          value(int idx, QhullPoint &defaultValue) const;
00100 
00101 #//Foreach
00102     ConstIterator       begin() const { return ConstIterator(*this); }
00103     Iterator            begin() { return Iterator(*this); }
00104     ConstIterator       constBegin() const { return ConstIterator(*this); }
00105     ConstIterator       constEnd() const { return ConstIterator(point_dimension, point_end); }
00106     ConstIterator       end() const { return ConstIterator(point_dimension, point_end); }
00107     Iterator            end() { return Iterator(point_dimension, point_end); }
00108 
00109 #//Search
00110     bool                contains(const QhullPoint &t) const;
00111     int                 count(const QhullPoint &t) const;
00112     int                 indexOf(const coordT *pointCoordinates) const;
00113     int                 indexOf(const coordT *pointCoordinates, int noThrow) const;
00114     int                 indexOf(const QhullPoint &t) const;
00115     int                 lastIndexOf(const QhullPoint &t) const;
00116 
00117 #//QhullPoints::iterator -- modeled on qvector.h and qlist.h
00118     // before const_iterator for conversion with comparison operators
00119     // See: QhullSet.h
00120     class iterator : public QhullPoint {
00121 
00122     public:
00123         typedef std::random_access_iterator_tag  iterator_category;
00124         typedef QhullPoint  value_type;
00125         typedef value_type *pointer;
00126         typedef value_type &reference;
00127         typedef ptrdiff_t   difference_type;
00128 
00129                         iterator() : QhullPoint() {}
00130                         iterator(const iterator &other): QhullPoint(*other) {}
00131         explicit        iterator(const QhullPoints &ps) : QhullPoint(ps.dimension(), ps.coordinates()) {}
00132         explicit        iterator(int pointDimension, coordT *c): QhullPoint(pointDimension, c) {}
00133         iterator       &operator=(const iterator &other) { defineAs( const_cast<iterator &>(other)); return *this; }
00134         QhullPoint     *operator->() { return this; }
00135         // value instead of reference since advancePoint() modifies self
00136         QhullPoint      operator*() const { return *this; }
00137         QhullPoint      operator[](int idx) const { QhullPoint n= *this; n.advancePoint(idx); return n; }
00138         bool            operator==(const iterator &other) const { QHULL_ASSERT(dimension()==other.dimension()); return coordinates()==other.coordinates(); }
00139         bool            operator!=(const iterator &other) const { return !operator==(other); }
00140         bool            operator<(const iterator &other) const  { QHULL_ASSERT(dimension()==other.dimension()); return coordinates() < other.coordinates(); }
00141         bool            operator<=(const iterator &other) const { QHULL_ASSERT(dimension()==other.dimension()); return coordinates() <= other.coordinates(); }
00142         bool            operator>(const iterator &other) const  { QHULL_ASSERT(dimension()==other.dimension()); return coordinates() > other.coordinates(); }
00143         bool            operator>=(const iterator &other) const { QHULL_ASSERT(dimension()==other.dimension()); return coordinates() >= other.coordinates(); }
00144         // reinterpret_cast to break circular dependency
00145         bool            operator==(const QhullPoints::const_iterator &other) const { QHULL_ASSERT(dimension()==reinterpret_cast<const iterator &>(other).dimension()); return coordinates()==reinterpret_cast<const iterator &>(other).coordinates(); }
00146         bool            operator!=(const QhullPoints::const_iterator &other) const { return !operator==(reinterpret_cast<const iterator &>(other)); }
00147         bool            operator<(const QhullPoints::const_iterator &other) const  { QHULL_ASSERT(dimension()==reinterpret_cast<const iterator &>(other).dimension()); return coordinates() < reinterpret_cast<const iterator &>(other).coordinates(); }
00148         bool            operator<=(const QhullPoints::const_iterator &other) const { QHULL_ASSERT(dimension()==reinterpret_cast<const iterator &>(other).dimension()); return coordinates() <= reinterpret_cast<const iterator &>(other).coordinates(); }
00149         bool            operator>(const QhullPoints::const_iterator &other) const  { QHULL_ASSERT(dimension()==reinterpret_cast<const iterator &>(other).dimension()); return coordinates() > reinterpret_cast<const iterator &>(other).coordinates(); }
00150         bool            operator>=(const QhullPoints::const_iterator &other) const { QHULL_ASSERT(dimension()==reinterpret_cast<const iterator &>(other).dimension()); return coordinates() >= reinterpret_cast<const iterator &>(other).coordinates(); }
00151         iterator       &operator++() { advancePoint(1); return *this; }
00152         iterator        operator++(int) { iterator n= *this; operator++(); return iterator(n); }
00153         iterator       &operator--() { advancePoint(-1); return *this; }
00154         iterator        operator--(int) { iterator n= *this; operator--(); return iterator(n); }
00155         iterator       &operator+=(int idx) { advancePoint(idx); return *this; }
00156         iterator       &operator-=(int idx) { advancePoint(-idx); return *this; }
00157         iterator        operator+(int idx) const { iterator n= *this; n.advancePoint(idx); return n; }
00158         iterator        operator-(int idx) const { iterator n= *this; n.advancePoint(-idx); return n; }
00159         difference_type operator-(iterator other) const { QHULL_ASSERT(dimension()==other.dimension()); return (coordinates()-other.coordinates())/dimension(); }
00160     };//QhullPoints::iterator
00161 
00162 #//QhullPoints::const_iterator -- FIXUP QH11018 const_iterator same as iterator
00163     class const_iterator : public QhullPoint {
00164 
00165     public:
00166         typedef std::random_access_iterator_tag  iterator_category;
00167         typedef QhullPoint          value_type;
00168         typedef const value_type   *pointer;
00169         typedef const value_type   &reference;
00170         typedef ptrdiff_t           difference_type;
00171 
00172                         const_iterator() : QhullPoint() {}
00173                         const_iterator(const const_iterator &other) : QhullPoint(*other) {}
00174                         const_iterator(const QhullPoints::iterator &other) : QhullPoint(*other) {}
00175         explicit        const_iterator(const QhullPoints &ps) : QhullPoint(ps.dimension(), ps.coordinates()) {}
00176         explicit        const_iterator(int pointDimension, coordT *c): QhullPoint(pointDimension, c) {}
00177         const_iterator &operator=(const const_iterator &other) { defineAs(const_cast<const_iterator &>(other)); return *this; }
00178         // value/non-const since advancePoint(1), etc. modifies self
00179         QhullPoint      operator*() const { return *this; }
00180         QhullPoint     *operator->() { return this; }
00181         QhullPoint      operator[](int idx) const { QhullPoint n= *this; n.advancePoint(idx); return n; }
00182         bool            operator==(const const_iterator &other) const { QHULL_ASSERT(dimension()==other.dimension()); return coordinates()==other.coordinates(); }
00183         bool            operator!=(const const_iterator &other) const { return !operator==(other); }
00184         bool            operator<(const const_iterator &other) const  { QHULL_ASSERT(dimension()==other.dimension()); return coordinates() < other.coordinates(); }
00185         bool            operator<=(const const_iterator &other) const { QHULL_ASSERT(dimension()==other.dimension()); return coordinates() <= other.coordinates(); }
00186         bool            operator>(const const_iterator &other) const  { QHULL_ASSERT(dimension()==other.dimension()); return coordinates() > other.coordinates(); }
00187         bool            operator>=(const const_iterator &other) const { QHULL_ASSERT(dimension()==other.dimension()); return coordinates() >= other.coordinates(); }
00188         const_iterator &operator++() { advancePoint(1); return *this; }
00189         const_iterator  operator++(int) { const_iterator n= *this; operator++(); return const_iterator(n); }
00190         const_iterator &operator--() { advancePoint(-1); return *this; }
00191         const_iterator  operator--(int) { const_iterator n= *this; operator--(); return const_iterator(n); }
00192         const_iterator &operator+=(int idx) { advancePoint(idx); return *this; }
00193         const_iterator &operator-=(int idx) { advancePoint(-idx); return *this; }
00194         const_iterator  operator+(int idx) const { const_iterator n= *this; n.advancePoint(idx); return n; }
00195         const_iterator  operator-(int idx) const { const_iterator n= *this; n.advancePoint(-idx); return n; }
00196         difference_type operator-(const_iterator other) const { QHULL_ASSERT(dimension()==other.dimension()); return (coordinates()-other.coordinates())/dimension(); }
00197     };//QhullPoints::const_iterator
00198 
00199 #//IO
00200     struct PrintPoints{
00201         const QhullPoints  *points;
00202         const char     *point_message;
00203         int             run_id;
00204         bool            with_identifier;
00205         PrintPoints(int qhRunId, const char *message, bool withIdentifier, const QhullPoints &ps) : points(&ps), point_message(message), run_id(qhRunId), with_identifier(withIdentifier) {}
00206     };//PrintPoints
00207     PrintPoints          print() const { return  PrintPoints(UsingLibQhull::NOqhRunId, "", false, *this); }
00208     PrintPoints          print(int qhRunId) const { return PrintPoints(qhRunId, "", true, *this); }
00209     PrintPoints          print(int qhRunId, const char *message) const { return PrintPoints(qhRunId, message, false, *this); }
00210     PrintPoints          printWithIdentifier(int qhRunId, const char *message) const { return PrintPoints(qhRunId, message, true, *this); }
00211     //FIXUP remove message for print()?
00212 };//QhullPoints
00213 
00214 // can't use QHULL_DECLARE_SEQUENTIAL_ITERATOR because next(),etc would return a reference to a temporary
00215 class QhullPointsIterator
00216 {
00217     typedef QhullPoints::const_iterator const_iterator;
00218 
00219 private:
00220 #//Fields
00221     const QhullPoints  *ps;
00222     const_iterator      i;
00223 
00224 public:
00225                         QhullPointsIterator(const QhullPoints &other) : ps(&other), i(ps->constBegin()) {}
00226     QhullPointsIterator &operator=(const QhullPoints &other) { ps = &other; i = ps->constBegin(); return *this; }
00227     bool                findNext(const QhullPoint &t);
00228     bool                findPrevious(const QhullPoint &t);
00229     bool                hasNext() const { return i != ps->constEnd(); }
00230     bool                hasPrevious() const { return i != ps->constBegin(); }
00231     QhullPoint          next() { return *i++; }
00232     QhullPoint          peekNext() const { return *i; }
00233     QhullPoint          peekPrevious() const { const_iterator p = i; return *--p; }
00234     QhullPoint          previous() { return *--i; }
00235     void                toBack() { i = ps->constEnd(); }
00236     void                toFront() { i = ps->constBegin(); }
00237 };//QhullPointsIterator
00238 
00239 }//namespace orgQhull
00240 
00241 #//Global functions
00242 
00243 std::ostream           &operator<<(std::ostream &os, const orgQhull::QhullPoints &p);
00244 std::ostream           &operator<<(std::ostream &os, const orgQhull::QhullPoints::PrintPoints &pr);
00245 
00246 #endif // QHULLPOINTS_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


libqhull
Author(s): Robert Krug
autogenerated on Tue Jun 18 2013 12:38:50