| 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 "MeshVolume.h" |
| 14 |
|
| 15 |
// --- Project includes --- |
| 16 |
#include "geometry/Triangle3D.h" |
| 17 |
|
| 18 |
//-- system includes -- |
| 19 |
#include <cstdlib> |
| 20 |
|
| 21 |
using std::rand; |
| 22 |
|
| 23 |
// --- STL includes --- |
| 24 |
#include <utility> |
| 25 |
|
| 26 |
// --- I/O includes |
| 27 |
#include <iostream> |
| 28 |
|
| 29 |
using std::make_pair; |
| 30 |
|
| 31 |
/*! |
| 32 |
helper function, return random value between min & max |
| 33 |
*/ |
| 34 |
double MeshVolume::m_random(double imin,double imax) const |
| 35 |
{ |
| 36 |
return imin+((imax-imin)*((double)(rand())/(double)(RAND_MAX))); |
| 37 |
} |
| 38 |
|
| 39 |
/*! |
| 40 |
Constructor |
| 41 |
*/ |
| 42 |
MeshVolume::MeshVolume() |
| 43 |
{} |
| 44 |
|
| 45 |
/*! |
| 46 |
Constructor |
| 47 |
|
| 48 |
\param tp the triangle set |
| 49 |
*/ |
| 50 |
MeshVolume::MeshVolume(const TriPatchSet& tp) |
| 51 |
{ |
| 52 |
m_mesh=tp; |
| 53 |
m_MinPoint=m_mesh.getBoundingBox().first; |
| 54 |
m_MaxPoint=m_mesh.getBoundingBox().second; |
| 55 |
m_DistPoint=m_MinPoint-Vector3(1.0,1.0,1.0); |
| 56 |
} |
| 57 |
|
| 58 |
/*! |
| 59 |
get bounding box |
| 60 |
*/ |
| 61 |
pair<Vector3,Vector3> MeshVolume::getBoundingBox() |
| 62 |
{ |
| 63 |
return m_mesh.getBoundingBox(); |
| 64 |
} |
| 65 |
|
| 66 |
/*! |
| 67 |
Get a point inside the volume. The argument is ignored. |
| 68 |
|
| 69 |
\warning if the mesh is incorrect the function might loop forever |
| 70 |
*/ |
| 71 |
Vector3 MeshVolume::getAPoint(int) const |
| 72 |
{ |
| 73 |
Vector3 res; |
| 74 |
double px,py,pz; |
| 75 |
|
| 76 |
do{ |
| 77 |
px=m_random(m_MinPoint.x(),m_MaxPoint.x()); |
| 78 |
py=m_random(m_MinPoint.y(),m_MaxPoint.y()); |
| 79 |
pz=m_random(m_MinPoint.z(),m_MaxPoint.z()); |
| 80 |
res=Vector3(px,py,pz); |
| 81 |
} while (!isIn(res)); |
| 82 |
|
| 83 |
return res; |
| 84 |
} |
| 85 |
|
| 86 |
/*! |
| 87 |
returns a map of the closest mesh facets and their distances to a given point |
| 88 |
|
| 89 |
\param P the point |
| 90 |
\param nmax the maximum number of objects to be returned |
| 91 |
*/ |
| 92 |
const map<double,const AGeometricObject*> MeshVolume::getClosestObjects(const Vector3& P,int nmax) const |
| 93 |
{ |
| 94 |
map<double,const AGeometricObject*> res; |
| 95 |
|
| 96 |
for(vector<Triangle3D>::const_iterator iter=m_mesh.triangles_begin(); |
| 97 |
iter!=m_mesh.triangles_end(); |
| 98 |
iter++){ |
| 99 |
double ndist=iter->getDist(P); |
| 100 |
res.insert(make_pair(ndist,&(*iter))); |
| 101 |
} |
| 102 |
|
| 103 |
return res; |
| 104 |
} |
| 105 |
|
| 106 |
/*! |
| 107 |
Check if a point is inside the volume. Works by checking if a |
| 108 |
line between the point and a point outside crosses |
| 109 |
an odd number of mesh facets. |
| 110 |
|
| 111 |
\param P the point |
| 112 |
*/ |
| 113 |
bool MeshVolume::isIn(const Vector3& P) const |
| 114 |
{ |
| 115 |
int cross_count=0; |
| 116 |
for(vector<Triangle3D>::const_iterator iter=m_mesh.triangles_begin(); |
| 117 |
iter!=m_mesh.triangles_end(); |
| 118 |
iter++){ |
| 119 |
if(iter->crosses(P,m_DistPoint)) cross_count++; |
| 120 |
} |
| 121 |
return (cross_count%2)==1; |
| 122 |
} |
| 123 |
|
| 124 |
/*! |
| 125 |
Check if a sphere is fully inside the volume. Works by testing if center of the |
| 126 |
sphere is inside and sphere doesn't intersect a mesh facet. |
| 127 |
|
| 128 |
\param S the sphere |
| 129 |
*/ |
| 130 |
bool MeshVolume::isIn(const Sphere& S) |
| 131 |
{ |
| 132 |
bool res=isIn(S.Center()); |
| 133 |
vector<Triangle3D>::const_iterator iter=m_mesh.triangles_begin(); |
| 134 |
while((iter!=m_mesh.triangles_end()) && res){ |
| 135 |
res=(iter->getDist(S.Center())>S.Radius()); |
| 136 |
iter++; |
| 137 |
} |
| 138 |
|
| 139 |
return res; |
| 140 |
} |
| 141 |
|
| 142 |
/*! |
| 143 |
Check if a sphere is fully outside the volume. Works by testing if center of the |
| 144 |
sphere is not inside and sphere doesn't intersect a mesh facet. |
| 145 |
|
| 146 |
\param S the sphere |
| 147 |
*/ |
| 148 |
bool MeshVolume::isFullyOutside(const Sphere& S) |
| 149 |
{ |
| 150 |
bool res=!isIn(S.Center()); |
| 151 |
|
| 152 |
vector<Triangle3D>::const_iterator iter=m_mesh.triangles_begin(); |
| 153 |
while((iter!=m_mesh.triangles_end()) && res){ |
| 154 |
res=(iter->getDist(S.Center())>S.Radius()); |
| 155 |
iter++; |
| 156 |
} |
| 157 |
|
| 158 |
return res; |
| 159 |
} |