Unverified Commit 60e884f2 authored by kledmundson's avatar kledmundson Committed by GitHub
Browse files

Algebra has been refactored to be callable; old Makefile tests have been...

Algebra has been refactored to be callable; old Makefile tests have been converted to gtests and removed. Addresses #5594. (#5597)
parent 3ecfce3c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ release.
- Added backplane options for SunIllumination and SurfaceObliqueDetectorResolution to phocube [#5467](https://github.com/DOI-USGS/ISIS3/issues/5467)

### Changed
- Algebra has been refactored to be callable; old Makefile tests have been removed and replaced by gtests. Issue: [#5594](https://github.com/USGS-Astrogeology/ISIS3/issues/5594)
- Photrim has been refactored to be callable; old Makefile tests have been removed and replaced by gtests. Issue: [#5581](https://github.com/USGS-Astrogeology/ISIS3/issues/5581)
- Bandtrim has been refactored to be callable; old Makefile tests have been removed and replaced by gtests. Issue: [#5571](https://github.com/USGS-Astrogeology/ISIS3/issues/5571)
- Modified kaguyasp2isis to work with new (detached) data [#5436](https://github.com/DOI-USGS/ISIS3/issues/5436)
+178 −0
Original line number Diff line number Diff line
/** This is free and unencumbered software released into the public domain.

The authors of ISIS do not claim copyright on the contents of this file.
For more details about the LICENSE terms and the AUTHORS, you will
find files of those names at the top level of this repository. **/

/* SPDX-License-Identifier: CC0-1.0 */

#include "algebra.h"

#include "Cube.h"
#include "FileName.h"
#include "ProcessByLine.h"
#include "SpecialPixel.h"

namespace Isis {

  /*
   * Algebra
   *
   * This program performs simple algebra on either one or two
   * cubes. The two cubes may be added, subtracted, multiplied or divided.
   * 
   * The following equations are used:
   *     UNARY:      out = (A * from1) + C
   *     ADD:        out = ((from1 - D) * A) + ((from2 - E) * B) + C
   *     SUBTRACT:   out = ((from1 - D) * A) - ((from2 - E) * B) + C
   *     MULTIPLY:   out = ((from1 - D) * A) * ((from2 - E) * B) + C
   *     DIVIDE:     out = ((from1 - D) * A) / ((from2 - E) * B) + C   
   *
   * The FROM2 cube must have either one band or the same number of bands
   * as the FROM cube. If the FROM2 cube has one band, then the algebraic
   * formula will be applied to all bands in FROM using that single band
   * in FROM2. If FROM2 is a multi-band cube, the algebra will be performed
   * between corresponding bands from FROM and FROM2.
   * 
   * @param ui UserInterface object containing parameters
   */
   void algebra(UserInterface &ui) {
    Cube* icube1 = new Cube();
    icube1->open(ui.GetCubeName("FROM"));

    Cube* icube2 = nullptr;
     if(ui.WasEntered("FROM2")) {
       icube2 = new Cube();
       icube2->open(ui.GetCubeName("FROM2"));
     }

    algebra(icube1, ui, icube2);
  }


  /*
   * Algebra
   *
   * This program performs simple algebra on either one or two
   * cubes. The two cubes may be added, subtracted, multiplied or divided.
   * 
   * The following equations are used:
   *     UNARY:      out = (A * from1) + C
   *     ADD:        out = ((from1 - D) * A) + ((from2 - E) * B) + C
   *     SUBTRACT:   out = ((from1 - D) * A) - ((from2 - E) * B) + C
   *     MULTIPLY:   out = ((from1 - D) * A) * ((from2 - E) * B) + C
   *     DIVIDE:     out = ((from1 - D) * A) / ((from2 - E) * B) + C   
   *
   * The FROM2 cube must have either one band or the same number of bands
   * as the FROM cube. If the FROM2 cube has one band, then the algebraic
   * formula will be applied to all bands in FROM using that single band
   * in FROM2. If FROM2 is a multi-band cube, the algebra will be performed
   * between corresponding bands from FROM and FROM2.
   *
   * @param icube1 Cube* input cube1
   * @param ui UserInterface object containing parameters
   * @param icube2 Cube* input cube2; optional second input cube
   */
  void algebra(Cube* icube1, UserInterface &ui, Cube* icube2) {

    // Processing by line
    ProcessByLine p;

    // Set input cubes and attributes into ProcessByLine p
    CubeAttributeInput inatts1 = ui.GetInputAttribute("FROM");
    CubeAttributeInput inatts2;
    p.SetInputCube(icube1->fileName(), inatts1);
    if(icube2 != nullptr) {
      inatts2 = ui.GetInputAttribute("FROM2");
      p.SetInputCube(icube2->fileName(), inatts2);
    }

    // Set output cube and attributes into ProcessByLine p
    QString outCubeFname = ui.GetCubeName("TO");
    CubeAttributeOutput &outatts = ui.GetOutputAttribute("TO");
    p.SetOutputCube(outCubeFname, outatts);

    // Get the coefficients
    double Isisa = ui.GetDouble("A");
    double Isisb = ui.GetDouble("B");
    double Isisc = ui.GetDouble("C");
    double Isisd = ui.GetDouble("D");
    double Isise = ui.GetDouble("E");

    QString op = ui.GetString("OPERATOR");
    
    //*****************************************
    // Lambda functions to perform operations *
    //*****************************************

    // operatorProcess for add, subtract, multiply, divide
    auto operatorProcess = [&](std::vector<Buffer *> &in, std::vector<Buffer *> &out)->void {
      Buffer &inp1 = *in[0];
      Buffer &inp2 = *in[1];
      Buffer &outp = *out[0];

      // Loop for each pixel in the line
      // Special pixel propagation:
      //   1) special pixels in inp1 propagate to output cube unchanged
      //   2) if inp1 is not special and inp2 is, the output pixel is set to Null
      for(int i = 0; i < inp1.size(); i++) {
        if(IsSpecial(inp1[i])) {
          outp[i] = inp1[i];
        }
        else if(IsSpecial(inp2[i])) {
          outp[i] = NULL8;
        }
        else {
          double operand1 = (inp1[i] - Isisd) * Isisa;
          double operand2 = (inp2[i] - Isise) * Isisb;
          if(op == "ADD") {
            outp[i] = (operand1 + operand2) + Isisc;
          }
          if(op == "SUBTRACT") {
            outp[i] = (operand1 - operand2) + Isisc;
          }
          if(op == "MULTIPLY") {
            outp[i] = (operand1 * operand2) + Isisc;
          }
          if(op == "DIVIDE") {
            if((inp2[i] - Isise) * Isisb  == 0.0) {
              outp[i] = NULL8;
            }
            else {
              outp[i] = (operand1 / operand2) + Isisc;
            }
          }
        }
      }
    };

    // Unary process
    auto unaryProcess = [&](Buffer &in, Buffer &out)->void {
      // Loop for each pixel in the line.
      // Special pixels propagate directly to output
      for(int i = 0; i < in.size(); i++) {
        if(IsSpecial(in[i])) {
          out[i] = in[i];
        }
        else {
          out[i] = in[i] * Isisa + Isisc;
        }
      }
    };

    //*****************************************
    // End Lambda functions                   *
    //*****************************************

    // Start processing based on the operator
    if(op == "UNARY") {
      p.ProcessCube(unaryProcess);
    }
    else {
      p.ProcessCubes(operatorProcess); // add, subtract, multiply, divide
    }

    p.EndProcess();
  }
}
+19 −0
Original line number Diff line number Diff line
/** This is free and unencumbered software released into the public domain.

The authors of ISIS do not claim copyright on the contents of this file.
For more details about the LICENSE terms and the AUTHORS, you will
find files of those names at the top level of this repository. **/

/* SPDX-License-Identifier: CC0-1.0 */

#ifndef algebra_h
#define algebra_h

#include "UserInterface.h"

namespace Isis{
  extern void algebra(UserInterface &ui);
  extern void algebra(Cube* icube1, UserInterface &ui, Cube* icube2=nullptr);
}

#endif
+3 −0
Original line number Diff line number Diff line
@@ -87,6 +87,9 @@
      Updated to use the new ProcessByLine API. This program now takes
      advantage of multiple global processing threads.
    </change>
    <change name="Ken Edmundson" date="2024-08-22">
      Converted to callable app and converted Makefile tests to gtests.
    </change>
  </history>

  <category>
+12 −137
Original line number Diff line number Diff line
#include "Isis.h"
#include "ProcessByBrick.h"
#include "ProcessByLine.h"
#include "SpecialPixel.h"
#include "IException.h"

using namespace std;
using namespace Isis;
/** This is free and unencumbered software released into the public domain.

void add(vector<Buffer *> &in,
         vector<Buffer *> &out);
void sub(vector<Buffer *> &in,
         vector<Buffer *> &out);
void mult(vector<Buffer *> &in,
          vector<Buffer *> &out);
void divide(vector<Buffer *> &in,
         vector<Buffer *> &out);
void unaryFunction(Buffer &in, Buffer &out);

double Isisa, Isisb, Isisc, Isisd, Isise;

void IsisMain() {
  std::cout << "algebra - got to main...\n";
The authors of ISIS do not claim copyright on the contents of this file.
For more details about the LICENSE terms and the AUTHORS, you will
find files of those names at the top level of this repository. **/

  // We will be processing by line
  ProcessByLine p;

  // Setup the input and output files
  UserInterface &ui = Application::GetUserInterface();
  // Setup the input and output cubes
  p.SetInputCube("FROM");
  if(ui.WasEntered("FROM2")) p.SetInputCube("FROM2");
  p.SetOutputCube("TO");

  // Get the coefficients
  Isisa = ui.GetDouble("A");
  Isisb = ui.GetDouble("B");
  Isisc = ui.GetDouble("C");
  Isisd = ui.GetDouble("D");
  Isise = ui.GetDouble("E");

  // Start the processing based on the operator
  QString op = ui.GetString("OPERATOR");
  if(op == "ADD") p.ProcessCubes(&add);
  if(op == "SUBTRACT") p.ProcessCubes(&sub);
  if(op == "MULTIPLY") p.ProcessCubes(&mult);
  if(op == "DIVIDE") p.ProcessCubes(&divide);
  if(op == "UNARY") p.ProcessCube(&unaryFunction);
}
/* SPDX-License-Identifier: CC0-1.0 */

// Add routine
void add(vector<Buffer *> &in, vector<Buffer *> &out) {
  Buffer &inp1 = *in[0];
  Buffer &inp2 = *in[1];
  Buffer &outp = *out[0];
  // Loop for each pixel in the line.
  for(int i = 0; i < inp1.size(); i++) {
    if(IsSpecial(inp1[i])) {
      outp[i] = inp1[i];
    }
    else if(IsSpecial(inp2[i])) {
      outp[i] = NULL8;
    }
    else {
      outp[i] = ((inp1[i] - Isisd) * Isisa) + ((inp2[i] - Isise) * Isisb) + Isisc;
    }
  }
}

// Sub routine
void sub(vector<Buffer *> &in, vector<Buffer *> &out) {
  Buffer &inp1 = *in[0];
  Buffer &inp2 = *in[1];
  Buffer &outp = *out[0];

  // Loop for each pixel in the line.
  for(int i = 0; i < inp1.size(); i++) {
    if(IsSpecial(inp1[i])) {
      outp[i] = inp1[i];
    }
    else if(IsSpecial(inp2[i])) {
      outp[i] = NULL8;
    }
    else {
      outp[i] = ((inp1[i] - Isisd) * Isisa) - ((inp2[i] - Isise) * Isisb) + Isisc;
    }
  }
}
#include "Isis.h"

// Sub routine
void mult(vector<Buffer *> &in, vector<Buffer *> &out) {
  Buffer &inp1 = *in[0];
  Buffer &inp2 = *in[1];
  Buffer &outp = *out[0];
#include "algebra.h"

  // Loop for each pixel in the line.
  for(int i = 0; i < inp1.size(); i++) {
    if(IsSpecial(inp1[i])) {
      outp[i] = inp1[i];
    }
    else if(IsSpecial(inp2[i])) {
      outp[i] = NULL8;
    }
    else {
      outp[i] = ((inp1[i] - Isisd) * Isisa) * ((inp2[i] - Isise) * Isisb) + Isisc;
    }
  }
}
#include "Application.h"

// Div routine
void divide(vector<Buffer *> &in, vector<Buffer *> &out) {
  Buffer &inp1 = *in[0];
  Buffer &inp2 = *in[1];
  Buffer &outp = *out[0];
using namespace Isis;

  // Loop for each pixel in the line.
  for(int i = 0; i < inp1.size(); i++) {
    if(IsSpecial(inp1[i])) {
      outp[i] = inp1[i];
    }
    else if(IsSpecial(inp2[i])) {
      outp[i] = NULL8;
    }
    else {
      if((inp2[i] - Isise) * Isisb  == 0.0) {
        outp[i] = NULL8;
      }
      else {
        outp[i] = ((inp1[i] - Isisd) * Isisa) / ((inp2[i] - Isise) * Isisb) + Isisc;
      }
    }
  }
void IsisMain() {
  UserInterface &ui = Application::GetUserInterface();
  algebra(ui);
}
// Unary routine
void unaryFunction(Buffer &in, Buffer &out) {
  // Loop for each pixel in the line.
  for(int i = 0; i < in.size(); i++) {
    if(IsSpecial(in[i])) {
      out[i] = in[i];
    }
    else {
      out[i] = in[i] * Isisa + Isisc;
    }
  }
}
Loading