How can I "join" quadratic or cubic splines? - c++

I have 2 function to either calculate a point on a spline, quadratic or cubic:
struct vec2 {float x, y;};
vec2 spline_quadratic(vec2 & a, vec2 & b, vec2 & c, float t) {
return {
(1 - t) * (1 - t) * p1.x + 2 * (1 - t) * t * p2.x + t * t * p3.x,
(1 - t) * (1 - t) * p1.y + 2 * (1 - t) * t * p2.y + t * t * p3.y
};
}
vec2 spline_cubic(vec2 & a, vec2 & b, vec2 & c, vec2 & d, float t){
return {
//B(t) = (1-t)**3 p0 + 3(1 - t)**2 t P1 + 3(1-t)t**2 P2 + t**3 P3
(1 - t) * (1 - t) * (1 - t) * p1.x + 3 * (1 - t) * (1 - t) * t * p2.x + 3 * (1 - t) * t * t * p3.x + t * t * t * p4.x,
(1 - t) * (1 - t) * (1 - t) * p1.y + 3 * (1 - t) * (1 - t) * t * p2.y + 3 * (1 - t) * t * t * p3.y + t * t * t * p4.y
};
Is it possible to join several curves of an array of points?
I'm looking to make a function that has this signature:
vector<vec2> spline_join(vector<vec2> & points, int segments = 16){
vector<vec2> spline_points;
for(int i = 0; i < points.size()-2; ++i){
for(int div = 0; div < segments; ++div){
spline_points.push_back(spline_quadratic(points[0], points[1], points[2], 1.f/segments);
}
}
}
I've read that it requires interpolation, but I'm not sure... What would the code look like? I've searched and I can't find relevant question and answers...
I've seen there are libraries, but I'm looking for a shorter implementation.
Edit: I've tried the question and answer here and apparently this is what I want:
Joining B-Spline segments in OpenGL / C++
The code is not really clean but after some cleaning, it does work.

I've cleaned this answer Joining B-Spline segments in OpenGL / C++
This is not an Hermite spline, an hermite spline passes through the points, a B-spline does not.
Here is what worked and the result
float B0(float u) {
//return float(pow(u - 1, 3) / 6.0);
// (1-t)*(1-t)*(1-t)/6.f
return float(pow(1-u, 3) / 6.0);
}
float B1(float u) {
return float((3 * pow(u, 3) - 6 * pow(u, 2) + 4) / 6.0);
// (3 * t * t * t - 6 * t * t + 4) / 6
}
float B2(float u) {
return float((-3 * pow(u, 3) + 3 * pow(u, 2) + 3 * u + 1) / 6.0);
// (-3 * t * t * t + 3 * t * t + 3 * t + 1) / 6
}
float B3(float u) {
return float(pow(u, 3) / 6.0);
// t * t * t / 6
}
vector<Vec2> computeBSpline(vector<Vec2>& points) {
vector<Vec2> result;
int MAX_STEPS = 100;
int NUM_OF_POINTS = points.size();
for (int i = 0; i < NUM_OF_POINTS - 3; i++)
{
//cout << "Computing for P" << i << " P " << i + 1 << " P " << i + 2 << " P " << i + 3 << endl;
for (int j = 0; j <= MAX_STEPS; j++)
{
float u = float(j) / float(MAX_STEPS);
float Qx =
B0(u) * points[i].x
+ B1(u) * points[i + 1].x
+ B2(u) * points[i + 2].x
+ B3(u) * points[i + 3].x;
float Qy =
B0(u) * points[i].y
+ B1(u) * points[i + 1].y
+ B2(u) * points[i + 2].y
+ B3(u) * points[i + 3].y;
result.push_back({ Qx, Qy });
//cout << count << '(' << Qx << ", " << Qy << ")\n";
}
}
return result;
}

Related

Converting RGB to Luv

i'm trying to convert an rgb image to Luv, i have some problem. The L component is good, but when i show the u and v component both are black(all pixels have value 0).
for (int i = 0; i<height; i++)
for (int j = 0; j<width; j++)
{
Vec3b v3 = src.at<Vec3b>(i, j);
float b = ((float)v3[0]) / 255;
float g = ((float)v3[1]) / 255;
float r = ((float)v3[2]) / 255;
float x = r * 0.412453 + g * 0.357580 + b * 0.180423;
float y = r * 0.212671 + g * 0.715160 + b * 0.072169;
float z = r * 0.019334 + g * 0.119193 + b * 0.950227;
//L
if (y > 0.008856) {
l_mat.at<uchar>(i, j) = 255 / 100 * (116 * pow(y, 1.0 / 3.0));
dst.at<Vec3b>(i, j)[0] = 255 / 100 * (116 * pow(y, 1.0 / 3.0));
// printf("%d / " , l_mat.at<uchar>(i, j));
}
else {
l_mat.at<uchar>(i, j) = 255 / 100 * (903.3 * y);
dst.at<Vec3b>(i, j)[0] = 255 / 100 * (903.3 * y);
}
float u = 4 * x / (x + 15 * y + 3 * z);
float v = 9 * y / (x + 15 * y + 3 * z);
//printf("u: %.2f , v:%.2f || ", u, v);
//U
u_mat.at<uchar>(i, j) = 255 / 354 * (13 * l_mat.at<uchar>(i, j)*(u - 0.19793943) + 134);
//printf("%d / ", u_mat.at<uchar>(i, j));
dst.at<Vec3b>(i, j) = 255 / 354 * (13 * l_mat.at<uchar>(i, j)*(u - 0.19793943) + 134);
//v
v_mat.at<uchar>(i, j) = 255 / 262 * (13 * l_mat.at<uchar>(i, j)*(v - 0.46831096)+140);
dst.at<Vec3b>(i, j) = 255 / 262 * (13 * l_mat.at<uchar>(i, j)*(v - 0.46831096) + 140);
}
I have to do the conversions pixel by pixel, i can't use cvtcolor.

Inverse of Cumulative Normal Distribution Function with parameters

I want to implement equivalent of matlab icdf function in C++, I have already found this useful post: https://www.johndcook.com/blog/cpp_phi_inverse/. But I want it with optional mu and sigma parameters as in matlab.
What I am supposed to change?
Inspired from https://gist.github.com/kmpm/1211922/6b7fcd0155b23c3dc71e6f4969f2c48785371292:
double inverse_of_normal_cdf(const double p, const double mu, const double sigma)
{
if (p <= 0.0 || p >= 1.0)
{
std::stringstream os;
os << "Invalid input argument (" << p
<< "); must be larger than 0 but less than 1.";
throw std::invalid_argument(os.str());
}
double r, val;
const double q = p - 0.5;
if (std::abs(q) <= .425) {
r = .180625 - q * q;
val =
q * (((((((r * 2509.0809287301226727 +
33430.575583588128105) * r + 67265.770927008700853) * r +
45921.953931549871457) * r + 13731.693765509461125) * r +
1971.5909503065514427) * r + 133.14166789178437745) * r +
3.387132872796366608)
/ (((((((r * 5226.495278852854561 +
28729.085735721942674) * r + 39307.89580009271061) * r +
21213.794301586595867) * r + 5394.1960214247511077) * r +
687.1870074920579083) * r + 42.313330701600911252) * r + 1);
}
else {
if (q > 0) {
r = 1 - p;
}
else {
r = p;
}
r = std::sqrt(-std::log(r));
if (r <= 5)
{
r += -1.6;
val = (((((((r * 7.7454501427834140764e-4 +
.0227238449892691845833) * r + .24178072517745061177) *
r + 1.27045825245236838258) * r +
3.64784832476320460504) * r + 5.7694972214606914055) *
r + 4.6303378461565452959) * r +
1.42343711074968357734)
/ (((((((r *
1.05075007164441684324e-9 + 5.475938084995344946e-4) *
r + .0151986665636164571966) * r +
.14810397642748007459) * r + .68976733498510000455) *
r + 1.6763848301838038494) * r +
2.05319162663775882187) * r + 1);
}
else { /* very close to 0 or 1 */
r += -5;
val = (((((((r * 2.01033439929228813265e-7 +
2.71155556874348757815e-5) * r +
.0012426609473880784386) * r + .026532189526576123093) *
r + .29656057182850489123) * r +
1.7848265399172913358) * r + 5.4637849111641143699) *
r + 6.6579046435011037772)
/ (((((((r *
2.04426310338993978564e-15 + 1.4215117583164458887e-7) *
r + 1.8463183175100546818e-5) * r +
7.868691311456132591e-4) * r + .0148753612908506148525)
* r + .13692988092273580531) * r +
.59983220655588793769) * r + 1);
}
if (q < 0.0) {
val = -val;
}
}
return mu + sigma * val;
}

C++ for loop stops before reaching end and restarts

I've been poking my nose into working with arrays in c++ and I've been writing a 1D Euler solver code that I wrote in matlab and converting it to c++ as a practice exercise.
This issue is that this for loop is supposed to run until the counter i reaches N_cells-1 but no matter how high I set the number, it always gets to 57, then restarts from 2 and continues doing this until I click on the output screen. I also ran the code with an N_cells number less than 57 and I get an error code which I've included below.
I'm pretty new to arrays and header files in c++ so I'm sure it's something simple, but I just can't find it. I know it's related to the fqL array but I don't know what.
Error when number <57 is used:
#include "stdafx.h"
#include "Flux.h"
#include <iostream>
#include <chrono>
using namespace std;
void Flux(double * q, double y, double R, int N_cells,double * Flux)
{
double qL[3];
double qR[3];
for (int i = 0; i < N_cells - 1; i++) {
//Initialize left and right sides
//-------------------
qL[0] = q[0, i];
qL[1] = q[1, i];
qL[2] = q[2, i];
qR[0] = q[0, i + 1];
qR[1] = q[1, i + 1];
qR[2] = q[2, i + 1];
//-------------------
//Calculate Left side Parameters
//-------------------
double PL;
//double fqL[3];
double cL2;
double HL;
double uL;
PL = (y - 1)*(qL[2] - 0.5 / qL[0] * (qL[1] * qL[1]));
double fqL[3] = { qL[1],
(3 - y)*0.5*(qL[1] * qL[1]) / qL[0] + (y - 1)*qL[2],
y*qL[1] * qL[2] / qL[0] - (y - 1)*0.5*(qL[1] * qL[1] * qL[1]) / (qL[0] * qL[0]) };
cL2 = y * (y - 1)*(qL[2] / qL[0] - 0.5*(qL[1] / qL[0])*(qL[1] / qL[0]));
HL = 0.5*(qL[1] / qL[0])*(qL[1] / qL[0]) + cL2 / (y - 1);
uL = qL[1] / qL[0];
//Calculate Right side Parameters
//-------------------
double PR;
//double fqR[3];
double cR2;
double HR;
double uR;
PR = (y - 1)*(qR[2] - 0.5 / qR[0] * (qR[1] * qR[1]));
double fqR[3] = { qR[1],
(3 - y)*0.5*(qR[1] * qR[1]) / qR[0] + (y - 1)*qR[2],
y*qR[1] * qR[2] / qR[0] - (y - 1)*0.5*(qR[1] * qR[1] * qR[1]) / (qR[0] * qR[0]) };
cR2 = y * (y - 1)*(qR[2] / qR[0] - 0.5*(qR[1] / qR[0])*(qR[1] / qR[0]));
HR = 0.5*(qR[1] / qR[0])*(qR[1] / qR[0]) + cR2 / (y - 1);
uR = qR[1] / qR[0];
//-------------------
//Calculate Roe's Variables
//-------------------------------- -
double u;
double H;
double c;
double rho;
u = (sqrt(qL[1])*qL[2] / qL[1] + sqrt(qR[1])*qR[2] / qR[1]) / (sqrt(qL[1]) + sqrt(qR[1]));
H = (sqrt(qL[1])*HL + sqrt(qR[1])*HR) / (sqrt(qL[1]) + sqrt(qR[1]));
c = sqrt((y - 1)*(H - 0.5*u *u));
rho = sqrt(qL[1] * qR[1]);
//-------------------------------- -
//-------------------------------- -
double g[3] = { u - c, u, u + c };
double v[3][3] = { {1, u - c, H - u * c},
{1, u, 0.5*u*u},
{1, u + c, H + u * c } };
double a[3] = { 0.5 / (c*c)*((PR - PL) - c * rho*(uR - uL)),
(qR[0] - qL[0]) - 1 * (PR - PL) / (c*c),
0.5 / (c*c)*((PR - PL) + c * rho*(uR - uL)) };
double SUM[3];
SUM[0] = g[0] * a[0] * v[0][0] + g[1] * a[1] * v[1][0] + g[2] * a[2] * v[2][0];
SUM[1] = g[0] * a[0] * v[0][1] + g[1] * a[1] * v[1][1] + g[2] * a[2] * v[2][1];
SUM[2] = g[0] * a[0] * v[0][2] + g[1] * a[1] * v[1][2] + g[2] * a[2] * v[2][2];
double Flux[3];
Flux[0,i] = 0.5*(fqL[0] + fqR[0]) - 0.5*SUM[0];
Flux[1,i] = 0.5*(fqL[1] + fqR[1]) - 0.5*SUM[1];
Flux[2,i] = 0.5*(fqL[2] + fqR[2]) - 0.5*SUM[2];
std::cout << i << endl;
}
}

B-spline Curve in c++

Could anyone help me about B-spline Curve error?
I want to draw B-spline Curve in c++, but even though all coordinates are positive, the segment's coordinate is negative.
This is B-spline Curve code.
void BSplineCurve(Dot &ControlPoint1, Dot &ControlPoint2,
Dot &ControlPoint3,Dot &ControlPoint4,
Dot &DrawCurve, double &t){
double t2 = t * t;
double t3 = t2 * t;
double mt3 = (1 - t) * (1 - t) * (1 - t);
double bi3 = mt3 / 6;
double bi2 = ((3 * t3) - (6 * t2) + 4) / 6;
double bi1 = ((-3 * t3) + (3 * t2) + (3 * t) + 1) / 6;
double bi = mt3 / 6;
DrawCurve.x = ControlPoint1.x * bi3;
DrawCurve.x += ControlPoint2.x * bi2;
DrawCurve.x += ControlPoint3.x * bi1;
DrawCurve.x += ControlPoint4.x * bi;
DrawCurve.y = ControlPoint1.y * bi3;
DrawCurve.y += ControlPoint2.y * bi2;
DrawCurve.y += ControlPoint3.y * bi1;
DrawCurve.y += ControlPoint4.y * bi;
}
This is Drawing Code.
double t = 3.f;
do{
if ((3 < t) && (t <= 4)) {
BSplineCurve(ControlPoint1, ControlPoint2, ControlPoint3, ControlPoint4, DrawCurve, t);
Draw1Dot(DrawCurve.x, DrawCurve.y, DrawCurve.R, DrawCurve.G, DrawCurve.B);
}
else if ((4 < t) && (t <= 5)) {
BSplineCurve(ControlPoint2, ControlPoint3, ControlPoint4, ControlPoint5, DrawCurve, t);
Draw1Dot(DrawCurve.x, DrawCurve.y, DrawCurve.R, DrawCurve.G, DrawCurve.B);
}
else if ((5 < t) && (t <= 6)) {
BSplineCurve(ControlPoint3, ControlPoint4, ControlPoint5, ControlPoint6, DrawCurve, t);
Draw1Dot(DrawCurve.x, DrawCurve.y, DrawCurve.R, DrawCurve.G, DrawCurve.B);
}
t += 0.001;
} while(t < 6.001);
This is Control Point's coordinate.
Poiont1 : 50, 50
Poiont2 : 50, 100
Poiont3 : 200, 100
Poiont4 : 200, 50
Poiont5 : 350, 50
Poiont6 : 350, 100
But this is 1st segment's coordinate.
Q3 : -1543, -349
Your drawing code looks wrong.
In function BSplineCurve the t parameter should take values in [0, 1] range. By changing t from 0 to 1 one will build a cubic B-spline between points ControlPoint2 and ControlPoint3.
You could try something like:
Dot points[6] = {ControlPoint1, ControlPoint2, ControlPoint3, ControlPoint4, ControlPoint5, ControlPoint6};
for(double t = 3.0; t < 6.0; t += 0.001)
{
const int start = static_cast<int>(t);
BSplineCurve(points[start - 3],
points[start - 2],
points[start - 1],
points[start],
DrawCurve,
t - start);
Draw1Dot(DrawCurve.x, DrawCurve.y, DrawCurve.R, DrawCurve.G, DrawCurve.B);
}
Update
Your B-spline calculation code looks wrong too :-)
bi should be t3/6.0 and not mt3/6.0. See here (slide 25).
The fixed function can look something like this (I did not test it):
void BSplineCurve(const Dot &point1,
const Dot &point2,
const Dot &point3,
const Dot &point4,
const double t,
Dot &result)
{
const double t2 = t * t;
const double t3 = t2 * t;
const double mt = 1.0 - t;
const double mt3 = mt * mt * mt;
const double bi3 = mt3;
const double bi2 = 3 * t3 - 6 * t2 + 4;
const double bi1 =-3 * t3 + 3 * t2 + 3 * t + 1;
const double bi = t3;
result.x = point1.x * bi3 +
point2.x * bi2 +
point3.x * bi1 +
point4.x * bi;
result.x /= 6.0;
result.y = point1.y * bi3 +
point2.y * bi2 +
point3.y * bi1 +
point4.y * bi;
result.y /= 6.0;
}
Maybe the point you use is too very close. In spline it is not good iidea to use very close point. becaue so we have very "galloping" curve. Like this:
red is original

What is wrong with my perlin noise generator?

I am trying to generate perlin noise for a math essay for school, and i have some difficulties figuring out the math behind it. This is my perlin class. The perlin noise function generates ( should generate) a number between 0 and 1, that i then multiply by 255 to apply color to every pixel on the screen, please help!
#include "perlinnoise.h"
perlinnoise::perlinnoise()
{
srand(time(NULL));
double random = rand() % 1000;
for (int i = 0; i < (651 * 2); i = i + 2)
{
random = (rand() % 1000);
vecGrad[i] = random / 1000;
vecGrad[i + 1] = vecGrad[i];
vecGrad[i] = cos(vecGrad[i] * 2 * 3.1416);
vecGrad[i + 1] = sin(vecGrad[i + 1] * 2 * 3.1416);
}
}
int perlinnoise::perlinNoise(int x, int y)
{
//20 pixel in each case
//30 boxes in width and 20 boxes in height
//651 vectors to create
sf::Vector2i boxXY;
boxXY.x = ((x / 20));
boxXY.y = ((y / 20));
sf::Vector2i displacement1; displacement1.x = x - boxXY.x * 20; displacement1.y = y - boxXY.y * 20;
sf::Vector2i displacement2; displacement2.x = x - (boxXY.x * 20 + 20); displacement2.y = y - boxXY.y * 20;
sf::Vector2i displacement3; displacement3.x = x - boxXY.x * 20; displacement3.y = y - (boxXY.y * 20 + 20);
sf::Vector2i displacement4; displacement4.x = x - (boxXY.x * 20 + 20); displacement4.y = y - (boxXY.y * 20 + 20);
/*std::cout << displacement1.x << std::endl; std::cout << displacement1.y << std::endl;
std::cout << displacement2.x << std::endl; std::cout << displacement2.y << std::endl;
std::cout << displacement3.x << std::endl; std::cout << displacement3.y << std::endl;
std::cout << displacement4.x << std::endl; std::cout << displacement4.y << std::endl;*/
double dotP1 = (vecGrad[((boxXY.y * 30) + boxXY.x)] * displacement1.x) + (vecGrad[(boxXY.y * 30) + boxXY.x + 1] * displacement1.y);
double dotP2 = (vecGrad[((boxXY.y * 30) + boxXY.x + 3)] * displacement2.x) + (vecGrad[(boxXY.y * 30) + boxXY.x + 4] * displacement2.y);
double dotP3 = (vecGrad[((boxXY.y * 30 + 1) + boxXY.x)] * displacement3.x) + (vecGrad[(boxXY.y * 30) + boxXY.x + 1] * displacement3.y);
double dotP4 = (vecGrad[((boxXY.y * 30 + 1) + boxXY.x + 3)] * displacement4.x) + (vecGrad[(boxXY.y * 30) + boxXY.x + 4] * displacement4.y);
This is where i have some troubles ( I think)
int intensity = 0;
double Sx = (3 * (x - boxXY.x * 20) * (x - boxXY.x * 20)) - (2 * (x - boxXY.x * 20) * (x - boxXY.x * 20) * (x - boxXY.x * 20));
double Sy = (3 * (y - boxXY.y * 20) * (y - boxXY.y * 20)) - (2 * (y - boxXY.y * 20) * (y - boxXY.y * 20) * (y - boxXY.y * 20));
double a = dotP1 + (Sx * (dotP2 - dotP1));
double b = dotP3 + (Sx * (dotP4 - dotP3));
double aa = dotP1 + (Sy * (dotP2 - dotP1));
double bb = dotP3 + (Sy * (dotP4 - dotP3));
intensity = (a+b+aa+bb)/4;
//Should generate number between 0 and 1, but doesn't :/
return intensity;
}
perlinnoise::~perlinnoise()
{
}
I've been reading lots of articles, and they are all very unclear about the math used.I ended up generating a grid with 20*20 pixels in each, with each cross section in the grid having a randomly generated gradient vector. I then calculate the displacement vectors and then do the dot product on the four corners with displacement and gradient vectors. This first part is a bit messy as i am not very experienced, but the last part is a bit more straightforward. I use a smoothing function on the x and y axis and use that number to generate a, b, aa and bb, and i then take the average of that. This is what i thought i understood from the articles i read, but apparently it's wrong :/ Any help please?
Thanks in advance!