Nebula
Loading...
Searching...
No Matches
plane.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
12//------------------------------------------------------------------------------
13#include "math/point.h"
14#include "math/vector.h"
15#include "math/line.h"
16#include "scalar.h"
17#include "clipstatus.h"
18
19namespace Math
20{
21struct plane
22{
23
25 plane() = default;
29 plane(const point& p, const vector& n);
31 plane(const point& p0, const point& p1, const point& p2);
33 plane(const __m128 & rhs);
34
36 bool intersect(line const& line, point& outIntersectPoint);
38 vector get_normal() const;
39
41 void set(scalar a, scalar b, scalar c, scalar d);
42
43 union
44 {
45 __m128 vec;
46 struct
47 {
48 float a, b, c, d;
49 };
50 };
51};
52
53//------------------------------------------------------------------------------
56__forceinline
58{
59 this->vec = _mm_setr_ps(a, b, c, d);
60}
61
62//------------------------------------------------------------------------------
65__forceinline
66plane::plane(const point& p, const vector& n)
67{
68 float d = dot(p, n);
69 this->vec = _mm_setr_ps(n.x, n.y, n.z, d);
70}
71
72//------------------------------------------------------------------------------
75__forceinline
76plane::plane(const point& p0, const point& p1, const point& p2)
77{
78 vector v21 = p0 - p1;
79 vector v31 = p0 - p2;
80
81 vector cr = cross(v21, v31);
82 cr = normalize(cr);
83 float d = dot(cr, p0);
84
85 this->vec = _mm_setr_ps(cr.x, cr.y, cr.z, d);
86}
87
88//------------------------------------------------------------------------------
91__forceinline
92plane::plane(const __m128& rhs)
93{
94 this->vec = rhs;
95}
96
97//------------------------------------------------------------------------------
100__forceinline bool
101plane::intersect(line const& line, point& outIntersectPoint)
102{
103 Math::vector dv = line.m;
104 Math::vector n = this->get_normal();
105 float d = (this->d - Math::dot(n, line.b)) / Math::dot(n, dv);
106
107 if (d >= 0.0f && d <= 1.0f)
108 {
109 outIntersectPoint = line.b + d * dv;
110 return true;
111 }
112
113 return false;
114}
115
116//------------------------------------------------------------------------------
119__forceinline vector
121{
122 vector res;
123 res.vec = _mm_and_ps(this->vec, _mask_xyz);
124 return res;
125}
126
127//------------------------------------------------------------------------------
130__forceinline
132{
133 this->vec = _mm_setr_ps(a, b, c, d);
134}
135
136//------------------------------------------------------------------------------
139inline vector
141{
142 vector res;
143 res.vec = _mm_and_ps(plane.vec, _mask_xyz);
144 return res;
145}
146
147//------------------------------------------------------------------------------
150inline point
152{
153 return _mm_mul_ps(get_normal(plane).vec, _mm_set1_ps(plane.d));
154}
155
156//------------------------------------------------------------------------------
159inline bool
160intersectline(const plane& plane, const point& startPoint, const point& endPoint, point& outIntersectPoint)
161{
162 Math::vector dv = endPoint - startPoint;
164 float d = (plane.d - Math::dot(n, startPoint)) / Math::dot(n, dv);
165
166 if (d >= 0.0f && d <= 1.0f)
167 {
168 outIntersectPoint = startPoint + d * dv;
169 return true;
170 }
171
172 return false;
173}
174
175//------------------------------------------------------------------------------
179inline bool
180intersectplane(const plane& p1, const plane& p2, line& outLine)
181{
182 vector n0 = get_normal(p1);
183 vector n1 = get_normal(p2);
184 float n00 = dot(n0, n0);
185 float n01 = dot(n0, n1);
186 float n11 = dot(n1, n1);
187 float det = n00 * n11 - n01 * n01;
188 const float tol = N_TINY;
189 if (fabs(det) < tol)
190 {
191 return false;
192 }
193 else
194 {
195 float inv_det = 1.0f / det;
196 float c0 = (n11 * p1.d - n01 * p2.d) * inv_det;
197 float c1 = (n00 * p2.d - n01 * p1.d) * inv_det;
198 outLine.m = cross(n0, n1);
199 outLine.b = vec4(n0 * c0 + n1 * c1, 1);
200 return true;
201 }
202}
203
204//------------------------------------------------------------------------------
207inline ClipStatus::Type
208clip(const plane& plane, const line& l, line& outClippedLine)
209{
210 n_assert(&l != &outClippedLine);
211 float d0 = dot(get_normal(plane), l.start());
212 float d1 = dot(get_normal(plane), l.end());
213 if ((d0 >= N_TINY) && (d1 >= N_TINY))
214 {
215 // start and end point above plane
216 outClippedLine = l;
217 return ClipStatus::Inside;
218 }
219 else if ((d0 < N_TINY) && (d1 < N_TINY))
220 {
221 // start and end point below plane
222 return ClipStatus::Outside;
223 }
224 else
225 {
226 // line is clipped
227 point clipPoint;
228 intersectline(plane, l.start(), l.end(), clipPoint);
229 if (d0 >= N_TINY)
230 {
231 outClippedLine.set(l.start(), clipPoint);
232 }
233 else
234 {
235 outClippedLine.set(clipPoint, l.end());
236 }
237 return ClipStatus::Clipped;
238 }
239}
240
241
242//------------------------------------------------------------------------------
245__forceinline scalar
246dot(const plane& p, const vec4& v1)
247{
248 return _mm_cvtss_f32(_mm_dp_ps(p.vec, v1.vec, 0xF1));
249}
250
251//------------------------------------------------------------------------------
254__forceinline plane
256{
257 vec4 f(p.vec);
258 scalar len = length3(f);
259 if (len < N_TINY)
260 {
261 return p;
262 }
263 f *= 1.0f / len;
264 plane ret;
265 ret.vec = f.vec;
266 return ret;
267}
268
269
270//------------------------------------------------------------------------------
273__forceinline plane
274operator*(const mat4& m, const plane& p)
275{
276 __m128 x = _mm_shuffle_ps(p.vec, p.vec, _MM_SHUFFLE(0, 0, 0, 0));
277 __m128 y = _mm_shuffle_ps(p.vec, p.vec, _MM_SHUFFLE(1, 1, 1, 1));
278 __m128 z = _mm_shuffle_ps(p.vec, p.vec, _MM_SHUFFLE(2, 2, 2, 2));
279 __m128 w = _mm_shuffle_ps(p.vec, p.vec, _MM_SHUFFLE(3, 3, 3, 3));
280
281 return _mm_add_ps(
282 _mm_add_ps(_mm_mul_ps(x, m.r[0].vec), _mm_mul_ps(y, m.r[1].vec)),
283 _mm_add_ps(_mm_mul_ps(z, m.r[2].vec), _mm_mul_ps(w, m.r[3].vec))
284 );
285}
286
287} // namespace Math
Type
Definition clipstatus.h:22
@ Outside
Definition clipstatus.h:24
@ Inside
Definition clipstatus.h:23
@ Clipped
Definition clipstatus.h:25
A line in 3d space.
Definition line.h:22
point end() const
get end point
Definition line.h:123
void set(const point &startPoint, const point &endPoint)
set start and end point
Definition line.h:94
point b
Definition line.h:55
const point & start() const
get start point
Definition line.h:114
vector m
Definition line.h:56
#define n_assert(exp)
Definition debug.h:50
Different curves.
Definition angularpfeedbackloop.h:17
__forceinline vec3 cross(const vec3 &v0, const vec3 &v1)
Definition vec3.h:417
point get_point(const plane &plane)
Definition plane.h:151
__forceinline scalar length3(const vec4 &v)
Definition vec4.h:395
bool intersectline(const plane &plane, const point &startPoint, const point &endPoint, point &outIntersectPoint)
Definition plane.h:160
__forceinline scalar dot(const plane &p, const vec4 &v1)
Definition plane.h:246
__forceinline plane normalize(const plane &p)
Definition plane.h:255
ClipStatus::Type clip(const plane &plane, const line &l, line &outClippedLine)
Definition plane.h:208
float scalar
Definition scalar.h:45
half operator*(half one, half two)
Definition half.h:123
bool intersectplane(const plane &p1, const plane &p2, line &outLine)
TODO: Test and make sure it produces correct intersection line.
Definition plane.h:180
static const __m128 _mask_xyz
Definition vec3.h:37
vector get_normal(const plane &plane)
Definition plane.h:140
Nebula's scalar datatype.
#define N_TINY
Definition scalar.h:43
A 4x4 single point precision float matrix.
Definition mat4.h:49
vec4 r[4]
as a two-dimensional array
Definition mat4.h:132
A mathematical plane represented by a normal and a distance from origin.
Definition plane.h:22
float a
Definition plane.h:48
plane()=default
default constructor, NOTE: does NOT setup components!
vector get_normal() const
get normal from plane
Definition plane.h:120
void set(scalar a, scalar b, scalar c, scalar d)
set content
Definition plane.h:131
float d
Definition plane.h:48
float c
Definition plane.h:48
__m128 vec
Definition plane.h:45
float b
Definition plane.h:48
bool intersect(line const &line, point &outIntersectPoint)
check intersection against line
Definition plane.h:101
Represents a 3D point in space.
Definition point.h:22
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
float z
Definition vector.h:85
float x
Definition vector.h:85
float y
Definition vector.h:85