-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVignette.cpp
More file actions
138 lines (120 loc) · 4.68 KB
/
Copy pathVignette.cpp
File metadata and controls
138 lines (120 loc) · 4.68 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
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
/*
* Copyright (C) 2024: Arizona Board of Regents on Behalf of the University of Arizona
*/
#include <cmath>
#include <iostream>
#include <Vignette.h>
using namespace asdp::render;
static double Pi = 3.14159265358979323846;
VignetteRadialPolynomail::VignetteRadialPolynomail(std::array<double, 2> const& COP, std::array<double, 2> const& FOVsDegrees,
std::vector<double> const& coefficients)
: m_COP(COP)
, m_coefficients(coefficients)
{
if (FOVsDegrees[0] <= 0) {
std::cerr << "VignetteRadialPolynomail: Invalid horizontal field of view: " << std::to_string(FOVsDegrees[0]) << std::endl;
m_coefficients.clear();
}
if (FOVsDegrees[1] <= 0) {
std::cerr << "VignetteRadialPolynomail: Invalid vertical field of view: " << std::to_string(FOVsDegrees[1]) << std::endl;
m_coefficients.clear();
}
if (m_coefficients.size() == 0) {
std::cerr << "VignetteRadialPolynomail: Not enough coefficients: " << std::to_string(m_coefficients.size()) << std::endl;
m_coefficients.clear();
}
// Compute the half-width and half-height sizes of the image.
m_halfSizes[0] = std::tan(FOVsDegrees[0] * 0.5 * Pi / 180.0);
m_halfSizes[1] = std::tan(FOVsDegrees[1] * 0.5 * Pi / 180.0);
// Compute the scale factor to take vertical distances to horizontal distances.
// We will multiply the Y difference by this to get equivalent X distance.
// When the width is greater than the height, this will be less than 1 because
// the total motion in Y is less than the total motion in X. Therefore we divide
// the height by the width.
m_vScale = m_halfSizes[1] / m_halfSizes[0];
}
double VignetteRadialPolynomail::EvaluateAtPoint(std::array<double, 2> point) const
{
// If we don't have enough coefficients, return 1.0
if (m_coefficients.size() == 0) {
return 1.0;
}
// Find our radial distance in the image.
double dx = point[0] - m_COP[0];
double dy = (point[1] - m_COP[1]) * m_vScale;
double r = std::sqrt(dx*dx + dy*dy);
// Evaluate the polynomial at this radius.
double value = 0.0;
double multiplier = 1.0;
double r2 = r * r;
for (double coefficient : m_coefficients) {
value += coefficient * multiplier;
multiplier *= r2;
}
return value;
}
//==============================================================================
// Test and its helper functions
bool isNear(double a, double b, double epsilon = 1e-6)
{
return std::abs(a-b) < epsilon;
}
std::string Vignette::Test()
{
// Test the VignetteNone class. All return values should be 1.0.
{
VignetteNone none;
if (none.EvaluateAtPoint({0.0, 0.0}) != 1.0) {
return "VignetteNone: Expected 1.0 at (0,0)";
}
if (none.EvaluateAtPoint({-1.0, 0.0}) != 1.0) {
return "VignetteNone: Expected 1.0 at (-1,0)";
}
if (none.EvaluateAtPoint({1.0, 0.0}) != 1.0) {
return "VignetteNone: Expected 1.0 at (1,0)";
}
if (none.EvaluateAtPoint({0.0, -1.0}) != 1.0) {
return "VignetteNone: Expected 1.0 at (0,-1)";
}
if (none.EvaluateAtPoint({0.0, 1.0}) != 1.0) {
return "VignetteNone: Expected 1.0 at (0,1)";
}
}
// Test the VignetteRadialPolynomail class.
{
std::array<double, 2> COP = {0.5, 0.1};
// This has an aspect ratio of 1:2 on the image, so we only need to move half as
// far in Y to match X motion.
std::array<double, 2> FOVsDegrees = {53.13, 90.0};
std::vector<double> coefficients = {1.0, 0.1};
VignetteRadialPolynomail vig(COP, FOVsDegrees, coefficients);
if (!isNear(vig.EvaluateAtPoint({0.5, 0.1}), 1.0)) {
return "VignetteRadialPolynomail: Expected 1.0 at (0.5,0.1)";
}
if (!isNear(vig.EvaluateAtPoint({-0.5, 0.1}), 1.1)) {
return "VignetteRadialPolynomail: Expected 1.1 at (-0.5,0.1)";
}
if (!isNear(vig.EvaluateAtPoint({0.5, 0.6}), 1.1)) {
return "VignetteRadialPolynomail: Expected 1.1 at (0.5,0.6)";
}
}
// Test with a four-term polynomial.
{
std::array<double, 2> COP = {0.0, 0.0};
std::array<double, 2> FOVsDegrees = {90.0, 90.0};
std::vector<double> coefficients = {1.0, 0.1, 0.01, 0.001};
VignetteRadialPolynomail vig(COP, FOVsDegrees, coefficients);
if (!isNear(vig.EvaluateAtPoint({0.0, 0.0}), 1.0)) {
return "VignetteRadialPolynomail (four terms): Expected 1.0 at (0.0,0.0)";
}
double expected = 1.0 + 0.1 + 0.01 + 0.001;
if (!isNear(vig.EvaluateAtPoint({1.0, 0.0}), expected)) {
return "VignetteRadialPolynomail (four terms): Expected " + std::to_string(expected) + " at (1.0,0.0)";
}
expected = 1.0 + 0.1*4 + 0.01*16 + 0.001*64;
if (!isNear(vig.EvaluateAtPoint({2.0, 0.0}), expected)) {
return "VignetteRadialPolynomail (four terms): Expected " + std::to_string(expected) + " at (2.0,0.0)";
}
}
return "";
}