| 515 | } // namespace |
| 516 | |
| 517 | bool Primitive::isPrimitiveVariableValid( const PrimitiveVariable &pv ) const |
| 518 | { |
| 519 | if( !pv.data || pv.interpolation==PrimitiveVariable::Invalid ) |
| 520 | { |
| 521 | return false; |
| 522 | } |
| 523 | |
| 524 | if( !pv.indices && pv.interpolation == PrimitiveVariable::Constant ) |
| 525 | { |
| 526 | // any data is reasonable for constant interpolation |
| 527 | // provided there are no indices. if there are indices |
| 528 | // we still need to validate their range below. |
| 529 | return true; |
| 530 | } |
| 531 | |
| 532 | // All other interpolations require an array of data or an array of indices |
| 533 | // of the correct length. It could be argued that SimpleTypedData should be |
| 534 | // accepted in the rare case that variableSize==1, but we're rejecting that |
| 535 | // argument on the grounds that it makes for a whole bunch of special cases |
| 536 | // with no gain - the general cases require arrays. Note that variableSize |
| 537 | // of 0 is a special case for empty data, which we must allow to pass here. |
| 538 | size_t sz = variableSize( pv.interpolation ); |
| 539 | size_t dataSize = despatchTypedData<TypedDataSize, TypeTraits::IsVectorTypedData, ReturnZeroErrorHandler>( pv.data.get() ); |
| 540 | if( !dataSize && sz ) |
| 541 | { |
| 542 | return false; |
| 543 | } |
| 544 | |
| 545 | /// without indices, the data must match exactly |
| 546 | /// \todo This is not correct in the case of CurvesPrimitives, where uniform |
| 547 | /// interpolation should be treated the same as constant. |
| 548 | if( !pv.indices ) |
| 549 | { |
| 550 | return dataSize == sz; |
| 551 | } |
| 552 | |
| 553 | // if there are indices, we must ensure they're within the range of data |
| 554 | // and that they match the expected variable size. the exception for |
| 555 | // constant variables, since size is irrelevant as the entire array |
| 556 | // is considered the variable value |
| 557 | if( pv.indices->readable().size() != sz && pv.interpolation != PrimitiveVariable::Constant ) |
| 558 | { |
| 559 | return false; |
| 560 | } |
| 561 | |
| 562 | for( const auto &index : pv.indices->readable() ) |
| 563 | { |
| 564 | if( index >= (int)dataSize ) |
| 565 | { |
| 566 | return false; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | // the data doesn't need further validation since we know its |
| 571 | // VectorTypedData and the indices are in the correct range. |
| 572 | return true; |
| 573 | } |
| 574 | |