Points&Forces (survey)
Software tools facilitating the task of surveying architecture
bspline.cxx
Go to the documentation of this file.
1 /*
2  Copyright 2002-2015 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 "vtkRenderer.h"
17 #include "vtkRenderWindow.h"
18 #include "vtkInteractorStyleTrackballCamera.h"
19 #include "vtkRenderWindowInteractor.h"
20 #include "vtkPolyData.h"
21 #include "vtkPoints.h"
22 #include "vtkProperty.h"
23 #include "vtkCellArray.h"
24 #include "vtkKochanekSpline.h"
25 #include "vtkPolyDataMapper.h"
26 #include "vtkActor.h"
27 
28 #include <sstream>
29 #include <iomanip>
30 
31 #include "SimpleOpt.h"
32 
34 
35 CSimpleOpt::SOption g_rgOptions[] = {
36  { OPT_PREC,_T("-p"), SO_REQ_SEP },
37  { OPT_PREC,_T("--precision"), SO_REQ_SEP },
38  { OPT_VIEW,_T("-v"), SO_NONE },
39  { OPT_VIEW,_T("--view"), SO_NONE },
40  { OPT_CLOSED,_T("-c"), SO_NONE },
41  { OPT_CLOSED,_T("--closed"), SO_NONE },
42  { OPT_HELP,_T("-?"), SO_NONE },
43  { OPT_HELP,_T("-h"), SO_NONE },
44  { OPT_HELP,_T("--help"), SO_NONE },
45  SO_END_OF_OPTIONS
46 };
47 
48 //---------------------------------------------------------------------------
49 int pre = 6;
50 bool closed = false;
51 bool view = false;
53 //---------------------------------------------------------------------------
54 int error(int val)
55 {
56  std::cerr << "bspline:" << std::endl;
57  std::cerr << " create a regular spaced list of point by Kochanek-spline interpolation" << std::endl;
58  std::cerr << "syntax: spline number_of_points [-p n_decimals|--precision n_decimals] [-v|--view] [-c|--closed] < input_file > output_file" << std::endl;
59  std::cerr << " -p n_decimals: number of decimals (default 6)" << std::endl;
60  std::cerr << " -v: view results" << std::endl;
61  std::cerr << " -c: curve is closed" << std::endl;
62  std::cerr << "author: Pierre Smars, 2001-8" << std::endl;
63  std::cerr << "version 2011-04-16" << std::endl;
64  return val;
65 }
66 //---------------------------------------------------------------------------
67 void draw(vtkPoints * pts_in, vtkPoints * pts_out)
68 {
69  vtkRenderer * ren = vtkRenderer::New();
70  vtkRenderWindow * renWin = vtkRenderWindow::New();
71  renWin->AddRenderer(ren);
72  ren->Delete();
73  vtkInteractorStyleTrackballCamera * style = vtkInteractorStyleTrackballCamera::New();
74  vtkRenderWindowInteractor * iren = vtkRenderWindowInteractor::New();
75  iren->SetRenderWindow(renWin);
76  renWin->Delete();
77  iren->SetInteractorStyle(style);
78  vtkCellArray * vertices = vtkCellArray::New();
79  for (vtkIdType i = 0; i < n_control; i++)
80  vertices->InsertNextCell(1,&i);
81  vtkPolyData * data = vtkPolyData::New();
82  data->SetPoints(pts_in);
83  pts_in->Delete();
84  data->SetVerts(vertices);
85  vertices->Delete();
86  vtkPolyDataMapper * map = vtkPolyDataMapper::New();
87  map->SetInputData(data);
88  data->Delete();
89  vtkActor * actor = vtkActor::New();
90  actor->SetMapper(map);
91  map->Delete();
92  actor->GetProperty()->SetPointSize(3);
93  vtkCellArray * vertices2 = vtkCellArray::New();
94  for (vtkIdType i = 0; i < n_output; i++)
95  vertices2->InsertNextCell(1,&i);
96  vtkPolyData * data2 = vtkPolyData::New();
97  data2->SetPoints(pts_out);
98  pts_out->Delete();
99  data2->SetVerts(vertices2);
100  vertices2->Delete();
101  vtkPolyDataMapper * map2 = vtkPolyDataMapper::New();
102  map2->SetInputData(data2);
103  data2->Delete();
104  vtkActor * actor2 = vtkActor::New();
105  actor2->SetMapper(map2);
106  map2->Delete();
107  actor2->GetProperty()->SetColor(1,1,0);
108  ren->AddActor(actor);
109  actor->Delete();
110  ren->AddActor(actor2);
111  actor2->Delete();
112  renWin->SetSize(640,480);
113  renWin->Render();
114  iren->Start();
115 }
116 //---------------------------------------------------------------------------
117 int main(int argc, char ** argv)
118 {
119  double tension = 0;
120  double bias = 0;
121  double continuity = 0;
122  CSimpleOpt args(argc, argv, g_rgOptions);
123  while (args.Next())
124  {
125  if (args.LastError() == SO_SUCCESS)
126  {
127  if (args.OptionId() == OPT_HELP)
128  return error(0);
129  else if (args.OptionId() == OPT_VIEW)
130  view = true;
131  else if (args.OptionId() == OPT_CLOSED)
132  closed = true;
133  else if (args.OptionId() == OPT_PREC)
134  {
135  std::ostringstream o;
136  o << args.OptionArg();
137  std::istringstream in(o.str().c_str());
138  int precision;
139  in >> precision;
140  if ((precision > 0)&&(precision < 12))
141  pre = precision;
142  } else
143  return error(-1);
144  // handle option, using OptionId(), OptionText() and OptionArg()
145  } else {
146  std::cerr << "Invalid argument: " << args.OptionText() << std::endl;
147  return error(args.LastError());
148  // handle error, one of: SO_OPT_INVALID, SO_OPT_MULTIPLE,
149  // SO_ARG_INVALID, SO_ARG_INVALID_TYPE, SO_ARG_MISSING
150  }
151  }
152  if (args.FileCount() != 1) return error(-2);
153 
154  std::ostringstream o(args.File(0));
155  std::istringstream in(o.str().c_str());
156  in >> n_output;
157  n_output--;
158 
159  vtkKochanekSpline * s[3];
160  for (int i = 0; i < 3; i++)
161  {
162  s[i] = vtkKochanekSpline::New();
163  s[i]->SetDefaultTension(tension);
164  s[i]->SetDefaultBias(bias);
165  s[i]->SetDefaultContinuity(continuity);
166  }
167  vtkPoints * pts_in, * pts_out;
168  if (view)
169  {
170  pts_in = vtkPoints::New();
171  pts_out = vtkPoints::New();
172  }
173  //input
174  std::cin >> n_control; // number of control points
175  double dist = 0;
176  double x[3], x0[3], x00[3];
177  for (int i=0; i < n_control; i++)
178  {
179  for (int j = 0; j < 3; j++) std::cin >> x[j];
180  if (i==0)
181  for (int j = 0; j < 3; j++) x00[j] = x0[j] = x[j];
182  else
183  dist += sqrt((x[0]-x0[0])*(x[0]-x0[0])+(x[1]-x0[1])*(x[1]-x0[1])+(x[2]-x0[2])*(x[2]-x0[2]));
184  if (dist==0)
185  for (int j = 0; j < 3; j++) s[j]->AddPoint(0,x[j]);
186  else
187  for (int j = 0; j < 3; j++) s[j]->AddPoint(dist,x[j]);
188  if (view)
189  pts_in->InsertNextPoint(x);
190  for (int j = 0; j < 3; j++) x0[j] = x[j];
191  }
192  if (closed)
193  {
194  if ((x00[1]!=x[1])||(x00[1]!=x[1])||(x00[1]!=x[1]))
195  {
196  dist += sqrt((x00[0]-x0[0])*(x00[0]-x0[0])+(x00[1]-x0[1])*(x00[1]-x0[1])+(x00[2]-x0[2])*(x00[2]-x0[2]));
197  for (int j = 0; j < 3; j++) s[j]->AddPoint(dist,x00[j]);
198  }
199  for (int j = 0; j < 3; j++) s[j]->ClosedOn();
200  }
201 
202  //output
203  std::cout << n_output+1 << std::endl;
204  std::cout << std::fixed << std::setprecision(pre);
205  double offset = dist/n_output;
206  double t = -offset;
207  for (int i=0; i <= n_output; i++)
208  {
209  t += offset;
210  if (view)
211  {
212  double x[3];
213  for (int j = 0; j < 3; j++)
214  x[j] = s[j]->Evaluate(t);
215  std::cout << x[0] << "\t" << x[1] << "\t" << x[2] << "\t" << std::endl;
216  pts_out->InsertNextPoint(x);
217  }
218  else
219  std::cout << s[0]->Evaluate(t) << "\t" << s[1]->Evaluate(t) << "\t" << s[2]->Evaluate(t) << "\t" << std::endl;
220  }
221 
222  if (view)
223  draw(pts_in,pts_out);
224  return 0;
225 }
int error(int val)
Definition: bspline.cxx:54
CSimpleOpt::SOption g_rgOptions[]
Definition: bspline.cxx:35
int main(int argc, char **argv)
Definition: bspline.cxx:117
bool view
Definition: bspline.cxx:51
bool closed
Definition: bspline.cxx:50
int n_control
Definition: bspline.cxx:52
@ OPT_VIEW
Definition: bspline.cxx:33
@ OPT_CLOSED
Definition: bspline.cxx:33
@ OPT_HELP
Definition: bspline.cxx:33
@ OPT_PREC
Definition: bspline.cxx:33
int pre
Definition: bspline.cxx:49
void draw(vtkPoints *pts_in, vtkPoints *pts_out)
Definition: bspline.cxx:67
int n_output
Definition: bspline.cxx:52
std::istringstream in
Definition: ply2tri.cxx:32
vtkRenderer * ren
Definition: view_li.cxx:81