Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Torus.cpp

Go to the documentation of this file.
00001 
00002 /*
00003     TEDDY - General graphics application library
00004     Copyright (C) 1999-2002  Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     $Id: Torus.cpp,v 1.5 2002/02/16 12:41:39 tksuoran Exp $
00022 */
00023 
00024 
00025 #include "Teddy/Maths/Vector.h"
00026 #include "Teddy/Models/QuadStrip.h"
00027 #include "Teddy/Models/Torus.h"
00028 #include "Teddy/Models/Vertex.h"
00029 #include "Teddy/SysSupport/StdMaths.h"
00030 
00031 
00032 namespace Teddy  {
00033 namespace Models {
00034 
00035 
00044 Torus::Torus(
00045     const std::string &name,
00046     const double       major_radius,
00047     const double       minor_radius,
00048     const int          stacks,
00049     const int          slices
00050 ):Model( name ){
00051     int    i;
00052     int    j;
00053     int    k;
00054     double s;
00055     double t;
00056     double x;
00057     double y;
00058     double z;
00059 
00060     double cp = M_2_PI/(double)stacks;
00061     double tp = M_2_PI/(double)slices;
00062 
00063     for( i=0; i<stacks; i++ ){
00064         QuadStrip *qs = new QuadStrip();
00065         for( j=0; j<=slices; j++ ){
00066             for( k=0; k<2; k++ ){
00067                 s = (i + k) % stacks + 0.5;
00068                 t = j % slices;
00069 
00070                 x = (major_radius + minor_radius * cos(s*cp)) * cos(t*tp);
00071                 y = (major_radius + minor_radius * cos(s*cp)) * sin(t*tp);
00072                 z = minor_radius * sin(s*cp);
00073                 Vertex *v = new Vertex( x, y, z );
00074 
00075                 x = cos(t*tp) * cos(s*cp);
00076                 y = sin(t*tp) * cos(s*cp);
00077                 z = sin(s*cp);
00078                 v->setNormal( Vector( x, y, z ) );
00079                 qs->add( v );
00080             }
00081         }
00082         add( qs );
00083     }
00084 
00085     setupClipRadius();
00086 }
00087 
00088 
00089 };  //  namespace Models
00090 };  //  namespace Teddy
00091