1 // -*- mode:C++ ; compile-command: "g++ -I.. -fPIC -DPIC -g -c renee.cc -o renee.lo && ln -sf renee.lo renee.o && gcc -shared renee.lo -lc  -Wl,-soname -Wl,librenee.so.0 -o librenee.so.0.0.0 && ln -sf librenee.so.0.0.0 librenee.so.0 && ln -sf librenee.so.0.0.0 librenee.so" -*-
2 /*
3  *  Copyright (C) 2002 Renee De Graeve, Inst. Fourier, 38402 St Martin d'Heres
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 using namespace std;
19 #include <stdexcept>
20 #include <cmath>
21 #include <cstdlib>
22 #ifndef IN_GIAC
23 #include <giac/giac.h>
24 #else
25 #include "giac.h"
26 #endif
27 //#include "renee.h"
28 //#include "papier.h"
29 
30 #ifndef NO_NAMESPACE_GIAC
31 namespace giac {
32 #endif // ndef NO_NAMESPACE_GIAC
33 
34 #include <stdexcept>
35 #include <cmath>
36 #include <cstdlib>
37 #ifndef IN_GIAC
38 #include <giac/giac.h>
39 #else
40 #include "giac.h"
41 #endif
42 
43   void read_option(const vecteur & v,double xmin,double xmax,double ymin,double ymax,vecteur & attributs, int & nstep,int & jstep,GIAC_CONTEXT);
44 
papier_lignes(vecteur & res,double xmin,double xmax,double ymin,double ymax,double angle,double deltax,double deltay,double pente,const vecteur & attributs,GIAC_CONTEXT)45   void papier_lignes(vecteur & res,double xmin,double xmax,double ymin,double ymax,double angle,double deltax,double deltay,double pente,const vecteur & attributs,GIAC_CONTEXT){
46     res.push_back(pnt_attrib(gen(makevecteur(xmin+ymin*cst_i,xmin+ymax*cst_i),_GROUP__VECT),attributs,contextptr));
47     res.push_back(pnt_attrib(gen(makevecteur(xmax+ymax*cst_i,xmin+ymax*cst_i),_GROUP__VECT),attributs,contextptr));
48     res.push_back(pnt_attrib(gen(makevecteur(xmax+ymin*cst_i,xmax+ymax*cst_i),_GROUP__VECT),attributs,contextptr));
49     res.push_back(pnt_attrib(gen(makevecteur(xmax+ymin*cst_i,xmin+ymin*cst_i),_GROUP__VECT),attributs,contextptr));
50     //const double cst_pi;
51     double pi=evalf_double(cst_pi,1,contextptr)._DOUBLE_val;
52     if (angle==pi/2){
53       for (double x=xmin;x<=xmax;x+=deltax){
54 	res.push_back(pnt_attrib(gen(makevecteur(x+ymin*cst_i,x+ymax*cst_i),_GROUP__VECT),attributs,contextptr));
55       }
56       //return res;
57     }
58     double Y=(ymax-ymin)/pente;
59     if (angle<pi/2){
60       double q=std::floor(Y/deltax+1e-12);
61       for (double x=xmin-q*deltax;x<=xmin;x+=deltax){
62 	double Y1=pente*(xmax-x)+ymin;
63 	double Y2=pente*(xmin-x)+ymin;
64 	double X1=(ymax-ymin)/pente+x;
65 	if (Y1<ymax){
66 	  res.push_back(pnt_attrib(gen(makevecteur(xmin+Y2*cst_i,xmax+Y1*cst_i),_GROUP__VECT),attributs,contextptr));
67 	}
68 	if (X1<xmax){
69 	  res.push_back(pnt_attrib(gen(makevecteur(xmin+Y2*cst_i,X1+ymax*cst_i),_GROUP__VECT),attributs,contextptr));
70 	}
71       }
72       for (double x=xmin;x<=xmax;x+=deltax){
73 	double Y1=pente*(xmax-x)+ymin;
74 	double Y2=pente*(xmin-x)+ymin;
75 	double X1=(ymax-ymin)/pente+x;
76 	if (Y1<ymax){
77 	  res.push_back(pnt_attrib(gen(makevecteur(x+ymin*cst_i,xmax+Y1*cst_i),_GROUP__VECT),attributs,contextptr));
78 	}
79 	if (X1<xmax){
80 	  res.push_back(pnt_attrib(gen(makevecteur(x+ymin*cst_i,X1+ymax*cst_i),_GROUP__VECT),attributs,contextptr));
81 	}
82       }
83       //return res;
84     }
85     if (angle>pi/2){
86       double x=xmin;
87       while (x<=xmax){
88 	double Y1=pente*(xmin-x)+ymin;
89 	double X1=(ymax-ymin)/pente+x;
90 	if (Y1<ymax){
91 	  res.push_back(pnt_attrib(gen(makevecteur(x+ymin*cst_i,xmin+Y1*cst_i),_GROUP__VECT),attributs,contextptr));
92 	}
93 	if (X1>xmin){
94 	  res.push_back(pnt_attrib(gen(makevecteur(x+ymin*cst_i,X1+ymax*cst_i),_GROUP__VECT),attributs,contextptr));
95 	}
96 	x=x+deltax;
97       }
98       double q=std::ceil(Y/deltax-1e-12);
99       while (x<=xmax-q*deltax){
100 	double Y1=pente*(xmin-x)+ymin;
101 	double Y2=pente*(xmax-x)+ymin;
102 	double X1=(ymax-ymin)/pente+x;
103 	if (Y1<ymax){
104 	  res.push_back(pnt_attrib(gen(makevecteur(xmax+Y2*cst_i,xmin+Y1*cst_i),_GROUP__VECT),attributs,contextptr));
105 	}
106 	if (X1>xmin){
107 	  res.push_back(pnt_attrib(gen(makevecteur(xmax+Y2*cst_i,X1+ymax*cst_i),_GROUP__VECT),attributs,contextptr));
108 	}
109 	x=x+deltax;
110       }
111       //return res;
112     }
113   }
114 
Papier_pointe_quadrillage(const gen & args,int quadrillage,GIAC_CONTEXT)115   gen Papier_pointe_quadrillage(const gen & args,int quadrillage,GIAC_CONTEXT){
116     double xmin=gnuplot_xmin,xmax=gnuplot_xmax,ymin=gnuplot_ymin,ymax=gnuplot_ymax;
117     double deltax=(xmax-xmin)/20,deltay=(ymax-ymin)/20,angle=evalf_double(cst_pi/2,1,contextptr)._DOUBLE_val;
118     vecteur attributs(1,default_color(contextptr));
119     if (args.type==_VECT){
120       vecteur & w=*args._VECTptr;
121       int s=w.size();
122       if (s>0){
123 	gen tmp=evalf_double(w[0],1,contextptr);
124 	if (tmp.type==_DOUBLE_)
125 	  deltax=fabs(tmp._DOUBLE_val);
126       }
127       if (s>1){
128 	gen tmp=evalf_double(w[1],1,contextptr);
129 	if (tmp.type==_DOUBLE_){
130 	  angle=tmp._DOUBLE_val;
131 	  double pi=M_PI;
132 	  angle=angle-std::floor(angle/pi)*pi;
133 	  std::cout<<angle << endl;
134 	  if (fabs(angle)<epsilon(contextptr) || fabs(pi-angle)<epsilon(contextptr))
135 	    setsizeerr();
136 	}
137       }
138       if (s>2){
139 	gen tmp=evalf_double(w[2],1,contextptr);
140 	if (tmp.type==_DOUBLE_)
141 	  deltay=fabs(tmp._DOUBLE_val);
142       }
143       //int nstep=int((xmax-xmin)/deltax),kstep=int((ymax-ymin)/deltay);
144       gen x,y;
145       for (int i=0;i<s;++i){
146 	if (w[i].is_symb_of_sommet(at_equal)){
147 	  if (w[i][1]==x__IDNT_e)
148 	    readrange(w[i],gnuplot_xmin,gnuplot_xmax,x,xmin,xmax,contextptr);
149 	  if (w[i][1]==y__IDNT_e)
150 	    readrange(w[i],gnuplot_xmin,gnuplot_xmax,y,ymin,ymax,contextptr);
151 	}
152       }
153       int n1,n2;
154       read_option(w,xmin,xmax,ymin,ymax,attributs,n1,n2,contextptr);
155       // if (!nstep)nstep=20;deltax=(xmax-xmin)/nstep;
156       //if (!kstep) kstep=20;deltay=(ymax-ymin)/kstep;
157     }
158 
159     deltax=(xmax-xmin)/std::floor(fabs((xmax-xmin)/deltax));
160     deltay=(ymax-ymin)/std::floor(fabs((ymax-ymin)/deltay));
161     if (quadrillage==2){
162       int color=attributs[0].val;
163       color = (color & 0xffff )| (7<<25) | (1 << 19);
164       attributs[0]=color;
165     }
166 
167     vecteur res;
168 
169     double pente=std::tan(angle);
170 
171     if (quadrillage==0 || quadrillage==1){
172       for (double y=ymin;y<=ymax;y+=deltay){
173 	res.push_back(pnt_attrib(gen(makevecteur(xmin+y*cst_i,xmax+y*cst_i),_GROUP__VECT),attributs,contextptr));
174       }
175       //papier_lignes(res,xmin,xmax,ymin,ymax,0,deltax,deltay,pente,attributs,contextptr);
176       papier_lignes(res,xmin,xmax,ymin,ymax,angle,deltax,deltay,pente,attributs,contextptr);
177     }
178     if (quadrillage==1){
179       double u1=deltay/pente;
180       if (u1-deltax==0) {angle=M_PI/2;}
181       if (u1-deltax>0) {angle=std::atan(deltay/(u1-deltax)); }
182       if (u1-deltax<0) {angle=std::atan(deltay/(u1-deltax))+M_PI;}
183       papier_lignes(res,xmin,xmax,ymin,ymax,angle,deltax,deltay,std::tan(angle),attributs,contextptr);
184 
185     } // end if quadrillage== 1
186     if (quadrillage==2) {
187       res.push_back(pnt_attrib(gen(makevecteur(xmin+ymin*cst_i,xmin+ymax*cst_i),_GROUP__VECT),attributs,contextptr));
188       res.push_back(pnt_attrib(gen(makevecteur(xmax+ymax*cst_i,xmin+ymax*cst_i),_GROUP__VECT),attributs,contextptr));
189       res.push_back(pnt_attrib(gen(makevecteur(xmax+ymin*cst_i,xmax+ymax*cst_i),_GROUP__VECT),attributs,contextptr));
190       res.push_back(pnt_attrib(gen(makevecteur(xmax+ymin*cst_i,xmin+ymin*cst_i),_GROUP__VECT),attributs,contextptr));
191       for (double y=ymin;y<=ymax;y+=deltay){
192 	double X=(y-ymin)/pente;
193 	int q=std::floor(X/deltax+1e-12);
194 	for (double x=xmin-q*deltax+X;x<xmax;x+=deltax){
195 	  res.push_back(pnt_attrib(x+y*cst_i,attributs,contextptr));
196 	}
197       }
198     }
199     if (quadrillage==3) {
200       papier_lignes(res,xmin,xmax,ymin,ymax,angle,deltax,deltay,pente,attributs,contextptr);
201     }
202     return res; // gen(res,_SEQ__VECT);
203   }
_Papier_pointe(const gen & args,GIAC_CONTEXT)204   gen _Papier_pointe(const gen & args,GIAC_CONTEXT){
205     return Papier_pointe_quadrillage(args,2,contextptr);
206   }
207   // const char _Papier_pointe_s []="papierp";
208   const string _Papier_pointe_s("papierp");
209   unary_function_eval __Papier_pointe(&giac::_Papier_pointe,_Papier_pointe_s);
210   unary_function_ptr at_Papier_pointe (&__Papier_pointe,0,true);
211 
_Papier_quadrille(const gen & args,GIAC_CONTEXT)212   gen _Papier_quadrille(const gen & args,GIAC_CONTEXT){
213     return Papier_pointe_quadrillage(args,0,contextptr);
214   }
215   // const char _Papier_quadrille_s []="papierq";
216   const string _Papier_quadrille_s ("papierq");
217   unary_function_eval __Papier_quadrille(&giac::_Papier_quadrille,_Papier_quadrille_s);
218   unary_function_ptr at_Papier_quadrille (&__Papier_quadrille,0,true);
219 
_Papier_triangule(const gen & args,GIAC_CONTEXT)220   gen _Papier_triangule(const gen & args,GIAC_CONTEXT){
221     return Papier_pointe_quadrillage(args,1,contextptr);
222   }
223   // const char _Papier_triangule_s []="papiert";
224   const string _Papier_triangule_s ("papiert");
225   unary_function_eval __Papier_triangule(&giac::_Papier_triangule,_Papier_triangule_s);
226   unary_function_ptr at_Papier_triangule (&__Papier_triangule,0,true);
227 
_Papier_ligne(const gen & args,GIAC_CONTEXT)228   gen _Papier_ligne(const gen & args,GIAC_CONTEXT){
229     return Papier_pointe_quadrillage(args,3,contextptr);
230   }
231   // const char _Papier_ligne_s []="papierl";
232   const string _Papier_ligne_s("papierl");
233   unary_function_eval __Papier_ligne(&giac::_Papier_ligne,_Papier_ligne_s);
234   unary_function_ptr at_Papier_ligne (&__Papier_ligne,0,true);
235 
236 #ifndef NO_NAMESPACE_GIAC
237 } // namespace giac
238 #endif // ndef NO_NAMESPACE_GIAC
239