44#ifndef _INCLUDED_Field3D_MACField_H_
45#define _INCLUDED_Field3D_MACField_H_
48#include <boost/lexical_cast.hpp>
94template <
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());
262 {
return V3i((
int)
m_u.size(), (
int)
m_v.size(), (
int)
m_w.size()); }
286 const real_t*
uPtr(
int i,
int j,
int k)
const;
292 const real_t*
vPtr(
int i,
int j,
int k)
const;
298 const real_t*
wPtr(
int i,
int j,
int k)
const;
358template <
class Data_T>
373 const V3i ¤tPos)
374 : x(currentPos.x), y(currentPos.y), z(currentPos.z),
375 m_p(NULL), m_window(window), m_comp(comp),
378 if (window.intersects(currentPos))
386 if (x == m_window.max.x) {
387 if (y == m_window.max.y) {
388 if (z == m_window.max.z) {
408 template <
class Iter_T>
409 inline bool operator == (
const Iter_T &rhs)
const
411 return m_p == &(*rhs);
414 template <
class Iter_T>
415 inline bool operator != (
const Iter_T &rhs)
const
417 return m_p != &(*rhs);
425 inline const real_t* operator -> ()
const
443 m_p = m_field.uPtr(x, y, z);
446 m_p = m_field.vPtr(x, y, z);
449 m_p = m_field.wPtr(x, y, z);
452 assert(
false &&
"Illegal MACComponent in const_mac_comp_iterator");
471template <
class Data_T>
484 const Box3i &window,
const V3i ¤tPos)
485 : x(currentPos.x), y(currentPos.y), z(currentPos.z),
486 m_p(NULL), m_window(window), m_comp(comp),
489 if (window.intersects(currentPos))
497 if (x == m_window.max.x) {
498 if (y == m_window.max.y) {
499 if (z == m_window.max.z) {
519 template <
class Iter_T>
520 inline bool operator == (
const Iter_T &rhs)
const
522 return m_p == &(*rhs);
525 template <
class Iter_T>
526 inline bool operator != (
const Iter_T &rhs)
const
528 return m_p != &(*rhs);
554 m_p = m_field.uPtr(x, y, z);
557 m_p = m_field.vPtr(x, y, z);
560 m_p = m_field.wPtr(x, y, z);
563 assert(
false &&
"Illegal MACComponent in const_mac_comp_iterator");
590 dataWindow.max +=
V3i(1, 0, 0);
593 dataWindow.max +=
V3i(0, 1, 0);
596 dataWindow.max +=
V3i(0, 0, 1);
599 assert(
false &&
"Illegal MACComponent in makeDataWindowForComponent");
610template <
class Data_T>
619template <
class Data_T>
622 std::fill(m_u.begin(), m_u.end(), value.x);
623 std::fill(m_v.begin(), m_v.end(), value.y);
624 std::fill(m_w.begin(), m_w.end(), value.z);
629template <
class Data_T>
632 return Data_T(uCenter(i, j, k), vCenter(i, j, k), wCenter(i, j, k));
637template <
class Data_T>
640 long long int superClassMemSize = base::memSize();
641 long long int vectorMemSize =
642 (m_u.capacity() + m_v.capacity() + m_w.capacity()) *
sizeof(
real_t);
643 return sizeof(*this) + vectorMemSize + superClassMemSize;
648template <
class Data_T>
651 m_dummy = value(i, j, k);
657template <
class Data_T>
664 base::m_dataWindow.max - base::m_dataWindow.min +
V3i(1);
666 if (std::min(std::min(baseSize.x, baseSize.y), baseSize.z) < 0)
667 throw Exc::ResizeException(
"Attempt to resize ResizableField object "
668 "using negative size. Data window was: " +
669 boost::lexical_cast<std::string>(baseSize));
672 m_uSize = baseSize +
V3i(1, 0, 0);
673 m_vSize = baseSize +
V3i(0, 1, 0);
674 m_wSize = baseSize +
V3i(0, 0, 1);
677 m_uSizeXY = m_uSize.x * m_uSize.y;
678 m_vSizeXY = m_vSize.x * m_vSize.y;
679 m_wSizeXY = m_wSize.x * m_wSize.y;
683 m_u.resize(m_uSize.x * m_uSize.y * m_uSize.z);
684 m_v.resize(m_vSize.x * m_vSize.y * m_vSize.z);
685 m_w.resize(m_wSize.x * m_wSize.y * m_wSize.z);
687 catch (std::bad_alloc &) {
688 throw Exc::MemoryException(
"Couldn't allocate MACField of size " +
689 boost::lexical_cast<std::string>(baseSize));
696template <
class Data_T>
700 assert (i >= base::m_dataWindow.min.x);
701 assert (i <= base::m_dataWindow.max.x + 1);
702 assert (j >= base::m_dataWindow.min.y);
703 assert (j <= base::m_dataWindow.max.y);
704 assert (k >= base::m_dataWindow.min.z);
705 assert (k <= base::m_dataWindow.max.z);
707 i -= base::m_dataWindow.min.x;
708 j -= base::m_dataWindow.min.y;
709 k -= base::m_dataWindow.min.z;
710 return m_u[i + j * m_uSize.x + k * m_uSizeXY];
715template <
class Data_T>
719 assert (i >= base::m_dataWindow.min.x);
720 assert (i <= base::m_dataWindow.max.x + 1);
721 assert (j >= base::m_dataWindow.min.y);
722 assert (j <= base::m_dataWindow.max.y);
723 assert (k >= base::m_dataWindow.min.z);
724 assert (k <= base::m_dataWindow.max.z);
726 i -= base::m_dataWindow.min.x;
727 j -= base::m_dataWindow.min.y;
728 k -= base::m_dataWindow.min.z;
729 return m_u[i + j * m_uSize.x + k * m_uSizeXY];
734template <
class Data_T>
738 assert (i >= base::m_dataWindow.min.x);
739 assert (i <= base::m_dataWindow.max.x);
740 assert (j >= base::m_dataWindow.min.y);
741 assert (j <= base::m_dataWindow.max.y + 1);
742 assert (k >= base::m_dataWindow.min.z);
743 assert (k <= base::m_dataWindow.max.z);
745 i -= base::m_dataWindow.min.x;
746 j -= base::m_dataWindow.min.y;
747 k -= base::m_dataWindow.min.z;
748 return m_v[i + j * m_vSize.x + k * m_vSizeXY];
753template <
class Data_T>
757 assert (i >= base::m_dataWindow.min.x);
758 assert (i <= base::m_dataWindow.max.x);
759 assert (j >= base::m_dataWindow.min.y);
760 assert (j <= base::m_dataWindow.max.y + 1);
761 assert (k >= base::m_dataWindow.min.z);
762 assert (k <= base::m_dataWindow.max.z);
764 i -= base::m_dataWindow.min.x;
765 j -= base::m_dataWindow.min.y;
766 k -= base::m_dataWindow.min.z;
767 return m_v[i + j * m_vSize.x + k * m_vSizeXY];
772template <
class Data_T>
776 assert (i >= base::m_dataWindow.min.x);
777 assert (i <= base::m_dataWindow.max.x);
778 assert (j >= base::m_dataWindow.min.y);
779 assert (j <= base::m_dataWindow.max.y);
780 assert (k >= base::m_dataWindow.min.z);
781 assert (k <= base::m_dataWindow.max.z + 1);
783 i -= base::m_dataWindow.min.x;
784 j -= base::m_dataWindow.min.y;
785 k -= base::m_dataWindow.min.z;
786 return m_w[i + j * m_wSize.x + k * m_wSizeXY];
791template <
class Data_T>
795 assert (i >= base::m_dataWindow.min.x);
796 assert (i <= base::m_dataWindow.max.x);
797 assert (j >= base::m_dataWindow.min.y);
798 assert (j <= base::m_dataWindow.max.y);
799 assert (k >= base::m_dataWindow.min.z);
800 assert (k <= base::m_dataWindow.max.z + 1);
802 i -= base::m_dataWindow.min.x;
803 j -= base::m_dataWindow.min.y;
804 k -= base::m_dataWindow.min.z;
805 return m_w[i + j * m_wSize.x + k * m_wSizeXY];
810template <
class Data_T>
816 return cend_comp(comp);
818 makeDataWindowForComponent(base::m_dataWindow, comp);
824template <
class Data_T>
829 if (subset.isEmpty())
830 return cend_comp(comp, subset);
831 Box3i dataWindow = makeDataWindowForComponent(subset, comp);
837template <
class Data_T>
843 makeDataWindowForComponent(base::m_dataWindow, comp);
845 V3i(dataWindow.min.x,
847 dataWindow.max.z + 1));
852template <
class Data_T>
857 Box3i dataWindow = makeDataWindowForComponent(subset, comp);
859 V3i(dataWindow.min.x,
861 dataWindow.max.z + 1));
866template <
class Data_T>
872 return end_comp(comp);
873 Box3i dataWindow = makeDataWindowForComponent(base::m_dataWindow, comp);
879template <
class Data_T>
884 if (subset.isEmpty())
885 return end_comp(comp, subset);
886 Box3i dataWindow = makeDataWindowForComponent(subset, comp);
892template <
class Data_T>
897 Box3i dataWindow = makeDataWindowForComponent(base::m_dataWindow, comp);
900 dataWindow.max.z + 1));
905template <
class Data_T>
910 Box3i dataWindow = makeDataWindowForComponent(subset, comp);
913 dataWindow.max.z + 1));
918template <
class Data_T>
923 i -= base::m_dataWindow.min.x;
924 j -= base::m_dataWindow.min.y;
925 k -= base::m_dataWindow.min.z;
926 return &m_u[i + j * m_uSize.x + k * m_uSizeXY];
931template <
class Data_T>
936 i -= base::m_dataWindow.min.x;
937 j -= base::m_dataWindow.min.y;
938 k -= base::m_dataWindow.min.z;
939 return &m_u[i + j * m_uSize.x + k * m_uSizeXY];
944template <
class Data_T>
949 i -= base::m_dataWindow.min.x;
950 j -= base::m_dataWindow.min.y;
951 k -= base::m_dataWindow.min.z;
952 return &m_v[i + j * m_vSize.x + k * m_vSizeXY];
957template <
class Data_T>
962 i -= base::m_dataWindow.min.x;
963 j -= base::m_dataWindow.min.y;
964 k -= base::m_dataWindow.min.z;
965 return &m_v[i + j * m_vSize.x + k * m_vSizeXY];
970template <
class Data_T>
975 i -= base::m_dataWindow.min.x;
976 j -= base::m_dataWindow.min.y;
977 k -= base::m_dataWindow.min.z;
978 return &m_w[i + j * m_wSize.x + k * m_wSizeXY];
983template <
class Data_T>
988 i -= base::m_dataWindow.min.x;
989 j -= base::m_dataWindow.min.y;
990 k -= base::m_dataWindow.min.z;
991 return &m_w[i + j * m_wSize.x + k * m_wSizeXY];
FIELD3D_VEC3_T< T > operator*(S s, const FIELD3D_VEC3_T< T > vec)
Scalar times Vec3 multiplication. Makes the interpolation calls cleaner.
Contains Field, WritableField and ResizableField classes.
#define FIELD3D_CLASSTYPE_TEMPL_INSTANTIATION(field)
MACField< V3d > MACField3d
MACField< V3f > MACField3f
MACField< V3h > MACField3h
boost::intrusive_ptr< FieldBase > Ptr
std::string name
Optional name of the field.
V3i const dataResolution() const
MACComponent m_comp
Component to look up.
MACField< Data_T > class_type
const real_t * m_p
Pointer to current element.
Box3i m_window
Window to traverse.
const_mac_comp_iterator(MACComponent comp, const class_type &field, const Box3i &window, const V3i ¤tPos)
MACField< Data_T >::real_t real_t
const class_type & m_field
Reference to field being iterated over.
real_t * m_p
Pointer to current element.
class_type & m_field
Reference to field being iterated over.
MACComponent m_comp
Component to look up.
mac_comp_iterator(MACComponent comp, class_type &field, const Box3i &window, const V3i ¤tPos)
MACField< Data_T > class_type
Box3i m_window
Window to traverse.
MACField< Data_T >::real_t real_t
This subclass of Field implements a standard MAC field. Refer to your favorite fluid simulations book...
CubicMACFieldInterp< Data_T > CubicInterp
Data_T::BaseType real_t
This typedef is used to refer to the scalar component type of the vectors.
boost::intrusive_ptr< MACField > Ptr
real_t vCenter(int i, int j, int k) const
Returns the v-component interpolated to the cell center.
std::vector< real_t > m_u
U component storage.
const real_t & w(int i, int j, int k) const
Read access to value on w-facing wall.
static TemplatedFieldType< MACField< Data_T > > ms_classType
MACField()
Constructs an empty buffer.
int m_wSizeXY
Size of xy slice for w component.
const real_t * uPtr(int i, int j, int k) const
Direct access to value on u-facing wall.
MACField< Data_T > class_type
int m_uSizeXY
Size of xy slice for u component.
V3i m_wSize
Size of W grid along each axis.
DEFINE_FIELD_RTTI_CONCRETE_CLASS
const real_t * vPtr(int i, int j, int k) const
Direct access to value on v-facing wall.
virtual void sizeChanged()
Subclasses should re-implement this if they need to perform memory allocations, etc....
const real_t & u(int i, int j, int k) const
Read access to value on u-facing wall.
V3i m_uSize
Size of U grid along each axis.
ResizableField< Data_T > base
static const char * staticClassType()
V3i m_vSize
Size of V grid along each axis.
int m_vSizeXY
Size of xy slice for v component.
virtual Data_T value(int i, int j, int k) const
mac_comp_iterator end_comp(MACComponent comp)
Iterator to first element.
Data_T m_dummy
Dummy storage of a temp value that lvalue() can write to.
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 * wPtr(int i, int j, int k) const
Direct access to value on w-facing wall.
real_t uCenter(int i, int j, int k) const
Returns the u-component interpolated to the cell center.
FIELD3D_CLASSNAME_CLASSTYPE_IMPLEMENTATION
std::vector< real_t > m_v
V component storage.
const real_t & v(int i, int j, int k) const
Read access to value on v-facing wall.
const_mac_comp_iterator cend_comp(MACComponent comp) const
Const iterator to first element. "cbegin" matches the tr1 c++ standard.
virtual void clear(const Data_T &value)
Clears all the voxels in the storage.
LinearMACFieldInterp< Data_T > LinearInterp
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...
const_mac_comp_iterator cbegin_comp(MACComponent comp) const
Const iterator to first element. "cbegin" matches the tr1 c++ standard.
std::vector< real_t > m_w
W component storage.
V3i getComponentSize() const
Returns the size of U,V,W components.
void copyMAC(typename MACField::Ptr other)
TEMP: Copies the MAC field data from another MAC field. This should be re-implemented using proper it...
virtual long long int memSize() const
Returns the memory usage (in bytes)
static const char * staticClassName()
real_t wCenter(int i, int j, int k) const
Returns the w-component interpolated to the cell center.
mac_comp_iterator begin_comp(MACComponent comp)
Iterator to first element.
void matchDefinition(FieldRes::Ptr fieldToMatch)
Sets up this field so that resolution and mapping matches the other.
Box3i makeDataWindowForComponent(Box3i dataWindow, MACComponent comp)
#define FIELD3D_NAMESPACE_HEADER_CLOSE
Used to return a string for the name of a templated field.