OpenVSP API Documentation 3.41.0
 
Loading...
Searching...
No Matches
Vec3d.h
1//
2// This file is released under the terms of the NASA Open Source Agreement (NOSA)
3// version 1.3 as detailed in the LICENSE file which accompanies this software.
4//
5
6//******************************************************************************
7//
8// 3D Point Double Class
9//
10// J.R. Gloudemans - 7/7/93
11//******************************************************************************
12
13#ifndef VSPVEC3D_H
14#define VSPVEC3D_H
15
16#include "Defines.h"
17#include "Vec2d.h"
18
19#include "eli/code_eli.hpp"
20typedef Eigen::Matrix< double, 1, 3 > threed_point_type;
21
22#include <iostream>
23#include <vector>
24
29class vec3d;
30
31//==== Forward declare to get around MS Compiler bug ====//
68vec3d operator+( const vec3d& a, const vec3d& b );
69
107vec3d operator-( const vec3d& a, const vec3d& b );
108
147vec3d operator*( const vec3d& a, double b );
148
187vec3d operator*( const vec3d& a, const vec3d& b );
188
227vec3d operator/( const vec3d& a, double b );
228
229
234class vec3d
235{
236private:
237
238
239public:
240 double v[3];
241
242 vec3d(); // vec3d x or new vec3d
243
244 ~vec3d() {} // delete vec3d
245
246 vec3d( double xx, double yy, double zz );
247
248 vec3d( const vec3d& a ); // vec3d x = y
249
250 vec3d( const threed_point_type &a );
251
252 vec3d( const double a[3] );
253 vec3d( const float a[3] );
254 vec3d( const std::vector<double> &a );
255
256 vec3d& operator=( const vec3d& a ); // x = y
257 vec3d& operator=( const vec2d& a );
258 vec3d& operator=( double a ); // x = 35.
259 vec3d& operator=( const threed_point_type &a );
260
261 double& operator [] ( int i )
262 {
263 return v[i];
264 }
265 const double& operator [] ( int i ) const
266 {
267 return v[i];
268 }
269
270 vec2d as_vec2d_xy();
271
272 // Set Point Values
298 vec3d& set_xyz( double xx, double yy, double zz );
299
300 vec3d& set_vec( const std::vector<double> &a );
301 vec3d& set_arr( const double a[] );
302 vec3d& set_arr( const float a[] );
326 vec3d& set_x( double xx );
327
351 vec3d& set_y( double yy );
352
376 vec3d& set_z( double zz );
377
378
379 vec3d& set_refx( const vec3d &a );
380 vec3d& set_refy( const vec3d &a );
381 vec3d& set_refz( const vec3d &a );
382
383 // Get Point Values
384 void get_pnt( double pnt[3] ) const
385 {
386 pnt[0] = v[0];
387 pnt[1] = v[1];
388 pnt[2] = v[2];
389 }
390 void get_pnt( float pnt[3] ) const
391 {
392 pnt[0] = ( float )v[0];
393 pnt[1] = ( float )v[1];
394 pnt[2] = ( float )v[2];
395 }
396 void get_pnt( threed_point_type &pnt ) const
397 {
398 pnt[0] = v[0];
399 pnt[1] = v[1];
400 pnt[2] = v[2];
401 }
402
435 double x() const
436
437 {
438 return( v[0] );
439 }
472 double y() const
473
474 {
475 return( v[1] );
476 }
509 double z() const
510
511 {
512 return( v[2] );
513 }
514
515 double* data()
516 {
517 return( v );
518 }
519
520 // Rotate About Axis --> Change Internal Values
553 void rotate_x( double cos_alpha, double sin_alpha )
554
555 {
556 double old_y = v[1];
557 v[1] = cos_alpha * v[1] + sin_alpha * v[2];
558 v[2] = -sin_alpha * old_y + cos_alpha * v[2];
559 }
592 void rotate_y( double cos_alpha, double sin_alpha )
593
594 {
595 double old_x = v[0];
596 v[0] = cos_alpha * v[0] - sin_alpha * v[2];
597 v[2] = sin_alpha * old_x + cos_alpha * v[2];
598 }
631 void rotate_z( double cos_alpha, double sin_alpha )
632
633 {
634 double old_x = v[0];
635 v[0] = cos_alpha * v[0] + sin_alpha * v[1];
636 v[1] = -sin_alpha * old_x + cos_alpha * v[1];
637 }
638
639 // Scale Up Single Coord --> Change Internal Values
668 void scale_x( double scale )
669
670 {
671 v[0] *= scale;
672 };
701 void scale_y( double scale )
702
703 {
704 v[1] *= scale;
705 };
734 void scale_z( double scale )
735
736 {
737 v[2] *= scale;
738 };
739
768 void offset_x( double offset )
769
770 {
771 v[0] += offset;
772 };
801 void offset_y( double offset )
802
803 {
804 v[1] += offset;
805 };
834 void offset_z( double offset )
835
836 {
837 v[2] += offset;
838 };
839 void offset_i( double offset, int idir )
840 {
841 v[idir] += offset;
842 };
843
844 // Rotate About Axis : Assume Zero Coord --> Change Internal Values
849 // TODO: Example
850
851 void rotate_z_zero_x( double cos_alpha, double sin_alpha )
852
853 {
854 v[0] = sin_alpha * v[1] + v[0];
855 v[1] = cos_alpha * v[1];
856 }
861 // TODO: Example
862
863 void rotate_z_zero_y( double cos_alpha, double sin_alpha )
864
865 {
866 v[0] = cos_alpha * v[0];
867 v[1] = -sin_alpha * v[0] + v[1];
868 }
869
870 // Transform Matrix
871 //vec3d transform(float mat[4][4]);
872 //vec3d transform(double mat[4][4]);
902
903 {
904 return( vec3d( v[0], v[1], -v[2] ) );
905 }
935
936 {
937 return( vec3d( v[0], -v[1], v[2] ) );
938 }
968
969 {
970 return( vec3d( -v[0], v[1], v[2] ) );
971 }
972
973 vec3d swap_xy() const
974 {
975 return( vec3d( v[1], v[0], v[2] ) );
976 }
977 vec3d swap_xz() const
978 {
979 return( vec3d( v[2], v[1], v[0] ) );
980 }
981 vec3d swap_yz() const
982 {
983 return( vec3d( v[0], v[2], v[1] ) );
984 }
985
986 // x = a + b, x = a - b, a*scale, a/scale
987 friend vec3d operator+( const vec3d& a, const vec3d& b );
988 friend vec3d operator-( const vec3d& a, const vec3d& b );
989 friend vec3d operator*( const vec3d& a, double b );
990 friend vec3d operator*( double b, const vec3d& a );
991 friend vec3d operator*( const vec3d& a, const vec3d& b );
992 friend vec3d operator/( const vec3d& a, double b );
993 vec3d& operator+=( const vec3d& b );
994 vec3d& operator-=( const vec3d& b );
995 vec3d& operator*=( double b );
996 vec3d& operator+=( double b[] );
997 vec3d& operator-=( double b[] );
998 vec3d& operator+=( float b[] );
999 vec3d& operator-=( float b[] );
1000
1001 friend vec3d operator-( const vec3d & in );
1002
1003 friend bool operator==( const vec3d &a, const vec3d& b );
1004 friend bool operator!=( const vec3d &a, const vec3d& b );
1005
1006 // cout << a
1007// friend ostream& operator<< (ostream& out, const vec3d& a);
1008
1009 friend double dist( const vec3d& a, const vec3d& b );
1010 friend double dist_squared( const vec3d& a, const vec3d& b );
1011
1041 double mag() const; // x = a.mag()
1042
1076 void normalize(); // a.normalize()
1077
1078
1079 int major_comp() const;
1080 int minor_comp() const;
1081
1082 bool isnan() const;
1083 bool isinf() const;
1084 bool isfinite() const;
1085
1086 friend double dot( const vec3d& a, const vec3d& b ); // x = dot(a,b)
1087 friend vec3d cross( const vec3d& a, const vec3d& b ); // a = cross(b,c)
1088 friend double angle( const vec3d& a, const vec3d& b );
1089 friend double signed_angle( const vec3d& a, const vec3d& b, const vec3d& ref );
1090 friend double cos_angle( const vec3d& a, const vec3d& b );
1091 friend double radius_of_circle( const vec3d& p1, const vec3d& p2, const vec3d& p3 );
1092 friend void center_of_circle( const vec3d& p1, const vec3d& p2, const vec3d& p3, vec3d& center );
1093 friend double dist_pnt_2_plane( const vec3d& org, const vec3d& norm, const vec3d& pnt );
1094 friend double dist_pnt_2_line( const vec3d& line_pt1, const vec3d& line_pt2, const vec3d& pnt );
1095// friend double dist_pnt_2_line_seg( vec3d& line_pt1, vec3d& line_pt2, vec3d& pnt);
1096 friend vec3d proj_pnt_on_line_seg( const vec3d& line_pt1, const vec3d& line_pt2, const vec3d& pnt );
1097 friend vec3d proj_pnt_on_ray( const vec3d& line_pt1, const vec3d& line_pt2, const vec3d& pnt );
1098 friend vec3d proj_pnt_on_line( const vec3d& line_pt1, const vec3d& line_pt2, const vec3d& pnt );
1099 friend vec3d proj_pnt_to_plane( const vec3d& org, const vec3d& plane_ln1, const vec3d& plane_ln2, const vec3d& pnt );
1100 friend vec3d proj_vec_to_plane( const vec3d& vec, const vec3d& norm );
1101 friend int tri_seg_intersect( const vec3d& A, const vec3d& B, const vec3d& C, const vec3d& D, const vec3d& E,
1102 double& u, double& w, double& t );
1103 friend int tri_ray_intersect( const vec3d& A, const vec3d& B, const vec3d& C, const vec3d& D, const vec3d& E,
1104 double& u, double& w, double& t );
1105 friend int plane_ray_intersect( const vec3d& A, const vec3d& B, const vec3d& C, const vec3d& D, const vec3d& E, double& t );
1106 friend int plane_ray_intersect( const vec3d& orig, const vec3d& norm, const vec3d& D, const vec3d& E, double& t );
1107 friend int ray_ray_intersect( const vec3d& A, const vec3d& B, const vec3d& C, const vec3d& D, vec3d& int_pnt1, vec3d& int_pnt2 );
1108 friend double tetra_volume( const vec3d& A, const vec3d& B, const vec3d& C );
1109 friend double area( const vec3d& A, const vec3d& B, const vec3d& C );
1110 friend double poly_area( const std::vector< vec3d > & pnt_vec );
1111 friend double dist3D_Segment_to_Segment( const vec3d& S1P0, const vec3d& S1P1, const vec3d& S2P0, const vec3d& S2P1 );
1112 friend double dist3D_Segment_to_Segment( const vec3d& S1P0, const vec3d& S1P1, const vec3d& S2P0, const vec3d& S2P1,
1113 double* Lt, vec3d* Ln, double* St, vec3d* Sn );
1114 friend double nearSegSeg( const vec3d& L0, const vec3d& L1, const vec3d& S0, const vec3d& S1, double* Lt, vec3d* Ln, double* St, vec3d* Sn );
1115 friend double pointLineDistSquared( const vec3d & p, const vec3d& lp0, const vec3d& lp1, double* t );
1116 friend double pointSegDistSquared( const vec3d& p, const vec3d& sp0, const vec3d& sp1, double* t );
1117 friend vec3d point_on_line( const vec3d & lp0, const vec3d & lp1, const double & t );
1118
1119 friend vec2d MapToPlane( const vec3d & p, const vec3d & planeOrig, const vec3d & planeVec1, const vec3d & planeVec2 );
1120 friend vec3d MapFromPlane( const vec2d & uw, const vec3d & planeOrig, const vec3d & planeVec1, const vec3d & planeVec2 );
1121
1122 friend int plane_half_space( const vec3d & planeOrig, const vec3d & planeNorm, const vec3d & pnt );
1123
1124 friend bool line_line_intersect( const vec3d & p1, const vec3d & p2, const vec3d & p3, const vec3d & p4, double* s, double* t );
1125
1126 friend vec3d RotateArbAxis( const vec3d & p, double theta, const vec3d & r );
1127
1128 friend bool PtInTri( const vec3d & v0, const vec3d & v1, const vec3d & v2, const vec3d & p );
1129 friend vec3d BarycentricWeights( const vec3d & v0, const vec3d & v1, const vec3d & v2, const vec3d & p );
1130 friend void BilinearWeights( const vec3d & p0, const vec3d & p1, const vec3d & p, std::vector< double > & weights );
1131 friend double tri_tri_min_dist( const vec3d & v0, const vec3d & v1, const vec3d & v2, const vec3d & v3, const vec3d & v4, const vec3d & v5 );
1132 friend double pnt_tri_min_dist( const vec3d & v0, const vec3d & v1, const vec3d & v2, const vec3d & pnt );
1133
1134 friend vec3d slerp( const vec3d& a, const vec3d& b, const double &t );
1135};
1136
1178double dist( const vec3d& a, const vec3d& b );
1179
1220double dist_squared( const vec3d& a, const vec3d& b );
1221
1257double dot( const vec3d& a, const vec3d& b );
1258
1298vec3d cross( const vec3d& a, const vec3d& b );
1299
1337double angle( const vec3d& a, const vec3d& b );
1338
1379double signed_angle( const vec3d& a, const vec3d& b, const vec3d& ref );
1380
1423 // TODO: verify description
1424
1425double cos_angle( const vec3d& a, const vec3d& b );
1426
1467vec3d RotateArbAxis( const vec3d & p, double theta, const vec3d & r );
1468
1469
1470namespace std
1471{
1472string to_string( const vec3d &v);
1473}
1474
1475vec3d slerp( const vec3d& a, const vec3d& b, const double &t );
1476
1477#endif
1478
1479
1480
Definition Vec3d.h:235
friend vec3d operator-(const vec3d &a, const vec3d &b)
vec3d & set_x(double xx)
void rotate_z_zero_x(double cos_alpha, double sin_alpha)
Definition Vec3d.h:851
friend double dot(const vec3d &a, const vec3d &b)
vec3d & set_xyz(double xx, double yy, double zz)
void offset_z(double offset)
Definition Vec3d.h:834
void scale_z(double scale)
Definition Vec3d.h:734
friend double signed_angle(const vec3d &a, const vec3d &b, const vec3d &ref)
double y() const
Definition Vec3d.h:472
double x() const
Definition Vec3d.h:435
void offset_x(double offset)
Definition Vec3d.h:768
void scale_y(double scale)
Definition Vec3d.h:701
void rotate_z(double cos_alpha, double sin_alpha)
Definition Vec3d.h:631
friend vec3d operator+(const vec3d &a, const vec3d &b)
double mag() const
friend vec3d operator*(const vec3d &a, double b)
friend vec3d operator/(const vec3d &a, double b)
vec3d reflect_yz() const
Definition Vec3d.h:967
void rotate_x(double cos_alpha, double sin_alpha)
Definition Vec3d.h:553
vec3d & set_z(double zz)
double z() const
Definition Vec3d.h:509
friend vec3d operator*(const vec3d &a, const vec3d &b)
friend vec3d cross(const vec3d &a, const vec3d &b)
void rotate_z_zero_y(double cos_alpha, double sin_alpha)
Definition Vec3d.h:863
friend double angle(const vec3d &a, const vec3d &b)
void offset_y(double offset)
Definition Vec3d.h:801
void scale_x(double scale)
Definition Vec3d.h:668
void normalize()
vec3d & set_y(double yy)
friend double dist(const vec3d &a, const vec3d &b)
void rotate_y(double cos_alpha, double sin_alpha)
Definition Vec3d.h:592
friend double cos_angle(const vec3d &a, const vec3d &b)
friend vec3d RotateArbAxis(const vec3d &p, double theta, const vec3d &r)
vec3d reflect_xz() const
Definition Vec3d.h:934
vec3d reflect_xy() const
Definition Vec3d.h:901
friend double dist_squared(const vec3d &a, const vec3d &b)
vec3d operator-(const vec3d &a, const vec3d &b)
double dot(const vec3d &a, const vec3d &b)
double signed_angle(const vec3d &a, const vec3d &b, const vec3d &ref)
vec3d operator+(const vec3d &a, const vec3d &b)
vec3d operator*(const vec3d &a, double b)
vec3d operator/(const vec3d &a, double b)
vec3d cross(const vec3d &a, const vec3d &b)
double angle(const vec3d &a, const vec3d &b)
double dist(const vec3d &a, const vec3d &b)
double cos_angle(const vec3d &a, const vec3d &b)
vec3d RotateArbAxis(const vec3d &p, double theta, const vec3d &r)
double dist_squared(const vec3d &a, const vec3d &b)