vspline 1.1.0
Generic C++11 Code for Uniform B-Splines
slice.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 slice.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, using vspline::transform()
38/// In this example, we use an 'array-based' transform, where the coordinates
39/// at which the spline is to be evaluated are held in an array of the same
40/// extent as the target.
41///
42/// compile with:
43/// clang++ -std=c++11 -march=native -o slice -O3 -pthread -DUSE_VC=1 slice.cc \
44/// -lvigraimpex -lVc
45/// (with Vc; use -DUSE_HWY and -lhwy for highway, or -std=c++17 and -DUSE_STDSIMD
46/// for the std::simd backend)
47/// or: clang++ -std=c++11 -march=native -o slice -O3 -pthread slice.cc -lvigraimpex
48/// (without a SIMD backend)
49/// g++ also works.
50
51#include <iostream>
52
53#include <vspline/vspline.h>
54
55#include <vigra/stdimage.hxx>
56#include <vigra/imageinfo.hxx>
57#include <vigra/impex.hxx>
58
59int main ( int argc , char * argv[] )
60{
61 // pixel_type is the result type, an RGB float pixel
62 typedef vigra::TinyVector < float , 3 > pixel_type ;
63
64 // voxel_type is the source data type - the same as pixel_type
65 typedef vigra::TinyVector < float , 3 > voxel_type ;
66
67 // coordinate_3d has a 3D coordinate
68 typedef vigra::TinyVector < float , 3 > coordinate_3d ;
69
70 // warp_type is a 2D array of coordinates
71 typedef vigra::MultiArray < 2 , coordinate_3d > warp_type ;
72
73 // target_type is a 2D array of pixels
74 typedef vigra::MultiArray < 2 , pixel_type > target_type ;
75
76 // we want a b-spline with natural boundary conditions
77 vigra::TinyVector < vspline::bc_code , 3 > bcv ( vspline::NATURAL ) ;
78
79 // create quintic 3D b-spline object containing voxels
81 space ( vigra::Shape3 ( 10 , 10 , 10 ) , 5 , bcv ) ;
82
83 // fill the b-spline's core with a three-way gradient
84 for ( int z = 0 ; z < 10 ; z++ )
85 {
86 for ( int y = 0 ; y < 10 ; y++ )
87 {
88 for ( int x = 0 ; x < 10 ; x++ )
89 {
90 voxel_type & c ( space.core [ vigra::Shape3 ( x , y , z ) ] ) ;
91 c[0] = 25.5 * x ;
92 c[1] = 25.5 * y ;
93 c[2] = 25.5 * z ;
94 }
95 }
96 }
97
98 // prefilter the b-spline
99 space.prefilter() ;
100
101 // get an evaluator for the b-spline
103 ev_type ev ( space ) ;
104
105 // now make a 'warp' array with 1920X1080 3D coordinates
106 warp_type warp ( vigra::Shape2 ( 1920 , 1080 ) ) ;
107
108 // we want the coordinates to follow this scheme:
109 // warp(x,y) = (x,1-x,y)
110 // scaled appropriately
111
112 for ( int y = 0 ; y < 1080 ; y++ )
113 {
114 for ( int x = 0 ; x < 1920 ; x++ )
115 {
116 coordinate_3d & c ( warp [ vigra::Shape2 ( x , y ) ] ) ;
117 c[0] = float ( x ) / 192.0 ;
118 c[1] = 10.0 - c[0] ;
119 c[2] = float ( y ) / 108.0 ;
120 }
121 }
122
123 // this is where the result should go:
124 target_type target ( vigra::Shape2 ( 1920 , 1080 ) ) ;
125
126 // now we perform the transform, yielding the result
127 vspline::transform ( ev , warp , target ) ;
128
129 // store the result with vigra impex
130 vigra::ImageExportInfo imageInfo ( "slice.tif" );
131
132 vigra::exportImage ( target ,
133 imageInfo
134 .setPixelType("UINT8")
135 .setCompression("100")
136 .setForcedRangeMapping ( 0 , 255 , 0 , 255 ) ) ;
137
138 std::cout << "result was written to slice.tif" << std::endl ;
139 exit ( 0 ) ;
140}
vigra::RGBValue< float, 0, 1, 2 > pixel_type
Definition: ca_correct.cc:103
vspline::evaluator< coordinate_type, float > ev_type
Definition: ca_correct.cc:121
vigra::MultiArray< 2, pixel_type > target_type
Definition: ca_correct.cc:115
class evaluator encodes evaluation of a spline-like object. This is a generalization of b-spline eval...
Definition: eval.h:1718
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
@ NATURAL
Definition: common.h:75
vigra::TinyVector< double, 3 > voxel_type
Definition: slice2.cc:62
int main(int argc, char *argv[])
Definition: slice.cc:59
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)