1 /* PaniniGeneral.c		15Jan2010 TKS
2 
3 Copyright (c) 2010, Thomas K Sharpless
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8     * Redistributions of source code must retain the above copyright
9       notice, this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright
11       notice, this list of conditions and the following disclaimer in the
12       documentation and/or other materials provided with the distribution.
13     * Neither the name of the <organization> nor the
14       names of its contributors may be used to endorse or promote products
15       derived from this software without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 This is the reference implementation of the General Pannini
29 Projection, an elaboration of the basic Pannini projection
30 discovered by Bruno Postle and Thomas Sharpless in December
31 2008 in paintings by Gian Paolo Pannini (1691-1765).
32 
33 */
34 #include "PaniniGeneral.h"
35 
36 #include <math.h>
37 
38 #include <filter.h>
39 
panini_general_toSphere(double * lon,double * lat,double h,double v,double d,double top,double bot)40 int panini_general_toSphere	( double* lon, double* lat,
41 							  double h,  double v,
42 							  double d, double top, double bot
43 							 )
44 {
45 	double S, cl, q, t;
46 
47 	if( d < 0 ) return 0;
48 	q = v < 0 ? top : bot;
49 	if( h == 0 ){
50         *lon = 0;
51 		S = 1;
52 		cl = 1;
53 	} else {
54 	/* solve quadratic for cosine of azimuth angle */
55         double k, kk, dd, del;
56         k = fabs(h) / (d + 1);
57         kk = k * k;
58         dd = d * d;
59         del = kk * kk * dd - (kk + 1) * (kk * dd - 1);
60         if( del < 0 )
61             return 0;
62         cl = (-kk * d + sqrt( del )) / (kk + 1);
63 	/* use that to compute S, and angle */
64         S = (d + cl)/(d + 1);
65 		*lon = atan2( S * h, cl );
66     }
67 	*lat = atan(S * v);
68 
69   /* squeeze */
70 	if( q > 0 ){
71 	/* hard squeeze */
72 		t = fabs(cl);
73 		if (t > EPSLN)
74 			t = q / t;
75 		t += 1 - q;
76 		if (fabs(t) < EPSLN)
77 			*lat = 0;
78 		else
79 			*lat = atan(S*v / t);
80 	} else if( q < 0 ){
81 	/* soft squeeze version 2 */
82 		double cc = cos(0.92 * *lon) - 1;
83 		double ss = 2 * d / (d + 1);
84 		t = v / (1 + ss * q * cc );
85 		*lat = atan( S * t );
86 	}
87 
88 	return 1;
89 }
panini_general_toPlane(double lon,double lat,double * h,double * v,double d,double top,double bot)90 int panini_general_toPlane	( double lon, double lat,
91 							  double*  h,  double*  v,
92 							  double d, double top, double bot
93 							 )
94 {
95 	double S, q, t;
96 
97 	if( d < 0 ) return 0;
98 
99 	S = (d + 1) / (d + cos(lon));
100 	*h = sin(lon) * S;
101 	*v = tan(lat) * S;
102 
103   /* squeeze */
104 	q = lat < 0 ? top : bot;
105 	if( q < 0 ){
106 	/* soft squeeze version 2 */
107 		double cc = cos(0.92 * lon) - 1;
108 		double ss = 2 * d / (d + 1);
109 		*v *= 1 + ss * q * cc;
110 	} else if( q > 0 ){
111 	/* hard squeeze */
112 		t = fabs(cos(lon));
113 		if( t > EPSLN )
114 			t = *v / t;
115 		*v += q * (t - *v);
116 	}
117 
118 
119 	return 1;
120 }
121 
panini_general_maxVAs(double d,double maxProj,double * maxView)122 int panini_general_maxVAs	( double d,
123 							  double maxProj,
124 							  double * maxView
125 							 )
126 {	double a, s;
127 
128 	if( d < 0 ) return 0;
129 
130 /* hFOV... */
131   // theoretical max angle (infeasible for d < 1.1 or so)
132 	if( d > 1. )
133 		s =  -1/d;
134 	else
135 		s = -d;
136 	a = acos( s );
137   // actual limit may be max projection angle...
138 	s = asin(d * sin(maxProj)) + maxProj ;
139 	if( a > s ){	// clip to projection angle limit
140 		a = s;
141 	}
142     maxView[0] = a;
143 
144 /* vFOV... */
145 	maxView[1] = maxProj;
146 
147 	return 1;
148 }
149