PLearn 0.1
RegularGridVMatrix.cc
Go to the documentation of this file.
00001 
00002 // -*- C++ -*-
00003 
00004 // RegularGridVMatrix.cc
00005 //
00006 // Copyright (C) 2003  Pascal Vincent
00007 //
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are met:
00010 //
00011 //  1. Redistributions of source code must retain the above copyright
00012 //     notice, this list of conditions and the following disclaimer.
00013 //
00014 //  2. Redistributions in binary form must reproduce the above copyright
00015 //     notice, this list of conditions and the following disclaimer in the
00016 //     documentation and/or other materials provided with the distribution.
00017 //
00018 //  3. The name of the authors may not be used to endorse or promote
00019 //     products derived from this software without specific prior written
00020 //     permission.
00021 //
00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 //
00033 // This file is part of the PLearn library. For more information on the PLearn
00034 // library, go to the PLearn Web site at www.plearn.org
00035 
00036 /* *******************************************************
00037  * $Id: RegularGridVMatrix.cc 6485 2006-12-11 18:55:02Z plearner $
00038  ******************************************************* */
00039 
00041 #include "RegularGridVMatrix.h"
00042 #include <plearn/math/TMat_maths.h>
00043 
00044 namespace PLearn {
00045 using namespace std;
00046 
00047 
00048 RegularGridVMatrix::RegularGridVMatrix()
00049 {
00050     // ...
00051 
00052     // ### You may or may not want to call build_() to finish building the object
00053     // build_();
00054 }
00055 
00056 RegularGridVMatrix::RegularGridVMatrix(TVec<int> the_dimensions, TVec< pair<real,real> > the_range)
00057     :dimensions(the_dimensions.copy()), range(the_range.copy())
00058 {
00059     build_();
00060 }
00061 
00062 RegularGridVMatrix::RegularGridVMatrix(TVec<int> the_dimensions, Vec the_coordstart, Vec the_coordend)
00063     :dimensions(the_dimensions.copy()), coordstart(the_coordstart.copy()), coordend(the_coordend.copy())
00064 {
00065     build_();
00066 }
00067 
00068 PLEARN_IMPLEMENT_OBJECT(RegularGridVMatrix, "ONE LINE DESCR",
00069                         "RegularGridVMatrix represents the list of coordinates along a regularly spaced grid.");
00070 
00071 void RegularGridVMatrix::getNewRow(int i, const Vec& v) const
00072 {
00073     int d = width();
00074     if(v.length()!=d)
00075         PLERROR("In RegularGridVMatrix::getNewRow, size of v (%d) differs from vmat width (%d)",v.length(), d);
00076     if(i<0 || i>=length())
00077         PLERROR("In RegularGridVMatrix::getNewRow, row %d out of range [0,%d]",i, length()-1);
00078     int idx = i;
00079     for(int k=d-1; k>=0; k--)
00080     {
00081         int idx_k = idx%dimensions[k];
00082         idx = idx/dimensions[k];
00083         v[k] = coordstart[k] + (coordend[k]-coordstart[k])/(dimensions[k]-1)*idx_k;
00084     }
00085 }
00086 
00087 void RegularGridVMatrix::declareOptions(OptionList& ol)
00088 {
00089     // ### Declare all of this object's options here
00090     // ### For the "flags" of each option, you should typically specify
00091     // ### one of OptionBase::buildoption, OptionBase::learntoption or
00092     // ### OptionBase::tuningoption. Another possible flag to be combined with
00093     // ### is OptionBase::nosave
00094 
00095     // ### ex:
00096     // declareOption(ol, "myoption", &RegularGridVMatrix::myoption, OptionBase::buildoption,
00097     //               "Help text describing this option");
00098     // ...
00099     declareOption(ol, "dimensions", &RegularGridVMatrix::dimensions, OptionBase::buildoption,
00100                   "A vector of integers giving the number of sample coordinates\n"
00101                   "for each dimension of the grid. Ex for a 100x100 2D grid: [ 100 100 ]\n");
00102     declareOption(ol, "range", &RegularGridVMatrix::range, OptionBase::buildoption,
00103                   "A vector of low:high pairs with as many dimensions as the grid\n"
00104                   "ex for 2D: [ -10:10 -3:4 ] \n");
00105     declareOption(ol, "coordstart", &RegularGridVMatrix::coordstart, OptionBase::buildoption,
00106                   "A vector of all the start ocoordinates of the grid in each dimension\n"
00107                   "ex for 2D: [ -10, -3 ] \n");
00108     declareOption(ol, "coordend", &RegularGridVMatrix::coordend, OptionBase::buildoption,
00109                   "A vector of all the end coordinates of the grid in each dimension\n"
00110                   "ex for 2D: [ 5, 4 ] \n");
00111 
00112     // Now call the parent class' declareOptions
00113     inherited::declareOptions(ol);
00114 }
00115 
00116 void RegularGridVMatrix::build_()
00117 {
00118     int n = range.length();
00119     if(n>0)
00120     {
00121         coordstart.resize(n);
00122         coordend.resize(n);
00123         for(int k=0; k<n; k++)
00124         {
00125             coordstart[k] = range[k].first;
00126             coordstart[k] = range[k].second;
00127         }
00128     }
00129 
00130     width_ = dimensions.length();
00131     length_ = (width_ ? product(dimensions) : 0);
00132     if(inputsize_<0)
00133     {
00134         inputsize_ = width_;
00135         targetsize_ = 0;
00136         weightsize_ = 0;
00137     }
00138 }
00139 
00140 // ### Nothing to add here, simply calls build_
00141 void RegularGridVMatrix::build()
00142 {
00143     inherited::build();
00144     build_();
00145 }
00146 
00147 void RegularGridVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00148 {
00149     inherited::makeDeepCopyFromShallowCopy(copies);
00150     deepCopyField(dimensions, copies);
00151     deepCopyField(range, copies);
00152     deepCopyField(coordstart, copies);
00153     deepCopyField(coordend, copies);
00154 }
00155 
00156 } // end of namespace PLearn
00157 
00158 
00159 /*
00160   Local Variables:
00161   mode:c++
00162   c-basic-offset:4
00163   c-file-style:"stroustrup"
00164   c-file-offsets:((innamespace . 0)(inline-open . 0))
00165   indent-tabs-mode:nil
00166   fill-column:79
00167   End:
00168 */
00169 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines