1 #ifndef vimt3d_image_3d_of_hxx_
2 #define vimt3d_image_3d_of_hxx_
3 //:
4 // \file
5 // \brief Container for vil_image_view<T> + transform
6 // \author Tim Cootes
7 
8 #include <iostream>
9 #include <string>
10 #include "vimt3d_image_3d_of.h"
11 
12 #ifdef _MSC_VER
13 #  include <vcl_msvc_warnings.h>
14 #endif
15 #include <cassert>
16 #include <vsl/vsl_indent.h>
17 #include <vsl/vsl_vector_io.h>
18 #include <vil3d/vil3d_print.h>
19 #include <vil3d/io/vil3d_io_image_view.h>
20 
21 
22 //=======================================================================
23 //: Perform deep copy of src into this image
24 template<class T>
deep_copy(const vimt3d_image_3d_of & src)25 void vimt3d_image_3d_of<T>::deep_copy(const vimt3d_image_3d_of& src)
26 {
27   world2im_ = src.world2im_;
28   image_.deep_copy(src.image_);
29 }
30 
31 
32 //=======================================================================
33 //: Shallow equality tester.
34 //  The parameter must be identical type to this.
35 template<class T>
equals(const vimt_image & im) const36 bool vimt3d_image_3d_of<T>::equals(const vimt_image &im) const
37 {
38   assert(dynamic_cast<const vimt3d_image_3d_of<T> *>(&im));
39   return operator==(static_cast<const vimt3d_image_3d_of<T> &>(im));
40 }
41 
42 
43 //=======================================================================
44 //: Define valid data region (including transform).
45 //  Resizes and sets the transformation so that
46 //  worldToIm(x,y) is valid for all points in range
47 template<class T>
set_valid_region(int i0,unsigned ni,int j0,unsigned nj,int k0,unsigned nk)48 void vimt3d_image_3d_of<T>::set_valid_region(int i0, unsigned ni,
49                                              int j0, unsigned nj,
50                                              int k0, unsigned nk)
51 {
52   image_.set_size(ni,nj,nk);
53   world2im_.set_translation(-i0,-j0,-k0);
54 }
55 
56 
57 //=======================================================================
58 template<class T>
is_class(std::string const & s) const59 bool vimt3d_image_3d_of<T>::is_class(std::string const& s) const
60 {
61   return s==vimt3d_image_3d_of<T>::is_a() || vimt3d_image_3d::is_class(s);
62 }
63 
64 
65 //=======================================================================
66 template<class T>
version_no() const67 short vimt3d_image_3d_of<T>::version_no() const
68 {
69   return 1;
70 }
71 
72 
73 //=======================================================================
74 template<class T>
deep_clone() const75 vimt_image* vimt3d_image_3d_of<T>::deep_clone() const
76 {
77   vimt3d_image_3d_of<T>* new_im = new vimt3d_image_3d_of<T>();
78   new_im->deep_copy(*this);
79   return new_im;
80 }
81 
82 
83 //=======================================================================
84 template<class T>
print_summary(std::ostream & os) const85 void vimt3d_image_3d_of<T>::print_summary(std::ostream& os) const
86 {
87   os << vsl_indent() << "Transform: " << world2im_
88     << vsl_indent() << " Image: " << image_ << '\n';
89 }
90 
91 
92 //=======================================================================
93 //: print all data to os
94 template<class T>
print_all(std::ostream & os) const95 void vimt3d_image_3d_of<T>::print_all(std::ostream& os) const
96 {
97   os<<vsl_indent();
98   vil3d_print_all(os,image_);
99   os<<'\n'<<vsl_indent() << "Transform: "<<world2im_<<'\n';
100 }
101 
102 
103 //=======================================================================
104 template<class T>
b_write(vsl_b_ostream & bfs) const105 void vimt3d_image_3d_of<T>::b_write(vsl_b_ostream& bfs) const
106 {
107   vsl_b_write(bfs,version_no());
108   vsl_b_write(bfs,image_);
109   vsl_b_write(bfs,world2im_);
110 }
111 
112 
113 //=======================================================================
114 template<class T>
b_read(vsl_b_istream & bfs)115 void vimt3d_image_3d_of<T>::b_read(vsl_b_istream& bfs)
116 {
117   if (!bfs) return;
118 
119   short version;
120   vsl_b_read(bfs,version);
121   switch (version)
122   {
123   case (1):
124     vsl_b_read(bfs,image_);
125     vsl_b_read(bfs,world2im_);
126     break;
127   default:
128     std::cerr << "I/O ERROR: vimt3d_image_3d_of<T>::b_read(vsl_b_istream&)\n"
129              << "           Unknown version number "<< version << '\n';
130     bfs.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
131     return;
132   }
133 }
134 
135 
136 //=======================================================================
137 //: True if transforms, etc. are equal, and they share same image data.
138 //  This does not do a deep equality on image data. If the images point
139 //  to different image data objects that contain identical images, then
140 //  the result will still be false.
141 template<class T>
operator ==(const vimt3d_image_3d_of<T> & other) const142 bool vimt3d_image_3d_of<T>::operator==(const vimt3d_image_3d_of<T> &other) const
143 {
144   return image_ == other.image_ &&
145       world2im_ == other.world2im_;
146 }
147 
148 
149 //=======================================================================
150 //: True if the transforms and the actual image data are identical.
151 // The image pointers need not be identical,
152 // provided that the underlying image data are the same.
153 // \relatesalso vimt3d_image_3d_of<T>
154 // \relatesalso vil3d_image_view
155 template<class T>
vimt3d_image_3d_deep_equality(const vimt3d_image_3d_of<T> & lhs,const vimt3d_image_3d_of<T> & rhs)156 bool vimt3d_image_3d_deep_equality(const vimt3d_image_3d_of<T>& lhs,
157                                    const vimt3d_image_3d_of<T>& rhs)
158 {
159   // First check the transforms are the same
160   if (!(lhs.world2im() == rhs.world2im()))
161     return false;
162 
163   // Now check that the underlying image data are identical
164   return vil3d_image_view_deep_equality(lhs.image(), rhs.image());
165 }
166 
167 
168 //=======================================================================
169 
170 
171 #define VIMT3D_IMAGE_3D_OF_INSTANTIATE(T) \
172 template <> std::string vimt3d_image_3d_of<T >::is_a() const \
173 { return std::string("vimt3d_image_3d_of<" #T ">"); } \
174 template class vimt3d_image_3d_of<T >; \
175 template bool vimt3d_image_3d_deep_equality(const vimt3d_image_3d_of<T >& lhs, \
176                                             const vimt3d_image_3d_of<T >& rhs)
177 
178 #endif // vimt3d_image_3d_of_hxx_
179