/extensions/org/mt4jx/input/inputProcessors/componentProcessors/Rotate3DProcessor/Rotate3DProcessor.java

http://mt4j.googlecode.com/ · Java · 865 lines · 514 code · 156 blank · 195 comment · 53 complexity · e487b1c1a71600bcfa1e64a964804751 MD5 · raw file

  1. package org.mt4jx.input.inputProcessors.componentProcessors.Rotate3DProcessor;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import org.mt4j.MTApplication;
  6. import org.mt4j.components.MTCanvas;
  7. import org.mt4j.components.MTComponent;
  8. import org.mt4j.components.PickResult;
  9. import org.mt4j.components.PickResult.PickEntry;
  10. import org.mt4j.components.interfaces.IMTComponent3D;
  11. import org.mt4j.input.inputData.AbstractCursorInputEvt;
  12. import org.mt4j.input.inputData.InputCursor;
  13. import org.mt4j.input.inputProcessors.IInputProcessor;
  14. import org.mt4j.input.inputProcessors.MTGestureEvent;
  15. import org.mt4j.input.inputProcessors.componentProcessors.AbstractComponentProcessor;
  16. import org.mt4j.input.inputProcessors.componentProcessors.AbstractCursorProcessor;
  17. import org.mt4j.util.camera.IFrustum;
  18. import org.mt4j.util.math.Tools3D;
  19. import org.mt4j.util.math.Vector3D;
  20. import org.mt4jx.input.inputProcessors.componentProcessors.Group3DProcessorNew.Cluster;
  21. import org.mt4jx.util.extension3D.ComponentHelper;
  22. import processing.core.PApplet;
  23. public class Rotate3DProcessor extends AbstractCursorProcessor {
  24. private PApplet pApplet;
  25. /** The un used cursors. */
  26. private List<InputCursor> unUsedCursors;
  27. /** The locked cursors. */
  28. private List<InputCursor> lockedCursors;
  29. /** The rc. */
  30. private RotationContext rc;
  31. /** The drag plane normal. */
  32. private Vector3D dragPlaneNormal;
  33. /** The target Obj */
  34. private MTComponent targetComp;
  35. /** is rotation paused by collision */
  36. private boolean gesturePaused;
  37. /** has rotation resumed after pausing from collision*/
  38. private boolean resumed;
  39. public Rotate3DProcessor(PApplet graphicsContext,MTComponent targetComp){
  40. this.pApplet = graphicsContext;
  41. this.unUsedCursors = new ArrayList<InputCursor>();
  42. this.lockedCursors = new ArrayList<InputCursor>();
  43. this.dragPlaneNormal = new Vector3D(0,0,1);
  44. this.setLockPriority(3);
  45. this.targetComp = targetComp;
  46. }
  47. @Override
  48. public void cursorEnded(InputCursor inputCursor, AbstractCursorInputEvt currentEvent) {
  49. IMTComponent3D comp = currentEvent.getTarget();
  50. logger.debug(this.getName() + " INPUT_ENDED RECIEVED - MOTION: " + inputCursor.getId());
  51. if (lockedCursors.size() == 3 && lockedCursors.contains(inputCursor)){
  52. //there must be 3 cursors for a 3d rotation
  53. InputCursor firstCursor;
  54. InputCursor secondCursor;
  55. InputCursor thirdCursor;
  56. //TODO proof if this could be done better
  57. if (lockedCursors.get(0).equals(inputCursor)){
  58. firstCursor = inputCursor;
  59. secondCursor = lockedCursors.get(1);
  60. thirdCursor = lockedCursors.get(2);
  61. }else if(lockedCursors.get(1).equals(inputCursor)){
  62. firstCursor = lockedCursors.get(0);
  63. secondCursor = inputCursor;
  64. thirdCursor = lockedCursors.get(2);
  65. }else
  66. {
  67. firstCursor = lockedCursors.get(0);
  68. secondCursor = lockedCursors.get(1);
  69. thirdCursor = inputCursor;
  70. }
  71. lockedCursors.remove(inputCursor);
  72. ArrayList<InputCursor> leftOverCursors = new ArrayList<InputCursor>();
  73. leftOverCursors.add(lockedCursors.get(0));
  74. leftOverCursors.add(lockedCursors.get(1));
  75. if (unUsedCursors.size() > 0){ //Check if there are other cursors we could use for scaling if one was removed
  76. InputCursor futureCursor = unUsedCursors.get(0);
  77. if (this.canLock(futureCursor)){ //check if we have priority to claim another cursor and use it
  78. rc = new RotationContext(futureCursor, leftOverCursors.get(0),leftOverCursors.get(1), comp);
  79. if (!rc.isGestureAborted()){
  80. this.getLock(futureCursor);
  81. unUsedCursors.remove(futureCursor);
  82. lockedCursors.add(futureCursor);
  83. logger.debug(this.getName() + " continue with different cursors (ID: " + futureCursor.getId() + ")" + " " + "(ID: " + leftOverCursors.get(0).getId() + ")");
  84. //TODO fire start evt?
  85. }else{ //couldnt start gesture - cursor's not on component
  86. this.endGesture(leftOverCursors, comp, firstCursor, secondCursor,thirdCursor);
  87. }
  88. }else{ //we dont have permission to use other cursor - End gesture
  89. this.endGesture(leftOverCursors, comp, firstCursor, secondCursor,thirdCursor);
  90. }
  91. }else{ //no more unused cursors on comp - End gesture
  92. this.endGesture(leftOverCursors, comp, firstCursor, secondCursor,thirdCursor);
  93. }
  94. this.unLock(inputCursor); //FIXME TEST
  95. }else{ //cursor was not a scaling involved cursor
  96. if (unUsedCursors.contains(inputCursor)){
  97. unUsedCursors.remove(inputCursor);
  98. }
  99. if(lockedCursors.contains(inputCursor))
  100. {
  101. lockedCursors.remove(inputCursor);
  102. }
  103. }
  104. }
  105. private void endGesture(ArrayList<InputCursor> leftOverCursors, IMTComponent3D component, InputCursor firstCursor, InputCursor secondCursor,InputCursor thirdCursor){
  106. lockedCursors.clear();
  107. unUsedCursors.addAll(leftOverCursors);
  108. Iterator<InputCursor> iter = leftOverCursors.iterator();
  109. while(iter.hasNext())
  110. {
  111. InputCursor iCursor = iter.next();
  112. this.unLock(iCursor);
  113. }
  114. this.fireGestureEvent(new Rotate3DEvent(this, MTGestureEvent.GESTURE_ENDED, component, firstCursor, secondCursor,thirdCursor, Vector3D.ZERO_VECTOR, rc.getRotationPoint(), rc.getRotationDirection(),0,0,0,rc.getRotationAxis()));
  115. }
  116. @Override
  117. public void cursorLocked(InputCursor cursor,
  118. IInputProcessor lockingprocessor) {
  119. if (lockingprocessor instanceof AbstractComponentProcessor){
  120. logger.debug(this.getName() + " Recieved MOTION LOCKED by (" + ((AbstractComponentProcessor)lockingprocessor).getName() + ") - cursor ID: " + cursor.getId());
  121. }else{
  122. logger.debug(this.getName() + " Recieved MOTION LOCKED by higher priority signal - cursor ID: " + cursor.getId());
  123. }
  124. if (lockedCursors.contains(cursor)){
  125. //cursors was used here! -> we have to stop the gesture
  126. //put all used cursors in the unused cursor list and clear the usedcursorlist
  127. unUsedCursors.addAll(lockedCursors);
  128. lockedCursors.clear();
  129. //TODO fire ended evt?
  130. logger.debug(this.getName() + " cursor:" + cursor.getId() + " MOTION LOCKED. Was an active cursor in this gesture!");
  131. }
  132. }
  133. @Override
  134. public void cursorStarted(InputCursor inputCursor,
  135. AbstractCursorInputEvt currentEvent) {
  136. IMTComponent3D comp = currentEvent.getTarget();
  137. if (lockedCursors.size() >= 3){ //gesture with 3 fingers already in progress
  138. unUsedCursors.add(inputCursor);
  139. logger.debug(this.getName() + " has already enough cursors for this gesture - adding to unused ID:" + inputCursor.getId());
  140. }else{ //no gesture in progress yet
  141. //save current selected Object inside of Cluster for correct rotation
  142. if(currentEvent.getTarget() instanceof Cluster)
  143. {
  144. Cluster cluster = (Cluster)currentEvent.getTarget();
  145. MTComponent sourceComponent = (MTComponent) currentEvent.getTarget();
  146. Vector3D currentPos = new Vector3D(currentEvent.getPosX(),currentEvent.getPosY(),0.0f);
  147. MTCanvas parentCanvas = this.getParentCanvas(sourceComponent);
  148. cluster.setComposite(false);
  149. PickResult prCanvas = parentCanvas.pick(currentPos.getX(), currentPos.getY(), true);
  150. cluster.setComposite(true);
  151. List<PickEntry> plCanvas = prCanvas.getPickList();
  152. //get the most top element in the picklist
  153. if(plCanvas.size()>0)
  154. {
  155. PickEntry currentPickEntry = plCanvas.get(plCanvas.size()-1);
  156. MTComponent currentComponent = currentPickEntry.hitObj;
  157. cluster.setCurrentlySelectedChildren(currentComponent);
  158. }
  159. }
  160. if (unUsedCursors.size() == 2){//in this case a new rotation3d can be started
  161. logger.debug(this.getName() + " has already has 2 unused cursor - we can try start gesture! used with ID:" + unUsedCursors.get(0).getId() + " and new cursor ID:" + inputCursor.getId());
  162. InputCursor otherCursor = unUsedCursors.get(0);//use the both unused Cursors for the rotation axis
  163. InputCursor secondCursor = unUsedCursors.get(1);
  164. if (this.canLock(otherCursor,secondCursor,inputCursor)){
  165. rc = new RotationContext(otherCursor, secondCursor,inputCursor,comp);//create new RotationContext
  166. if (!rc.isGestureAborted()){
  167. this.getLock(otherCursor, inputCursor,secondCursor);
  168. unUsedCursors.remove(otherCursor);//remove the cursors from unUsedCursors
  169. unUsedCursors.remove(secondCursor);
  170. lockedCursors.add(otherCursor);//put all cursors in lockedCursors
  171. lockedCursors.add(inputCursor);
  172. lockedCursors.add(secondCursor);
  173. logger.debug(this.getName() + " we could lock both cursors!");
  174. //this.fireGestureEvent(new Rotate3DEvent(this, MTGestureEvent.GESTURE_DETECTED, comp, otherCursor, secondCursor,inputCursor, Vector3D.ZERO_VECTOR, rc.getRotationPoint(),rc.getRotationDirection(),rc.getRotationDegreesX(),rc.getRotationDegreesY(),rc.getRotationDegreesZ(),rc.getRotationAxis()));
  175. }else{
  176. rc = null;
  177. unUsedCursors.add(inputCursor);
  178. }
  179. }else{
  180. logger.debug(this.getName() + " we could NOT lock both cursors!");
  181. unUsedCursors.add(inputCursor);
  182. }
  183. }else if(lockedCursors.size()==2&&inputCursor==rc.getRotateFingerCursor())
  184. {
  185. InputCursor otherCursor = lockedCursors.get(0);
  186. InputCursor secondCursor = lockedCursors.get(1);
  187. if (this.canLock(inputCursor)){
  188. rc = new RotationContext(otherCursor, secondCursor,inputCursor,comp);
  189. if (!rc.isGestureAborted()){
  190. this.getLock(inputCursor);
  191. lockedCursors.add(inputCursor);
  192. logger.debug(this.getName() + " we could lock both cursors!");
  193. this.fireGestureEvent(new Rotate3DEvent(this, MTGestureEvent.GESTURE_STARTED, comp, otherCursor, secondCursor,inputCursor, Vector3D.ZERO_VECTOR, rc.getRotationPoint(),rc.getRotationDirection(), rc.getRotationDegreesX(),rc.getRotationDegreesY(),rc.getRotationDegreesZ(),rc.getRotationAxis()));
  194. }else{
  195. rc = null;
  196. unUsedCursors.add(inputCursor);
  197. }
  198. }
  199. }
  200. else{
  201. logger.debug(this.getName() + " we didnt have a unused cursor previously to start gesture now");
  202. unUsedCursors.add(inputCursor);
  203. }
  204. }
  205. }
  206. public void cursorUnlocked(InputCursor cursor) {
  207. logger.debug(this.getName() + " Recieved UNLOCKED signal for cursor ID: " + cursor.getId());
  208. if (lockedCursors.size() >= 3){ //we dont need the unlocked cursor, gesture still in progress
  209. return;
  210. }
  211. if (unUsedCursors.contains(cursor)){ //should always be true here!?
  212. if (unUsedCursors.size() >= 3){ //we can try to resume the gesture
  213. InputCursor firstCursor = unUsedCursors.get(0);
  214. InputCursor secondCursor = unUsedCursors.get(1);
  215. InputCursor thirdCursor = unUsedCursors.get(2);
  216. //See if we can obtain a lock on both cursors
  217. if (this.canLock(firstCursor, secondCursor,thirdCursor)){
  218. IMTComponent3D comp = firstCursor.getFirstEvent().getTarget();
  219. rc = new RotationContext(firstCursor, secondCursor,thirdCursor, comp);
  220. if (!rc.isGestureAborted()){ //Check if we could start gesture (ie. if fingers on component)
  221. this.getLock(firstCursor, secondCursor,thirdCursor);
  222. lockedCursors.add(firstCursor);
  223. lockedCursors.add(secondCursor);
  224. lockedCursors.add(thirdCursor);
  225. logger.debug(this.getName() + " we could lock cursors: " + firstCursor.getId() +", " + secondCursor.getId() + ", " + thirdCursor.getId());
  226. unUsedCursors.remove(firstCursor);
  227. unUsedCursors.remove(secondCursor);
  228. unUsedCursors.remove(thirdCursor);
  229. }else{
  230. rc = null;
  231. logger.debug(this.getName() + " we could NOT resume gesture - cursors not on component: " + firstCursor.getId() +", " + secondCursor.getId());
  232. }
  233. //TODO fire started evt?
  234. }else{
  235. logger.debug(this.getName() + " we could NOT lock cursors: " + firstCursor.getId() +", " + secondCursor.getId() + ", " + thirdCursor.getId());
  236. }
  237. }
  238. }else{
  239. logger.error(this.getName() + "hmmm - investigate why is cursor not in unusedList?");
  240. }
  241. }
  242. public void cursorUpdated(InputCursor inputCursor,
  243. AbstractCursorInputEvt currentEvent) {
  244. IMTComponent3D comp = currentEvent.getTarget();
  245. if (lockedCursors.size() == 3 && lockedCursors.contains(inputCursor)){
  246. rc.updateAndGetRotationAngle(inputCursor);
  247. this.fireGestureEvent(new Rotate3DEvent(this, MTGestureEvent.GESTURE_UPDATED, comp, rc.getPinFingerCursor(), rc.getPinFingerSecondCursor(),rc.getRotateFingerCursor(), Vector3D.ZERO_VECTOR, rc.getRotationPoint(),rc.getRotationDirection(),rc.getRotationDegreesX(),rc.getRotationDegreesY(),rc.getRotationDegreesZ(),rc.getRotationAxis() ));
  248. }
  249. }
  250. private MTCanvas getParentCanvas(MTComponent as) {
  251. MTComponent tmp = as.getRoot();
  252. if(tmp instanceof MTCanvas){
  253. return (MTCanvas)tmp;
  254. }else{
  255. MTComponent mtc = as;
  256. while ((!(mtc == null)) && (!((mtc = mtc.getParent()) instanceof MTCanvas))) {
  257. }
  258. return (MTCanvas) mtc;
  259. }
  260. }
  261. /**
  262. * The Class RotationContext.
  263. */
  264. public class RotationContext {
  265. /** The pin finger start. */
  266. private Vector3D pinFingerStart;
  267. /** The pin finger last. */
  268. private Vector3D pinFingerLast;
  269. /** The pin finger new. */
  270. private Vector3D pinFingerNew;
  271. /** The second pin finger start. */
  272. private Vector3D pinFingerSecondStart;
  273. private Vector3D pinFingerSecondLast;
  274. private Vector3D pinFingerSecondNew;
  275. /** The rotate finger start. */
  276. private Vector3D rotateFingerStart;
  277. /** The rotate finger last. */
  278. private Vector3D rotateFingerLast;
  279. /** The rotate finger new. */
  280. private Vector3D rotateFingerNew;
  281. /** The last rotation vect. */
  282. private Vector3D lastRotationVect;
  283. /** The object. */
  284. private IMTComponent3D object;
  285. /** The rotation point. */
  286. private Vector3D rotationPoint;
  287. private Vector3D rotationAxis;
  288. /** The pin finger cursor. */
  289. private InputCursor pinFingerCursor;
  290. /** The second pin finger cursor. */
  291. private InputCursor pinFingerSecondCursor;
  292. /** The rotate finger cursor. */
  293. private InputCursor rotateFingerCursor;
  294. private Vector3D rotateCursorVectorLast;
  295. /** The new finger middle pos. */
  296. private Vector3D newFingerMiddlePos;
  297. /** The old finger middle pos. */
  298. private Vector3D oldFingerMiddlePos;
  299. /** The pin finger translation vect. */
  300. private Vector3D pinFingerTranslationVect;
  301. private boolean gestureAborted;
  302. private float percentageX=0.0f,percentageY=0.0f,percentageZ=0.0f;
  303. private float rotateLineLength = 0.0f;
  304. private float degreesPerLengthUnit = 0.01f;
  305. private short rotationDirection = 1;
  306. private Vector3D directionFinderLeft;
  307. private Vector3D directionFinderRight;
  308. /**
  309. * Instantiates a new rotation context.
  310. *
  311. * @param pinFingerCursor the pin finger cursor
  312. * @param rotateFingerCursor the rotate finger cursor
  313. * @param object the object
  314. */
  315. public RotationContext(InputCursor pinFingerCursor, InputCursor pinFingerCursor2, InputCursor rotateFingerCursor, IMTComponent3D object){
  316. this.pinFingerCursor = pinFingerCursor;
  317. this.pinFingerSecondCursor = pinFingerCursor2;
  318. this.rotateFingerCursor = rotateFingerCursor;
  319. //check if first two fingers hit an object
  320. Vector3D interPoint = getIntersection(pApplet, object, pinFingerCursor);
  321. Vector3D interPoint2 = getIntersection(pApplet, object, pinFingerSecondCursor);
  322. this.object = object;
  323. //if they hit take their x,y values and make an axis parallel
  324. if (interPoint !=null&& interPoint2 != null){
  325. interPoint = projectPointToNearPlane(interPoint);
  326. interPoint2 = projectPointToNearPlane(interPoint2);
  327. pinFingerNew = interPoint;
  328. pinFingerSecondNew = interPoint2;
  329. setRotationAxis(getRotationAxis(interPoint,interPoint2));
  330. //rotationAxis = interPoint.getSubtracted(interPoint2);
  331. //z value of no interest
  332. //rotationAxis.z = 0.0f;
  333. }else{
  334. logger.error(getName() + " Pinfinger NEW = NULL");
  335. pinFingerNew = new Vector3D();
  336. pinFingerSecondNew = new Vector3D();
  337. setRotationAxis(new Vector3D());
  338. //TODO ABORT THE Rotation HERE!
  339. gestureAborted = true;
  340. }
  341. //reset all fingers
  342. this.pinFingerStart = pinFingerNew.getCopy();
  343. this.pinFingerSecondStart = pinFingerSecondNew.getCopy();
  344. this.pinFingerLast = pinFingerStart.getCopy();
  345. this.pinFingerSecondLast = pinFingerSecondStart.getCopy();
  346. this.rotateCursorVectorLast = new Vector3D(rotateFingerCursor.getCurrentEvtPosX(),rotateFingerCursor.getCurrentEvtPosY(),0.0f);
  347. updateCalculations();
  348. }
  349. private void updateCalculations()
  350. {
  351. float rotationAxisLength = getRotationAxis().length();
  352. MTComponent comp = (MTComponent)object;
  353. //get center point
  354. if(!(comp instanceof Cluster))
  355. {
  356. rotationPoint = ComponentHelper.getCenterPointGlobal(comp);
  357. }else
  358. {
  359. Cluster cl = (Cluster)comp;
  360. rotationPoint = ComponentHelper.getCenterPointGlobal(cl.getCurrentlySelectedChildren());
  361. }
  362. Vector3D vec = rotationPoint.getCopy();
  363. vec.z = vec.z - 1.0f;
  364. //get direction in which the rotation should be
  365. directionFinderRight = vec.getSubtracted(rotationPoint);//rotationPoint.getSubtracted(vec);
  366. directionFinderRight = rotationAxis.getCross(directionFinderRight);
  367. directionFinderLeft = directionFinderRight.getInverted();
  368. //comp.translate(rotationPoint);
  369. //direction Finding
  370. //Vector3D com = object.getCenterOfMass();
  371. //rotationAxis.z = com.z;
  372. Vector3D rotationPartX = new Vector3D(getRotationAxis().x,0.0f,0.0f);
  373. Vector3D rotationPartY = new Vector3D(0.0f,getRotationAxis().y,0.0f);
  374. Vector3D rotationPartZ = new Vector3D(0.0f,0.0f,getRotationAxis().z);
  375. // Tools3D.endGL(pApplet);
  376. float dotX = rotationPartX.dot(getRotationAxis());
  377. float dotY = rotationPartY.dot(getRotationAxis());
  378. float dotZ = rotationPartZ.dot(getRotationAxis());
  379. //System.out.println("dotX " + dotX);
  380. //System.out.println("dotY " + dotY);
  381. //System.out.println("dot Z " + dotZ);
  382. //System.out.println("rotationAxis length" + getRotationAxis().length());
  383. //System.out.println();
  384. float degreesX = (float)Math.toDegrees(Math.acos(dotX/(rotationPartX.length()*getRotationAxis().length())));
  385. float degreesY = (float)Math.toDegrees(Math.acos(dotY/(rotationPartY.length()*getRotationAxis().length())));
  386. //float degreesZ = (float)Math.toDegrees(Math.acos(dotZ/(rotationPartZ.length()*rotationAxis.length())));
  387. if (Float.isNaN(degreesY)){
  388. degreesY = 0.0f;
  389. }
  390. if (Float.isNaN(degreesX)){
  391. degreesX = 0.0f;
  392. }
  393. percentageX = (degreesY/90.0f)*100.0f;
  394. percentageY = (degreesX/90.0f)*100.0f;
  395. if(getRotationAxis().x<0.0f)
  396. {
  397. percentageY = -percentageY;
  398. }
  399. //System.out.println("percentageX " + percentageX);
  400. //System.out.println("percentageY " + percentageY);
  401. percentageZ = 0.0f;
  402. }
  403. //get the rotation axis which looks always from the less y too the more y value
  404. private Vector3D getRotationAxis(Vector3D pointOne,Vector3D pointTwo)
  405. {
  406. Vector3D rotationAxis = pointOne.getSubtracted(pointTwo);
  407. Vector3D rotationAxis2 = pointTwo.getSubtracted(pointOne);
  408. if(rotationAxis.y>=rotationAxis2.y)
  409. {
  410. return rotationAxis;
  411. }else
  412. {
  413. return rotationAxis2;
  414. }
  415. }
  416. private Vector3D projectPointToNearPlane(Vector3D point)
  417. {
  418. IFrustum frustum = object.getViewingCamera().getFrustum();
  419. //projiziere Punkt auf die Near Plane
  420. point = Tools3D.projectPointToPlaneInPerspectiveMode(point, frustum, frustum.getZValueOfNearPlane(),(MTApplication)object.getRenderer());
  421. return point;
  422. }
  423. /**
  424. * Update and get rotation angle.
  425. *
  426. * @param moveCursor the move cursor
  427. *
  428. * @return the float
  429. */
  430. public void updateAndGetRotationAngle(InputCursor moveCursor) {
  431. // /*
  432. float newAngleRad;
  433. float newAngleDegrees;
  434. //save the current pinfinger location as the old one
  435. if(!resumed)
  436. {
  437. this.pinFingerLast = this.pinFingerNew;
  438. //save the current pinfingertwo location as the old one
  439. this.pinFingerSecondLast = this.pinFingerSecondNew;
  440. //save the current pinfinger location as the old one
  441. this.rotateFingerLast = this.rotateFingerNew;
  442. //Check which finger moved and has to be updated
  443. if (moveCursor.equals(pinFingerCursor)){
  444. updatePinFinger();
  445. updateCalculations();
  446. }
  447. else if(moveCursor.equals(pinFingerSecondCursor))
  448. {
  449. updatePinFingerSecond();
  450. updateCalculations();
  451. }
  452. else if (moveCursor.equals(rotateFingerCursor)){
  453. updateRotateFinger(moveCursor);
  454. //updateCalculations();
  455. }
  456. }else
  457. {
  458. this.pinFingerNew = this.pinFingerLast;
  459. //save the current pinfingertwo location as the old one
  460. this.pinFingerSecondNew = this.pinFingerSecondLast;
  461. //save the current pinfinger location as the old one
  462. this.rotateFingerNew = this.rotateFingerLast;
  463. resumed = false;
  464. }
  465. //// */
  466. }
  467. /**
  468. * Update rotate finger.
  469. */
  470. public void updateRotateFinger(InputCursor rotateCursor){
  471. //TODO save last position and use that one if new one is null.. everywhere!
  472. /*Vector3D newRotateFingerPos = ToolsIntersection.getRayPlaneIntersection(
  473. Tools3D.getCameraPickRay(pApplet, object,rotateFingerCursor.getCurrentEvent().getPosX(), rotateFingerCursor.getCurrentEvent().getPosY()),
  474. dragPlaneNormal,
  475. rotateFingerStart.getCopy());*/
  476. //Update the field
  477. //if (newRotateFingerPos != null){
  478. float x = rotateCursorVectorLast.x - rotateCursor.getCurrentEvtPosX();
  479. float y = rotateCursorVectorLast.y - rotateCursor.getCurrentEvtPosY();
  480. rotateCursorVectorLast.x = rotateCursor.getCurrentEvtPosX();
  481. rotateCursorVectorLast.y = rotateCursor.getCurrentEvtPosY();
  482. Vector3D rotateLengthVec = new Vector3D(x,y,0.0f);
  483. this.rotateLineLength = rotateLengthVec.length();
  484. Vector3D rotateVector = new Vector3D(rotateCursor.getCurrentEvtPosX(),rotateCursor.getCurrentEvtPosY(),0.0f);
  485. //System.out.println("rotate " + directionFinder.dot(rotateLengthVec));
  486. Vector3D finder = this.getRotationAxis().getAdded(rotateVector);
  487. float dotRight = directionFinderRight.dot(rotateLengthVec);
  488. float deg = this.getRotationAxis().dot(finder)/(finder.length()*this.getRotationAxis().length());
  489. /*if(this.getRotationAxis().normalizeLocal().x>rotateVector.normalizeLocal().x)
  490. {
  491. dotRight = -dot;
  492. }*/
  493. if(this.getRotationAxis().x>0.0f)
  494. {
  495. dotRight = -dotRight;
  496. }
  497. /*if(directionFinder.dot(rotateLengthVec)>0.0f&&rotateLengthVec.x>yAxis.x)
  498. {
  499. setRotationDirection((short)-1);
  500. }else if(directionFinder.dot(rotateLengthVec)>0.0f&&rotateLengthVec.x<yAxis.x)
  501. {
  502. setRotationDirection((short)1);
  503. }else if(directionFinder.dot(rotateLengthVec)<0.0f&&rotateLengthVec.x>yAxis.x)
  504. {
  505. setRotationDirection((short)1);
  506. }else if(directionFinder.dot(rotateLengthVec)<0.0f&&rotateLengthVec.x<yAxis.x)
  507. {
  508. setRotationDirection((short)-1);
  509. }*/
  510. /* if(directionFinder.dot(rotateLengthVec)>0.0f)
  511. {
  512. setRotationDirection((short)-1);
  513. }else
  514. {
  515. setRotationDirection((short)1);
  516. }*/
  517. /*if(Math.toDegrees(deg)>0.0d)
  518. {
  519. setRotationDirection((short)-1);
  520. }else
  521. {
  522. setRotationDirection((short)1);
  523. }*/
  524. if(dotRight>0.0d)
  525. {
  526. setRotationDirection((short)-1);
  527. }else
  528. {
  529. setRotationDirection((short)1);
  530. }
  531. //System.out.println("Rotate legnth" + rotateLineLength + " dir " + rotationDirection);
  532. /*}else{
  533. logger.error(getName() + " new newRotateFinger Pos = null at update");
  534. }*/
  535. }
  536. /**
  537. * Update pin finger.
  538. */
  539. private void updatePinFinger(){
  540. Vector3D newPinFingerPos = getPlaneIntersection(pApplet, dragPlaneNormal, pinFingerStart.getCopy(), pinFingerCursor);
  541. if (newPinFingerPos != null){
  542. newPinFingerPos = projectPointToNearPlane(newPinFingerPos);
  543. this.pinFingerNew = newPinFingerPos;
  544. this.setRotationAxis(this.getRotationAxis(newPinFingerPos, this.pinFingerSecondNew));
  545. //Vector3D middlePoint = getMiddlePointBetweenPinFingers();
  546. //this.rotationPoint = targetComp.getCenterPointGlobal();
  547. }else{
  548. // do nothing
  549. }
  550. }
  551. private void updatePinFingerSecond()
  552. {
  553. Vector3D newPinFingerPos = getPlaneIntersection(pApplet, dragPlaneNormal, pinFingerSecondStart.getCopy(), pinFingerSecondCursor);
  554. if(newPinFingerPos != null)
  555. {
  556. newPinFingerPos = projectPointToNearPlane(newPinFingerPos);
  557. this.pinFingerSecondNew = newPinFingerPos;
  558. this.setRotationAxis(this.getRotationAxis(newPinFingerPos, this.pinFingerNew));
  559. //Vector3D middlePoint = getMiddlePointBetweenPinFingers();
  560. //this.rotationPoint = targetComp.getCenterPointGlobal();
  561. }else{
  562. // do nothing
  563. }
  564. }
  565. //if obj is drag enabled, und not scalable! send middlepoint delta f???r translate,
  566. /**
  567. * Gets the updated middle finger pos delta.
  568. *
  569. * @return the updated middle finger pos delta
  570. */
  571. public Vector3D getUpdatedMiddleFingerPosDelta(){
  572. newFingerMiddlePos = getMiddlePointBetweenFingers();
  573. Vector3D returnVect = newFingerMiddlePos.getSubtracted(oldFingerMiddlePos);
  574. this.oldFingerMiddlePos = newFingerMiddlePos;
  575. return returnVect;
  576. }
  577. /**
  578. * Gets the middle point between fingers.
  579. *
  580. * @return the middle point between fingers
  581. */
  582. public Vector3D getMiddlePointBetweenFingers(){
  583. Vector3D bla = rotateFingerNew.getSubtracted(pinFingerNew); //= Richtungsvektor vom 1. zum 2. finger
  584. bla.scaleLocal(0.5f); //take the half
  585. return (new Vector3D(pinFingerNew.getX() + bla.getX(), pinFingerNew.getY() + bla.getY(), pinFingerNew.getZ() + bla.getZ()));
  586. }
  587. /**
  588. * Gets the middle point between the two pin fingers()
  589. *
  590. * @return the middle point between both
  591. */
  592. public Vector3D getMiddlePointBetweenPinFingers()
  593. {
  594. Vector3D midPoint = pinFingerNew.getSubtracted(pinFingerSecondNew);
  595. midPoint.scaleLocal(0.5f);
  596. return (new Vector3D(pinFingerSecondNew.getX() + midPoint.getX(), pinFingerSecondNew.getY() + midPoint.getY(), pinFingerSecondNew.getZ() + midPoint.getZ()));
  597. }
  598. /**
  599. * Gets the pin finger translation vect.
  600. *
  601. * @return the pin finger translation vect
  602. */
  603. public Vector3D getPinFingerTranslationVect() {
  604. return pinFingerTranslationVect;
  605. }
  606. /**
  607. * Gets the pin finger start.
  608. *
  609. * @return the pin finger start
  610. */
  611. public Vector3D getPinFingerStart() {
  612. return pinFingerStart;
  613. }
  614. /**
  615. * Gets the pin finger start.
  616. *
  617. * @return the pin finger start
  618. */
  619. public Vector3D getPinFingerSecondStart() {
  620. return pinFingerSecondStart;
  621. }
  622. /**
  623. * Gets the rotate finger start.
  624. *
  625. * @return the rotate finger start
  626. */
  627. public Vector3D getRotateFingerStart() {
  628. return rotateFingerStart;
  629. }
  630. /**
  631. * Gets the rotation point.
  632. *
  633. * @return the rotation point
  634. */
  635. public Vector3D getRotationPoint() {
  636. return rotationPoint;
  637. }
  638. /**
  639. * Gets the pin finger cursor.
  640. *
  641. * @return the pin finger cursor
  642. */
  643. public InputCursor getPinFingerCursor() {
  644. return pinFingerCursor;
  645. }
  646. /**
  647. * Gets the pin finger cursor.
  648. *
  649. * @return the pin finger cursor
  650. */
  651. public InputCursor getPinFingerSecondCursor() {
  652. return pinFingerSecondCursor;
  653. }
  654. /**
  655. * Gets the rotate finger cursor.
  656. *
  657. * @return the rotate finger cursor
  658. */
  659. public InputCursor getRotateFingerCursor() {
  660. return rotateFingerCursor;
  661. }
  662. public boolean isGestureAborted() {
  663. return gestureAborted;
  664. }
  665. public float percentageX()
  666. {
  667. return this.percentageX;
  668. }
  669. public float percentageY()
  670. {
  671. return this.percentageY;
  672. }
  673. public float percentageZ()
  674. {
  675. return this.percentageZ;
  676. }
  677. public float getRotateLineLength()
  678. {
  679. return this.rotateLineLength;
  680. }
  681. public float getRotationDegreesX()
  682. {
  683. return this.degreesPerLengthUnit*percentageX*rotateLineLength;
  684. }
  685. public float getRotationDegreesY()
  686. {
  687. return this.degreesPerLengthUnit*percentageY*rotateLineLength;
  688. }
  689. public float getRotationDegreesZ()
  690. {
  691. return this.degreesPerLengthUnit*percentageZ*rotateLineLength;
  692. }
  693. public void setRotationDirection(short rotationDirection) {
  694. this.rotationDirection = rotationDirection;
  695. }
  696. public short getRotationDirection() {
  697. return rotationDirection;
  698. }
  699. public void setRotationAxis(Vector3D rotationAxis) {
  700. this.rotationAxis = rotationAxis;
  701. }
  702. public Vector3D getRotationAxis() {
  703. return rotationAxis;
  704. }
  705. }
  706. public String getName() {
  707. return "Rotate3DProcessor";
  708. }
  709. }