PageRenderTime 162ms CodeModel.GetById 43ms app.highlight 44ms RepoModel.GetById 33ms app.codeStats 0ms

/tests/plf_tests.cc

https://bitbucket.org/tetonedge/libsrvf
C++ | 241 lines | 220 code | 20 blank | 1 comment | 15 complexity | 350056132c0112c653329612a56650f3 MD5 | raw file
Possible License(s): GPL-3.0
  1#include <boost/test/unit_test.hpp>
  2
  3#include <FL/Fl.H>
  4
  5#include <srvf/plf.h>
  6#include <srvf/pointset.h>
  7#include <srvf/plot.h>
  8#include <srvf/plotwin.h>
  9#include <srvf/render.h>
 10#include <srvf/matrix.h>
 11#include <srvf/util.h>
 12
 13BOOST_AUTO_TEST_SUITE(plf_tests)
 14
 15BOOST_AUTO_TEST_CASE(evaluate_test1)
 16{
 17  std::vector<double> samps_data=srvf::util::linspace(0.0,1.0,2);
 18  srvf::Pointset samps(1,2,samps_data);
 19  srvf::Plf F(samps);
 20  std::vector<double> tv=srvf::util::linspace(0.0,1.0,5);
 21  double expdata[]={0.0, 0.25, 0.5, 0.75, 1.0};
 22  srvf::Pointset Ftv1 = F.evaluate(tv);
 23  BOOST_REQUIRE_EQUAL(Ftv1.dim(), F.dim());
 24  BOOST_REQUIRE_EQUAL(Ftv1.npts(), tv.size());
 25  for (size_t i=0; i<5; ++i)
 26  {
 27    srvf::Point Ftv2 = F.evaluate(tv[i]);
 28    BOOST_REQUIRE_EQUAL(Ftv2.dim(), F.dim());
 29    BOOST_CHECK_EQUAL(Ftv1[i][0],expdata[i]);
 30    BOOST_CHECK_EQUAL(Ftv2[0],expdata[i]);
 31  }
 32}
 33
 34BOOST_AUTO_TEST_CASE(evaluate_test2)
 35{
 36  std::vector<double> uv=srvf::util::linspace(0.0,1.0,500);
 37  std::vector<double> uvl=srvf::util::linspace(0.0,1.0,999);
 38  srvf::Pointset X(3,500);
 39  for (size_t i=0; i<3; ++i)
 40  {
 41    for (size_t j=0; j<500; ++j)
 42    {
 43      X[j][i]=(double)j;
 44    }
 45  }
 46  srvf::Plf F(X,uv);
 47  srvf::Pointset res = F.evaluate(uvl);
 48  BOOST_REQUIRE_EQUAL(res.dim(), F.dim());
 49  BOOST_REQUIRE_EQUAL(res.npts(), uvl.size());
 50  for (size_t i=0; i<3; ++i)
 51  {
 52    for (size_t j=0; j<999; ++j)
 53    {
 54      double ev=(double)(j) * 499.0 / 998.0;
 55      BOOST_CHECK_CLOSE(ev,res[j][i],1e-5);
 56    }
 57  }
 58}
 59
 60BOOST_AUTO_TEST_CASE(preimages_test1)
 61{
 62  std::vector<double> samps_data=srvf::util::linspace(0.0,1.0,2);
 63  std::vector<double> uv=srvf::util::linspace(0.0,1.0,13);
 64  srvf::Pointset samps(1,2,samps_data);
 65  srvf::Plf F(samps);
 66  std::vector<double> Fiuv = F.preimages(uv);
 67  for (size_t i=0; i<uv.size(); ++i)
 68  {
 69    BOOST_CHECK_CLOSE(uv[i],Fiuv[i],1e-5);
 70  }
 71}
 72
 73BOOST_AUTO_TEST_CASE(preimages_test2)
 74{
 75  double samps_data[]={0.0, 0.5, 0.5, 1.0};
 76  srvf::Pointset samps(1,4,samps_data);
 77  std::vector<double> tv=srvf::util::linspace(0.0,1.0,5);
 78  double exp_data[]={0.0, 1.0/6.0, 2.0/3.0, 5.0/6.0, 1.0};
 79  srvf::Plf F(samps);
 80  std::vector<double> Fitv = F.preimages(tv);
 81  for (size_t i=0; i<tv.size(); ++i)
 82  {
 83    BOOST_CHECK_CLOSE(exp_data[i],Fitv[i],1e-5);
 84  }
 85}
 86
 87BOOST_AUTO_TEST_CASE(arc_length_test1)
 88{
 89  double samps_data[]=
 90  {
 91    0.0, 1.0, 1.0, 0.0,
 92    0.0, 0.0, 1.0, 1.0
 93  };
 94  srvf::Plf F(srvf::Pointset(2,4,samps_data,srvf::Pointset::POINT_PER_COLUMN));
 95  double ev=3.0;
 96  double av=F.arc_length();
 97  BOOST_CHECK_CLOSE(ev,av,1e-5);
 98}
 99
100BOOST_AUTO_TEST_CASE(translate_test1)
101{
102  srvf::Pointset samps(3,20,0.0);
103  srvf::Plf F(samps);
104  double v_data[]={1.0,2.0,3.0};
105  srvf::Point v(&v_data[0],&v_data[3]);
106  F.translate(v);
107  for (size_t i=0; i<F.samps().npts(); ++i)
108  {
109    for (size_t j=0; j<F.samps().dim(); ++j)
110    {
111      BOOST_CHECK_EQUAL(F.samps()[i][j],v[j]);
112    }
113  }
114}
115
116BOOST_AUTO_TEST_CASE(rotate_test1)
117{
118  // Rotation by 90 degrees counter-clockwise
119  double R_data[]={
120    0.0, -1.0,
121    1.0, 0.0
122  };
123  srvf::Matrix R(2,2,R_data);
124  srvf::Pointset samps(2,3,1.0);
125  srvf::Plf F(samps);
126  F.rotate(R);
127  for (size_t i=0; i<F.samps().npts(); ++i)
128  {
129    BOOST_CHECK_EQUAL(F.samps()[i][0],-1.0);
130    BOOST_CHECK_EQUAL(F.samps()[i][1],1.0);
131  }
132}
133
134BOOST_AUTO_TEST_CASE(scale_test1)
135{
136  srvf::Matrix samps_data(3,57,1.0);
137  srvf::Pointset samps(samps_data);
138  srvf::Plf F(samps);
139  F.scale(2.0);
140  for (size_t i=0; i<F.samps().npts(); ++i)
141  {
142    for (size_t j=0; j<F.samps().dim(); ++j)
143    {
144      BOOST_CHECK_EQUAL(F.samps()[i][j],2.0);
145    }
146  }
147}
148
149BOOST_AUTO_TEST_CASE(linear_combination_test1)
150{
151  double params1_data[]= { 0.0, 0.1, 0.2, 0.9, 1.0 };
152  double params2_data[]= { 0.0, 0.3, 0.4, 0.6, 1.0 };
153  size_t ncp1=sizeof(params1_data)/sizeof(double);
154  size_t ncp2=sizeof(params2_data)/sizeof(double);
155  srvf::Pointset samps1(1,5,1.0);
156  srvf::Pointset samps2(1,5,-1.0);
157  std::vector<double> params1(&params1_data[0],&params1_data[ncp1]);
158  std::vector<double> params2(&params2_data[0],&params2_data[ncp2]);
159  double exp_params_data[]={ 0.0, 0.1, 0.2, 0.3, 0.4, 0.6, 0.9, 1.0 };
160  srvf::Plf F1(samps1,params1);
161  srvf::Plf F2(samps2,params2);
162  srvf::Plf Fr=srvf::linear_combination(F1,F2,0.75,0.25);
163
164  BOOST_REQUIRE_EQUAL(Fr.samps().npts(),8);
165  BOOST_REQUIRE_EQUAL(Fr.ncp(),8);
166  for (size_t i=0; i<8; ++i)
167  {
168    BOOST_CHECK_EQUAL(Fr.samps()[i][0],0.5);
169    BOOST_CHECK_EQUAL(Fr.params()[i],exp_params_data[i]);
170  }
171}
172
173BOOST_AUTO_TEST_CASE(composition_test1)
174{
175  double params1_data[]={0.0, 0.25, 0.5, 1.0};
176  double params2_data[]={0.0, 1.0/3.0, 2.0/3.0, 1.0};
177  double exp_params[]={0.0, 1.0/6.0, 1.0/3.0, 2.0/3.0, 1.0};
178  double samps1_data[]={0.0, 0.5, 0.0, 0.5};
179  double samps2_data[]={0.0, 0.5, 0.5, 1.0};
180  double exp_samps[]={0.0, 0.5, 0.0, 0.0, 0.5};
181  size_t n1=sizeof(params1_data)/sizeof(double);
182  size_t n2=sizeof(params2_data)/sizeof(double);
183  size_t n3=sizeof(exp_params)/sizeof(double);
184  std::vector<double> params1(&params1_data[0],&params1_data[n1]);
185  std::vector<double> params2(&params2_data[0],&params2_data[n2]);
186  srvf::Pointset samps1(1,n1,samps1_data);
187  srvf::Pointset samps2(1,n2,samps2_data);
188  srvf::Plf F1(samps1,params1);
189  srvf::Plf F2(samps2,params2);
190  srvf::Plf F12=srvf::composition(F1,F2);
191  
192  BOOST_REQUIRE_EQUAL(F12.ncp(),n3);
193  for (size_t i=0; i<n3; ++i)
194  {
195    BOOST_CHECK_EQUAL(F12.params()[i],exp_params[i]);
196    BOOST_CHECK_EQUAL(F12.samps()[i][0],exp_samps[i]);
197  }
198}
199
200BOOST_AUTO_TEST_CASE(inverse_test1)
201{
202  double params_data[]={0.0, 0.25, 0.5, 0.75, 1.0};
203  double samps_data[]={0.0, 1.0/3.0, 1.0/3.0, 2.0/3.0, 1.0};
204  size_t n1=sizeof(params_data)/sizeof(double);
205  size_t nexp=n1;
206  std::vector<double> params(&params_data[0],&params_data[n1]);
207  srvf::Pointset samps(1,n1,samps_data);
208  srvf::Plf F(samps,params);
209  srvf::Plf Fi=srvf::inverse(F);
210  
211  BOOST_REQUIRE_EQUAL(Fi.ncp(),nexp);
212  for (size_t i=0; i<nexp; ++i)
213  {
214    BOOST_CHECK_EQUAL(Fi.params()[i],samps_data[i]);
215    BOOST_CHECK_EQUAL(Fi.samps()[i][0],params_data[i]);
216  }
217}
218
219BOOST_AUTO_TEST_CASE(constant_speed_reparam_test1)
220{
221  double samps_data[] = { 0.0, 0.1, 1.0, 0.0, 0.1, 0.0 };
222  double params_data[] = { 0.0, 0.6, 0.7, 0.8, 1.0 };
223  size_t npts = sizeof(params_data) / sizeof(double);
224  srvf::Pointset samps(1, npts, samps_data);
225  std::vector<double> params(&(params_data[0]), &(params_data[npts]));
226  srvf::Plf F(samps, params);
227  srvf::Plf G = constant_speed_reparam(F, 0.0, 1.0);
228  srvf::Plf Fn = composition(F, G);
229
230  srvf::plot::FunctionPlot plot;
231  plot.insert(F, srvf::plot::Color(0.0,0.0,1.0));
232  plot.insert(Fn, srvf::plot::Color(0.0,1.0,0.0));
233  plot.insert(G, srvf::plot::Color(1.0,0.0,0.0));
234  srvf::plot::FltkGlPlotWindow plotwin(800,400,
235    "constant_speed_reparam_test1: F(blue), Fn(green), and G(red)");
236  plotwin.add_plot(static_cast<srvf::plot::Plot*>(&plot));
237  plotwin.show();
238  Fl::run();
239}
240
241BOOST_AUTO_TEST_SUITE_END()