vspline 1.1.0
Generic C++11 Code for Uniform B-Splines
quickstart.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/// \file quickstart.cc
33///
34/// \brief sample code from the documentation
35///
36/// just the bits of code given in the 'Quickstart' section of the
37/// documentation overview
38///
39/// compile: clang++ -std=c++11 -o quickstart -pthread quickstart.cc
40
41#include <vspline/vspline.h>
42#include <iostream>
43#include <iomanip>
44
45using namespace std ;
46using namespace vigra ;
47using namespace vspline ;
48
49int main ( int argc , char * argv[] )
50{
51 // given a vigra::MultiArray of data
52
53 vigra::MultiArray < 2 , float > a ( 10 , 20 ) ;
54
55 // let's initialize the whole array with 42
56
57 a = 42 ;
58
59 typedef vspline::bspline < float , 2 > spline_type ; // fix the type of the spline
60
61 spline_type bspl ( a.shape() ) ; // create bspline object 'bspl' suitable for your data
62
63 bspl.core = a ; // copy the source data into the bspline object's 'core' area
64
65 bspl.prefilter() ; // run prefilter() to convert original data to b-spline coefficients
66
67 // for a 2D spline, we want 2D coordinates
68
69 typedef vigra::TinyVector < float , 2 > coordinate_type ;
70
71 // get the appropriate evaluator type
72
74
75 // create the evaluator
76
77 eval_type ev ( bspl ) ;
78
79 // create variables for input and output:
80
81 float x = 3 , y = 4 ;
82 coordinate_type coordinate ( x , y ) ;
83 float result ;
84
85 // use the evaluator to produce the result
86
87 ev.eval ( coordinate , result ) ; // evaluate at (x,y)
88
89 auto r = ev ( coordinate ) ; // alternative evaluation as a functor
90
91 assert ( r == result ) ;
92
93 // create a 1D array containing (2D) coordinates into 'a'
94
95 vigra::MultiArray < 1 , coordinate_type > coordinate_array ( 3 ) ;
96
97 // we initialize the coordinate array by hand...
98
99 coordinate_array[0] = coordinate_array[1] = coordinate_array[2] = coordinate ;
100
101 // create an array to accomodate the result of the remap operation
102
103 vigra::MultiArray < 1 , float > target_array ( 3 ) ;
104
105 // perform the remap
106
107 vspline::remap ( a , coordinate_array , target_array ) ;
108
109 auto ic = coordinate_array.begin() ;
110 for ( auto k : target_array )
111 assert ( k == ev ( *(ic++) ) ) ;
112
113 // instead of the remap, we can use transform, passing the evaluator for
114 // the b-spline over 'a' instead of 'a' itself. the result is the same.
115
116 vspline::transform ( ev , coordinate_array , target_array ) ;
117
118 // create a 2D array for the index-based transform operation
119
120 vigra::MultiArray < 2 , float > target_array_2d ( 3 , 4 ) ;
121
122 // use transform to evaluate the spline for the coordinates of
123 // all values in this array
124
125 vspline::transform ( ev , target_array_2d ) ;
126
127 for ( int x = 0 ; x < 3 ; x ++ )
128 {
129 for ( y = 0 ; y < 4 ; y++ )
130 {
131 coordinate_type c { float(x) , float(y) } ;
132 assert ( target_array_2d [ c ] == ev ( c ) ) ;
133 }
134 }
135
136 vigra::MultiArray < 2 , float > b ( 10 , 20 ) ;
137 vspline::transform ( ev , b ) ;
138
139 auto ia = a.begin() ;
140 for ( auto r : b )
141 assert ( vigra::closeAtTolerance ( *(ia++) , r , .00001 ) ) ;
142
143 vigra::MultiArray < 2 , float > c ( 10 , 20 ) ;
144 vspline::restore ( bspl , c ) ; // TODO: problem with g++
145
146 auto ib = b.begin() ;
147 for ( auto & ic : c )
148 assert ( vigra::closeAtTolerance ( *(ib++) , ic , .00001 ) ) ;
149}
vigra::TinyVector< float, 2 > coordinate_type
Definition: ca_correct.cc:107
vspline::bspline< pixel_type, 2 > spline_type
Definition: ca_correct.cc:111
Definition: basis.h:79
void transform(const unary_functor_type &functor, const vigra::MultiArrayView< dimension, typename unary_functor_type::in_type > &input, vigra::MultiArrayView< dimension, typename unary_functor_type::out_type > &output, int njobs=vspline::default_njobs, vspline::atomic< bool > *p_cancel=0)
implementation of two-array transform using wielding::coupled_wield.
Definition: transform.h:211
void restore(const vspline::bspline< value_type, dimension > &bspl, vigra::MultiArrayView< dimension, value_type > &target)
restore restores the original data from the b-spline coefficients. This is done efficiently using a s...
Definition: transform.h:1429
void remap(const vigra::MultiArrayView< cf_dimension, original_type > &input, const vigra::MultiArrayView< trg_dimension, coordinate_type > &coordinates, vigra::MultiArrayView< trg_dimension, result_type > &output, bcv_type< bcv_dimension > bcv=bcv_type< bcv_dimension >(MIRROR), int degree=3, int njobs=vspline::default_njobs, vspline::atomic< bool > *p_cancel=0)
Implementation of 'classic' remap, which directly takes an array of values and remaps it,...
Definition: transform.h:600
int main(int argc, char *argv[])
Definition: quickstart.cc:49
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
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
includes all headers from vspline (most of them indirectly)