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

face.cpp « lib « carve « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cb56478626f6c672613efe29e1cb50180e11a3dd (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
// 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:


#if defined(HAVE_CONFIG_H)
#  include <carve_config.h>
#endif

#include <carve/poly.hpp>

namespace {

  double CALC_X(const carve::geom::plane<3> &p, double y, double z) { return -(p.d + p.N.y * y + p.N.z * z) / p.N.x; }
  double CALC_Y(const carve::geom::plane<3> &p, double x, double z) { return -(p.d + p.N.x * x + p.N.z * z) / p.N.y; }
  double CALC_Z(const carve::geom::plane<3> &p, double x, double y) { return -(p.d + p.N.x * x + p.N.y * y) / p.N.z; }

}  // namespace

namespace carve {
  namespace poly {

    namespace {

      carve::geom2d::P2 _project_1(const carve::geom3d::Vector &v) {
        return carve::geom::VECTOR(v.z, v.y);
      }

      carve::geom2d::P2 _project_2(const carve::geom3d::Vector &v) {
        return carve::geom::VECTOR(v.x, v.z);
      }

      carve::geom2d::P2 _project_3(const carve::geom3d::Vector &v) {
        return carve::geom::VECTOR(v.y, v.x);
      }

      carve::geom2d::P2 _project_4(const carve::geom3d::Vector &v) {
        return carve::geom::VECTOR(v.y, v.z);
      }

      carve::geom2d::P2 _project_5(const carve::geom3d::Vector &v) {
        return carve::geom::VECTOR(v.z, v.x);
      }

      carve::geom2d::P2 _project_6(const carve::geom3d::Vector &v) {
        return carve::geom::VECTOR(v.x, v.y);
      }


      carve::geom3d::Vector _unproject_1(const carve::geom2d::P2 &p, const carve::geom3d::Plane &plane_eqn) {
        return carve::geom::VECTOR(CALC_X(plane_eqn, p.y, p.x), p.y, p.x);
      }

      carve::geom3d::Vector _unproject_2(const carve::geom2d::P2 &p, const carve::geom3d::Plane &plane_eqn) {
        return carve::geom::VECTOR(p.x, CALC_Y(plane_eqn, p.x, p.y), p.y);
      }

      carve::geom3d::Vector _unproject_3(const carve::geom2d::P2 &p, const carve::geom3d::Plane &plane_eqn) {
        return carve::geom::VECTOR(p.y, p.x, CALC_Z(plane_eqn, p.y, p.x));
      }

      carve::geom3d::Vector _unproject_4(const carve::geom2d::P2 &p, const carve::geom3d::Plane &plane_eqn) {
        return carve::geom::VECTOR(CALC_X(plane_eqn, p.x, p.y), p.x, p.y);
      }

      carve::geom3d::Vector _unproject_5(const carve::geom2d::P2 &p, const carve::geom3d::Plane &plane_eqn) {
        return carve::geom::VECTOR(p.y, CALC_Y(plane_eqn, p.y, p.x), p.x);
      }

      carve::geom3d::Vector _unproject_6(const carve::geom2d::P2 &p, const carve::geom3d::Plane &plane_eqn) {
        return carve::geom::VECTOR(p.x, p.y, CALC_Z(plane_eqn, p.x, p.y));
      }

    }  // namespace

    static carve::geom2d::P2 (*project_tab[2][3])(const carve::geom3d::Vector &) = {
      { &_project_1, &_project_2, &_project_3 },
      { &_project_4, &_project_5, &_project_6 }
    };

    static carve::geom3d::Vector (*unproject_tab[2][3])(const carve::geom2d::P2 &, const carve::geom3d::Plane &) = {
      { &_unproject_1, &_unproject_2, &_unproject_3 },
      { &_unproject_4, &_unproject_5, &_unproject_6 }
    };

    // only implemented for 3d.
    template<unsigned ndim>
    typename Face<ndim>::project_t Face<ndim>::getProjector(bool positive_facing, int axis) {
      return NULL;
    }

    template<>
    Face<3>::project_t Face<3>::getProjector(bool positive_facing, int axis) {
      return project_tab[positive_facing ? 1 : 0][axis];
    }

    template<unsigned ndim>
    typename Face<ndim>::unproject_t Face<ndim>::getUnprojector(bool positive_facing, int axis) {
      return NULL;
    }

    template<>
    Face<3>::unproject_t Face<3>::getUnprojector(bool positive_facing, int axis) {
      return unproject_tab[positive_facing ? 1 : 0][axis];
    }



    template<unsigned ndim>
    Face<ndim>::Face(const std::vector<const vertex_t *> &_vertices,
                     bool delay_recalc) : tagable() {
      vertices = _vertices;
      edges.resize(nVertices(), NULL);
      if (!delay_recalc && !recalc()) { }
    }

    template<unsigned ndim>
    Face<ndim>::Face(const vertex_t *a,
                     const vertex_t *b,
                     const vertex_t *c,
                     bool delay_recalc) : tagable() {
      vertices.reserve(3);
      vertices.push_back(a);
      vertices.push_back(b);
      vertices.push_back(c);
      edges.resize(3, NULL);
      if (!delay_recalc && !recalc()) { }
    }

    template<unsigned ndim>
    Face<ndim>::Face(const vertex_t *a,
                     const vertex_t *b,
                     const vertex_t *c,
                     const vertex_t *d,
                     bool delay_recalc) : tagable() {
      vertices.reserve(4);
      vertices.push_back(a);
      vertices.push_back(b);
      vertices.push_back(c);
      vertices.push_back(d);
      edges.resize(4, NULL);
      if (!delay_recalc && !recalc()) { }
    }

    template<unsigned ndim>
    void Face<ndim>::invert() {
      size_t n_verts = vertices.size();
      std::reverse(vertices.begin(), vertices.end());

      if (project != NULL) {
        plane_eqn.negate();

        int da = carve::geom::largestAxis(plane_eqn.N);

        project = getProjector(plane_eqn.N.v[da] > 0, da);
        unproject = getUnprojector(plane_eqn.N.v[da] > 0, da);
      }

      std::reverse(edges.begin(), edges.end() - 1);
      for (size_t i = 0; i < n_verts; i++) {
        const vertex_t *v1 = vertices[i];
        const vertex_t *v2 = vertices[(i+1) % n_verts];
        CARVE_ASSERT((edges[i]->v1 == v1 && edges[i]->v2 == v2) || (edges[i]->v1 == v2 && edges[i]->v2 == v1));
      }
    }

    template<unsigned ndim>
    bool Face<ndim>::recalc() {
      aabb.fit(vertices.begin(), vertices.end(), vec_adapt_vertex_ptr());

      if (!carve::geom3d::fitPlane(vertices.begin(), vertices.end(), vec_adapt_vertex_ptr(), plane_eqn)) {
        return false;
      }

      int da = carve::geom::largestAxis(plane_eqn.N);
      project = getProjector(false, da);

      double A = carve::geom2d::signedArea(vertices, projector());
      if ((A < 0.0) ^ (plane_eqn.N.v[da] < 0.0)) {
        plane_eqn.negate();
      }

      project = getProjector(plane_eqn.N.v[da] > 0, da);
      unproject = getUnprojector(plane_eqn.N.v[da] > 0, da);

      return true;
    }

    template<unsigned ndim>
    Face<ndim> *Face<ndim>::init(const Face *base, const std::vector<const vertex_t *> &_vertices, bool flipped) {
      return init(base, _vertices.begin(), _vertices.end(), flipped);
    }

    template<unsigned ndim>
    bool Face<ndim>::containsPoint(const vector_t &p) const {
      if (!carve::math::ZERO(carve::geom::distance(plane_eqn, p))) return false;
      // return pointInPolySimple(vertices, projector(), (this->*project)(p));
      return carve::geom2d::pointInPoly(vertices, projector(), face::project(this, p)).iclass != POINT_OUT;
    }

    template<unsigned ndim>
    bool Face<ndim>::containsPointInProjection(const vector_t &p) const {
      return carve::geom2d::pointInPoly(vertices, projector(), face::project(this, p)).iclass != POINT_OUT;
    }

    template<unsigned ndim>
    bool Face<ndim>::simpleLineSegmentIntersection(const carve::geom::linesegment<ndim> &line,
                                                   vector_t &intersection) const {
      if (!line.OK()) return false;

      carve::geom3d::Vector p;
      IntersectionClass intersects = carve::geom3d::lineSegmentPlaneIntersection(plane_eqn,
                                                                  line,
                                                                  p);
      if (intersects == INTERSECT_NONE || intersects == INTERSECT_BAD) {
        return false;
      }

      carve::geom2d::P2 proj_p(face::project(this, p));
      if (carve::geom2d::pointInPolySimple(vertices, projector(), proj_p)) {
        intersection = p;
        return true;
      }
      return false;
    }

    // XXX: should try to return a pre-existing vertex in the case of a
    // line-vertex intersection.  as it stands, this code isn't used,
    // so... meh.
    template<unsigned ndim>
    IntersectionClass Face<ndim>::lineSegmentIntersection(const carve::geom::linesegment<ndim> &line,
                                                          vector_t &intersection) const {
      if (!line.OK()) return INTERSECT_NONE;

  
      carve::geom3d::Vector p;
      IntersectionClass intersects = carve::geom3d::lineSegmentPlaneIntersection(plane_eqn,
                                                                  line,
                                                                  p);
      if (intersects == INTERSECT_NONE || intersects == INTERSECT_BAD) {
        return intersects;
      }

      carve::geom2d::P2 proj_p(face::project(this, p));

      carve::geom2d::PolyInclusionInfo pi = carve::geom2d::pointInPoly(vertices, projector(), proj_p);
      switch (pi.iclass) {
      case POINT_VERTEX:
        intersection = p;
        return INTERSECT_VERTEX;

      case POINT_EDGE:
        intersection = p;
        return INTERSECT_EDGE;

      case POINT_IN:
        intersection = p;
        return INTERSECT_FACE;
      
      case POINT_OUT:
        return INTERSECT_NONE;

      default:
        break;
      }
      return INTERSECT_NONE;
    }


  }
}

// explicit instantiations.
template class carve::poly::Face<3>;