| 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 "TriBox.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 |
TriBox::TriBox() |
| 31 |
{} |
| 32 |
|
| 33 |
double TriBox::m_random(double imin,double imax)const |
| 34 |
{ |
| 35 |
return imin+((imax-imin)*((double)(rand())/(double)(RAND_MAX))); |
| 36 |
} |
| 37 |
|
| 38 |
TriBox::TriBox(const Vector3& pmin,const Vector3& pmax,bool inverted):m_pmin(pmin),m_pmax(pmax) |
| 39 |
{ |
| 40 |
m_inverted=inverted; |
| 41 |
} |
| 42 |
|
| 43 |
/*! |
| 44 |
add a plane to the box |
| 45 |
|
| 46 |
\param P the plane |
| 47 |
*/ |
| 48 |
void TriBox::addPlane(const Plane& P) |
| 49 |
{ |
| 50 |
m_planes.push_back(P); |
| 51 |
} |
| 52 |
|
| 53 |
pair<Vector3,Vector3> TriBox::getBoundingBox() |
| 54 |
{ |
| 55 |
return make_pair(m_pmin,m_pmax); |
| 56 |
} |
| 57 |
|
| 58 |
Vector3 TriBox::getAPoint(int)const |
| 59 |
{ |
| 60 |
double px,py,pz; |
| 61 |
|
| 62 |
pz=m_random(m_pmin.z(),m_pmax.z()); |
| 63 |
py=m_random(m_pmin.y(),m_pmax.y()); |
| 64 |
double dy=m_pmax.y()-m_pmin.y(); |
| 65 |
double y=py-m_pmin.y(); |
| 66 |
double ry=m_inverted ? y/dy : 1.0-(y/dy); |
| 67 |
double dx=(m_pmax.x()-m_pmin.x())/2.0; |
| 68 |
double xmin=(m_pmin.x()+m_pmax.x())/2.0-dx*ry; |
| 69 |
double xmax=(m_pmin.x()+m_pmax.x())/2.0+dx*ry; |
| 70 |
px=m_random(xmin,xmax); |
| 71 |
|
| 72 |
return Vector3(px,py,pz); |
| 73 |
} |
| 74 |
|
| 75 |
Plane TriBox::getClosestPlane(const Vector3& p) |
| 76 |
{ |
| 77 |
vector<Plane>::iterator PL=m_planes.begin(); |
| 78 |
double dist=PL->getDist(p); |
| 79 |
|
| 80 |
for(vector<Plane>::iterator iter=m_planes.begin(); |
| 81 |
iter!=m_planes.end(); |
| 82 |
iter++){ |
| 83 |
double ndist=iter->getDist(p); |
| 84 |
if(ndist<dist){ |
| 85 |
PL=iter; |
| 86 |
dist=ndist; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return (*PL); |
| 91 |
} |
| 92 |
|
| 93 |
const map<double,const AGeometricObject*> TriBox::getClosestObjects(const Vector3& p,int nmax) const |
| 94 |
{ |
| 95 |
map<double,const AGeometricObject*> res; |
| 96 |
|
| 97 |
for(vector<Plane>::const_iterator iter=m_planes.begin(); |
| 98 |
iter!=m_planes.end(); |
| 99 |
iter++){ |
| 100 |
double ndist=iter->getDist(p); |
| 101 |
res.insert(make_pair(ndist,&(*iter))); |
| 102 |
} |
| 103 |
|
| 104 |
return res; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
bool TriBox::isIn(const Vector3& V) const |
| 109 |
{ |
| 110 |
bool res; |
| 111 |
|
| 112 |
res=(V.z()<m_pmax.z()) && (V.z()>m_pmin.z()); |
| 113 |
if((V.y()<m_pmax.y()) && (V.y()>m_pmin.y())){ |
| 114 |
double dy=m_pmax.y()-m_pmin.y(); |
| 115 |
double y=V.y()-m_pmin.y(); |
| 116 |
double ry=m_inverted ? y/dy : 1.0-(y/dy); |
| 117 |
double dx=(m_pmax.x()-m_pmin.x())/2.0; |
| 118 |
double xmin=(m_pmin.x()+m_pmax.x())/2.0-dx*ry; |
| 119 |
double xmax=(m_pmin.x()+m_pmax.x())/2.0+dx*ry; |
| 120 |
// std::cout << "y: " << V.Y() << " dy: " << dy << " ry: " << ry << " dx: " << dx << " xmin: " << xmin << "xmax: " << xmax << std::endl; |
| 121 |
res=res && (V.x()<xmax) && (V.x()>xmin); |
| 122 |
} else { |
| 123 |
res=false; |
| 124 |
} |
| 125 |
|
| 126 |
return res; |
| 127 |
} |
| 128 |
|
| 129 |
bool TriBox::isIn(const Sphere& S) |
| 130 |
{ |
| 131 |
double r=S.Radius(); |
| 132 |
Vector3 p=S.Center(); |
| 133 |
bool res=isIn(p); |
| 134 |
|
| 135 |
vector<Plane>::iterator iter=m_planes.begin(); |
| 136 |
|
| 137 |
// std::cout << "p: " << p<< " r: " << r; |
| 138 |
double dist=2*r; |
| 139 |
while((iter!=m_planes.end()) && (dist>r)){ |
| 140 |
dist=iter->getDist(p); |
| 141 |
// std::cout << " dist: " << dist ; |
| 142 |
iter++; |
| 143 |
} |
| 144 |
// std::cout << std::endl; |
| 145 |
|
| 146 |
return res && (dist>r); |
| 147 |
} |
| 148 |
|
| 149 |
/*! |
| 150 |
Check if sphere is fully outside volume |
| 151 |
|
| 152 |
\param S the sphere |
| 153 |
\warning DUMMY IMPLEMENTATION |
| 154 |
*/ |
| 155 |
bool TriBox::isFullyOutside(const Sphere& S) |
| 156 |
{ |
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
ostream& operator<< (ostream& ost, const TriBox& L) |
| 161 |
{ |
| 162 |
ost << L.m_pmin << " to " << L.m_pmax; |
| 163 |
|
| 164 |
return ost; |
| 165 |
} |