AGX Dynamics 2.42.3.0
Loading...
Searching...
No Matches
agx/Plane.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#ifndef AGX_PLANE_H
18#define AGX_PLANE_H
19
20#include <agx/Vec4.h>
21#include <agx/AffineMatrix4x4.h>
22
23namespace agx
24{
25
30 template <typename T>
31 class PlaneT
32 {
33 public:
34
36 PlaneT();
37
42 PlaneT( const agx::Vec4T<T>& plane);
43
48 PlaneT( const T a, const T b, const T c, const T d );
49
55 PlaneT( const agx::Vec3T<T>& normal, const T d );
56
62 PlaneT( const agx::Vec3T<T>& normal, const agx::Vec3T<T>& point );
63
67 PlaneT(const agx::Vec3T<T>& p1, const agx::Vec3T<T>& p2, const agx::Vec3T<T>& p3);
68
73 operator agx::Vec4T<T> () const;
74
77
79 T getDistance() const;
80
82 void setNormal( const agx::Vec3T<T>& normal );
83
85 void setDistance( const T distance );
86
91 void set(const agx::Vec4T<T>& plane);
92
94 void set(const agx::PlaneT<T>& plane);
95
99 T signedDistanceToPoint( const agx::Vec3T<T>& point ) const;
100
106 agx::Vec3T<T> project( const agx::Vec3T<T>& point ) const;
107
109 PlaneT calculatePlane( const agx::AffineMatrix4x4& transform ) const;
110
113
114 private:
115 void init(const agx::Vec3T<T>& normal, const T distance);
116
117 private:
118 agx::Vec4T<T> m_data;
119 };
120
124
125
126
127
128
130
131 template <typename T>
132 std::ostream& operator<<(std::ostream& output, const PlaneT<T>& plane)
133 {
134 output << plane.getNormal() << " : " << plane.getDistance();
135 return output;
136 }
137
138 template <typename T>
140 {
141 this->init(Vec3T<T>(0, 0, 1), 0);
142 }
143
144 template <typename T>
145 AGX_FORCE_INLINE PlaneT<T>::PlaneT(const Vec4T<T>& plane) : m_data(plane)
146 {}
147
148 template <typename T>
149 AGX_FORCE_INLINE PlaneT<T>::PlaneT( const T a, const T b, const T c, const T d )
150 {
151 this->init(Vec3T<T>(a, b, c), d);
152 }
153
154 template <typename T>
155 AGX_FORCE_INLINE PlaneT<T>::PlaneT( const Vec3T<T>& normal, const T d )
156 {
157 this->init(normal, d);
158 }
159
160 template <typename T>
162 {
163 T d = - (point * normal);
164 this->init(normal, d);
165 }
166
167 template <typename T>
169 {
170 Vec3T<T> one = p1 - p2;
171 Vec3T<T> two = p1 - p3;
172 Vec3T<T> normal = one ^ two;
173 normal.normalize();
174 T d = - (p1*normal);
175 this->init(normal, d);
176 }
177
178 template <typename T>
179 AGX_FORCE_INLINE void PlaneT<T>::init(const Vec3T<T>& normal, T distance)
180 {
181 m_data.set(normal[0], normal[1], normal[2], distance);
182 }
183
184 template <typename T>
185 AGX_FORCE_INLINE PlaneT<T>::operator Vec4T<T> () const
186 {
187 return m_data;
188 }
189
190 template <typename T>
191 AGX_FORCE_INLINE Vec3T<T> PlaneT<T>::getNormal() const { return Vec3T<T>(m_data[0], m_data[1], m_data[2]); }
192
193 template <typename T>
194 AGX_FORCE_INLINE T PlaneT<T>::getDistance() const { return m_data[3]; }
195
196 template <typename T>
197 AGX_FORCE_INLINE void PlaneT<T>::setNormal( const Vec3T<T>& normal ) { m_data = Vec4T<T>(normal, m_data[3]); }
198
199 template <typename T>
200 AGX_FORCE_INLINE void PlaneT<T>::setDistance( T distance ) { m_data[3] = distance; }
201
202 template <typename T>
203 AGX_FORCE_INLINE void PlaneT<T>::set(const agx::Vec4T<T>& plane) { this->init(agx::Vec3T<T>(plane[0], plane[1], plane[2]), plane[3]); }
204
205 template <typename T>
206 AGX_FORCE_INLINE void PlaneT<T>::set(const agx::PlaneT<T>& plane) { this->init(plane.getNormal(), plane.getDistance()); }
207
208 template <typename T>
210 {
211 return ( getNormal() * point ) + getDistance();
212 }
213
214 template <typename T>
216 {
217 return point - signedDistanceToPoint( point ) * getNormal();
218 }
219
220 template <typename T>
222 {
223 agx::Vec3T<T> normal, point;
224
225 // calculate one known point on the current plane
226 point = getNormal() * (-getDistance());
227
228 // transform the point (so it'll be on the new plane)
229 point = point * transform;
230
231 // calculate the new planes normal
232 normal = transform.transform3x3( getNormal() );
233
234 // return the plane, n dot p = -d
235 return PlaneT<T>(normal, - (normal * point));
236 }
237
238 template <typename T>
240 {
241 Vec3T<T> normal, point;
242
243 // calculate one known point on the current plane
244 point = getNormal() * (-getDistance());
245
246 // transform the point (so it'll be on the new plane)
247 point = point * transform;
248
249 // calculate the new planes normal
250 normal = transform.transform3x3( getNormal() );
251
252 // return the plane, n dot p = -d
253 return PlaneT<T>(normal, - (normal * point));
254 }
255
256}
257
259AGX_TYPE_BINDING(agx::Plane64, "Plane")
260
261#endif
262
263
#define AGX_TYPE_BINDING(_Type, _Name)
Definition: Type.h:179
Class representing the mathematical concept of a plane, also called a half- space.
Definition: agx/Plane.h:32
PlaneT calculateTransformedPlane(const agx::AffineMatrix4x4 &transform) const
Create a new plane that is equal to the current plane transformed by the given transform.
Definition: agx/Plane.h:239
void setNormal(const agx::Vec3T< T > &normal)
Change the orientation of the plane so that the given normal becomes the new normal of the plane.
Definition: agx/Plane.h:197
agx::Vec3T< T > project(const agx::Vec3T< T > &point) const
Projects the point point onto this plane.
Definition: agx/Plane.h:215
T getDistance() const
Definition: agx/Plane.h:194
agx::Vec3T< T > getNormal() const
Definition: agx/Plane.h:191
T signedDistanceToPoint(const agx::Vec3T< T > &point) const
Calculates the distance from the plane to a point.
Definition: agx/Plane.h:209
void setDistance(const T distance)
Translate the plane parallel to the normal so that the given distance becomes the new distance from t...
Definition: agx/Plane.h:200
PlaneT()
Create a plane with it surface on the X and Y axes.
Definition: agx/Plane.h:139
PlaneT calculatePlane(const agx::AffineMatrix4x4 &transform) const
Create a new plane that is equal to the current plane transformed by the given transform.
Definition: agx/Plane.h:221
void set(const agx::Vec4T< T > &plane)
Reconfigure this plane according the given specification.
Definition: agx/Plane.h:203
A class holding 3 dimensional vectors and providing basic arithmetic.
Definition: Vec3Template.h:46
Real normalize()
Normalize the vector so that it has length unity.
Definition: Vec3Template.h:701
A class holding 4 dimensional vectors and providing basic arithmetic.
Definition: Vec4Template.h:35
#define AGX_FORCE_INLINE
Definition: macros.h:58
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
PlaneT< agx::Real64 > Plane64
Definition: agx/Plane.h:123
PlaneT< agx::Real > Plane
Definition: agx/Plane.h:121
std::ostream & operator<<(std::ostream &os, const agx::AddedMassInteraction::Matrix6x6 &m)
PlaneT< agx::Real32 > Plane32
Definition: agx/Plane.h:122
void AGXPHYSICS_EXPORT init()
Initialize AGX Dynamics API including thread resources and must be executed before using the AGX API.