Example of translating code from C# to C++
I have code in C# (esemples. cs). I'm trying to translate it to C++ and I have a couple of questions. I will try to explain the code itself first, then what I could "make up", and then the details that I would like to understand and "clarify".
Here is the code esemples. cs :
// Описание битого пикселя и набора его соседей с весовыми коэффициентами
public struct BadPixel
{
public int Index; // Теперь это первый два элемента массивы - X и Y
public InterpolationPixel[] InterpolationPixels; // Теперь это блоки по 3 значения, начиная с 3-го элемента массива (xi, yi, wi)
}
// Описание сосднего пикселя с битым и его весового коэффициента
public struct InterpolationPixel
{
public int Index; // Теперь это xi и yi
public float Weight; // Теперь это wi
}
// Метод создания структуры коррекции битвых пикселей изображения
public DetectorCalibration.InterpolationPixel[] FindBadPixelInterpolations(int index, int imageWidth, int imageHeight, ISet<int> badPixelSet) {
var interpolationPixels = new List<DetectorCalibration.InterpolationPixel>();
var x = index % imageWidth;
var y = index / imageWidth;
// Крест для поиска усреднения лево-право-верх-низ
var leftRight = new InterpolationLine(badPixelSet, index, x, imageWidth - x - 1, 1);
var topBottom = new InterpolationLine(badPixelSet, index, y, imageHeight - y - 1, imageWidth);
if (!AddInterpolationLines(interpolationPixels, leftRight, topBottom)) {
// Диагональный крест для усреднения
var diag1 = new InterpolationLine(badPixelSet, index, Math.Min(x, y), Math.Min(imageWidth - x - 1, imageHeight - y - 1), imageWidth + 1);
var diag2 = new InterpolationLine(badPixelSet, index, Math.Min(imageWidth - x - 1, y), Math.Min(x, imageHeight - y - 1), imageWidth - 1);
if (!AddInterpolationLines(interpolationPixels, diag1, diag2)) {
// Берем хоть какие-то точки из креста лево-право-верх-низ
var weight = 1f / (
(leftRight.Has1 ? 1f : 0f) +
(leftRight.Has2 ? 1f : 0f) +
(topBottom.Has1 ? 1f : 0f) +
(topBottom.Has2 ? 1f : 0f));
if (leftRight.Has1)
interpolationPixels.Add(new DetectorCalibration.InterpolationPixel {Index = leftRight.PixelIndex1, Weight = weight});
if (leftRight.Has2)
interpolationPixels.Add(new DetectorCalibration.InterpolationPixel {Index = leftRight.PixelIndex2, Weight = weight});
if (topBottom.Has1)
interpolationPixels.Add(new DetectorCalibration.InterpolationPixel {Index = topBottom.PixelIndex1, Weight = weight});
if (topBottom.Has2)
interpolationPixels.Add(new DetectorCalibration.InterpolationPixel
{Index = topBottom.PixelIndex2,
Weight = weight});
}
}
return interpolationPixels.ToArray();
}
// Внутренняя структура для алгоритма, описывающая одну линию интерполяции
private class InterpolationLine
{
public readonly float Distance1 = float.PositiveInfinity;
public readonly int PixelIndex1;
public readonly float Distance2 = float.PositiveInfinity;
public readonly int PixelIndex2;
public InterpolationLine(ISet<int> badPixelIndexes, int badPixelIndex, int maxDistance1, int maxDistance2, int step) {
maxDistance1 = Math.Min(MAX_DISTANCE, maxDistance1);
maxDistance2 = Math.Min(MAX_DISTANCE, maxDistance2);
var index = badPixelIndex;
for (var distance = 1; distance <= maxDistance1; distance++) {
index -= step;
if (!badPixelIndexes.Contains(index)) {
PixelIndex1 = index;
Distance1 = distance;
break;
}
}
index = badPixelIndex;
for (var distance = 1; distance <= maxDistance2; distance++) {
index += step;
if (!badPixelIndexes.Contains(index)) {
PixelIndex2 = index;
Distance2 = distance;
break;
}
}
}
public bool Has1 =>
MMath.IsFinite(Distance1);
public bool Has2 =>
MMath.IsFinite(Distance2);
public bool IsComplete =>
Has1 && Has2;
public float MaxDistance =>
Math.Max(Distance1, Distance2);
public float Weight1 =>
1f / (1f + Distance1 / Distance2);
public float Weight2 =>
1f / (1f + Distance2 / Distance1);
}
// Внутренний метод
private bool AddInterpolationLines(ICollection<DetectorCalibration.InterpolationPixel> interpolationPixels, InterpolationLine a, InterpolationLine b) {
if (a.IsComplete && b.IsComplete && Math.Abs(a.MaxDistance - b.MaxDistance) < DISTANCE_DIFFERENCE_THRESHOLD) {
// Усреднение крестом
interpolationPixels.Add(new DetectorCalibration.InterpolationPixel {Index = a.PixelIndex1, Weight = 0.5f * a.Weight1});
interpolationPixels.Add(new DetectorCalibration.InterpolationPixel {Index = a.PixelIndex2, Weight = 0.5f * a.Weight2});
interpolationPixels.Add(new DetectorCalibration.InterpolationPixel {Index = b.PixelIndex1, Weight = 0.5f * b.Weight1});
interpolationPixels.Add(new DetectorCalibration.InterpolationPixel {Index = b.PixelIndex2, Weight = 0.5f * b.Weight2});
}
else if (a.IsComplete && (!b.IsComplete || a.MaxDistance < b.MaxDistance)) {
// Усреднение по одному направлению
interpolationPixels.Add(new DetectorCalibration.InterpolationPixel {Index = a.PixelIndex1, Weight = a.Weight1});
interpolationPixels.Add(new DetectorCalibration.InterpolationPixel {Index = a.PixelIndex2, Weight = a.Weight2});
}
else if (b.IsComplete && (!a.IsComplete || b.MaxDistance < a.MaxDistance)) {
// Усреднение по другому направлению
interpolationPixels.Add(new DetectorCalibration.InterpolationPixel {Index = b.PixelIndex1, Weight = b.Weight1});
interpolationPixels.Add(new DetectorCalibration.InterpolationPixel {Index = b.PixelIndex2, Weight = b.Weight2});
}
return interpolationPixels.Count > 0;
}
Here's what I managed to "wave".
Here is the C++ header (esemples. h):
#include <vector>
#include <cmath>
#include <limits>
// Описание битого пикселя и набора его соседей с весовыми коэффициентами
class BadPixel
{
public:
int Index = 0; // Теперь это первый два элемента массивы - X и Y
std::vector<InterpolationPixel> InterpolationPixels; // Теперь это блоки по 3 значения, начиная с 3-го элемента массива (xi, yi, wi)
};
// Описание сосднего пикселя с битым и его весового коэффициента
class InterpolationPixel
{
public:
int Index = 0; // Теперь это xi и yi
float Weight = 0; // Теперь это wi
};
// Внутренняя структура для алгоритма, описывающая одну линию интерполяции
class InterpolationLine
{
public:
const float Distance1 = std::numeric_limits<float>::infinity();
const int PixelIndex1;
const float Distance2 = std::numeric_limits<float>::infinity();
const int PixelIndex2;
InterpolationLine(ISet<int> *badPixelIndexes, int badPixelIndex, int maxDistance1, int maxDistance2, int step);
bool getHas1() const;
bool getHas2() const;
bool getIsComplete() const;
float getMaxDistance() const;
float getWeight1() const;
float getWeight2() const;
};
// Метод создания структуры коррекции битвых пикселей изображения
public:
std::vector<DetectorCalibration::InterpolationPixel*> FindBadPixelInterpolations(int index, int imageWidth, int imageHeight, ISet<int> *badPixelSet);
// Внутренний метод
private:
bool AddInterpolationLines(ICollection<DetectorCalibration::InterpolationPixel*> *interpolationPixels, InterpolationLine *a, InterpolationLine *b);
Here is the executive (esemples.cpp):
#include "esemples.h"
?????? (int index, int imageWidth, int imageHeight, ISet<int> *badPixelSet)
{
auto interpolationPixels = std::vector<DetectorCalibration::InterpolationPixel*>();
auto x = index % imageWidth;
auto y = index / imageWidth;
// Крест для поиска усреднения лево-право-верх-низ
auto leftRight = new InterpolationLine(badPixelSet, index, x, imageWidth - x - 1, 1);
auto topBottom = new InterpolationLine(badPixelSet, index, y, imageHeight - y - 1, imageWidth);
if (!AddInterpolationLines(interpolationPixels, leftRight, topBottom))
{
// Диагональный крест для усреднения
auto diag1 = new InterpolationLine(badPixelSet, index, std::min(x, y), std::min(imageWidth - x - 1, imageHeight - y - 1), imageWidth + 1);
auto diag2 = new InterpolationLine(badPixelSet, index, std::min(imageWidth - x - 1, y), std::min(x, imageHeight - y - 1), imageWidth - 1);
if (!AddInterpolationLines(interpolationPixels, diag1, diag2))
{
// Берем хоть какие-то точки из креста лево-право-верх-низ
auto weight = 1.0f / ((leftRight->getHas1() ? 1.0f : 0.0f) + (leftRight->getHas2() ? 1.0f : 0.0f) + (topBottom->getHas1() ? 1.0f : 0.0f) + (topBottom->getHas2() ? 1.0f : 0.0f));
if (leftRight->getHas1())
{
DetectorCalibration::InterpolationPixel *tempVar = new DetectorCalibration::InterpolationPixel();
tempVar->Index = leftRight->PixelIndex1;
tempVar->Weight = weight;
interpolationPixels.push_back(tempVar);
}
if (leftRight->getHas2())
{
DetectorCalibration::InterpolationPixel *tempVar2 = new DetectorCalibration::InterpolationPixel();
tempVar2->Index = leftRight->PixelIndex2;
tempVar2->Weight = weight;
interpolationPixels.push_back(tempVar2);
}
if (topBottom->getHas1())
{
DetectorCalibration::InterpolationPixel *tempVar3 = new DetectorCalibration::InterpolationPixel();
tempVar3->Index = topBottom->PixelIndex1;
tempVar3->Weight = weight;
interpolationPixels.push_back(tempVar3);
}
if (topBottom->getHas2())
{
DetectorCalibration::InterpolationPixel *tempVar4 = new DetectorCalibration::InterpolationPixel();
tempVar4->Index = topBottom->PixelIndex2;
tempVar4->Weight = weight;
interpolationPixels.push_back(tempVar4);
}
}
}
return interpolationPixels.ToArray();
}
InterpolationLine::InterpolationLine(ISet<int> *badPixelIndexes, int badPixelIndex, int maxDistance1, int maxDistance2, int step)
{
maxDistance1 = std::min(MAX_DISTANCE, maxDistance1);
maxDistance2 = std::min(MAX_DISTANCE, maxDistance2);
auto index = badPixelIndex;
for (auto distance = 1; distance <= maxDistance1; distance++)
{
index -= step;
if (!badPixelIndexes->Contains(index))
{
PixelIndex1 = index;
Distance1 = distance;
break;
}
}
index = badPixelIndex;
for (auto distance = 1; distance <= maxDistance2; distance++)
{
index += step;
if (!badPixelIndexes->Contains(index))
{
PixelIndex2 = index;
Distance2 = distance;
break;
}
}
}
bool InterpolationLine::getHas1() const
{
return isfinite(Distance1);
}
bool InterpolationLine::getHas2() const
{
return isfinite(Distance2);
}
bool InterpolationLine::getIsComplete() const
{
return getHas1() && getHas2();
}
float InterpolationLine::getMaxDistance() const
{
return std::max(Distance1, Distance2);
}
float InterpolationLine::getWeight1() const
{
return 1.0f / (1.0f + Distance1 / Distance2);
}
float InterpolationLine::getWeight2() const
{
return 1.0f / (1.0f + Distance2 / Distance1);
}
?????? ( *interpolationPixels, InterpolationLine *a, InterpolationLine *b)
{
if (a->getIsComplete() && b->getIsComplete() && std::abs(a->getMaxDistance() - b->getMaxDistance()) < DISTANCE_DIFFERENCE_THRESHOLD)
{
// Усреднение крестом
DetectorCalibration::InterpolationPixel *tempVar = new DetectorCalibration::InterpolationPixel();
tempVar->Index = a->PixelIndex1;
tempVar->Weight = 0.5f * a->getWeight1();
interpolationPixels->Add(tempVar);
DetectorCalibration::InterpolationPixel *tempVar2 = new DetectorCalibration::InterpolationPixel();
tempVar2->Index = a->PixelIndex2;
tempVar2->Weight = 0.5f * a->getWeight2();
interpolationPixels->Add(tempVar2);
DetectorCalibration::InterpolationPixel *tempVar3 = new DetectorCalibration::InterpolationPixel();
tempVar3->Index = b->PixelIndex1;
tempVar3->Weight = 0.5f * b->getWeight1();
interpolationPixels->Add(tempVar3);
DetectorCalibration::InterpolationPixel *tempVar4 = new DetectorCalibration::InterpolationPixel();
tempVar4->Index = b->PixelIndex2;
tempVar4->Weight = 0.5f * b->getWeight2();
interpolationPixels->Add(tempVar4);
}
else if (a->getIsComplete() && (!b->getIsComplete() || a->getMaxDistance() < b->getMaxDistance()))
{
// Усреднение по одному направлению
DetectorCalibration::InterpolationPixel *tempVar5 = new DetectorCalibration::InterpolationPixel();
tempVar5->Index = a->PixelIndex1;
tempVar5->Weight = a->getWeight1();
interpolationPixels->Add(tempVar5);
DetectorCalibration::InterpolationPixel *tempVar6 = new DetectorCalibration::InterpolationPixel();
tempVar6->Index = a->PixelIndex2;
tempVar6->Weight = a->getWeight2();
interpolationPixels->Add(tempVar6);
}
else if (b->getIsComplete() && (!a->getIsComplete() || b->getMaxDistance() < a->getMaxDistance()))
{
// Усреднение по другому направлению
DetectorCalibration::InterpolationPixel *tempVar7 = new DetectorCalibration::InterpolationPixel();
tempVar7->Index = b->PixelIndex1;
tempVar7->Weight = b->getWeight1();
interpolationPixels->Add(tempVar7);
DetectorCalibration::InterpolationPixel *tempVar8 = new DetectorCalibration::InterpolationPixel();
tempVar8->Index = b->PixelIndex2;
tempVar8->Weight = b->getWeight2();
interpolationPixels->Add(tempVar8);
}
return interpolationPixels->Count > 0;
}
My problem is as follows it is correct to translate methods to C++, as well as what to do with the created variables tempVar0 ... tempVar8
is it worth using memory cleaning ? and how to use it: 3
here on c sharp :
public DetectorCalibration.InterpolationPixel[] FindBadPixelInterpolations(int index, int imageWidth, int imageHeight, ISet<int> badPixelSet)
private bool AddInterpolationLines(ICollection<DetectorCalibration.InterpolationPixel> interpolationPixels, InterpolationLine a, InterpolationLine b)
And how it should look in c ++, I do not quite understand ;_;
?????? (int index, int imageWidth, int imageHeight, ISet<int> *badPixelSet)
?????? (*interpolationPixels, InterpolationLine *a, InterpolationLine *b)
1