# SpatialED.TarquinTorusBuilder

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 |
    //=============================================================================
    // Spatial Fear
    // Class: TarquinPanoramaBuilder
    // Description: Builds a 3D torus brush.  Compatible with cylinder of same
    //				size in both axes.
    //
    // Author: Tarquin
    // + Used with permission for packaging with SpatialED v2.1
    //=============================================================================
    class TarquinTorusBuilder
    	expands BrushBuilder;
    var() float OuterRadius, TubeThickness;
    var() int WheelSides, WheelSidesUsed, TubeSides;
    enum EAlignStyle
    {
    	AL_Plain,
    	AL_Side,
    	AL_Caps,
    };
    var() EAlignStyle WheelAlignType;
    var() bool TubeAlignToSide;
    var() name GroupName;

    /*
    var() enum ItemNamingType
    {
    	BySpoke,
    	BySlice,
    } ItemNaming;
    */

    function bool Build()
    {
    	local int i, j, TubeOfs, WheelOfs, XWheelSidesUsed, WheelPartial;
    	local float CentrumRadius, TubeRadius, TubeSquishRadius, CACentrumRadius;

    	if( TubeSides<3 || WheelSides<3 )
    		return BadParameters("The tube and wheel must have at least three sides");
    	if( OuterRadius<=0 || TubeThickness <= 0 )
    		return BadParameters("Radius and thickness must be positive");
    	if( TubeThickness >= OuterRadius )
    		return BadParameters("Thickness must be smaller than Outer Radius");

    /* internal nomenclature:
    w, t: wheel and tube sides
    p: partial sides required
    m: centrum radius
    b: tube radius
    q: tube squish radius
    */

    // define some new variables
    // leave user entered data intact
    	if( WheelSidesUsed <= 0 || WheelSidesUsed >= WheelSides )
    		XWheelSidesUsed = WheelSides ;
    	else
    		XWheelSidesUsed = WheelSidesUsed ;

    	WheelPartial 		= XWheelSidesUsed ;

    	TubeRadius 		= TubeThickness / 2 ;
    	TubeSquishRadius 	= TubeRadius ;
    	CentrumRadius 		= OuterRadius - TubeRadius ;
    	CACentrumRadius 	= CentrumRadius ;

    /*
     The TubeSquishRadius
    This is EXTREMELY important -- big trip up here
    The wheel offset is basically a transformation of the x-y plane
    Whether Tube is ofset or not, we are now asking the hoops
    to be non-regular -- with w=4 , they lie on the diagonals of the square,
    and with t=4 too for clarity it can easily be seen that the hoop is no longer a square
    The z component must NOT be affected by wheel trans, hence the (annoying!) extra variable
    This one was a headache and a half!
    */

    if (TubeAlignToSide)
    {
    	TubeOfs = 1 ;
    	TubeRadius /= cos( pi / TubeSides ) ;
    	TubeSquishRadius /= cos( pi / TubeSides ) ;
    }
    if ( WheelAlignType !=  AL_PLain )
    {
    	// old  WheelAlignToSide = true
    	WheelOfs = 1 ;
    	TubeSquishRadius /= cos( pi / WheelSides ) ;
    	CentrumRadius /= cos( pi / WheelSides ) ;
    }
    if ( XWheelSidesUsed == WheelSides )
    {
    	WheelPartial-- ;
    /*
    this is a doctored value of sides used to make it work! (arggg!)
    basically, whether we want full wheel or full-1 wheel, we have the same
    number of vertices, we just skip the final bridge.
    for 1,2,3...,w-1 sections, we want 2,3,4...,w hoops
    for the full w sections we also want w hoops
    the +1 is included in the count for the vertex builder, this knocks it
    down in anticipation so we don't go to w+1
    this is only needed for the vertices, not the bridges.
    */
    }

    // make the brush!

    	BeginBrush( false, GroupName );

    if( ( WheelAlignType == AL_Caps ) && ( XWheelSidesUsed != WheelSides ) )
    {
    j = 0 ;
    log("wheel offset");
    log(WheelOfs);
    // log("j:");
    // log(j);
    // this takes the old value of CentrumRadius -- recalculate from user input
    for( i = 0 ; i < TubeSides ; i++ )
    			Vertex3f(
    	CACentrumRadius * cos( 2*j*pi / WheelSides )
    		+ TubeRadius
    			* cos( (2*i + TubeOfs)*pi/TubeSides )
    			* cos( 2*j*pi / WheelSides ) ,
    	CACentrumRadius * sin( 2*j*pi / WheelSides ) +
    		TubeRadius
    			* cos( (2*i + TubeOfs)*pi/TubeSides )
    			* sin( 2*j*pi / WheelSides ) ,
    	TubeRadius * sin( (2*i + TubeOfs)*pi/TubeSides )
    			 );
    // If we are in this loop, WheelPartial has NOT been doctored
    // a loop from 1 to WheelPartial
    for( j = 1 ;  j < WheelPartial + 1; j++ )
    	{
    		for( i = 0 ; i < TubeSides ; i++ )
    			Vertex3f(
    	CentrumRadius * cos( (2*j - WheelOfs)*pi / WheelSides )
    		+ TubeSquishRadius
    			* cos( (2*i + TubeOfs)*pi/TubeSides )
    			* cos( (2*j - WheelOfs)*pi / WheelSides ) ,
    	CentrumRadius * sin( (2*j - WheelOfs)*pi / WheelSides ) +
    		TubeSquishRadius
    			* cos( (2*i + TubeOfs)*pi/TubeSides )
    			* sin( (2*j - WheelOfs)*pi / WheelSides ) ,
    	TubeRadius * sin( (2*i + TubeOfs)*pi/TubeSides )
    			 );
    	}
    j = WheelPartial ;
    for( i = 0 ; i < TubeSides ; i++ )
    			Vertex3f(
    	CACentrumRadius * cos( 2*j*pi / WheelSides )
    		+ TubeRadius
    			* cos( (2*i + TubeOfs)*pi/TubeSides )
    			* cos( 2*j*pi / WheelSides ) ,
    	CACentrumRadius * sin( 2*j*pi / WheelSides ) +
    		TubeRadius
    			* cos( (2*i + TubeOfs)*pi/TubeSides )
    			* sin( 2*j*pi / WheelSides ) ,
    	TubeRadius * sin( (2*i + TubeOfs)*pi/TubeSides )
    			 );
    // now doctor wheelpartial for the polys
    WheelPartial++ ;
    }
    else
    {
    	// remember, TubeSquishRadius only differs from TubeRadius if WheelAlign
    	for( j = 0 ; j < WheelPartial + 1 ; j++ )
    		for( i = 0 ; i < TubeSides ; i++ )
    			Vertex3f(
    	CentrumRadius * cos( (2*j - WheelOfs)*pi / WheelSides )
    		+ TubeSquishRadius
    			* cos( (2*i + TubeOfs)*pi/TubeSides )
    			* cos( (2*j - WheelOfs)*pi / WheelSides ) ,
    	CentrumRadius * sin( (2*j - WheelOfs)*pi / WheelSides ) +
    		TubeSquishRadius
    			* cos( (2*i + TubeOfs)*pi/TubeSides )
    			* sin( (2*j - WheelOfs)*pi / WheelSides ) ,
    	TubeRadius * sin( (2*i + TubeOfs)*pi/TubeSides )
    			 );
    	WheelPartial = XWheelSidesUsed ;
    	// un-doctor this value
    }
    // build polys
    // the modulo enables wrapping at both ends.
    // interestingly, fiddle with it and you might be able to get a Klein bottle! Would look terrible though!

    if( XWheelSidesUsed == WheelSides - 1 )
    {
    	for( j = 0 ; j < WheelPartial ; j++ )
    		for( i = 0 ; i < TubeSides ; i++ )
    			Poly4i( -1 ,
    				j*TubeSides + i ,
    				j*TubeSides + (i+1)%TubeSides ,
    				( (j+1) )*TubeSides + ((i+1)%TubeSides) ,
    				( (j+1) )*TubeSides + i ,
    				'wall' );
    }
    else
    {
    	log("wheelPartial:");
    	log( WheelPartial );
    	for( j = 0 ; j < WheelPartial ; j++ )
    		for( i = 0 ; i < TubeSides ; i++ )
    			Poly4i( -1 ,
    				j*TubeSides + i ,
    				j*TubeSides + (i+1)%TubeSides ,
    				( (j+1)%WheelSides )*TubeSides + ((i+1)%TubeSides) ,
    				( (j+1)%WheelSides )*TubeSides + i ,
    				'wall' );
    }

    if( XWheelSidesUsed < WheelSides )
    {
    	PolyBegin( 1, 'cap' );
    		for( i=0; i<TubeSides; i++ )
    			Polyi( i );
    		PolyEnd();
    	PolyBegin( -1, 'cap' );
    		for( i=0; i<TubeSides; i++ )
    			Polyi( TubeSides*WheelPartial + i );
    		PolyEnd();
    	// uses doctored wheelpartial to catch the cap align case.
    }
    	return EndBrush();
    }

    defaultproperties
    {
         OuterRadius=256.000000
         TubeThickness=64.000000
         WheelSides=8
         WheelSidesUsed=8
         TubeSides=8
         TubeAlignToSide=True
         GroupName=Torus
         BitmapFilename="SFEDBBTarquinTorus"
         ToolTip="Torus Builder"
    }

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