Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cpp/emscripten/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s ALLOW_MEMORY_GROWTH=1")
if (NOT WASM EQUAL 0)
set(WASM 1)
endif()
if (NOT ESM EQUAL 0)
set(ESM 1)
endif()

if (WASM)
message(STATUS "WASM Emscripten output has been enabled")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s WASM=1 -s \"BINARYEN_METHOD='native-wasm'\"")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s MODULARIZE=1")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s WASM_BIGINT -s EXPORTED_FUNCTIONS=['_malloc, _free']")
if (ESM)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s EXPORT_ES6=1")
else (ESM)
Expand Down
77 changes: 77 additions & 0 deletions cpp/emscripten/laz-perf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "header.hpp"
#include "readers.hpp"
#include "writers.hpp"

using namespace emscripten;

Expand Down Expand Up @@ -70,6 +71,72 @@ class ChunkDecoder
std::shared_ptr<lazperf::reader::chunk_decompressor> decomp_;
};

class LazWriter
{
public:
LazWriter(): c()
{}

LazWriter& setPointFormat(int point_format)
{
c.pdrf = point_format;
return *this;
}

LazWriter& setTransform(
float offset_x,
float offset_y,
float offset_z,
float scale_x,
float scale_y,
float scale_z
)
{
c.scale = lazperf::vector3(scale_x, scale_y, scale_z);
c.offset = lazperf::vector3(offset_x, offset_y, offset_z);
return *this;
}

LazWriter& setChunkSize(uint32_t chunk_size)
{
c.chunk_size = chunk_size;
return *this;
}

void open(
unsigned int b,
size_t len
)
{
char *buf = (char*) b;
mem_file_.reset(new lazperf::writer::mem_file(
buf,
len,
c
));
}

void writePoint(unsigned int inputBuf)
{
char *buf = reinterpret_cast<char *>(inputBuf);
mem_file_->writePoint(buf);
}

uint64_t newChunk()
{
return static_cast<uint64_t>(mem_file_->newChunk());
}

uint64_t close()
{
return static_cast<uint64_t>(mem_file_->close());
}

private:
std::shared_ptr<lazperf::writer::mem_file> mem_file_;
lazperf::writer::named_file::config c;
};

EMSCRIPTEN_BINDINGS(my_module) {
class_<LASZip>("LASZip")
.constructor()
Expand All @@ -83,5 +150,15 @@ EMSCRIPTEN_BINDINGS(my_module) {
.constructor()
.function("open", &ChunkDecoder::open)
.function("getPoint", &ChunkDecoder::getPoint);

class_<LazWriter>("LazWriter")
.constructor()
.function("setPointFormat", &LazWriter::setPointFormat, return_value_policy::reference())
.function("setTransform", &LazWriter::setTransform, return_value_policy::reference())
.function("setChunkSize", &LazWriter::setChunkSize, return_value_policy::reference())
.function("open", &LazWriter::open)
.function("writePoint", &LazWriter::writePoint)
.function("newChunk", &LazWriter::newChunk)
.function("close", &LazWriter::close);
}

50 changes: 41 additions & 9 deletions cpp/lazperf/writers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "streams.hpp"
#include "vlr.hpp"
#include "writers.hpp"
#include "charbuf.hpp"

namespace lazperf
{
Expand All @@ -45,7 +46,7 @@ struct basic_file::Private
f(nullptr)
{}

void close();
uint64_t close();
uint64_t newChunk();
uint64_t firstChunkOffset() const;
bool compressed() const;
Expand Down Expand Up @@ -181,7 +182,7 @@ void basic_file::Private::writePoint(const char *p)
updateMinMax(*(reinterpret_cast<const las::point10*>(p)));
}

void basic_file::Private::close()
uint64_t basic_file::Private::close()
{
if (compressed())
{
Expand All @@ -190,9 +191,14 @@ void basic_file::Private::close()
chunks.push_back({ chunk_point_num, (uint64_t)f->tellp() });
}

uint64_t chunk_table_offset = (uint64_t)f->tellp();
writeHeader();
f->seekp(chunk_table_offset);

if (compressed())
writeChunkTable();

return (uint64_t)f->tellp();
}

void basic_file::Private::writeHeader()
Expand Down Expand Up @@ -262,9 +268,6 @@ void basic_file::Private::writeHeader()

void basic_file::Private::writeChunkTable()
{
// move to the end of the file to start emitting our compresed table
f->seekp(0, std::ios::end);

// take note of where we're writing the chunk table, we need this later
int64_t chunk_table_offset = static_cast<int64_t>(f->tellp());

Expand All @@ -288,9 +291,13 @@ void basic_file::Private::writeChunkTable()
OutCbStream outStream(w.cb());

compress_chunk_table(w.cb(), chunks, chunk_size == VariableChunkSize);

int64_t eof = f->tellp();
// go back to where we're supposed to write chunk table offset
f->seekp(head12.point_offset);
f->write(reinterpret_cast<char*>(&chunk_table_offset), sizeof(chunk_table_offset));
// go back to the end of the file
f->seekp(eof);
}


Expand Down Expand Up @@ -326,9 +333,9 @@ uint64_t basic_file::newChunk()
return p_->newChunk();
}

void basic_file::close()
uint64_t basic_file::close()
{
p_->close();
return p_->close();
}

// named_file
Expand Down Expand Up @@ -394,11 +401,12 @@ named_file::named_file(const std::string& filename, const named_file::config& c)
named_file::~named_file()
{}

void named_file::close()
uint64_t named_file::close()
{
basic_file::close();
uint64_t byte_size = basic_file::close();
if (p_->file.is_open())
p_->file.close();
return byte_size;
}

// Chunk compressor
Expand Down Expand Up @@ -428,6 +436,30 @@ std::vector<unsigned char> chunk_compressor::done()
return p_->stream.buffer();
}

// writer::mem_file

struct mem_file::Private
{
Private(char *buf, size_t count) : sbuf(buf, count), f(&sbuf)
{}
charbuf sbuf;
std::ostream f;
};

mem_file::mem_file(
char *buf,
size_t count,
const lazperf::writer::named_file::config& c
) : p_(new Private(buf, count))
{
if (!open(p_->f, c.to_header(), c.chunk_size))
throw error("Couldn't open mem_file as LAS/LAZ");
}

mem_file::~mem_file()
{
}

} // namespace writer
} // namespace lazperf

20 changes: 18 additions & 2 deletions cpp/lazperf/writers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class basic_file
public:
LAZPERF_EXPORT bool open(std::ostream& out, const header12& h, uint32_t chunk_size);
LAZPERF_EXPORT void writePoint(const char *p);
LAZPERF_EXPORT void close();
LAZPERF_EXPORT uint64_t close();
LAZPERF_EXPORT uint64_t newChunk();
LAZPERF_EXPORT uint64_t firstChunkOffset() const;
LAZPERF_EXPORT virtual bool compressed() const;
Expand Down Expand Up @@ -78,7 +78,7 @@ class named_file : public basic_file
LAZPERF_EXPORT named_file(const std::string& filename, const config& c);
LAZPERF_EXPORT virtual ~named_file();

LAZPERF_EXPORT void close();
LAZPERF_EXPORT uint64_t close();

private:
std::unique_ptr<Private> p_;
Expand All @@ -97,6 +97,22 @@ class chunk_compressor
std::unique_ptr<Private> p_;
};

class mem_file : public basic_file
{
struct Private;

public:
LAZPERF_EXPORT mem_file(
char *buf,
size_t count,
const lazperf::writer::named_file::config& c
);
LAZPERF_EXPORT ~mem_file();

private:
std::unique_ptr<Private> p_;
};

} // namespace writer
} // namespace lazperf

13 changes: 13 additions & 0 deletions js/src/laz-perf.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,23 @@ declare class ChunkDecoder {

getPoint(pointer: Pointer): void
}
declare class LazWriter {
constructor()
delete(): void

open(data: Pointer, length: number): void
setPointFormat(format: number): LazWriter
setTransform(offsetX: number, offsetY: number, offsetZ: number, scaleX: number, scaleY: number, scaleZ: number): LazWriter
setChunkSize(chunkSize: number): LazWriter
writePoint(inputBuf: Pointer): void
newChunk(): bigint
close(): bigint
}

export declare interface LazPerf extends EmscriptenModule {
LASZip: typeof LASZip
ChunkDecoder: typeof ChunkDecoder
LazWriter: typeof LazWriter
}

declare const createLazPerf: EmscriptenModuleFactory<LazPerf>
Expand Down
21 changes: 8 additions & 13 deletions js/src/laz-perf.js

Large diffs are not rendered by default.

Binary file modified js/src/laz-perf.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion js/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"declarationDir": "lib",

"target": "es2019",
"module": "commonjs",
"module": "ESNext",

"lib": ["es2020", "dom"],
"declaration": true,
Expand Down