source: cpp/frams/util/3d.h @ 155

Last change on this file since 155 was 155, checked in by sz, 6 years ago

Pt3D: allCoordsLowerThan()/HigherThan?() functions instead of the potentially misleading operators < >

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1// This file is a part of the Framsticks GDK.
2// Copyright (C) 2002-2014  Maciej Komosinski and Szymon Ulatowski.  See LICENSE.txt for details.
3// Refer to http://www.framsticks.com/ for further information.
4
5#ifndef _3D_H_
6#define _3D_H_
7
8#ifdef SHP
9#include <string.h> //memcpy
10#else
11#include <memory.h> //memcpy
12#endif
13
14/**********************************
15\file 3d.h 3d.cpp
16
17   basic 3d classes and operators     
18*********************************/
19
20/// point in 3d space
21
22class Pt3D
23{
24public: double x,y,z;
25static bool report_errors;
26
27Pt3D(double _x,double _y,double _z):x(_x),y(_y),z(_z) {} ///< constructor initializing all coords
28Pt3D(double xyz):x(xyz),y(xyz),z(xyz) {} ///< all coords equal
29Pt3D() {} ///< coords will be not initialized!
30Pt3D(const Pt3D &p):x(p.x),y(p.y),z(p.z) {} ///< copy from another point
31bool    operator==(const Pt3D& p)       {return (x==p.x)&&(y==p.y)&&(z==p.z);}
32void    operator+=(const Pt3D& p)       {x+=p.x;y+=p.y;z+=p.z;}
33void    operator-=(const Pt3D& p)       {x-=p.x;y-=p.y;z-=p.z;}
34void    operator*=(double d)    {x*=d;y*=d;z*=d;}
35Pt3D    operator*(const Pt3D &p) const {return Pt3D(y*p.z-z*p.y, z*p.x-x*p.z, x*p.y-y*p.x);}
36void    operator/=(double d)    {x/=d; y/=d; z/=d;}
37//Pt3D  operator+(const Pt3D& p) const {return Pt3D(x+p.x,y+p.y,z+p.z);}
38//Pt3D  operator-(const Pt3D& p) const {return Pt3D(x-p.x,y-p.y,z-p.z);}
39Pt3D    operator-() const {return Pt3D(-x,-y,-z);}
40Pt3D    operator*(double d) const {return Pt3D(x*d,y*d,z*d);}
41Pt3D    operator/(double d) const {return Pt3D(x/d,y/d,z/d);}
42bool    allCoordsLowerThan(const Pt3D& p) const {return (x<p.x)&&(y<p.y)&&(z<p.z);}
43bool    allCoordsHigherThan(const Pt3D& p) const {return (x>p.x)&&(y>p.y)&&(z>p.z);}
44void getMin(const Pt3D& p);
45void getMax(const Pt3D& p);
46/** vector length = \f$\sqrt{x^2+y^2+z^2}\f$  */
47double operator()() const;
48/** vector length = \f$\sqrt{x^2+y^2+z^2}\f$  */
49double length() const {return operator()();}
50double length2() const {return x*x+y*y+z*z;}
51double distanceTo(const Pt3D& p) const;
52double manhattanDistanceTo(const Pt3D& p) const;
53/** calculate angle between (0,0)-(dx,dy), @return 1=ok, 0=can't calculate */
54static int getAngle(double dx,double dy,double &angle);
55/** calculate 3 rotation angles translating (1,0,0) into 'X' and (0,0,1) into 'dir' */
56void getAngles(const Pt3D& X,const Pt3D& dir);
57void vectorProduct(const Pt3D& a,const Pt3D& b);
58Pt3D vectorProduct(const Pt3D& p) const {return (*this)*p;}
59Pt3D entrywiseProduct(const Pt3D &p) const {return Pt3D(x*p.x,y*p.y,z*p.z);} ///< also known as Hadamard product or Schur product
60double dotProduct(const Pt3D& p) const {return x*p.x+y*p.y+z*p.z;}
61bool normalize();
62};
63Pt3D operator+(const Pt3D &p1,const Pt3D &p2);
64Pt3D operator-(const Pt3D &p1,const Pt3D &p2);
65
66class Pt3D_DontReportErrors
67{
68bool state;
69public:
70Pt3D_DontReportErrors() {state=Pt3D::report_errors; Pt3D::report_errors=false;}
71~Pt3D_DontReportErrors() {Pt3D::report_errors=state;}
72};
73
74///  orientation in 3d space = rotation matrix
75
76class Matrix44;
77
78class Orient
79{
80public: Pt3D x,y,z; ///< 3 vectors (= 3x3 matrix)
81
82        Orient() {}
83        Orient(const Orient& src) {x=src.x; y=src.y; z=src.z;}
84        Orient(const Pt3D& a,const Pt3D& b,const Pt3D& c):x(a),y(b),z(c) {}
85//      Orient(const Pt3D& rot) {*this=rot;}
86        Orient(const Matrix44& m);
87        void operator=(const Pt3D &rot);
88        void rotate(const Pt3D &); ///< rotate matrix around 3 axes
89
90        void transform(Pt3D &target,const Pt3D &src) const;     ///< transform a vector
91        void revTransform(Pt3D &target,const Pt3D &src) const;  ///< reverse transform
92        Pt3D transform(const Pt3D &src) const {Pt3D t; transform(t,src); return t;}
93        Pt3D revTransform(const Pt3D &src) const {Pt3D t; revTransform(t,src); return t;}
94
95        void transform(Orient& target,const Orient& src) const;    ///< transform other orient
96        void revTransform(Orient& target,const Orient& src) const; ///< reverse transform other orient
97        Orient transform(const Orient& src) const {Orient o; transform(o,src); return o;}    ///< transform other orient
98        Orient revTransform(const Orient& src) const {Orient o; revTransform(o,src); return o;} ///< reverse transform other orient
99
100        void transformSelf(const Orient &rot) {Orient tmp; rot.transform(tmp,*this); *this=tmp;}
101        void revTransformSelf(const Orient &rot) {Orient tmp; rot.revTransform(tmp,*this); *this=tmp;}
102
103        void getAngles(Pt3D &) const; ///< calculate rotation from current matrix
104        Pt3D getAngles() const {Pt3D ret; getAngles(ret); return ret;}; ///< calculate rotation from current matrix
105        void lookAt(const Pt3D &X,const Pt3D &dir); ///< calculate orientation matrix from 2 vectors
106
107        bool normalize();
108};
109
110class Matrix44
111{
112public:
113double m[16];
114Matrix44() {}
115Matrix44(const Matrix44& src) {memcpy(m,src.m,sizeof(m));}
116Matrix44(double *srcm) {memcpy(m,srcm,sizeof(m));}
117Matrix44(const Orient &rot);
118
119const double& operator()(int i,int j) const {return m[i+16*j];}
120const double& operator[](int i) const {return m[i];}
121double& operator()(int i,int j) {return m[i+16*j];}
122double& operator[](int i) {return m[i];}
123
124void operator+=(const Pt3D &); ///< translate matrix
125void operator*=(const Pt3D &); ///< scale matrix
126void operator*=(double sc); ///< scale matrix
127};
128
129extern Pt3D Pt3D_0; ///< zero vector
130extern Orient Orient_1; ///< standard unit matrix: 100 010 001
131extern Matrix44 Matrix44_1; ///< standard unit matrix: 1000 0100 0010 0001
132
133void rotate2D(double,double &,double &); ///< rotate 2d vector, given angle
134void rotate2D(double,double,double &,double &); ///< rotate 2d vector, given sin and cos
135double d2(double,double); ///< distance in 2D
136
137#endif
Note: See TracBrowser for help on using the repository browser.