Geogram Version 1.10.1
A programming library of geometric algorithms
Loading...
Searching...
No Matches
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
44#include <geogram/basic/vecg.h>
46
52namespace 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 rationalg<T> cartesian(coord_index_t i) const {
123 geo_debug_assert(i < 2);
124 return rationalg<T>(data()[i], data()[2]);
125 }
126
127 void optimize() {
131 }
132
133 T x;
134 T y;
135 T w;
136 };
137
138 /************************************************************************/
139
140 template <class T> inline vec2Hg<T> operator-(
141 const vec2Hg<T>& p1, const vec2Hg<T>& p2
142 ) {
143 if(p2.w == p1.w) {
144 return vec2Hg<T>(
145 p1.x-p2.x,
146 p1.y-p2.y,
147 p1.w
148 );
149 }
150 return vec2Hg<T>(
151 det2x2(p1.x,p1.w,p2.x,p2.w),
152 det2x2(p1.y,p1.w,p2.y,p2.w),
153 p1.w*p2.w
154 );
155 }
156
157 /************************************************************************/
158
164 template <class T> class vec2HgLexicoCompare {
165 public:
172 bool operator()(const vec2Hg<T>& v1, const vec2Hg<T>& v2) const {
173 Sign s = Numeric::ratio_compare(v2.x, v2.w, v1.x, v1.w);
174 if(s == POSITIVE) {
175 return true;
176 }
177 if(s == NEGATIVE) {
178 return false;
179 }
180 s = Numeric::ratio_compare(v2.y, v2.w, v1.y, v1.w);
181 return (s == POSITIVE);
182 }
183 };
184
185 /************************************************************************/
186
190 template <class T> class vec3Hg {
191 public:
193 typedef T value_type;
194
195 vec3Hg() = default;
196
197 vec3Hg(const T& x_in, const T& y_in, const T& z_in, const T& w_in) :
198 x(x_in),
199 y(y_in),
200 z(z_in),
201 w(w_in) {
202 }
203
204 vec3Hg(T&& x_in, T&& y_in, T&& z_in, T&& w_in) :
205 x(x_in),
206 y(y_in),
207 z(z_in),
208 w(w_in) {
209 }
210
211 vec3Hg(double x_in, double y_in, double z_in, double w_in) :
212 x(x_in),
213 y(y_in),
214 z(z_in),
215 w(w_in) {
216 }
217
218 vec3Hg(const vec3Hg& rhs) = default;
219
220 vec3Hg(vec3Hg&& rhs) = default;
221
222 template <class T2> explicit vec3Hg(const vecng<3,T2>& rhs) :
223 x(rhs.x),
224 y(rhs.y),
225 z(rhs.z),
226 w(1.0) {
227 }
228
229 template <class T2> explicit vec3Hg(const vec3Hg<T2>& rhs) :
230 x(rhs.x),
231 y(rhs.y),
232 z(rhs.z),
233 w(rhs.w) {
234 }
235
236 vec3Hg& operator=(const vec3Hg& rhs) = default;
237 vec3Hg& operator=(vec3Hg&& rhs) = default;
238
239 T* data() {
240 return &x;
241 }
242
243 const T* data() const {
244 return &x;
245 }
246
247 T& operator[](coord_index_t i) {
248 geo_debug_assert(i <= 3);
249 return data()[i];
250 }
251
252 const T& operator[](coord_index_t i) const {
253 geo_debug_assert(i <= 3);
254 return data()[i];
255 }
256
257 rationalg<T> cartesian(coord_index_t i) const {
258 geo_debug_assert(i < 3);
259 return rationalg<T>(data()[i], data()[3]);
260 }
261
262 void optimize() {
267 }
268
269 T x;
270 T y;
271 T z;
272 T w;
273 };
274
275 /************************************************************************/
276
277 template <class T> inline vec3Hg<T> operator-(
278 const vec3Hg<T>& p1, const vec3Hg<T>& p2
279 ) {
280 if(p1.w == p2.w) {
281 return vec3Hg<T>(
282 p1.x - p2.x,
283 p1.y - p2.y,
284 p1.z - p2.z,
285 p1.w
286 );
287 }
288 return vec3Hg<T>(
289 det2x2(p1.x,p1.w,p2.x,p2.w),
290 det2x2(p1.y,p1.w,p2.y,p2.w),
291 det2x2(p1.z,p1.w,p2.z,p2.w),
292 p1.w * p2.w
293 );
294 }
295
296 /************************************************************************/
297
303 template <class T> class vec3HgLexicoCompare {
304 public:
311 bool operator()(const vec3Hg<T>& v1, const vec3Hg<T>& v2) const {
312 Sign s = Numeric::ratio_compare(v2.x, v2.w, v1.x, v1.w);
313 if(s == POSITIVE) {
314 return true;
315 }
316 if(s == NEGATIVE) {
317 return false;
318 }
319
320 s = Numeric::ratio_compare(v2.y, v2.w, v1.y, v1.w);
321 if(s == POSITIVE) {
322 return true;
323 }
324 if(s == NEGATIVE) {
325 return false;
326 }
327
328 s = Numeric::ratio_compare(v2.z, v2.w, v1.z, v1.w);
329 return (s == POSITIVE);
330 }
331 };
332
333 /************************************************************************/
334
335 template <class T> inline vec2Hg<T> mix(
336 const rationalg<T>& t,
337 const vecng<2,double>& p1, const vecng<2,double>& p2
338 ) {
339 const T& st_d = t.denom();
340 const T& t_n = t.num();
341 T s_n = st_d - t_n;
342 return vec2Hg<T>(
343 s_n * T(p1.x) + t_n * T(p2.x),
344 s_n * T(p1.y) + t_n * T(p2.y),
345 st_d
346 );
347 }
348
349 template <class T> inline vec3Hg<T> mix(
350 const rationalg<T>& t,
351 const vecng<3,double>& p1, const vecng<3,double>& p2
352 ) {
353 const T& st_d = t.denom();
354 const T& t_n = t.num();
355 T s_n = st_d - t_n;
356 return vec3Hg<T>(
357 s_n * T(p1.x) + t_n * T(p2.x),
358 s_n * T(p1.y) + t_n * T(p2.y),
359 s_n * T(p1.z) + t_n * T(p2.z),
360 st_d
361 );
362 }
363
364
365 template <class T> inline vec2Hg<T> mix(
366 const rationalg<T>& t, const vec2Hg<T>& p1, const vec2Hg<T>& p2
367 ) {
368 if(p1.w == p2.w) {
369 T sn = t.denom() - t.num();
370 T tn = t.num();
371 return vec2Hg<T>(
372 sn * p1.x + tn * p2.x,
373 sn * p1.y + tn * p2.y,
374 t.denom() * p1.w
375 );
376 } else {
377 T sn = p2.w*(t.denom() - t.num());
378 T tn = p1.w*t.num();
379 return vec2Hg<T>(
380 sn * p1.x + tn * p2.x,
381 sn * p1.y + tn * p2.y,
382 t.denom() * p1.w * p2.w
383 );
384 }
385 }
386
387 template <class T> inline vec3Hg<T> mix(
388 const rationalg<T>& t, const vec3Hg<T>& p1, const vec3Hg<T>& p2
389 ) {
390 if(p1.w == p2.w) {
391 T sn = t.denom() - t.num();
392 T tn = t.num();
393 return vec3Hg<T>(
394 sn * p1.x + tn * p2.x,
395 sn * p1.y + tn * p2.y,
396 sn * p1.z + tn * p2.z,
397 t.denom() * p1.w
398 );
399 } else {
400 T sn = p2.w*(t.denom() - t.num());
401 T tn = p1.w*t.num();
402 return vec3Hg<T>(
403 sn * p1.x + tn * p2.x,
404 sn * p1.y + tn * p2.y,
405 sn * p1.z + tn * p2.z,
406 t.denom() * p1.w * p2.w
407 );
408 }
409 }
410
411
412 /************************************************************************/
413
414 namespace Numeric {
415
416 template<class T>
417 inline void optimize_number_representation(vec2Hg<T>& v) {
418 v.optimize();
419 }
420
421 template<class T>
422 inline void optimize_number_representation(vec3Hg<T>& v) {
423 v.optimize();
424 }
425
426 }
427
428 /************************************************************************/
429}
430
431
432#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:164
bool operator()(const vec2Hg< T > &v1, const vec2Hg< T > &v2) const
Compares two vec2Hg.
Definition vechg.h:172
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:303
bool operator()(const vec3Hg< T > &v1, const vec3Hg< T > &v2) const
Compares two vec3Hg.
Definition vechg.h:311
3d vector with homogeneous coordinates
Definition vechg.h:190
T value_type
The type of the vector coordinates.
Definition vechg.h:193
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:289
void optimize_number_representation(T &x)
place holder for optimizing internal number representation
Definition numeric.h:278
Global Vorpaline namespace.
Definition basic.h:55
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:72
@ NEGATIVE
Definition numeric.h:74
@ POSITIVE
Definition numeric.h:78
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:374
Generic implementation of rational type.
Generic implementation of geometric vectors.