Skip to content
This repository was archived by the owner on Mar 8, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
703464a
Compute vertex normals in Mesh.
fester Jan 15, 2019
9bf2414
Added has_normals() to Mesh.
fester Jan 25, 2019
cc2b9b7
QuantizedMeshIO can write mesh normals.
fester Jan 25, 2019
8b85ced
Addded a command line switch to write QM normals.
fester Jan 25, 2019
002aeee
Fix formatting.
fester Jan 28, 2019
1d965aa
Correctly limit zoom levels for raster overviews.
fester Feb 4, 2019
c9a11ec
Formatting gone wrong, again.
fester Feb 5, 2019
a8cb8ef
Make windows build compatible with VS 2017
guischulz Apr 28, 2019
08c03e5
add max-iterations for Terra mesh
lekks Nov 15, 2019
97c33eb
throw error if max-iterations set for non terra method
lekks Nov 15, 2019
2cebb52
default to triangle mesh mode when generating a single .terrain file
jtfell Jan 16, 2020
3d18e29
fix out of bounds on to_average array - 17435580.tif
qGYdXbY2 Feb 25, 2020
42acac5
fix out of bounds on to_average array - 17435580.tif
qGYdXbY2 Feb 25, 2020
74f1e54
Merge branch 'master' of https://github.com/qGYdXbY2/tin-terrain
qGYdXbY2 Feb 25, 2020
91f1d7d
Merge branch 'master' into terrain-normals.fester
qGYdXbY2 Feb 25, 2020
c962616
Terrain normals.fester #55
qGYdXbY2 Feb 25, 2020
7a3f94b
Merge branch 'windows-msvc-compatibility' of https://github.com/guisc…
qGYdXbY2 Feb 25, 2020
4c21cc4
Make windows build compatible with VS 2017 #59
qGYdXbY2 Feb 25, 2020
20e4a9e
Merge branch 'max-iterations' of https://github.com/TraceAir/tin-terr…
qGYdXbY2 Feb 25, 2020
b64f657
issue4 --max-iterations #66
qGYdXbY2 Feb 25, 2020
866300f
Merge branch 'terrain-output-dem2tin.jtfell' of https://github.com/jt…
qGYdXbY2 Feb 25, 2020
654a939
Default to triangle mesh mode when generating a single .terrain file #67
qGYdXbY2 Feb 25, 2020
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
24 changes: 17 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ option(TNTN_DOWNLOAD_DEPS "download dependencies during cmake configure" ON)

set(CMAKE_CXX_STANDARD 14)

if(MSVC)
if(MSVC_VERSION LESS 1910)
message(FATAL_ERROR "Visual Studio 2017 version 15.0 or higher is required")
endif()
endif()

# get the current working branch
execute_process(
Expand All @@ -31,14 +36,19 @@ find_package(GDAL REQUIRED)
if(NOT GDAL_FOUND)
message(FATAL_ERROR "GDAL not found, cannot proceed")
endif()
if(NOT GDAL_CONFIG)
message(FATAL_ERROR "gdal-config command not found (not in PATH?), cannot proceed")
endif()

execute_process(
COMMAND ${GDAL_CONFIG} --version
OUTPUT_VARIABLE SYSTEM_GDAL_VERSION
)
if(GDAL_VERSION)
set(SYSTEM_GDAL_VERSION ${GDAL_VERSION})
else()
if(NOT GDAL_CONFIG)
message(FATAL_ERROR "gdal-config command not found (not in PATH?), cannot proceed")
endif()

execute_process(
COMMAND ${GDAL_CONFIG} --version
OUTPUT_VARIABLE SYSTEM_GDAL_VERSION
)
endif()

if(SYSTEM_GDAL_VERSION VERSION_LESS "2.2")
message(FATAL_ERROR "GDAL version \"${SYSTEM_GDAL_VERSION}\" is too old, at least 2.2 is required")
Expand Down
4 changes: 4 additions & 0 deletions include/tntn/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include <string>
#include <vector>

#ifdef _MSC_VER
#define ftello ftell
#endif

namespace tntn {

class FileLike
Expand Down
9 changes: 7 additions & 2 deletions include/tntn/FileFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
#include <string.h>
#include "tntn/MeshMode.h"

#ifdef _MSC_VER
#define strncasecmp _strnicmp
#define strcasecmp _stricmp
#endif

namespace tntn {

class FileFormat
Expand Down Expand Up @@ -114,8 +119,8 @@ class FileFormat
switch(m_value)
{
case OBJ: //fallthrough
case OFF: //fallthrough
case TERRAIN: return MeshMode::decomposed;
case OFF: return MeshMode::decomposed;
case TERRAIN: return MeshMode::triangles;
default: return MeshMode::none;
}
}
Expand Down
5 changes: 5 additions & 0 deletions include/tntn/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Mesh
SimpleRange<const Triangle*> triangles() const;
SimpleRange<const Face*> faces() const;
SimpleRange<const Vertex*> vertices() const;
SimpleRange<const Normal*> vertex_normals() const;

void grab_triangles(std::vector<Triangle>& into);
void grab_decomposed(std::vector<Vertex>& vertices, std::vector<Face>& faces);
Expand All @@ -62,6 +63,9 @@ class Mesh
bool is_square() const;
bool check_for_holes_in_square_mesh() const;

void compute_vertex_normals();
bool has_normals() const;

private:
bool semantic_equal_tri_tri(const Mesh& other) const;
bool semantic_equal_dec_dec(const Mesh& other) const;
Expand All @@ -70,6 +74,7 @@ class Mesh
std::vector<Vertex> m_vertices;
std::vector<Face> m_faces;
std::vector<Triangle> m_triangles;
std::vector<Normal> m_normals;

void decompose_triangle(const Triangle& t);
};
Expand Down
2 changes: 1 addition & 1 deletion include/tntn/TerraMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TerraMesh : public TerraBaseMesh
const double no_data_value);

public:
void greedy_insert(double max_error);
void greedy_insert(double max_error, int max_iterations);
void scan_triangle(dt_ptr t) override;
std::unique_ptr<Mesh> convert_to_mesh();
};
Expand Down
7 changes: 5 additions & 2 deletions include/tntn/TileMaker.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ namespace tntn {
class TileMaker
{
std::unique_ptr<Mesh> m_mesh;

bool m_normals_enabled;

public:
TileMaker() : m_mesh(std::make_unique<Mesh>()) {}
TileMaker(bool normals_enabled=false) : m_mesh(std::make_unique<Mesh>()),
m_normals_enabled(normals_enabled) {}

bool normals_enabled() const;
void setMeshWriter(MeshWriter* w);
bool loadObj(const char* filename);
void loadMesh(std::unique_ptr<Mesh> mesh);
Expand Down
1 change: 1 addition & 0 deletions include/tntn/dem2tintiles_workflow.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ std::vector<Partition> create_partitions_for_zoom_level(const RasterDouble& dem,
bool create_tiles_for_zoom_level(const RasterDouble& dem,
const std::vector<Partition>& partitions,
int zoom,
bool include_normals,
const std::string& output_basedir,
const double method_parameter,
const std::string& meshing_method,
Expand Down
2 changes: 1 addition & 1 deletion include/tntn/endianness.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# define TNTN_BIG_ENDIAN
#elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN || defined(__LITTLE_ENDIAN__) || \
defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || defined(_MIPSEL) || \
defined(__MIPSEL) || defined(__MIPSEL__)
defined(__MIPSEL) || defined(__MIPSEL__) || defined(_WIN32)
# define TNTN_LITTLE_ENDIAN
#else
# error unknown architecture
Expand Down
2 changes: 2 additions & 0 deletions include/tntn/geometrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ typedef std::array<Vertex, 3> Triangle;
typedef size_t VertexIndex; //0-based index into an array/vector of vertices
typedef std::array<VertexIndex, 3> Face;

typedef glm::dvec3 Normal;

struct Edge
{
Edge() = default;
Expand Down
6 changes: 3 additions & 3 deletions include/tntn/terra_meshing.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

namespace tntn {

std::unique_ptr<Mesh> generate_tin_terra(std::unique_ptr<RasterDouble> raster, double max_error);
std::unique_ptr<Mesh> generate_tin_terra(std::unique_ptr<RasterDouble> raster, double max_error, int max_iterations=0);

std::unique_ptr<Mesh> generate_tin_terra(std::unique_ptr<SurfacePoints> surface_points,
double max_error);
std::unique_ptr<Mesh> generate_tin_terra(const SurfacePoints& surface_points, double max_error);
double max_error, int max_iterations=0);
std::unique_ptr<Mesh> generate_tin_terra(const SurfacePoints& surface_points, double max_error, int max_iterations=0);

} //namespace tntn
51 changes: 50 additions & 1 deletion src/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
#include <limits>
#include <unordered_map>
#include <unordered_set>
#include <glm/gtx/normal.hpp>

#include "tntn/tntn_assert.h"
#include "tntn/logging.h"
#include "tntn/geometrix.h"

namespace tntn {

void Mesh::clear()
{
m_triangles.clear();
m_vertices.clear();
m_faces.clear();
m_normals.clear();
}

void Mesh::clear_triangles()
Expand All @@ -36,6 +37,7 @@ Mesh Mesh::clone() const
out.m_faces = m_faces;
out.m_vertices = m_vertices;
out.m_triangles = m_triangles;
out.m_normals = m_normals;
return out;
}

Expand Down Expand Up @@ -217,6 +219,15 @@ SimpleRange<const Vertex*> Mesh::vertices() const
return {m_vertices.data(), m_vertices.data() + m_vertices.size()};
}

SimpleRange<const Normal*> Mesh::vertex_normals() const
{
if(m_normals.empty())
{
return {nullptr, nullptr};
}
return {m_normals.data(), m_normals.data() + m_normals.size()};
}

void Mesh::grab_triangles(std::vector<Triangle>& into)
{
into.clear();
Expand Down Expand Up @@ -710,4 +721,42 @@ bool Mesh::check_tin_properties() const
return true;
}

bool Mesh::has_normals() const
{
return !m_normals.empty();
}

void Mesh::compute_vertex_normals()
{
using namespace glm;

std::vector<dvec3> face_normals;
face_normals.reserve(m_faces.size());
m_normals.resize(m_vertices.size());

auto face_normal = [](const Triangle& t) {
return normalize(cross(t[0] - t[2], t[1] - t[2]));
};

std::transform(m_triangles.begin(), m_triangles.end(), face_normals.begin(), face_normal);

const int faces_count = m_faces.size();

for(int f = 0; f < faces_count; f++)
{
const auto& face = m_faces[f];
const auto& face_normal = face_normals[f];

for(int v = 0; v < 3; v++)
{
const int vertex_id = face[v];
m_normals[vertex_id] += face_normal;
}
}

std::transform(m_normals.begin(), m_normals.end(), m_normals.begin(), [](const auto& n) {
return normalize(n);
});
}

} //namespace tntn
43 changes: 43 additions & 0 deletions src/QuantizedMeshIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ struct QuantizedMeshHeader
// double HorizonOcclusionPointZ;
};

enum QuantizedMeshExtensionList
{
QMExtensionNormals = 1,
QMExtensionWaterMask = 2
};

namespace detail {

uint16_t zig_zag_encode(int16_t i)
Expand Down Expand Up @@ -317,6 +323,37 @@ bool point_to_ecef(Vertex& p)
return (bool)tr->Transform(1, &p.x, &p.y, &p.z);
}

void oct_encode(const Normal& v, uint8_t out[2])
{
const glm::dvec2 p = v.xy * (1.0 / (abs(v.x) + abs(v.y) + abs(v.z)));
const auto signNotZero = glm::dvec2(v.x >= 0 ? 1.0 : -1.0, v.y > 0 ? 1.0 : -1.0);
const glm::dvec2 abs_v = p.yx;
glm::dvec2 ov = (v.z <= 0.0) ? ((1.0 - glm::abs(abs_v)) * signNotZero) : p;

ov = (ov + 1.0) / 2.0;

out[0] = static_cast<uint8_t>(ov.x * 255);
out[1] = static_cast<uint8_t>(ov.y * 255);
}

void write_normals(BinaryIO& bio,
BinaryIOErrorTracker& e,
const SimpleRange<const Normal*>& normals)
{
// Write extension header
bio.write_byte(QMExtensionNormals, e);
bio.write_uint32(normals.distance() * 2, e);

uint8_t oct_normal[2];

for(auto n = normals.begin; n != normals.end; n++)
{
oct_encode(*n, oct_normal);
bio.write_byte(oct_normal[0], e);
bio.write_byte(oct_normal[1], e);
}
}

bool write_mesh_as_qm(const std::shared_ptr<FileLike>& f,
const Mesh& m,
const BBox3D& bbox,
Expand Down Expand Up @@ -496,13 +533,19 @@ bool write_mesh_as_qm(const std::shared_ptr<FileLike>& f,
write_indices<uint32_t>(bio, e, northlings);
}

if(m.has_normals())
{
write_normals(bio, e, m.vertex_normals());
}

if(e.has_error())
{
TNTN_LOG_ERROR("{} in file {}", e.to_string(), f->name());
return false;
}

TNTN_LOG_INFO("writer log: {}", log.to_string());

return true;
}

Expand Down
Loading