57 #ifndef OPENMESH_KERNEL_OSG_BINDT_HH
58 #define OPENMESH_KERNEL_OSG_BINDT_HH
67 #include <OpenMesh/Core/Mesh/TriMeshT.hh>
68 #include <OpenMesh/Core/Utils/color_cast.hh>
69 #include <OpenMesh/Tools/Utils/GLConstAsString.hh>
70 #include <OpenSG/OSGGeometry.h>
72 #include "color_cast.hh"
77 namespace Kernel_OSG {
83 bool type_is_valid(
unsigned char _t )
85 return _t == GL_TRIANGLES
86 || _t == GL_TRIANGLE_STRIP
98 template <
typename Mesh >
inline
99 bool bind( osg::GeometryPtr& _geo, Mesh& _mesh )
101 _geo = _mesh.createGeometryPtr();
112 template <
typename Mesh >
inline
113 bool bind( Mesh& _mesh, osg::GeometryPtr& _geo )
115 using namespace OpenMesh;
123 GeoPTypesPtr types = _geo->getTypes();
125 if ( (
size_t)count_if( types->getData(), types->getData()+types->size(),
126 ptr_fun(type_is_valid) ) != (
size_t)types->size() )
131 if ( _geo->getIndexMapping().getSize() > 1 )
133 omerr <<
"OpenMesh::Kernel_OSG::bind(): Multi-indexed geometry is not supported!\n";
140 GeoPLengthsPtr lengths = _geo->getLengths();
141 GeoIndicesPtr indices = _geo->getIndices();
142 GeoPositionsPtr pos = _geo->getPositions();
143 GeoNormalsPtr normals = _geo->getNormals();
144 GeoColorsPtr colors = _geo->getColors();
150 vector< VertexHandle > vhandles;
158 bool bind_normal = (normals!=NullFC) && _mesh.has_vertex_normals();
159 bool bind_color = (colors !=NullFC) && _mesh.has_vertex_colors();
161 for (bidx=0; bidx < pos->size(); ++bidx)
163 vh = _mesh.add_vertex( pos->getValue(bidx) );
165 _mesh.set_normal(vh, normals->getValue(bidx));
167 _mesh.set_color(vh, color_cast<color_t>(colors->getValue(bidx)));
175 size_t max_bidx = indices != NullFC ? indices->size() : pos->size();
177 for (bidx=tidx=0; ok && tidx<types->size() && bidx < max_bidx; ++tidx)
179 switch( types->getValue(tidx) )
183 for(
size_t lidx=0; lidx < lengths->getValue(tidx)-2; lidx+=3)
185 if (indices == NullFC ) {
191 vhandles[0] =
VertexHandle(indices->getValue(bidx+lidx ) );
192 vhandles[1] =
VertexHandle(indices->getValue(bidx+lidx+1) );
193 vhandles[2] =
VertexHandle(indices->getValue(bidx+lidx+2) );
196 if ( !(fh = _mesh.add_face( vhandles )).is_valid() )
199 swap(vhandles[2], vhandles[1]);
200 fh = _mesh.add_face( vhandles );
206 case GL_TRIANGLE_STRIP:
208 for (
size_t lidx=0; lidx < lengths->getValue(tidx)-2; ++lidx)
210 if (indices == NullFC ) {
216 vhandles[0] =
VertexHandle(indices->getValue(bidx+lidx ) );
217 vhandles[1] =
VertexHandle(indices->getValue(bidx+lidx+1) );
218 vhandles[2] =
VertexHandle(indices->getValue(bidx+lidx+2) );
221 if (vhandles[0]!=vhandles[2] &&
222 vhandles[0]!=vhandles[1] &&
223 vhandles[1]!=vhandles[2])
229 swap(vhandles[2], vhandles[1]);
231 if ( !(fh = _mesh.add_face( vhandles )).is_valid() )
233 omlog <<
"OpenMesh::Kernel_OSG::bind(): complex entity!\n";
235 swap(vhandles[2], vhandles[1]);
236 fh = _mesh.add_face( vhandles );
246 for(
size_t nf=_mesh.n_faces(), lidx=0;
247 lidx < lengths->getValue(tidx)-3; lidx+=4)
249 if (indices == NullFC ) {
256 vhandles[0] =
VertexHandle(indices->getValue(bidx+lidx ) );
257 vhandles[1] =
VertexHandle(indices->getValue(bidx+lidx+1) );
258 vhandles[2] =
VertexHandle(indices->getValue(bidx+lidx+2) );
259 vhandles[3] =
VertexHandle(indices->getValue(bidx+lidx+3) );
262 fh = _mesh.add_face( vhandles );
263 ok = ( Mesh::Face::is_triangle() && (_mesh.n_faces()==(nf+2)))
265 nf = _mesh.n_faces();
271 size_t ne = lengths->getValue(tidx);
272 size_t nf = _mesh.n_faces();
276 for(
size_t lidx=0; lidx < ne; ++lidx)
277 vhandles[lidx] = (indices == NullFC)
281 fh = _mesh.add_face( vhandles );
282 ok = ( Mesh::Face::is_triangle() && (_mesh.n_faces()==nf+ne-2) )
288 cerr <<
"Warning! Skipping unsupported type "
289 << types->getValue(tidx) <<
" '"
290 << Utils::GLenum_as_string( types->getValue(tidx) ) <<
"'\n";
294 bidx += lengths->getValue(tidx);
310 #endif // OPENMESH_KERNEL_OSG_BINDT_HH defined
bool is_valid() const
The handle is valid iff the index is not equal to -1.
Definition: Handles.hh:70
bool bind(osg::GeometryPtr &_geo, Mesh &_mesh)
Bind a OpenSG geometry to a mesh.
Definition: bindT.hh:99
Handle for a face entity.
Definition: Handles.hh:135
Kernel::Color Color
Color type.
Definition: PolyMeshT.hh:112
Handle for a vertex entity.
Definition: Handles.hh:114