VPathEditor.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205
  1. //-----------------------------------------------------------------------------
  2. // Verve
  3. // Copyright (C) 2014 - Violent Tulip
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. #include "VPathEditor.h"
  24. #include "console/consoleTypes.h"
  25. #include "gfx/gfxDrawUtil.h"
  26. #include "gfx/primBuilder.h"
  27. #include "gui/worldEditor/worldEditor.h"
  28. #include "math/mathUtils.h"
  29. #include "sim/netConnection.h"
  30. //-----------------------------------------------------------------------------
  31. static F32 gProjectDistance = 2000.f;
  32. static F32 gSelectionDistance = 2.f;
  33. static ColorI gPathColor( 255, 255, 255 );
  34. static ColorI gPathColorSel( 0, 255, 255 );
  35. static ColorI gNodeLookAtPointColor( 255, 127, 39 );
  36. //-----------------------------------------------------------------------------
  37. // Implement the Edit Mode enum list.
  38. ImplementEnumType( VPathEditorMode, "" )
  39. { VPathEditor::k_Gizmo, "GIZMO" },
  40. { VPathEditor::k_AddNode, "ADDNODE" },
  41. { VPathEditor::k_DeleteNode, "DELETENODE" },
  42. EndImplementEnumType;
  43. //-----------------------------------------------------------------------------
  44. IMPLEMENT_CONOBJECT( VPathEditor );
  45. //-----------------------------------------------------------------------------
  46. VPathEditor::VPathEditor( void ) :
  47. mIsDirty( false ),
  48. mEditMode( k_Gizmo ),
  49. mEditWeight( false ),
  50. mEditWeightHandle( -1 )
  51. {
  52. // Void.
  53. }
  54. bool VPathEditor::onAdd( void )
  55. {
  56. if ( !Parent::onAdd() )
  57. {
  58. return false;
  59. }
  60. // Assign Gizmo Name.
  61. mGizmo->assignName( "VPathEditorGizmo" );
  62. return true;
  63. }
  64. bool VPathEditor::onWake( void )
  65. {
  66. // Clear Selection.
  67. updateSelection( NULL, -1 );
  68. // Return Parent Value.
  69. return Parent::onWake();
  70. }
  71. void VPathEditor::initPersistFields( void )
  72. {
  73. addField( "IsDirty", TypeBool, Offset( mIsDirty, VPathEditor ) );
  74. addField( "EditMode", TYPEID<EditMode>(), Offset( mEditMode, VPathEditor ) );
  75. Parent::initPersistFields();
  76. }
  77. //-----------------------------------------------------------------------------
  78. //
  79. // Gui Events
  80. //
  81. //-----------------------------------------------------------------------------
  82. void VPathEditor::on3DMouseDown( const Gui3DMouseEvent &pEvent )
  83. {
  84. // Using the Gizmo?
  85. if ( mEditMode != k_Gizmo )
  86. {
  87. // No, Quit Now.
  88. return;
  89. }
  90. // Gizmo Event.
  91. mGizmo->on3DMouseDown( pEvent );
  92. if ( isValidSelection() )
  93. {
  94. // Store Node Information.
  95. pushNodeEdit();
  96. switch( mGizmoProfile->mode )
  97. {
  98. case MoveMode:
  99. case RotateMode:
  100. {
  101. if ( mGizmo->getSelection() != Gizmo::None )
  102. {
  103. // Using Gizmo.
  104. return;
  105. }
  106. } break;
  107. case ScaleMode:
  108. {
  109. if ( isEditingWeight( pEvent ) )
  110. {
  111. // Editing Weights.
  112. return;
  113. }
  114. } break;
  115. }
  116. }
  117. else if ( mSelection.Path )
  118. {
  119. // Store Path Information.
  120. pushPathEdit();
  121. if ( mGizmo->getSelection() != Gizmo::None )
  122. {
  123. // Using Gizmo.
  124. return;
  125. }
  126. }
  127. // Update Selection.
  128. if ( !updateSelection( pEvent ) )
  129. {
  130. // Clear Selection.
  131. updateSelection( NULL, -1 );
  132. }
  133. }
  134. void VPathEditor::on3DMouseUp( const Gui3DMouseEvent &pEvent )
  135. {
  136. switch ( mEditMode )
  137. {
  138. case k_Gizmo :
  139. {
  140. // Gizmo Event.
  141. mGizmo->on3DMouseUp( pEvent );
  142. // Handle History Actions.
  143. popPathEdit();
  144. popNodeEdit();
  145. // Clear Editing.
  146. mEditWeight = false;
  147. } break;
  148. case k_AddNode :
  149. {
  150. if ( mSelection.Path != NULL )
  151. {
  152. // Add New!
  153. addNode( pEvent );
  154. // Dirty.
  155. mIsDirty = true;
  156. }
  157. } break;
  158. case k_DeleteNode :
  159. {
  160. // Update Selection.
  161. if ( updateSelection( pEvent ) )
  162. {
  163. if ( isValidSelection() )
  164. {
  165. // Delete Node.
  166. deleteNode( mSelection.Node );
  167. // Dirty.
  168. mIsDirty = true;
  169. }
  170. // Clear Node Selection.
  171. updateSelection( mSelection.Path, -1 );
  172. }
  173. } break;
  174. }
  175. }
  176. void VPathEditor::on3DMouseMove( const Gui3DMouseEvent &pEvent )
  177. {
  178. // Update?
  179. if ( mEditMode != k_Gizmo || !mSelection.Path )
  180. {
  181. return;
  182. }
  183. // Update Gizmo?
  184. if ( mSelection.Node == -1 || mGizmoProfile->mode != ScaleMode )
  185. {
  186. // Gizmo Event.
  187. mGizmo->on3DMouseMove( pEvent );
  188. }
  189. }
  190. void VPathEditor::on3DMouseDragged( const Gui3DMouseEvent &pEvent )
  191. {
  192. // Update?
  193. if ( mEditMode != k_Gizmo || !mSelection.Path )
  194. {
  195. return;
  196. }
  197. // Update Gizmo?
  198. if ( mSelection.Node == -1 || mGizmoProfile->mode != ScaleMode )
  199. {
  200. // Gizmo Event.
  201. mGizmo->on3DMouseDragged( pEvent );
  202. // Handle Gizmo?
  203. if ( mGizmo->getSelection() == Gizmo::None )
  204. {
  205. // Return.
  206. return;
  207. }
  208. }
  209. // Editing the Path?
  210. if ( mSelection.Node == -1 )
  211. {
  212. switch ( mGizmoProfile->mode )
  213. {
  214. case MoveMode :
  215. {
  216. // Fetch Node Position.
  217. const Point3F oldPosition = mSelection.Path->getPosition();
  218. // Determine New Position.
  219. const Point3F newPosition = ( oldPosition + mGizmo->getOffset() );
  220. // Apply New Position.
  221. setPathPosition( newPosition );
  222. // Dirty.
  223. mIsDirty = true;
  224. mPathEdit.Dirty = true;
  225. } break;
  226. /*
  227. case RotateMode :
  228. {
  229. // Rotation Delta.
  230. MatrixF deltaRotation( EulerF( mGizmo->getDeltaRot() ) );
  231. // Fetch Current Transform.
  232. MatrixF mat = mSelection.Path->getTransform();
  233. mat.mul( deltaRotation );
  234. // Apply New Transform.
  235. setPathTransform( mat );
  236. // Dirty.
  237. mIsDirty = true;
  238. mPathEdit.Dirty = true;
  239. } break;
  240. case ScaleMode :
  241. {
  242. // Apply New Scale.
  243. setPathScale( mGizmo->getScale() );
  244. // Dirty.
  245. mIsDirty = true;
  246. mPathEdit.Dirty = true;
  247. } break;
  248. */
  249. }
  250. }
  251. // No, Editing a Node
  252. else
  253. {
  254. switch ( mGizmoProfile->mode )
  255. {
  256. case MoveMode :
  257. {
  258. // Fetch Node.
  259. VPathNode *node = mSelection.Path->getNode( mSelection.Node );
  260. // Fetch Node Position.
  261. const Point3F oldPosition = node->getLocalPosition();
  262. // Invert Transform.
  263. MatrixF pathTransform = mSelection.Path->getTransform();
  264. pathTransform.setPosition( Point3F::Zero );
  265. pathTransform.inverse();
  266. Point3F deltaPosition = mGizmo->getOffset();
  267. pathTransform.mulP( deltaPosition );
  268. // Apply New Position.
  269. setNodePosition( mSelection.Node, ( oldPosition + deltaPosition ) );
  270. } break;
  271. case RotateMode :
  272. {
  273. // Fetch Node.
  274. VPathNode *node = mSelection.Path->getNode( mSelection.Node );
  275. // Invert Transform.
  276. MatrixF pathTransform = mSelection.Path->getTransform();
  277. pathTransform.setPosition( Point3F::Zero );
  278. pathTransform.inverse();
  279. // Rotation Delta.
  280. MatrixF deltaRotation( EulerF( mGizmo->getDeltaRot() ) );
  281. pathTransform.mul( deltaRotation );
  282. // Fetch Current Transform.
  283. MatrixF mat = node->getWorldTransform();
  284. mat.mul( deltaRotation );
  285. // Construct Quat.
  286. QuatF newRotation;
  287. newRotation.set( mat );
  288. // Apply New Rotation.
  289. setNodeRotation( mSelection.Node, newRotation );
  290. } break;
  291. case ScaleMode :
  292. {
  293. if ( isEditingWeight() )
  294. {
  295. // Edit Weight.
  296. updateWeight( pEvent );
  297. }
  298. } break;
  299. }
  300. }
  301. }
  302. //-----------------------------------------------------------------------------
  303. //
  304. // Reference Methods.
  305. //
  306. //-----------------------------------------------------------------------------
  307. VPath *VPathEditor::getClientPath( VPath *pPath )
  308. {
  309. if ( !pPath )
  310. {
  311. return NULL;
  312. }
  313. NetConnection *toServer = NetConnection::getConnectionToServer();
  314. NetConnection *toClient = NetConnection::getLocalClientConnection();
  315. if ( !toServer || !toClient )
  316. {
  317. return NULL;
  318. }
  319. const S32 ghostIndex = toClient->getGhostIndex( pPath );
  320. if ( ghostIndex == -1 )
  321. {
  322. return NULL;
  323. }
  324. return dynamic_cast<VPath*>( toServer->resolveGhost( ghostIndex ) );
  325. }
  326. //-----------------------------------------------------------------------------
  327. //
  328. // Selection Methods.
  329. //
  330. //-----------------------------------------------------------------------------
  331. bool VPathEditor::updateSelection( const Gui3DMouseEvent &pEvent )
  332. {
  333. const Point3F pt0 = pEvent.pos;
  334. const Point3F pt1 = pEvent.pos + pEvent.vec * gProjectDistance;
  335. RayInfo ri;
  336. if ( !gServerContainer.collideBox( pt0, pt1, MarkerObjectType, &ri ) )
  337. {
  338. // No Object.
  339. return false;
  340. }
  341. VPath *path = dynamic_cast<VPath*>( ri.object );
  342. if ( !path )
  343. {
  344. // No Path Object.
  345. return false;
  346. }
  347. // No Node.
  348. S32 nodeIndex = -1;
  349. for ( VPathNodeIterator itr = path->mNodeList.begin(); itr != path->mNodeList.end(); itr++ )
  350. {
  351. VPathNode *node = ( *itr );
  352. Point3F projPosition;
  353. project( node->getWorldPosition(), &projPosition );
  354. if ( projPosition.z <= 0.0f )
  355. {
  356. continue;
  357. }
  358. const Point2I rectHalfSize( 8, 8 );
  359. const Point2I screenPosition( ( S32 )projPosition.x, ( S32 )projPosition.y );
  360. const RectI screenRect( screenPosition - rectHalfSize, 2 * rectHalfSize );
  361. // Mouse Close Enough?
  362. if ( screenRect.pointInRect( pEvent.mousePoint ) )
  363. {
  364. // Select Node.
  365. nodeIndex = ( itr - path->mNodeList.begin() );
  366. }
  367. }
  368. // Set Selection.
  369. updateSelection( path, nodeIndex );
  370. // Valid Selection.
  371. return true;
  372. }
  373. void VPathEditor::updateSelection( VPath *pPathObject, const S32 &pNodeIndex )
  374. {
  375. // Store Selection.
  376. mSelection.Path = pPathObject;
  377. mSelection.Node = pNodeIndex;
  378. // Quick Update.
  379. updateSelection();
  380. // Return Buffer.
  381. char buffer[2][32];
  382. dSprintf( buffer[0], sizeof( buffer[0] ), "%d", ( pPathObject ) ? pPathObject->getId() : 0 );
  383. dSprintf( buffer[1], sizeof( buffer[1] ), "%d", pNodeIndex );
  384. // Callback.
  385. Con::executef( this, "onUpdateSelection", buffer[0], buffer[1] );
  386. }
  387. void VPathEditor::updateSelection( void )
  388. {
  389. if ( !isValidSelection() )
  390. {
  391. // No Further Updates.
  392. return;
  393. }
  394. // Fetch Node.
  395. VPathNode *node = mSelection.Path->getNode( mSelection.Node );
  396. // Fetch Node Rotation Matrix.
  397. MatrixF mat;
  398. node->getWorldRotation().setMatrix( &mat );
  399. // Determine Tangent Axis.
  400. Point3F pt0( VPath::gBezierAxis * node->getWeight() );
  401. Point3F pt1( -VPath::gBezierAxis * node->getWeight() );
  402. // Rotate Axis.
  403. mat.mulP( pt0 );
  404. mat.mulP( pt1 );
  405. // Offset Points.
  406. pt0 += node->getWorldPosition();
  407. pt1 += node->getWorldPosition();
  408. // Store Points.
  409. mSelection.TangentHandle[0] = pt0;
  410. mSelection.TangentHandle[1] = pt1;
  411. }
  412. ConsoleMethod( VPathEditor, clearSelection, void, 2, 2, "( void )" )
  413. {
  414. // Clear Selection.
  415. object->updateSelection( NULL, -1 );
  416. }
  417. ConsoleMethod( VPathEditor, setSelection, void, 3, 4, "( pObject, [pNodeIndex] )" )
  418. {
  419. // Fetch Path.
  420. VPath *path = dynamic_cast<VPath*>( Sim::findObject( argv[2] ) );
  421. if ( !path )
  422. {
  423. Con::errorf( "VPathEditor::setSelection() - Unable to select target Object." );
  424. return;
  425. }
  426. if ( argc == 3 )
  427. {
  428. // Select Path.
  429. object->updateSelection( path, -1 );
  430. return;
  431. }
  432. // Select Path & Node.
  433. object->updateSelection( path, dAtoi( argv[3] ) );
  434. }
  435. ConsoleMethod( VPathEditor, isValidSelection, bool, 2, 2, "( void )" )
  436. {
  437. return object->isValidSelection();
  438. }
  439. ConsoleMethod( VPathEditor, getSelectedPath, S32, 2, 2, "( void )" )
  440. {
  441. // Fetch Path.
  442. VPath *path = object->mSelection.Path;
  443. // Return ID.
  444. return ( path ) ? path->getId() : 0;
  445. }
  446. ConsoleMethod( VPathEditor, getSelectedNode, S32, 2, 2, "( void )" )
  447. {
  448. // Return Node Index.
  449. return ( object->mSelection.Path ) ? object->mSelection.Node : -1;
  450. }
  451. ConsoleMethod( VPathEditor, deleteSelection, void, 2, 2, "( void )" )
  452. {
  453. // Valid Selection?
  454. if ( object->isValidSelection() )
  455. {
  456. object->deleteNode( object->mSelection.Node );
  457. }
  458. }
  459. //-----------------------------------------------------------------------------
  460. //
  461. // Weight Editing Methods.
  462. //
  463. //-----------------------------------------------------------------------------
  464. bool VPathEditor::isEditingWeight( const Gui3DMouseEvent &pEvent )
  465. {
  466. if ( !isValidSelection() || mSelection.Path->mPathType != VPath::k_PathBezier )
  467. {
  468. // False.
  469. mEditWeight = false;
  470. // Invalid Selection.
  471. return false;
  472. }
  473. const Point3F pt0 = pEvent.pos;
  474. const Point3F pt1 = pEvent.pos + pEvent.vec * gProjectDistance;
  475. // Min Index.
  476. S32 minNode = -1;
  477. F32 minDistance = F32_MAX;
  478. for ( S32 i = 0; i < 2; i++ )
  479. {
  480. Point3F pt;
  481. if ( !Utility::FindNearestPointOnLine( mSelection.TangentHandle[i], pt0, pt1, &pt ) )
  482. {
  483. // Skip.
  484. continue;
  485. }
  486. // Distance.
  487. const F32 ptDistance = ( pt - mSelection.TangentHandle[i] ).len();
  488. if ( ptDistance < minDistance )
  489. {
  490. // Store Index.
  491. minNode = i;
  492. // Store Distance.
  493. minDistance = ptDistance;
  494. }
  495. }
  496. if ( minDistance > gSelectionDistance )
  497. {
  498. // False.
  499. mEditWeight = false;
  500. // Too Far Away.
  501. return false;
  502. }
  503. // True.
  504. mEditWeight = true;
  505. mEditWeightHandle = minNode;
  506. return true;
  507. }
  508. void VPathEditor::updateWeight( const Gui3DMouseEvent &pEvent )
  509. {
  510. if ( !isEditingWeight() )
  511. {
  512. // Woops!
  513. return;
  514. }
  515. // Fetch Current Node.
  516. VPathNode *node = mSelection.Path->getNode( mSelection.Node );
  517. Point3F nodePos = node->getWorldPosition();
  518. // Fetch Node Transform.
  519. MatrixF mat = node->getWorldTransform();
  520. // Fetch the Normal.
  521. const VectorF planeNormal = mat.getColumn3F( 0 );
  522. // Construct Plane.
  523. const PlaneF plane( nodePos, planeNormal );
  524. Point3F iPt;
  525. if ( plane.intersect( pEvent.pos, pEvent.vec, &iPt ) )
  526. {
  527. /*
  528. // Fetch Edit Vector.
  529. VectorF tangentVect( mSelection.TangentHandle[mEditWeightHandle] - nodePos );
  530. tangentVect.normalize();
  531. // Fetch Mouse Vector.
  532. VectorF mouseVec( iPt - nodePos );
  533. F32 mouseDist = mouseVec.len();
  534. mouseVec.normalize();
  535. // Find the Angles.
  536. F32 tangentAngle = mAtan2( -tangentVect.z, tangentVect.x );
  537. F32 mouseAngle = mAtan2( -mouseVec.z, mouseVec.x );
  538. // Determine Sign.
  539. const S32 sign = ( planeNormal.y > 0.f ) ? -1.f : 1.f;
  540. // Delta Rotation..
  541. const QuatF deltaRotation( AngAxisF( planeNormal, sign * ( mouseAngle - tangentAngle ) ) );
  542. // Calculate New Rotation.
  543. QuatF newRotation;
  544. newRotation.mul( nodePos, deltaRotation );
  545. // Apply Rotation.
  546. setNodeRotation( mSelection.Node, newRotation );
  547. */
  548. /*
  549. // Fetch Edit Vector.
  550. VectorF handleVec( mSelection.TangentHandle[mEditWeightHandle] - nodePos );
  551. handleVec.normalize();
  552. // Fetch Mouse Vector.
  553. VectorF mouseVec( iPt - nodePos );
  554. mouseVec.normalize();
  555. // Find the Angles.
  556. F32 handleAngle = Utility::GetPitch( handleVec ); //mAtan2( -handleVec.z, handleVec.x );
  557. F32 mouseAngle = Utility::GetPitch( mouseVec ); //mAtan2( -mouseVec.z, mouseVec.x );
  558. // Determine Sign.
  559. const S32 sign = ( planeNormal.y > 0.f ) ? -1.f : 1.f;
  560. // Delta Rotation.
  561. MatrixF rotMat;
  562. AngAxisF::RotateY( sign * ( mouseAngle - handleAngle ), &rotMat );
  563. // Rotate.
  564. mat.mul( rotMat );
  565. QuatF newRotation;
  566. newRotation.set( mat );
  567. // Apply Rotation.
  568. setNodeRotation( mSelection.Node, newRotation );
  569. */
  570. // Apply Weight.
  571. setNodeWeight( mSelection.Node, ( iPt - nodePos ).len() );
  572. }
  573. }
  574. //-----------------------------------------------------------------------------
  575. //
  576. // Path Editing Methods.
  577. //
  578. //-----------------------------------------------------------------------------
  579. void VPathEditor::setPathPosition( const Point3F &pPosition )
  580. {
  581. // Fetch Paths.
  582. VPath *serverPath = mSelection.Path;
  583. VPath *clientPath = getClientPath( serverPath );
  584. // Update Position.
  585. serverPath->setPosition( pPosition );
  586. clientPath->setPosition( pPosition );
  587. // Update Selection.
  588. updateSelection();
  589. }
  590. void VPathEditor::setPathRotation( const QuatF &pRotation )
  591. {
  592. // Determine the Matrix.
  593. MatrixF mat;
  594. pRotation.setMatrix( &mat );
  595. mat.setPosition( mSelection.Path->getPosition() );
  596. // Update Transform.
  597. setPathTransform( mat );
  598. }
  599. void VPathEditor::setPathTransform( const MatrixF &pTransform )
  600. {
  601. // Fetch Paths.
  602. VPath *serverPath = mSelection.Path;
  603. VPath *clientPath = getClientPath( serverPath );
  604. // Update Transform.
  605. serverPath->setTransform( pTransform );
  606. clientPath->setTransform( pTransform );
  607. // Update Selection.
  608. updateSelection();
  609. }
  610. void VPathEditor::setPathScale( const VectorF &pScale )
  611. {
  612. // Fetch Paths.
  613. VPath *serverPath = mSelection.Path;
  614. VPath *clientPath = getClientPath( serverPath );
  615. // Fetch Current Scale.
  616. VectorF scale = serverPath->getScale();
  617. scale.convolve( pScale );
  618. // Update Scale.
  619. serverPath->setScale( scale );
  620. clientPath->setScale( scale );
  621. // Update Selection.
  622. updateSelection();
  623. }
  624. //-----------------------------------------------------------------------------
  625. //
  626. // Node Editing Methods.
  627. //
  628. //-----------------------------------------------------------------------------
  629. bool VPathEditor::getPointOnPath( VPath *pPath, const Gui3DMouseEvent &pEvent, S32 &pNode, MatrixF &pTransform )
  630. {
  631. if ( pPath->getNodeCount() < 2 )
  632. {
  633. // Start / End Points.
  634. const Point3F pt0 = pEvent.pos;
  635. const Point3F pt1 = pEvent.pos + pEvent.vec * gProjectDistance;
  636. // Create Intersection Plane.
  637. const PlaneF plane( pPath->getPosition(), VPath::gBezierUp );
  638. // Intersection Point.
  639. Point3F intersectionPoint;
  640. if ( !plane.intersect( pEvent.pos, pEvent.vec, &intersectionPoint ) )
  641. {
  642. // No Intersection.
  643. return false;
  644. }
  645. // I'th Node.
  646. pNode = pPath->getNodeCount();
  647. // Set Identity.
  648. pTransform.identity();
  649. // Set Position.
  650. pTransform.setPosition( intersectionPoint );
  651. // Return.
  652. return true;
  653. }
  654. switch ( pPath->mPathType )
  655. {
  656. case VPath::k_PathLinear :
  657. {
  658. return getPointOnLinearPath( pPath, pEvent, pNode, pTransform );
  659. } break;
  660. case VPath::k_PathBezier :
  661. {
  662. return getPointOnBezierPath( pPath, pEvent, pNode, pTransform );
  663. } break;
  664. }
  665. return false;
  666. }
  667. bool VPathEditor::getPointOnLinearPath( VPath *pPath, const Gui3DMouseEvent &pEvent, S32 &pNode, MatrixF &pTransform )
  668. {
  669. // Start / End Points.
  670. const Point3F pt0 = pEvent.pos;
  671. const Point3F pt1 = pEvent.pos + pEvent.vec * gProjectDistance;
  672. S32 minNode = -1;
  673. F32 minDistance = F32_MAX;
  674. Point3F minPoint( 0.f, 0.f, 0.f );
  675. for ( VPathNodeIterator itr = pPath->mNodeList.begin(); itr != pPath->mNodeList.end(); itr++ )
  676. {
  677. // Fetch Nodes.
  678. VPathNode *srcNode = ( *itr );
  679. VPathNode *dstNode = ( itr == ( pPath->mNodeList.end() - 1 ) ) ? ( *( pPath->mNodeList.begin() ) ) : ( *( itr + 1 ) );
  680. // Project to Screen.
  681. Point3F srcNodeScreenPosition, dstNodeScreenPosition;
  682. project( srcNode->getWorldPosition(), &srcNodeScreenPosition );
  683. project( dstNode->getWorldPosition(), &dstNodeScreenPosition );
  684. // Skip?
  685. if ( srcNodeScreenPosition.z > 1.f && dstNodeScreenPosition.z > 1.f )
  686. {
  687. continue;
  688. }
  689. Point3F ptOut0, ptOut1;
  690. F32 ptOutDistance;
  691. if ( !Utility::FindNearestDistanceBetweenLines( pt0, pt1,
  692. srcNode->getWorldPosition(), dstNode->getWorldPosition(),
  693. &ptOut0, &ptOut1, &ptOutDistance ) )
  694. {
  695. continue;
  696. }
  697. if ( ptOutDistance < minDistance )
  698. {
  699. minDistance = ptOutDistance;
  700. minPoint = ptOut1;
  701. minNode = ( itr - pPath->mNodeList.begin() );
  702. }
  703. }
  704. // Distance too Large?
  705. if ( minDistance > 0.25f )
  706. {
  707. // Invalid.
  708. return false;
  709. }
  710. // Setup.
  711. pTransform.identity();
  712. pTransform.setPosition( minPoint );
  713. // Store Node.
  714. pNode = minNode;
  715. return true;
  716. }
  717. bool VPathEditor::getPointOnBezierPath( VPath *pPath, const Gui3DMouseEvent &pEvent, S32 &pNode, MatrixF &pTransform )
  718. {
  719. S32 minNode = -1;
  720. F32 minInterp = 0.f;
  721. F32 minDistance = F32_MAX;
  722. Point3F minPoint( 0.f, 0.f, 0.f );
  723. for ( VPathNodeIterator itr = pPath->mNodeList.begin(); itr != pPath->mNodeList.end(); itr++ )
  724. {
  725. // Fetch Nodes.
  726. VPathNode *srcNode = ( *itr );
  727. VPathNode *dstNode = ( itr == ( pPath->mNodeList.end() - 1 ) ) ? ( *( pPath->mNodeList.begin() ) ) : ( *( itr + 1 ) );
  728. // Project to Screen.
  729. Point3F srcNodeScreenPosition, dstNodeScreenPosition;
  730. project( srcNode->getWorldPosition(), &srcNodeScreenPosition );
  731. project( dstNode->getWorldPosition(), &dstNodeScreenPosition );
  732. // Skip?
  733. if ( srcNodeScreenPosition.z > 1.f && dstNodeScreenPosition.z > 1.f )
  734. {
  735. continue;
  736. }
  737. // Positions.
  738. const Point3F &pt0 = srcNode->getWorldPosition();
  739. const Point3F &pt3 = dstNode->getWorldPosition();
  740. // Fetch Node Rotation Matrices.
  741. MatrixF mat0, mat1;
  742. srcNode->getWorldRotation().setMatrix( &mat0 );
  743. dstNode->getWorldRotation().setMatrix( &mat1 );
  744. // Determine Tangent Axis.
  745. Point3F pt1( VPath::gBezierAxis * srcNode->getWeight() );
  746. Point3F pt2( -VPath::gBezierAxis * dstNode->getWeight() );
  747. // Rotate Axis.
  748. mat0.mulP( pt1 );
  749. mat1.mulP( pt2 );
  750. // Offset Points.
  751. pt1 += pt0;
  752. pt2 += pt3;
  753. for ( F32 t = 0.f, it = 1.f; t <= 1.f; t += 0.1f, it = ( 1.f - t ) )
  754. {
  755. // Calculate Position.
  756. Point3F pos = ( pt0 * it * it * it ) + ( 3 * pt1 * it * it * t ) + ( 3 * pt2 * it * t * t ) + ( pt3 * t * t * t );
  757. // Determine the Screen Position.
  758. Point3F screenPos;
  759. project( pos, &screenPos );
  760. // Behind?
  761. if ( screenPos.z > 1.f )
  762. {
  763. // Skip Point.
  764. continue;
  765. }
  766. // Determine the Distance.
  767. F32 screenDistance = Point2F( screenPos.x - pEvent.mousePoint.x, screenPos.y - pEvent.mousePoint.y ).lenSquared();
  768. // Min Distance?
  769. if ( screenDistance < minDistance )
  770. {
  771. // Store.
  772. minDistance = screenDistance;
  773. minInterp = t;
  774. minPoint = pos;
  775. minNode = ( itr - pPath->mNodeList.begin() );
  776. }
  777. }
  778. }
  779. // Distance too Large?
  780. if ( minDistance > 1000.f )
  781. {
  782. // Invalid.
  783. return false;
  784. }
  785. // Fetch Orientation.
  786. const VectorF &orientation = pPath->getPathOrientation( pPath->getNode( minNode ),
  787. pPath->getNode( ( minNode + 1 ) % pPath->getNodeCount() ),
  788. minInterp, true );
  789. // Z-Axis.
  790. VectorF zVec = -orientation;
  791. zVec.normalize();
  792. // X-Axis.
  793. VectorF xVec = mCross( VPath::gBezierUp, zVec );
  794. xVec.normalize();
  795. // Y-Axis.
  796. VectorF yVec = mCross( zVec, xVec );
  797. yVec.normalize();
  798. // Setup Object Transform.
  799. pTransform.identity();
  800. pTransform.setColumn( 0, xVec );
  801. pTransform.setColumn( 1, -zVec );
  802. pTransform.setColumn( 2, yVec );
  803. // Set the Position.
  804. pTransform.setPosition( minPoint );
  805. // Store Node.
  806. pNode = minNode;
  807. return true;
  808. }
  809. void VPathEditor::addNode( const Gui3DMouseEvent &pEvent )
  810. {
  811. VPath *path = mSelection.Path;
  812. if ( !path )
  813. {
  814. // Woops!
  815. return;
  816. }
  817. // Min Index.
  818. S32 nodeIndex = -1;
  819. MatrixF nodeTransform( true );
  820. if ( !getPointOnPath( path, pEvent, nodeIndex, nodeTransform ) )
  821. {
  822. // Can't Add.
  823. return;
  824. }
  825. // Invert Transform.
  826. MatrixF pathTransform = mSelection.Path->getTransform();
  827. pathTransform.setPosition( Point3F::Zero );
  828. pathTransform.inverse();
  829. Point3F nodePosition = ( nodeTransform.getPosition() - mSelection.Path->getPosition() );
  830. pathTransform.mulP( nodePosition );
  831. // Node Rotation.
  832. nodeTransform.mul( pathTransform );
  833. QuatF nodeRotation( nodeTransform );
  834. // Node Weights.
  835. F32 nodeWeight = 10.f;
  836. // Add New Node.
  837. VPathNode *node = path->addNode( nodePosition, nodeRotation, nodeWeight, ++nodeIndex );
  838. // Valid Node?
  839. if ( !node )
  840. {
  841. return;
  842. }
  843. // Update Size.
  844. path->updateContainer();
  845. // Calculate Path.
  846. path->calculatePath();
  847. UndoManager *historyManager = NULL;
  848. if ( !Sim::findObject( "EUndoManager", historyManager ) )
  849. {
  850. Con::errorf( "VPathEditor::addNode() - EUndoManager not found!" );
  851. return;
  852. }
  853. // Create Undo Action.
  854. VPathEditorAddNodeAction *editAction = new VPathEditorAddNodeAction();
  855. // Store Editor.
  856. editAction->mEditor = this;
  857. // Store Node Details.
  858. editAction->mPath = path;
  859. editAction->mNodeIndex = nodeIndex;
  860. editAction->mNodePosition = nodePosition;
  861. editAction->mNodeRotation = nodeRotation;
  862. editAction->mNodeWeight = nodeWeight;
  863. // Add To Manager.
  864. historyManager->addAction( editAction );
  865. // Set World Editor Dirty.
  866. setWorldEditorDirty();
  867. }
  868. void VPathEditor::deleteNode( const S32 &pNodeIndex )
  869. {
  870. VPath *path = mSelection.Path;
  871. if ( !path )
  872. {
  873. // Woops!
  874. return;
  875. }
  876. // Fetch Node Properites.
  877. VPathNode *node = path->getNode( pNodeIndex );
  878. const Point3F position = node->getLocalPosition();
  879. const QuatF rotation = node->getLocalRotation();
  880. const F32 weight = node->getWeight();
  881. // Delete Node.
  882. path->deleteNode( pNodeIndex );
  883. // Update Path.
  884. path->updateContainer();
  885. // Calculate Path.
  886. path->calculatePath();
  887. // Selected Node?
  888. const S32 _nodeIndex = pNodeIndex;
  889. if ( pNodeIndex == mSelection.Node )
  890. {
  891. // Update Selection.
  892. updateSelection( mSelection.Path, -1 );
  893. }
  894. UndoManager *historyManager = NULL;
  895. if ( !Sim::findObject( "EUndoManager", historyManager ) )
  896. {
  897. Con::errorf( "VPathEditor::deleteNode() - EUndoManager not found!" );
  898. return;
  899. }
  900. // Create Undo Action.
  901. VPathEditorDeleteNodeAction *editAction = new VPathEditorDeleteNodeAction();
  902. // Store Editor.
  903. editAction->mEditor = this;
  904. // Store Node Details.
  905. editAction->mPath = path;
  906. editAction->mNodeIndex = _nodeIndex;
  907. editAction->mNodePosition = position;
  908. editAction->mNodeRotation = rotation;
  909. editAction->mNodeWeight = weight;
  910. // Add To Manager.
  911. historyManager->addAction( editAction );
  912. // Set World Editor Dirty.
  913. setWorldEditorDirty();
  914. }
  915. void VPathEditor::setNodePosition( const S32 &pNodeIndex, const Point3F &pPosition )
  916. {
  917. // Fetch Paths.
  918. VPath *serverPath = mSelection.Path;
  919. VPath *clientPath = getClientPath( serverPath );
  920. // Sanity!
  921. if ( !serverPath || !clientPath )
  922. {
  923. return;
  924. }
  925. // Change?
  926. if ( serverPath->getNodeLocalPosition( pNodeIndex ) == pPosition )
  927. {
  928. return;
  929. }
  930. // Set Position.
  931. serverPath->setNodePosition( pNodeIndex, pPosition );
  932. clientPath->setNodePosition( pNodeIndex, pPosition );
  933. // Update Selection.
  934. updateSelection();
  935. // Dirty.
  936. mIsDirty = true;
  937. mNodeEdit.Dirty = true;
  938. // Arg Buffer.
  939. char buffer[3][32];
  940. dSprintf( buffer[0], sizeof( buffer[0] ), "%d", mSelection.Path->getId() );
  941. dSprintf( buffer[1], sizeof( buffer[1] ), "%d", pNodeIndex );
  942. dSprintf( buffer[2], sizeof( buffer[2] ), "%d", ( mSelection.Node == pNodeIndex ) );
  943. // Callback.
  944. Con::executef( this, "onUpdateNodePosition", buffer[0], buffer[1], buffer[2] );
  945. }
  946. void VPathEditor::setNodeRotation( const S32 &pNodeIndex, const QuatF &pRotation )
  947. {
  948. // Fetch Paths.
  949. VPath *serverPath = mSelection.Path;
  950. VPath *clientPath = getClientPath( serverPath );
  951. // Sanity!
  952. if ( !serverPath || !clientPath )
  953. {
  954. return;
  955. }
  956. // Change?
  957. if ( serverPath->getNodeLocalRotation( pNodeIndex ) == pRotation )
  958. {
  959. return;
  960. }
  961. // Set Position.
  962. serverPath->setNodeRotation( pNodeIndex, pRotation );
  963. clientPath->setNodeRotation( pNodeIndex, pRotation );
  964. // Update Selection.
  965. updateSelection();
  966. // Dirty.
  967. mIsDirty = true;
  968. mNodeEdit.Dirty = true;
  969. // Arg Buffer.
  970. char buffer[3][32];
  971. dSprintf( buffer[0], sizeof( buffer[0] ), "%d", mSelection.Path->getId() );
  972. dSprintf( buffer[1], sizeof( buffer[1] ), "%d", pNodeIndex );
  973. dSprintf( buffer[2], sizeof( buffer[2] ), "%d", ( mSelection.Node == pNodeIndex ) );
  974. // Callback.
  975. Con::executef( this, "onUpdateNodeRotation", buffer[0], buffer[1], buffer[2] );
  976. }
  977. void VPathEditor::setNodeWeight( const S32 &pNodeIndex, const F32 &pWeight )
  978. {
  979. // Fetch Paths.
  980. VPath *serverPath = mSelection.Path;
  981. VPath *clientPath = getClientPath( serverPath );
  982. // Sanity!
  983. if ( !serverPath || !clientPath )
  984. {
  985. return;
  986. }
  987. // Change?
  988. if ( serverPath->getNodeWeight( pNodeIndex ) == pWeight )
  989. {
  990. return;
  991. }
  992. // Set Weight.
  993. serverPath->setNodeWeight( pNodeIndex, pWeight );
  994. clientPath->setNodeWeight( pNodeIndex, pWeight );
  995. // Update Selection.
  996. updateSelection();
  997. // Dirty.
  998. mIsDirty = true;
  999. mNodeEdit.Dirty = true;
  1000. // Arg Buffer.
  1001. char buffer[3][32];
  1002. dSprintf( buffer[0], sizeof( buffer[0] ), "%d", mSelection.Path->getId() );
  1003. dSprintf( buffer[1], sizeof( buffer[1] ), "%d", pNodeIndex );
  1004. dSprintf( buffer[2], sizeof( buffer[2] ), "%d", ( mSelection.Node == pNodeIndex ) );
  1005. // Callback.
  1006. Con::executef( this, "onUpdateNodeWeight", buffer[0], buffer[1], buffer[2] );
  1007. }
  1008. void VPathEditor::setNodeOrientationMode( const S32 &pNodeIndex, const VPathNode::eOrientationType &pType )
  1009. {
  1010. // Fetch Paths.
  1011. VPath *serverPath = mSelection.Path;
  1012. VPath *clientPath = getClientPath( serverPath );
  1013. // Sanity!
  1014. if ( !serverPath || !clientPath )
  1015. {
  1016. return;
  1017. }
  1018. // Set Orientation Mode.
  1019. serverPath->setNodeOrientationMode( pNodeIndex, pType );
  1020. clientPath->setNodeOrientationMode( pNodeIndex, pType );
  1021. // Dirty.
  1022. mIsDirty = true;
  1023. mNodeEdit.Dirty = true;
  1024. // Arg Buffer.
  1025. char buffer[3][32];
  1026. dSprintf( buffer[0], sizeof( buffer[0] ), "%d", mSelection.Path->getId() );
  1027. dSprintf( buffer[1], sizeof( buffer[1] ), "%d", pNodeIndex );
  1028. dSprintf( buffer[2], sizeof( buffer[2] ), "%d", ( mSelection.Node == pNodeIndex ) );
  1029. // Callback.
  1030. Con::executef( this, "onUpdateNodeOrientation", buffer[0], buffer[1], buffer[2] );
  1031. }
  1032. void VPathEditor::setNodeOrientationMode( const S32 &pNodeIndex, const VPathNode::eOrientationType &pType, const Point3F &pPoint )
  1033. {
  1034. // Fetch Paths.
  1035. VPath *serverPath = mSelection.Path;
  1036. VPath *clientPath = getClientPath( serverPath );
  1037. // Sanity!
  1038. if ( !serverPath || !clientPath )
  1039. {
  1040. return;
  1041. }
  1042. // Set Orientation Mode.
  1043. serverPath->setNodeOrientationMode( pNodeIndex, pType, pPoint );
  1044. clientPath->setNodeOrientationMode( pNodeIndex, pType, pPoint );
  1045. // Dirty.
  1046. mIsDirty = true;
  1047. mNodeEdit.Dirty = true;
  1048. // Arg Buffer.
  1049. char buffer[3][32];
  1050. dSprintf( buffer[0], sizeof( buffer[0] ), "%d", mSelection.Path->getId() );
  1051. dSprintf( buffer[1], sizeof( buffer[1] ), "%d", pNodeIndex );
  1052. dSprintf( buffer[2], sizeof( buffer[2] ), "%d", ( mSelection.Node == pNodeIndex ) );
  1053. // Callback.
  1054. Con::executef( this, "onUpdateNodeOrientation", buffer[0], buffer[1], buffer[2] );
  1055. }
  1056. void VPathEditor::pushPathEdit( void )
  1057. {
  1058. // Clear Current Edit Dirty.
  1059. mPathEdit.Dirty = false;
  1060. if ( mSelection.Path != NULL )
  1061. {
  1062. // Store Node Details.
  1063. mPathEdit.Transform = mSelection.Path->getTransform();
  1064. }
  1065. }
  1066. void VPathEditor::popPathEdit( void )
  1067. {
  1068. // Did Edit?
  1069. if ( mPathEdit.Dirty && mSelection.Path != NULL )
  1070. {
  1071. UndoManager *historyManager = NULL;
  1072. if ( !Sim::findObject( "EUndoManager", historyManager ) )
  1073. {
  1074. Con::errorf( "VPathEditor - EUndoManager not found!" );
  1075. return;
  1076. }
  1077. // Create Undo Action.
  1078. VPathEditorEditPathAction *editAction = new VPathEditorEditPathAction( "Edit Path" );
  1079. // Store Editor.
  1080. editAction->mEditor = this;
  1081. // Store Path Details.
  1082. editAction->mPath = mSelection.Path;
  1083. editAction->mTransform = mPathEdit.Transform;
  1084. // Add To Manager.
  1085. historyManager->addAction( editAction );
  1086. // Clear Dirty.
  1087. mPathEdit.Dirty = false;
  1088. // Set World Editor Dirty.
  1089. setWorldEditorDirty();
  1090. }
  1091. }
  1092. void VPathEditor::pushNodeEdit( void )
  1093. {
  1094. // Clear Current Edit Dirty.
  1095. mNodeEdit.Dirty = false;
  1096. if ( isValidSelection() )
  1097. {
  1098. // Fetch Node.
  1099. VPathNode *node = mSelection.Path->getNode( mSelection.Node );
  1100. // Store Node Details.
  1101. mNodeEdit.Position = node->getLocalPosition();
  1102. mNodeEdit.Rotation = node->getLocalRotation();
  1103. mNodeEdit.Weight = node->getWeight();
  1104. }
  1105. }
  1106. void VPathEditor::popNodeEdit( void )
  1107. {
  1108. // Did Edit?
  1109. if ( mNodeEdit.Dirty && isValidSelection() )
  1110. {
  1111. UndoManager *historyManager = NULL;
  1112. if ( !Sim::findObject( "EUndoManager", historyManager ) )
  1113. {
  1114. Con::errorf( "VPathEditor - EUndoManager not found!" );
  1115. return;
  1116. }
  1117. // Create Undo Action.
  1118. VPathEditorEditNodeAction *editAction = new VPathEditorEditNodeAction( "Edit Node" );
  1119. // Store Editor.
  1120. editAction->mEditor = this;
  1121. // Store Node Details.
  1122. editAction->mPath = mSelection.Path;
  1123. editAction->mNodeIndex = mSelection.Node;
  1124. editAction->mNodePosition = mNodeEdit.Position;
  1125. editAction->mNodeRotation = mNodeEdit.Rotation;
  1126. editAction->mNodeWeight = mNodeEdit.Weight;
  1127. editAction->mNodeOrientation = mSelection.Path->getNode( mSelection.Node )->getOrientationMode();
  1128. // Add To Manager.
  1129. historyManager->addAction( editAction );
  1130. // Clear Dirty.
  1131. mNodeEdit.Dirty = false;
  1132. // Set World Editor Dirty.
  1133. setWorldEditorDirty();
  1134. }
  1135. }
  1136. void VPathEditor::setWorldEditorDirty( void )
  1137. {
  1138. WorldEditor *worldEditor;
  1139. if ( Sim::findObject( "EWorldEditor", worldEditor ) )
  1140. {
  1141. worldEditor->setDirty();
  1142. }
  1143. }
  1144. //-----------------------------------------------------------------------------
  1145. //
  1146. // Render Methods.
  1147. //
  1148. //-----------------------------------------------------------------------------
  1149. void VPathEditor::setStateBlock( void )
  1150. {
  1151. // Valid State Block?
  1152. if ( !mStateBlock )
  1153. {
  1154. // Setup Definition.
  1155. GFXStateBlockDesc def;
  1156. def.blendDefined = true;
  1157. def.blendEnable = true;
  1158. def.blendSrc = GFXBlendSrcAlpha;
  1159. def.blendDest = GFXBlendInvSrcAlpha;
  1160. def.zDefined = true;
  1161. def.cullDefined = false;
  1162. // Create State Block.
  1163. mStateBlock = GFX->createStateBlock( def );
  1164. }
  1165. // Set State Block.
  1166. GFX->setStateBlock( mStateBlock );
  1167. }
  1168. void VPathEditor::renderScene( const RectI &pUpdateRect )
  1169. {
  1170. // Setup State Block.
  1171. setStateBlock();
  1172. if ( isValidSelection() )
  1173. {
  1174. // Fetch Current Node.
  1175. VPathNode *node = mSelection.Path->getNode( mSelection.Node );
  1176. // Render Gizmo?
  1177. if ( mEditMode == k_Gizmo && mGizmoProfile->mode != ScaleMode )
  1178. {
  1179. // Fetch Node Transform.
  1180. MatrixF mat= node->getWorldTransform();
  1181. // Move Gizmo.
  1182. mGizmo->set( mat, node->getWorldPosition(), Point3F( 1.0f, 1.0f, 1.0f ) );
  1183. // Render Gizmo.
  1184. mGizmo->renderGizmo( mLastCameraQuery.cameraMatrix );
  1185. }
  1186. // Render Handles?
  1187. if ( mSelection.Path->mPathType == VPath::k_PathBezier )
  1188. {
  1189. // Fetch Tangent Handles.
  1190. const Point3F &pt0 = mSelection.TangentHandle[0];
  1191. const Point3F &pt1 = mSelection.TangentHandle[1];
  1192. // State Block.
  1193. GFXStateBlockDesc desc;
  1194. desc.setZReadWrite( true, true );
  1195. desc.fillMode = GFXFillSolid;
  1196. // Set Color.
  1197. PrimBuild::color( gPathColorSel );
  1198. // Render Line.
  1199. PrimBuild::begin( GFXLineList, 2 );
  1200. PrimBuild::vertex3fv( pt0 );
  1201. PrimBuild::vertex3fv( pt1 );
  1202. PrimBuild::end();
  1203. // Render Handles.
  1204. GFX->getDrawUtil()->drawSphere( desc, 0.1f, pt0, gPathColorSel );
  1205. GFX->getDrawUtil()->drawSphere( desc, 0.1f, pt1, gPathColorSel );
  1206. }
  1207. // ToPoint Node?
  1208. if ( node->getOrientationMode().Type == VPathNode::k_OrientationToPoint )
  1209. {
  1210. PrimBuild::color( gNodeLookAtPointColor );
  1211. PrimBuild::begin( GFXLineStrip, 2 );
  1212. PrimBuild::vertex3fv( node->getWorldPosition() );
  1213. PrimBuild::vertex3fv( node->getOrientationMode().Point );
  1214. PrimBuild::end();
  1215. }
  1216. }
  1217. else if ( mSelection.Path && mEditMode == k_Gizmo )
  1218. {
  1219. switch ( mGizmoProfile->mode )
  1220. {
  1221. case MoveMode:
  1222. {
  1223. // Fetch Path Transform.
  1224. const MatrixF &mat = mSelection.Path->getTransform();
  1225. // Fetch the Path's Box Center.
  1226. const Point3F &pos = mSelection.Path->getWorldBox().getCenter();
  1227. // Move Gizmo.
  1228. mGizmo->set( mat, pos, Point3F( 1.0f, 1.0f, 1.0f ) );
  1229. // Render Gizmo.
  1230. mGizmo->renderGizmo( mLastCameraQuery.cameraMatrix );
  1231. } break;
  1232. }
  1233. }
  1234. // Render Path Segments.
  1235. renderPaths( k_RenderSegments );
  1236. // Set Clip Rect.
  1237. GFX->setClipRect( pUpdateRect );
  1238. // Render Path Nodes.
  1239. renderPaths( k_RenderNodes );
  1240. if ( isValidSelection() )
  1241. {
  1242. // Fetch Current Node.
  1243. VPathNode *node = mSelection.Path->getNode( mSelection.Node );
  1244. // ToPoint Node?
  1245. if ( node->getOrientationMode().Type == VPathNode::k_OrientationToPoint )
  1246. {
  1247. // Project to Screen.
  1248. Point3F screenPosition;
  1249. project( node->getOrientationMode().Point, &screenPosition );
  1250. if ( screenPosition.z <= 1.0f )
  1251. {
  1252. // Determine the center & size of the node rectangle.
  1253. Point2I nodeCenter = Point2I( screenPosition.x, screenPosition.y );
  1254. Point2I nodeHalfSize = Point2I( 8, 8 );
  1255. // Determine Render Rectangle.
  1256. RectI nodeRect;
  1257. nodeRect.point = nodeCenter - nodeHalfSize;
  1258. nodeRect.extent = ( 2 * nodeHalfSize );
  1259. // Draw?
  1260. if ( getBounds().overlaps( nodeRect ) )
  1261. {
  1262. // Render the Point.
  1263. GFX->getDrawUtil()->drawRectFill( nodeRect, gNodeLookAtPointColor );
  1264. }
  1265. }
  1266. }
  1267. }
  1268. }
  1269. void VPathEditor::renderPaths( const RenderType &pRenderType )
  1270. {
  1271. SimSet *objectSet = VPath::getServerSet();
  1272. for ( SimSetIterator itr( objectSet ); *itr; ++itr )
  1273. {
  1274. VPath *path = dynamic_cast<VPath*>( *itr );
  1275. if ( path )
  1276. {
  1277. // Render Path.
  1278. renderPath( pRenderType, path, ( path == mSelection.Path ) ? gPathColorSel : gPathColor );
  1279. }
  1280. }
  1281. }
  1282. void VPathEditor::renderPath( const RenderType &pRenderType, VPath *pPath, const ColorI &pColor )
  1283. {
  1284. if ( !pPath )
  1285. {
  1286. // Sanity!
  1287. return;
  1288. }
  1289. switch ( pRenderType )
  1290. {
  1291. case k_RenderSegments :
  1292. {
  1293. switch ( pPath->mPathType )
  1294. {
  1295. case VPath::k_PathLinear :
  1296. {
  1297. renderLinearPath( pPath, pColor );
  1298. } break;
  1299. case VPath::k_PathBezier :
  1300. {
  1301. renderBezierPath( pPath, pColor );
  1302. } break;
  1303. }
  1304. } break;
  1305. case k_RenderNodes :
  1306. {
  1307. // Fetch Draw Util.
  1308. GFXDrawUtil *drawUtil = GFX->getDrawUtil();
  1309. // Fetch Bounds.
  1310. RectI bounds = getBounds();
  1311. const Point2I nodeMinHalfSize( 8, 8 );
  1312. for ( VPathNodeIterator itr = pPath->mNodeList.begin(); itr != pPath->mNodeList.end(); itr++ )
  1313. {
  1314. // Fetch Node.
  1315. VPathNode *node = ( *itr );
  1316. // Project to Screen.
  1317. Point3F screenPosition;
  1318. project( node->getWorldPosition(), &screenPosition );
  1319. if ( screenPosition.z > 1.0f )
  1320. {
  1321. continue;
  1322. }
  1323. // Determine the node text information.
  1324. const char *nodeText = avar( "%d", ( itr - pPath->mNodeList.begin() ) );
  1325. const Point2I nodeTextHalfSize = Point2I( 0.5f * (F32)getControlProfile()->mFont->getStrWidth( nodeText ),
  1326. 0.5f * (F32)getControlProfile()->mFont->getHeight() );
  1327. // Determine the center & size of the node rectangle.
  1328. Point2I nodeCenter = Point2I( screenPosition.x, screenPosition.y );
  1329. Point2I nodeHalfSize = Point2I( nodeTextHalfSize.x + 3, nodeTextHalfSize.y + 3 );
  1330. nodeHalfSize.setMax( nodeMinHalfSize );
  1331. // Determine Render Rectangle.
  1332. RectI nodeRect;
  1333. nodeRect.point = nodeCenter - nodeHalfSize;
  1334. nodeRect.extent = ( 2 * nodeHalfSize );
  1335. // Draw?
  1336. if ( !bounds.overlaps( nodeRect ) )
  1337. {
  1338. continue;
  1339. }
  1340. // Render the Point.
  1341. drawUtil->drawRectFill( nodeRect, pColor );
  1342. // Draw the node index text.
  1343. drawUtil->setBitmapModulation( getControlProfile()->mFontColor );
  1344. drawUtil->drawText( getControlProfile()->mFont, nodeCenter - nodeTextHalfSize, nodeText );
  1345. }
  1346. } break;
  1347. }
  1348. }
  1349. void VPathEditor::renderLinearPath( VPath *pPath, const ColorI &pColor )
  1350. {
  1351. if ( pPath->mNodeList.size() < 2 )
  1352. {
  1353. // No Lines.
  1354. return;
  1355. }
  1356. PrimBuild::color( pColor );
  1357. PrimBuild::begin( GFXLineStrip, ( pPath->mNodeList.size() + 1 ) );
  1358. for ( VPathNodeIterator itr = pPath->mNodeList.begin(); itr != pPath->mNodeList.end(); itr++ )
  1359. {
  1360. // Apply Vertex.
  1361. PrimBuild::vertex3fv( ( *itr )->getWorldPosition() );
  1362. }
  1363. // Loop Back.
  1364. PrimBuild::vertex3fv( pPath->mNodeList.front()->getWorldPosition() );
  1365. PrimBuild::end();
  1366. }
  1367. void VPathEditor::renderBezierPath( VPath *pPath, const ColorI &pColor )
  1368. {
  1369. if ( pPath->mNodeList.size() < 2 )
  1370. {
  1371. // No Lines.
  1372. return;
  1373. }
  1374. PrimBuild::color( pColor );
  1375. PrimBuild::begin( GFXLineStrip, U32( ( ( 1.01f / 0.01f ) + 1 ) * pPath->mNodeList.size() ) );
  1376. for ( VPathNodeIterator itr = pPath->mNodeList.begin(); itr != pPath->mNodeList.end(); itr++ )
  1377. {
  1378. // Fetch Nodes.
  1379. VPathNode *srcNode = ( *itr );
  1380. VPathNode *dstNode = ( itr == ( pPath->mNodeList.end() - 1 ) ) ? ( *( pPath->mNodeList.begin() ) ) : ( *( itr + 1 ) );
  1381. // Positions.
  1382. const Point3F &pt0 = srcNode->getWorldPosition();
  1383. const Point3F &pt3 = dstNode->getWorldPosition();
  1384. // Fetch Node Rotation Matrices.
  1385. MatrixF mat0, mat1;
  1386. srcNode->getWorldRotation().setMatrix( &mat0 );
  1387. dstNode->getWorldRotation().setMatrix( &mat1 );
  1388. // Determine Tangent Axis.
  1389. Point3F pt1( VPath::gBezierAxis * srcNode->getWeight() );
  1390. Point3F pt2( -VPath::gBezierAxis * dstNode->getWeight() );
  1391. // Rotate Axis.
  1392. mat0.mulP( pt1 );
  1393. mat1.mulP( pt2 );
  1394. // Offset Points.
  1395. pt1 += pt0;
  1396. pt2 += pt3;
  1397. for ( F32 t = 0.f, it = 1.f; t <= 1.f; t += 0.01f, it = ( 1.f - t ) )
  1398. {
  1399. // Calculate Position.
  1400. Point3F pos = ( pt0 * it * it * it ) + ( 3 * pt1 * it * it * t ) + ( 3 * pt2 * it * t * t ) + ( pt3 * t * t * t );
  1401. // Apply Vertex.
  1402. PrimBuild::vertex3fv( pos );
  1403. }
  1404. }
  1405. PrimBuild::end();
  1406. }
  1407. //-----------------------------------------------------------------------------
  1408. //
  1409. // History Events
  1410. //
  1411. //-----------------------------------------------------------------------------
  1412. void VPathEditor::VPathEditorEditPathAction::undo( void )
  1413. {
  1414. const MatrixF oldTransform = mTransform;
  1415. const MatrixF newTransform = mPath->getTransform();
  1416. // Apply Old Values.
  1417. mEditor->setPathTransform( oldTransform );
  1418. // The ol' Switcheroo.
  1419. mTransform = newTransform;
  1420. // Update Selection.
  1421. mEditor->updateSelection();
  1422. if ( mPath == mEditor->mSelection.Path )
  1423. {
  1424. // Arg Buffer.
  1425. char buffer[32];
  1426. dSprintf( buffer, sizeof( buffer ), "%d", mPath->getId() );
  1427. // Callback.
  1428. Con::executef( mEditor, "onUpdatePath", buffer );
  1429. }
  1430. // Set World Editor Dirty.
  1431. mEditor->setWorldEditorDirty();
  1432. }
  1433. void VPathEditor::VPathEditorEditPathAction::redo( void )
  1434. {
  1435. // Undo.
  1436. undo();
  1437. }
  1438. void VPathEditor::VPathEditorEditNodeAction::undo( void )
  1439. {
  1440. // Fetch Properties.
  1441. const Point3F oldPosition = mNodePosition;
  1442. const QuatF oldRotation = mNodeRotation;
  1443. const F32 oldWeight = mNodeWeight;
  1444. const VPathNode::sOrientation oldOrientation = mNodeOrientation;
  1445. VPathNode *node = mPath->getNode( mNodeIndex );
  1446. const Point3F newPosition = node->getLocalPosition();
  1447. const QuatF newRotation = node->getLocalRotation();
  1448. const F32 newWeight = node->getWeight();
  1449. const VPathNode::sOrientation newOrientation = node->getOrientationMode();
  1450. // Apply Old Values.
  1451. mPath->setNodePosition( mNodeIndex, oldPosition );
  1452. mPath->setNodeRotation( mNodeIndex, oldRotation );
  1453. mPath->setNodeWeight( mNodeIndex, oldWeight );
  1454. switch( oldOrientation.Type )
  1455. {
  1456. case VPathNode::k_OrientationFree :
  1457. {
  1458. // Orient Free.
  1459. mPath->setNodeOrientationMode( mNodeIndex, oldOrientation.Type );
  1460. } break;
  1461. case VPathNode::k_OrientationToPoint :
  1462. {
  1463. // Orient To Point.
  1464. mPath->setNodeOrientationMode( mNodeIndex, oldOrientation.Type, oldOrientation.Point );
  1465. } break;
  1466. }
  1467. // The ol' Switcheroo.
  1468. mNodePosition = newPosition;
  1469. mNodeRotation = newRotation;
  1470. mNodeWeight = newWeight;
  1471. mNodeOrientation = newOrientation;
  1472. // Update Selection.
  1473. mEditor->updateSelection();
  1474. if ( mPath == mEditor->mSelection.Path )
  1475. {
  1476. // Arg Buffer.
  1477. char buffer[3][32];
  1478. dSprintf( buffer[0], sizeof( buffer[0] ), "%d", mPath->getId() );
  1479. dSprintf( buffer[1], sizeof( buffer[1] ), "%d", mNodeIndex );
  1480. dSprintf( buffer[2], sizeof( buffer[2] ), "%d", ( mEditor->mSelection.Node == mNodeIndex ) );
  1481. // Callback.
  1482. Con::executef( mEditor, "onUpdateNode", buffer[0], buffer[1], buffer[2] );
  1483. }
  1484. // Set World Editor Dirty.
  1485. mEditor->setWorldEditorDirty();
  1486. }
  1487. void VPathEditor::VPathEditorEditNodeAction::redo( void )
  1488. {
  1489. // Undo.
  1490. undo();
  1491. }
  1492. void VPathEditor::VPathEditorAddNodeAction::undo( void )
  1493. {
  1494. // Selected Node?
  1495. if ( mNodeIndex == mEditor->mSelection.Node )
  1496. {
  1497. // Update Selection.
  1498. mEditor->updateSelection( mEditor->mSelection.Path, -1 );
  1499. }
  1500. // Delete Node.
  1501. mPath->deleteNode( mNodeIndex );
  1502. // Update Size.
  1503. mPath->updateContainer();
  1504. // Calculate Path.
  1505. mPath->calculatePath();
  1506. // Set World Editor Dirty.
  1507. mEditor->setWorldEditorDirty();
  1508. }
  1509. void VPathEditor::VPathEditorAddNodeAction::redo( void )
  1510. {
  1511. // Add Node.
  1512. VPathNode *node = mPath->addNode( mNodePosition, mNodeRotation, mNodeWeight, mNodeIndex );
  1513. // Valid Node?
  1514. if ( node )
  1515. {
  1516. // Update Size.
  1517. mPath->updateContainer();
  1518. // Calculate Path.
  1519. mPath->calculatePath();
  1520. }
  1521. // Set World Editor Dirty.
  1522. mEditor->setWorldEditorDirty();
  1523. }
  1524. void VPathEditor::VPathEditorDeleteNodeAction::undo( void )
  1525. {
  1526. // Add Node.
  1527. VPathNode *node = mPath->addNode( mNodePosition, mNodeRotation, mNodeWeight, mNodeIndex );
  1528. // Valid Node?
  1529. if ( node )
  1530. {
  1531. // Update Size.
  1532. mPath->updateContainer();
  1533. // Calculate Path.
  1534. mPath->calculatePath();
  1535. }
  1536. // Set World Editor Dirty.
  1537. mEditor->setWorldEditorDirty();
  1538. }
  1539. void VPathEditor::VPathEditorDeleteNodeAction::redo( void )
  1540. {
  1541. // Delete Node.
  1542. mPath->deleteNode( mNodeIndex );
  1543. // Update Size.
  1544. mPath->updateContainer();
  1545. // Calculate Path.
  1546. mPath->calculatePath();
  1547. // Set World Editor Dirty.
  1548. mEditor->setWorldEditorDirty();
  1549. }
  1550. //-----------------------------------------------------------------------------
  1551. //
  1552. // Script Edit Methods
  1553. //
  1554. //-----------------------------------------------------------------------------
  1555. ConsoleMethod( VPathEditor, setNodePosition, void, 3, 3, "( pPosition )" )
  1556. {
  1557. // Valid Selection?
  1558. if ( !object->isValidSelection() )
  1559. {
  1560. Con::warnf( "VPathEditor::setNodePosition() - Invalid Node Selection." );
  1561. return;
  1562. }
  1563. // Fetch Position.
  1564. Point3F position;
  1565. dSscanf( argv[2], "%g %g %g", &position.x, &position.y, &position.z );
  1566. // Store.
  1567. object->pushNodeEdit();
  1568. // Apply Update.
  1569. object->setNodePosition( object->mSelection.Node, position );
  1570. // Create Undo Action.
  1571. object->popNodeEdit();
  1572. }
  1573. ConsoleMethod( VPathEditor, setNodeRotation, void, 3, 3, "( pRotation )" )
  1574. {
  1575. // Valid Selection?
  1576. if ( !object->isValidSelection() )
  1577. {
  1578. Con::warnf( "VPathEditor::setNodeRotation() - Invalid Node Selection." );
  1579. return;
  1580. }
  1581. // Fetch Rotation.
  1582. AngAxisF aa;
  1583. QuatF rotation;
  1584. dSscanf( argv[2], "%g %g %g %g", &aa.axis.x, &aa.axis.y, &aa.axis.z, &aa.angle );
  1585. // Set Rotation.
  1586. rotation.set( aa );
  1587. // Store.
  1588. object->pushNodeEdit();
  1589. // Apply Update.
  1590. object->setNodeRotation( object->mSelection.Node, rotation );
  1591. // Create Undo Action.
  1592. object->popNodeEdit();
  1593. }
  1594. ConsoleMethod( VPathEditor, setNodeWeight, void, 3, 3, "( pWeight )" )
  1595. {
  1596. // Valid Selection?
  1597. if ( !object->isValidSelection() )
  1598. {
  1599. Con::warnf( "VPathEditor::setNodeWeight() - Invalid Node Selection." );
  1600. return;
  1601. }
  1602. // Store.
  1603. object->pushNodeEdit();
  1604. // Apply Update.
  1605. object->setNodeWeight( object->mSelection.Node, dAtof( argv[2] ) );
  1606. // Create Undo Action.
  1607. object->popNodeEdit();
  1608. }
  1609. ConsoleMethod( VPathEditor, setNodeOrientationMode, void, 3, 4, "( string pOrientationType, [vector pPoint] )" )
  1610. {
  1611. // Valid Selection?
  1612. if ( !object->isValidSelection() )
  1613. {
  1614. Con::warnf( "VPathEditor::setNodeOrientationMode() - Invalid Node Selection." );
  1615. return;
  1616. }
  1617. // Store.
  1618. object->pushNodeEdit();
  1619. // Orient?
  1620. const VPathNode::eOrientationType type = VPathNode::getOrientationTypeEnum( argv[2] );
  1621. switch ( type )
  1622. {
  1623. case VPathNode::k_OrientationFree :
  1624. {
  1625. // Apply Mode.
  1626. object->setNodeOrientationMode( object->mSelection.Node, type );
  1627. } break;
  1628. case VPathNode::k_OrientationToPoint:
  1629. {
  1630. // Fetch Point.
  1631. Point3F lookAtPoint( 0.f, 0.f, 0.f );
  1632. dSscanf( argv[3], "%g %g %g", &lookAtPoint.x, &lookAtPoint.y, &lookAtPoint.z );
  1633. // Apply Mode.
  1634. object->setNodeOrientationMode( object->mSelection.Node, type, lookAtPoint );
  1635. } break;
  1636. }
  1637. // Create Undo Action.
  1638. object->popNodeEdit();
  1639. }
  1640. //-----------------------------------------------------------------------------
  1641. //
  1642. // Utility
  1643. //
  1644. //-----------------------------------------------------------------------------
  1645. bool Utility::FindNearestDistanceBetweenLines( const Point3F &pA0, const Point3F &pA1, const Point3F &pB0, const Point3F &pB1, Point3F *pOutA, Point3F *pOutB, F32 *pDist )
  1646. {
  1647. const Point3F pA1A0 = ( pA1 - pA0 );
  1648. if ( pA1A0.isZero() )
  1649. {
  1650. return false;
  1651. }
  1652. const Point3F pB1B0 = ( pB1 - pB0 );
  1653. if ( pB1B0.isZero() )
  1654. {
  1655. return false;
  1656. }
  1657. const Point3F pA0B0 = ( pA0 - pB0 );
  1658. const F32 &d1343 = pA0B0.x * pB1B0.x + pA0B0.y * pB1B0.y + pA0B0.z * pB1B0.z;
  1659. const F32 &d4321 = pB1B0.x * pA1A0.x + pB1B0.y * pA1A0.y + pB1B0.z * pA1A0.z;
  1660. const F32 &d1321 = pA0B0.x * pA1A0.x + pA0B0.y * pA1A0.y + pA0B0.z * pA1A0.z;
  1661. const F32 &d4343 = pB1B0.x * pB1B0.x + pB1B0.y * pB1B0.y + pB1B0.z * pB1B0.z;
  1662. const F32 &d2121 = pA1A0.x * pA1A0.x + pA1A0.y * pA1A0.y + pA1A0.z * pA1A0.z;
  1663. const F32 &denom = d2121 * d4343 - d4321 * d4321;
  1664. if ( mIsZero( denom ) )
  1665. {
  1666. return false;
  1667. }
  1668. const F32 &mua = ( d1343 * d4321 - d1321 * d4343 ) / denom;
  1669. const F32 &mub = ( d1343 + d4321 * mua ) / d4343;
  1670. *pOutA = pA0 + mua *pA1A0;
  1671. *pOutB = pB0 + mub *pB1B0;
  1672. // Store Distance.
  1673. *pDist = ( ( *pOutA ) - ( *pOutB ) ).len();
  1674. return true;
  1675. }
  1676. bool Utility::IntersectLineSegment( const Point3F &pA0, const Point3F &pA1, const Point3F &pB0, const Point3F &pB1, const bool pSnap, Point3F *pX )
  1677. {
  1678. //
  1679. // Finding the intersection with the following method:
  1680. // We have line a going from P1 to P2:
  1681. // Pa = P1 + ua( P2 - P1 )
  1682. // and line b going from P3 to P4:
  1683. // Pb = P3 + ub( P4 - P3 )
  1684. //
  1685. // Solving for Pa = Pb:
  1686. // x1 + ua( x2 - x1 ) = x3 + ub( x4 - x3 )
  1687. // y1 + ua( y2 - y1 ) = y3 + ub( y4 - y3 )
  1688. //
  1689. // Solving for ua and ub:
  1690. // ua = ( ( x4 - x3 )( y1 - y3 ) - ( y4 - y3 )( x1 - x3 ) ) / d
  1691. // ub = ( ( x2 - x1 )( y1 - y3 ) - ( y2 - y1 )( x1 - x3 ) ) / d
  1692. // denom = ( y4 - y3 )( x2 - x1 ) - ( x4 - x3 )( y2 - y1 )
  1693. //
  1694. // x = x1 + ua( x2 - x1 )
  1695. // y = y1 + ua( y2 - y1 )
  1696. //
  1697. const F32 d = ( ( pB1.y - pB0.y ) * ( pA1.x - pA0.x ) ) - ( ( pB1.x - pB0.x ) * ( pA1.y - pA0.y ) );
  1698. if ( d == 0.0f )
  1699. {
  1700. // Lines are parallel
  1701. return false;
  1702. }
  1703. // Find the point of intersection
  1704. const F32 uA = ( ( ( pB1.x - pB0.x ) * ( pA0.y - pB0.y ) ) - ( ( pB1.y - pB0.y ) * ( pA0.x - pB0.x ) ) ) / d;
  1705. const F32 uB = ( ( ( pA1.x - pA0.x ) * ( pA0.y - pB0.y ) ) - ( ( pA1.y - pA0.y ) * ( pA0.x - pB0.x ) ) ) / d;
  1706. if ( !pSnap
  1707. && ( ( uA < 0.0f ) || ( uA > 1.0f )
  1708. || ( uB < 0.0f ) || ( uB > 1.0f ) ) )
  1709. {
  1710. return false;
  1711. }
  1712. if ( pX )
  1713. {
  1714. if ( uA < 0.0f )
  1715. {
  1716. *pX = pA0;
  1717. }
  1718. else if ( uA > 1.f )
  1719. {
  1720. *pX = pA1;
  1721. }
  1722. else
  1723. {
  1724. // The path intersects the segment
  1725. *pX = pA0 + uA * ( pA1 - pA0 );
  1726. }
  1727. }
  1728. return true;
  1729. }
  1730. bool Utility::FindNearestPointOnLine( const Point3F &pSrcPosition, const Point3F &pA0, const Point3F &pA1, Point3F *pDstPosition )
  1731. {
  1732. const Point3F up( 0.0f, 0.0f, 1.0f );
  1733. Point3F dir = ( pA1 - pA0 );
  1734. dir.normalize();
  1735. Point3F normal = mCross( dir, up );
  1736. normal.normalize();
  1737. // Find the nearest intersection point between the point and the line
  1738. const Point3F b0 = pSrcPosition + ( normal * 100000.0f );
  1739. const Point3F b1 = pSrcPosition - ( normal * 100000.0f );
  1740. return IntersectLineSegment( pA0, pA1, b0, b1, true, pDstPosition );
  1741. }
  1742. F32 Utility::GetPitch( const VectorF &pVec )
  1743. {
  1744. F32 pitch;
  1745. if ( mFabs( pVec.x ) > mFabs( pVec.y ) )
  1746. {
  1747. pitch = mAtan2( mFabs( pVec.z ), mFabs( pVec.x ) );
  1748. }
  1749. else
  1750. {
  1751. pitch = mAtan2( mFabs( pVec.z ), mFabs( pVec.y ) );
  1752. }
  1753. if ( pVec.z < 0.f )
  1754. {
  1755. pitch = -pitch;
  1756. }
  1757. return pitch;
  1758. }
  1759. F32 Utility::GetYaw( const VectorF &pVec )
  1760. {
  1761. F32 yaw = mAtan2( pVec.x, pVec.y );
  1762. if ( yaw < 0.f )
  1763. {
  1764. yaw += M_2PI_F;
  1765. }
  1766. return yaw;
  1767. }