SubsetMapping.inl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef SOFA_COMPONENT_MAPPING_SUBSETMAPPING_INL
00026 #define SOFA_COMPONENT_MAPPING_SUBSETMAPPING_INL
00027
00028 #include "SubsetMapping.h"
00029
00030 namespace sofa
00031 {
00032
00033 namespace component
00034 {
00035
00036 namespace mapping
00037 {
00038
00039
00040 template <class BaseMapping>
00041 SubsetMapping<BaseMapping>::SubsetMapping(In* from, Out* to)
00042 : Inherit(from, to)
00043 , f_indices( initData(&f_indices, "indices", "list of input indices"))
00044 , f_first( initData(&f_first, -1, "first", "first index (use if indices are sequential)"))
00045 , f_last( initData(&f_last, -1, "last", "last index (use if indices are sequential)"))
00046 , f_radius( initData(&f_radius, (Real)1.0e-5, "radius", "search radius to find corresponding points in case no indices are given"))
00047 {
00048 }
00049
00050 template <class BaseMapping>
00051 SubsetMapping<BaseMapping>::~SubsetMapping()
00052 {
00053 }
00054
00055
00056 template <class BaseMapping>
00057 void SubsetMapping<BaseMapping>::clear(int reserve)
00058 {
00059 IndexArray& indices = *f_indices.beginEdit();
00060 indices.clear();
00061 if (reserve) indices.reserve(reserve);
00062 f_indices.endEdit();
00063 }
00064
00065 template <class BaseMapping>
00066 int SubsetMapping<BaseMapping>::addPoint(int index)
00067 {
00068 IndexArray& indices = *f_indices.beginEdit();
00069 int i = indices.size();
00070 indices.push_back(index);
00071 f_indices.endEdit();
00072 return i;
00073 }
00074
00075
00076 template <class BaseMapping>
00077 void SubsetMapping<BaseMapping>::init()
00078 {
00079 unsigned int inSize = this->fromModel->getX()->size();
00080 if (f_indices.getValue().empty() && f_first.getValue() != -1)
00081 {
00082 IndexArray& indices = *f_indices.beginEdit();
00083 unsigned int first = (unsigned int)f_first.getValue();
00084 unsigned int last = (unsigned int)f_last.getValue();
00085 if (first >= inSize)
00086 first = 0;
00087 if (last >= inSize)
00088 last = inSize-1;
00089 indices.resize(last-first+1);
00090 for (unsigned int i=0; i<indices.size(); ++i)
00091 indices[i] = first+i;
00092 f_indices.endEdit();
00093 }
00094 else if (f_indices.getValue().empty())
00095 {
00096
00097
00098 const InVecCoord& in = *this->fromModel->getX();
00099 const OutVecCoord& out = *this->toModel->getX();
00100 IndexArray& indices = *f_indices.beginEdit();
00101 indices.resize(out.size());
00102
00103
00104 for (unsigned int i = 0; i < out.size(); ++i)
00105 {
00106 bool found = false;
00107 Real rmax = f_radius.getValue();
00108 for (unsigned int j = 0; j < in.size(); ++j )
00109 {
00110 Real r = (Real)((out[i] - in[j]).norm());
00111 if ( r < rmax )
00112 {
00113 indices[i] = j;
00114 found = true;
00115 rmax = r;
00116 }
00117 }
00118 if (!found)
00119 {
00120 std::cerr<<"ERROR(SubsetMapping): point "<<i<<"="<<out[i]<<" not found in input model within a radius of "<<rmax<<"."<<std::endl;
00121 indices[i] = 0;
00122 }
00123 }
00124 f_indices.endEdit();
00125 }
00126 else
00127 {
00128 IndexArray& indices = *f_indices.beginEdit();
00129 for (unsigned int i=0; i<indices.size(); ++i)
00130 {
00131 if ((unsigned)indices[i] >= inSize)
00132 {
00133 std::cerr << "ERROR(SubsetMapping): incorrect index "<<indices[i]<<" (input size "<<inSize<<")\n";
00134 indices.erase(indices.begin()+i);
00135 --i;
00136 }
00137 }
00138 f_indices.endEdit();
00139 }
00140 this->Inherit::init();
00141 postInit();
00142 }
00143
00144 template <class BaseMapping>
00145 void SubsetMapping<BaseMapping>::postInit()
00146 {
00147 }
00148
00149 template <class BaseMapping>
00150 void SubsetMapping<BaseMapping>::apply( typename Out::VecCoord& out, const typename In::VecCoord& in )
00151 {
00152 const IndexArray& indices = f_indices.getValue();
00153 out.resize(indices.size());
00154 for(unsigned int i = 0; i < out.size(); ++i)
00155 {
00156 out[i] = in[ indices[i] ];
00157 }
00158 }
00159
00160 template <class BaseMapping>
00161 void SubsetMapping<BaseMapping>::applyJ( typename Out::VecDeriv& out, const typename In::VecDeriv& in )
00162 {
00163 const IndexArray& indices = f_indices.getValue();
00164 out.resize(indices.size());
00165 for(unsigned int i = 0; i < out.size(); ++i)
00166 {
00167 out[i] = in[ indices[i] ];
00168 }
00169 }
00170
00171 template <class BaseMapping>
00172 void SubsetMapping<BaseMapping>::applyJT( typename In::VecDeriv& out, const typename Out::VecDeriv& in )
00173 {
00174 const IndexArray& indices = f_indices.getValue();
00175 for(unsigned int i = 0; i < in.size(); ++i)
00176 {
00177 out[indices[i]] += in[ i ];
00178 }
00179 }
00180
00181 template <class BaseMapping>
00182 void SubsetMapping<BaseMapping>::applyJT( typename In::VecConst& out, const typename Out::VecConst& in )
00183 {
00184 int offset = out.size();
00185 out.resize(offset+in.size());
00186
00187 const IndexArray& indices = f_indices.getValue();
00188 for(unsigned int c = 0; c < in.size(); ++c)
00189 {
00190 for(unsigned int j=0;j<in[c].size();j++)
00191 {
00192 const typename Out::SparseDeriv cIn = in[c][j];
00193 out[c+offset].push_back(typename In::SparseDeriv( indices[cIn.index] , (typename In::Deriv) cIn.data ));
00194 }
00195 }
00196
00197 }
00198
00199 }
00200
00201 }
00202
00203 }
00204
00205 #endif