00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 #ifndef lint
00188 static const char RCSpart[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/librt/g_part.c,v 14.13 2006/09/16 02:04:24 lbutler Exp $ (BRL)";
00189 #endif
00190
00191 #include "common.h"
00192
00193 #include <stddef.h>
00194 #include <stdio.h>
00195 #ifdef HAVE_STRING_H
00196 # include <string.h>
00197 #else
00198 # include <strings.h>
00199 #endif
00200 #include <math.h>
00201
00202 #include "machine.h"
00203 #include "vmath.h"
00204 #include "db.h"
00205 #include "raytrace.h"
00206 #include "nmg.h"
00207 #include "rtgeom.h"
00208 #include "./debug.h"
00209
00210 struct part_specific {
00211 struct rt_part_internal part_int;
00212 mat_t part_SoR;
00213 mat_t part_invRoS;
00214 fastf_t part_vrad_prime;
00215 fastf_t part_hrad_prime;
00216
00217 fastf_t part_v_hdist;
00218 fastf_t part_h_hdist;
00219 fastf_t part_v_erad;
00220 fastf_t part_h_erad;
00221 };
00222
00223
00224 #define RT_PARTICLE_SURF_VSPHERE 1
00225 #define RT_PARTICLE_SURF_BODY 2
00226 #define RT_PARTICLE_SURF_HSPHERE 3
00227
00228 const struct bu_structparse rt_part_parse[] = {
00229 { "%f", 3, "V", bu_offsetof(struct rt_part_internal, part_V[X]), BU_STRUCTPARSE_FUNC_NULL },
00230 { "%f", 3, "H", bu_offsetof(struct rt_part_internal, part_H[X]), BU_STRUCTPARSE_FUNC_NULL },
00231 { "%f", 1, "r_v",bu_offsetof(struct rt_part_internal, part_vrad), BU_STRUCTPARSE_FUNC_NULL },
00232 { "%f", 1, "r_h",bu_offsetof(struct rt_part_internal, part_hrad), BU_STRUCTPARSE_FUNC_NULL },
00233 { {'\0','\0','\0','\0'}, 0, (char *)NULL, 0, BU_STRUCTPARSE_FUNC_NULL }
00234 };
00235
00236 BU_EXTERN( void rt_part_ifree, (struct rt_db_internal *ip) );
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253 int
00254 rt_part_prep(struct soltab *stp, struct rt_db_internal *ip, struct rt_i *rtip)
00255 {
00256 register struct part_specific *part;
00257 struct rt_part_internal *pip;
00258 vect_t Hunit;
00259 vect_t a, b;
00260 mat_t R, Rinv;
00261 mat_t S;
00262 vect_t max, min;
00263 vect_t tip;
00264 fastf_t inv_hlen;
00265 fastf_t hlen;
00266 fastf_t hlen_sq;
00267 fastf_t r_diff;
00268 fastf_t r_diff_sq;
00269 fastf_t sin_theta;
00270 fastf_t cos_theta;
00271
00272 RT_CK_DB_INTERNAL( ip );
00273 pip = (struct rt_part_internal *)ip->idb_ptr;
00274 RT_PART_CK_MAGIC(pip);
00275
00276 BU_GETSTRUCT( part, part_specific );
00277 stp->st_specific = (genptr_t)part;
00278 part->part_int = *pip;
00279 pip = &part->part_int;
00280
00281 if( pip->part_type == RT_PARTICLE_TYPE_SPHERE ) {
00282
00283
00284 VMOVE( stp->st_center, pip->part_V );
00285 stp->st_aradius = stp->st_bradius = pip->part_vrad;
00286 stp->st_min[X] = pip->part_V[X] - pip->part_vrad;
00287 stp->st_max[X] = pip->part_V[X] + pip->part_vrad;
00288 stp->st_min[Y] = pip->part_V[Y] - pip->part_vrad;
00289 stp->st_max[Y] = pip->part_V[Y] + pip->part_vrad;
00290 stp->st_min[Z] = pip->part_V[Z] - pip->part_vrad;
00291 stp->st_max[Z] = pip->part_V[Z] + pip->part_vrad;
00292 return(0);
00293 }
00294
00295
00296 hlen_sq = MAGSQ(pip->part_H );
00297 if( hlen_sq < SMALL ) {
00298 bu_log("part(%s): 0-length H vector\n", stp->st_dp->d_namep);
00299 return 1;
00300 }
00301 hlen = sqrt(hlen_sq);
00302 inv_hlen = 1/hlen;
00303 VSCALE( Hunit, pip->part_H, inv_hlen );
00304 bn_vec_ortho( a, Hunit );
00305 VCROSS( b, Hunit, a );
00306
00307
00308
00309
00310
00311
00312 if( pip->part_vrad >= pip->part_hrad ) {
00313
00314 r_diff = (pip->part_vrad - pip->part_hrad) * inv_hlen;
00315 r_diff_sq = r_diff * r_diff;
00316 if( r_diff_sq > 1 ) {
00317
00318 sin_theta = 1;
00319 cos_theta = 0;
00320 } else {
00321 sin_theta = sqrt( 1 - r_diff_sq );
00322 cos_theta = fabs( r_diff );
00323 }
00324
00325 part->part_v_erad = pip->part_vrad / sin_theta;
00326 part->part_h_erad = pip->part_hrad / sin_theta;
00327
00328
00329 part->part_v_hdist = cos_theta * pip->part_vrad * inv_hlen;
00330 part->part_h_hdist = 1 + cos_theta * pip->part_hrad * inv_hlen;
00331 } else {
00332
00333 r_diff = (pip->part_hrad - pip->part_vrad) * inv_hlen;
00334 r_diff_sq = r_diff * r_diff;
00335 if( r_diff_sq > 1 ) {
00336
00337 sin_theta = 1;
00338 cos_theta = 0;
00339 } else {
00340 sin_theta = sqrt( 1 - r_diff_sq );
00341 cos_theta = fabs( r_diff );
00342 }
00343
00344 part->part_v_erad = pip->part_vrad / sin_theta;
00345 part->part_h_erad = pip->part_hrad / sin_theta;
00346
00347
00348 part->part_v_hdist = -cos_theta * pip->part_vrad * inv_hlen;
00349 part->part_h_hdist = 1 - cos_theta * pip->part_hrad * inv_hlen;
00350 }
00351
00352
00353
00354 part->part_vrad_prime = 1;
00355 part->part_hrad_prime = part->part_h_erad / part->part_v_erad;
00356
00357
00358 MAT_IDN( R );
00359 VMOVE( &R[0], a );
00360 VMOVE( &R[4], b );
00361 VMOVE( &R[8], Hunit );
00362 bn_mat_trn( Rinv, R );
00363
00364
00365 MAT_IDN( S );
00366 S[ 0] = 1.0 / part->part_v_erad;
00367 S[ 5] = S[0];
00368 S[10] = inv_hlen;
00369
00370 bn_mat_mul( part->part_SoR, S, R );
00371 bn_mat_mul( part->part_invRoS, Rinv, S );
00372
00373
00374 VJOIN1( stp->st_center, pip->part_V, 0.5, pip->part_H );
00375
00376 stp->st_aradius = stp->st_bradius = pip->part_vrad;
00377
00378 stp->st_min[X] = pip->part_V[X] - pip->part_vrad;
00379 stp->st_max[X] = pip->part_V[X] + pip->part_vrad;
00380 stp->st_min[Y] = pip->part_V[Y] - pip->part_vrad;
00381 stp->st_max[Y] = pip->part_V[Y] + pip->part_vrad;
00382 stp->st_min[Z] = pip->part_V[Z] - pip->part_vrad;
00383 stp->st_max[Z] = pip->part_V[Z] + pip->part_vrad;
00384
00385 VADD2( tip, pip->part_V, pip->part_H );
00386 min[X] = tip[X] - pip->part_hrad;
00387 max[X] = tip[X] + pip->part_hrad;
00388 min[Y] = tip[Y] - pip->part_hrad;
00389 max[Y] = tip[Y] + pip->part_hrad;
00390 min[Z] = tip[Z] - pip->part_hrad;
00391 max[Z] = tip[Z] + pip->part_hrad;
00392 VMINMAX( stp->st_min, stp->st_max, min );
00393 VMINMAX( stp->st_min, stp->st_max, max );
00394
00395
00396 {
00397 register fastf_t f;
00398 vect_t work;
00399 VSUB2SCALE( work, stp->st_max, stp->st_min, 0.5 );
00400 f = work[X];
00401 if( work[Y] > f ) f = work[Y];
00402 if( work[Z] > f ) f = work[Z];
00403 stp->st_aradius = f;
00404 stp->st_bradius = MAGNITUDE(work);
00405 }
00406 return(0);
00407 }
00408
00409
00410
00411
00412 void
00413 rt_part_print(register const struct soltab *stp)
00414 {
00415 register const struct part_specific *part =
00416 (struct part_specific *)stp->st_specific;
00417
00418 VPRINT("part_V", part->part_int.part_V );
00419 VPRINT("part_H", part->part_int.part_H );
00420 bu_log("part_vrad=%g\n", part->part_int.part_vrad );
00421 bu_log("part_hrad=%g\n", part->part_int.part_hrad );
00422
00423 switch( part->part_int.part_type ) {
00424 case RT_PARTICLE_TYPE_SPHERE:
00425 bu_log("part_type = SPHERE\n");
00426 break;
00427 case RT_PARTICLE_TYPE_CYLINDER:
00428 bu_log("part_type = CYLINDER\n");
00429 bn_mat_print("part_SoR", part->part_SoR );
00430 bn_mat_print("part_invRoS", part->part_invRoS );
00431 break;
00432 case RT_PARTICLE_TYPE_CONE:
00433 bu_log("part_type = CONE\n");
00434 bn_mat_print("part_SoR", part->part_SoR );
00435 bn_mat_print("part_invRoS", part->part_invRoS );
00436 break;
00437 default:
00438 bu_log("part_type = %d ???\n", part->part_int.part_type );
00439 break;
00440 }
00441 }
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454 int
00455 rt_part_shot(struct soltab *stp, register struct xray *rp, struct application *ap, struct seg *seghead)
00456 {
00457 register struct part_specific *part =
00458 (struct part_specific *)stp->st_specific;
00459 register struct seg *segp;
00460 LOCAL vect_t dprime;
00461 LOCAL point_t pprime;
00462 LOCAL point_t xlated;
00463 LOCAL fastf_t t1, t2;
00464 LOCAL fastf_t f;
00465 LOCAL struct hit hits[4];
00466 register struct hit *hitp = &hits[0];
00467 int check_v, check_h;
00468
00469 if( part->part_int.part_type == RT_PARTICLE_TYPE_SPHERE ) {
00470 LOCAL vect_t ov;
00471 FAST fastf_t vrad_sq;
00472 FAST fastf_t magsq_ov;
00473 FAST fastf_t b;
00474 FAST fastf_t root;
00475
00476 VSUB2( ov, part->part_int.part_V, rp->r_pt );
00477 b = VDOT( rp->r_dir, ov );
00478 magsq_ov = MAGSQ(ov);
00479
00480 if( magsq_ov >= (vrad_sq = part->part_int.part_vrad *
00481 part->part_int.part_vrad) ) {
00482
00483 if( b < 0 ) {
00484
00485 return(0);
00486 }
00487 root = b*b - magsq_ov + vrad_sq;
00488 if( root <= 0 ) {
00489
00490 return(0);
00491 }
00492 } else {
00493 root = b*b - magsq_ov + vrad_sq;
00494 }
00495 root = sqrt(root);
00496
00497 RT_GET_SEG(segp, ap->a_resource);
00498 segp->seg_stp = stp;
00499
00500
00501 segp->seg_in.hit_magic = RT_HIT_MAGIC;
00502 segp->seg_in.hit_dist = b - root;
00503 segp->seg_in.hit_surfno = RT_PARTICLE_SURF_VSPHERE;
00504 segp->seg_out.hit_magic = RT_HIT_MAGIC;
00505 segp->seg_out.hit_dist = b + root;
00506 segp->seg_out.hit_surfno = RT_PARTICLE_SURF_VSPHERE;
00507 BU_LIST_INSERT( &(seghead->l), &(segp->l) );
00508 return(2);
00509 }
00510
00511
00512 MAT4X3VEC( dprime, part->part_SoR, rp->r_dir );
00513 VSUB2( xlated, rp->r_pt, part->part_int.part_V );
00514 MAT4X3VEC( pprime, part->part_SoR, xlated );
00515
00516 if( NEAR_ZERO(dprime[X], SMALL) && NEAR_ZERO(dprime[Y], SMALL) ) {
00517 check_v = check_h = 1;
00518 goto check_hemispheres;
00519 }
00520 check_v = check_h = 0;
00521
00522
00523
00524 if( part->part_int.part_type == RT_PARTICLE_TYPE_CYLINDER ) {
00525
00526 FAST fastf_t a, b, c;
00527 FAST fastf_t root;
00528
00529 a = dprime[X]*dprime[X] + dprime[Y]*dprime[Y];
00530 b = dprime[X]*pprime[X] + dprime[Y]*pprime[Y];
00531 c = pprime[X]*pprime[X] + pprime[Y]*pprime[Y] - 1;
00532 if( (root = b*b - a * c) <= 0 )
00533 goto check_hemispheres;
00534 root = sqrt(root);
00535 t1 = (root-b) / a;
00536 t2 = -(root+b) / a;
00537 } else {
00538
00539 FAST fastf_t a, b, c;
00540 FAST fastf_t root;
00541 FAST fastf_t m, msq;
00542
00543 m = part->part_hrad_prime - part->part_vrad_prime;
00544
00545
00546
00547
00548 a = dprime[X]*dprime[X] + dprime[Y]*dprime[Y] -
00549 (msq = m*m) * dprime[Z]*dprime[Z];
00550 b = dprime[X]*pprime[X] + dprime[Y]*pprime[Y] -
00551 msq * dprime[Z]*pprime[Z] -
00552 m * dprime[Z];
00553 c = pprime[X]*pprime[X] + pprime[Y]*pprime[Y] -
00554 msq * pprime[Z]*pprime[Z] -
00555 2 * m * pprime[Z] - 1;
00556
00557
00558 if( (root = b*b - a * c) <= 0 )
00559 goto check_hemispheres;
00560 root = sqrt(root);
00561
00562 t1 = (root-b) / a;
00563 t2 = -(root+b) / a;
00564 }
00565
00566
00567
00568
00569
00570 if( (f = pprime[Z] + t1 * dprime[Z]) >= part->part_v_hdist ) {
00571 check_h = 1;
00572 if( f <= part->part_h_hdist ) {
00573 hitp->hit_magic = RT_HIT_MAGIC;
00574
00575 hitp->hit_vpriv[X] = pprime[X] + t1 * dprime[X];
00576 hitp->hit_vpriv[Y] = pprime[Y] + t1 * dprime[Y];
00577 hitp->hit_vpriv[Z] = f;
00578 hitp->hit_dist = t1;
00579 hitp->hit_surfno = RT_PARTICLE_SURF_BODY;
00580 hitp++;
00581 }
00582 } else {
00583 check_v = 1;
00584 }
00585
00586 if( (f = pprime[Z] + t2 * dprime[Z]) >= part->part_v_hdist ) {
00587 check_h = 1;
00588 if( f <= part->part_h_hdist ) {
00589 hitp->hit_magic = RT_HIT_MAGIC;
00590
00591 hitp->hit_vpriv[X] = pprime[X] + t2 * dprime[X];
00592 hitp->hit_vpriv[Y] = pprime[Y] + t2 * dprime[Y];
00593 hitp->hit_vpriv[Z] = f;
00594 hitp->hit_dist = t2;
00595 hitp->hit_surfno = RT_PARTICLE_SURF_BODY;
00596 hitp++;
00597 }
00598 } else {
00599 check_v = 1;
00600 }
00601
00602
00603
00604
00605 check_hemispheres:
00606 if( check_v ) {
00607 LOCAL vect_t ov;
00608 FAST fastf_t rad_sq;
00609 FAST fastf_t magsq_ov;
00610 FAST fastf_t b;
00611 FAST fastf_t root;
00612
00613
00614
00615
00616 VSUB2( ov, part->part_int.part_V, rp->r_pt );
00617 b = VDOT( rp->r_dir, ov );
00618 magsq_ov = MAGSQ(ov);
00619 if( magsq_ov >= (rad_sq = part->part_int.part_vrad *
00620 part->part_int.part_vrad) ) {
00621
00622 if( b < 0 ) {
00623
00624 goto do_check_h;
00625 }
00626 root = b*b - magsq_ov + rad_sq;
00627 if( root <= 0 ) {
00628
00629 goto do_check_h;
00630 }
00631 } else {
00632 root = b*b - magsq_ov + rad_sq;
00633 }
00634 root = sqrt(root);
00635 t1 = b - root;
00636
00637 if( pprime[Z] + t1 * dprime[Z] <= part->part_v_hdist ) {
00638 hitp->hit_magic = RT_HIT_MAGIC;
00639 hitp->hit_dist = t1;
00640 hitp->hit_surfno = RT_PARTICLE_SURF_VSPHERE;
00641 hitp++;
00642 }
00643 t2 = b + root;
00644 if( pprime[Z] + t2 * dprime[Z] <= part->part_v_hdist ) {
00645 hitp->hit_magic = RT_HIT_MAGIC;
00646 hitp->hit_dist = t2;
00647 hitp->hit_surfno = RT_PARTICLE_SURF_VSPHERE;
00648 hitp++;
00649 }
00650 }
00651
00652 do_check_h:
00653 if( check_h ) {
00654 LOCAL vect_t ov;
00655 FAST fastf_t rad_sq;
00656 FAST fastf_t magsq_ov;
00657 FAST fastf_t b;
00658 FAST fastf_t root;
00659
00660
00661
00662
00663 VADD2( ov, part->part_int.part_V, part->part_int.part_H );
00664 VSUB2( ov, ov, rp->r_pt );
00665 b = VDOT( rp->r_dir, ov );
00666 magsq_ov = MAGSQ(ov);
00667 if( magsq_ov >= (rad_sq = part->part_int.part_hrad *
00668 part->part_int.part_hrad) ) {
00669
00670 if( b < 0 ) {
00671
00672 goto out;
00673 }
00674 root = b*b - magsq_ov + rad_sq;
00675 if( root <= 0 ) {
00676
00677 goto out;
00678 }
00679 } else {
00680 root = b*b - magsq_ov + rad_sq;
00681 }
00682 root = sqrt(root);
00683 t1 = b - root;
00684
00685 if( pprime[Z] + t1 * dprime[Z] >= part->part_h_hdist ) {
00686 hitp->hit_magic = RT_HIT_MAGIC;
00687 hitp->hit_dist = t1;
00688 hitp->hit_surfno = RT_PARTICLE_SURF_HSPHERE;
00689 hitp++;
00690 }
00691 t2 = b + root;
00692 if( pprime[Z] + t2 * dprime[Z] >= part->part_h_hdist ) {
00693 hitp->hit_magic = RT_HIT_MAGIC;
00694 hitp->hit_dist = t2;
00695 hitp->hit_surfno = RT_PARTICLE_SURF_HSPHERE;
00696 hitp++;
00697 }
00698 }
00699 out:
00700 if( hitp == &hits[0] )
00701 return(0);
00702 if( hitp == &hits[1] ) {
00703
00704 hits[1] = hits[0];
00705 hitp++;
00706 } else if( hitp > &hits[2] ) {
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717 rt_hitsort( hits, hitp - &hits[0] );
00718
00719
00720 hits[1] = hitp[-1];
00721 }
00722
00723 if( hits[0].hit_dist < hits[1].hit_dist ) {
00724
00725 register struct seg *segp;
00726
00727 RT_GET_SEG(segp, ap->a_resource);
00728 segp->seg_stp = stp;
00729 segp->seg_in = hits[0];
00730 segp->seg_out = hits[1];
00731 BU_LIST_INSERT( &(seghead->l), &(segp->l) );
00732 } else {
00733
00734 register struct seg *segp;
00735
00736 RT_GET_SEG(segp, ap->a_resource);
00737 segp->seg_stp = stp;
00738 segp->seg_in = hits[1];
00739 segp->seg_out = hits[0];
00740 BU_LIST_INSERT( &(seghead->l), &(segp->l) );
00741 }
00742 return(2);
00743 }
00744
00745 #define SEG_MISS(SEG) (SEG).seg_stp=(struct soltab *) 0;
00746
00747
00748
00749
00750
00751
00752 void
00753 rt_part_vshot(struct soltab **stp, struct xray **rp, struct seg *segp, int n, struct application *ap)
00754
00755
00756
00757
00758
00759 {
00760 rt_vstub( stp, rp, segp, n, ap );
00761 }
00762
00763
00764
00765
00766
00767
00768 void
00769 rt_part_norm(register struct hit *hitp, struct soltab *stp, register struct xray *rp)
00770 {
00771 register struct part_specific *part =
00772 (struct part_specific *)stp->st_specific;
00773
00774 VJOIN1( hitp->hit_point, rp->r_pt, hitp->hit_dist, rp->r_dir );
00775 switch( hitp->hit_surfno ) {
00776 case RT_PARTICLE_SURF_VSPHERE:
00777 VSUB2( hitp->hit_normal, hitp->hit_point, part->part_int.part_V );
00778 VUNITIZE( hitp->hit_normal );
00779 break;
00780 case RT_PARTICLE_SURF_HSPHERE:
00781 VSUB3( hitp->hit_normal, hitp->hit_point,
00782 part->part_int.part_V, part->part_int.part_H );
00783 VUNITIZE( hitp->hit_normal );
00784 break;
00785 case RT_PARTICLE_SURF_BODY:
00786
00787 if( part->part_int.part_type == RT_PARTICLE_TYPE_CYLINDER ) {
00788
00789 hitp->hit_vpriv[Z] = 0;
00790 MAT4X3VEC( hitp->hit_normal, part->part_invRoS,
00791 hitp->hit_vpriv );
00792 VUNITIZE( hitp->hit_normal );
00793 } else {
00794
00795 FAST fastf_t s, m;
00796 vect_t unorm;
00797
00798
00799 m = part->part_hrad_prime - part->part_vrad_prime;
00800 s = 1/(part->part_vrad_prime + m * hitp->hit_vpriv[Z]);
00801 unorm[X] = hitp->hit_vpriv[X] * s;
00802 unorm[Y] = hitp->hit_vpriv[Y] * s;
00803
00804 unorm[Z] = -m / sqrt(m*m+1);
00805 MAT4X3VEC( hitp->hit_normal, part->part_invRoS, unorm );
00806 VUNITIZE( hitp->hit_normal );
00807 }
00808 break;
00809 }
00810 }
00811
00812
00813
00814
00815
00816
00817
00818 void
00819 rt_part_curve(register struct curvature *cvp, register struct hit *hitp, struct soltab *stp)
00820 {
00821 register struct part_specific *part =
00822 (struct part_specific *)stp->st_specific;
00823 point_t hit_local;
00824 point_t hit_unit;
00825
00826 switch( hitp->hit_surfno ) {
00827 case RT_PARTICLE_SURF_VSPHERE:
00828 bn_vec_ortho( cvp->crv_pdir, hitp->hit_normal );
00829 cvp->crv_c1 = cvp->crv_c2 = -part->part_int.part_vrad;
00830 break;
00831 case RT_PARTICLE_SURF_HSPHERE:
00832 bn_vec_ortho( cvp->crv_pdir, hitp->hit_normal );
00833 cvp->crv_c1 = cvp->crv_c2 = -part->part_int.part_hrad;
00834 break;
00835 case RT_PARTICLE_SURF_BODY:
00836
00837 VCROSS( cvp->crv_pdir, hitp->hit_normal, part->part_int.part_H );
00838 VUNITIZE( cvp->crv_pdir );
00839
00840 VSUB2( hit_local, hitp->hit_point, part->part_int.part_V );
00841 MAT4X3VEC( hit_unit, part->part_SoR, hit_local );
00842
00843 cvp->crv_c1 = -(
00844 part->part_v_erad * hit_unit[Z] +
00845 part->part_h_erad * (1 - hit_unit[Z]) );
00846 cvp->crv_c2 = 0;
00847 break;
00848 }
00849 }
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865 void
00866 rt_part_uv(struct application *ap, struct soltab *stp, register struct hit *hitp, register struct uvcoord *uvp)
00867 {
00868 register const struct part_specific *part =
00869 (struct part_specific *)stp->st_specific;
00870 point_t hit_local;
00871 point_t hit_unit;
00872 fastf_t hsize;
00873 fastf_t hmag_inv;
00874 fastf_t vrad_unit;
00875 fastf_t r;
00876 fastf_t minrad;
00877
00878 RT_PART_CK_MAGIC(&part->part_int.part_magic);
00879
00880 hmag_inv = 1.0/MAGNITUDE(part->part_int.part_H);
00881 hsize = 1 + (vrad_unit = part->part_v_erad*hmag_inv) +
00882 part->part_h_erad*hmag_inv;
00883
00884
00885 VSUB2( hit_local, hitp->hit_point, part->part_int.part_V );
00886 MAT4X3VEC( hit_unit, part->part_SoR, hit_local );
00887
00888 uvp->uv_v = (hit_unit[Z] + vrad_unit) / hsize;
00889
00890
00891 uvp->uv_u = bn_atan2( hit_unit[Y], hit_unit[X] ) * bn_inv2pi;
00892 if( uvp->uv_u < 0 )
00893 uvp->uv_u += 1.0;
00894
00895
00896 minrad = part->part_v_erad;
00897 V_MIN(minrad, part->part_h_erad);
00898 r = ap->a_rbeam + ap->a_diverge * hitp->hit_dist;
00899 uvp->uv_du = uvp->uv_dv =
00900 bn_inv2pi * r / minrad;
00901 }
00902
00903
00904
00905
00906 void
00907 rt_part_free(register struct soltab *stp)
00908 {
00909 register struct part_specific *part =
00910 (struct part_specific *)stp->st_specific;
00911
00912 bu_free( (char *)part, "part_specific" );
00913 stp->st_specific = GENPTR_NULL;
00914 }
00915
00916
00917
00918
00919 int
00920 rt_part_class(void)
00921 {
00922 return(0);
00923 }
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937 HIDDEN void
00938 rt_part_hemisphere(register point_t (*ov), register fastf_t *v, fastf_t *a, fastf_t *b, fastf_t *h)
00939 {
00940 register float cos45 = 0.707107;
00941
00942
00943 VADD2( ov[12], v, h );
00944
00945 VADD2( ov[0], v, a );
00946 VJOIN2( ov[1], v, cos45, a, cos45, b );
00947 VADD2( ov[2], v, b );
00948 VJOIN2( ov[3], v, -cos45, a, cos45, b );
00949 VSUB2( ov[4], v, a );
00950 VJOIN2( ov[5], v, -cos45, a, -cos45, b );
00951 VSUB2( ov[6], v, b );
00952 VJOIN2( ov[7], v, cos45, a, -cos45, b );
00953
00954 VJOIN2( ov[8], v, cos45, a, cos45, h );
00955 VJOIN2( ov[10], v, -cos45, a, cos45, h );
00956
00957 VJOIN2( ov[9], v, cos45, b, cos45, h );
00958 VJOIN2( ov[11], v, -cos45, b, cos45, h );
00959
00960 }
00961
00962
00963
00964
00965 int
00966 rt_part_plot(struct bu_list *vhead, struct rt_db_internal *ip, const struct rt_tess_tol *ttol, const struct bn_tol *tol)
00967 {
00968 struct rt_part_internal *pip;
00969 point_t tail;
00970 point_t sphere_rim[16];
00971 point_t vhemi[13];
00972 point_t hhemi[13];
00973 vect_t a, b, c;
00974 vect_t as, bs, hs;
00975 vect_t Hunit;
00976 register int i;
00977
00978 RT_CK_DB_INTERNAL(ip);
00979 pip = (struct rt_part_internal *)ip->idb_ptr;
00980 RT_PART_CK_MAGIC(pip);
00981
00982 if( pip->part_type == RT_PARTICLE_TYPE_SPHERE ) {
00983
00984 VSET( a, pip->part_vrad, 0, 0 );
00985 VSET( b, 0, pip->part_vrad, 0 );
00986 VSET( c, 0, 0, pip->part_vrad );
00987
00988 rt_ell_16pts( &sphere_rim[0][X], pip->part_V, a, b );
00989 RT_ADD_VLIST( vhead, sphere_rim[15], BN_VLIST_LINE_MOVE );
00990 for( i=0; i<16; i++ ) {
00991 RT_ADD_VLIST( vhead, sphere_rim[i], BN_VLIST_LINE_DRAW );
00992 }
00993
00994 rt_ell_16pts( &sphere_rim[0][X], pip->part_V, b, c );
00995 RT_ADD_VLIST( vhead, sphere_rim[15], BN_VLIST_LINE_MOVE );
00996 for( i=0; i<16; i++ ) {
00997 RT_ADD_VLIST( vhead, sphere_rim[i], BN_VLIST_LINE_DRAW );
00998 }
00999
01000 rt_ell_16pts( &sphere_rim[0][X], pip->part_V, a, c );
01001 RT_ADD_VLIST( vhead, sphere_rim[15], BN_VLIST_LINE_MOVE );
01002 for( i=0; i<16; i++ ) {
01003 RT_ADD_VLIST( vhead, sphere_rim[i], BN_VLIST_LINE_DRAW );
01004 }
01005 return(0);
01006 }
01007
01008 VMOVE( Hunit, pip->part_H );
01009 VUNITIZE( Hunit );
01010 bn_vec_perp( a, Hunit );
01011 VUNITIZE(a);
01012 VCROSS( b, Hunit, a );
01013 VUNITIZE(b);
01014
01015 VSCALE( as, a, pip->part_vrad );
01016 VSCALE( bs, b, pip->part_vrad );
01017 VSCALE( hs, Hunit, -pip->part_vrad );
01018 rt_part_hemisphere( vhemi, pip->part_V, as, bs, hs );
01019
01020 VADD2( tail, pip->part_V, pip->part_H );
01021 VSCALE( as, a, pip->part_hrad );
01022 VSCALE( bs, b, pip->part_hrad );
01023 VSCALE( hs, Hunit, pip->part_hrad );
01024 rt_part_hemisphere( hhemi, tail, as, bs, hs );
01025
01026
01027 RT_ADD_VLIST( vhead, vhemi[0], BN_VLIST_LINE_MOVE );
01028 for( i=7; i >= 0; i-- ) {
01029 RT_ADD_VLIST( vhead, vhemi[i], BN_VLIST_LINE_DRAW );
01030 }
01031 RT_ADD_VLIST( vhead, vhemi[8], BN_VLIST_LINE_DRAW );
01032 RT_ADD_VLIST( vhead, vhemi[12], BN_VLIST_LINE_DRAW );
01033 RT_ADD_VLIST( vhead, vhemi[10], BN_VLIST_LINE_DRAW );
01034 RT_ADD_VLIST( vhead, vhemi[4], BN_VLIST_LINE_DRAW );
01035 RT_ADD_VLIST( vhead, vhemi[2], BN_VLIST_LINE_MOVE );
01036 RT_ADD_VLIST( vhead, vhemi[9], BN_VLIST_LINE_DRAW );
01037 RT_ADD_VLIST( vhead, vhemi[12], BN_VLIST_LINE_DRAW );
01038 RT_ADD_VLIST( vhead, vhemi[11], BN_VLIST_LINE_DRAW );
01039 RT_ADD_VLIST( vhead, vhemi[6], BN_VLIST_LINE_DRAW );
01040
01041
01042 RT_ADD_VLIST( vhead, hhemi[0], BN_VLIST_LINE_MOVE );
01043 for( i=7; i >= 0; i-- ) {
01044 RT_ADD_VLIST( vhead, hhemi[i], BN_VLIST_LINE_DRAW );
01045 }
01046 RT_ADD_VLIST( vhead, hhemi[8], BN_VLIST_LINE_DRAW );
01047 RT_ADD_VLIST( vhead, hhemi[12], BN_VLIST_LINE_DRAW );
01048 RT_ADD_VLIST( vhead, hhemi[10], BN_VLIST_LINE_DRAW );
01049 RT_ADD_VLIST( vhead, hhemi[4], BN_VLIST_LINE_DRAW );
01050 RT_ADD_VLIST( vhead, hhemi[2], BN_VLIST_LINE_MOVE );
01051 RT_ADD_VLIST( vhead, hhemi[9], BN_VLIST_LINE_DRAW );
01052 RT_ADD_VLIST( vhead, hhemi[12], BN_VLIST_LINE_DRAW );
01053 RT_ADD_VLIST( vhead, hhemi[11], BN_VLIST_LINE_DRAW );
01054 RT_ADD_VLIST( vhead, hhemi[6], BN_VLIST_LINE_DRAW );
01055
01056
01057 RT_ADD_VLIST( vhead, vhemi[0], BN_VLIST_LINE_MOVE );
01058 RT_ADD_VLIST( vhead, hhemi[0], BN_VLIST_LINE_DRAW );
01059 RT_ADD_VLIST( vhead, vhemi[2], BN_VLIST_LINE_MOVE );
01060 RT_ADD_VLIST( vhead, hhemi[2], BN_VLIST_LINE_DRAW );
01061 RT_ADD_VLIST( vhead, vhemi[4], BN_VLIST_LINE_MOVE );
01062 RT_ADD_VLIST( vhead, hhemi[4], BN_VLIST_LINE_DRAW );
01063 RT_ADD_VLIST( vhead, vhemi[6], BN_VLIST_LINE_MOVE );
01064 RT_ADD_VLIST( vhead, hhemi[6], BN_VLIST_LINE_DRAW );
01065
01066 return(0);
01067 }
01068
01069 struct part_state {
01070 struct shell *s;
01071 mat_t upper_invRinvS;
01072 mat_t upper_invRoS;
01073 mat_t lower_invRinvS;
01074 mat_t lower_invRoS;
01075 fastf_t theta_tol;
01076 };
01077
01078 struct part_vert_strip {
01079 int nverts_per_strip;
01080 int nverts;
01081 struct vertex **vp;
01082 vect_t *norms;
01083 int nfaces;
01084 struct faceuse **fu;
01085 };
01086
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097 int
01098 rt_part_tess(struct nmgregion **r, struct model *m, struct rt_db_internal *ip, const struct rt_tess_tol *ttol, const struct bn_tol *tol)
01099 {
01100 struct rt_part_internal *pip;
01101 LOCAL mat_t R;
01102 LOCAL mat_t S;
01103 LOCAL mat_t invR;
01104 LOCAL mat_t invS;
01105 LOCAL vect_t zz;
01106 LOCAL vect_t hcenter;
01107 struct part_state state;
01108 register int i;
01109 fastf_t radius;
01110 int nsegs;
01111 int nstrips;
01112 struct part_vert_strip *strips;
01113 int j;
01114 struct vertex **vertp[5];
01115 int faceno;
01116 int stripno;
01117 int boff;
01118 int toff;
01119 int blim;
01120 int tlim;
01121 fastf_t rel;
01122
01123 RT_CK_DB_INTERNAL(ip);
01124 pip = (struct rt_part_internal *)ip->idb_ptr;
01125 RT_PART_CK_MAGIC(pip);
01126
01127 if( pip->part_type == RT_PARTICLE_TYPE_SPHERE )
01128 return(-1);
01129
01130
01131 VADD2( hcenter, pip->part_V, pip->part_H );
01132
01133
01134
01135
01136
01137 VSET( zz, 0, 0, 1 );
01138 bn_mat_fromto( R, pip->part_H, zz );
01139 bn_mat_trn( invR, R );
01140
01141
01142
01143
01144
01145 MAT_IDN( S );
01146 S[ 0] = S[ 5] = S[10] = 1/pip->part_hrad;
01147 bn_mat_inv( invS, S );
01148
01149
01150 bn_mat_mul( state.upper_invRinvS, invR, invS );
01151
01152
01153 bn_mat_mul( state.upper_invRoS, invR, S );
01154
01155
01156
01157
01158
01159 MAT_IDN( S );
01160 S[ 0] = S[ 5] = S[10] = 1/pip->part_vrad;
01161 bn_mat_inv( invS, S );
01162
01163
01164 bn_mat_mul( state.lower_invRinvS, invR, invS );
01165
01166
01167 bn_mat_mul( state.lower_invRoS, invR, S );
01168
01169
01170 radius = pip->part_vrad;
01171 if( pip->part_hrad > radius )
01172 radius = pip->part_hrad;
01173
01174
01175
01176
01177 if( ttol->rel <= 0.0 || ttol->rel >= 1.0 ) {
01178 rel = 0.0;
01179 } else {
01180
01181 rel = ttol->rel * radius;
01182 }
01183 if( ttol->abs <= 0.0 ) {
01184 if( rel <= 0.0 ) {
01185
01186 rel = 0.10 * radius;
01187 } else {
01188
01189 }
01190 } else {
01191
01192 if( ttol->rel <= 0.0 || rel > ttol->abs )
01193 {
01194 rel = ttol->abs;
01195 if( rel > radius )
01196 rel = radius;
01197 }
01198 }
01199
01200
01201
01202
01203
01204 state.theta_tol = 2 * acos( 1.0 - rel / radius );
01205
01206
01207 if( ttol->norm > 0.0 && ttol->norm < state.theta_tol ) {
01208 state.theta_tol = ttol->norm;
01209 }
01210
01211 *r = nmg_mrsv( m );
01212 state.s = BU_LIST_FIRST(shell, &(*r)->s_hd);
01213
01214
01215 nsegs = bn_halfpi / state.theta_tol + 0.999;
01216 if( nsegs < 2 ) nsegs = 2;
01217
01218
01219
01220
01221
01222
01223
01224 nstrips = 2 * nsegs + 2;
01225 strips = (struct part_vert_strip *)bu_calloc( nstrips,
01226 sizeof(struct part_vert_strip), "strips[]" );
01227
01228
01229 strips[0].nverts = 1;
01230 strips[0].nverts_per_strip = 0;
01231 strips[0].nfaces = 4;
01232
01233 strips[nstrips-1].nverts = 1;
01234 strips[nstrips-1].nverts_per_strip = 0;
01235 strips[nstrips-1].nfaces = 4;
01236
01237 strips[nsegs].nverts = nsegs * 4;
01238 strips[nsegs].nverts_per_strip = nsegs;
01239 strips[nsegs].nfaces = nsegs * 4;
01240
01241 strips[nsegs+1].nverts = nsegs * 4;
01242 strips[nsegs+1].nverts_per_strip = nsegs;
01243 strips[nsegs+1].nfaces = 0;
01244
01245 for( i=1; i<nsegs; i++ ) {
01246 strips[i].nverts_per_strip =
01247 strips[nstrips-1-i].nverts_per_strip = i;
01248 strips[i].nverts =
01249 strips[nstrips-1-i].nverts = i * 4;
01250 strips[i].nfaces =
01251 strips[nstrips-1-i].nfaces = (2 * i + 1)*4;
01252 }
01253
01254 for( i=0; i<nstrips; i++ ) {
01255 strips[i].vp = (struct vertex **)bu_calloc( strips[i].nverts,
01256 sizeof(struct vertex *), "strip vertex[]" );
01257 strips[i].norms = (vect_t *)bu_calloc( strips[i].nverts,
01258 sizeof( vect_t ), "strip normals[]" );
01259 }
01260
01261 for( i=0; i < nstrips; i++ ) {
01262 if( strips[i].nfaces <= 0 ) {
01263 strips[i].fu = (struct faceuse **)NULL;
01264 continue;
01265 }
01266 strips[i].fu = (struct faceuse **)bu_calloc( strips[i].nfaces,
01267 sizeof(struct faceuse *), "strip faceuse[]" );
01268 }
01269
01270
01271
01272 for( i = 1; i <= nsegs; i++ ) {
01273 faceno = 0;
01274 tlim = strips[i-1].nverts;
01275 blim = strips[i].nverts;
01276 for( stripno=0; stripno<4; stripno++ ) {
01277 toff = stripno * strips[i-1].nverts_per_strip;
01278 boff = stripno * strips[i].nverts_per_strip;
01279
01280
01281 for( j = 0; j < strips[i].nverts_per_strip; j++ ) {
01282
01283
01284 vertp[0] = &(strips[i].vp[j+boff]);
01285 vertp[1] = &(strips[i].vp[(j+1+boff)%blim]);
01286 vertp[2] = &(strips[i-1].vp[(j+toff)%tlim]);
01287 if( (strips[i-1].fu[faceno++] = nmg_cmface(state.s, vertp, 3 )) == 0 ) {
01288 bu_log("rt_part_tess() nmg_cmface failure\n");
01289 goto fail;
01290 }
01291 if( j+1 >= strips[i].nverts_per_strip ) break;
01292
01293
01294 vertp[0] = &(strips[i].vp[(j+1+boff)%blim]);
01295 vertp[1] = &(strips[i-1].vp[(j+1+toff)%tlim]);
01296 vertp[2] = &(strips[i-1].vp[(j+toff)%tlim]);
01297 if( (strips[i-1].fu[faceno++] = nmg_cmface(state.s, vertp, 3 )) == 0 ) {
01298 bu_log("rt_part_tess() nmg_cmface failure\n");
01299 goto fail;
01300 }
01301 }
01302 }
01303 }
01304
01305
01306 i = nsegs+1;
01307 {
01308 faceno = 0;
01309 tlim = strips[i-1].nverts;
01310 blim = strips[i].nverts;
01311 {
01312
01313 for( j = 0; j < strips[i].nverts; j++ ) {
01314
01315
01316 vertp[3] = &(strips[i].vp[(j)%blim]);
01317 vertp[2] = &(strips[i-1].vp[(j)%tlim]);
01318 vertp[1] = &(strips[i-1].vp[(j+1)%tlim]);
01319 vertp[0] = &(strips[i].vp[(j+1)%blim]);
01320 if( (strips[i-1].fu[faceno++] = nmg_cmface(state.s, vertp, 4 )) == 0 ) {
01321 bu_log("rt_part_tess() nmg_cmface failure\n");
01322 goto fail;
01323 }
01324 }
01325 }
01326 }
01327
01328
01329 for( i = nsegs+1; i < nstrips; i++ ) {
01330 faceno = 0;
01331 tlim = strips[i+1].nverts;
01332 blim = strips[i].nverts;
01333 for( stripno=0; stripno<4; stripno++ ) {
01334 toff = stripno * strips[i+1].nverts_per_strip;
01335 boff = stripno * strips[i].nverts_per_strip;
01336
01337
01338 for( j = 0; j < strips[i].nverts_per_strip; j++ ) {
01339
01340
01341 vertp[0] = &(strips[i].vp[j+boff]);
01342 vertp[1] = &(strips[i+1].vp[(j+toff)%tlim]);
01343 vertp[2] = &(strips[i].vp[(j+1+boff)%blim]);
01344 if( (strips[i+1].fu[faceno++] = nmg_cmface(state.s, vertp, 3 )) == 0 ) {
01345 bu_log("rt_part_tess() nmg_cmface failure\n");
01346 goto fail;
01347 }
01348 if( j+1 >= strips[i].nverts_per_strip ) break;
01349
01350
01351 vertp[0] = &(strips[i].vp[(j+1+boff)%blim]);
01352 vertp[1] = &(strips[i+1].vp[(j+toff)%tlim]);
01353 vertp[2] = &(strips[i+1].vp[(j+1+toff)%tlim]);
01354 if( (strips[i+1].fu[faceno++] = nmg_cmface(state.s, vertp, 3 )) == 0 ) {
01355 bu_log("rt_part_tess() nmg_cmface failure\n");
01356 goto fail;
01357 }
01358 }
01359 }
01360 }
01361
01362
01363
01364
01365
01366 for( i=0; i < nstrips; i++ ) {
01367 double alpha;
01368 double beta;
01369 fastf_t cos_alpha, sin_alpha;
01370 fastf_t cos_beta, sin_beta;
01371 point_t sphere_pt;
01372 point_t model_pt;
01373
01374
01375 if( i <= nsegs )
01376 alpha = (((double)i) / (nstrips-1-1));
01377 else
01378 alpha = (((double)i-1) / (nstrips-1-1));
01379 cos_alpha = cos(alpha*bn_pi);
01380 sin_alpha = sin(alpha*bn_pi);
01381 for( j=0; j < strips[i].nverts; j++ ) {
01382
01383 beta = ((double)j) / strips[i].nverts;
01384 cos_beta = cos(beta*bn_twopi);
01385 sin_beta = sin(beta*bn_twopi);
01386 VSET( sphere_pt,
01387 cos_beta * sin_alpha,
01388 sin_beta * sin_alpha,
01389 cos_alpha );
01390
01391 if( i <= nsegs ) {
01392 MAT4X3PNT( model_pt, state.upper_invRinvS, sphere_pt );
01393 VADD2( model_pt, model_pt, hcenter );
01394
01395 MAT4X3VEC( strips[i].norms[j], state.upper_invRoS, sphere_pt );
01396 } else {
01397 MAT4X3PNT( model_pt, state.lower_invRinvS, sphere_pt );
01398 VADD2( model_pt, model_pt, pip->part_V );
01399
01400 MAT4X3VEC( strips[i].norms[j], state.lower_invRoS, sphere_pt );
01401 }
01402
01403
01404 VUNITIZE( strips[i].norms[j] );
01405
01406 nmg_vertex_gv( strips[i].vp[j], model_pt );
01407 }
01408 }
01409
01410
01411 for( i=0; i < nstrips; i++ ) {
01412 for( j=0; j < strips[i].nfaces; j++ ) {
01413 if( nmg_fu_planeeqn( strips[i].fu[j], tol ) < 0 )
01414 goto fail;
01415 }
01416 }
01417
01418
01419 for( i=0; i < nstrips; i++ )
01420 {
01421 for( j=0; j < strips[i].nverts; j++ )
01422 {
01423 struct faceuse *fu;
01424 struct vertexuse *vu;
01425 vect_t norm_opp;
01426
01427 NMG_CK_VERTEX( strips[i].vp[j] );
01428 VREVERSE( norm_opp , strips[i].norms[j] )
01429
01430 for( BU_LIST_FOR( vu , vertexuse , &strips[i].vp[j]->vu_hd ) )
01431 {
01432 fu = nmg_find_fu_of_vu( vu );
01433 NMG_CK_FACEUSE( fu );
01434
01435
01436
01437 if( fu->orientation == OT_SAME )
01438 nmg_vertexuse_nv( vu , strips[i].norms[j] );
01439 else if( fu->orientation == OT_OPPOSITE )
01440 nmg_vertexuse_nv( vu , norm_opp );
01441 }
01442 }
01443 }
01444
01445
01446 nmg_region_a( *r, tol );
01447
01448
01449
01450 for( i=0; i<nstrips; i++ ) {
01451 bu_free( (char *)strips[i].vp, "strip vertex[]" );
01452 bu_free( (char *)strips[i].norms, "strip norms[]" );
01453 }
01454
01455 for( i=0; i < nstrips; i++ ) {
01456 if( strips[i].fu == (struct faceuse **)0 ) continue;
01457 bu_free( (char *)strips[i].fu, "strip faceuse[]" );
01458 }
01459 bu_free( (char *)strips, "strips[]" );
01460 return(0);
01461 fail:
01462
01463
01464 for( i=0; i<nstrips; i++ ) {
01465 bu_free( (char *)strips[i].vp, "strip vertex[]" );
01466 bu_free( (char *)strips[i].norms, "strip norms[]" );
01467 }
01468
01469 for( i=0; i < nstrips; i++ ) {
01470 if( strips[i].fu == (struct faceuse **)0 ) continue;
01471 bu_free( (char *)strips[i].fu, "strip faceuse[]" );
01472 }
01473 bu_free( (char *)strips, "strips[]" );
01474 return(-1);
01475 }
01476
01477
01478
01479
01480 int
01481 rt_part_import(struct rt_db_internal *ip, const struct bu_external *ep, register const fastf_t *mat, const struct db_i *dbip)
01482 {
01483 point_t v;
01484 vect_t h;
01485 double vrad;
01486 double hrad;
01487 fastf_t maxrad, minrad;
01488 union record *rp;
01489 struct rt_part_internal *part;
01490
01491 BU_CK_EXTERNAL( ep );
01492 rp = (union record *)ep->ext_buf;
01493
01494 if( rp->u_id != DBID_PARTICLE ) {
01495 bu_log("rt_part_import: defective record\n");
01496 return(-1);
01497 }
01498
01499
01500 ntohd( (unsigned char *)v, rp->part.p_v, 3 );
01501 ntohd( (unsigned char *)h, rp->part.p_h, 3 );
01502 ntohd( (unsigned char *)&vrad, rp->part.p_vrad, 1 );
01503 ntohd( (unsigned char *)&hrad, rp->part.p_hrad, 1 );
01504
01505 RT_CK_DB_INTERNAL( ip );
01506 ip->idb_major_type = DB5_MAJORTYPE_BRLCAD;
01507 ip->idb_type = ID_PARTICLE;
01508 ip->idb_meth = &rt_functab[ID_PARTICLE];
01509 ip->idb_ptr = bu_malloc( sizeof(struct rt_part_internal), "rt_part_internal");
01510 part = (struct rt_part_internal *)ip->idb_ptr;
01511 part->part_magic = RT_PART_INTERNAL_MAGIC;
01512
01513
01514 MAT4X3PNT( part->part_V, mat, v );
01515 MAT4X3VEC( part->part_H, mat, h );
01516 if( (part->part_vrad = vrad / mat[15]) < 0 ) {
01517 bu_log("unable to import particle, negative v radius\n");
01518 bu_free( ip->idb_ptr, "rt_part_internal" );
01519 ip->idb_ptr=NULL;
01520 return(-2);
01521 }
01522 if( (part->part_hrad = hrad / mat[15]) < 0 ) {
01523 bu_log("unable to import particle, negative h radius\n");
01524 bu_free( ip->idb_ptr, "rt_part_internal" );
01525 ip->idb_ptr=NULL;
01526 return(-3);
01527 }
01528
01529 if( part->part_vrad > part->part_hrad ) {
01530 maxrad = part->part_vrad;
01531 minrad = part->part_hrad;
01532 } else {
01533 maxrad = part->part_hrad;
01534 minrad = part->part_vrad;
01535 }
01536 if( maxrad <= 0 ) {
01537 bu_log("unable to import particle, negative radius\n");
01538 bu_free( ip->idb_ptr, "rt_part_internal" );
01539 ip->idb_ptr=NULL;
01540 return(-4);
01541 }
01542
01543 if( MAGSQ( part->part_H ) * 1000000 < maxrad * maxrad ) {
01544
01545 part->part_vrad = part->part_hrad = maxrad;
01546 VSETALL( part->part_H, 0 );
01547 part->part_type = RT_PARTICLE_TYPE_SPHERE;
01548 return(0);
01549 }
01550
01551 if( (maxrad - minrad) / maxrad < 0.001 ) {
01552
01553 part->part_vrad = part->part_hrad = maxrad;
01554 part->part_type = RT_PARTICLE_TYPE_CYLINDER;
01555 return(0);
01556 }
01557
01558 part->part_type = RT_PARTICLE_TYPE_CONE;
01559 return(0);
01560 }
01561
01562
01563
01564
01565 int
01566 rt_part_export(struct bu_external *ep, const struct rt_db_internal *ip, double local2mm, const struct db_i *dbip)
01567 {
01568 struct rt_part_internal *pip;
01569 union record *rec;
01570 point_t vert;
01571 vect_t hi;
01572 fastf_t vrad;
01573 fastf_t hrad;
01574
01575 RT_CK_DB_INTERNAL(ip);
01576 if( ip->idb_type != ID_PARTICLE ) return(-1);
01577 pip = (struct rt_part_internal *)ip->idb_ptr;
01578 RT_PART_CK_MAGIC(pip);
01579
01580 BU_CK_EXTERNAL(ep);
01581 ep->ext_nbytes = sizeof(union record);
01582 ep->ext_buf = (genptr_t)bu_calloc( 1, ep->ext_nbytes, "part external");
01583 rec = (union record *)ep->ext_buf;
01584
01585
01586 VSCALE( vert, pip->part_V, local2mm );
01587 VSCALE( hi, pip->part_H, local2mm );
01588 vrad = pip->part_vrad * local2mm;
01589 hrad = pip->part_hrad * local2mm;
01590
01591
01592 rec->part.p_id = DBID_PARTICLE;
01593 htond( rec->part.p_v, (unsigned char *)vert, 3 );
01594 htond( rec->part.p_h, (unsigned char *)hi, 3 );
01595 htond( rec->part.p_vrad, (unsigned char *)&vrad, 1 );
01596 htond( rec->part.p_hrad, (unsigned char *)&hrad, 1 );
01597
01598 return(0);
01599 }
01600
01601
01602
01603
01604
01605 int
01606 rt_part_import5(struct rt_db_internal *ip, const struct bu_external *ep, register const fastf_t *mat, const struct db_i *dbip)
01607 {
01608 fastf_t maxrad, minrad;
01609 struct rt_part_internal *part;
01610 fastf_t vec[8];
01611
01612 BU_CK_EXTERNAL( ep );
01613
01614 BU_ASSERT_LONG( ep->ext_nbytes, ==, SIZEOF_NETWORK_DOUBLE * 8 );
01615
01616 RT_CK_DB_INTERNAL( ip );
01617 ip->idb_major_type = DB5_MAJORTYPE_BRLCAD;
01618 ip->idb_type = ID_PARTICLE;
01619 ip->idb_meth = &rt_functab[ID_PARTICLE];
01620 ip->idb_ptr = bu_malloc( sizeof(struct rt_part_internal), "rt_part_internal");
01621
01622 part = (struct rt_part_internal *)ip->idb_ptr;
01623 part->part_magic = RT_PART_INTERNAL_MAGIC;
01624
01625
01626 ntohd( (unsigned char *)vec, ep->ext_buf, 8 );
01627
01628
01629 MAT4X3PNT( part->part_V, mat, &vec[0*3] );
01630 MAT4X3VEC( part->part_H, mat, &vec[1*3] );
01631 if( (part->part_vrad = vec[2*3] / mat[15]) < 0 ) {
01632 bu_free( ip->idb_ptr, "rt_part_internal" );
01633 ip->idb_ptr=NULL;
01634 bu_log("unable to import particle, negative v radius\n");
01635 return(-2);
01636 }
01637 if( (part->part_hrad = vec[2*3+1] / mat[15]) < 0 ) {
01638 bu_free( ip->idb_ptr, "rt_part_internal" );
01639 ip->idb_ptr=NULL;
01640 bu_log("unable to import particle, negative h radius\n");
01641 return(-3);
01642 }
01643
01644 if( part->part_vrad > part->part_hrad ) {
01645 maxrad = part->part_vrad;
01646 minrad = part->part_hrad;
01647 } else {
01648 maxrad = part->part_hrad;
01649 minrad = part->part_vrad;
01650 }
01651 if( maxrad <= 0 ) {
01652 bu_free( ip->idb_ptr, "rt_part_internal" );
01653 ip->idb_ptr=NULL;
01654 bu_log("unable to import particle, negative radius\n");
01655 return(-4);
01656 }
01657
01658 if( MAGSQ( part->part_H ) * 1000000 < maxrad * maxrad ) {
01659
01660 part->part_vrad = part->part_hrad = maxrad;
01661 VSETALL( part->part_H, 0 );
01662 part->part_type = RT_PARTICLE_TYPE_SPHERE;
01663 return(0);
01664 }
01665
01666 if( (maxrad - minrad) / maxrad < 0.001 ) {
01667
01668 part->part_vrad = part->part_hrad = maxrad;
01669 part->part_type = RT_PARTICLE_TYPE_CYLINDER;
01670 return(0);
01671 }
01672
01673 part->part_type = RT_PARTICLE_TYPE_CONE;
01674 return(0);
01675 }
01676
01677
01678
01679
01680 int
01681 rt_part_export5(struct bu_external *ep, const struct rt_db_internal *ip, double local2mm, const struct db_i *dbip)
01682 {
01683 struct rt_part_internal *pip;
01684 fastf_t vec[8];
01685
01686 RT_CK_DB_INTERNAL(ip);
01687 if( ip->idb_type != ID_PARTICLE ) return(-1);
01688 pip = (struct rt_part_internal *)ip->idb_ptr;
01689 RT_PART_CK_MAGIC(pip);
01690
01691 BU_CK_EXTERNAL(ep);
01692 ep->ext_nbytes = SIZEOF_NETWORK_DOUBLE * 8;
01693 ep->ext_buf = (genptr_t)bu_malloc( ep->ext_nbytes, "part external");
01694
01695
01696 VSCALE( &vec[0*3], pip->part_V, local2mm );
01697 VSCALE( &vec[1*3], pip->part_H, local2mm );
01698 vec[2*3] = pip->part_vrad * local2mm;
01699 vec[2*3+1] = pip->part_hrad * local2mm;
01700
01701
01702
01703
01704 htond( ep->ext_buf, (unsigned char *)vec, 8 );
01705
01706 return(0);
01707 }
01708
01709
01710
01711
01712
01713
01714
01715
01716 int
01717 rt_part_describe(struct bu_vls *str, const struct rt_db_internal *ip, int verbose, double mm2local)
01718 {
01719 register struct rt_part_internal *pip =
01720 (struct rt_part_internal *)ip->idb_ptr;
01721 char buf[256];
01722
01723 RT_PART_CK_MAGIC(pip);
01724 switch( pip->part_type ) {
01725 case RT_PARTICLE_TYPE_SPHERE:
01726 bu_vls_strcat( str, "spherical particle\n");
01727 sprintf(buf, "\tV (%g, %g, %g)\n",
01728 INTCLAMP(pip->part_V[X] * mm2local),
01729 INTCLAMP(pip->part_V[Y] * mm2local),
01730 INTCLAMP(pip->part_V[Z] * mm2local) );
01731 bu_vls_strcat( str, buf );
01732 sprintf(buf, "\tradius = %g\n",
01733 INTCLAMP(pip->part_vrad * mm2local) );
01734 bu_vls_strcat( str, buf );
01735 break;
01736 case RT_PARTICLE_TYPE_CYLINDER:
01737 bu_vls_strcat( str, "cylindrical particle (lozenge)\n");
01738 sprintf(buf, "\tV (%g, %g, %g)\n",
01739 INTCLAMP(pip->part_V[X] * mm2local),
01740 INTCLAMP(pip->part_V[Y] * mm2local),
01741 INTCLAMP(pip->part_V[Z] * mm2local) );
01742 bu_vls_strcat( str, buf );
01743 sprintf(buf, "\tH (%g, %g, %g)\n",
01744 INTCLAMP(pip->part_H[X] * mm2local),
01745 INTCLAMP(pip->part_H[Y] * mm2local),
01746 INTCLAMP(pip->part_H[Z] * mm2local) );
01747 bu_vls_strcat( str, buf );
01748 sprintf(buf, "\tradius = %g\n",
01749 INTCLAMP(pip->part_vrad * mm2local) );
01750 bu_vls_strcat( str, buf );
01751 break;
01752 case RT_PARTICLE_TYPE_CONE:
01753 bu_vls_strcat( str, "conical particle\n");
01754 sprintf(buf, "\tV (%g, %g, %g)\n",
01755 INTCLAMP(pip->part_V[X] * mm2local),
01756 INTCLAMP(pip->part_V[Y] * mm2local),
01757 INTCLAMP(pip->part_V[Z] * mm2local) );
01758 bu_vls_strcat( str, buf );
01759 sprintf(buf, "\tH (%g, %g, %g)\n",
01760 INTCLAMP(pip->part_H[X] * mm2local),
01761 INTCLAMP(pip->part_H[Y] * mm2local),
01762 INTCLAMP(pip->part_H[Z] * mm2local) );
01763 bu_vls_strcat( str, buf );
01764 sprintf(buf, "\tv end radius = %g\n",
01765 INTCLAMP(pip->part_vrad * mm2local) );
01766 bu_vls_strcat( str, buf );
01767 sprintf(buf, "\th end radius = %g\n",
01768 INTCLAMP(pip->part_hrad * mm2local) );
01769 bu_vls_strcat( str, buf );
01770 break;
01771 default:
01772 bu_vls_strcat( str, "Unknown particle type\n");
01773 return(-1);
01774 }
01775 return(0);
01776 }
01777
01778
01779
01780
01781
01782
01783 void
01784 rt_part_ifree(struct rt_db_internal *ip)
01785 {
01786 RT_CK_DB_INTERNAL(ip);
01787 bu_free( ip->idb_ptr, "particle ifree" );
01788 ip->idb_ptr = GENPTR_NULL;
01789 }
01790
01791
01792
01793
01794
01795
01796
01797
01798
01799