[5] | 1 | // This file is a part of Framsticks GDK library. |
---|
| 2 | // Copyright (C) 2002-2006 Szymon Ulatowski. See LICENSE.txt for details. |
---|
| 3 | // Refer to http://www.frams.alife.pl/ for further information. |
---|
| 4 | |
---|
| 5 | #include <math.h> |
---|
| 6 | #include <string.h> |
---|
| 7 | #include "nonstd.h" |
---|
| 8 | #include "3d.h" |
---|
| 9 | #include "framsg.h" |
---|
| 10 | |
---|
| 11 | Pt3D operator+(const Pt3D &p1,const Pt3D &p2) {return Pt3D(p1.x+p2.x,p1.y+p2.y,p1.z+p2.z);} |
---|
| 12 | Pt3D operator-(const Pt3D &p1,const Pt3D &p2) {return Pt3D(p1.x-p2.x,p1.y-p2.y,p1.z-p2.z);} |
---|
| 13 | |
---|
| 14 | Pt3D Pt3D_0(0,0,0); |
---|
| 15 | |
---|
| 16 | bool Pt3D::report_errors=true; |
---|
| 17 | |
---|
| 18 | double Pt3D::operator()() const |
---|
| 19 | { |
---|
| 20 | double q=x*x+y*y+z*z; |
---|
| 21 | if (q<0) {if (report_errors) FramMessage("Pt3D","operator()","sqrt domain error",3); return 0;} |
---|
| 22 | return sqrt(q); |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | bool Pt3D::normalize() |
---|
| 26 | { |
---|
| 27 | double len=length(); |
---|
| 28 | if (fabs(len)<1e-50) {if (report_errors) FramMessage("Pt3D","normalize()","vector too small",1); x=1;y=0;z=0; return false;} |
---|
| 29 | operator/=(len); |
---|
| 30 | return true; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | double Pt3D::distanceTo(const Pt3D& p) const |
---|
| 34 | { |
---|
| 35 | return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)+(z-p.z)*(z-p.z)); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | double Pt3D::manhattanDistanceTo(const Pt3D& p) const |
---|
| 39 | { |
---|
| 40 | return fabs(x-p.x)+fabs(y-p.y)+fabs(z-p.z); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | Orient Orient_1(Pt3D(1,0,0),Pt3D(0,1,0),Pt3D(0,0,1)); |
---|
| 44 | |
---|
| 45 | // prosty obrot |
---|
| 46 | void rotate2D(double k,double &x,double &y) |
---|
| 47 | {double s=sin(k),c=cos(k); |
---|
| 48 | double t=c*x-s*y; y=s*x+c*y; x=t;} |
---|
| 49 | |
---|
| 50 | void rotate2D(double s,double c,double &x,double &y) |
---|
| 51 | {double t=c*x-s*y; y=s*x+c*y; x=t;} |
---|
| 52 | |
---|
| 53 | int Pt3D::getAngle(double dx,double dy,double &wyn) |
---|
| 54 | { |
---|
| 55 | if ((fabs(dx)+fabs(dy))<0.0001) return 0; |
---|
| 56 | wyn=atan2(dy,dx); |
---|
| 57 | return 1; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | void Pt3D::getAngles(const Pt3D& X,const Pt3D& dir) |
---|
| 61 | { |
---|
| 62 | Pt3D t1(X), t2(dir); |
---|
| 63 | if (getAngle(t1.x,t1.y,z)) |
---|
| 64 | { // nie pionowo |
---|
| 65 | rotate2D(-z,t1.x,t1.y); |
---|
| 66 | rotate2D(-z,t2.x,t2.y); |
---|
| 67 | getAngle(t1.x, t1.z, y); |
---|
| 68 | } |
---|
| 69 | else |
---|
| 70 | { // pionowo |
---|
| 71 | z=0; |
---|
| 72 | if (t1.z<0) |
---|
| 73 | y=-M_PI_2; // w dol |
---|
| 74 | else |
---|
| 75 | y=M_PI_2; // w gore |
---|
| 76 | } |
---|
| 77 | rotate2D(-y,t2.x,t2.z); |
---|
| 78 | if (!getAngle(t2.z,-t2.y,x)) |
---|
| 79 | x=0;// to zly wynik ale dobrego nie ma :P |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | void Pt3D::getMin(const Pt3D& p) |
---|
| 83 | { |
---|
| 84 | if (p.x<x) x=p.x; |
---|
| 85 | if (p.y<y) y=p.y; |
---|
| 86 | if (p.z<z) z=p.z; |
---|
| 87 | } |
---|
| 88 | void Pt3D::getMax(const Pt3D& p) |
---|
| 89 | { |
---|
| 90 | if (p.x>x) x=p.x; |
---|
| 91 | if (p.y>y) y=p.y; |
---|
| 92 | if (p.z>z) z=p.z; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | void Pt3D::vectorProduct(const Pt3D& a,const Pt3D& b) |
---|
| 96 | { |
---|
| 97 | x=a.y*b.z-a.z*b.y; |
---|
| 98 | y=a.z*b.x-a.x*b.z; |
---|
| 99 | z=a.x*b.y-a.y*b.x; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | void Orient::lookAt(const Pt3D& X,const Pt3D& dir) |
---|
| 103 | { |
---|
| 104 | x=X; x.normalize(); |
---|
| 105 | y.vectorProduct(dir,x); |
---|
| 106 | z.vectorProduct(x,y); |
---|
| 107 | if ((!y.normalize()) || (!z.normalize())) |
---|
| 108 | { |
---|
| 109 | y.x=dir.y; y.y=dir.z; y.z=dir.x; |
---|
| 110 | z.vectorProduct(x,y); |
---|
| 111 | z.normalize(); |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | // odleglosc 2d |
---|
| 116 | double d2(double x,double y) |
---|
| 117 | { |
---|
| 118 | double q=x*x+y*y; |
---|
| 119 | if (q<0) {if (Pt3D::report_errors) FramMessage("","d2()","sqrt domain error",3); return 0;} |
---|
| 120 | return sqrt(q); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | Orient::Orient(const Matrix44& m) |
---|
| 124 | { |
---|
| 125 | x.x=m[0]; x.y=m[1]; x.z=m[2]; |
---|
| 126 | y.x=m[4]; y.y=m[5]; y.z=m[6]; |
---|
| 127 | z.x=m[8]; z.y=m[9]; z.z=m[10]; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | void Orient::operator=(const Pt3D &rot) |
---|
| 131 | { |
---|
| 132 | *this=Orient_1; |
---|
| 133 | rotate(rot); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | void Orient::rotate(const Pt3D &v) |
---|
| 137 | { |
---|
| 138 | double s,c; |
---|
| 139 | if (fabs(v.x)>0.0001) |
---|
| 140 | { |
---|
| 141 | s=sin(v.x); c=cos(v.x); |
---|
| 142 | rotate2D(s,c,x.y,x.z); |
---|
| 143 | rotate2D(s,c,y.y,y.z); |
---|
| 144 | rotate2D(s,c,z.y,z.z); |
---|
| 145 | } |
---|
| 146 | if (fabs(v.y)>0.0001) |
---|
| 147 | { |
---|
| 148 | s=sin(v.y); c=cos(v.y); |
---|
| 149 | rotate2D(s,c,x.x,x.z); |
---|
| 150 | rotate2D(s,c,y.x,y.z); |
---|
| 151 | rotate2D(s,c,z.x,z.z); |
---|
| 152 | } |
---|
| 153 | if (fabs(v.z)>0.0001) |
---|
| 154 | { |
---|
| 155 | s=sin(v.z); c=cos(v.z); |
---|
| 156 | rotate2D(s,c,x.x,x.y); |
---|
| 157 | rotate2D(s,c,y.x,y.y); |
---|
| 158 | rotate2D(s,c,z.x,z.y); |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | void Orient::transform(Pt3D& target,const Pt3D &s) const |
---|
| 163 | { |
---|
| 164 | target.x=s.x*x.x+s.y*y.x+s.z*z.x; |
---|
| 165 | target.y=s.x*x.y+s.y*y.y+s.z*z.y; |
---|
| 166 | target.z=s.x*x.z+s.y*y.z+s.z*z.z; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | void Orient::revTransform(Pt3D& target,const Pt3D &s) const |
---|
| 170 | { |
---|
| 171 | target.x=s.x*x.x+s.y*x.y+s.z*x.z; |
---|
| 172 | target.y=s.x*y.x+s.y*y.y+s.z*y.z; |
---|
| 173 | target.z=s.x*z.x+s.y*z.y+s.z*z.z; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | void Orient::transform(Orient& target,const Orient& src) const |
---|
| 177 | { |
---|
| 178 | transform(target.x,src.x); |
---|
| 179 | transform(target.y,src.y); |
---|
| 180 | transform(target.z,src.z); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | void Orient::revTransform(Orient& target,const Orient& src) const |
---|
| 184 | { |
---|
| 185 | revTransform(target.x,src.x); |
---|
| 186 | revTransform(target.y,src.y); |
---|
| 187 | revTransform(target.z,src.z); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | void Orient::getAngles(Pt3D &angles) const |
---|
| 191 | { |
---|
| 192 | angles.getAngles(x,z); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | bool Orient::normalize() |
---|
| 196 | { |
---|
| 197 | bool ret=1; |
---|
| 198 | y.vectorProduct(z,x); |
---|
| 199 | z.vectorProduct(x,y); |
---|
| 200 | if (!x.normalize()) ret=0; |
---|
| 201 | if (!z.normalize()) ret=0; |
---|
| 202 | if (!y.normalize()) ret=0; |
---|
| 203 | return ret; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | Matrix44::Matrix44(const Orient &rot) |
---|
| 207 | { |
---|
| 208 | m[0]=rot.x.x; m[1]=rot.x.y; m[2]=rot.x.z; m[3]=0; |
---|
| 209 | m[4]=rot.y.x; m[5]=rot.y.y; m[6]=rot.y.z; m[7]=0; |
---|
| 210 | m[8]=rot.z.x; m[9]=rot.z.y; m[10]=rot.z.z; m[11]=0; |
---|
| 211 | m[12]=0; m[13]=0; m[14]=0; m[15]=1; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | void Matrix44::operator+=(const Pt3D &) |
---|
| 215 | { |
---|
| 216 | |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | void Matrix44::operator*=(const Pt3D &) |
---|
| 220 | { |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | void Matrix44::operator*=(double sc) |
---|
| 224 | { |
---|
| 225 | } |
---|