Graphite  Version 3
An experimental 3D geometry processing program
vechg.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2000-2022 Inria
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * * Neither the name of the ALICE Project-Team nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Contact: Bruno Levy
30  *
31  * https://www.inria.fr/fr/bruno-levy
32  *
33  * Inria,
34  * Domaine de Voluceau,
35  * 78150 Le Chesnay - Rocquencourt
36  * FRANCE
37  *
38  */
39 
40 #ifndef GEOGRAM_BASIC_VECHG
41 #define GEOGRAM_BASIC_VECHG
42 
43 #include <geogram/basic/common.h>
44 #include <geogram/basic/vecg.h>
46 
52 namespace GEO {
53 
54 
55  /************************************************************************/
56 
60  template <class T> class vec2Hg {
61  public:
63  typedef T value_type;
64 
65  vec2Hg() = default;
66 
67  vec2Hg(const T& x_in, const T& y_in, const T& w_in) :
68  x(x_in),
69  y(y_in),
70  w(w_in) {
71  }
72 
73  vec2Hg(double x_in, double y_in, double w_in) :
74  x(x_in),
75  y(y_in),
76  w(w_in) {
77  }
78 
79  vec2Hg(T&& x_in, T&& y_in, T&& w_in) :
80  x(x_in),
81  y(y_in),
82  w(w_in) {
83  }
84 
85  vec2Hg(const vec2Hg& rhs) = default;
86 
87  vec2Hg(vec2Hg&& rhs) = default;
88 
89  template <class T2> explicit vec2Hg(const vecng<2,T2>& rhs) :
90  x(rhs.x),
91  y(rhs.y),
92  w(1.0) {
93  }
94 
95  template <class T2> explicit vec2Hg(const vec2Hg<T2>& rhs) :
96  x(rhs.x),
97  y(rhs.y),
98  w(rhs.w) {
99  }
100 
101  vec2Hg& operator=(const vec2Hg& rhs) = default;
102  vec2Hg& operator=(vec2Hg&& rhs) = default;
103 
104  T* data() {
105  return &x;
106  }
107 
108  const T* data() const {
109  return &x;
110  }
111 
112  T& operator[](coord_index_t i) {
113  geo_debug_assert(i < 2);
114  return data()[i];
115  }
116 
117  const T& operator[](coord_index_t i) const {
118  geo_debug_assert(i < 2);
119  return data()[i];
120  }
121 
122  void optimize() {
126  }
127 
128  T x;
129  T y;
130  T w;
131  };
132 
133  /************************************************************************/
134 
135  template <class T> inline vec2Hg<T> operator-(
136  const vec2Hg<T>& p1, const vec2Hg<T>& p2
137  ) {
138  if(p2.w == p1.w) {
139  return vec2Hg<T>(
140  p1.x-p2.x,
141  p1.y-p2.y,
142  p1.w
143  );
144  }
145  return vec2Hg<T>(
146  det2x2(p1.x,p1.w,p2.x,p2.w),
147  det2x2(p1.y,p1.w,p2.y,p2.w),
148  p1.w*p2.w
149  );
150  }
151 
152  /************************************************************************/
153 
159  template <class T> class vec2HgLexicoCompare {
160  public:
167  bool operator()(const vec2Hg<T>& v1, const vec2Hg<T>& v2) const {
168  Sign s = Numeric::ratio_compare(v2.x, v2.w, v1.x, v1.w);
169  if(s == POSITIVE) {
170  return true;
171  }
172  if(s == NEGATIVE) {
173  return false;
174  }
175  s = Numeric::ratio_compare(v2.y, v2.w, v1.y, v1.w);
176  return (s == POSITIVE);
177  }
178  };
179 
180  /************************************************************************/
181 
185  template <class T> class vec3Hg {
186  public:
188  typedef T value_type;
189 
190  vec3Hg() = default;
191 
192  vec3Hg(const T& x_in, const T& y_in, const T& z_in, const T& w_in) :
193  x(x_in),
194  y(y_in),
195  z(z_in),
196  w(w_in) {
197  }
198 
199  vec3Hg(T&& x_in, T&& y_in, T&& z_in, T&& w_in) :
200  x(x_in),
201  y(y_in),
202  z(z_in),
203  w(w_in) {
204  }
205 
206  vec3Hg(double x_in, double y_in, double z_in, double w_in) :
207  x(x_in),
208  y(y_in),
209  z(z_in),
210  w(w_in) {
211  }
212 
213  vec3Hg(const vec3Hg& rhs) = default;
214 
215  vec3Hg(vec3Hg&& rhs) = default;
216 
217  template <class T2> explicit vec3Hg(const vecng<3,T2>& rhs) :
218  x(rhs.x),
219  y(rhs.y),
220  z(rhs.z),
221  w(1.0) {
222  }
223 
224  template <class T2> explicit vec3Hg(const vec3Hg<T2>& rhs) :
225  x(rhs.x),
226  y(rhs.y),
227  z(rhs.z),
228  w(rhs.w) {
229  }
230 
231  vec3Hg& operator=(const vec3Hg& rhs) = default;
232  vec3Hg& operator=(vec3Hg&& rhs) = default;
233 
234  T* data() {
235  return &x;
236  }
237 
238  const T* data() const {
239  return &x;
240  }
241 
242  T& operator[](coord_index_t i) {
243  geo_debug_assert(i < 3);
244  return data()[i];
245  }
246 
247  const T& operator[](coord_index_t i) const {
248  geo_debug_assert(i < 3);
249  return data()[i];
250  }
251 
252  void optimize() {
257  }
258 
259  T x;
260  T y;
261  T z;
262  T w;
263  };
264 
265  /************************************************************************/
266 
267  template <class T> inline vec3Hg<T> operator-(
268  const vec3Hg<T>& p1, const vec3Hg<T>& p2
269  ) {
270  if(p1.w == p2.w) {
271  return vec3Hg<T>(
272  p1.x - p2.x,
273  p1.y - p2.y,
274  p1.z - p2.z,
275  p1.w
276  );
277  }
278  return vec3Hg<T>(
279  det2x2(p1.x,p1.w,p2.x,p2.w),
280  det2x2(p1.y,p1.w,p2.y,p2.w),
281  det2x2(p1.z,p1.w,p2.z,p2.w),
282  p1.w * p2.w
283  );
284  }
285 
286  /************************************************************************/
287 
293  template <class T> class vec3HgLexicoCompare {
294  public:
301  bool operator()(const vec3Hg<T>& v1, const vec3Hg<T>& v2) const {
302  Sign s = Numeric::ratio_compare(v2.x, v2.w, v1.x, v1.w);
303  if(s == POSITIVE) {
304  return true;
305  }
306  if(s == NEGATIVE) {
307  return false;
308  }
309 
310  s = Numeric::ratio_compare(v2.y, v2.w, v1.y, v1.w);
311  if(s == POSITIVE) {
312  return true;
313  }
314  if(s == NEGATIVE) {
315  return false;
316  }
317 
318  s = Numeric::ratio_compare(v2.z, v2.w, v1.z, v1.w);
319  return (s == POSITIVE);
320  }
321  };
322 
323  /************************************************************************/
324 
325  template <class T> inline vec2Hg<T> mix(
326  const rationalg<T>& t,
327  const vecng<2,double>& p1, const vecng<2,double>& p2
328  ) {
329  const T& st_d = t.denom();
330  const T& t_n = t.num();
331  T s_n = st_d - t_n;
332  return vec2Hg<T>(
333  s_n * T(p1.x) + t_n * T(p2.x),
334  s_n * T(p1.y) + t_n * T(p2.y),
335  st_d
336  );
337  }
338 
339  template <class T> inline vec3Hg<T> mix(
340  const rationalg<T>& t,
341  const vecng<3,double>& p1, const vecng<3,double>& p2
342  ) {
343  const T& st_d = t.denom();
344  const T& t_n = t.num();
345  T s_n = st_d - t_n;
346  return vec3Hg<T>(
347  s_n * T(p1.x) + t_n * T(p2.x),
348  s_n * T(p1.y) + t_n * T(p2.y),
349  s_n * T(p1.z) + t_n * T(p2.z),
350  st_d
351  );
352  }
353 
354 
355  template <class T> inline vec2Hg<T> mix(
356  const rationalg<T>& t, const vec2Hg<T>& p1, const vec2Hg<T>& p2
357  ) {
358  if(p1.w == p2.w) {
359  T sn = t.denom() - t.num();
360  T tn = t.num();
361  return vec2Hg<T>(
362  sn * p1.x + tn * p2.x,
363  sn * p1.y + tn * p2.y,
364  t.denom() * p1.w
365  );
366  } else {
367  T sn = p2.w*(t.denom() - t.num());
368  T tn = p1.w*t.num();
369  return vec2Hg<T>(
370  sn * p1.x + tn * p2.x,
371  sn * p1.y + tn * p2.y,
372  t.denom() * p1.w * p2.w
373  );
374  }
375  }
376 
377  template <class T> inline vec3Hg<T> mix(
378  const rationalg<T>& t, const vec3Hg<T>& p1, const vec3Hg<T>& p2
379  ) {
380  if(p1.w == p2.w) {
381  T sn = t.denom() - t.num();
382  T tn = t.num();
383  return vec3Hg<T>(
384  sn * p1.x + tn * p2.x,
385  sn * p1.y + tn * p2.y,
386  sn * p1.z + tn * p2.z,
387  t.denom() * p1.w
388  );
389  } else {
390  T sn = p2.w*(t.denom() - t.num());
391  T tn = p1.w*t.num();
392  return vec3Hg<T>(
393  sn * p1.x + tn * p2.x,
394  sn * p1.y + tn * p2.y,
395  sn * p1.z + tn * p2.z,
396  t.denom() * p1.w * p2.w
397  );
398  }
399  }
400 
401 
402  /************************************************************************/
403 
404  namespace Numeric {
405 
406  template<class T>
407  inline void optimize_number_representation(vec2Hg<T>& v) {
408  v.optimize();
409  }
410 
411  template<class T>
412  inline void optimize_number_representation(vec3Hg<T>& v) {
413  v.optimize();
414  }
415 
416  }
417 
418  /************************************************************************/
419 }
420 
421 
422 #endif
#define geo_debug_assert(x)
Verifies that a condition is met.
Definition: assert.h:196
Comparator class for vec2Hg \detail Used to create maps indexed by vec2Hg or SOS symbolic perturbatio...
Definition: vechg.h:159
bool operator()(const vec2Hg< T > &v1, const vec2Hg< T > &v2) const
Compares two vec2Hg.
Definition: vechg.h:167
2d vector with homogeneous coordinates
Definition: vechg.h:60
T value_type
The type of the vector coordinates.
Definition: vechg.h:63
Comparator class for vec3Hg \detail Used to create maps indexed by vec3Hg or SOS symbolic perturbatio...
Definition: vechg.h:293
bool operator()(const vec3Hg< T > &v1, const vec3Hg< T > &v2) const
Compares two vec3Hg.
Definition: vechg.h:301
3d vector with homogeneous coordinates
Definition: vechg.h:185
T value_type
The type of the vector coordinates.
Definition: vechg.h:188
Common include file, providing basic definitions. Should be included before anything else by all head...
Sign ratio_compare(const T &a_num, const T &a_denom, const T &b_num, const T &b_denom)
Compares two rational numbers given as separate numerators and denominators.
Definition: numeric.h:278
void optimize_number_representation(T &x)
place holder for optimizing internal number representation
Definition: numeric.h:267
Global Vorpaline namespace.
Quaternion operator-(const Quaternion &a, const Quaternion &b)
Computes the difference between two Quaternion.
Definition: quaternion.h:252
Sign
Integer constants that represent the sign of a value.
Definition: numeric.h:68
@ NEGATIVE
Definition: numeric.h:70
@ POSITIVE
Definition: numeric.h:74
T det2x2(const T &a11, const T &a12, const T &a21, const T &a22)
Computes a two-by-two determinant.
Definition: determinant.h:58
geo_coord_index_t coord_index_t
The type for storing coordinate indices, and iterating on the coordinates of a point.
Definition: numeric.h:363
Generic implementation of rational type.
Generic implementation of geometric vectors.