Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    DistanceQuery

    Cinema 4D SDK
    c++ r20 sdk
    2
    3
    462
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • N
      NesNes
      last edited by NesNes

      Hi,

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

      Does anyone have an example of this?

      Thanks

      1 Reply Last reply Reply Quote 0
      • ManuelM
        Manuel
        last edited by Manuel

        hello

        For your next threads, please help us keeping things organised and clean.

        • Q&A New Functionality.

        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;
        
        	
        	CPolygon *polyAdrW = mesh->GetPolygonW();
        	Vector* padrW	=	mesh->GetPointW();
        	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;
        			padrW[pointIndice] = pointA;
        			padrW[pointIndice + 1] = pointB;
        			padrW[pointIndice + 2] = pointC;
        			padrW[pointIndice + 3] = pointD;
        			poly.a = pointIndice;
        			poly.b = pointIndice + 1;
        			poly.c = pointIndice + 2;
        			poly.d = pointIndice + 3;
        		
        		polyAdrW[i] = poly;
        
        	}
        	
        	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

        MAXON SDK Specialist

        MAXON Registered Developer

        1 Reply Last reply Reply Quote 0
        • N
          NesNes
          last edited by

          thank you so much!!

          1 Reply Last reply Reply Quote 0
          • First post
            Last post