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