[121] | 1 | // This file is a part of the Framsticks GDK. |
---|
| 2 | // Copyright (C) 2002-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
| 3 | // Refer to http://www.framsticks.com/ for further information. |
---|
| 4 | |
---|
[109] | 5 | #include <frams/util/3d.h> |
---|
| 6 | #include "3dobject.h" |
---|
| 7 | #include <frams/param/param.h> |
---|
| 8 | #include "collectionobj.h" |
---|
| 9 | |
---|
| 10 | ParamEntry* Pt3D_Ext::getStaticParamtab() |
---|
| 11 | { |
---|
| 12 | #define FIELDSTRUCT Pt3D_Ext |
---|
| 13 | static ParamEntry paramtab[]= |
---|
| 14 | { |
---|
| 15 | {"XYZ",1,18,"XYZ","3D vector"}, |
---|
| 16 | |
---|
| 17 | {"x",0,PARAM_NOSTATIC,"x","f",FIELD(p.x),}, |
---|
| 18 | {"y",0,PARAM_NOSTATIC,"y","f",FIELD(p.y),}, |
---|
| 19 | {"z",0,PARAM_NOSTATIC,"z","f",FIELD(p.z),}, |
---|
| 20 | {"new",0,0,"create new XYZ object","p oXYZ(f x,f y,f z)",PROCEDURE(p_new),"3D vectors objects can be also created using the (x,y,z) notation, i.e. var v=(1,2,3) is the same as var v=XYZ.new(1,2,3);",}, |
---|
| 21 | {"newFromVector",0,0,"create new XYZ object","p oXYZ(oVector)",PROCEDURE(p_newFromVector),"used for deserialization"}, |
---|
| 22 | {"clone",0,PARAM_NOSTATIC,"create new XYZ object copying the coordinates","p oXYZ()",PROCEDURE(p_clone),"Note: copying object references does not create new objects. Use clone() if a new object is needed.\n\nExample:\nvar o1=(1,2,3), o2=o1, o3=o1.clone();\no1.y=9999;\n//o2 is now (1,9999,3) but o3 is still (1,2,3)",}, |
---|
| 23 | {"set",0,PARAM_NOSTATIC,"set (copy) coordinates from another XYZ object","p(oXYZ)",PROCEDURE(p_set),}, |
---|
| 24 | {"set3",0,PARAM_NOSTATIC,"set individual 3 coordinates","p(f x,f y,f z)",PROCEDURE(p_set3),}, |
---|
| 25 | {"add",0,PARAM_NOSTATIC,"add","p(oXYZ)",PROCEDURE(p_addvec),"Note: it does not return a new object, just modifies the existing one"}, |
---|
| 26 | {"sub",0,PARAM_NOSTATIC,"subtract","p(oXYZ)",PROCEDURE(p_subvec),"Note: it does not return a new object, just modifies the existing one"}, |
---|
| 27 | {"scale",0,PARAM_NOSTATIC,"multiply by scalar","p(f)",PROCEDURE(p_scale),}, |
---|
| 28 | {"length",0,PARAM_READONLY+PARAM_NOSTATIC,"length","f",GETONLY(length),}, |
---|
| 29 | {"normalize",0,PARAM_NOSTATIC,"normalize","p()",PROCEDURE(p_normalize),"scales the vector length to 1.0"}, |
---|
| 30 | {"toString",0,PARAM_READONLY+PARAM_NOSTATIC,"textual form","s",GETONLY(toString),}, |
---|
| 31 | {"toVector",0,PARAM_READONLY+PARAM_NOSTATIC,"vector of [x,y,z]","oVector",GETONLY(toVector),}, |
---|
| 32 | {"rotate",0,PARAM_NOSTATIC,"rotate using Orient object","p(oOrient)",PROCEDURE(p_rotate),}, |
---|
| 33 | {"revRotate",0,PARAM_NOSTATIC,"reverse rotate using Orient object","p(oOrient)",PROCEDURE(p_revrotate),}, |
---|
| 34 | {"get",0,PARAM_NOSTATIC,"get one of coordinates","p f(d index)",PROCEDURE(p_get),"this function makes the XYZ objects \"indexable\" (so you can use [] for accessing subsequent fields, like in Vector)",}, |
---|
| 35 | {0,0,0,}, |
---|
| 36 | }; |
---|
| 37 | #undef FIELDSTRUCT |
---|
| 38 | return paramtab; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | void Pt3D_Ext::p_new(ExtValue *args,ExtValue *ret) |
---|
| 42 | { |
---|
| 43 | *ret=makeDynamicObject(new Pt3D_Ext(args[2].getDouble(),args[1].getDouble(),args[0].getDouble())); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | static double doubleFromVec(VectorObject *vec,int i) |
---|
| 47 | { |
---|
| 48 | if (i>=vec->data.size()) return 0; |
---|
| 49 | ExtValue *v=(ExtValue*)vec->data.get(i); |
---|
| 50 | if (v) |
---|
| 51 | return v->getDouble(); |
---|
| 52 | return 0; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | static Pt3D pt3DFromVec(VectorObject* v,int offset=0) |
---|
| 56 | { |
---|
| 57 | return Pt3D(doubleFromVec(v,offset),doubleFromVec(v,offset+1),doubleFromVec(v,offset+2)); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | void Pt3D_Ext::p_newFromVector(ExtValue *args,ExtValue *ret) |
---|
| 61 | { |
---|
| 62 | VectorObject *vec=VectorObject::fromObject(args->getObject()); |
---|
| 63 | if (vec) |
---|
| 64 | *ret=makeDynamicObject(new Pt3D_Ext(pt3DFromVec(vec))); |
---|
| 65 | else |
---|
| 66 | ret->setEmpty(); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | void Pt3D_Ext::p_clone(ExtValue *args,ExtValue *ret) |
---|
| 70 | { |
---|
| 71 | *ret=makeDynamicObject(new Pt3D_Ext(p.x,p.y,p.z)); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | void Pt3D_Ext::p_set3(ExtValue *args,ExtValue *ret) |
---|
| 75 | { |
---|
| 76 | p.x=args[2].getDouble(); |
---|
| 77 | p.y=args[1].getDouble(); |
---|
| 78 | p.z=args[0].getDouble(); |
---|
| 79 | ret->setEmpty(); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | void Pt3D_Ext::p_set(ExtValue *args,ExtValue *ret) |
---|
| 83 | { |
---|
| 84 | Pt3D_Ext *other=fromObject(args[0]); |
---|
| 85 | if (other) |
---|
| 86 | p=other->p; |
---|
| 87 | ret->setEmpty(); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | void Pt3D_Ext::get_length(ExtValue *ret) |
---|
| 91 | { |
---|
| 92 | ret->setDouble(p.length()); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | void Pt3D_Ext::get_toString(ExtValue *ret) |
---|
| 96 | { |
---|
| 97 | SString s="("; |
---|
| 98 | ExtValue v; |
---|
| 99 | v.setDouble(p.x); s+=v.getString(); |
---|
| 100 | s+=","; |
---|
| 101 | v.setDouble(p.y); s+=v.getString(); |
---|
| 102 | s+=","; |
---|
| 103 | v.setDouble(p.z); s+=v.getString(); |
---|
| 104 | s+=")"; |
---|
| 105 | ret->setString(s); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | static void add3Coords(VectorObject* vec,const Pt3D& p) |
---|
| 109 | { |
---|
| 110 | vec->data+=new ExtValue(p.x); |
---|
| 111 | vec->data+=new ExtValue(p.y); |
---|
| 112 | vec->data+=new ExtValue(p.z); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | void Pt3D_Ext::get_toVector(ExtValue *ret) |
---|
| 116 | { |
---|
| 117 | VectorObject *vec=new VectorObject; |
---|
| 118 | add3Coords(vec,p); |
---|
| 119 | ret->setObject(ExtObject(&VectorObject::par,vec)); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | void Pt3D_Ext::p_addvec(ExtValue *args,ExtValue *ret) |
---|
| 123 | { |
---|
| 124 | Pt3D_Ext *other=fromObject(args[0]); |
---|
| 125 | if (other) |
---|
| 126 | p+=other->p; |
---|
| 127 | ret->setEmpty(); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | void Pt3D_Ext::p_subvec(ExtValue *args,ExtValue *ret) |
---|
| 131 | { |
---|
| 132 | Pt3D_Ext *other=fromObject(args[0]); |
---|
| 133 | if (other) |
---|
| 134 | p-=other->p; |
---|
| 135 | ret->setEmpty(); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | void Pt3D_Ext::p_scale(ExtValue *args,ExtValue *ret) |
---|
| 139 | { |
---|
| 140 | double d=args[0].getDouble(); |
---|
| 141 | p.x*=d; p.y*=d; p.z*=d; |
---|
| 142 | ret->setEmpty(); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | void Pt3D_Ext::p_normalize(ExtValue *args,ExtValue *ret) |
---|
| 146 | { |
---|
| 147 | p.normalize(); |
---|
| 148 | ret->setEmpty(); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | void Pt3D_Ext::p_rotate(ExtValue *args,ExtValue *ret) |
---|
| 152 | { |
---|
| 153 | Orient_Ext *o=Orient_Ext::fromObject(args[0]); |
---|
| 154 | if (o) |
---|
| 155 | { |
---|
| 156 | Pt3D tmp=p; |
---|
| 157 | o->o.transform(p,tmp); |
---|
| 158 | } |
---|
| 159 | ret->setEmpty(); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | void Pt3D_Ext::p_revrotate(ExtValue *args,ExtValue *ret) |
---|
| 163 | { |
---|
| 164 | Orient_Ext *o=Orient_Ext::fromObject(args[0]); |
---|
| 165 | if (o) |
---|
| 166 | { |
---|
| 167 | Pt3D tmp=p; |
---|
| 168 | o->o.revTransform(p,tmp); |
---|
| 169 | } |
---|
| 170 | ret->setEmpty(); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | void Pt3D_Ext::p_get(ExtValue *args,ExtValue *ret) |
---|
| 174 | { |
---|
| 175 | int index=args->getInt(); |
---|
| 176 | if ((index<0)||(index>2)) |
---|
| 177 | ret->setEmpty(); |
---|
| 178 | else |
---|
| 179 | ret->setDouble((&p.x)[index]); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | Param& Pt3D_Ext::getStaticParam() |
---|
| 183 | { |
---|
| 184 | #ifdef __CODEGUARD__ |
---|
| 185 | static Pt3D_Ext static_pt3dobj; |
---|
| 186 | static Param static_pt3dparam(getStaticParamtab(),&static_pt3dobj); |
---|
| 187 | #else |
---|
| 188 | static Param static_pt3dparam(getStaticParamtab()); |
---|
| 189 | #endif |
---|
| 190 | return static_pt3dparam; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | Pt3D_Ext* Pt3D_Ext::fromObject(const ExtValue& v) |
---|
| 194 | { |
---|
| 195 | if (v.type!=TObj) return 0; |
---|
| 196 | const ExtObject& o=v.getObject(); |
---|
| 197 | return (Pt3D_Ext*)o.getTarget(getStaticParam().getName()); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | ParamInterface* Pt3D_Ext::getInterface() {return &getStaticParam();} |
---|
| 201 | |
---|
| 202 | ExtObject Pt3D_Ext::makeStaticObject(Pt3D* p) |
---|
| 203 | {return ExtObject(&getStaticParam(),((char*)p)+(((char*)&p->x)-((char*)&((Pt3D_Ext*)p)->p.x)));} |
---|
| 204 | |
---|
| 205 | ExtObject Pt3D_Ext::makeDynamicObject(Pt3D_Ext* p) |
---|
| 206 | {return ExtObject(&getStaticParam(),p);} |
---|
| 207 | |
---|
| 208 | ////////////////////////////////////// |
---|
| 209 | |
---|
| 210 | ParamEntry* Orient_Ext::getStaticParamtab() |
---|
| 211 | { |
---|
| 212 | #define FIELDSTRUCT Orient_Ext |
---|
| 213 | static ParamEntry paramtab[]= |
---|
| 214 | { |
---|
| 215 | {"Orient",1,27,"Orient","3D orientation, stored as 3x3 matrix."}, |
---|
| 216 | |
---|
| 217 | {"xx",1,PARAM_NOSTATIC,"orientation.x.x","f",FIELD(o.x.x),}, |
---|
| 218 | {"xy",1,PARAM_NOSTATIC,"orientation.x.y","f",FIELD(o.x.y),}, |
---|
| 219 | {"xz",1,PARAM_NOSTATIC,"orientation.x.z","f",FIELD(o.x.z),}, |
---|
| 220 | {"yx",1,PARAM_NOSTATIC,"orientation.y.x","f",FIELD(o.y.x),}, |
---|
| 221 | {"yy",1,PARAM_NOSTATIC,"orientation.y.y","f",FIELD(o.y.y),}, |
---|
| 222 | {"yz",1,PARAM_NOSTATIC,"orientation.y.z","f",FIELD(o.y.z),}, |
---|
| 223 | {"zx",1,PARAM_NOSTATIC,"orientation.z.x","f",FIELD(o.z.x),}, |
---|
| 224 | {"zy",1,PARAM_NOSTATIC,"orientation.z.y","f",FIELD(o.z.y),}, |
---|
| 225 | {"zz",1,PARAM_NOSTATIC,"orientation.z.z","f",FIELD(o.z.z),}, |
---|
| 226 | |
---|
[127] | 227 | {"x",0,PARAM_NOSTATIC+PARAM_READONLY,"x vector","oXYZ",GETONLY(x),}, |
---|
| 228 | {"y",0,PARAM_NOSTATIC+PARAM_READONLY,"y vector","oXYZ",GETONLY(y),}, |
---|
| 229 | {"z",0,PARAM_NOSTATIC+PARAM_READONLY,"z vector","oXYZ",GETONLY(z),}, |
---|
[109] | 230 | |
---|
| 231 | {"new",0,0,"create new Orient object","p oOrient()",PROCEDURE(p_new),}, |
---|
| 232 | {"newFromVector",0,0,"create new Orient object","p oOrient(oVector)",PROCEDURE(p_newFromVector),}, |
---|
| 233 | {"toVector",0,PARAM_READONLY+PARAM_NOSTATIC,"vector representation","oVector",GETONLY(toVector),"for serialization"}, |
---|
| 234 | {"clone",0,PARAM_NOSTATIC,"create new Orient object","p oOrient()",PROCEDURE(p_clone),}, |
---|
| 235 | {"set",0,PARAM_NOSTATIC,"copy from another Orient object","p(oOrient)",PROCEDURE(p_set),}, |
---|
| 236 | {"reset",0,PARAM_NOSTATIC,"set identity matrix","p()",PROCEDURE(p_reset),}, |
---|
| 237 | {"rotate3",0,PARAM_NOSTATIC,"rotate around 3 axes","p(f x,f y,f z)",PROCEDURE(p_rotate3),}, |
---|
| 238 | {"rotate",0,PARAM_NOSTATIC,"rotate using Orient object","p(oOrient)",PROCEDURE(p_rotate),}, |
---|
| 239 | {"revRotate",0,PARAM_NOSTATIC,"reverse rotate using Orient object","p(oOrient)",PROCEDURE(p_revrotate),}, |
---|
| 240 | {"lookAt",0,PARAM_NOSTATIC,"calculate rotation from 2 vectors","p(oXYZ direction,oXYZ up)",PROCEDURE(p_lookat),}, |
---|
| 241 | {"normalize",0,PARAM_NOSTATIC,"normalize","p()",PROCEDURE(p_normalize),}, |
---|
| 242 | {"between2",0,PARAM_NOSTATIC,"interpolate orientation","p(oOrient,oOrient,f amount)",PROCEDURE(p_between2),"The calling Orient receives the orientation interpolated from 2 input orientations.\nExample:\n" |
---|
| 243 | "var o1=Orient.new(), o2=Orient.new(), o3=Orient.new();\n" |
---|
| 244 | "o2.rotate3(0,Math.pi/2,0);\n" |
---|
| 245 | "o3.between2(o1,o2,0); // o3 equals o2\n" |
---|
| 246 | "o3.between2(o1,o2,1); // o3 equals o1\n" |
---|
| 247 | "o3.between2(o1,o2,0.5); // o3 is halfway between o1 and o2\n"}, |
---|
| 248 | {"betweenOV",0,PARAM_NOSTATIC,"interpolate orientation","p(oOrient,oXYZ,f amount)",PROCEDURE(p_betweenOV),"Like between2(), but the second Orient is composed of the supplied XYZ vector (X component) and Y Z vectors from the calling object.\n" |
---|
| 249 | "Example:\n" |
---|
| 250 | "var o=Orient.new();\n" |
---|
| 251 | "o.betweenOV(o,(0,1,0),1); //no change, o remains 100 010 001\n" |
---|
| 252 | "o.betweenOV(o,(0,1,0),0.9); //o is slightly rotated towards (0,1,0)\n" |
---|
| 253 | "o.betweenOV(o,(0,1,0),0); //o is completely transformed, o.x=(0,1,0)\n" |
---|
| 254 | }, |
---|
[156] | 255 | {"localToWorld",0,PARAM_NOSTATIC,"transform coordinates","p oXYZ(oXYZ point,oXYZ center)",PROCEDURE(p_localToWorld),}, |
---|
| 256 | {"worldToLocal",0,PARAM_NOSTATIC,"transform coordinates","p oXYZ(oXYZ point,oXYZ center)",PROCEDURE(p_worldToLocal),}, |
---|
[109] | 257 | {0,0,0,}, |
---|
| 258 | }; |
---|
| 259 | #undef FIELDSTRUCT |
---|
| 260 | return paramtab; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | void Orient_Ext::p_new(ExtValue *args,ExtValue *ret) |
---|
| 264 | { |
---|
| 265 | *ret=makeDynamicObject(new Orient_Ext()); |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | void Orient_Ext::p_newFromVector(ExtValue *args,ExtValue *ret) |
---|
| 269 | { |
---|
| 270 | VectorObject *vec=VectorObject::fromObject(args->getObject()); |
---|
| 271 | if (vec) |
---|
| 272 | *ret=makeDynamicObject(new Orient_Ext(Orient(pt3DFromVec(vec,0),pt3DFromVec(vec,3),pt3DFromVec(vec,6)))); |
---|
| 273 | else |
---|
| 274 | ret->setEmpty(); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | void Orient_Ext::get_toVector(ExtValue *ret) |
---|
| 278 | { |
---|
| 279 | VectorObject *vec=new VectorObject; |
---|
| 280 | add3Coords(vec,o.x); |
---|
| 281 | add3Coords(vec,o.y); |
---|
| 282 | add3Coords(vec,o.z); |
---|
| 283 | ret->setObject(ExtObject(&VectorObject::par,vec)); |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | void Orient_Ext::p_clone(ExtValue *args,ExtValue *ret) |
---|
| 287 | { |
---|
| 288 | *ret=makeDynamicObject(new Orient_Ext(o)); |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | void Orient_Ext::p_set(ExtValue *args,ExtValue *ret) |
---|
| 292 | { |
---|
| 293 | Orient_Ext *other=fromObject(args[0]); |
---|
| 294 | if (other) |
---|
| 295 | o=other->o; |
---|
| 296 | ret->setEmpty(); |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | void Orient_Ext::p_reset(ExtValue *args,ExtValue *ret) |
---|
| 300 | { |
---|
| 301 | o=Orient_1; |
---|
| 302 | ret->setEmpty(); |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | void Orient_Ext::get_x(PARAMGETARGS) |
---|
| 306 | { |
---|
| 307 | *ret=Pt3D_Ext::makeStaticObject(&o.x); |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | void Orient_Ext::get_y(PARAMGETARGS) |
---|
| 311 | { |
---|
| 312 | *ret=Pt3D_Ext::makeStaticObject(&o.y); |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | void Orient_Ext::get_z(PARAMGETARGS) |
---|
| 316 | { |
---|
| 317 | *ret=Pt3D_Ext::makeStaticObject(&o.z); |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | void Orient_Ext::p_lookat(ExtValue *args,ExtValue *ret) |
---|
| 321 | { |
---|
| 322 | Pt3D_Ext *dir=Pt3D_Ext::fromObject(args[1]),*up=Pt3D_Ext::fromObject(args[0]); |
---|
| 323 | if (dir&&up) |
---|
| 324 | o.lookAt(dir->p,up->p); |
---|
| 325 | ret->setEmpty(); |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | void Orient_Ext::p_rotate3(ExtValue *args,ExtValue *ret) |
---|
| 329 | { |
---|
| 330 | Pt3D p(args[2].getDouble(),args[1].getDouble(),args[0].getDouble()); |
---|
| 331 | o.rotate(p); |
---|
| 332 | ret->setEmpty(); |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | void Orient_Ext::p_rotate(ExtValue *args,ExtValue *ret) |
---|
| 336 | { |
---|
| 337 | Orient_Ext *obj=Orient_Ext::fromObject(args[0]); |
---|
| 338 | if (!obj) |
---|
| 339 | { |
---|
| 340 | Orient tmp=o; |
---|
| 341 | obj->o.transform(o,tmp); |
---|
| 342 | } |
---|
| 343 | ret->setEmpty(); |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | void Orient_Ext::p_revrotate(ExtValue *args,ExtValue *ret) |
---|
| 347 | { |
---|
| 348 | Orient_Ext *obj=Orient_Ext::fromObject(args[0]); |
---|
| 349 | if (obj) |
---|
| 350 | { |
---|
| 351 | Orient tmp=o; |
---|
| 352 | obj->o.revTransform(o,tmp); |
---|
| 353 | } |
---|
| 354 | ret->setEmpty(); |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | void Orient_Ext::p_normalize(ExtValue *args,ExtValue *ret) |
---|
| 358 | { |
---|
| 359 | o.normalize(); |
---|
| 360 | ret->setEmpty(); |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | void Orient_Ext::p_between2(ExtValue *args,ExtValue *ret) |
---|
| 364 | { |
---|
| 365 | Orient_Ext *o1=Orient_Ext::fromObject(args[2]); |
---|
| 366 | Orient_Ext *o2=Orient_Ext::fromObject(args[1]); |
---|
| 367 | if (o1&&o2) |
---|
| 368 | { |
---|
| 369 | double q1=args[0].getDouble(),q2=1.0-q1; |
---|
| 370 | o.x.x=q1*o1->o.x.x+q2*o2->o.x.x; |
---|
| 371 | o.x.y=q1*o1->o.x.y+q2*o2->o.x.y; |
---|
| 372 | o.x.z=q1*o1->o.x.z+q2*o2->o.x.z; |
---|
| 373 | o.y.x=q1*o1->o.y.x+q2*o2->o.y.x; |
---|
| 374 | o.y.y=q1*o1->o.y.y+q2*o2->o.y.y; |
---|
| 375 | o.y.z=q1*o1->o.y.z+q2*o2->o.y.z; |
---|
| 376 | o.z.x=q1*o1->o.z.x+q2*o2->o.z.x; |
---|
| 377 | o.z.y=q1*o1->o.z.y+q2*o2->o.z.y; |
---|
| 378 | o.z.z=q1*o1->o.z.z+q2*o2->o.z.z; |
---|
| 379 | o.normalize(); |
---|
| 380 | } |
---|
| 381 | ret->setEmpty(); |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | void Orient_Ext::p_betweenOV(ExtValue *args,ExtValue *ret) |
---|
| 385 | { |
---|
| 386 | Orient_Ext *o1=Orient_Ext::fromObject(args[2]); |
---|
| 387 | Pt3D_Ext *p2=Pt3D_Ext::fromObject(args[1]); |
---|
| 388 | if (o1&&p2) |
---|
| 389 | { |
---|
| 390 | double q1=args[0].getDouble(),q2=1.0-q1; |
---|
| 391 | o.x.x=q1*o1->o.x.x+q2*p2->p.x; |
---|
| 392 | o.x.y=q1*o1->o.x.y+q2*p2->p.y; |
---|
| 393 | o.x.z=q1*o1->o.x.z+q2*p2->p.z; |
---|
| 394 | o.normalize(); |
---|
| 395 | } |
---|
| 396 | ret->setEmpty(); |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | void Orient_Ext::p_localToWorld(ExtValue *args,ExtValue *ret) |
---|
| 400 | { |
---|
| 401 | Pt3D_Ext *center,*point; |
---|
| 402 | point=Pt3D_Ext::fromObject(args[1]); |
---|
| 403 | center=Pt3D_Ext::fromObject(args[0]); |
---|
| 404 | if (center && point) |
---|
| 405 | { |
---|
| 406 | Pt3D d; |
---|
| 407 | Pt3D src=point->p; |
---|
| 408 | o.transform(d,src); |
---|
| 409 | d+=center->p; |
---|
| 410 | *ret=Pt3D_Ext::makeDynamicObject(new Pt3D_Ext(d)); |
---|
| 411 | } |
---|
| 412 | else |
---|
| 413 | ret->setEmpty(); |
---|
| 414 | } |
---|
| 415 | |
---|
| 416 | void Orient_Ext::p_worldToLocal(ExtValue *args,ExtValue *ret) |
---|
| 417 | { |
---|
| 418 | Pt3D_Ext *center,*point; |
---|
| 419 | point=Pt3D_Ext::fromObject(args[1]); |
---|
| 420 | center=Pt3D_Ext::fromObject(args[0]); |
---|
| 421 | if (center && point) |
---|
| 422 | { |
---|
| 423 | Pt3D d; |
---|
| 424 | Pt3D src=point->p; |
---|
| 425 | d-=center->p; |
---|
| 426 | o.revTransform(d,src); |
---|
| 427 | *ret=Pt3D_Ext::makeDynamicObject(new Pt3D_Ext(d)); |
---|
| 428 | } |
---|
| 429 | else |
---|
| 430 | ret->setEmpty(); |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | Param& Orient_Ext::getStaticParam() |
---|
| 434 | { |
---|
| 435 | #ifdef __CODEGUARD__ |
---|
| 436 | static Orient_Ext static_orientobj; |
---|
| 437 | static Param static_orientparam(getStaticParamtab(),&static_orientobj); |
---|
| 438 | #else |
---|
| 439 | static Param static_orientparam(getStaticParamtab()); |
---|
| 440 | #endif |
---|
| 441 | return static_orientparam; |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | Orient_Ext* Orient_Ext::fromObject(const ExtValue& v) |
---|
| 445 | { |
---|
| 446 | if (v.type!=TObj) return 0; |
---|
| 447 | const ExtObject& o=v.getObject(); |
---|
| 448 | return (Orient_Ext*)o.getTarget(getStaticParam().getName()); |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | ParamInterface* Orient_Ext::getInterface() {return &getStaticParam();} |
---|
| 452 | |
---|
| 453 | ExtObject Orient_Ext::makeStaticObject(Orient* o) |
---|
| 454 | {return ExtObject(&getStaticParam(),((char*)o)+(((char*)&o->x)-((char*)&((Orient_Ext*)o)->o.x)));} |
---|
| 455 | |
---|
| 456 | ExtObject Orient_Ext::makeDynamicObject(Orient_Ext* p) |
---|
| 457 | {return ExtObject(&getStaticParam(),p);} |
---|
| 458 | |
---|
| 459 | ParamEntry* ReferenceObj::getStaticParamtab() |
---|
| 460 | { |
---|
| 461 | #define FIELDSTRUCT ReferenceObj |
---|
| 462 | static ParamEntry paramtab[]= |
---|
| 463 | { |
---|
| 464 | {"Ref",1,5,"Ref","Reference objects. Useful for returning things from functions.\n\nExample:\nvar x=111;\nsquare(&x);// '&' creates the Reference object\nSimulator.print(x);//x is now 12321\n\nfunction square(r)\n{r.value=r.value*r.value;}\n//square receives the Reference objects and changes its 'value' field"}, |
---|
| 465 | |
---|
| 466 | {"value",0,PARAM_NOSTATIC,"value","x",GETSET(value),}, |
---|
| 467 | {"newS",0,0,"create new reference","p",PROCEDURE(p_newS),"(for internal use only) use &variablename to create Ref objects.",}, |
---|
| 468 | {"newO",0,0,"create new reference","p",PROCEDURE(p_newO),"(for internal use only) use &variablename to create Ref objects.",}, |
---|
| 469 | {"copyFrom",0,0,"copy the reference","p(oRef)",PROCEDURE(p_copyFrom),"make the reference point to the same target,"}, |
---|
| 470 | {"toString",0,PARAM_READONLY+PARAM_NOSTATIC,"textual form","s",GETONLY(toString),}, |
---|
| 471 | {0,0,0,}, |
---|
| 472 | }; |
---|
| 473 | #undef FIELDSTRUCT |
---|
| 474 | return paramtab; |
---|
| 475 | } |
---|
| 476 | |
---|
| 477 | Param& ReferenceObj::getStaticParam() |
---|
| 478 | { |
---|
| 479 | #ifdef __CODEGUARD__ |
---|
| 480 | static ReferenceObj static_referenceobj; |
---|
| 481 | static Param static_refobjectparam(getStaticParamtab(),&static_referenceobj); |
---|
| 482 | #else |
---|
| 483 | static Param static_refobjectparam(getStaticParamtab()); |
---|
| 484 | #endif |
---|
| 485 | return static_refobjectparam; |
---|
| 486 | } |
---|
| 487 | |
---|
| 488 | void ReferenceObj::p_newS(ExtValue *args,ExtValue *ret) |
---|
| 489 | { |
---|
| 490 | *ret=makeDynamicObject(new ReferenceObj((ExtValue*)args->getInt())); |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | void ReferenceObj::p_newO(ExtValue *args,ExtValue *ret) |
---|
| 494 | { |
---|
| 495 | if (args[0].type==TInt) |
---|
| 496 | *ret=makeDynamicObject(new ReferenceObj(args[1].getObject(),args[0].getInt())); |
---|
| 497 | else |
---|
| 498 | *ret=makeDynamicObject(new ReferenceObj(args[1].getObject(),args[0].getString())); |
---|
| 499 | } |
---|
| 500 | |
---|
| 501 | void ReferenceObj::p_copyFrom(ExtValue *args,ExtValue *ret) |
---|
| 502 | { |
---|
| 503 | ReferenceObj* other=fromObject(args[0]); |
---|
| 504 | if (other) |
---|
| 505 | { |
---|
| 506 | value=other->value; |
---|
| 507 | obj=other->obj; |
---|
| 508 | prop=other->prop; |
---|
| 509 | } |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | void ReferenceObj::get_toString(ExtValue *ret) |
---|
| 513 | { |
---|
| 514 | SString s="("; |
---|
| 515 | static SListTempl<ReferenceObj*> trace; |
---|
| 516 | if (trace.find(this)>=0) |
---|
| 517 | s+="..."; |
---|
| 518 | else |
---|
| 519 | { |
---|
| 520 | trace+=this; |
---|
| 521 | if (value) |
---|
| 522 | s+=value->getString(); |
---|
| 523 | else |
---|
| 524 | { |
---|
| 525 | ExtValue v; |
---|
| 526 | Param tmp_param; |
---|
| 527 | ParamInterface *pi=obj.getParamInterface(tmp_param); |
---|
| 528 | pi->get(prop,v); |
---|
| 529 | s+=v.getString(); |
---|
| 530 | } |
---|
| 531 | trace-=this; |
---|
| 532 | } |
---|
| 533 | s+=")"; |
---|
| 534 | ret->setString(s); |
---|
| 535 | } |
---|
| 536 | |
---|
| 537 | void ReferenceObj::get_value(ExtValue *ret) |
---|
| 538 | { |
---|
| 539 | if (value) |
---|
| 540 | *ret=*value; |
---|
| 541 | else |
---|
| 542 | { |
---|
| 543 | Param tmp_param; |
---|
| 544 | ParamInterface *pi=obj.getParamInterface(tmp_param); |
---|
| 545 | pi->get(prop,*ret); |
---|
| 546 | } |
---|
| 547 | } |
---|
| 548 | |
---|
| 549 | int ReferenceObj::set_value(const ExtValue *val) |
---|
| 550 | { |
---|
| 551 | if (value) |
---|
| 552 | *value=*val; |
---|
| 553 | else |
---|
| 554 | { |
---|
| 555 | Param tmp_param; |
---|
| 556 | ParamInterface *pi=obj.getParamInterface(tmp_param); |
---|
| 557 | pi->set(prop,*val); |
---|
| 558 | } |
---|
| 559 | return PSET_CHANGED; |
---|
| 560 | } |
---|
| 561 | |
---|
| 562 | ReferenceObj::ReferenceObj(const ExtObject &o,const SString &p) |
---|
| 563 | :value(0),obj(o) |
---|
| 564 | { |
---|
| 565 | Param tmp_param; |
---|
| 566 | ParamInterface *pi=obj.getParamInterface(tmp_param); |
---|
| 567 | prop=pi->findId(p); |
---|
| 568 | } |
---|
| 569 | |
---|
| 570 | ExtObject ReferenceObj::makeDynamicObject(ReferenceObj* r) |
---|
| 571 | {return ExtObject(&getStaticParam(),r);} |
---|
| 572 | |
---|
| 573 | ReferenceObj* ReferenceObj::fromObject(const ExtValue& v) |
---|
| 574 | { |
---|
| 575 | if (v.type!=TObj) return 0; |
---|
| 576 | const ExtObject& o=v.getObject(); |
---|
| 577 | return (ReferenceObj*)o.getTarget(getStaticParam().getName()); |
---|
| 578 | } |
---|