# SpatialED.TarquinCylinderBuilder

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 |
    //=============================================================================
    // Spatial Fear
    // Class: TarquinCylinderBuilder
    // Description: Builds a complete or partial cylinder.
    //
    // Author: Tarquin
    // + Used with permission for packaging with SpatialED v2.1
    //=============================================================================
    class TarquinCylinderBuilder
    	expands BrushBuilder;

    var() float Height, OuterRadius, InnerRadius;
    var() int Sides, SidesUsed;
    var() bool Hollow, TriangulateCaps;
    enum EAlignStyle
    {
    	AL_Plain,
    	AL_Side,
    	AL_Caps,
    };
    var() EAlignStyle AlignType;
    var() name GroupName;

    function BuildCirclet( int sgn, EAlignStyle Align, int Sides, int Panels, float Height, float Radius, name Item, bool Complete )
    {
    	local int n,i,j,Ofs;
    	local float NonApothem;
    	n = GetVertexCount();
    	// ------------------------------------------------------------------------------------ build vertices
    	// NOTE: the old termination problem: split into cases, i<Panels or <Panels+1 depending
    	if( Complete )
    	{
    		// ---------------------------- case: standard
    		if( Align != AL_Plain )
    		{
    			// Radius is converted to apothem. For complete case, AL_Caps counts as AL_Side
    			Radius /= cos(pi/Sides);
    			Ofs = 1;
    		}
    		for( j = -1 ; j<2 ; j += 2 )
    			for( i=0 ; i < Panels ; i++ )
    				Vertex3f(
    					Radius * cos( (2*i + Ofs)*pi / Sides ) ,
    					Radius * sin( (2*i + Ofs)*pi / Sides ) ,
    					j * Height/2
    					);
    	}
    	else if( Align != AL_Caps )
    	{
    		// ---------------------------- case: partial, non-cap
    		if( Align == AL_Side )
    		{
    			// Radius is converted to apothem
    			Radius /= cos(pi/Sides);
    			Ofs = 1;
    		}
    		for( j = -1 ; j<2 ; j += 2 )
    			for( i=0 ; i < Panels + 1 ; i++ )
    				Vertex3f(
    					Radius * cos( (2*i + Ofs)*pi / Sides ) ,
    					Radius * sin( (2*i + Ofs)*pi / Sides ) ,
    					j * Height/2
    					);
    	}
    	else
    	{
    		// ---------------------------- case: partial, cap align
    		// note Panels reflects the ACTUAL number of panels, not user-set sides.
    		for( j = -1 ; j<2 ; j += 2 )
    		{
    			// this is a tricky case!
    			// user-entered radius is the apothem
    			// middle vertices have to be SLIPPED BACK, on true radius
    			// first and last have to be on USER radius
    			// last: CAUTION! it may be the i=p vertex, but in terms of angle,
    			// it sits at theta*sides. ie. i=s=p-1
    			NonApothem = Radius / cos(pi/Sides);
    			Vertex3f( Radius , 0 , j * Height/2 );//starter
    			for( i=1 ; i < Panels ; i++ )
    				Vertex3f(
    					NonApothem * cos( (2*i - 1)*pi / Sides ) ,
    					NonApothem * sin( (2*i - 1)*pi / Sides ) ,
    					j * Height/2
    					);
    			Vertex3f( 	Radius * cos( 2*(Panels-1)*pi / Sides ),
    						Radius * sin( 2*(Panels-1)*pi / Sides ),
    						j * Height/2 );
    		}
    	}
    	// ------------------------------------------------------------------------------------ side panel polys
    	// note to self: doh?!?! is it me, or is last and first case below == ? re-arrange perhaps?
    	if( Complete )
    		for( i = 0 ; i < Panels ; i++ )
    			Poly4i( sgn ,
    				n + i ,
    				n + (i+1)%Sides ,
    				n + Panels + ( (i+1)%Sides ) ,
    				n + Panels + i,
    				Item );
    	else if( Align == AL_Caps )
    		for( i = 0 ; i < Panels ; i++ )
    			Poly4i( sgn ,
    				n + i ,
    				n + (i+1) ,
    				n + Panels+1 + ( (i+1) ) ,
    				n + Panels+1 + i,
    				Item );
    	else
    		for( i = 0 ; i < Panels ; i++ )
    			Poly4i( sgn ,
    				n + i ,
    				n + (i+1)%Sides ,
    				n + Panels+1 + ( (i+1)%Sides ) ,
    				n + Panels+1 + i,
    				Item );
    }
    // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ BUILD
    function bool Build()
    {
    	local int i,j,n,InnerPanels,OuterPanels,PoleS;
    	local EAlignStyle InnerAlign,OuterAlign;
    	local bool Complete;
    	// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ check parameters
    	if( Sides<3 )
    		return BadParameters("Must have at least 3 sides.");
    	if( Height<=0 || OuterRadius<=0 )
    		return BadParameters("Dimensions must be positive.");
    	if( Hollow && InnerRadius>=OuterRadius )
    		return BadParameters("OuterRadius must be greater than InnerRadius.");
    	if( Hollow && InnerRadius<=0 )
    		return BadParameters("InnerRadius must be positive.");
    	// if partial cylinder, styles must be compatible:
    	// either the same, or a AL_Plain with AL_SideCaps
    		// if partial, proportion of circle must be the same in inner, outer.

    	// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ set variables

    	// internal variables are ready for multicylinder, set them to user option.
    	// Panels reflects the actual number of panels, for CAPS this is ++.
    	InnerAlign = AlignType ;
    	OuterAlign = AlignType ;

    	if( SidesUsed >= Sides || SidesUsed == 0)
    	{
    		InnerPanels = Sides ;
    		OuterPanels = Sides ;
    		Complete = True ;
    	}
    	else
    	{
    		InnerPanels = SidesUsed ;
    		OuterPanels = SidesUsed ;
    		if( InnerAlign == AL_Caps )
    			InnerPanels ++ ;
    		if( OuterAlign == AL_Caps )
    			OuterPanels ++ ;
    	}
    	// ---------------------------------- check & set styles here
    	//	if( !Complete && OuterAlign != InnerAlign )

    	// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ build brush
    	BeginBrush( false, GroupName );
    	n = GetVertexCount();
    	BuildCirclet( 1 , OuterAlign , Sides, OuterPanels, Height, OuterRadius, 'outerwall',Complete );

    	if( Hollow )
    		BuildCirclet( -1 , InnerAlign , Sides, InnerPanels, Height, InnerRadius, 'innerwall',Complete );

    	// ----------------------------------------- option: POLES

    	if( !Hollow &&
    			( TriangulateCaps ||
    				( !Complete && !( !TriangulateCaps && SidesUsed == Sides/2 ) ) ) )
    	{
    		/*
    		when do we want poles?
    		above all, ONLY non-hollow
    		1. triagulated in any case -- by that catch complete & triangulated
    		2. partial, UNLESS: non-tri, 1/2 circle
    			parse this as !complete AND NOT ( opt-out condition )
    		bear in mind: triangulation always leads to poles, so this goes first
    		even if TRI=false, poles are FORCED on the !complete case.
    		*/
    		PoleS = GetVertexCount();
    		Vertex3f( 0 , 0 , -Height/2 );
    		Vertex3f( 0 , 0 , Height/2 );
    	}

    	// ----------------------------------------- caps & possibly ends
    	if( !Hollow && Complete )
    	{
    		// -------------- CASE solid, complete: two caps
    		// j flips the poly direction.
    		if( TriangulateCaps )
    		{
    			// Polar vertices required. two sets of triangles, with modulo for the join.
    			for( j=0 ; j < 2 ; j++ )
    				for( i=0; i<OuterPanels; i++ )
    					Poly3i( -2*j +1, j*OuterPanels + i ,PoleS + j,
    						j*OuterPanels + (( i + 1 ) % OuterPanels ) ,'cap');
    		}
    		else
    		{
    			// two caps. j flips the poly direction.
    			for( j=0 ; j < 2 ; j++ )
    			{
    				PolyBegin( 2*j - 1 , 'cap' );
    					for( i=0; i<Sides; i++ )
    						Polyi( j*Sides + i );
    				PolyEnd();
    			}
    		}
    	}
    	else if( !Hollow )
    	{
    		// -------------- CASE solid, partial: two sets of triangles, two edge pieces
    		if( TriangulateCaps || SidesUsed > Sides/2 )
    		{
    			for( j=0 ; j < 2 ; j++ )
    				for( i=0; i<OuterPanels; i++ )
    					Poly3i( -2*j +1, j*(OuterPanels+1) + i ,PoleS + j, j*(OuterPanels+1) + i + 1 ,'cap');
    		}
    		else
    		{
    			// these cause concave polys! ONLY use if we can verify that total curvature <= pi
    			for( j=0 ; j < 2 ; j++ )
    			{
    				PolyBegin( 2*j - 1 , 'cap' );
    					for( i=0; i<OuterPanels+1; i++ )
    						Polyi( j*(OuterPanels+1) + i );
    					if( TriangulateCaps || SidesUsed != Sides/2 )
    						Polyi( PoleS + j ); // pole is present in the non-opt-out case
    				PolyEnd();
    			}
    		}
    		// if NO triangulate caps, and 1/2 circle, we have no poles; need only one end poly!
    		if( !TriangulateCaps && SidesUsed == Sides/2 )
    			Poly4i( 1, 0 , OuterPanels+1 + 0 , 2*OuterPanels+1 , OuterPanels , 'end' );
    		else
    		{
    			Poly4i( 1, 0 , OuterPanels+1 + 0 , PoleS + 1, PoleS , 'end' );
    			Poly4i( -1, OuterPanels , 2*OuterPanels+1 , PoleS + 1, PoleS , 'end' );
    		}
    	}
    	else if( !Complete )
    	{
    		// -------------- CASE hollow, partial: two rings of poly4, two edge pieces
    		// note we count tracks with OuterPanels+1 since there is a terminal vertex
    		for( j=0 ; j < 2 ; j++ )
    			for( i=0; i<OuterPanels; i++ )
    				Poly4i( -2*j +1, j*(OuterPanels+1) + i + 1 , j*(OuterPanels+1) + i ,
    					2*(OuterPanels+1) + j*(OuterPanels+1) + i ,
    					2*(OuterPanels+1) + j*(OuterPanels+1) + i + 1 ,
    					'cap' );
    		Poly4i( 1, 0, OuterPanels+1 , 3*(OuterPanels+1) , 2*(OuterPanels+1), 'end' );
    		Poly4i( 1, 3*(OuterPanels+1)-1 , 4*(OuterPanels+1)-1,
    			2*(OuterPanels+1)-1, 1*(OuterPanels+1)-1,'end' );
    	}
    	else
    	{
    		// -------------- CASE hollow, complete: two rings of poly4, with modulo for join.
    		for( j=0 ; j < 2 ; j++ )
    			for( i=0; i<OuterPanels; i++ )
    				Poly4i( -2*j +1, j*(OuterPanels) + ((i + 1)%OuterPanels) ,
    					j*(OuterPanels) + i ,
    					2*(OuterPanels) + j*(OuterPanels) + i ,
    					2*(OuterPanels) + j*(OuterPanels) + ((i + 1)%OuterPanels) ,
    					'cap' );
    	}
    	return EndBrush();
    }

    defaultproperties
    {
         Height=256.000000
         OuterRadius=256.000000
         InnerRadius=128.000000
         Sides=8
         SidesUsed=7
         GroupName=Cylinder
         BitmapFilename="SFEDBBTarquinCylinder"
         ToolTip="Enhanced Cylinder Builder"
    }

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