File: BinaryWindingNumberOperations.h

package info (click to toggle)
meshlab 2022.02%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 47,348 kB
  • sloc: cpp: 536,635; ansic: 27,783; sh: 539; makefile: 36
file content (167 lines) | stat: -rw-r--r-- 5,289 bytes parent folder | download | duplicates (4)
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
// This file is part of libigl, a simple c++ geometry processing library.
// 
// Copyright (C) 2015 Qingnan Zhou <qnzhou@gmail.com>
// 
// This Source Code Form is subject to the terms of the Mozilla Public License 
// v. 2.0. If a copy of the MPL was not distributed with this file, You can 
// obtain one at http://mozilla.org/MPL/2.0/.
//
#ifndef IGL_COPYLEFT_CGAL_BINARY_WINDING_NUMBER_OPERATIONS_H
#define IGL_COPYLEFT_CGAL_BINARY_WINDING_NUMBER_OPERATIONS_H

#include <stdexcept>
#include "../../igl_inline.h"
#include "../../MeshBooleanType.h"
#include <Eigen/Core>

// TODO: This is not written according to libigl style. These should be
// function handles.
//
// Why is this templated on DerivedW
//
// These are all generalized to n-ary operations
namespace igl
{
  namespace copyleft
  {
    namespace cgal
    {
      template <igl::MeshBooleanType Op>
      class BinaryWindingNumberOperations {
        public:
          template<typename DerivedW>
            typename DerivedW::Scalar operator()(
                const Eigen::PlainObjectBase<DerivedW>& /*win_nums*/) const {
              throw (std::runtime_error("not implemented!"));
            }
      };

      // A ∪ B ∪ ... ∪ Z
      template <>
      class BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_UNION> {
        public:
          template<typename DerivedW>
          typename DerivedW::Scalar operator()(
              const Eigen::PlainObjectBase<DerivedW>& win_nums) const 
          {
            for(int i = 0;i<win_nums.size();i++)
            {
              if(win_nums(i) > 0) return true;
            }
            return false;
          }
      };

      // A ∩ B ∩ ... ∩ Z
      template <>
      class BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_INTERSECT> {
        public:
          template<typename DerivedW>
          typename DerivedW::Scalar operator()(
              const Eigen::PlainObjectBase<DerivedW>& win_nums) const 
          {
            for(int i = 0;i<win_nums.size();i++)
            {
              if(win_nums(i)<=0) return false;
            }
            return true;
          }
      };

      // A \ B \ ... \ Z = A \ (B ∪ ... ∪ Z)
      template <>
      class BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_MINUS> {
        public:
          template<typename DerivedW>
          typename DerivedW::Scalar operator()(
              const Eigen::PlainObjectBase<DerivedW>& win_nums) const 
          {
            assert(win_nums.size()>1);
            // Union of objects 1 through n-1
            bool union_rest = false;
            for(int i = 1;i<win_nums.size();i++)
            {
              union_rest = union_rest || win_nums(i) > 0;
              if(union_rest) break;
            }
            // Must be in object 0 and not in union of objects 1 through n-1
            return win_nums(0) > 0 && !union_rest;
          }
      };

      // A ∆ B ∆ ... ∆ Z  (equivalent to set inside odd number of objects)
      template <>
      class BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_XOR> {
        public:
          template<typename DerivedW>
          typename DerivedW::Scalar operator()(
              const Eigen::PlainObjectBase<DerivedW>& win_nums) const 
          {
            // If inside an odd number of objects
            int count = 0;
            for(int i = 0;i<win_nums.size();i++)
            {
              if(win_nums(i) > 0) count++;
            }
            return count % 2 == 1;
          }
      };

      template <>
      class BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_RESOLVE> {
        public:
          template<typename DerivedW>
            typename DerivedW::Scalar operator()(
                const Eigen::PlainObjectBase<DerivedW>& /*win_nums*/) const {
              return true;
            }
      };

      typedef BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_UNION> BinaryUnion;
      typedef BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_INTERSECT> BinaryIntersect;
      typedef BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_MINUS> BinaryMinus;
      typedef BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_XOR> BinaryXor;
      typedef BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_RESOLVE> BinaryResolve;

      enum KeeperType {
        KEEP_INSIDE,
        KEEP_ALL
      };

      template<KeeperType T>
      class WindingNumberFilter {
        public:
          template<typename DerivedW>
            short operator()(
                const Eigen::PlainObjectBase<DerivedW>& /*win_nums*/) const {
              throw std::runtime_error("Not implemented");
            }
      };

      template<>
      class WindingNumberFilter<KEEP_INSIDE> {
        public:
          template<typename T>
          short operator()(T out_w, T in_w) const {
            if (in_w > 0 && out_w <= 0) return 1;
            else if (in_w <= 0 && out_w > 0) return -1;
            else return 0;
          }
      };

      template<>
      class WindingNumberFilter<KEEP_ALL> {
        public:
          template<typename T>
            short operator()(T /*out_w*/, T /*in_w*/) const {
              return 1;
            }
      };

      typedef WindingNumberFilter<KEEP_INSIDE> KeepInside;
      typedef WindingNumberFilter<KEEP_ALL> KeepAll;
    }
  }
}

#endif