Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
1ad857b5
Commit
1ad857b5
authored
Jul 14, 2015
by
Chinmay Garde
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow explicitly setting tolerances on simulations
parent
1f1cd6c2
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
56 additions
and
17 deletions
+56
-17
newton.dart
packages/newton/lib/newton.dart
+1
-0
friction_simulation.dart
packages/newton/lib/src/friction_simulation.dart
+1
-1
scroll_simulation.dart
packages/newton/lib/src/scroll_simulation.dart
+7
-5
simulation.dart
packages/newton/lib/src/simulation.dart
+1
-2
simulation_group.dart
packages/newton/lib/src/simulation_group.dart
+12
-3
spring_simulation.dart
packages/newton/lib/src/spring_simulation.dart
+2
-1
tolerance.dart
packages/newton/lib/src/tolerance.dart
+17
-0
utils.dart
packages/newton/lib/src/utils.dart
+3
-5
newton_test.dart
packages/newton/test/newton_test.dart
+12
-0
No files found.
packages/newton/lib/newton.dart
View file @
1ad857b5
...
...
@@ -8,6 +8,7 @@ import 'dart:math' as math;
part
'src/simulation.dart'
;
part
'src/simulation_group.dart'
;
part
'src/tolerance.dart'
;
part
'src/utils.dart'
;
part
'src/friction_simulation.dart'
;
...
...
packages/newton/lib/src/friction_simulation.dart
View file @
1ad857b5
...
...
@@ -22,5 +22,5 @@ class FrictionSimulation extends Simulation {
double
dx
(
double
time
)
=>
_v
*
math
.
pow
(
_drag
,
time
);
@override
bool
isDone
(
double
time
)
=>
dx
(
time
).
abs
()
<
1.0
;
bool
isDone
(
double
time
)
=>
dx
(
time
).
abs
()
<
this
.
tolerance
.
velocity
;
}
packages/newton/lib/src/scroll_simulation.dart
View file @
1ad857b5
...
...
@@ -27,7 +27,7 @@ class ScrollSimulation extends SimulationGroup {
}
@override
void
step
(
double
time
)
=>
_chooseSimulation
(
bool
step
(
double
time
)
=>
_chooseSimulation
(
_currentSimulation
.
x
(
time
-
_offset
),
_currentSimulation
.
dx
(
time
-
_offset
),
time
);
...
...
@@ -37,7 +37,7 @@ class ScrollSimulation extends SimulationGroup {
@override
double
get
currentIntervalOffset
=>
_offset
;
void
_chooseSimulation
(
bool
_chooseSimulation
(
double
position
,
double
velocity
,
double
intervalOffset
)
{
/// This simulation can only step forward.
...
...
@@ -47,19 +47,21 @@ class ScrollSimulation extends SimulationGroup {
_offset
=
intervalOffset
;
_currentSimulation
=
new
SpringSimulation
(
_springDesc
,
position
,
_trailingExtent
,
velocity
);
return
;
return
true
;
}
else
if
(
position
<
_leadingExtent
)
{
_isSpringing
=
true
;
_offset
=
intervalOffset
;
_currentSimulation
=
new
SpringSimulation
(
_springDesc
,
position
,
_leadingExtent
,
velocity
);
return
;
return
true
;
}
}
if
(
_currentSimulation
==
null
)
{
_currentSimulation
=
new
FrictionSimulation
(
_drag
,
position
,
velocity
);
return
;
return
true
;
}
return
false
;
}
}
packages/newton/lib/src/simulation.dart
View file @
1ad857b5
...
...
@@ -15,9 +15,8 @@ abstract class Simulatable {
/// The base class for all simulations. The user is meant to instantiate an
/// instance of a simulation and query the same for the position and velocity
/// of the body at a given interval.
///
/// Note: All operations on subclasses of Simulation are idempotent.
abstract
class
Simulation
implements
Simulatable
{
Tolerance
tolerance
=
toleranceDefault
;
/// Returns if the simulation is done at a given time
bool
isDone
(
double
time
);
...
...
packages/newton/lib/src/simulation_group.dart
View file @
1ad857b5
...
...
@@ -19,7 +19,8 @@ abstract class SimulationGroup extends Simulation {
/// Called when a significant change in the interval is detected. Subclasses
/// must decide if the the current simulation must be switched (or updated).
void
step
(
double
time
);
/// The result is whether the simulation was switched in this step.
bool
step
(
double
time
);
double
x
(
double
time
)
{
_stepIfNecessary
(
time
);
...
...
@@ -31,6 +32,12 @@ abstract class SimulationGroup extends Simulation {
return
currentSimulation
.
dx
(
time
-
currentIntervalOffset
);
}
@override
void
set
tolerance
(
Tolerance
t
)
{
this
.
currentSimulation
.
tolerance
=
t
;
super
.
tolerance
=
t
;
}
@override
bool
isDone
(
double
time
)
{
_stepIfNecessary
(
time
);
...
...
@@ -39,11 +46,13 @@ abstract class SimulationGroup extends Simulation {
double
_lastStep
=
-
1.0
;
void
_stepIfNecessary
(
double
time
)
{
if
(
_nearEqual
(
_lastStep
,
time
))
{
if
(
_nearEqual
(
_lastStep
,
time
,
toleranceDefault
.
time
))
{
return
;
}
_lastStep
=
time
;
step
(
time
);
if
(
step
(
time
))
{
this
.
currentSimulation
.
tolerance
=
this
.
tolerance
;
}
}
}
packages/newton/lib/src/spring_simulation.dart
View file @
1ad857b5
...
...
@@ -58,5 +58,6 @@ class SpringSimulation extends Simulation {
@override
bool
isDone
(
double
time
)
=>
_nearEqual
(
x
(
time
),
_endPosition
)
&&
_nearZero
(
dx
(
time
));
_nearEqual
(
x
(
time
),
_endPosition
,
this
.
tolerance
.
distance
)
&&
_nearZero
(
dx
(
time
),
this
.
tolerance
.
velocity
);
}
packages/newton/lib/src/tolerance.dart
0 → 100644
View file @
1ad857b5
// Copyright (c) 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of
newton
;
class
Tolerance
{
final
double
distance
;
final
double
time
;
final
double
velocity
;
const
Tolerance
({
this
.
distance
:
epsilonDefault
,
this
.
time
:
epsilonDefault
,
this
.
velocity
:
epsilonDefault
});
}
const
double
epsilonDefault
=
1
e
-
3
;
const
Tolerance
toleranceDefault
=
const
Tolerance
();
packages/newton/lib/src/utils.dart
View file @
1ad857b5
...
...
@@ -4,9 +4,7 @@
part of
newton
;
const
double
_simulationEpsilon
=
0.2
;
bool
_nearEqual
(
double
a
,
double
b
,
double
epsilon
)
=>
(
a
>
(
b
-
epsilon
))
&&
(
a
<
(
b
+
epsilon
));
bool
_nearEqual
(
double
a
,
double
b
)
=>
(
a
>
(
b
-
_simulationEpsilon
))
&&
(
a
<
(
b
+
_simulationEpsilon
));
bool
_nearZero
(
double
a
)
=>
_nearEqual
(
a
,
0.0
);
bool
_nearZero
(
double
a
,
double
epsilon
)
=>
_nearEqual
(
a
,
0.0
,
epsilon
);
packages/newton/test/newton_test.dart
View file @
1ad857b5
...
...
@@ -12,6 +12,8 @@ void main() {
test
(
'test_friction'
,
()
{
var
friction
=
new
FrictionSimulation
(
0.3
,
100.0
,
400.0
);
friction
.
tolerance
=
const
Tolerance
(
velocity:
1.0
);
expect
(
friction
.
isDone
(
0.0
),
false
);
expect
(
friction
.
x
(
0.0
),
100
);
expect
(
friction
.
dx
(
0.0
),
400.0
);
...
...
@@ -84,6 +86,9 @@ void main() {
test
(
'crit_spring'
,
()
{
var
crit
=
new
SpringSimulation
(
new
SpringDescription
.
withDampingRatio
(
mass:
1.0
,
springConstant:
100.0
,
ratio:
1.0
),
0.0
,
500.0
,
0.0
);
crit
.
tolerance
=
const
Tolerance
(
distance:
0.01
,
velocity:
0.01
);
expect
(
crit
.
type
,
SpringType
.
criticallyDamped
);
expect
(
crit
.
isDone
(
0.0
),
false
);
...
...
@@ -106,6 +111,9 @@ void main() {
test
(
'overdamped_spring'
,
()
{
var
over
=
new
SpringSimulation
(
new
SpringDescription
.
withDampingRatio
(
mass:
1.0
,
springConstant:
100.0
,
ratio:
1.25
),
0.0
,
500.0
,
0.0
);
over
.
tolerance
=
const
Tolerance
(
distance:
0.01
,
velocity:
0.01
);
expect
(
over
.
type
,
SpringType
.
overDamped
);
expect
(
over
.
isDone
(
0.0
),
false
);
...
...
@@ -145,6 +153,8 @@ void main() {
var
scroll
=
new
ScrollSimulation
(
100.0
,
800.0
,
0.0
,
300.0
,
spring
,
0.3
);
scroll
.
tolerance
=
const
Tolerance
(
velocity:
0.01
,
distance:
0.01
);
expect
(
scroll
.
isDone
(
0.0
),
false
);
expect
(
scroll
.
isDone
(
3.5
),
true
);
...
...
@@ -159,6 +169,8 @@ void main() {
var
scroll
=
new
ScrollSimulation
(
100.0
,
400.0
,
0.0
,
double
.
INFINITY
,
spring
,
0.3
);
scroll
.
tolerance
=
const
Tolerance
(
velocity:
1.0
);
expect
(
scroll
.
isDone
(
0.0
),
false
);
expect
(
scroll
.
x
(
0.0
),
100
);
expect
(
scroll
.
dx
(
0.0
),
400.0
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment