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 
159  const FieldMetadata& metadata() const
160  { return m_metadata; }
161 
163  void copyMetadata(const FieldBase &field)
164  { m_metadata = field.metadata(); }
165 
167 
168  // Public data members -------------------------------------------------------
169 
171  std::string name;
173  std::string attribute;
174 
175  private:
176 
177  // Private data members ------------------------------------------------------
178 
181 
182 };
183 
184 //----------------------------------------------------------------------------//
185 // FieldRes
186 //----------------------------------------------------------------------------//
187 
205 //----------------------------------------------------------------------------//
206 
207 class FieldRes : public FieldBase
208 {
209 public:
210 
211  // Typedefs ------------------------------------------------------------------
212 
213  typedef boost::intrusive_ptr<FieldRes> Ptr;
214  typedef std::vector<Ptr> Vec;
215 
216  // RTTI replacement ----------------------------------------------------------
217 
220 
221  virtual std::string dataTypeString() const
222  { return std::string("FieldRes"); }
223 
224  static const char *staticClassName()
225  {
226  return "FieldRes";
227  }
228 
229  static const char *staticClassType()
230  {
231  return staticClassName();
232  }
233 
234  // Ctor, dtor ----------------------------------------------------------------
235 
237  FieldRes();
238 
241  FieldRes(const FieldRes &src);
242 
243  // Main methods --------------------------------------------------------------
244 
249  inline const Box3i& extents() const
250  { return m_extents; }
253  inline const Box3i& dataWindow() const
254  { return m_dataWindow; }
255 
256  inline V3i const dataResolution() const
257  { return m_dataWindow.max - m_dataWindow.min + V3i(1); }
258 
261 
264  { return m_mapping; }
265 
268  { return m_mapping; }
269 
271  bool isInBounds(int i, int j, int k) const;
272 
273  // To be implemented by subclasses -------------------------------------------
274 
279  virtual long long int memSize() const
280  { return sizeof(*this); }
281 
283  virtual void mappingChanged()
284  { /* Empty */ }
285 
289  virtual size_t voxelCount() const
290  {
291  V3i res = m_dataWindow.size() + V3i(1);
292  return res.x * res.y * res.z;
293  }
294 
295 protected:
296 
297  // Typedefs ------------------------------------------------------------------
298 
300 
301  // Data members --------------------------------------------------------------
302 
313 
314 private:
315 
316  // Typedefs ------------------------------------------------------------------
317 
319  typedef FieldBase base;
320 
321 };
322 
323 //----------------------------------------------------------------------------//
324 
326  : m_mapping(new default_mapping)
327 {
328  m_extents = Box3i(V3i(0), V3i(-1));
330  m_mapping->setExtents(m_extents);
331 }
332 
333 //----------------------------------------------------------------------------//
334 
335 inline FieldRes::FieldRes(const FieldRes &src)
336  : FieldBase(src)
337 {
338  // Call base class first
339  // FieldBase(src);
340  // Copy self
341  *this = src;
342  m_mapping = src.mapping()->clone();
343 }
344 
345 //----------------------------------------------------------------------------//
346 
348 {
349  if (mapping) {
350  m_mapping = mapping->clone();
351  m_mapping->setExtents(m_extents);
352  } else {
354  "Tried to call FieldRes::setMapping with null pointer");
355  }
356  // Tell subclasses about the mapping change
357  mappingChanged();
358 }
359 
360 //----------------------------------------------------------------------------//
361 
362 inline bool FieldRes::isInBounds(int i, int j, int k) const
363 {
364  // Check bounds
365  if (i < m_dataWindow.min.x || i > m_dataWindow.max.x ||
366  j < m_dataWindow.min.y || j > m_dataWindow.max.y ||
367  k < m_dataWindow.min.z || k > m_dataWindow.max.z) {
368  return false;
369  }
370 
371  return true;
372 }
373 
374 //----------------------------------------------------------------------------//
375 // Field
376 //----------------------------------------------------------------------------//
377 
388 template <class Data_T>
389 class Field : public FieldRes
390 {
391 public:
392 
393  // Typedefs ------------------------------------------------------------------
394 
395  typedef boost::intrusive_ptr<Field> Ptr;
396 
398  typedef Data_T value_type;
399 
403  typedef std::vector<Ptr> Vec;
404 
405  // RTTI replacement ----------------------------------------------------------
406 
409 
410  static const char *staticClassName()
411  {
412  return "Field";
413  }
414 
415  static const char* staticClassType()
416  {
418  }
419 
420  // Constructors --------------------------------------------------------------
421 
423  virtual ~Field()
424  { /* Empty */ }
425 
426  // Iterators -----------------------------------------------------------------
427 
430  class const_iterator;
431 
433  const_iterator cbegin() const;
435  const_iterator cbegin(const Box3i &subset) const;
437  const_iterator cend() const;
440  const_iterator cend(const Box3i &subset) const;
441 
442  // To be implemented by subclasses -------------------------------------------
443 
450  virtual Data_T value(int i, int j, int k) const = 0;
451 
452  // Other member functions ----------------------------------------------------
453 
454  virtual std::string dataTypeString() const
455  { return DataTypeTraits<Data_T>::name(); }
456 
457 
458 private:
459 
460  // Static data members -------------------------------------------------------
461 
463 
464  // Typedefs ------------------------------------------------------------------
465 
467  typedef FieldRes base;
468 
469 };
470 
471 //----------------------------------------------------------------------------//
472 
473 #define FIELD3D_CLASSNAME_CLASSTYPE_IMPLEMENTATION \
474  virtual std::string className() const \
475  { return staticClassName(); } \
476  virtual std::string classType() const \
477  { return staticClassType(); } \
478 
479 #define FIELD3D_CLASSTYPE_TEMPL_INSTANTIATION(field) \
480  template <typename Data_T> \
481  TemplatedFieldType<field<Data_T> > field<Data_T>::ms_classType = \
482  TemplatedFieldType<field<Data_T> >(); \
483 
485 
486 //----------------------------------------------------------------------------//
487 // Field::const_iterator
488 //----------------------------------------------------------------------------//
489 
490 template <class Data_T>
491 class Field<Data_T>::const_iterator
492 {
493 
494 public:
495 #if defined(WIN32) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
496  typedef std::forward_iterator_tag iterator_category;
497  typedef Data_T value_type;
498  typedef ptrdiff_t difference_type;
499  typedef ptrdiff_t distance_type;
500  typedef Data_T *pointer;
501  typedef Data_T& reference;
502 #endif
503 
504  // Constructors --------------------------------------------------------------
505 
507  : x(i.x), y(i.y), z(i.z),
508  m_window(i.m_window), m_field(i.m_field)
509  { }
510 
511  const_iterator(const Field<Data_T> &field, const Box3i &window,
512  const V3i &currentPos)
513  : x(currentPos.x), y(currentPos.y), z(currentPos.z),
514  m_window(window), m_field(field)
515  { }
516 
517  // Operators -----------------------------------------------------------------
518 
519  inline const const_iterator& operator ++ ()
520  {
521  if (x == m_window.max.x) {
522  if (y == m_window.max.y) {
523  x = m_window.min.x;
524  y = m_window.min.y;
525  ++z;
526  } else {
527  x = m_window.min.x;
528  ++y;
529  }
530  } else {
531  ++x;
532  }
533  return *this;
534  }
535  template <class Iter_T>
536  bool operator == (const Iter_T &rhs) const
537  {
538  return x == rhs.x && y == rhs.y && z == rhs.z;
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  inline Data_T operator * () const
546  {
547  return m_field.value(x, y, z);
548  }
549  // Public data members -------------------------------------------------------
550 
552  int x, y, z;
553 
554 private:
555 
556  // Private data members ------------------------------------------------------
557 
562 
563 };
564 
565 //----------------------------------------------------------------------------//
566 
567 template <class Data_T>
570 {
571  if (FieldRes::dataResolution() == V3i(0))
572  return cend();
573  return const_iterator(*this, m_dataWindow, m_dataWindow.min);
574 }
575 
576 //----------------------------------------------------------------------------//
577 
578 template <class Data_T>
580 Field<Data_T>::cbegin(const Box3i &subset) const
581 {
582  if (subset.isEmpty())
583  return cend(subset);
584  return const_iterator(*this, subset, subset.min);
585 }
586 
587 //----------------------------------------------------------------------------//
588 
589 template <class Data_T>
592 {
593  return const_iterator(*this, m_dataWindow,
594  V3i(m_dataWindow.min.x,
595  m_dataWindow.min.y,
596  m_dataWindow.max.z + 1));
597 }
598 
599 //----------------------------------------------------------------------------//
600 
601 template <class Data_T>
603 Field<Data_T>::cend(const Box3i &subset) const
604 {
605  return const_iterator(*this, subset, V3i(subset.min.x,
606  subset.min.y,
607  subset.max.z + 1));
608 }
609 
610 //----------------------------------------------------------------------------//
611 // WritableField
612 //----------------------------------------------------------------------------//
613 
620 //----------------------------------------------------------------------------//
621 
622 template <class Data_T>
624  : public Field<Data_T>
625 {
626 public:
627 
628  // Typedefs ------------------------------------------------------------------
629 
630  typedef boost::intrusive_ptr<WritableField> Ptr;
631 
632  // RTTI replacement ----------------------------------------------------------
633 
636 
637  static const char *staticClassName()
638  {
639  return "WritableField";
640  }
641 
642  static const char* staticClassType()
643  {
645  }
646 
647  // Iterators -----------------------------------------------------------------
648 
651  class iterator;
652 
654  inline iterator begin();
656  inline iterator begin(const Box3i &subset);
658  inline iterator end();
661  inline iterator end(const Box3i &subset);
662 
663  // To be implemented by subclasses -------------------------------------------
664 
673  virtual Data_T& lvalue(int i, int j, int k) = 0;
674 
675  // Main methods --------------------------------------------------------------
676 
679  virtual void clear(const Data_T &value)
680  { std::fill(begin(), end(), value); }
681 
682 private:
683 
684  // Static data members -------------------------------------------------------
685 
687 
688  // Typedefs ------------------------------------------------------------------
689 
691 
692 };
693 
694 //----------------------------------------------------------------------------//
695 
697 
698 //----------------------------------------------------------------------------//
699 // WritableField::iterator
700 //----------------------------------------------------------------------------//
701 
702 template <class Data_T>
703 class WritableField<Data_T>::iterator
704 {
705 public:
706 #if defined(WIN32) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
707  typedef std::forward_iterator_tag iterator_category;
708  typedef Data_T value_type;
709  typedef ptrdiff_t difference_type;
710  typedef ptrdiff_t distance_type;
711  typedef Data_T *pointer;
712  typedef Data_T& reference;
713 #endif
714 
715  // Constructors --------------------------------------------------------------
716 
717  iterator(WritableField<Data_T> &field, const Box3i &window,
718  const V3i &currentPos)
719  : x(currentPos.x), y(currentPos.y), z(currentPos.z),
720  m_window(window), m_field(field)
721  { }
722 
723  // Operators -----------------------------------------------------------------
724 
725  inline const iterator& operator ++ ()
726  {
727  if (x == m_window.max.x) {
728  if (y == m_window.max.y) {
729  x = m_window.min.x;
730  y = m_window.min.y;
731  ++z;
732  } else {
733  x = m_window.min.x;
734  ++y;
735  }
736  } else {
737  ++x;
738  }
739  return *this;
740  }
741 
742  template <class Iter_T>
743  bool operator == (const Iter_T &rhs) const
744  {
745  return x == rhs.x && y == rhs.y && z == rhs.z;
746  }
747 
748  template <class Iter_T>
749  bool operator != (const Iter_T &rhs) const
750  {
751  return x != rhs.x || y != rhs.y || z != rhs.z;
752  }
753 
754  inline Data_T& operator * () const
755  {
756  return m_field.lvalue(x, y, z);
757  }
758 
759  // Public data members -------------------------------------------------------
760 
762  int x, y, z;
763 
764 private:
765 
766  // Private data members ------------------------------------------------------
767 
772 
773 };
774 
775 //----------------------------------------------------------------------------//
776 
777 template <class Data_T>
778 inline typename WritableField<Data_T>::iterator
780 {
781  if (FieldRes::dataResolution() == V3i(0))
782  return end();
783  return iterator(*this, Field<Data_T>::m_dataWindow,
785 }
786 
787 //----------------------------------------------------------------------------//
788 
789 template <class Data_T>
790 inline typename WritableField<Data_T>::iterator
792 {
793  if (subset.isEmpty())
794  return end(subset);
795  return iterator(*this, subset, subset.min);
796 }
797 
798 //----------------------------------------------------------------------------//
799 
800 template <class Data_T>
801 inline typename WritableField<Data_T>::iterator
803 { return iterator(*this, Field<Data_T>::m_dataWindow,
806  Field<Data_T>::m_dataWindow.max.z + 1));
807 }
808 
809 //----------------------------------------------------------------------------//
810 
811 template <class Data_T>
812 inline typename WritableField<Data_T>::iterator
814 { return iterator(*this, subset,
815  V3i(subset.min.x, subset.min.y, subset.max.z + 1));
816 }
817 
818 //----------------------------------------------------------------------------//
819 // ResizableField
820 //----------------------------------------------------------------------------//
821 
830 //----------------------------------------------------------------------------//
831 
832 template <class Data_T>
834  : public WritableField<Data_T>
835 {
836 public:
837 
838  // Typedefs ------------------------------------------------------------------
839 
840  typedef boost::intrusive_ptr<ResizableField> Ptr;
841 
842  // RTTI replacement ----------------------------------------------------------
843 
846 
847  static const char *staticClassName()
848  {
849  return "ResizableField";
850  }
851 
852  static const char* staticClassType()
853  {
855  }
856 
857  // Main methods --------------------------------------------------------------
858 
862  void setSize(const V3i &size);
866  void setSize(const Box3i &extents);
870  void setSize(const Box3i &extents, const Box3i &dataWindow);
874  void setSize(const V3i &size, int padding);
875 
877  void copyFrom(typename Field<Data_T>::Ptr other);
880  template <class Data_T2>
881  void copyFrom(typename Field<Data_T2>::Ptr other);
882 
884  void matchDefinition(FieldRes::Ptr fieldToMatch);
885 
886 protected:
887 
888  // Static data members -------------------------------------------------------
889 
891 
892  // Typedefs ------------------------------------------------------------------
893 
895 
896  // To be implemented by subclasses -------------------------------------------
897 
901  virtual void sizeChanged()
902  { base::m_mapping->setExtents(base::m_extents); }
903 
904 };
905 
906 //----------------------------------------------------------------------------//
907 
909 
910 //----------------------------------------------------------------------------//
911 
912 template <class Data_T>
914 {
915  assert(size.x >= 0);
916  assert(size.y >= 0);
917  assert(size.z >= 0);
918 
919  Field<Data_T>::m_extents.min = V3i(0);
920  Field<Data_T>::m_extents.max = size - V3i(1);
922 
923  // Tell subclasses that the size changed so they can update themselves.
924  sizeChanged();
925 }
926 
927 //----------------------------------------------------------------------------//
928 
929 template <class Data_T>
931 {
932  Field<Data_T>::m_extents = extents;
933  Field<Data_T>::m_dataWindow = extents;
934  // Tell subclasses that the size changed so they can update themselves.
935  sizeChanged();
936 }
937 
938 //----------------------------------------------------------------------------//
939 
940 template <class Data_T>
942  const Box3i &dataWindow)
943 {
944  Field<Data_T>::m_extents = extents;
945  Field<Data_T>::m_dataWindow = dataWindow;
946  // Tell subclasses that the size changed so they can update themselves.
947  sizeChanged();
948 }
949 
950 //----------------------------------------------------------------------------//
951 
952 template <class Data_T>
953 void ResizableField<Data_T>::setSize(const V3i &size, int padding)
954 {
955  assert(size.x >= 0);
956  assert(size.y >= 0);
957  assert(size.z >= 0);
958  assert(padding >= 0);
959 
960  setSize(Box3i(V3i(0), size - V3i(1)),
961  Box3i(V3i(-padding), size + V3i(padding - 1)));
962 }
963 
964 //----------------------------------------------------------------------------//
965 
966 template <class Data_T>
968 {
969  // Set mapping
970  FieldRes::setMapping(other->mapping());
971  // Set size to match
972  setSize(other->extents(), other->dataWindow());
973 
974  // Copy over the data
975  typename base::iterator i = base::begin();
976  typename base::iterator end = base::end();
977  typename Field<Data_T>::const_iterator c = other->cbegin();
978  for (; i != end; ++i, ++c)
979  *i = *c;
980 }
981 
982 //----------------------------------------------------------------------------//
983 
984 template <class Data_T>
985 template <class Data_T2>
987 {
988  // Set mapping
989  FieldRes::setMapping(other->mapping());
990  // Set size to match
991  setSize(other->extents(), other->dataWindow());
992  // Copy over the data
993  typename base::iterator i = base::begin();
994  typename base::iterator end = base::end();
995  typename Field<Data_T2>::const_iterator c = other->cbegin();
996  for (; i != end; ++i, ++c)
997  *i = *c;
998 }
999 
1000 //----------------------------------------------------------------------------//
1001 
1002 template <class Data_T>
1004 {
1005  setSize(fieldToMatch->extents(), fieldToMatch->dataWindow());
1006  FieldRes::setMapping(fieldToMatch->mapping());
1007 }
1008 
1009 //----------------------------------------------------------------------------//
1010 // Field-related utility functions
1011 //----------------------------------------------------------------------------//
1012 
1015 template <class Data_T, class Data_T2>
1017  typename Field<Data_T2>::Ptr b,
1018  double tolerance = 0.0)
1019 {
1020  if (a->extents() != b->extents()) {
1021  return false;
1022  }
1023  if (a->dataWindow() != b->dataWindow()) {
1024  return false;
1025  }
1026  if (!a->mapping()->isIdentical(b->mapping(), tolerance)) {
1027  return false;
1028  }
1029  return true;
1030 }
1031 
1032 //----------------------------------------------------------------------------//
1033 
1036 template <class Data_T>
1038 {
1039  if (!sameDefinition<Data_T, Data_T>(a, b)) {
1040  return false;
1041  }
1042  // If data window is the same, we can safely assume that the range of
1043  // both fields' iterators are the same.
1044  typename Field<Data_T>::const_iterator is1 = a->cbegin();
1045  typename Field<Data_T>::const_iterator is2 = b->cbegin();
1046  typename Field<Data_T>::const_iterator ie1 = a->cend();
1047  bool same = true;
1048  for (; is1 != ie1; ++is1, ++is2) {
1049  if (*is1 != *is2) {
1050  same = false;
1051  break;
1052  }
1053  }
1054  return same;
1055 }
1056 
1057 //----------------------------------------------------------------------------//
1058 
1061 inline int contToDisc(double contCoord)
1062 {
1063  return static_cast<int>(std::floor(contCoord));
1064 }
1065 
1066 //----------------------------------------------------------------------------//
1067 
1070 inline double discToCont(int discCoord)
1071 {
1072  return static_cast<double>(discCoord) + 0.5;
1073 }
1074 
1075 //----------------------------------------------------------------------------//
1076 
1078 inline V2i contToDisc(const V2d &contCoord)
1079 {
1080  return V2i(contToDisc(contCoord.x), contToDisc(contCoord.y));
1081 }
1082 
1083 //----------------------------------------------------------------------------//
1084 
1086 inline V2d discToCont(const V2i &discCoord)
1087 {
1088  return V2d(discToCont(discCoord.x), discToCont(discCoord.y));
1089 }
1090 
1091 //----------------------------------------------------------------------------//
1092 
1094 inline V3i contToDisc(const V3d &contCoord)
1095 {
1096  return V3i(contToDisc(contCoord.x), contToDisc(contCoord.y),
1097  contToDisc(contCoord.z));
1098 }
1099 
1100 //----------------------------------------------------------------------------//
1101 
1103 inline V3d discToCont(const V3i &discCoord)
1104 {
1105  return V3d(discToCont(discCoord.x), discToCont(discCoord.y),
1106  discToCont(discCoord.z));
1107 }
1108 
1109 //----------------------------------------------------------------------------//
1110 
1111 inline Box3d continuousBounds(const Box3i &bbox)
1112 {
1113  Box3d result;
1114  result.min.x = static_cast<float>(bbox.min.x);
1115  result.min.y = static_cast<float>(bbox.min.y);
1116  result.min.z = static_cast<float>(bbox.min.z);
1117  result.max.x = static_cast<float>(bbox.max.x + 1);
1118  result.max.y = static_cast<float>(bbox.max.y + 1);
1119  result.max.z = static_cast<float>(bbox.max.z + 1);
1120  return result;
1121 }
1122 
1123 //----------------------------------------------------------------------------//
1124 
1128 inline Box3i discreteBounds(const Box3d &bbox)
1129 {
1130  using std::floor;
1131  using std::ceil;
1132 
1133  Box3i result;
1134  result.min.x = static_cast<int>(floor(clampForType<double, int>(bbox.min.x)));
1135  result.min.y = static_cast<int>(floor(clampForType<double, int>(bbox.min.y)));
1136  result.min.z = static_cast<int>(floor(clampForType<double, int>(bbox.min.z)));
1137  result.max.x = static_cast<int>(ceil(clampForType<double, int>(bbox.max.x)));
1138  result.max.y = static_cast<int>(ceil(clampForType<double, int>(bbox.max.y)));
1139  result.max.z = static_cast<int>(ceil(clampForType<double, int>(bbox.max.z)));
1140  return result;
1141 }
1142 
1143 //----------------------------------------------------------------------------//
1144 
1145 inline Box3i clipBounds(const Box3i &bbox, const Box3i &bounds)
1146 {
1147  Box3i result;
1148  result.min.x = std::max(bbox.min.x, bounds.min.x);
1149  result.min.y = std::max(bbox.min.y, bounds.min.y);
1150  result.min.z = std::max(bbox.min.z, bounds.min.z);
1151  result.max.x = std::min(bbox.max.x, bounds.max.x);
1152  result.max.y = std::min(bbox.max.y, bounds.max.y);
1153  result.max.z = std::min(bbox.max.z, bounds.max.z);
1154  return result;
1155 }
1156 
1157 //----------------------------------------------------------------------------//
1158 
1160 template <class Iter_T>
1161 void advance(Iter_T &iter, int num)
1162 {
1163  if (num <= 0) {
1164  return;
1165  }
1166  for (int i=0; i<num; ++i, ++iter) {
1167  // Empty
1168  }
1169 }
1170 
1171 //----------------------------------------------------------------------------//
1172 
1174 template <class Iter_T>
1175 void advance(Iter_T &iter, int num, const Iter_T &end)
1176 {
1177  if (num <= 0) {
1178  return;
1179  }
1180  for (int i=0; i<num && iter != end; ++i, ++iter) {
1181  // Empty
1182  }
1183 }
1184 
1185 //----------------------------------------------------------------------------//
1186 
1187 inline V3i indexToCoord(const size_t idx, const V3i &res)
1188 {
1189  const int i = idx % res.x;
1190  const int j = (idx / res.x) % res.y;
1191  const int k = idx / (res.x * res.y);
1192  return V3i(i, j, k);
1193 }
1194 
1195 //----------------------------------------------------------------------------//
1196 
1198 
1199 //----------------------------------------------------------------------------//
1200 
1201 #endif // Include guard
1202 
FieldRes::m_extents
Box3i m_extents
Defines the extents of the the storage. This may be larger or smaller than the data window,...
Definition: Field.h:307
ResizableField::setSize
void setSize(const V3i &size)
Resizes the object.
Definition: Field.h:913
FieldRes::Vec
std::vector< Ptr > Vec
Definition: Field.h:214
FIELD3D_NAMESPACE_HEADER_CLOSE
#define FIELD3D_NAMESPACE_HEADER_CLOSE
Definition: ns.h:58
FieldBase
Definition: Field.h:92
isIdentical
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:1037
WritableField::iterator::x
int x
Current position.
Definition: Field.h:762
FieldRes::dataWindow
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:253
FieldBase::classType
virtual std::string classType() const =0
Returns the full class type string.
V3i
Imath::V3i V3i
Definition: SpiMathLib.h:71
Field::DEFINE_FIELD_RTTI_ABSTRACT_CLASS
DEFINE_FIELD_RTTI_ABSTRACT_CLASS
Definition: Field.h:408
WritableField::lvalue
virtual Data_T & lvalue(int i, int j, int k)=0
Write access to a voxel. The coordinates are global coordinates.
FieldRes::staticClassType
static const char * staticClassType()
Definition: Field.h:229
FieldRes::voxelCount
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:289
Field::dataTypeString
virtual std::string dataTypeString() const
Definition: Field.h:454
contToDisc
int contToDisc(double contCoord)
Goes from continuous coordinates to discrete coordinates See Graphics Gems - What is a pixel.
Definition: Field.h:1061
WritableField::iterator
Definition: Field.h:704
V3d
Imath::V3d V3d
Definition: SpiMathLib.h:74
operator*
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
FieldRes::mappingChanged
virtual void mappingChanged()
Tells the subclass that the mapping changed.
Definition: Field.h:283
Msg::SevWarning
@ SevWarning
Definition: Log.h:68
FieldBase::class_type
FieldBase class_type
Definition: Field.h:98
Types.h
Contains typedefs for the commonly used types in Field3D.
discToCont
double discToCont(int discCoord)
Goes from discrete coordinates to continuous coordinates See Graphics Gems - What is a pixel.
Definition: Field.h:1070
FieldBase::metadata
FieldMetadata & metadata()
accessor to the m_metadata class
Definition: Field.h:155
Field::Ptr
boost::intrusive_ptr< Field > Ptr
Definition: Field.h:395
FieldRes::mapping
const FieldMapping::Ptr mapping() const
Returns a pointer to the mapping.
Definition: Field.h:267
FieldMetadata.h
Basic container for metedata.
Field::const_iterator::x
int x
Current position.
Definition: Field.h:552
FIELD3D_CLASSTYPE_TEMPL_INSTANTIATION
#define FIELD3D_CLASSTYPE_TEMPL_INSTANTIATION(field)
Definition: Field.h:479
WritableField::end
iterator end()
Iterator pointing one element past the last valid one.
Definition: Field.h:802
FieldRes::m_dataWindow
Box3i m_dataWindow
Defines the area where data is allocated. This should be treated as a closed (i.e....
Definition: Field.h:310
FieldBase::className
virtual std::string className() const =0
Returns the class name of the object. Used by the class pool and when writing the data to disk.
FieldBase::staticClassType
static const char * staticClassType()
Definition: Field.h:123
FieldBase::metadata
const FieldMetadata & metadata() const
Read only access to the m_metadata class.
Definition: Field.h:159
WritableField::ms_classType
static TemplatedFieldType< WritableField< Data_T > > ms_classType
Definition: Field.h:686
Field::value_type
Data_T value_type
Allows us to reference the template class.
Definition: Field.h:398
WritableField::DEFINE_FIELD_RTTI_ABSTRACT_CLASS
DEFINE_FIELD_RTTI_ABSTRACT_CLASS
Definition: Field.h:635
FieldBase::clone
virtual Ptr clone() const =0
Returns a pointer to a copy of the field, pure virtual so ensure derived classes properly implement i...
ResizableField::base
WritableField< Data_T > base
Definition: Field.h:894
FieldMapping::Ptr
boost::intrusive_ptr< FieldMapping > Ptr
Definition: FieldMapping.h:92
Field::const_iterator
Definition: Field.h:492
MetadataCallback
Definition: FieldMetadata.h:71
FieldBase::name
std::string name
Optional name of the field.
Definition: Field.h:171
FieldMapping.h
Contains the FieldMapping base class and the NullFieldMapping and MatrixFieldMapping subclasses.
RefCount.h
Contains base class for reference counting with Mutex.
indexToCoord
V3i indexToCoord(const size_t idx, const V3i &res)
Definition: Field.h:1187
Field::cbegin
const_iterator cbegin() const
Const iterator to first element. "cbegin" matches the tr1 c++ standard.
Definition: Field.h:569
FieldBase::Ptr
boost::intrusive_ptr< FieldBase > Ptr
Definition: Field.h:97
FieldBase::copyMetadata
void copyMetadata(const FieldBase &field)
Copies the metadata from a second field.
Definition: Field.h:163
FieldRes::setMapping
void setMapping(FieldMapping::Ptr mapping)
Sets the field's mapping.
Definition: Field.h:347
Field::cend
const_iterator cend(const Box3i &subset) const
Const iterator pointing one element past the last valid one (for a subset)
Definition: Field.h:603
WritableField::iterator::m_window
Box3i m_window
Window to traverse.
Definition: Field.h:769
FieldRes::dataResolution
V3i const dataResolution() const
Definition: Field.h:256
sameDefinition
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:1016
FieldRes::FieldRes
FieldRes()
This constructor ensures that we have a valid mapping at all times.
Definition: Field.h:325
FieldRes
Definition: Field.h:208
WritableField::class_type
WritableField< Data_T > class_type
Definition: Field.h:634
detail::floor
FIELD3D_VEC3_T< T > floor(const FIELD3D_VEC3_T< T > &v)
Floor function for Vec3.
Definition: CoordSys.h:104
Field::staticClassType
static const char * staticClassType()
Definition: Field.h:415
Log.h
Contains the Log class which can be used to redirect output to an arbitrary destination.
FieldRes::class_type
FieldRes class_type
Definition: Field.h:218
FieldRes::mapping
FieldMapping::Ptr mapping()
Returns a pointer to the mapping.
Definition: Field.h:263
discreteBounds
Box3i discreteBounds(const Box3d &bbox)
Converts a floating point bounding box to an integer bounding box.
Definition: Field.h:1128
ns.h
Exception.h
Contains Exception base class.
continuousBounds
Box3d continuousBounds(const Box3i &bbox)
Definition: Field.h:1111
FieldRes::Ptr
boost::intrusive_ptr< FieldRes > Ptr
Definition: Field.h:213
FieldRes::isInBounds
bool isInBounds(int i, int j, int k) const
Returns true is the indicies are in bounds of the data window.
Definition: Field.h:362
FieldRes::default_mapping
MatrixFieldMapping default_mapping
Definition: Field.h:299
ResizableField::staticClassType
static const char * staticClassType()
Definition: Field.h:852
FieldBase::attribute
std::string attribute
Optional name of the attribute the field represents.
Definition: Field.h:173
detail::max
T max(const T a, const T2 b)
Max operation on mixed types.
Definition: FieldSampler.h:32
WritableField::begin
iterator begin()
Iterator to first element.
Definition: Field.h:779
Field::ms_classType
static TemplatedFieldType< Field< Data_T > > ms_classType
Definition: Field.h:462
FieldRes::m_mapping
FieldMapping::Ptr m_mapping
Pointer to the field's mapping.
Definition: Field.h:312
advance
void advance(Iter_T &iter, int num)
Definition: Field.h:1161
ResizableField::ms_classType
static TemplatedFieldType< ResizableField< Data_T > > ms_classType
Definition: Field.h:890
WritableField::base
Field< Data_T > base
Definition: Field.h:690
WritableField::Ptr
boost::intrusive_ptr< WritableField > Ptr
Definition: Field.h:630
V2i
Imath::V2i V2i
Definition: SpiMathLib.h:65
Field::cend
const_iterator cend() const
Const iterator pointing one element past the last valid one.
Definition: Field.h:591
Field::Vec
std::vector< Ptr > Vec
This is a convenience typedef for the list that Field3DInputFile::readScalarLayers() and Field3DInput...
Definition: Field.h:403
Box3d
Imath::Box3d Box3d
Definition: SpiMathLib.h:79
FieldMetadata
Definition: FieldMetadata.h:85
FieldRes::memSize
virtual long long int memSize() const
Returns the memory usage (in bytes)
Definition: Field.h:279
ResizableField::staticClassName
static const char * staticClassName()
Definition: Field.h:847
ResizableField::matchDefinition
void matchDefinition(FieldRes::Ptr fieldToMatch)
Sets up this field so that resolution and mapping matches the other.
Definition: Field.h:1003
Field::const_iterator::m_field
const Field< Data_T > & m_field
Reference to field being iterated over.
Definition: Field.h:561
Field::staticClassName
static const char * staticClassName()
Definition: Field.h:410
ResizableField::Ptr
boost::intrusive_ptr< ResizableField > Ptr
Definition: Field.h:840
FieldRes::staticClassName
static const char * staticClassName()
Definition: Field.h:224
detail::ceil
FIELD3D_VEC3_T< T > ceil(const FIELD3D_VEC3_T< T > &v)
Ceil function for Vec3.
Definition: CoordSys.h:114
MatrixFieldMapping
Represents the mapping of a field by a matrix transform.
Definition: FieldMapping.h:335
WritableField::staticClassName
static const char * staticClassName()
Definition: Field.h:637
V2d
Imath::V2d V2d
Definition: SpiMathLib.h:67
Exc
Namespace for Exception objects.
Definition: Exception.h:57
Field::const_iterator::const_iterator
const_iterator(const const_iterator &i)
Definition: Field.h:506
WritableField::iterator::m_field
WritableField< Data_T > & m_field
Reference to field being iterated over.
Definition: Field.h:771
Field::~Field
virtual ~Field()
Dtor.
Definition: Field.h:423
Field::const_iterator::m_window
Box3i m_window
Window to traverse.
Definition: Field.h:559
FieldRes::extents
const Box3i & extents() const
Returns the extents of the data. This signifies the relevant area that the data exists over....
Definition: Field.h:249
Field::const_iterator::const_iterator
const_iterator(const Field< Data_T > &field, const Box3i &window, const V3i &currentPos)
Definition: Field.h:511
Traits.h
Field::base
FieldRes base
Convenience typedef for referring to base class.
Definition: Field.h:467
FieldBase::m_metadata
FieldMetadata m_metadata
metadata
Definition: Field.h:180
ResizableField::sizeChanged
virtual void sizeChanged()
Subclasses should re-implement this if they need to perform memory allocations, etc....
Definition: Field.h:901
FieldRes::base
FieldBase base
Convenience typedef for referring to base class.
Definition: Field.h:319
Field::cbegin
const_iterator cbegin(const Box3i &subset) const
Const iterator to first element of specific subset.
Definition: Field.h:580
FieldRes::dataTypeString
virtual std::string dataTypeString() const
Definition: Field.h:221
FIELD3D_API
#define FIELD3D_API
Definition: ns.h:77
FIELD3D_NAMESPACE_OPEN
Definition: FieldMapping.cpp:74
Box3i
Imath::Box3i Box3i
Definition: SpiMathLib.h:77
WritableField::iterator::z
int z
Definition: Field.h:762
ResizableField::DEFINE_FIELD_RTTI_ABSTRACT_CLASS
DEFINE_FIELD_RTTI_ABSTRACT_CLASS
Definition: Field.h:845
clipBounds
Box3i clipBounds(const Box3i &bbox, const Box3i &bounds)
Definition: Field.h:1145
Msg::print
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
Field
Definition: Field.h:390
ResizableField::class_type
ResizableField< Data_T > class_type
Definition: Field.h:844
TemplatedFieldType
Used to return a string for the name of a templated field.
Definition: Traits.h:283
RefBase
Definition: RefCount.h:107
DataTypeTraits::name
static std::string name()
Definition: Traits.h:267
detail::min
T min(const T a, const T2 b)
Min operation on mixed types.
Definition: FieldSampler.h:25
WritableField::staticClassType
static const char * staticClassType()
Definition: Field.h:642
FieldBase::staticClassName
static const char * staticClassName()
Definition: Field.h:118
Field::const_iterator::z
int z
Definition: Field.h:552
Field::class_type
Field< Data_T > class_type
Definition: Field.h:407
WritableField
Definition: Field.h:625
ResizableField::copyFrom
void copyFrom(typename Field< Data_T >::Ptr other)
Copies the data from another Field, also resizes.
Definition: Field.h:967
Field::value
virtual Data_T value(int i, int j, int k) const =0
Read access to a voxel. The coordinates are in integer voxel space .
DECLARE_FIELD3D_GENERIC_EXCEPTION
#define DECLARE_FIELD3D_GENERIC_EXCEPTION(name, base_class)
Used to declare a generic but named exception.
Definition: Exception.h:107
ResizableField
Definition: Field.h:835
WritableField::iterator::iterator
iterator(WritableField< Data_T > &field, const Box3i &window, const V3i &currentPos)
Definition: Field.h:717
WritableField::clear
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:679
FieldRes::DEFINE_FIELD_RTTI_ABSTRACT_CLASS
DEFINE_FIELD_RTTI_ABSTRACT_CLASS
Definition: Field.h:219