/** * Generates an artificial topographyGrid of size numRows x numCols if no * topographic data is available. Results are dumped into topographyGrid. * @param topographyGrid A pointer to a zero-initialized Grid of size * numRows x numCols. * @param numRows The desired number of non-border rows in the resulting matrix. * @param numCols The desired number of non-border cols in the resulting matrix. */ void simulatetopographyGrid(Grid* topographyGrid, int numRows, int numCols) { Eigen::VectorXd refx = refx.LinSpaced(numCols, -2*M_PI, 2*M_PI); Eigen::VectorXd refy = refx.LinSpaced(numRows, -2*M_PI, 2*M_PI); Eigen::MatrixXd X = refx.replicate(1, numRows); X.transposeInPlace(); Eigen::MatrixXd Y = refy.replicate(1, numCols); // Eigen can only deal with two matrices at a time, // so split the computation: // topographyGrid = sin(X) * sin(Y) * abs(X) * abs(Y) -pi Eigen::MatrixXd absXY = X.cwiseAbs().cwiseProduct(Y.cwiseAbs()); Eigen::MatrixXd sins = X.array().sin().cwiseProduct(Y.array().sin()); Eigen::MatrixXd temp; temp.resize(numRows, numCols); temp = absXY.cwiseProduct(sins); // All this work to create a matrix of pi... Eigen::MatrixXd pi; pi.resize(numRows, numCols); pi.setConstant(M_PI); temp = temp - pi; // And the final result. topographyGrid->data.block(border, border, numRows, numCols) = temp.block(0, 0, numRows, numCols); // Ignore positive values. topographyGrid->data = topographyGrid->data.unaryExpr(std::ptr_fun(validateDepth)); topographyGrid->clearNA(); }
void Model::construct_contrast_matrix(Eigen::MatrixXd &C) { if (n_alternatives > 1) { C.setConstant(-1.0 / (n_alternatives - 1.0)); } else { C.setZero(); } for (unsigned int i = 0; i < n_alternatives; i++) { C(i, i) = 1.0; } }
void colorEdgeMeshFaces(const Eigen::VectorXd &values, const double &minimum, const double &maximum, Eigen::MatrixXd &C) { C.setConstant(Fbs.rows(),3,1); Eigen::MatrixXd colors; igl::jet(values, minimum, maximum, colors); for (int ei = 0; ei<E.rows(); ++ei) { const Eigen::RowVector3d &this_color = colors.row(ei); int f0 = E2F(ei,0); int f1 = E2F(ei,1); if(f0 != -1) { int i0 = -1; for (int k = 0; k<3; k++) if (F2E(f0,k)== ei) { i0 = k; break; } C.row(3*f0+i0) = this_color; } if(f1 != -1) { int i1 = -1; for (int k = 0; k<3; k++) if (F2E(f1,k)== ei) { i1 = k; break; } C.row(3*f1+i1) = this_color; } } }
void display() { using namespace Eigen; using namespace igl; using namespace std; if(!trackball_on && tot_num_samples < 10000) { if(S.size() == 0) { S.resize(V.rows()); S.setZero(); } VectorXd Si; const int num_samples = 20; ambient_occlusion(ei,V,N,num_samples,Si); S *= (double)tot_num_samples; S += Si*(double)num_samples; tot_num_samples += num_samples; S /= (double)tot_num_samples; } // Convert to 1-intensity C.conservativeResize(S.rows(),3); if(ao_on) { C<<S,S,S; C.array() = (1.0-ao_factor*C.array()); }else { C.setConstant(1.0); } if(ao_normalize) { C.col(0) *= ((double)C.rows())/C.col(0).sum(); C.col(1) *= ((double)C.rows())/C.col(1).sum(); C.col(2) *= ((double)C.rows())/C.col(2).sum(); } C.col(0) *= color(0); C.col(1) *= color(1); C.col(2) *= color(2); glClearColor(back[0],back[1],back[2],0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // All smooth points glEnable( GL_POINT_SMOOTH ); glDisable(GL_LIGHTING); if(lights_on) { lights(); } push_scene(); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glEnable(GL_NORMALIZE); glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE); push_object(); // Draw the model // Set material properties glEnable(GL_COLOR_MATERIAL); draw_mesh(V,F,N,C); pop_object(); // Draw a nice floor glPushMatrix(); const double floor_offset = -2./bbd*(V.col(1).maxCoeff()-mid(1)); glTranslated(0,floor_offset,0); const float GREY[4] = {0.5,0.5,0.6,1.0}; const float DARK_GREY[4] = {0.2,0.2,0.3,1.0}; draw_floor(GREY,DARK_GREY); glPopMatrix(); pop_scene(); report_gl_error(); TwDraw(); glutSwapBuffers(); glutPostRedisplay(); }
int main() { bool test = false, simulateBathy = false; if (test) { runTests(); return 0; } acousticParams.insert({ "debug", "0" }); acousticParams.insert({ "cellSize", "5" }); acousticParams.insert({ "fishmodel", "0" }); acousticParams.insert({ "suppressionRangeFactor", "1"}), acousticParams.insert({ "suppressionMethod", "suppression.quick"}), acousticParams.insert({ "userSensors", "100,100,0,0,100,0,0,200" }); acousticParams.insert({ "numOptimalSensors", "10" }); acousticParams.insert({ "numProjectedSensors", "2" }); acousticParams.insert({ "bias", "2" }); acousticParams.insert({ "ousdx", ".1" }); acousticParams.insert({ "ousdy", ".1" }); acousticParams.insert({ "oucor", "0" }); acousticParams.insert({ "mux", ".5" }); acousticParams.insert({ "muy", ".5" }); acousticParams.insert({ "fishmodel", "1" }); acousticParams.insert({ "minDepth", "15" }); acousticParams.insert({ "maxDepth", "30" }); acousticParams.insert({ "meanRelativePosition", "1" }); acousticParams.insert({ "relativePositionSD", "1" }); // acousticParams.insert({"inputFile", "himbsyn.bathy.v19.grd"}); acousticParams.insert({"inputFile", "himbsyn.bathytopo.1km.v19.grd"}); // acousticParams.insert({ "inputFile", "pal_5m.asc" }); acousticParams.insert({ "inputFileType", "netcdf" }); acousticParams.insert({ "seriesName", "z" }); acousticParams.insert({ "timestamp", "-1" }); acousticParams.insert({ "logScaleGraphColoring", "1" }); acousticParams.insert({ "contourDepths", "0,-20,-40,-80" }); double suppressionRangeFactor = std::stod(acousticParams["suppressionRangeFactor"]), ousdx = std::stod(acousticParams["ousdx"]), ousdy = std::stod(acousticParams["ousdy"]), oucor = std::stod(acousticParams["oucor"]), mux = std::stod(acousticParams["mux"]), muy = std::stod(acousticParams["muy"]), fishmodel = std::stod(acousticParams["fishmodel"]), networkSparsity = 0, absRecoveryRate = 0, uniqueRecoveryRate = 0, goodnessGridComputationTime = 0, totalComputationTime = 0; int startRow = 450, startCol = 340, // 450,340,201,201 (1km)netcdf rowDist = 1001, // 100 0 800 1500 (5m)netcdf colDist = 1001, // 300 200 501 501 (palmyra) asc height = 1000, width = 1000, bias = 2, sensorDetectionRange = 4, sensorDetectionDiameter = 2 * sensorDetectionRange + 1, sensorPeakDetectionProbability = 1, SDofSensorDetectionRange = 1, i = 0, row = 0, col = 0, cellSize = std::stoi(acousticParams["cellSize"]), numOptimalSensors = std::stoi(acousticParams["numOptimalSensors"]), numProjectedSensors = std::stoi(acousticParams["numProjectedSensors"]), numSensorsToPlace = numOptimalSensors + numProjectedSensors, suppressionDiameter = (2 * ceil(sensorDetectionRange * suppressionRangeFactor)) + 1; // Set the global border size border = sensorDetectionRange; omp_set_num_threads(numThreads); Eigen::setNbThreads(numThreads); clock_t begin, end, vizBegin, vizEnd; // TODO(Greg) Data validation // Parse User Sensor Placements std::cout << "\nReading userSensor List...\n"; std::vector<std::string> userSensors; parseCDString(&userSensors, acousticParams["userSensors"], ','); Eigen::MatrixXd userSensorList; userSensorList.resize(userSensors.size()/2, 4); for (i = 0; i < userSensorList.rows(); i ++) { row = std::stoi(userSensors[2 * i]); col = std::stoi(userSensors[2 * i + 1]); if (row < 0 || col < 0 || row >= rowDist || col >= colDist) { printError("A user-defined sensor is out of bounds.", 1, acousticParams["timestamp"]); } // Translate user points to our internal grid userSensorList(i, 0) = row; userSensorList(i, 1) = col; } begin = clock(); // Compute contour depth meta data (used for graphical output) std::vector<std::string> contourLevels; parseCDString(&contourLevels, acousticParams["contourDepths"], ','); // Note the number of contours we need to graph acousticParams.insert({ "numContourDepths", std::to_string(contourLevels.size()) }); // TODO(Greg) Sort is broken, throws segfaults. Possibly b/c file doesn't // exist (tried with pal5m.asc). fix plx. // sort(&contourLevels, &contourLevels + numContourDepths); // File path variables std::string outputDataFilePath = "data/", outputDataFileType = ".dat", bathymetryTitle = "Topography", habitatTitle = "Habitat", goodnessTitle = "Goodness", coverageTitle = "Acoustic Coverage", bathymetryFilePath = outputDataFilePath + bathymetryTitle + outputDataFileType, habitatFilePath = outputDataFilePath + habitatTitle + outputDataFileType, goodnessFilePath = outputDataFilePath + goodnessTitle + outputDataFileType, coverageFilePath = outputDataFilePath + coverageTitle + outputDataFileType; Grid bGrid(rowDist + 2 * border, colDist + 2 * border, "Behavior"); Grid gGrid(rowDist + 2 * border, colDist + 2 * border, "Goodness"); Grid tGrid(rowDist + 2 * border, colDist + 2 * border, "Topography"); Grid cGrid(rowDist + 2 * border, colDist + 2 * border, "Coverage"); Eigen::MatrixXd suppressionReference; Eigen::MatrixXd distanceGradient; Eigen::MatrixXd detectionGradient; distanceGradient.resize(sensorDetectionDiameter, sensorDetectionDiameter); distanceGradient.setConstant(0); detectionGradient.resize(sensorDetectionDiameter, sensorDetectionDiameter); detectionGradient.setConstant(0); // Create a gradient of distances to avoid redundant computation makeDistGradient(&distanceGradient, sensorDetectionRange); // Create a gradient of probability of detection (due to sensorRange) to // avoid redundant computation makeDetectionGradient(&detectionGradient, & distanceGradient, sensorPeakDetectionProbability, SDofSensorDetectionRange); // Fetch or simulate topography std::cout << "Getting topography..."; if (simulateBathy) { // Simulate topography simulatetopographyGrid(&tGrid, rowDist, colDist); } else { // Fetch actual topography getBathy(&tGrid, acousticParams["inputFile"], acousticParams["inputFileType"], size_t(startRow), size_t(startCol), size_t(rowDist), size_t(colDist), acousticParams["seriesName"], acousticParams["timestamp"]); } std::cout << bGrid.data; // Fill in Behavior Grid std::cout << "\nGetting Behavior..."; populateBehaviorGrid(&tGrid, &bGrid, bias, cellSize, ousdx, ousdy, oucor, mux, muy, fishmodel); // Initalize the Coverage Grid cGrid.data.block(border, border, rowDist, colDist).setConstant(1); // Mr. Gaeta, START THE CLOCK! vizBegin = clock(); std::cout << "\nGetting Goodness...\nBias: " << bias << "\n"; // Calculate good sensor locations calculateGoodnessGrid(&tGrid, &bGrid, &gGrid, &suppressionReference, &detectionGradient, &distanceGradient, bias, sensorDetectionRange, border, border, rowDist, colDist); vizEnd = clock(); goodnessGridComputationTime = static_cast<double>(end - begin) / CLOCKS_PER_SEC; std::cout << "Copying goodness grid...\n"; Grid UGGrid(&gGrid, "Unsuppressed Goodness"); // Find optimal placements std::cout << "\nPlacing Sensors...\n"; Eigen::MatrixXd bestSensors; bestSensors.resize(numSensorsToPlace, 4); //============================ gGrid.name = "x1"; Graph x1Graph = Graph(&gGrid); x1Graph.writeMat(); // Grab the top n sensor r,c locations and recovery rates. selectTopSensorLocations(&tGrid, &bGrid, &gGrid, &UGGrid, &bestSensors, &userSensorList, &suppressionReference, &detectionGradient, &distanceGradient, numSensorsToPlace, sensorDetectionRange, bias, suppressionRangeFactor, suppressionDiameter, sensorPeakDetectionProbability, SDofSensorDetectionRange, acousticParams["timestamp"]); gGrid.name = "x2"; Graph x2Graph = Graph(&gGrid); x2Graph.writeMat(); std::cout << bestSensors << "\n"; std::cout << "Computing Statistics...\n"; getStats(&UGGrid, &gGrid, &bestSensors, sensorDetectionRange, &networkSparsity, &absRecoveryRate, &uniqueRecoveryRate, &cGrid); gGrid.name = "x3"; Graph x3Graph = Graph(&gGrid); x3Graph.writeMat(); gGrid.name = "Goodness"; // Generate graphs std::cout<< "\nWriting Graphs..."; Graph gGraph = Graph(&UGGrid); Graph tGraph = Graph(&tGrid); Graph bGraph = Graph(&bGrid); Graph cGraph = Graph(&cGrid); try { // Print the matrix & data files for Topography Grid tGraph.writeMat(); tGraph.writeDat(); // Print the contour file used by all graphs. // (Do this just once as it takes a loooong time). tGraph.printContourFile(&contourLevels); // Graph the Topography Grid with contour lines. bool plotSensors = true; tGraph.printContourGraph(width, height, &contourLevels, plotSensors, &userSensorList, &bestSensors, numProjectedSensors, false); // Print the matrix & data files for Bathymetry Grid bGraph.writeMat(); bGraph.writeDat(); // Graph Behavior Grid with contour lines. bGraph.printContourGraph(width, height, &contourLevels, plotSensors, &userSensorList, &bestSensors, numProjectedSensors, false); // Print the matrix & data files for Goodness Grid gGraph.writeMat(); gGraph.writeDat(); // Graph Goodness Grid with contour lines. gGraph.printContourGraph(width, height, &contourLevels, plotSensors, &userSensorList, &bestSensors, numProjectedSensors, false); // Print the matrix & data files for Goodness Grid cGraph.writeMat(); cGraph.writeDat(); // Graph Goodness Grid with contour lines. cGraph.printContourGraph(width, height, &contourLevels, plotSensors, &userSensorList, &bestSensors, numProjectedSensors, false); } catch (int e) { std::cout << "Error:" << e << "\n"; return 0; } end = clock(); goodnessGridComputationTime = static_cast<double>(vizEnd - vizBegin) / CLOCKS_PER_SEC; std::cout << "\nVisibility calculation took " << goodnessGridComputationTime << " s"; totalComputationTime = static_cast<double>(end - begin) / CLOCKS_PER_SEC; std::cout << "\nEntire Run took " << totalComputationTime << " s"; return 0; }