Changeset 1020
- Timestamp:
- 07/24/20 21:44:10 (4 years ago)
- Location:
- cpp/frams/util
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/util/3d.cpp
r375 r1020 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 15Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 6 6 #include <common/log.h> 7 7 #include "3d.h" 8 #include <algorithm> // std::min(), std::max() 8 9 9 10 Pt3D operator+(const Pt3D &p1, const Pt3D &p2) { return Pt3D(p1.x + p2.x, p1.y + p2.y, p1.z + p2.z); } … … 16 17 double Pt3D::operator()() const 17 18 { 18 double q = x *x + y*y + z*z;19 double q = x * x + y * y + z * z; 19 20 if (q < 0) { if (report_errors) logPrintf("Pt3D", "operator()", LOG_ERROR, "sqrt(%g): domain error", q); return 0; } 20 21 return sqrt(q); … … 34 35 double dy = y - p.y; 35 36 double dz = z - p.z; 36 return sqrt(dx *dx + dy*dy + dz*dz);37 return sqrt(dx * dx + dy * dy + dz * dz); 37 38 } 38 39 … … 48 49 { 49 50 double s = sin(k), c = cos(k); 50 double t = c *x - s*y;51 y = s *x + c*y;51 double t = c * x - s * y; 52 y = s * x + c * y; 52 53 x = t; 53 54 } … … 55 56 void rotate2D(double s, double c, double &x, double &y) 56 57 { 57 double t = c *x - s*y;58 y = s *x + c*y;58 double t = c * x - s * y; 59 y = s * x + c * y; 59 60 x = t; 60 61 } … … 105 106 } 106 107 108 double Pt3D::minComponent() const 109 { 110 return std::min(x, std::min(y, z)); 111 } 112 113 double Pt3D::maxComponent() const 114 { 115 return std::max(x, std::max(y, z)); 116 } 117 107 118 void Pt3D::vectorProduct(const Pt3D& a, const Pt3D& b) 108 119 { 109 x = a.y *b.z - a.z*b.y;110 y = a.z *b.x - a.x*b.z;111 z = a.x *b.y - a.y*b.x;120 x = a.y * b.z - a.z * b.y; 121 y = a.z * b.x - a.x * b.z; 122 z = a.x * b.y - a.y * b.x; 112 123 } 113 124 … … 146 157 double d2(double x, double y) 147 158 { 148 double q = x *x + y*y;159 double q = x * x + y * y; 149 160 if (q < 0) { if (Pt3D::report_errors) logPrintf("", "d2()", LOG_ERROR, "sqrt(%g): domain error", q); return 0; } 150 161 return sqrt(q); … … 192 203 void Orient::transform(Pt3D& target, const Pt3D &s) const 193 204 { 194 target.x = s.x *x.x + s.y*y.x + s.z*z.x;195 target.y = s.x *x.y + s.y*y.y + s.z*z.y;196 target.z = s.x *x.z + s.y*y.z + s.z*z.z;205 target.x = s.x * x.x + s.y * y.x + s.z * z.x; 206 target.y = s.x * x.y + s.y * y.y + s.z * z.y; 207 target.z = s.x * x.z + s.y * y.z + s.z * z.z; 197 208 } 198 209 199 210 void Orient::revTransform(Pt3D& target, const Pt3D &s) const 200 211 { 201 target.x = s.x *x.x + s.y*x.y + s.z*x.z;202 target.y = s.x *y.x + s.y*y.y + s.z*y.z;203 target.z = s.x *z.x + s.y*z.y + s.z*z.z;212 target.x = s.x * x.x + s.y * x.y + s.z * x.z; 213 target.y = s.x * y.x + s.y * y.y + s.z * y.z; 214 target.z = s.x * z.x + s.y * z.y + s.z * z.z; 204 215 } 205 216 -
cpp/frams/util/3d.h
r305 r1020 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 15Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 29 29 Pt3D() {} ///< coords will be not initialized! 30 30 Pt3D(const Pt3D &p) :x(p.x), y(p.y), z(p.z) {} ///< copy from another point 31 bool operator==(const Pt3D& p) 32 void operator+=(const Pt3D& p) 33 void operator-=(const Pt3D& p) 34 void operator*=(double d) 35 Pt3D 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); }36 void operator/=(double d) 31 bool operator==(const Pt3D& p) { return (x == p.x) && (y == p.y) && (z == p.z); } 32 void operator+=(const Pt3D& p) { x += p.x; y += p.y; z += p.z; } 33 void operator-=(const Pt3D& p) { x -= p.x; y -= p.y; z -= p.z; } 34 void operator*=(double d) { x *= d; y *= d; z *= d; } 35 Pt3D 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); } 36 void operator/=(double d) { x /= d; y /= d; z /= d; } 37 37 //Pt3D operator+(const Pt3D& p) const {return Pt3D(x+p.x,y+p.y,z+p.z);} 38 38 //Pt3D operator-(const Pt3D& p) const {return Pt3D(x-p.x,y-p.y,z-p.z);} 39 39 Pt3D operator-() const { return Pt3D(-x, -y, -z); } 40 Pt3D operator*(double d) const { return Pt3D(x *d, y*d, z*d); }40 Pt3D operator*(double d) const { return Pt3D(x * d, y * d, z * d); } 41 41 Pt3D operator/(double d) const { return Pt3D(x / d, y / d, z / d); } 42 bool allCoordsLowerThan(const Pt3D& p) const { return (x < p.x) && (y < p.y) && (z <p.z); }43 bool allCoordsHigherThan(const Pt3D& p) const { return (x >p.x) && (y > p.y) && (z > p.z); }42 bool allCoordsLowerThan(const Pt3D& p) const { return (x < p.x) && (y < p.y) && (z < p.z); } 43 bool allCoordsHigherThan(const Pt3D& p) const { return (x > p.x) && (y > p.y) && (z > p.z); } 44 44 void getMin(const Pt3D& p); 45 45 void getMax(const Pt3D& p); 46 double minComponent() const; 47 double maxComponent() const; 46 48 /** vector length = \f$\sqrt{x^2+y^2+z^2}\f$ */ 47 49 double operator()() const; 48 50 /** vector length = \f$\sqrt{x^2+y^2+z^2}\f$ */ 49 51 double length() const { return operator()(); } 50 double length2() const { return x *x + y*y + z*z; }52 double length2() const { return x * x + y * y + z * z; } 51 53 double distanceTo(const Pt3D& p) const; 52 54 double manhattanDistanceTo(const Pt3D& p) const; … … 56 58 void getAngles(const Pt3D& X, const Pt3D& dir); 57 59 void vectorProduct(const Pt3D& a, const Pt3D& b); 58 Pt3D vectorProduct(const Pt3D& p) const { return (*this) *p; }59 Pt3D entrywiseProduct(const Pt3D &p) const { return Pt3D(x *p.x, y*p.y, z*p.z); } ///< also known as Hadamard product or Schur product60 double dotProduct(const Pt3D& p) const { return x *p.x + y*p.y + z*p.z; }60 Pt3D vectorProduct(const Pt3D& p) const { return (*this) * p; } 61 Pt3D entrywiseProduct(const Pt3D &p) const { return Pt3D(x * p.x, y * p.y, z * p.z); } ///< also known as Hadamard product or Schur product 62 double dotProduct(const Pt3D& p) const { return x * p.x + y * p.y + z * p.z; } 61 63 bool normalize(); 62 64 };
Note: See TracChangeset
for help on using the changeset viewer.