Changeset 1020 for cpp/frams/util


Ignore:
Timestamp:
07/24/20 21:44:10 (4 years ago)
Author:
Maciej Komosinski
Message:

Added double Pt3D::minComponent() and maxComponent()

Location:
cpp/frams/util
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/util/3d.cpp

    r375 r1020  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    66#include <common/log.h>
    77#include "3d.h"
     8#include <algorithm> // std::min(), std::max()
    89
    910Pt3D operator+(const Pt3D &p1, const Pt3D &p2) { return Pt3D(p1.x + p2.x, p1.y + p2.y, p1.z + p2.z); }
     
    1617double Pt3D::operator()() const
    1718{
    18         double q = x*x + y*y + z*z;
     19        double q = x * x + y * y + z * z;
    1920        if (q < 0) { if (report_errors) logPrintf("Pt3D", "operator()", LOG_ERROR, "sqrt(%g): domain error", q); return 0; }
    2021        return sqrt(q);
     
    3435        double dy = y - p.y;
    3536        double dz = z - p.z;
    36         return sqrt(dx*dx + dy*dy + dz*dz);
     37        return sqrt(dx * dx + dy * dy + dz * dz);
    3738}
    3839
     
    4849{
    4950        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;
    5253        x = t;
    5354}
     
    5556void rotate2D(double s, double c, double &x, double &y)
    5657{
    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;
    5960        x = t;
    6061}
     
    105106}
    106107
     108double Pt3D::minComponent() const
     109{
     110        return std::min(x, std::min(y, z));
     111}
     112
     113double Pt3D::maxComponent() const
     114{
     115        return std::max(x, std::max(y, z));
     116}
     117
    107118void Pt3D::vectorProduct(const Pt3D& a, const Pt3D& b)
    108119{
    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;
    112123}
    113124
     
    146157double d2(double x, double y)
    147158{
    148         double q = x*x + y*y;
     159        double q = x * x + y * y;
    149160        if (q < 0) { if (Pt3D::report_errors) logPrintf("", "d2()", LOG_ERROR, "sqrt(%g): domain error", q); return 0; }
    150161        return sqrt(q);
     
    192203void Orient::transform(Pt3D& target, const Pt3D &s) const
    193204{
    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;
    197208}
    198209
    199210void Orient::revTransform(Pt3D& target, const Pt3D &s) const
    200211{
    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;
    204215}
    205216
  • cpp/frams/util/3d.h

    r305 r1020  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    2929        Pt3D() {} ///< coords will be not initialized!
    3030        Pt3D(const Pt3D &p) :x(p.x), y(p.y), z(p.z) {} ///< copy from another point
    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; }
     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; }
    3737        //Pt3D  operator+(const Pt3D& p) const {return Pt3D(x+p.x,y+p.y,z+p.z);}
    3838        //Pt3D  operator-(const Pt3D& p) const {return Pt3D(x-p.x,y-p.y,z-p.z);}
    3939        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); }
    4141        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); }
    4444        void getMin(const Pt3D& p);
    4545        void getMax(const Pt3D& p);
     46        double minComponent() const;
     47        double maxComponent() const;
    4648        /** vector length = \f$\sqrt{x^2+y^2+z^2}\f$  */
    4749        double operator()() const;
    4850        /** vector length = \f$\sqrt{x^2+y^2+z^2}\f$  */
    4951        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; }
    5153        double distanceTo(const Pt3D& p) const;
    5254        double manhattanDistanceTo(const Pt3D& p) const;
     
    5658        void getAngles(const Pt3D& X, const Pt3D& dir);
    5759        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 product
    60         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; }
    6163        bool normalize();
    6264};
Note: See TracChangeset for help on using the changeset viewer.