1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (c) 2009-2012 C.B. Barber. All rights reserved.
|
---|
4 | ** $Id: //main/2011/qhull/src/libqhullcpp/QhullHyperplane.cpp#6 $$Change: 1464 $
|
---|
5 | ** $DateTime: 2012/01/25 22:58:41 $$Author: bbarber $
|
---|
6 | **
|
---|
7 | ****************************************************************************/
|
---|
8 |
|
---|
9 | #include "QhullHyperplane.h"
|
---|
10 | #include "QhullPoint.h"
|
---|
11 |
|
---|
12 | #include <iostream>
|
---|
13 |
|
---|
14 |
|
---|
15 | #ifdef _MSC_VER // Microsoft Visual C++ -- warning level 4
|
---|
16 | #endif
|
---|
17 |
|
---|
18 | namespace orgQhull {
|
---|
19 |
|
---|
20 | #//Conversion
|
---|
21 |
|
---|
22 | // See qt-qhull.cpp for QList conversions
|
---|
23 |
|
---|
24 | #ifndef QHULL_NO_STL
|
---|
25 | std::vector<coordT> QhullHyperplane::
|
---|
26 | toStdVector() const
|
---|
27 | {
|
---|
28 | QhullHyperplaneIterator i(*this);
|
---|
29 | std::vector<coordT> fs;
|
---|
30 | while(i.hasNext()){
|
---|
31 | fs.push_back(i.next());
|
---|
32 | }
|
---|
33 | fs.push_back(hyperplane_offset);
|
---|
34 | return fs;
|
---|
35 | }//toStdVector
|
---|
36 | #endif //QHULL_NO_STL
|
---|
37 |
|
---|
38 | #//Value
|
---|
39 |
|
---|
40 | //! Return distance from point to hyperplane.
|
---|
41 | //! If greater than zero, the point is above the facet (i.e., outside).
|
---|
42 | // qh_distplane [geom.c], QhullFacet::distance, and QhullHyperplane::distance are copies
|
---|
43 | // Does not support RANDOMdist or logging
|
---|
44 | double QhullHyperplane::
|
---|
45 | distance(const QhullPoint &p) const
|
---|
46 | {
|
---|
47 | const coordT *point= p.coordinates();
|
---|
48 | int dim= p.dimension();
|
---|
49 | QHULL_ASSERT(dim==dimension());
|
---|
50 | const coordT *normal= coordinates();
|
---|
51 | double dist;
|
---|
52 |
|
---|
53 | switch (dim){
|
---|
54 | case 2:
|
---|
55 | dist= offset() + point[0] * normal[0] + point[1] * normal[1];
|
---|
56 | break;
|
---|
57 | case 3:
|
---|
58 | dist= offset() + point[0] * normal[0] + point[1] * normal[1] + point[2] * normal[2];
|
---|
59 | break;
|
---|
60 | case 4:
|
---|
61 | dist= offset()+point[0]*normal[0]+point[1]*normal[1]+point[2]*normal[2]+point[3]*normal[3];
|
---|
62 | break;
|
---|
63 | case 5:
|
---|
64 | dist= offset()+point[0]*normal[0]+point[1]*normal[1]+point[2]*normal[2]+point[3]*normal[3]+point[4]*normal[4];
|
---|
65 | break;
|
---|
66 | case 6:
|
---|
67 | dist= offset()+point[0]*normal[0]+point[1]*normal[1]+point[2]*normal[2]+point[3]*normal[3]+point[4]*normal[4]+point[5]*normal[5];
|
---|
68 | break;
|
---|
69 | case 7:
|
---|
70 | dist= offset()+point[0]*normal[0]+point[1]*normal[1]+point[2]*normal[2]+point[3]*normal[3]+point[4]*normal[4]+point[5]*normal[5]+point[6]*normal[6];
|
---|
71 | break;
|
---|
72 | case 8:
|
---|
73 | dist= offset()+point[0]*normal[0]+point[1]*normal[1]+point[2]*normal[2]+point[3]*normal[3]+point[4]*normal[4]+point[5]*normal[5]+point[6]*normal[6]+point[7]*normal[7];
|
---|
74 | break;
|
---|
75 | default:
|
---|
76 | dist= offset();
|
---|
77 | for (int k=dim; k--; )
|
---|
78 | dist += *point++ * *normal++;
|
---|
79 | break;
|
---|
80 | }
|
---|
81 | return dist;
|
---|
82 | }//distance
|
---|
83 |
|
---|
84 | double QhullHyperplane::
|
---|
85 | norm() const {
|
---|
86 | double d= 0.0;
|
---|
87 | const coordT *c= coordinates();
|
---|
88 | for (int k=dimension(); k--; ){
|
---|
89 | d += *c * *c;
|
---|
90 | ++c;
|
---|
91 | }
|
---|
92 | return sqrt(d);
|
---|
93 | }//norm
|
---|
94 |
|
---|
95 | #//Operator
|
---|
96 |
|
---|
97 | bool QhullHyperplane::
|
---|
98 | operator==(const QhullHyperplane &other) const
|
---|
99 | {
|
---|
100 | if(hyperplane_dimension!=other.hyperplane_dimension){
|
---|
101 | return false;
|
---|
102 | }
|
---|
103 | double d= fabs(hyperplane_offset-other.hyperplane_offset);
|
---|
104 | if(d>UsingLibQhull::globalDistanceEpsilon()){
|
---|
105 | return false;
|
---|
106 | }
|
---|
107 | const coordT *c= hyperplane_coordinates;
|
---|
108 | const coordT *c2= other.hyperplane_coordinates;
|
---|
109 | if(c==c2){
|
---|
110 | return true;
|
---|
111 | }
|
---|
112 | double dist2= 0.0;
|
---|
113 | for(int k= hyperplane_dimension; k--; ){
|
---|
114 | double diff= *c++ - *c2++;
|
---|
115 | dist2 += diff*diff;
|
---|
116 | }
|
---|
117 | if(dist2 > UsingLibQhull::globalAngleEpsilon()){
|
---|
118 | return false;
|
---|
119 | }
|
---|
120 | return true;
|
---|
121 | }//operator==
|
---|
122 |
|
---|
123 | #//GetSet
|
---|
124 |
|
---|
125 | }//namespace orgQhull
|
---|
126 |
|
---|
127 | #//Global functions
|
---|
128 |
|
---|
129 | using std::ostream;
|
---|
130 | using orgQhull::QhullHyperplane;
|
---|
131 | using orgQhull::UsingLibQhull;
|
---|
132 |
|
---|
133 | #//operator<<
|
---|
134 |
|
---|
135 | ostream &
|
---|
136 | operator<<(ostream &os, const QhullHyperplane &p)
|
---|
137 | {
|
---|
138 | os << p.print();
|
---|
139 | return os;
|
---|
140 | }
|
---|
141 |
|
---|
142 | ostream &
|
---|
143 | operator<<(ostream &os, const QhullHyperplane::PrintHyperplane &pr)
|
---|
144 | {
|
---|
145 | QhullHyperplane p= *pr.hyperplane;
|
---|
146 | if(pr.print_message){
|
---|
147 | os << pr.print_message;
|
---|
148 | }
|
---|
149 | const realT *c= p.coordinates();
|
---|
150 | for(int k=p.dimension(); k--; ){
|
---|
151 | realT r= *c++;
|
---|
152 | if(pr.print_message){
|
---|
153 | os << " " << r; // FIXUP QH11010 %8.4g
|
---|
154 | }else{
|
---|
155 | os << " " << r; // FIXUP QH11010 qh_REAL_1
|
---|
156 | }
|
---|
157 | }
|
---|
158 | if(pr.hyperplane_offset_message){
|
---|
159 | os << pr.hyperplane_offset_message << " " << p.offset();
|
---|
160 | }else{
|
---|
161 | os << " " << p.offset();
|
---|
162 | }
|
---|
163 | os << std::endl;
|
---|
164 | return os;
|
---|
165 | }//PrintHyperplane
|
---|
166 |
|
---|