3dmath.h

Go to the documentation of this file.
00001 /*      3DMath.h - the 3D math family of classes - vectors, rays, quat, matrices for MAXScript
00002  *
00003  *      Copyright (c) John Wainwright, 1996
00004  *      
00005  *
00006  */
00007 
00008 #pragma once
00009 
00010 #include "..\ScripterExport.h"
00011 #include "..\kernel\value.h"
00012 #include "..\..\quat.h"
00013 #include "..\..\ipoint3.h"
00014 
00015 #pragma warning(push)
00016 #pragma warning(disable:4100)
00017 
00018 extern ScripterExport void _QuatToEuler(Quat& q, float* ang);
00019 extern ScripterExport void _EulerToQuat(float* ang, Quat& q);
00020 
00021 /* ------------------------ Point3Value ------------------------------ */
00022 
00023 applyable_class_debug_ok (Point3Value)
00024 
00025 class Point3Value : public Value
00026 {
00027 public:
00028     Point3      p;
00029 
00030     ENABLE_STACK_ALLOCATE(Point3Value);
00031 
00032  ScripterExport Point3Value(Point3 init_point);
00033  ScripterExport Point3Value(float x, float y, float z);
00034  ScripterExport Point3Value(Value* x, Value* y, Value* z);
00035 
00036                 classof_methods(Point3Value, Value);
00037     void        collect() { delete this; }
00038     ScripterExport void     sprin1(CharStream* s);
00039 #   define      is_point3(v) ((DbgVerify(!is_sourcepositionwrapper(v)), (v))->tag == class_tag(Point3Value))
00040 
00041     static Value* make(Value**arg_list, int count);
00042     
00043     /* operations */
00044 
00045 #include "..\macros\define_implementations.h"
00046 #   include "..\protocols\vector.inl"
00047     use_generic  ( coerce,  "coerce");
00048     use_generic  ( copy,    "copy");
00049     use_generic  ( get,     "get");
00050     use_generic  ( put,     "put");
00051 
00052     /* built-in property accessors */
00053 
00054     def_property ( x );
00055     def_property ( y );
00056     def_property ( z );
00057 
00058     Point3      to_point3() { return p; }
00059     AColor      to_acolor() { return AColor (p.x / 255.0f, p.y / 255.0f, p.z / 255.0f); }
00060     Point2      to_point2() { return Point2 (p.x, p.y); }
00061     ScripterExport void     to_fpvalue(FPValue& v);
00062     COLORREF    to_colorref() { return RGB((int)p.x, (int)p.y, (int)p.z); }
00063 
00064     // scene I/O 
00065     IOResult Save(ISave* isave);
00066     static Value* Load(ILoad* iload, USHORT chunkID, ValueLoader* vload);
00067 };
00068 
00069 class ConstPoint3Value : public Point3Value
00070 {
00071 public:
00072  ScripterExport ConstPoint3Value(float x, float y, float z)
00073      : Point3Value (x, y, z) { }
00074 
00075     void        collect() { delete this; }
00076     BOOL        is_const() { return TRUE; }
00077 
00078     Value* set_x(Value** arg_list, int count) { throw RuntimeError (_M("Constant vector, not settable")); return NULL; }
00079     Value* set_y(Value** arg_list, int count) { throw RuntimeError (_M("Constant vector, not settable")); return NULL; }
00080     Value* set_z(Value** arg_list, int count) { throw RuntimeError (_M("Constant vector, not settable")); return NULL; }
00081 };
00082 
00083 // The following function has been added
00084 // in 3ds max 4.2.  If your plugin utilizes this new
00085 // mechanism, be sure that your clients are aware that they
00086 // must run your plugin with 3ds max version 4.2 or higher.
00087 
00088 inline  IPoint3 to_ipoint3(Value* val) { 
00089             Point3 p = val->to_point3(); 
00090             return IPoint3((int)p.x, (int)p.y, (int)p.z); }
00091 
00092 // End of 3ds max 4.2 Extension
00093 
00094 /* ------------------------ RayValue ------------------------------ */
00095 
00096 applyable_class_debug_ok (RayValue)
00097 
00098 class RayValue : public Value
00099 {
00100 public:
00101     Ray         r;
00102 
00103     ENABLE_STACK_ALLOCATE(RayValue);
00104 
00105  ScripterExport RayValue(Point3 init_origin, Point3 init_dir);
00106  ScripterExport RayValue(Ray init_ray);
00107 
00108                 classof_methods (RayValue, Value);
00109     void        collect() { delete this; }
00110     ScripterExport void sprin1(CharStream* s);
00111 
00112     /* operations */
00113     
00114     use_generic  ( copy,    "copy");
00115 
00116     /* built-in property accessors */
00117 
00118     def_property ( pos );
00119     def_property_alias ( position, pos );
00120     def_property ( dir );
00121 
00122     Ray         to_ray() { return r; }
00123     ScripterExport void     to_fpvalue(FPValue& v);
00124 #   define      is_ray(v) ((DbgVerify(!is_sourcepositionwrapper(v)), (v))->tag == class_tag(RayValue))
00125 
00126     // scene I/O 
00127     IOResult Save(ISave* isave);
00128     static Value* Load(ILoad* iload, USHORT chunkID, ValueLoader* vload);
00129 };
00130 
00131 /* ------------------------ QuatValue ------------------------------ */
00132 
00133 applyable_class_debug_ok (QuatValue)
00134 
00135 class QuatValue : public Value
00136 {
00137 public:
00138     Quat        q;
00139 
00140     ENABLE_STACK_ALLOCATE(QuatValue);
00141 
00142  ScripterExport QuatValue(const Quat& init_quat);
00143  ScripterExport QuatValue(float w, float x, float y, float z);
00144  ScripterExport QuatValue(Value* w, Value* x, Value* y, Value* z);
00145  ScripterExport QuatValue(Value* val);
00146  ScripterExport QuatValue(AngAxis& aa);
00147  ScripterExport QuatValue(float* angles);
00148  ScripterExport QuatValue(Matrix3& m);
00149 
00150                 classof_methods (QuatValue, Value);
00151 #   define      is_quat(v) ((DbgVerify(!is_sourcepositionwrapper(v)), (v))->tag == class_tag(QuatValue))
00152     void        collect() { delete this; }
00153     ScripterExport void sprin1(CharStream* s);
00154 
00155     /* operations */
00156 
00157 #include "..\macros\define_implementations.h"
00158 #   include "..\protocols\quat.inl"
00159     use_generic  ( copy,    "copy");
00160 
00161     /* built-in property accessors */
00162 
00163     def_property ( w );
00164     def_property ( x );
00165     def_property ( y );
00166     def_property ( z );
00167     def_property ( angle );
00168     def_property ( axis );
00169 
00170     Quat        to_quat() { return q; }
00171     AngAxis     to_angaxis() { return AngAxis(q); }
00172     ScripterExport void     to_fpvalue(FPValue& v);
00173 
00174     // scene I/O 
00175     IOResult Save(ISave* isave);
00176     static Value* Load(ILoad* iload, USHORT chunkID, ValueLoader* vload);
00177 };
00178 
00179 /* ------------------------ AngleAxis ------------------------------ */
00180 
00181 applyable_class_debug_ok (AngAxisValue)
00182 
00183 class AngAxisValue : public Value
00184 {
00185 public:
00186     AngAxis     aa;
00187 
00188     ENABLE_STACK_ALLOCATE(AngAxisValue);
00189 
00190  ScripterExport AngAxisValue(const AngAxis& iaa);
00191  ScripterExport AngAxisValue(const Quat& q);
00192  ScripterExport AngAxisValue(const Matrix3& m);
00193  ScripterExport AngAxisValue(float* angles);
00194  ScripterExport AngAxisValue(float angle, Point3 axis);
00195  ScripterExport AngAxisValue(Value*);
00196  ScripterExport AngAxisValue(Value* angle, Value* axis);
00197 
00198                 classof_methods (AngAxisValue, Value);
00199 #   define      is_angaxis(v) ((DbgVerify(!is_sourcepositionwrapper(v)), (v))->tag == class_tag(AngAxisValue))
00200     void        collect() { delete this; }
00201     ScripterExport void sprin1(CharStream* s);
00202 
00203     /* operations */
00204     
00205 #include "..\macros\define_implementations.h"
00206 
00207     use_generic( coerce,    "coerce" );
00208     use_generic( eq,        "=");
00209     use_generic( ne,        "!=");
00210     use_generic( random,    "random");
00211     use_generic( copy,      "copy");
00212 
00213     /* built-in property accessors */
00214 
00215     def_property ( angle );
00216     def_property ( axis );
00217     def_property ( numrevs );
00218 
00219     AngAxis     to_angaxis() { return aa; }
00220     Quat        to_quat() { return Quat (aa); }
00221     ScripterExport void     to_fpvalue(FPValue& v);
00222 
00223     // scene I/O 
00224     IOResult Save(ISave* isave);
00225     static Value* Load(ILoad* iload, USHORT chunkID, ValueLoader* vload);
00226 };
00227 
00228 /* ------------------------ EulerAngles ------------------------------ */
00229 
00230 applyable_class_debug_ok (EulerAnglesValue)
00231 
00232 class EulerAnglesValue : public Value
00233 {
00234 public:
00235     float       angles[3];
00236 
00237     ENABLE_STACK_ALLOCATE(EulerAnglesValue);
00238 
00239  ScripterExport EulerAnglesValue(float ax, float ay, float az);
00240  ScripterExport EulerAnglesValue(const Quat&);
00241  ScripterExport EulerAnglesValue(const Matrix3&);
00242  ScripterExport EulerAnglesValue(const AngAxis&);
00243 
00244                 classof_methods (EulerAnglesValue, Value);
00245 #   define      is_eulerangles(v) ((DbgVerify(!is_sourcepositionwrapper(v)), (v))->tag == class_tag(EulerAnglesValue))
00246     void        collect() { delete this; }
00247     ScripterExport void sprin1(CharStream* s);
00248 
00249     /* operations */
00250     
00251 #include "..\macros\define_implementations.h"
00252 
00253     use_generic( coerce,    "coerce" );
00254     use_generic( eq,        "=");
00255     use_generic( ne,        "!=");
00256     use_generic( random,    "random");
00257     use_generic( copy,      "copy");
00258 
00259     /* built-in property accessors */
00260 
00261     def_property ( x );
00262     def_property ( y );
00263     def_property ( z );
00264     def_property ( x_rotation );
00265     def_property ( y_rotation );
00266     def_property ( z_rotation );
00267 
00268     AngAxis     to_angaxis() { return AngAxis (to_quat()); }
00269     Quat        to_quat() { Quat q; _EulerToQuat(angles, q); return Quat (q); }
00270     ScripterExport void     to_fpvalue(FPValue& v);
00271     float*      to_eulerangles() { return angles; }
00272 
00273     // scene I/O 
00274     IOResult Save(ISave* isave);
00275     static Value* Load(ILoad* iload, USHORT chunkID, ValueLoader* vload);
00276 };
00277 
00278 /* ------------------------ Matrix ------------------------------ */
00279 
00280 applyable_class_debug_ok (Matrix3Value)
00281 
00282 class Matrix3Value : public Value
00283 {
00284 public:
00285     Matrix3     m;
00286 
00287     ENABLE_STACK_ALLOCATE(Matrix3Value);
00288 
00289  ScripterExport Matrix3Value(int i);
00290  ScripterExport Matrix3Value(const Matrix3& im);
00291  ScripterExport Matrix3Value(const Quat& q);
00292  ScripterExport Matrix3Value(const AngAxis& aa);
00293  ScripterExport Matrix3Value(float* angles);
00294  ScripterExport Matrix3Value(const Point3& row0, const Point3& row1, const Point3& row2, const Point3& row3);
00295 
00296                 classof_methods (Matrix3Value, Value);
00297     void        collect() { delete this; }
00298     ScripterExport void sprin1(CharStream* s);
00299 #   define      is_matrix3(v) ((DbgVerify(!is_sourcepositionwrapper(v)), (v))->tag == class_tag(Matrix3Value))
00300 
00301     /* operations */
00302     
00303 #include "..\macros\define_implementations.h"
00304 #   include "..\protocols\matrix.inl"
00305     use_generic( copy,      "copy");
00306 
00307     /* built-in property accessors */
00308 
00309     def_property ( row1 );
00310     def_property ( row2 );
00311     def_property ( row3 );
00312     def_property ( row4 );
00313     def_property ( translation );
00314     def_property ( pos );
00315     def_property ( rotation );
00316     def_property ( scale );
00317 
00318     use_generic( get,       "get");
00319     use_generic( put,       "put");
00320 
00321 
00322     Value*      get_property(Value** arg_list, int count);
00323     Value*      set_property(Value** arg_list, int count);
00324 
00325     Matrix3&    to_matrix3() { return m; }
00326     Quat        to_quat() { return Quat (m); }
00327     ScripterExport void     to_fpvalue(FPValue& v);
00328 
00329     // scene I/O 
00330     IOResult Save(ISave* isave);
00331     static Value* Load(ILoad* iload, USHORT chunkID, ValueLoader* vload);
00332 };
00333 
00334 /* ------------------------ Point2Value ------------------------------ */
00335 
00336 applyable_class_debug_ok (Point2Value)
00337 
00338 class Point2Value : public Value
00339 {
00340 public:
00341     Point2      p;
00342 
00343     ENABLE_STACK_ALLOCATE(Point2Value);
00344 
00345     ScripterExport Point2Value(Point2 ipoint);
00346     ScripterExport Point2Value(POINT ipoint);
00347     ScripterExport Point2Value(float x, float y);
00348     ScripterExport Point2Value(Value* x, Value* y);
00349 
00350                 classof_methods(Point2Value, Value);
00351     void        collect() { delete this; }
00352     ScripterExport void sprin1(CharStream* s);
00353 #   define      is_point2(v) ((DbgVerify(!is_sourcepositionwrapper(v)), (v))->tag == class_tag(Point2Value))
00354 
00355     static Value* make(Value**arg_list, int count);
00356     
00357     /* operations */
00358 
00359 #include "..\macros\define_implementations.h"
00360     use_generic( plus,      "+" );
00361     use_generic( minus,     "-" );
00362     use_generic( times,     "*" );
00363     use_generic( div,       "/" );
00364     use_generic( uminus,    "u-");
00365     use_generic( eq,        "=");
00366     use_generic( ne,        "!=");
00367     use_generic( random,    "random");
00368     use_generic( length,    "length");
00369     use_generic( distance,  "distance");
00370     use_generic( normalize, "normalize");
00371     use_generic( copy,      "copy");
00372     use_generic( get,       "get");
00373     use_generic( put,       "put");
00374 
00375     /* built-in property accessors */
00376 
00377     def_property ( x );
00378     def_property ( y );
00379 
00380     Point2      to_point2() { return p; }
00381     ScripterExport void     to_fpvalue(FPValue& v);
00382 
00383     // scene I/O 
00384     IOResult Save(ISave* isave);
00385     static Value* Load(ILoad* iload, USHORT chunkID, ValueLoader* vload);
00386 };
00387 
00388 // The following function has been added
00389 // in 3ds max 4.2.  If your plugin utilizes this new
00390 // mechanism, be sure that your clients are aware that they
00391 // must run your plugin with 3ds max version 4.2 or higher.
00392 
00393 inline  IPoint2 to_ipoint2(Value* val) { 
00394             Point2 p = val->to_point2(); 
00395             return IPoint2((int)p.x, (int)p.y); }
00396 
00397 // End of 3ds max 4.2 Extension
00398 
00399 /* ------------------------ Point4Value ------------------------------ */
00400 
00401 applyable_class_debug_ok (Point4Value)
00402 
00403 class Point4Value : public Value
00404 {
00405 public:
00406     Point4      p;
00407 
00408     ENABLE_STACK_ALLOCATE(Point4Value);
00409 
00410     ScripterExport Point4Value(Point4 init_point);
00411     ScripterExport Point4Value(float x, float y, float z, float w);
00412     ScripterExport Point4Value(Value* x, Value* y, Value* z, Value* w);
00413 
00414     classof_methods(Point4Value, Value);
00415     void        collect() { delete this; }
00416     ScripterExport void     sprin1(CharStream* s);
00417 #   define      is_point4(v) ((DbgVerify(!is_sourcepositionwrapper(v)), (v))->tag == class_tag(Point4Value))
00418 
00419     static Value* make(Value**arg_list, int count);
00420 
00421     /* operations */
00422 
00423 #include "..\macros\define_implementations.h"
00424 #   include "..\protocols\vector.inl"
00425     use_generic  ( coerce,  "coerce");
00426     use_generic  ( copy,    "copy");
00427     use_generic  ( get,     "get");
00428     use_generic  ( put,     "put");
00429 
00430     /* built-in property accessors */
00431 
00432     def_property ( x );
00433     def_property ( y );
00434     def_property ( z );
00435     def_property ( w );
00436 
00437     Point4      to_point4() { return p; }
00438     Point3      to_point3() { return Point3 (p.x, p.y, p.z); }
00439     AColor      to_acolor() { return AColor (p.x, p.y, p.z, p.w); }
00440     Point2      to_point2() { return Point2 (p.x, p.y); }
00441     ScripterExport void     to_fpvalue(FPValue& v);
00442     COLORREF    to_colorref() { return RGB((int)(p.x*255.f), (int)(p.y*255.f), (int)(p.z*255.f)); }
00443 
00444     // scene I/O 
00445     IOResult Save(ISave* isave);
00446     static Value* Load(ILoad* iload, USHORT chunkID, ValueLoader* vload);
00447 };
00448 
00449 
00450 // The following class has been added
00451 // in 3ds max 4.2.  If your plugin utilizes this new
00452 // mechanism, be sure that your clients are aware that they
00453 // must run your plugin with 3ds max version 4.2 or higher.
00454 
00455 // Wraps Box2 in SDK which is a sub-class of the RECT structure in windows
00456 applyable_class_debug_ok (Box2Value)
00457 
00458 class Box2Value : public Value
00459 {
00460 
00461 public:
00462     Box2 b;
00463                         
00464     ScripterExport  Box2Value();
00465     ScripterExport  Box2Value(Box2 box);    
00466     ScripterExport  Box2Value(RECT rect);
00467     ScripterExport  Box2Value(IPoint2 ul, IPoint2 lr);
00468     ScripterExport  Box2Value(int x, int y, int w, int h);
00469     ScripterExport  Box2Value(Value* x, Value* y, Value* w, Value* h);
00470 
00471                     classof_methods(Box2Value, Value);
00472     void            collect() { delete this; }
00473     ScripterExport  void    sprin1(CharStream* s);  
00474 #   define          is_box2(v) ((DbgVerify(!is_sourcepositionwrapper(v)), (v))->tag == class_tag(Box2Value))
00475     
00476     static Value*   make(Value**arg_list, int count);
00477 
00478     /* operations */
00479 
00480 #include "..\macros\define_implementations.h"   
00481 #   include "..\protocols\box.inl"
00482     
00483     /* built-in property accessors */
00484 
00485     def_property ( x );
00486     def_property ( y );
00487     def_property ( w );
00488     def_property ( h );
00489     def_property ( left );
00490     def_property ( top );
00491     def_property ( right );
00492     def_property ( bottom );
00493     def_property ( center );
00494 
00495     Box2&       to_box2() { return b; }
00496 };
00497 
00498 #pragma warning(pop)
00499 // End of 3ds max 4.2 Extension
00500