Field3D
MIPUtil.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------//
2
3/*
4 * Copyright (c) 2009 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// Header include
45#include "MIPUtil.h"
46
47// System includes
48#include <cmath>
49
50// Library includes
51#include <boost/foreach.hpp>
52
53// Project includes
54#include "CoordSys.h"
55
56//----------------------------------------------------------------------------//
57
59
60//----------------------------------------------------------------------------//
61
62namespace detail {
63
64 //--------------------------------------------------------------------------//
65
66 const std::string k_mipOffsetStr = "mipoffset";
67
68 //--------------------------------------------------------------------------//
69
70 V3i mipResolution(const V3i &baseRes, const size_t level, const V3i &add)
71 {
72 const float factor = 1.0 / (1 << level);
73 const V3f floatRes(baseRes);
74 return V3i(static_cast<int>(std::ceil(floatRes.x * factor)) + add.x,
75 static_cast<int>(std::ceil(floatRes.y * factor)) + add.y,
76 static_cast<int>(std::ceil(floatRes.z * factor)) + add.z);
77 }
78
79 //--------------------------------------------------------------------------//
80
83 const V3i &/*baseRes*/,
84 const Box3i &extents,
85 const size_t level)
86 {
87 typedef MatrixFieldMapping::MatrixCurve MatrixCurve;
88
89 FieldMapping::Ptr mapping = base->mapping();
90
91 const V3i zero = V3i(0);
93 const float mult = 1 << level;
94 const V3i res = extents.size() + V3i(1);
95
96 // Compute offset of current level
97 const V3i offset((mipOff.x >> level) << level,
98 (mipOff.y >> level) << level,
99 (mipOff.z >> level) << level);
100
101 // Difference between current offset and base offset is num voxels
102 // to offset current level by
103 const V3d diff = offset - mipOff;
104
107 // Local space positions
108 const V3d lsOrigin(0.0), lsX(1.0, 0.0, 0.0), lsY(0.0, 1.0, 0.0),
109 lsZ(0.0, 0.0, 0.1);
110 // Find base voxel size
111 const V3f wsBaseVoxelSize = mfm->wsVoxelSize(0, 0, 0);
112 // Compute current levels' voxel size
113 const V3f wsVoxelSize = wsBaseVoxelSize * mult;
114 // Grab the matrices
115 const MatrixCurve::SampleVec lsToWsSamples = mfm->localToWorldSamples();
116 // New mapping to construct
118 // For each time sample
119 BOOST_FOREACH (const MatrixCurve::Sample &sample, lsToWsSamples){
120 // Find origin and orientation vectors
122 mfm->localToWorld(lsOrigin, wsOrigin, sample.first);
123 mfm->localToWorld(lsX, wsX, sample.first);
124 mfm->localToWorld(lsY, wsY, sample.first);
125 mfm->localToWorld(lsZ, wsZ, sample.first);
126 // Normalize orientation vectors
127 wsX = (wsX - wsOrigin).normalized();
128 wsY = (wsY - wsOrigin).normalized();
129 wsZ = (wsZ - wsOrigin).normalized();
130 // Origin shift due to mip offset
131 wsOrigin += wsX * wsBaseVoxelSize.x * diff.x;
132 wsOrigin += wsY * wsBaseVoxelSize.y * diff.y;
133 wsOrigin += wsZ * wsBaseVoxelSize.z * diff.z;
134 // Mult by voxel size
135 wsX *= wsVoxelSize.x * res.x;
136 wsY *= wsVoxelSize.y * res.y;
137 wsZ *= wsVoxelSize.z * res.z;
138 // Construct new mapping
140 // Update mapping
141 newMapping->setLocalToWorld(sample.first, mtx);
142 }
143 // Done
144 return newMapping;
145 } else {
146 // For non-uniform grids, there is nothing we can do.
147 return mapping;
148 }
149 }
150
151 //--------------------------------------------------------------------------//
152
153} // namespace detail
154
155//----------------------------------------------------------------------------//
156
158{
159 V3d wsOrigin(0.0), vsOrigin;
160
161 f.mapping()->worldToVoxel(wsOrigin, vsOrigin);
162
163 V3i offset(-static_cast<int>(std::floor(vsOrigin.x + 0.5)),
164 -static_cast<int>(std::floor(vsOrigin.y + 0.5)),
165 -static_cast<int>(std::floor(vsOrigin.z + 0.5)));
166
167 return offset;
168}
169
170//----------------------------------------------------------------------------//
171
173
174//----------------------------------------------------------------------------//
Contains utility functions for constructing coordinate systems.
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
V3i computeOffset(const FieldRes &f)
Computes the origin/offset of a field.
Definition MIPUtil.cpp:157
Contains MIP-related utility functions.
Imath::V3i V3i
Definition SpiMathLib.h:71
Imath::V3d V3d
Definition SpiMathLib.h:74
Imath::Box3i Box3i
Definition SpiMathLib.h:77
Imath::V3f V3f
Definition SpiMathLib.h:73
Imath::M44d M44d
Definition SpiMathLib.h:82
#define FIELD3D_MTX_T
Definition StdMathLib.h:99
FieldMetadata & metadata()
accessor to the m_metadata class
Definition Field.h:155
boost::intrusive_ptr< FieldMapping > Ptr
V3i vecIntMetadata(const std::string &name, const V3i &defaultVal) const
Tries to retrieve a V3i metadata value. Returns the specified default value if no metadata was found.
FieldMapping::Ptr mapping()
Returns a pointer to the mapping.
Definition Field.h:263
Represents the mapping of a field by a matrix transform.
boost::intrusive_ptr< MatrixFieldMapping > Ptr
Convenience typedef.
const std::string k_mipOffsetStr
Definition MIPUtil.cpp:66
FIELD3D_API V3i mipResolution(const V3i &baseRes, const size_t level, const V3i &add)
Definition MIPUtil.cpp:70
FIELD3D_API FieldMapping::Ptr adjustedMIPFieldMapping(const FieldRes *base, const V3i &baseRes, const Box3i &extents, const size_t level)
Definition MIPUtil.cpp:82
#define FIELD3D_NAMESPACE_SOURCE_CLOSE
Definition ns.h:60