• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pmp-library / pmp-library / 15070098106

16 May 2025 01:52PM UTC coverage: 91.027% (-0.07%) from 91.097%
15070098106

push

github

mbotsch
improve curvature example GUI

5204 of 5717 relevant lines covered (91.03%)

646093.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.29
/src/pmp/io/write_obj.cpp
1
// Copyright 2011-2022 the Polygon Mesh Processing Library developers.
2
// Distributed under a MIT-style license, see LICENSE.txt for details.
3

4
#include "pmp/io/write_obj.h"
5
#include <limits>
6
#include "pmp/exceptions.h"
7

8
namespace pmp {
9

10
void write_obj(const SurfaceMesh& mesh, const std::filesystem::path& file,
1✔
11
               const IOFlags& flags)
12
{
13
    FILE* out = fopen(file.string().c_str(), "w");
1✔
14
    if (!out)
1✔
15
        throw IOException("Failed to open file: " + file.string());
×
16

17
    // check if we can write the mesh using 32-bit indices
18
    const auto uint_max = std::numeric_limits<uint32_t>::max();
1✔
19
    if (mesh.n_vertices() > uint_max)
1✔
20
        throw InvalidInputException(
21
            "Mesh too large to be written with 32-bit indices.");
×
22

23
    // comment
24
    fprintf(out, "# OBJ export from PMP\n");
1✔
25

26
    // write vertices
27
    auto points = mesh.get_vertex_property<Point>("v:point");
1✔
28
    for (auto v : mesh.vertices())
4✔
29
    {
30
        const Point& p = points[v];
3✔
31
        fprintf(out, "v %.10f %.10f %.10f\n", p[0], p[1], p[2]);
3✔
32
    }
33

34
    // write normals
35
    auto normals = mesh.get_vertex_property<Normal>("v:normal");
1✔
36
    const bool write_normals = normals && flags.use_vertex_normals;
1✔
37
    if (write_normals)
1✔
38
    {
39
        for (auto v : mesh.vertices())
×
40
        {
41
            const Normal& n = normals[v];
×
42
            fprintf(out, "vn %.10f %.10f %.10f\n", n[0], n[1], n[2]);
×
43
        }
44
    }
45

46
    // write texture coordinates
47
    auto tex_coords = mesh.get_halfedge_property<TexCoord>("h:tex");
1✔
48
    const bool write_texcoords = tex_coords && flags.use_halfedge_texcoords;
1✔
49

50
    if (write_texcoords)
1✔
51
    {
52
        if (mesh.n_halfedges() > uint_max)
×
53
            throw InvalidInputException(
54
                "Mesh too large to be written with 32-bit indices.");
×
55

56
        for (auto h : mesh.halfedges())
×
57
        {
58
            const TexCoord& pt = tex_coords[h];
×
59
            fprintf(out, "vt %.10f %.10f\n", pt[0], pt[1]);
×
60
        }
61
    }
62

63
    // write faces
64
    for (auto f : mesh.faces())
2✔
65
    {
66
        fprintf(out, "f");
1✔
67

68
        auto h = mesh.halfedges(f);
1✔
69
        for (auto v : mesh.vertices(f))
4✔
70
        {
71
            auto idx = (uint32_t)(v.idx() + 1);
3✔
72
            if (write_texcoords)
3✔
73
            {
74
                if (write_normals)
×
75
                {
76
                    // write vertex index, texCoord index and normal index
77
                    fprintf(out, " %d/%d/%d", idx, (uint32_t)(*h).idx() + 1,
×
78
                            idx);
79
                }
80
                else
81
                {
82
                    // write vertex index, texCoord index
83
                    fprintf(out, " %d/%d/", idx, (uint32_t)(*h).idx() + 1);
×
84
                }
85
                ++h;
×
86
            }
87
            else if (write_normals)
3✔
88
            {
89
                // write vertex index and normal index
90
                fprintf(out, " %d//%d", idx, idx);
×
91
            }
92
            else
93
            {
94
                // write only vertex index
95
                fprintf(out, " %d", idx);
3✔
96
            }
97
        }
98
        fprintf(out, "\n");
1✔
99
    }
100

101
    fclose(out);
1✔
102
}
1✔
103

104
} // namespace pmp
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc