16 #include "vtkAppendPolyData.h"
17 #include "vtkBoxWidget.h"
18 #include "vtkCamera.h"
19 #include "vtkCellArray.h"
20 #include "vtkClipPolyData.h"
21 #include "vtkCommand.h"
22 #include "vtkDataSetMapper.h"
23 #include "vtkInteractorStyleTrackballCamera.h"
24 #include "vtkInteractorStyleTrackballActor.h"
25 #include "vtkInteractorStyleSwitch.h"
26 #include "vtkIterativeClosestPointTransform.h"
27 #include "vtkJPEGWriter.h"
28 #include "vtkPNGWriter.h"
30 #include "vtkLightCollection.h"
31 #include "vtkObject.h"
33 #include "vtkPlanes.h"
34 #include "vtkPlaneWidget.h"
35 #include "vtkPoints.h"
36 #include "vtkPolyDataMapper.h"
37 #include "vtkPolyDataWriter.h"
38 #include "vtkProperty.h"
39 #include "vtkRenderLargeImage.h"
40 #include "vtkRenderer.h"
41 #include "vtkRenderWindow.h"
42 #include "vtkRenderWindowInteractor.h"
43 #include "vtkRIBExporter.h"
44 #include "vtkTIFFWriter.h"
45 #include "vtkTransform.h"
46 #include "vtkVRMLExporter.h"
47 #include "vtkWindowToImageFilter.h"
49 #include "vtkCellPicker.h"
50 #include "vtkPropPicker.h"
51 #include "a_dxf_group.h"
52 #include "a_dxf_entity.h"
53 #include "a_dxf_point.h"
54 #include "a_dxf_line.h"
55 #include "a_dxf_polyline.h"
62 #define DE(x) std::cerr << #x << ": " << x << std::endl;
64 const double d2r=3.14159265358979/180.;
65 const double r2d=180./3.14159265358979;
71 o <<
"**********" << std::endl;
72 o <<
"a_canvas:" << std::endl;
73 o <<
"**********" << std::endl;
74 o <<
"This is a 3D window class" << std::endl;
75 o <<
"Commands:" << std::endl;
76 o <<
"--------" << std::endl;
77 o <<
"a_canvas C: create a window named C" << std::endl;
78 o <<
"windows can display various types of objects:" << std::endl;
79 o <<
"a_pointcloud, a_linecloud, a_trianglecloud" << std::endl;
80 o <<
"(look at the corresponding help files for details)" << std::endl;
81 o <<
"Object management:" << std::endl;
82 o <<
"................" << std::endl;
83 o <<
"an object receives a handle when it is created" << std::endl;
84 o <<
" ex: if 'a_pointcloud pts' is used to create an object" << std::endl;
85 o <<
" 'pts' is the object handle" << std::endl;
86 o <<
" the handle is used to access the functions" << std::endl;
87 o <<
" 'pts name somename' is used to assign the name 'somename' to the object" << std::endl;
88 o <<
" name and handle should be different!!!" << std::endl;
89 o <<
"object add HANDLE: add/link a new object (created before)" << std::endl;
90 o <<
"object set NAME: set current object" << std::endl;
91 o <<
"object unlink NAME: unlink object" << std::endl;
92 o <<
"object list: return the name list of the existing objects" << std::endl;
93 o <<
"(alternatively: 'object ls')" << std::endl;
94 o <<
"object name: return the name of the current object" << std::endl;
95 o <<
"objecth: return the handle of the current object" << std::endl;
96 o <<
"object exist NAME: check whether object exists (returns 0 or 1)" << std::endl;
97 o <<
"Navigating:" << std::endl;
98 o <<
".........." << std::endl;
99 o <<
"interact: start mouse control (click 'e' to exit)" << std::endl;
100 o <<
"redraw: refresh screen" << std::endl;
101 o <<
"fit: fit data to screen" << std::endl;
102 o <<
"straighten: put the vertical vertical" << std::endl;
103 o <<
"background R G B: change background color" << std::endl;
104 o <<
"perspective on: perspective view" << std::endl;
105 o <<
"perspective off: parallel view" << std::endl;
106 o <<
"cursor on: show cursor" << std::endl;
107 o <<
"cursor off: hide cursor" << std::endl;
108 o <<
"x: look in the direction of positive x" << std::endl;
109 o <<
"X: look in the direction of negative X" << std::endl;
110 o <<
"y: look in the direction of positive y" << std::endl;
111 o <<
"Y: look in the direction of negative Y" << std::endl;
112 o <<
"z: look in the direction of positive z" << std::endl;
113 o <<
"Z: look in the direction of negative Z" << std::endl;
114 o <<
"azimuth V: set azimuth angle" << std::endl;
115 o <<
"azimuth: get azimuth angle" << std::endl;
116 o <<
"(similar functions for elevation, roll, pich, yaw, distance)" << std::endl;
117 o <<
"up: set Z up" << std::endl;
118 o <<
"camerapoint X Y Z: set view point" << std::endl;
119 o <<
"camerapoint: get view point" << std::endl;
120 o <<
"focalpoint X Y Z: set focal point" << std::endl;
121 o <<
"focalpoint: get focal point" << std::endl;
122 o <<
"light sun: fixed light" << std::endl;
123 o <<
"light spot: light follow camera" << std::endl;
124 o <<
"Input/Output:" << std::endl;
125 o <<
"............" << std::endl;
126 o <<
"save CODE FILE" << std::endl;
127 o <<
"(code: jpeg, png, vec, vrml, vtk)" << std::endl;
128 o <<
"magnification V: set factor of magnification for the production of images" << std::endl;
129 o <<
"scale V: set scale of parallel views" << std::endl;
130 o <<
"scale: get scale of parallel views" << std::endl;
131 o <<
"logfile A_TEXT: log is saved to a a_text object" << std::endl;
132 o <<
"outputfile A_TEXT: output is saved to a a_text object" << std::endl;
133 o <<
"A_TEXT should be created before: ex: a_text NAME" << std::endl;
134 o <<
"(not all functions are documented, to be completed...)" << std::endl;
143 virtual void Execute(vtkObject *caller,
unsigned long,
void*)
145 std::cout <<
"." << std::endl;;
146 iren_->CreateTimer(VTKI_TIMER_UPDATE);
149 vtkRenderWindowInteractor * iren_;
156 virtual void Execute(vtkObject *caller,
unsigned long,
void*)
158 vtkTransform *t = vtkTransform::New();
159 vtkBoxWidget *widget =
reinterpret_cast<vtkBoxWidget*
>(caller);
160 widget->GetTransform(t);
161 widget->GetProp3D()->SetUserTransform(t);
172 virtual void Execute(vtkObject *caller,
unsigned long,
void*)
174 vtkPlaneWidget * widget =
reinterpret_cast<vtkPlaneWidget *
>(caller);
175 widget->GetPlane(plane_);
176 actor_->VisibilityOn();
186 ren_ = vtkRenderer::New();
187 renWin_ = vtkRenderWindow::New();
190 renWin_->SetStereoTypeToAnaglyph();
196 style->
name(
"a_canvas");
198 iren_ = vtkRenderWindowInteractor::New();
200 iren_->SetInteractorStyle(style);
211 log_.file(
"a_canvas.log");
213 log_ <<
"#!/usr/bin/env tclsh" << std::endl;
214 log_ <<
"package require a_canvas" << std::endl;
215 log_ <<
"a_canvas window_name" << std::endl;
227 log_ <<
"rename window_name \"\"" << std::endl;
233 log_ <<
"window_name clear" << std::endl;
247 log_ <<
"setlogfile" << std::endl;
253 log_ <<
"setoutputfile" << std::endl;
263 double m(
const char)
const;
273 for (
int i = 0; i < 3; i++) l_[i*2] = 1e30;
274 for (
int i = 0; i < 3; i++) l_[i*2+1] = -1e30;
281 l->
actor()->GetBounds(b);
282 for (
int i = 0; i < 3; i++)
283 if (b[i*2]<l_[i*2]) {l_[i*2] = b[i*2];}
284 for (
int i = 0; i < 3; i++)
285 if (b[i*2+1]>l_[i*2+1]) {l_[i*2+1] = b[i*2+1];}
303 for (
int i = 0; i < 3; i++) d0 += (l_[i*2+1]-l_[i*2])*(l_[i*2+1]-l_[i*2]);
308 return (l_[i*2]+l_[i*2+1])/2;
315 std::string
name = la->name();
326 std::string
name = la->name();
348 for (
int i =0; i <
elements_.size(); i++)
350 for (
int i = 0; i < 3; i++) fo[i] = box.
m(i);
357 log_ <<
"window_name object add $" << la_in.
name() << std::endl;
370 s += la->name() +
" ";
372 s = s.substr(0,s.size()-1);
373 log_ <<
"window_name object ls" << std::endl;
383 log_ <<
"window_name object set " << s << std::endl;
393 std::vector<a_element *>::iterator li = std::find(
elements_.begin(),
elements_.end(),p);
397 log_ <<
"window_name object rm " << s << std::endl;
412 std::vector<a_element *>::iterator li = std::find(
elements_.begin(),
elements_.end(),mesh);
414 if (mesh->
type() ==
"a_trianglecloud")
416 vtkIterativeClosestPointTransform * transform = vtkIterativeClosestPointTransform::New();
417 transform->SetSource(points->
polydata());
418 transform->SetTarget(mesh->
polydata());
419 transform->SetMaximumNumberOfIterations(100);
420 transform->CheckMeanDistanceOn();
421 transform->SetMeanDistanceModeToRMS();
422 transform->SetMaximumMeanDistance(0.01);
430 ren_->LightFollowCameraOn();
432 iren_->LightFollowCameraOn();
434 log_ <<
"window_name light spot" << std::endl;
442 ren_->LightFollowCameraOff();
444 iren_->LightFollowCameraOff();
447 log_ <<
"window_name light sun" << std::endl;
453 ren_->GetActiveCamera()->ParallelProjectionOn();
455 log_ <<
"window_name perspective off" << std::endl;
461 ren_->GetActiveCamera()->ParallelProjectionOff();
463 log_ <<
"window_name perspective on" << std::endl;
471 log_ <<
"window_name magnification " << val << std::endl;
476 double h =
ren_->GetSize()[1];
477 double s =
ren_->GetActiveCamera()->GetParallelScale();
486 double h =
ren_->GetSize()[1];
488 ren_->GetActiveCamera()->SetParallelScale(s);
490 log_ <<
"window_name scale " << val << std::endl;
495 vtkCamera * cam =
ren_->GetActiveCamera();
496 a_point c(cam->GetPosition());
497 a_point f(cam->GetFocalPoint());
507 int n_total =
ren_->GetActors()->GetNumberOfItems();
508 for (
int i=0;i<n_total-n_actors;i++)
510 vtkActor * actor =
ren_->GetActors()->GetLastActor();
511 ren_->RemoveActor(actor);
514 std::cout << std::endl;
520 vtkBoxWidget * boxWidget = vtkBoxWidget::New();
521 boxWidget->SetInteractor(
iren_);
522 boxWidget->SetPlaceFactor(1.25);
524 boxWidget->PlaceWidget();
525 boxWidget->ScalingEnabledOff();
526 boxWidget->OutlineFaceWiresOff();
527 boxWidget->HandlesOff();
529 boxWidget->AddObserver(vtkCommand::InteractionEvent, callback);
532 boxWidget->RemoveObserver(callback);
540 vtkBoxWidget * boxWidget = vtkBoxWidget::New();
541 boxWidget->SetInteractor(
iren_);
542 boxWidget->SetPlaceFactor(1.25);
544 boxWidget->PlaceWidget();
546 boxWidget->AddObserver(vtkCommand::InteractionEvent, callback);
549 boxWidget->RemoveObserver(callback);
557 vtkPlaneWidget * Widget = vtkPlaneWidget::New();
558 Widget->SetInteractor(
iren_);
559 Widget->SetRepresentationToSurface();
560 Widget->SetPlaceFactor(1.25);
562 Widget->PlaceWidget();
563 Widget->GetPlaneProperty()->SetOpacity(.3);
564 Widget->GetSelectedPlaneProperty()->SetColor(0.9,0.9,0.9);
565 vtkPlane * plane = vtkPlane::New();
566 Widget->GetPlane(plane);
571 Widget->AddObserver(vtkCommand::InteractionEvent, callback);
576 Widget->RemoveObserver(callback);
660 return 90.-
ren_->GetActiveCamera()->GetRoll();
663 vtkCamera * cam =
ren_->GetActiveCamera();
664 a_point c(cam->GetPosition());
665 a_point f(cam->GetFocalPoint());
674 vtkCamera * cam =
ren_->GetActiveCamera();
679 cam->SetViewUp(cos(
x*
d2r),sin(
x*
d2r),0.);
684 a_point c(cam->GetPosition());
685 a_point f(cam->GetFocalPoint());
687 double d =
dir.norm();
689 double dh = sqrt(d*d-dz*dz);
690 a_point dir2(dh*cos(
x*
d2r),dh*sin(
x*
d2r),dz);
692 cam->SetPosition(c2.x(),c2.y(),c2.z());
700 vtkCamera * cam =
ren_->GetActiveCamera();
701 a_point c(cam->GetPosition());
702 a_point f(cam->GetFocalPoint());
704 a_point vert(0.,0.,-1.);
705 return angle(vert,
dir)*
r2d-90.;
712 vtkCamera * cam =
ren_->GetActiveCamera();
713 a_point c(cam->GetPosition());
714 a_point f(cam->GetFocalPoint());
716 a_point dirh(cos(az*
d2r),sin(az*
d2r),0.);
718 double d =
dir.norm();
719 double dz = d*sin(
y*
d2r);
720 double dh = d*cos(
y*
d2r);
721 a_point dir2(dirh.x()*dh,dirh.y()*dh,dz);
723 cam->SetPosition(c2.x(),c2.y(),c2.z());
734 return ren_->GetActiveCamera()->GetRoll();
743 ren_->GetActiveCamera()->Roll(-
z);
750 vtkCamera * cam =
ren_->GetActiveCamera();
751 a_point c(cam->GetPosition());
752 a_point f(cam->GetFocalPoint());
754 double d =
dir.norm();
756 double dh = sqrt(d*d-dz*dz);
757 a_point dir2(dh*cos(
x*
d2r),dh*sin(
x*
d2r),dz);
759 cam->SetFocalPoint(f2.x(),f2.y(),f2.z());
767 vtkCamera * cam =
ren_->GetActiveCamera();
768 a_point c(cam->GetPosition());
769 a_point f(cam->GetFocalPoint());
771 double d =
dir.norm();
773 a_point dirh(cos(az*
d2r),sin(az*
d2r),0.);
775 double dz = d*sin(
y*
d2r);
776 double dh = d*cos(
y*
d2r);
777 a_point dir2(dirh.x()*dh,dirh.y()*dh,dz);
779 cam->SetFocalPoint(f2.x(),f2.y(),f2.z());
789 ren_->GetActiveCamera()->Roll(0.);
790 this->
up(a_point(0.,0.,1.));
797 a_point pop2 = d*(pop-fop).normalise();
806 return (fop-pop).norm();
812 for (
int i =0; i <
elements_.size(); i++)
816 double d = 2*d0*exp(log(10.)*
v/100.);
822 if (R>=0. && R<=1. && G>=0. && G<=1. && B>=0. && B<=1.)
824 ren_->SetBackground(R,G,B);
831 vtkCamera * cam =
ren_->GetActiveCamera();
833 cam->GetPosition(po);
834 cam->GetFocalPoint(fo);
837 a_point
dir(1.,0.,0.);
838 double dist = (p_fo-p_po).norm();
839 a_point np_po = p_fo-
dir*dist;
840 cam->SetPosition(np_po.x(),np_po.y(),np_po.z());
841 cam->SetViewUp(0.,0.,1.);
847 vtkCamera * cam =
ren_->GetActiveCamera();
849 cam->GetPosition(po);
850 cam->GetFocalPoint(fo);
853 a_point
dir(-1.,0.,0.);
854 double dist = (p_fo-p_po).norm();
855 a_point np_po = p_fo-
dir*dist;
856 cam->SetPosition(np_po.x(),np_po.y(),np_po.z());
857 cam->SetViewUp(0.,0.,1.);
863 vtkCamera * cam =
ren_->GetActiveCamera();
865 cam->GetPosition(po);
866 cam->GetFocalPoint(fo);
869 a_point
dir(0.,1.,0.);
870 double dist = (p_fo-p_po).norm();
871 a_point np_po = p_fo-
dir*dist;
872 cam->SetPosition(np_po.x(),np_po.y(),np_po.z());
873 cam->SetViewUp(0.,0.,1.);
879 vtkCamera * cam =
ren_->GetActiveCamera();
881 cam->GetPosition(po);
882 cam->GetFocalPoint(fo);
885 a_point
dir(0.,-1.,0.);
886 double dist = (p_fo-p_po).norm();
887 a_point np_po = p_fo-
dir*dist;
888 cam->SetPosition(np_po.x(),np_po.y(),np_po.z());
889 cam->SetViewUp(0.,0.,1.);
895 vtkCamera * cam =
ren_->GetActiveCamera();
897 cam->GetPosition(po);
898 cam->GetFocalPoint(fo);
901 a_point
dir(0.,0.,1.);
902 double dist = (p_fo-p_po).norm();
903 a_point np_po = p_fo-
dir*dist;
904 cam->SetPosition(np_po.x(),np_po.y(),np_po.z());
905 cam->SetViewUp(1.,0.,0.);
911 vtkCamera * cam =
ren_->GetActiveCamera();
913 cam->GetPosition(po);
914 cam->GetFocalPoint(fo);
917 a_point
dir(0.,0.,-1.);
918 double dist = (p_fo-p_po).norm();
919 a_point np_po = p_fo-
dir*dist;
920 cam->SetPosition(np_po.x(),np_po.y(),np_po.z());
921 cam->SetViewUp(1.,0.,0.);
927 return ren_->GetSize()[0];
933 return ren_->GetSize()[1];
938 vtkCamera * cam =
ren_->GetActiveCamera();
940 cam->GetFocalPoint(
pt);
941 log_ <<
"window_name focalpoint " <<
pt[0] <<
" " <<
pt[1] <<
" " <<
pt[2] << std::endl;
942 cam->GetPosition(
pt);
943 log_ <<
"window_name viewpoint " <<
pt[0] <<
" " <<
pt[1] <<
" " <<
pt[2] << std::endl;
953 double vna[] = {vn.x(), vn.y(), vn.z()};
954 double fna[] = {fn.x(), fn.y(), fn.z()};
962 vtkCamera * cam =
ren_->GetActiveCamera();
964 cam->GetPosition(po);
970 vtkCamera * cam =
ren_->GetActiveCamera();
972 cam->GetFocalPoint(fp);
978 vtkCamera * cam =
ren_->GetActiveCamera();
986 vtkCamera * cam =
ren_->GetActiveCamera();
987 a_point f(cam->GetFocalPoint());
992 cam->SetPosition(c.x(),c.y(),c.z());
1001 vtkCamera * cam =
ren_->GetActiveCamera();
1002 a_point c(cam->GetPosition());
1008 cam->SetFocalPoint(f.x(),f.y(),f.z());
1020 vtkCamera * cam =
ren_->GetActiveCamera();
1021 cam->SetViewUp(p2.x(),p2.y(),p2.z());
1039 this->
up(a_point(
pt[0],
pt[1],
pt[2]));
1044 std::string f0 = file.substr(0,file.find_first_of(
'.'))+
".jpg";
1045 vtkRenderLargeImage * grab = vtkRenderLargeImage::New();
1046 grab->SetInput(
ren_);
1048 vtkJPEGWriter * writer = vtkJPEGWriter::New();
1049 writer->SetFileName(
const_cast<char *
>(f0.c_str()));
1050 writer->SetInputConnection(grab->GetOutputPort());
1051 writer->SetQuality(100);
1056 log_ <<
"window_name save jpg " << file << std::endl;
1061 std::string f0 = file.substr(0,file.find_first_of(
'.'))+
".png";
1062 vtkRenderLargeImage * grab = vtkRenderLargeImage::New();
1063 grab->SetInput(
ren_);
1065 vtkPNGWriter * writer = vtkPNGWriter::New();
1066 writer->SetFileName(
const_cast<char *
>(f0.c_str()));
1067 writer->SetInputConnection(grab->GetOutputPort());
1072 log_ <<
"window_name save png " << file << std::endl;
1077 vtkVRMLExporter * writer = vtkVRMLExporter::New();
1078 writer->SetFileName(file.c_str());
1082 log_ <<
"window_name save vrml " << file << std::endl;
1087 vtkAppendPolyData * datas = vtkAppendPolyData::New();
1089 datas->AddInputData(la->polydata());
1090 vtkPolyDataWriter * writer = vtkPolyDataWriter::New();
1091 writer->SetFileName(file.c_str());
1092 writer->SetInputConnection(datas->GetOutputPort());
1096 log_ <<
"window_name save vtk " << file << std::endl;
1101 std::string f0 = file.substr(0,file.find_first_of(
'.'));
1102 vtkRIBExporter * writer = vtkRIBExporter::New();
1103 writer->SetFilePrefix(f0.c_str());
1108 log_ <<
"window_name save rib " << file << std::endl;
1113 std::string f0 = file.substr(0,file.find_first_of(
'.'))+
".tif";
1114 vtkWindowToImageFilter * grab = vtkWindowToImageFilter::New();
1116 vtkTIFFWriter * writer = vtkTIFFWriter::New();
1117 writer->SetFileName(
const_cast<char *
>(f0.c_str()));
1118 writer->SetInputConnection(grab->GetOutputPort());
1123 log_ <<
"window_name save tiff " << file << std::endl;
1135 for (
int i =0; i <
elements_.size(); i++)
1139 for (
int i = 0; i < 3; i++) fo[i] = box.
m(i);
1140 double d0 = box.
d();
1141 vtkCamera * cam =
ren_->GetActiveCamera();
1143 cam->SetParallelScale(box.
z());
1145 cam->SetFocalPoint(fo);
1146 cam->SetPosition(fo[0],fo[1]-2*d0,fo[2]);
1147 cam->ComputeViewPlaneNormal();
1148 cam->SetViewAngle(30);
1149 cam->SetViewUp(0,0,1);
1156 double po[3], fo[3];
1157 vtkCamera * cam =
ren_->GetActiveCamera();
1158 cam->GetPosition(po);
1159 cam->GetFocalPoint(fo);
1160 vtkLightCollection * lights =
ren_->GetLights();
1161 lights->InitTraversal();
1162 vtkLight * light = lights->GetNextItem();
1163 light->SetPosition(po);
1164 light->SetFocalPoint(fo);
1165 light->SetIntensity(1);
1184 std::ifstream ff(file.c_str());
1189 log_ <<
"window_name open " << file << std::endl;
1195 std::ofstream ff(file.c_str());
1199 log_ <<
"window_name save " << file << std::endl;
void delete_vector(std::vector< a_axes * > &)
std::ostream & operator<<(std::ostream &o, a_canvas &w)
std::istream & operator>>(std::istream &i, a_canvas &w)
double m(const char) const
void operator()(a_element *)
void jpgout(const std::string &file)
vtkRenderWindowInteractor * iren_
void vtkout(const std::string &file)
std::string listelements()
std::vector< a_element * > elements_
void save(const std::string &file)
a_element * active_element_
void magnification(unsigned short val)
void outputfile(a_text &text)
virtual void fit_deform_interact()
vtkRenderWindow * renWin_
void open(const std::string &file)
void ribout(const std::string &file)
void tifout(const std::string &file)
static const std::string help()
bool existelement(const std::string &s) const
void pan(const double *pt)
a_point viewpoint() const
void background(double R, double G, double B)
virtual void fit_interact()
const std::string getelementname() const
void addelement(a_element &l)
void unlinkelement(const std::string &s)
a_point focalpoint() const
double magnification_
magnification value for exports
virtual void reset_interact()
void pngout(const std::string &file)
void setlightoncameraposition()
void vrmlout(const std::string &file)
void setelement(const std::string &s)
virtual void straighten()
void fitelements(const std::string &m, const std::string &p)
virtual void cut_interact()
layer used by screen to draw vector graphics
void setcanvas(a_canvas &can)
void name(std::string &aname)
virtual std::string type() const
void name(const std::string &name)
static a_interactor * New()
void magnification(int val)
void keep_color(const bool val=true)
static cutcallback * New()
void set_plane(vtkPlane *plane)
void set_actor(vtkActor *actor)
virtual void Execute(vtkObject *caller, unsigned long, void *)
void iren(vtkRenderWindowInteractor *iren)
virtual void Execute(vtkObject *caller, unsigned long, void *)
static testcallback * New()
double v(const uint32_t step, const uint32_t n)
a_point dir(int i, int l)