Field3D
FieldWrapper.h
Go to the documentation of this file.
1 //----------------------------------------------------------------------------//
2 
3 #ifndef __F3DUTIL_FIELDWRAPPER_H__
4 #define __F3DUTIL_FIELDWRAPPER_H__
5 
6 //------------------------------------------------------------------------------
7 
8 // Library includes
9 #include <OpenEXR/ImathMatrixAlgo.h>
10 
11 // Project includes
12 #include "DenseField.h"
13 #include "Field3DFile.h"
14 #include "FieldInterp.h"
15 #include "InitIO.h"
16 #include "MIPField.h"
17 #include "MIPUtil.h"
18 #include "SparseField.h"
19 #include "FieldSampler.h"
20 #include "FieldMapping.h"
21 
22 //----------------------------------------------------------------------------//
23 
24 #include "ns.h"
25 
27 
28 //------------------------------------------------------------------------------
29 // ValueRemapOp
30 //------------------------------------------------------------------------------
31 
40 {
41 public:
42 
43  // Typedefs ---
44 
45  typedef boost::shared_ptr<ValueRemapOp> Ptr;
46 
47  // To be implemented by subclasses ---
48 
50  virtual float remap(const float value) const = 0;
52  virtual V3f remap(const V3f &value) const = 0;
53 
54 };
55 
56 //------------------------------------------------------------------------------
57 // FieldWrapper
58 //------------------------------------------------------------------------------
59 
62 template <typename Field_T>
64 {
65  typedef Field_T field_type;
66  typedef std::vector<FieldWrapper> Vec;
67 
68  FieldWrapper(const typename Field_T::Ptr f)
69  : field(f.get()),
70  fieldPtr(f),
71  mapping(f->mapping().get()),
72  vsBounds(continuousBounds(f->dataWindow())),
73  worldScale(1.0),
74  doOsToWs(false),
76  valueRemapOp(NULL)
77  { }
78 
79  void setOsToWs(const M44d &i_osToWs)
80  {
81  osToWs = i_osToWs;
82  wsToOs = osToWs.inverse();
83  // Compute world scale
84  V3d ws(1.0);
85  if (!Imath::extractScaling(osToWs, ws, false)) {
86  Msg::print("WARNING: FieldGroup/FieldWrapper: "
87  "Couldn't extract world scale from object-to-world "
88  "transform. Defaulting to 1.0.");
89  }
90  worldScale = std::max(std::max(ws.x, ws.y), ws.z);
91  // Set boolean
92  doOsToWs = true;
93 
94  // Update wsBounds
97  }
98  }
99 
100  void setWsBoundsOptimization(const bool doWsBoundsOptimization_)
101  {
102  if (!doWsBoundsOptimization_)
103  return;
104  // wsBounds can be set only if mapping is a matrix
105  const MatrixFieldMapping *mtx_mapping =
106  dynamic_cast<const MatrixFieldMapping*>(mapping);
107  if (mtx_mapping) {
108  const float time = 0;
109  M44d vsToWs;
110  if (doOsToWs) {
111  wsToVs = wsToOs * mtx_mapping->worldToVoxel(time);
112  vsToWs = wsToVs.inverse();
113  } else {
114  wsToVs = mtx_mapping->worldToVoxel(time);
115  vsToWs = wsToVs.inverse();
116  }
117  const Imath::Box3d wsBounds_d = Imath::transform(vsBounds,
118  vsToWs);
119  wsBounds = Imath::Box3f(wsBounds_d.min, wsBounds_d.max);
120  doWsBoundsOptimization = true;
121  }
122  }
123 
125  {
126  valueRemapOpPtr = op;
128  }
129 
130  typename Field_T::LinearInterp interp;
131  const Field_T *field;
132  typename Field_T::Ptr fieldPtr;
133  const Field3D::FieldMapping *mapping;
138  double worldScale;
139  bool doOsToWs;
148 };
149 
150 //------------------------------------------------------------------------------
151 // MIPFieldWrapper
152 //------------------------------------------------------------------------------
153 
156 template <typename Field_T>
158 {
159  typedef Field_T field_type;
160  typedef std::vector<MIPFieldWrapper> Vec;
161  typedef typename Field_T::LinearInterp LinearInterp;
162 
163  MIPFieldWrapper(const typename Field_T::Ptr f)
164  : interpPtr(new LinearInterp(*f)),
165  field(f.get()),
166  fieldPtr(f),
167  mapping(f->mapping().get()),
168  vsBounds(continuousBounds(f->dataWindow())),
169  worldScale(1.0),
170  doOsToWs(false),
171  valueRemapOp(NULL)
172  {
173  interp = interpPtr.get();
174  }
175 
176  void setOsToWs(const M44d &i_osToWs)
177  {
178  osToWs = i_osToWs;
179  wsToOs = osToWs.inverse();
180  // Compute world scale
181  V3d ws(1.0);
182  if (!Imath::extractScaling(osToWs, ws, false)) {
183  Msg::print("WARNING: FieldGroup/FieldWrapper: "
184  "Couldn't extract world scale from object-to-world "
185  "transform. Defaulting to 1.0.");
186  }
187  worldScale = std::max(std::max(ws.x, ws.y), ws.z);
188  // Set boolean
189  doOsToWs = true;
190 
191  // Update wsBounds
194  }
195  }
196 
197  void setWsBoundsOptimization(const bool doWsBoundsOptimization_)
198  {
199  if (!doWsBoundsOptimization_)
200  return;
201  // wsBounds can be set only if mapping is a matrix
202  const MatrixFieldMapping *mtx_mapping =
203  dynamic_cast<const MatrixFieldMapping*>(mapping);
204  if (mtx_mapping) {
205  const float time = 0;
206  M44d vsToWs;
207  if (doOsToWs) {
208  wsToVs = wsToOs * mtx_mapping->worldToVoxel(time);
209  vsToWs = wsToVs.inverse();
210  } else {
211  wsToVs = mtx_mapping->worldToVoxel(time);
212  vsToWs = wsToVs.inverse();
213  }
214  const Imath::Box3d wsBounds_d = Imath::transform(vsBounds,
215  vsToWs);
216  wsBounds = Imath::Box3f(wsBounds_d.min, wsBounds_d.max);
217  doWsBoundsOptimization = true;
218  }
219  }
220 
222  {
223  valueRemapOpPtr = op;
225  }
226 
227  boost::shared_ptr<LinearInterp> interpPtr;
229  const Field_T *field;
230  typename Field_T::Ptr fieldPtr;
231  const Field3D::FieldMapping *mapping;
236  double worldScale;
237  bool doOsToWs;
246 };
247 
248 //----------------------------------------------------------------------------//
249 
251 
252 //------------------------------------------------------------------------------
253 
254 #endif // include guard
255 
256 //------------------------------------------------------------------------------
Contains the DenseField class.
Contains the Field3DFile classes.
Contains the FieldInterp base class and some standard interpolation classes.
Contains the FieldMapping base class and the NullFieldMapping and MatrixFieldMapping subclasses.
Box3d continuousBounds(const Box3i &bbox)
Definition: Field.h:1111
Contains the initIO function.
Contains the MIPField class.
Contains MIP-related utility functions.
Contains the SparseField class.
Imath::Box3d Box3d
Definition: SpiMathLib.h:79
Imath::V3d V3d
Definition: SpiMathLib.h:74
Imath::Box3f Box3f
Definition: SpiMathLib.h:78
Imath::V3f V3f
Definition: SpiMathLib.h:73
Imath::M44d M44d
Definition: SpiMathLib.h:82
Represents the mapping of a field by a matrix transform.
Definition: FieldMapping.h:335
const M44d & worldToVoxel() const
Returns a reference to the world to voxel space transform.
Definition: FieldMapping.h:381
The ValueRemapOp class is used when shader-like calculations need to be applied to individual fields ...
Definition: FieldWrapper.h:40
boost::shared_ptr< ValueRemapOp > Ptr
Definition: FieldWrapper.h:45
virtual V3f remap(const V3f &value) const =0
Remaps a V3f value.
virtual float remap(const float value) const =0
Remaps a float value.
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity.
Definition: Log.cpp:70
T max(const T a, const T2 b)
Max operation on mixed types.
Definition: FieldSampler.h:32
#define FIELD3D_NAMESPACE_HEADER_CLOSE
Definition: ns.h:58
This class wraps up a single field to make its interpolator and its mapping easily accessible....
Definition: FieldWrapper.h:64
bool doWsBoundsOptimization
Definition: FieldWrapper.h:144
Field_T field_type
Definition: FieldWrapper.h:65
ValueRemapOp::Ptr valueRemapOpPtr
Optionally, set a ValueRemapOp to remap values.
Definition: FieldWrapper.h:146
M44d osToWs
Optionally, enable doOsToWs to apply a world to object transform before lookups.
Definition: FieldWrapper.h:137
FieldWrapper(const typename Field_T::Ptr f)
Definition: FieldWrapper.h:68
void setOsToWs(const M44d &i_osToWs)
Definition: FieldWrapper.h:79
const ValueRemapOp * valueRemapOp
Definition: FieldWrapper.h:147
double worldScale
Definition: FieldWrapper.h:138
Field_T::Ptr fieldPtr
Definition: FieldWrapper.h:132
std::vector< FieldWrapper > Vec
Definition: FieldWrapper.h:66
M44d wsToVs
Optionally, enable wsBounds optimization to use a world axis aligned bounding box in lookups.
Definition: FieldWrapper.h:142
Field_T::LinearInterp interp
Definition: FieldWrapper.h:130
void setValueRemapOp(ValueRemapOp::Ptr op)
Definition: FieldWrapper.h:124
void setWsBoundsOptimization(const bool doWsBoundsOptimization_)
Definition: FieldWrapper.h:100
Imath::Box3f wsBounds
Definition: FieldWrapper.h:143
const Field_T * field
Definition: FieldWrapper.h:131
const Field3D::FieldMapping * mapping
Definition: FieldWrapper.h:133
This class wraps up a single MIP field to make its interpolator and its mapping easily accessible....
Definition: FieldWrapper.h:158
MIPFieldWrapper(const typename Field_T::Ptr f)
Definition: FieldWrapper.h:163
void setWsBoundsOptimization(const bool doWsBoundsOptimization_)
Definition: FieldWrapper.h:197
boost::shared_ptr< LinearInterp > interpPtr
Definition: FieldWrapper.h:227
LinearInterp * interp
Definition: FieldWrapper.h:228
void setValueRemapOp(ValueRemapOp::Ptr op)
Definition: FieldWrapper.h:221
void setOsToWs(const M44d &i_osToWs)
Definition: FieldWrapper.h:176
Field_T::Ptr fieldPtr
Definition: FieldWrapper.h:230
std::vector< MIPFieldWrapper > Vec
Definition: FieldWrapper.h:160
ValueRemapOp::Ptr valueRemapOpPtr
Optionally, set a ValueRemapOp to remap values.
Definition: FieldWrapper.h:244
Field_T::LinearInterp LinearInterp
Definition: FieldWrapper.h:161
M44d osToWs
Optionally, enable doOsToWs to apply a world to object transform before lookups.
Definition: FieldWrapper.h:235
Field_T field_type
Definition: FieldWrapper.h:159
Imath::Box3f wsBounds
Definition: FieldWrapper.h:241
M44d wsToVs
Optionally, enable wsBounds optimization to use a world axis aligned bounding box in lookups.
Definition: FieldWrapper.h:240
const Field_T * field
Definition: FieldWrapper.h:229
bool doWsBoundsOptimization
Definition: FieldWrapper.h:242
const Field3D::FieldMapping * mapping
Definition: FieldWrapper.h:231
const ValueRemapOp * valueRemapOp
Definition: FieldWrapper.h:245