glc_plane.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  *****************************************************************************/
00025 
00026 #include <QtDebug>
00027 #include "glc_plane.h"
00028 
00029 GLC_Plane::GLC_Plane()
00030 : m_Eq()
00031 {
00032         m_Eq[0]= 0.0;
00033         m_Eq[1]= 0.0;
00034         m_Eq[2]= 0.0;
00035         m_Eq[3]= 0.0;
00036 }
00037 
00038 GLC_Plane::GLC_Plane(double a, double b, double c, double d)
00039 : m_Eq()
00040 {
00041         m_Eq[0]= a;
00042         m_Eq[1]= b;
00043         m_Eq[2]= c;
00044         m_Eq[3]= d;
00045 
00046 }
00047 
00048 GLC_Plane::GLC_Plane(const GLC_Vector3d& normal, double d)
00049 : m_Eq()
00050 {
00051         m_Eq[0]= normal.x();
00052         m_Eq[1]= normal.y();
00053         m_Eq[2]= normal.z();
00054         m_Eq[3]= d;
00055 }
00056 
00057 GLC_Plane::GLC_Plane(const GLC_Vector3d& normal, const GLC_Point3d& point)
00058 : m_Eq()
00059 {
00060         m_Eq[0]= normal.x();
00061         m_Eq[1]= normal.y();
00062         m_Eq[2]= normal.z();
00063         m_Eq[3]= -normal * point;
00064 }
00065 
00066 
00067 GLC_Plane::GLC_Plane(const GLC_Point3d& p1, const GLC_Point3d& p2, const GLC_Point3d& p3)
00068 : m_Eq()
00069 {
00070         const GLC_Vector3d v1(p2 - p1);
00071         const GLC_Vector3d v2(p3 - p1);
00072         const GLC_Vector3d normal((v1 ^ v2).normalize());
00073         m_Eq[0]= normal.x();
00074         m_Eq[1]= normal.y();
00075         m_Eq[2]= normal.z();
00076         m_Eq[3]= -normal * p1;
00077 }
00078 
00079 
00080 GLC_Plane::GLC_Plane(const GLC_Plane& plane)
00081 : m_Eq()
00082 {
00083         memcpy(m_Eq, plane.m_Eq, sizeof(double) * 4);
00084 }
00085 
00086 
00087 GLC_Plane& GLC_Plane::operator=(const GLC_Plane& p)
00088 {
00089         if ((this != &p) && (*this != p))
00090         {
00091                 memcpy(m_Eq, p.m_Eq, sizeof(double) * 4);
00092         }
00093         return *this;
00094 }
00095 
00096 
00097 GLC_Plane::~GLC_Plane()
00098 {
00099 
00100 }
00101 
00103 // Get Functions
00105 
00106 
00107 bool GLC_Plane::operator==(GLC_Plane p2) const
00108 {
00109         GLC_Plane p1(*this);
00110         p1.normalize();
00111         p2.normalize();
00112         bool areEqual= qFuzzyCompare(p1.m_Eq[0], p2.m_Eq[0]);
00113         areEqual= areEqual && qFuzzyCompare(p1.m_Eq[1], p2.m_Eq[1]);
00114         areEqual= areEqual && qFuzzyCompare(p1.m_Eq[2], p2.m_Eq[2]);
00115         areEqual= areEqual && qFuzzyCompare(p1.m_Eq[3], p2.m_Eq[3]);
00116 
00117         return areEqual;
00118 }
00119 
00120 QString GLC_Plane::toString() const
00121 {
00122         return QString::number(m_Eq[0]) + "x + " + QString::number(m_Eq[1]) + "y + " + QString::number(m_Eq[2]) + "z + " + QString::number(m_Eq[3]);
00123 }
00125 // Set Functions
00127 
00128 // Normalize the plane
00129 void GLC_Plane::normalize()
00130 {
00131         const double invMag= 1.0 / sqrt(m_Eq[0] * m_Eq[0] + m_Eq[1] * m_Eq[1] + m_Eq[2] * m_Eq[2]);
00132 
00133         m_Eq[0]= m_Eq[0] * invMag;
00134         m_Eq[1]= m_Eq[1] * invMag;
00135         m_Eq[2]= m_Eq[2] * invMag;
00136         m_Eq[3]= m_Eq[3] * invMag;
00137 }
00138 
00139 GLC_Plane& GLC_Plane::setPlane(const GLC_Vector3d& normal, const GLC_Point3d& point)
00140 {
00141         m_Eq[0]= normal.x();
00142         m_Eq[1]= normal.y();
00143         m_Eq[2]= normal.z();
00144         m_Eq[3]= -normal * point;
00145 
00146         return *this;
00147 }

SourceForge.net Logo

©2005 Laurent Ribon