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
73namespace Exc {
74
75DECLARE_FIELD3D_GENERIC_EXCEPTION(MemoryException, Exception)
76DECLARE_FIELD3D_GENERIC_EXCEPTION(ResizeException, Exception)
77
78} // namespace Exc
79
80//----------------------------------------------------------------------------//
81// FieldBase
82//----------------------------------------------------------------------------//
83
92{
93public:
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
207class FieldRes : public FieldBase
208{
209public:
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
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
295protected:
296
297 // Typedefs ------------------------------------------------------------------
298
300
301 // Data members --------------------------------------------------------------
302
313
314private:
315
316 // Typedefs ------------------------------------------------------------------
317
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
335inline 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
358}
359
360//----------------------------------------------------------------------------//
361
362inline 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
388template <class Data_T>
389class Field : public FieldRes
390{
391public:
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
458private:
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
490template <class Data_T>
491class Field<Data_T>::const_iterator
492{
493
494public:
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
554private:
555
556 // Private data members ------------------------------------------------------
557
562
563};
564
565//----------------------------------------------------------------------------//
566
567template <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
578template <class Data_T>
580Field<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
589template <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
601template <class Data_T>
603Field<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
622template <class Data_T>
624 : public Field<Data_T>
625{
626public:
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
682private:
683
684 // Static data members -------------------------------------------------------
685
687
688 // Typedefs ------------------------------------------------------------------
689
691
692};
693
694//----------------------------------------------------------------------------//
695
697
698//----------------------------------------------------------------------------//
699// WritableField::iterator
700//----------------------------------------------------------------------------//
701
702template <class Data_T>
704{
705public:
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
764private:
765
766 // Private data members ------------------------------------------------------
767
772
773};
774
775//----------------------------------------------------------------------------//
776
777template <class Data_T>
778inline typename WritableField<Data_T>::iterator
780{
781 if (FieldRes::dataResolution() == V3i(0))
782 return end();
785}
786
787//----------------------------------------------------------------------------//
788
789template <class Data_T>
790inline 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
800template <class Data_T>
801inline typename WritableField<Data_T>::iterator
808
809//----------------------------------------------------------------------------//
810
811template <class Data_T>
812inline 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
832template <class Data_T>
834 : public WritableField<Data_T>
835{
836public:
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
886protected:
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
912template <class Data_T>
914{
915 assert(size.x >= 0);
916 assert(size.y >= 0);
917 assert(size.z >= 0);
918
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
929template <class Data_T>
931{
932 Field<Data_T>::m_extents = extents;
934 // Tell subclasses that the size changed so they can update themselves.
935 sizeChanged();
936}
937
938//----------------------------------------------------------------------------//
939
940template <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
952template <class Data_T>
953void 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
966template <class Data_T>
968{
969 // Set 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
984template <class Data_T>
985template <class Data_T2>
987{
988 // Set 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
1002template <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
1015template <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
1036template <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
1061inline int contToDisc(double contCoord)
1062{
1063 return static_cast<int>(std::floor(contCoord));
1064}
1065
1066//----------------------------------------------------------------------------//
1067
1070inline double discToCont(int discCoord)
1071{
1072 return static_cast<double>(discCoord) + 0.5;
1073}
1074
1075//----------------------------------------------------------------------------//
1076
1078inline V2i contToDisc(const V2d &contCoord)
1079{
1080 return V2i(contToDisc(contCoord.x), contToDisc(contCoord.y));
1081}
1082
1083//----------------------------------------------------------------------------//
1084
1086inline V2d discToCont(const V2i &discCoord)
1087{
1088 return V2d(discToCont(discCoord.x), discToCont(discCoord.y));
1089}
1090
1091//----------------------------------------------------------------------------//
1092
1094inline V3i contToDisc(const V3d &contCoord)
1095{
1096 return V3i(contToDisc(contCoord.x), contToDisc(contCoord.y),
1097 contToDisc(contCoord.z));
1098}
1099
1100//----------------------------------------------------------------------------//
1101
1103inline V3d discToCont(const V3i &discCoord)
1104{
1105 return V3d(discToCont(discCoord.x), discToCont(discCoord.y),
1106 discToCont(discCoord.z));
1107}
1108
1109//----------------------------------------------------------------------------//
1110
1111inline 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
1128inline 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
1145inline 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
1160template <class Iter_T>
1161void 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
1174template <class Iter_T>
1175void 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
1187inline 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
Contains Exception base class.
#define DECLARE_FIELD3D_GENERIC_EXCEPTION(name, base_class)
Used to declare a generic but named exception.
Definition Exception.h:107
FIELD3D_VEC3_T< T > operator*(S s, const FIELD3D_VEC3_T< T > vec)
Scalar times Vec3 multiplication. Makes the interpolation calls cleaner.
Contains the FieldMapping base class and the NullFieldMapping and MatrixFieldMapping subclasses.
Basic container for metedata.
Box3d continuousBounds(const Box3i &bbox)
Definition Field.h:1111
int contToDisc(double contCoord)
Goes from continuous coordinates to discrete coordinates See Graphics Gems - What is a pixel.
Definition Field.h:1061
Box3i discreteBounds(const Box3d &bbox)
Converts a floating point bounding box to an integer bounding box.
Definition Field.h:1128
Box3i clipBounds(const Box3i &bbox, const Box3i &bounds)
Definition Field.h:1145
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
#define FIELD3D_CLASSTYPE_TEMPL_INSTANTIATION(field)
Definition Field.h:479
double discToCont(int discCoord)
Goes from discrete coordinates to continuous coordinates See Graphics Gems - What is a pixel.
Definition Field.h:1070
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
V3i indexToCoord(const size_t idx, const V3i &res)
Definition Field.h:1187
Contains the Log class which can be used to redirect output to an arbitrary destination.
Contains base class for reference counting with Mutex.
Imath::V2i V2i
Definition SpiMathLib.h:65
Imath::V2d V2d
Definition SpiMathLib.h:67
Imath::Box3d Box3d
Definition SpiMathLib.h:79
Imath::V3i V3i
Definition SpiMathLib.h:71
Imath::V3d V3d
Definition SpiMathLib.h:74
Imath::Box3i Box3i
Definition SpiMathLib.h:77
Contains typedefs for the commonly used types in Field3D.
To_T clampForType(const From_T v)
Definition Types.h:94
virtual Ptr clone() const =0
Returns a pointer to a copy of the field, pure virtual so ensure derived classes properly implement i...
FieldBase class_type
Definition Field.h:98
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.
const FieldMetadata & metadata() const
Read only access to the m_metadata class.
Definition Field.h:159
boost::intrusive_ptr< FieldBase > Ptr
Definition Field.h:97
FieldMetadata & metadata()
accessor to the m_metadata class
Definition Field.h:155
static const char * staticClassName()
Definition Field.h:118
static const char * staticClassType()
Definition Field.h:123
std::string attribute
Optional name of the attribute the field represents.
Definition Field.h:173
void copyMetadata(const FieldBase &field)
Copies the metadata from a second field.
Definition Field.h:163
virtual std::string classType() const =0
Returns the full class type string.
FieldMetadata m_metadata
metadata
Definition Field.h:180
std::string name
Optional name of the field.
Definition Field.h:171
boost::intrusive_ptr< FieldMapping > Ptr
Box3i m_extents
Defines the extents of the the storage. This may be larger or smaller than the data window,...
Definition Field.h:307
boost::intrusive_ptr< FieldRes > Ptr
Definition Field.h:213
static const char * staticClassType()
Definition Field.h:229
V3i const dataResolution() const
Definition Field.h:256
virtual std::string dataTypeString() const
Definition Field.h:221
void setMapping(FieldMapping::Ptr mapping)
Sets the field's mapping.
Definition Field.h:347
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
FieldMapping::Ptr mapping()
Returns a pointer to the mapping.
Definition Field.h:263
Box3i m_dataWindow
Defines the area where data is allocated. This should be treated as a closed (i.e....
Definition Field.h:310
DEFINE_FIELD_RTTI_ABSTRACT_CLASS
Definition Field.h:219
virtual long long int memSize() const
Returns the memory usage (in bytes)
Definition Field.h:279
const FieldMapping::Ptr mapping() const
Returns a pointer to the mapping.
Definition Field.h:267
FieldMapping::Ptr m_mapping
Pointer to the field's mapping.
Definition Field.h:312
FieldRes()
This constructor ensures that we have a valid mapping at all times.
Definition Field.h:325
FieldBase base
Convenience typedef for referring to base class.
Definition Field.h:319
const Box3i & extents() const
Returns the extents of the data. This signifies the relevant area that the data exists over....
Definition Field.h:249
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
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
MatrixFieldMapping default_mapping
Definition Field.h:299
virtual void mappingChanged()
Tells the subclass that the mapping changed.
Definition Field.h:283
std::vector< Ptr > Vec
Definition Field.h:214
FieldRes class_type
Definition Field.h:218
static const char * staticClassName()
Definition Field.h:224
int x
Current position.
Definition Field.h:552
const Field< Data_T > & m_field
Reference to field being iterated over.
Definition Field.h:561
Box3i m_window
Window to traverse.
Definition Field.h:559
const_iterator(const const_iterator &i)
Definition Field.h:506
const_iterator(const Field< Data_T > &field, const Box3i &window, const V3i &currentPos)
Definition Field.h:511
Definition Field.h:390
static TemplatedFieldType< Field< Data_T > > ms_classType
Definition Field.h:462
DEFINE_FIELD_RTTI_ABSTRACT_CLASS
Definition Field.h:408
boost::intrusive_ptr< Field > Ptr
Definition Field.h:395
virtual std::string dataTypeString() const
Definition Field.h:454
static const char * staticClassType()
Definition Field.h:415
virtual Data_T value(int i, int j, int k) const =0
Read access to a voxel. The coordinates are in integer voxel space .
const_iterator cend(const Box3i &subset) const
Const iterator pointing one element past the last valid one (for a subset)
Definition Field.h:603
virtual ~Field()
Dtor.
Definition Field.h:423
Field< Data_T > class_type
Definition Field.h:407
const_iterator cbegin(const Box3i &subset) const
Const iterator to first element of specific subset.
Definition Field.h:580
const_iterator cend() const
Const iterator pointing one element past the last valid one.
Definition Field.h:591
const_iterator cbegin() const
Const iterator to first element. "cbegin" matches the tr1 c++ standard.
Definition Field.h:569
FieldRes base
Convenience typedef for referring to base class.
Definition Field.h:467
Data_T value_type
Allows us to reference the template class.
Definition Field.h:398
std::vector< Ptr > Vec
This is a convenience typedef for the list that Field3DInputFile::readScalarLayers() and Field3DInput...
Definition Field.h:403
static const char * staticClassName()
Definition Field.h:410
Represents the mapping of a field by a matrix transform.
WritableField< Data_T > base
Definition Field.h:894
void setSize(const V3i &size)
Resizes the object.
Definition Field.h:913
void matchDefinition(FieldRes::Ptr fieldToMatch)
Sets up this field so that resolution and mapping matches the other.
Definition Field.h:1003
ResizableField< Data_T > class_type
Definition Field.h:844
DEFINE_FIELD_RTTI_ABSTRACT_CLASS
Definition Field.h:845
static TemplatedFieldType< ResizableField< Data_T > > ms_classType
Definition Field.h:890
static const char * staticClassType()
Definition Field.h:852
virtual void sizeChanged()
Subclasses should re-implement this if they need to perform memory allocations, etc....
Definition Field.h:901
boost::intrusive_ptr< ResizableField > Ptr
Definition Field.h:840
void copyFrom(typename Field< Data_T >::Ptr other)
Copies the data from another Field, also resizes.
Definition Field.h:967
static const char * staticClassName()
Definition Field.h:847
Box3i m_window
Window to traverse.
Definition Field.h:769
iterator(WritableField< Data_T > &field, const Box3i &window, const V3i &currentPos)
Definition Field.h:717
int x
Current position.
Definition Field.h:762
WritableField< Data_T > & m_field
Reference to field being iterated over.
Definition Field.h:771
WritableField< Data_T > class_type
Definition Field.h:634
boost::intrusive_ptr< WritableField > Ptr
Definition Field.h:630
static const char * staticClassType()
Definition Field.h:642
iterator end()
Iterator pointing one element past the last valid one.
Definition Field.h:802
Field< Data_T > base
Definition Field.h:690
virtual Data_T & lvalue(int i, int j, int k)=0
Write access to a voxel. The coordinates are global coordinates.
DEFINE_FIELD_RTTI_ABSTRACT_CLASS
Definition Field.h:635
static TemplatedFieldType< WritableField< Data_T > > ms_classType
Definition Field.h:686
static const char * staticClassName()
Definition Field.h:637
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
iterator begin()
Iterator to first element.
Definition Field.h:779
void advance(Iter_T &iter, int num)
Definition Field.h:1161
Namespace for Exception objects.
Definition Exception.h:57
@ SevWarning
Definition Log.h:68
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
#define FIELD3D_API
Definition ns.h:77
#define FIELD3D_NAMESPACE_HEADER_CLOSE
Definition ns.h:58
static std::string name()
Definition Traits.h:267
Used to return a string for the name of a templated field.
Definition Traits.h:283