1

I'm trying to write my own CAD program, and it's pretty important that I give the user the ability to select vertices/edges/faces(triangles) of interest by drawing a box or polygon on a 2D screen, and then highlighting whatever is underneath in the 3D view (both ignoring the back faces and also not ignoring the back faces).

How is this done usually? Is there any open-source example I can look at? What's generally the process for this?

This is especially harder when you are trying to handle 1 million+ triangles.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Luca
  • 171
  • 1
  • 13
  • If your renderer allows it, a possibility is to assign every element to be detected a unique color (though the limit will be 24 millions) and render the image without shading in an image of the same size as the display but not visible. The the cursor position immediately tells you over which element it is. –  Apr 06 '21 at 20:04

1 Answers1

1

there are many ways to do this here two most often used:

  1. Ray picking

    First see:

    The idea is to cast a ray(s) from camera focal point into mouse (or cursor) direction and what the ray hits is selected. The link above exploits OpenGL rendering where you can do this very easily and fast (almost for free) and the result is pixel perfect. In order to use selecting box/polygon you need to read all of its pixels on CPU side and convert them to list of entities selected. This is slightly slower but still can be done very fast (regardless of complexity of the rendered scene). This approach is O(1) however if you do the same on CPU it will be much much slower with complexity O(n) while n is number of entities total (unless BVH or is Octree used). This method however will select only what is visible (so no back faces or objects behind).

  2. Geometry tests

    Basicaly your 2D rectangle will slice the perspective 3D frustrum to smaller one and what is inside or intersecting should be selected. You can compute this with geometry on CPU side in form of tests (object inside cuboid or box) something like this:

    The complexity is also O(n) unless BVH or Octree is used. This method will select all objects (even not visible ones).

Also I think this might be interesting reading for you:

It shows a simple C++ app architecture able of placing and moving objects in 2D. Its a basic start point for CAD like app. For 3D you just add the matrix math and or editation controls...

Also a hint for selecting in CAD/CAM software (IIRC AUTOCAD started with) is that if your selection box was created from left to right and from top to bottom manner you select all objects that are fully inside or intersect and if the selection box was created in reverse direction you select only what is fully inside. This way allows for more comfortable editation.

Spektre
  • 49,595
  • 11
  • 110
  • 380