[10207] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
| 3 | ** Copyright (c) 2009-2012 C.B. Barber. All rights reserved.
|
---|
| 4 | ** $Id: //main/2011/qhull/src/libqhullcpp/QhullPointSet.cpp#5 $$Change: 1464 $
|
---|
| 5 | ** $DateTime: 2012/01/25 22:58:41 $$Author: bbarber $
|
---|
| 6 | **
|
---|
| 7 | ****************************************************************************/
|
---|
| 8 |
|
---|
| 9 | #include "QhullPointSet.h"
|
---|
| 10 |
|
---|
| 11 | #include <iostream>
|
---|
| 12 | #include <algorithm>
|
---|
| 13 |
|
---|
| 14 | #ifdef _MSC_VER // Microsoft Visual C++ -- warning level 4
|
---|
| 15 | #endif
|
---|
| 16 |
|
---|
| 17 | namespace orgQhull {
|
---|
| 18 |
|
---|
| 19 | #// Conversion
|
---|
| 20 |
|
---|
| 21 | // See qt-qhull.cpp for QList conversion
|
---|
| 22 |
|
---|
| 23 | #ifndef QHULL_NO_STL
|
---|
| 24 | std::vector<QhullPoint> QhullPointSet::
|
---|
| 25 | toStdVector() const
|
---|
| 26 | {
|
---|
| 27 | QhullPointSetIterator i(*this);
|
---|
| 28 | std::vector<QhullPoint> vs;
|
---|
| 29 | while(i.hasNext()){
|
---|
| 30 | vs.push_back(i.next());
|
---|
| 31 | }
|
---|
| 32 | return vs;
|
---|
| 33 | }//toStdVector
|
---|
| 34 | #endif //QHULL_NO_STL
|
---|
| 35 |
|
---|
| 36 | #//Element-access
|
---|
| 37 | //! Derived from QhullSet::value
|
---|
| 38 | QhullPoint QhullPointSet::
|
---|
| 39 | value(int idx) const
|
---|
| 40 | {
|
---|
| 41 | // Avoid call to qh_setsize() and assert in elementPointer()
|
---|
| 42 | //const T *n= reinterpret_cast<const T *>(&SETelem_(getSetT(), idx));
|
---|
| 43 | void **n= reinterpret_cast<void **>(&SETelem_(getSetT(), idx));
|
---|
| 44 | coordT **n2= reinterpret_cast<coordT **>(n);
|
---|
| 45 | if(idx>=0 && n<endPointer()){
|
---|
| 46 | return QhullPoint(dimension(), *n2);
|
---|
| 47 | }else{
|
---|
| 48 | return QhullPoint();
|
---|
| 49 | }
|
---|
| 50 | }//value
|
---|
| 51 |
|
---|
| 52 | //! Non-const since copy is an alias
|
---|
| 53 | //! Derived from QhullSet::value
|
---|
| 54 | QhullPoint QhullPointSet::
|
---|
| 55 | value(int idx, QhullPoint &defaultValue) const
|
---|
| 56 | {
|
---|
| 57 | // Avoid call to qh_setsize() and assert in elementPointer()
|
---|
| 58 | void **n= reinterpret_cast<void **>(&SETelem_(getSetT(), idx));
|
---|
| 59 | coordT **n2= reinterpret_cast<coordT **>(n);
|
---|
| 60 | if(idx>=0 && n<endPointer()){
|
---|
| 61 | return QhullPoint(dimension(), *n2);
|
---|
| 62 | }else{
|
---|
| 63 | return defaultValue;
|
---|
| 64 | }
|
---|
| 65 | }//value
|
---|
| 66 |
|
---|
| 67 | #//Read-only
|
---|
| 68 |
|
---|
| 69 | bool QhullPointSet::
|
---|
| 70 | operator==(const QhullPointSet &o) const
|
---|
| 71 | {
|
---|
| 72 | if(dimension()!=o.dimension() || count()!=o.count()){
|
---|
| 73 | return false;
|
---|
| 74 | }
|
---|
| 75 | QhullPointSetIterator i(*this);
|
---|
| 76 | QhullPointSetIterator j(o);
|
---|
| 77 | while(i.hasNext()){
|
---|
| 78 | if(i.next()!=j.next()){
|
---|
| 79 | return false;
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | return true;
|
---|
| 83 | }//operator==
|
---|
| 84 |
|
---|
| 85 | #//Search
|
---|
| 86 | bool QhullPointSet::
|
---|
| 87 | contains(const QhullPoint &t) const
|
---|
| 88 | {
|
---|
| 89 | QhullPointSetIterator i(*this);
|
---|
| 90 | while(i.hasNext()){
|
---|
| 91 | if(i.next()==t){
|
---|
| 92 | return true;
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | return false;
|
---|
| 96 | }//contains
|
---|
| 97 |
|
---|
| 98 | int QhullPointSet::
|
---|
| 99 | count(const QhullPoint &t) const
|
---|
| 100 | {
|
---|
| 101 | int n= 0;
|
---|
| 102 | QhullPointSetIterator i(*this);
|
---|
| 103 | while(i.hasNext()){
|
---|
| 104 | if(i.next()==t){
|
---|
| 105 | ++n;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | return n;
|
---|
| 109 | }//count
|
---|
| 110 |
|
---|
| 111 | int QhullPointSet::
|
---|
| 112 | indexOf(const QhullPoint &t) const
|
---|
| 113 | {
|
---|
| 114 | int idx= 0;
|
---|
| 115 | QhullPointSetIterator i(*this);
|
---|
| 116 | while(i.hasNext()){
|
---|
| 117 | if(i.next()==t){
|
---|
| 118 | return idx;
|
---|
| 119 | }
|
---|
| 120 | ++idx;
|
---|
| 121 | }
|
---|
| 122 | return -1;
|
---|
| 123 | }//indexOf
|
---|
| 124 |
|
---|
| 125 | int QhullPointSet::
|
---|
| 126 | lastIndexOf(const QhullPoint &t) const
|
---|
| 127 | {
|
---|
| 128 | int idx= count()-1;
|
---|
| 129 | QhullPointSetIterator i(*this);
|
---|
| 130 | i.toBack();
|
---|
| 131 | while(i.hasPrevious()){
|
---|
| 132 | if(i.previous()==t){
|
---|
| 133 | break;
|
---|
| 134 | }
|
---|
| 135 | --idx;
|
---|
| 136 | }
|
---|
| 137 | return idx;
|
---|
| 138 | }//lastIndexOf
|
---|
| 139 |
|
---|
| 140 |
|
---|
| 141 | #//QhullPointSetIterator
|
---|
| 142 |
|
---|
| 143 | bool QhullPointSetIterator::
|
---|
| 144 | findNext(const QhullPoint &p)
|
---|
| 145 | {
|
---|
| 146 | while(i!=c->constEnd()){
|
---|
| 147 | if(*i++ == p){
|
---|
| 148 | return true;
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 | return false;
|
---|
| 152 | }//findNext
|
---|
| 153 |
|
---|
| 154 | bool QhullPointSetIterator::
|
---|
| 155 | findPrevious(const QhullPoint &p)
|
---|
| 156 | {
|
---|
| 157 | while(i!=c->constBegin()){
|
---|
| 158 | if(*(--i) == p){
|
---|
| 159 | return true;
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 | return false;
|
---|
| 163 | }//findPrevious
|
---|
| 164 |
|
---|
| 165 | }//namespace orgQhull
|
---|
| 166 |
|
---|
| 167 | #//Global functions
|
---|
| 168 |
|
---|
| 169 | using std::endl;
|
---|
| 170 | using std::ostream;
|
---|
| 171 | using orgQhull::QhullPoint;
|
---|
| 172 | using orgQhull::QhullPointSet;
|
---|
| 173 | using orgQhull::UsingLibQhull;
|
---|
| 174 |
|
---|
| 175 | #//operator<<
|
---|
| 176 |
|
---|
| 177 | ostream &
|
---|
| 178 | operator<<(ostream &os, const QhullPointSet &ps)
|
---|
| 179 | {
|
---|
| 180 | os << ps.print(UsingLibQhull::NOqhRunId);
|
---|
| 181 | return os;
|
---|
| 182 | }//<<QhullPointSet
|
---|
| 183 |
|
---|
| 184 | ostream &
|
---|
| 185 | operator<<(ostream &os, const QhullPointSet::PrintIdentifiers &pr)
|
---|
| 186 | {
|
---|
| 187 | const QhullPointSet s= *pr.point_set;
|
---|
| 188 | if (pr.print_message) {
|
---|
| 189 | os << pr.print_message;
|
---|
| 190 | }
|
---|
| 191 | for(QhullPointSet::const_iterator i=s.begin(); i != s.end(); ++i){
|
---|
| 192 | if(i!=s.begin()){
|
---|
| 193 | os << " ";
|
---|
| 194 | }
|
---|
| 195 | const QhullPoint point= *i;
|
---|
| 196 | int id= point.id(pr.run_id);
|
---|
| 197 | os << "p" << id;
|
---|
| 198 | }
|
---|
| 199 | os << endl;
|
---|
| 200 | return os;
|
---|
| 201 | }//PrintIdentifiers
|
---|
| 202 |
|
---|
| 203 | ostream &
|
---|
| 204 | operator<<(ostream &os, const QhullPointSet::PrintPointSet &pr)
|
---|
| 205 | {
|
---|
| 206 | const QhullPointSet s= *pr.point_set;
|
---|
| 207 | if (pr.print_message) {
|
---|
| 208 | os << pr.print_message;
|
---|
| 209 | }
|
---|
| 210 | for(QhullPointSet::const_iterator i=s.begin(); i != s.end(); ++i){
|
---|
| 211 | const QhullPoint point= *i;
|
---|
| 212 | os << point.print(pr.run_id);
|
---|
| 213 | }
|
---|
| 214 | return os;
|
---|
| 215 | }//printPointSet
|
---|
| 216 |
|
---|
| 217 |
|
---|