Showing results for 
Search instead for 
Do you mean 
Reply

How to check for line intersection?

Is there a way to check that two lines intersect without using their geometry? C++

3 REPLIES

Re: How to check for line intersection?

You can use Open C calls in NXOpen C++, so this looks like it should do what you want:

 

UF_MODL_intersect_curve_to_curve
 

Defined in: uf_modl.h
 
Overview
Determines the intersection between the two input curves, and outputs information describing the intersection.

Re: How to check for line intersection?

 

Here is a simple example, which should appear on the support website tomorrow:

 

#include <stdio.h>
#include <string.h>
#include <uf.h>
#include <uf_obj.h>
#include <uf_modl.h>
#include <uf_part.h>
#include <uf_ui.h>

#include <stdarg.h>

#pragma warning(disable:4996)

static void ECHO(char *format, ...)
{
    char msg[UF_UI_MAX_STRING_LEN+1];
    va_list args;
    va_start(args, format);
    vsprintf(msg, format, args);
    va_end(args);
    UF_UI_open_listing_window();
    UF_UI_write_listing_window(msg);
    UF_print_syslog(msg, FALSE);
}

#define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X)))

static int report_error( char *file, int line, char *call, int irc)
{
    if (irc)
    {
        char err[133];

        UF_get_fail_message(irc, err);
        ECHO("*** ERROR code %d at line %d in %s:\n",
            irc, line, file);
        ECHO("+++ %s\n", err);
        ECHO("%s;\n", call);
    }

    return(irc);
}

static int mask_for_curves_and_edges(UF_UI_selection_p_t select, void *type)
{
    UF_UI_mask_t
        mask[5] = { { UF_line_type, 0, 0 },
                    { UF_circle_type, 0, 0 },
                    { UF_conic_type, 0, 0 },
                    { UF_spline_type, 0, 0 },
                    { UF_solid_type, 0, UF_UI_SEL_FEATURE_ANY_EDGE } };

    if (!UF_CALL(UF_UI_set_sel_mask(select,
            UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC, 5, mask)))
        return (UF_UI_SEL_SUCCESS);
    else
        return (UF_UI_SEL_FAILURE);
}

extern tag_t select_a_curve_or_edge(char *prompt)
{
    int
        resp;
    double
        cp[3];
    tag_t
        object,
        view;

    UF_CALL(UF_UI_select_with_single_dialog("Select a curve or edge", prompt,
        UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY, mask_for_curves_and_edges, NULL,
        &resp, &object, cp, &view));

    if (resp == UF_UI_OBJECT_SELECTED || resp == UF_UI_OBJECT_SELECTED_BY_NAME)
    {
        UF_CALL(UF_DISP_set_highlight(object, 0));
        return object;
    }
    else return NULL_TAG;
}

static void do_it(void)
{
    int
        inx = 0,
        numInts = 0;
    tag_t
        curve_1 = NULL_TAG,
        curve_2 = NULL_TAG;
    double
        *outputData = NULL;
    tag_t
        part = UF_PART_ask_display_part();

    if(NULL_TAG == part) return;
    curve_1 = select_a_curve_or_edge("Select First Curve:");
    curve_2 = select_a_curve_or_edge("Select Second Curve:");

    UF_CALL(UF_MODL_intersect_curve_to_curve(curve_1, curve_2,
                                         &numInts, &outputData));

    ECHO("Number of Intersections: %d\n", numInts);

    for(inx = 0; inx < numInts; inx++)
    {
        ECHO("  Intersection Point %d:  %4.6f  %4.6f  %4.6f\n", inx+1,
            outputData[5*inx], outputData[(5*inx)+1], outputData[(5*inx)+2]);
        ECHO("  Parameter on First Curve: %f   Second Curve: %f\n",
              outputData[(5*inx)+3], outputData[(5*inx)+4]);
    }

    if(numInts > 0) UF_free(outputData);
}

void ufusr(char *param, int *retcode, int paramLen)
{
    if (UF_CALL(UF_initialize())) return;
    do_it();
    UF_terminate();
}

int ufusr_ask_unload(void)
{
    return (UF_UNLOAD_IMMEDIATELY);
}

Re: How to check for line intersection?

Yes, You can use below function for the same

 

int UF_MODL_intersect_curve_to_curve
(

tag_t curve1_id,
tag_t curve2_id,
int* num_intersections,
double* * data

)

 

tag_tcurve1_idInputfirst curve or edge to intersect
tag_tcurve2_idInputsecond curve or edge to intersect
int*num_intersectionsOutputnumber of intersections
double* *dataOutput to UF_*free*It is a one dimensional array whose size is num_intersections 5
data[5i + 0], data[5i + 1] and data[5i + 2] holds the coordinates of the point
data[5i + 3] holds the parameter of first curve or edge
data[5i + 4] holds the parameter of second curve or edge