File indexing completed on 2026-04-09 07:48:55
0001 #pragma once
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138 LEAF_FUNC
0139 void intersect_leaf_thetacut(bool& valid_isect, float4& isect, const quad& q0, const quad& q1, const float t_min, const float3& o, const float3& d)
0140 {
0141 const float& cosTheta0 = q0.f.x ;
0142 const float& sinTheta0 = q0.f.y ;
0143 const float& cosTheta1 = q0.f.z ;
0144 const float& sinTheta1 = q0.f.w ;
0145 const float& tanTheta0sq = q1.f.x ;
0146 const float& tanTheta1sq = q1.f.y ;
0147
0148
0149
0150 const float cosTheta0Sign = cosTheta0 < 0.f ? -1.f : 1.f ;
0151 const float cosTheta1Sign = cosTheta1 < 0.f ? -1.f : 1.f ;
0152
0153
0154 const float dxx_dyy = d.x * d.x + d.y * d.y ;
0155 const float d_xy = sqrt( dxx_dyy );
0156 const float cross0 = cosTheta0*d_xy - sinTheta0*d.z;
0157 const float cross1 = cosTheta1*d_xy - sinTheta1*d.z;
0158 bool unbounded_exit = cross0 > 0.f && cross1 < 0.f ;
0159
0160 #ifdef DEBUG
0161 printf("//intersect_leaf_thetacut q0.f (%10.4f %10.4f %10.4f %10.4f) %s \n" , q0.f.x, q0.f.y, q0.f.z, q0.f.w, "cosTheta0/sinTheta0/cosTheta1/sinTheta1" ) ;
0162 printf("//intersect_leaf_thetacut q1.f (%10.4f %10.4f %10.4f %10.4f)\n" , q1.f.x, q1.f.y, q1.f.z, q1.f.w ) ;
0163 printf("//intersect_leaf_thetacut dxx_dyy %10.4f d_xy %10.4f d.z %10.4f \n", dxx_dyy, d_xy, d.z );
0164 printf("//intersect_leaf_thetacut cross0 %10.4f cosTheta0*d_xy - sinTheta0*d.z \n", cross0 );
0165 printf("//intersect_leaf_thetacut cross1 %10.4f cosTheta1*d_xy - sinTheta1*d.z\n", cross1 );
0166 printf("//intersect_leaf_thetacut unbounded_exit %d \n", unbounded_exit );
0167 #endif
0168
0169
0170
0171 float dd = dxx_dyy - d.z * d.z * tanTheta0sq ;
0172 float od = o.x * d.x + o.y * d.y - o.z * d.z * tanTheta0sq ;
0173 float oo = o.x * o.x + o.y * o.y - o.z * o.z * tanTheta0sq ;
0174 float disc = od * od - oo * dd ;
0175 bool intersects = disc > 0.f;
0176 float discRoot = intersects ? sqrt(disc) : 0.f;
0177
0178 float t_cand = intersects ? (-od + discRoot) / dd : RT_DEFAULT_MAX;
0179 float t0 = intersects ? (-od - discRoot) / dd : RT_DEFAULT_MAX;
0180
0181
0182
0183 if (cosTheta0Sign * (t_cand * d.z + o.z) < 0.f || t_cand <= t_min) t_cand = RT_DEFAULT_MAX;
0184
0185
0186 if (cosTheta0Sign * (t0 * d.z + o.z) > 0.f && t0 > t_min ) t_cand = fminf(t_cand, t0);
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201 dd += d.z * d.z * (tanTheta0sq - tanTheta1sq );
0202 od += o.z * d.z * (tanTheta0sq - tanTheta1sq );
0203 oo += o.z * o.z * (tanTheta0sq - tanTheta1sq );
0204 disc = od * od - oo * dd ;
0205
0206 intersects = disc > 0.f;
0207 discRoot = intersects ? sqrt(disc) : 0.f;
0208
0209 t0 = intersects ? (-od + discRoot) / dd : RT_DEFAULT_MAX;
0210 const float t1 = intersects ? (-od - discRoot) / dd : RT_DEFAULT_MAX;
0211
0212 if (cosTheta1Sign * (t0 * d.z + o.z) > 0.f && t0 > t_min) t_cand = fminf(t_cand, t0);
0213 if (cosTheta1Sign * (t1 * d.z + o.z) > 0.f && t1 > t_min) t_cand = fminf(t_cand, t1);
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231 const float t_plane = -o.z / d.z;
0232 const bool plane = cosTheta0 * cosTheta1 == 0.0 && t_plane > t_min && t_cand > t_plane ;
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243 valid_isect = t_cand < RT_DEFAULT_MAX || plane;
0244
0245 if (valid_isect) {
0246 const bool side = t_cand == t0 || t_cand == t1;
0247
0248
0249
0250 isect.x = plane ? 0.f : (side ? cosTheta1Sign * (o.x + t_cand * d.x) : -cosTheta0Sign * (o.x + t_cand * d.x));
0251 isect.y = plane ? 0.f : (side ? cosTheta1Sign * (o.y + t_cand * d.y) : -cosTheta0Sign * (o.y + t_cand * d.y));
0252 isect.z = plane ? (cosTheta0 == 0.f ? 1.f : -1.f) : (side ? -cosTheta1Sign * (o.z + t_cand * d.z) * tanTheta1sq : cosTheta0Sign * (o.z + t_cand * d.z) * tanTheta0sq );
0253 isect = normalize(isect);
0254
0255
0256 isect.w = plane ? t_plane : t_cand;
0257 }
0258 else if( unbounded_exit )
0259 {
0260 isect.y = -isect.y ;
0261
0262 }
0263
0264 #ifdef DEBUG
0265 printf("//intersect_leaf_thetacut isect (%10.4f %10.4f %10.4f %10.4f) valid_isect %d \n" , isect.x, isect.y, isect.z, isect.w, valid_isect ) ;
0266 #endif
0267 }
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296 LEAF_FUNC
0297 void intersect_leaf_thetacut_lucas(bool& valid_isect, float4& isect, const quad& thetaDat, const float t_min, const float3& rayOrigin, const float3& rayDirection)
0298 {
0299
0300 float dirMag = rayDirection.x * rayDirection.x + rayDirection.y * rayDirection.y - rayDirection.z * rayDirection.z * thetaDat.f.y;
0301 float originDirMag = rayOrigin.x * rayDirection.x + rayOrigin.y * rayDirection.y - rayOrigin.z * rayDirection.z * thetaDat.f.y;
0302 float originMag = rayOrigin.x * rayOrigin.x + rayOrigin.y * rayOrigin.y - rayOrigin.z * rayOrigin.z * thetaDat.f.y;
0303 float disc = originDirMag * originDirMag - originMag * dirMag;
0304
0305 bool intersects = disc > 0.f;
0306 float discRoot = intersects ? sqrt(disc) : 0.f;
0307
0308 float t_cand = intersects ? (-originDirMag + discRoot) / dirMag : RT_DEFAULT_MAX;
0309
0310 if (thetaDat.f.x * (t_cand * rayDirection.z + rayOrigin.z) < 0.f || t_cand <= t_min) t_cand = RT_DEFAULT_MAX;
0311
0312 float t0 = intersects ? (-originDirMag - discRoot) / dirMag : RT_DEFAULT_MAX;
0313 if (thetaDat.f.x * (t0 * rayDirection.z + rayOrigin.z) > 0.f && t0 > t_min) t_cand = fminf(t_cand, t0);
0314
0315
0316 dirMag += rayDirection.z * rayDirection.z * (thetaDat.f.y - thetaDat.f.w);
0317 originDirMag += rayOrigin.z * rayDirection.z * (thetaDat.f.y - thetaDat.f.w);
0318 originMag += rayOrigin.z * rayOrigin.z * (thetaDat.f.y - thetaDat.f.w);
0319 disc = originDirMag * originDirMag - originMag * dirMag;
0320
0321 intersects = disc > 0.f;
0322 discRoot = intersects ? sqrt(disc) : 0.f;
0323
0324 t0 = intersects ? (-originDirMag + discRoot) / dirMag : RT_DEFAULT_MAX;
0325 if (thetaDat.f.z * (t0 * rayDirection.z + rayOrigin.z) > 0.f && t0 > t_min) t_cand = fminf(t_cand, t0);
0326
0327 const float t1 = intersects ? (-originDirMag - discRoot) / dirMag : RT_DEFAULT_MAX;
0328 if (thetaDat.f.z * (t1 * rayDirection.z + rayOrigin.z) > 0.f && t1 > t_min) t_cand = fminf(t_cand, t1);
0329
0330
0331 const float t_plane = -rayOrigin.z / rayDirection.z;
0332 const bool plane = thetaDat.f.x * thetaDat.f.z == 0.0 && t_plane > t_min && t_cand > t_plane;
0333
0334 valid_isect = t_cand < RT_DEFAULT_MAX || plane;
0335
0336 if (valid_isect) {
0337 const bool side = t_cand == t0 || t_cand == t1;
0338
0339 isect.x = plane ? 0.0 : (side ? thetaDat.f.z * (rayOrigin.x + t_cand * rayDirection.x)
0340 : - thetaDat.f.x * (rayOrigin.x + t_cand * rayDirection.x));
0341
0342
0343
0344 isect.y = plane ? 0.0 : (side ? thetaDat.f.z * (rayOrigin.y + t_cand * rayDirection.y)
0345 : - thetaDat.f.x * (rayOrigin.y + t_cand * rayDirection.y));
0346
0347
0348 isect.z = plane ? (thetaDat.f.x == 0.0 ? 1.0 : -1.0)
0349
0350
0351
0352 : ( side ? - thetaDat.f.z * (rayOrigin.z + t_cand * rayDirection.z) * thetaDat.f.w
0353 : thetaDat.f.x * (rayOrigin.z + t_cand * rayDirection.z) * thetaDat.f.y );
0354 isect = normalize(isect);
0355 isect.w = plane ? t_plane : t_cand;
0356 }
0357
0358
0359 #ifdef DEBUG
0360 const quad& q0 = thetaDat ;
0361 printf("//intersect_leaf_thetacut_lucas q0.f (%10.4f %10.4f %10.4f %10.4f) valid_isect %d isect (%10.4f %10.4f %10.4f %10.4f) \n" ,
0362 q0.f.x, q0.f.y, q0.f.z, q0.f.z, valid_isect, isect.x, isect.y, isect.z, isect.w ) ;
0363 #endif
0364
0365 }
0366
0367