vspline 1.1.0
Generic C++11 Code for Uniform B-Splines
gradient2.cc
Go to the documentation of this file.
1/************************************************************************/
2/* */
3/* vspline - a set of generic tools for creation and evaluation */
4/* of uniform b-splines */
5/* */
6/* Copyright 2015 - 2023 by Kay F. Jahnke */
7/* */
8/* Permission is hereby granted, free of charge, to any person */
9/* obtaining a copy of this software and associated documentation */
10/* files (the "Software"), to deal in the Software without */
11/* restriction, including without limitation the rights to use, */
12/* copy, modify, merge, publish, distribute, sublicense, and/or */
13/* sell copies of the Software, and to permit persons to whom the */
14/* Software is furnished to do so, subject to the following */
15/* conditions: */
16/* */
17/* The above copyright notice and this permission notice shall be */
18/* included in all copies or substantial portions of the */
19/* Software. */
20/* */
21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
23/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
24/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
25/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
26/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
27/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
28/* OTHER DEALINGS IN THE SOFTWARE. */
29/* */
30/************************************************************************/
31
32/// gradient.cc
33///
34/// If we create a b-spline over an array containing, at each grid point,
35/// the sum of the grid point's coordinates, each 1D row, column, etc will
36/// hold a linear gradient with first derivative == 1. If we use NATURAL
37/// BCs, evaluating the spline with real coordinates anywhere inside the
38/// defined range should produce precisely the sum of the coordinates.
39/// This is a good test for both the precision of the evaluation and it's
40/// correct functioning, particularly with higher-D arrays.
41///
42/// In this variant of the program, we use a vspline::domain functor.
43/// This functor provides a handy way to access a b-spline with normalized
44/// coordinates: instead of evaluating coordinates in the range of
45/// [ 0 , core_shape - 1 ] (natural spline coordinates), we pass coordinates
46/// in the range of [ 0 , 1 ] to the domain object, which is chained to
47/// the evaluator and passes natural spline coordinates to it.
48///
49/// compile: clang++ -O3 -DUSE_VC -march=native -std=c++11 -pthread \
50/// -o gradient2 gradient2.cc -lVc
51/// (with Vc; use -DUSE_HWY and -lhwy for highway, or -std=c++17 and -DUSE_STDSIMD
52/// for the std::simd backend)
53/// or clang++ -O3 -march=native -std=c++11 -pthread -o gradient2 gradient2.cc
54
55#include <random>
56#include <iostream>
57
58#include <vspline/vspline.h>
59
60int main ( int argc , char * argv[] )
61{
63 typedef typename spline_type::shape_type shape_type ;
64 typedef typename spline_type::view_type view_type ;
65 typedef typename spline_type::bcv_type bcv_type ;
66
67 // let's have a knot point array with nicely odd shape
68
69 shape_type core_shape = { 35 , 43 , 19 } ;
70
71 // we have to use a longish call to the constructor since we want to pass
72 // 0.0 to 'tolerance' and it's way down in the argument list, so we have to
73 // explicitly pass a few arguments which usually take default values before
74 // we have a chance to pass the tolerance
75
76 spline_type bspl ( core_shape , // shape of knot point array
77 3 , // cubic b-spline
78 bcv_type ( vspline::NATURAL ) , // natural boundary conditions
79 0.0 ) ; // no tolerance
80
81 // get a view to the bspline's core, to fill it with data
82
83 view_type core = bspl.core ;
84
85 // create the gradient in each dimension
86
87 for ( int d = 0 ; d < bspl.dimension ; d++ )
88 {
89 for ( int c = 0 ; c < core_shape[d] ; c++ )
90 core.bindAt ( d , c ) += c ;
91 }
92
93 // now prefilter the spline
94
95 bspl.prefilter() ;
96
97 // set up an evaluator
98
99 typedef vigra::TinyVector < double , 3 > coordinate_type ;
101
102 evaluator_type inner_ev ( bspl ) ;
103
104 // create the domain from the bspline:
105
106 auto dom = vspline::domain < coordinate_type >
107 ( coordinate_type(0) , coordinate_type(1) , bspl ) ;
108
109 // chain domain and inner evaluator
110
111 auto ev = dom + inner_ev ;
112
113 // we want to bombard the evaluator with random in-range coordinates
114
115 std::random_device rd;
116 std::mt19937 gen(rd());
117 // std::mt19937 gen(12345); // fix starting value for reproducibility
118
120
121 // here comes our test, feed 100 random 3D coordinates and compare the
122 // evaluator's result with the expected value, which is precisely the
123 // sum of the coordinate's components
124
125 for ( int times = 0 ; times < 100 ; times++ )
126 {
127 for ( int d = 0 ; d < bspl.dimension ; d++ )
128 {
129 // note the difference to the code in gardient.cc here:
130 // our test coordinates are in the range of [ 0 , 1 ]
131 c[d] = std::generate_canonical<double, 20>(gen) ;
132 }
133 double result ;
134 ev.eval ( c , result ) ;
135
136 // 'result' is the same as in gradient.cc, but we have to calculate
137 // the expected value differently.
138
139 double delta = result - sum ( c * ( core_shape - 1 ) ) ;
140
141 std::cout << "eval(" << c << ") = "
142 << result << " -> delta = " << delta << std::endl ;
143 }
144}
vigra::TinyVector< float, 2 > coordinate_type
Definition: ca_correct.cc:107
vspline::bspline< pixel_type, 2 > spline_type
Definition: ca_correct.cc:111
class evaluator encodes evaluation of a spline-like object. This is a generalization of b-spline eval...
Definition: eval.h:1718
int main(int argc, char *argv[])
gradient.cc
Definition: gradient2.cc:60
vigra::TinyVector< vspline::bc_code, dimension > bcv_type
a type for a set of boundary condition codes, one per axis
Definition: transform.h:554
@ NATURAL
Definition: common.h:75
class bspline now builds on class bspline_base, adding coefficient storage, while bspline_base provid...
Definition: bspline.h:499
view_type core
Definition: bspline.h:566
vigra::TinyVector< std::ptrdiff_t, dimension > shape_type
type of a multidimensional index type
Definition: bspline.h:199
void prefilter(vspline::xlf_type boost=vspline::xlf_type(1), int njobs=vspline::default_njobs)
prefilter converts the knot point data in the 'core' area into b-spline coefficients....
Definition: bspline.h:815
vigra::TinyVector< bc_code, dimension > bcv_type
nD type for one boundary condition per axis
Definition: bspline.h:203
vigra::MultiArrayView< dimension, value_type > view_type
data are read and written to vigra MultiArrayViews:
Definition: bspline.h:539
includes all headers from vspline (most of them indirectly)