17 #include "vtkAppendPolyData.h"
18 #include "vtkCamera.h"
19 #include "vtkCellArray.h"
20 #include "vtkDataSetMapper.h"
21 #include "vtkInteractorStyleTrackballCamera.h"
22 #include "vtkJPEGWriter.h"
24 #include "vtkLightCollection.h"
25 #include "vtkPoints.h"
26 #include "vtkPolyDataWriter.h"
27 #include "vtkRenderLargeImage.h"
28 #include "vtkRenderer.h"
29 #include "vtkRenderWindow.h"
30 #include "vtkRenderWindowInteractor.h"
31 #include "vtkRIBExporter.h"
32 #include "vtkTIFFWriter.h"
33 #include "vtkPNGWriter.h"
34 #include "vtkVRMLExporter.h"
35 #include "vtkWindowToImageFilter.h"
37 #include "a_dxf_group.h"
38 #include "a_dxf_entity.h"
39 #include "a_dxf_point.h"
40 #include "a_dxf_line.h"
41 #include "a_dxf_polyline.h"
52 o <<
"**********" << std::endl;
53 o <<
"a_screen:" << std::endl;
54 o <<
"**********" << std::endl;
55 o <<
"This is a 3D window class" << std::endl;
56 o <<
"Commands:" << std::endl;
57 o <<
"--------" << std::endl;
58 o <<
"a_screen W: create a window" << std::endl;
59 o <<
"Drawing:" << std::endl;
60 o <<
"......." << std::endl;
61 o <<
"point X Y Z: draw a point." << std::endl;
62 o <<
"line X1 Y1 Z1 X2 Y2 Z2: draw a line." << std::endl;
63 o <<
"vertex X Y Z: draw point of a polyline." << std::endl;
64 o <<
"endpolyline: end the polyline." << std::endl;
65 o <<
"closepolyline: link the last vertex with the first vertex." << std::endl;
66 o <<
"surfacepoint X Y Z: add a point to a surface (1 surface per layer)." << std::endl;
67 o <<
"......." << std::endl;
68 o <<
"erase point: erase last point." << std::endl;
69 o <<
"erase line: erase last line." << std::endl;
70 o <<
"erase polyline: erase last polyline." << std::endl;
71 o <<
"Layer management:" << std::endl;
72 o <<
"................" << std::endl;
73 o <<
"the default layer is '0'" << std::endl;
74 o <<
"layer new NAME: create new layer" << std::endl;
75 o <<
"layer set NAME: set current layer" << std::endl;
76 o <<
"layer on NAME: make layer visible" << std::endl;
77 o <<
"layer off NAME: hide layer" << std::endl;
78 o <<
"layer erase NAME: erase layer" << std::endl;
79 o <<
"(alternatively: layer rm NAME)" << std::endl;
80 o <<
"layer list: return a list of existing layers" << std::endl;
81 o <<
"(alternatively: layer ls)" << std::endl;
82 o <<
"layer current: return name of current layer" << std::endl;
83 o <<
"layer exist NAME: check whether layer exists (returns 0 or 1)" << std::endl;
84 o <<
"color R G B: set color of current layer" << std::endl;
85 o <<
"R: get the red component of current layer" << std::endl;
86 o <<
"G: get the green component of current layer" << std::endl;
87 o <<
"B: get the blue component of current layer" << std::endl;
88 o <<
"thickness T: set thickness of lines in currrent layer" << std::endl;
89 o <<
"thickness: get thickness of lines in currrent layer" << std::endl;
90 o <<
"Navigating:" << std::endl;
91 o <<
".........." << std::endl;
92 o <<
"interact: start mouse control (click 'e' to exit)" << std::endl;
93 o <<
"redraw: refresh screen" << std::endl;
94 o <<
"fit: fit data to screen" << std::endl;
95 o <<
"background R G B: change background color" << std::endl;
96 o <<
"perspective on: perspective view" << std::endl;
97 o <<
"perspective off: parallel view" << std::endl;
98 o <<
"cursor on: show cursor" << std::endl;
99 o <<
"cursor off: hide cursor" << std::endl;
100 o <<
"azimuth V: set azimuth angle" << std::endl;
101 o <<
"azimuth: get azimuth angle" << std::endl;
102 o <<
"(similar functions for elevation, roll, pich, yaw, distance)" << std::endl;
103 o <<
"camerapoint X Y Z: set view point" << std::endl;
104 o <<
"camerapoint: get view point" << std::endl;
105 o <<
"focalpoint X Y Z: set focal point" << std::endl;
106 o <<
"focalpoint: get focal point" << std::endl;
107 o <<
"light sun: fixed light" << std::endl;
108 o <<
"light spot: light follow camera" << std::endl;
109 o <<
"Input/Output:" << std::endl;
110 o <<
"............" << std::endl;
111 o <<
"import CODE FILE: import data" << std::endl;
112 o <<
"(code: pt, dxf)" << std::endl;
113 o <<
"save CODE FILE" << std::endl;
114 o <<
"(code: pt, dxf, jpeg, rib, png, vec, vrml, vtk)" << std::endl;
115 o <<
"logfile VE_TEXT: log is saved to a a_text object" << std::endl;
116 o <<
"outputfile VE_TEXT: output is saved to a a_text object" << std::endl;
117 o <<
"VE_TEXT should be created before: ex: a_text NAME" << std::endl;
130 ren_ = vtkRenderer::New();
131 renWin_ = vtkRenderWindow::New();
138 renWin_->SetStereoTypeToAnaglyph();
153 style->
name(
"screen");
155 iren_ = vtkRenderWindowInteractor::New();
157 iren_->SetInteractorStyle(style);
165 renWin_->SetWindowName(
"screen");
166 log_.file(
"a_screen.log");
168 log_ <<
"#!/usr/bin/env tclsh" << std::endl;
169 log_ <<
"package require a_screen" << std::endl;
170 log_ <<
"a_screen window_name" << std::endl;
180 log_ <<
"rename window_name \"\"" << std::endl;
189 double m(
const char)
const;
196 for (
int i = 0; i < 3; i++) l_[i*2] = 1e30;
197 for (
int i = 0; i < 3; i++) l_[i*2+1] = -1e30;
204 l->
actor()->GetBounds(b);
205 for (
int i = 0; i < 3; i++)
206 if (b[i*2]<l_[i*2]) {l_[i*2] = b[i*2];}
207 for (
int i = 0; i < 3; i++)
208 if (b[i*2+1]>l_[i*2+1]) {l_[i*2+1] = b[i*2+1];}
214 for (
int i = 0; i < 3; i++) d0 += (l_[i*2+1]-l_[i*2])*(l_[i*2+1]-l_[i*2]);
219 return (l_[i*2]+l_[i*2+1])/2;
257 for (
int i = 0; i < 3; i++) fo[i] = box.
m(i);
262 ren_->AddActor(l->actor());
263 ren_->AddActor(l->actor2());
265 log_ <<
"window_name newlayer " << s << std::endl;
282 for (
int i = 0; i < 3; i++) fo[i] = box.
m(i);
286 la_out->append(la_in);
288 ren_->AddActor(la_out->actor());
289 ren_->AddActor(la_out->actor2());
299 s += la->name() +
" ";
301 s = s.substr(0,s.size()-1);
302 log_ <<
"window_name listlayers" << std::endl;
312 log_ <<
"window_name setlayer " << s << std::endl;
324 std::vector<a_screenlayer *>::iterator li = std::find(
layers_.begin(),
layers_.end(),p);
337 log_ <<
"window_name eraselayer " << s << std::endl;
368 log_ <<
"window_name color " << r <<
" " << g <<
" " << b << std::endl;
377 log_ <<
"window_name thickness " << t << std::endl;
384 ren_->LightFollowCameraOn();
386 iren_->LightFollowCameraOn();
388 log_ <<
"window_name light spot" << std::endl;
395 ren_->LightFollowCameraOff();
397 iren_->LightFollowCameraOff();
400 log_ <<
"window_name light sun" << std::endl;
405 ren_->GetActiveCamera()->ParallelProjectionOn();
407 log_ <<
"window_name perspective off" << std::endl;
412 ren_->GetActiveCamera()->ParallelProjectionOff();
414 log_ <<
"window_name perspective on" << std::endl;
455 this->
point(p.x(),p.y(),p.z());
466 renWin_->SetWindowName(
"screen");
474 log_ <<
"window_name point " << x <<
" " << y <<
" " << z <<
" " << std::endl;
479 this->
line(p1.x(),p1.y(),p1.z(),p2.x(),p2.y(),p2.z());
489 renWin_->SetWindowName(
"screen");
493 log_ <<
"window_name line " << x1 <<
" " << y1 <<
" " << z1 <<
" "
494 <<
x2 <<
" " << y2 <<
" " << z2 <<
" " << std::endl;
499 this->
vertex(p.x(),p.y(),p.z());
505 log_ <<
"window_name vertex " << x <<
" " << y <<
" " << z <<
" " << std::endl;
515 renWin_->SetWindowName(
"screen");
519 log_ <<
"window_name endpolyline" << std::endl;
529 renWin_->SetWindowName(
"screen");
533 log_ <<
"window_name closepolyline" << std::endl;
578 renWin_->SetWindowName(
"screen");
584 log_ <<
"window_name surfacepoint " << x <<
" " << y <<
" " << z << std::endl;
591 log_ <<
"window_name erase point" << std::endl;
598 log_ <<
"window_name erase line" << std::endl;
605 log_ <<
"window_name erase polylineline" << std::endl;
611 ren_->GetActiveCamera()->Azimuth(
x0_-x);
620 ren_->GetActiveCamera()->Elevation(
y0_-y);
628 ren_->GetActiveCamera()->Roll(
z0_-z);
636 ren_->GetActiveCamera()->Pitch(u-
y0_);
645 ren_->GetActiveCamera()->Yaw(
x0_-v);
655 ren_->GetActiveCamera()->Roll(
z0_);
664 a_point pop2 = d*(pop-fop).normalise();
673 return (fop-pop).norm();
683 double d = 2*d0*exp(log(10)*
v/100);
689 if (
R>=0 && R<=1 && G>=0 && G<=1 && B>=0 &&
B<=1)
698 vtkCamera * cam =
ren_->GetActiveCamera();
700 cam->GetFocalPoint(
pt);
701 log_ <<
"window_name setfocalpoint " <<
pt[0] <<
" " <<
pt[1] <<
" " <<
pt[2] << std::endl;
702 cam->GetPosition(
pt);
703 log_ <<
"window_name setviewpoint " <<
pt[0] <<
" " <<
pt[1] <<
" " <<
pt[2] << std::endl;
709 if (move.norm() > 0.00001)
713 double vna[] = {vn.x(), vn.y(), vn.z()};
714 double fna[] = {fn.x(), fn.y(), fn.z()};
722 vtkCamera * cam =
ren_->GetActiveCamera();
724 cam->GetPosition(po);
730 vtkCamera * cam =
ren_->GetActiveCamera();
732 cam->GetFocalPoint(fp);
738 vtkCamera * cam =
ren_->GetActiveCamera();
741 if ((fpo-poo).norm() > 0.00001)
744 cam->SetPosition(poo.x(),poo.y(),poo.z());
745 cam->ComputeViewPlaneNormal();
746 cam->SetViewAngle(30);
747 a_point
n = (fpo-poo).normalise();
748 if ((
n.x()>0)||(
n.y()>0))
749 cam->SetViewUp(0,0,1);
751 cam->SetViewUp(1,0,0);
755 x0_ = (180/3.14159265358979)*atan2(
n.y(),
n.x());
763 y0_ = 90-(180/3.14159265358979)*acos(
n.z());
769 vtkCamera * cam =
ren_->GetActiveCamera();
772 if ((fpo-poo).norm() > 0.00001)
775 cam->SetFocalPoint(fpo.x(),fpo.y(),fpo.z());
776 cam->ComputeViewPlaneNormal();
777 cam->SetViewAngle(30);
778 a_point
n = (fpo-poo).normalise();
779 if ((
n.x()>0)||(
n.y()>0))
780 cam->SetViewUp(0,0,1);
782 cam->SetViewUp(1,0,0);
786 x0_ = (180/3.14159265358979)*atan2(
n.y(),
n.x());
794 y0_ = 90-(180/3.14159265358979)*acos(
n.z());
810 std::ifstream ff(file.c_str());
812 a_dxf_group group(&ff);
814 a_dxf_point
point(&ff);
815 a_dxf_line
line(&ff);
816 a_dxf_polyline polyline(&ff);
817 a_dxf_entity entity(&ff);
818 std::string layer0 =
"0";
823 if (group.data_is(
"POINT"))
827 if (
point.layer() != layer0)
829 layer0 =
point.layer();
836 else if (group.data_is(
"LINE"))
840 if (
line.layer() != layer0)
842 layer0 =
line.layer();
849 else if (group.data_is(
"POLYLINE"))
854 if (polyline.layer() != layer0)
856 layer0 = polyline.layer();
860 for (
int i=0; i<polyline.n(); i++)
861 this->
vertex0(polyline.x(i),polyline.y(i),polyline.z(i));
872 renWin_->SetWindowName(
"screen");
876 log_ <<
"window_name import dxf " << file << std::endl;
881 std::ofstream o(file.c_str());
884 output_ <<
"cannot create file '" << file <<
"'" << std::endl;
888 o <<
" 0" << std::endl <<
"SECTION" << std::endl;
889 o <<
" 2" << std::endl <<
"ENTITIES" << std::endl;
897 o <<
" 0" << std::endl <<
"ENDSEC" << std::endl;
898 o <<
" 0" << std::endl <<
"EOF" << std::endl;
900 log_ <<
"window_name save dxf " << file << std::endl;
905 std::ifstream ff(file.c_str());
913 renWin_->SetWindowName(
"screen");
917 log_ <<
"window_name import pt " << file << std::endl;
922 std::ofstream o(file.c_str());
925 output_ <<
"cannot create file '" << file <<
"'" << std::endl;
930 log_ <<
"window_name save pt " << file << std::endl;
935 std::string f0 = file.substr(0,file.find_first_of(
'.'))+
".jpg";
936 vtkWindowToImageFilter * grab = vtkWindowToImageFilter::New();
938 vtkJPEGWriter * writer = vtkJPEGWriter::New();
939 writer->SetFileName(
const_cast<char *
>(f0.c_str()));
940 writer->SetInputConnection(grab->GetOutputPort());
941 writer->SetQuality(100);
946 log_ <<
"window_name save jpg " << file << std::endl;
951 std::string f0 = file.substr(0,file.find_first_of(
'.'))+
".png";
952 vtkRenderLargeImage * grab = vtkRenderLargeImage::New();
953 grab->SetInput(
ren_);
954 vtkPNGWriter * writer = vtkPNGWriter::New();
955 writer->SetFileName(
const_cast<char *
>(f0.c_str()));
956 writer->SetInputConnection(grab->GetOutputPort());
961 log_ <<
"window_name save png " << file << std::endl;
966 vtkVRMLExporter * writer = vtkVRMLExporter::New();
967 writer->SetFileName(file.c_str());
971 log_ <<
"window_name save vrml " << file << std::endl;
976 vtkAppendPolyData * datas = vtkAppendPolyData::New();
979 datas->AddInputData(la->polydata());
980 datas->AddInputData(la->polydata2());
982 vtkPolyDataWriter * writer = vtkPolyDataWriter::New();
983 writer->SetFileName(file.c_str());
984 writer->SetInputConnection(datas->GetOutputPort());
988 log_ <<
"window_name save vtk " << file << std::endl;
993 std::string f0 = file.substr(0,file.find_first_of(
'.'));
994 vtkRIBExporter * writer = vtkRIBExporter::New();
995 writer->SetFilePrefix(f0.c_str());
1000 log_ <<
"window_name save rib " << file << std::endl;
1005 std::string f0 = file.substr(0,file.find_first_of(
'.'))+
".tif";
1006 vtkWindowToImageFilter * grab = vtkWindowToImageFilter::New();
1008 vtkTIFFWriter * writer = vtkTIFFWriter::New();
1009 writer->SetFileName(
const_cast<char *
>(f0.c_str()));
1010 writer->SetInputConnection(grab->GetOutputPort());
1015 log_ <<
"window_name save tiff " << file << std::endl;
1031 for (
int i = 0; i < 3; i++) fo[i] = box.
m(i);
1032 double d0 = box.
d();
1033 vtkCamera * cam =
ren_->GetActiveCamera();
1034 cam->SetFocalPoint(fo);
1035 cam->SetPosition(fo[0],fo[1]-2*d0,fo[2]);
1036 cam->ComputeViewPlaneNormal();
1037 cam->SetViewAngle(30);
1038 cam->SetViewUp(0,0,1);
1046 double po[3], fo[3];
1047 vtkCamera * cam =
ren_->GetActiveCamera();
1048 cam->GetPosition(po);
1049 cam->GetFocalPoint(fo);
1050 vtkLightCollection * lights =
ren_->GetLights();
1051 lights->InitTraversal();
1052 vtkLight * light = lights->GetNextItem();
1053 light->SetPosition(po);
1054 light->SetFocalPoint(fo);
1055 light->SetIntensity(1);
1065 for (
int k = 0; k < n_layers; k++)
1073 w.
ren_->AddActor(la->actor());
1074 w.
ren_->AddActor(la->actor2());
1082 o << w.
layers_.size() << std::endl;
1090 std::ifstream ff(file.c_str());
1095 renWin_->SetWindowName(
"screen");
1096 log_ <<
"window_name open " << file << std::endl;
1102 std::ofstream ff(file.c_str());
1106 log_ <<
"window_name save " << file << std::endl;
void delete_vector(std::vector< a_axes * > &)
std::istream & operator>>(std::istream &i, a_screen &w)
std::ostream & operator<<(std::ostream &o, a_screen &w)
double m(const char) const
void operator()(a_element *)
void name(const std::string &name)
static a_interactor * New()
void keep_color(const bool val=true)
virtual void eraselayer(const std::string &s)
void point(const a_point &p)
static const std::string help()
std::vector< a_screenlayer * > layers_
void vertex0(double x, double y, double z)
void vrmlout(const std::string &file)
a_point viewpoint() const
void jpgout(const std::string &file)
void setofflayer(const std::string &s)
void point0(double x, double y, double z)
const std::string getlayer() const
void pan(const double *pt)
void setonlayer(const std::string &s)
void line(const a_point &p1, const a_point &p2)
void line0(double x1, double y1, double z1, double x2, double y2, double z2)
void color(float r, float g, float b)
void tifout(const std::string &file)
virtual void newlayer(const std::string &s)
void ptout(const std::string &file)
void save(const std::string &file)
vtkRenderWindowInteractor * iren_
a_point focalpoint() const
void setlightoncameraposition()
void vertex(const a_point &p)
bool existlayer(const std::string &s) const
void ribout(const std::string &file)
virtual void setlayer(const std::string &s)
void open(const std::string &file)
a_screenlayer * active_layer_
void dxfin(const std::string &file)
void pngout(const std::string &file)
void dxfout(const std::string &file)
void background(double R, double G, double B)
void ptin(const std::string &file)
void vtkout(const std::string &file)
void surfacepoint(const a_point &p)
vtkRenderWindow * renWin_
layer used by screen to draw vector graphics
void append(const a_screenlayer &)
void surfacepoint(double x, double y, double z)
void name(std::string aname)
void color(float r, float g, float b)
void vertex(double x, double y, double z)
vtkActor * surfaceactor()
void line(double x1, double y1, double z1, double x2, double y2, double z2)
void thickness(const int)
void visibility(const int code)
void ptout(std::ostream &o) const
void point(double x, double y, double z)
void ptin(std::istream &in)
double v(const uint32_t step, const uint32_t n)