Commit 19ef73b5 authored by Hans-Christian Ebke's avatar Hans-Christian Ebke
Browse files

Fixed bug in DrawMesh where picking of a mesh without precomputed normals results in a crash.


git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@17266 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 12e4d8e8
......@@ -1463,6 +1463,7 @@ void DrawMeshT<Mesh>::updatePerEdgeBuffers()
}
template <class Mesh>
template<typename Mesh::Normal (DrawMeshT<Mesh>::*NormalLookup)(typename Mesh::FaceHandle)>
void DrawMeshT<Mesh>::updatePerHalfedgeBuffers()
{
// Only update buffers if they are invalid
......@@ -1478,11 +1479,11 @@ void DrawMeshT<Mesh>::updatePerHalfedgeBuffers()
unsigned int idx = 0;
typename Mesh::ConstHalfedgeIter he_it(mesh_.halfedges_sbegin()), he_end(mesh_.halfedges_end());
for (; he_it!=he_end; ++he_it) {
for (typename Mesh::ConstHalfedgeIter he_it(mesh_.halfedges_sbegin()), he_end(mesh_.halfedges_end());
he_it != he_end; ++he_it) {
perHalfedgeVertexBuf_[idx] = halfedge_point(*he_it);
perHalfedgeVertexBuf_[idx+1] = halfedge_point(mesh_.prev_halfedge_handle(*he_it));
perHalfedgeVertexBuf_[idx] = halfedge_point<NormalLookup>(*he_it);
perHalfedgeVertexBuf_[idx+1] = halfedge_point<NormalLookup>(mesh_.prev_halfedge_handle(*he_it));
if ( mesh_.has_halfedge_colors() ) {
const Vec4f color = OpenMesh::color_cast<Vec4f>( mesh_.color(*he_it) ) ;
......@@ -1498,10 +1499,9 @@ void DrawMeshT<Mesh>::updatePerHalfedgeBuffers()
updateEdgeHalfedgeVertexDeclarations();
}
template <class Mesh>
typename Mesh::Point
DrawMeshT<Mesh>::
halfedge_point(const typename Mesh::HalfedgeHandle _heh) {
template<class Mesh>
template<typename Mesh::Normal (DrawMeshT<Mesh>::*NormalLookup)(typename Mesh::FaceHandle)>
typename Mesh::Point DrawMeshT<Mesh>::halfedge_point(const typename Mesh::HalfedgeHandle _heh) {
typename Mesh::Point p = mesh_.point(mesh_.to_vertex_handle (_heh));
typename Mesh::Point pp = mesh_.point(mesh_.from_vertex_handle(_heh));
......@@ -1510,9 +1510,11 @@ halfedge_point(const typename Mesh::HalfedgeHandle _heh) {
// typename Mesh::Point n = (p-pp)%(pn-p);
typename Mesh::Point fn;
if( !mesh_.is_boundary(_heh))
fn = mesh_.normal(mesh_.face_handle(_heh));
//fn = mesh_.normal(mesh_.face_handle(_heh));
fn = (this->*NormalLookup)(mesh_.face_handle(_heh));
else
fn = mesh_.normal(mesh_.face_handle(mesh_.opposite_halfedge_handle(_heh)));
//fn = mesh_.normal(mesh_.face_handle(mesh_.opposite_halfedge_handle(_heh)));
fn = (this->*NormalLookup)(mesh_.face_handle(mesh_.opposite_halfedge_handle(_heh)));
typename Mesh::Point upd = ((fn%(pn-p)).normalize() + (fn%(p-pp)).normalize()).normalize();
......@@ -1530,9 +1532,15 @@ halfedge_point(const typename Mesh::HalfedgeHandle _heh) {
template <class Mesh>
ACG::Vec3f* DrawMeshT<Mesh>::perHalfedgeVertexBuffer()
{
// Force update of the buffers if required
if (updatePerHalfedgeBuffers_)
updatePerHalfedgeBuffers();
// Force update of the buffers if required
if (updatePerHalfedgeBuffers_) {
if (mesh_.has_face_normals())
updatePerHalfedgeBuffers<&DrawMeshT::cachedNormalLookup>();
else if (mesh_.is_trimesh())
updatePerHalfedgeBuffers<&DrawMeshT::computedTriMeshNormal>();
else
updatePerHalfedgeBuffers<&DrawMeshT::computedNormal>();
}
return perHalfedgeVertexBuf_.empty() ? 0 : &(perHalfedgeVertexBuf_[0]);
}
......@@ -1540,8 +1548,14 @@ template <class Mesh>
ACG::Vec4f* DrawMeshT<Mesh>::perHalfedgeColorBuffer()
{
// Force update of the buffers if required
if (updatePerHalfedgeBuffers_)
updatePerHalfedgeBuffers();
if (updatePerHalfedgeBuffers_) {
if (mesh_.has_face_normals())
updatePerHalfedgeBuffers<&DrawMeshT::cachedNormalLookup>();
else if (mesh_.is_trimesh())
updatePerHalfedgeBuffers<&DrawMeshT::computedTriMeshNormal>();
else
updatePerHalfedgeBuffers<&DrawMeshT::computedNormal>();
}
return perHalfedgeColorBuf_.empty() ? 0 : &(perHalfedgeColorBuf_[0]);
}
......
......@@ -816,6 +816,7 @@ public:
*n
* The updated buffers are: per edge vertex buffer ( 2 vertices per edge )
*/
template<typename Mesh::Normal (DrawMeshT::*NormalLookup)(typename Mesh::FaceHandle)>
void updatePerHalfedgeBuffers();
/** \brief get a pointer to the per edge vertex buffer
......@@ -865,8 +866,31 @@ private:
*
* @return
*/
template<typename Mesh::Normal (DrawMeshT::*NormalLookup)(typename Mesh::FaceHandle)>
typename Mesh::Point halfedge_point(const typename Mesh::HalfedgeHandle _heh);
inline
typename Mesh::Normal cachedNormalLookup(typename Mesh::FaceHandle fh) {
return mesh_.normal(fh);
}
inline
typename Mesh::Normal computedTriMeshNormal(typename Mesh::FaceHandle fh) {
typename Mesh::FVIter fv_iter = mesh_.fv_begin(fh);
return (mesh_.point(*fv_iter) + mesh_.point(*(++fv_iter)) + mesh_.point(*(++fv_iter))) / 3.0;
}
inline
typename Mesh::Normal computedNormal(typename Mesh::FaceHandle fh) {
unsigned int count = 0;
typename Mesh::Normal normal(0, 0, 0);
for (typename Mesh::FVIter fv_it = mesh_.fv_begin(fh), fv_end = mesh_.fv_end(fh); fv_it != fv_end; ++fv_it) {
normal += mesh_.point(*fv_it);
++count;
}
normal /= count;
return normal;
}
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment