Field3D
Resample.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 #include "Resample.h"
45 
46 //----------------------------------------------------------------------------//
47 
49 
50 //----------------------------------------------------------------------------//
51 
52 namespace detail {
53 
54  //--------------------------------------------------------------------------//
55 
56  Box3i srcSupportBBox(const V3f &tgtP, const float support, const V3i &doUpres,
57  const V3f &srcSize, const V3f &tgtSize)
58  {
59  Box3i srcBox;
60  for (int dim = 0; dim < 3; ++dim) {
61  if (doUpres[dim]) {
62  srcBox.min[dim] =
63  static_cast<int>(std::floor(tgtP[dim] * tgtSize[dim] / srcSize[dim] -
64  support));
65  srcBox.max[dim] =
66  static_cast<int>(std::ceil(tgtP[dim] * tgtSize[dim] / srcSize[dim] +
67  support)) - 1;
68  } else {
69  srcBox.min[dim] =
70  static_cast<int>(std::floor((tgtP[dim] - support) *
71  tgtSize[dim] / srcSize[dim]));
72  srcBox.max[dim] =
73  static_cast<int>(std::ceil((tgtP[dim] + support) *
74  tgtSize[dim] / srcSize[dim]));
75  }
76  }
77  return srcBox;
78  }
79 
80  //--------------------------------------------------------------------------//
81 
82  std::pair<int, int>
83  srcSupportBBox(const float &tgtP, const float support, const bool doUpres,
84  const float &srcSize, const float &tgtSize)
85  {
86  std::pair<int, int> srcInterval;
87  if (doUpres) {
88  srcInterval.first =
89  static_cast<int>(std::floor(tgtP * tgtSize / srcSize - support));
90  srcInterval.second =
91  static_cast<int>(std::ceil(tgtP * tgtSize / srcSize + support)) - 1;
92  } else {
93  srcInterval.first =
94  static_cast<int>(std::floor((tgtP - support) * tgtSize / srcSize));
95  srcInterval.second =
96  static_cast<int>(std::ceil((tgtP + support) * tgtSize / srcSize));
97  }
98  return srcInterval;
99  }
100 
101  //--------------------------------------------------------------------------//
102 
103  V3f getDist(const V3i &doUpres, const V3f &srcP, const V3f &tgtP,
104  const V3f &srcSize, const V3f &tgtSize)
105  {
106  V3f dist;
107  for (int dim = 0; dim < 3; ++dim) {
108  if (doUpres[dim]) {
109  const float tgtSrc = tgtP[dim] * tgtSize[dim] / srcSize[dim];
110  dist[dim] = std::abs(tgtSrc - srcP[dim]);
111  } else {
112  const float srcTgt = srcP[dim] * srcSize[dim] / tgtSize[dim];
113  dist[dim] = std::abs(srcTgt - tgtP[dim]);
114  }
115  }
116  return dist;
117  }
118 
119  //--------------------------------------------------------------------------//
120 
121  float getDist(const bool doUpres, const float &srcP, const float &tgtP,
122  const float &srcSize, const float &tgtSize)
123  {
124  if (doUpres) {
125  const float tgtSrc = tgtP * tgtSize / srcSize;
126  return std::abs(tgtSrc - srcP);
127  } else {
128  const float srcTgt = srcP * srcSize / tgtSize;
129  return std::abs(srcTgt - tgtP);
130  }
131  }
132 
133  //--------------------------------------------------------------------------//
134 
135 } // namespace detail
136 
137 //----------------------------------------------------------------------------//
138 
140 
141 //----------------------------------------------------------------------------//
Imath::Box3i Box3i
Definition: SpiMathLib.h:77
#define FIELD3D_NAMESPACE_SOURCE_CLOSE
Definition: ns.h:60
Contains functions for resampling fields.
Imath::V3i V3i
Definition: SpiMathLib.h:71
V3f getDist(const V3i &doUpres, const V3f &srcP, const V3f &tgtP, const V3f &srcSize, const V3f &tgtSize)
Definition: Resample.cpp:103
FIELD3D_VEC3_T< T > floor(const FIELD3D_VEC3_T< T > &v)
Floor function for Vec3.
Definition: CoordSys.h:95
Box3i srcSupportBBox(const V3f &tgtP, const float support, const V3i &doUpres, const V3f &srcSize, const V3f &tgtSize)
Definition: Resample.cpp:56
Imath::V3f V3f
Definition: SpiMathLib.h:73
FIELD3D_VEC3_T< T > ceil(const FIELD3D_VEC3_T< T > &v)
Ceil function for Vec3.
Definition: CoordSys.h:105