Field3D
Field.h
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 
43 //----------------------------------------------------------------------------//
44 
45 #ifndef _INCLUDED_Field3D_Field_H_
46 #define _INCLUDED_Field3D_Field_H_
47 
48 #include <cmath>
49 #include <vector>
50 #include <map>
51 
52 #include <boost/intrusive_ptr.hpp>
53 #include <boost/thread/mutex.hpp>
54 
55 #include "Traits.h"
56 #include "Exception.h"
57 #include "FieldMapping.h"
58 #include "FieldMetadata.h"
59 #include "Log.h"
60 #include "RefCount.h"
61 #include "Types.h"
62 
63 //----------------------------------------------------------------------------//
64 
65 #include "ns.h"
66 
68 
69 //----------------------------------------------------------------------------//
70 // Exceptions
71 //----------------------------------------------------------------------------//
72 
73 namespace Exc {
74 
75 DECLARE_FIELD3D_GENERIC_EXCEPTION(MemoryException, Exception)
76 DECLARE_FIELD3D_GENERIC_EXCEPTION(ResizeException, Exception)
77 
78 } // namespace Exc
79 
80 //----------------------------------------------------------------------------//
81 // FieldBase
82 //----------------------------------------------------------------------------//
83 
92 {
93 public:
94 
95  // Typedefs ------------------------------------------------------------------
96 
97  typedef boost::intrusive_ptr<FieldBase> Ptr;
99 
100  // Constructors --------------------------------------------------------------
101 
104 
106  FieldBase();
107 
109  FieldBase(const FieldBase &);
110 
112  virtual ~FieldBase();
113 
115 
116  // RTTI replacement ----------------------------------------------------------
117 
118  static const char *staticClassName()
119  {
120  return "FieldBase";
121  }
122 
123  static const char* staticClassType()
124  {
125  return staticClassName();
126  }
127 
128  // To be implemented by subclasses -------------------------------------------
129 
132 
138  virtual std::string className() const = 0;
139 
141  virtual std::string classType() const = 0;
142 
145  virtual Ptr clone() const = 0;
146 
148 
149  // Access to metadata --------------------------------------------------------
150 
153 
156  { return m_metadata; }
157 
160  { return m_metadata; }
161 
164  virtual void metadataHasChanged(const std::string &/* name */)
165  { /* Empty */ }
166 
168  void copyMetadata(const FieldBase &field)
169  { m_metadata = field.metadata(); }
170 
172 
173  // Public data members -------------------------------------------------------
174 
176  std::string name;
178  std::string attribute;
179 
180  private:
181 
182  // Private data members ------------------------------------------------------
183 
186 
187 };
188 
189 //----------------------------------------------------------------------------//
190 // FieldRes
191 //----------------------------------------------------------------------------//
192 
210 //----------------------------------------------------------------------------//
211 
212 class FieldRes : public FieldBase
213 {
214 public:
215 
216  // Typedefs ------------------------------------------------------------------
217 
218  typedef boost::intrusive_ptr<FieldRes> Ptr;
219  typedef std::vector<Ptr> Vec;
220 
221  // RTTI replacement ----------------------------------------------------------
222 
225 
226  virtual std::string dataTypeString() const
227  { return std::string("FieldRes"); }
228 
229  static const char *staticClassName()
230  {
231  return "FieldRes";
232  }
233 
234  static const char *staticClassType()
235  {
236  return staticClassName();
237  }
238 
239  // Ctor, dtor ----------------------------------------------------------------
240 
242  FieldRes();
243 
246  FieldRes(const FieldRes &src);
247 
248  // Main methods --------------------------------------------------------------
249 
254  inline const Box3i& extents() const
255  { return m_extents; }
258  inline const Box3i& dataWindow() const
259  { return m_dataWindow; }
260 
261  inline V3i const dataResolution() const
262  { return m_dataWindow.max - m_dataWindow.min + V3i(1); }
263 
265  void setMapping(FieldMapping::Ptr mapping);
266 
269  { return m_mapping; }
270 
273  { return m_mapping; }
274 
276  bool isInBounds(int i, int j, int k) const;
277 
278  // To be implemented by subclasses -------------------------------------------
279 
284  virtual long long int memSize() const
285  { return sizeof(*this); }
286 
288  virtual void mappingChanged()
289  { /* Empty */ }
290 
294  virtual size_t voxelCount() const
295  {
296  V3i res = m_dataWindow.size() + V3i(1);
297  return res.x * res.y * res.z;
298  }
299 
300 protected:
301 
302  // Typedefs ------------------------------------------------------------------
303 
305 
306  // Data members --------------------------------------------------------------
307 
318 
319 private:
320 
321  // Typedefs ------------------------------------------------------------------
322 
324  typedef FieldBase base;
325 
326 };
327 
328 //----------------------------------------------------------------------------//
329 
331  : m_mapping(new default_mapping)
332 {
333  m_extents = Box3i(V3i(0), V3i(-1));
335  m_mapping->setExtents(m_extents);
336 }
337 
338 //----------------------------------------------------------------------------//
339 
340 inline FieldRes::FieldRes(const FieldRes &src)
341  : FieldBase(src)
342 {
343  // Call base class first
344  // FieldBase(src);
345  // Copy self
346  *this = src;
347  m_mapping = src.mapping()->clone();
348 }
349 
350 //----------------------------------------------------------------------------//
351 
353 {
354  if (mapping) {
355  m_mapping = mapping->clone();
356  m_mapping->setExtents(m_extents);
357  } else {
359  "Tried to call FieldRes::setMapping with null pointer");
360  }
361  // Tell subclasses about the mapping change
362  mappingChanged();
363 }
364 
365 //----------------------------------------------------------------------------//
366 
367 inline bool FieldRes::isInBounds(int i, int j, int k) const
368 {
369  // Check bounds
370  if (i < m_dataWindow.min.x || i > m_dataWindow.max.x ||
371  j < m_dataWindow.min.y || j > m_dataWindow.max.y ||
372  k < m_dataWindow.min.z || k > m_dataWindow.max.z) {
373  return false;
374  }
375 
376  return true;
377 }
378 
379 //----------------------------------------------------------------------------//
380 // Field
381 //----------------------------------------------------------------------------//
382 
393 template <class Data_T>
394 class Field : public FieldRes
395 {
396 public:
397 
398  // Typedefs ------------------------------------------------------------------
399 
400  typedef boost::intrusive_ptr<Field> Ptr;
401 
403  typedef Data_T value_type;
404 
408  typedef std::vector<Ptr> Vec;
409 
410  // RTTI replacement ----------------------------------------------------------
411 
414 
415  static const char *staticClassName()
416  {
417  return "Field";
418  }
419 
420  static const char* staticClassType()
421  {
423  }
424 
425  // Constructors --------------------------------------------------------------
426 
428  virtual ~Field()
429  { /* Empty */ }
430 
431  // Iterators -----------------------------------------------------------------
432 
435  class const_iterator;
436 
438  const_iterator cbegin() const;
440  const_iterator cbegin(const Box3i &subset) const;
442  const_iterator cend() const;
445  const_iterator cend(const Box3i &subset) const;
446 
447  // To be implemented by subclasses -------------------------------------------
448 
455  virtual Data_T value(int i, int j, int k) const = 0;
456 
457  // Other member functions ----------------------------------------------------
458 
459  virtual std::string dataTypeString() const
460  { return DataTypeTraits<Data_T>::name(); }
461 
462 
463 private:
464 
465  // Static data members -------------------------------------------------------
466 
468 
469  // Typedefs ------------------------------------------------------------------
470 
472  typedef FieldRes base;
473 
474 };
475 
476 //----------------------------------------------------------------------------//
477 
478 #define FIELD3D_CLASSNAME_CLASSTYPE_IMPLEMENTATION \
479  virtual std::string className() const \
480  { return staticClassName(); } \
481  virtual std::string classType() const \
482  { return staticClassType(); } \
483 
484 #define FIELD3D_CLASSTYPE_TEMPL_INSTANTIATION(field) \
485  template <typename Data_T> \
486  TemplatedFieldType<field<Data_T> > field<Data_T>::ms_classType = \
487  TemplatedFieldType<field<Data_T> >(); \
488 
490 
491 //----------------------------------------------------------------------------//
492 // Field::const_iterator
493 //----------------------------------------------------------------------------//
494 
495 template <class Data_T>
496 class Field<Data_T>::const_iterator
497 {
498 
499 public:
500 #if defined(WIN32) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
501  typedef std::forward_iterator_tag iterator_category;
502  typedef Data_T value_type;
503  typedef ptrdiff_t difference_type;
504  typedef ptrdiff_t distance_type;
505  typedef Data_T *pointer;
506  typedef Data_T& reference;
507 #endif
508 
509  // Constructors --------------------------------------------------------------
510 
512  : x(i.x), y(i.y), z(i.z),
513  m_window(i.m_window), m_field(i.m_field)
514  { }
515 
516  const_iterator(const Field<Data_T> &field, const Box3i &window,
517  const V3i &currentPos)
518  : x(currentPos.x), y(currentPos.y), z(currentPos.z),
519  m_window(window), m_field(field)
520  { }
521 
522  // Operators -----------------------------------------------------------------
523 
524  inline const const_iterator& operator ++ ()
525  {
526  if (x == m_window.max.x) {
527  if (y == m_window.max.y) {
528  x = m_window.min.x;
529  y = m_window.min.y;
530  ++z;
531  } else {
532  x = m_window.min.x;
533  ++y;
534  }
535  } else {
536  ++x;
537  }
538  return *this;
539  }
540  template <class Iter_T>
541  bool operator == (const Iter_T &rhs) const
542  {
543  return x == rhs.x && y == rhs.y && z == rhs.z;
544  }
545  template <class Iter_T>
546  bool operator != (const Iter_T &rhs) const
547  {
548  return x != rhs.x || y != rhs.y || z != rhs.z;
549  }
550  inline Data_T operator * () const
551  {
552  return m_field.value(x, y, z);
553  }
554  // Public data members -------------------------------------------------------
555 
557  int x, y, z;
558 
559 private:
560 
561  // Private data members ------------------------------------------------------
562 
567 
568 };
569 
570 //----------------------------------------------------------------------------//
571 
572 template <class Data_T>
575 {
576  if (FieldRes::dataResolution() == V3i(0))
577  return cend();
578  return const_iterator(*this, m_dataWindow, m_dataWindow.min);
579 }
580 
581 //----------------------------------------------------------------------------//
582 
583 template <class Data_T>
585 Field<Data_T>::cbegin(const Box3i &subset) const
586 {
587  if (subset.isEmpty())
588  return cend(subset);
589  return const_iterator(*this, subset, subset.min);
590 }
591 
592 //----------------------------------------------------------------------------//
593 
594 template <class Data_T>
597 {
598  return const_iterator(*this, m_dataWindow,
599  V3i(m_dataWindow.min.x,
600  m_dataWindow.min.y,
601  m_dataWindow.max.z + 1));
602 }
603 
604 //----------------------------------------------------------------------------//
605 
606 template <class Data_T>
608 Field<Data_T>::cend(const Box3i &subset) const
609 {
610  return const_iterator(*this, subset, V3i(subset.min.x,
611  subset.min.y,
612  subset.max.z + 1));
613 }
614 
615 //----------------------------------------------------------------------------//
616 // WritableField
617 //----------------------------------------------------------------------------//
618 
625 //----------------------------------------------------------------------------//
626 
627 template <class Data_T>
629  : public Field<Data_T>
630 {
631 public:
632 
633  // Typedefs ------------------------------------------------------------------
634 
635  typedef boost::intrusive_ptr<WritableField> Ptr;
636 
637  // RTTI replacement ----------------------------------------------------------
638 
641 
642  static const char *staticClassName()
643  {
644  return "WritableField";
645  }
646 
647  static const char* staticClassType()
648  {
650  }
651 
652  // Iterators -----------------------------------------------------------------
653 
656  class iterator;
657 
659  inline iterator begin();
661  inline iterator begin(const Box3i &subset);
663  inline iterator end();
666  inline iterator end(const Box3i &subset);
667 
668  // To be implemented by subclasses -------------------------------------------
669 
678  virtual Data_T& lvalue(int i, int j, int k) = 0;
679 
680  // Main methods --------------------------------------------------------------
681 
684  virtual void clear(const Data_T &value)
685  { std::fill(begin(), end(), value); }
686 
687 private:
688 
689  // Static data members -------------------------------------------------------
690 
692 
693  // Typedefs ------------------------------------------------------------------
694 
696 
697 };
698 
699 //----------------------------------------------------------------------------//
700 
702 
703 //----------------------------------------------------------------------------//
704 // WritableField::iterator
705 //----------------------------------------------------------------------------//
706 
707 template <class Data_T>
708 class WritableField<Data_T>::iterator
709 {
710 public:
711 #if defined(WIN32) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
712  typedef std::forward_iterator_tag iterator_category;
713  typedef Data_T value_type;
714  typedef ptrdiff_t difference_type;
715  typedef ptrdiff_t distance_type;
716  typedef Data_T *pointer;
717  typedef Data_T& reference;
718 #endif
719 
720  // Constructors --------------------------------------------------------------
721 
722  iterator(WritableField<Data_T> &field, const Box3i &window,
723  const V3i &currentPos)
724  : x(currentPos.x), y(currentPos.y), z(currentPos.z),
725  m_window(window), m_field(field)
726  { }
727 
728  // Operators -----------------------------------------------------------------
729 
730  inline const iterator& operator ++ ()
731  {
732  if (x == m_window.max.x) {
733  if (y == m_window.max.y) {
734  x = m_window.min.x;
735  y = m_window.min.y;
736  ++z;
737  } else {
738  x = m_window.min.x;
739  ++y;
740  }
741  } else {
742  ++x;
743  }
744  return *this;
745  }
746 
747  template <class Iter_T>
748  bool operator == (const Iter_T &rhs) const
749  {
750  return x == rhs.x && y == rhs.y && z == rhs.z;
751  }
752 
753  template <class Iter_T>
754  bool operator != (const Iter_T &rhs) const
755  {
756  return x != rhs.x || y != rhs.y || z != rhs.z;
757  }
758 
759  inline Data_T& operator * () const
760  {
761  return m_field.lvalue(x, y, z);
762  }
763 
764  // Public data members -------------------------------------------------------
765 
767  int x, y, z;
768 
769 private:
770 
771  // Private data members ------------------------------------------------------
772 
777 
778 };
779 
780 //----------------------------------------------------------------------------//
781 
782 template <class Data_T>
783 inline typename WritableField<Data_T>::iterator
785 {
786  if (FieldRes::dataResolution() == V3i(0))
787  return end();
788  return iterator(*this, Field<Data_T>::m_dataWindow,
790 }
791 
792 //----------------------------------------------------------------------------//
793 
794 template <class Data_T>
795 inline typename WritableField<Data_T>::iterator
797 {
798  if (subset.isEmpty())
799  return end(subset);
800  return iterator(*this, subset, subset.min);
801 }
802 
803 //----------------------------------------------------------------------------//
804 
805 template <class Data_T>
806 inline typename WritableField<Data_T>::iterator
808 { return iterator(*this, Field<Data_T>::m_dataWindow,
811  Field<Data_T>::m_dataWindow.max.z + 1));
812 }
813 
814 //----------------------------------------------------------------------------//
815 
816 template <class Data_T>
817 inline typename WritableField<Data_T>::iterator
819 { return iterator(*this, subset,
820  V3i(subset.min.x, subset.min.y, subset.max.z + 1));
821 }
822 
823 //----------------------------------------------------------------------------//
824 // ResizableField
825 //----------------------------------------------------------------------------//
826 
835 //----------------------------------------------------------------------------//
836 
837 template <class Data_T>
839  : public WritableField<Data_T>
840 {
841 public:
842 
843  // Typedefs ------------------------------------------------------------------
844 
845  typedef boost::intrusive_ptr<ResizableField> Ptr;
846 
847  // RTTI replacement ----------------------------------------------------------
848 
851 
852  static const char *staticClassName()
853  {
854  return "ResizableField";
855  }
856 
857  static const char* staticClassType()
858  {
860  }
861 
862  // Main methods --------------------------------------------------------------
863 
867  void setSize(const V3i &size);
871  void setSize(const Box3i &extents);
875  void setSize(const Box3i &extents, const Box3i &dataWindow);
879  void setSize(const V3i &size, int padding);
880 
882  void copyFrom(typename Field<Data_T>::Ptr other);
885  template <class Data_T2>
886  void copyFrom(typename Field<Data_T2>::Ptr other);
887 
889  void matchDefinition(FieldRes::Ptr fieldToMatch);
890 
891 protected:
892 
893  // Static data members -------------------------------------------------------
894 
896 
897  // Typedefs ------------------------------------------------------------------
898 
900 
901  // To be implemented by subclasses -------------------------------------------
902 
906  virtual void sizeChanged()
907  { base::m_mapping->setExtents(base::m_extents); }
908 
909 };
910 
911 //----------------------------------------------------------------------------//
912 
914 
915 //----------------------------------------------------------------------------//
916 
917 template <class Data_T>
919 {
920  assert(size.x >= 0);
921  assert(size.y >= 0);
922  assert(size.z >= 0);
923 
924  Field<Data_T>::m_extents.min = V3i(0);
925  Field<Data_T>::m_extents.max = size - V3i(1);
927 
928  // Tell subclasses that the size changed so they can update themselves.
929  sizeChanged();
930 }
931 
932 //----------------------------------------------------------------------------//
933 
934 template <class Data_T>
936 {
939  // Tell subclasses that the size changed so they can update themselves.
940  sizeChanged();
941 }
942 
943 //----------------------------------------------------------------------------//
944 
945 template <class Data_T>
947  const Box3i &dataWindow)
948 {
951  // Tell subclasses that the size changed so they can update themselves.
952  sizeChanged();
953 }
954 
955 //----------------------------------------------------------------------------//
956 
957 template <class Data_T>
958 void ResizableField<Data_T>::setSize(const V3i &size, int padding)
959 {
960  assert(size.x >= 0);
961  assert(size.y >= 0);
962  assert(size.z >= 0);
963  assert(padding >= 0);
964 
965  setSize(Box3i(V3i(0), size - V3i(1)),
966  Box3i(V3i(-padding), size + V3i(padding - 1)));
967 }
968 
969 //----------------------------------------------------------------------------//
970 
971 template <class Data_T>
973 {
974  // Set mapping
975  FieldRes::setMapping(other->mapping());
976  // Set size to match
977  setSize(other->extents(), other->dataWindow());
978 
979  // Copy over the data
980  typename base::iterator i = base::begin();
981  typename base::iterator end = base::end();
982  typename Field<Data_T>::const_iterator c = other->cbegin();
983  for (; i != end; ++i, ++c)
984  *i = *c;
985 }
986 
987 //----------------------------------------------------------------------------//
988 
989 template <class Data_T>
990 template <class Data_T2>
992 {
993  // Set mapping
994  FieldRes::setMapping(other->mapping());
995  // Set size to match
996  setSize(other->extents(), other->dataWindow());
997  // Copy over the data
998  typename base::iterator i = base::begin();
999  typename base::iterator end = base::end();
1000  typename Field<Data_T2>::const_iterator c = other->cbegin();
1001  for (; i != end; ++i, ++c)
1002  *i = *c;
1003 }
1004 
1005 //----------------------------------------------------------------------------//
1006 
1007 template <class Data_T>
1009 {
1010  setSize(fieldToMatch->extents(), fieldToMatch->dataWindow());
1011  FieldRes::setMapping(fieldToMatch->mapping());
1012 }
1013 
1014 //----------------------------------------------------------------------------//
1015 // Field-related utility functions
1016 //----------------------------------------------------------------------------//
1017 
1020 template <class Data_T, class Data_T2>
1022  typename Field<Data_T2>::Ptr b,
1023  double tolerance = 0.0)
1024 {
1025  if (a->extents() != b->extents()) {
1026  return false;
1027  }
1028  if (a->dataWindow() != b->dataWindow()) {
1029  return false;
1030  }
1031  if (!a->mapping()->isIdentical(b->mapping(), tolerance)) {
1032  return false;
1033  }
1034  return true;
1035 }
1036 
1037 //----------------------------------------------------------------------------//
1038 
1041 template <class Data_T>
1043 {
1044  if (!sameDefinition<Data_T, Data_T>(a, b)) {
1045  return false;
1046  }
1047  // If data window is the same, we can safely assume that the range of
1048  // both fields' iterators are the same.
1049  typename Field<Data_T>::const_iterator is1 = a->cbegin();
1050  typename Field<Data_T>::const_iterator is2 = b->cbegin();
1051  typename Field<Data_T>::const_iterator ie1 = a->cend();
1052  bool same = true;
1053  for (; is1 != ie1; ++is1, ++is2) {
1054  if (*is1 != *is2) {
1055  same = false;
1056  break;
1057  }
1058  }
1059  return same;
1060 }
1061 
1062 //----------------------------------------------------------------------------//
1063 
1066 inline int contToDisc(double contCoord)
1067 {
1068  return static_cast<int>(std::floor(contCoord));
1069 }
1070 
1071 //----------------------------------------------------------------------------//
1072 
1075 inline double discToCont(int discCoord)
1076 {
1077  return static_cast<double>(discCoord) + 0.5;
1078 }
1079 
1080 //----------------------------------------------------------------------------//
1081 
1083 inline V2i contToDisc(const V2d &contCoord)
1084 {
1085  return V2i(contToDisc(contCoord.x), contToDisc(contCoord.y));
1086 }
1087 
1088 //----------------------------------------------------------------------------//
1089 
1091 inline V2d discToCont(const V2i &discCoord)
1092 {
1093  return V2d(discToCont(discCoord.x), discToCont(discCoord.y));
1094 }
1095 
1096 //----------------------------------------------------------------------------//
1097 
1099 inline V3i contToDisc(const V3d &contCoord)
1100 {
1101  return V3i(contToDisc(contCoord.x), contToDisc(contCoord.y),
1102  contToDisc(contCoord.z));
1103 }
1104 
1105 //----------------------------------------------------------------------------//
1106 
1108 inline V3d discToCont(const V3i &discCoord)
1109 {
1110  return V3d(discToCont(discCoord.x), discToCont(discCoord.y),
1111  discToCont(discCoord.z));
1112 }
1113 
1114 //----------------------------------------------------------------------------//
1115 
1116 inline Box3d continuousBounds(const Box3i &bbox)
1117 {
1118  Box3d result;
1119  result.min.x = static_cast<float>(bbox.min.x);
1120  result.min.y = static_cast<float>(bbox.min.y);
1121  result.min.z = static_cast<float>(bbox.min.z);
1122  result.max.x = static_cast<float>(bbox.max.x + 1);
1123  result.max.y = static_cast<float>(bbox.max.y + 1);
1124  result.max.z = static_cast<float>(bbox.max.z + 1);
1125  return result;
1126 }
1127 
1128 //----------------------------------------------------------------------------//
1129 
1130 inline Box3i discreteBounds(const Box3d &bbox)
1131 {
1132  Box3i result;
1133  result.min.x = static_cast<int>(std::floor(bbox.min.x));
1134  result.min.y = static_cast<int>(std::floor(bbox.min.y));
1135  result.min.z = static_cast<int>(std::floor(bbox.min.z));
1136  result.max.x = static_cast<int>(std::ceil(bbox.max.x));
1137  result.max.y = static_cast<int>(std::ceil(bbox.max.y));
1138  result.max.z = static_cast<int>(std::ceil(bbox.max.z));
1139  return result;
1140 }
1141 
1142 //----------------------------------------------------------------------------//
1143 
1144 inline Box3i clipBounds(const Box3i &bbox, const Box3i &bounds)
1145 {
1146  Box3i result;
1147  result.min.x = std::max(bbox.min.x, bounds.min.x);
1148  result.min.y = std::max(bbox.min.y, bounds.min.y);
1149  result.min.z = std::max(bbox.min.z, bounds.min.z);
1150  result.max.x = std::min(bbox.max.x, bounds.max.x);
1151  result.max.y = std::min(bbox.max.y, bounds.max.y);
1152  result.max.z = std::min(bbox.max.z, bounds.max.z);
1153  return result;
1154 }
1155 
1156 //----------------------------------------------------------------------------//
1157 
1159 template <class Iter_T>
1160 void advance(Iter_T &iter, int num)
1161 {
1162  if (num <= 0) {
1163  return;
1164  }
1165  for (int i=0; i<num; ++i, ++iter) {
1166  // Empty
1167  }
1168 }
1169 
1170 //----------------------------------------------------------------------------//
1171 
1173 template <class Iter_T>
1174 void advance(Iter_T &iter, int num, const Iter_T &end)
1175 {
1176  if (num <= 0) {
1177  return;
1178  }
1179  for (int i=0; i<num && iter != end; ++i, ++iter) {
1180  // Empty
1181  }
1182 }
1183 
1184 //----------------------------------------------------------------------------//
1185 
1186 inline V3i indexToCoord(const size_t idx, const V3i &res)
1187 {
1188  const int i = idx % res.x;
1189  const int j = (idx / res.x) % res.y;
1190  const int k = idx / (res.x * res.y);
1191  return V3i(i, j, k);
1192 }
1193 
1194 //----------------------------------------------------------------------------//
1195 
1197 
1198 //----------------------------------------------------------------------------//
1199 
1200 #endif // Include guard
1201 
#define FIELD3D_NAMESPACE_HEADER_CLOSE
Definition: ns.h:58
Box3i clipBounds(const Box3i &bbox, const Box3i &bounds)
Definition: Field.h:1144
Imath::Box3i Box3i
Definition: SpiMathLib.h:77
const Box3i & extents() const
Returns the extents of the data. This signifies the relevant area that the data exists over...
Definition: Field.h:254
FieldMapping::Ptr m_mapping
Pointer to the field&#39;s mapping.
Definition: Field.h:317
Contains typedefs for the commonly used types in Field3D.
FieldMetadata< FieldBase > m_metadata
metadata
Definition: Field.h:185
static const char * staticClassType()
Definition: Field.h:123
FieldMetadata< FieldBase > & metadata()
accessor to the m_metadata class
Definition: Field.h:155
DEFINE_FIELD_RTTI_ABSTRACT_CLASS
Definition: Field.h:850
Namespace for Exception objects.
Definition: Exception.h:57
FieldRes()
This constructor ensures that we have a valid mapping at all times.
Definition: Field.h:330
MatrixFieldMapping default_mapping
Definition: Field.h:304
static const char * staticClassName()
Definition: Field.h:229
std::vector< Ptr > Vec
Definition: Field.h:219
void matchDefinition(FieldRes::Ptr fieldToMatch)
Sets up this field so that resolution and mapping matches the other.
Definition: Field.h:1008
static const char * staticClassName()
Definition: Field.h:118
boost::intrusive_ptr< ResizableField > Ptr
Definition: Field.h:845
Contains base class for reference counting with Mutex.
Definition: Field.h:394
boost::intrusive_ptr< FieldBase > Ptr
Definition: Field.h:97
WritableField< Data_T > class_type
Definition: Field.h:639
static const char * staticClassType()
Definition: Field.h:647
const_iterator cend() const
Const iterator pointing one element past the last valid one.
Definition: Field.h:596
static std::string name()
Definition: Traits.h:101
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:66
virtual void sizeChanged()
Subclasses should re-implement this if they need to perform memory allocations, etc. every time the size of the storage changes.
Definition: Field.h:906
FIELD3D_VEC3_T< T > operator*(S s, const FIELD3D_VEC3_T< T > vec)
Scalar times Vec3 multiplication. Makes the interpolation calls cleaner.
Definition: FieldInterp.h:1558
virtual size_t voxelCount() const
Counts the number of voxels. For most fields, this is just the volume of the data window...
Definition: Field.h:294
Data_T value_type
Allows us to reference the template class.
Definition: Field.h:403
boost::intrusive_ptr< FieldRes > Ptr
Definition: Field.h:218
const FieldMapping::Ptr mapping() const
Returns a pointer to the mapping.
Definition: Field.h:272
static const char * staticClassName()
Definition: Field.h:642
virtual void clear(const Data_T &value)
Clears all the voxels in the storage. Should be re-implemented by subclasses that can provide a more ...
Definition: Field.h:684
void copyFrom(typename Field< Data_T >::Ptr other)
Copies the data from another Field, also resizes.
Definition: Field.h:972
Box3i m_dataWindow
Defines the area where data is allocated. This should be treated as a closed (i.e. inclusive) interval.
Definition: Field.h:315
Contains the Log class which can be used to redirect output to an arbitrary destination.
int x
Current position.
Definition: Field.h:557
ResizableField< Data_T > class_type
Definition: Field.h:849
Imath::V3i V3i
Definition: SpiMathLib.h:71
Box3i m_window
Window to traverse.
Definition: Field.h:774
boost::intrusive_ptr< FieldMapping > Ptr
Definition: FieldMapping.h:92
void setSize(const V3i &size)
Resizes the object.
Definition: Field.h:918
const Field< Data_T > & m_field
Reference to field being iterated over.
Definition: Field.h:566
static const char * staticClassType()
Definition: Field.h:234
static const char * staticClassType()
Definition: Field.h:420
bool isInBounds(int i, int j, int k) const
Returns true is the indicies are in bounds of the data window.
Definition: Field.h:367
iterator begin()
Iterator to first element.
Definition: Field.h:784
void advance(Iter_T &iter, int num)
Definition: Field.h:1160
iterator end()
Iterator pointing one element past the last valid one.
Definition: Field.h:807
void setMapping(FieldMapping::Ptr mapping)
Sets the field&#39;s mapping.
Definition: Field.h:352
Imath::Box3d Box3d
Definition: SpiMathLib.h:79
virtual ~Field()
Dtor.
Definition: Field.h:428
FIELD3D_VEC3_T< T > floor(const FIELD3D_VEC3_T< T > &v)
Floor function for Vec3.
Definition: CoordSys.h:95
Imath::V3d V3d
Definition: SpiMathLib.h:74
Box3i discreteBounds(const Box3d &bbox)
Definition: Field.h:1130
virtual std::string dataTypeString() const
Definition: Field.h:459
std::vector< Ptr > Vec
This is a convenience typedef for the list that Field3DInputFile::readScalarLayers() and Field3DInput...
Definition: Field.h:408
const_iterator(const Field< Data_T > &field, const Box3i &window, const V3i &currentPos)
Definition: Field.h:516
#define FIELD3D_API
Definition: ns.h:77
Contains the FieldMapping base class and the NullFieldMapping and MatrixFieldMapping subclasses...
virtual std::string dataTypeString() const
Definition: Field.h:226
static TemplatedFieldType< ResizableField< Data_T > > ms_classType
Definition: Field.h:895
static const char * staticClassName()
Definition: Field.h:415
void copyMetadata(const FieldBase &field)
Copies the metadata from a second field.
Definition: Field.h:168
DEFINE_FIELD_RTTI_ABSTRACT_CLASS
Definition: Field.h:224
static TemplatedFieldType< Field< Data_T > > ms_classType
Definition: Field.h:467
DEFINE_FIELD_RTTI_ABSTRACT_CLASS
Definition: Field.h:640
Box3i m_extents
Defines the extents of the the storage. This may be larger or smaller than the data window...
Definition: Field.h:312
FieldBase base
Convenience typedef for referring to base class.
Definition: Field.h:324
Imath::V2d V2d
Definition: SpiMathLib.h:67
Used to return a string for the name of a templated field.
Definition: Traits.h:116
FieldRes base
Convenience typedef for referring to base class.
Definition: Field.h:472
V3i const dataResolution() const
Definition: Field.h:261
static TemplatedFieldType< WritableField< Data_T > > ms_classType
Definition: Field.h:691
const_iterator cbegin() const
Const iterator to first element. "cbegin" matches the tr1 c++ standard.
Definition: Field.h:574
Imath::V2i V2i
Definition: SpiMathLib.h:65
bool isIdentical(typename Field< Data_T >::Ptr a, typename Field< Data_T >::Ptr b)
Checks whether the span and data in two different fields are identical.
Definition: Field.h:1042
#define DECLARE_FIELD3D_GENERIC_EXCEPTION(name, base_class)
Used to declare a generic but named exception.
Definition: Exception.h:107
std::string attribute
Optional name of the attribute the field represents.
Definition: Field.h:178
static const char * staticClassType()
Definition: Field.h:857
virtual void mappingChanged()
Tells the subclass that the mapping changed.
Definition: Field.h:288
int contToDisc(double contCoord)
Goes from continuous coordinates to discrete coordinates See Graphics Gems - What is a pixel...
Definition: Field.h:1066
WritableField< Data_T > & m_field
Reference to field being iterated over.
Definition: Field.h:776
const_iterator(const const_iterator &i)
Definition: Field.h:511
V3i indexToCoord(const size_t idx, const V3i &res)
Definition: Field.h:1186
FieldRes class_type
Definition: Field.h:223
Basic container for metedata.
boost::intrusive_ptr< WritableField > Ptr
Definition: Field.h:635
Field< Data_T > base
Definition: Field.h:695
virtual long long int memSize() const
Returns the memory usage (in bytes)
Definition: Field.h:284
FieldBase class_type
Definition: Field.h:98
bool sameDefinition(typename Field< Data_T >::Ptr a, typename Field< Data_T2 >::Ptr b, double tolerance=0.0)
Checks whether the mapping and resolution in two different fields are identical.
Definition: Field.h:1021
FIELD3D_VEC3_T< T > ceil(const FIELD3D_VEC3_T< T > &v)
Ceil function for Vec3.
Definition: CoordSys.h:105
const Box3i & dataWindow() const
Returns the data window. Any coordinate inside this window is safe to pass to value() in the Field su...
Definition: Field.h:258
DEFINE_FIELD_RTTI_ABSTRACT_CLASS
Definition: Field.h:413
virtual void metadataHasChanged(const std::string &)
This function should implemented by concrete classes to get the callback when metadata changes...
Definition: Field.h:164
WritableField< Data_T > base
Definition: Field.h:899
const FieldMetadata< FieldBase > & metadata() const
Read only access to the m_metadata class.
Definition: Field.h:159
iterator(WritableField< Data_T > &field, const Box3i &window, const V3i &currentPos)
Definition: Field.h:722
Contains Exception base class.
Represents the mapping of a field by a matrix transform.
Definition: FieldMapping.h:327
Box3i m_window
Window to traverse.
Definition: Field.h:564
Box3d continuousBounds(const Box3i &bbox)
Definition: Field.h:1116
int x
Current position.
Definition: Field.h:767
boost::intrusive_ptr< Field > Ptr
Definition: Field.h:400
FieldMapping::Ptr mapping()
Returns a pointer to the mapping.
Definition: Field.h:268
std::string name
Optional name of the field.
Definition: Field.h:176
Field< Data_T > class_type
Definition: Field.h:412
static const char * staticClassName()
Definition: Field.h:852
double discToCont(int discCoord)
Goes from discrete coordinates to continuous coordinates See Graphics Gems - What is a pixel...
Definition: Field.h:1075
#define FIELD3D_CLASSTYPE_TEMPL_INSTANTIATION(field)
Definition: Field.h:484