44 #ifndef _INCLUDED_Field3D_MACField_H_
45 #define _INCLUDED_Field3D_MACField_H_
48 #include <boost/lexical_cast.hpp>
94 template <
class Data_T>
101 typedef boost::intrusive_ptr<MACField>
Ptr;
102 typedef std::vector<Ptr>
Vec;
105 typedef typename Data_T::BaseType
real_t;
146 virtual Data_T
value(
int i,
int j,
int k)
const;
147 virtual long long int memSize()
const;
159 virtual Data_T&
lvalue(
int i,
int j,
int k);
170 const real_t&
u(
int i,
int j,
int k)
const;
173 real_t&
u(
int i,
int j,
int k);
176 const real_t&
v(
int i,
int j,
int k)
const;
179 real_t&
v(
int i,
int j,
int k);
182 const real_t&
w(
int i,
int j,
int k)
const;
185 real_t&
w(
int i,
int j,
int k);
202 class const_mac_comp_iterator;
204 class mac_comp_iterator;
210 const Box3i &subset)
const;
215 const Box3i &subset)
const;
221 const Box3i &subset);
226 const Box3i &subset);
235 return (
u(i, j, k) +
u(i + 1, j, k)) * 0.5;
240 return (
v(i, j, k) +
v(i, j + 1, k)) * 0.5;
245 return (
w(i, j, k) +
w(i, j, k + 1)) * 0.5;
253 std::copy(other->m_u.begin(), other->m_u.end(),
m_u.begin());
254 std::copy(other->m_v.begin(), other->m_v.end(),
m_v.begin());
255 std::copy(other->m_w.begin(), other->m_w.end(),
m_w.begin());
287 const real_t*
uPtr(
int i,
int j,
int k)
const;
293 const real_t*
vPtr(
int i,
int j,
int k)
const;
299 const real_t*
wPtr(
int i,
int j,
int k)
const;
359 template <
class Data_T>
374 const V3i ¤tPos)
375 : x(currentPos.x), y(currentPos.y), z(currentPos.z),
376 m_p(NULL), m_window(window), m_comp(comp),
386 if (x == m_window.max.x) {
387 if (y == m_window.max.y) {
403 template <
class Iter_T>
404 inline bool operator == (
const Iter_T &rhs)
const
406 return m_p == &(*rhs);
409 template <
class Iter_T>
410 inline bool operator != (
const Iter_T &rhs)
const
412 return m_p != &(*rhs);
420 inline const real_t* operator -> ()
const
438 m_p = m_field.uPtr(x, y, z);
441 m_p = m_field.vPtr(x, y, z);
444 m_p = m_field.wPtr(x, y, z);
447 assert(
false &&
"Illegal MACComponent in const_mac_comp_iterator");
466 template <
class Data_T>
479 const Box3i &window,
const V3i ¤tPos)
480 : x(currentPos.x), y(currentPos.y), z(currentPos.z),
481 m_p(NULL), m_window(window), m_comp(comp),
491 if (x == m_window.max.x) {
492 if (y == m_window.max.y) {
508 template <
class Iter_T>
509 inline bool operator == (
const Iter_T &rhs)
const
511 return m_p == &(*rhs);
514 template <
class Iter_T>
515 inline bool operator != (
const Iter_T &rhs)
const
517 return m_p != &(*rhs);
543 m_p = m_field.uPtr(x, y, z);
546 m_p = m_field.vPtr(x, y, z);
549 m_p = m_field.wPtr(x, y, z);
552 assert(
false &&
"Illegal MACComponent in const_mac_comp_iterator");
573 namespace MACFieldUtil {
579 dataWindow.max +=
V3i(1, 0, 0);
582 dataWindow.max +=
V3i(0, 1, 0);
585 dataWindow.max +=
V3i(0, 0, 1);
588 assert(
false &&
"Illegal MACComponent in makeDataWindowForComponent");
599 template <
class Data_T>
608 template <
class Data_T>
611 std::fill(m_u.begin(), m_u.end(), value.x);
612 std::fill(m_v.begin(), m_v.end(), value.y);
613 std::fill(m_w.begin(), m_w.end(), value.z);
618 template <
class Data_T>
621 return Data_T(uCenter(i, j, k), vCenter(i, j, k), wCenter(i, j, k));
626 template <
class Data_T>
629 long long int superClassMemSize = base::memSize();
630 long long int vectorMemSize =
631 (m_u.capacity() + m_v.capacity() + m_w.capacity()) *
sizeof(
real_t);
632 return sizeof(*this) + vectorMemSize + superClassMemSize;
637 template <
class Data_T>
640 m_dummy = value(i, j, k);
646 template <
class Data_T>
653 base::m_dataWindow.max - base::m_dataWindow.min +
V3i(1);
655 if (std::min(std::min(baseSize.x, baseSize.y), baseSize.z) < 0)
656 throw Exc::ResizeException(
"Attempt to resize ResizableField object "
657 "using negative size. Data window was: " +
658 boost::lexical_cast<std::string>(baseSize));
661 m_uSize = baseSize +
V3i(1, 0, 0);
662 m_vSize = baseSize +
V3i(0, 1, 0);
663 m_wSize = baseSize +
V3i(0, 0, 1);
666 m_uSizeXY = m_uSize.x * m_uSize.y;
667 m_vSizeXY = m_vSize.x * m_vSize.y;
668 m_wSizeXY = m_wSize.x * m_wSize.y;
672 m_u.resize(m_uSize.x * m_uSize.y * m_uSize.z);
673 m_v.resize(m_vSize.x * m_vSize.y * m_vSize.z);
674 m_w.resize(m_wSize.x * m_wSize.y * m_wSize.z);
676 catch (std::bad_alloc &e) {
677 throw Exc::MemoryException(
"Couldn't allocate MACField of size " +
678 boost::lexical_cast<std::string>(baseSize));
685 template <
class Data_T>
689 assert (i >= base::m_dataWindow.min.x);
690 assert (i <= base::m_dataWindow.max.x + 1);
691 assert (j >= base::m_dataWindow.min.y);
692 assert (j <= base::m_dataWindow.max.y);
693 assert (k >= base::m_dataWindow.min.z);
694 assert (k <= base::m_dataWindow.max.z);
696 i -= base::m_dataWindow.min.x;
697 j -= base::m_dataWindow.min.y;
698 k -= base::m_dataWindow.min.z;
699 return m_u[i + j * m_uSize.x + k * m_uSizeXY];
704 template <
class Data_T>
708 assert (i >= base::m_dataWindow.min.x);
709 assert (i <= base::m_dataWindow.max.x + 1);
710 assert (j >= base::m_dataWindow.min.y);
711 assert (j <= base::m_dataWindow.max.y);
712 assert (k >= base::m_dataWindow.min.z);
713 assert (k <= base::m_dataWindow.max.z);
715 i -= base::m_dataWindow.min.x;
716 j -= base::m_dataWindow.min.y;
717 k -= base::m_dataWindow.min.z;
718 return m_u[i + j * m_uSize.x + k * m_uSizeXY];
723 template <
class Data_T>
727 assert (i >= base::m_dataWindow.min.x);
728 assert (i <= base::m_dataWindow.max.x);
729 assert (j >= base::m_dataWindow.min.y);
730 assert (j <= base::m_dataWindow.max.y + 1);
731 assert (k >= base::m_dataWindow.min.z);
732 assert (k <= base::m_dataWindow.max.z);
734 i -= base::m_dataWindow.min.x;
735 j -= base::m_dataWindow.min.y;
736 k -= base::m_dataWindow.min.z;
737 return m_v[i + j * m_vSize.x + k * m_vSizeXY];
742 template <
class Data_T>
746 assert (i >= base::m_dataWindow.min.x);
747 assert (i <= base::m_dataWindow.max.x);
748 assert (j >= base::m_dataWindow.min.y);
749 assert (j <= base::m_dataWindow.max.y + 1);
750 assert (k >= base::m_dataWindow.min.z);
751 assert (k <= base::m_dataWindow.max.z);
753 i -= base::m_dataWindow.min.x;
754 j -= base::m_dataWindow.min.y;
755 k -= base::m_dataWindow.min.z;
756 return m_v[i + j * m_vSize.x + k * m_vSizeXY];
761 template <
class Data_T>
765 assert (i >= base::m_dataWindow.min.x);
766 assert (i <= base::m_dataWindow.max.x);
767 assert (j >= base::m_dataWindow.min.y);
768 assert (j <= base::m_dataWindow.max.y);
769 assert (k >= base::m_dataWindow.min.z);
770 assert (k <= base::m_dataWindow.max.z + 1);
772 i -= base::m_dataWindow.min.x;
773 j -= base::m_dataWindow.min.y;
774 k -= base::m_dataWindow.min.z;
775 return m_w[i + j * m_wSize.x + k * m_wSizeXY];
780 template <
class Data_T>
784 assert (i >= base::m_dataWindow.min.x);
785 assert (i <= base::m_dataWindow.max.x);
786 assert (j >= base::m_dataWindow.min.y);
787 assert (j <= base::m_dataWindow.max.y);
788 assert (k >= base::m_dataWindow.min.z);
789 assert (k <= base::m_dataWindow.max.z + 1);
791 i -= base::m_dataWindow.min.x;
792 j -= base::m_dataWindow.min.y;
793 k -= base::m_dataWindow.min.z;
794 return m_w[i + j * m_wSize.x + k * m_wSizeXY];
799 template <
class Data_T>
803 using namespace MACFieldUtil;
805 return cend_comp(comp);
813 template <
class Data_T>
817 using namespace MACFieldUtil;
818 if (subset.isEmpty())
819 return cend_comp(comp, subset);
826 template <
class Data_T>
830 using namespace MACFieldUtil;
834 V3i(dataWindow.min.x,
836 dataWindow.max.z + 1));
841 template <
class Data_T>
845 using namespace MACFieldUtil;
848 V3i(dataWindow.min.x,
850 dataWindow.max.z + 1));
855 template <
class Data_T>
859 using namespace MACFieldUtil;
861 return end_comp(comp);
868 template <
class Data_T>
872 using namespace MACFieldUtil;
873 if (subset.isEmpty())
874 return end_comp(comp, subset);
881 template <
class Data_T>
885 using namespace MACFieldUtil;
889 dataWindow.max.z + 1));
894 template <
class Data_T>
898 using namespace MACFieldUtil;
902 dataWindow.max.z + 1));
907 template <
class Data_T>
912 i -= base::m_dataWindow.min.x;
913 j -= base::m_dataWindow.min.y;
914 k -= base::m_dataWindow.min.z;
915 return &m_u[i + j * m_uSize.x + k * m_uSizeXY];
920 template <
class Data_T>
925 i -= base::m_dataWindow.min.x;
926 j -= base::m_dataWindow.min.y;
927 k -= base::m_dataWindow.min.z;
928 return &m_u[i + j * m_uSize.x + k * m_uSizeXY];
933 template <
class Data_T>
938 i -= base::m_dataWindow.min.x;
939 j -= base::m_dataWindow.min.y;
940 k -= base::m_dataWindow.min.z;
941 return &m_v[i + j * m_vSize.x + k * m_vSizeXY];
946 template <
class Data_T>
951 i -= base::m_dataWindow.min.x;
952 j -= base::m_dataWindow.min.y;
953 k -= base::m_dataWindow.min.z;
954 return &m_v[i + j * m_vSize.x + k * m_vSizeXY];
959 template <
class Data_T>
964 i -= base::m_dataWindow.min.x;
965 j -= base::m_dataWindow.min.y;
966 k -= base::m_dataWindow.min.z;
967 return &m_w[i + j * m_wSize.x + k * m_wSizeXY];
972 template <
class Data_T>
977 i -= base::m_dataWindow.min.x;
978 j -= base::m_dataWindow.min.y;
979 k -= base::m_dataWindow.min.z;
980 return &m_w[i + j * m_wSize.x + k * m_wSizeXY];
989 #endif // Include guard
#define FIELD3D_NAMESPACE_HEADER_CLOSE
real_t wCenter(int i, int j, int k) const
Returns the w-component interpolated to the cell center.
MACField< Data_T >::real_t real_t
const real_t * m_p
Pointer to current element.
const class_type & m_field
Reference to field being iterated over.
mac_comp_iterator(MACComponent comp, class_type &field, const Box3i &window, const V3i ¤tPos)
std::vector< real_t > m_v
V component storage.
mac_comp_iterator begin_comp(MACComponent comp)
Iterator to first element.
MACComponent m_comp
Component to look up.
V3i getComponentSize() const
Returns the size of U,V,W components.
This subclass of Field implements a standard MAC field. Refer to your favorite fluid simulations book...
Box3i makeDataWindowForComponent(Box3i dataWindow, MACComponent comp)
ResizableField< Data_T > base
void matchDefinition(FieldRes::Ptr fieldToMatch)
Sets up this field so that resolution and mapping matches the other.
virtual FieldBase::Ptr clone() const
Returns a pointer to a copy of the field, pure virtual so ensure derived classes properly implement i...
const real_t & v(int i, int j, int k) const
Read access to value on v-facing wall.
std::vector< real_t > m_w
W component storage.
CubicMACFieldInterp< Data_T > CubicInterp
const real_t & u(int i, int j, int k) const
Read access to value on u-facing wall.
boost::intrusive_ptr< FieldBase > Ptr
real_t uCenter(int i, int j, int k) const
Returns the u-component interpolated to the cell center.
FIELD3D_VEC3_T< T > operator*(S s, const FIELD3D_VEC3_T< T > vec)
Scalar times Vec3 multiplication. Makes the interpolation calls cleaner.
MACField< Data_T > class_type
real_t * m_p
Pointer to current element.
virtual long long int memSize() const
Returns the memory usage (in bytes)
const real_t * wPtr(int i, int j, int k) const
Direct access to value on w-facing wall.
class_type & m_field
Reference to field being iterated over.
virtual Data_T value(int i, int j, int k) const
MACField< V3d > MACField3d
int m_wSizeXY
Size of xy slice for w component.
const real_t & w(int i, int j, int k) const
Read access to value on w-facing wall.
DEFINE_FIELD_RTTI_CONCRETE_CLASS
boost::intrusive_ptr< MACField > Ptr
virtual Data_T & lvalue(int i, int j, int k)
This will return the appropriate interpolated value but setting that to something else does not chang...
static TemplatedFieldType< MACField< Data_T > > ms_classType
MACComponent m_comp
Component to look up.
int m_vSizeXY
Size of xy slice for v component.
const real_t * uPtr(int i, int j, int k) const
Direct access to value on u-facing wall.
MACField< V3h > MACField3h
mac_comp_iterator end_comp(MACComponent comp)
Iterator to first element.
V3i m_uSize
Size of U grid along each axis.
const_mac_comp_iterator(MACComponent comp, const class_type &field, const Box3i &window, const V3i ¤tPos)
static const char * classType()
real_t vCenter(int i, int j, int k) const
Returns the v-component interpolated to the cell center.
Used to return a string for the name of a templated field.
V3i const dataResolution() const
Data_T m_dummy
Dummy storage of a temp value that lvalue() can write to.
Data_T::BaseType real_t
This typedef is used to refer to the scalar component type of the vectors.
Contains Field, WritableField and ResizableField classes.
int m_uSizeXY
Size of xy slice for u component.
virtual void clear(const Data_T &value)
Clears all the voxels in the storage.
const_mac_comp_iterator cend_comp(MACComponent comp) const
Const iterator to first element. "cbegin" matches the tr1 c++ standard.
const real_t * vPtr(int i, int j, int k) const
Direct access to value on v-facing wall.
V3i m_vSize
Size of V grid along each axis.
Box3i m_window
Window to traverse.
const_mac_comp_iterator cbegin_comp(MACComponent comp) const
Const iterator to first element. "cbegin" matches the tr1 c++ standard.
virtual void sizeChanged()
Subclasses should re-implement this if they need to perform memory allocations, etc. every time the size of the storage changes.
MACField< Data_T >::real_t real_t
MACField()
Constructs an empty buffer.
Box3i m_window
Window to traverse.
MACField< Data_T > class_type
LinearMACFieldInterp< Data_T > LinearInterp
virtual std::string className() const
Returns the class name of the object. Used by the class pool and when writing the data to disk...
MACField< Data_T > class_type
static const char * staticClassName()
void copyMAC(MACField::Ptr other)
TEMP: Copies the MAC field data from another MAC field. This should be re-implemented using proper it...
V3i m_wSize
Size of W grid along each axis.
MACField< V3f > MACField3f
std::vector< real_t > m_u
U component storage.
std::string name
Optional name of the field.
#define FIELD3D_NAMESPACE_OPEN
#define FIELD3D_CLASSTYPE_TEMPL_INSTANTIATION(field)