How to: Find all geometries inside a area using selection

Below is sample code to find all geometry inside a given area/volume using selection.

  1. Covert rectangular region used in Selection_By_Area() into a polyline describing the area box.

  2. Call Compute_Selection_By_Polyline()

Compute_Selection_By_Polyline() will select and return all geometries intersecting this polyline

  1. Call Copute_Selection_By_Area().

Compute_Selection_By_Area() lets you specify a rectangular region of the screen instead of a single point. Every item of geometry within or overlapping that region is identified and returned as part of the selection event.

  1. Using the selection results from Selection_By_Area() and cross-checking the selection results from Selection_By_Polyline, you will find all geometries inside your rectangle region.

Sample code is tested in HOOPS PartViewer

Sample Code:

void CSolidHoopsView::OnExtraSlot5()
{

    m_pHView->SetViewMode(HViewXY);
    m_pHView->Update();

    HC_Open_Segment_By_Key(m_pHView->GetSceneKey()); {

        float cube[24] = {
         -1.0f, -1.0f, -1.0f,
          1.0f, -1.0f, -1.0f,
         -1.0f,  1.0f, -1.0f,
          1.0f,  1.0f, -1.0f,
         -1.0f, -1.0f,  1.0f,
          1.0f, -1.0f,  1.0f,
         -1.0f,  1.0f,  1.0f,
          1.0f,  1.0f,  1.0f };

        int conn[30] = {
         4, 0,1,3,2,
         4, 1,5,7,3,
         4, 5,4,6,7,
         4, 4,0,2,6,
         4, 2,3,7,6,
         4, 4,5,1,0 };

        // insert two cubes

        HC_Open_Segment("enclosed"); {
            HC_Insert_Shell(8, cube, 30, conn);
            HC_Scale_Object(0.2f, 0.2f, 0.2f);
        } HC_Close_Segment();

        HC_Open_Segment("intersecting"); {
            HC_Insert_Shell(8, cube, 30, conn);
            HC_Scale_Object(0.2f, 0.2f, 0.2f);
            HC_Translate_Object(1.0, 0, 0);
        } HC_Close_Segment();


        // insert markers to visually see selection points
        // geometry is not selectable

        HC_Open_Segment_By_Key(m_pHView->GetModelKey()); {
            HC_Open_Segment("selection_markers"); {
                HC_Set_Visibility("markers=on");
                HC_Set_Marker_Symbol("x");
                HC_Set_Selectability("off");
                HC_Insert_Marker(cube[0], cube[1], cube[2]);
                HC_Insert_Marker(cube[3], cube[4], cube[6]);
                HC_Insert_Marker(cube[6], cube[7], cube[8]);
                HC_Insert_Marker(cube[9], cube[10], cube[11]);
                HC_Set_Rendering_Options("depth range=(0,0.1)");
            } HC_Close_Segment();
        } HC_Close_Segment();

        m_pHView->FitWorld();
        m_pHView->Update();

        // Convert markers points (world) to window coordinates

        HPoint topLeft, topRight, bottomLeft, bottomRight;
        HC_KEY key_path[3] = { m_pHView->GetModelKey(), m_pHView->GetIncludeLinkKey(), m_pHView->GetViewKey() };

        HC_Compute_Coordinates_By_Path(3, key_path, "world", &cube[0], "local window", &bottomLeft);
        HC_Compute_Coordinates_By_Path(3, key_path, "world", &cube[3], "local window", &bottomRight);
        HC_Compute_Coordinates_By_Path(3, key_path, "world", &cube[6], "local window", &topLeft);
        HC_Compute_Coordinates_By_Path(3, key_path, "world", &cube[9], "local window", &topRight);


        int count_selection_by_line;
        HPoint points[5] = { topLeft, bottomLeft, bottomRight, topRight, topLeft };

        // Select only intersecting cube

        HC_Open_Segment_By_Key(m_pHView->GetViewKey()); {
            count_selection_by_line = HC_Compute_Selection_By_Polyline(".", "./scene", "v, selection level = entity,related selection limit =  5", 5, points);
        } HC_Close_Segment();

        // Remove this key from the selection list when calling Selection_By_Area
        HC_KEY testKey = INVALID_KEY;

        if (count_selection_by_line)
        {
            do {
                int count;
                HC_Show_Selection_Keys_Count(&count);
                HC_KEY* selection_path = new HC_KEY[count];
                HC_Show_Selection_Keys(&count, selection_path);

                
                testKey = selection_path[0];
            } while (HC_Find_Related_Selection());

        }

        m_pHView->Update();

        // Area selection to select all geometries (overlapping and within)

        int count_selection_by_area;
        HC_Open_Segment_By_Key(m_pHView->GetViewKey()); {
            count_selection_by_area = HC_Compute_Selection_By_Area(".", "./scene", "v, selection level = entity,related selection limit =  5", topLeft.x, bottomRight.x, bottomLeft.y, topRight.y);
        } HC_Close_Segment();

        if (count_selection_by_area)
        {

            do {

                HC_KEY key;
                int o1, o2;

                HC_Show_Selection_Item(&key, &o1, &o2);

                // test to see if geometry is inside rectangle region 
                if (key != testKey)
                {
                    // selected geometry is inside rectangle region 


                    m_pHView->Update();

                }

                
            } while (HC_Find_Related_Selection());
        
        }


        m_pHView->Update();


    }
}