Skip to content

Commit cc71ff2

Browse files
Hacking
1 parent 92bee1a commit cc71ff2

File tree

1 file changed

+73
-109
lines changed

1 file changed

+73
-109
lines changed

src/IECoreScene/MeshAlgoSplit.cpp

Lines changed: 73 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -335,115 +335,7 @@ void initializeFaceToSegments(
335335

336336
IECoreScene::MeshAlgo::MeshSplitter::MeshSplitter( ConstMeshPrimitivePtr mesh, const PrimitiveVariable &segmentPrimitiveVariable, const IECore::Canceller *canceller ) : m_mesh( mesh ), m_segmentPrimitiveVariable( segmentPrimitiveVariable )
337337
{
338-
if( segmentPrimitiveVariable.interpolation != IECoreScene::PrimitiveVariable::Interpolation::Uniform )
339-
{
340-
throw IECore::Exception( "Primitive variable passed to MeshSplitter must be uniform." );
341-
}
342-
343-
if( !mesh->isPrimitiveVariableValid( segmentPrimitiveVariable ) )
344-
{
345-
throw IECore::Exception( "Primitive variable passed to MeshSplitter must be valid." );
346-
}
347-
348-
const size_t numFaces = mesh->numFaces();
349-
if( numFaces == 0 )
350-
{
351-
// If we don't initialize anything, numMeshes() will return 0, meaning there is no valid context to
352-
// call mesh() in, which is correct for an empty mesh
353-
return;
354-
}
355-
356-
int numSegments = 0;
357-
ConstIntVectorDataPtr faceToSegmentIndexData;
358-
std::vector<int> remapSegmentIndices;
359-
// remapSegmentIndexMin specifies the lowest value in the faceToSegmentIndexBuffer that we need to remap:
360-
// it shifts all accesses to the remapSegmentIndices, allowing remapSegmentIndices to be used when the
361-
// lowest element is not 0
362-
int remapSegmentIndexMin = 0;
363-
364-
365-
IECore::dispatch( segmentPrimitiveVariable.data.get(), [ segmentPrimitiveVariable, &numSegments, &faceToSegmentIndexData, &remapSegmentIndices, &remapSegmentIndexMin, canceller]( const auto *primVarData )
366-
{
367-
using DataType = typename std::remove_pointer_t< decltype( primVarData ) >;
368-
if constexpr ( !TypeTraits::IsVectorTypedData<DataType>::value )
369-
{
370-
throw IECore::Exception( "Invalid PrimitiveVariable, data is not a vector." );
371-
}
372-
else
373-
{
374-
initializeFaceToSegments< typename DataType::ValueType::value_type >(
375-
primVarData,
376-
segmentPrimitiveVariable.indices.get(),
377-
numSegments,
378-
faceToSegmentIndexData,
379-
remapSegmentIndices,
380-
remapSegmentIndexMin,
381-
canceller
382-
);
383-
}
384-
}
385-
);
386-
387-
const std::vector<int> &faceToSegmentIndex = faceToSegmentIndexData->readable();
388-
389-
// Now that we have our faceToSegmentIndex and remapSegmentIndices vector, we can count the number of faces
390-
// for each output mesh
391-
std::vector<int> faceCounts;
392-
faceCounts.resize( numSegments, 0 );
393-
394-
Canceller::check( canceller );
395-
396-
for( int i : faceToSegmentIndex )
397-
{
398-
faceCounts[ remapSegmentIndices[ i - remapSegmentIndexMin ] ]++;
399-
}
400-
401-
// We need store the faces so that it's easy to access all the faces for one output mesh at a time.
402-
// To keep things nice and contiguous, and avoid small allocations for small meshes, we will allocate
403-
// some vectors with the original size of the verticesPerFace vector, but sorted by output mesh index
404-
405-
Canceller::check( canceller );
406-
407-
// meshIndices stores the offset in m_faceRemap where each mesh starts
408-
m_meshIndices.reserve( faceCounts.size() );
409-
int meshStartIndex = 0;
410-
for( int c : faceCounts )
411-
{
412-
m_meshIndices.push_back( meshStartIndex );
413-
meshStartIndex += c;
414-
}
415-
416-
// Now output the faceRemap vector, which tells us for each output face, the index of the source face
417-
const std::vector<int> &verticesPerFace = mesh->verticesPerFace()->readable();
418-
419-
// We do this by keeping track of the current position for each output mesh, and scanning through
420-
// all the input faces, incrementing the correct output mesh position when we find a face for that
421-
// mesh.
422-
std::vector<int> curMeshIndices( m_meshIndices );
423-
424-
Canceller::check( canceller );
425-
426-
m_faceRemap.resize( numFaces );
427-
for( unsigned int faceIndex = 0; faceIndex < numFaces; faceIndex++ )
428-
{
429-
int meshId = remapSegmentIndices[ faceToSegmentIndex[ faceIndex ] - remapSegmentIndexMin ];
430-
m_faceRemap[ curMeshIndices[ meshId ] ] = faceIndex;
431-
curMeshIndices[ meshId ]++;
432-
}
433-
434-
Canceller::check( canceller );
435-
436-
// When accessing faces through m_faceRemap, we need to independently access a face based on its index.
437-
// We don't want to scan from the start summing all the verticesPerFace each time, so this requires
438-
// us to pre-accumulate a running sum of verticesPerFace, that we can index directly into
439-
int faceVertexIndex = 0;
440-
m_faceIndices.reserve( numFaces );
441-
for( int f : verticesPerFace )
442-
{
443-
m_faceIndices.push_back( faceVertexIndex );
444-
faceVertexIndex += f;
445-
}
446-
338+
throw IECore::Exception( "Hello world" );
447339
}
448340

449341
namespace {
@@ -596,6 +488,13 @@ class Reindexer
596488
}
597489
}
598490
}
491+
492+
std::string message;
493+
for( int i = 0; i < (int)dataRemap.size(); i++ )
494+
{
495+
message += std::to_string( dataRemap[ i ] ) + " ";
496+
}
497+
throw IECore::Exception( "DEBUG: " + message );
599498
}
600499

601500
private:
@@ -611,6 +510,11 @@ class Reindexer
611510

612511
m_indicesComputed = true;
613512

513+
std::cout << "COMPUTE INDICES\n";
514+
515+
516+
m_numIdsUsed = 0;
517+
614518
for( unsigned int blockId = 0; blockId < m_fromOldIds.size(); blockId++ )
615519
{
616520
auto &block = m_fromOldIds[ blockId ];
@@ -629,13 +533,55 @@ class Reindexer
629533
}
630534
}
631535

536+
632537
for( int &id : m_newIndices )
633538
{
634539
int blockId = id / m_blockSize;
635540
int subIndex = id % m_blockSize;
636541

542+
std::cout << id << " : " << (*m_fromOldIds[ blockId ])[subIndex] << "\n";
637543
id = (*m_fromOldIds[ blockId ])[subIndex];
544+
545+
}
546+
547+
std::cout << "DONE COMPUTE INDICES\n";
548+
549+
550+
std::vector<int> debugIds;
551+
552+
for( unsigned int blockId = 0; blockId < m_fromOldIds.size(); blockId++ )
553+
{
554+
auto &block = m_fromOldIds[ blockId ];
555+
if( !block )
556+
{
557+
continue;
558+
}
559+
560+
for( int i = 0; i < m_blockSize; i++ )
561+
{
562+
if( (*block)[i] != -1 )
563+
{
564+
debugIds.push_back( (*block)[i] );
565+
}
566+
}
638567
}
568+
569+
bool failed = false;
570+
for( int i = 0; i < (int)debugIds.size(); i++ )
571+
{
572+
failed |= debugIds[i] != i;
573+
}
574+
575+
if( failed )
576+
{
577+
std::string message;
578+
for( int i = 0; i < (int)debugIds.size(); i++ )
579+
{
580+
message += std::to_string( debugIds[ i ] ) + " ";
581+
}
582+
throw IECore::Exception( "BAD IDS " + message );
583+
}
584+
639585
}
640586

641587
// IntVectorData to hold the new indices
@@ -868,6 +814,24 @@ MeshPrimitivePtr IECoreScene::MeshAlgo::MeshSplitter::mesh( int segmentId, const
868814
std::vector<int> vertRemapBackwards;
869815
vertReindexer.getDataRemapping( vertRemapBackwards );
870816

817+
bool failure = false;
818+
for( int i = startIndex; i < endIndex; i++ )
819+
{
820+
int originalFaceIndex = m_faceRemap[i];
821+
int faceVerts = sourceVerticesPerFace[ originalFaceIndex ];
822+
int faceStart = m_faceIndices[ originalFaceIndex ];
823+
for( int j = 0; j < faceVerts; j++ )
824+
{
825+
int q = sourceVertexIds[ faceStart + j ];
826+
failure |= vertRemapBackwards[ vertReindexer.testIndex( q ) ] != q;
827+
}
828+
}
829+
830+
if( failure )
831+
{
832+
throw IECore::Exception( "detected" );
833+
}
834+
871835
MeshPrimitivePtr ret = new MeshPrimitive( verticesPerFaceData, vertReindexer.getNewIndices(), m_mesh->interpolation() );
872836

873837
// In order to remap the corners, we test every vertex in the original corner list, and see if it is

0 commit comments

Comments
 (0)