CGALライブラリのブーリアン演算を使用して、直方体と円柱の表面メッシュに対する差演算を行います。
プログラムでは、直方体の表面メッシュ(box.off)と円柱の表面メッシュ(cyl.off)を読み込み、Nef多面体のデータ構造CGAL::Nef_polyhedron_3<Traits>に変換します。CGALではNef多面体に対してブーリアン演算が定義されていますので、これを使用してブーリアン差を計算します。結果はOFFファイル形式でdiff.offというファイル名で出力しています。
polygon_difference.cxx †
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
|
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
!
|
-
|
|
!
|
|
!
| #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <CGAL/Nef_polyhedron_3.h>
#include <fstream>
typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron;
typedef CGAL::Nef_polyhedron_3<Kernel> Nef_polyhedron;
int main() {
Polyhedron P1;
std::ifstream in1("box.off");
in1 >> P1;
Polyhedron P2;
std::ifstream in2("cyl.off");
in2 >> P2;
Nef_polyhedron N1(P1);
Nef_polyhedron N2(P2);
N1 -= N2;
Polyhedron P;
if(N1.is_simple()) {
N1.convert_to_Polyhedron(P);
std::ofstream out("diff.off");
out << P;
}
else
{
std::cerr << "N1 is not a 2-manifold." << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|