-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmca.cpp
More file actions
101 lines (95 loc) · 3.7 KB
/
mca.cpp
File metadata and controls
101 lines (95 loc) · 3.7 KB
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
/*============================================================================
* Daniel J. Greenhoe
* routines for Mean Circular arcs
* y
* | o p Let (rp,tp) be the polar location of point p.
* | / where rp is the Euclidean distance from (0,0) to p
* | / and tp is radian measure from the x-axis to p.
* |/tp Let (rq,tq) be the polar location of point q.
* --------|---------- x The "Mean Circular arc" r(theta) is defined here as
* |\tq rp + rq
* | \ r(theta) = ------- for tq <= theta <= tp
* | o q 2
*
* note: this metric is *deprecated* because it has been found to be not so
* "useful" for "practical" applications.
* For example, the Mean Circular Arc metric from (0,1/2) to (1,100) is
* 99.505 for the Euclidean metric
* 63.347 for the 2/pi scaled Euclidean metric
* 63.348 for the Lagrange Arc metric BUT only
* 0.319907 for the Mean Circular Arc metric
*============================================================================*/
/*=====================================
* headers
*=====================================*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "main.h"
#include "r1.h"
#include "r2.h"
#include "r3.h"
#include "mca.h"
/*-------------------------------------------------------------------------
* path length s of Mean Circular Arc from a point p at polar coordinate (rp,tp)
* to point q at polar coordinate (rq,tq).
* rp+rq
* s = ------- tdiff
* 2
*-------------------------------------------------------------------------*/
double mca_arclength(double rp, double rq, double tdiff){
if(rp==0) return -1;
if(rq==0) return -2;
if(tdiff==0) return -3;
if(tdiff>PI) tdiff = 2*PI-tdiff;
double y = (rp+rq)/2*tdiff;
return y;
}
/*-------------------------------------------------------------------------
* Mean Circular arc metric from <p> to <q> in R^2
*-------------------------------------------------------------------------*/
double mca_metric(vectR2 p, vectR2 q){
double rp=p.mag(), rq=q.mag();
double tdiff = pqtheta(p,q);
vectR2 pq=p-q;
double d;
if(rp==0 || rq==0 || tdiff<=0) d = pq.mag();
else if(rp==rq) d = rp*tdiff;
else d = mca_arclength(rp, rq, tdiff);
//printf("rp=%lf rq=%lf tdiff=%lf d=%lf ds=%lf\n",rp,rq,tdiff,d,d*2/PI);
return d*2/PI;
}
/*-------------------------------------------------------------------------
* Mean Circular arc metric from <p> to <q> in R^3
*-------------------------------------------------------------------------*/
double mca_metric(vectR3 p, vectR3 q){
double rp=p.mag(), rq=q.mag();
double tdiff = pqtheta(p,q);
vectR3 pq=p-q;
double d;
if(rp==0 || rq==0 || tdiff<=0) d = pq.mag();
else if(rp==rq) d = rp*tdiff;
else d = mca_arclength(rp, rq, tdiff);
return d*2/PI;
}
/*-------------------------------------------------------------------------
* Find the polar length of a point q with radial measure tq that is a
* distance <d> from the point <p> with polar coordinates (rp,tp)
* using search resolution <N>
*-------------------------------------------------------------------------*/
vectR3 mca_findq(vectR3 p, double theta, double phi, double d, long int N){
double smallesterror=100000,rq,dd,errord;
vectR3 bestq;
vectR3 q;
vectR3 pq;
for(rq=0; rq<=5; rq+=5.0/N){
q.polartoxyz(rq,theta,phi);
dd=mca_metric(p,q);
errord=fabs(d-dd);
if(errord<smallesterror){
bestq=q;
smallesterror=errord;
}
}
return bestq;
}