-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pather_ripple.cpp
187 lines (164 loc) · 4.25 KB
/
er_ripple.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
#include "plugin.hpp"
#include <iostream>
#include <cmath>
float ripple(float r, float decay)
{
//erfc is for cutting off the progressive ropples
return cos(2*M_PI*r)*erfc(-r*2)*exp(-r*decay);
}
void
angle2vec(float theta, float phi, float (&vec)[3])
{
vec[2] = cos(theta);
float r = sin(theta);
vec[0] = r * cos(phi);
vec[1] = r * sin(phi);
}
void
vec2angle(const float (&e)[3], float &theta, float &phi)
{
float r = sqrt(e[0]*e[0]+e[1]*e[1]);
phi = atan2(e[1],e[0]);
theta = atan2(r,e[2]);
}
float dot(const float (&v1)[3], const float (&v2)[3])
{
return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
}
float norm(const float (&v)[3])
{
return sqrt(dot(v,v));
}
void
cross(const float (&v1)[3], const float (&v2)[3], float (&result)[3])
{
result[0] = v1[1]*v2[2] - v1[2]*v2[1];
result[1] = v1[2]*v2[0] - v1[0]*v2[2];
result[2] = v1[0]*v2[1] - v1[1]*v2[0];
}
void
rotation_matrix(const float (&axis)[3], float& theta, float (&matrix)[3][3])
{
/*
Return the rotation matrix associated with counterclockwise rotation about
the given axis by theta radians.
*/
float e[3];
float L = norm(axis);
for(int i=0;i<3; i++){
e[i] = axis[i] / L;
}
float a = cos(theta/2.0);
float s = sin(theta/2.0);
float b = -e[0]*s;
float c = -e[1]*s;
float d = -e[2]*s;
float aa=a*a;
float bb=b*b;
float cc=c*c;
float dd=d*d;
float bc=b*c;
float ad=a*d;
float ac=a*c;
float ab=a*b;
float bd=b*d;
float cd=c*d;
matrix[0][0] = aa+bb-cc-dd;
matrix[0][1] = 2*(bc+ad);
matrix[0][2] = 2*(bd-ac);
matrix[1][0] = 2*(bc-ad);
matrix[1][1] = aa+cc-bb-dd;
matrix[1][2] = 2*(cd+ab);
matrix[2][0] = 2*(bd+ac);
matrix[2][1] = 2*(cd-ab);
matrix[2][2] = aa+dd-bb-cc;
}
class Tilt : public Projector {
protected:
float a;
Projector* child;
float center[3];
float radius; //in radian
float wavelen; // in radian
float amplitude; // magnitude of pixel shift
float decay; // in 1 wavelength
public:
void usage( int argc, char* argv[] )
{
fprintf( stderr, "Usage: %s [-r long,lat,radius,wavelength,amp,decay] [projectors]\n", argv[0] );
fprintf( stderr, "Add ripple on a equirectangular image.\n" );
fprintf( stderr, "Options:\n" );
fprintf( stderr, "\t-r a,b,c,d,e,f\tSpecify ripple parameters.\n" );
exit(1);
}
Tilt(int argc, char* argv[])
{
float longitude=0, latitude=0;//in [-90,90],[180,180]
amplitude = 5;
radius = 1;
wavelen = 0.1;
decay = 0.2;
int c = 1;
while ( c < argc ){
if ( 0 == strcmp( argv[c], "-r" )){
c++;
sscanf(argv[c], "%f,%f,%f,%f,%f,%f", &longitude, &latitude, &radius, &wavelen, &litude, &decay );
c++;
}
else if ( argv[c][0] == '-' ){
usage(argc, argv);
}
else{
break;
}
}
//longitude, latitude are zero at the center of ER picture.
//ranges [-pi,pi] and [-pi/2,pi/2]
//lat=pi/2 is the north pole.
//change them. now origin is at the center top of ER
latitude = 90 - latitude;
latitude *= M_PI / 180.0;
longitude *= M_PI / 180.0;
angle2vec(latitude, longitude, center);
argv += c;
argc -= c;
//fprintf( stderr, "%d\n", argc );
child = plugin_load( argc, argv );
}
uchar* map(float dstx, float dsty)
{
float psi = dstx * M_PI;
float theta = (dsty+0.5) * M_PI;
// In this plugin, theta=0 is the z-axis and is the horizontal line at y=-0.5 (a quaerter from the top)
float point[3];
angle2vec(theta,psi,point);
float axis[3];
cross(center,point,axis);
float sine = norm(axis);
float cosine = dot(center, point);
float arc;
arc = atan2(sine, cosine); //y, x
float ripple_height = amplitude * ripple((radius-arc)/wavelen,decay);
float displ = arc + ripple_height; // in radian
float rotmat[3][3];
rotation_matrix(axis, displ, rotmat);
float source[3];
for(int i=0; i<3; i++){
float s=0;
for(int j=0; j<3; j++){
s += rotmat[i][j] * center[j];
}
source[i] = s;
}
float src_th, src_ph;
vec2angle(source, src_th, src_ph);
return child->map( src_ph/M_PI, src_th/M_PI+0.5 );
}
};
// the class factories
extern "C" Projector* create(int argc, char* argv[]) {
return new Tilt(argc, argv);
}
extern "C" void destroy(Tilt* p) {
delete p;
}