|
#include <string> |
|
#include <fstream> // ifstream |
|
#include <iostream> |
|
#include <unordered_set> |
|
#include <Rcpp.h> |
|
|
|
using namespace Rcpp; |
|
|
|
const float FLOAT_MAX = std::numeric_limits<float>::max (); |
|
|
|
// [[Rcpp::export]] |
|
S4 line_new () |
|
{ |
|
const int nLines = 3; |
|
int nrow, count=0; |
|
float tempf, xmin = FLOAT_MAX, xmax = -FLOAT_MAX, |
|
ymin = FLOAT_MAX, ymax = -FLOAT_MAX; |
|
std::vector <float> vec; |
|
List result (nLines); |
|
|
|
std::vector <std::string> colnames, rownames; |
|
colnames.push_back ("x"); |
|
colnames.push_back ("y"); |
|
List dimnames (0); |
|
|
|
Rcpp::Language line_call ("new", "Line"); |
|
Rcpp::Language lines_call ("new", "Lines"); |
|
Rcpp::S4 line; |
|
Rcpp::S4 lines; |
|
Rcpp::List dummy_list (0); |
|
|
|
for (int i=0; i<nLines; i++) |
|
{ |
|
nrow = 10 - i * 2; |
|
rownames.resize (0); |
|
vec.resize (0); |
|
for (int j=0; j<(2 * nrow); j++) |
|
vec.push_back (10.0 * (float) i + (float) j); |
|
NumericMatrix nmat (Dimension (nrow, 2)); |
|
for (int j=0; j<nrow; j++) |
|
{ |
|
rownames.push_back ("r" + std::to_string (j)); |
|
nmat (j, 0) = vec [j * 2]; |
|
nmat (j, 1) = vec [j * 2 + 1]; |
|
if (nmat (j, 0) < xmin) |
|
xmin = nmat (j, 0); |
|
else if (nmat (j, 0) > xmax) |
|
xmax = nmat (j, 0); |
|
if (nmat (j, 1) < ymin) |
|
ymin = nmat (j, 1); |
|
else if (nmat (j, 1) > ymax) |
|
ymax = nmat (j, 1); |
|
} |
|
dimnames.push_back (rownames); |
|
dimnames.push_back (colnames); |
|
nmat.attr ("dimnames") = dimnames; |
|
while (dimnames.size () > 0) |
|
dimnames.erase (0); |
|
|
|
line = line_call.eval (); |
|
line.slot ("coords") = nmat; |
|
dummy_list.push_back (line); |
|
lines = lines_call.eval (); |
|
lines.slot ("Lines") = dummy_list; |
|
lines.slot ("ID") = "number#" + std::to_string (i); |
|
result [count++] = lines; |
|
|
|
dummy_list.erase (0); |
|
} |
|
|
|
vec.resize (0); |
|
Rcpp::Language sp_lines_call ("new", "SpatialLines"); |
|
Rcpp::S4 sp_lines; |
|
sp_lines = sp_lines_call.eval (); |
|
sp_lines.slot ("lines") = result; |
|
|
|
return sp_lines; |
|
} |
|
|
|
// [[Rcpp::export]] |
|
S4 line_fill () |
|
{ |
|
const int nLines = 3; |
|
int nrow, count=0; |
|
float tempf, xmin = FLOAT_MAX, xmax = -FLOAT_MAX, |
|
ymin = FLOAT_MAX, ymax = -FLOAT_MAX; |
|
std::string tempstr; |
|
std::vector <float> vec; |
|
List result (nLines); |
|
|
|
std::vector <std::string> colnames, rownames; |
|
colnames.push_back ("x"); |
|
colnames.push_back ("y"); |
|
List dimnames (0); |
|
|
|
for (int i=0; i<nLines; i++) |
|
{ |
|
nrow = 10 - i * 2; |
|
rownames.resize (0); |
|
vec.resize (0); |
|
for (int j=0; j<(2 * nrow); j++) |
|
vec.push_back (10.0 * (float) i + (float) j); |
|
NumericMatrix nmat (Dimension (nrow, 2)); |
|
for (int j=0; j<nrow; j++) |
|
{ |
|
rownames.push_back ("r" + std::to_string (j)); |
|
nmat (j, 0) = vec [j * 2]; |
|
nmat (j, 1) = vec [j * 2 + 1]; |
|
if (nmat (j, 0) < xmin) |
|
xmin = nmat (j, 0); |
|
else if (nmat (j, 0) > xmax) |
|
xmax = nmat (j, 0); |
|
if (nmat (j, 1) < ymin) |
|
ymin = nmat (j, 1); |
|
else if (nmat (j, 1) > ymax) |
|
ymax = nmat (j, 1); |
|
} |
|
dimnames.push_back (rownames); |
|
dimnames.push_back (colnames); |
|
nmat.attr ("dimnames") = dimnames; |
|
while (dimnames.size () > 0) |
|
dimnames.erase (0); |
|
|
|
Rcpp::S4 line = Rcpp::Language ("Line", nmat).eval (); |
|
tempstr = "number#" + std::to_string (i); |
|
Rcpp::S4 lines = Rcpp::Language ("Lines", line, tempstr).eval (); |
|
result [count++] = lines; |
|
} |
|
|
|
vec.resize (0); |
|
Rcpp::Language sp_lines_call ("new", "SpatialLines"); |
|
Rcpp::S4 sp_lines; |
|
sp_lines = sp_lines_call.eval (); |
|
sp_lines.slot ("lines") = result; |
|
|
|
return sp_lines; |
|
} |