| 46 |
private PerspectiveCamera _camera; |
private PerspectiveCamera _camera; |
| 47 |
private Point3D _cameraLookPoint; |
private Point3D _cameraLookPoint; |
| 48 |
private RotateTransform3D _rotationTransform = new RotateTransform3D(); |
private RotateTransform3D _rotationTransform = new RotateTransform3D(); |
|
//ModelVisual3D clickedVisual; |
|
| 49 |
ModelVisual3D visul3d; |
ModelVisual3D visul3d; |
| 50 |
|
|
| 51 |
//rotate and pan object clicked |
//rotate and pan object clicked |
| 52 |
private TranslateTransform3D _clickedTranslate = new TranslateTransform3D(); |
private TranslateTransform3D _clickedTranslate = new TranslateTransform3D(); |
|
private TranslateTransform3D _clickedTranslateOriginal;// = new TranslateTransform3D(); |
|
| 53 |
private RotateTransform3D _clickedRotate = new RotateTransform3D(); |
private RotateTransform3D _clickedRotate = new RotateTransform3D(); |
| 54 |
private AxisAngleRotation3D _clickedAxisAngleRotate = new AxisAngleRotation3D(); |
private AxisAngleRotation3D _clickedAxisAngleRotate = new AxisAngleRotation3D(); |
| 55 |
Point3D _clickedCenterPosition; |
Point3D _clickedCenterPosition; |
| 66 |
_transform = new Transform3DGroup(); |
_transform = new Transform3DGroup(); |
| 67 |
// _transform.Children.Add(_translate); |
// _transform.Children.Add(_translate); |
| 68 |
_transform.Children.Add(_rotationTransform); |
_transform.Children.Add(_rotationTransform); |
|
// _transform.Children.Add(_scale); |
|
| 69 |
|
|
| 70 |
_cameraLookDirection = _camera.LookDirection; |
_cameraLookDirection = _camera.LookDirection; |
| 71 |
|
|
| 80 |
get { return _transform; } |
get { return _transform; } |
| 81 |
} |
} |
| 82 |
|
|
| 83 |
|
[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SetCursorPos")] |
| 84 |
|
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] |
| 85 |
|
public static extern bool SetCursorPos(int X, int Y); |
| 86 |
|
|
| 87 |
|
|
| 88 |
#region Event Handling |
#region Event Handling |
| 89 |
|
|
| 90 |
/// <summary> |
/// <summary> |
| 103 |
_eventSource.MouseMove -= this.OnMouseMove; |
_eventSource.MouseMove -= this.OnMouseMove; |
| 104 |
_eventSource.MouseLeftButtonDown -= _eventSource_MouseLeftButtonDown; |
_eventSource.MouseLeftButtonDown -= _eventSource_MouseLeftButtonDown; |
| 105 |
_eventSource.MouseRightButtonDown -= _eventSource_MouseRightButtonDown; |
_eventSource.MouseRightButtonDown -= _eventSource_MouseRightButtonDown; |
|
|
|
|
|
|
|
|
|
| 106 |
} |
} |
| 107 |
_eventSource = value; |
_eventSource = value; |
| 108 |
_eventSource.MouseWheel += _eventSource_MouseWheel; |
_eventSource.MouseWheel += _eventSource_MouseWheel; |
| 110 |
_eventSource.MouseRightButtonDown += _eventSource_MouseRightButtonDown; |
_eventSource.MouseRightButtonDown += _eventSource_MouseRightButtonDown; |
| 111 |
_eventSource.MouseUp += this.OnMouseUp; |
_eventSource.MouseUp += this.OnMouseUp; |
| 112 |
_eventSource.MouseMove += this.OnMouseMove; |
_eventSource.MouseMove += this.OnMouseMove; |
|
|
|
|
|
|
| 113 |
} |
} |
| 114 |
} |
} |
| 115 |
|
|
| 116 |
|
|
|
|
|
| 117 |
void _eventSource_MouseRightButtonDown(object sender, MouseButtonEventArgs e) |
void _eventSource_MouseRightButtonDown(object sender, MouseButtonEventArgs e) |
| 118 |
{ |
{ |
| 119 |
Mouse.Capture(EventSource, CaptureMode.Element); |
Mouse.Capture(_eventSource, CaptureMode.Element); |
| 120 |
|
|
| 121 |
Point pclick = e.GetPosition(EventSource); |
Point pclick = e.GetPosition(_eventSource); |
| 122 |
HitTestResult hitresult = VisualTreeHelper.HitTest(EventSource, pclick); |
HitTestResult hitresult = VisualTreeHelper.HitTest(EventSource, pclick); |
| 123 |
RayMeshGeometry3DHitTestResult result3d = hitresult as RayMeshGeometry3DHitTestResult; |
RayMeshGeometry3DHitTestResult result3d = hitresult as RayMeshGeometry3DHitTestResult; |
| 124 |
if (result3d == null) |
if (result3d == null) |
| 135 |
{ |
{ |
| 136 |
if (tran is RotateTransform3D) |
if (tran is RotateTransform3D) |
| 137 |
{ |
{ |
| 138 |
_clickedRotate = new RotateTransform3D(); |
|
| 139 |
_clickedRotate = tran as RotateTransform3D; |
_clickedRotate = tran as RotateTransform3D; |
| 140 |
if (_clickedRotate != null) |
if (_clickedRotate != null) |
| 141 |
{ |
{ |
| 149 |
} |
} |
| 150 |
else |
else |
| 151 |
return; |
return; |
|
//for pan the object |
|
|
//_clickedTranslateOriginal = _clickedTranslate.Clone(); |
|
| 152 |
|
|
| 153 |
//for ALT + MOUSE + ROTATE |
//for ALT + MOUSE + ROTATE |
| 154 |
Petzold.Media3D.LineRange line; |
VisualTreeHelper.HitTest(_eventSource, null, HitTestCallback, new PointHitTestParameters(pclick)); |
|
Petzold.Media3D.ViewportInfo.Point2DtoPoint3D(_eventSource as Viewport3D, pclick, out line); |
|
|
_cameraLookPoint = line.PointFromY(visul3d.Content.Bounds.Y); |
|
| 155 |
|
|
| 156 |
_rotationTransform.CenterX = _cameraLookPoint.X; |
_rotationTransform.CenterX = _cameraLookPoint.X; |
| 157 |
_rotationTransform.CenterY = _cameraLookPoint.Y; |
_rotationTransform.CenterY = _cameraLookPoint.Y; |
| 166 |
e.Handled = true; |
e.Handled = true; |
| 167 |
} |
} |
| 168 |
|
|
| 169 |
|
Point originalMouseClickPos = new Point(); |
| 170 |
void _eventSource_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) |
void _eventSource_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) |
| 171 |
{ |
{ |
| 172 |
Mouse.Capture(EventSource, CaptureMode.Element); |
Mouse.Capture(EventSource, CaptureMode.Element); |
| 173 |
|
|
| 174 |
Point pclick = e.GetPosition(EventSource); |
Point pclick = e.GetPosition(EventSource); |
| 175 |
|
originalMouseClickPos = _eventSource.PointToScreen(pclick); |
| 176 |
|
|
| 177 |
HitTestResult hitresult = VisualTreeHelper.HitTest(EventSource, pclick); |
HitTestResult hitresult = VisualTreeHelper.HitTest(EventSource, pclick); |
| 178 |
RayMeshGeometry3DHitTestResult result3d = hitresult as RayMeshGeometry3DHitTestResult; |
RayMeshGeometry3DHitTestResult result3d = hitresult as RayMeshGeometry3DHitTestResult; |
| 179 |
if (result3d == null) |
if (result3d == null) |
| 183 |
if (visul3d == null) |
if (visul3d == null) |
| 184 |
return; |
return; |
| 185 |
|
|
|
|
|
| 186 |
_clickedGroup = visul3d.Transform as Transform3DGroup; |
_clickedGroup = visul3d.Transform as Transform3DGroup; |
| 187 |
if (_clickedGroup != null) |
if (_clickedGroup != null) |
| 188 |
{ |
{ |
| 191 |
{ |
{ |
| 192 |
if (tran is RotateTransform3D) |
if (tran is RotateTransform3D) |
| 193 |
{ |
{ |
|
// _clickedRotate = new RotateTransform3D(); |
|
| 194 |
_clickedRotate = tran as RotateTransform3D; |
_clickedRotate = tran as RotateTransform3D; |
| 195 |
if (_clickedRotate != null) |
if (_clickedRotate != null) |
| 196 |
{ |
{ |
| 210 |
else |
else |
| 211 |
return; |
return; |
| 212 |
|
|
|
//for pan the object |
|
|
//_clickedTranslateOriginal = _clickedTranslate.Clone(); |
|
|
|
|
| 213 |
//for ALT + MOUSE + ROTATE |
//for ALT + MOUSE + ROTATE |
| 214 |
Petzold.Media3D.LineRange line; |
VisualTreeHelper.HitTest(_eventSource, null, HitTestCallback, new PointHitTestParameters(pclick)); |
|
Petzold.Media3D.ViewportInfo.Point2DtoPoint3D(_eventSource as Viewport3D, pclick, out line); |
|
|
_cameraLookPoint = line.PointFromY(visul3d.Content.Bounds.Y); |
|
|
|
|
|
//for test |
|
|
|
|
|
//VisualTreeHelper.HitTest(_eventSource, null, HitTestCallback, new PointHitTestParameters(pclick)); |
|
|
//end of test |
|
| 215 |
|
|
| 216 |
_rotationTransform.CenterX = _cameraLookPoint.X; |
_rotationTransform.CenterX = _cameraLookPoint.X; |
| 217 |
_rotationTransform.CenterY = _cameraLookPoint.Y; |
_rotationTransform.CenterY = _cameraLookPoint.Y; |
| 241 |
{ |
{ |
| 242 |
Mouse.Capture(EventSource, CaptureMode.None); |
Mouse.Capture(EventSource, CaptureMode.None); |
| 243 |
isTracking = false; |
isTracking = false; |
| 244 |
|
//set the mouse to the original position when clicked the object. |
| 245 |
|
SetCursorPos((int)originalMouseClickPos.X, (int)originalMouseClickPos.Y); |
| 246 |
|
|
| 247 |
_eventSource.ReleaseMouseCapture(); |
_eventSource.ReleaseMouseCapture(); |
| 248 |
_eventSource.Cursor = Cursors.Arrow; |
_eventSource.Cursor = Cursors.Arrow; |
| 271 |
} |
} |
| 272 |
else if (e.RightButton == MouseButtonState.Pressed && Keyboard.IsKeyDown(Key.A)) |
else if (e.RightButton == MouseButtonState.Pressed && Keyboard.IsKeyDown(Key.A)) |
| 273 |
{ |
{ |
|
|
|
| 274 |
if (isTracking) |
if (isTracking) |
| 275 |
{ |
{ |
| 276 |
_eventSource.Cursor = Cursors.None; |
_eventSource.Cursor = Cursors.None; |
| 301 |
LookAround(currentPosition); |
LookAround(currentPosition); |
| 302 |
} |
} |
| 303 |
|
|
| 304 |
|
|
| 305 |
_previousPosition2D = currentPosition; |
_previousPosition2D = currentPosition; |
| 306 |
|
|
| 307 |
} |
} |
| 308 |
|
|
| 309 |
private void LookAround(Point currentPosition) |
private void LookAround(Point currentPosition) |
| 344 |
|
|
| 345 |
void _eventSource_MouseWheel(object sender, MouseWheelEventArgs e) |
void _eventSource_MouseWheel(object sender, MouseWheelEventArgs e) |
| 346 |
{ |
{ |
| 347 |
Point3D PreCameraPos = _camera.Position; |
double scale = -e.Delta / 1200.000; |
|
|
|
|
double scale = e.Delta / 1200.000; |
|
|
|
|
|
_camera.Position = new Point3D(_camera.Position.X + scale * _cameraLookDirection.X, |
|
|
_camera.Position.Y + scale * _cameraLookDirection.Y, |
|
|
_camera.Position.Z + scale * _cameraLookDirection.Z); |
|
| 348 |
|
|
| 349 |
|
AdjustCameraPosition(_camera, _cameraLookDirection, scale); |
|
double distance = Point3D.Subtract(_cameraLookPoint, _camera.Position).Length; |
|
|
|
|
|
if (distance > 524 || distance <= 5) |
|
|
{ |
|
|
_camera.Position = PreCameraPos; |
|
|
} |
|
|
|
|
|
_camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
|
| 350 |
} |
} |
| 351 |
|
|
| 352 |
private void RotateObject(Point currentPosition) |
private void RotateObject(Point currentPosition) |
| 403 |
scalex = Math.Abs( scalex); |
scalex = Math.Abs( scalex); |
| 404 |
|
|
| 405 |
|
|
|
|
|
| 406 |
Vector3D vector3D = GetTranslationVector3D(visul3d, _previousPosition2D, currentPosition); |
Vector3D vector3D = GetTranslationVector3D(visul3d, _previousPosition2D, currentPosition); |
| 407 |
|
|
| 408 |
_clickedTranslate.OffsetX += scalex * vector3D.X;// _clickedTranslateOriginal.OffsetX + vector3D.X; |
_clickedTranslate.OffsetX += scalex * vector3D.X;// _clickedTranslateOriginal.OffsetX + vector3D.X; |
| 444 |
|
|
| 445 |
private void Focus(Point currentPosition) |
private void Focus(Point currentPosition) |
| 446 |
{ |
{ |
|
|
|
| 447 |
//zoom |
//zoom |
| 448 |
double yDelta = currentPosition.Y - _previousPosition2D.Y; |
double yDelta = currentPosition.Y - _previousPosition2D.Y; |
| 449 |
double scale = yDelta / 80.00000; |
double scale = yDelta / 100.000000; |
| 450 |
|
AdjustCameraPosition(_camera,_cameraLookDirection,scale); |
|
|
|
|
Point3D cameraPos = new Point3D(_camera.Position.X - scale * _cameraLookDirection.X, |
|
|
_camera.Position.Y - scale * _cameraLookDirection.Y, |
|
|
_camera.Position.Z - scale * _cameraLookDirection.Z); |
|
|
|
|
|
//Maybe this part need replaced by collision detection method |
|
|
double distance = Point3D.Subtract(_cameraLookPoint, cameraPos).Length; |
|
|
|
|
|
if (distance > 524 || distance <= 1.000000) |
|
|
{ |
|
|
} |
|
|
else |
|
|
_camera.Position = cameraPos; |
|
| 451 |
|
|
| 452 |
//rotate the camera |
//rotate the camera |
| 453 |
double axisAngle = currentPosition.X - _previousPosition2D.X; |
double axisAngle = currentPosition.X - _previousPosition2D.X; |
|
if (axisAngle > 360 || axisAngle < -360) |
|
|
return; |
|
|
//if (_rotation.Angle - axisAngle <= -360) |
|
|
// _rotation.Angle = 0; |
|
| 454 |
_rotation.Axis = new Vector3D(0, 0, 1); |
_rotation.Axis = new Vector3D(0, 0, 1); |
| 455 |
_rotation.Angle -= axisAngle; |
_rotation.Angle -= axisAngle; |
| 456 |
|
|
| 457 |
_camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
_camera.LookDirection = Point3D.Subtract(_cameraLookPoint, _camera.Position); |
| 458 |
|
} |
| 459 |
|
|
| 460 |
//_translate.OffsetX -= scale * _cameraLookDirection.X;// _camera.LookDirection.X; |
private void AdjustCameraPosition(PerspectiveCamera camera,Vector3D lookDirection, double scale ) |
| 461 |
//_translate.OffsetY -= scale * _cameraLookDirection.Y;//_camera.LookDirection.Y; |
{ |
| 462 |
//_translate.OffsetZ -= scale * _cameraLookDirection.Z;//_camera.LookDirection.Z; |
Point3D cameraPos = new Point3D(camera.Position.X - scale * lookDirection.X, |
| 463 |
|
camera.Position.Y - scale * lookDirection.Y, |
| 464 |
|
camera.Position.Z - scale * lookDirection.Z); |
| 465 |
|
|
| 466 |
|
//too fast |
| 467 |
|
if (Point3D.Subtract(cameraPos, camera.Position).Length > 1.000000000) |
| 468 |
|
cameraPos = new Point3D(camera.Position.X - scale / 10 * lookDirection.X, |
| 469 |
|
camera.Position.Y - scale / 10 * lookDirection.Y, |
| 470 |
|
camera.Position.Z - scale / 10 * lookDirection.Z); |
| 471 |
|
|
| 472 |
} |
double distance = Point3D.Subtract(_cameraLookPoint, cameraPos).Length; |
| 473 |
|
|
| 474 |
|
if (distance > 524 || distance <= 1.000000) |
| 475 |
|
{ |
| 476 |
|
} |
| 477 |
|
else |
| 478 |
|
camera.Position = cameraPos; |
| 479 |
|
} |
| 480 |
|
//get the clicked 3d position |
| 481 |
public HitTestResultBehavior HitTestCallback(HitTestResult result) |
public HitTestResultBehavior HitTestCallback(HitTestResult result) |
| 482 |
{ |
{ |
| 483 |
if (result.VisualHit != null) |
if (result.VisualHit != null) |
| 487 |
{ |
{ |
| 488 |
RayMeshGeometry3DHitTestResult t = r as RayMeshGeometry3DHitTestResult; |
RayMeshGeometry3DHitTestResult t = r as RayMeshGeometry3DHitTestResult; |
| 489 |
_cameraLookPoint = t.PointHit; |
_cameraLookPoint = t.PointHit; |
| 490 |
|
return HitTestResultBehavior.Stop; |
| 491 |
} |
} |
| 492 |
} |
} |
| 493 |
return HitTestResultBehavior.Continue; |
return HitTestResultBehavior.Continue; |