3 #ifndef __F3DUTIL_FIELDGROUP_H__ 4 #define __F3DUTIL_FIELDGROUP_H__ 9 #include <boost/foreach.hpp> 10 #include <boost/tokenizer.hpp> 11 #include <boost/thread/mutex.hpp> 12 #include <boost/mpl/vector.hpp> 13 #include <boost/mpl/for_each.hpp> 14 #include <boost/mpl/placeholders.hpp> 15 #include <boost/mpl/push_back.hpp> 16 #include <boost/mpl/transform.hpp> 17 #include <boost/fusion/mpl.hpp> 18 #include <boost/fusion/algorithm/iteration/for_each.hpp> 19 #include <boost/fusion/include/for_each.hpp> 20 #include <boost/fusion/include/as_vector.hpp> 23 #include <lava/field3d15/DenseField.h> 24 #include <lava/field3d15/Field3DFile.h> 25 #include <lava/field3d15/FieldInterp.h> 26 #include <lava/field3d15/InitIO.h> 27 #include <lava/field3d15/MIPField.h> 28 #include <lava/field3d15/MIPUtil.h> 29 #include <lava/field3d15/SparseField.h> 44 namespace mpl = boost::mpl;
45 namespace ph = mpl::placeholders;
46 namespace fusion = boost::fusion;
47 namespace fusion_ro = boost::fusion::result_of;
49 typedef mpl::vector<Field3D::half, float, double>
ScalarTypes;
50 typedef mpl::vector<Field3D::V3h, Field3D::V3f, Field3D::V3d>
VectorTypes;
103 LoadFields(Field3D::Field3DInputFile &in,
const std::string &name,
104 const std::string &attribute, Field3D::FieldRes::Vec &results)
105 : m_in(in), m_name(name), m_attribute(attribute), m_results(results)
108 template <
typename T>
112 typename Field3D::Field<T>::Vec fields =
113 m_in.readScalarLayers<T>(m_name, m_attribute);
115 BOOST_FOREACH (
const typename Field3D::Field<T>::Ptr &ptr, fields) {
116 m_results.push_back(ptr);
120 Field3D::Field3DInputFile &
m_in;
130 LoadFields(Field3D::Field3DInputFile &in,
const std::string &name,
131 const std::string &attribute, Field3D::FieldRes::Vec &results)
132 : m_in(in), m_name(name), m_attribute(attribute), m_results(results)
135 template <
typename Vec_T>
138 typedef typename Vec_T::BaseType T;
141 typename Field3D::Field<Vec_T>::Vec fields =
142 m_in.readVectorLayers<T>(m_name, m_attribute);
144 BOOST_FOREACH (
const typename Field3D::Field<Vec_T>::Ptr &ptr, fields) {
145 m_results.push_back(ptr);
149 Field3D::Field3DInputFile &
m_in;
157 inline std::vector<V3d>
160 std::vector<V3d> result;
161 result.push_back(
V3d(box.min.x, box.min.y, box.min.z));
162 result.push_back(
V3d(box.max.x, box.min.y, box.min.z));
163 result.push_back(
V3d(box.min.x, box.max.y, box.min.z));
164 result.push_back(
V3d(box.max.x, box.max.y, box.min.z));
165 result.push_back(
V3d(box.min.x, box.min.y, box.max.z));
166 result.push_back(
V3d(box.max.x, box.min.y, box.max.z));
167 result.push_back(
V3d(box.min.x, box.max.y, box.max.z));
168 result.push_back(
V3d(box.max.x, box.max.y, box.max.z));
177 double tNear = -std::numeric_limits<double>::max();
178 double tFar = std::numeric_limits<double>::max();
179 const double epsilon = std::numeric_limits<double>::epsilon() * 10.0;
181 for (
size_t dim = 0; dim < 3; ++dim) {
183 if (std::abs(ray.dir[dim]) < epsilon) {
185 if (ray.pos[dim] < box.min[dim] || ray.pos[dim] > box.max[dim]) {
189 t0 = (box.min[dim] - ray.pos[dim]) / ray.dir[dim];
190 t1 = (box.max[dim] - ray.pos[dim]) / ray.dir[dim];
194 tNear = std::max(tNear, t0);
195 tFar = std::min(tFar, t1);
223 template <
typename BaseTypeList_T,
int Dims_T>
232 typedef typename mpl::transform<
235 typedef typename mpl::transform<
238 typedef typename mpl::transform<
241 typedef typename mpl::transform<
246 typedef typename fusion_ro::as_vector<MPLDenseTypes>::type
DenseTypes;
247 typedef typename fusion_ro::as_vector<MPLSparseTypes>::type
SparseTypes;
248 typedef typename fusion_ro::as_vector<MPLMIPDenseTypes>::type
MIPDenseTypes;
254 static const int k_missingFile = -1;
261 FieldGroup(
const Field3D::FieldRes::Vec &fields);
265 void setup(
const Field3D::FieldRes::Vec &fields);
270 int load(
const std::string &filename,
const std::string &attribute);
275 void sample(
const V3d &vsP,
float *result,
bool isVs)
const;
277 void sampleMIP(
const V3d &vsP,
const float wsSpotSize,
278 float *result,
bool isVs)
const;
282 void getMinMax(
const Box3d &wsBounds,
float *min,
float *max)
const;
284 long long int memSize()
const;
287 {
return m_allFields; }
322 template <
typename BaseTypeList_T,
int Dims_T>
328 template <
typename BaseTypeList_T,
int Dims_T>
330 (
const Field3D::FieldRes::Vec &fields)
337 template <
typename BaseTypeList_T,
int Dims_T>
342 for (
size_t i = 0, end = fields.size(); i < end; ++i) {
344 fusion::for_each(m_dense, op);
345 fusion::for_each(m_sparse, op);
346 fusion::for_each(m_mipDense, op);
347 fusion::for_each(m_mipSparse, op);
353 template <
typename BaseTypeList_T,
int Dims_T>
356 (
const std::string &filename,
const std::string &attribute)
358 using namespace Field3D;
363 const size_t sizeBeforeLoading = size();
367 std::vector<std::string> filenames;
368 filenames.push_back(filename);
370 BOOST_FOREACH (
const std::string fn, filenames) {
374 return k_missingFile;
378 std::vector<std::string> names;
381 BOOST_FOREACH (
const std::string &name, names) {
383 mpl::for_each<BaseTypeList_T>(op);
389 m_allFields = results;
395 return size() - sizeBeforeLoading;
400 template <
typename BaseTypeList_T,
int Dims_T>
405 fusion::for_each(m_dense, op);
406 fusion::for_each(m_sparse, op);
407 fusion::for_each(m_mipDense, op);
408 fusion::for_each(m_mipSparse, op);
414 template <
typename BaseTypeList_T,
int Dims_T>
420 Sample op(vsP, result, isVs);
421 fusion::for_each(m_dense, op);
422 fusion::for_each(m_sparse, op);
427 template <
typename BaseTypeList_T,
int Dims_T>
430 const float wsSpotSize,
434 SampleMIP op(vsP, wsSpotSize, result, isVs);
435 fusion::for_each(m_mipDense, op);
436 fusion::for_each(m_mipSparse, op);
441 template <
typename BaseTypeList_T,
int Dims_T>
447 fusion::for_each(m_dense, op);
448 fusion::for_each(m_sparse, op);
449 fusion::for_each(m_mipDense, op);
450 fusion::for_each(m_mipSparse, op);
451 return intervals.size() > 0;
456 template <
typename BaseTypeList_T,
int Dims_T>
463 fusion::for_each(m_dense, op);
464 fusion::for_each(m_sparse, op);
466 fusion::for_each(m_mipDense, opMIP);
467 fusion::for_each(m_mipSparse, opMIP);
472 template <
typename BaseTypeList_T,
int Dims_T>
476 long long int result = 0;
478 fusion::for_each(m_dense, op);
479 fusion::for_each(m_sparse, op);
480 fusion::for_each(m_mipDense, op);
481 fusion::for_each(m_mipSparse, op);
489 template <
typename BaseTypeList_T,
int Dims_T>
497 template <
typename WrapperVec_T>
500 typedef typename WrapperVec_T::value_type Wrapper_T;
501 typedef typename Wrapper_T::field_type Field_T;
502 typedef typename Field_T::Ptr FieldPtr;
504 Field3D::field_dynamic_cast<Field_T>(m_field)) {
514 template <
typename BaseTypeList_T,
int Dims_T>
522 template <
typename T>
524 { count += vec.size(); }
531 template <
typename BaseTypeList_T,
int Dims_T>
536 : m_p(p), m_result(result), m_isVs(isVs)
539 template <
typename T>
550 template <
typename BaseTypeList_T,
int Dims_T>
556 : m_p(p), m_wsSpotSize(wsSpotSize), m_result(result), m_isVs(isVs)
559 template <
typename T>
574 template <
typename BaseTypeList_T,
int Dims_T>
579 : m_wsRay(wsRay), m_intervals(intervals)
586 const float time = 0.0f;
599 const double minLen = min(min(wsVoxelSize.x, wsVoxelSize.y),
601 m_intervals.push_back(
Interval(t0, t1, minLen));
609 typedef std::vector<V3d> PointVec;
611 const float time = 0.0f;
617 PointVec wsCorners(lsCorners.size());
618 for (PointVec::iterator lsP = lsCorners.begin(), wsP = wsCorners.begin(),
619 end = lsCorners.end(); lsP != end; ++lsP, ++wsP) {
625 planes[0] =
Plane3d(wsCorners[4], wsCorners[0], wsCorners[6]);
626 planes[1] =
Plane3d(wsCorners[1], wsCorners[5], wsCorners[3]);
627 planes[2] =
Plane3d(wsCorners[4], wsCorners[5], wsCorners[0]);
628 planes[3] =
Plane3d(wsCorners[2], wsCorners[3], wsCorners[6]);
629 planes[4] =
Plane3d(wsCorners[0], wsCorners[1], wsCorners[2]);
630 planes[5] =
Plane3d(wsCorners[5], wsCorners[4], wsCorners[7]);
633 double t0 = -std::numeric_limits<double>::max();
634 double t1 = std::numeric_limits<double>::max();
635 for (
int i = 0; i < 6; ++i) {
638 if (p.intersectT(m_wsRay, t)) {
639 if (m_wsRay.dir.dot(p.normal) > 0.0) {
641 t1 = std::min(t1, t);
644 t0 = std::max(t0, t);
649 t0 = std::max(t0, 0.0);
651 const double minLen = min(min(wsVoxelSize.x, wsVoxelSize.y),
653 m_intervals.push_back(
Interval(t0, t1, minLen));
657 template <
typename T>
661 for (
size_t field = 0, end = vec.size(); field < end; ++field) {
666 dynamic_cast<const MatrixFieldMapping*>(m)) {
667 intersectMatrixMapping(mtx);
671 dynamic_cast<const FrustumFieldMapping*>(m)) {
672 intersectFrustumMapping(f);
683 template <
typename BaseTypeList_T,
int Dims_T>
688 : m_wsBounds(wsBounds), m_min(min), m_max(max)
691 template <
typename T>
704 template <
typename BaseTypeList_T,
int Dims_T>
709 : m_wsBounds(wsBounds), m_min(min), m_max(max)
712 template <
typename T>
725 template <
typename BaseTypeList_T,
int Dims_T>
730 : m_memSize(&memSize)
733 template <
typename T>
736 for (
size_t field = 0, end = vec.size(); field < end; ++field) {
737 *m_memSize += vec[field].field->memSize();
742 {
return m_memSize; }
753 #endif // include guard void intersectMatrixMapping(const MatrixFieldMapping *mtx) const
Intersect matrix mapping.
MIPSparseTypes m_mipSparse
#define FIELD3D_NAMESPACE_HEADER_CLOSE
FieldWrapper< Field3D::DenseField< T > >::Vec type
fusion_ro::as_vector< MPLDenseTypes >::type DenseTypes
void intersectFrustumMapping(const FrustumFieldMapping *mtx) const
Intersect frustum mapping.
const std::string & m_attribute
MemSize(long long int &memSize)
Ctor.
virtual void worldToLocal(const V3d &wsP, V3d &lsP) const
Transform from world space position into local space.
SampleMIP(const V3d &p, const float wsSpotSize, float *result, bool isVs)
Ctor.
FieldGroup()
Default constructor, does nothing.
int load(const std::string &filename, const std::string &attribute)
Loads all fields from a given file and optional attribute pattern.
Sample(const V3d &p, float *result, bool isVs)
Ctor.
GrabFields(Field3D::FieldRes::Ptr f)
Ctor.
Field3D::FieldRes::Vec & m_results
void getMinMax(const Box3d &wsBounds, float *min, float *max) const
Returns the min/max range within a given bounding box.
fusion_ro::as_vector< MPLSparseTypes >::type SparseTypes
virtual V3d wsVoxelSize(int, int, int) const
Returns world-space size of a voxel at the specified coordinate.
virtual void localToWorld(const V3d &lsP, V3d &wsP) const
Transform from local space position into world space.
GetMinMax(const Box3d &wsBounds, float *min, float *max)
Ctor.
Base class for mapping between world-, local- and voxel coordinates.
const std::string & m_name
static void getMinMax(const WrapperVec_T &f, const Box3d &wsBounds, float *min, float *max)
void operator()(const T &vec) const
Functor.
long long int memSize() const
Returns the memory use in bytes for the fields in the group.
FieldWrapper< Field3D::SparseField< T > >::Vec type
size_t size() const
The number of fields in the group.
This class wraps up a single field to make its interpolator and its mapping easily accessible...
bool getIntersections(const Ray3d &ray, IntervalVec &intervals) const
Gets the intersection intervals between the ray and the fields.
static void getMinMaxMIP(const WrapperVec_T &f, const Box3d &wsBounds, float *min, float *max)
long long int result() const
Result.
GetMinMaxMIP(const Box3d &wsBounds, float *min, float *max)
Ctor.
void operator()(const T &vec) const
Functor.
mpl::transform< MPLBaseTypes, detail::MakeSparse< ph::_1 > >::type MPLSparseTypes
Represents a single integration interval. The interval is assumed to be inclusive, i.e. [t0,t1].
void operator()(WrapperVec_T &vec) const
Functor.
void sample(const V3d &vsP, float *result, bool isVs) const
Samples the group of fields at the given point. This call will not include MIP fields, which require a spot size.
void operator()(const T &vec) const
Functor.
FieldRes::Vec m_allFields
LoadFields(Field3D::Field3DInputFile &in, const std::string &name, const std::string &attribute, Field3D::FieldRes::Vec &results)
MIPFieldWrapper< Field3D::MIPField< Field3D::DenseField< T > > >::Vec type
MIPFieldWrapper< Field3D::MIPField< Field3D::SparseField< T > > >::Vec type
std::vector< Interval > IntervalVec
void sampleMIP(const V3d &vsP, const float wsSpotSize, float *result, bool isVs) const
Samples all the MIP fields in the group.
const FieldRes::Vec & fields() const
Returns a vector of FieldRes::Ptrs to the fields in the group.
Field3D::FieldRes::Vec & m_results
void operator()(const T &vec) const
Functor.
void operator()(const T &vec) const
Functor.
static void sample(const WrapperVec_T &f, const V3d &p, float *value, bool isVs)
mpl::transform< MPLBaseTypes, detail::MakeDense< ph::_1 > >::type MPLDenseTypes
void getPartitionNames(std::vector< std::string > &names) const
Gets the names of all the partitions in the file.
IntervalVec & m_intervals
LoadFields(Field3D::Field3DInputFile &in, const std::string &name, const std::string &attribute, Field3D::FieldRes::Vec &results)
long long int * m_memSize
Field3D::FieldRes::Ptr m_field
The field to work on. Will be matched against the type of operator().
This class wraps up a single MIP field to make its interpolator and its mapping easily accessible...
const std::string & m_name
mpl::transform< MPLBaseTypes, detail::MakeMIPDense< ph::_1 > >::type MPLMIPDenseTypes
void operator()(const T &vec) const
Functor.
FIELD3D_NAMESPACE_OPEN typedef mpl::vector< Field3D::half, float, double > ScalarTypes
virtual V3d wsVoxelSize(int i, int j, int k) const
Returns world-space size of a voxel at the specified coordinate.
FieldGroup< VectorTypes, 3 > VectorFieldGroup
Represents the mapping of a field by a perspective transform.
Field3D::Field3DInputFile & m_in
void setup(const Field3D::FieldRes::Vec &fields)
mpl::vector< Field3D::V3h, Field3D::V3f, Field3D::V3d > VectorTypes
static void sampleMIP(const WrapperVec_T &f, const V3d &p, const float wsSpotSize, float *value, bool isVs)
bool intersect(const Ray3d &ray, const Box3d &box, double &outT0, double &outT1)
void worldToLocalDir(const V3d &wsV, V3d &lsV) const
std::vector< V3d > cornerPoints(const Box3d &box)
void operator()(const T &vec) const
Functor.
fusion_ro::as_vector< MPLMIPSparseTypes >::type MIPSparseTypes
GetIntersections(const Ray3d &wsRay, IntervalVec &intervals)
Ctor.
mpl::transform< MPLBaseTypes, detail::MakeMIPSparse< ph::_1 > >::type MPLMIPSparseTypes
Represents the mapping of a field by a matrix transform.
Field3D::Field3DInputFile & m_in
fusion_ro::as_vector< MPLMIPDenseTypes >::type MIPDenseTypes
FieldGroup< ScalarTypes, 1 > ScalarFieldGroup
const std::string & m_attribute
BaseTypeList_T MPLBaseTypes