Skip to content

Commit aaf9b25

Browse files
committed
generalize angleBetween
1 parent a72ca05 commit aaf9b25

2 files changed

Lines changed: 17 additions & 52 deletions

File tree

src/slicer/geometry.cpp

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -220,50 +220,6 @@ void rotArcballTrans(Matrix4fT &matfT, const Vector3d &axis, double angle)
220220
}
221221
}
222222

223-
// // from V1 to V2
224-
// template< size_t M, typename T >
225-
// long double angleBetween(const vmml::vector< M, T > V1, const vmml::vector< M, T > V2 )
226-
// {
227-
// long double dotproduct = V1.dot(V2);
228-
// long double length = V1.length() * V2.length();
229-
// long double quot = dotproduct / length;
230-
// if (quot > 1 && quot < 1.0001) quot = 1; // strange case where acos => NaN
231-
// if (quot < -1 && quot > -1.0001) quot = -1;
232-
// long double result = acosl( quot ); // 0 .. pi
233-
// if (isleftof(T(), V2, V1))
234-
// result = -result;
235-
// return result;
236-
// }
237-
238-
// from V1 to V2
239-
long double angleBetween(const Vector3d &V1, const Vector3d &V2)
240-
{
241-
long double dotproduct = V1.dot(V2);
242-
long double length = sqrt(V1.squared_length() * V2.squared_length());
243-
if (length==0) return 0;
244-
long double quot = dotproduct / length;
245-
if (quot > 1 && quot < 1.0001) quot = 1; // strange case where acos => NaN
246-
if (quot < -1 && quot > -1.0001) quot = -1;
247-
long double result = acosl( quot ); // 0 .. pi
248-
if (isleftof(Vector3d(0,0,0), V2, V1))
249-
result = -result;
250-
return result;
251-
}
252-
253-
long double angleBetween(const Vector2d &V1, const Vector2d &V2)
254-
{
255-
long double dotproduct = V1.dot(V2);
256-
long double length = sqrt(V1.squared_length() * V2.squared_length());
257-
if (length==0) return 0;
258-
long double quot = dotproduct / length;
259-
if (quot > 1 && quot < 1.0001) quot = 1;
260-
if (quot < -1 && quot > -1.0001) quot = -1;
261-
long double result = acosl( quot ); // 0 .. pi
262-
if (isleftof(Vector2d(0,0), V2, V1))
263-
result = -result;
264-
return result;
265-
}
266-
267223
// return A halfway rotated around center in direction of B
268224
Vector2d angle_bipartition(const Vector2d &center, const Vector2d &A, const Vector2d &B)
269225
{
@@ -272,7 +228,6 @@ Vector2d angle_bipartition(const Vector2d &center, const Vector2d &A, const Vect
272228
}
273229

274230

275-
276231
// is B left of A wrt center?
277232
bool isleftof(const Vector2d &center, const Vector2d &A, const Vector2d &B)
278233
{

src/slicer/geometry.h

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,27 @@ void moveArcballTrans(Matrix4fT &matfT, const Vector3d &delta);
4444
void setArcballTrans(Matrix4fT &matfT, const Vector3d &trans);
4545
void rotArcballTrans(Matrix4fT &transform, const Vector3d &axis, double angle);
4646

47-
/* template< size_t M, typename T > */
48-
/* long double angleBetween(const vmml::vector< M, T > V1, const vmml::vector< M, T > V2 ); */
49-
long double angleBetween(const Vector3d &V1, const Vector3d &V2);
50-
long double angleBetween(const Vector2d &V1, const Vector2d &V2);
47+
bool isleftof(const Vector2d &center, const Vector2d &A, const Vector2d &B);
48+
bool isleftof(const Vector3d &center, const Vector3d &A, const Vector3d &B);
49+
50+
template<size_t M>
51+
long double angleBetween(const vmml::vector<M, double> &V1, const vmml::vector<M, double> &V2) {
52+
long double dotproduct = V1.dot(V2);
53+
long double length = sqrt(V1.squared_length() * V2.squared_length());
54+
if (length==0) return 0;
55+
long double quot = dotproduct / length;
56+
if (quot > 1 && quot < 1.0001) quot = 1; // strange case where acos => NaN
57+
if (quot < -1 && quot > -1.0001) quot = -1;
58+
long double result = acosl( quot ); // 0 .. pi
59+
if (isleftof(vmml::vector<M, double>(), V2, V1))
60+
result = -result;
61+
return result;
62+
}
63+
5164
Vector2d angle_bipartition(const Vector2d &center, const Vector2d &A, const Vector2d &B);
5265
void center_perpendicular(const Vector2d &from, const Vector2d &to,
5366
Vector2d &p1, Vector2d &p2);
5467

55-
bool isleftof(const Vector2d &center, const Vector2d &A, const Vector2d &B);
56-
bool isleftof(const Vector3d &center, const Vector3d &A, const Vector3d &B);
57-
5868
Vector3d cross2d(const Vector2d &A, const Vector2d &B, double z=0);
5969

6070
double point_segment_distance_Sq(const Vector2d &s1, const Vector2d &s2,

0 commit comments

Comments
 (0)