64 #ifndef OPENMESH_KERNEL_OSG_BINDT_HH
65 #define OPENMESH_KERNEL_OSG_BINDT_HH
74 #include <OpenMesh/Core/Mesh/TriMeshT.hh>
75 #include <OpenMesh/Core/Utils/color_cast.hh>
76 #include <OpenMesh/Tools/Utils/GLConstAsString.hh>
77 #include <OpenSG/OSGGeometry.h>
79 #include "color_cast.hh"
84 namespace Kernel_OSG {
90 bool type_is_valid(
unsigned char _t )
92 return _t == GL_TRIANGLES
93 || _t == GL_TRIANGLE_STRIP
105 template <
typename Mesh >
inline
106 bool bind( osg::GeometryPtr& _geo, Mesh& _mesh )
108 _geo = _mesh.createGeometryPtr();
119 template <
typename Mesh >
inline
120 bool bind( Mesh& _mesh, osg::GeometryPtr& _geo )
130 GeoPTypesPtr types = _geo->getTypes();
132 if ( (
size_t)count_if( types->getData(), types->getData()+types->size(),
133 ptr_fun(type_is_valid) ) != (
size_t)types->size() )
138 if ( _geo->getIndexMapping().getSize() > 1 )
140 omerr <<
"OpenMesh::Kernel_OSG::bind(): Multi-indexed geometry is not supported!\n";
147 GeoPLengthsPtr lengths = _geo->getLengths();
148 GeoIndicesPtr indices = _geo->getIndices();
149 GeoPositionsPtr pos = _geo->getPositions();
150 GeoNormalsPtr normals = _geo->getNormals();
151 GeoColorsPtr colors = _geo->getColors();
157 vector< VertexHandle > vhandles;
165 bool bind_normal = (normals!=NullFC) && _mesh.has_vertex_normals();
166 bool bind_color = (colors !=NullFC) && _mesh.has_vertex_colors();
168 for (bidx=0; bidx < pos->size(); ++bidx)
170 vh = _mesh.add_vertex( pos->getValue(bidx) );
172 _mesh.set_normal(vh, normals->getValue(bidx));
174 _mesh.set_color(vh, color_cast<color_t>(colors->getValue(bidx)));
182 size_t max_bidx = indices != NullFC ? indices->size() : pos->size();
184 for (bidx=tidx=0; ok && tidx<types->size() && bidx < max_bidx; ++tidx)
186 switch( types->getValue(tidx) )
190 for(
size_t lidx=0; lidx < lengths->getValue(tidx)-2; lidx+=3)
192 if (indices == NullFC ) {
198 vhandles[0] =
VertexHandle(indices->getValue(bidx+lidx ) );
199 vhandles[1] =
VertexHandle(indices->getValue(bidx+lidx+1) );
200 vhandles[2] =
VertexHandle(indices->getValue(bidx+lidx+2) );
203 if ( !(fh = _mesh.add_face( vhandles )).is_valid() )
206 swap(vhandles[2], vhandles[1]);
207 fh = _mesh.add_face( vhandles );
213 case GL_TRIANGLE_STRIP:
215 for (
size_t lidx=0; lidx < lengths->getValue(tidx)-2; ++lidx)
217 if (indices == NullFC ) {
223 vhandles[0] =
VertexHandle(indices->getValue(bidx+lidx ) );
224 vhandles[1] =
VertexHandle(indices->getValue(bidx+lidx+1) );
225 vhandles[2] =
VertexHandle(indices->getValue(bidx+lidx+2) );
228 if (vhandles[0]!=vhandles[2] &&
229 vhandles[0]!=vhandles[1] &&
230 vhandles[1]!=vhandles[2])
236 swap(vhandles[2], vhandles[1]);
238 if ( !(fh = _mesh.add_face( vhandles )).is_valid() )
240 omlog <<
"OpenMesh::Kernel_OSG::bind(): complex entity!\n";
242 swap(vhandles[2], vhandles[1]);
243 fh = _mesh.add_face( vhandles );
253 for(
size_t nf=_mesh.n_faces(), lidx=0;
254 lidx < lengths->getValue(tidx)-3; lidx+=4)
256 if (indices == NullFC ) {
263 vhandles[0] =
VertexHandle(indices->getValue(bidx+lidx ) );
264 vhandles[1] =
VertexHandle(indices->getValue(bidx+lidx+1) );
265 vhandles[2] =
VertexHandle(indices->getValue(bidx+lidx+2) );
266 vhandles[3] =
VertexHandle(indices->getValue(bidx+lidx+3) );
269 fh = _mesh.add_face( vhandles );
270 ok = ( Mesh::Face::is_triangle() && (_mesh.n_faces()==(nf+2)))
272 nf = _mesh.n_faces();
278 size_t ne = lengths->getValue(tidx);
279 size_t nf = _mesh.n_faces();
283 for(
size_t lidx=0; lidx < ne; ++lidx)
284 vhandles[lidx] = (indices == NullFC)
288 fh = _mesh.add_face( vhandles );
289 ok = ( Mesh::Face::is_triangle() && (_mesh.n_faces()==nf+ne-2) )
295 cerr <<
"Warning! Skipping unsupported type "
296 << types->getValue(tidx) <<
" '"
297 << Utils::GLenum_as_string( types->getValue(tidx) ) <<
"'\n";
301 bidx += lengths->getValue(tidx);
317 #endif // OPENMESH_KERNEL_OSG_BINDT_HH defined
Handle for a vertex entity.
Definition: Handles.hh:125
bool is_valid() const
The handle is valid iff the index is not equal to -1.
Definition: Handles.hh:77
bool bind(osg::GeometryPtr &_geo, Mesh &_mesh)
Bind a OpenSG geometry to a mesh.
Definition: bindT.hh:106
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:64
Kernel::Color Color
Color type.
Definition: PolyMeshT.hh:119
Handle for a face entity.
Definition: Handles.hh:146