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

geom.hpp « carve « include « carve « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 360f14f2607e3fb7dfedf58e2bf2b19a932a9645 (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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Begin License:
// Copyright (C) 2006-2011 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 the GNU General Public
// License version 2.0 as published by the Free Software Foundation
// and appearing in the file LICENSE.GPL2 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 <carve/carve.hpp>

#include <vector>

namespace carve {
  namespace geom {

    template<unsigned ndim> struct aabb;

    // ========================================================================
    struct _uninitialized { };

    template<unsigned ndim>
    struct base {
      double v[ndim];
    };

    template<> struct base<2> {union { double v[2]; struct { double x, y; }; }; };
    template<> struct base<3> {union { double v[3]; struct { double x, y, z; }; }; };
    template<> struct base<4> {union { double v[4]; struct { double x, y, z, w; }; }; };

    template<unsigned ndim>
    struct vector : public base<ndim> {
      enum { __ndim = ndim };

      static vector ZERO();
      double length2() const;
      double length() const;
      vector<ndim> &normalize();
      vector<ndim> normalized() const;
      bool exactlyZero() const;
      bool isZero(double epsilon = EPSILON) const;
      void setZero();
      void fill(double val);
      vector<ndim> &scaleBy(double d);
      vector<ndim> &invscaleBy(double d);
      vector<ndim> scaled(double d) const;
      vector<ndim> invscaled(double d) const;
      vector<ndim> &negate();
      vector<ndim> negated() const;
      double &operator[](unsigned i);
      const double &operator[](unsigned i) const;
      template<typename assign_t>
      vector<ndim> &operator=(const assign_t &t);
      std::string asStr() const;

      aabb<ndim> getAABB() const;

      vector() { setZero(); }
      vector(noinit_t) { }
    };

    template<unsigned ndim>
    vector<ndim> vector<ndim>::ZERO() { vector<ndim> r; r.setZero(); return r; }

    static inline vector<2> VECTOR(double x, double y) { vector<2> r; r.x = x; r.y = y; return r; }
    static inline vector<3> VECTOR(double x, double y, double z) { vector<3> r; r.x = x; r.y = y; r.z = z; return r; }
    static inline vector<4> VECTOR(double x, double y, double z, double w) { vector<4> r; r.x = x; r.y = y; r.z = z; r.w = w; return r; }

    template<unsigned ndim> vector<ndim> operator+(const vector<ndim> &a, const vector<ndim> &b);
    template<unsigned ndim> vector<ndim> operator+(const vector<ndim> &a, double b);
    template<unsigned ndim, typename val_t> vector<ndim> operator+(const vector<ndim> &a, const val_t &b);
    template<unsigned ndim, typename val_t> vector<ndim> operator+(const val_t &a, const vector<ndim> &b);

    template<unsigned ndim> vector<ndim> &operator+=(vector<ndim> &a, const vector<ndim> &b);
    template<unsigned ndim> vector<ndim> &operator+=(vector<ndim> &a, double b);
    template<unsigned ndim, typename val_t> vector<ndim> &operator+=(vector<ndim> &a, const val_t &b);

    template<unsigned ndim> vector<ndim> operator-(const vector<ndim> &a);

    template<unsigned ndim> vector<ndim> operator-(const vector<ndim> &a, const vector<ndim> &b);
    template<unsigned ndim> vector<ndim> operator-(const vector<ndim> &a, double b);
    template<unsigned ndim, typename val_t> vector<ndim> operator-(const vector<ndim> &a, const val_t &b);
    template<unsigned ndim, typename val_t> vector<ndim> operator-(const val_t &a, const vector<ndim> &b);

    template<unsigned ndim> vector<ndim> &operator-=(vector<ndim> &a, const vector<ndim> &b);
    template<unsigned ndim> vector<ndim> &operator-=(vector<ndim> &a, double b);
    template<unsigned ndim, typename val_t> vector<ndim> &operator-=(vector<ndim> &a, const val_t &b);

    template<unsigned ndim> vector<ndim> operator*(const vector<ndim> &a, double s);
    template<unsigned ndim> vector<ndim> operator*(double s, const vector<ndim> &a);
    template<unsigned ndim> vector<ndim> &operator*=(vector<ndim> &a, double s);

    template<unsigned ndim> vector<ndim> operator/(const vector<ndim> &a, double s);
    template<unsigned ndim> vector<ndim> &operator/=(vector<ndim> &a, double s);

    template<unsigned ndim> bool operator==(const vector<ndim> &a, const vector<ndim> &b);
    template<unsigned ndim> bool operator!=(const vector<ndim> &a, const vector<ndim> &b);
    template<unsigned ndim> bool operator<(const vector<ndim> &a, const vector<ndim> &b);
    template<unsigned ndim> bool operator<=(const vector<ndim> &a, const vector<ndim> &b);
    template<unsigned ndim> bool operator>(const vector<ndim> &a, const vector<ndim> &b);
    template<unsigned ndim> bool operator>=(const vector<ndim> &a, const vector<ndim> &b);

    template<unsigned ndim> vector<ndim> abs(const vector<ndim> &a);

    template<unsigned ndim> double distance2(const vector<ndim> &a, const vector<ndim> &b);

    template<unsigned ndim> double distance(const vector<ndim> &a, const vector<ndim> &b);

    template<unsigned ndim> bool equal(const vector<ndim> &a, const vector<ndim> &b);

    template<unsigned ndim> int smallestAxis(const vector<ndim> &a);

    template<unsigned ndim> int largestAxis(const vector<ndim> &a);

    template<unsigned ndim> vector<2> select(const vector<ndim> &a, int a1, int a2);

    template<unsigned ndim> vector<3> select(const vector<ndim> &a, int a1, int a2, int a3);

    template<unsigned ndim, typename assign_t, typename oper_t>
    vector<ndim> &assign_op(vector<ndim> &a, const assign_t &t, oper_t op);

    template<unsigned ndim, typename assign1_t, typename assign2_t, typename oper_t>
    vector<ndim> &assign_op(vector<ndim> &a, const assign1_t &t1, const assign2_t &t2, oper_t op);

    template<unsigned ndim, typename iter_t>
    void bounds(iter_t begin, iter_t end, vector<ndim> &min, vector<ndim> &max);

    template<unsigned ndim, typename iter_t, typename adapt_t>
    void bounds(iter_t begin, iter_t end, adapt_t adapt, vector<ndim> &min, vector<ndim> &max);

    template<unsigned ndim, typename iter_t>
    void centroid(iter_t begin, iter_t end, vector<ndim> &c);

    template<unsigned ndim, typename iter_t, typename adapt_t>
    void centroid(iter_t begin, iter_t end, adapt_t adapt, vector<ndim> &c);

    template<unsigned ndim, typename val_t> double dot(const vector<ndim> &a, const val_t &b);

    static inline vector<3> cross(const vector<3> &a, const vector<3> &b);

    static inline double cross(const vector<2> &a, const vector<2> &b);

    static inline double dotcross(const vector<3> &a, const vector<3> &b, const vector<3> &c);



    // ========================================================================
    struct axis_pos {
      int axis;
      double pos;

      axis_pos(int _axis, double _pos) : axis(_axis), pos(_pos) { }
    };

    template<unsigned ndim>
    double distance(const axis_pos &a, const vector<ndim> &b);

    template<unsigned ndim>
    double distance2(const axis_pos &a, const vector<ndim> &b);

    template<unsigned ndim> bool operator<(const axis_pos &a, const vector<ndim> &b);
    template<unsigned ndim> bool operator<(const vector<ndim> &a, const axis_pos &b);

    template<unsigned ndim> bool operator<=(const axis_pos &a, const vector<ndim> &b);
    template<unsigned ndim> bool operator<=(const vector<ndim> &a, const axis_pos &b);

    template<unsigned ndim> bool operator>(const axis_pos &a, const vector<ndim> &b);
    template<unsigned ndim> bool operator>(const vector<ndim> &a, const axis_pos &b);

    template<unsigned ndim> bool operator>=(const axis_pos &a, const vector<ndim> &b);
    template<unsigned ndim> bool operator>=(const vector<ndim> &a, const axis_pos &b);

    template<unsigned ndim> bool operator==(const axis_pos &a, const vector<ndim> &b);
    template<unsigned ndim> bool operator==(const vector<ndim> &a, const axis_pos &b);

    template<unsigned ndim> bool operator!=(const axis_pos &a, const vector<ndim> &b);
    template<unsigned ndim> bool operator!=(const vector<ndim> &a, const axis_pos &b);



    // ========================================================================
    template<unsigned ndim>
    struct ray {
      typedef vector<ndim> vector_t;

      vector_t D, v;

      bool OK() const;

      ray() { }
      ray(vector_t _D, vector_t _v) : D(_D), v(_v) { }
    };

    template<unsigned ndim>
    ray<ndim> rayThrough(const vector<ndim> &a, const vector<ndim> &b);

    static inline double distance2(const ray<3> &r, const vector<3> &v);

    static inline double distance(const ray<3> &r, const vector<3> &v);

    static inline double distance2(const ray<2> &r, const vector<2> &v);

    static inline double distance(const ray<2> &r, const vector<2> &v);



    // ========================================================================
    template<unsigned ndim>
    struct linesegment {
      typedef vector<ndim> vector_t;

      vector_t v1;
      vector_t v2;
      vector_t midpoint;
      vector_t half_length;

      void update();
      bool OK() const;
      void flip();

      aabb<ndim> getAABB() const;

      linesegment(const vector_t &_v1, const vector_t &_v2);
    };

    template<unsigned ndim>
    double distance2(const linesegment<ndim> &l, const vector<ndim> &v);

    template<unsigned ndim>
    double distance(const linesegment<ndim> &l, const vector<ndim> &v);



    // ========================================================================
    template<unsigned ndim>
    struct plane {
      typedef vector<ndim> vector_t;

      vector_t N;
      double d;

      void negate();

      plane();
      plane(const vector_t &_N, vector_t _p);
      plane(const vector_t &_N, double _d);
    };

    template<unsigned ndim>
    inline plane<ndim> operator-(const plane<ndim> &p);

    template<unsigned ndim, typename val_t>
    double distance(const plane<ndim> &plane, const val_t &point);

    template<unsigned ndim, typename val_t>
    double distance2(const plane<ndim> &plane, const val_t &point);

    template<unsigned ndim>
    static inline vector<ndim> closestPoint(const plane<ndim> &p, const vector<ndim> &v);



    // ========================================================================
    template<unsigned ndim>
    struct sphere {
      typedef vector<ndim> vector_t;

      vector_t C;
      double r;

      aabb<ndim> getAABB() const;

      sphere();
      sphere(const vector_t &_C, double _r);
    };

    template<unsigned ndim, typename val_t>
    double distance(const sphere<ndim> &sphere, const val_t &point);

    template<unsigned ndim, typename val_t>
    double distance2(const sphere<ndim> &sphere, const val_t &point);

    template<unsigned ndim>
    static inline vector<ndim> closestPoint(const sphere<ndim> &sphere, const vector<ndim> &point);


    // ========================================================================
    template<unsigned ndim>
    struct tri {
      typedef vector<ndim> vector_t;

      vector_t v[3];

      aabb<ndim> getAABB() const;

      tri() { }
      tri(vector_t _v[3]);
      tri(const vector_t &a, const vector_t &b, const vector_t &c);

      vector_t normal() const {
        return cross(v[1] - v[0], v[2] - v[1]).normalized();
      }
    };



    template<unsigned ndim> std::ostream &operator<<(std::ostream &o, const vector<ndim> &v);
    template<unsigned ndim> std::ostream &operator<<(std::ostream &o, const carve::geom::plane<ndim> &p);
    template<unsigned ndim> std::ostream &operator<<(std::ostream &o, const carve::geom::sphere<ndim> &sphere);
    template<unsigned ndim> std::ostream &operator<<(std::ostream &o, const carve::geom::tri<ndim> &tri);



    template<unsigned ndim> vector<ndim> closestPoint(const tri<ndim> &tri, const vector<ndim> &pt);
    template<unsigned ndim> double distance(const tri<ndim> &tri, const vector<ndim> &pt);
    template<unsigned ndim> double distance2(const tri<ndim> &tri, const vector<ndim> &pt);



    // ========================================================================
    struct distance_functor {
      template<typename obj1_t, typename obj2_t>
      double operator()(const obj1_t &o1, const obj2_t &o2) {
        return distance(o1, o2);
      }
    };



    // ========================================================================
    template<int base, int power> struct __pow__          { enum { val = __pow__<base, (power >> 1)>::val * __pow__<base, power - (power >> 1)>::val }; };
    template<int base>            struct __pow__<base, 1> { enum { val = base }; };
    template<int base>            struct __pow__<base, 0> { enum { val = 1 }; };

    template<unsigned base, unsigned ndigits>
    struct quantize {
      typedef __pow__<base, ndigits> fac;

      double operator()(double in) {
        return round(in * fac::val) / fac::val;
      }

      template<unsigned ndim>
      vector<ndim> operator()(const vector<ndim> &in) {
        vector<ndim> r(NOINIT);
        assign_op(r, in, *this);
        return r;
      }
    };



  }
}


#include <carve/geom_impl.hpp>