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 
62 namespace detail {
63 
64  //--------------------------------------------------------------------------//
65 
66  V3i mipResolution(const V3i &baseRes, const size_t level)
67  {
68  const float factor = 1.0 / (1 << level);
69  const V3f floatRes(baseRes);
70  return V3i(static_cast<int>(std::ceil(floatRes.x * factor)),
71  static_cast<int>(std::ceil(floatRes.y * factor)),
72  static_cast<int>(std::ceil(floatRes.z * factor)));
73  }
74 
75  //--------------------------------------------------------------------------//
76 
78  const V3i &/*baseRes*/,
79  const Box3i &extents,
80  const size_t level)
81  {
82  typedef MatrixFieldMapping::MatrixCurve MatrixCurve;
83 
84  const float mult = 1 << level;
85  const V3i res = extents.size() + V3i(1);
86 
87  if (MatrixFieldMapping::Ptr mfm =
88  field_dynamic_cast<MatrixFieldMapping>(mapping)) {
89  // Local space positions
90  const V3d lsOrigin(0.0), lsX(1.0, 0.0, 0.0), lsY(0.0, 1.0, 0.0),
91  lsZ(0.0, 0.0, 0.1);
92  // Find base voxel size
93  const V3f wsBaseVoxelSize = mfm->wsVoxelSize(0, 0, 0);
94  // Compute current levels' voxel size
95  const V3f wsVoxelSize = wsBaseVoxelSize * mult;
96  // Grab the matrices
97  const MatrixCurve::SampleVec lsToWsSamples = mfm->localToWorldSamples();
98  // New mapping to construct
100  // For each time sample
101  BOOST_FOREACH (const MatrixCurve::Sample &sample, lsToWsSamples){
102  // Find origin and orientation vectors
103  V3d wsOrigin, wsX, wsY, wsZ;
104  mfm->localToWorld(lsOrigin, wsOrigin, sample.first);
105  mfm->localToWorld(lsX, wsX, sample.first);
106  mfm->localToWorld(lsY, wsY, sample.first);
107  mfm->localToWorld(lsZ, wsZ, sample.first);
108  // Normalize orientation vectors
109  wsX = (wsX - wsOrigin).normalized();
110  wsY = (wsY - wsOrigin).normalized();
111  wsZ = (wsZ - wsOrigin).normalized();
112  // Mult by voxel size
113  wsX *= wsVoxelSize.x * res.x;
114  wsY *= wsVoxelSize.y * res.y;
115  wsZ *= wsVoxelSize.z * res.z;
116  // Construct new mapping
117  M44d mtx = coordinateSystem(wsX, wsY, wsZ, wsOrigin);
118  // Update mapping
119  newMapping->setLocalToWorld(sample.first, mtx);
120  }
121  // Done
122  return newMapping;
123  } else {
124  // For non-uniform grids, there is nothing we can do.
125  return mapping;
126  }
127  }
128 
129  //--------------------------------------------------------------------------//
130 
131 } // namespace detail
132 
133 //----------------------------------------------------------------------------//
134 
136 
137 //----------------------------------------------------------------------------//
Imath::Box3i Box3i
Definition: SpiMathLib.h:77
Imath::M44d M44d
Definition: SpiMathLib.h:82
#define FIELD3D_NAMESPACE_SOURCE_CLOSE
Definition: ns.h:60
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:119
FIELD3D_API FieldMapping::Ptr adjustedMIPFieldMapping(const FieldMapping::Ptr baseMapping, const V3i &baseRes, const Box3i &extents, const size_t level)
Definition: MIPUtil.cpp:77
Imath::V3i V3i
Definition: SpiMathLib.h:71
boost::intrusive_ptr< FieldMapping > Ptr
Definition: FieldMapping.h:92
Imath::V3d V3d
Definition: SpiMathLib.h:74
Imath::V3f V3f
Definition: SpiMathLib.h:73
Contains MIP-related utility functions.
FIELD3D_API V3i mipResolution(const V3i &baseRes, const size_t level)
Definition: MIPUtil.cpp:66
FIELD3D_VEC3_T< T > ceil(const FIELD3D_VEC3_T< T > &v)
Ceil function for Vec3.
Definition: CoordSys.h:105
Contains utility functions for constructing coordinate systems.
Represents the mapping of a field by a matrix transform.
Definition: FieldMapping.h:327
boost::intrusive_ptr< MatrixFieldMapping > Ptr
Convenience typedef.
Definition: FieldMapping.h:334