bool Object3D::findNearestVertex(Vec3f point, std::pair<unsigned int, Vec3f>& foundVertex) { bool found = false; unsigned int nbDrawables = getNumDrawables(); Vec3Array *vertexArray = NULL; double distMin = 100.0; double dist; for(unsigned int i=0; i<nbDrawables; i++) { vertexArray = getVertexList(i); if(vertexArray) { std::vector<Vec3>::iterator itr; for (itr=vertexArray->begin(); itr != vertexArray->end(); ++itr) { dist = Util::computeDist(*itr, point); if(dist < distMin) { distMin = dist; foundVertex = std::make_pair(i, *itr); found = true; } } } } return found; }
void Normals::MakeNormalsVisitor::apply( Geode &geode ) { for( unsigned int i = 0; i < geode.getNumDrawables(); i++ ) { Geometry *geom = dynamic_cast<Geometry *>(geode.getDrawable(i)); if( geom ) { if (geom->containsDeprecatedData()) geom->fixDeprecatedData(); Vec3Array *coords = dynamic_cast<Vec3Array*>(geom->getVertexArray()); if( coords == 0L ) continue; Vec3Array *normals = dynamic_cast<Vec3Array*>(geom->getNormalArray()); if( normals == 0L ) continue; Geometry::AttributeBinding binding = geom->getNormalBinding(); if( binding == Geometry::BIND_OFF ) continue; if( binding == Geometry::BIND_OVERALL ) { Vec3 v(0,0,0); Vec3 n = normals->front(); Vec3Array::iterator coord_index = coords->begin(); while( coord_index != coords->end() ) v += *(coord_index++) * _mat; v /= (float)(coords->size()); n *= _normal_scale; _local_coords->push_back( v ); _local_coords->push_back( (v + n)); } else // BIND_PER_PRIMITIVE_SET, BIND_PER_VERTEX { Geometry::PrimitiveSetList& primitiveSets = geom->getPrimitiveSetList(); Geometry::PrimitiveSetList::iterator itr; Vec3Array::iterator coord_index = coords->begin(); Vec3Array::iterator normals_index = normals->begin(); for(itr=primitiveSets.begin(); itr!=primitiveSets.end(); ++itr) { #ifdef DEBUG _printPrimitiveType( (*itr).get() ); #endif if( binding == Geometry::BIND_PER_PRIMITIVE_SET ) { Vec3 v(0,0,0); Vec3 n = *(normals_index++); int ni = (*itr)->getNumIndices(); for( int i = 0; i < ni; i++ ) v += *(coord_index++) * _mat; v /= (float)(ni); n *= _normal_scale; _local_coords->push_back( v ); _local_coords->push_back( (v + n)); } else { switch((*itr)->getMode()) { case(PrimitiveSet::TRIANGLES): { for( unsigned int j = 0; j < (*itr)->getNumPrimitives(); j++ ) { _processPrimitive( 3, coord_index, normals_index, binding ); coord_index += 3; normals_index+=3; } break; } case(PrimitiveSet::TRIANGLE_STRIP): { for( unsigned int j = 0; j < (*itr)->getNumIndices()-2; j++ ) { _processPrimitive( 3, coord_index, normals_index, binding ); coord_index++; normals_index++; } coord_index += 2; if( binding == Geometry::BIND_PER_VERTEX ) normals_index += 2; break; } case(PrimitiveSet::TRIANGLE_FAN): break; case(PrimitiveSet::QUADS): { for( unsigned int j = 0; j < (*itr)->getNumPrimitives(); j++ ) { _processPrimitive( 4, coord_index, normals_index, binding ); coord_index += 4; normals_index +=4; } break; } case(PrimitiveSet::QUAD_STRIP): break; case(PrimitiveSet::POLYGON): { DrawArrayLengths* dal = dynamic_cast<DrawArrayLengths*>((*itr).get()); if (dal) { for (unsigned int j = 0; j < dal->size(); ++j) { unsigned int num_prim = (*dal)[j]; //OSG_WARN << "j=" << j << " num_prim=" << num_prim << std::endl; _processPrimitive(num_prim, coord_index, normals_index, binding); coord_index += num_prim; normals_index += num_prim; } } break; } default: break; } } } } } } traverse( geode ); }