| 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 "MeshVolWithJointSet.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 |
Constructor |
| 33 |
*/ |
| 34 |
MeshVolWithJointSet::MeshVolWithJointSet() |
| 35 |
{} |
| 36 |
|
| 37 |
/*! |
| 38 |
Constructor |
| 39 |
|
| 40 |
\param tp the triangle set |
| 41 |
*/ |
| 42 |
MeshVolWithJointSet::MeshVolWithJointSet(const TriPatchSet& tp) |
| 43 |
{ |
| 44 |
m_mesh=tp; |
| 45 |
m_MinPoint=m_mesh.getBoundingBox().first; |
| 46 |
m_MaxPoint=m_mesh.getBoundingBox().second; |
| 47 |
m_DistPoint=m_MinPoint-Vector3(1.0,1.0,1.0); |
| 48 |
} |
| 49 |
|
| 50 |
/*! |
| 51 |
returns a map of the closest mesh facets and their distances to a given point |
| 52 |
|
| 53 |
\param P the point |
| 54 |
\param nmax the maximum number of objects to be returned |
| 55 |
*/ |
| 56 |
const map<double,const AGeometricObject*> MeshVolWithJointSet::getClosestObjects(const Vector3& P,int nmax) const |
| 57 |
{ |
| 58 |
map<double,const AGeometricObject*> res; |
| 59 |
|
| 60 |
for(vector<Triangle3D>::const_iterator iter=m_mesh.triangles_begin(); |
| 61 |
iter!=m_mesh.triangles_end(); |
| 62 |
iter++){ |
| 63 |
double ndist=iter->getDist(P); |
| 64 |
res.insert(make_pair(ndist,&(*iter))); |
| 65 |
} |
| 66 |
|
| 67 |
for(vector<Triangle3D>::const_iterator iter=m_joints.begin(); |
| 68 |
iter!=m_joints.end(); |
| 69 |
iter++){ |
| 70 |
double ndist=iter->getDist(P); |
| 71 |
res.insert(make_pair(ndist,&(*iter))); |
| 72 |
} |
| 73 |
|
| 74 |
return res; |
| 75 |
} |
| 76 |
|
| 77 |
/*! |
| 78 |
Check if a point is inside the volume. Works by checking if a |
| 79 |
line between the point and a point outside crosses |
| 80 |
an odd number of mesh facets. |
| 81 |
|
| 82 |
\param P the point |
| 83 |
*/ |
| 84 |
bool MeshVolWithJointSet::isIn(const Vector3& P) const |
| 85 |
{ |
| 86 |
int cross_count=0; |
| 87 |
for(vector<Triangle3D>::const_iterator iter=m_mesh.triangles_begin(); |
| 88 |
iter!=m_mesh.triangles_end(); |
| 89 |
iter++){ |
| 90 |
if(iter->crosses(P,m_DistPoint)) cross_count++; |
| 91 |
} |
| 92 |
return (cross_count%2)==1; |
| 93 |
} |
| 94 |
|
| 95 |
/*! |
| 96 |
Check if a sphere is fully inside the volume. Works by testing if center of the |
| 97 |
sphere is inside and sphere doesn't intersect a mesh facet. |
| 98 |
|
| 99 |
\param S the sphere |
| 100 |
*/ |
| 101 |
bool MeshVolWithJointSet::isIn(const Sphere& S) |
| 102 |
{ |
| 103 |
double r=S.Radius(); |
| 104 |
Vector3 p=S.Center(); |
| 105 |
|
| 106 |
bool res=isIn(S.Center()); |
| 107 |
vector<Triangle3D>::const_iterator iter=m_mesh.triangles_begin(); |
| 108 |
while((iter!=m_mesh.triangles_end()) && res){ |
| 109 |
res=(iter->getDist(S.Center())>S.Radius()); |
| 110 |
iter++; |
| 111 |
} |
| 112 |
|
| 113 |
if(res){ |
| 114 |
// check intersection with joints |
| 115 |
vector<Triangle3D>::iterator iter=m_joints.begin(); |
| 116 |
double dist=2*r; |
| 117 |
while((iter!=m_joints.end()) && res){ |
| 118 |
dist=iter->getDist(p); |
| 119 |
res=(dist>r); |
| 120 |
iter++; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return res; |
| 125 |
} |
| 126 |
|
| 127 |
void MeshVolWithJointSet::addJoints(const TriPatchSet& t) |
| 128 |
{ |
| 129 |
for(vector<Triangle3D>::const_iterator iter=t.triangles_begin(); |
| 130 |
iter!=t.triangles_end(); |
| 131 |
iter++){ |
| 132 |
m_joints.push_back(*iter); |
| 133 |
} |
| 134 |
} |