Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python 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
    • Recent
    • Tags
    • Users
    • Register
    • Login

    How to find the closest primitive in a mesh

    Scheduled Pinned Locked Moved Cinema 4D SDK
    4 Posts 2 Posters 76 Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic was forked from [FWD] How to retrieve the index of the closest polygon in a mesh to a point? ferdinand
    This topic has been deleted. Only users with topic management privileges can see it.
    • dexD Offline
      dex
      last edited by dex

      I was very happy to find this nice example. Thanks Ferdinand!
      However looking at the docs and If I have understood this correctly, I should be able to include <distancequery.h> to get access to the DistanceQueryInterface.
      My problem is that the <distancequery.h> source file is not found.
      All common frameworks are available to my build system.
      Any help or hint on what to look for would be much appreciated. I'm sure I have overlooked something trivial. 😃

      set(maxon_targetFrameworkDependencies
      asset.framework
      cinema.framework
      cinema_hybrid.framework
      crypt.framework
      core.framework
      corenodes.framework
      command.framework
      image.framework
      math.framework
      misc.framework
      mesh_misc.framework
      python.framework
      )

      ferdinandF 1 Reply Last reply Reply Quote 0
      • ferdinandF Offline
        ferdinand @dex
        last edited by ferdinand

        Hey @dex,

        distancequery.h is part of the mesh_misc.framework which you alreeady include. Sounds a bit like you have a corrupted build system on your hands or are not using the frameworks you think you are using. Where does the CMake fragment come from you show us? I assume you are using the old projectdefintion.txt sourced workflow (in which case you should us show that file). Or are you a beta member and have access to the 2027 SDK (which is natively pure CMake), in which case this would be invalid CMake markup, as some stuff has changed.

        Have you checked in the IDE of your choice that the mesh_misc.framework shows up as a dependency of the project in which you want to #include "maxon/distancequery.h"? And just for the recoord, this is not a std lib, so you cannot use #include <> but must use #include "". And since this is part of the Maxon API, you also need to specify the maxon folder.

        Cheers,
        Ferdinand

        MAXON SDK Specialist
        developers.maxon.net

        1 Reply Last reply Reply Quote 0
        • dexD Offline
          dex
          last edited by dex

          Thank you Ferdinand.
          I have everything working now. The #include <> brackets was just a copy paste thing from the docs. I just had to add the path as you pointed out "maxon/distancequery.h".
          One last question about the behavior of
          float distance = distanceQuery.GetClosestMeshPrimitive(p, info).

          Suppose the user have scaled the polygon mesh using the scale values under the attributes/coordinates. How will this be reflected as a result of the returned distance (assuming the point in the point mesh is first converted to global coordinates and then to local coordinates in the polygon node) like your example above? I see an issue here in my tests where the distance returned is unaffected by the scaling. Either I have done something wrong or the distanceQuery is done on the non "deformed/scaled" mesh.

          The point generator in this case is not transformed or deformed in any way.

          torus.png torus_scaled.png scale.pngscale.png

          ferdinandF 1 Reply Last reply Reply Quote 0
          • ferdinandF Offline
            ferdinand @dex
            last edited by ferdinand

            First of all, you should open new topics for new questions, as one otherwise tends to derail old topics, which makes it hard to for others to follow things later. I forked your topic.

            And I also do not quite understand what your question is. GetClosestMeshPrimitive returns you the primitive type via primInfo.

            When you only want to get the closest point on a mesh, I would build a KD-Tree for its vertices. You then get the closest vertex and its index for your point p. Then you orthogonally project p onto the polygons attached to that vertex (via cinema:: Neighbor). Then you chose the closest point. We have this Python example for KDTree and I also used Neighbor there. Building a KD-tree or just brute forcing it depends a bit on how often you want to do it. For just a handful of points, brute forcing it is usually faster.

            I am sure you can also do this with DistanceQueryInterface and the uvw coordinate returned in the PrimitiveInformation, but all that extra work where you have to deal with all the primitive types seems like more work than it is worth. And when you want to do this in a generator (by the looks of your screens), using a manual KDTree approach will make it easier to parallelize it effectively and make it go zoom zoom instead of computing all that extra data.

            Cheers,
            Ferdinand

            MAXON SDK Specialist
            developers.maxon.net

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