Who's Online:

  • [Bot]
Now online:
  • 3 guests
  • 1 robot

 

DBPRO CODE : LINE SEGMENT INTERSECT PLANE

 

LineSegmentIntersectPlane(param_Vector3SP0,param_Vector3SP1,param_Vector3PlanePoint,param_Vector3PlaneNormal,param_Vector3IntersectResult)

This function tests against an infinite plane and a line.
Using DPro's 3DMath, Vector3SP0 and Vector3SP1 are endpoints of a 3d line. The Vector3PlanePoint is the point in 3d space where the infinite plane resides. Vector3PlaneNormal is the front facing of the infinite plane. If successful, the Vector3IntersectResult Vector will be filled with the intersection point of where the line crossed the infinite plane at.

When running the example code below, move the mouse pointer over the red box object. PICK OBJECT will detect the mouse pointing at it and then if the LineSegmentIntersectPlane function succeeds, a yellow sphere will appear at the point where the mouse pointer to the distance of the line intersects the infinite plane at. The 3D line is determined from the PICK SCREEN command.

The function will return a 1 if successful.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
` Line Segment Intersect Plane Function
` Ported to DBPro - BY: Todd Riggins - ExoDev.Com - Nov 11,2010
` Original C Code? intersect3D_SegmentPlane() : http://www.softsurfer.com/Archive/algorithm_0104/algorithm_0104B.htm#intersect3D_SegPlane()
` * May not be where I got it from, It's the same function ... but it's old code.
 
 
 
GLOBAL iret as INTEGER
GLOBAL LSIPVectorResult1 as INTEGER = 1
GLOBAL LSIPVectorResult2 as INTEGER = 2
GLOBAL Vector3_Point1 as INTEGER = 3
GLOBAL Vector3_Point2 as INTEGER = 4
GLOBAL Vector3_PlanePosition as INTEGER = 5
GLOBAL Vector3_PlaneFaceNormal as INTEGER = 6
GLOBAL Vector3_IntersectResult as INTEGER = 7
 
iret=MAKE VECTOR3(LSIPVectorResult1)
iret=MAKE VECTOR3(LSIPVectorResult2)                    
iret=MAKE VECTOR3(Vector3_Point1)
iret=MAKE VECTOR3(Vector3_Point2)
iret=MAKE VECTOR3(Vector3_PlanePosition)
iret=MAKE VECTOR3(Vector3_PlaneFaceNormal)
iret=MAKE VECTOR3(Vector3_IntersectResult)
 
 
 
InitDisplay()
Main()
 
 
iret=DELETE VECTOR3(LSIPVectorResult1)
iret=DELETE VECTOR3(LSIPVectorResult2)  
iret=DELETE VECTOR3(Vector3_Point1)
iret=DELETE VECTOR3(Vector3_Point2)
iret=DELETE VECTOR3(Vector3_PlanePosition)
iret=DELETE VECTOR3(Vector3_PlaneFaceNormal)
iret=DELETE VECTOR3(Vector3_IntersectResult)
 
 
End
 
 
 
`       intersect3D_SegmentPlane(): intersect a segment and a plane
`               Input:  S = a segment, and Pn = a plane = {Point V0; Vector n;}
`               Output: *I0 = the intersect point (when it exists)
`               Return: 0 = disjoint (no intersection)
`                               1 = intersection in the unique point *I0
`                               2 = the segment lies in the plane
`               Segment S, Plane Pn, Point* I
Function LineSegmentIntersectPlane(param_Vector3SP0,param_Vector3SP1,param_Vector3PlanePoint,param_Vector3PlaneNormal,param_Vector3IntersectResult)
        LOCAL local_D as FLOAT
        LOCAL local_N as FLOAT
        LOCAL local_sI as FLOAT
        LOCAL CONST_SMALL_NUM as FLOAT
        CONST_SMALL_NUM = 0.00001
       
        Subtract Vector3 LSIPVectorResult1, param_Vector3SP1, param_Vector3SP0
        Subtract Vector3 LSIPVectorResult2, param_Vector3SP0, param_Vector3PlanePoint
       
        local_D=Dot Product Vector3( param_Vector3PlaneNormal, LSIPVectorResult1 )
        local_N=-Dot Product Vector3( param_Vector3PlaneNormal, LSIPVectorResult2 )
       
        if (abs(local_D) < CONST_SMALL_NUM)      `segment is parallel to plane
                if (abs(local_N) < CONST_SMALL_NUM)  `segment lies in plane
                        exitfunction 2
                else
                        exitfunction 0                   `no intersection
                endif
        endif
       
        ` they are not parallel
        ` compute intersect param
        local_sI = local_N / local_D
        if (local_sI < 0.0) OR (local_sI > 1.0)
                exitfunction 0                     `no intersection
        endif
 
        ` compute segment intersect point
        Set Vector3 param_Vector3IntersectResult,_
                X Vector3(param_Vector3SP0) + (local_sI * X Vector3(LSIPVectorResult1)),_
                Y Vector3(param_Vector3SP0) + (local_sI * Y Vector3(LSIPVectorResult1)),_
                Z Vector3(param_Vector3SP0) + (local_sI * Z Vector3(LSIPVectorResult1))
 
EndFunction 1
 
 
Function InitDisplay()
        sync on
        sync rate 60
 
        Set Window Position 0,0
        Set Window Layout 0,0,0
        Maximize Window
 
        set display mode DESKTOP WIDTH(),DESKTOP HEIGHT(),0, 0  
        set window off
                               
        make camera 1
        position camera 1,0,15,-400
        color backdrop 1, rgb(32,64,64)
        set camera fov 1,45
       
 
       
EndFunction
 
 
Function Main()
 
 
 
        testbox=1
        MAKE OBJECT BOX testbox,500,1,500
        POSITION OBJECT testbox,0,0,0
        SET OBJECT DIFFUSE testbox, rgb(255,0,0)
       
        sphere=2
        MAKE OBJECT SPHERE sphere,10
        SET OBJECT DIFFUSE sphere, rgb(255,255,0)
 
 
        repeat
       
 
                if pick object(mousex(),mousey(),testbox,testbox)=testbox
                        PICK SCREEN mousex(),mousey(),1000
                        pickx=camera position x(1) +GET PICK VECTOR X()
                        picky=camera position y(1) +GET PICK VECTOR Y()
                        pickz=camera position z(1) +GET PICK VECTOR Z()
 
                        set vector3 Vector3_Point1,camera position x(1),camera position y(1),camera position z(1)
                        set vector3 Vector3_Point2,pickx,picky,pickz
                       
                        ` Check against infinite plane at box position
                        set vector3 Vector3_PlanePosition,0,object position y(testbox),0
                        set vector3 Vector3_PlaneFaceNormal,0,1,0 ` Up the z axis
                        iret=LineSegmentIntersectPlane( Vector3_Point1, Vector3_Point2, Vector3_PlanePosition, Vector3_PlaneFaceNormal, Vector3_IntersectResult)
                        if iret=1
                                show object sphere
                                position object sphere,x vector3(Vector3_IntersectResult),y vector3(Vector3_IntersectResult),z vector3(Vector3_IntersectResult)
                                text 10,130,"Line Intersecting Plane"
                        endif
                        text 10,110,"Picked Red Box Object"
                else
                        hide object sphere
                endif
 
       
       
        text 10,30,"Move mouse over red box object."    
        text 10,50,"A yellow sphere should appear on the"
        text 10,70,"infinite plane that the red box is lying on."
        sync
        until escapekey()=1    
       
 
EndFunction