Geometric midpoint (three-dimensional vector), line, surface

zhaozj2021-02-16  56

Asspoint.h

// Copyright (C) Liang Yi, 2004

// Finally modified: 2004.6. Hangzhou

#ifndef asspoint_header

#define assist_header

#include

#include

#include

#include

#include

// # Define Container Vector

#define Container Deque

Using namespace std;

#DEFINE PI 3.1415926535897932384626 // m_pi

#define ass_delta 1e-6 // OR flt_epsilon (1.192092896e-07f) or dbl_epsilon in float.h

Template

Class Asspoint

{

PUBLIC:

Typedef assepoint_myt;

Asspoint () // Constructor, origin

{

X = 0;

Y = 0;

Z = 0;

}

Asspoint (T _x, T _Y, T_Z = 0) // Constructor, X, Y, Z

{

X = _x;

Y = _y;

Z = _z;

}

Asspoint (const_myt & p) // Constructor, X, Y, Z

{

X = p.x;

Y = p.y;

Z = p.z;

}

Bool operator == (_myt & p) / / Two points are equal, coordinate error Delta

{

Return (FABS (X-P.x)}

_MYT Operator - (_myt & p) // Two points

{

Return_myt (x-p.x, y-p.y, z-p.z);

}

_MYT Operator (_myt & p) // two points plus

{

Return_myt (x p.x, y p.y, z p.z);

}

_MYT Operator - (t D) // Three-Direction minus D

{

Return_myt (x-d, y-d, z-d);

}

_MYT Operator (T D) // Triangular plus D

{

Return_myt (x D, Y D, Z D);

}

_Myt operator * (t d) // three-direction multiply multiply D

{

Return_myt (x * d, y * d, z * d);

}

_Myt operator / (t d) // three-direction simultaneously divided D

{

Return_myt (x / d, y / d, z / d);

}

T Operator * (_myt & p) // points

{

Return x * p.x y * p.y z * p.z;

}

T DIS (_MYT & P) / / to another distance

{

Return SQRT (POW (X-P.x, 2) Pow (Y-P.Y, 2) POW (Z-P.Z, 2));

}

_MYT _X (_MYT & P) // fork

{

Return_myt (y * p.z-z * p.y, z * p.x-x * p.z, x * p.y-y * p.x);

}

T modul () // Sample

{

RETURN SQRT ((* this) * (* this));

}

Bool HasthesameDirection (_myt & _p) // Whether the vector is the same

{

Return (_P / _P.MODUL ()) == ((* this) / modul ());

}

Bool isnearpt (_myt & _p, t _dis) // is in _P

{

Return Dis (_P) <= FABS (_dis);

_MYT Scale (t sx, t, t s, _myt & basepoint = _myt ()) // Zoom SX, SY, SZ separately from the basepoint, three directions

{

_Myt tmp = (* this) -baSepoint;

Return_myt (Tmp.x * SX, TMP.Y * SY, TMP.Z * SZ) Basepoint;

}

_MYT Transfrom (T TX, T Ty, T Tz) // Mobile TX, TY, TZ

{

Return_myt (x TX, Y TY, Z TZ);

}

TAngleyz () / / with YZ face angle

{

Return asin (X / MODUL ());

}

T anglezx () / / with ZX face angle

{

Return asin (Y / MODUL ());

}

T anglexy () / / with xy face angle

{

Return asin (z / modul ());

}

T anglex_xy () // xy surface projection with X-axis angle

{

IF (y> = 0)

Return ACOS (X / SQRT (POW (X, 2) Pow (Y, 2)));

RETURN PI * 2.0-ACOS (X / SQRT (Pow (x, 2) Pow (Y, 2)));

}

T angley_xy () // xy surface projection and Y-axis angle

{

IF (x <= 0)

Return ACOS (Y / SQRT (Pow (x, 2) Pow (Y, 2)));

RETURN PI * 2.0-ACOS (Y / SQRT (Pow (x, 2) Pow (Y, 2)));

}

T angley_yz () // YZ surface projection with Y-axis angle

{

IF (z> = 0)

Return ACOS (Y / SQRT (Pow (Y, 2) Pow (Z, 2)));

RETURN PI * 2.0-ACOS (Y / SQRT (Pow (Y, 2) Pow (Z, 2));

}

T anglez_yz () // YZ surface projection with Z-axis forward angle

{

IF (y <= 0)

RETURN ACOS (Z / SQRT (Pow (Y, 2) Pow (Z, 2)));

RETURN PI * 2.0-ACOS (Z / SQRT (Pow (Y, 2) POW (Z, 2)));

}

T anglez_zx () // ZX surface projection with Z-axis forward angle

{

IF (x> = 0)

RETURN ACOS (Z / SQRT (POW (Z, 2) POW (X, 2)));

RETURN PI * 2.0-ACOS (Z / SQRT (POW (Z, 2) Pow (x, 2)));

}

T anglex_zx () // ZX surface projection with X-axis forward angle

{

IF (z <= 0)

Return ACOS (X / SQRT (Pow (Z, 2) POW (X, 2)));

RETURN PI * 2.0-ACOS (X / SQRT (POW (Z, 2) Pow (x, 2)));

}

_MYT Rotatex (T Ceta) / / Rotate Ceta around X-axis

{

Return (_MYT (x, y * cos (ceta) -z * sin (ceta), z * cos (ceta) y * sin (ceta)))))

}

_MYT ROTATEY (T CETA) / / Rotate Ceta around Y-axis

{

Return (_MYT (x * cos (cos (cos (cos (cos (ceta) z * sin (ceta), y, z * cos (ceta) -x * sin (ceta)))

}

_MYT Rotatez (T Ceta) / / Rotate Ceta around Z-axis

{

Return (_MYT (x * cos (cos (cos (ceta) -y * sin (ceta), y * cos (ceta) x * sin (ceta), z));

}

T angle (_myt & other) / / with another vector angle {

Return Acos ((* this) * Other / this-> modul () / other.Modul ());

}

_MYT Rotate (T CETA, _MYT & DIR) // Rotate Ceta by a shaft of DIR around an origin

{

T a = -dir.anglez_yz ();

T b = -dir.angleyz ();

Return Rotatex (a) .rotatey (b) .rotatez (ceta) .rotatey (-b) .rotatex (-a);

}

Tx;

T;

T Z;

}

#ENDIF

Assline.h

// Copyright (C) Liang Yi, 2004

// Finally modified: 2004.6. Hangzhou

#ifndef assline_header

#define assline_header

#include "asspoint.h"

Template

Class Assline

{

PUBLIC:

ENUM INPUT_TYPE

{

Point_slope,

TWOPOINTS

}

Typedef asspoint _mypoint;

Typedef assline_myt;

Assline () //

{

}

ASSLINE (const _myt & other) // two-point line

{

P1 = other.p1;

P2 = other.p2;

}

Assline (_mypoint & _p1, _mypoint & _p2) //

{

P1 = _P1;

P2 = _P2;

}

Assline (_Mypoint & P, T A, T B, T C, Input_Type Type = _MYT :: Point_SLOPE)

{

IF (Type == _ myt :: point_slope)

{

P2 = _mypoint (p.x a, p.y b, p.z c);

}

IF (Type == _ myt :: twopoints)

{

P2 = _mypoint (a, b, c);

}

P1 = P;

}

ASSLINE (T x, t y, t z, t x2, t y2, t z2) // two points line

{

P1 = _mypoint (x, y, z);

P2 = _mypoint (x2, y2, z2);

}

_MYT & Operator = (const _myt & other)

{

P1 = other.p1;

P2 = other.p2;

Return (* this);

}

T getLength () // return length

{

Return SQRT (POW (P1.x-P2.x, 2) Pow (p1.y-p2.y, 2) POW (P1.Z-P2.Z, 2));

}

T anglex () // Parallel angle

{

RETURN ACOS ((P2-P1) .x / (p2-p1) .MODUL ());

}

T angley () //

{

RETURN ACOS ((p2-p1) .y / (p2-p1) .MODUL ());

}

T anglez () // Z-axis angle

{

RETURN ACOS ((p2-p1) .z / (p2-p1) .MODUL ());

}

T anglexy () // Pair of XY plane angle

{

Return (p2-p1) .anglexy ();

}

T anglezx () // ZX face angle

{

Return (P2-P1) .anglezx ();

}

TAngleyz () // Mouth to YZ

{

Return (p2-p1) .angleyz ();

}

Bool isline () // Length is too small is not line, Delta control

{

Return getLength ()> = ass_delta;}

Bool operator == (_myt & _l) // Whether the two lines are combined

{

Return (* this || _l) && isonline (_l.p1);

}

Bool isonline (_mypoint & p) // is online

{

Return (P-P1) .haasthesamedirection (P2-P1);

}

Bool isinlinesegment (_mypoint & p) // point in the line segment

{

Return isonline (P) && ((p-p1) .haasthesamedirection (P2-P));

}

T angle (assline & _l) / / angle with another line

{

RETURN ACOS ((P2-P1) * (_ L.P2-_L.P1) / ((p2-p1) .MODUL () * (_l.p2-_l.p1) .MODUL ()));

}

Bool isperpendicular (_MYT & _L) // is vertical with another line

{

RETURN (P2-P1) * (_ L.P2-_L.p1)

}

BOOL Operator || (_myt & _l) // Is it parallel to another?

{

_Mypoint TMP1 = P2-P1;

_Mypoint TMP2 = _l.p2-_l.p1;

Return ((TMP1 / TMP1.MODUL ()) ._x (TMP2 / TMP2.MODUL ()))))))). MODUL ()}

_MYT Parallelline (_mypoint & p) // Overpot Parallel Line

{

Return_myt (p, p2-p1 p);

}

_Myt perpendicularline (_mypoint & p) // Overpot

{

Return_myt (p, (p2-p1) * ((p-p1) * (P2-P1) / POW ((P2-P1) .MODUL (), 2)) P1);

}

Dis (_MyPoint & P) //

{

RETURN (P-P1) ._ x (p2-p1) .MODUL () / (p2-p1) .MODUL ();

}

T from (_MYT & _L) //

{

_Mypoint p = (p2-p1) ._ x (_l.p2-_l.p1);

Return Fabs ((p1-_l.p1) * p) /p.modul ();

}

_Mypoint intersection (_MYT & _L) // intersection of line

{

IF (DIS (_L)> ASS_DELTA) RETURN _MYPOINT (DBL_MAX, 0);

_Mypoint _tmp1 (p2-p1);

_Mypoint _tmp2 (_l.p2-_l.p1);

_Mypoint _tmp4 = _tmp1._x (_tmp2) ._ x (_TMP2);

_Mypoint _tmp5 = _l.p1-p1;

T t = (_tmp4 * _tmp5) / (_ TMP4 * _TMP1);

Return (P1 (_tmp1 * t));

}

_MYT Perpendicularline (Assline & _l) // Line with another line

{

IF ((* this) || _l)) Return_MYT (0, 0, 0, DBL_MAX, DBL_MAX, DBL_MAX);

_Mypoint _tmp1 (p2-p1);

_Mypoint _tmp2 (_l.p2-_l.p1);

_Mypoint _tmp3 = _tmp1._x (_tmp2);

_Mypoint _tmp4 = _tmp3._x (_tmp2);

_Mypoint _tmp5 = _l.p1-p1; t = (_tmp4 * _tmp5) / (_ TMP4 * _TMP1);

Return_myt (p1 (_tmp1 * t), p1 (_tmp1 * t) _ TMP3);

}

Bool isnearpt (_mypoint & _p, t _dis) // is within the distance of offline _dis

{

Return Dis (_P) <= FABS (_dis);

}

Void getNDIVIDE (Container <_mypoint> & points, size_t n) // 等 分

{

Points.clear ();

// IF (n <= 0) return;

IF (n == 1)

{

Points.push_back (p1);

Points.push_back (p2);

Return;

}

T TMP_STEPX = (P2.x-P1.x) / ((t) n);

T TMP_STEPY = (P2.Y-P1.Y) / ((t) n);

T TMP_STEPZ = (P2.Z-P1.Z) / ((t) n);

For (int i = 0; i <= n; i )

{

Points.push_back (_mypoint (TMP_STEPX, TMP_STEPY, TMP_STEPZ) * ((T) I))))))

}

}

_Mypoint rotate (_mypoint p, t ceta) // rotate

{

Return (P-P1). Rotate (CETA, P2-P1) P1;

}

_Mypoint p1, p2;

}

#ENDIF

Assplane.h

// Copyright (C) Liang Yi, 2004

// Finally modified: 2004.6. Hangzhou

#ifndef assplane_header

#define Assplane_Header

#include "assline.h"

Template

Class Assplane

{

PUBLIC:

Typedef asspoint _mypoint;

Typedef assline_myline;

Typedef assplane_myt;

Assplane () / / default constructor, direction (1, 1, 1), over the original

{

A = 1;

B = 1;

C = 1;

}

Assplane (const_myt & other)

{

A = other.a;

B = other.b;

C = other.c;

P = other.p;

}

Assplane (T_A, T_B, T_C, _myPoint & _P = _mypoint ()) // Direction (_A, _B, _C), flat _p

{

A = _A;

B = _b;

C = _C;

P = _P;

}

Assplane (_mypoint & vector, _mypoint _p = _mypoint ()) // direction vector, overpot _P plane

{

A = vector.x;

B = vector.y;

C = vector.z;

P = _P;

}

Assplane (_mypoint & p1, _mypoint & p2, _mypoint & p3) / / over 3 o'clock

{

_Mypoint TMP1 = P2-P1;

_Mypoint TMP2 = P3-P1;

_Mypoint TMP3 = TMP1._X (TMP2);

A = tmp3.x;

B = tmp3.y;

C = tmp3.z;

P = P1;

}

Assplane (_MYLINE & _L, _MYPOINT & _P) / / Flat of Over Point and Line {

_Mypoint tmp1 = _l.p1-_p;

_Mypoint tmp2 = _l.p2-_p;

_Mypoint TMP3 = TMP1._X (TMP2);

A = tmp3.x;

B = tmp3.y;

C = tmp3.z;

P = _P;

}

_MYLINE Perpendicularline (_Mypoint & _P) // Overpot _P with surface vertical line

{

Return_Myline (_P, A, B, C, _MYLINE :: Point_SLOPE);

}

_Myt parallelplane (_mypoint & _p) // Overpot and face parallel plane

{

Return_myt (A, B, C, _P);

}

T Dis (_MyPoint & _P) // Surface to Distance

{

_Mypoint _tmp1 (a, b, c);

Return Fabs (_TMP1 * (_P-P)) / _ Tmp1.Modul ();

}

T Dis (_MYT & _PL) // Distance to the face

{

_Mypoint _tmp1 (a, b, c);

_Mypoint _tmp2 (_pl.a, _pl.b, _pl.c);

_Mypoint _tmp3 = _tmp1._x (_tmp2);

IF (_TMP3.MODUL ()> ass_delta) Return 0;

Return Dis (_PL.P);

}

_MYT PerpendicularPlane (_MYLINE & _L) / / Overline and face vertical face

{

_Mypoint _tmp1 = _l.p2-_l.p1;

_Mypoint _tmp2 = _tmp1._x (_mypoint (a, b, c));

Return_myt (_tmp2.x, _tmp2.y, _tmp2.z, _l.p1);

}

_MYT Parallelplane (_MYLINE & _L) // Overline and face parallel surface

{

IF (! (* this) || _l)) Return Assplane (0, 0, 0, Point (0, 0, 0));

Return_myt (a, b, c, _l.p1);

}

_Mypoint intersection (_MYLINE & _L) // face and line cross

{

IF (* this) || _l) Return_mypoint (DBL_MAX, DBL_MAX, DBL_MAX);

_Mypoint _tmp1 = _l.p2-_l.p1;

_Mypoint _tmp2 = p- _l.p1;

_Mypoint _tmp3 (a, b, c);

Return (_tmp1 * (_tmp3 * _tmp2) / (_tmp3 * _tmp1))) _l.p1;

}

Bool Operator || (_MYLINE & _L) // Decisive line parallel

{

Return Fabs ((_l.p2-_l.p1) * _ mypoint (a, b, c))}

Bool operator || (_myt & _pl) // is a parallel judgment

{

_Mypoint _tmp1 (a, b, c);

_Mypoint _tmp2 (_pl.a, _pl.b, _pl.c);

Return ((_tmp1 / _tmp1.modul ()) ._ x (_tmp2 / _tmp2.modul ()))))))). MODUL ()}

T angle (_MYLINE & _L) // face with line angle

{

_Mypoint _tmp1 = _l.p2-_l.p1;

_Mypoint _tmp2 (a, b, c);

Return Acos (_TMP1 * _TMP2 / (_tmp1.modul () * _tmp2.modul ()))));

}

T angle (_myt & _pl) // Cylinder with face {

_Mypoint _tmp1 (a, b, c);

_Mypoint _tmp2 (_pl.a, _pl.b, _pl.c);

Return ACOS ((_tmp1 * _tmp2) / (_tmp1.modul () * _tmp2.modul ()));

}

_MYLINE INTERSECTION (Assplane & _PL) // face with face

{

IF ((* this) || _pl) Return_Myline (0, 0, 0, 0, 0);

_Mypoint _tmp1 (a, b, c);

_Mypoint _tmp2 (_pl.a, _pl.b, _pl.c);

_Mypoint _tmp3 = _tmp1._x (_tmp2);

_MYLINE _TMPL1 (_pl.p, _pl.p _tmp3._x (_tmp2));

_MYLINE _TMPL2 (P, P _TMP3._X (_TMP1));

IF (_TMPL1.DIS (_tmpl2) return_myline (_tmpl1.intersection (_tmpl2), _ tmp3.x, _tmp3.y, _tmp3.z, _myline :: point_slope);

Return_tmpl1.perpendicularline (_TMPL2);

}

Bool isinplane (_mypoint & _p) // Dot in the plane

{

Return Fabs ((_ p-p) * _ mypoint (a, b, c))}

Bool isinplane (_MYLINE & _L) // Judging in the plane

{

Return isinplane (_l.p1) && isinplane (_l.p2);

}

_Mypoint getnormal () // unit direction

{

_Mypoint _tmp (a, b, c);

Return_TMP / _TMP.MODUL ();

}

Bool isnerpt (_mypoint & _p, t _dis) // is within the distance _dis

{

Return Dis (_P) <= FABS (_dis);

}

_Myt offset (t_h) // offset _H getting the plane

{

_Mypoint TMPP (A, B, C);

T t = _H / TMPP.MODUL ();

Return_MYT (A, B, C, P (_ mypoint (a, b, c) * t));

}

T A, B, C;

_Mypoint p;

}

#ENDIF

转载请注明原文地址:https://www.9cbs.com/read-21659.html

New Post(0)