aboutsummaryrefslogtreecommitdiff
path: root/dep/include/g3dlite/G3D/MeshBuilder.h
blob: 9920d59d7d3ceecd02ae83ac56958083c376f981 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
  @file MeshBuilder.h

  @maintainer Morgan McGuire, http://graphics.cs.williams.edu

  @created 2002-02-27
  @edited  2004-10-04
 */ 
#ifndef G3D_MESHBUILDER_H
#define G3D_MESHBUILDER_H

#include "G3D/platform.h"
#include "G3D/Array.h"
#include "G3D/Vector3.h"
#include "G3D/Triangle.h"

namespace G3D {

/**
 Allows creation of optimized watertight meshes from unoptimized polygon soups.
 See also G3D::MeshAlg for algorithms that operate on the output.
 */
class MeshBuilder {
public:

    /**
     Set setWeldRadius to AUTO_WELD to weld vertices closer than 1/2
     the smallest edge length in a model.
     */
    enum {AUTO_WELD = -100};

private:
    /** Indices of vertices in <B>or near</B>  a grid cell. */
    typedef Array<int> List;
   
    std::string                 name;
    
    /**
     All of the triangles, as a long triangle list.
     */
    Array<Vector3>              triList;

    void centerTriList();
    void computeBounds(Vector3& min, Vector3& max);

	bool _twoSided;

    /** Collapse radius */
    double close;

public:

	inline MeshBuilder(bool twoSided = false) : _twoSided(twoSided), close(AUTO_WELD) {}

    /** Writes the model to the arrays, which can then be used with
        G3D::IFSModel::save and G3D::MeshAlg */
    void commit(std::string& name, Array<int>& indexArray, Array<Vector3>& vertexArray);

    /**
     Adds a new triangle to the model. (Counter clockwise)
     */
    void addTriangle(const Vector3& a, const Vector3& b, const Vector3& c);

    /**
     Adds two new triangles to the model. (Counter clockwise)
     */
    void addQuad(const Vector3& a, const Vector3& b, const Vector3& c, const Vector3& d);

    void addTriangle(const Triangle& t);

    void setName(const std::string& n);

    /** Vertices within this distance are considered identical.  
        Use AUTO_WELD (the default) to have the distance be a function of the model size.*/
    void setWeldRadius(double r) {
        close = r;
    }
};

} // namespace

#endif