Field3D
CoordSys.h
Go to the documentation of this file.
1 //----------------------------------------------------------------------------//
2 
3 /*
4  * Copyright (c) 2014 Sony Pictures Imageworks Inc
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the
17  * distribution. Neither the name of Sony Pictures Imageworks nor the
18  * names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior written
20  * permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 //----------------------------------------------------------------------------//
37 
42 //----------------------------------------------------------------------------//
43 
44 #ifndef _INCLUDED_Field3D_CoordSys_H_
45 #define _INCLUDED_Field3D_CoordSys_H_
46 
47 //----------------------------------------------------------------------------//
48 
49 // System includes
50 #include <cmath>
51 
52 #include "Types.h"
53 
54 //----------------------------------------------------------------------------//
55 
56 #include "ns.h"
57 
59 
60 //----------------------------------------------------------------------------//
61 // Utility functions
62 //----------------------------------------------------------------------------//
63 
65 template <typename T>
66 FIELD3D_MTX_T<T> coordinateSystem(const FIELD3D_VEC3_T<T> &e1,
67  const FIELD3D_VEC3_T<T> &e2,
68  const FIELD3D_VEC3_T<T> &e3,
69  const FIELD3D_VEC3_T<T> &origin);
70 
72 template <typename T>
73 FIELD3D_MTX_T<T>
74 coordinateSystem(const FIELD3D_BOX_T<FIELD3D_VEC3_T<T> > &wsBounds);
75 
79 template <typename T>
80 FIELD3D_MTX_T<T>
81 coordinateSystem(const FIELD3D_BOX_T<FIELD3D_VEC3_T<T> > &wsBounds,
82  const FIELD3D_VEC3_T<T> &wsVoxelSize,
83  Box3i &extents);
84 
88 template <typename T>
89 FIELD3D_MTX_T<T>
90 coordinateSystem(const FIELD3D_BOX_T<FIELD3D_VEC3_T<T> > &wsBounds,
91  const FIELD3D_VEC3_T<T> &wsVoxelSize);
92 
93 //----------------------------------------------------------------------------//
94 // Detail namespace
95 //----------------------------------------------------------------------------//
96 
97 namespace detail {
98 
99  //--------------------------------------------------------------------------//
100 
102  template <typename T>
103  FIELD3D_VEC3_T<T>
104  floor(const FIELD3D_VEC3_T<T> &v)
105  {
106  return FIELD3D_VEC3_T<T>(std::floor(v.x), std::floor(v.y), std::floor(v.z));
107  }
108 
109  //--------------------------------------------------------------------------//
110 
112  template <typename T>
113  FIELD3D_VEC3_T<T>
114  ceil(const FIELD3D_VEC3_T<T> &v)
115  {
116  return FIELD3D_VEC3_T<T>(std::ceil(v.x), std::ceil(v.y), std::ceil(v.z));
117  }
118 
119  //--------------------------------------------------------------------------//
120 
121 } // Detail namespace
122 
123 //----------------------------------------------------------------------------//
124 // Template implementations
125 //----------------------------------------------------------------------------//
126 
127 template <typename T>
128 FIELD3D_MTX_T<T> coordinateSystem(const FIELD3D_VEC3_T<T> &e1,
129  const FIELD3D_VEC3_T<T> &e2,
130  const FIELD3D_VEC3_T<T> &e3,
131  const FIELD3D_VEC3_T<T> &origin)
132 {
133  FIELD3D_MTX_T<T> m;
134  m[0][0] = e1.x;
135  m[0][1] = e1.y;
136  m[0][2] = e1.z;
137  m[1][0] = e2.x;
138  m[1][1] = e2.y;
139  m[1][2] = e2.z;
140  m[2][0] = e3.x;
141  m[2][1] = e3.y;
142  m[2][2] = e3.z;
143  m[3][0] = origin.x;
144  m[3][1] = origin.y;
145  m[3][2] = origin.z;
146  return m;
147 }
148 
149 //----------------------------------------------------------------------------//
150 
151 template <typename T>
152 FIELD3D_MTX_T<T>
153 coordinateSystem(const FIELD3D_BOX_T<FIELD3D_VEC3_T<T> > &wsBounds,
154  const FIELD3D_VEC3_T<T> &wsVoxelSize,
155  Box3i &extents)
156 {
157  const FIELD3D_VEC3_T<T> voxelMin =
158  detail::floor<T>(wsBounds.min / wsVoxelSize) * wsVoxelSize;
159  const FIELD3D_VEC3_T<T> voxelMax =
160  detail::ceil<T>(wsBounds.max / wsVoxelSize) * wsVoxelSize;
161 
162  // Resolution
163  extents.min = V3i(detail::floor<T>(voxelMin / wsVoxelSize) + V3f(0.5));
164  extents.max = V3i(detail::floor<T>(voxelMax / wsVoxelSize) + V3f(0.5));
165 
166  // Bounding box
167  const FIELD3D_BOX_T<FIELD3D_VEC3_T<T> > box(voxelMin, voxelMax);
168 
169  return coordinateSystem(box);
170 }
171 
172 //----------------------------------------------------------------------------//
173 
174 template <typename T>
175 FIELD3D_MTX_T<T>
177 (const FIELD3D_BOX_T<FIELD3D_VEC3_T<T> > &wsBounds,
178  const FIELD3D_VEC3_T<T> &wsVoxelSize)
179 {
180  Box3i dummy;
181  return coordinateSystem(wsBounds, wsVoxelSize, dummy);
182 }
183 
184 //----------------------------------------------------------------------------//
185 
186 template <typename T>
187 FIELD3D_MTX_T<T>
189 (const FIELD3D_BOX_T<FIELD3D_VEC3_T<T> > &wsBounds)
190 {
191  FIELD3D_VEC3_T<T> e1(wsBounds.max.x - wsBounds.min.x, 0, 0);
192  FIELD3D_VEC3_T<T> e2(0, wsBounds.max.y - wsBounds.min.y, 0);
193  FIELD3D_VEC3_T<T> e3(0, 0, wsBounds.max.z - wsBounds.min.z);
194  FIELD3D_VEC3_T<T> origin(wsBounds.min);
195  return coordinateSystem(e1, e2, e3, origin);
196 }
197 
198 //----------------------------------------------------------------------------//
199 
201 
202 //----------------------------------------------------------------------------//
203 
204 #endif // Include guard
FIELD3D_NAMESPACE_HEADER_CLOSE
#define FIELD3D_NAMESPACE_HEADER_CLOSE
Definition: ns.h:58
V3i
Imath::V3i V3i
Definition: SpiMathLib.h:71
Types.h
Contains typedefs for the commonly used types in Field3D.
detail
Definition: CoordSys.h:97
V3f
Imath::V3f V3f
Definition: SpiMathLib.h:73
detail::floor
FIELD3D_VEC3_T< T > floor(const FIELD3D_VEC3_T< T > &v)
Floor function for Vec3.
Definition: CoordSys.h:104
ns.h
coordinateSystem
FIELD3D_NAMESPACE_OPEN FIELD3D_MTX_T< T > coordinateSystem(const FIELD3D_VEC3_T< T > &e1, const FIELD3D_VEC3_T< T > &e2, const FIELD3D_VEC3_T< T > &e3, const FIELD3D_VEC3_T< T > &origin)
Constructs a coordinate systems given a set of basis vectors and an origin.
Definition: CoordSys.h:128
detail::ceil
FIELD3D_VEC3_T< T > ceil(const FIELD3D_VEC3_T< T > &v)
Ceil function for Vec3.
Definition: CoordSys.h:114
FIELD3D_NAMESPACE_OPEN
Definition: FieldMapping.cpp:74
Box3i
Imath::Box3i Box3i
Definition: SpiMathLib.h:77
FIELD3D_BOX_T
#define FIELD3D_BOX_T
Definition: StdMathLib.h:75