|
| 1 | +/** This is free and unencumbered software released into the public domain. |
| 2 | +
|
| 3 | +The authors of ISIS do not claim copyright on the contents of this file. |
| 4 | +For more details about the LICENSE terms and the AUTHORS, you will |
| 5 | +find files of those names at the top level of this repository. **/ |
| 6 | + |
| 7 | +/* SPDX-License-Identifier: CC0-1.0 */ |
| 8 | + |
| 9 | +#include "algebra.h" |
| 10 | + |
| 11 | +#include "Cube.h" |
| 12 | +#include "FileName.h" |
| 13 | +#include "ProcessByLine.h" |
| 14 | +#include "SpecialPixel.h" |
| 15 | + |
| 16 | +namespace Isis { |
| 17 | + |
| 18 | + /* |
| 19 | + * Algebra |
| 20 | + * |
| 21 | + * This program performs simple algebra on either one or two |
| 22 | + * cubes. The two cubes may be added, subtracted, multiplied or divided. |
| 23 | + * |
| 24 | + * The following equations are used: |
| 25 | + * UNARY: out = (A * from1) + C |
| 26 | + * ADD: out = ((from1 - D) * A) + ((from2 - E) * B) + C |
| 27 | + * SUBTRACT: out = ((from1 - D) * A) - ((from2 - E) * B) + C |
| 28 | + * MULTIPLY: out = ((from1 - D) * A) * ((from2 - E) * B) + C |
| 29 | + * DIVIDE: out = ((from1 - D) * A) / ((from2 - E) * B) + C |
| 30 | + * |
| 31 | + * The FROM2 cube must have either one band or the same number of bands |
| 32 | + * as the FROM cube. If the FROM2 cube has one band, then the algebraic |
| 33 | + * formula will be applied to all bands in FROM using that single band |
| 34 | + * in FROM2. If FROM2 is a multi-band cube, the algebra will be performed |
| 35 | + * between corresponding bands from FROM and FROM2. |
| 36 | + * |
| 37 | + * @param ui UserInterface object containing parameters |
| 38 | + */ |
| 39 | + void algebra(UserInterface &ui) { |
| 40 | + Cube* icube1 = new Cube(); |
| 41 | + icube1->open(ui.GetCubeName("FROM")); |
| 42 | + |
| 43 | + Cube* icube2 = nullptr; |
| 44 | + if(ui.WasEntered("FROM2")) { |
| 45 | + icube2 = new Cube(); |
| 46 | + icube2->open(ui.GetCubeName("FROM2")); |
| 47 | + } |
| 48 | + |
| 49 | + algebra(icube1, ui, icube2); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + /* |
| 54 | + * Algebra |
| 55 | + * |
| 56 | + * This program performs simple algebra on either one or two |
| 57 | + * cubes. The two cubes may be added, subtracted, multiplied or divided. |
| 58 | + * |
| 59 | + * The following equations are used: |
| 60 | + * UNARY: out = (A * from1) + C |
| 61 | + * ADD: out = ((from1 - D) * A) + ((from2 - E) * B) + C |
| 62 | + * SUBTRACT: out = ((from1 - D) * A) - ((from2 - E) * B) + C |
| 63 | + * MULTIPLY: out = ((from1 - D) * A) * ((from2 - E) * B) + C |
| 64 | + * DIVIDE: out = ((from1 - D) * A) / ((from2 - E) * B) + C |
| 65 | + * |
| 66 | + * The FROM2 cube must have either one band or the same number of bands |
| 67 | + * as the FROM cube. If the FROM2 cube has one band, then the algebraic |
| 68 | + * formula will be applied to all bands in FROM using that single band |
| 69 | + * in FROM2. If FROM2 is a multi-band cube, the algebra will be performed |
| 70 | + * between corresponding bands from FROM and FROM2. |
| 71 | + * |
| 72 | + * @param icube1 Cube* input cube1 |
| 73 | + * @param ui UserInterface object containing parameters |
| 74 | + * @param icube2 Cube* input cube2; optional second input cube |
| 75 | + */ |
| 76 | + void algebra(Cube* icube1, UserInterface &ui, Cube* icube2) { |
| 77 | + |
| 78 | + // Processing by line |
| 79 | + ProcessByLine p; |
| 80 | + |
| 81 | + // Set input cubes and attributes into ProcessByLine p |
| 82 | + CubeAttributeInput inatts1 = ui.GetInputAttribute("FROM"); |
| 83 | + CubeAttributeInput inatts2; |
| 84 | + p.SetInputCube(icube1->fileName(), inatts1); |
| 85 | + if(icube2 != nullptr) { |
| 86 | + inatts2 = ui.GetInputAttribute("FROM2"); |
| 87 | + p.SetInputCube(icube2->fileName(), inatts2); |
| 88 | + } |
| 89 | + |
| 90 | + // Set output cube and attributes into ProcessByLine p |
| 91 | + QString outCubeFname = ui.GetCubeName("TO"); |
| 92 | + CubeAttributeOutput &outatts = ui.GetOutputAttribute("TO"); |
| 93 | + p.SetOutputCube(outCubeFname, outatts); |
| 94 | + |
| 95 | + // Get the coefficients |
| 96 | + double Isisa = ui.GetDouble("A"); |
| 97 | + double Isisb = ui.GetDouble("B"); |
| 98 | + double Isisc = ui.GetDouble("C"); |
| 99 | + double Isisd = ui.GetDouble("D"); |
| 100 | + double Isise = ui.GetDouble("E"); |
| 101 | + |
| 102 | + QString op = ui.GetString("OPERATOR"); |
| 103 | + |
| 104 | + //***************************************** |
| 105 | + // Lambda functions to perform operations * |
| 106 | + //***************************************** |
| 107 | + |
| 108 | + // operatorProcess for add, subtract, multiply, divide |
| 109 | + auto operatorProcess = [&](std::vector<Buffer *> &in, std::vector<Buffer *> &out)->void { |
| 110 | + Buffer &inp1 = *in[0]; |
| 111 | + Buffer &inp2 = *in[1]; |
| 112 | + Buffer &outp = *out[0]; |
| 113 | + |
| 114 | + // Loop for each pixel in the line |
| 115 | + // Special pixel propagation: |
| 116 | + // 1) special pixels in inp1 propagate to output cube unchanged |
| 117 | + // 2) if inp1 is not special and inp2 is, the output pixel is set to Null |
| 118 | + for(int i = 0; i < inp1.size(); i++) { |
| 119 | + if(IsSpecial(inp1[i])) { |
| 120 | + outp[i] = inp1[i]; |
| 121 | + } |
| 122 | + else if(IsSpecial(inp2[i])) { |
| 123 | + outp[i] = NULL8; |
| 124 | + } |
| 125 | + else { |
| 126 | + double operand1 = (inp1[i] - Isisd) * Isisa; |
| 127 | + double operand2 = (inp2[i] - Isise) * Isisb; |
| 128 | + if(op == "ADD") { |
| 129 | + outp[i] = (operand1 + operand2) + Isisc; |
| 130 | + } |
| 131 | + if(op == "SUBTRACT") { |
| 132 | + outp[i] = (operand1 - operand2) + Isisc; |
| 133 | + } |
| 134 | + if(op == "MULTIPLY") { |
| 135 | + outp[i] = (operand1 * operand2) + Isisc; |
| 136 | + } |
| 137 | + if(op == "DIVIDE") { |
| 138 | + if((inp2[i] - Isise) * Isisb == 0.0) { |
| 139 | + outp[i] = NULL8; |
| 140 | + } |
| 141 | + else { |
| 142 | + outp[i] = (operand1 / operand2) + Isisc; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + }; |
| 148 | + |
| 149 | + // Unary process |
| 150 | + auto unaryProcess = [&](Buffer &in, Buffer &out)->void { |
| 151 | + // Loop for each pixel in the line. |
| 152 | + // Special pixels propagate directly to output |
| 153 | + for(int i = 0; i < in.size(); i++) { |
| 154 | + if(IsSpecial(in[i])) { |
| 155 | + out[i] = in[i]; |
| 156 | + } |
| 157 | + else { |
| 158 | + out[i] = in[i] * Isisa + Isisc; |
| 159 | + } |
| 160 | + } |
| 161 | + }; |
| 162 | + |
| 163 | + //***************************************** |
| 164 | + // End Lambda functions * |
| 165 | + //***************************************** |
| 166 | + |
| 167 | + // Start processing based on the operator |
| 168 | + if(op == "UNARY") { |
| 169 | + p.ProcessCube(unaryProcess); |
| 170 | + } |
| 171 | + else { |
| 172 | + p.ProcessCubes(operatorProcess); // add, subtract, multiply, divide |
| 173 | + } |
| 174 | + |
| 175 | + p.EndProcess(); |
| 176 | + } |
| 177 | +} |
| 178 | + |
0 commit comments