1 // MEFISTO : library to compute 2D triangulation from segmented boundaries
3 // Copyright (C) 2003 Laboratoire J.-L. Lions UPMC Paris
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License.
10 // This library 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 GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 // See http://www.ann.jussieu.fr/~perronne or email Perronnet@ann.jussieu.fr
20 // or email Hecht@ann.jussieu.fr
25 // Authors: Frederic HECHT & Alain PERRONNET
31 #include <gp_Pnt.hxx> //Dans OpenCascade
32 #include <gp_Vec.hxx> //Dans OpenCascade
33 #include <gp_Dir.hxx> //Dans OpenCascade
35 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
36 // BUT: Definir les espaces affines R R2 R3 R4 soit Rn pour n=1,2,3,4
37 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
38 // AUTEUR : Frederic HECHT ANALYSE NUMERIQUE UPMC PARIS OCTOBRE 2000
39 // MODIFS : Alain PERRONNET ANALYSE NUMERIQUE UPMC PARIS NOVEMBRE 2000
40 //...............................................................................
45 template<class T> inline T Abs (const T &a){return a <0 ? -a : a;}
46 template<class T> inline void Echange (T& a,T& b) {T c=a;a=b;b=c;}
48 template<class T> inline T Min (const T &a,const T &b) {return a < b ? a : b;}
49 template<class T> inline T Max (const T &a,const T & b) {return a > b ? a : b;}
51 template<class T> inline T Max (const T &a,const T & b,const T & c){return Max(Max(a,b),c);}
52 template<class T> inline T Min (const T &a,const T & b,const T & c){return Min(Min(a,b),c);}
54 template<class T> inline T Max (const T &a,const T & b,const T & c,const T & d)
55 {return Max(Max(a,b),Max(c,d));}
56 template<class T> inline T Min (const T &a,const T & b,const T & c,const T & d)
57 {return Min(Min(a,b),Min(c,d));}
59 //le type Nom des entites geometriques P L S V O
61 typedef char Nom[1+24];
63 //le type N des nombres entiers positifs
65 typedef unsigned long int N;
67 //le type Z des nombres entiers relatifs
71 //le type R des nombres "reels"
75 //le type XPoint des coordonnees d'un pixel dans une fenetre
77 //typedef struct { short int x,y } XPoint; //en fait ce type est defini dans X11-Window
78 // #include <X11/Xlib.h>
83 friend std::ostream& operator << (std::ostream& f, const R2 & P)
84 { f << P.x << ' ' << P.y ; return f; }
85 friend std::istream& operator >> (std::istream& f, R2 & P)
86 { f >> P.x >> P.y ; return f; }
88 friend std::ostream& operator << (std::ostream& f, const R2 * P)
89 { f << P->x << ' ' << P->y ; return f; }
90 friend std::istream& operator >> (std::istream& f, R2 * P)
91 { f >> P->x >> P->y ; return f; }
96 R2 () :x(0),y(0) {} //les constructeurs
97 R2 (R a,R b) :x(a),y(b) {}
98 R2 (R2 A,R2 B) :x(B.x-A.x),y(B.y-A.y) {} //vecteur defini par 2 points
100 R2 operator+(R2 P) const {return R2(x+P.x,y+P.y);} // Q+P possible
101 R2 operator+=(R2 P) {x += P.x;y += P.y; return *this;}// Q+=P;
102 R2 operator-(R2 P) const {return R2(x-P.x,y-P.y);} // Q-P
103 R2 operator-=(R2 P) {x -= P.x;y -= P.y; return *this;} // Q-=P;
104 R2 operator-()const {return R2(-x,-y);} // -Q
105 R2 operator+()const {return *this;} // +Q
106 R operator,(R2 P)const {return x*P.x+y*P.y;} // produit scalaire (Q,P)
107 R operator^(R2 P)const {return x*P.y-y*P.x;} // produit vectoriel Q^P
108 R2 operator*(R c)const {return R2(x*c,y*c);} // produit a droite P*c
109 R2 operator*=(R c) {x *= c; y *= c; return *this;}
110 R2 operator/(R c)const {return R2(x/c,y/c);} // division par un reel
111 R2 operator/=(R c) {x /= c; y /= c; return *this;}
112 R & operator[](int i) {return (&x)[i];} // la coordonnee i
113 R2 orthogonal() {return R2(-y,x);} //le vecteur orthogonal dans R2
114 friend R2 operator*(R c,R2 P) {return P*c;} // produit a gauche c*P
122 friend std::ostream& operator << (std::ostream& f, const R3 & P)
123 { f << P.x << ' ' << P.y << ' ' << P.z ; return f; }
124 friend std::istream& operator >> (std::istream& f, R3 & P)
125 { f >> P.x >> P.y >> P.z ; return f; }
127 friend std::ostream& operator << (std::ostream& f, const R3 * P)
128 { f << P->x << ' ' << P->y << ' ' << P->z ; return f; }
129 friend std::istream& operator >> (std::istream& f, R3 * P)
130 { f >> P->x >> P->y >> P->z ; return f; }
133 R x,y,z; //les 3 coordonnees
135 R3 () :x(0),y(0),z(0) {} //les constructeurs
136 R3 (R a,R b,R c):x(a),y(b),z(c) {} //Point ou Vecteur (a,b,c)
137 R3 (R3 A,R3 B):x(B.x-A.x),y(B.y-A.y),z(B.z-A.z) {} //Vecteur AB
139 R3 (gp_Pnt P) : x(P.X()), y(P.Y()), z(P.Z()) {} //Point d'OpenCascade
140 R3 (gp_Vec V) : x(V.X()), y(V.Y()), z(V.Z()) {} //Vecteur d'OpenCascade
141 R3 (gp_Dir P) : x(P.X()), y(P.Y()), z(P.Z()) {} //Direction d'OpenCascade
143 R3 operator+(R3 P)const {return R3(x+P.x,y+P.y,z+P.z);}
144 R3 operator+=(R3 P) {x += P.x; y += P.y; z += P.z; return *this;}
145 R3 operator-(R3 P)const {return R3(x-P.x,y-P.y,z-P.z);}
146 R3 operator-=(R3 P) {x -= P.x; y -= P.y; z -= P.z; return *this;}
147 R3 operator-()const {return R3(-x,-y,-z);}
148 R3 operator+()const {return *this;}
149 R operator,(R3 P)const {return x*P.x+y*P.y+z*P.z;} // produit scalaire
150 R3 operator^(R3 P)const {return R3(y*P.z-z*P.y ,P.x*z-x*P.z, x*P.y-y*P.x);} // produit vectoriel
151 R3 operator*(R c)const {return R3(x*c,y*c,z*c);}
152 R3 operator*=(R c) {x *= c; y *= c; z *= c; return *this;}
153 R3 operator/(R c)const {return R3(x/c,y/c,z/c);}
154 R3 operator/=(R c) {x /= c; y /= c; z /= c; return *this;}
155 R & operator[](int i) {return (&x)[i];}
156 friend R3 operator*(R c,R3 P) {return P*c;}
158 R3 operator=(gp_Pnt P) {return R3(P.X(),P.Y(),P.Z());}
159 R3 operator=(gp_Dir P) {return R3(P.X(),P.Y(),P.Z());}
161 friend gp_Pnt gp_pnt(R3 xyz) { return gp_Pnt(xyz.x,xyz.y,xyz.z); }
162 //friend gp_Pnt operator=() { return gp_Pnt(x,y,z); }
163 friend gp_Dir gp_dir(R3 xyz) { return gp_Dir(xyz.x,xyz.y,xyz.z); }
165 bool DansPave( R3 & xyzMin, R3 & xyzMax )
166 { return xyzMin.x<=x && x<=xyzMax.x &&
167 xyzMin.y<=y && y<=xyzMax.y &&
168 xyzMin.z<=z && z<=xyzMax.z; }
175 friend std::ostream& operator <<(std::ostream& f, const R4 & P )
176 { f << P.x << ' ' << P.y << ' ' << P.z << ' ' << P.omega; return f; }
177 friend istream& operator >>(istream& f, R4 & P)
178 { f >> P.x >> P.y >> P.z >> P.omega ; return f; }
180 friend std::ostream& operator <<(std::ostream& f, const R4 * P )
181 { f << P->x << ' ' << P->y << ' ' << P->z << ' ' << P->omega; return f; }
182 friend istream& operator >>(istream& f, R4 * P)
183 { f >> P->x >> P->y >> P->z >> P->omega ; return f; }
186 R omega; //la donnee du poids supplementaire
188 R4 () :omega(1.0) {} //les constructeurs
189 R4 (R a,R b,R c,R d):R3(a,b,c),omega(d) {}
190 R4 (R4 A,R4 B) :R3(B.x-A.x,B.y-A.y,B.z-A.z),omega(B.omega-A.omega) {}
192 R4 operator+(R4 P)const {return R4(x+P.x,y+P.y,z+P.z,omega+P.omega);}
193 R4 operator+=(R4 P) {x += P.x;y += P.y;z += P.z;omega += P.omega;return *this;}
194 R4 operator-(R4 P)const {return R4(x-P.x,y-P.y,z-P.z,omega-P.omega);}
195 R4 operator-=(R4 P) {x -= P.x;y -= P.y;z -= P.z;omega -= P.omega;return *this;}
196 R4 operator-()const {return R4(-x,-y,-z,-omega);}
197 R4 operator+()const {return *this;}
198 R operator,(R4 P)const {return x*P.x+y*P.y+z*P.z+omega*P.omega;} // produit scalaire
199 R4 operator*(R c)const {return R4(x*c,y*c,z*c,omega*c);}
200 R4 operator*=(R c) {x *= c; y *= c; z *= c; omega *= c; return *this;}
201 R4 operator/(R c)const {return R4(x/c,y/c,z/c,omega/c);}
202 R4 operator/=(R c) {x /= c; y /= c; z /= c; omega /= c; return *this;}
203 R & operator[](int i) {return (&x)[i];}
204 friend R4 operator*(R c,R4 P) {return P*c;}
207 //quelques fonctions supplementaires sur ces classes
208 //==================================================
209 inline R Aire2d(const R2 A,const R2 B,const R2 C){return (B-A)^(C-A);}
210 inline R Angle2d(R2 P){ return atan2(P.y,P.x);}
212 inline R Norme2_2(const R2 & A){ return (A,A);}
213 inline R Norme2(const R2 & A){ return sqrt((A,A));}
214 inline R NormeInfinie(const R2 & A){return Max(Abs(A.x),Abs(A.y));}
216 inline R Norme2_2(const R3 & A){ return (A,A);}
217 inline R Norme2(const R3 & A){ return sqrt((A,A));}
218 inline R NormeInfinie(const R3 & A){return Max(Abs(A.x),Abs(A.y),Abs(A.z));}
220 inline R Norme2_2(const R4 & A){ return (A,A);}
221 inline R Norme2(const R4 & A){ return sqrt((A,A));}
222 inline R NormeInfinie(const R4 & A){return Max(Abs(A.x),Abs(A.y),Abs(A.z),Abs(A.omega));}
224 inline R2 XY(R3 P) {return R2(P.x, P.y);} //restriction a R2 d'un R3 par perte de z
225 inline R3 Min(R3 P, R3 Q)
226 {return R3(P.x<Q.x ? P.x : Q.x, P.y<Q.y ? P.y : Q.y, P.z<Q.z ? P.z : Q.z);} //Pt de xyz Min
227 inline R3 Max(R3 P, R3 Q)
228 {return R3(P.x>Q.x ? P.x : Q.x, P.y>Q.y ? P.y : Q.y, P.z>Q.z ? P.z : Q.z);} //Pt de xyz Max