Welcome to mirror list, hosted at ThFree Co, Russian Federation.

input.hpp « carve « include « carve « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cddf6456c05bda87a03a3cbb2d033f7ea61c9e57 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Begin License:
// Copyright (C) 2006-2014 Tobias Sargeant (tobias.sargeant@gmail.com).
// All rights reserved.
//
// This file is part of the Carve CSG Library (http://carve-csg.com/)
//
// This file may be used under the terms of either the GNU General
// Public License version 2 or 3 (at your option) as published by the
// Free Software Foundation and appearing in the files LICENSE.GPL2
// and LICENSE.GPL3 included in the packaging of this file.
//
// This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
// INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE.
// End:


#pragma once

#include <map>
#include <string>

#include <carve/carve.hpp>
#include <carve/poly.hpp>
#include <carve/mesh.hpp>
#include <carve/polyline.hpp>
#include <carve/pointset.hpp>



namespace carve {
  namespace input {

    typedef std::map<std::string, std::string> Options;

    static inline Options opts() {
      return Options();
    }

    static inline Options opts(const char **kv) {
      Options r;
      for (size_t i = 0; kv[i] != NULL; i += 2) {
        r[kv[i]] = kv[i+1];
      }
      return r;
    }

    static inline Options opts(const std::string &k1, const std::string &v1) {
      Options r;
      r[k1] = v1;
      return r;
    }

    static inline Options opts(const std::string &k1, const std::string &v1,
                               const std::string &k2, const std::string &v2) {
      Options r;
      r[k1] = v1;
      r[k2] = v2;
      return r;
    }

    static inline Options opts(const std::string &k1, const std::string &v1,
                               const std::string &k2, const std::string &v2,
                               const std::string &k3, const std::string &v3) {
      Options r;
      r[k1] = v1;
      r[k2] = v2;
      r[k3] = v3;
      return r;
    }

    static inline bool _bool(const std::string &str, bool _default = false) {
      if (str == "true") return true;
      if (str == "false") return false;
      return _default;
    }

    struct Data {
      Data() {
      }

      virtual ~Data() {
      }

      virtual void transform(const carve::math::Matrix & /* transform */) {
      }
    };



    struct VertexData : public Data {
      std::vector<carve::geom3d::Vector> points;

      VertexData() : Data() {
      }

      virtual ~VertexData() {
      }

      virtual void transform(const carve::math::Matrix &transform) {
        for (size_t i = 0; i < points.size(); ++i) {
          points[i] *= transform;
        }
      }

      size_t addVertex(carve::geom3d::Vector point) {
        size_t index = points.size();
        points.push_back(point);
        return index;
      }
  
      inline void reserveVertices(int count) {
        points.reserve(count);
      }

      size_t getVertexCount() const {
        return points.size();
      }

      const carve::geom3d::Vector &getVertex(int index) const {
        return points[index];
      }
    };



    struct PolyhedronData : public VertexData {
      std::vector<int> faceIndices;
      int faceCount;

      PolyhedronData() : VertexData(), faceIndices(), faceCount(0) {
      }

      virtual ~PolyhedronData() {
      }

      void reserveFaces(int count, int avgFaceSize) {
        faceIndices.reserve(faceIndices.size() + count * (1 + avgFaceSize));
      }
  
      int getFaceCount() const {
        return faceCount;
      }
  
      template <typename Iter>
      void addFace(Iter begin, Iter end) {
        size_t n = std::distance(begin, end);
        faceIndices.reserve(faceIndices.size() + n + 1);
        faceIndices.push_back(n);
        std::copy(begin, end, std::back_inserter(faceIndices));
        ++faceCount;
      }
  
      void addFace(int a, int b, int c) {
        faceIndices.push_back(3);
        faceIndices.push_back(a);
        faceIndices.push_back(b);
        faceIndices.push_back(c);
        ++faceCount;
      }

      void addFace(int a, int b, int c, int d) {
        faceIndices.push_back(4);
        faceIndices.push_back(a);
        faceIndices.push_back(b);
        faceIndices.push_back(c);
        faceIndices.push_back(d);
        ++faceCount;
      }

      void clearFaces() {
        faceIndices.clear();
        faceCount = 0;
      }

      carve::poly::Polyhedron *create(const Options &options) const {
        return new carve::poly::Polyhedron(points, faceCount, faceIndices);
      }

      carve::mesh::MeshSet<3> *createMesh(const Options &options) const {
        Options::const_iterator i;
        carve::mesh::MeshOptions opts;
        i = options.find("avoid_cavities");
        if (i != options.end()) {
          opts.avoid_cavities(_bool((*i).second));
        }
        return new carve::mesh::MeshSet<3>(points, faceCount, faceIndices, opts);
      }
    };



    struct PolylineSetData : public VertexData {
      typedef std::pair<bool, std::vector<int> > polyline_data_t;
      std::list<polyline_data_t> polylines;

      PolylineSetData() : VertexData(), polylines() {
      }

      virtual ~PolylineSetData() {
      }

      void beginPolyline(bool closed = false) {
        polylines.push_back(std::make_pair(closed, std::vector<int>()));
      }

      void reservePolyline(size_t len) {
        polylines.back().second.reserve(len);
      }

      void addPolylineIndex(int idx) {
        polylines.back().second.push_back(idx);
      }

      carve::line::PolylineSet *create(const Options &options) const {
        carve::line::PolylineSet *p = new carve::line::PolylineSet(points);

        for (std::list<polyline_data_t>::const_iterator i = polylines.begin();
             i != polylines.end();
             ++i) {
          p->addPolyline((*i).first, (*i).second.begin(), (*i).second.end());
        }
        return p;
      }
    };



    struct PointSetData : public VertexData {

      PointSetData() : VertexData() {
      }

      virtual ~PointSetData() {
      }

      carve::point::PointSet *create(const Options &options) const {
        carve::point::PointSet *p = new carve::point::PointSet(points);
        return p;
      }
    };



    class Input {
    public:
      std::list<Data *> input;

      Input() {
      }

      ~Input() {
        for (std::list<Data *>::iterator i = input.begin(); i != input.end(); ++i) {
          delete (*i);
        }
      }

      void addDataBlock(Data *data) {
        input.push_back(data);
      }

      void transform(const carve::math::Matrix &transform) {
        if (transform == carve::math::Matrix::IDENT()) return;
        for (std::list<Data *>::iterator i = input.begin(); i != input.end(); ++i) {
          (*i)->transform(transform);
        }
      }

      template<typename T>
      static inline T *create(Data *d, const Options &options = Options()) {
        return NULL;
      }
    };

    template<>
    inline carve::mesh::MeshSet<3> *Input::create(Data *d, const Options &options) {
      PolyhedronData *p = dynamic_cast<PolyhedronData *>(d);
      if (p == NULL) return NULL;
      return p->createMesh(options);
    }

    template<>
    inline carve::poly::Polyhedron *Input::create(Data *d, const Options &options) {
      PolyhedronData *p = dynamic_cast<PolyhedronData *>(d);
      if (p == NULL) return NULL;
      return p->create(options);
    }

    template<>
    inline carve::line::PolylineSet *Input::create(Data *d, const Options &options) {
      PolylineSetData *p = dynamic_cast<PolylineSetData *>(d);
      if (p == NULL) return NULL;
      return p->create(options);
    }

    template<>
    inline carve::point::PointSet *Input::create(Data *d, const Options &options) {
      PointSetData *p = dynamic_cast<PointSetData *>(d);
      if (p == NULL) return NULL;
      return p->create(options);
    }

  }
}