working linear system gui
Some checks failed
Linux arm64 / Build (push) Has been cancelled

This commit is contained in:
2024-05-23 17:18:22 +02:00
parent 9b84b9bf59
commit 49e4d4b1e2

View File

@@ -14,6 +14,32 @@ struct GuiMatrix {
int matrixSizeY = 4; int matrixSizeY = 4;
}; };
static void ResizeGuiMatrix(bool refresh, GuiMatrix& guiMatrix) {
if (refresh) {
guiMatrix.matrixValues.resize(guiMatrix.matrixSizeY);
for (auto& row : guiMatrix.matrixValues) {
row.resize(guiMatrix.matrixSizeX, 0);
}
}
}
static void RenderMatrix(bool& refresh, GuiMatrix& guiMatrix) {
ResizeGuiMatrix(refresh, guiMatrix);
for (int y = 0; y < guiMatrix.matrixSizeY; y++) {
for (int x = 0; x < guiMatrix.matrixSizeX; x++) {
if (x > 0)
ImGui::SameLine();
ImGui::PushID((guiMatrix.matrixSizeX * 20 + guiMatrix.matrixSizeY * 50) + y * guiMatrix.matrixSizeX + x);
ImGui::PushItemWidth(60); // Adjust this value to change the cell size
if (ImGui::InputInt("", &guiMatrix.matrixValues[y][x], 0, 0, ImGuiInputTextFlags_CharsDecimal))
refresh = true;
ImGui::PopItemWidth();
ImGui::PopID();
}
}
}
static Matrix LoadMatrixFromStdVect(const std::vector<std::vector<int>>& data) { static Matrix LoadMatrixFromStdVect(const std::vector<std::vector<int>>& data) {
Matrix result {data.size(), data.empty() ? 0 : data[0].size()}; Matrix result {data.size(), data.empty() ? 0 : data[0].size()};
for (std::size_t i = 0; i < result.GetRawCount(); i++) { for (std::size_t i = 0; i < result.GetRawCount(); i++) {
@@ -30,6 +56,19 @@ static std::string ElementToString(Matrix::Element e) {
return ss.str(); return ss.str();
} }
static std::string PrintRawMatrix(const Matrix& mat) {
if (mat.GetRawCount() == 0)
return "";
std::string result = " ( ";
for (std::size_t j = 0; j < mat.GetRawCount(); j++) {
result += ElementToString(mat.at(j, 0)) + ", ";
}
result = result.substr(0, result.size() - 2);
result += " )";
return result;
}
static std::string PrintVect(const Vect& vect) { static std::string PrintVect(const Vect& vect) {
if (vect.GetCardinal() == 0) if (vect.GetCardinal() == 0)
return "{0}"; return "{0}";
@@ -37,12 +76,8 @@ static std::string PrintVect(const Vect& vect) {
std::string result = "Vect( "; std::string result = "Vect( ";
for (std::size_t i = 0; i < vect.GetCardinal(); i++) { for (std::size_t i = 0; i < vect.GetCardinal(); i++) {
Matrix vector = vect.GetVector(i); Matrix vector = vect.GetVector(i);
result += " ("; result += PrintRawMatrix(vector);
for (std::size_t j = 0; j < vect.GetDimension(); j++) { result += ", ";
result += ElementToString(vector.at(j, 0)) + ", ";
}
result = result.substr(0, result.size() - 2);
result += " ), ";
} }
result = result.substr(0, result.size() - 2); result = result.substr(0, result.size() - 2);
result += " )"; result += " )";
@@ -51,7 +86,77 @@ static std::string PrintVect(const Vect& vect) {
void PivotGui::Init() {} void PivotGui::Init() {}
static void RenderSystemTab() {} static void RenderLeftSystemChild(bool& refresh, GuiMatrix& system, GuiMatrix& origin) {
ImVec2 topLeftWindowSize(ImGui::GetContentRegionAvail().x * 0.5f, 0);
ImGui::BeginChild("Left Child", topLeftWindowSize, ImGuiChildFlags_Border);
ImGui::Text("Système de la forme AX=B");
ImGui::Separator();
ImGui::Text("Taille matrice A :");
if (ImGui::InputInt("##RowsMatriceInitiale", &system.matrixSizeY))
refresh = true;
system.matrixSizeY = std::max(1, system.matrixSizeY);
ImGui::SameLine();
ImGui::Text("Lignes");
if (ImGui::InputInt("##ColumnsMatriceInitiale", &system.matrixSizeX))
refresh = true;
system.matrixSizeX = std::max(1, system.matrixSizeX);
ImGui::SameLine();
ImGui::Text("Colonnes");
ImGui::NewLine();
RenderMatrix(refresh, system);
if (refresh) {
origin.matrixSizeX = 1;
origin.matrixSizeY = system.matrixSizeY;
}
ImGui::NewLine();
ImGui::Separator();
ImGui::Text("Matrice B :");
ImGui::NewLine();
RenderMatrix(refresh, origin);
ImGui::EndChild();
}
static void RenderRightSystemChild(bool& refresh, GuiMatrix& system, GuiMatrix& origin) {
ImGui::BeginChild("Right Child", {0, 0}, ImGuiChildFlags_Border);
static std::string result = "";
static Solver solver;
if (refresh) {
VectAffine solutions =
solver.RectangularSystem(LoadMatrixFromStdVect(system.matrixValues), LoadMatrixFromStdVect(origin.matrixValues));
result = "Solutions :\n";
result += PrintVect(solutions.GetBase());
result += "\n\n+\n\n";
result += PrintRawMatrix(solutions.GetOrigin());
}
ImGui::TextWrapped("%s", result.c_str());
ImGui::EndChild();
}
static void RenderSystemTab() {
static GuiMatrix guiMatrix, originMatrix;
static bool refresh = true;
RenderLeftSystemChild(refresh, guiMatrix, originMatrix);
ImGui::SameLine();
RenderRightSystemChild(refresh, guiMatrix, originMatrix);
}
static void RenderLeftGaussChild(bool& refresh, GuiMatrix& guiMatrix) { static void RenderLeftGaussChild(bool& refresh, GuiMatrix& guiMatrix) {
// divisions des fenetres // divisions des fenetres
@@ -89,25 +194,7 @@ static void RenderLeftGaussChild(bool& refresh, GuiMatrix& guiMatrix) {
// Resize matrixValues and initialize new elements to 0 // Resize matrixValues and initialize new elements to 0
if (refresh) { RenderMatrix(refresh, guiMatrix);
guiMatrix.matrixValues.resize(guiMatrix.matrixSizeY);
for (auto& row : guiMatrix.matrixValues) {
row.resize(guiMatrix.matrixSizeX, 0);
}
}
for (int y = 0; y < guiMatrix.matrixSizeY; y++) {
for (int x = 0; x < guiMatrix.matrixSizeX; x++) {
if (x > 0)
ImGui::SameLine();
ImGui::PushID(y * guiMatrix.matrixSizeX + x);
ImGui::PushItemWidth(60); // Adjust this value to change the cell size
if (ImGui::InputInt("", &guiMatrix.matrixValues[y][x], 0, 0, ImGuiInputTextFlags_CharsDecimal))
refresh = true;
ImGui::PopItemWidth();
ImGui::PopID();
}
}
// ImGui::EndChild(); // End Matrice initiale // ImGui::EndChild(); // End Matrice initiale
@@ -166,7 +253,7 @@ static void RenderRightGaussChild(bool& refresh, GuiMatrix& guiMatrix) {
equationsResultImage = equationsResultImage.substr(0, equationsResultImage.size() - 3) + " = 0\n"; equationsResultImage = equationsResultImage.substr(0, equationsResultImage.size() - 3) + " = 0\n";
} }
result = std::string("Noyau: ") + "\n" + PrintVect(solver.Kernel(LoadMatrixFromStdVect(guiMatrix.matrixValues))) + "\n" + result = "\nNoyau: \n" + PrintVect(solver.Kernel(LoadMatrixFromStdVect(guiMatrix.matrixValues))) + "\n" +
"\n" + "Rang: " + "\n" + std::to_string(solver.Rank(LoadMatrixFromStdVect(guiMatrix.matrixValues))) + "\n" + "\n" + "\n" + "Rang: " + "\n" + std::to_string(solver.Rank(LoadMatrixFromStdVect(guiMatrix.matrixValues))) + "\n" + "\n" +
"Image: " + "\n" + PrintVect(image); "Image: " + "\n" + PrintVect(image);
} }