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

SourceForge.net Logo

©2005-2011 Laurent Ribon