Points&Forces (survey)
Software tools facilitating the task of surveying architecture
orient.cxx
Go to the documentation of this file.
1 /*
2  Copyright 2002-2019 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 #include <iostream>
17 #include <iomanip>
18 #include <string>
19 #include <fstream>
20 #include <sstream>
21 
22 #include "a_point.h"
23 #include "a_pos.h"
24 #include "a_mat_c.h"
25 
26 #include "SimpleOpt.h"
27 
29 
30 CSimpleOpt::SOption g_rgOptions[] = {
31  { OPT_DIR,_T("-x"), SO_NONE },
32  { OPT_DIR,_T("-y"), SO_NONE },
33  { OPT_PREC,_T("-p"), SO_REQ_SEP },
34  { OPT_PREC,_T("--precision"), SO_REQ_SEP },
35  { OPT_ORI,_T("-ox"), SO_REQ_SEP },
36  { OPT_ORI,_T("-oy"), SO_REQ_SEP },
37  { OPT_ORI,_T("-oz"), SO_REQ_SEP },
38  { OPT_REF,_T("-px"), SO_REQ_SEP },
39  { OPT_REF,_T("-py"), SO_REQ_SEP },
40  { OPT_REF,_T("-pz"), SO_REQ_SEP },
41  { OPT_MATRIX,_T("-m"), SO_REQ_SEP },
42  { OPT_MATRIX,_T("--matrix"), SO_REQ_SEP },
43  { OPT_HELP,_T("-?"), SO_NONE },
44  { OPT_HELP,_T("-h"), SO_NONE },
45  { OPT_HELP,_T("--help"), SO_NONE },
46  SO_END_OF_OPTIONS
47 };
48 
49 const double pi = 3.14159265358979;
50 //---------------------------------------------------------------------------
51 int error(int val)
52 {
53 #include "orient.help"
54  return val;
55 }
56 //---------------------------------------------------------------------------
57 int main( int argc, char *argv[] )
58 {
59  std::string code("x");
60  int pre = 6;
61  a_point ori(0.,0.,0.);
62  a_point p(1.,0.,0.);
63  a_pos mat;
64  bool with_matrix = false;
65  CSimpleOpt args(argc, argv, g_rgOptions);
66  while (args.Next())
67  {
68  if (args.LastError() == SO_SUCCESS)
69  {
70  if (args.OptionId() == OPT_HELP)
71  return error(0);
72  else if (args.OptionId() == OPT_DIR)
73  {
74  std::string code0(args.OptionText());
75  code = code0.substr(1,1);
76  }
77  else if (args.OptionId() == OPT_ORI)
78  {
79  std::ostringstream o;
80  o << args.OptionArg();
81  std::istringstream in(o.str().c_str());
82  double val;
83  in >> val;
84  std::string dir(args.OptionText());
85  if (dir == "-ox") ori.x(val);
86  else if (dir == "-oy") ori.y(val);
87  else if (dir == "-oz") ori.z(val);
88  }
89  else if (args.OptionId() == OPT_REF)
90  {
91  std::ostringstream o;
92  o << args.OptionArg();
93  std::istringstream in(o.str().c_str());
94  double val;
95  in >> val;
96  std::string dir(args.OptionText());
97  if (dir == "-px") p.x(val);
98  else if (dir == "-py") p.y(val);
99  else if (dir == "-pz") p.z(val);
100  }
101  else if (args.OptionId() == OPT_PREC)
102  {
103  std::ostringstream o;
104  o << args.OptionArg();
105  std::istringstream in(o.str().c_str());
106  int prec;
107  in >> prec;
108  if ((prec >= 0)&&(prec < 14))
109  pre = prec;
110  }
111  else if (args.OptionId() == OPT_MATRIX)
112  {
113  std::ifstream in(args.OptionArg());
114  if (!in)
115  {
116  std::cerr << "cannot open file: '" << args.OptionArg() << "'" << std::endl;
117  return error(-2);
118  }
119  else
120  {
121  try { in >> mat; }
122  catch (...)
123  {
124  std::cerr << "The matrix should be 4x4! Take a look to the manual page." << std::endl << std::endl;
125  return error(-3);
126  }
127  with_matrix = true;
128  }
129  } else
130  return error(-1);
131  // handle option, using OptionId(), OptionText() and OptionArg()
132  } else {
133  std::cerr << "Invalid argument: " << args.OptionText() << std::endl;
134  return error(args.LastError());
135  // handle error, one of: SO_OPT_INVALID, SO_OPT_MULTIPLE,
136  // SO_ARG_INVALID, SO_ARG_INVALID_TYPE, SO_ARG_MISSING
137  }
138  }
139  a_point nx,ny;
140  a_point nz(0,0,1);
141  a_point nref = p-ori;
142  nref.z(0);
143  nref.normalise();
144  if (code=="x")
145  {
146  nx = nref;
147  nx.y(-nref.y());
148  ny = cross(nz,nx).normalise();
149  }
150  else
151  {
152  ny = nref;
153  ny.x(-nref.x());
154  nx = cross(ny,nz).normalise();
155  }
156  int npts;
157  std::cin >> npts;
158  std::cout << npts << std::endl;
159  std::cout << std::fixed << std::setprecision(pre);
160  for (unsigned int k = 0; k < npts; k++)
161  {
162  if (with_matrix)
163  {
164  a_mat_c x0(4);
165  a_mat_c x(4);
166  x0(3) = 1.;
167  x(3) = 1.;
168  std::cin >> x0(0) >> x0(1) >> x0(2);
169  x = mat*x0;
170  if (x(3)!=0.)
171  std::cout << x(0)/x(3) << " " << x(1)/x(3) << " " << x(2)/x(3) << std::endl;
172  else
173  {
174  std::cerr << "point projected to infinity! Inspect output before use!!" << std::endl;
175  std::cout << x(0) << " " << x(1) << " " << x(2) << " " << x(3) << " inf" << std::endl;
176  }
177  }
178  else
179  {
180  a_point x;
181  std::cin >> x;
182  x.rotate(nx,ny,nz).translate(ori.x(),ori.y(),ori.z());
183  std::cout << x << std::endl;
184  }
185  }
186 #include "copy.h"
187  return 0;
188 }
int pre
Definition: bspline.cxx:49
double x() const
Definition: e_point.h:37
e_point & normalise()
Definition: e_point.cxx:67
e_point cross(e_point &a, e_point &b)
Definition: e_point.cxx:105
int main(int argc, char *argv[])
Definition: orient.cxx:57
int error(int val)
Definition: orient.cxx:51
CSimpleOpt::SOption g_rgOptions[]
Definition: orient.cxx:30
const double pi
Definition: orient.cxx:49
@ OPT_DIR
Definition: orient.cxx:28
@ OPT_REF
Definition: orient.cxx:28
@ OPT_HELP
Definition: orient.cxx:28
@ OPT_ORI
Definition: orient.cxx:28
@ OPT_MATRIX
Definition: orient.cxx:28
@ OPT_PREC
Definition: orient.cxx:28
std::istringstream in
Definition: ply2tri.cxx:32
a_point dir(int i, int l)
Definition: rib0.cxx:60