#include <float.h>
#include <limits.h>
#ifdef MAYA_TBB
#include <tbb/blocked_range.h>
#include <tbb/parallel_reduce.h>
#else
#ifdef _OPENMP
#include <omp.h>
#endif
#endif
#include <maya/MIOStream.h>
#include <maya/MSimple.h>
#include <maya/MSelectionList.h>
#include <maya/MGlobal.h>
#include <maya/MFnMesh.h>
#include <maya/MDagPath.h>
#include <maya/MTimer.h>
#include <maya/MFloatPointArray.h>
#include <maya/MThreadUtils.h>
{
const int floatSize = sizeof(float);
int spacing = 1;
if(padding) {
spacing = 2+(cacheLineSize/floatSize);
}
#if defined(MAYA_TBB)
float finalMinX = tbb::parallel_reduce(tbb::blocked_range<int>(0, vertexArray.
length()),
float(FLT_MAX),
[&](const tbb::blocked_range<int> &br, float myMin) -> float {
for (int i = br.begin(); i < br.end(); i++) {
if (p.
x < myMin) myMin = p.
x;
}
return myMin;
},
[&](const float rMin, const float lMin) -> float {
return rMin < lMin ? rMin : lMin;
});
#else
int numThreads = 1;
#ifdef _OPENMP
numThreads = omp_get_max_threads();
#endif
float* pointArray = new float[numThreads*spacing];
const int nb = vertexArray.
length();
const int nbStep = nb / numThreads;
#ifdef _OPENMP
#pragma omp parallel for
#endif
for(int i=0; i<numThreads; i++) {
float& minX = pointArray[i*spacing];
minX = FLT_MAX;
const int n1 = i*nbStep;
const int n2 = (i<(numThreads-1)) ? (i+1)*nbStep : nb;
for (int n=n1; n<n2; n++) {
if(p.
x < minX) minX = p.
x;
}
}
float finalMinX = FLT_MAX;
for(int i=0; i<numThreads; i++) {
if(pointArray[i*spacing] < finalMinX) finalMinX = pointArray[i*spacing];
}
delete [] pointArray;
#endif
return finalMinX;
}
{
int numSelected = curSel.
length();
for( int s = 0; s < numSelected; s++ )
{
{
cout<<" Error - object is not a polymesh"<<endl;
stat = MS::kFailure;
return stat;
}
if( stat != MS::kSuccess )
{
cout<<" Error - unable to create MFnMesh object"<<endl;
return stat;
}
stat = fnMesh.getPoints(vertexArray );
if( stat != MS::kSuccess ) {
cout<<" Error - unable to retrieve vertices"<<endl;
return stat;
}
float minX1 = 0.0f;
float minX2 = 0.0f;
cout<<
" Poly has "<< vertexArray.
length()<<
" vertices"<<endl;
bool padding = false;
int numIterations = 100;
for(int i=0; i<numIterations; i++) {
minX1 = computeMinX(vertexArray, padding);
}
printf(
"Runtime without padding %f\n", timer.
elapsedTime());
padding = true;
for(int i=0; i<numIterations; i++) {
minX2 = computeMinX(vertexArray, padding);
}
printf(
"Runtime with padding %f\n", timer.
elapsedTime());
if(fabs(minX1-minX2)<1e-10) {
cout << "Boxes match" << endl;
} else {
cout << "Boxes do not match" << endl;
stat = MS::kFailure;
return stat;
}
}
setResult( "threadedBoundingBox completed." );
return stat;
}