~ubuntu-branches/ubuntu/trusty/qhull/trusty-proposed

« back to all changes in this revision

Viewing changes to src/libqhullcpp/QhullPoint.cpp

  • Committer: Package Import Robot
  • Author(s): Barak A. Pearlmutter
  • Date: 2014-02-13 11:09:12 UTC
  • mfrom: (8.1.4 sid)
  • Revision ID: package-import@ubuntu.com-20140213110912-ifwyxorlsnnl1ebh
Tags: 2012.1-4
Add convenience link to #include <qhull/qhull.h> to simplify transition.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (c) 2009-2012 C.B. Barber. All rights reserved.
 
4
** $Id: //main/2011/qhull/src/libqhullcpp/QhullPoint.cpp#4 $$Change: 1464 $
 
5
** $DateTime: 2012/01/25 22:58:41 $$Author: bbarber $
 
6
**
 
7
****************************************************************************/
 
8
 
 
9
#include "UsingLibQhull.h"
 
10
#include "QhullPoint.h"
 
11
 
 
12
#include <iostream>
 
13
#include <algorithm>
 
14
 
 
15
#ifdef _MSC_VER  // Microsoft Visual C++ -- warning level 4
 
16
#endif
 
17
 
 
18
namespace orgQhull {
 
19
 
 
20
#//Class public variables and methods
 
21
 
 
22
//! If qhRundID undefined uses QhullPoint::s_points_begin and dimension
 
23
int QhullPoint::
 
24
id(int qhRunId, int dimension, const coordT *c)
 
25
{
 
26
    QHULL_UNUSED(dimension);
 
27
 
 
28
    if(UsingLibQhull::hasPoints()){
 
29
        if(qhRunId==UsingLibQhull::NOqhRunId){
 
30
            const coordT *pointsEnd;
 
31
            int dim;
 
32
            const coordT *points= UsingLibQhull::globalPoints(&dim, &pointsEnd);
 
33
            if(c>=points && c<pointsEnd){
 
34
                int offset= (int)(c-points); // WARN64
 
35
                return offset/dim;
 
36
            }
 
37
        }else{
 
38
            UsingLibQhull q(qhRunId);
 
39
            // NOerrors from qh_pointid or qh_setindex
 
40
            return qh_pointid(const_cast<coordT *>(c));
 
41
        }
 
42
    }
 
43
    long long i=(long long)c;
 
44
    return (int)i; // WARN64
 
45
}//id
 
46
 
 
47
#//Conversion
 
48
 
 
49
// See qt-qhull.cpp for QList conversion
 
50
 
 
51
#ifndef QHULL_NO_STL
 
52
std::vector<coordT> QhullPoint::
 
53
toStdVector() const
 
54
{
 
55
    QhullPointIterator i(*this);
 
56
    std::vector<coordT> vs;
 
57
    while(i.hasNext()){
 
58
        vs.push_back(i.next());
 
59
    }
 
60
    return vs;
 
61
}//toStdVector
 
62
#endif //QHULL_NO_STL
 
63
 
 
64
#//Operator
 
65
 
 
66
bool QhullPoint::
 
67
operator==(const QhullPoint &other) const
 
68
{
 
69
    if(point_dimension!=other.point_dimension){
 
70
        return false;
 
71
    }
 
72
    const coordT *c= point_coordinates;
 
73
    const coordT *c2= other.point_coordinates;
 
74
    if(c==c2){
 
75
        return true;
 
76
    }
 
77
    double dist2= 0.0;
 
78
    for(int k= point_dimension; k--; ){
 
79
        double diff= *c++ - *c2++;
 
80
        dist2 += diff*diff;
 
81
    }
 
82
    double epsilon= UsingLibQhull::globalDistanceEpsilon();
 
83
    // std::cout << "DEBUG dist2 " << dist2 << " epsilon^2 " << epsilon*epsilon << std::endl;
 
84
    return (dist2<=(epsilon*epsilon));
 
85
}//operator==
 
86
 
 
87
 
 
88
#//Value
 
89
 
 
90
//! Return distance betweeen two points.
 
91
double QhullPoint::
 
92
distance(const QhullPoint &p) const
 
93
{
 
94
    const coordT *c= coordinates();
 
95
    const coordT *c2= p.coordinates();
 
96
    int dim= dimension();
 
97
    QHULL_ASSERT(dim==p.dimension());
 
98
    double dist;
 
99
 
 
100
    switch(dim){
 
101
  case 2:
 
102
      dist= (c[0]-c2[0])*(c[0]-c2[0]) + (c[1]-c2[1])*(c[1]-c2[1]);
 
103
      break;
 
104
  case 3:
 
105
      dist= (c[0]-c2[0])*(c[0]-c2[0]) + (c[1]-c2[1])*(c[1]-c2[1]) + (c[2]-c2[2])*(c[2]-c2[2]);
 
106
      break;
 
107
  case 4:
 
108
      dist= (c[0]-c2[0])*(c[0]-c2[0]) + (c[1]-c2[1])*(c[1]-c2[1]) + (c[2]-c2[2])*(c[2]-c2[2]) + (c[3]-c2[3])*(c[3]-c2[3]);
 
109
      break;
 
110
  case 5:
 
111
      dist= (c[0]-c2[0])*(c[0]-c2[0]) + (c[1]-c2[1])*(c[1]-c2[1]) + (c[2]-c2[2])*(c[2]-c2[2]) + (c[3]-c2[3])*(c[3]-c2[3]) + (c[4]-c2[4])*(c[4]-c2[4]);
 
112
      break;
 
113
  case 6:
 
114
      dist= (c[0]-c2[0])*(c[0]-c2[0]) + (c[1]-c2[1])*(c[1]-c2[1]) + (c[2]-c2[2])*(c[2]-c2[2]) + (c[3]-c2[3])*(c[3]-c2[3]) + (c[4]-c2[4])*(c[4]-c2[4]) + (c[5]-c2[5])*(c[5]-c2[5]);
 
115
      break;
 
116
  case 7:
 
117
      dist= (c[0]-c2[0])*(c[0]-c2[0]) + (c[1]-c2[1])*(c[1]-c2[1]) + (c[2]-c2[2])*(c[2]-c2[2]) + (c[3]-c2[3])*(c[3]-c2[3]) + (c[4]-c2[4])*(c[4]-c2[4]) + (c[5]-c2[5])*(c[5]-c2[5]) + (c[6]-c2[6])*(c[6]-c2[6]);
 
118
      break;
 
119
  case 8:
 
120
      dist= (c[0]-c2[0])*(c[0]-c2[0]) + (c[1]-c2[1])*(c[1]-c2[1]) + (c[2]-c2[2])*(c[2]-c2[2]) + (c[3]-c2[3])*(c[3]-c2[3]) + (c[4]-c2[4])*(c[4]-c2[4]) + (c[5]-c2[5])*(c[5]-c2[5]) + (c[6]-c2[6])*(c[6]-c2[6]) + (c[7]-c2[7])*(c[7]-c2[7]);
 
121
      break;
 
122
  default:
 
123
      dist= 0.0;
 
124
      for(int k=dim; k--; ){
 
125
          dist += (*c - *c2) * (*c - *c2);
 
126
          ++c;
 
127
          ++c2;
 
128
      }
 
129
      break;
 
130
    }
 
131
    return sqrt(dist);
 
132
}//distance
 
133
 
 
134
}//namespace orgQhull
 
135
 
 
136
#//Global functions
 
137
 
 
138
using std::ostream;
 
139
using orgQhull::QhullPoint;
 
140
using orgQhull::UsingLibQhull;
 
141
 
 
142
#//operator<<
 
143
 
 
144
ostream &
 
145
operator<<(ostream &os, const QhullPoint &p)
 
146
{
 
147
    os << p.printWithIdentifier(UsingLibQhull::NOqhRunId, "");
 
148
    return os;
 
149
}
 
150
 
 
151
//! Same as qh_printpointid [io.c]
 
152
ostream &
 
153
operator<<(ostream &os, const QhullPoint::PrintPoint &pr)
 
154
{
 
155
    QhullPoint p= *pr.point; 
 
156
    int i= p.id(pr.run_id);
 
157
    if(pr.point_message){
 
158
        if(*pr.point_message){
 
159
            os << pr.point_message << " ";
 
160
        }
 
161
        if(pr.with_identifier && (i!=-1)){
 
162
            os << "p" << i << ": ";
 
163
        }
 
164
    }
 
165
    const realT *c= p.coordinates();
 
166
    for(int k=p.dimension(); k--; ){
 
167
        realT r= *c++;
 
168
        if(pr.point_message){
 
169
            os << " " << r; // FIXUP QH11010 %8.4g
 
170
        }else{
 
171
            os << " " << r; // FIXUP QH11010 qh_REAL_1
 
172
        }
 
173
    }
 
174
    os << std::endl;
 
175
    return os;
 
176
}//printPoint
 
177