Debian Codegraph @

src/CylinderVol.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 "CylinderVol.h"
14
15 //---
16 #include <cmath>
17 #include <cstdlib>
18
19 using std::cos;
20 using std::sin;
21 using std::fabs;
22
23 // --- STL includes ---
24 #include <utility>
25
26 using std::make_pair;
27
28 CylinderVol::CylinderVol()
29 { }
30
31 CylinderVol::CylinderVol(const Vector3& c,const Vector3& axis,double l,double r): m_cyl(c,axis,r)
32 {
33 m_bottom=Plane(c,axis);
34 m_top=Plane(c+l*axis.unit(),-1.0*axis);
35 m_len=l;
36 // calculate "in plane" vectors
37 m_xd=Vector3(axis.y(),-axis.x(),0);
38 if (m_xd.norm()==0) {
39 m_xd = Vector3(0,axis.z(),-axis.y()) ;
40 }
41 if (m_xd.norm()==0) {
42 m_xd = Vector3(axis.z(),0,-axis.x()) ;
43 }
44 m_xd = m_xd.unit();
45 m_yd = cross(axis,m_xd);
46
47 }
48
49 pair<Vector3,Vector3> CylinderVol::getBoundingBox()
50 {
51 Vector3 center=m_cyl.getBasePoint()+0.5*m_len*m_cyl.getAxis();
52 Vector3 abs_axis=Vector3(fabs(m_cyl.getAxis().X()),fabs(m_cyl.getAxis().Y()),fabs(m_cyl.getAxis().Z()));
53 Vector3 p0=center-0.5*m_len*abs_axis-Vector3(m_cyl.getRadius(),m_cyl.getRadius(),m_cyl.getRadius());
54 Vector3 p1=center+0.5*m_len*abs_axis+Vector3(m_cyl.getRadius(),m_cyl.getRadius(),m_cyl.getRadius());
55
56 return make_pair(p0,p1);
57 }
58
59 Vector3 CylinderVol::getAPoint(int) const
60 {
61 double l=m_len*((double)(rand())/(double)(RAND_MAX));
62 double r=m_cyl.getRadius()*((double)(rand())/(double)(RAND_MAX));
63 double phi=2*M_PI*((double)(rand())/(double)(RAND_MAX));
64
65 Vector3 p0=m_cyl.getBasePoint()+l*m_cyl.getAxis();
66 double x=cos(phi);
67 double y=sin(phi);
68 Vector3 dir=x*m_xd+y*m_yd;
69
70 return p0+r*dir;
71 }
72
73 const map<double,const AGeometricObject*> CylinderVol::getClosestObjects(const Vector3& P,int) const
74 {
75 map<double,const AGeometricObject*> res;
76
77 res.insert(make_pair(m_cyl.getDist(P),&m_cyl));
78 res.insert(make_pair(m_bottom.getDist(P),&m_bottom));
79 res.insert(make_pair(m_top.getDist(P),&m_top));
80
81 return res;
82 }
83
84 bool CylinderVol::isIn(const Vector3& P) const
85 {
86 bool in_cyl=(m_cyl.getDirDist(P)>0);
87 bool above_bottom=(m_bottom.getNormal()*(P-m_bottom.getOrig())>0);
88 bool below_top=(m_top.getNormal()*(P-m_top.getOrig())>0);
89
90 return in_cyl && above_bottom && below_top;
91 }
92
93 bool CylinderVol::isIn(const Sphere& S)
94 {
95 bool in_cyl=(m_cyl.getDirDist(S.Center())>S.Radius());
96 bool above_bottom=(m_bottom.getNormal()*(S.Center()-m_bottom.getOrig())>S.Radius());
97 bool below_top=(m_top.getNormal()*(S.Center()-m_top.getOrig())>S.Radius());
98 bool res=in_cyl && above_bottom && below_top;
99
100 return res;
101 }
102
103 /*
104 check if a Sphere is completely outside the cylinder
105
106 \param S the sphere
107 */
108 bool CylinderVol::isFullyOutside(const Sphere& S)
109 {
110 bool outside_cyl=(m_cyl.getDirDist(S.Center())<(-1.0*S.Radius()));
111 bool below_bottom=(m_bottom.getNormal()*(S.Center()-m_bottom.getOrig())<(-1.0*S.Radius()));
112 bool above_top=(m_top.getNormal()*(S.Center()-m_top.getOrig())<(-1.0*S.Radius()));
113 bool res=outside_cyl || below_bottom || above_top;
114
115 return res;
116
117 }
118
119 ostream& operator << (ostream& ost,const CylinderVol& T)
120 {
121 return ost;
122 }
123