# SOLVEDDistanceQuery

Hi,

I'm trying to understand how to use the DistanceQueryInterface.

Does anyone have an example of this?

Thanks

hello

This example show how to use the Interface.

The declaration is in `distancequery.h`
In this file you find this `MAXON_DECLARATION` that define what you have to use to create your ref.

``````MAXON_DECLARATION(Class<DistanceQueryRef>, DistanceCalculator, "net.maxon.geom.interface.distancequery.distancecalculator");
``````

so you can use :

``````maxon::DistanceQueryRef distanceQueryRef = maxon::DistanceCalculator().Create() iferr_return;
``````
``````        iferr_scope;
// ------- Create a mesh

const maxon::Int32 polyCnt{ 500 };

PolygonObject *mesh = PolygonObject::Alloc(polyCnt * 4, polyCnt );
if (mesh == nullptr)
return maxon::OutOfMemoryError(MAXON_SOURCE_LOCATION);

maxon::LinearCongruentialRandom<maxon::Float32> random;

const Float polySize{ 10 };

for (maxon::Int i = 0; i < polyCnt; i++)
{
CPolygon poly;

maxon::Vector pointA(i * polySize, 0, 0);
maxon::Vector pointB(i * polySize, 0, polySize);
maxon::Vector pointC(i * polySize + polySize, 0, polySize);
maxon::Vector pointD(i * polySize + polySize, 0, 0);

const maxon::Int pointIndice = i * 4;
poly.a = pointIndice;
poly.b = pointIndice + 1;
poly.c = pointIndice + 2;
poly.d = pointIndice + 3;

}

doc->InsertObject(mesh, nullptr, nullptr);
// Creates the reference.
maxon::DistanceQueryRef distanceQueryRef = maxon::DistanceCalculator().Create() iferr_return;
// Inits the mesh
distanceQueryRef.Init(mesh, true) iferr_return;

maxon::PrimitiveInformation result;
const maxon::Int samplePointCnt = polyCnt;
//----------------------------------------------------------------------------------------
/// sample using standard loop
maxon::TimeValue start = maxon::TimeValue(maxon::TimeValue::NOW);

for (maxon::Int i = 0; i < samplePointCnt; ++i)
{
const maxon::Vector pos{ i * polySize + polySize * 0.5, 0, 0 };
distanceQueryRef.GetClosestMeshPrimitive(pos, result);
if (result.type == maxon::PRIMITIVETYPE::POLYGON)
ApplicationOutput("pos @, polyIndex @ ", pos, result.GetRealPolyIndex());
else
ApplicationOutput("not a polygon");
}

ApplicationOutput("time to sample the points @ with MP", start.Stop());

//----------------------------------------------------------------------------------------
/// sample using parallelFor
start = maxon::TimeValue(maxon::TimeValue::NOW);
maxon::BaseArray< maxon::PrimitiveInformation> resultArray;
resultArray.Resize(samplePointCnt) iferr_return;

maxon::ParallelFor::Dynamic(0, samplePointCnt,
[&resultArray, &polySize, &distanceQueryRef](maxon::Int i)
{
maxon::PrimitiveInformation result;

const maxon::Vector pos{ i * polySize + polySize * 0.5, 0, 0 };
distanceQueryRef.GetClosestMeshPrimitive(pos, result);
if (result.type == maxon::PRIMITIVETYPE::POLYGON)
ApplicationOutput("pos @, polyIndex @ ", pos, result.GetRealPolyIndex());
else
ApplicationOutput("not a polygon");
resultArray[i] = result;
}
);
ApplicationOutput("time to sample the points @ with MP", start.Stop());

for (auto& value : resultArray)
{
if (value.type == maxon::PRIMITIVETYPE::POLYGON)
ApplicationOutput("polyIndex @ ", value.GetRealPolyIndex());
else
ApplicationOutput("not a polygon");

}

``````

Cheers,
Manuel

hello

This example show how to use the Interface.

The declaration is in `distancequery.h`
In this file you find this `MAXON_DECLARATION` that define what you have to use to create your ref.

``````MAXON_DECLARATION(Class<DistanceQueryRef>, DistanceCalculator, "net.maxon.geom.interface.distancequery.distancecalculator");
``````

so you can use :

``````maxon::DistanceQueryRef distanceQueryRef = maxon::DistanceCalculator().Create() iferr_return;
``````
``````        iferr_scope;
// ------- Create a mesh

const maxon::Int32 polyCnt{ 500 };

PolygonObject *mesh = PolygonObject::Alloc(polyCnt * 4, polyCnt );
if (mesh == nullptr)
return maxon::OutOfMemoryError(MAXON_SOURCE_LOCATION);

maxon::LinearCongruentialRandom<maxon::Float32> random;

const Float polySize{ 10 };

for (maxon::Int i = 0; i < polyCnt; i++)
{
CPolygon poly;

maxon::Vector pointA(i * polySize, 0, 0);
maxon::Vector pointB(i * polySize, 0, polySize);
maxon::Vector pointC(i * polySize + polySize, 0, polySize);
maxon::Vector pointD(i * polySize + polySize, 0, 0);

const maxon::Int pointIndice = i * 4;
poly.a = pointIndice;
poly.b = pointIndice + 1;
poly.c = pointIndice + 2;
poly.d = pointIndice + 3;

}

doc->InsertObject(mesh, nullptr, nullptr);
// Creates the reference.
maxon::DistanceQueryRef distanceQueryRef = maxon::DistanceCalculator().Create() iferr_return;
// Inits the mesh
distanceQueryRef.Init(mesh, true) iferr_return;

maxon::PrimitiveInformation result;
const maxon::Int samplePointCnt = polyCnt;
//----------------------------------------------------------------------------------------
/// sample using standard loop
maxon::TimeValue start = maxon::TimeValue(maxon::TimeValue::NOW);

for (maxon::Int i = 0; i < samplePointCnt; ++i)
{
const maxon::Vector pos{ i * polySize + polySize * 0.5, 0, 0 };
distanceQueryRef.GetClosestMeshPrimitive(pos, result);
if (result.type == maxon::PRIMITIVETYPE::POLYGON)
ApplicationOutput("pos @, polyIndex @ ", pos, result.GetRealPolyIndex());
else
ApplicationOutput("not a polygon");
}

ApplicationOutput("time to sample the points @ with MP", start.Stop());

//----------------------------------------------------------------------------------------
/// sample using parallelFor
start = maxon::TimeValue(maxon::TimeValue::NOW);
maxon::BaseArray< maxon::PrimitiveInformation> resultArray;
resultArray.Resize(samplePointCnt) iferr_return;

maxon::ParallelFor::Dynamic(0, samplePointCnt,
[&resultArray, &polySize, &distanceQueryRef](maxon::Int i)
{
maxon::PrimitiveInformation result;

const maxon::Vector pos{ i * polySize + polySize * 0.5, 0, 0 };
distanceQueryRef.GetClosestMeshPrimitive(pos, result);
if (result.type == maxon::PRIMITIVETYPE::POLYGON)
ApplicationOutput("pos @, polyIndex @ ", pos, result.GetRealPolyIndex());
else
ApplicationOutput("not a polygon");
resultArray[i] = result;
}
);
ApplicationOutput("time to sample the points @ with MP", start.Stop());

for (auto& value : resultArray)
{
if (value.type == maxon::PRIMITIVETYPE::POLYGON)
ApplicationOutput("polyIndex @ ", value.GetRealPolyIndex());
else
ApplicationOutput("not a polygon");

}

``````

Cheers,
Manuel

thank you so much!!