Debian Codegraph @

src/BoxWithPlanes3D.cc

raw
1 /////////////////////////////////////////////////////////////
2 // //
3 // Copyright (c) 2007-2017 by The University of Queensland //
4 // Centre for Geoscience Computing //
5 // http://earth.uq.edu.au/centre-geoscience-computing //
6 // //
7 // Primary Business: Brisbane, Queensland, Australia //
8 // Licensed under the Open Software License version 3.0 //
9 // http://www.apache.org/licenses/LICENSE-2.0 //
10 // //
11 /////////////////////////////////////////////////////////////
12
13 #include "BoxWithPlanes3D.h"
14
15
16 //-- system includes --
17 #include <cstdlib>
18
19 using std::rand;
20
21 // --- STL includes ---
22 #include <utility>
23 #include <map>
24
25 using std::make_pair;
26 using std::map;
27
28 //--- IO includes ---
29 #include <iostream>
30
31
32 BoxWithPlanes3D::BoxWithPlanes3D()
33 {
34 }
35
36 /*!
37 helper function, return random value between min & max
38 */
39 double BoxWithPlanes3D::m_random(double imin,double imax) const
40 {
41 return imin+((imax-imin)*((double)(rand())/(double)(RAND_MAX)));
42 }
43
44
45 /*!
46 Construct box
47
48 \param pmin minimum point of bounding box
49 \param pmax maximum point of bounding box
50 */
51 BoxWithPlanes3D::BoxWithPlanes3D(const Vector3& pmin,const Vector3& pmax): m_pmin(pmin),m_pmax(pmax)
52 {}
53
54 /*!
55 add a plane to the box
56
57 \param P the plane
58 */
59 void BoxWithPlanes3D::addPlane(const Plane& P)
60 {
61 m_planes.push_back(P);
62 }
63
64 /*!
65 get bounding box
66 */
67 pair<Vector3,Vector3> BoxWithPlanes3D::getBoundingBox()
68 {
69 return make_pair(m_pmin,m_pmax);
70 }
71
72
73 /*!
74 get point inside the box. The Argument is ignored
75 */
76 Vector3 BoxWithPlanes3D::getAPoint(int) const
77 {
78 double px,py,pz;
79
80 px=m_random(m_pmin.x(),m_pmax.x());
81 py=m_random(m_pmin.y(),m_pmax.y());
82 pz=m_random(m_pmin.z(),m_pmax.z());
83
84 return Vector3(px,py,pz);
85 }
86
87 /*!
88 Return closest plane to a given point. Assumes that the list of Planes is not empty.
89
90 \param p the point
91 \warning check non-empty list of lines (via hasPlane()) before invoking
92 */
93 Plane BoxWithPlanes3D::getClosestPlane(const Vector3& p)
94 {
95 vector<Plane>::iterator PL=m_planes.begin();
96 double dist=PL->getDist(p);
97
98 for(vector<Plane>::iterator iter=m_planes.begin();
99 iter!=m_planes.end();
100 iter++){
101 double ndist=iter->getDist(p);
102 if(ndist<dist){
103 PL=iter;
104 dist=ndist;
105 }
106 }
107
108 return (*PL);
109 }
110
111 /*!
112 get the n closes planes to a given point
113
114 \param p the point
115 \param nmax the max. nr. of planes
116 */
117 const map<double,const AGeometricObject*> BoxWithPlanes3D::getClosestObjects(const Vector3& p,int nmax) const
118 {
119 map<double,const AGeometricObject*> res;
120
121 for(vector<Plane>::const_iterator iter=m_planes.begin();
122 iter!=m_planes.end();
123 iter++){
124 double ndist=iter->getDist(p);
125 res.insert(make_pair(ndist,&(*iter)));
126 }
127
128 return res;
129 }
130
131 /*!
132 check if point is inside
133
134 \param p the point
135 */
136 bool BoxWithPlanes3D::isIn(const Vector3& p) const
137 {
138 return ((p.x()>m_pmin.x()) && (p.x()<m_pmax.x()) &&
139 (p.y()>m_pmin.y()) && (p.y()<m_pmax.y()) &&
140 (p.z()>m_pmin.z()) && (p.z()<m_pmax.z()));
141 }
142
143 /*!
144 check if a Sphere is inside and doesn't intersect any planes
145
146 \param S the Sphere
147 */
148 bool BoxWithPlanes3D::isIn(const Sphere& S)
149 {
150 double r=S.Radius();
151 Vector3 p=S.Center();
152
153 // std::cout << "rad: " << r << " pos: " << p << std::endl;
154
155 bool inside=(p.X()>m_pmin.X()+r) && (p.X()<m_pmax.X()-r) &&
156 (p.Y()>m_pmin.Y()+r) && (p.Y()<m_pmax.Y()-r) &&
157 (p.Z()>m_pmin.Z()+r) && (p.Z()<m_pmax.Z()-r);
158
159 vector<Plane>::iterator iter=m_planes.begin();
160
161 double dist=2*r;
162 while((iter!=m_planes.end()) && (dist>r)){
163 dist=iter->getDist(p);
164 iter++;
165 }
166
167 return inside && (dist>r);
168 }
169
170 /*
171 check if a Sphere is fully outside the box
172 \param S the Sphere
173 */
174 bool BoxWithPlanes3D::isFullyOutside(const Sphere& S)
175 {
176 double r=S.Radius();
177 Vector3 p=S.Center();
178
179 // std::cout << "rad: " << r << " pos: " << p << std::endl;
180
181 bool outside=
182 (p.X()<m_pmin.X()-r) || (p.X()>m_pmax.X()+r) ||
183 (p.Y()<m_pmin.Y()-r) || (p.Y()>m_pmax.Y()+r) ||
184 (p.Z()<m_pmin.Z()-r) || (p.Z()>m_pmax.Z()+r);
185
186 return outside;
187
188 }
189
190 ostream& operator<< (ostream& ost, const BoxWithPlanes3D& L)
191 {
192 ost << L.m_pmin << " to " << L.m_pmax;
193
194 return ost;
195 }
196