OWL
Loading...
Searching...
No Matches
array3D.h
1// ======================================================================== //
2// Copyright 2019-2020 Ingo Wald //
3// //
4// Licensed under the Apache License, Version 2.0 (the "License"); //
5// you may not use this file except in compliance with the License. //
6// You may obtain a copy of the License at //
7// //
8// http://www.apache.org/licenses/LICENSE-2.0 //
9// //
10// Unless required by applicable law or agreed to in writing, software //
11// distributed under the License is distributed on an "AS IS" BASIS, //
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
13// See the License for the specific language governing permissions and //
14// limitations under the License. //
15// ======================================================================== //
16
17#pragma once
18
19#include "owl/common/math/vec.h"
20#include "owl/common/parallel/parallel_for.h"
21
22namespace owl {
23 namespace common {
24 namespace array3D {
25
26 inline __both__ int64_t linear(const vec3i &ID,
27 const vec3i &dims)
28 { return ID.x + dims.x*(ID.y + dims.y*(int64_t)ID.z); }
29
30 template<typename Lambda>
31 inline void for_each(const vec3i &dims,
32 const Lambda &lambda)
33 {
34 for (int iz=0;iz<dims.z;iz++)
35 for (int iy=0;iy<dims.y;iy++)
36 for (int ix=0;ix<dims.x;ix++)
37 lambda(vec3i(ix,iy,iz));
38 }
39
40 template<typename Lambda>
41 inline void for_each(const vec3i &begin,
42 const vec3i &end,
43 const Lambda &lambda)
44 {
45 for (int iz=begin.z;iz<end.z;iz++)
46 for (int iy=begin.y;iy<end.y;iy++)
47 for (int ix=begin.x;ix<end.x;ix++)
48 lambda(vec3i(ix,iy,iz));
49 }
50
51 template<typename Lambda>
52 inline void parallel_for(const vec3i &dims, const Lambda &lambda)
53 {
54 owl::common::parallel_for
55 (dims.x*(size_t)dims.y*dims.z,[&](size_t index){
56 lambda(vec3i(index%dims.x,
57 (index/dims.x)%dims.y,
58 index/((size_t)dims.x*dims.y)));
59 });
60 }
61 template<typename Lambda>
62 inline void serial_for(const vec3i &dims, const Lambda &lambda)
63 {
64 owl::common::serial_for(dims.x*size_t(dims.y)*dims.z,
65 [&](size_t index){
66 lambda(vec3i(index%dims.x,
67 (index/dims.x)%dims.y,
68 index/((size_t)dims.x*dims.y)));
69 });
70 }
71
72 inline __both__ bool validIndex(const vec3i &idx,
73 const vec3i &dims)
74 {
75 if (idx.x < 0 || idx.x >= dims.x) return false;
76 if (idx.y < 0 || idx.y >= dims.y) return false;
77 if (idx.z < 0 || idx.z >= dims.z) return false;
78 return true;
79 }
80
81 } // owl::common::array3D
82 } // owl::common
83} // owl