Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Namespace Members | Data Fields | Globals

KDPoint.h

Go to the documentation of this file.
00001 /* Copyright (C) 2004
00002    K-Dimension Point used in K-Dimension Tree
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with this library; if not, write to the Free Software
00016    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 */
00018 
00019 #ifndef _KDPOINT_H_
00020 #define _KDPOINT_H_
00021 
00022 #include "Object.h"
00023 #include <stdio.h>
00024 #include <iostream>
00025 #include <math.h>
00026 
00027 
00028 // Template class for point coordinate
00029 // IMPORTANT: class requires =, -, >, < operators
00030 //            it should usually be either double, float, int, ...
00031 template <class KDData>
00032 class KDPoint : public FD::Object
00033 {
00034 public:
00035         /*
00036                 KDPoint
00037 
00038                 Default constructor
00039         */
00040         KDPoint()
00041         : m_dimSize(0), m_point(NULL)
00042         {
00043 
00044         }
00045 
00046         /*
00047                 KDPoint
00048 
00049                 Other constructor with user data
00050 
00051                 PARAMETERS
00052                 i_dimSize : dimension size of current point
00053         */
00054         KDPoint(int i_dimSize)
00055         : m_dimSize(i_dimSize)
00056         {
00057                 m_point = new KDData[m_dimSize];
00058         }
00059 
00060         /*
00061                 KDPoint
00062 
00063                 Other constructor with user data
00064 
00065                 PARAMETERS
00066                 i_dimSize : dimension size of current point
00067                 i_point : array with the current point data
00068         */
00069         KDPoint(int i_dimSize, KDData *i_point)
00070         : m_dimSize(i_dimSize)
00071         {
00072                 m_point = new KDData[m_dimSize];
00073 
00074                 for (int i=0; i<m_dimSize; ++i) {
00075                         m_point[i] = i_point[i];
00076                 }
00077         }
00078 
00079         /*
00080                 KDPoint
00081 
00082                 Other constructor with user data
00083 
00084                 PARAMETERS
00085                 i_dimSize : dimension size of current point
00086                 i_point : single value to assign to all dimension in point
00087         */
00088         KDPoint(int i_dimSize, KDData i_point)
00089         : m_dimSize(i_dimSize)
00090         {
00091                 m_point = new KDData[m_dimSize];
00092 
00093                 for (int i=0; i<m_dimSize; ++i) {
00094                         m_point[i] = i_point;
00095                 }
00096         }
00097 
00098         /*
00099                 KDPoint
00100 
00101                 Copy constructor
00102 
00103                 PARAMETERS
00104                 i_cpy : reference to point to copy
00105         */
00106         KDPoint(const KDPoint & i_cpy)
00107         {
00108                 m_dimSize = i_cpy.m_dimSize;
00109                 m_point = new KDData[m_dimSize];
00110 
00111                 for (int i=0; i<m_dimSize; ++i) {
00112                         m_point[i] = i_cpy.m_point[i];
00113                 }
00114         }
00115 
00116         /*
00117                 operator =
00118 
00119                 Equal operator
00120 
00121                 PARAMETERS
00122                 i_ref : reference to point to copy
00123 
00124                 RETURNS
00125                 A copy of the given reference point
00126         */
00127         KDPoint & operator =(const KDPoint &i_ref)
00128         {
00129                 // Avoid self assignment
00130                 if (&i_ref == this) {
00131                         return *this;
00132                 }
00133 
00134                 // Prevent memory leak
00135                 delete [] m_point;
00136 
00137                 m_dimSize = i_ref.m_dimSize;
00138                 m_point = new KDData[m_dimSize];
00139 
00140                 for (int i=0; i<m_dimSize; ++i) {
00141                         m_point[i] = i_ref.m_point[i];
00142                 }
00143 
00144                 return *this;
00145         }
00146 
00147         /*
00148                 ~KDPoint
00149 
00150                 Destructor
00151 
00152         */
00153         ~KDPoint()
00154         {
00155                 delete [] m_point;
00156         }
00157 
00158         /*
00159                 printOn
00160 
00161                 Object routine required to print its content
00162 
00163                 PARAMETERS
00164                 out : output stream to print the object content
00165         */
00166         void printOn(std::ostream &out) const
00167         {
00168                 out << "<KDPoint " << std::endl;
00169                 out << "<DimSize " << m_dimSize << " >" << std::endl;
00170 
00171                 out << "<PointData " << std::endl;
00172                 for (int d=0; d<m_dimSize; ++d) {
00173                         out << (int)(m_point[d]) << std::endl;
00174                 }
00175                 out << " >" << std::endl;
00176 
00177                 out << " >" << std::endl;
00178         }
00179 
00180         /*
00181                 readFrom
00182 
00183                 Object routine required to read an Object content
00184 
00185                 PARAMETERS
00186                 in : input stream to read the object content
00187         */
00188         void readFrom(std::istream &in)
00189         {
00190                 std::string tag;
00191 
00192                 while (1) {
00193                         char ch;
00194                         in >> ch;
00195 
00196                         if (ch == '>') {
00197                                 break;
00198                         }
00199                         else if (ch != '<') {
00200                                 throw new FD::GeneralException ("KDPoint::readFrom : Parse error: '<' expected",__FILE__,__LINE__);
00201                         }
00202 
00203                         in >> tag;
00204 
00205                         if (tag == "KDPoint") {
00206                                 continue;
00207                         }
00208                         else if (tag == "DimSize") {
00209                                 in >> m_dimSize;
00210                         }
00211                         else if (tag == "PointData") {
00212                                 // Allocate point
00213                                 m_point = new KDData[m_dimSize];
00214 
00215                                 for (int d=0; d<m_dimSize; ++d) {
00216                                         in >> m_point[d];
00217                                 }
00218                         }
00219                         else {
00220                                 throw new FD::GeneralException ("KDPoint::readFrom : Unknown argument: " + tag,__FILE__,__LINE__);
00221                         }
00222 
00223                         if (!in) {
00224                                 throw new FD::GeneralException ("KDPoint::readFrom : Parse error trying to build " + tag,__FILE__,__LINE__);
00225                         }
00226 
00227                         in >> tag;
00228                         if (tag != ">") {
00229                                 throw new FD::GeneralException ("KDPoint::readFrom : Parse error: '>' expected ",__FILE__,__LINE__);
00230                         }
00231                 }
00232         }
00233 
00234         /*
00235                 SetPoint
00236 
00237                 Set the current point information with the given array
00238 
00239                 PARAMETERS
00240                 i_point :  array with the new point data
00241         */
00242         void SetPoint(KDData *i_point)
00243         {
00244                 for (int i=0; i<m_dimSize; ++i) {
00245                         m_point[i] = i_point[i];
00246                 }
00247         }
00248 
00249 
00250         /*
00251                 SetCoord
00252 
00253                 Set a single coordinate (dimension) in the current point
00254 
00255                 PARAMETERS
00256                 i_dim :  dimension where to change the value
00257                 i_coord : value of the new coordinate
00258         */
00259         void SetCoord(int i_dim, KDData i_coord)
00260         {
00261                 m_point[i_dim] = i_coord;
00262         }
00263 
00264         /*
00265                 SetDimSize
00266 
00267                 Set a point dimension size (reset its point data array)
00268 
00269                 PARAMETERS
00270                 i_dimSize :  dimension size of the point
00271         */
00272         void SetDimSize(int i_dimSize)
00273         {
00274                 m_dimSize = i_dimSize;
00275 
00276                 delete [] m_point;
00277                 m_point = new KDData[m_dimSize];
00278         }
00279 
00280         /*
00281                 GetPoint
00282 
00283                 Get a reference to point data array
00284 
00285                 RETURNS
00286                 A reference to point data array
00287         */
00288         inline KDData *GetPoint() const
00289         {
00290                 return m_point;
00291         }
00292 
00293         /*
00294                 GetCoord
00295 
00296                 Get a coordinate value in the current point
00297 
00298                 PARAMETERS
00299                 i_dim :  the desired dimension
00300 
00301                 RETURNS
00302                 A coordinate value in the current point
00303         */
00304         inline KDData GetCoord(int i_dim) const
00305         {
00306                 return m_point[i_dim];
00307         }
00308 
00309         /*
00310                 GetDimSize
00311 
00312                 Get the current point dimension size
00313 
00314                 RETURNS
00315                 The current point dimension size
00316         */
00317         inline int GetDimSize() const
00318         {
00319                 return m_dimSize;
00320         }
00321 
00322 private:
00323         // Dimension size of point
00324         int m_dimSize;
00325         // The point data array
00326         KDData *m_point;
00327 };
00328 
00329 #endif

Generated on Wed Oct 5 14:36:12 2005 for RobotFlow by  doxygen 1.4.4