| 46 | using namespace IECoreScene; |
| 47 | |
| 48 | std::vector<MeshPrimitivePtr> IECoreScene::MeshAlgo::segment( const MeshPrimitive *mesh, const PrimitiveVariable &primitiveVariable, const IECore::Data *segmentValues, const Canceller *canceller ) |
| 49 | { |
| 50 | MeshSplitter ms( mesh, primitiveVariable, canceller ); |
| 51 | int numMeshes = ms.numMeshes(); |
| 52 | std::vector<MeshPrimitivePtr> ret; |
| 53 | ret.reserve( numMeshes ); |
| 54 | |
| 55 | if( !segmentValues ) |
| 56 | { |
| 57 | for( int i = 0; i < numMeshes; i++ ) |
| 58 | { |
| 59 | ret.push_back( ms.mesh( i ) ); |
| 60 | } |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | IECore::dispatch( segmentValues, |
| 65 | [ ms, numMeshes, &ret, canceller ]( auto *typedSegmentValues ) |
| 66 | { |
| 67 | using DataType = typename std::remove_pointer_t< decltype( typedSegmentValues ) >; |
| 68 | if constexpr ( TypeTraits::IsVectorTypedData<DataType>::value ) |
| 69 | { |
| 70 | using ElementType = typename DataType::ValueType::value_type; |
| 71 | |
| 72 | std::unordered_map< ElementType, int > idMap; |
| 73 | Canceller::check( canceller ); |
| 74 | for( int i = 0; i < numMeshes; i++ ) |
| 75 | { |
| 76 | idMap[ ms.value<ElementType>( i ) ] = i; |
| 77 | } |
| 78 | |
| 79 | Canceller::check( canceller ); |
| 80 | for( const auto &i : typedSegmentValues->readable() ) |
| 81 | { |
| 82 | auto f = idMap.find( i ); |
| 83 | if( f == idMap.end() ) |
| 84 | { |
| 85 | ret.push_back( nullptr ); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | ret.push_back( ms.mesh( idMap[i] ) ); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | throw IECore::Exception( "Invalid Primitive Variable with non-vector typed data." ); |
| 96 | } |
| 97 | } |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | return ret; |
| 102 | } |