Laxkit  0.0.7.1
vectors.h
1 //
2 //
3 // The Laxkit, a windowing toolkit
4 // Please consult http://laxkit.sourceforge.net about where to send any
5 // correspondence about this software.
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Library General Public License for more details.
16 //
17 // You should have received a copy of the GNU Library General Public
18 // License along with this library; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 //
21 // Copyright (C) 2004-2009,2012-2013 by Tom Lechner
22 //
23 #ifndef _LAX_VECTORS_H
24 #define _LAX_VECTORS_H
25 
26 #include <cmath>
27 
28 //namespace LaxMath {
29 
30 int vector_error();
31 
32 enum PointInfoTags {
33  LINE_Start =(1<<0),
34  LINE_Vertex =(1<<1),
35  LINE_Bez =(1<<2),
36  LINE_Closed =(1<<3),
37  LINE_Open =(1<<4),
38  LINE_End =(1<<5)
39 };
40 
42 {
43  public:
44  double x,y,z;
45  int info;
46  spacevector(void) {x=y=z=0; info=0; }
47  spacevector(double xx, double yy, double zz) {x=xx; y=yy; z=zz; info=0;}
48  spacevector(double *v) { x=v[0]; y=v[1]; z=v[2]; info=0; }
49  spacevector(const spacevector &vec) { x=vec.x; y=vec.y; z=vec.z; info=vec.info; }
51  void normalize();
52  void set(double xx,double yy,double zz) { x=xx; y=yy; z=zz; }
53 
54  bool isZero() const { return x==0 && y==0 && z==0; }
55  double norm() const { return sqrt(x*x+y*y+z*z); }
56 };
57 
64 spacevector operator*(double r, spacevector a); /*const times spacevector */
65 spacevector operator*(spacevector a, double r); /*const times spacevector */
66 spacevector operator*=(spacevector &a, double r); /*const times spacevector */
67 spacevector operator/(spacevector a,spacevector b); /* cross product */
69 spacevector operator/(spacevector a,double r); /* divide y double */
71 double operator*(spacevector a,spacevector b); /*dot product */
72 spacevector operator||(spacevector a, spacevector b); /*gives part of a parallel to b */
73 spacevector operator|=(spacevector a, spacevector b); /*gives part of a perp. to b */
74 int isvector(spacevector a);
77 double norm(spacevector p1);
78 double norm2(spacevector p1);
79 typedef spacevector spacepoint;
80 
82 {
83  public:
84  double x,y;
85  int info;
86  flatvector(void) {x=y=0; info=0; }
87  flatvector(double xx, double yy) { x=xx; y=yy; info=0; }
88  flatvector(double xx, double yy, int ninfo) { x=xx; y=yy; info=ninfo; }
89  flatvector(double *v) { x=v[0]; y=v[1]; info=0; }
90  flatvector(const flatvector &vec) { x=vec.x; y=vec.y; info=vec.info; }
92  void normalize();
93  void set(double xx,double yy) { x=xx; y=yy; }
94 
95  bool isZero() const { return x==0 && y==0; }
96  double angle() const { return atan2(y,x); }
97  double norm() const { return sqrt(x*x+y*y); }
98 };
99 
101 int operator==(flatvector v1, flatvector v2);
102 int operator!=(flatvector v1, flatvector v2);
107 flatvector operator*(double r, flatvector a); /*const times flatvector */
108 flatvector operator*(flatvector a, double r); /*const times flatvector */
109 flatvector operator*=(flatvector &a, double r); /*const times flatvector */
110 flatvector operator/(flatvector a,double r); /*flatvector by const */
111 flatvector operator/=(flatvector &a,double r); /*flatvector by const */
112 double operator*(flatvector a,flatvector b); /*dot product */
113 flatvector operator||(flatvector a, flatvector b); /*gives part of x parallel to y*/
114 flatvector operator|=(flatvector a, flatvector b); /*gives part of x*/
115 double distparallel(flatvector a, flatvector b); /*gives part of a parallel to b*/
116 int isnotvector(flatvector v);
117 int areparallel(flatvector v1, flatvector v2);
118 double norm(flatvector p1);
119 double norm2(flatvector p1);
120 int point_is_in(flatvector p,flatvector *points, int n);
121 typedef flatvector flatpoint;
122 
123 class Basis
124 {
125  public:
126  spacepoint p;
127  spacevector x,y,z;
128  Basis();
129  Basis(spacepoint p1, spacepoint p2, spacepoint p3); /* p=p1, z=p2-p1, x->p3 */
130  Basis(spacepoint pp, spacepoint xx, spacepoint yy, spacepoint zz);
131  Basis(double *np,double *nx,double *ny,double*nz);
132  void Set(spacepoint p1, spacepoint p2, spacepoint p3);
133  spacepoint transformFrom(spacepoint &v);
134  spacepoint transformTo(spacepoint &v);
135 };
136 
138 {
139  public:
140  spacepoint p;
141  spacevector v;
142  spaceline() {p=spacepoint(); v=spacevector();}
143  spaceline(spacepoint p1, spacepoint p2) { p=p1; v=p1-p2; }
144  spaceline(double *v1,double *v2) { p=spacevector(v1); v=spacevector(v2); }
145 };
146 
147 class Plane
148 {
149  public:
150  spacepoint p;
151  spacevector n;
152  Plane() { p=spacepoint(); n=spacevector(); }
153  Plane(spacepoint p1,spacepoint p2,spacepoint p3)
154  { p=p3-p2; spacevector t=p1-p2; n=p/t; p=p2; }
155  Plane(spacepoint p1, spacevector n1) { p=p1; n=n1; }
156  Plane(spaceline l, spacevector v) { p=l.p; n=l.v/v; }
157  Plane(double *v1,double *v2) { p=spacevector(v1); n=spacevector(v2); }
158 };
159 
160 int operator==(Basis b1,Basis b2);
161 double distance(spacepoint p, spaceline l);
162 double distance(spacepoint p, Plane pln);
163 int issameplane(Plane p1,Plane p2);
164 spacepoint operator*(spaceline l1, spaceline l2);
165 spacepoint intersection(spaceline l1, spaceline l2, int &err); /* 2=skew, 1=parallel, -1=same line, 0=ok */
167 spaceline intersection(Plane p1, Plane p2, int &err);
168 spacepoint operator*(spaceline l, Plane p);
169 spacepoint intersection(spaceline l, Plane pl, int &err);
171 Plane linesplane(spaceline l1, spaceline l2, int &err); /* same line=-1 */
172 void normalize(flatvector &v);
173 void normalize(spacevector &v);
174 void normalize(Basis &b);
175 double angle(spacevector p1,spacevector p2,int dec=0);
176 double angle(flatvector p1,flatvector p2,int dec=0);
177 double angle_full(flatvector p1,flatvector p2,int dec=0);
178 double angle2(flatvector p1,flatvector p2,int dec=0);
179 class flatline
180 {
181  public:
182  flatpoint p;
183  flatvector v;
184  flatline() {p=flatpoint(); v=flatvector(1,0);}
185  flatline(flatline &fl) {p=fl.p; v=fl.v;}
186  flatline(flatpoint p1, flatpoint p2) { p=p1; v=p2-p1; }
187  flatline(double *v1,double *v2) { p=flatvector(v1); v=flatvector(v2); }
188 };
189 
190 double distance(spacepoint p1,spacepoint p2);
191 double distance(flatpoint p1,flatpoint p2);
192 double distance(flatpoint p, flatpoint p1, flatpoint p2);
193 double distance(flatpoint p, flatline l);
194 flatpoint operator*(flatline l1, flatline l2);
195 int intersection(flatline l1, flatline l2, flatpoint &p);
196 int intersection(flatline l1, flatline l2, flatpoint *p, double *index1, double *index2);
197 double findindex(flatpoint p,flatline l); /* p=lp+t*lv, find t,uses p-l.p||l.v */
198 int segmentandline(flatpoint p1,flatpoint p2,flatline l,flatpoint &p);
199 int segmentcross(flatpoint a1,flatpoint a2,flatpoint b1,flatpoint b2,flatpoint &p);
201 flatpoint flatten(spacepoint pnt, Basis bas);
202 flatpoint rotate(flatpoint p, double ang, int dec=0);
203 flatpoint rotate(flatpoint p, flatpoint orig, double ang, int dec=0);
204 spacepoint rotate(spacepoint p, spaceline axis, double ang, int dec=0);
205 void rotate(Basis &b, char w, double ang, int dec=0);
206 spacevector rotate(spacevector v, spacevector axis, double ang, int dec=0);
207 void rotate(Basis &b, spacevector w); /* ||w||=ang,w=axis */
208 void rotate(Basis &b, spacevector w, double ang);
209 void transform(flatline &l,Basis b);
210 void transform(spaceline &l,Basis b);
211 void transform(spacevector &v,Basis b);
212 spacepoint invert(spacepoint p, spacepoint orig);
213 
214 //} //namespace LaxMath
215 
216 #endif
217 

Mon Feb 17 2014 11:52:57, Laxkit