source: git/src/vector3.h @ dfb7670

RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereostereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since dfb7670 was e4ec4b4, checked in by Olly Betts <olly@…>, 22 years ago

Added += -= and == operators for Vector3 class

git-svn-id: file:///home/survex-svn/survex/branches/survex-1_1@2442 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 2.5 KB
Line 
1//
2//  vector3.h
3//
4//  C++ class for 3-element vectors
5//
6//  Copyright (C) 2000-2002, Mark R. Shinwell.
7//  Copyright (C) 2002-2003 Olly Betts
8//
9//  This program is free software; you can redistribute it and/or modify
10//  it under the terms of the GNU General Public License as published by
11//  the Free Software Foundation; either version 2 of the License, or
12//  (at your option) any later version.
13//
14//  This program is distributed in the hope that it will be useful,
15//  but WITHOUT ANY WARRANTY; without even the implied warranty of
16//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17//  GNU General Public License for more details.
18//
19//  You should have received a copy of the GNU General Public License
20//  along with this program; if not, write to the Free Software
21//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22//
23
24#ifndef Vector3_h
25#define Vector3_h
26
27#include <stdio.h>
28#include <math.h>
29
30class Vector3 {
31    double x; //--FIXME
32    double y;
33    double z;
34
35public:
36    Vector3();
37    Vector3(double, double, double);
38    ~Vector3();
39
40#ifdef AVENPRES
41    void Save(FILE* fp) const { //--Pres: FIXME
42        fwrite(&x, sizeof(double), 1, fp);
43        fwrite(&y, sizeof(double), 1, fp);
44        fwrite(&z, sizeof(double), 1, fp);
45    }
46
47    void Load(FILE* fp) { //--Pres: FIXME
48        fread(&x, sizeof(double), 1, fp);
49        fread(&y, sizeof(double), 1, fp);
50        fread(&z, sizeof(double), 1, fp);
51    }
52#endif
53
54    double getX() const { return x; }
55    double getY() const { return y; }
56    double getZ() const { return z; }
57
58    double magnitude() const {
59        return sqrt(x*x + y*y + z*z);
60    }
61
62    void normalise();
63
64    void set(double, double, double);
65
66    friend Vector3 operator-(const Vector3& v) {
67        Vector3 o;
68        o.x = -v.x;
69        o.y = -v.y;
70        o.z = -v.z;
71        return o;
72    }
73    Vector3& operator*=(const double);
74    Vector3& operator/=(const double);
75    Vector3& operator+=(const Vector3&);
76    Vector3& operator-=(const Vector3&);
77    Vector3& operator=(const Vector3&);
78
79    friend Vector3 operator*(const double, const Vector3&);
80    friend Vector3 operator*(const Vector3& v, const double f) {
81        return f * v;
82    }
83    friend Vector3 operator*(const Vector3&, const Vector3&); // cross product
84    friend Vector3 operator+(const Vector3&, const Vector3&);
85    friend Vector3 operator-(const Vector3&, const Vector3&);
86    friend bool operator==(const Vector3&, const Vector3&);
87    friend double dot(const Vector3&, const Vector3&);
88};
89
90inline bool operator==(const Vector3& a, const Vector3& b) {
91    return a.x == b.x && a.y == b.y && a.z == b.z;
92}
93
94#endif
Note: See TracBrowser for help on using the repository browser.