glc_circle.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the GLC-lib library.
00004  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
00005  Version 2.0.0 Beta 1, packaged on April 2010.
00006 
00007  http://glc-lib.sourceforge.net
00008 
00009  GLC-lib is free software; you can redistribute it and/or modify
00010  it under the terms of the GNU General Public License as published by
00011  the Free Software Foundation; either version 2 of the License, or
00012  (at your option) any later version.
00013 
00014  GLC-lib is distributed in the hope that it will be useful,
00015  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  GNU General Public License for more details.
00018 
00019  You should have received a copy of the GNU General Public License
00020  along with GLC-lib; if not, write to the Free Software
00021  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023 *****************************************************************************/
00024 
00026 
00027 #include "glc_circle.h"
00028 #include "../glc_openglexception.h"
00029 #include "../glc_state.h"
00030 
00031 using namespace glc;
00033 // Constructor destructor
00035 
00036 GLC_Circle::GLC_Circle(const double &dRadius, double Angle)
00037 :GLC_Geometry("Circle", true)
00038 , m_Radius(dRadius)
00039 , m_Discret(GLC_DISCRET)
00040 , m_Angle(Angle)
00041 , m_Step(0)
00042 {
00043 
00044 }
00045 
00046 GLC_Circle::GLC_Circle(const GLC_Circle& sourceCircle)
00047 :GLC_Geometry(sourceCircle)
00048 , m_Radius(sourceCircle.m_Radius)
00049 , m_Discret(sourceCircle.m_Discret)
00050 , m_Angle(sourceCircle.m_Angle)
00051 , m_Step(sourceCircle.m_Step)
00052 {
00053 
00054 }
00055 GLC_Circle::~GLC_Circle()
00056 {
00057 
00058 }
00060 // Get Functions
00062 
00063 // return the circle bounding box
00064 const GLC_BoundingBox& GLC_Circle::boundingBox(void)
00065 {
00066         if (NULL == m_pBoundingBox)
00067         {
00068                 //qDebug() << "GLC_Mesh2::boundingBox create boundingBox";
00069                 m_pBoundingBox= new GLC_BoundingBox();
00070                 if (m_WireData.isEmpty()) createWire();
00071                 m_pBoundingBox->combine(m_WireData.boundingBox());
00072                 m_pBoundingBox->combine(GLC_Vector3d(0.0, 0.0, -2 * glc::EPSILON));
00073         }
00074         return *m_pBoundingBox;
00075 }
00076 
00077 // Return a copy of the current geometry
00078 GLC_Geometry* GLC_Circle::clone() const
00079 {
00080         return new GLC_Circle(*this);
00081 }
00082 
00084 // Set Functions
00086 
00087 // Set Circle diameter
00088 void GLC_Circle::setDiameter(double D)
00089 {
00090         Q_ASSERT(!qFuzzyCompare(D, 0.0));
00091         setRadius(D / 2);
00092 }
00093 
00094 // Set Circle Radius
00095 void GLC_Circle::setRadius(double R)
00096 {
00097         Q_ASSERT(!qFuzzyCompare(R, 0.0));
00098         if (!qFuzzyCompare(R - m_Radius, 0.0))
00099         {       // Radius is changing
00100                 m_Radius= R;
00101 
00102                 GLC_Geometry::clearWireAndBoundingBox();
00103         }
00104 }
00105 
00106 // Set Circle discret
00107 void GLC_Circle::setDiscretion(int TargetDiscret)
00108 {
00109         Q_ASSERT(TargetDiscret > 0);
00110         if (TargetDiscret != m_Discret)
00111         {
00112                 m_Discret= TargetDiscret;
00113                 if (m_Discret < 6) m_Discret= 6;
00114 
00115                 GLC_Geometry::clearWireAndBoundingBox();
00116         }
00117 }
00118 
00119 // Set Circle Angle
00120 void GLC_Circle::setAngle(double AngleRadians)  // Angle in Radians
00121 {
00122         Q_ASSERT((!qFuzzyCompare(AngleRadians, 0.0)) && (AngleRadians < 2 * PI));
00123         if (!qFuzzyCompare(AngleRadians - m_Angle, 0.0))
00124         {       // Angle is changing
00125                         m_Angle= AngleRadians;
00126 
00127                         GLC_Geometry::clearWireAndBoundingBox();
00128         }
00129 }
00130 
00132 // OpenGL Functions
00134 
00135 // Circle drawing
00136 void GLC_Circle::glDraw(const GLC_RenderProperties& renderProperties)
00137 {
00138         if (m_WireData.isEmpty())
00139         {
00140                 createWire();
00141         }
00142 
00143         m_WireData.glDraw(renderProperties);
00144 }
00145 
00146 // Create the wire
00147 void GLC_Circle::createWire()
00148 {
00149         Q_ASSERT(m_WireData.isEmpty());
00150 
00151         m_Step= static_cast<GLuint>(static_cast<double>(m_Discret) * (m_Angle / (2 * glc::PI)));
00152         if (m_Step < 2) m_Step= 2;
00153 
00154         // Float vector
00155         GLfloatVector floatVector;
00156 
00157         // Resize the Vertex vector
00158         const int size= (m_Step + 1) * 3;
00159         floatVector.resize(size);
00160         // Fill Vertex Vector
00161         const double angleOnStep= m_Angle / static_cast<double>(m_Step);
00162         for (GLuint i= 0; i <= m_Step; ++i)
00163         {
00164                 floatVector[(i * 3)]= static_cast<float>(m_Radius * cos(static_cast<double>(i) * angleOnStep));
00165                 floatVector[(i * 3) + 1]= static_cast<float>(m_Radius * sin(static_cast<double>(i) * angleOnStep));
00166                 floatVector[(i * 3) + 2]= 0.0f;
00167         }
00168         GLC_Geometry::addPolyline(floatVector);
00169 }

SourceForge.net Logo

©2005 Laurent Ribon