Points&Forces (survey)
Software tools facilitating the task of surveying architecture
a_image.cxx
Go to the documentation of this file.
1 /*
2  Copyright 2002-2016 Pierre SMARS (smars@yuntech.edu.tw)
3  This program is free software: you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation, either version 2 of the License, or
6  (at your option) any later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16 
17 #include <sstream>
18 #include "vtkImageData.h"
19 #include "vtkImageReader2.h"
20 #include "vtkTIFFReader.h"
21 #include "vtkJPEGReader.h"
22 #include "vtkPNGReader.h"
23 
24 #include "a_image.h"
25 
26 //--------------Stroustroup, p.591
27 //comparisons without case
28 int cmp_nocase(const std::string& s, const std::string& s2)
29 {
30  std::string::const_iterator p = s.begin();
31  std::string::const_iterator p2 = s2.begin();
32  while (p != s.end() && p2 != s2.end())
33  {
34  if (toupper(*p) != toupper(*p2))
35  return (toupper(*p) < toupper(*p2)) ? -1 : 1;
36  ++p;
37  ++p2;
38  }
39  return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1; //size is unsigned
40 }
41 
42 //---------------------------------------------------------------------------
43 a_image::a_image(const std::string & image_file, const std::string & matrix_file)
44 {
45  name_ = "noname";
46  texture_ = 0;
47  p_ = 0;
48  this->image(image_file);
49  this->pmatrix(matrix_file);
50 }
51 //---------------------------------------------------------------------------
53 {
54  texture_->Delete();
55 }
56 //---------------------------------------------------------------------------
57 const std::string a_image::help()
58 {
59  std::ostringstream o;
60  o << "*******" << std::endl;
61  o << "a_image" << std::endl;
62  o << "*******" << std::endl;
63  o << "An object of this class is meant to store an image and its projection parameters" << std::endl;
64  o << "and to be use as a texture for a_element (a_trianglecloud in particular)" << std::endl;
65  o << "Commands:" << std::endl;
66  o << "--------" << std::endl;
67  o << "type: returns the string 'a_image'" << std::endl;
68  o << "image 'filename': load an image" << std::endl;
69  o << "pmatrix 'filename': load the P project matrix" << std::endl;
70  o << " see p_compute(1) to compute the matrix of a given image" << std::endl;
71  o << "name: set/get the name of the object" << std::endl;
72  return o.str();
73 }
74 //---------------------------------------------------------------------------
75 void a_image::file_exist(const std::string& file) const
76 {
77  std::ifstream in(file.c_str());
78  if (!in)
79  throw file_error();
80  in.close();
81 }
82 //---------------------------------------------------------------------------
83 void a_image::image(const std::string& image_file)
84 {
85  this->file_exist(image_file);
86  if (texture_ !=0)
87  texture_->Delete();
88  texture_ = vtkTexture::New();
89  texture_->InterpolateOn();
90  std::string ext = image_file.substr(image_file.find_first_of('.')+1,image_file.size());
91  name_ = image_file;
92  vtkImageReader2 * reader;
93  if ((cmp_nocase(ext,"tif")==0)||(cmp_nocase(ext,"tiff")==0))
94  reader = vtkTIFFReader::New();
95  else if ((cmp_nocase(ext,"jpg")==0)||(cmp_nocase(ext,"jpeg")==0))
96  reader = vtkJPEGReader::New();
97  else if (cmp_nocase(ext,"png")==0)
98  reader = vtkPNGReader::New();
99  reader->SetFileName(const_cast<char *>(image_file.c_str()));
100  texture_->SetInputConnection(reader->GetOutputPort());
101  reader->Update();
102  bb_ = reader->GetOutput()->GetDimensions();
103 }
104 //---------------------------------------------------------------------------
105 void a_image::pmatrix(const std::string& matrix_file)
106 {
107  this->file_exist(matrix_file);
108  if (p_ !=0)
109  delete p_;
110  p_ = new a_pmat;
111  std::ifstream ff(matrix_file.c_str());
112  ff >> *p_;
113  ff.close();
114 }
115 //---------------------------------------------------------------------------
116 void a_image::texture_coordinates(const double * x, float * tcoord) const
117 {
118  a_3dh p3d(x[0],x[1],x[2]);
119  a_2dh p2d = p_->project(p3d);
120  tcoord[0] = p2d.x();
121  tcoord[1] = p2d.y();
122  if ((tcoord[0]<0.)||(tcoord[0]>bb_[0])||(tcoord[1]<0.)||(tcoord[1]>bb_[1]))
123  {
124  tcoord[0] = 0.;
125  tcoord[1] = 0.;
126  }
127  tcoord[0] /= bb_[0];
128  tcoord[1] /= bb_[1];
129 }
130 //---------------------------------------------------------------------------
131 void a_image::gettcoords(vtkPoints * points, vtkFloatArray * tcoords) const
132 {
133  int n_pts = points->GetNumberOfPoints();
134  for (int k = 0; k < n_pts; k++)
135  {
136  double x[3];
137  points->GetPoint(k,x);
138  float tc[2];
139  texture_coordinates(x,tc);
140  tcoords->InsertNextTuple(tc);
141  }
142 }
143 //---------------------------------------------------------------------------
144 void d_v(a_image * la) {delete la;}
145 //---------------------------------------------------------------------------
146 void delete_vector(std::vector<a_image *> & v)
147 {
148  for (int i = 0; i < v.size(); i++)
149  delete v[i];
150  // std::for_each(v.begin(),v.end(),d_v);
151  v.clear();
152 }
void delete_vector(std::vector< a_image * > &v)
Definition: a_image.cxx:146
void d_v(a_image *la)
Definition: a_image.cxx:144
int cmp_nocase(const std::string &s, const std::string &s2)
Definition: a_image.cxx:28
a_image(const std::string &image_file, const std::string &matrix_file)
Definition: a_image.cxx:43
void file_exist(const std::string &name) const
Definition: a_image.cxx:75
void image(const std::string &file_name)
Definition: a_image.cxx:83
std::string name_
Definition: a_image.h:61
void texture_coordinates(const double *x, float *tcoord) const
Definition: a_image.cxx:116
void pmatrix(const std::string &file_name)
Definition: a_image.cxx:105
static const std::string help()
Definition: a_image.cxx:57
int * bb_
Definition: a_image.h:63
void gettcoords(vtkPoints *, vtkFloatArray *tcoords) const
Definition: a_image.cxx:131
a_pmat * p_
Definition: a_image.h:64
vtkTexture * texture_
Definition: a_image.h:62
~a_image()
Definition: a_image.cxx:52
double v(const uint32_t step, const uint32_t n)
Definition: generate.cxx:42
std::istringstream in
Definition: ply2tri.cxx:32
std::string image_file("")