Debian Codegraph @

src/BoxWithLines2D.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 "BoxWithLines2D.h"
14
15 //-- system includes --
16 #include <cstdlib>
17
18 using std::rand;
19
20 // --- STL includes ---
21 #include <utility>
22 #include <map>
23
24 using std::make_pair;
25 using std::map;
26
27 //--- IO includes ---
28 #include <iostream>
29
30
31 /*!
32 helper function, return random value between min & max
33 */
34 double BoxWithLines2D::m_random(double imin,double imax) const
35 {
36 return imin+((imax-imin)*((double)(rand())/(double)(RAND_MAX)));
37 }
38
39 BoxWithLines2D::BoxWithLines2D()
40 {}
41
42 /*!
43 Construct box
44
45 \param pmin minimum point of bounding box
46 \param pmax maximum point of bounding box
47 */
48 BoxWithLines2D::BoxWithLines2D(const Vector3& pmin,const Vector3& pmax)
49 : m_pmin(pmin),m_pmax(pmax)
50 {}
51
52 /*!
53 add a line
54
55 \param l the line
56 */
57 void BoxWithLines2D::addLine(const Line2D& l)
58 {
59 m_lines.push_back(l);
60 }
61
62 /*!
63 get bounding box
64 */
65 pair<Vector3,Vector3> BoxWithLines2D::getBoundingBox()
66 {
67 return make_pair(m_pmin,m_pmax);
68 }
69
70 /*!
71 get point inside the box. The Argument is ignored
72 */
73 Vector3 BoxWithLines2D::getAPoint(int) const
74 {
75 double px,py;
76
77 px=m_random(m_pmin.x(),m_pmax.x());
78 py=m_random(m_pmin.y(),m_pmax.y());
79
80 return Vector3(px,py,0.0);
81 }
82
83 /*!
84 Return closest plane to a given point. Assumes that the list of Planes/Lines is not empty.
85
86 \param p the point
87 \warning check non-empty list of lines (via hasPlane()) before invoking
88 */
89 Line2D BoxWithLines2D::getClosestPlane(const Vector3& p)
90 {
91 std::cout << "getClosestPlane : " << p << std::endl;
92
93 vector<Line2D>::iterator PL=m_lines.begin();
94 double dist=PL->getDist(p);
95
96 for(vector<Line2D>::iterator iter=m_lines.begin();
97 iter!=m_lines.end();
98 iter++){
99 double ndist=iter->getDist(p);
100 std::cout << "Line: " << *iter << " Distance: " << ndist << std::endl;
101 if(ndist<dist){
102 PL=iter;
103 dist=ndist;
104 }
105 }
106 std::cout << "closest line: " << *PL << " Distance: " << dist << std::endl;
107
108 return (*PL);
109 }
110
111 const map<double, const Line2D*> BoxWithLines2D::getClosestPlanes(const Vector3& p,int nmax) const
112 {
113 map<double,const Line2D*> res;
114
115 for(vector<Line2D>::const_iterator iter=m_lines.begin();
116 iter!=m_lines.end();
117 iter++){
118 double ndist=iter->getDist(p);
119 const Line2D *l=&(*iter);
120 res.insert(make_pair(ndist,l));
121 }
122
123 return res;
124 }
125
126
127 /*!
128 check if point is inside
129
130 \param p the point
131 */
132 bool BoxWithLines2D::isIn(const Vector3& p) const
133 {
134 return ((p.x()>m_pmin.x()) && (p.x()<m_pmax.x()) && (p.y()>m_pmin.y()) && (p.y()<m_pmax.y()));
135 }
136
137 /*!
138 check if a Sphere is inside and doesn't intersect any lines
139
140 \param S the Sphere
141 */
142 bool BoxWithLines2D::isIn(const Sphere& S)
143 {
144 double r=S.Radius();
145 Vector3 p=S.Center();
146
147 // std::cout << "rad: " << r << " pos: " << p << std::endl;
148
149 bool inside=(p.X()>m_pmin.X()+r) && (p.X()<m_pmax.X()-r) && (p.Y()>m_pmin.Y()+r) && (p.Y()<m_pmax.Y()-r);
150
151 vector<Line2D>::iterator iter=m_lines.begin();
152
153 double dist=2*r;
154 while(iter!=m_lines.end() && dist>r){
155 dist=iter->getDist(p);
156 iter++;
157 }
158
159 return inside && (dist>r);
160 }
161
162 ostream& operator<< (ostream& ost, const BoxWithLines2D& L)
163 {
164 ost << L.m_pmin << " to " << L.m_pmax;
165
166 return ost;
167 }
168