Nebula
Loading...
Searching...
No Matches
point.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
15//------------------------------------------------------------------------------
16#include "vector.h"
17#include "vec4.h"
18namespace Math
19{
20
22{
24 point();
26 point(scalar x, scalar y, scalar z);
28 point(scalar v);
30 point(const point& rhs);
32 point(const vec3& rhs);
34 point(const vec4& rhs);
36 point(const __m128& rhs);
37
39 void load(const scalar* ptr);
41 void loadu(const scalar* ptr);
43 void store(scalar* ptr) const;
45 void storeu(scalar* ptr) const;
47 void store3(scalar* ptr) const;
49 void storeu3(scalar* ptr) const;
50
52 void operator=(const point& rhs);
54 void operator=(const __m128& rhs);
56 void operator+=(const vector& rhs);
58 void operator-=(const vector& rhs);
60 bool operator==(const point& rhs) const;
62 bool operator!=(const point& rhs) const;
64 float operator[](int index) const;
66 float& operator[](int index);
68 operator vec4() const;
69
71 void set(scalar x, scalar y, scalar z);
72
73 union
74 {
75 __m128 vec;
76 struct
77 {
78 float x, y, z;
79 };
80 };
81};
82
83//------------------------------------------------------------------------------
86__forceinline
88{
89 this->vec = _mm_setr_ps(0, 0, 0, 1);
90}
91
92//------------------------------------------------------------------------------
95__forceinline
97{
98 this->vec = _mm_setr_ps(x, y, z, 1);
99}
100
101//------------------------------------------------------------------------------
104__forceinline
106{
107 this->vec = _mm_setr_ps(v, v, v, 1);
108}
109
110//------------------------------------------------------------------------------
113__forceinline
115{
116 this->vec = rhs.vec;
117}
118
119//------------------------------------------------------------------------------
122__forceinline
124{
125 // mask out xyz and set w to 1
126#if NEBULA_DEBUG
127 n_assert(rhs.__w == 0.0f || rhs.__w == 1.0f);
128#endif
129 this->vec = _mm_or_ps(rhs.vec, _id_w);
130}
131
132//------------------------------------------------------------------------------
135__forceinline
137{
138 // mask out xyz and set w to 1
139 this->vec = _mm_or_ps(_mm_and_ps(rhs.vec, _mask_xyz), _id_w);
140}
141
142//------------------------------------------------------------------------------
145__forceinline
146point::point(const __m128& rhs)
147{
148 this->vec = _mm_insert_ps(rhs, _id_w, 0b11110000);
149}
150
151//------------------------------------------------------------------------------
154__forceinline void
156{
157 this->vec = rhs.vec;
158}
159
160//------------------------------------------------------------------------------
163__forceinline void
164point::operator=(const __m128& rhs)
165{
166 this->vec = _mm_insert_ps(rhs, _id_w, 0b11110000);
167}
168
169//------------------------------------------------------------------------------
172__forceinline void
174{
175 this->vec = _mm_add_ps(this->vec, rhs.vec);
176}
177
178//------------------------------------------------------------------------------
181__forceinline void
183{
184 this->vec = _mm_sub_ps(this->vec, rhs.vec);
185}
186
187//------------------------------------------------------------------------------
191__forceinline void
193{
194 this->vec = _mm_load_ps(ptr);
195}
196
197//------------------------------------------------------------------------------
201__forceinline void
203{
204 this->vec = _mm_loadu_ps(ptr);
205}
206
207//------------------------------------------------------------------------------
211__forceinline void
213{
214 _mm_store_ps(ptr, this->vec);
215}
216
217//------------------------------------------------------------------------------
221__forceinline void
223{
224 _mm_storeu_ps(ptr, this->vec);
225}
226
227//------------------------------------------------------------------------------
231__forceinline void
233{
234 __m128 v = _mm_permute_ps(this->vec, _MM_SHUFFLE(2, 2, 2, 2));
235 _mm_storel_epi64(reinterpret_cast<__m128i*>(ptr), _mm_castps_si128(this->vec));
236 _mm_store_ss(&ptr[2], v);
237}
238
239//------------------------------------------------------------------------------
243__forceinline void
245{
246 __m128 t1 = _mm_permute_ps(this->vec, _MM_SHUFFLE(1, 1, 1, 1));
247 __m128 t2 = _mm_permute_ps(this->vec, _MM_SHUFFLE(2, 2, 2, 2));
248 _mm_store_ss(&ptr[0], this->vec);
249 _mm_store_ss(&ptr[1], t1);
250 _mm_store_ss(&ptr[2], t2);
251}
252//------------------------------------------------------------------------------
255__forceinline point
256operator+(const point& lhs, const vector& rhs)
257{
258 return _mm_add_ps(lhs.vec, rhs.vec);
259}
260
261//------------------------------------------------------------------------------
264__forceinline point
265operator-(const point& lhs, const vector& rhs)
266{
267 return _mm_sub_ps(lhs.vec, rhs.vec);
268}
269
270//------------------------------------------------------------------------------
273__forceinline vector
274operator-(const point& lhs, const point& rhs)
275{
276 return _mm_sub_ps(lhs.vec, rhs.vec);
277}
278
279//------------------------------------------------------------------------------
282__forceinline bool
283point::operator==(const point& rhs) const
284{
285 __m128 vTemp = _mm_cmpeq_ps(this->vec, rhs.vec);
286 return ((_mm_movemask_ps(vTemp) == 0x0f) != 0);
287}
288
289//------------------------------------------------------------------------------
292__forceinline bool
293point::operator!=(const point& rhs) const
294{
295 __m128 vTemp = _mm_cmpeq_ps(this->vec, rhs.vec);
296 return ((_mm_movemask_ps(vTemp) == 0x0f) == 0);
297}
298
299//------------------------------------------------------------------------------
302__forceinline float
303point::operator[](int index) const
304{
305 n_assert(index >= 0 && index < 4);
306 return *((&this->x) + index);
307}
308
309//------------------------------------------------------------------------------
312__forceinline float&
314{
315 n_assert(index >= 0 && index < 4);
316 return *((&this->x) + index);
317}
318
319//------------------------------------------------------------------------------
322__forceinline
323point::operator vec4() const
324{
325 return vec4(this->vec);
326}
327
328//------------------------------------------------------------------------------
331__forceinline void
333{
334 this->vec = _mm_setr_ps(x, y, z, 1);
335}
336
337//------------------------------------------------------------------------------
340__forceinline scalar
341dot(const point& p0, const point& p1)
342{
343 return _mm_cvtss_f32(_mm_dp_ps(p0.vec, p1.vec, 0x71));
344}
345
346//------------------------------------------------------------------------------
349__forceinline scalar
350dot(const vector& v, const point& p)
351{
352 return _mm_cvtss_f32(_mm_dp_ps(v.vec, p.vec, 0x71));
353}
354
355//------------------------------------------------------------------------------
358__forceinline scalar
359dot(const point& p, const vector& v)
360{
361 return _mm_cvtss_f32(_mm_dp_ps(p.vec, v.vec, 0x71));
362}
363
364//------------------------------------------------------------------------------
367__forceinline point
368maximize(const point& v0, const point& v1)
369{
370 return _mm_max_ps(v0.vec, v1.vec);
371}
372
373//------------------------------------------------------------------------------
376__forceinline point
377minimize(const point& v0, const point& v1)
378{
379 return _mm_min_ps(v0.vec, v1.vec);
380}
381
382//------------------------------------------------------------------------------
385__forceinline bool
386less_any(const point& v0, const point& v1)
387{
388 __m128 vTemp = _mm_cmpge_ps(v0.vec, v1.vec);
389 int res = _mm_movemask_ps(vTemp) & 7;
390 return res != 7;
391}
392
393//------------------------------------------------------------------------------
396__forceinline bool
397less_all(const point& v0, const point& v1)
398{
399 __m128 vTemp = _mm_cmpge_ps(v0.vec, v1.vec);
400 int res = _mm_movemask_ps(vTemp) & 7;
401 return res == 0;
402}
403
404//------------------------------------------------------------------------------
407__forceinline bool
408lessequal_any(const point& v0, const point& v1)
409{
410 __m128 vTemp = _mm_cmpgt_ps(v0.vec, v1.vec);
411 int res = _mm_movemask_ps(vTemp) & 7;
412 return res != 0x7;
413}
414
415//------------------------------------------------------------------------------
418__forceinline bool
419lessequal_all(const point& v0, const point& v1)
420{
421 __m128 vTemp = _mm_cmpgt_ps(v0.vec, v1.vec);
422 int res = _mm_movemask_ps(vTemp) & 7;
423 return res == 0;
424}
425
426//------------------------------------------------------------------------------
429__forceinline bool
430greater_any(const point& v0, const point& v1)
431{
432 __m128 vTemp = _mm_cmpgt_ps(v0.vec, v1.vec);
433 int res = _mm_movemask_ps(vTemp) & 7;
434 return res != 0;
435}
436
437//------------------------------------------------------------------------------
440__forceinline bool
441greater_all(const point& v0, const point& v1)
442{
443 __m128 vTemp = _mm_cmpgt_ps(v0.vec, v1.vec);
444 int res = _mm_movemask_ps(vTemp) & 7;
445 return res == 0x7;
446}
447
448//------------------------------------------------------------------------------
451__forceinline bool
452greaterequal_any(const point& v0, const point& v1)
453{
454 __m128 vTemp = _mm_cmpge_ps(v0.vec, v1.vec);
455 int res = _mm_movemask_ps(vTemp) & 7;
456 return res != 0;
457}
458
459//------------------------------------------------------------------------------
462__forceinline bool
463greaterequal_all(const point& v0, const point& v1)
464{
465 __m128 vTemp = _mm_cmpge_ps(v0.vec, v1.vec);
466 int res = _mm_movemask_ps(vTemp) & 7;
467 return res == 0x7;
468}
469
470//------------------------------------------------------------------------------
473__forceinline bool
474equal_any(const point& v0, const point& v1)
475{
476 __m128 vTemp = _mm_cmpeq_ps(v0.vec, v1.vec);
477 int res = _mm_movemask_ps(vTemp) & 7;
478 return res != 0;
479}
480
481//------------------------------------------------------------------------------
484__forceinline bool
485nearequal(const point& v0, const point& v1, float epsilon)
486{
487 __m128 eps = _mm_set1_ps(epsilon);
488 __m128 delta = _mm_sub_ps(v0.vec, v1.vec);
489 __m128 temp = _mm_setzero_ps();
490 temp = _mm_sub_ps(temp, delta);
491 temp = _mm_max_ps(temp, delta);
492 temp = _mm_cmple_ps(temp, eps);
493 temp = _mm_and_ps(temp, _mask_xyz);
494 return (_mm_movemask_ps(temp) == 0x7) != 0;
495}
496
497//------------------------------------------------------------------------------
500__forceinline point
501less(const point& v0, const point& v1)
502{
503 return _mm_min_ps(_mm_cmplt_ps(v0.vec, v1.vec), _plus1);
504}
505
506//------------------------------------------------------------------------------
509__forceinline point
510greater(const point& v0, const point& v1)
511{
512 return _mm_min_ps(_mm_cmpgt_ps(v0.vec, v1.vec), _plus1);
513}
514
515//------------------------------------------------------------------------------
518__forceinline point
519equal(const point& v0, const point& v1)
520{
521 return _mm_min_ps(_mm_cmpeq_ps(v0.vec, v1.vec), _plus1);
522}
523
524//------------------------------------------------------------------------------
527__forceinline vec3
528xyz(const point& v)
529{
530 vec3 res;
531 res.vec = _mm_and_ps(v.vec, _mask_xyz);
532 return res;
533}
534
535} // namespace Math
#define n_assert(exp)
Definition debug.h:50
Different curves.
Definition angularpfeedbackloop.h:17
__forceinline point less(const point &v0, const point &v1)
Definition point.h:501
__forceinline point maximize(const point &v0, const point &v1)
Definition point.h:368
__forceinline bool equal_any(const point &v0, const point &v1)
Definition point.h:474
__forceinline bool greaterequal_any(const point &v0, const point &v1)
Definition point.h:452
__forceinline point equal(const point &v0, const point &v1)
Definition point.h:519
__forceinline scalar dot(const plane &p, const vec4 &v1)
Definition plane.h:246
__forceinline bool greaterequal_all(const point &v0, const point &v1)
Definition point.h:463
__forceinline bool greater_any(const point &v0, const point &v1)
Definition point.h:430
half operator-(half one, half two)
Definition half.h:114
__forceinline bool less_any(const point &v0, const point &v1)
Definition point.h:386
static const __m128 _id_w
Definition vec3.h:32
__forceinline bool less_all(const point &v0, const point &v1)
Definition point.h:397
__forceinline bool lessequal_all(const point &v0, const point &v1)
Definition point.h:419
__forceinline point minimize(const point &v0, const point &v1)
Definition point.h:377
float scalar
Definition scalar.h:45
__forceinline point greater(const point &v0, const point &v1)
Definition point.h:510
half operator+(half one, half two)
Definition half.h:105
__forceinline bool greater_all(const point &v0, const point &v1)
Definition point.h:441
__forceinline bool nearequal(const point &v0, const point &v1, float epsilon)
Definition point.h:485
static const __m128 _mask_xyz
Definition vec3.h:37
__forceinline bool lessequal_any(const point &v0, const point &v1)
Definition point.h:408
__forceinline vec3 xyz(const point &v)
Definition point.h:528
static const __m128 _plus1
Definition vec3.h:34
Represents a 3D point in space.
Definition point.h:22
void set(scalar x, scalar y, scalar z)
set content
Definition point.h:332
float z
Definition point.h:78
void operator=(const point &rhs)
assignment operator
Definition point.h:155
bool operator==(const point &rhs) const
equality operator
Definition point.h:283
void storeu(scalar *ptr) const
write content to unaligned memory through the write cache
Definition point.h:222
bool operator!=(const point &rhs) const
inequality operator
Definition point.h:293
void operator+=(const vector &rhs)
inplace add
Definition point.h:173
__m128 vec
Definition point.h:75
void store(scalar *ptr) const
write content to 16-byte-aligned memory through the write cache
Definition point.h:212
void store3(scalar *ptr) const
write content to 16-byte-aligned memory through the write cache
Definition point.h:232
float x
Definition point.h:78
void load(const scalar *ptr)
load content from 16-byte-aligned memory
Definition point.h:192
point()
default constructor
Definition point.h:87
void operator-=(const vector &rhs)
inplace sub
Definition point.h:182
void loadu(const scalar *ptr)
load content from unaligned memory
Definition point.h:202
float y
Definition point.h:78
void storeu3(scalar *ptr) const
write content to unaligned memory through the write cache
Definition point.h:244
float operator[](int index) const
Definition point.h:303
A 3D vector.
Definition vec3.h:40
float __w
Definition vec3.h:94
__m128 vec
Definition vec3.h:96
A 4D vector.
Definition vec4.h:24
__m128 vec
Definition vec4.h:95
A vector is a 3D direction in space.
Definition vector.h:22
__m128 vec
Definition vector.h:82
bool operator==(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.h:272
bool operator!=(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.h:282
#define NEBULA_ALIGN16
Definition types.h:181