Manifold 3.0
Robust geometry
Loading...
Searching...
No Matches
mesh.h
1// Copyright 2021 The Manifold Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <cstdint>
18#include <type_traits>
19#include <vector>
20
21#include "manifold/common.h"
22
23namespace manifold {
24
28
99// MeshGLP / MeshGL / MeshGL64 are forward-declared in common.h; the
100// default `I = uint32_t` lives on the forward decl.
101template <typename Precision, typename I>
102struct MeshGLP {
104 I NumVert() const { return vertProperties.size() / numProp; };
106 I NumTri() const { return triVerts.size() / 3; };
108 I NumRun() const { return runOriginalID.size(); };
110 I numProp = 3;
114 std::vector<Precision> vertProperties;
117 std::vector<I> triVerts;
120 std::vector<I> mergeFromVert;
124 std::vector<I> mergeToVert;
132 std::vector<I> runIndex;
138 std::vector<uint32_t> runOriginalID;
143 std::vector<Precision> runTransform;
147 std::vector<uint8_t> runFlags;
153 std::vector<I> faceID;
158 std::vector<Precision> halfedgeTangent;
163 Precision tolerance = 0;
164
165 MeshGLP() = default;
166
182 bool Merge();
183
189 la::vec<Precision, 3> GetVertPos(size_t v) const {
190 size_t offset = v * numProp;
191 return la::vec<Precision, 3>(vertProperties[offset],
192 vertProperties[offset + 1],
193 vertProperties[offset + 2]);
194 }
195
201 la::vec<I, 3> GetTriVerts(size_t t) const {
202 size_t offset = 3 * t;
203 return la::vec<I, 3>(triVerts[offset], triVerts[offset + 1],
204 triVerts[offset + 2]);
205 }
206
212 la::vec<Precision, 4> GetTangent(size_t h) const {
213 size_t offset = 4 * h;
214 return la::vec<Precision, 4>(
215 halfedgeTangent[offset], halfedgeTangent[offset + 1],
216 halfedgeTangent[offset + 2], halfedgeTangent[offset + 3]);
217 }
218
224 mat3x4 GetRunTransform(size_t run) const {
225 size_t offset = 12 * run;
226 if (offset + 12 > runTransform.size()) {
227 return la::identity;
228 }
229 return mat3x4(la::mat<Precision, 3, 4>(&runTransform[offset]));
230 }
231
240 bool Backside(size_t run) const {
241 return run < runFlags.size() && (runFlags[run] & 1) != 0;
242 }
243
260 bool HasNormals(size_t run) const {
261 return run < runFlags.size() && (runFlags[run] & 2) != 0;
262 }
263};
264
265// MeshGL / MeshGL64 aliases live in common.h. Pin the default template
266// arg so dropping `I = uint32_t` breaks the build instead of silently
267// changing MeshGL's index type.
268static_assert(std::is_same<MeshGL, MeshGLP<float, uint32_t>>::value,
269 "MeshGL default index type must stay uint32_t");
270
271#ifndef MANIFOLD_NO_IOSTREAM
272MeshGL64 ReadOBJ(std::istream& stream);
273bool WriteOBJ(std::ostream& stream, const MeshGL64& mesh);
274#endif
276} // namespace manifold
la::vec< I, 3 > GetTriVerts(size_t t) const
Definition mesh.h:201
bool Backside(size_t run) const
Definition mesh.h:240
std::vector< I > mergeFromVert
Definition mesh.h:120
I NumRun() const
Number of triangle runs.
Definition mesh.h:108
std::vector< I > runIndex
Definition mesh.h:132
std::vector< float > vertProperties
Definition mesh.h:114
std::vector< float > runTransform
Definition mesh.h:143
std::vector< uint8_t > runFlags
Definition mesh.h:147
std::vector< I > mergeToVert
Definition mesh.h:124
I NumTri() const
Number of triangles.
Definition mesh.h:106
mat3x4 GetRunTransform(size_t run) const
Definition mesh.h:224
std::vector< float > halfedgeTangent
Definition mesh.h:158
bool HasNormals(size_t run) const
Definition mesh.h:260
std::vector< uint32_t > runOriginalID
Definition mesh.h:138
std::vector< I > triVerts
Definition mesh.h:117
la::vec< Precision, 4 > GetTangent(size_t h) const
Definition mesh.h:212
float tolerance
Definition mesh.h:163
I NumVert() const
Number of property vertices.
Definition mesh.h:104
std::vector< I > faceID
Definition mesh.h:153
la::vec< Precision, 3 > GetVertPos(size_t v) const
Definition mesh.h:189
I numProp
Definition mesh.h:110