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

CMatrix.cpp

Go to the documentation of this file.
00001 /**************************************************************************
00008  *
00009  * Source code for Real Time Image Library (libRTImage)
00010  *
00011  * Leeds Vision Group give permission for this code to be copied, modified 
00012  * and distributed within the University of Leeds subject to the following 
00013  * conditions:-
00014  *
00015  * - The code is not to be used for commercial gain.
00016  * - The code and use thereof will be attributed to the author where
00017  *   appropriate (inluding demonstrations which rely on it's use).
00018  * - All modified, distributions of the source files will retain this header.
00019  *
00020  ****************************************************************************/
00021 
00022 #include <stdio.h>
00023 #include <math.h>
00024 #include <CMatrix.h>
00025 
00026 CMatrix::CMatrix(unsigned int rows, unsigned int columns)
00027 {
00028 // Constructor for new object
00029 
00030     InitOk = true ;
00031     r = rows ;
00032     c = columns ;
00033     size = r*c ;
00034 
00035     if((data = new float[size])==NULL) InitOk = false ;
00036 }
00037 
00038 CMatrix::CMatrix(char *filename)
00039 {
00040 // Constructor to load from formated file
00041 
00042     FILE *fp ;
00043     unsigned int cnt ;
00044     float        f_in ;
00045     float        *d_ptr ; ;
00046     int          r_in, c_in ;
00047 
00048     InitOk = true ;
00049 
00050     if((fp=fopen(filename,"r"))==NULL) InitOk = false ;
00051     else if(fscanf(fp,"%d",&r_in)==EOF) InitOk = false ;
00052     else if(fscanf(fp,"%d",&c_in)==EOF) InitOk = false ;
00053     else if(r_in<1 || c_in<1) InitOk = false ;
00054     else if((data = new float[r_in*c_in])==NULL) InitOk = false ;
00055     else{
00056         r = r_in ;
00057         c = c_in ;
00058         size = r*c ;
00059     }
00060     
00061     for(cnt=0, d_ptr=data ; InitOk && cnt<size ; cnt++,d_ptr++){
00062         if(fscanf(fp,"%f",&f_in)==EOF) InitOk = false ;
00063         else                           *d_ptr = f_in ;
00064     }
00065 }
00066 
00067 CMatrix::CMatrix(char *filename, unsigned int rows, unsigned int columns)
00068 {
00069 // Constructor to load from unformated text file
00070 
00071     FILE *fp ;
00072     unsigned int cnt ;
00073     float        f_in ;
00074     float        *d_ptr ; ;
00075 
00076     InitOk = true ;
00077     r = rows ;
00078     c = columns ;
00079     size = r*c ;
00080 
00081     if((fp=fopen(filename,"r"))==NULL) InitOk = false ;
00082     else if((data = new float[r*c])==NULL) InitOk = false ;
00083 
00084     for(cnt=0, d_ptr=data ; InitOk && cnt<size ; cnt++,d_ptr++){
00085         if(fscanf(fp,"%f",&f_in)==EOF) InitOk = false ;
00086         else                           *d_ptr = f_in ;
00087     }
00088 }
00089 
00090 void CMatrix::set_element(unsigned int row,
00091                           unsigned int col,
00092                           double       val)
00093 {
00094 // Sets element x,y to val
00095 
00096     data[col + c*row] = val ;
00097 }
00098 
00099 float CMatrix::get_element(unsigned int row,
00100                            unsigned int col) 
00101 {
00102 // Returns element x,y 
00103 
00104     return data[col + c*row] ;
00105 }
00106 
00107 unsigned int CMatrix::get_no_rows() 
00108 {
00109 // Returns the no. of row in the matrix
00110 
00111     return r ;
00112 }
00113 
00114 unsigned int CMatrix::get_no_columns() 
00115 {
00116 // Returns the no. of columns in the matrix
00117 
00118     return c ;
00119 }
00120 
00121 bool CMatrix::save(char *filename)
00122 {
00123 // Saves matrix to a formatted file
00124 
00125     FILE         *fp ;
00126     unsigned int cntx ;
00127     unsigned int cnty ;
00128     bool         rv  = true ;
00129     float        *d_ptr ;
00130 
00131     if((fp = fopen(filename,"w"))==NULL) rv = false ;
00132     else if(fprintf(fp,"%d %d\n",r,c)==EOF) rv = false ;
00133 
00134     for(cnty=0,d_ptr=data ; rv && cnty<r ; cnty++){
00135         for(cntx=0 ; rv && cntx<c ; cntx++,d_ptr++){
00136             if(fprintf(fp,"%f ",*d_ptr)==EOF) rv = false ;
00137         }
00138         if(fprintf(fp,"\n")==EOF) rv = false ;
00139     }
00140 
00141     return rv ;
00142 }
00143 
00144 bool CMatrix::save_raw(char *filename)
00145 {
00146 // Saves matrix to a raw text file (suitable for Matlab etc.)
00147 
00148     FILE         *fp ;
00149     unsigned int cntx ;
00150     unsigned int cnty ;
00151     bool         rv  = true ;
00152     float        *d_ptr ;
00153 
00154     if((fp = fopen(filename,"w"))==NULL) rv = false ;
00155 
00156     for(cnty=0,d_ptr=data ; rv && cnty<r ; cnty++){
00157         for(cntx=0 ; rv && cntx<c ; cntx++,d_ptr++){
00158             if(fprintf(fp,"%f ",*d_ptr)==EOF) rv = false ;
00159         }
00160         if(fprintf(fp,"\n")==EOF) rv = false ;
00161     }
00162 
00163     return rv ;
00164 }
00165 
00166 float CMatrix::calc_max_kernel_val()
00167 {
00168 // Returns maximum value of kernel
00169 
00170     unsigned int cnt ;
00171     float        *ptr ;
00172     float        ret_val = -HUGE_VAL ;
00173 
00174     ptr = data ;
00175     for(cnt=0 ; cnt<size ; cnt++,ptr++){
00176         if(*ptr > ret_val) ret_val = *ptr ;
00177     }
00178 
00179     return ret_val ;
00180 }
00181 
00182 float CMatrix::calc_min_kernel_val()
00183 {
00184 // Returns maximum value of kernel
00185 
00186     unsigned int cnt ;
00187     float        *ptr ;
00188     float        ret_val = HUGE_VAL ;
00189 
00190     ptr = data ;
00191     for(cnt=0 ; cnt<size ; cnt++,ptr++){
00192         if(*ptr < ret_val) ret_val = *ptr ;
00193     }
00194 
00195     return ret_val ;
00196 }
00197  
00198 
00199 bool CMatrix::initialised_ok()
00200 {
00201 // Returns constructor sucess flag
00202 
00203     return InitOk ;
00204 }    
00205 

Generated at Fri Aug 13 17:29:20 2004 for libRTImage by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001