Skip to content

Commit

Permalink
add CSV Writer
Browse files Browse the repository at this point in the history
  • Loading branch information
m-schuetz committed Jul 16, 2023
1 parent 02e9be8 commit 7207c16
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
"xstddef": "cpp",
"xtr1common": "cpp",
"xtree": "cpp",
"xutility": "cpp"
"xutility": "cpp",
"format": "cpp"
},
"files.trimTrailingWhitespace": false,
"editor.fontSize": 28,
Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ cmake ../

# Usage

## extract_profile

Extract points with the elevation profile:

// minimal
Expand All @@ -42,3 +44,28 @@ With ```--get-candidates```, you'll get the number of candidate points, i.e., th
A practical example:

./extract_profile ~/dev/tmp/retz -o ~/dev/tmp/retz.laz --coordinates "{-37.601, -100.733, 4.940},{-22.478, 75.982, 8.287},{66.444, 54.042, 5.388},{71.294, -67.140, -2.481},{165.519, -26.288, 0.253}" --width 2 --min-level 0 --max-level 3


## extract_area

Syntax:
<pre>
./extract_area potreePath -o targetFile --area "matrix(clipmatrix elements)"
./extract_area potreePath -o targetFile --area "minmax([x,y,z],[x,y,z])"
</pre>

Examples:
<pre>
// filter by clipping matrix
./extract_area "C:/pointcloud1" "C:/pointcloud2" -o F:/area.las --area "matrix(18.477832643548027, 0, 0, 0, 0, 7.667634693905711, 0, 0, 0, 0, 15.976286462171942, 0, 2020586.929216755, 7204767.992882229, 233.72644370784667, 1)"
</pre>

<pre>
// filter by axis-aligned bounding box
./extract_area "C:/pointcloud1" "C:/pointcloud2" -o F:/area.las --area "minmax([2020586.9,7204768.4,229],[2020591.2,7204777.9,230.8])"
</pre>

<pre>
// save as CSV instead of las.
./extract_area "C:/pointcloud" -o F:/area.csv --area "minmax([2020586.9,7204768.4,229],[2020591.2,7204777.9,230.8])"
</pre>
125 changes: 93 additions & 32 deletions include/CsvWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,69 +34,106 @@ using std::lock_guard;
using std::ofstream;


vector<function<void(int64_t, uint8_t*)>> createAttributeHandlers(shared_ptr<ofstream> stream, Attributes& attributes, Attributes outputAttributes) {
vector<function<void(int64_t)>> createAttributeHandlers(shared_ptr<ofstream> stream, shared_ptr<Points> points, Attributes outputAttributes) {

vector<function<void(int64_t, uint8_t*)>> handlers;
vector<function<void(int64_t)>> handlers;

auto& inputAttributes = points->attributes;

{ // STANDARD LAS ATTRIBUTES

unordered_map<string, function<void(int64_t, uint8_t*)>> mapping;
unordered_map<string, function<void(int64_t)>> mapping;

{ // POSITION
int offset = attributes.getOffset("position");
auto handler = [stream, offset, attributes](int64_t index, uint8_t* data) {
auto attribute = inputAttributes.get("position");
auto buff_position = points->attributeBuffersMap["position"];
auto posScale = inputAttributes.posScale;
auto posOffset = inputAttributes.posOffset;
auto handler = [stream, buff_position, attribute, posScale, posOffset](int64_t index) {

int32_t X, Y, Z;

memcpy(&X, data + index * attributes.bytes + offset + 0, 4);
memcpy(&Y, data + index * attributes.bytes + offset + 4, 4);
memcpy(&Z, data + index * attributes.bytes + offset + 8, 4);
memcpy(&X, buff_position->data_u8 + index * attribute->size + 0, 4);
memcpy(&Y, buff_position->data_u8 + index * attribute->size + 4, 4);
memcpy(&Z, buff_position->data_u8 + index * attribute->size + 8, 4);

double x = double(X) * attributes.posScale.x + attributes.posOffset.x;
double y = double(Y) * attributes.posScale.y + attributes.posOffset.y;
double z = double(Z) * attributes.posScale.z + attributes.posOffset.z;
double x = double(X) * posScale.x + posOffset.x;
double y = double(Y) * posScale.y + posOffset.y;
double z = double(Z) * posScale.z + posOffset.z;

*stream << x << " " << y << " " << z;
*stream << x << ", " << y << ", " << z;
};

mapping["position"] = handler;
}

{ // RGB
int offset = attributes.getOffset("rgb");
auto handler = [stream, offset, attributes](int64_t index, uint8_t* data) {
uint16_t rgb[3];
{ // RGB
auto attribute = inputAttributes.get("rgb");
auto source = points->attributeBuffersMap["rgb"];
auto handler = [stream, source, attribute](int64_t index) {

if(source){
uint16_t rgb[3];

memcpy(&rgb, source->data_u8 + index * attribute->size, attribute->size);

memcpy(&rgb, data + index * attributes.bytes + offset, 6);
*stream << rgb[0] << ", " << rgb[1] << ", " << rgb[2];
}else{
*stream << "0, 0, 0";
}

*stream << " " << rgb[0] << " " << rgb[1] << " " << rgb[2];

};

mapping["rgb"] = handler;
}

{ // INTENSITY
auto attribute = inputAttributes.get("intensity");
auto source = points->attributeBuffersMap["intensity"];
auto handler = [stream, source, attribute](int64_t index) {
if(source){
uint16_t intensity;

memcpy(&intensity, source->data_u8 + index * attribute->size, attribute->size);

*stream << intensity;
}else{
*stream << "0, 0, 0";
}
};

mapping["rgb"] = handler;
mapping["intensity"] = handler;
}

{ // INTENSITY
int offset = attributes.getOffset("intensity");
auto handler = [stream, offset, attributes](int64_t index, uint8_t* data) {
uint16_t intensity;
memcpy(&intensity, data + index * attributes.bytes + offset, 2);
{ // CLASSIFICATION
auto attribute = inputAttributes.get("classification");
auto source = points->attributeBuffersMap["classification"];
auto handler = [stream, source, attribute](int64_t index) {
if(source){
uint8_t classification;

memcpy(&classification, source->data_u8 + index * attribute->size, attribute->size);

*stream << " " << intensity;
*stream << classification;
}else{
*stream << "0, 0, 0";
}
};

mapping["intensity"] = handler;
mapping["classification"] = handler;
}


*stream << "#";
// *stream << "#";
for (auto& attribute : outputAttributes.list) {

if (mapping.find(attribute.name) != mapping.end()) {
*stream << attribute.name << " ";
// *stream << attribute.name << " ";
handlers.push_back(mapping[attribute.name]);
}
}
*stream << endl;
// *stream << endl;

}

Expand Down Expand Up @@ -137,12 +174,36 @@ struct CsvWriter : public Writer {
lock_guard<mutex> lock(mtx_write);

auto inputAttributes = points->attributes;
auto handlers = createAttributeHandlers(stream, inputAttributes, outputAttributes);
auto handlers = createAttributeHandlers(stream, points, outputAttributes);

int64_t numPoints = points->numPoints;

cout << "TODO" << endl;
exit(123);

for (int64_t i = 0; i < numPoints; i++) {

//for(auto& handler : handlers){
for(int j = 0; j < handlers.size(); j++){
auto& handler = handlers[j];
handler(i);

if(j < handlers.size() - 1){
*stream << ", ";
}
}

*stream << "\n";
}


// for(int i = 0; i < numPoints; i++){

// string line = std::format("");

// }


//cout << "TODO" << endl;
//exit(123);

//int posOffset = inputAttributes.getOffset("position");

Expand Down

0 comments on commit 7207c16

Please sign in to comment.