IsoEx
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
csg.hh
1 /*===========================================================================*\
2  * *
3  * IsoEx *
4  * Copyright (C) 2002 by Computer Graphics Group, RWTH Aachen *
5  * www.rwth-graphics.de *
6  * *
7  *---------------------------------------------------------------------------*
8  * *
9  * License *
10  * *
11  * This library is free software; you can redistribute it and/or modify it *
12  * under the terms of the GNU Library General Public License as published *
13  * by the Free Software Foundation, version 2. *
14  * *
15  * This library is distributed in the hope that it will be useful, but *
16  * WITHOUT ANY WARRANTY; without even the implied warranty of *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
18  * Library General Public License for more details. *
19  * *
20  * You should have received a copy of the GNU Library General Public *
21  * License along with this library; if not, write to the Free Software *
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
23  * *
24 \*===========================================================================*/
25 
26 //=============================================================================
27 //
28 // CLASS CSGUnion, CSGIntersection, CSGDifference
29 //
30 //=============================================================================
31 
32 
33 #ifndef ISOEX_CSG_HH
34 #define ISOEX_CSG_HH
35 
36 
37 //== INCLUDES =================================================================
38 
39 #include "Implicit.hh"
40 
41 //== NAMESPACES ===============================================================
42 
43 namespace IsoEx {
44 namespace CSG {
45 
46 //== CLASS DEFINITION =========================================================
47 
48 
54 template< class Vec3>
55 class Union : public Implicit<Vec3>
56 {
57 public:
58 
59  typedef typename Vec3::value_type real;
60 
62  Union(const Implicit<Vec3>& _implicit1, const Implicit<Vec3>& _implicit2)
63  : implicit1_(_implicit1), implicit2_(_implicit2)
64  {}
65 
66 
68 
69 
70  bool is_inside(const Vec3& _p) const {
71  return implicit1_.is_inside(_p) || implicit2_.is_inside(_p);
72  }
73 
74  real scalar_distance(const Vec3& _p) const {
75  return std::min(implicit1_.scalar_distance(_p),
76  implicit2_.scalar_distance(_p));
77  }
78 
79  bool directed_distance(const Vec3& _p0,
80  const Vec3& _p1,
81  Vec3& _point,
82  Vec3& _normal,
83  real& _distance) const
84  {
85  bool ok1, ok2;
86  Vec3 p1, n1, p2, n2;
87  real d1, d2;
88 
89  ok1 = implicit1_.directed_distance(_p0, _p1, p1, n1, d1);
90  ok2 = implicit2_.directed_distance(_p0, _p1, p2, n2, d2);
91 
92  if (ok1 && ok2) { if (d1 < d2) ok2 = false; else ok1 = false; }
93 
94  if (ok1) { _point = p1; _normal = n1; _distance = d1; return true; }
95  else if (ok2) { _point = p2; _normal = n2; _distance = d2; return true; }
96 
97  return false;
98  }
99 
101 
102 private:
103 
104  const Implicit<Vec3>& implicit1_;
105  const Implicit<Vec3>& implicit2_;
106 };
107 
108 
109 
110 //=============================================================================
111 
112 
113 
119 template< class Vec3>
120 class Intersection : public Implicit<Vec3>
121 {
122 public:
123 
124  typedef typename Vec3::value_type real;
125 
127  Intersection(const Implicit<Vec3>& _implicit1, const Implicit<Vec3>& _implicit2)
128  : implicit1_(_implicit1), implicit2_(_implicit2)
129  {}
130 
131 
133 
134 
135  bool is_inside(const Vec3& _p) const {
136  return (implicit1_.is_inside(_p) && implicit2_.is_inside(_p));
137  }
138 
139  real scalar_distance(const Vec3& _p) const {
140  return std::max(implicit1_.scalar_distance(_p),
141  implicit2_.scalar_distance(_p));
142  }
143 
144  bool directed_distance(const Vec3& _p0,
145  const Vec3& _p1,
146  Vec3& _point,
147  Vec3& _normal,
148  real& _distance) const
149  {
150  bool ok1, ok2;
151  Vec3 p1, n1, p2, n2;
152  real d1, d2;
153 
154  ok1 = implicit1_.directed_distance(_p0, _p1, p1, n1, d1);
155  ok2 = implicit2_.directed_distance(_p0, _p1, p2, n2, d2);
156 
157  if (ok1 && ok2) { if (d1 > d2) ok2 = false; else ok1 = false; }
158 
159  if (ok1) { _point = p1; _normal = n1; _distance = d1; return true; }
160  else if (ok2) { _point = p2; _normal = n2; _distance = d2; return true; }
161 
162  return false;
163  }
164 
166 
167 
168 private:
169 
170  const Implicit<Vec3>& implicit1_;
171  const Implicit<Vec3>& implicit2_;
172 };
173 
174 
175 
176 //=============================================================================
177 
178 
179 
185 template< class Vec3>
186 class Difference : public Implicit<Vec3>
187 {
188 public:
189 
190  typedef typename Vec3::value_type real;
191 
195  Difference(const Implicit<Vec3>& _implicit1, const Implicit<Vec3>& _implicit2)
196  : implicit1_(_implicit1), implicit2_(_implicit2)
197  {}
198 
199 
201 
202 
203  bool is_inside(const Vec3& _p) const {
204  return implicit1_.is_inside(_p) && !implicit2_.is_inside(_p);
205  }
206 
207  real scalar_distance(const Vec3& _p) const {
208  return std::max(implicit1_.scalar_distance(_p),
209  -implicit2_.scalar_distance(_p));
210  }
211 
212  bool directed_distance(const Vec3& _p0,
213  const Vec3& _p1,
214  Vec3& _point,
215  Vec3& _normal,
216  real& _distance) const
217  {
218  bool ok1, ok2;
219  Vec3 p1, n1, p2, n2;
220  real d1, d2;
221 
222  ok1 = implicit1_.directed_distance(_p0, _p1, p1, n1, d1);
223  ok2 = implicit2_.directed_distance(_p0, _p1, p2, n2, d2);
224  d2 = -d2;
225 
226  if (ok1 && ok2) { if (d1 > d2) ok2 = false; else ok1 = false; }
227 
228  if (ok1) { _point = p1; _normal = n1; _distance = d1; return true; }
229  else if (ok2) { _point = p2; _normal = n2; _distance = d2; return true; }
230 
231  return false;
232  }
233 
235 
236 private:
237 
238  const Implicit<Vec3>& implicit1_;
239  const Implicit<Vec3>& implicit2_;
240 };
241 
242 
243 
244 //=============================================================================
245 } // namespace CSG
246 } // namespace IsoEx
247 //=============================================================================
248 #endif // ISOEX_CSG_HH defined
249 //=============================================================================
250 
Difference(const Implicit< Vec3 > &_implicit1, const Implicit< Vec3 > &_implicit2)
Definition: csg.hh:195
Definition: csg.hh:55
bool is_inside(const Vec3 &_p) const
Definition: csg.hh:135
real scalar_distance(const Vec3 &_p) const
Definition: csg.hh:139
bool directed_distance(const Vec3 &_p0, const Vec3 &_p1, Vec3 &_point, Vec3 &_normal, real &_distance) const
Definition: csg.hh:79
bool directed_distance(const Vec3 &_p0, const Vec3 &_p1, Vec3 &_point, Vec3 &_normal, real &_distance) const
Definition: csg.hh:212
bool is_inside(const Vec3 &_p) const
Definition: csg.hh:203
Union(const Implicit< Vec3 > &_implicit1, const Implicit< Vec3 > &_implicit2)
Constructor: given the two implicit objects to unite.
Definition: csg.hh:62
Intersection(const Implicit< Vec3 > &_implicit1, const Implicit< Vec3 > &_implicit2)
Constructor: given the two implicit objects to intersect.
Definition: csg.hh:127
Definition: csg.hh:120
real scalar_distance(const Vec3 &_p) const
Definition: csg.hh:74
Definition: csg.hh:186
Definition: Implicit.hh:57
A type for volume images, or 3D textures.
real scalar_distance(const Vec3 &_p) const
Definition: csg.hh:207
bool directed_distance(const Vec3 &_p0, const Vec3 &_p1, Vec3 &_point, Vec3 &_normal, real &_distance) const
Definition: csg.hh:144
bool is_inside(const Vec3 &_p) const
Definition: csg.hh:70