#include #include #include #include /* * Create a 1 dimensional normalized gauss kernel * * arameters! * " sigma! the parameter o the gaussian * " kernel_size! the size o the kernel $has to be odd% * " kernel! an arra& 'ith size kernel_size elements that * has to be illed 'ith the kernel values * */ void (mage!!create_gaussian_kernel_1d$ loat sigma) int kernel_size) loat* kernel% cg_assert$kernel_size + , -- 1% // 00! calculate ilter values as described on the exercise sheet. // 2ake sure &our kernel is normalized int lo'estx - "$loor$kernel_size / ,%% loat normalize - 3.3
or $int i - 3 i < kernel_size 44i% kernel5i6 - $1.3 / $glm!!s7rt$,.3 * glm!!pi<loat>$%%* sigma%% * glm!!po'$glm!!e<loat>$%) "$$lo'estx% * $lo'estx% / $,.3* sigma * sigma%%% lo'estx44 8
or $int i - 3 normalize 8 or $int i - 3 kernel5i6 8
i < kernel_size 44i% 4- kernel5i6 i < kernel_size 44i% - kernel5i6 / normalize
8 /* * Create a , dimensional 7uadratic and normalized gauss kernel * * arameters! * " sigma! the parameter o the gaussian
* " kernel_size! the size o the kernel $has to be odd% * " kernel! an arra& 'ith kernel_size*kernel_size elements that * has to be illed 'ith the kernel values */ void (mage!!create_gaussian_kernel_,d$ loat sigma) int kernel_size) loat* kernel% cg_assert$kernel_size + , -- 1%
int lo'estnumber - "$loor$kernel_size / ,%% loat normalize - 3.3
or $int 9 - 3 9 < kernel_size 449% or $int i - 3 i < kernel_size 44i% kernel5i 4 9 * kernel_size6 - $1.3 / $,.3 * glm!!pi<loat>$% * $sigma * sigma%%%* glm!!po'$glm!!e<loat>$%) "$$$lo'estnumber 4 9% * $lo'estnumber 4 9% 4 $lo'estnumber 4 i% * $lo'estnumber 4 i%% / $,.3 * sigma * sigma%%%
8 8 or $int 9 - 3 9 < kernel_size 449% or $int i - 3 i < kernel_size 44i% normalize 4- kernel5i 4 9*kernel_size6 8 8 or $int 9 - 3 9 < kernel_size 449% or $int i - 3 i < kernel_size 44i% kernel5i 4 9 * kernel_size6 - kernel5i 4 9*kernel_size6 / normalize 8 8 8
/* * Convolve an image 'ith a ,d ilter kernel * * arameters! * " kernel_size! the size o the ,d"kernel * " kernel! the ,d"kernel 'ith kernel_size*kernel_size elements * " 'rap_mode! needs to be kno'n to handle repeating * textures correctl& */ void (mage!!ilter$(mage *target) int kernel_size) loat* kernel) :rap2ode 'rap_mode% const cg_assert$kernel_size + , -- 1 ;; kernel size should be odd.% cg_assert$kernel_size > 3 ;; kernel size should be greater than 3.% cg_assert$target% cg_assert$target">get:idth$% -- m_'idth ;; target">get=eight$% -- m_height% int lo'estnumber - "$loor$kernel_size / ,%% or $int i - 3 i < m_'idth 44i% //rupert hier 'idth statt height or $int 9 - 3 9 < m_height 449% // height statt 'idth glm!!vec ne'pixel or $int a - 3 a < kernel_size 44a% or $int b - 3 b < kernel_size 44b% ne'pixel 4- kernel5a 4 kernel_size*b6 * $getixel$i 4 lo'estnumber 4 a) 9 4 lo'estnumber 4 b%% 8 // a 4 kernelsize *b nachragen 8 target">setixel$i) 9) ne'pixel% 8 8 8
/* /* * Convolve an image 'ith a separable 1d ilter kernel * * arameters! * " kernel_size! the size o the 1d kernel * " kernel! the 1d"kernel 'ith kernel_size elements * " 'rap_mode! needs to be kno'n to handle repeating * textures correctl& */ void (mage!!ilter_separable$(mage *target) int kernel_size) loat* kernel) :rap2ode 'rap_mode% const cg_assert $kernel_size+,--1 ;; kernel size should be odd.% cg_assert $kernel_size > 3 ;; kernel size should be greater than 3.% cg_assert $target% cg_assert $target">get:idth$% -- m_'idth ;; target">get=eight$% -- m_height%
(mage temp$m_'idth) m_height% int lo'estnumber - "loor$$kernel_size / ,%% or $int i - 3 i < m_'idth 44i% or $int 9 - 3 9 < m_height 944% glm!!vec ixel=eight or $int k - 3 k < kernel_size k44% ixel=eight 4- kernel5k6 * getixel$i) 94 lo'estnumber 4 k) 'rap_mode% 8 temp.setixel$i) 9) ixel=eight% 8 8
k)9)
or $int i - 3 i < m_'idth 44i% or $int 9 - 3 9 < m_height 944% glm!!vec ixel:idth or $int k - 3 k < kernel_size k44% ixel:idth 4- kernel5k6 * temp.getixel$i4 lo'estnumber 4 'rap_mode% 8 target">setixel$i) 9) ixel:idth% 8 8 // 00! realize the ,d convolution 'ith t'o // convolutions o the image 'ith a 1d"kernel. // convolve the image horizontall& and then convolve // the result verticall& $or vise"versa%. // // use the methods getixel$x) &) 'rap_mode% and // setixel$x) &) value% to get and set pixels o an image
8 /** * ?eorder triangle indices in the vector triangle_indices * in the range 5irst_triangle_idx) irst_triangle_idx4num_triangles"16 * so that the range is split in t'o sets 'here all triangles in the irst set * are less than e7ual than the median) and all triangles in the second set * are greater than e7ual the median. * * 0rdering $less than% is deined b& the ordering o triangle * bounding box centers along the given axis. * * riangle indices 'ithin a set need not be sorted. * * he resulting sets must have an e7ual number o elements i num_triangles * is even. 0ther'ise) one o the sets must have one more element. * * @or example) A triangles must be split ". B riangles must be split * " or ". * * arameters! * " irst_triangle_idx! he index o the irst triangle in the given range. * " num_triangles! he number o triangles in the range. * " axis! he sort axis. 3 is D) 1 is E) , is F. * * ?eturn value! * " he number o triangles in the irst set. */
int GH=!!reorder_triangles_median$ int irst_triangle_idx) int num_triangles) int axis% cg_assert$irst_triangle_idx < static_cast$triangle_indices.size$%%% cg_assert$irst_triangle_idx >- 3% cg_assert$num_triangles <- static_cast$triangle_indices.size$% " irst_triangle_idx%% cg_assert$num_triangles > 1% cg_assert$axis >- 3% cg_assert$axis < % // 00! (mplement reordering. return 3 8 /* * Guild a GH= recursivel& using the ob9ect median split heuristic. * * his method must irst ull& initialize the current node) and then * potentiall& split it. * * I node must not be split i it contains 2ID_?(IJKLMN_(J_LMI@ triangles or * less. Jo lea node ma& be empt&. Ill nodes must have either t'o or no * children. * * Ose reorder_triangles_median to perorm the split in triangle_indices. * Nplit along D or depth 3. hen) proceed in the order E) F) D) E) F) D) E) .. * * arameters! * " node_idx! he index o the node to be split. * " irst_triangle_idx! In index into the arra& triangle_indices. (t points * to the irst triangle contained in the current node. * " num_triangles! he number o triangles contained in the current node. */ void GH=!! build_bvh$int node_idx) int irst_triangle_idx) int num_triangles) int depth% cg_assert$num_triangles > 3% cg_assert$node_idx >- 3% cg_assert$node_idx < static_cast$nodes.size$%%% cg_assert$depth >- 3% Jode; node - nodes5node_idx6 // 00! (mplement node.triangle_idx node.num_triangles node.aabb.min node.aabb.max node.let node.right
recursive build. - irst_triangle_idx - num_triangles - glm!!vec$"@L_2ID% - glm!!vec$@L_2ID% - "1 - "1
8 /* * (ntersect the GH= recursivel&) returning the nearest intersection i * there is one. *
* Caution! GH= nodes ma& overlap. * * arameters! * " ra&! he ra& to intersect the GH= 'ith. * " idx! he node to be intersected. * " nearest_intersection! he distance to the intersection point) i an * intersection 'as ound. 2ust not be changed * other'ise. * " isect! he intersection) i one 'as ound. 2ust not be * changed other'ise. * * ?eturn value! * true i an intersection 'as ound) alse other'ise. */ bool GH=!! intersect_recursive$const ?a& ;ra&) int idx) loat *nearest_intersection) (ntersection* isect% const cg_assert$nearest_intersection% cg_assert$isect% cg_assert$idx >- 3% cg_assert$idx < static_cast$nodes.size$%%% const Jode ;n - nodes5idx6 // his is a lea node. (ntersect all triangles. i$n.let < 3% glm!!vec bar&$3.% bool hit - alse or$int i - 3 i < n.num_triangles i44% int x - triangle_indices5n.triangle_idx 4 i6 loat dist glm!!vec b i$intersect_triangle$ra&.origin) ra&.direction) triangle_soup.vertices5x * 4 36) triangle_soup.vertices5x * 4 16) triangle_soup.vertices5x * 4 ,6) b) dist%% hit - true i$dist <- *nearest_intersection% *nearest_intersection - dist bar& - b cg_assert$x >- 3% i$isect% triangle_soup.ill_intersection$isect) x) *nearest_intersection) bar&% 8 8 8 return hit 8 // his is an inner node. ?ecurse into child nodes. else // 00! (mplement recursive traversal here. 8 return alse 8
// Npectrum and reconstruction are , memor& blocks o the same size 5sx) s&6 // in ro' ma9or la&out) i.e one ro' o size sx ater the other. // // Npectrum contains the complex ourier"coeicients Phatx8_kl8. // // ?econstruct the original gra&scale image using ourier transormationQ void iscrete@ourier,!!reconstruct$ int sx) int s&) std!!complex<loat> const* spectrum) std!!c !!comple plex<lo loat> * reco econstru truction ion% 8 // CK_?MH(N(0J dRdd1,bSS,aRaA3ccBc1T1ddSSSBeed