diff options
author | Karl Berry <karl@freefriends.org> | 2009-05-16 00:19:13 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2009-05-16 00:19:13 +0000 |
commit | bab45528d65eaafe68a705dbb2a57075c7b7cbd8 (patch) | |
tree | 10b4ae2b5195c8dede153ab89359ec00f55f325f /Build/source/utils/asymptote/triple.h | |
parent | 8643d90372e9c31e0f461c15c596b60a545bd7d3 (diff) |
asymptote 1.72 sources (not integrated into build yet)
git-svn-id: svn://tug.org/texlive/trunk@13110 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Build/source/utils/asymptote/triple.h')
-rw-r--r-- | Build/source/utils/asymptote/triple.h | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/Build/source/utils/asymptote/triple.h b/Build/source/utils/asymptote/triple.h new file mode 100644 index 00000000000..e1ea5a41e4d --- /dev/null +++ b/Build/source/utils/asymptote/triple.h @@ -0,0 +1,185 @@ +/***** + * triple.h + * John Bowman + * + * Stores a three-dimensional point. + * + *****/ + +#ifndef TRIPLE_H +#define TRIPLE_H + +#include <cassert> +#include <iostream> +#include <cmath> + +#include "common.h" +#include "angle.h" + +namespace camp { + +typedef double Triple[3]; + +class triple : virtual public gc { + double x; + double y; + double z; + +public: + triple() : x(0.0), y(0.0), z(0.0) {} + triple(double x, double y=0.0, double z=0.0) : x(x), y(y), z(z) {} + triple(const Triple& v) : x(v[0]), y(v[1]), z(v[2]) {} + + virtual ~triple() {} + + double getx() const { return x; } + double gety() const { return y; } + double getz() const { return z; } + + friend triple operator+ (const triple& z, const triple& w) + { + return triple(z.x + w.x, z.y + w.y, z.z + w.z); + } + + friend triple operator- (const triple& z, const triple& w) + { + return triple(z.x - w.x, z.y - w.y, z.z - w.z); + } + + friend triple operator- (const triple& z) + { + return triple(-z.x, -z.y, -z.z); + } + + friend triple operator* (double s, const triple& z) + { + return triple(s*z.x, s*z.y, s*z.z); + } + + friend triple operator* (const triple& z, double s) + { + return triple(z.x*s, z.y*s, z.z*s); + } + + friend triple operator/ (const triple& z, double s) + { + if (s == 0.0) + reportError("division by 0"); + s=1.0/s; + return triple(z.x*s, z.y*s, z.z*s); + } + + const triple& operator+= (const triple& w) + { + x += w.x; + y += w.y; + z += w.z; + return *this; + } + + const triple& operator-= (const triple& w) + { + x -= w.x; + y -= w.y; + z -= w.z; + return *this; + } + + friend bool operator== (const triple& z, const triple& w) + { + return z.x == w.x && z.y == w.y && z.z == w.z; + } + + friend bool operator!= (const triple& z, const triple& w) + { + return z.x != w.x || z.y != w.y || z.z != w.z; + } + + double abs2() const + { + return x*x+y*y+z*z; + } + + double length() const /* r */ + { + return sqrt(abs2()); + } + + friend double length(const triple& v) + { + return v.length(); + } + + double polar() const /* theta */ + { + double r=length(); + if (r == 0.0) + reportError("taking polar angle of (0,0,0)"); + return acos(z/r); + } + + double azimuth() const /* phi */ + { + return angle(x,y); + } + + friend triple unit(const triple& v) + { + double scale=v.length(); + if(scale != 0.0) scale=1.0/scale; + return triple(v.x*scale,v.y*scale,v.z*scale); + } + + friend double dot(const triple& u, const triple& v) + { + return u.getx()*v.getx()+u.gety()*v.gety()+u.getz()*v.getz(); + } + + friend triple cross(const triple& u, const triple& v) + { + return triple(u.gety()*v.getz()-u.getz()*v.gety(), + u.getz()*v.getx()-u.getx()*v.getz(), + u.getx()*v.gety()-v.getx()*u.gety()); + } + + // Returns a unit triple in the direction (theta,phi), in radians. + friend triple expi(double theta, double phi) + { + double sintheta=sin(theta); + return triple(sintheta*cos(phi),sintheta*sin(phi),cos(theta)); + } + + friend istream& operator >> (istream& s, triple& z) + { + char c; + s >> std::ws; + bool paren=s.peek() == '('; // parenthesis are optional + if(paren) s >> c; + s >> z.x >> std::ws; + if(s.peek() == ',') s >> c >> z.y; + else z.y=0.0; + if(s.peek() == ',') s >> c >> z.z; + else z.z=0.0; + if(paren) { + s >> std::ws; + if(s.peek() == ')') s >> c; + } + + return s; + } + + friend ostream& operator << (ostream& out, const triple& z) + { + out << "(" << z.x << "," << z.y << "," << z.z << ")"; + return out; + } + +}; + +triple expi(double theta, double phi); + +} //namespace camp + +GC_DECLARE_PTRFREE(camp::triple); + +#endif |