1+ /* *
2+ * =============================================================================
3+ * Source Python
4+ * Copyright (C) 2012 Source Python Development Team. All rights reserved.
5+ * =============================================================================
6+ *
7+ * This program is free software; you can redistribute it and/or modify it under
8+ * the terms of the GNU General Public License, version 3.0, as published by the
9+ * Free Software Foundation.
10+ *
11+ * This program is distributed in the hope that it will be useful, but WITHOUT
12+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14+ * details.
15+ *
16+ * You should have received a copy of the GNU General Public License along with
17+ * this program. If not, see <http://www.gnu.org/licenses/>.
18+ *
19+ * As a special exception, the Source Python Team gives you permission
20+ * to link the code of this program (as well as its derivative works) to
21+ * "Half-Life 2," the "Source Engine," and any Game MODs that run on software
22+ * by the Valve Corporation. You must obey the GNU General Public License in
23+ * all respects for all other code used. Additionally, the Source.Python
24+ * Development Team grants this exception to all derivative works.
25+ */
26+
27+ // -----------------------------------------------------------------------------
28+ // Includes
29+ // -----------------------------------------------------------------------------
30+ #include " modules/export_main.h"
31+ #include " mathlib/vector.h"
32+
33+
34+ // -----------------------------------------------------------------------------
35+ // Exposes the mathlib_c module.
36+ // -----------------------------------------------------------------------------
37+ void export_vector ();
38+
39+ DECLARE_SP_MODULE (mathlib_c)
40+ {
41+ export_vector ();
42+ }
43+
44+ // -----------------------------------------------------------------------------
45+ // Exposes Vector
46+ // -----------------------------------------------------------------------------
47+ class VectorExt
48+ {
49+ public:
50+ static void SetItem (Vector* pVec, int iIndex, float fValue )
51+ {
52+ pVec[iIndex] = fValue ;
53+ }
54+ };
55+
56+ BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS (is_zero_overload, IsZero, 0 , 1 )
57+
58+ void export_vector()
59+ {
60+ // TODO: Rename
61+ class_<Vector>(" CVector" )
62+ .def (init<float , float , float >())
63+ .def (init<float >())
64+
65+ // Members
66+ .def_readwrite (" x" ,
67+ &Vector::x
68+ )
69+
70+ .def_readwrite (" y" ,
71+ &Vector::y
72+ )
73+
74+ .def_readwrite (" z" ,
75+ &Vector::z
76+ )
77+
78+ // Methods
79+ .def (" init" ,
80+ &Vector::Init
81+ )
82+
83+ .def (" is_valid" ,
84+ &Vector::IsValid,
85+ " Returns True if the vector is valid."
86+ )
87+
88+ .def (" invalidated" ,
89+ &Vector::Invalidate,
90+ " Invalidates the vector."
91+ )
92+
93+ .def (" __getitem__" ,
94+ static_cast < float (Vector::*)( int ) const >(&Vector::operator [])
95+ )
96+
97+ .def (" __setitem__" ,
98+ &VectorExt::SetItem
99+ )
100+
101+ .def (" as_vector_2D" ,
102+ GET_METHOD (Vector2D&, Vector, AsVector2D),
103+ reference_existing_object_policy ()
104+ )
105+
106+ .def (" random" ,
107+ &Vector::Random,
108+ args (" min" , " max" ),
109+ " Fills the vector with random values within the given range."
110+ )
111+
112+ .def (" zero" ,
113+ &Vector::Zero,
114+ " Zeros out the vector."
115+ )
116+
117+ .def (self == self)
118+ .def (self != self)
119+ .def (self += self)
120+ .def (self -= self)
121+ .def (self *= self)
122+ .def (self *= float ())
123+ .def (self /= self)
124+ .def (self /= float ())
125+ .def (self += float ())
126+ .def (self -= float ())
127+
128+ .def (" negate" ,
129+ &Vector::Negate,
130+ " Negates the vector."
131+ )
132+
133+ .def (" get_length" ,
134+ &Vector::Length,
135+ " Returns the vector's 3D length."
136+ )
137+
138+ .def (" get_length_sqr" ,
139+ &Vector::LengthSqr,
140+ " Returns the vector's 3D length as a square product."
141+ )
142+
143+ .def (" is_zero" ,
144+ &Vector::IsZero,
145+ is_zero_overload (
146+ args (" tolerance" ),
147+ " Returns True if x, y and z are zero or within the tolerance."
148+ )
149+ )
150+
151+ .def (" normalize" ,
152+ &Vector::NormalizeInPlace,
153+ " Normalizes the vector."
154+ )
155+
156+ .def (" is_length_greater_than" ,
157+ &Vector::IsLengthGreaterThan
158+ )
159+
160+ .def (" is_length_less_than" ,
161+ &Vector::IsLengthLessThan
162+ )
163+
164+ .def (" is_within_box" ,
165+ &Vector::WithinAABox,
166+ args (" mins" , " maxs" ),
167+ " Returns True if the vector is within the given box coordinates."
168+ )
169+
170+ .def (" get_distance" ,
171+ &Vector::DistTo,
172+ args (" other" ),
173+ " Returns the distance to the other vector."
174+ )
175+
176+ .def (" get_distance_sqr" ,
177+ &Vector::DistToSqr,
178+ args (" other" ),
179+ " Returns the distance to the other vector as a square product."
180+ )
181+
182+ .def (" mul_add" ,
183+ &Vector::MulAdd,
184+ args (" a" , " b" , " scalar" ),
185+ " Multiply and add. this = a + b * scalar."
186+ )
187+
188+ .def (" dot" ,
189+ &Vector::Dot,
190+ " Returns the dot product."
191+ )
192+
193+ .def (" get_length_2D" ,
194+ &Vector::Length2D,
195+ " Returns the vector's 2D length."
196+ )
197+
198+ .def (" get_length_2D_sqr" ,
199+ &Vector::Length2DSqr,
200+ " Returns the vector's 2D length as a square product."
201+ )
202+
203+ .def (self + self)
204+ .def (self - self)
205+ .def (self * self)
206+ .def (self / self)
207+ .def (self * float ())
208+ .def (self / float ())
209+
210+ .def (" cross" ,
211+ &Vector::Cross,
212+ " Returns the cross product between two vectors."
213+ )
214+
215+ .def (" min" ,
216+ &Vector::Min,
217+ " Returns a new vector containing the lowest values of both vectors."
218+ )
219+
220+ .def (" max" ,
221+ &Vector::Max,
222+ " Returns a new vector containing the biggest values of both vectors."
223+ )
224+ ;
225+ }
0 commit comments