vspline 1.1.0
Generic C++11 Code for Uniform B-Splines
slice3.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 slice3.cc
33///
34/// \brief create 2D image data from a 3D spline
35///
36/// build a 3D volume from samples of the RGB colour space
37/// build a spline over it and extract a 2D slice
38///
39/// Here we use a quick shot solution.
40/// Oftentimes all it takes is a single run of an interpolation with
41/// as little programming effort as possible, never mind the performance.
42/// Again we use a b-spline with double-precision voxels as value_type,
43/// but instead of using vspline::transform, we simply run the calculations
44/// in loops.
45///
46/// compile with:
47/// clang++ -std=c++11 -o slice3 -O3 -pthread slice3.cc -lvigraimpex
48/// g++ also works.
49
50#include <iostream>
51
52#include <vspline/vspline.h>
53
54#include <vigra/stdimage.hxx>
55#include <vigra/imageinfo.hxx>
56#include <vigra/impex.hxx>
57
58// voxel_type is the source data type
59typedef vigra::RGBValue < double , 0 , 1 , 2 > voxel_type ;
60
61// pixel_type is the result type, here we use a vigra::RGBValue for a change
62typedef vigra::RGBValue < unsigned char , 0 , 1 , 2 > pixel_type ;
63
64// coordinate_type has a 3D coordinate
65typedef vigra::TinyVector < float , 3 > coordinate_type ;
66
67int main ( int argc , char * argv[] )
68{
69 // we want a b-spline with natural boundary conditions
70 vigra::TinyVector < vspline::bc_code , 3 > bcv ( vspline::NATURAL ) ;
71
72 // create quintic 3D b-spline object containing voxels
74 bspl ( vigra::Shape3 ( 10 , 10 , 10 ) , 5 , bcv ) ;
75
76 // fill the b-spline's core with a three-way gradient
77
78 for ( int z = 0 ; z < 10 ; z++ )
79 {
80 for ( int y = 0 ; y < 10 ; y++ )
81 {
82 for ( int x = 0 ; x < 10 ; x++ )
83 {
84 bspl.core ( x , y , z ) = voxel_type ( 25.5 * x , 25.5 * y , 25.5 * z ) ;
85 }
86 }
87 }
88
89 // prefilter the b-spline
90
91 bspl.prefilter() ;
92
93 // get an evaluator for the b-spline
94
95 auto ev = vspline::make_evaluator ( bspl ) ;
96
97 // this is where the result should go:
98
99 vigra::MultiArray < 2 , pixel_type > target ( vigra::Shape2 ( 1920 , 1080 ) ) ;
100
101 // we want the pick-up coordinates to follow this scheme:
102 // pick ( x , y ) = ( x , 1 - x , y )
103 // scaled appropriately
104
105 for ( int y = 0 ; y < 1080 ; y++ )
106 {
107 for ( int x = 0 ; x < 1920 ; x++ )
108 {
109 // calculate the pick-up coordinate
110 coordinate_type pick { x / 192.0f ,
111 10.0f - x / 192.0f ,
112 y / 108.0f } ;
113 // call the evaluator and store the result to 'target'
114 target ( x , y ) = ev ( pick ) ;
115 }
116 }
117
118 // store the result with vigra impex
119 vigra::ImageExportInfo imageInfo ( "slice.tif" );
120
121 vigra::exportImage ( target ,
122 imageInfo
123 .setPixelType("UINT8")
124 .setCompression("100")
125 .setForcedRangeMapping ( 0 , 255 , 0 , 255 ) ) ;
126
127 std::cout << "result was written to slice.tif" << std::endl ;
128 exit ( 0 ) ;
129}
vigra::TinyVector< float, 2 > coordinate_type
Definition: ca_correct.cc:107
vspline::grok_type< bspl_coordinate_type< spline_type, rc_type >, result_type, _vsize > make_evaluator(const spline_type &bspl, vigra::TinyVector< int, spline_type::dimension > dspec=vigra::TinyVector< int, spline_type::dimension >(0), int shift=0)
make_evaluator is a factory function, producing a functor which provides access to an evaluator objec...
Definition: eval.h:2292
@ NATURAL
Definition: common.h:75
int main(int argc, char *argv[])
Definition: slice3.cc:67
vigra::RGBValue< unsigned char, 0, 1, 2 > pixel_type
Definition: slice3.cc:62
vigra::RGBValue< double, 0, 1, 2 > voxel_type
Definition: slice3.cc:59
vigra::TinyVector< float, 3 > coordinate_type
Definition: slice3.cc:65
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)