Skip to content
Snippets Groups Projects
Commit 6b97a0bf authored by Jan Möbius's avatar Jan Möbius
Browse files

Marlin:

Get all faces hit by ray

Improved splitting of the cells to improve ray casting.

Generate polymesh for visualizing Bounding boxes in the tree





git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@9759 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 7f9f3bc8
No related branches found
No related tags found
No related merge requests found
......@@ -59,6 +59,7 @@
//== CLASS DEFINITION =========================================================
#include <vector>
template <class BSPCore>
......@@ -121,7 +122,7 @@ _nearest(Node* _node, NearestNeighborData& _data) const
//-----------------------------------------------------------------------------
template <class BSPCore>
typename BSPImplT<BSPCore>::NearestNeighbor
typename BSPImplT<BSPCore>::RayCollision
BSPImplT<BSPCore>::
raycollision(const Point& _p, const Point& _r) const
{
......@@ -129,9 +130,10 @@ raycollision(const Point& _p, const Point& _r) const
data.ref = _p;
data.dist = FLT_MAX;
data.ray = _r;
data.hit_vertices.clear();
_raycollision(this->root_, data);
return NearestNeighbor(data.nearest, data.dist);
return RayCollision(data.nearest, data.dist, data.hit_vertices);
}
......@@ -155,6 +157,8 @@ _raycollision(Node* _node, RayCollisionData& _data) const
this->traits_.points(*it, v0, v1, v2);
if (ACG::Geometry::triangleIntersection(_data.ref, _data.ray, v0, v1, v2, dist, u, v)) {
_data.hit_vertices.push_back(*it);
//face intersects with ray. But is it closer than any that we have found so far?
if (dist < _data.dist)
{
......
......@@ -60,6 +60,7 @@
//== CLASS DEFINITION =========================================================
#include <vector>
template <class BSPCore>
......@@ -91,11 +92,21 @@ public: //---------------------------------------------------------------------
Scalar dist;
};
/// Store nearest neighbor information
struct RayCollision
{
RayCollision() {}
RayCollision(Handle _h, Scalar _d, std::vector<Handle> _v) : handle(_h), dist(_d), hit_vertices(_v) {}
Handle handle;
Scalar dist;
std::vector<Handle> hit_vertices;
};
/// Return handle of the nearest neighbor face
NearestNeighbor nearest(const Point& _p) const;
/// Return handle of the nearest collided face
NearestNeighbor raycollision (const Point& _p, const Point& _r) const;
/// Return handles of all hit collided faces, and the handle of the nearest collided face
RayCollision raycollision (const Point& _p, const Point& _r) const;
private: //---------------------------------------------------------------------
......@@ -116,6 +127,7 @@ private: //---------------------------------------------------------------------
Point ray;
Scalar dist;
Handle nearest;
std::vector<Handle> hit_vertices;
};
......
......@@ -56,6 +56,7 @@
#include <ACG/Geometry/Types/PlaneT.hh>
#include <ACG/Geometry/Algorithms.hh>
#include <ObjectTypes/PolyMesh/PolyMeshTypes.hh>
//== CLASS DEFINITION =========================================================
......@@ -65,6 +66,7 @@ struct TreeNode
{
typedef typename Mesh::FaceHandle Handle;
typedef typename Mesh::Point Point;
typedef typename Mesh::VertexHandle VertexHandle;
typedef std::vector<Handle> Handles;
typedef typename Handles::iterator HandleIter;
typedef typename Point::value_type Scalar;
......@@ -98,6 +100,78 @@ struct TreeNode
TreeNode *parent_, *left_child_, *right_child_;
Plane plane_;
Point bb_min, bb_max;
/// This visualizes the bounding boxes
void visualizeTree(PolyMesh *_object, int _max_depth)
{
if (_max_depth > 0 && (left_child_ || right_child_) )
{
if (left_child_)
left_child_->visualizeTree(_object, _max_depth-1);
if (right_child_)
right_child_->visualizeTree(_object, _max_depth-1);
}
else
{
Point size_ = bb_max - bb_min;
std::vector<VertexHandle> vhandle(8);
vhandle[0] = _object->add_vertex(bb_min+Point(0.0,0.0,size_[2]));
vhandle[1] = _object->add_vertex(bb_min+Point(size_[0],0.0,size_[2]));
vhandle[2] = _object->add_vertex(bb_min+Point(size_[0],size_[1],size_[2]));
vhandle[3] = _object->add_vertex(bb_min+Point(0.0,size_[1],size_[2]));
vhandle[4] = _object->add_vertex(bb_min+Point(0.0,0.0,0.0));
vhandle[5] = _object->add_vertex(bb_min+Point(size_[0],0.0,0.0));
vhandle[6] = _object->add_vertex(bb_min+Point(size_[0],size_[1],0.0));
vhandle[7] = _object->add_vertex(bb_min+Point(0.0,size_[1],0.0));
// generate (quadrilateral) faces
std::vector<VertexHandle> face_vhandles;
face_vhandles.clear();
face_vhandles.push_back(vhandle[0]);
face_vhandles.push_back(vhandle[1]);
face_vhandles.push_back(vhandle[2]);
face_vhandles.push_back(vhandle[3]);
_object->add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[7]);
face_vhandles.push_back(vhandle[6]);
face_vhandles.push_back(vhandle[5]);
face_vhandles.push_back(vhandle[4]);
_object->add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[1]);
face_vhandles.push_back(vhandle[0]);
face_vhandles.push_back(vhandle[4]);
face_vhandles.push_back(vhandle[5]);
_object->add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[2]);
face_vhandles.push_back(vhandle[1]);
face_vhandles.push_back(vhandle[5]);
face_vhandles.push_back(vhandle[6]);
_object->add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[3]);
face_vhandles.push_back(vhandle[2]);
face_vhandles.push_back(vhandle[6]);
face_vhandles.push_back(vhandle[7]);
_object->add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[0]);
face_vhandles.push_back(vhandle[3]);
face_vhandles.push_back(vhandle[7]);
face_vhandles.push_back(vhandle[4]);
_object->add_face(face_vhandles);
}
}
};
//=============================================================================
......
......@@ -165,3 +165,16 @@ _build(Node* _node,
//=============================================================================
template <class BSPTraits>
void
TriangleBSPCoreT<BSPTraits>::
visualizeTree(PolyMesh *_object, int _max_depth)
{
root_->visualizeTree(_object, _max_depth-1);
_object->update_normals();
}
//=============================================================================
\ No newline at end of file
......@@ -56,9 +56,10 @@
//== INCLUDES =================================================================
#include <vector>
#include <ACG/Geometry/Types/PlaneT.hh>
#include <OpenMesh/Core/Geometry/VectorT.hh>
#include <vector>
#include <ObjectTypes/PolyMesh/PolyMeshTypes.hh>
#include "TriangleBSPT.hh"
......@@ -98,6 +99,8 @@ public: //---------------------------------------------------------------------
void push_back(Handle _h) { handles_.push_back(_h); }
/// Finally build the tree
void build(unsigned int _max_handles, unsigned int _max_depth);
/// Create a PolyMesh object that visualizes the bounding boxes of the BSP tree
void visualizeTree(PolyMesh *_object, int _max_depth);
private: //---------------------------------------------------------------------
......
......@@ -119,19 +119,24 @@ public:
bb_max.vectorize(-FLT_MAX);
std::list<Point>* vertices = new std::list<Point>;
for (it_h=_node->begin(); it_h!=_node->end(); ++it_h)
{
points(*it_h, p0, p1, p2);
/*
bb_min.minimize(p0);
bb_min.minimize(p1);
bb_min.minimize(p2);
bb_max.maximize(p0);
bb_max.maximize(p1);
bb_max.maximize(p2);
bb_max.maximize(p2);*/
vertices->push_back (p0);
vertices->push_back (p1);
vertices->push_back (p2);
}
bb_min = _node->bb_min;
bb_max = _node->bb_max;
// split longest side of bounding box
Point bb = bb_max - bb_min;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment