| 212 | } |
| 213 | |
| 214 | MeshPrimitiveEvaluator::MeshPrimitiveEvaluator( ConstMeshPrimitivePtr mesh ) : m_uvTree(nullptr), m_haveMassProperties( false ), m_haveSurfaceArea( false ), m_haveAverageNormals( false ) |
| 215 | { |
| 216 | if (! mesh ) |
| 217 | { |
| 218 | throw InvalidArgumentException( "No mesh given to MeshPrimitiveEvaluator"); |
| 219 | } |
| 220 | |
| 221 | if (! mesh->arePrimitiveVariablesValid() ) |
| 222 | { |
| 223 | throw InvalidArgumentException( "Mesh with invalid primitive variables given to MeshPrimitiveEvaluator"); |
| 224 | } |
| 225 | |
| 226 | m_mesh = mesh->copy(); |
| 227 | |
| 228 | |
| 229 | PrimitiveVariableMap::const_iterator primVarIt = m_mesh->variables.find("P"); |
| 230 | if ( primVarIt == m_mesh->variables.end() ) |
| 231 | { |
| 232 | throw InvalidArgumentException( "Mesh given to MeshPrimitiveEvaluator has no \"P\""); |
| 233 | } |
| 234 | |
| 235 | m_verts = runTimeCast< const V3fVectorData > ( primVarIt->second.data ); |
| 236 | |
| 237 | if (! m_verts ) |
| 238 | { |
| 239 | throw InvalidArgumentException( "Mesh given to MeshPrimitiveEvaluator has no \"P\" primvar of type V3fVectorData"); |
| 240 | } |
| 241 | |
| 242 | m_meshVertexIds = &(m_mesh->vertexIds()->readable()); |
| 243 | |
| 244 | m_uv.interpolation = PrimitiveVariable::Invalid; |
| 245 | primVarIt = m_mesh->variables.find( "uv" ); |
| 246 | if ( primVarIt != m_mesh->variables.end() ) |
| 247 | { |
| 248 | m_uv = primVarIt->second; |
| 249 | } |
| 250 | |
| 251 | const std::vector<int> &verticesPerFace = m_mesh->verticesPerFace()->readable(); |
| 252 | m_triangles.reserve( verticesPerFace.size() ); |
| 253 | m_uvTriangles.reserve( verticesPerFace.size() ); |
| 254 | unsigned int triangleIdx = 0; |
| 255 | IntVectorData::ValueType::const_iterator vertexIdIt = m_meshVertexIds->begin(); |
| 256 | for ( IntVectorData::ValueType::const_iterator it = verticesPerFace.begin(); |
| 257 | it != verticesPerFace.end(); ++it, ++triangleIdx) |
| 258 | { |
| 259 | |
| 260 | if (*it != 3 ) |
| 261 | { |
| 262 | throw InvalidArgumentException( "Non-triangular mesh given to MeshPrimitiveEvaluator"); |
| 263 | } |
| 264 | |
| 265 | Imath::V3i triangleVertexIds; |
| 266 | |
| 267 | triangleVertexIds[0] = *vertexIdIt++; |
| 268 | assert( triangleVertexIds[0] < (int)( m_verts->readable().size() ) ); |
| 269 | triangleVertexIds[1] = *vertexIdIt++; |
| 270 | assert( triangleVertexIds[1] < (int)( m_verts->readable().size() ) ); |
| 271 | triangleVertexIds[2] = *vertexIdIt++; |