@@ -87,60 +87,86 @@ public static bool Intersects(ref Ray3d ray, ref AxisAlignedBox3d box, double ex
87
87
Vector3d AWdU = Vector3d . Zero ;
88
88
Vector3d DdU = Vector3d . Zero ;
89
89
Vector3d ADdU = Vector3d . Zero ;
90
- Vector3d AWxDdU = Vector3d . Zero ;
91
90
double RHS ;
92
91
93
92
Vector3d diff = ray . Origin - box . Center ;
94
93
Vector3d extent = box . Extents + expandExtents ;
95
94
96
- WdU [ 0 ] = ray . Direction . x ; // ray.Direction.Dot(Vector3d.AxisX);
97
- AWdU [ 0 ] = Math . Abs ( WdU [ 0 ] ) ;
98
- DdU [ 0 ] = diff . x ; // diff.Dot(Vector3d.AxisX);
99
- ADdU [ 0 ] = Math . Abs ( DdU [ 0 ] ) ;
100
- if ( ADdU [ 0 ] > extent . x && DdU [ 0 ] * WdU [ 0 ] >= ( double ) 0 ) {
95
+ WdU . x = ray . Direction . x ; // ray.Direction.Dot(Vector3d.AxisX);
96
+ AWdU . x = Math . Abs ( WdU . x ) ;
97
+ DdU . x = diff . x ; // diff.Dot(Vector3d.AxisX);
98
+ ADdU . x = Math . Abs ( DdU . x ) ;
99
+ if ( ADdU . x > extent . x && DdU . x * WdU . x >= ( double ) 0 ) {
101
100
return false ;
102
101
}
103
102
104
- WdU [ 1 ] = ray . Direction . y ; // ray.Direction.Dot(Vector3d.AxisY);
105
- AWdU [ 1 ] = Math . Abs ( WdU [ 1 ] ) ;
106
- DdU [ 1 ] = diff . y ; // diff.Dot(Vector3d.AxisY);
107
- ADdU [ 1 ] = Math . Abs ( DdU [ 1 ] ) ;
108
- if ( ADdU [ 1 ] > extent . y && DdU [ 1 ] * WdU [ 1 ] >= ( double ) 0 ) {
103
+ WdU . y = ray . Direction . y ; // ray.Direction.Dot(Vector3d.AxisY);
104
+ AWdU . y = Math . Abs ( WdU . y ) ;
105
+ DdU . y = diff . y ; // diff.Dot(Vector3d.AxisY);
106
+ ADdU . y = Math . Abs ( DdU . y ) ;
107
+ if ( ADdU . y > extent . y && DdU . y * WdU . y >= ( double ) 0 ) {
109
108
return false ;
110
109
}
111
110
112
- WdU [ 2 ] = ray . Direction . z ; // ray.Direction.Dot(Vector3d.AxisZ);
113
- AWdU [ 2 ] = Math . Abs ( WdU [ 2 ] ) ;
114
- DdU [ 2 ] = diff . z ; // diff.Dot(Vector3d.AxisZ);
115
- ADdU [ 2 ] = Math . Abs ( DdU [ 2 ] ) ;
116
- if ( ADdU [ 2 ] > extent . z && DdU [ 2 ] * WdU [ 2 ] >= ( double ) 0 ) {
111
+ WdU . z = ray . Direction . z ; // ray.Direction.Dot(Vector3d.AxisZ);
112
+ AWdU . z = Math . Abs ( WdU . z ) ;
113
+ DdU . z = diff . z ; // diff.Dot(Vector3d.AxisZ);
114
+ ADdU . z = Math . Abs ( DdU . z ) ;
115
+ if ( ADdU . z > extent . z && DdU . z * WdU . z >= ( double ) 0 ) {
117
116
return false ;
118
117
}
119
118
120
119
Vector3d WxD = ray . Direction . Cross ( diff ) ;
120
+ Vector3d AWxDdU = Vector3d . Zero ;
121
121
122
- AWxDdU [ 0 ] = Math . Abs ( WxD . x ) ; // Math.Abs(WxD.Dot(Vector3d.AxisX));
123
- RHS = extent . y * AWdU [ 2 ] + extent . z * AWdU [ 1 ] ;
124
- if ( AWxDdU [ 0 ] > RHS ) {
122
+ AWxDdU . x = Math . Abs ( WxD . x ) ; // Math.Abs(WxD.Dot(Vector3d.AxisX));
123
+ RHS = extent . y * AWdU . z + extent . z * AWdU . y ;
124
+ if ( AWxDdU . x > RHS ) {
125
125
return false ;
126
126
}
127
127
128
- AWxDdU [ 1 ] = Math . Abs ( WxD . y ) ; // Math.Abs(WxD.Dot(Vector3d.AxisY));
129
- RHS = extent . x * AWdU [ 2 ] + extent . z * AWdU [ 0 ] ;
130
- if ( AWxDdU [ 1 ] > RHS ) {
128
+ AWxDdU . y = Math . Abs ( WxD . y ) ; // Math.Abs(WxD.Dot(Vector3d.AxisY));
129
+ RHS = extent . x * AWdU . z + extent . z * AWdU . x ;
130
+ if ( AWxDdU . y > RHS ) {
131
131
return false ;
132
132
}
133
133
134
- AWxDdU [ 2 ] = Math . Abs ( WxD . z ) ; // Math.Abs(WxD.Dot(Vector3d.AxisZ));
135
- RHS = extent . x * AWdU [ 1 ] + extent . y * AWdU [ 0 ] ;
136
- if ( AWxDdU [ 2 ] > RHS ) {
134
+ AWxDdU . z = Math . Abs ( WxD . z ) ; // Math.Abs(WxD.Dot(Vector3d.AxisZ));
135
+ RHS = extent . x * AWdU . y + extent . y * AWdU . x ;
136
+ if ( AWxDdU . z > RHS ) {
137
137
return false ;
138
138
}
139
139
140
140
return true ;
141
141
}
142
142
143
143
144
+ /// <summary>
145
+ /// Find intersection of ray with AABB, without having to construct any new classes.
146
+ /// Returns ray T-value of first intersection (or double.MaxVlaue on miss)
147
+ /// </summary>
148
+ public static bool FindRayIntersectT ( ref Ray3d ray , ref AxisAlignedBox3d box , out double RayParam )
149
+ {
150
+ double RayParam0 = 0.0 ;
151
+ double RayParam1 = double . MaxValue ;
152
+ int Quantity = 0 ;
153
+ Vector3d Point0 = Vector3d . Zero ;
154
+ Vector3d Point1 = Vector3d . Zero ;
155
+ IntersectionType Type = IntersectionType . Empty ;
156
+ IntrLine3AxisAlignedBox3 . DoClipping ( ref RayParam0 , ref RayParam1 , ref ray . Origin , ref ray . Direction , ref box ,
157
+ true , ref Quantity , ref Point0 , ref Point1 , ref Type ) ;
158
+
159
+ if ( Type != IntersectionType . Empty ) {
160
+ RayParam = RayParam0 ;
161
+ return true ;
162
+ } else {
163
+ RayParam = double . MaxValue ;
164
+ return false ;
165
+ }
166
+ }
167
+
168
+
169
+
144
170
145
171
}
146
172
}
0 commit comments