# SpatialED.TarquinExtrudeBuilder

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 00188 00189 00190 00191 00192 00193 00194 00195 00196 00197 00198 00199 00200 00201 00202 00203 00204 00205 00206 00207 00208 00209 00210 00211 00212 00213 00214 00215 00216 00217 00218 00219 00220 00221 00222 00223 00224 00225 00226 00227 00228 00229 00230 00231 00232 00233 00234 00235 00236 00237 00238 00239 00240 00241 00242 00243 00244 00245 00246 00247 00248 00249 00250 00251 00252 00253 00254 00255 00256 00257 00258 00259 00260 00261 00262 00263 00264 00265 00266 00267 00268 00269 00270 00271 00272 00273 00274 00275 00276 00277 00278 00279 00280 00281 00282 00283 00284 00285 00286 00287 00288 00289 00290 00291 00292 00293 00294 00295 00296 00297 00298 00299 00300 00301 00302 00303 00304 00305 00306 00307 00308 00309 00310 00311 00312 00313 00314 00315 00316 00317 00318 00319 00320 00321 00322 00323 00324 00325 00326 00327 00328 00329 00330 00331 00332 00333 00334 00335 00336 00337 00338 00339 00340 00341 00342 00343 00344 00345 00346 00347 00348 00349 00350 00351 00352 00353 00354 00355 00356 00357 00358 00359 00360 00361 00362 00363 00364 00365 00366 00367 00368 00369 00370 00371 00372 00373 00374 00375 00376 00377 00378 00379 00380 00381 00382 00383 00384 00385 00386 00387 00388 00389 00390 00391 00392 00393 00394 00395 00396 00397 00398 00399 00400 00401 00402 00403 00404 00405 00406 00407 00408 00409 00410 00411 00412 00413 00414 00415 00416 00417 00418 00419 00420 00421 00422 00423 00424 00425 00426 00427 00428 00429 00430 00431 00432 00433 00434 00435 00436 00437 00438 00439 00440 00441 00442 00443 00444 00445 00446 00447 00448 00449 00450 00451 00452 00453 00454 00455 00456 00457 00458 00459 00460 00461 00462 00463 00464 00465 00466 00467 00468 00469 00470 00471 00472 00473 00474 00475 00476 00477 00478 00479 00480 00481 00482 00483 00484 00485 00486 00487 00488 00489 00490 00491 00492 00493 00494 00495 00496 00497 00498 00499 00500 00501 00502 00503 00504 00505 00506 00507 00508 00509 00510 00511 00512 00513 00514 00515 00516 00517 00518 00519 00520 00521 00522 00523 00524 00525 00526 00527 00528 00529 00530 00531 00532 00533 00534 00535 00536 00537 00538 00539 00540 00541 00542 00543 00544 00545 00546 00547 00548 00549 00550 00551 00552 00553 00554 00555 00556 00557 00558 00559 00560 00561 00562 00563 00564 00565 00566 00567 00568 00569 00570 00571 00572 00573 00574 00575 00576 00577 00578 00579 00580 00581 00582 00583 00584 00585 00586 00587 00588 00589 00590 00591 00592 00593 00594 00595 00596 00597 00598 00599 00600 00601 00602 00603 00604 00605 00606 00607 00608 00609 00610 00611 00612 00613 00614 00615 00616 00617 00618 00619 00620 00621 00622 00623 00624 00625 00626 00627 00628 00629 00630 00631 00632 00633 00634 00635 00636 00637 00638 00639 00640 00641 00642 00643 00644 00645 00646 00647 00648 00649 00650 00651 00652 00653 00654 00655 00656 00657 00658 00659 00660 00661 00662 00663 00664 00665 00666 00667 00668 00669 00670 00671 00672 00673 00674 00675 00676 00677 00678 00679 00680 00681 00682 00683 00684 00685 00686 00687 00688 00689 00690 00691 00692 00693 00694 00695 00696 00697 00698 00699 00700 00701 00702 00703 00704 00705 00706 00707 00708 00709 00710 00711 00712 00713 00714 00715 00716 00717 00718 00719 00720 00721 00722 00723 00724 00725 00726 00727 00728 00729 00730 00731 00732 00733 00734 00735 00736 00737 00738 00739 00740 00741 00742 00743 00744 00745 00746 00747 00748 00749 00750 00751 00752 00753 00754 00755 00756 00757 00758 00759 00760 00761 00762 00763 00764 00765 00766 00767 00768 00769 00770 00771 00772 00773 00774 00775 00776 00777 00778 00779 00780 00781 00782 00783 00784 00785 00786 00787 00788 00789 00790 00791 00792 00793 00794 00795 00796 00797 00798 00799 00800 00801 00802 00803 00804 00805 00806 00807 00808 00809 00810 00811 00812 00813 00814 00815 00816 00817 00818 00819 00820 00821 00822 00823 00824 00825 00826 00827 00828 00829 00830 00831 00832 00833 00834 00835 00836 00837 00838 00839 00840 00841 00842 00843 00844 00845 00846 00847 00848 00849 00850 00851 00852 00853 00854 00855 00856 00857 00858 00859 00860 00861 00862 00863 00864 00865 00866 00867 00868 00869 00870 00871 00872 00873 00874 00875 00876 00877 00878 00879 00880 00881 00882 00883 00884 00885 00886 00887 00888 00889 00890 00891 00892 00893 00894 00895 00896 00897 00898 00899 00900 00901 00902 00903 00904 00905 00906 00907 00908 00909 00910 00911 00912 00913 00914 00915 00916 00917 00918 00919 00920 00921 00922 00923 00924 00925 00926 00927 00928 00929 00930 00931 00932 00933 00934 00935 00936 00937 00938 00939 00940 00941 00942 00943 00944 00945 00946 00947 00948 00949 00950 00951 00952 00953 00954 00955 00956 00957 00958 00959 00960 00961 00962 00963 00964 00965 00966 00967 00968 00969 00970 00971 00972 00973 00974 00975 00976 00977 00978 00979 00980 00981 00982 00983 00984 00985 00986 00987 00988 00989 00990 00991 00992 00993 00994 00995 00996 00997 00998 00999 01000 01001 01002 01003 01004 01005 01006 01007 01008 01009 01010 01011 01012 01013 01014 01015 01016 01017 01018 01019 01020 01021 01022 01023 01024 01025 01026 01027 01028 01029 01030 01031 01032 01033 01034 01035 01036 01037 01038 01039 01040 01041 01042 01043 01044 01045 01046 01047 01048 01049 01050 01051 01052 01053 01054 01055 01056 01057 01058 01059 01060 01061 01062 01063 01064 01065 01066 01067 01068 01069 01070 01071 01072 01073 01074 01075 01076 01077 01078 01079 01080 01081 01082 01083 01084 01085 01086 01087 01088 01089 01090 01091 01092 01093 01094 01095 01096 01097 01098 01099 01100 01101 01102 01103 01104 01105 01106 01107 01108 01109 01110 01111 01112 01113 01114 01115 01116 01117 01118 01119 01120 01121 01122 01123 01124 01125 01126 01127 01128 01129 01130 01131 01132 01133 01134 01135 01136 01137 01138 01139 01140 01141 01142 01143 01144 01145 01146 01147 01148 01149 01150 01151 01152 01153 01154 01155 01156 01157 01158 01159 01160 01161 01162 01163 01164 01165 01166 01167 01168 01169 01170 01171 01172 01173 01174 01175 01176 01177 01178 01179 01180 01181 01182 01183 01184 01185 01186 01187 01188 01189 01190 01191 01192 01193 01194 01195 01196 01197 01198 01199 01200 01201 01202 01203 01204 01205 01206 |
    //=============================================================================
    // Spatial Fear
    // Class: TarquinExtrudeBuilder
    // Description: extrudes a shape to a path, version 080.
    //
    // Author: Tarquin
    // + Used with permission for packaging with SpatialED v2.1
    //=============================================================================
    class TarquinExtrudeBuilder
    	expands BrushBuilder;
    /*
    DOCU notes
    notes: if you are going to use Origin as a Shape point,  use it first.
    same for path
    */
    const ArraySize = 16 ;
    // sadly this doesnt work how I'd like it to
    // in case of future fixes, places that refer to the input array sizes labeled thus: ##arraysize##

    var() vector ShapePoints[16] , PathPoints[16] ;
    var() float ShapeFactor , PathFactor ;
    enum EExtrudeType
    {
    	EX_Translational,
    	EX_Tangential,
    	EX_ThirdWay
    };
    var() EExtrudeType ExtrudeType ;
    enum EShapeSource
    {
    	SO_Vectors,
    	SO_pSquare,
    	SO_pCircle,
    	SO_MrPointy
    };
    //	SO_2DS
    var() EShapeSource ShapeSource ;
    enum EPathSource
    {
    	SO_Vect_Abs,
    	SO_Vect_Rel
    };
    var() EPathSource PathSource ;
    var() float PresetMetrics[6];
    var() bool ClosedPath ;
    var() bool MergeCoplanars ;
    // var() bool ReversePolys ; // this option no longer needed tx to DirectionDetector
    var() bool SheetPolys ;
    var() bool InvisibleFinal ;
    var() vector Randomiser ;
    var() vector RandomiserCaps ;
    var() enum EMaths
    {
    	MA_Matrix,
    	MA_Quaternion
    } Maths ;
    var() name GroupName ;
    var() enum ECommands
    {
    	C_None,
    	C_ShowShape,
    	C_ShowJoints,
    	C_ResetShape,
    	C_ResetPath,
    	C_ScaleShape,
    	C_MoveShape,
    	C_RotShape,
    	C_ConvPath,
    	C_MakeArc,
    	C_MakeMirror
    } _COMMAND ;
    var() float _Parameters[3] ;

    var bool LevelsOnly ; // this is no longer set by the user

    enum ECommandReturn
    {
    	CR_Stop,
    	CR_BuildStop,
    	CR_Continue
    };

    const UUCircle 		= 65536.0f ;
    const ZeroVect 		= vect(0,0,0);
    const SheetPolyFlags = 0x00000108 ;

    // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ structs
    struct Matrix3x3
    {
    	var() float a11,a12,a13,a21,a22,a23,a31,a32,a33;
    };
    struct Quat
    {
    	var() float W,X,Y,Z;
    };
    // ---------------------------------------------------------- quaternion functions
    final operator(16) quat * ( quat Q1 , quat Q2 )
    {
    	local vector V1 , V2 , Vp ;
    	local quat Qp ;
    	V1 = eVect( Q1.X , Q1.Y , Q1.Z ) ;
    	V2 = eVect( Q2.X , Q2.Y , Q2.Z ) ;
    	Qp.W = Q1.W * Q2.W - ( V1 dot V2 ) ;
    	Vp = ( Q1.W * V2 ) + ( Q2.W * V1 ) - ( V1 cross V2 ) ;
    	Qp.X = Vp.X ;
    	Qp.Y = Vp.Y ;
    	Qp.Z = Vp.Z ;
    	return Qp ;
    }
    function quat RotationToQuat( vector Axis , float Theta )
    {
    	// Theta must be given in radians
    	// Axis need not be normalised
    	local quat Q ;
    	local float L ;
    	Axis = Normal( Axis );
    	Q.W = cos( Theta / 2 ) ;
    	Q.X = Axis.X * sin( Theta / 2 ) ;
    	Q.Y = Axis.Y * sin( Theta / 2 ) ;
    	Q.Z = Axis.Z * sin( Theta / 2 ) ;
    	// NORMALISE
    	L = Sqrt( Q.W**2 + Q.X ** 2 + Q.Y ** 2 + Q.Z**2 ) ;
    	Q.W /= L ;
    	Q.X /= L ;
    	Q.Y /= L ;
    	Q.Z /= L ;
    	return Q ;
    }
    function matrix3x3 QuatToMatrix ( quat Q )
    {
    	local matrix3x3 M ;
    	local float w,x,y,z ;
    	w = Q.W ; x = Q.X ; y = Q.Y ; z = Q.Z ;
    	M.a11 = 1 - ( 2 * y** 2 ) -  ( 2 * z** 2 ) ;
    	M.a12 = 2*x*y - 2*z*w ;
    	M.a13 = 2*x*z + 2*y*w ;

    	M.a21 = 2*x*y + 2*z*w ;
    	M.a22 = 1 - 2*x**2 -2*z**2 ;
    	M.a23 = 2*y*z - 2*x*w ;

    	M.a31 = 2*x*z - 2*y*w ;
    	M.a32 = 2*y*z + 2*x*w ;
    	M.a33 = 1 - 2*x**2 - 2*y**2 ;
    	/*
    	1 - 2Y  - 2Z    2XY - 2ZW      2XZ + 2YW
    	2XY + 2ZW       1 - 2X  - 2Z   2YZ - 2XW
    	2XZ - 2YW       2YZ + 2XW      1 - 2X  - 2Y
    	*/
    	return M ;
    }
    // ---------------------------------------------------------- matrix functions
    function Matrix3x3 defMatrix9f( float a11,float a12,float a13, float a21,float a22,float a23,float a31,float a32,float a33 )
    {
    	local Matrix3x3 theMatrix ;
    	theMatrix.a11 = a11 ;
    	theMatrix.a12 = a12 ;
    	theMatrix.a13 = a13;
    	theMatrix.a21 = a21 ;
    	theMatrix.a22 = a22 ;
    	theMatrix.a23 = a23 ;
    	theMatrix.a31 = a31 ;
    	theMatrix.a32 = a32 ;
    	theMatrix.a33 = a33 ;
    	return theMatrix ;
    }
    static final operator(16) vector * ( Matrix3x3 M , vector v )
    {
    	local vector Mv , R1 , R2 , R3 ;
    	R1.x	= M.a11 ;	R1.y	= M.a12 ;	R1.z	= M.a13 ;
    	R2.x	= M.a21 ;	R2.y	= M.a22 ;	R2.z	= M.a23 ;
    	R3.x	= M.a31 ;	R3.y	= M.a32 ;	R3.z	= M.a33 ;

    	Mv.x 	= R1 dot v ;
    	Mv.y 	= R2 dot v ;
    	Mv.z 	= R3 dot v ;
    	return Mv ;
    }
    // ---------------------------------------------------------- Basis functions
    function coords DipBasisRelative( float Theta, float Phi , coords inBasis )
    {
    	// this is bad!
    	local Matrix3x3 DipMatrix ;
    	local coords transBasis ;
    	DipMatrix = defMatrix9f(  sin2(Phi) 			+ cos2(Phi)*cos(Theta) ,
    											-sin(Phi)*cos(Phi)  	+ cos(Phi)*sin(Phi)*cos(Theta) ,
    											sin(Theta)*cos(Phi) ,
    											-cos(Phi)*sin(Phi) 	+ sin(Phi)*cos(Phi)*cos(Theta) ,
    											cos2(Phi)				+ sin2(Phi)*cos(Theta) ,
    											sin(Theta)*sin(Phi) ,
    											-sin(Theta)*cos(phi) ,
    											-sin(Theta)*sin(Phi) ,
    											cos(Theta)
    										);
    	transBasis.Xaxis	= DipMatrix * inBasis.Xaxis ;
    	transBasis.Yaxis	= DipMatrix * inBasis.Yaxis ;
    	transBasis.Zaxis	= DipMatrix * inBasis.Zaxis ;
    	return transBasis ;
    }
    function coords DipBasis( float Theta, float Phi )
    {
    	// dip of Theta about an axis that makes an angle phi with X axis.
    	local coords theBasis ;
    	theBasis.Xaxis.x	= sin2(Phi) 			+ cos2(Phi)*cos(Theta) ;
    	theBasis.Xaxis.y	= -cos(Phi)*sin(Phi) 	+ sin(Phi)*cos(Phi)*cos(Theta) ;
    	theBasis.Xaxis.z	= -sin(Theta)*cos(phi)		;

    	theBasis.Yaxis.x	= -sin(Phi)*cos(Phi)  	+ cos(Phi)*sin(Phi)*cos(Theta) ;
    	theBasis.Yaxis.y	= cos2(Phi)				+ sin2(Phi)*cos(Theta) ;
    	theBasis.Yaxis.z	= -sin(Theta)*sin(Phi)		;
    	// not currently used but here anyway
    	theBasis.Zaxis.x 	= sin(Theta)*cos(Phi) ;
    	theBasis.Zaxis.y 	= sin(Theta)*sin(Phi) ;
    	theBasis.Zaxis.z 	= cos(Theta) ;
    	return theBasis ;
    }
    function vector changeBasis( vector r , vector e1B , vector e2B, vector e3B )
    {
    	// GIVEN: elementary vectors of B given in B'
    	// GIVEN: vector in B to interpret in B'
    	// Q is coord trans. matrix from B to B'
    	// columns of Q are [eB]B' which is our input.
    	// [r]B' = Q * [r]B
    	local Matrix3x3 Q ;
    	Q = defMatrix9f( 	e1B.x , e2B.x, e3B.x,
    						e1B.y , e2B.y, e3B.y,
    						e1B.z , e2B.z, e3B.z );
    	return Q * r ;
    }
    function coords defCoords( vector O , vector i , vector j , vector k )
    {
    	local coords theCoords ;
    	theCoords.Origin = O ;
    	theCoords.XAxis = i ;
    	theCoords.YAxis = j ;
    	theCoords.ZAxis = k ;
    	return theCoords ;
    }
    // ---------------------------------------------------------- Plane functions
    // struct Vector var() config float X, Y, Z;
    // struct Plane extends Vector var() config float W;
    function plane defPlane4f( float x ,  float y ,  float z ,  float w )
    {
    	local plane planePi ;
    	planePi.x 	= x ;
    	planePi.y 	= y ;
    	planePi.z 	= z ;
    	planePi.w 	= w ;
    	return PlanePi ;
    }
    function plane defPlanevf( vector n ,  float w )
    {
    	n = Normal( n ); // hats the vector, so this can receive any vector.
    	// I don't plan on using this with nonzero w.
    	//bear in mind that when determining w, n must be hatted
    	return defPlane4f( n.x, n.y, n.z,w ) ;
    }
    function vector PlaneNormal( plane planePi )
    {
    	//	return vect( planePi.x , planePi.y , planePi.z ); why wont this work?
    	local vector n ;
    	n.x = planePi.x ;
    	n.y = planePi.y ;
    	n.z = planePi.z ;
    	return n ;
    }
    function vector ProjectToPlane( vector r , vector v , plane planePi )
    {
    	// returns r projected onto Pi in direction v. v need not be normalized.
    	local float mu ;
    	if( PlaneNormal( planePi ) dot v == 0 )
    		return r ; // bad case: v is parallel to Pi
    	mu = ( planePI.w - ( PlaneNormal( planePi ) dot r ) ) / ( PlaneNormal( planePi ) dot v ) ;
    	return r + ( mu * v ) ;
    }
    // ---------------------------------------------------------- preset builders
    function vector eVect( float x , float y , float z )
    {

    	// for some reason vect() wont work when setting an array element
    	local vector v ;
    	v.x = x ;
    	v.y = y ;
    	v.z = z ;
    	return v ;
    }
    function bool PutPreset_Square( out int Ns )
    {
    	local float a , b , rx , ry ;
    	a	= PresetMetrics[0] ;
    	b	= PresetMetrics[1] ;
    	rx	= PresetMetrics[2] ;
    	ry	= PresetMetrics[3] ;
    	/*
    	a = ShapePoints[0].x ;
    	b = ShapePoints[0].y ;
    	rx = ShapePoints[1].x ;
    	ry = ShapePoints[1].y ;
    	*/
    	if( a == 0 )
    		a = 1 ;
    	if( b == 0 )
    		b = 1 ;
    	ResetShape();
    	ShapePoints[0] = eVect( rx, 		ry, 		0 );
    	ShapePoints[1] = eVect( rx + a , 	ry, 		0 );
    	ShapePoints[2] = eVect( rx + a , 	ry + b , 	0 );
    	ShapePoints[3] = eVect( rx, 		ry + b , 	0 );
    	Ns = 4 ;
    	return true;
    }
    function bool PutPreset_Circle( out int Ns )
    {
    	local int i , Ofs , Sides , SidesUsed , iPush , iPartial ;
    	local float rx, ry , r ;
    	Sides 		= int( PresetMetrics[0] ) ;
    	SidesUsed	= int( PresetMetrics[1] ) ;
    	if( Sides == 0 )
    		Sides = 8 ; // set default
    	if( SidesUsed == 0 || SidesUsed >= Sides )
    		SidesUsed = Sides ; // set to complete circle
    	else
    		iPartial = 1 ; // set to partial
    	rx			= PresetMetrics[2] ;
    	ry			= PresetMetrics[3] ;
    	Ofs			= int( bool ( PresetMetrics[4] ) ) ;
    	r = 1 ;
    	if( Ofs == 1 )
    		r /= cos(pi/Sides);
    	if( !ClosedPath && SidesUsed > Sides/2 && SidesUsed != Sides )
    	{
    		BadParameters("Partial circle may not be concave unless the path is looped.");
    		return false ;
    	}
    	ResetShape();
    	if( !( SidesUsed == Sides || SidesUsed == Sides/2 ) )
    	{
    		// if neither full circle nor half circle, centre is needed
    		iPush = 1 ;
    		ShapePoints[0] = ZeroVect ;
    	}
    	for( i = 0 ; i < SidesUsed + iPartial ; i++ )
    	{
    		ShapePoints[i + iPush ].x = rx + r * cos( (2*i + Ofs)*pi / Sides ) ;
    		ShapePoints[i + iPush ].y = ry + r * sin( (2*i + Ofs)*pi / Sides ) ;
    	}
    	Ns = SidesUsed + iPartial + iPush ;
    	return true;
    }
    function ResetShape()
    {
    	local int i;
    	for( i=0 ; i < ArraySize ; i++ )
    		ShapePoints[i] = vect(0,0,0);
    	return;
    }
    // ---------------------------------------------------------- vertex builders
    function Vertex3fC( float a , float b , float c , coords Axes )
    {
    	Vertexv( (Axes.Origin) +a*(Axes.XAxis) +b*(Axes.YAxis) + c* (Axes.ZAxis) );
    }
    // ---------------------------------------------------------- function Sgn( x )

    function float Sgn( float theValue )
    {
    if( theValue == 0 )
    	return 0;
    return theValue / Abs(theValue);
    }
    // ---------------------------------------------------------- function modulo
    static final operator(18) float mod  ( float A, float B )
    {
    	if( A % B >= 0 )
    		return A % B ;
    	else
    		return ( A % B ) + B ;
    }
    // ---------------------------------------------------------- function Acos( x )
    function float Acos( float x )
    {
    	// x = cos( Alpha )  ,  returns 0 <= cos^-1 <= pi
    	if( Abs( x ) > 1 )
    	{
    		log("error in Acos");
    		return 0 ;
    	}
    	if( x == 0 )
    		return pi/2 ;
    	if( x < 0 )
    		return pi - Atan( Sqrt( 1 - Square( x ) ) / -x ) ; // feed -x into acos formula, but irrelevant inside Square!
    	if( x == -1 )
    		return pi ;
    	return Atan( Sqrt( 1 - Square( x ) ) / x );
    }
    // ---------------------------------------------------------- square trigs
    function float sin2( float Alpha )
    {
    	return Square( sin ( Alpha ));
    }
    function float cos2( float Alpha )
    {
    	return Square( cos ( Alpha ));
    }
    // ---------------------------------------------------------- angle unit conversions
    function float Rad2Deg( float theAngle )
    {
    	return theAngle * 180.0f / pi ;
    }
    function float Deg2Rad( float theAngle )
    {
    	return theAngle * pi / 180.0f ;
    }
    // ----------------------------------------------------------
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tweedlet DressTube version 1.1
    // DRESS TUBE and dependencies: builds polys in a tube, option Complete, option Looped
    // requires Coplanar tweedlet to be present
    // NB levels is number of HOOPS, ie vertex rings, NOT panel rings
    // this tweedlet by tarquin 3/3/01
    function bool SafePoly4i( int Direction , int p0 , int p1 , int p2 , int p3 ,
    		optional name ItemName, optional int PolyFlags )
    {
    	if( isCoplanar4v(getVertex(p0),getVertex(p1),getVertex(p2),getVertex(p3)) )
    		Poly4i( Direction , p0 , p1 , p2 , p3 , ItemName, PolyFlags );
    	else
    	{
    		Poly3i( Direction , p0 , p1 , p2 , ItemName, PolyFlags );
    		Poly3i( Direction , p2 , p3 , p0 , ItemName, PolyFlags );
    	}
    }
    function DressTube( int Ofs , int LevelOfs , int Panels, int Levels ,
    	bool Looped , bool Closed , bool Triangulated , int sgn ,
    	optional name ItemName , optional int PolyFlags )
    {
    	// NB Levels is the number of vertex rings rather than number of poly stages.
    	// Sides is the number of poly panels required.
    	local int i , j , iPartial , iNonLooped , TrackJump  ;
    	iPartial = int( ! Closed ) ;
    	iNonLooped = int ( ! Looped );
    	TrackJump = Panels + iPartial + LevelOfs ;//closed: points=spaces, open: points = spaces+1
    	for( j = 0 ; j < Levels - iNonLooped ; j++ )
    	{
    		for( i = 0 ; i < Panels ; i++ )
    			if( !Triangulated )
    			{
    				SafePoly4i( sgn ,
    					Ofs + j*TrackJump 						+ ( (i + 1)%(Panels+iPartial) ) ,
    					Ofs + ( ( j+1 ) % Levels )*TrackJump 	+ ( (i + 1)%(Panels+iPartial) ) ,
    					Ofs + ( ( j+1 ) % Levels )*TrackJump 	+ i ,
    					Ofs + j*TrackJump 						+ i ,
    					ItemName , PolyFlags );
    			}
    			else
    			{
    				Poly3i( sgn ,
    					Ofs + j*TrackJump						+ ( (i + 1)%(Panels+iPartial) ) ,
    					Ofs + ( ( j+1 ) % Levels )*TrackJump 	+ ( (i + 1)%(Panels+iPartial) ) ,
    					Ofs + j*TrackJump 								+ i ,
    					ItemName , PolyFlags );
    				Poly3i( sgn ,
    					Ofs + ( ( j+1 ) % Levels )*TrackJump 	+ ( (i + 1)%(Panels+iPartial) ) ,
    					Ofs + ( ( j+1 ) % Levels )*TrackJump 	+ i ,
    					Ofs + j*TrackJump 						+ i ,
    					ItemName , PolyFlags );
    			}
    	}
    	return;
    }
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ end tweedlet
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PolyLogging tweedlet. Tarquin 3/3/01
    function LogPoly4i( int Direction , int p0 , int p1 , int p2 , int p3 ,
    		optional name ItemName, optional int PolyFlags )
    {
    	Poly4i( Direction , p0 , p1 , p2 , p3 , ItemName, PolyFlags );
    	log("poly:" $ String(p0) $","$ String(p1) $","$ String(p2) $","$ String(p3) );
    }
    function LogPoly3i( int Direction , int p0 , int p1 , int p2 ,
    		optional name ItemName, optional int PolyFlags )
    {
    	Poly3i( Direction , p0 , p1 , p2 , ItemName, PolyFlags );
    	log("poly:" $ String(p0) $","$ String(p1) $","$ String(p2) );
    }
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ end tweedlet
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ isCoplanar tweedlet. tarquin 3/3/01
    function bool isCoplanar4v( vector r0, vector r1, vector r2, vector r3 )
    {
    	local vector a, b, c ;
    	a = r1 - r0 ;
    	b = r2 - r0 ;
    	c = r3 - r0 ;
    	if( ( a cross b ) dot c == 0 )
    		return true;
    }
    function bool isCoplanar4i( int i0, int i1, int i2, int i3 )
    {
    	return isCoplanar4v(getVertex(i0),getVertex(i1),getVertex(i2),getVertex(i3));
    }
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ end tweedlet
    // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ wacky temp: builds a pointy arrow // newer ver than Rotator class
    function BuildPointy( coords theBasis )
    {
    	local int n , i , j ;
    	local float s ;
    	n = GetVertexCount();
    	s = ShapeFactor ; // accessing outside. ooh, la! bad OO programming!
    	//theBasis.Xaxis *= 128.0f ;
    	//theBasis.Yaxis *= 128.0f ;
    	//theBasis.Zaxis *=  128.0f ;

    	// Vertexv( ZeroVect ); BAD BAD BAD!!!! zero WITHIN the basis!
    	//Vertex3fC( float a , float b , float c , coords Axes )
    	Vertex3fC( 0,0,0, theBasis );
    	for( j = -1 ; j<2 ; j+= 2 )
    	{
    		for( i = -3 ; i<=3 ; i+= 2 )
    			Vertex3fC( i*s , j*s , 0 , theBasis );
    		for( i = -1 ; i<=1 ; i+= 2 )
    			Vertex3fC( i*s , j*3*s , 0 , theBasis );
    		Vertex3fC( 3*s , j*2.5*s , 0 , theBasis ); // X arrow #7,14
    	}
    	Vertex3fC( 0 , 5*s , 0 , theBasis ); // tip Y #15
    	Vertex3fC( 9*s ,0 , 0 , theBasis ); // tip X #16

    	Poly5i( 1 , n+0 , n+9 , n+8 , n+1 , n+2 , 'negX' , SheetPolyFlags );
    	Poly5i( 1 , n+0 , n+2 , n+5 , n+6 , n+3 , 'negY' , SheetPolyFlags );
    	Poly5i( 1 , n+0 , n+3 , n+4 , n+11 , n+10 , 'posX' , SheetPolyFlags );
    	Poly5i( 1 , n+0 , n+10 , n+13 , n+12 , n+9 , 'posY' , SheetPolyFlags );

    	Poly3i( 1 , n+12 , n+13 , n+15 , 'tipY' , SheetPolyFlags );
    	Poly5i( 1 , n+4 , n+7 , n+16 , n+14 , n+11 , 'tipX' , SheetPolyFlags );
    	return;
    }
    function Poly5i( int Direction , int p0 , int p1 , int p2 , int p3 , int p4 ,
    		optional name ItemName, optional int PolyFlags )
    {
    	PolyBegin( Direction , ItemName , PolyFlags );
    			Polyi( p0 );
    			Polyi( p1 );
    			Polyi( p2 );
    			Polyi( p3 );
    			Polyi( p4 );
    	PolyEnd();
    	return;
    }
    // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    // ****************************************************** COMMAND MODULE
    function ECommandReturn CommandModule()
    {
    	local int i ;
    	/*
    	CR_Stop, 		: finish with no build
    	CR_BuildStop,	: finish with build
    	CR_Continue		: continue with main brush code
    	*/
    	if( _COMMAND == C_ResetShape )
    	{
    		ResetShape() ;
    		_COMMAND = C_None ; // reset module
    		return CR_Stop ;
    	}
    	if( _COMMAND == C_ResetPath )
    	{
    		for( i=0 ; i<ArraySize ; i++ )
    			PathPoints[i] = vect(0,0,0);
    		_COMMAND = C_None ; // reset module
    		return CR_Stop ;
    	}
    	if( _COMMAND == C_ShowJoints )
    	{
    		LevelsOnly = true ;
    		// leave the module at this option
    		return CR_Continue ; // continue with brush build as normal
    	}
    	if( _COMMAND == C_ScaleShape )
    	{
    		if( _Parameters[0] != 0 )
    			for( i=0 ; i<ArraySize ; i++ )
    				ShapePoints[i].x *= _Parameters[0] ;
    		if( _Parameters[1] != 0 )
    			for( i=0 ; i<ArraySize ; i++ )
    				ShapePoints[i].y *= _Parameters[1] ;
    		/*
    		if( PresetMetrics[2] != 0 )
    			for( i=0 ; i<ArraySize ; i++ )
    				ShapePoints[i].z *= _Parameters[2] ;
    		*/
    		CommandShowShape( NumNonZeroElements( ShapePoints ) ) ; // show the shape
    		_COMMAND = C_None ; // reset module
    		return CR_BuildStop ;
    	}
    	if( _COMMAND == C_MoveShape )
    	{
    		for( i=0 ; i<ArraySize ; i++ )
    		{
    			ShapePoints[i].x += _Parameters[0] ;
    			ShapePoints[i].y += _Parameters[1] ;
    		}
    		CommandShowShape( NumNonZeroElements( ShapePoints ) ) ; // show the shape
    		_COMMAND = C_None ; // reset module
    		return CR_BuildStop ;
    	}
    	if( _COMMAND == C_RotShape )
    	{
    		for( i=0 ; i<ArraySize ; i++ )
    			ShapePoints[i] = eVect(
    				ShapePoints[i].x * cos( Deg2Rad( _Parameters[0] ) )
    					- ShapePoints[i].y * sin( Deg2Rad( _Parameters[0] ) ) ,
    				ShapePoints[i].x * sin( Deg2Rad( _Parameters[0] ) )
    					+ ShapePoints[i].y * cos( Deg2Rad( _Parameters[0] ) ) ,
    				0 ) ;
    		CommandShowShape( NumNonZeroElements( ShapePoints ) ) ; // show the shape
    		_COMMAND = C_None ; // reset module
    		return CR_BuildStop ;
    	}
    	if( _COMMAND == C_ConvPath )
    	{
    		if( PathSource == SO_Vect_Rel )
    		{
    			// note: first point is considered absolute
    			// further points are relative successively
    			for( i = 1 ; i < NumNonZeroElements(PathPoints) ; i++ )
    				PathPoints[i] = PathPoints[i] + PathPoints[i-1] ;
    			PathSource = SO_Vect_Abs ;
    		}
    		else if( PathSource == SO_Vect_Abs )
    		{
    			// note: first point is considered absolute
    			// further points are relative successively
    			for( i = NumNonZeroElements(PathPoints) - 1 ; i > 0 ; i-- )
    				PathPoints[i] = PathPoints[i] - PathPoints[i-1] ;
    			// note we go backwards to avoid overwriting what we need to read
    			PathSource = SO_Vect_Rel ;
    		}
    		else
    			log("C_ConvertPath did not understand the value of PathSource");
    		_COMMAND = C_None ; // reset module
    		return CR_Stop ;
    	}
    	if( _COMMAND == C_MakeArc )
    	{
    		_COMMAND = C_None ; // reset module
    		return CommandMakeArc(); // pass up the return value
    	}
    	if( _COMMAND == C_MakeMirror )
    	{
    		CommandMakeMirror() ;
    		_COMMAND = C_None ; // reset module
    		return CR_Continue ;
    	}

    	// just in case....
    	BadParameters("command option not recognized");
    	return CR_Stop ;
    }
    function CommandShowShape( int Ns )
    {
    	local int i ;
    	BeginBrush( false , GroupName );
    	PolyBegin( 1 , 'Sheet' , SheetPolyFlags );
    		for( i=0 ; i< Ns ; i++ )
    		{
    			VertexV( ShapeFactor * ShapePoints[i] );
    			Polyi( i );
    		}
    	PolyEnd();
    	return ;
    }
    function ECommandReturn CommandMakeArc()
    {
    	local vector StartRadius , EndRadius , Origin , StartApothem, EndApothem ;
    	local int n , i , Sides, Ofs ;
    	local float Alpha  ;
    	// _Parameters[0] = Sides
    	// _Parameters[1] = SidesUsed
    	// _Parameters[2] = Align
    	n = NumNonZeroElements(PathPoints) ;
    	if( n < 3 )
    	{
    		BadParameters("Must have at least 3 path points to define an arc.");
    		return CR_Stop ;
    	}
    	if( _Parameters[0] != 0 )
    		Sides = _Parameters[0] ;
    	else
    		Sides = 8 ;
    	Alpha = 2*pi / Sides ;
    	if( _Parameters[1] == 0 )
    	{
    		BadParameters("MakeArc command: set the value of _Parameters[1] to the number of sides.");
    		return CR_Stop ;
    	}
    	// *** make radii vectors
    	if( PathSource == SO_Vect_Rel )
    	{
    		StartRadius = -PathPoints[ n-2 ] ;
    		EndRadius	= PathPoints[ n-1 ] ;
    	}
    	else if( PathSource == SO_Vect_Abs )
    	{
    		StartRadius	= PathPoints[ n-3 ] - PathPoints[ n-2 ] ;
    		EndRadius	= PathPoints[ n-1 ] - PathPoints[ n-2 ] ;
    	}
    	else return CR_Stop ; // in case of future expansion of pathsource

    	Origin = PathPoints[ n-2 ] ;
    	// deal with offset
    	if( _Parameters[2] != 0 )
    	{
    		Ofs = 1 ;
    		StartApothem 	= StartRadius ;
    		EndApothem 		= EndRadius ;
    		StartRadius 	/= cos(pi/Sides) ;
    		EndRadius 		/= cos(pi/Sides) ;
    	}
    	for( i=1 ; i < _Parameters[1] + 1 ; i++ )
    	{
    		PathPoints[ n-3 + i ] = StartRadius*cos(i*Alpha - Ofs*Alpha*0.5)
    					+ EndRadius*sin(i*Alpha - Ofs*Alpha*0.5)
    					+ Origin ;
    		log("i:" $ i $ ", pp: " $ String(n-3+i) );
    	}
    	if( ofs == 1 )
    		PathPoints[ n-3 + i ] = StartApothem*cos( (i-1)*Alpha )
    					+ EndApothem*sin((i-1)*Alpha )
    					+ Origin ;

    	return CR_Continue ;
    }
    function CommandMakeMirror()
    {
    	local int i , n , Ofs ;
    	local vector MirrorNormal ;
    	n 				= NumNonZeroElements(PathPoints) ;
    	// check n large enough perhaps? should be at least 3
    	if( PathSource == SO_Vect_Rel )
    		MirrorNormal 	= Normal( PathPoints[n-1] ) ;
    	else
    		MirrorNormal	= Normal( PathPoints[n-1] - PathPoints[n-2] );
    	// apply parameters
    	if( _Parameters[0] == 0 )
    		Ofs = 1 ;
    	else if( _Parameters[0] == 2 )
    		{
    			if( PathSource == SO_Vect_Rel )
    				PathPoints[n-1] *= 2 ; // rel case
    			else
    				PathPoints[n-1] += PathPoints[n-1] - PathPoints[n-2] ;
    		}

    	// odd case first. last joint goes thru the mirror, is the normal.
    	// whether odd or even, we write the same # of points.
    	for( i=0 ; i<n-2 ; i++)
    	{
    		PathPoints[n+i-Ofs] = 2*(PathPoints[n-2-i] dot MirrorNormal)*MirrorNormal - PathPoints[n-2-i] ;
    		log("writing PP:" $ String(n+i) );
    	}
    	if( PathSource == SO_Vect_Abs )
    	{
    		for( i=n-Ofs ; i < n + n - 3 - Ofs ; i++ )
    			PathPoints[i] = PathPoints[i-1] + PathPoints[i] - PathPoints[i+1] ;
    		// note that this loop above ^^ may not execute at all if n=3 -- this is desired effect.
    		PathPoints[n+n-3-Ofs] = PathPoints[n+n-4-Ofs] + PathPoints[n+n-3-Ofs] ;
    	}
    	return ;
    }
    // ****************************************************** shape direction // ##arraysize##
    function int GetShapeDirection(int Ns)
    {
    	local vector RelativePoints[16];
    	local rotator VectorAngles[16] ;
    	local float RelativeAngles[16] , AngleSum ;
    	local int i ;

    	for( i=1 ; i< Ns+1 ; i++ )
    	{
    		RelativePoints[i%Ns] = ShapePoints[i%Ns] - ShapePoints[i-1] ;
    		log("v[" $ i $ "]=" $ RelativePoints[i%Ns] );
    	}
    	for( i=0 ; i< Ns ; i++ )
    	{
    		VectorAngles[i] = rotator( RelativePoints[i] );
    		log("abs angles: " $ VectorAngles[i] );
    	}
    	for( i=0 ; i< Ns ; i++ )
    	{
    		RelativeAngles[i] = VectorAngles[i].Yaw - VectorAngles[(i-1) mod Ns ].Yaw ;
    		RelativeAngles[i] = RelativeAngles[i] mod UUCircle ;
    		if( RelativeAngles[i] >= UUCircle /2 )
    			RelativeAngles[i] -= UUCircle ; // note a 180 turn will never occur anyway
    		log("rel angles: " $ RelativeAngles[i] );
    	}
    	// !!! check concave
    	for( i=1 ; i< Ns ; i++ )
    	{
    		if( sgn( RelativeAngles[i-1] ) != sgn( RelativeAngles[i] ) )
    			BadParameters("Your current shape is concave. " $
    				"This may cause problems with an extruded non-looped brush or " $
    				"a disc made with the ShowShape command. ");
    	}
    	for( i=0 ; i< Ns ; i++ )
    	{
    	AngleSum += RelativeAngles[i] ;
    	}
    	log("Angle Sum: " $ AngleSum );
    	log("Angle Sum mod: " $ String( AngleSum % UUCircle ) );
    	return int( sgn(AngleSum) ) ;
    }
    // ****************************************************** array sizer // ##arraysize##
    function int NumNonZeroElements( vector VectorArray[16] )
    {
    	// strictly speaking, will never return zero. but 0,1,2 is bad anyway!
    	local int i ;
    	for( i = 1 ; i < ArraySize ; i++ )
    		if( VectorArray[i] == vect( 0 , 0 , 0 ) )
    			return  i ;
    	return ArraySize ;
    }
    // ****************************************************** ThetaPhi
    function GetAngles( out float Theta, out float Phi, coords theBasis , vector vectorW )
    {
    	local vector Wflat ;
    	vectorW = Normal( vectorW ) ;
    	if( vectorW dot theBasis.Zaxis == -1 )
    		log("vector dips directly down: bad!");
    	Theta = Acos( vectorW dot theBasis.Zaxis );

    	Wflat = ProjectToPlane( vectorW ,  theBasis.Zaxis , defPlaneVF( theBasis.Zaxis , 0 ) );

    	// !!!!!!!!!!!!!! Wflat is in Standard basis -- is this prob?

    	// catch the special cas that Wproj = 0vector , in this case define Phi=0
    	// this happens if W is parallel to z axis
    	if( Theta == 0 )
    		Phi = 0 ; // originally Wflat == ZeroVect but roundoff errors leave cases uncaught
    	/*
    	else if( Wflat dot theBasis.Xaxis == 0 )
    	{
    		// catches phi = 0 or 180 XXX -- is this still necessary?
    		Phi = Sgn( Wflat dot theBasis.Yaxis ) * pi /2 ;
    		log("Phi sgn:" $ Normal(Wflat) dot vect(0,1,0) );
    	}
    	*/
    	else
    	{
    		Phi = Acos( Normal(Wflat) dot theBasis.Xaxis );
    		if( Normal(Wflat) dot theBasis.Yaxis < 0 )
    			Phi = -Phi ; // handles creation of negative Phi
    	}
    	log("W=" $ vectorW );
    	log( "Wflat=" $ Wflat );
    	log("[W]b =" 	$ String( vectorW dot theBasis.Xaxis ) $ ","
    					$ String( vectorW dot theBasis.Yaxis ) $ ","
    					$ String( vectorW dot theBasis.Zaxis ) );
    	log("[Wf]b =" 	$ String( Wflat dot theBasis.Xaxis ) $ ","
    					$ String( Wflat dot theBasis.Yaxis ) $ ","
    					$ String( Wflat dot theBasis.Zaxis ) );
    	log( "Wflat dot Xaxis=" $ String( Wflat dot theBasis.Xaxis ) );
    	log("Theta,Phi:" $ Rad2Deg(Theta) $ ", " $ Rad2Deg(Phi) );
    	return;
    }
    // ****************************************************** tangential build vertices ##arraysize##
    function BuildTangential( vector vectorS[16], int Ns , vector vectorP[16] , int Np , bool Looped )
    {
    	// cover open case first, then modify for looped!
    	local int i , j ;
    	local float Theta, Phi ;
    	local vector segmentL[16] ; // ##arraysize##
    	local plane CrossSection[16] , Incident[16] ; // ##arraysize##
    	local coords StandardBasis , Basis[16] , ProjBasis[16] ; // ##arraysize##
    	local quat QuaternionRotation ;
    	local matrix3x3 RotationMatrix ;

    	StandardBasis =  defCoords( ZeroVect , vect(1,0,0) , vect(0,1,0) , vect(0,0,1) );
    	// ----------------------------------------------------------- factor
    	for( i = 0 ; i<Np ; i++ )
    		vectorP[i]	*= PathFactor ;
    	for( i = 0 ; i<Ns ; i++ )
    		vectorS[i]	*= ShapeFactor; // * ( FRand() / 2 + 0.5 ) ;

    	// ----------------------------------------------------------- define segments
    	if( PathSource == SO_Vect_Rel )
    	{
    		// for relative vectorss: remap the path points
    		// note: first point is considered absolute
    		// further points are relative successively
    		for( i = 1 ; i<Np ; i++ )
    			vectorP[i] = vectorP[i] + vectorP[i-1] ;
    	}
    	for( i = 0 ; i<Np ; i++ )
    	{
    		// note we only need up to Np-1 in open case, but may as well build it whatever.
    		segmentL[i]= vectorP[(i+1) % Np] - vectorP[i] ;
    		CrossSection[i] = defPlanevf( segmentL[i] , 0 ) ; // the call will normalize
    	}
    	// ----------------------------------------------------------- define incidence planes
    	for( i = 0 ; i<Np ; i++ )
    	{
    		// IncPerp = Normal( segmentL[(i-1)%Np] ) + Normal( segmentL[i] ) ;
    		Incident[i] = defPlanevf( Normal( segmentL[(i-1) mod Np] ) + Normal( segmentL[i] ),0 );
    		// these planes bisect the angle between each path segment.
    	}
    	// ----------------------------------------------------------- rotate

    	if( Maths == MA_Matrix )
    	{
    		// determine Theta, Phi for first basis:
    		GetAngles( Theta, Phi, StandardBasis , segmentL[0] ) ;
    		Basis[0] = DipBasisRelative( Theta, Phi , StandardBasis );

    		for( i = 1 ; i<Np ; i++ )
    		{
    			// determine Theta, Phi for this basis, relative to previous.
    			log("-------- basis " $ i );
    			GetAngles( Theta, Phi, Basis[i-1] , segmentL[i] );
    			// within basis i-1:
    			Basis[i] = DipBasis( Theta, Phi );
    			// basis[i] is now expressed as coordinates in basis i-1
    			Basis[i].Xaxis = changeBasis( Basis[i].Xaxis,Basis[i-1].Xaxis,Basis[i-1].Yaxis,Basis[i-1].Zaxis);
    			Basis[i].Yaxis = changeBasis( Basis[i].Yaxis,Basis[i-1].Xaxis,Basis[i-1].Yaxis,Basis[i-1].Zaxis);
    			Basis[i].Zaxis = changeBasis( Basis[i].Zaxis,Basis[i-1].Xaxis,Basis[i-1].Yaxis,Basis[i-1].Zaxis);
    			// basis[i] is now reinterpreted in standard basis.
    		}
    	}
    	else
    	{
    		// --------------------------------------- rotate QUAT
    		// dip of theta on an axis at phi
    		// ^^^^^^^^ first basis
    		GetAngles( Theta, Phi, StandardBasis , segmentL[0] ) ;
    		QuaternionRotation = RotationToQuat( vect(0,0,1) cross segmentL[0] , Theta );
    		Basis[0] = DipBasisRelative( Theta, Phi , StandardBasis );
    		// ^^^^^^^^ other bases
    		for( i=1 ; i<Np ; i++ )
    		{
    			if( ExtrudeType == EX_Tangential )
    			{
    				GetAngles( Theta, Phi, Basis[i-1] , segmentL[i] );
    				// Axis = segmentL[i-1]	 cross segmentL[i] ;
    				QuaternionRotation = QuaternionRotation
    					* RotationToQuat( segmentL[i-1] cross segmentL[i]  , Theta ) ;
    				RotationMatrix = QuatToMatrix( QuaternionRotation ) ;
    			}
    			else if( ExtrudeType == EX_ThirdWay )
    			{
    				// get our dip from Joint0 rather than Joint[previous]
    				GetAngles( Theta, Phi, Basis[0] , segmentL[i] );
    				// Q = QuaternionRotation * RotationToQuat( segmentL[0] cross segmentL[i]  , Theta )
    				RotationMatrix = QuatToMatrix(
    					QuaternionRotation * RotationToQuat( segmentL[0] cross segmentL[i]  , Theta ) ) ;
    			}
    			Basis[i].Xaxis = RotationMatrix * StandardBasis.Xaxis ;
    			Basis[i].Yaxis = RotationMatrix * StandardBasis.Yaxis ;
    			Basis[i].Zaxis = RotationMatrix * StandardBasis.Zaxis ;
    		}
    	}
    	// ----------------------------------------------------------- project
    	for( i = 0 ; i<Np ; i++ )

    	{
    		//make ProjectToPlane( vector r , vector v , plane planePi )
    		// do the lot...
    		projBasis[i].Xaxis = ProjectToPlane( Basis[i].Xaxis , Basis[i].Zaxis , Incident[i] );
    		projBasis[i].Yaxis = ProjectToPlane( Basis[i].Yaxis , Basis[i].Zaxis , Incident[i] );
    		projBasis[i].Zaxis = Basis[i].Zaxis ; // line below is plain silly!
    		// projBasis[i].Zaxis = ProjectToPlane( Basis[i].Zaxis , Basis[i].Zaxis , Incident[i] );
    	}
    	if( !Looped )
    	{
    		projBasis[0] 			= Basis[0]; // doctor first PHI to same as PI -- dont project.
    		projBasis[Np-1]			= Basis[Np-2]; // WATCH THIS! look at picture: must be one PI back!
    		// ... and doctor the ends for nonLooped.
    		log("first and last proj bases set for non-loop: 0, " $ String(Np-1) );
    	}
    	// ----------------------------------------------------------- LOG
    	for( i = 0 ; i<Np ; i++ )
    	{
    		log("Basis X " 		$ i $ ": " $ String( Basis[i].Xaxis ) );
    		log("Proj  X " 	$ i $ ": " $ String( projBasis[i].Xaxis ) );
    	}
    	// ----------------------------------------------------------- offset
    	for( i = 0 ; i<Np ; i++ )
    		projBasis[i].Origin 	= vectorP[i] ;
    	// ----------------------------------------------------------- vertices
    	// projBasis has been set and if nonLooped, the first and last have been suitably doctored.
    	// make!
    	if( ShapeSource == SO_MrPointy )
    	{
    		for( i=0 ; i<Np ; i++ )
    		{
    			log("pointy:" $ String(projBasis[i].Xaxis) );
    			BuildPointy( projBasis[i] );
    		}
    	}
    	else
    	{
    		for( j = 0 ; j < Np-int(InvisibleFinal) ; j++ )
    		{
    			for( i = 0 ; i < Ns ; i++ )
    			{
    				if( j == 0 || ( ClosedPath && j == Np-int(InvisibleFinal) - 1 ) )
    					Vertex3fC(
    					vectorS[i].x * ( 1 + FRand() * RandomiserCaps.X ) ,
    					vectorS[i].y * ( 1 + FRand() * RandomiserCaps.Y ) ,
    					vectorS[i].z + ( FRand() * RandomiserCaps.Z ) *
    						( Vsize(segmentL[j])  ) ,
    					projBasis[j] );
    				else if( !ClosedPath && j == Np-int(InvisibleFinal) - 1 )
    					Vertex3fC(
    					vectorS[i].x * ( 1 + FRand() * RandomiserCaps.X ) ,
    					vectorS[i].y * ( 1 + FRand() * RandomiserCaps.Y ) ,
    					vectorS[i].z + ( FRand() * RandomiserCaps.Z ) *
    						( Vsize(segmentL[j-1])  ) ,
    					projBasis[j] );
    				else
    					Vertex3fC(
    					vectorS[i].x * ( 1 + FRand() * Randomiser.X ) ,
    					vectorS[i].y * ( 1 + FRand() * Randomiser.Y ) ,
    					vectorS[i].z + ( FRand() * Randomiser.Z * ( Vsize(segmentL[j-1]) + Vsize(segmentL[j]) )
    						 ) ,
    					projBasis[j] );
    			}
    		}
    	}
    	return;
    }
    // ****************************************************** build
    function bool Build()
    {
    	local int i, j, PolyDirection ;
    	local int NumShapePoints , NumPathPoints ;
    	local bool PresetOK ;
    	local ECommandReturn CommandReturn ;

    	LevelsOnly = false ;
    	// bad hack, but otherwise it remembers the value from one build to the next!

    	// ################## CALL COMMAND MODULE
    	if( _COMMAND != C_None && _COMMAND != C_ShowShape )
    	{
    		CommandReturn = CommandModule() ;
    		/*
    		CR_Stop, 		: finish with no build
    		CR_BuildStop,	: finish with build
    		CR_Continue		: continue with main brush code
    		*/
    		if( CommandReturn == CR_Stop )
    		{
    			// command module returns stop.
    			return false ;
    		}
    		if( CommandReturn == CR_BuildStop )
    		{
    			// command module has built something.
    			return EndBrush() ;
    		}
    		// command executed; we now continue with the brush
    	}

    	// ################## PRESETS
    	PresetOK = true ;
    	if( ShapeSource == SO_pSquare )
    		PresetOK = PutPreset_Square( NumShapePoints );
    	if( ShapeSource == SO_pCircle )
    		PresetOK = PutPreset_Circle( NumShapePoints );
    	if( !PresetOK )
    		return false; 	// stop here if the preset has been given bad metrics
    						// the preset has already called BadParams to explain.

    	// ################## count shape points
    	if( ShapeSource == SO_Vectors )
    		NumShapePoints 	= NumNonZeroElements( ShapePoints ) ; // presets set Ns

    	// ################## CALL COMMAND MODULE post
    	if( _COMMAND == C_ShowShape )
    	{
    		if( ShapeSource == SO_MrPointy )
    			return BadParameters("MrPointy doesn't like being revealed!" $
    				"Choose another shape source.");
    		// bad presets have already been caught.
    		// bad vector shapes are the user's prerogative
    		CommandShowShape( NumShapePoints ) ;
    		return EndBrush() ;
    	}

    	// ################## count path points
    	NumPathPoints 	= NumNonZeroElements( PathPoints ) ;

    	log("Num Shape=" $ String( NumShapePoints ) );
    	log("Num Path=" $ String( NumPathPoints ) );

    	// ################## check params
    	if( ShapeSource != SO_MrPointy )
    	{
    		// note: sheet condition can be factored in by using value of
    		// int(SheetPolys) as a modifier of the inequality
    		if( SheetPolys && NumShapePoints < 2 )
    			return BadParameters("A sheet extrusion must have at least 2 shape points.");
    		if( !SheetPolys && NumShapePoints < 3 )
    			return BadParameters("Must have at least 3 shape points.");
    	}
    	if ( ClosedPath )
    	{
    		if( NumPathPoints < 3 )
    			return BadParameters("A closed path must have at least 3 path points.");
    		// invisible final check no also looped:
    		if( InvisibleFinal )
    			return BadParameters("A closed path can't have Invisible Final option set.");
    	}
    	else
    	{
    		// factor in the InvisibleFinal, since if true we have one less effective path point
    		if( NumPathPoints < 2 + int(InvisibleFinal)  )
    			return BadParameters("Must have at least 2 path points.");
    	}

    	// check no points are identical in each array.
    	for( i = 0 ; i < NumShapePoints ; i++ )
    		for( j = 0 ; j < NumShapePoints ; j++ )
    			if( ShapePoints[i] == ShapePoints[j] && i != j )
    				return BadParameters("Shape points must be distinct.");
    	if( PathSource != SO_Vect_Rel )
    		for( i = 0 ; i < NumPathPoints ; i++ )
    			for( j = 0 ; j < NumPathPoints ; j++ )
    				if( PathPoints[i] == PathPoints[j] && i != j )
    					return BadParameters("Path points must be distinct.");

    	// ****************************************************** get shape direction
    	// only run this if user-entered vectors are used
    	if( ShapeSource == SO_Vectors )
    		PolyDirection = GetShapeDirection( NumShapePoints );
    	else
    		PolyDirection = 1 ;
    	// PolyDirection = ( -2 * int( ReversePolys ) ) + 1 ; // old system

    	/*
    	we proceed in hoops.
    	take O to be anchor point of shape.
    	*/

    	BeginBrush( MergeCoplanars , GroupName );
    	// ****************************************************** vertices
    	if( ExtrudeType == EX_Translational )
    	{
    		for( j = 0 ; j < NumPathPoints ; j++ )
    			for( i = 0 ; i < NumShapePoints ; i++ )
    				VertexV( ( ShapeFactor * ShapePoints[i] ) + ( PathFactor * PathPoints[j] ) );
    	}
    	else if( ExtrudeType == EX_Tangential || ExtrudeType == EX_ThirdWay )
    	{
    		BuildTangential( ShapePoints , NumShapePoints , PathPoints , NumPathPoints , ClosedPath );
    	}
    	// ****************************************************** polys
    	/*
    	NOTE: we have no idea which way round its facing! quick check may be needed
    	points: i + j , i + j + 1 , i + NumShapePoints + j + 1 ,  i + NumShapePoints + j
    	*/
    	// InvisibleFinal:
    	// Bool to Int: Converts True to 1; False to 0.
    	if( ShapeSource != SO_MrPointy && LevelsOnly == False )
    	{
    		if( SheetPolys )
    			DressTube( 0 , 0 ,  NumShapePoints , NumPathPoints-int(InvisibleFinal) ,
    				ClosedPath , true , false , PolyDirection , 'sides', SheetPolyFlags );
    		else
    			DressTube( 0 , 0 ,  NumShapePoints , NumPathPoints-int(InvisibleFinal) ,
    				ClosedPath , true , false , PolyDirection , 'sides' );
    		// ----------- cap
    		if( !ClosedPath && !SheetPolys )
    		{
    			for( j = 0 ; j < 2 ; j++ )
    			{
    				PolyBegin( (2*j - 1)*PolyDirection , 'cap' );
    				for( i = 0 ; i < NumShapePoints ; i++ )
    					Polyi( j*(NumPathPoints-1-int(InvisibleFinal))*NumShapePoints + i );
    				PolyEnd();
    			}
    		}
    	}
    	if( ShapeSource != SO_MrPointy && LevelsOnly == true )
    	{
    		for( j=0 ; j < NumPathPoints ; j++ )
    		{
    			PolyBegin( 1 , 'level' , SheetPolyFlags );
    			for( i=0 ; i < NumShapePoints ; i++ )
    			{
    				Polyi( j*NumShapePoints + i );
    			}
    			PolyEnd();
    		}
    	}
    	log("vertices built:" $ String(getvertexcount() ));
    	return EndBrush();
    }
    // (c) 28/5/2001 Tarquin (tarquindarkling@bigfoot.com)
    // I assert that I am the author and copyright holder of this code, and retain full ownership rights of this code
    // you may NOT re-use or alter or recompile this code without my express permission.

    defaultproperties
    {
         ShapeFactor=64.000000
         PathFactor=64.000000
         MergeCoplanars=True
         GroupName=Extrusion
         BitmapFilename="SFEDBBTarquinExtrude"
         ToolTip="Extrusion Builder"
    }

class file time: 12/7/2003 3:58:26 PM - creation time: 12/7/2003 4:03:46 PM
Created with UnCodeX