-
Notifications
You must be signed in to change notification settings - Fork 5
/
Point3D.cpp
226 lines (189 loc) · 5.59 KB
/
Point3D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "Point3D.h"
#include <cmath>
#include <cstdlib>
#define Pi 3.14159
using namespace std;
namespace trimeshVec
{
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
CPoint3D::CPoint3D( ) // normal constructor
{
x = 0; y = 0; z = 0;
}
CPoint3D::CPoint3D( const CPoint3D &v )
{
x = v.x;
y = v.y;
z = v.z;
}
CPoint3D::CPoint3D( int xx, int yy, int zz ) // normal constructor
{
x = (float)xx; y = (float)yy; z = (float)zz;
}
CPoint3D::CPoint3D( float xx, float yy, float zz ) // normal constructor
{
x = xx; y = yy; z = zz;
}
CPoint3D::CPoint3D( double xx, double yy, double zz ) // normal constructor
{
x = (float)xx; y = (float)yy; z = (float)zz;
}
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
CPoint3D::~CPoint3D(void)
{
}
//------------------------------------------------------------------------------
// Plus
//------------------------------------------------------------------------------
const CPoint3D& CPoint3D::operator +=(const CPoint3D &v)
{
x += v.x;
y += v.y;
z += v.z;
return *this;
}
//------------------------------------------------------------------------------
// Subtraction
//------------------------------------------------------------------------------
const CPoint3D& CPoint3D::operator -=(const CPoint3D &v)
{
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}
//------------------------------------------------------------------------------
// CVectors plus
//------------------------------------------------------------------------------
CPoint3D CPoint3D::operator + ( const CPoint3D& v )
{
CPoint3D r(*this);
r += v;
return ( r );
}
//------------------------------------------------------------------------------
// CVectors subtraction
//------------------------------------------------------------------------------
CPoint3D CPoint3D::operator - ( const CPoint3D& v )
{
CPoint3D r(*this);
r -= v;
return ( r );
}
//------------------------------------------------------------------------------
// CVectors cross product
//------------------------------------------------------------------------------
CPoint3D CPoint3D::operator * ( const CPoint3D& v )
{
CPoint3D r;
r.x = y*v.z - v.y*z;
r.y = v.x*z - x*v.z;
r.z = x*v.y - v.x*y;
return( r );
}
//------------------------------------------------------------------------------
// CPoint3D scale
//------------------------------------------------------------------------------
CPoint3D CPoint3D::operator * ( int k ) // scaled by int
{
CPoint3D r;
r.x = x * (float)k;
r.y = y * (float)k;
r.z = z * (float)k;
return( r );
}
CPoint3D CPoint3D::operator * ( float k ) // scaled by float
{
CPoint3D r;
r.x = x * k;
r.y = y * k;
r.z = z * k;
return( r );
}
CPoint3D CPoint3D::operator * ( double k ) // scaled by double
{
CPoint3D r;
r.x = x * (float)k;
r.y = y * (float)k;
r.z = z * (float)k;
return( r );
}
//------------------------------------------------------------------------------
// CVectors dot product
//------------------------------------------------------------------------------
float CPoint3D::operator & ( const CPoint3D& v )
{
return ( x * v.x + y * v.y + z * v.z );
}
//------------------------------------------------------------------------------
// CPoint3D length
//------------------------------------------------------------------------------
float CPoint3D::length()
{
return( (float)sqrt( x*x + y*y + z*z ) );
}
//------------------------------------------------------------------------------
// CPoint3D unify
//------------------------------------------------------------------------------
void CPoint3D::unify()
{
float len = length();
if( len < 1.0e-20 )
{
x = float(rand()%1000+20);
y = float(rand()%1000+20);
z = float(rand()%1000+20);
len=length();
x/=len;
y/=len;
z/=len;
}
else
{
len = 1.0f/len;
x *= len;
y *= len;
z *= len;
}
}
void CPoint3D::RangeUnify(float min,float max)
{
if (x>max) x=max;
if (y>max) y=max;
if (z>max) z=max;
if (x<min) x=min;
if (y<min) y=min;
if (z<min) z=min;
}
//Project the vetor to new coordinate frame which is determined by coordX,coordY and coordZ
CPoint3D CPoint3D::ProjectToNewCoordinate(CPoint3D& coordX,CPoint3D& coordY,CPoint3D& coordZ)
{
return CPoint3D((*this)&coordX,(*this)&coordY,(*this)&coordZ);
}
void VectorToAngle(CPoint3D& vec,float& fPdir,float& fAdir)
{
//polar angle
fPdir=atan2(sqrt(vec.x*vec.x+vec.z*vec.z),vec.y);
//azimuthal angle
fAdir=atan2(-vec.z,vec.x);
if (fAdir<0) fAdir+=2*float(Pi);
}
//------------------------------------------------------------------------------
// CVectors blend product
//------------------------------------------------------------------------------
float blend_product(CPoint3D v1, CPoint3D v2, CPoint3D v3)
{
return( ( v1 * v2 ) & v3 );
}
//------------------------------------------------------------------------------
// bulid a normal by three 3D points
//------------------------------------------------------------------------------
CPoint3D BuildNormal( CPoint3D p1, CPoint3D p2, CPoint3D p3 )
{
return ( ( p3-p2 ) * ( p1-p2 ) );
}
}